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.
12 /* Turn this on to disable all the dangerous
13 rebooting stuff when debugging.
27 #include <sys/fcntl.h>
28 #include <sys/ioctl.h>
29 #include <sys/types.h>
31 #include <sys/reboot.h>
34 #include "init_shared.h"
38 # include <sys/syslog.h>
43 # include <selinux/selinux.h>
44 #endif /* CONFIG_SELINUX */
47 #define INIT_BUFFS_SIZE 256
49 /* From <linux/vt.h> */
51 unsigned short v_active; /* active vt */
52 unsigned short v_signal; /* signal to send */
53 unsigned short v_state; /* vt bitmask */
55 enum { VT_GETSTATE = 0x5603 }; /* get global vt state info */
57 /* From <linux/serial.h> */
58 struct serial_struct {
67 unsigned short close_delay;
69 char reserved_char[1];
71 unsigned short closing_wait; /* time to wait before closing */
72 unsigned short closing_wait2; /* no longer used... */
73 unsigned char *iomem_base;
74 unsigned short iomem_reg_shift;
75 unsigned int port_high;
76 unsigned long iomap_base; /* cookie passed into ioremap */
82 #define _PATH_STDPATH "/usr/bin:/bin:/usr/sbin:/sbin"
85 #if defined CONFIG_FEATURE_INIT_COREDUMPS
87 * When a file named CORE_ENABLE_FLAG_FILE exists, setrlimit is called
88 * before processes are spawned to set core file size as unlimited.
89 * This is for debugging only. Don't use this is production, unless
90 * you want core dumps lying about....
92 #define CORE_ENABLE_FLAG_FILE "/.init_enable_core"
93 #include <sys/resource.h>
97 #define KERNEL_VERSION(a,b,c) (((a) << 16) + ((b) << 8) + (c))
99 #define INITTAB "/etc/inittab" /* inittab file location */
101 #define INIT_SCRIPT "/etc/init.d/rcS" /* Default sysinit script. */
104 #define MAXENV 16 /* Number of env. vars */
106 #define CONSOLE_BUFF_SIZE 32
108 /* Allowed init action types */
109 #define SYSINIT 0x001
110 #define RESPAWN 0x002
111 #define ASKFIRST 0x004
114 #define CTRLALTDEL 0x020
115 #define SHUTDOWN 0x040
116 #define RESTART 0x080
118 /* A mapping between "inittab" action name strings and action type codes. */
119 struct init_action_type {
124 static const struct init_action_type actions[] = {
125 {"sysinit", SYSINIT},
126 {"respawn", RESPAWN},
127 {"askfirst", ASKFIRST},
130 {"ctrlaltdel", CTRLALTDEL},
131 {"shutdown", SHUTDOWN},
132 {"restart", RESTART},
136 /* Set up a linked list of init_actions, to be read from inittab */
139 char command[INIT_BUFFS_SIZE];
140 char terminal[CONSOLE_BUFF_SIZE];
141 struct init_action *next;
145 /* Static variables */
146 static struct init_action *init_action_list = NULL;
147 static char console[CONSOLE_BUFF_SIZE] = _PATH_CONSOLE;
149 #ifndef CONFIG_SYSLOGD
150 static char *log_console = VC_5;
152 static sig_atomic_t got_cont = 0;
158 #if defined CONFIG_FEATURE_EXTRA_QUIET
161 MAYBE_CONSOLE = CONSOLE,
164 #ifndef RB_HALT_SYSTEM
165 RB_HALT_SYSTEM = 0xcdef0123,
166 RB_ENABLE_CAD = 0x89abcdef,
168 RB_POWER_OFF = 0x4321fedc,
169 RB_AUTOBOOT = 0x01234567,
173 static const char * const environment[] = {
175 "PATH=" _PATH_STDPATH,
181 /* Function prototypes */
182 static void delete_init_action(struct init_action *a);
183 static int waitfor(const struct init_action *a);
184 static void halt_signal(int sig);
187 static void loop_forever(void)
193 /* Print a message to the specified device.
194 * Device may be bitwise-or'd from LOG | CONSOLE */
196 static inline void messageD(int ATTRIBUTE_UNUSED device,
197 const char ATTRIBUTE_UNUSED *fmt, ...)
201 #define messageD message
203 static void message(int device, const char *fmt, ...)
204 __attribute__ ((format(printf, 2, 3)));
205 static void message(int device, const char *fmt, ...)
210 #ifndef CONFIG_SYSLOGD
211 static int log_fd = -1;
215 va_start(arguments, fmt);
216 l = vsnprintf(msg + 1, sizeof(msg) - 2, fmt, arguments) + 1;
219 #ifdef CONFIG_SYSLOGD
220 /* Log the message to syslogd */
222 /* don`t out "\r\n" */
223 openlog(bb_applet_name, 0, LOG_DAEMON);
224 syslog(LOG_INFO, "%s", msg + 1);
234 /* Take full control of the log tty, and never close it.
235 * It's mine, all mine! Muhahahaha! */
237 if ((log_fd = device_open(log_console, O_RDWR | O_NONBLOCK | O_NOCTTY)) < 0) {
239 bb_error_msg("Bummer, can't write to log on %s!", log_console);
242 fcntl(log_fd, F_SETFD, FD_CLOEXEC);
245 if ((device & LOG) && (log_fd >= 0)) {
246 bb_full_write(log_fd, msg, l);
250 if (device & CONSOLE) {
251 int fd = device_open(_PATH_CONSOLE,
252 O_WRONLY | O_NOCTTY | O_NONBLOCK);
253 /* Always send console messages to /dev/console so people will see them. */
255 bb_full_write(fd, msg, l);
258 /* all descriptors may be closed */
260 bb_error_msg("Bummer, can't print: ");
261 va_start(arguments, fmt);
262 vfprintf(stderr, fmt, arguments);
269 /* Set terminal settings to reasonable defaults */
270 static void set_term(int fd)
276 /* set control chars */
277 tty.c_cc[VINTR] = 3; /* C-c */
278 tty.c_cc[VQUIT] = 28; /* C-\ */
279 tty.c_cc[VERASE] = 127; /* C-? */
280 tty.c_cc[VKILL] = 21; /* C-u */
281 tty.c_cc[VEOF] = 4; /* C-d */
282 tty.c_cc[VSTART] = 17; /* C-q */
283 tty.c_cc[VSTOP] = 19; /* C-s */
284 tty.c_cc[VSUSP] = 26; /* C-z */
286 /* use line dicipline 0 */
289 /* Make it be sane */
290 tty.c_cflag &= CBAUD | CBAUDEX | CSIZE | CSTOPB | PARENB | PARODD;
291 tty.c_cflag |= CREAD | HUPCL | CLOCAL;
295 tty.c_iflag = ICRNL | IXON | IXOFF;
298 tty.c_oflag = OPOST | ONLCR;
302 ISIG | ICANON | ECHO | ECHOE | ECHOK | ECHOCTL | ECHOKE | IEXTEN;
304 tcsetattr(fd, TCSANOW, &tty);
307 static void console_init(void)
312 struct serial_struct sr;
315 if ((s = getenv("CONSOLE")) != NULL || (s = getenv("console")) != NULL) {
316 safe_strncpy(console, s, sizeof(console));
318 /* sparc kernel supports console=tty[ab] parameter which is also
319 * passed to init, so catch it here */
320 /* remap tty[ab] to /dev/ttyS[01] */
321 if (strcmp(s, "ttya") == 0)
322 safe_strncpy(console, SC_0, sizeof(console));
323 else if (strcmp(s, "ttyb") == 0)
324 safe_strncpy(console, SC_1, sizeof(console));
327 /* 2.2 kernels: identify the real console backend and try to use it */
328 if (ioctl(0, TIOCGSERIAL, &sr) == 0) {
329 /* this is a serial console */
330 snprintf(console, sizeof(console) - 1, SC_FORMAT, sr.line);
331 } else if (ioctl(0, VT_GETSTATE, &vt) == 0) {
332 /* this is linux virtual tty */
333 snprintf(console, sizeof(console) - 1, VC_FORMAT, vt.v_active);
335 safe_strncpy(console, _PATH_CONSOLE, sizeof(console));
340 while ((fd = open(console, O_RDONLY | O_NONBLOCK)) < 0 && tried < 2) {
341 /* Can't open selected console -- try
342 logical system console and VT_MASTER */
343 safe_strncpy(console, (tried == 0 ? _PATH_CONSOLE : CURRENT_VC),
348 /* Perhaps we should panic here? */
349 #ifndef CONFIG_SYSLOGD
352 safe_strncpy(console, bb_dev_null, sizeof(console));
355 /* check for serial console */
356 if (ioctl(fd, TIOCGSERIAL, &sr) == 0) {
357 /* Force the TERM setting to vt102 for serial console --
358 * if TERM is set to linux (the default) */
359 if (s == NULL || strcmp(s, "linux") == 0)
360 putenv("TERM=vt102");
361 #ifndef CONFIG_SYSLOGD
362 log_console = console;
366 putenv("TERM=linux");
370 messageD(LOG, "console=%s", console);
373 static void fixup_argv(int argc, char **argv, char *new_argv0)
377 /* Fix up argv[0] to be certain we claim to be init */
378 len = strlen(argv[0]);
379 memset(argv[0], 0, len);
380 safe_strncpy(argv[0], new_argv0, len + 1);
382 /* Wipe argv[1]-argv[N] so they don't clutter the ps listing */
385 memset(argv[len], 0, strlen(argv[len]));
390 static pid_t run(const struct init_action *a)
394 char *s, *tmpCmd, *cmd[INIT_BUFFS_SIZE], *cmdpath;
395 char buf[INIT_BUFFS_SIZE + 6]; /* INIT_BUFFS_SIZE+strlen("exec ")+1 */
396 sigset_t nmask, omask;
397 static const char press_enter[] =
398 #ifdef CUSTOMIZED_BANNER
399 #include CUSTOMIZED_BANNER
401 "\nPlease press Enter to activate this console. ";
403 /* Block sigchild while forking. */
405 sigaddset(&nmask, SIGCHLD);
406 sigprocmask(SIG_BLOCK, &nmask, &omask);
408 if ((pid = fork()) == 0) {
415 sigprocmask(SIG_SETMASK, &omask, NULL);
417 /* Reset signal handlers that were set by the parent process */
418 signal(SIGUSR1, SIG_DFL);
419 signal(SIGUSR2, SIG_DFL);
420 signal(SIGINT, SIG_DFL);
421 signal(SIGTERM, SIG_DFL);
422 signal(SIGHUP, SIG_DFL);
423 signal(SIGQUIT, SIG_DFL);
424 signal(SIGCONT, SIG_DFL);
425 signal(SIGSTOP, SIG_DFL);
426 signal(SIGTSTP, SIG_DFL);
428 /* Create a new session and make ourself the process
432 /* Open the new terminal device */
433 if ((device_open(a->terminal, O_RDWR)) < 0) {
434 if (stat(a->terminal, &sb) != 0) {
435 message(LOG | CONSOLE, "device '%s' does not exist.", a->terminal);
437 message(LOG | CONSOLE, "Bummer, can't open %s", a->terminal);
442 /* Make sure the terminal will act fairly normal for us */
444 /* Setup stdout, stderr for the new process so
445 * they point to the supplied terminal */
449 /* If the init Action requires us to wait, then force the
450 * supplied terminal to be the controlling tty. */
451 if (a->action & (SYSINIT | WAIT | CTRLALTDEL | SHUTDOWN | RESTART)) {
454 /* Now fork off another process to just hang around */
455 if ((pid = fork()) < 0) {
456 message(LOG | CONSOLE, "Can't fork!");
462 /* We are the parent -- wait till the child is done */
463 signal(SIGINT, SIG_IGN);
464 signal(SIGTSTP, SIG_IGN);
465 signal(SIGQUIT, SIG_IGN);
466 signal(SIGCHLD, SIG_DFL);
468 /* Wait for child to exit */
469 while ((tmp_pid = waitpid(pid, &junk, 0)) != pid) {
470 if (tmp_pid == -1 && errno == ECHILD) {
473 /* FIXME handle other errors */
476 /* See if stealing the controlling tty back is necessary */
478 if (pgrp != getpid())
481 /* Use a temporary process to steal the controlling tty. */
482 if ((pid = fork()) < 0) {
483 message(LOG | CONSOLE, "Can't fork!");
488 ioctl(0, TIOCSCTTY, 1);
491 while ((tmp_pid = waitpid(pid, &junk, 0)) != pid) {
492 if (tmp_pid < 0 && errno == ECHILD)
498 /* Now fall though to actually execute things */
501 /* See if any special /bin/sh requiring characters are present */
502 if (strpbrk(a->command, "~`!$^&*()=|\\{}[];\"'<>?") != NULL) {
503 cmd[0] = (char *)DEFAULT_SHELL;
505 cmd[2] = strcat(strcpy(buf, "exec "), a->command);
508 /* Convert command (char*) into cmd (char**, one word per string) */
509 strcpy(buf, a->command);
511 for (tmpCmd = buf, i = 0; (tmpCmd = strsep(&s, " \t")) != NULL;) {
512 if (*tmpCmd != '\0') {
523 Interactive shells want to see a dash in argv[0]. This
524 typically is handled by login, argv will be setup this
525 way if a dash appears at the front of the command path
529 if (*cmdpath == '-') {
531 /* skip over the dash */
534 /* find the last component in the command pathname */
535 s = bb_get_last_path_component(cmdpath);
537 /* make a new argv[0] */
538 if ((cmd[0] = malloc(strlen(s) + 2)) == NULL) {
539 message(LOG | CONSOLE, bb_msg_memory_exhausted);
543 strcpy(cmd[0] + 1, s);
545 #ifdef CONFIG_FEATURE_INIT_SCTTY
546 /* Establish this process as session leader and
547 * (attempt) to make the tty (if any) a controlling tty.
550 (void) ioctl(0, TIOCSCTTY, 0/*don't steal it*/);
554 #if !defined(__UCLIBC__) || defined(__ARCH_HAS_MMU__)
555 if (a->action & ASKFIRST) {
558 * Save memory by not exec-ing anything large (like a shell)
559 * before the user wants it. This is critical if swap is not
560 * enabled and the system has low memory. Generally this will
561 * be run on the second virtual console, and the first will
562 * be allowed to start a shell or whatever an init script
565 messageD(LOG, "Waiting for enter to start '%s'"
566 "(pid %d, terminal %s)\n",
567 cmdpath, getpid(), a->terminal);
568 bb_full_write(1, press_enter, sizeof(press_enter) - 1);
569 while(read(0, &c, 1) == 1 && c != '\n')
574 /* Log the process name and args */
575 message(LOG, "Starting pid %d, console %s: '%s'",
576 getpid(), a->terminal, cmdpath);
578 #if defined CONFIG_FEATURE_INIT_COREDUMPS
579 if (stat(CORE_ENABLE_FLAG_FILE, &sb) == 0) {
582 limit.rlim_cur = RLIM_INFINITY;
583 limit.rlim_max = RLIM_INFINITY;
584 setrlimit(RLIMIT_CORE, &limit);
588 /* Now run it. The new program will take over this PID,
589 * so nothing further in init.c should be run. */
592 /* We're still here? Some error happened. */
593 message(LOG | CONSOLE, "Bummer, could not run '%s': %m", cmdpath);
596 sigprocmask(SIG_SETMASK, &omask, NULL);
600 static int waitfor(const struct init_action *a)
607 wpid = waitpid(pid,&status,0);
610 if (wpid == -1 && errno == ECHILD) {
611 /* we missed its termination */
614 /* FIXME other errors should maybe trigger an error, but allow
615 * the program to continue */
620 /* Run all commands of a particular type */
621 static void run_actions(int action)
623 struct init_action *a, *tmp;
625 for (a = init_action_list; a; a = tmp) {
627 if (a->action == action) {
628 if (access(a->terminal, R_OK | W_OK)) {
629 delete_init_action(a);
630 } else if (a->action & (SYSINIT | WAIT | CTRLALTDEL | SHUTDOWN | RESTART)) {
632 delete_init_action(a);
633 } else if (a->action & ONCE) {
635 delete_init_action(a);
636 } else if (a->action & (RESPAWN | ASKFIRST)) {
637 /* Only run stuff with pid==0. If they have
638 * a pid, that means it is still running */
648 static void init_reboot(unsigned long magic)
651 /* We have to fork here, since the kernel calls do_exit(0) in
652 * linux/kernel/sys.c, which can cause the machine to panic when
653 * the init process is killed.... */
654 if ((pid = fork()) == 0) {
658 waitpid (pid, NULL, 0);
661 static void shutdown_system(void)
663 sigset_t block_signals;
665 /* run everything to be run at "shutdown". This is done _prior_
666 * to killing everything, in case people wish to use scripts to
667 * shut things down gracefully... */
668 run_actions(SHUTDOWN);
670 /* first disable all our signals */
671 sigemptyset(&block_signals);
672 sigaddset(&block_signals, SIGHUP);
673 sigaddset(&block_signals, SIGQUIT);
674 sigaddset(&block_signals, SIGCHLD);
675 sigaddset(&block_signals, SIGUSR1);
676 sigaddset(&block_signals, SIGUSR2);
677 sigaddset(&block_signals, SIGINT);
678 sigaddset(&block_signals, SIGTERM);
679 sigaddset(&block_signals, SIGCONT);
680 sigaddset(&block_signals, SIGSTOP);
681 sigaddset(&block_signals, SIGTSTP);
682 sigprocmask(SIG_BLOCK, &block_signals, NULL);
684 /* Allow Ctrl-Alt-Del to reboot system. */
685 init_reboot(RB_ENABLE_CAD);
687 message(CONSOLE | LOG, "The system is going down NOW !!");
690 /* Send signals to every process _except_ pid 1 */
691 message(CONSOLE | LOG, "Sending SIGTERM to all processes.");
696 message(CONSOLE | LOG, "Sending SIGKILL to all processes.");
703 static void exec_signal(int sig ATTRIBUTE_UNUSED)
705 struct init_action *a, *tmp;
706 sigset_t unblock_signals;
708 for (a = init_action_list; a; a = tmp) {
710 if (a->action & RESTART) {
713 /* unblock all signals, blocked in shutdown_system() */
714 sigemptyset(&unblock_signals);
715 sigaddset(&unblock_signals, SIGHUP);
716 sigaddset(&unblock_signals, SIGQUIT);
717 sigaddset(&unblock_signals, SIGCHLD);
718 sigaddset(&unblock_signals, SIGUSR1);
719 sigaddset(&unblock_signals, SIGUSR2);
720 sigaddset(&unblock_signals, SIGINT);
721 sigaddset(&unblock_signals, SIGTERM);
722 sigaddset(&unblock_signals, SIGCONT);
723 sigaddset(&unblock_signals, SIGSTOP);
724 sigaddset(&unblock_signals, SIGTSTP);
725 sigprocmask(SIG_UNBLOCK, &unblock_signals, NULL);
727 /* Close whatever files are open. */
732 /* Open the new terminal device */
733 if ((device_open(a->terminal, O_RDWR)) < 0) {
735 if (stat(a->terminal, &sb) != 0) {
736 message(LOG | CONSOLE, "device '%s' does not exist.", a->terminal);
738 message(LOG | CONSOLE, "Bummer, can't open %s", a->terminal);
740 halt_signal(SIGUSR1);
743 /* Make sure the terminal will act fairly normal for us */
745 /* Setup stdout, stderr on the supplied terminal */
749 messageD(CONSOLE | LOG, "Trying to re-exec %s", a->command);
750 execl(a->command, a->command, NULL);
752 message(CONSOLE | LOG, "exec of '%s' failed: %m",
756 init_reboot(RB_HALT_SYSTEM);
762 static void halt_signal(int sig ATTRIBUTE_UNUSED)
765 message(CONSOLE | LOG, "The system is halted.");
768 /* allow time for last message to reach serial console */
772 init_reboot(RB_POWER_OFF);
774 init_reboot(RB_HALT_SYSTEM);
779 static void reboot_signal(int sig ATTRIBUTE_UNUSED)
782 message(CONSOLE | LOG, "Please stand by while rebooting the system.");
785 /* allow time for last message to reach serial console */
788 init_reboot(RB_AUTOBOOT);
793 static void ctrlaltdel_signal(int sig ATTRIBUTE_UNUSED)
795 run_actions(CTRLALTDEL);
798 /* The SIGSTOP & SIGTSTP handler */
799 static void stop_handler(int sig ATTRIBUTE_UNUSED)
801 int saved_errno = errno;
810 /* The SIGCONT handler */
811 static void cont_handler(int sig ATTRIBUTE_UNUSED)
816 #endif /* ! DEBUG_INIT */
818 static void new_init_action(int action, const char *command, const char *cons)
820 struct init_action *new_action, *a, *last;
825 if (strcmp(cons, bb_dev_null) == 0 && (action & ASKFIRST))
828 new_action = calloc((size_t) (1), sizeof(struct init_action));
830 message(LOG | CONSOLE, "Memory allocation failure");
834 /* Append to the end of the list */
835 for (a = last = init_action_list; a; a = a->next) {
836 /* don't enter action if it's already in the list,
837 * but do overwrite existing actions */
838 if ((strcmp(a->command, command) == 0) &&
839 (strcmp(a->terminal, cons) ==0)) {
847 last->next = new_action;
849 init_action_list = new_action;
851 strcpy(new_action->command, command);
852 new_action->action = action;
853 strcpy(new_action->terminal, cons);
854 #if 0 /* calloc zeroed always */
857 messageD(LOG|CONSOLE, "command='%s' action='%d' terminal='%s'\n",
858 new_action->command, new_action->action, new_action->terminal);
861 static void delete_init_action(struct init_action *action)
863 struct init_action *a, *b = NULL;
865 for (a = init_action_list; a; b = a, a = a->next) {
868 init_action_list = a->next;
878 /* NOTE that if CONFIG_FEATURE_USE_INITTAB is NOT defined,
879 * then parse_inittab() simply adds in some default
880 * actions(i.e., runs INIT_SCRIPT and then starts a pair
881 * of "askfirst" shells). If CONFIG_FEATURE_USE_INITTAB
882 * _is_ defined, but /etc/inittab is missing, this
883 * results in the same set of default behaviors.
885 static void parse_inittab(void)
887 #ifdef CONFIG_FEATURE_USE_INITTAB
889 char buf[INIT_BUFFS_SIZE], lineAsRead[INIT_BUFFS_SIZE];
890 char tmpConsole[CONSOLE_BUFF_SIZE];
891 char *id, *runlev, *action, *command, *eol;
892 const struct init_action_type *a = actions;
895 file = fopen(INITTAB, "r");
897 /* No inittab file -- set up some default behavior */
899 /* Reboot on Ctrl-Alt-Del */
900 new_init_action(CTRLALTDEL, "/sbin/reboot", "");
901 /* Umount all filesystems on halt/reboot */
902 new_init_action(SHUTDOWN, "/bin/umount -a -r", "");
903 /* Swapoff on halt/reboot */
904 if(ENABLE_SWAPONOFF) new_init_action(SHUTDOWN, "/sbin/swapoff -a", "");
905 /* Prepare to restart init when a HUP is received */
906 new_init_action(RESTART, "/sbin/init", "");
907 /* Askfirst shell on tty1-4 */
908 new_init_action(ASKFIRST, bb_default_login_shell, "");
909 new_init_action(ASKFIRST, bb_default_login_shell, VC_2);
910 new_init_action(ASKFIRST, bb_default_login_shell, VC_3);
911 new_init_action(ASKFIRST, bb_default_login_shell, VC_4);
913 new_init_action(SYSINIT, INIT_SCRIPT, "");
916 #ifdef CONFIG_FEATURE_USE_INITTAB
919 while (fgets(buf, INIT_BUFFS_SIZE, file) != NULL) {
920 /* Skip leading spaces */
921 for (id = buf; *id == ' ' || *id == '\t'; id++);
923 /* Skip the line if it's a comment */
924 if (*id == '#' || *id == '\n')
927 /* Trim the trailing \n */
928 eol = strrchr(id, '\n');
932 /* Keep a copy around for posterity's sake (and error msgs) */
933 strcpy(lineAsRead, buf);
935 /* Separate the ID field from the runlevels */
936 runlev = strchr(id, ':');
937 if (runlev == NULL || *(runlev + 1) == '\0') {
938 message(LOG | CONSOLE, "Bad inittab entry: %s", lineAsRead);
945 /* Separate the runlevels from the action */
946 action = strchr(runlev, ':');
947 if (action == NULL || *(action + 1) == '\0') {
948 message(LOG | CONSOLE, "Bad inittab entry: %s", lineAsRead);
955 /* Separate the action from the command */
956 command = strchr(action, ':');
957 if (command == NULL || *(command + 1) == '\0') {
958 message(LOG | CONSOLE, "Bad inittab entry: %s", lineAsRead);
965 /* Ok, now process it */
966 for (a = actions; a->name != 0; a++) {
967 if (strcmp(a->name, action) == 0) {
969 if(strncmp(id, "/dev/", 5) == 0)
971 strcpy(tmpConsole, "/dev/");
972 safe_strncpy(tmpConsole + 5, id,
973 CONSOLE_BUFF_SIZE - 5);
976 new_init_action(a->action, command, id);
981 /* Choke on an unknown action */
982 message(LOG | CONSOLE, "Bad inittab entry: %s", lineAsRead);
987 #endif /* CONFIG_FEATURE_USE_INITTAB */
990 #ifdef CONFIG_FEATURE_USE_INITTAB
991 static void reload_signal(int sig ATTRIBUTE_UNUSED)
993 struct init_action *a, *tmp;
995 message(LOG, "Reloading /etc/inittab");
997 /* disable old entrys */
998 for (a = init_action_list; a; a = a->next ) {
1004 /* remove unused entrys */
1005 for (a = init_action_list; a; a = tmp) {
1007 if (a->action & (ONCE | SYSINIT | WAIT ) &&
1009 delete_init_action(a);
1012 run_actions(RESPAWN);
1015 #endif /* CONFIG_FEATURE_USE_INITTAB */
1017 int init_main(int argc, char **argv)
1019 struct init_action *a;
1023 if (argc > 1 && !strcmp(argv[1], "-q")) {
1024 return kill(1,SIGHUP);
1027 /* Expect to be invoked as init with PID=1 or be invoked as linuxrc */
1028 if (getpid() != 1 &&
1029 (!ENABLE_FEATURE_INITRD || !strstr(bb_applet_name, "linuxrc")))
1033 /* Set up sig handlers -- be sure to
1034 * clear all of these in run() */
1035 signal(SIGHUP, exec_signal);
1036 signal(SIGQUIT, exec_signal);
1037 signal(SIGUSR1, halt_signal);
1038 signal(SIGUSR2, halt_signal);
1039 signal(SIGINT, ctrlaltdel_signal);
1040 signal(SIGTERM, reboot_signal);
1041 signal(SIGCONT, cont_handler);
1042 signal(SIGSTOP, stop_handler);
1043 signal(SIGTSTP, stop_handler);
1045 /* Turn off rebooting via CTL-ALT-DEL -- we get a
1046 * SIGINT on CAD so we can shut things down gracefully... */
1047 init_reboot(RB_DISABLE_CAD);
1050 /* Figure out where the default console should be */
1053 /* Close whatever files are open, and reset the console. */
1058 if (device_open(console, O_RDWR | O_NOCTTY) == 0) {
1066 const char * const *e;
1067 /* Make sure environs is set to something sane */
1068 for(e = environment; *e; e++)
1069 putenv((char *) *e);
1072 message(MAYBE_CONSOLE | LOG, "init started: %s", bb_msg_full_version);
1074 /* Make sure there is enough memory to do something useful. */
1075 if (ENABLE_SWAPONOFF) {
1076 struct sysinfo info;
1078 if (!sysinfo(&info) &&
1079 (info.mem_unit ? : 1) * (long long)info.totalram < MEGABYTE)
1081 message(CONSOLE,"Low memory: forcing swapon.");
1082 /* swapon -a requires /proc typically */
1083 new_init_action(SYSINIT, "/bin/mount -t proc proc /proc", "");
1084 /* Try to turn on swap */
1085 new_init_action(SYSINIT, "/sbin/swapon -a", "");
1086 run_actions(SYSINIT); /* wait and removing */
1090 /* Check if we are supposed to be in single user mode */
1091 if (argc > 1 && (!strcmp(argv[1], "single") ||
1092 !strcmp(argv[1], "-s") || !strcmp(argv[1], "1"))) {
1093 /* Start a shell on console */
1094 new_init_action(RESPAWN, bb_default_login_shell, "");
1096 /* Not in single user mode -- see what inittab says */
1098 /* NOTE that if CONFIG_FEATURE_USE_INITTAB is NOT defined,
1099 * then parse_inittab() simply adds in some default
1100 * actions(i.e., runs INIT_SCRIPT and then starts a pair
1101 * of "askfirst" shells */
1105 #ifdef CONFIG_SELINUX
1106 if (getenv("SELINUX_INIT") == NULL) {
1109 putenv("SELINUX_INIT=YES");
1110 if (selinux_init_load_policy(&enforce) == 0) {
1111 execv(argv[0], argv);
1112 } else if (enforce > 0) {
1113 /* SELinux in enforcing mode but load_policy failed */
1114 /* At this point, we probably can't open /dev/console, so log() won't work */
1115 message(CONSOLE,"Unable to load SELinux Policy. Machine is in enforcing mode. Halting now.");
1119 #endif /* CONFIG_SELINUX */
1121 /* Make the command line just say "init" -- thats all, nothing else */
1122 fixup_argv(argc, argv, "init");
1124 /* Now run everything that needs to be run */
1126 /* First run the sysinit command */
1127 run_actions(SYSINIT);
1129 /* Next run anything that wants to block */
1132 /* Next run anything to be run only once */
1135 #ifdef CONFIG_FEATURE_USE_INITTAB
1136 /* Redefine SIGHUP to reread /etc/inittab */
1137 signal(SIGHUP, reload_signal);
1139 signal(SIGHUP, SIG_IGN);
1140 #endif /* CONFIG_FEATURE_USE_INITTAB */
1143 /* Now run the looping stuff for the rest of forever */
1145 /* run the respawn stuff */
1146 run_actions(RESPAWN);
1148 /* run the askfirst stuff */
1149 run_actions(ASKFIRST);
1151 /* Don't consume all CPU time -- sleep a bit */
1154 /* Wait for a child process to exit */
1155 wpid = wait(&status);
1157 /* Find out who died and clean up their corpse */
1158 for (a = init_action_list; a; a = a->next) {
1159 if (a->pid == wpid) {
1160 /* Set the pid to 0 so that the process gets
1161 * restarted by run_actions() */
1163 message(LOG, "Process '%s' (pid %d) exited. "
1164 "Scheduling it for restart.",
1168 /* see if anyone else is waiting to be reaped */
1169 wpid = waitpid (-1, &status, WNOHANG);
1176 c-file-style: "linux"