diff options
author | andersen <andersen@69ca8d6d-28ef-0310-b511-8ec308f3f277> | 2001-10-28 05:12:20 +0000 |
---|---|---|
committer | andersen <andersen@69ca8d6d-28ef-0310-b511-8ec308f3f277> | 2001-10-28 05:12:20 +0000 |
commit | 79362aa78e62f5d1539b61ac300328c7235b1f0b (patch) | |
tree | 5dbf1995f603a95960c0ef94558954091ce7beef /init | |
parent | b71780b77e18183cdb25fc9402a786370bc7dbd6 (diff) | |
download | busybox-w32-79362aa78e62f5d1539b61ac300328c7235b1f0b.tar.gz busybox-w32-79362aa78e62f5d1539b61ac300328c7235b1f0b.tar.bz2 busybox-w32-79362aa78e62f5d1539b61ac300328c7235b1f0b.zip |
Add in some (theoretical) uClinux support. Some init cleanups
git-svn-id: svn://busybox.net/trunk/busybox@3596 69ca8d6d-28ef-0310-b511-8ec308f3f277
Diffstat (limited to 'init')
-rw-r--r-- | init/init.c | 97 |
1 files changed, 51 insertions, 46 deletions
diff --git a/init/init.c b/init/init.c index b6eaa46ea..1a5056c8c 100644 --- a/init/init.c +++ b/init/init.c | |||
@@ -75,6 +75,12 @@ struct serial_struct { | |||
75 | }; | 75 | }; |
76 | 76 | ||
77 | 77 | ||
78 | #if (__GNU_LIBRARY__ > 5) || defined(__dietlibc__) | ||
79 | #include <sys/reboot.h> | ||
80 | #define init_reboot(magic) reboot(magic) | ||
81 | #else | ||
82 | #define init_reboot(magic) reboot(0xfee1dead, 672274793, magic) | ||
83 | #endif | ||
78 | 84 | ||
79 | #ifndef RB_HALT_SYSTEM | 85 | #ifndef RB_HALT_SYSTEM |
80 | static const int RB_HALT_SYSTEM = 0xcdef0123; | 86 | static const int RB_HALT_SYSTEM = 0xcdef0123; |
@@ -84,13 +90,6 @@ static const int RB_DISABLE_CAD = 0; | |||
84 | static const int RB_AUTOBOOT = 0x01234567; | 90 | static const int RB_AUTOBOOT = 0x01234567; |
85 | #endif | 91 | #endif |
86 | 92 | ||
87 | #if (__GNU_LIBRARY__ > 5) || defined(__dietlibc__) | ||
88 | #include <sys/reboot.h> | ||
89 | #define init_reboot(magic) reboot(magic) | ||
90 | #else | ||
91 | #define init_reboot(magic) reboot(0xfee1dead, 672274793, magic) | ||
92 | #endif | ||
93 | |||
94 | #ifndef _PATH_STDPATH | 93 | #ifndef _PATH_STDPATH |
95 | #define _PATH_STDPATH "/usr/bin:/bin:/usr/sbin:/sbin" | 94 | #define _PATH_STDPATH "/usr/bin:/bin:/usr/sbin:/sbin" |
96 | #endif | 95 | #endif |
@@ -218,7 +217,7 @@ static void message(int device, char *fmt, ...) | |||
218 | device = CONSOLE; | 217 | device = CONSOLE; |
219 | } else if ((log_fd = device_open(log, O_RDWR|O_NDELAY)) < 0) { | 218 | } else if ((log_fd = device_open(log, O_RDWR|O_NDELAY)) < 0) { |
220 | log_fd = -2; | 219 | log_fd = -2; |
221 | fprintf(stderr, "Bummer, can't write to log on %s!\r\n", log); | 220 | fprintf(stderr, "Bummer, can't write to log on %s!\n", log); |
222 | log = NULL; | 221 | log = NULL; |
223 | device = CONSOLE; | 222 | device = CONSOLE; |
224 | } | 223 | } |
@@ -382,7 +381,7 @@ static void console_init(void) | |||
382 | if (strcmp( termType, "TERM=linux" ) == 0) | 381 | if (strcmp( termType, "TERM=linux" ) == 0) |
383 | safe_strncpy(termType, "TERM=vt102", sizeof(termType)); | 382 | safe_strncpy(termType, "TERM=vt102", sizeof(termType)); |
384 | message(LOG | CONSOLE, | 383 | message(LOG | CONSOLE, |
385 | "serial console detected. Disabling virtual terminals.\r\n"); | 384 | "serial console detected. Disabling virtual terminals.\n"); |
386 | } | 385 | } |
387 | close(fd); | 386 | close(fd); |
388 | } | 387 | } |
@@ -395,7 +394,7 @@ static void fixup_argv(int argc, char **argv, char *new_argv0) | |||
395 | /* Fix up argv[0] to be certain we claim to be init */ | 394 | /* Fix up argv[0] to be certain we claim to be init */ |
396 | len = strlen(argv[0]); | 395 | len = strlen(argv[0]); |
397 | memset(argv[0], 0, len); | 396 | memset(argv[0], 0, len); |
398 | strncpy(argv[0], new_argv0, len); | 397 | safe_strncpy(argv[0], new_argv0, len + 1); |
399 | 398 | ||
400 | /* Wipe argv[1]-argv[N] so they don't clutter the ps listing */ | 399 | /* Wipe argv[1]-argv[N] so they don't clutter the ps listing */ |
401 | len = 1; | 400 | len = 1; |
@@ -425,7 +424,7 @@ static pid_t run(char *command, char *terminal, int get_enter) | |||
425 | char *environment[MAXENV+1] = { | 424 | char *environment[MAXENV+1] = { |
426 | termType, | 425 | termType, |
427 | "HOME=/", | 426 | "HOME=/", |
428 | "PATH=/usr/bin:/bin:/usr/sbin:/sbin", | 427 | "PATH=" _PATH_STDPATH, |
429 | "SHELL=" SHELL, | 428 | "SHELL=" SHELL, |
430 | "USER=root", | 429 | "USER=root", |
431 | NULL | 430 | NULL |
@@ -444,7 +443,12 @@ static pid_t run(char *command, char *terminal, int get_enter) | |||
444 | } | 443 | } |
445 | } | 444 | } |
446 | 445 | ||
447 | if ((pid = fork()) == 0) { | 446 | #if !defined(__UCLIBC__) || defined(__UCLIBC_HAS_MMU__) |
447 | if ((pid = fork()) == 0) | ||
448 | #else | ||
449 | if ((pid = vfork()) == 0) | ||
450 | #endif | ||
451 | { | ||
448 | /* Clean up */ | 452 | /* Clean up */ |
449 | ioctl(0, TIOCNOTTY, 0); | 453 | ioctl(0, TIOCNOTTY, 0); |
450 | close(0); | 454 | close(0); |
@@ -465,7 +469,7 @@ static pid_t run(char *command, char *terminal, int get_enter) | |||
465 | terminal); | 469 | terminal); |
466 | exit(1); | 470 | exit(1); |
467 | } | 471 | } |
468 | message(LOG | CONSOLE, "Bummer, can't open %s\r\n", terminal); | 472 | message(LOG | CONSOLE, "Bummer, can't open %s\n", terminal); |
469 | exit(1); | 473 | exit(1); |
470 | } | 474 | } |
471 | dup2(fd, 0); | 475 | dup2(fd, 0); |
@@ -480,13 +484,16 @@ static pid_t run(char *command, char *terminal, int get_enter) | |||
480 | cmd[0] = SHELL; | 484 | cmd[0] = SHELL; |
481 | cmd[1] = "-c"; | 485 | cmd[1] = "-c"; |
482 | strcpy(buf, "exec "); | 486 | strcpy(buf, "exec "); |
483 | strncat(buf, command, sizeof(buf) - strlen(buf) - 1); | 487 | safe_strncpy(buf + sizeof("exec "), command, |
488 | sizeof(buf) - sizeof("exec ")); | ||
484 | cmd[2] = buf; | 489 | cmd[2] = buf; |
485 | cmd[3] = NULL; | 490 | cmd[3] = NULL; |
486 | } else { | 491 | } else { |
487 | /* Convert command (char*) into cmd (char**, one word per string) */ | 492 | /* Convert command (char*) into cmd (char**, one word per string) */ |
488 | for (tmpCmd = command, i = 0; | 493 | safe_strncpy(buf, command, sizeof(buf)); |
489 | (tmpCmd = strsep(&command, " \t")) != NULL;) { | 494 | s = buf; |
495 | for (tmpCmd = buf, i = 0; | ||
496 | (tmpCmd = strsep(&s, " \t")) != NULL;) { | ||
490 | if (*tmpCmd != '\0') { | 497 | if (*tmpCmd != '\0') { |
491 | cmd[i] = tmpCmd; | 498 | cmd[i] = tmpCmd; |
492 | tmpCmd++; | 499 | tmpCmd++; |
@@ -533,7 +540,7 @@ static pid_t run(char *command, char *terminal, int get_enter) | |||
533 | * specifies. | 540 | * specifies. |
534 | */ | 541 | */ |
535 | #ifdef DEBUG_INIT | 542 | #ifdef DEBUG_INIT |
536 | message(LOG, "Waiting for enter to start '%s' (pid %d, console %s)\r\n", | 543 | message(LOG, "Waiting for enter to start '%s' (pid %d, console %s)\n", |
537 | cmd[0], getpid(), terminal); | 544 | cmd[0], getpid(), terminal); |
538 | #endif | 545 | #endif |
539 | write(fileno(stdout), press_enter, sizeof(press_enter) - 1); | 546 | write(fileno(stdout), press_enter, sizeof(press_enter) - 1); |
@@ -542,7 +549,7 @@ static pid_t run(char *command, char *terminal, int get_enter) | |||
542 | 549 | ||
543 | #ifdef DEBUG_INIT | 550 | #ifdef DEBUG_INIT |
544 | /* Log the process name and args */ | 551 | /* Log the process name and args */ |
545 | message(LOG, "Starting pid %d, console %s: '%s'\r\n", | 552 | message(LOG, "Starting pid %d, console %s: '%s'\n", |
546 | getpid(), terminal, command); | 553 | getpid(), terminal, command); |
547 | #endif | 554 | #endif |
548 | 555 | ||
@@ -592,6 +599,7 @@ static void check_memory(void) | |||
592 | if (check_free_memory() > 1000) | 599 | if (check_free_memory() > 1000) |
593 | return; | 600 | return; |
594 | 601 | ||
602 | #if !defined(__UCLIBC__) || defined(__UCLIBC_HAS_MMU__) | ||
595 | if (stat("/etc/fstab", &statBuf) == 0) { | 603 | if (stat("/etc/fstab", &statBuf) == 0) { |
596 | /* swapon -a requires /proc typically */ | 604 | /* swapon -a requires /proc typically */ |
597 | waitfor("mount proc /proc -t proc", console, FALSE); | 605 | waitfor("mount proc /proc -t proc", console, FALSE); |
@@ -602,10 +610,11 @@ static void check_memory(void) | |||
602 | } else | 610 | } else |
603 | goto goodnight; | 611 | goto goodnight; |
604 | return; | 612 | return; |
613 | #endif | ||
605 | 614 | ||
606 | goodnight: | 615 | goodnight: |
607 | message(CONSOLE, | 616 | message(CONSOLE, |
608 | "Sorry, your computer does not have enough memory.\r\n"); | 617 | "Sorry, your computer does not have enough memory.\n"); |
609 | loop_forever(); | 618 | loop_forever(); |
610 | } | 619 | } |
611 | 620 | ||
@@ -627,22 +636,29 @@ static void run_actions(initActionEnum action) | |||
627 | static void shutdown_system(void) | 636 | static void shutdown_system(void) |
628 | { | 637 | { |
629 | 638 | ||
630 | /* first disable our SIGHUP signal */ | 639 | /* first disable all our signals */ |
631 | signal(SIGHUP, SIG_DFL); | 640 | sigemptyset(&block_signals); |
641 | sigaddset(&block_signals, SIGHUP); | ||
642 | sigaddset(&block_signals, SIGCHLD); | ||
643 | sigaddset(&block_signals, SIGUSR1); | ||
644 | sigaddset(&block_signals, SIGUSR2); | ||
645 | sigaddset(&block_signals, SIGINT); | ||
646 | sigaddset(&block_signals, SIGTERM); | ||
647 | sigprocmask(SIG_BLOCK, &block_signals, NULL); | ||
632 | 648 | ||
633 | /* Allow Ctrl-Alt-Del to reboot system. */ | 649 | /* Allow Ctrl-Alt-Del to reboot system. */ |
634 | init_reboot(RB_ENABLE_CAD); | 650 | init_reboot(RB_ENABLE_CAD); |
635 | 651 | ||
636 | message(CONSOLE|LOG, "\r\nThe system is going down NOW !!\r\n"); | 652 | message(CONSOLE|LOG, "\nThe system is going down NOW !!\n"); |
637 | sync(); | 653 | sync(); |
638 | 654 | ||
639 | /* Send signals to every process _except_ pid 1 */ | 655 | /* Send signals to every process _except_ pid 1 */ |
640 | message(CONSOLE|LOG, "Sending SIGTERM to all processes.\r\n"); | 656 | message(CONSOLE|LOG, "Sending SIGTERM to all processes.\n"); |
641 | kill(-1, SIGTERM); | 657 | kill(-1, SIGTERM); |
642 | sleep(1); | 658 | sleep(1); |
643 | sync(); | 659 | sync(); |
644 | 660 | ||
645 | message(CONSOLE|LOG, "Sending SIGKILL to all processes.\r\n"); | 661 | message(CONSOLE|LOG, "Sending SIGKILL to all processes.\n"); |
646 | kill(-1, SIGKILL); | 662 | kill(-1, SIGKILL); |
647 | sleep(1); | 663 | sleep(1); |
648 | 664 | ||
@@ -661,7 +677,7 @@ static void halt_signal(int sig) | |||
661 | { | 677 | { |
662 | shutdown_system(); | 678 | shutdown_system(); |
663 | message(CONSOLE|LOG, | 679 | message(CONSOLE|LOG, |
664 | "The system is halted. Press %s or turn off power\r\n", | 680 | "The system is halted. Press %s or turn off power\n", |
665 | (secondConsole == NULL) /* serial console */ | 681 | (secondConsole == NULL) /* serial console */ |
666 | ? "Reset" : "CTRL-ALT-DEL"); | 682 | ? "Reset" : "CTRL-ALT-DEL"); |
667 | sync(); | 683 | sync(); |
@@ -680,7 +696,7 @@ static void halt_signal(int sig) | |||
680 | static void reboot_signal(int sig) | 696 | static void reboot_signal(int sig) |
681 | { | 697 | { |
682 | shutdown_system(); | 698 | shutdown_system(); |
683 | message(CONSOLE|LOG, "Please stand by while rebooting the system.\r\n"); | 699 | message(CONSOLE|LOG, "Please stand by while rebooting the system.\n"); |
684 | sync(); | 700 | sync(); |
685 | 701 | ||
686 | /* allow time for last message to reach serial console */ | 702 | /* allow time for last message to reach serial console */ |
@@ -701,9 +717,7 @@ static void ctrlaltdel_signal(int sig) | |||
701 | static void new_initAction(initActionEnum action, char *process, char *cons) | 717 | static void new_initAction(initActionEnum action, char *process, char *cons) |
702 | { | 718 | { |
703 | initAction *newAction; | 719 | initAction *newAction; |
704 | #ifdef CONFIG_FEATURE_INIT_NORMAL_ORDER | ||
705 | initAction *a; | 720 | initAction *a; |
706 | #endif | ||
707 | 721 | ||
708 | if (*cons == '\0') | 722 | if (*cons == '\0') |
709 | cons = console; | 723 | cons = console; |
@@ -723,20 +737,15 @@ static void new_initAction(initActionEnum action, char *process, char *cons) | |||
723 | message(LOG | CONSOLE, "Memory allocation failure\n"); | 737 | message(LOG | CONSOLE, "Memory allocation failure\n"); |
724 | loop_forever(); | 738 | loop_forever(); |
725 | } | 739 | } |
726 | #ifdef CONFIG_FEATURE_INIT_NORMAL_ORDER | ||
727 | for (a = initActionList; a && a->nextPtr; a = a->nextPtr) ; | 740 | for (a = initActionList; a && a->nextPtr; a = a->nextPtr) ; |
728 | if (a) { | 741 | if (a) { |
729 | a->nextPtr = newAction; | 742 | a->nextPtr = newAction; |
730 | } else { | 743 | } else { |
731 | initActionList = newAction; | 744 | initActionList = newAction; |
732 | } | 745 | } |
733 | #else | 746 | safe_strncpy(newAction->process, process, 255); |
734 | newAction->nextPtr = initActionList; | ||
735 | initActionList = newAction; | ||
736 | #endif | ||
737 | strncpy(newAction->process, process, 255); | ||
738 | newAction->action = action; | 747 | newAction->action = action; |
739 | strncpy(newAction->console, cons, 255); | 748 | safe_strncpy(newAction->console, cons, 255); |
740 | newAction->pid = 0; | 749 | newAction->pid = 0; |
741 | // message(LOG|CONSOLE, "process='%s' action='%d' console='%s'\n", | 750 | // message(LOG|CONSOLE, "process='%s' action='%d' console='%s'\n", |
742 | // newAction->process, newAction->action, newAction->console); | 751 | // newAction->process, newAction->action, newAction->console); |
@@ -782,16 +791,11 @@ static void parse_inittab(void) | |||
782 | #endif | 791 | #endif |
783 | /* Reboot on Ctrl-Alt-Del */ | 792 | /* Reboot on Ctrl-Alt-Del */ |
784 | new_initAction(CTRLALTDEL, "/sbin/reboot", console); | 793 | new_initAction(CTRLALTDEL, "/sbin/reboot", console); |
785 | #ifdef CONFIG_FEATURE_INIT_NORMAL_ORDER | ||
786 | /* Umount all filesystems on halt/reboot */ | 794 | /* Umount all filesystems on halt/reboot */ |
787 | new_initAction(SHUTDOWN, "/bin/umount -a -r", console); | 795 | new_initAction(SHUTDOWN, "/bin/umount -a -r", console); |
796 | #if !defined(__UCLIBC__) || defined(__UCLIBC_HAS_MMU__) | ||
788 | /* Swapoff on halt/reboot */ | 797 | /* Swapoff on halt/reboot */ |
789 | new_initAction(SHUTDOWN, "/sbin/swapoff -a", console); | 798 | new_initAction(SHUTDOWN, "/sbin/swapoff -a", console); |
790 | #else | ||
791 | /* Swapoff on halt/reboot */ | ||
792 | new_initAction(SHUTDOWN, "/sbin/swapoff -a", console); | ||
793 | /* Umount all filesystems on halt/reboot */ | ||
794 | new_initAction(SHUTDOWN, "/bin/umount -a -r", console); | ||
795 | #endif | 799 | #endif |
796 | /* Askfirst shell on tty1 */ | 800 | /* Askfirst shell on tty1 */ |
797 | new_initAction(ASKFIRST, LOGIN_SHELL, console); | 801 | new_initAction(ASKFIRST, LOGIN_SHELL, console); |
@@ -916,13 +920,14 @@ extern int init_main(int argc, char **argv) | |||
916 | /* Figure out what kernel this is running */ | 920 | /* Figure out what kernel this is running */ |
917 | kernelVersion = get_kernel_revision(); | 921 | kernelVersion = get_kernel_revision(); |
918 | 922 | ||
919 | /* Figure out where the default console should be */ | ||
920 | console_init(); | ||
921 | |||
922 | /* Close whatever files are open, and reset the console. */ | 923 | /* Close whatever files are open, and reset the console. */ |
923 | close(0); | 924 | close(0); |
924 | close(1); | 925 | close(1); |
925 | close(2); | 926 | close(2); |
927 | |||
928 | /* Figure out where the default console should be */ | ||
929 | console_init(); | ||
930 | |||
926 | set_term(0); | 931 | set_term(0); |
927 | chdir("/"); | 932 | chdir("/"); |
928 | setsid(); | 933 | setsid(); |
@@ -937,14 +942,14 @@ extern int init_main(int argc, char **argv) | |||
937 | CONSOLE| | 942 | CONSOLE| |
938 | #endif | 943 | #endif |
939 | LOG, | 944 | LOG, |
940 | "init started: %s\r\n", full_version); | 945 | "init started: %s\n", full_version); |
941 | #else | 946 | #else |
942 | message( | 947 | message( |
943 | #if ! defined CONFIG_FEATURE_EXTRA_QUIET | 948 | #if ! defined CONFIG_FEATURE_EXTRA_QUIET |
944 | CONSOLE| | 949 | CONSOLE| |
945 | #endif | 950 | #endif |
946 | LOG, | 951 | LOG, |
947 | "init(%d) started: %s\r\n", getpid(), full_version); | 952 | "init(%d) started: %s\n", getpid(), full_version); |
948 | #endif | 953 | #endif |
949 | 954 | ||
950 | 955 | ||