From dd97e4191bd29ffaa363f2fe785f4bcd662f046d Mon Sep 17 00:00:00 2001 From: Aaron Griffin Date: Tue, 27 May 2008 17:27:44 -0400 Subject: [PATCH] Properly specify arguments passed to init Only arguments after the 'init=' string should be passed to the init binary. Previous versions of this code would fail on init=/bin/sh due to the fact that all command line arguments were passed as sh arguments. Now we only pass cmdline args specified after the init= param. Signed-off-by: Aaron Griffin --- usr/kinit/kinit.c | 26 +++++++++++++++----------- 1 files changed, 15 insertions(+), 11 deletions(-) diff --git a/usr/kinit/kinit.c b/usr/kinit/kinit.c index 7c33718..d40391f 100644 --- a/usr/kinit/kinit.c +++ b/usr/kinit/kinit.c @@ -191,29 +191,23 @@ static const char *find_init(const char *root, const char *user) /* This is the argc and argv we pass to init */ const char *init_path; -int init_argc; -char **init_argv; extern ssize_t readfile(const char *, char **); int main(int argc, char *argv[]) { - char **cmdv, **args; + char **init_argv, **cmdv, **args; char *cmdlines[3]; int i; const char *errmsg; int ret = 0; - int cmdc; + int init_argc, cmdc; int fd; struct timeval now; gettimeofday(&now, NULL); srand48(now.tv_usec ^ (now.tv_sec << 24)); - /* Default parameters for anything init-like we execute */ - init_argc = argc; - init_argv = alloca((argc+1)*sizeof(char *)); - memcpy(init_argv, argv, (argc+1)*sizeof(char *)); if ((fd = open("/dev/console", O_RDWR)) != -1) { dup2(fd, STDIN_FILENO); @@ -298,15 +292,25 @@ int main(int argc, char *argv[]) mnt_sysfs = 0; } - init_path = find_init("/root", get_arg(cmdc, cmdv, "init=")); + /* Make sure we handle arguments to init= properly */ + for (i = argc-1; i > 0; i--) { + if (argv[i] && strncmp(argv[i], "init=", 5) == 0 && + (argv[i][5] != '\0')) { + init_argc = i; + init_argv = alloca((init_argc+1)*sizeof(char *)); + memcpy(init_argv, argv+i, (init_argc+1)*sizeof(char *)); + init_argv[0] += 5; /* Skip over 'init=' */ + break; + } + } + init_path = find_init("/root", init_argv[0]); + if (!init_path) { fprintf(stderr, "%s: init not found!\n", progname); ret = 2; goto bail; } - init_argv[0] = strrchr(init_path, '/') + 1; - errmsg = run_init("/root", "/dev/console", init_path, init_argv); /* If run_init returned, something went bad */ -- 1.5.5