aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEric Andersen <andersen@codepoet.org>2001-03-20 17:39:08 +0000
committerEric Andersen <andersen@codepoet.org>2001-03-20 17:39:08 +0000
commit7ef1a5beb27b26e5f6cecd632b299a6ab0424c67 (patch)
tree81d3dc95e85186a24420bc7f2c94193432157642
parent4ef37d0c176cde66c0365c4d1096bba0c57f486b (diff)
downloadbusybox-w32-7ef1a5beb27b26e5f6cecd632b299a6ab0424c67.tar.gz
busybox-w32-7ef1a5beb27b26e5f6cecd632b299a6ab0424c67.tar.bz2
busybox-w32-7ef1a5beb27b26e5f6cecd632b299a6ab0424c67.zip
Fix up command line munging in init. Postpone the askfirst thing till
a bit later in run().
-rw-r--r--init.c140
-rw-r--r--init/init.c140
2 files changed, 152 insertions, 128 deletions
diff --git a/init.c b/init.c
index 145452ff3..b77589385 100644
--- a/init.c
+++ b/init.c
@@ -188,6 +188,7 @@ static char console[32] = _PATH_CONSOLE;
188static void delete_initAction(initAction * action); 188static void delete_initAction(initAction * action);
189 189
190 190
191
191/* Print a message to the specified device. 192/* Print a message to the specified device.
192 * Device may be bitwise-or'd from LOG | CONSOLE */ 193 * Device may be bitwise-or'd from LOG | CONSOLE */
193static void message(int device, char *fmt, ...) 194static void message(int device, char *fmt, ...)
@@ -393,6 +394,23 @@ static void console_init()
393 } 394 }
394 message(LOG, "console=%s\n", console); 395 message(LOG, "console=%s\n", console);
395} 396}
397
398static void fixup_argv(int argc, char **argv, char *new_argv0)
399{
400 int len;
401 /* Fix up argv[0] to be certain we claim to be init */
402 len = strlen(argv[0]);
403 memset(argv[0], 0, len);
404 strncpy(argv[0], new_argv0, len);
405
406 /* Wipe argv[1]-argv[N] so they don't clutter the ps listing */
407 len = 1;
408 while (argc > len) {
409 memset(argv[len], 0, strlen(argv[len]));
410 len++;
411 }
412}
413
396 414
397static pid_t run(char *command, char *terminal, int get_enter) 415static pid_t run(char *command, char *terminal, int get_enter)
398{ 416{
@@ -402,6 +420,7 @@ static pid_t run(char *command, char *terminal, int get_enter)
402 char *tmpCmd, *s; 420 char *tmpCmd, *s;
403 char *cmd[255], *cmdpath; 421 char *cmd[255], *cmdpath;
404 char buf[255]; 422 char buf[255];
423 struct stat sb;
405 static const char press_enter[] = 424 static const char press_enter[] =
406 425
407#ifdef CUSTOMIZED_BANNER 426#ifdef CUSTOMIZED_BANNER
@@ -447,8 +466,7 @@ static pid_t run(char *command, char *terminal, int get_enter)
447 signal(SIGHUP, SIG_DFL); 466 signal(SIGHUP, SIG_DFL);
448 467
449 if ((fd = device_open(terminal, O_RDWR)) < 0) { 468 if ((fd = device_open(terminal, O_RDWR)) < 0) {
450 struct stat statBuf; 469 if (stat(terminal, &sb) != 0) {
451 if (stat(terminal, &statBuf) != 0) {
452 message(LOG | CONSOLE, "device '%s' does not exist.\n", 470 message(LOG | CONSOLE, "device '%s' does not exist.\n",
453 terminal); 471 terminal);
454 exit(1); 472 exit(1);
@@ -463,29 +481,6 @@ static pid_t run(char *command, char *terminal, int get_enter)
463 tcsetpgrp(0, getpgrp()); 481 tcsetpgrp(0, getpgrp());
464 set_term(0); 482 set_term(0);
465 483
466 if (get_enter == TRUE) {
467 /*
468 * Save memory by not exec-ing anything large (like a shell)
469 * before the user wants it. This is critical if swap is not
470 * enabled and the system has low memory. Generally this will
471 * be run on the second virtual console, and the first will
472 * be allowed to start a shell or whatever an init script
473 * specifies.
474 */
475#ifdef DEBUG_INIT
476 message(LOG, "Waiting for enter to start '%s' (pid %d, console %s)\r\n",
477 command, getpid(), terminal);
478#endif
479 write(fileno(stdout), press_enter, sizeof(press_enter) - 1);
480 getc(stdin);
481 }
482
483#ifdef DEBUG_INIT
484 /* Log the process name and args */
485 message(LOG, "Starting pid %d, console %s: '%s'\r\n",
486 getpid(), terminal, command);
487#endif
488
489 /* See if any special /bin/sh requiring characters are present */ 484 /* See if any special /bin/sh requiring characters are present */
490 if (strpbrk(command, "~`!$^&*()=|\\{}[];\"'<>?") != NULL) { 485 if (strpbrk(command, "~`!$^&*()=|\\{}[];\"'<>?") != NULL) {
491 cmd[0] = SHELL; 486 cmd[0] = SHELL;
@@ -497,7 +492,7 @@ static pid_t run(char *command, char *terminal, int get_enter)
497 } else { 492 } else {
498 /* Convert command (char*) into cmd (char**, one word per string) */ 493 /* Convert command (char*) into cmd (char**, one word per string) */
499 for (tmpCmd = command, i = 0; 494 for (tmpCmd = command, i = 0;
500 (tmpCmd = strsep(&command, " \t")) != NULL;) { 495 (tmpCmd = strsep(&command, " \t")) != NULL;) {
501 if (*tmpCmd != '\0') { 496 if (*tmpCmd != '\0') {
502 cmd[i] = tmpCmd; 497 cmd[i] = tmpCmd;
503 tmpCmd++; 498 tmpCmd++;
@@ -507,53 +502,73 @@ static pid_t run(char *command, char *terminal, int get_enter)
507 cmd[i] = NULL; 502 cmd[i] = NULL;
508 } 503 }
509 504
510 cmdpath = cmd[0]; 505 cmdpath = cmd[0];
511 506
512 /* 507 /*
513 Interactive shells want to see a dash in argv[0]. This 508 Interactive shells want to see a dash in argv[0]. This
514 typically is handled by login, argv will be setup this 509 typically is handled by login, argv will be setup this
515 way if a dash appears at the front of the command path 510 way if a dash appears at the front of the command path
516 (like "-/bin/sh"). 511 (like "-/bin/sh").
517 */ 512 */
518 513
519 if (*cmdpath == '-') { 514 if (*cmdpath == '-') {
520 char *s; 515 char *s;
521 516
522 /* skip over the dash */ 517 /* skip over the dash */
523 ++cmdpath; 518 ++cmdpath;
524 519
525 /* find the last component in the command pathname */ 520 /* find the last component in the command pathname */
526 s = get_last_path_component(cmdpath); 521 s = get_last_path_component(cmdpath);
527 522
528 /* make a new argv[0] */ 523 /* make a new argv[0] */
529 if ((cmd[0] = malloc(strlen(s)+2)) == NULL) { 524 if ((cmd[0] = malloc(strlen(s)+2)) == NULL) {
530 message(LOG | CONSOLE, "malloc failed"); 525 message(LOG | CONSOLE, "malloc failed");
531 cmd[0] = cmdpath; 526 cmd[0] = cmdpath;
532 } else { 527 } else {
533 cmd[0][0] = '-'; 528 cmd[0][0] = '-';
534 strcpy(cmd[0]+1, s); 529 strcpy(cmd[0]+1, s);
535 } 530 }
536 } 531 }
532
533 if (get_enter == TRUE) {
534 /*
535 * Save memory by not exec-ing anything large (like a shell)
536 * before the user wants it. This is critical if swap is not
537 * enabled and the system has low memory. Generally this will
538 * be run on the second virtual console, and the first will
539 * be allowed to start a shell or whatever an init script
540 * specifies.
541 */
542#ifdef DEBUG_INIT
543 message(LOG, "Waiting for enter to start '%s' (pid %d, console %s)\r\n",
544 cmd[0], getpid(), terminal);
545#endif
546 write(fileno(stdout), press_enter, sizeof(press_enter) - 1);
547 getc(stdin);
548 }
549
550#ifdef DEBUG_INIT
551 /* Log the process name and args */
552 message(LOG, "Starting pid %d, console %s: '%s'\r\n",
553 getpid(), terminal, command);
554#endif
537 555
538#if defined BB_FEATURE_INIT_COREDUMPS 556#if defined BB_FEATURE_INIT_COREDUMPS
539 { 557 if (stat (CORE_ENABLE_FLAG_FILE, &sb) == 0) {
540 struct stat sb; 558 struct rlimit limit;
541 if (stat (CORE_ENABLE_FLAG_FILE, &sb) == 0) { 559 limit.rlim_cur = RLIM_INFINITY;
542 struct rlimit limit; 560 limit.rlim_max = RLIM_INFINITY;
543 limit.rlim_cur = RLIM_INFINITY; 561 setrlimit(RLIMIT_CORE, &limit);
544 limit.rlim_max = RLIM_INFINITY;
545 setrlimit(RLIMIT_CORE, &limit);
546 }
547 } 562 }
548#endif 563#endif
549 564
550 /* Now run it. The new program will take over this PID, 565 /* Now run it. The new program will take over this PID,
551 * so nothing further in init.c should be run. */ 566 * so nothing further in init.c should be run. */
552 execve(cmdpath, cmd, environment); 567 execve(cmdpath, cmd, environment);
553 568
554 /* We're still here? Some error happened. */ 569 /* We're still here? Some error happened. */
555 message(LOG | CONSOLE, "Bummer, could not run '%s': %s\n", cmdpath, 570 message(LOG | CONSOLE, "Bummer, could not run '%s': %s\n", cmdpath,
556 strerror(errno)); 571 strerror(errno));
557 exit(-1); 572 exit(-1);
558 } 573 }
559 return pid; 574 return pid;
@@ -937,11 +952,8 @@ extern int init_main(int argc, char **argv)
937 parse_inittab(); 952 parse_inittab();
938 } 953 }
939 954
940 /* Fix up argv[0] to be certain we claim to be init */ 955 /* Make the command line just say "init" -- thats all, nothing else */
941 argv[0]="init"; 956 fixup_argv(argc, argv, "init");
942
943 if (argc > 1)
944 argv[1][0]=0;
945 957
946 /* Now run everything that needs to be run */ 958 /* Now run everything that needs to be run */
947 959
diff --git a/init/init.c b/init/init.c
index 145452ff3..b77589385 100644
--- a/init/init.c
+++ b/init/init.c
@@ -188,6 +188,7 @@ static char console[32] = _PATH_CONSOLE;
188static void delete_initAction(initAction * action); 188static void delete_initAction(initAction * action);
189 189
190 190
191
191/* Print a message to the specified device. 192/* Print a message to the specified device.
192 * Device may be bitwise-or'd from LOG | CONSOLE */ 193 * Device may be bitwise-or'd from LOG | CONSOLE */
193static void message(int device, char *fmt, ...) 194static void message(int device, char *fmt, ...)
@@ -393,6 +394,23 @@ static void console_init()
393 } 394 }
394 message(LOG, "console=%s\n", console); 395 message(LOG, "console=%s\n", console);
395} 396}
397
398static void fixup_argv(int argc, char **argv, char *new_argv0)
399{
400 int len;
401 /* Fix up argv[0] to be certain we claim to be init */
402 len = strlen(argv[0]);
403 memset(argv[0], 0, len);
404 strncpy(argv[0], new_argv0, len);
405
406 /* Wipe argv[1]-argv[N] so they don't clutter the ps listing */
407 len = 1;
408 while (argc > len) {
409 memset(argv[len], 0, strlen(argv[len]));
410 len++;
411 }
412}
413
396 414
397static pid_t run(char *command, char *terminal, int get_enter) 415static pid_t run(char *command, char *terminal, int get_enter)
398{ 416{
@@ -402,6 +420,7 @@ static pid_t run(char *command, char *terminal, int get_enter)
402 char *tmpCmd, *s; 420 char *tmpCmd, *s;
403 char *cmd[255], *cmdpath; 421 char *cmd[255], *cmdpath;
404 char buf[255]; 422 char buf[255];
423 struct stat sb;
405 static const char press_enter[] = 424 static const char press_enter[] =
406 425
407#ifdef CUSTOMIZED_BANNER 426#ifdef CUSTOMIZED_BANNER
@@ -447,8 +466,7 @@ static pid_t run(char *command, char *terminal, int get_enter)
447 signal(SIGHUP, SIG_DFL); 466 signal(SIGHUP, SIG_DFL);
448 467
449 if ((fd = device_open(terminal, O_RDWR)) < 0) { 468 if ((fd = device_open(terminal, O_RDWR)) < 0) {
450 struct stat statBuf; 469 if (stat(terminal, &sb) != 0) {
451 if (stat(terminal, &statBuf) != 0) {
452 message(LOG | CONSOLE, "device '%s' does not exist.\n", 470 message(LOG | CONSOLE, "device '%s' does not exist.\n",
453 terminal); 471 terminal);
454 exit(1); 472 exit(1);
@@ -463,29 +481,6 @@ static pid_t run(char *command, char *terminal, int get_enter)
463 tcsetpgrp(0, getpgrp()); 481 tcsetpgrp(0, getpgrp());
464 set_term(0); 482 set_term(0);
465 483
466 if (get_enter == TRUE) {
467 /*
468 * Save memory by not exec-ing anything large (like a shell)
469 * before the user wants it. This is critical if swap is not
470 * enabled and the system has low memory. Generally this will
471 * be run on the second virtual console, and the first will
472 * be allowed to start a shell or whatever an init script
473 * specifies.
474 */
475#ifdef DEBUG_INIT
476 message(LOG, "Waiting for enter to start '%s' (pid %d, console %s)\r\n",
477 command, getpid(), terminal);
478#endif
479 write(fileno(stdout), press_enter, sizeof(press_enter) - 1);
480 getc(stdin);
481 }
482
483#ifdef DEBUG_INIT
484 /* Log the process name and args */
485 message(LOG, "Starting pid %d, console %s: '%s'\r\n",
486 getpid(), terminal, command);
487#endif
488
489 /* See if any special /bin/sh requiring characters are present */ 484 /* See if any special /bin/sh requiring characters are present */
490 if (strpbrk(command, "~`!$^&*()=|\\{}[];\"'<>?") != NULL) { 485 if (strpbrk(command, "~`!$^&*()=|\\{}[];\"'<>?") != NULL) {
491 cmd[0] = SHELL; 486 cmd[0] = SHELL;
@@ -497,7 +492,7 @@ static pid_t run(char *command, char *terminal, int get_enter)
497 } else { 492 } else {
498 /* Convert command (char*) into cmd (char**, one word per string) */ 493 /* Convert command (char*) into cmd (char**, one word per string) */
499 for (tmpCmd = command, i = 0; 494 for (tmpCmd = command, i = 0;
500 (tmpCmd = strsep(&command, " \t")) != NULL;) { 495 (tmpCmd = strsep(&command, " \t")) != NULL;) {
501 if (*tmpCmd != '\0') { 496 if (*tmpCmd != '\0') {
502 cmd[i] = tmpCmd; 497 cmd[i] = tmpCmd;
503 tmpCmd++; 498 tmpCmd++;
@@ -507,53 +502,73 @@ static pid_t run(char *command, char *terminal, int get_enter)
507 cmd[i] = NULL; 502 cmd[i] = NULL;
508 } 503 }
509 504
510 cmdpath = cmd[0]; 505 cmdpath = cmd[0];
511 506
512 /* 507 /*
513 Interactive shells want to see a dash in argv[0]. This 508 Interactive shells want to see a dash in argv[0]. This
514 typically is handled by login, argv will be setup this 509 typically is handled by login, argv will be setup this
515 way if a dash appears at the front of the command path 510 way if a dash appears at the front of the command path
516 (like "-/bin/sh"). 511 (like "-/bin/sh").
517 */ 512 */
518 513
519 if (*cmdpath == '-') { 514 if (*cmdpath == '-') {
520 char *s; 515 char *s;
521 516
522 /* skip over the dash */ 517 /* skip over the dash */
523 ++cmdpath; 518 ++cmdpath;
524 519
525 /* find the last component in the command pathname */ 520 /* find the last component in the command pathname */
526 s = get_last_path_component(cmdpath); 521 s = get_last_path_component(cmdpath);
527 522
528 /* make a new argv[0] */ 523 /* make a new argv[0] */
529 if ((cmd[0] = malloc(strlen(s)+2)) == NULL) { 524 if ((cmd[0] = malloc(strlen(s)+2)) == NULL) {
530 message(LOG | CONSOLE, "malloc failed"); 525 message(LOG | CONSOLE, "malloc failed");
531 cmd[0] = cmdpath; 526 cmd[0] = cmdpath;
532 } else { 527 } else {
533 cmd[0][0] = '-'; 528 cmd[0][0] = '-';
534 strcpy(cmd[0]+1, s); 529 strcpy(cmd[0]+1, s);
535 } 530 }
536 } 531 }
532
533 if (get_enter == TRUE) {
534 /*
535 * Save memory by not exec-ing anything large (like a shell)
536 * before the user wants it. This is critical if swap is not
537 * enabled and the system has low memory. Generally this will
538 * be run on the second virtual console, and the first will
539 * be allowed to start a shell or whatever an init script
540 * specifies.
541 */
542#ifdef DEBUG_INIT
543 message(LOG, "Waiting for enter to start '%s' (pid %d, console %s)\r\n",
544 cmd[0], getpid(), terminal);
545#endif
546 write(fileno(stdout), press_enter, sizeof(press_enter) - 1);
547 getc(stdin);
548 }
549
550#ifdef DEBUG_INIT
551 /* Log the process name and args */
552 message(LOG, "Starting pid %d, console %s: '%s'\r\n",
553 getpid(), terminal, command);
554#endif
537 555
538#if defined BB_FEATURE_INIT_COREDUMPS 556#if defined BB_FEATURE_INIT_COREDUMPS
539 { 557 if (stat (CORE_ENABLE_FLAG_FILE, &sb) == 0) {
540 struct stat sb; 558 struct rlimit limit;
541 if (stat (CORE_ENABLE_FLAG_FILE, &sb) == 0) { 559 limit.rlim_cur = RLIM_INFINITY;
542 struct rlimit limit; 560 limit.rlim_max = RLIM_INFINITY;
543 limit.rlim_cur = RLIM_INFINITY; 561 setrlimit(RLIMIT_CORE, &limit);
544 limit.rlim_max = RLIM_INFINITY;
545 setrlimit(RLIMIT_CORE, &limit);
546 }
547 } 562 }
548#endif 563#endif
549 564
550 /* Now run it. The new program will take over this PID, 565 /* Now run it. The new program will take over this PID,
551 * so nothing further in init.c should be run. */ 566 * so nothing further in init.c should be run. */
552 execve(cmdpath, cmd, environment); 567 execve(cmdpath, cmd, environment);
553 568
554 /* We're still here? Some error happened. */ 569 /* We're still here? Some error happened. */
555 message(LOG | CONSOLE, "Bummer, could not run '%s': %s\n", cmdpath, 570 message(LOG | CONSOLE, "Bummer, could not run '%s': %s\n", cmdpath,
556 strerror(errno)); 571 strerror(errno));
557 exit(-1); 572 exit(-1);
558 } 573 }
559 return pid; 574 return pid;
@@ -937,11 +952,8 @@ extern int init_main(int argc, char **argv)
937 parse_inittab(); 952 parse_inittab();
938 } 953 }
939 954
940 /* Fix up argv[0] to be certain we claim to be init */ 955 /* Make the command line just say "init" -- thats all, nothing else */
941 argv[0]="init"; 956 fixup_argv(argc, argv, "init");
942
943 if (argc > 1)
944 argv[1][0]=0;
945 957
946 /* Now run everything that needs to be run */ 958 /* Now run everything that needs to be run */
947 959