1 /* vi: set sw=4 ts=4: */
3 * Mini init implementation for busybox
5 * Copyright (C) 1995, 1996 by Bruce Perens <bruce@pixar.com>.
6 * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org>
7 * Adjusted by so many folks, it's impossible to keep track.
9 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
16 #include <sys/ioctl.h>
18 #include <sys/reboot.h>
20 #include "init_shared.h"
23 # include <sys/syslog.h>
26 #define INIT_BUFFS_SIZE 256
28 /* From <linux/vt.h> */
30 unsigned short v_active; /* active vt */
31 unsigned short v_signal; /* signal to send */
32 unsigned short v_state; /* vt bitmask */
34 enum { VT_GETSTATE = 0x5603 }; /* get global vt state info */
36 /* From <linux/serial.h> */
37 struct serial_struct {
46 unsigned short close_delay;
48 char reserved_char[1];
50 unsigned short closing_wait; /* time to wait before closing */
51 unsigned short closing_wait2; /* no longer used... */
52 unsigned char *iomem_base;
53 unsigned short iomem_reg_shift;
54 unsigned int port_high;
55 unsigned long iomap_base; /* cookie passed into ioremap */
60 #define _PATH_STDPATH "/usr/bin:/bin:/usr/sbin:/sbin"
63 #if ENABLE_FEATURE_INIT_COREDUMPS
65 * When a file named CORE_ENABLE_FLAG_FILE exists, setrlimit is called
66 * before processes are spawned to set core file size as unlimited.
67 * This is for debugging only. Don't use this is production, unless
68 * you want core dumps lying about....
70 #define CORE_ENABLE_FLAG_FILE "/.init_enable_core"
71 #include <sys/resource.h>
74 #define INITTAB "/etc/inittab" /* inittab file location */
76 #define INIT_SCRIPT "/etc/init.d/rcS" /* Default sysinit script. */
79 #define MAXENV 16 /* Number of env. vars */
81 #define CONSOLE_BUFF_SIZE 32
83 /* Allowed init action types */
86 #define ASKFIRST 0x004
89 #define CTRLALTDEL 0x020
90 #define SHUTDOWN 0x040
93 /* A mapping between "inittab" action name strings and action type codes. */
94 struct init_action_type {
99 static const struct init_action_type actions[] = {
100 {"sysinit", SYSINIT},
101 {"respawn", RESPAWN},
102 {"askfirst", ASKFIRST},
105 {"ctrlaltdel", CTRLALTDEL},
106 {"shutdown", SHUTDOWN},
107 {"restart", RESTART},
111 /* Set up a linked list of init_actions, to be read from inittab */
114 char command[INIT_BUFFS_SIZE];
115 char terminal[CONSOLE_BUFF_SIZE];
116 struct init_action *next;
120 /* Static variables */
121 static struct init_action *init_action_list = NULL;
122 static char console[CONSOLE_BUFF_SIZE] = CONSOLE_DEV;
125 static const char *log_console = VC_5;
127 #if !ENABLE_DEBUG_INIT
128 static sig_atomic_t got_cont = 0;
135 #if ENABLE_FEATURE_EXTRA_QUIET
138 MAYBE_CONSOLE = CONSOLE,
141 #ifndef RB_HALT_SYSTEM
142 RB_HALT_SYSTEM = 0xcdef0123, /* FIXME: this overflows enum */
143 RB_ENABLE_CAD = 0x89abcdef,
145 RB_POWER_OFF = 0x4321fedc,
146 RB_AUTOBOOT = 0x01234567,
150 static const char * const environment[] = {
152 "PATH=" _PATH_STDPATH,
158 /* Function prototypes */
159 static void delete_init_action(struct init_action *a);
160 static int waitfor(const struct init_action *a, pid_t pid);
161 #if !ENABLE_DEBUG_INIT
162 static void shutdown_signal(int sig);
165 #if !ENABLE_DEBUG_INIT
166 static void loop_forever(void)
173 /* Print a message to the specified device.
174 * Device may be bitwise-or'd from LOG | CONSOLE */
175 #if ENABLE_DEBUG_INIT
176 #define messageD message
178 #define messageD(...) do {} while (0)
180 static void message(int device, const char *fmt, ...)
181 __attribute__ ((format(printf, 2, 3)));
182 static void message(int device, const char *fmt, ...)
186 RESERVE_CONFIG_BUFFER(msg, 1024);
188 static int log_fd = -1;
192 va_start(arguments, fmt);
193 l = vsnprintf(msg + 1, 1024 - 2, fmt, arguments) + 1;
197 /* Log the message to syslogd */
199 /* don`t out "\r\n" */
200 openlog(applet_name, 0, LOG_DAEMON);
201 syslog(LOG_INFO, "%s", msg + 1);
211 /* Take full control of the log tty, and never close it.
212 * It's mine, all mine! Muhahahaha! */
214 if ((log_fd = device_open(log_console, O_RDWR | O_NONBLOCK | O_NOCTTY)) < 0) {
216 bb_error_msg("bummer, can't write to log on %s!", log_console);
219 fcntl(log_fd, F_SETFD, FD_CLOEXEC);
222 if ((device & LOG) && (log_fd >= 0)) {
223 full_write(log_fd, msg, l);
227 if (device & CONSOLE) {
228 int fd = device_open(CONSOLE_DEV,
229 O_WRONLY | O_NOCTTY | O_NONBLOCK);
230 /* Always send console messages to /dev/console so people will see them. */
232 full_write(fd, msg, l);
234 #if ENABLE_DEBUG_INIT
235 /* all descriptors may be closed */
237 bb_error_msg("bummer, can't print: ");
238 va_start(arguments, fmt);
239 vfprintf(stderr, fmt, arguments);
244 RELEASE_CONFIG_BUFFER(msg);
247 /* Set terminal settings to reasonable defaults */
248 static void set_term(void)
252 tcgetattr(STDIN_FILENO, &tty);
254 /* set control chars */
255 tty.c_cc[VINTR] = 3; /* C-c */
256 tty.c_cc[VQUIT] = 28; /* C-\ */
257 tty.c_cc[VERASE] = 127; /* C-? */
258 tty.c_cc[VKILL] = 21; /* C-u */
259 tty.c_cc[VEOF] = 4; /* C-d */
260 tty.c_cc[VSTART] = 17; /* C-q */
261 tty.c_cc[VSTOP] = 19; /* C-s */
262 tty.c_cc[VSUSP] = 26; /* C-z */
264 /* use line dicipline 0 */
267 /* Make it be sane */
268 tty.c_cflag &= CBAUD | CBAUDEX | CSIZE | CSTOPB | PARENB | PARODD;
269 tty.c_cflag |= CREAD | HUPCL | CLOCAL;
273 tty.c_iflag = ICRNL | IXON | IXOFF;
276 tty.c_oflag = OPOST | ONLCR;
280 ISIG | ICANON | ECHO | ECHOE | ECHOK | ECHOCTL | ECHOKE | IEXTEN;
282 tcsetattr(STDIN_FILENO, TCSANOW, &tty);
285 static void console_init(void)
290 struct serial_struct sr;
293 if ((s = getenv("CONSOLE")) != NULL || (s = getenv("console")) != NULL) {
294 safe_strncpy(console, s, sizeof(console));
296 /* 2.2 kernels: identify the real console backend and try to use it */
297 if (ioctl(0, TIOCGSERIAL, &sr) == 0) {
298 /* this is a serial console */
299 snprintf(console, sizeof(console) - 1, SC_FORMAT, sr.line);
300 } else if (ioctl(0, VT_GETSTATE, &vt) == 0) {
301 /* this is linux virtual tty */
302 snprintf(console, sizeof(console) - 1, VC_FORMAT, vt.v_active);
304 safe_strncpy(console, CONSOLE_DEV, sizeof(console));
309 while ((fd = open(console, O_RDONLY | O_NONBLOCK)) < 0 && tried < 2) {
310 /* Can't open selected console -- try
311 logical system console and VT_MASTER */
312 safe_strncpy(console, (tried == 0 ? CONSOLE_DEV : CURRENT_VC),
317 /* Perhaps we should panic here? */
321 safe_strncpy(console, bb_dev_null, sizeof(console));
324 /* check for serial console */
325 if (ioctl(fd, TIOCGSERIAL, &sr) == 0) {
326 /* Force the TERM setting to vt102 for serial console --
327 * if TERM is set to linux (the default) */
328 if (s == NULL || strcmp(s, "linux") == 0)
329 putenv((char*)"TERM=vt102");
331 log_console = console;
335 putenv((char*)"TERM=linux");
339 messageD(LOG, "console=%s", console);
342 static void fixup_argv(int argc, char **argv, const char *new_argv0)
346 /* Fix up argv[0] to be certain we claim to be init */
347 len = strlen(argv[0]);
348 strncpy(argv[0], new_argv0, len);
350 /* Wipe argv[1]-argv[N] so they don't clutter the ps listing */
353 memset(argv[len], 0, strlen(argv[len]));
358 /* Open the new terminal device */
359 static void open_new_terminal(const char * const device, const int fail) {
362 if ((device_open(device, O_RDWR)) < 0) {
363 if (stat(device, &sb) != 0) {
364 message(LOG | CONSOLE, "device '%s' does not exist.", device);
366 message(LOG | CONSOLE, "Bummer, can't open %s", device);
371 #if !ENABLE_DEBUG_INIT
372 shutdown_signal(SIGUSR1);
379 static pid_t run(const struct init_action *a)
383 char *s, *tmpCmd, *cmdpath;
384 char *cmd[INIT_BUFFS_SIZE];
385 char buf[INIT_BUFFS_SIZE + 6]; /* INIT_BUFFS_SIZE+strlen("exec ")+1 */
386 sigset_t nmask, omask;
387 static const char press_enter[] =
388 #ifdef CUSTOMIZED_BANNER
389 #include CUSTOMIZED_BANNER
391 "\nPlease press Enter to activate this console. ";
393 /* Block sigchild while forking. */
395 sigaddset(&nmask, SIGCHLD);
396 sigprocmask(SIG_BLOCK, &nmask, &omask);
398 if ((pid = fork()) == 0) {
404 sigprocmask(SIG_SETMASK, &omask, NULL);
406 /* Reset signal handlers that were set by the parent process */
407 signal(SIGUSR1, SIG_DFL);
408 signal(SIGUSR2, SIG_DFL);
409 signal(SIGINT, SIG_DFL);
410 signal(SIGTERM, SIG_DFL);
411 signal(SIGHUP, SIG_DFL);
412 signal(SIGQUIT, SIG_DFL);
413 signal(SIGCONT, SIG_DFL);
414 signal(SIGSTOP, SIG_DFL);
415 signal(SIGTSTP, SIG_DFL);
417 /* Create a new session and make ourself the process
421 /* Open the new terminal device */
422 open_new_terminal(a->terminal, 1);
424 /* Make sure the terminal will act fairly normal for us */
426 /* Setup stdout, stderr for the new process so
427 * they point to the supplied terminal */
431 /* If the init Action requires us to wait, then force the
432 * supplied terminal to be the controlling tty. */
433 if (a->action & (SYSINIT | WAIT | CTRLALTDEL | SHUTDOWN | RESTART)) {
435 /* Now fork off another process to just hang around */
436 if ((pid = fork()) < 0) {
437 message(LOG | CONSOLE, "Can't fork!");
443 /* We are the parent -- wait till the child is done */
444 signal(SIGINT, SIG_IGN);
445 signal(SIGTSTP, SIG_IGN);
446 signal(SIGQUIT, SIG_IGN);
447 signal(SIGCHLD, SIG_DFL);
450 /* See if stealing the controlling tty back is necessary */
451 if (tcgetpgrp(0) != getpid())
454 /* Use a temporary process to steal the controlling tty. */
455 if ((pid = fork()) < 0) {
456 message(LOG | CONSOLE, "Can't fork!");
461 ioctl(0, TIOCSCTTY, 1);
468 /* Now fall though to actually execute things */
471 /* See if any special /bin/sh requiring characters are present */
472 if (strpbrk(a->command, "~`!$^&*()=|\\{}[];\"'<>?") != NULL) {
473 cmd[0] = (char *)DEFAULT_SHELL;
474 cmd[1] = (char*)"-c";
475 cmd[2] = strcat(strcpy(buf, "exec "), a->command);
478 /* Convert command (char*) into cmd (char**, one word per string) */
479 strcpy(buf, a->command);
481 for (tmpCmd = buf, i = 0; (tmpCmd = strsep(&s, " \t")) != NULL;) {
482 if (*tmpCmd != '\0') {
493 Interactive shells want to see a dash in argv[0]. This
494 typically is handled by login, argv will be setup this
495 way if a dash appears at the front of the command path
499 if (*cmdpath == '-') {
501 /* skip over the dash */
504 /* find the last component in the command pathname */
505 s = bb_get_last_path_component(cmdpath);
507 /* make a new argv[0] */
508 if ((cmd[0] = malloc(strlen(s) + 2)) == NULL) {
509 message(LOG | CONSOLE, bb_msg_memory_exhausted);
513 strcpy(cmd[0] + 1, s);
515 #if ENABLE_FEATURE_INIT_SCTTY
516 /* Establish this process as session leader and
517 * (attempt) to make the tty (if any) a controlling tty.
520 (void) ioctl(0, TIOCSCTTY, 0/*don't steal it*/);
524 #if !defined(__UCLIBC__) || defined(__ARCH_HAS_MMU__)
525 if (a->action & ASKFIRST) {
528 * Save memory by not exec-ing anything large (like a shell)
529 * before the user wants it. This is critical if swap is not
530 * enabled and the system has low memory. Generally this will
531 * be run on the second virtual console, and the first will
532 * be allowed to start a shell or whatever an init script
535 messageD(LOG, "Waiting for enter to start '%s'"
536 "(pid %d, terminal %s)\n",
537 cmdpath, getpid(), a->terminal);
538 full_write(1, press_enter, sizeof(press_enter) - 1);
539 while (read(0, &c, 1) == 1 && c != '\n')
544 /* Log the process name and args */
545 message(LOG, "Starting pid %d, console %s: '%s'",
546 getpid(), a->terminal, cmdpath);
548 #if ENABLE_FEATURE_INIT_COREDUMPS
551 if (stat(CORE_ENABLE_FLAG_FILE, &sb) == 0) {
554 limit.rlim_cur = RLIM_INFINITY;
555 limit.rlim_max = RLIM_INFINITY;
556 setrlimit(RLIMIT_CORE, &limit);
561 /* Now run it. The new program will take over this PID,
562 * so nothing further in init.c should be run. */
563 BB_EXECVP(cmdpath, cmd);
565 /* We're still here? Some error happened. */
566 message(LOG | CONSOLE, "Bummer, cannot run '%s': %m", cmdpath);
569 sigprocmask(SIG_SETMASK, &omask, NULL);
573 static int waitfor(const struct init_action *a, pid_t pid)
578 runpid = (NULL == a)? pid : run(a);
580 wpid = waitpid(runpid,&status,0);
583 if (wpid == -1 && errno == ECHILD) {
584 /* we missed its termination */
587 /* FIXME other errors should maybe trigger an error, but allow
588 * the program to continue */
593 /* Run all commands of a particular type */
594 static void run_actions(int action)
596 struct init_action *a, *tmp;
598 for (a = init_action_list; a; a = tmp) {
600 if (a->action == action) {
601 if (access(a->terminal, R_OK | W_OK)) {
602 delete_init_action(a);
603 } else if (a->action & (SYSINIT | WAIT | CTRLALTDEL | SHUTDOWN | RESTART)) {
605 delete_init_action(a);
606 } else if (a->action & ONCE) {
608 delete_init_action(a);
609 } else if (a->action & (RESPAWN | ASKFIRST)) {
610 /* Only run stuff with pid==0. If they have
611 * a pid, that means it is still running */
620 #if !ENABLE_DEBUG_INIT
621 static void init_reboot(unsigned long magic)
624 /* We have to fork here, since the kernel calls do_exit(0) in
625 * linux/kernel/sys.c, which can cause the machine to panic when
626 * the init process is killed.... */
628 if (pid == 0) { /* child */
632 waitpid(pid, NULL, 0);
635 static void shutdown_system(void)
637 sigset_t block_signals;
639 /* run everything to be run at "shutdown". This is done _prior_
640 * to killing everything, in case people wish to use scripts to
641 * shut things down gracefully... */
642 run_actions(SHUTDOWN);
644 /* first disable all our signals */
645 sigemptyset(&block_signals);
646 sigaddset(&block_signals, SIGHUP);
647 sigaddset(&block_signals, SIGQUIT);
648 sigaddset(&block_signals, SIGCHLD);
649 sigaddset(&block_signals, SIGUSR1);
650 sigaddset(&block_signals, SIGUSR2);
651 sigaddset(&block_signals, SIGINT);
652 sigaddset(&block_signals, SIGTERM);
653 sigaddset(&block_signals, SIGCONT);
654 sigaddset(&block_signals, SIGSTOP);
655 sigaddset(&block_signals, SIGTSTP);
656 sigprocmask(SIG_BLOCK, &block_signals, NULL);
658 /* Allow Ctrl-Alt-Del to reboot system. */
659 init_reboot(RB_ENABLE_CAD);
661 message(CONSOLE | LOG, "The system is going down NOW !!");
664 /* Send signals to every process _except_ pid 1 */
665 message(CONSOLE | LOG, init_sending_format, "TERM");
670 message(CONSOLE | LOG, init_sending_format, "KILL");
677 static void exec_signal(int sig ATTRIBUTE_UNUSED)
679 struct init_action *a, *tmp;
680 sigset_t unblock_signals;
682 for (a = init_action_list; a; a = tmp) {
684 if (a->action & RESTART) {
687 /* unblock all signals, blocked in shutdown_system() */
688 sigemptyset(&unblock_signals);
689 sigaddset(&unblock_signals, SIGHUP);
690 sigaddset(&unblock_signals, SIGQUIT);
691 sigaddset(&unblock_signals, SIGCHLD);
692 sigaddset(&unblock_signals, SIGUSR1);
693 sigaddset(&unblock_signals, SIGUSR2);
694 sigaddset(&unblock_signals, SIGINT);
695 sigaddset(&unblock_signals, SIGTERM);
696 sigaddset(&unblock_signals, SIGCONT);
697 sigaddset(&unblock_signals, SIGSTOP);
698 sigaddset(&unblock_signals, SIGTSTP);
699 sigprocmask(SIG_UNBLOCK, &unblock_signals, NULL);
701 /* Close whatever files are open. */
706 /* Open the new terminal device */
707 open_new_terminal(a->terminal, 0);
709 /* Make sure the terminal will act fairly normal for us */
711 /* Setup stdout, stderr on the supplied terminal */
715 messageD(CONSOLE | LOG, "Trying to re-exec %s", a->command);
716 BB_EXECLP(a->command, a->command, NULL);
718 message(CONSOLE | LOG, "exec of '%s' failed: %m",
722 init_reboot(RB_HALT_SYSTEM);
728 static void shutdown_signal(int sig)
737 if (sig == SIGTERM) {
740 } else if (sig == SIGUSR2) {
744 message(CONSOLE | LOG, "Requesting system %s.", m);
747 /* allow time for last message to reach serial console */
754 static void ctrlaltdel_signal(int sig ATTRIBUTE_UNUSED)
756 run_actions(CTRLALTDEL);
759 /* The SIGSTOP & SIGTSTP handler */
760 static void stop_handler(int sig ATTRIBUTE_UNUSED)
762 int saved_errno = errno;
771 /* The SIGCONT handler */
772 static void cont_handler(int sig ATTRIBUTE_UNUSED)
777 #endif /* !ENABLE_DEBUG_INIT */
779 static void new_init_action(int action, const char *command, const char *cons)
781 struct init_action *new_action, *a, *last;
786 if (strcmp(cons, bb_dev_null) == 0 && (action & ASKFIRST))
789 new_action = xzalloc(sizeof(struct init_action));
791 /* Append to the end of the list */
792 for (a = last = init_action_list; a; a = a->next) {
793 /* don't enter action if it's already in the list,
794 * but do overwrite existing actions */
795 if ((strcmp(a->command, command) == 0) &&
796 (strcmp(a->terminal, cons) ==0)) {
804 last->next = new_action;
806 init_action_list = new_action;
808 strcpy(new_action->command, command);
809 new_action->action = action;
810 strcpy(new_action->terminal, cons);
811 messageD(LOG|CONSOLE, "command='%s' action='%d' terminal='%s'\n",
812 new_action->command, new_action->action, new_action->terminal);
815 static void delete_init_action(struct init_action *action)
817 struct init_action *a, *b = NULL;
819 for (a = init_action_list; a; b = a, a = a->next) {
822 init_action_list = a->next;
832 /* NOTE that if CONFIG_FEATURE_USE_INITTAB is NOT defined,
833 * then parse_inittab() simply adds in some default
834 * actions(i.e., runs INIT_SCRIPT and then starts a pair
835 * of "askfirst" shells). If CONFIG_FEATURE_USE_INITTAB
836 * _is_ defined, but /etc/inittab is missing, this
837 * results in the same set of default behaviors.
839 static void parse_inittab(void)
841 #if ENABLE_FEATURE_USE_INITTAB
843 char buf[INIT_BUFFS_SIZE], lineAsRead[INIT_BUFFS_SIZE];
844 char tmpConsole[CONSOLE_BUFF_SIZE];
845 char *id, *runlev, *action, *command, *eol;
846 const struct init_action_type *a = actions;
849 file = fopen(INITTAB, "r");
851 /* No inittab file -- set up some default behavior */
853 /* Reboot on Ctrl-Alt-Del */
854 new_init_action(CTRLALTDEL, "reboot", "");
855 /* Umount all filesystems on halt/reboot */
856 new_init_action(SHUTDOWN, "umount -a -r", "");
857 /* Swapoff on halt/reboot */
858 if(ENABLE_SWAPONOFF) new_init_action(SHUTDOWN, "swapoff -a", "");
859 /* Prepare to restart init when a HUP is received */
860 new_init_action(RESTART, "init", "");
861 /* Askfirst shell on tty1-4 */
862 new_init_action(ASKFIRST, bb_default_login_shell, "");
863 new_init_action(ASKFIRST, bb_default_login_shell, VC_2);
864 new_init_action(ASKFIRST, bb_default_login_shell, VC_3);
865 new_init_action(ASKFIRST, bb_default_login_shell, VC_4);
867 new_init_action(SYSINIT, INIT_SCRIPT, "");
870 #if ENABLE_FEATURE_USE_INITTAB
873 while (fgets(buf, INIT_BUFFS_SIZE, file) != NULL) {
874 /* Skip leading spaces */
875 for (id = buf; *id == ' ' || *id == '\t'; id++);
877 /* Skip the line if it's a comment */
878 if (*id == '#' || *id == '\n')
881 /* Trim the trailing \n */
882 eol = strrchr(id, '\n');
886 /* Keep a copy around for posterity's sake (and error msgs) */
887 strcpy(lineAsRead, buf);
889 /* Separate the ID field from the runlevels */
890 runlev = strchr(id, ':');
891 if (runlev == NULL || *(runlev + 1) == '\0') {
892 message(LOG | CONSOLE, "Bad inittab entry: %s", lineAsRead);
899 /* Separate the runlevels from the action */
900 action = strchr(runlev, ':');
901 if (action == NULL || *(action + 1) == '\0') {
902 message(LOG | CONSOLE, "Bad inittab entry: %s", lineAsRead);
909 /* Separate the action from the command */
910 command = strchr(action, ':');
911 if (command == NULL || *(command + 1) == '\0') {
912 message(LOG | CONSOLE, "Bad inittab entry: %s", lineAsRead);
919 /* Ok, now process it */
920 for (a = actions; a->name != 0; a++) {
921 if (strcmp(a->name, action) == 0) {
923 if(strncmp(id, "/dev/", 5) == 0)
925 strcpy(tmpConsole, "/dev/");
926 safe_strncpy(tmpConsole + 5, id,
927 CONSOLE_BUFF_SIZE - 5);
930 new_init_action(a->action, command, id);
935 /* Choke on an unknown action */
936 message(LOG | CONSOLE, "Bad inittab entry: %s", lineAsRead);
941 #endif /* FEATURE_USE_INITTAB */
944 #if ENABLE_FEATURE_USE_INITTAB
945 static void reload_signal(int sig ATTRIBUTE_UNUSED)
947 struct init_action *a, *tmp;
949 message(LOG, "Reloading /etc/inittab");
951 /* disable old entrys */
952 for (a = init_action_list; a; a = a->next ) {
958 /* remove unused entrys */
959 for (a = init_action_list; a; a = tmp) {
961 if (a->action & (ONCE | SYSINIT | WAIT ) &&
963 delete_init_action(a);
966 run_actions(RESPAWN);
969 #endif /* FEATURE_USE_INITTAB */
971 int init_main(int argc, char **argv);
972 int init_main(int argc, char **argv)
974 struct init_action *a;
977 die_sleep = 30 * 24*60*60; /* if xmalloc will ever die... */
979 if (argc > 1 && !strcmp(argv[1], "-q")) {
980 return kill(1,SIGHUP);
982 #if !ENABLE_DEBUG_INIT
983 /* Expect to be invoked as init with PID=1 or be invoked as linuxrc */
985 (!ENABLE_FEATURE_INITRD || !strstr(applet_name, "linuxrc")))
989 /* Set up sig handlers -- be sure to
990 * clear all of these in run() */
991 signal(SIGHUP, exec_signal);
992 signal(SIGQUIT, exec_signal);
993 signal(SIGUSR1, shutdown_signal);
994 signal(SIGUSR2, shutdown_signal);
995 signal(SIGINT, ctrlaltdel_signal);
996 signal(SIGTERM, shutdown_signal);
997 signal(SIGCONT, cont_handler);
998 signal(SIGSTOP, stop_handler);
999 signal(SIGTSTP, stop_handler);
1001 /* Turn off rebooting via CTL-ALT-DEL -- we get a
1002 * SIGINT on CAD so we can shut things down gracefully... */
1003 init_reboot(RB_DISABLE_CAD);
1006 /* Figure out where the default console should be */
1009 /* Close whatever files are open, and reset the console. */
1014 if (device_open(console, O_RDWR | O_NOCTTY) == 0) {
1022 const char * const *e;
1023 /* Make sure environs is set to something sane */
1024 for (e = environment; *e; e++)
1025 putenv((char *) *e);
1028 if (argc > 1) setenv("RUNLEVEL", argv[1], 1);
1031 message(MAYBE_CONSOLE | LOG, "init started: %s", bb_msg_full_version);
1033 /* Make sure there is enough memory to do something useful. */
1034 if (ENABLE_SWAPONOFF) {
1035 struct sysinfo info;
1037 if (!sysinfo(&info) &&
1038 (info.mem_unit ? : 1) * (long long)info.totalram < 1024*1024)
1040 message(CONSOLE,"Low memory: forcing swapon.");
1041 /* swapon -a requires /proc typically */
1042 new_init_action(SYSINIT, "mount -t proc proc /proc", "");
1043 /* Try to turn on swap */
1044 new_init_action(SYSINIT, "swapon -a", "");
1045 run_actions(SYSINIT); /* wait and removing */
1049 /* Check if we are supposed to be in single user mode */
1051 && (!strcmp(argv[1], "single") || !strcmp(argv[1], "-s") || LONE_CHAR(argv[1], '1'))
1053 /* Start a shell on console */
1054 new_init_action(RESPAWN, bb_default_login_shell, "");
1056 /* Not in single user mode -- see what inittab says */
1058 /* NOTE that if CONFIG_FEATURE_USE_INITTAB is NOT defined,
1059 * then parse_inittab() simply adds in some default
1060 * actions(i.e., runs INIT_SCRIPT and then starts a pair
1061 * of "askfirst" shells */
1066 if (getenv("SELINUX_INIT") == NULL) {
1069 putenv("SELINUX_INIT=YES");
1070 if (selinux_init_load_policy(&enforce) == 0) {
1071 BB_EXECVP(argv[0], argv);
1072 } else if (enforce > 0) {
1073 /* SELinux in enforcing mode but load_policy failed */
1074 /* At this point, we probably can't open /dev/console, so log() won't work */
1075 message(CONSOLE,"Unable to load SELinux Policy. Machine is in enforcing mode. Halting now.");
1079 #endif /* CONFIG_SELINUX */
1081 /* Make the command line just say "init" -- thats all, nothing else */
1082 fixup_argv(argc, argv, "init");
1084 /* Now run everything that needs to be run */
1086 /* First run the sysinit command */
1087 run_actions(SYSINIT);
1089 /* Next run anything that wants to block */
1092 /* Next run anything to be run only once */
1095 #if ENABLE_FEATURE_USE_INITTAB
1096 /* Redefine SIGHUP to reread /etc/inittab */
1097 signal(SIGHUP, reload_signal);
1099 signal(SIGHUP, SIG_IGN);
1100 #endif /* FEATURE_USE_INITTAB */
1103 /* Now run the looping stuff for the rest of forever */
1105 /* run the respawn stuff */
1106 run_actions(RESPAWN);
1108 /* run the askfirst stuff */
1109 run_actions(ASKFIRST);
1111 /* Don't consume all CPU time -- sleep a bit */
1114 /* Wait for a child process to exit */
1117 /* Find out who died and clean up their corpse */
1118 for (a = init_action_list; a; a = a->next) {
1119 if (a->pid == wpid) {
1120 /* Set the pid to 0 so that the process gets
1121 * restarted by run_actions() */
1123 message(LOG, "Process '%s' (pid %d) exited. "
1124 "Scheduling it for restart.",
1128 /* see if anyone else is waiting to be reaped */
1129 wpid = waitpid(-1, NULL, WNOHANG);