aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSally, Gene <Gene.Sally@timesys.com>2008-10-29 09:54:17 -0400
committerSam Ravnborg <sam@ravnborg.org>2008-12-03 15:32:03 -0500
commit3b1ec9fb8197197d5e3bcca3a05e82d4f50f11bc (patch)
tree4581dc3493883f00abb0c3b61e3576edf1032197
parentefddd79512cc582675004bfdf7e66585198b38f9 (diff)
kbuild: gen_init_cpio expands shell variables in file names
Modify gen_init_cpio so that lines that specify files can contain what looks like a shell variable that's expanded during processing. For example: file /sbin/kinit ${RFS_BASE}/usr/src/klibc/kinit/kinit 0755 0 0 given RFS_BASE is "/some/directory" in the environment would be expanded to file /sbin/kinit /some/directory/usr/src/klibc/kinit/kinit 0755 0 0 If several environment variables appear in a line, they are all expanded with processing happening from left to right. Undefined variables expand to a null string. Syntax errors stop processing, letting the existing error handling show the user offending line. This patch helps embedded folks who frequently create several RFS directories and then switch between them as they're tuning an initramfs. Signed-off-by: gene.sally@timesys.com Signed-off-by: Sam Ravnborg <sam@ravnborg.org>
-rw-r--r--usr/gen_init_cpio.c28
1 files changed, 27 insertions, 1 deletions
diff --git a/usr/gen_init_cpio.c b/usr/gen_init_cpio.c
index 7abc07f0fcd2..f1d3fe34176a 100644
--- a/usr/gen_init_cpio.c
+++ b/usr/gen_init_cpio.c
@@ -370,6 +370,30 @@ error:
370 return rc; 370 return rc;
371} 371}
372 372
373static char *cpio_replace_env(char *new_location)
374{
375 char expanded[PATH_MAX + 1];
376 char env_var[PATH_MAX + 1];
377 char *start;
378 char *end;
379
380 for (start = NULL; (start = strstr(new_location, "${")); ) {
381 end = strchr(start, '}');
382 if (start < end) {
383 *env_var = *expanded = '\0';
384 strncat(env_var, start + 2, end - start - 2);
385 strncat(expanded, new_location, start - new_location);
386 strncat(expanded, getenv(env_var), PATH_MAX);
387 strncat(expanded, end + 1, PATH_MAX);
388 strncpy(new_location, expanded, PATH_MAX);
389 } else
390 break;
391 }
392
393 return new_location;
394}
395
396
373static int cpio_mkfile_line(const char *line) 397static int cpio_mkfile_line(const char *line)
374{ 398{
375 char name[PATH_MAX + 1]; 399 char name[PATH_MAX + 1];
@@ -415,7 +439,8 @@ static int cpio_mkfile_line(const char *line)
415 } else { 439 } else {
416 dname = name; 440 dname = name;
417 } 441 }
418 rc = cpio_mkfile(dname, location, mode, uid, gid, nlinks); 442 rc = cpio_mkfile(dname, cpio_replace_env(location),
443 mode, uid, gid, nlinks);
419 fail: 444 fail:
420 if (dname_len) free(dname); 445 if (dname_len) free(dname);
421 return rc; 446 return rc;
@@ -439,6 +464,7 @@ void usage(const char *prog)
439 "\n" 464 "\n"
440 "<name> name of the file/dir/nod/etc in the archive\n" 465 "<name> name of the file/dir/nod/etc in the archive\n"
441 "<location> location of the file in the current filesystem\n" 466 "<location> location of the file in the current filesystem\n"
467 " expands shell variables quoted with ${}\n"
442 "<target> link target\n" 468 "<target> link target\n"
443 "<mode> mode/permissions of the file\n" 469 "<mode> mode/permissions of the file\n"
444 "<uid> user id (0=root)\n" 470 "<uid> user id (0=root)\n"