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.
23 #include <sys/ioctl.h>
24 #include <sys/types.h>
26 #include <sys/reboot.h>
29 #include "init_shared.h"
33 # include <sys/syslog.h>
38 # include <selinux/selinux.h>
39 #endif /* CONFIG_SELINUX */
42 #define INIT_BUFFS_SIZE 256
44 /* From <linux/vt.h> */
46 unsigned short v_active; /* active vt */
47 unsigned short v_signal; /* signal to send */
48 unsigned short v_state; /* vt bitmask */
50 enum { VT_GETSTATE = 0x5603 }; /* get global vt state info */
52 /* From <linux/serial.h> */
53 struct serial_struct {
62 unsigned short close_delay;
64 char reserved_char[1];
66 unsigned short closing_wait; /* time to wait before closing */
67 unsigned short closing_wait2; /* no longer used... */
68 unsigned char *iomem_base;
69 unsigned short iomem_reg_shift;
70 unsigned int port_high;
71 unsigned long iomap_base; /* cookie passed into ioremap */
77 #define _PATH_STDPATH "/usr/bin:/bin:/usr/sbin:/sbin"
80 #if defined CONFIG_FEATURE_INIT_COREDUMPS
82 * When a file named CORE_ENABLE_FLAG_FILE exists, setrlimit is called
83 * before processes are spawned to set core file size as unlimited.
84 * This is for debugging only. Don't use this is production, unless
85 * you want core dumps lying about....
87 #define CORE_ENABLE_FLAG_FILE "/.init_enable_core"
88 #include <sys/resource.h>
91 #define KERNEL_VERSION(a,b,c) (((a) << 16) + ((b) << 8) + (c))
93 #define INITTAB "/etc/inittab" /* inittab file location */
95 #define INIT_SCRIPT "/etc/init.d/rcS" /* Default sysinit script. */
98 #define MAXENV 16 /* Number of env. vars */
100 #define CONSOLE_BUFF_SIZE 32
102 /* Allowed init action types */
103 #define SYSINIT 0x001
104 #define RESPAWN 0x002
105 #define ASKFIRST 0x004
108 #define CTRLALTDEL 0x020
109 #define SHUTDOWN 0x040
110 #define RESTART 0x080
112 /* A mapping between "inittab" action name strings and action type codes. */
113 struct init_action_type {
118 static const struct init_action_type actions[] = {
119 {"sysinit", SYSINIT},
120 {"respawn", RESPAWN},
121 {"askfirst", ASKFIRST},
124 {"ctrlaltdel", CTRLALTDEL},
125 {"shutdown", SHUTDOWN},
126 {"restart", RESTART},
130 /* Set up a linked list of init_actions, to be read from inittab */
133 char command[INIT_BUFFS_SIZE];
134 char terminal[CONSOLE_BUFF_SIZE];
135 struct init_action *next;
139 /* Static variables */
140 static struct init_action *init_action_list = NULL;
141 static char console[CONSOLE_BUFF_SIZE] = _PATH_CONSOLE;
143 #ifndef CONFIG_SYSLOGD
144 static char *log_console = VC_5;
146 static sig_atomic_t got_cont = 0;
152 #if defined CONFIG_FEATURE_EXTRA_QUIET
155 MAYBE_CONSOLE = CONSOLE,
158 #ifndef RB_HALT_SYSTEM
159 RB_HALT_SYSTEM = 0xcdef0123,
160 RB_ENABLE_CAD = 0x89abcdef,
162 RB_POWER_OFF = 0x4321fedc,
163 RB_AUTOBOOT = 0x01234567,
167 static const char * const environment[] = {
169 "PATH=" _PATH_STDPATH,
175 /* Function prototypes */
176 static void delete_init_action(struct init_action *a);
177 static int waitfor(const struct init_action *a, pid_t pid);
178 static void shutdown_signal(int sig);
181 static void loop_forever(void)
187 /* Print a message to the specified device.
188 * Device may be bitwise-or'd from LOG | CONSOLE */
189 #if ENABLE_DEBUG_INIT
190 #define messageD message
192 static inline void messageD(int ATTRIBUTE_UNUSED device,
193 const char ATTRIBUTE_UNUSED *fmt, ...)
197 static void message(int device, const char *fmt, ...)
198 __attribute__ ((format(printf, 2, 3)));
199 static void message(int device, const char *fmt, ...)
203 RESERVE_CONFIG_BUFFER(msg, 1024);
204 #ifndef CONFIG_SYSLOGD
205 static int log_fd = -1;
209 va_start(arguments, fmt);
210 l = vsnprintf(msg + 1, 1024 - 2, fmt, arguments) + 1;
213 #ifdef CONFIG_SYSLOGD
214 /* Log the message to syslogd */
216 /* don`t out "\r\n" */
217 openlog(bb_applet_name, 0, LOG_DAEMON);
218 syslog(LOG_INFO, "%s", msg + 1);
228 /* Take full control of the log tty, and never close it.
229 * It's mine, all mine! Muhahahaha! */
231 if ((log_fd = device_open(log_console, O_RDWR | O_NONBLOCK | O_NOCTTY)) < 0) {
233 bb_error_msg("Bummer, can't write to log on %s!", log_console);
236 fcntl(log_fd, F_SETFD, FD_CLOEXEC);
239 if ((device & LOG) && (log_fd >= 0)) {
240 bb_full_write(log_fd, msg, l);
244 if (device & CONSOLE) {
245 int fd = device_open(_PATH_CONSOLE,
246 O_WRONLY | O_NOCTTY | O_NONBLOCK);
247 /* Always send console messages to /dev/console so people will see them. */
249 bb_full_write(fd, msg, l);
251 #if ENABLE_DEBUG_INIT
252 /* all descriptors may be closed */
254 bb_error_msg("Bummer, can't print: ");
255 va_start(arguments, fmt);
256 vfprintf(stderr, fmt, arguments);
261 RELEASE_CONFIG_BUFFER(msg);
264 /* Set terminal settings to reasonable defaults */
265 static void set_term(void)
269 tcgetattr(STDIN_FILENO, &tty);
271 /* set control chars */
272 tty.c_cc[VINTR] = 3; /* C-c */
273 tty.c_cc[VQUIT] = 28; /* C-\ */
274 tty.c_cc[VERASE] = 127; /* C-? */
275 tty.c_cc[VKILL] = 21; /* C-u */
276 tty.c_cc[VEOF] = 4; /* C-d */
277 tty.c_cc[VSTART] = 17; /* C-q */
278 tty.c_cc[VSTOP] = 19; /* C-s */
279 tty.c_cc[VSUSP] = 26; /* C-z */
281 /* use line dicipline 0 */
284 /* Make it be sane */
285 tty.c_cflag &= CBAUD | CBAUDEX | CSIZE | CSTOPB | PARENB | PARODD;
286 tty.c_cflag |= CREAD | HUPCL | CLOCAL;
290 tty.c_iflag = ICRNL | IXON | IXOFF;
293 tty.c_oflag = OPOST | ONLCR;
297 ISIG | ICANON | ECHO | ECHOE | ECHOK | ECHOCTL | ECHOKE | IEXTEN;
299 tcsetattr(STDIN_FILENO, TCSANOW, &tty);
302 static void console_init(void)
307 struct serial_struct sr;
310 if ((s = getenv("CONSOLE")) != NULL || (s = getenv("console")) != NULL) {
311 safe_strncpy(console, s, sizeof(console));
312 #if 0 /* #cpu(sparc) */
313 /* sparc kernel supports console=tty[ab] parameter which is also
314 * passed to init, so catch it here */
315 /* remap tty[ab] to /dev/ttyS[01] */
316 if (strcmp(s, "ttya") == 0)
317 safe_strncpy(console, SC_0, sizeof(console));
318 else if (strcmp(s, "ttyb") == 0)
319 safe_strncpy(console, SC_1, sizeof(console));
322 /* 2.2 kernels: identify the real console backend and try to use it */
323 if (ioctl(0, TIOCGSERIAL, &sr) == 0) {
324 /* this is a serial console */
325 snprintf(console, sizeof(console) - 1, SC_FORMAT, sr.line);
326 } else if (ioctl(0, VT_GETSTATE, &vt) == 0) {
327 /* this is linux virtual tty */
328 snprintf(console, sizeof(console) - 1, VC_FORMAT, vt.v_active);
330 safe_strncpy(console, _PATH_CONSOLE, sizeof(console));
335 while ((fd = open(console, O_RDONLY | O_NONBLOCK)) < 0 && tried < 2) {
336 /* Can't open selected console -- try
337 logical system console and VT_MASTER */
338 safe_strncpy(console, (tried == 0 ? _PATH_CONSOLE : CURRENT_VC),
343 /* Perhaps we should panic here? */
344 #ifndef CONFIG_SYSLOGD
347 safe_strncpy(console, bb_dev_null, sizeof(console));
350 /* check for serial console */
351 if (ioctl(fd, TIOCGSERIAL, &sr) == 0) {
352 /* Force the TERM setting to vt102 for serial console --
353 * if TERM is set to linux (the default) */
354 if (s == NULL || strcmp(s, "linux") == 0)
355 putenv("TERM=vt102");
356 #ifndef CONFIG_SYSLOGD
357 log_console = console;
361 putenv("TERM=linux");
365 messageD(LOG, "console=%s", console);
368 static void fixup_argv(int argc, char **argv, char *new_argv0)
372 /* Fix up argv[0] to be certain we claim to be init */
373 len = strlen(argv[0]);
374 memset(argv[0], 0, len);
375 safe_strncpy(argv[0], new_argv0, len + 1);
377 /* Wipe argv[1]-argv[N] so they don't clutter the ps listing */
380 memset(argv[len], 0, strlen(argv[len]));
385 /* Open the new terminal device */
386 static void open_new_terminal(const char *device, char fail) {
389 if ((device_open(device, O_RDWR)) < 0) {
390 if (stat(device, &sb) != 0) {
391 message(LOG | CONSOLE, "device '%s' does not exist.", device);
393 message(LOG | CONSOLE, "Bummer, can't open %s", device);
398 shutdown_signal(SIGUSR1);
402 static pid_t run(const struct init_action *a)
406 char *s, *tmpCmd, *cmd[INIT_BUFFS_SIZE], *cmdpath;
407 char buf[INIT_BUFFS_SIZE + 6]; /* INIT_BUFFS_SIZE+strlen("exec ")+1 */
408 sigset_t nmask, omask;
409 static const char press_enter[] =
410 #ifdef CUSTOMIZED_BANNER
411 #include CUSTOMIZED_BANNER
413 "\nPlease press Enter to activate this console. ";
415 /* Block sigchild while forking. */
417 sigaddset(&nmask, SIGCHLD);
418 sigprocmask(SIG_BLOCK, &nmask, &omask);
420 if ((pid = fork()) == 0) {
426 sigprocmask(SIG_SETMASK, &omask, NULL);
428 /* Reset signal handlers that were set by the parent process */
429 signal(SIGUSR1, SIG_DFL);
430 signal(SIGUSR2, SIG_DFL);
431 signal(SIGINT, SIG_DFL);
432 signal(SIGTERM, SIG_DFL);
433 signal(SIGHUP, SIG_DFL);
434 signal(SIGQUIT, SIG_DFL);
435 signal(SIGCONT, SIG_DFL);
436 signal(SIGSTOP, SIG_DFL);
437 signal(SIGTSTP, SIG_DFL);
439 /* Create a new session and make ourself the process
443 /* Open the new terminal device */
444 open_new_terminal(a->terminal, 1);
446 /* Make sure the terminal will act fairly normal for us */
448 /* Setup stdout, stderr for the new process so
449 * they point to the supplied terminal */
453 /* If the init Action requires us to wait, then force the
454 * supplied terminal to be the controlling tty. */
455 if (a->action & (SYSINIT | WAIT | CTRLALTDEL | SHUTDOWN | RESTART)) {
457 /* Now fork off another process to just hang around */
458 if ((pid = fork()) < 0) {
459 message(LOG | CONSOLE, "Can't fork!");
465 /* We are the parent -- wait till the child is done */
466 signal(SIGINT, SIG_IGN);
467 signal(SIGTSTP, SIG_IGN);
468 signal(SIGQUIT, SIG_IGN);
469 signal(SIGCHLD, SIG_DFL);
472 /* See if stealing the controlling tty back is necessary */
473 if (tcgetpgrp(0) != getpid())
476 /* Use a temporary process to steal the controlling tty. */
477 if ((pid = fork()) < 0) {
478 message(LOG | CONSOLE, "Can't fork!");
483 ioctl(0, TIOCSCTTY, 1);
490 /* Now fall though to actually execute things */
493 /* See if any special /bin/sh requiring characters are present */
494 if (strpbrk(a->command, "~`!$^&*()=|\\{}[];\"'<>?") != NULL) {
495 cmd[0] = (char *)DEFAULT_SHELL;
497 cmd[2] = strcat(strcpy(buf, "exec "), a->command);
500 /* Convert command (char*) into cmd (char**, one word per string) */
501 strcpy(buf, a->command);
503 for (tmpCmd = buf, i = 0; (tmpCmd = strsep(&s, " \t")) != NULL;) {
504 if (*tmpCmd != '\0') {
515 Interactive shells want to see a dash in argv[0]. This
516 typically is handled by login, argv will be setup this
517 way if a dash appears at the front of the command path
521 if (*cmdpath == '-') {
523 /* skip over the dash */
526 /* find the last component in the command pathname */
527 s = bb_get_last_path_component(cmdpath);
529 /* make a new argv[0] */
530 if ((cmd[0] = malloc(strlen(s) + 2)) == NULL) {
531 message(LOG | CONSOLE, bb_msg_memory_exhausted);
535 strcpy(cmd[0] + 1, s);
537 #ifdef CONFIG_FEATURE_INIT_SCTTY
538 /* Establish this process as session leader and
539 * (attempt) to make the tty (if any) a controlling tty.
542 (void) ioctl(0, TIOCSCTTY, 0/*don't steal it*/);
546 #if !defined(__UCLIBC__) || defined(__ARCH_HAS_MMU__)
547 if (a->action & ASKFIRST) {
550 * Save memory by not exec-ing anything large (like a shell)
551 * before the user wants it. This is critical if swap is not
552 * enabled and the system has low memory. Generally this will
553 * be run on the second virtual console, and the first will
554 * be allowed to start a shell or whatever an init script
557 messageD(LOG, "Waiting for enter to start '%s'"
558 "(pid %d, terminal %s)\n",
559 cmdpath, getpid(), a->terminal);
560 bb_full_write(1, press_enter, sizeof(press_enter) - 1);
561 while(read(0, &c, 1) == 1 && c != '\n')
566 /* Log the process name and args */
567 message(LOG, "Starting pid %d, console %s: '%s'",
568 getpid(), a->terminal, cmdpath);
570 #if defined CONFIG_FEATURE_INIT_COREDUMPS
573 if (stat(CORE_ENABLE_FLAG_FILE, &sb) == 0) {
576 limit.rlim_cur = RLIM_INFINITY;
577 limit.rlim_max = RLIM_INFINITY;
578 setrlimit(RLIMIT_CORE, &limit);
583 /* Now run it. The new program will take over this PID,
584 * so nothing further in init.c should be run. */
587 /* We're still here? Some error happened. */
588 message(LOG | CONSOLE, "Bummer, could not run '%s': %m", cmdpath);
591 sigprocmask(SIG_SETMASK, &omask, NULL);
595 static int waitfor(const struct init_action *a, pid_t pid)
600 runpid = (NULL == a)? pid : run(a);
602 wpid = waitpid(runpid,&status,0);
605 if (wpid == -1 && errno == ECHILD) {
606 /* we missed its termination */
609 /* FIXME other errors should maybe trigger an error, but allow
610 * the program to continue */
615 /* Run all commands of a particular type */
616 static void run_actions(int action)
618 struct init_action *a, *tmp;
620 for (a = init_action_list; a; a = tmp) {
622 if (a->action == action) {
623 if (access(a->terminal, R_OK | W_OK)) {
624 delete_init_action(a);
625 } else if (a->action & (SYSINIT | WAIT | CTRLALTDEL | SHUTDOWN | RESTART)) {
627 delete_init_action(a);
628 } else if (a->action & ONCE) {
630 delete_init_action(a);
631 } else if (a->action & (RESPAWN | ASKFIRST)) {
632 /* Only run stuff with pid==0. If they have
633 * a pid, that means it is still running */
642 #if !ENABLE_DEBUG_INIT
643 static void init_reboot(unsigned long magic)
646 /* We have to fork here, since the kernel calls do_exit(0) in
647 * linux/kernel/sys.c, which can cause the machine to panic when
648 * the init process is killed.... */
649 if ((pid = fork()) == 0) {
653 waitpid (pid, NULL, 0);
656 static void shutdown_system(void)
658 sigset_t block_signals;
660 /* run everything to be run at "shutdown". This is done _prior_
661 * to killing everything, in case people wish to use scripts to
662 * shut things down gracefully... */
663 run_actions(SHUTDOWN);
665 /* first disable all our signals */
666 sigemptyset(&block_signals);
667 sigaddset(&block_signals, SIGHUP);
668 sigaddset(&block_signals, SIGQUIT);
669 sigaddset(&block_signals, SIGCHLD);
670 sigaddset(&block_signals, SIGUSR1);
671 sigaddset(&block_signals, SIGUSR2);
672 sigaddset(&block_signals, SIGINT);
673 sigaddset(&block_signals, SIGTERM);
674 sigaddset(&block_signals, SIGCONT);
675 sigaddset(&block_signals, SIGSTOP);
676 sigaddset(&block_signals, SIGTSTP);
677 sigprocmask(SIG_BLOCK, &block_signals, NULL);
679 /* Allow Ctrl-Alt-Del to reboot system. */
680 init_reboot(RB_ENABLE_CAD);
682 message(CONSOLE | LOG, "The system is going down NOW !!");
685 /* Send signals to every process _except_ pid 1 */
686 message(CONSOLE | LOG, init_sending_format, "TERM");
691 message(CONSOLE | LOG, init_sending_format, "KILL");
698 static void exec_signal(int sig ATTRIBUTE_UNUSED)
700 struct init_action *a, *tmp;
701 sigset_t unblock_signals;
703 for (a = init_action_list; a; a = tmp) {
705 if (a->action & RESTART) {
708 /* unblock all signals, blocked in shutdown_system() */
709 sigemptyset(&unblock_signals);
710 sigaddset(&unblock_signals, SIGHUP);
711 sigaddset(&unblock_signals, SIGQUIT);
712 sigaddset(&unblock_signals, SIGCHLD);
713 sigaddset(&unblock_signals, SIGUSR1);
714 sigaddset(&unblock_signals, SIGUSR2);
715 sigaddset(&unblock_signals, SIGINT);
716 sigaddset(&unblock_signals, SIGTERM);
717 sigaddset(&unblock_signals, SIGCONT);
718 sigaddset(&unblock_signals, SIGSTOP);
719 sigaddset(&unblock_signals, SIGTSTP);
720 sigprocmask(SIG_UNBLOCK, &unblock_signals, NULL);
722 /* Close whatever files are open. */
727 /* Open the new terminal device */
728 open_new_terminal(a->terminal, 0);
730 /* Make sure the terminal will act fairly normal for us */
732 /* Setup stdout, stderr on the supplied terminal */
736 messageD(CONSOLE | LOG, "Trying to re-exec %s", a->command);
737 execl(a->command, a->command, NULL);
739 message(CONSOLE | LOG, "exec of '%s' failed: %m",
743 init_reboot(RB_HALT_SYSTEM);
749 static void shutdown_signal(int sig)
756 if (sig == SIGTERM) {
759 } else if (sig == SIGUSR2) {
766 message(CONSOLE | LOG, "Requesting system %s.", m);
769 /* allow time for last message to reach serial console */
777 static void ctrlaltdel_signal(int sig ATTRIBUTE_UNUSED)
779 run_actions(CTRLALTDEL);
782 /* The SIGSTOP & SIGTSTP handler */
783 static void stop_handler(int sig ATTRIBUTE_UNUSED)
785 int saved_errno = errno;
794 /* The SIGCONT handler */
795 static void cont_handler(int sig ATTRIBUTE_UNUSED)
800 #endif /* ! ENABLE_DEBUG_INIT */
802 static void new_init_action(int action, const char *command, const char *cons)
804 struct init_action *new_action, *a, *last;
809 if (strcmp(cons, bb_dev_null) == 0 && (action & ASKFIRST))
812 new_action = calloc((size_t) (1), sizeof(struct init_action));
814 message(LOG | CONSOLE, "Memory allocation failure");
818 /* Append to the end of the list */
819 for (a = last = init_action_list; a; a = a->next) {
820 /* don't enter action if it's already in the list,
821 * but do overwrite existing actions */
822 if ((strcmp(a->command, command) == 0) &&
823 (strcmp(a->terminal, cons) ==0)) {
831 last->next = new_action;
833 init_action_list = new_action;
835 strcpy(new_action->command, command);
836 new_action->action = action;
837 strcpy(new_action->terminal, cons);
838 #if 0 /* calloc zeroed always */
841 messageD(LOG|CONSOLE, "command='%s' action='%d' terminal='%s'\n",
842 new_action->command, new_action->action, new_action->terminal);
845 static void delete_init_action(struct init_action *action)
847 struct init_action *a, *b = NULL;
849 for (a = init_action_list; a; b = a, a = a->next) {
852 init_action_list = a->next;
862 /* NOTE that if CONFIG_FEATURE_USE_INITTAB is NOT defined,
863 * then parse_inittab() simply adds in some default
864 * actions(i.e., runs INIT_SCRIPT and then starts a pair
865 * of "askfirst" shells). If CONFIG_FEATURE_USE_INITTAB
866 * _is_ defined, but /etc/inittab is missing, this
867 * results in the same set of default behaviors.
869 static void parse_inittab(void)
871 #ifdef CONFIG_FEATURE_USE_INITTAB
873 char buf[INIT_BUFFS_SIZE], lineAsRead[INIT_BUFFS_SIZE];
874 char tmpConsole[CONSOLE_BUFF_SIZE];
875 char *id, *runlev, *action, *command, *eol;
876 const struct init_action_type *a = actions;
879 file = fopen(INITTAB, "r");
881 /* No inittab file -- set up some default behavior */
883 /* Reboot on Ctrl-Alt-Del */
884 new_init_action(CTRLALTDEL, "/sbin/reboot", "");
885 /* Umount all filesystems on halt/reboot */
886 new_init_action(SHUTDOWN, "/bin/umount -a -r", "");
887 /* Swapoff on halt/reboot */
888 if(ENABLE_SWAPONOFF) new_init_action(SHUTDOWN, "/sbin/swapoff -a", "");
889 /* Prepare to restart init when a HUP is received */
890 new_init_action(RESTART, "/sbin/init", "");
891 /* Askfirst shell on tty1-4 */
892 new_init_action(ASKFIRST, bb_default_login_shell, "");
893 new_init_action(ASKFIRST, bb_default_login_shell, VC_2);
894 new_init_action(ASKFIRST, bb_default_login_shell, VC_3);
895 new_init_action(ASKFIRST, bb_default_login_shell, VC_4);
897 new_init_action(SYSINIT, INIT_SCRIPT, "");
900 #ifdef CONFIG_FEATURE_USE_INITTAB
903 while (fgets(buf, INIT_BUFFS_SIZE, file) != NULL) {
904 /* Skip leading spaces */
905 for (id = buf; *id == ' ' || *id == '\t'; id++);
907 /* Skip the line if it's a comment */
908 if (*id == '#' || *id == '\n')
911 /* Trim the trailing \n */
912 eol = strrchr(id, '\n');
916 /* Keep a copy around for posterity's sake (and error msgs) */
917 strcpy(lineAsRead, buf);
919 /* Separate the ID field from the runlevels */
920 runlev = strchr(id, ':');
921 if (runlev == NULL || *(runlev + 1) == '\0') {
922 message(LOG | CONSOLE, "Bad inittab entry: %s", lineAsRead);
929 /* Separate the runlevels from the action */
930 action = strchr(runlev, ':');
931 if (action == NULL || *(action + 1) == '\0') {
932 message(LOG | CONSOLE, "Bad inittab entry: %s", lineAsRead);
939 /* Separate the action from the command */
940 command = strchr(action, ':');
941 if (command == NULL || *(command + 1) == '\0') {
942 message(LOG | CONSOLE, "Bad inittab entry: %s", lineAsRead);
949 /* Ok, now process it */
950 for (a = actions; a->name != 0; a++) {
951 if (strcmp(a->name, action) == 0) {
953 if(strncmp(id, "/dev/", 5) == 0)
955 strcpy(tmpConsole, "/dev/");
956 safe_strncpy(tmpConsole + 5, id,
957 CONSOLE_BUFF_SIZE - 5);
960 new_init_action(a->action, command, id);
965 /* Choke on an unknown action */
966 message(LOG | CONSOLE, "Bad inittab entry: %s", lineAsRead);
971 #endif /* CONFIG_FEATURE_USE_INITTAB */
974 #ifdef CONFIG_FEATURE_USE_INITTAB
975 static void reload_signal(int sig ATTRIBUTE_UNUSED)
977 struct init_action *a, *tmp;
979 message(LOG, "Reloading /etc/inittab");
981 /* disable old entrys */
982 for (a = init_action_list; a; a = a->next ) {
988 /* remove unused entrys */
989 for (a = init_action_list; a; a = tmp) {
991 if (a->action & (ONCE | SYSINIT | WAIT ) &&
993 delete_init_action(a);
996 run_actions(RESPAWN);
999 #endif /* CONFIG_FEATURE_USE_INITTAB */
1001 int init_main(int argc, char **argv)
1003 struct init_action *a;
1006 if (argc > 1 && !strcmp(argv[1], "-q")) {
1007 return kill(1,SIGHUP);
1009 #if !ENABLE_DEBUG_INIT
1010 /* Expect to be invoked as init with PID=1 or be invoked as linuxrc */
1011 if (getpid() != 1 &&
1012 (!ENABLE_FEATURE_INITRD || !strstr(bb_applet_name, "linuxrc")))
1016 /* Set up sig handlers -- be sure to
1017 * clear all of these in run() */
1018 signal(SIGHUP, exec_signal);
1019 signal(SIGQUIT, exec_signal);
1020 signal(SIGUSR1, shutdown_signal);
1021 signal(SIGUSR2, shutdown_signal);
1022 signal(SIGINT, ctrlaltdel_signal);
1023 signal(SIGTERM, shutdown_signal);
1024 signal(SIGCONT, cont_handler);
1025 signal(SIGSTOP, stop_handler);
1026 signal(SIGTSTP, stop_handler);
1028 /* Turn off rebooting via CTL-ALT-DEL -- we get a
1029 * SIGINT on CAD so we can shut things down gracefully... */
1030 init_reboot(RB_DISABLE_CAD);
1033 /* Figure out where the default console should be */
1036 /* Close whatever files are open, and reset the console. */
1041 if (device_open(console, O_RDWR | O_NOCTTY) == 0) {
1049 const char * const *e;
1050 /* Make sure environs is set to something sane */
1051 for(e = environment; *e; e++)
1052 putenv((char *) *e);
1055 message(MAYBE_CONSOLE | LOG, "init started: %s", bb_msg_full_version);
1057 /* Make sure there is enough memory to do something useful. */
1058 if (ENABLE_SWAPONOFF) {
1059 struct sysinfo info;
1061 if (!sysinfo(&info) &&
1062 (info.mem_unit ? : 1) * (long long)info.totalram < MEGABYTE)
1064 message(CONSOLE,"Low memory: forcing swapon.");
1065 /* swapon -a requires /proc typically */
1066 new_init_action(SYSINIT, "/bin/mount -t proc proc /proc", "");
1067 /* Try to turn on swap */
1068 new_init_action(SYSINIT, "/sbin/swapon -a", "");
1069 run_actions(SYSINIT); /* wait and removing */
1073 /* Check if we are supposed to be in single user mode */
1074 if (argc > 1 && (!strcmp(argv[1], "single") ||
1075 !strcmp(argv[1], "-s") || !strcmp(argv[1], "1"))) {
1076 /* Start a shell on console */
1077 new_init_action(RESPAWN, bb_default_login_shell, "");
1079 /* Not in single user mode -- see what inittab says */
1081 /* NOTE that if CONFIG_FEATURE_USE_INITTAB is NOT defined,
1082 * then parse_inittab() simply adds in some default
1083 * actions(i.e., runs INIT_SCRIPT and then starts a pair
1084 * of "askfirst" shells */
1088 #ifdef CONFIG_SELINUX
1089 if (getenv("SELINUX_INIT") == NULL) {
1092 putenv("SELINUX_INIT=YES");
1093 if (selinux_init_load_policy(&enforce) == 0) {
1094 execv(argv[0], argv);
1095 } else if (enforce > 0) {
1096 /* SELinux in enforcing mode but load_policy failed */
1097 /* At this point, we probably can't open /dev/console, so log() won't work */
1098 message(CONSOLE,"Unable to load SELinux Policy. Machine is in enforcing mode. Halting now.");
1102 #endif /* CONFIG_SELINUX */
1104 /* Make the command line just say "init" -- thats all, nothing else */
1105 fixup_argv(argc, argv, "init");
1107 /* Now run everything that needs to be run */
1109 /* First run the sysinit command */
1110 run_actions(SYSINIT);
1112 /* Next run anything that wants to block */
1115 /* Next run anything to be run only once */
1118 #ifdef CONFIG_FEATURE_USE_INITTAB
1119 /* Redefine SIGHUP to reread /etc/inittab */
1120 signal(SIGHUP, reload_signal);
1122 signal(SIGHUP, SIG_IGN);
1123 #endif /* CONFIG_FEATURE_USE_INITTAB */
1126 /* Now run the looping stuff for the rest of forever */
1128 /* run the respawn stuff */
1129 run_actions(RESPAWN);
1131 /* run the askfirst stuff */
1132 run_actions(ASKFIRST);
1134 /* Don't consume all CPU time -- sleep a bit */
1137 /* Wait for a child process to exit */
1140 /* Find out who died and clean up their corpse */
1141 for (a = init_action_list; a; a = a->next) {
1142 if (a->pid == wpid) {
1143 /* Set the pid to 0 so that the process gets
1144 * restarted by run_actions() */
1146 message(LOG, "Process '%s' (pid %d) exited. "
1147 "Scheduling it for restart.",
1151 /* see if anyone else is waiting to be reaped */
1152 wpid = waitpid (-1, NULL, WNOHANG);