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.
24 #include <sys/ioctl.h>
25 #include <sys/types.h>
27 #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] = CONSOLE_DEV;
143 #ifndef CONFIG_SYSLOGD
144 static char *log_console = VC_5;
146 #if !ENABLE_DEBUG_INIT
147 static sig_atomic_t got_cont = 0;
154 #if defined CONFIG_FEATURE_EXTRA_QUIET
157 MAYBE_CONSOLE = CONSOLE,
160 #ifndef RB_HALT_SYSTEM
161 RB_HALT_SYSTEM = 0xcdef0123, /* FIXME: this overflows enum */
162 RB_ENABLE_CAD = 0x89abcdef,
164 RB_POWER_OFF = 0x4321fedc,
165 RB_AUTOBOOT = 0x01234567,
169 static const char * const environment[] = {
171 "PATH=" _PATH_STDPATH,
177 /* Function prototypes */
178 static void delete_init_action(struct init_action *a);
179 static int waitfor(const struct init_action *a, pid_t pid);
180 #if !ENABLE_DEBUG_INIT
181 static void shutdown_signal(int sig);
184 static void loop_forever(void)
190 /* Print a message to the specified device.
191 * Device may be bitwise-or'd from LOG | CONSOLE */
192 #if ENABLE_DEBUG_INIT
193 #define messageD message
195 static inline void messageD(int ATTRIBUTE_UNUSED device,
196 const char ATTRIBUTE_UNUSED *fmt, ...)
200 static void message(int device, const char *fmt, ...)
201 __attribute__ ((format(printf, 2, 3)));
202 static void message(int device, const char *fmt, ...)
206 RESERVE_CONFIG_BUFFER(msg, 1024);
207 #ifndef CONFIG_SYSLOGD
208 static int log_fd = -1;
212 va_start(arguments, fmt);
213 l = vsnprintf(msg + 1, 1024 - 2, fmt, arguments) + 1;
216 #ifdef CONFIG_SYSLOGD
217 /* Log the message to syslogd */
219 /* don`t out "\r\n" */
220 openlog(bb_applet_name, 0, LOG_DAEMON);
221 syslog(LOG_INFO, "%s", msg + 1);
231 /* Take full control of the log tty, and never close it.
232 * It's mine, all mine! Muhahahaha! */
234 if ((log_fd = device_open(log_console, O_RDWR | O_NONBLOCK | O_NOCTTY)) < 0) {
236 bb_error_msg("Bummer, can't write to log on %s!", log_console);
239 fcntl(log_fd, F_SETFD, FD_CLOEXEC);
242 if ((device & LOG) && (log_fd >= 0)) {
243 bb_full_write(log_fd, msg, l);
247 if (device & CONSOLE) {
248 int fd = device_open(CONSOLE_DEV,
249 O_WRONLY | O_NOCTTY | O_NONBLOCK);
250 /* Always send console messages to /dev/console so people will see them. */
252 bb_full_write(fd, msg, l);
254 #if ENABLE_DEBUG_INIT
255 /* all descriptors may be closed */
257 bb_error_msg("Bummer, can't print: ");
258 va_start(arguments, fmt);
259 vfprintf(stderr, fmt, arguments);
264 RELEASE_CONFIG_BUFFER(msg);
267 /* Set terminal settings to reasonable defaults */
268 static void set_term(void)
272 tcgetattr(STDIN_FILENO, &tty);
274 /* set control chars */
275 tty.c_cc[VINTR] = 3; /* C-c */
276 tty.c_cc[VQUIT] = 28; /* C-\ */
277 tty.c_cc[VERASE] = 127; /* C-? */
278 tty.c_cc[VKILL] = 21; /* C-u */
279 tty.c_cc[VEOF] = 4; /* C-d */
280 tty.c_cc[VSTART] = 17; /* C-q */
281 tty.c_cc[VSTOP] = 19; /* C-s */
282 tty.c_cc[VSUSP] = 26; /* C-z */
284 /* use line dicipline 0 */
287 /* Make it be sane */
288 tty.c_cflag &= CBAUD | CBAUDEX | CSIZE | CSTOPB | PARENB | PARODD;
289 tty.c_cflag |= CREAD | HUPCL | CLOCAL;
293 tty.c_iflag = ICRNL | IXON | IXOFF;
296 tty.c_oflag = OPOST | ONLCR;
300 ISIG | ICANON | ECHO | ECHOE | ECHOK | ECHOCTL | ECHOKE | IEXTEN;
302 tcsetattr(STDIN_FILENO, TCSANOW, &tty);
305 static void console_init(void)
310 struct serial_struct sr;
313 if ((s = getenv("CONSOLE")) != NULL || (s = getenv("console")) != NULL) {
314 safe_strncpy(console, s, sizeof(console));
315 #if 0 /* #cpu(sparc) */
316 /* sparc kernel supports console=tty[ab] parameter which is also
317 * passed to init, so catch it here */
318 /* remap tty[ab] to /dev/ttyS[01] */
319 if (strcmp(s, "ttya") == 0)
320 safe_strncpy(console, SC_0, sizeof(console));
321 else if (strcmp(s, "ttyb") == 0)
322 safe_strncpy(console, SC_1, sizeof(console));
325 /* 2.2 kernels: identify the real console backend and try to use it */
326 if (ioctl(0, TIOCGSERIAL, &sr) == 0) {
327 /* this is a serial console */
328 snprintf(console, sizeof(console) - 1, SC_FORMAT, sr.line);
329 } else if (ioctl(0, VT_GETSTATE, &vt) == 0) {
330 /* this is linux virtual tty */
331 snprintf(console, sizeof(console) - 1, VC_FORMAT, vt.v_active);
333 safe_strncpy(console, CONSOLE_DEV, sizeof(console));
338 while ((fd = open(console, O_RDONLY | O_NONBLOCK)) < 0 && tried < 2) {
339 /* Can't open selected console -- try
340 logical system console and VT_MASTER */
341 safe_strncpy(console, (tried == 0 ? CONSOLE_DEV : CURRENT_VC),
346 /* Perhaps we should panic here? */
347 #ifndef CONFIG_SYSLOGD
350 safe_strncpy(console, bb_dev_null, sizeof(console));
353 /* check for serial console */
354 if (ioctl(fd, TIOCGSERIAL, &sr) == 0) {
355 /* Force the TERM setting to vt102 for serial console --
356 * if TERM is set to linux (the default) */
357 if (s == NULL || strcmp(s, "linux") == 0)
358 putenv("TERM=vt102");
359 #ifndef CONFIG_SYSLOGD
360 log_console = console;
364 putenv("TERM=linux");
368 messageD(LOG, "console=%s", console);
371 static void fixup_argv(int argc, char **argv, char *new_argv0)
375 /* Fix up argv[0] to be certain we claim to be init */
376 len = strlen(argv[0]);
377 memset(argv[0], 0, len);
378 safe_strncpy(argv[0], new_argv0, len + 1);
380 /* Wipe argv[1]-argv[N] so they don't clutter the ps listing */
383 memset(argv[len], 0, strlen(argv[len]));
388 /* Open the new terminal device */
389 static void open_new_terminal(const char * const device, const int fail) {
392 if ((device_open(device, O_RDWR)) < 0) {
393 if (stat(device, &sb) != 0) {
394 message(LOG | CONSOLE, "device '%s' does not exist.", device);
396 message(LOG | CONSOLE, "Bummer, can't open %s", device);
401 #if !ENABLE_DEBUG_INIT
402 shutdown_signal(SIGUSR1);
409 static pid_t run(const struct init_action *a)
413 char *s, *tmpCmd, *cmd[INIT_BUFFS_SIZE], *cmdpath;
414 char buf[INIT_BUFFS_SIZE + 6]; /* INIT_BUFFS_SIZE+strlen("exec ")+1 */
415 sigset_t nmask, omask;
416 static const char press_enter[] =
417 #ifdef CUSTOMIZED_BANNER
418 #include CUSTOMIZED_BANNER
420 "\nPlease press Enter to activate this console. ";
422 /* Block sigchild while forking. */
424 sigaddset(&nmask, SIGCHLD);
425 sigprocmask(SIG_BLOCK, &nmask, &omask);
427 if ((pid = fork()) == 0) {
433 sigprocmask(SIG_SETMASK, &omask, NULL);
435 /* Reset signal handlers that were set by the parent process */
436 signal(SIGUSR1, SIG_DFL);
437 signal(SIGUSR2, SIG_DFL);
438 signal(SIGINT, SIG_DFL);
439 signal(SIGTERM, SIG_DFL);
440 signal(SIGHUP, SIG_DFL);
441 signal(SIGQUIT, SIG_DFL);
442 signal(SIGCONT, SIG_DFL);
443 signal(SIGSTOP, SIG_DFL);
444 signal(SIGTSTP, SIG_DFL);
446 /* Create a new session and make ourself the process
450 /* Open the new terminal device */
451 open_new_terminal(a->terminal, 1);
453 /* Make sure the terminal will act fairly normal for us */
455 /* Setup stdout, stderr for the new process so
456 * they point to the supplied terminal */
460 /* If the init Action requires us to wait, then force the
461 * supplied terminal to be the controlling tty. */
462 if (a->action & (SYSINIT | WAIT | CTRLALTDEL | SHUTDOWN | RESTART)) {
464 /* Now fork off another process to just hang around */
465 if ((pid = fork()) < 0) {
466 message(LOG | CONSOLE, "Can't fork!");
472 /* We are the parent -- wait till the child is done */
473 signal(SIGINT, SIG_IGN);
474 signal(SIGTSTP, SIG_IGN);
475 signal(SIGQUIT, SIG_IGN);
476 signal(SIGCHLD, SIG_DFL);
479 /* See if stealing the controlling tty back is necessary */
480 if (tcgetpgrp(0) != getpid())
483 /* Use a temporary process to steal the controlling tty. */
484 if ((pid = fork()) < 0) {
485 message(LOG | CONSOLE, "Can't fork!");
490 ioctl(0, TIOCSCTTY, 1);
497 /* Now fall though to actually execute things */
500 /* See if any special /bin/sh requiring characters are present */
501 if (strpbrk(a->command, "~`!$^&*()=|\\{}[];\"'<>?") != NULL) {
502 cmd[0] = (char *)DEFAULT_SHELL;
504 cmd[2] = strcat(strcpy(buf, "exec "), a->command);
507 /* Convert command (char*) into cmd (char**, one word per string) */
508 strcpy(buf, a->command);
510 for (tmpCmd = buf, i = 0; (tmpCmd = strsep(&s, " \t")) != NULL;) {
511 if (*tmpCmd != '\0') {
522 Interactive shells want to see a dash in argv[0]. This
523 typically is handled by login, argv will be setup this
524 way if a dash appears at the front of the command path
528 if (*cmdpath == '-') {
530 /* skip over the dash */
533 /* find the last component in the command pathname */
534 s = bb_get_last_path_component(cmdpath);
536 /* make a new argv[0] */
537 if ((cmd[0] = malloc(strlen(s) + 2)) == NULL) {
538 message(LOG | CONSOLE, bb_msg_memory_exhausted);
542 strcpy(cmd[0] + 1, s);
544 #ifdef CONFIG_FEATURE_INIT_SCTTY
545 /* Establish this process as session leader and
546 * (attempt) to make the tty (if any) a controlling tty.
549 (void) ioctl(0, TIOCSCTTY, 0/*don't steal it*/);
553 #if !defined(__UCLIBC__) || defined(__ARCH_HAS_MMU__)
554 if (a->action & ASKFIRST) {
557 * Save memory by not exec-ing anything large (like a shell)
558 * before the user wants it. This is critical if swap is not
559 * enabled and the system has low memory. Generally this will
560 * be run on the second virtual console, and the first will
561 * be allowed to start a shell or whatever an init script
564 messageD(LOG, "Waiting for enter to start '%s'"
565 "(pid %d, terminal %s)\n",
566 cmdpath, getpid(), a->terminal);
567 bb_full_write(1, press_enter, sizeof(press_enter) - 1);
568 while(read(0, &c, 1) == 1 && c != '\n')
573 /* Log the process name and args */
574 message(LOG, "Starting pid %d, console %s: '%s'",
575 getpid(), a->terminal, cmdpath);
577 #if defined CONFIG_FEATURE_INIT_COREDUMPS
580 if (stat(CORE_ENABLE_FLAG_FILE, &sb) == 0) {
583 limit.rlim_cur = RLIM_INFINITY;
584 limit.rlim_max = RLIM_INFINITY;
585 setrlimit(RLIMIT_CORE, &limit);
590 /* Now run it. The new program will take over this PID,
591 * so nothing further in init.c should be run. */
594 /* We're still here? Some error happened. */
595 message(LOG | CONSOLE, "Bummer, could not run '%s': %m", cmdpath);
598 sigprocmask(SIG_SETMASK, &omask, NULL);
602 static int waitfor(const struct init_action *a, pid_t pid)
607 runpid = (NULL == a)? pid : run(a);
609 wpid = waitpid(runpid,&status,0);
612 if (wpid == -1 && errno == ECHILD) {
613 /* we missed its termination */
616 /* FIXME other errors should maybe trigger an error, but allow
617 * the program to continue */
622 /* Run all commands of a particular type */
623 static void run_actions(int action)
625 struct init_action *a, *tmp;
627 for (a = init_action_list; a; a = tmp) {
629 if (a->action == action) {
630 if (access(a->terminal, R_OK | W_OK)) {
631 delete_init_action(a);
632 } else if (a->action & (SYSINIT | WAIT | CTRLALTDEL | SHUTDOWN | RESTART)) {
634 delete_init_action(a);
635 } else if (a->action & ONCE) {
637 delete_init_action(a);
638 } else if (a->action & (RESPAWN | ASKFIRST)) {
639 /* Only run stuff with pid==0. If they have
640 * a pid, that means it is still running */
649 #if !ENABLE_DEBUG_INIT
650 static void init_reboot(unsigned long magic)
653 /* We have to fork here, since the kernel calls do_exit(0) in
654 * linux/kernel/sys.c, which can cause the machine to panic when
655 * the init process is killed.... */
656 if ((pid = fork()) == 0) {
660 waitpid (pid, NULL, 0);
663 static void shutdown_system(void)
665 sigset_t block_signals;
667 /* run everything to be run at "shutdown". This is done _prior_
668 * to killing everything, in case people wish to use scripts to
669 * shut things down gracefully... */
670 run_actions(SHUTDOWN);
672 /* first disable all our signals */
673 sigemptyset(&block_signals);
674 sigaddset(&block_signals, SIGHUP);
675 sigaddset(&block_signals, SIGQUIT);
676 sigaddset(&block_signals, SIGCHLD);
677 sigaddset(&block_signals, SIGUSR1);
678 sigaddset(&block_signals, SIGUSR2);
679 sigaddset(&block_signals, SIGINT);
680 sigaddset(&block_signals, SIGTERM);
681 sigaddset(&block_signals, SIGCONT);
682 sigaddset(&block_signals, SIGSTOP);
683 sigaddset(&block_signals, SIGTSTP);
684 sigprocmask(SIG_BLOCK, &block_signals, NULL);
686 /* Allow Ctrl-Alt-Del to reboot system. */
687 init_reboot(RB_ENABLE_CAD);
689 message(CONSOLE | LOG, "The system is going down NOW !!");
692 /* Send signals to every process _except_ pid 1 */
693 message(CONSOLE | LOG, init_sending_format, "TERM");
698 message(CONSOLE | LOG, init_sending_format, "KILL");
705 static void exec_signal(int sig ATTRIBUTE_UNUSED)
707 struct init_action *a, *tmp;
708 sigset_t unblock_signals;
710 for (a = init_action_list; a; a = tmp) {
712 if (a->action & RESTART) {
715 /* unblock all signals, blocked in shutdown_system() */
716 sigemptyset(&unblock_signals);
717 sigaddset(&unblock_signals, SIGHUP);
718 sigaddset(&unblock_signals, SIGQUIT);
719 sigaddset(&unblock_signals, SIGCHLD);
720 sigaddset(&unblock_signals, SIGUSR1);
721 sigaddset(&unblock_signals, SIGUSR2);
722 sigaddset(&unblock_signals, SIGINT);
723 sigaddset(&unblock_signals, SIGTERM);
724 sigaddset(&unblock_signals, SIGCONT);
725 sigaddset(&unblock_signals, SIGSTOP);
726 sigaddset(&unblock_signals, SIGTSTP);
727 sigprocmask(SIG_UNBLOCK, &unblock_signals, NULL);
729 /* Close whatever files are open. */
734 /* Open the new terminal device */
735 open_new_terminal(a->terminal, 0);
737 /* Make sure the terminal will act fairly normal for us */
739 /* Setup stdout, stderr on the supplied terminal */
743 messageD(CONSOLE | LOG, "Trying to re-exec %s", a->command);
744 execl(a->command, a->command, NULL);
746 message(CONSOLE | LOG, "exec of '%s' failed: %m",
750 init_reboot(RB_HALT_SYSTEM);
756 static void shutdown_signal(int sig)
763 if (sig == SIGTERM) {
766 } else if (sig == SIGUSR2) {
773 message(CONSOLE | LOG, "Requesting system %s.", m);
776 /* allow time for last message to reach serial console */
784 static void ctrlaltdel_signal(int sig ATTRIBUTE_UNUSED)
786 run_actions(CTRLALTDEL);
789 /* The SIGSTOP & SIGTSTP handler */
790 static void stop_handler(int sig ATTRIBUTE_UNUSED)
792 int saved_errno = errno;
801 /* The SIGCONT handler */
802 static void cont_handler(int sig ATTRIBUTE_UNUSED)
807 #endif /* ! ENABLE_DEBUG_INIT */
809 static void new_init_action(int action, const char *command, const char *cons)
811 struct init_action *new_action, *a, *last;
816 if (strcmp(cons, bb_dev_null) == 0 && (action & ASKFIRST))
819 new_action = calloc((size_t) (1), sizeof(struct init_action));
821 message(LOG | CONSOLE, "Memory allocation failure");
825 /* Append to the end of the list */
826 for (a = last = init_action_list; a; a = a->next) {
827 /* don't enter action if it's already in the list,
828 * but do overwrite existing actions */
829 if ((strcmp(a->command, command) == 0) &&
830 (strcmp(a->terminal, cons) ==0)) {
838 last->next = new_action;
840 init_action_list = new_action;
842 strcpy(new_action->command, command);
843 new_action->action = action;
844 strcpy(new_action->terminal, cons);
845 #if 0 /* calloc zeroed always */
848 messageD(LOG|CONSOLE, "command='%s' action='%d' terminal='%s'\n",
849 new_action->command, new_action->action, new_action->terminal);
852 static void delete_init_action(struct init_action *action)
854 struct init_action *a, *b = NULL;
856 for (a = init_action_list; a; b = a, a = a->next) {
859 init_action_list = a->next;
869 /* NOTE that if CONFIG_FEATURE_USE_INITTAB is NOT defined,
870 * then parse_inittab() simply adds in some default
871 * actions(i.e., runs INIT_SCRIPT and then starts a pair
872 * of "askfirst" shells). If CONFIG_FEATURE_USE_INITTAB
873 * _is_ defined, but /etc/inittab is missing, this
874 * results in the same set of default behaviors.
876 static void parse_inittab(void)
878 #ifdef CONFIG_FEATURE_USE_INITTAB
880 char buf[INIT_BUFFS_SIZE], lineAsRead[INIT_BUFFS_SIZE];
881 char tmpConsole[CONSOLE_BUFF_SIZE];
882 char *id, *runlev, *action, *command, *eol;
883 const struct init_action_type *a = actions;
886 file = fopen(INITTAB, "r");
888 /* No inittab file -- set up some default behavior */
890 /* Reboot on Ctrl-Alt-Del */
891 new_init_action(CTRLALTDEL, "/sbin/reboot", "");
892 /* Umount all filesystems on halt/reboot */
893 new_init_action(SHUTDOWN, "/bin/umount -a -r", "");
894 /* Swapoff on halt/reboot */
895 if(ENABLE_SWAPONOFF) new_init_action(SHUTDOWN, "/sbin/swapoff -a", "");
896 /* Prepare to restart init when a HUP is received */
897 new_init_action(RESTART, "/sbin/init", "");
898 /* Askfirst shell on tty1-4 */
899 new_init_action(ASKFIRST, bb_default_login_shell, "");
900 new_init_action(ASKFIRST, bb_default_login_shell, VC_2);
901 new_init_action(ASKFIRST, bb_default_login_shell, VC_3);
902 new_init_action(ASKFIRST, bb_default_login_shell, VC_4);
904 new_init_action(SYSINIT, INIT_SCRIPT, "");
907 #ifdef CONFIG_FEATURE_USE_INITTAB
910 while (fgets(buf, INIT_BUFFS_SIZE, file) != NULL) {
911 /* Skip leading spaces */
912 for (id = buf; *id == ' ' || *id == '\t'; id++);
914 /* Skip the line if it's a comment */
915 if (*id == '#' || *id == '\n')
918 /* Trim the trailing \n */
919 eol = strrchr(id, '\n');
923 /* Keep a copy around for posterity's sake (and error msgs) */
924 strcpy(lineAsRead, buf);
926 /* Separate the ID field from the runlevels */
927 runlev = strchr(id, ':');
928 if (runlev == NULL || *(runlev + 1) == '\0') {
929 message(LOG | CONSOLE, "Bad inittab entry: %s", lineAsRead);
936 /* Separate the runlevels from the action */
937 action = strchr(runlev, ':');
938 if (action == NULL || *(action + 1) == '\0') {
939 message(LOG | CONSOLE, "Bad inittab entry: %s", lineAsRead);
946 /* Separate the action from the command */
947 command = strchr(action, ':');
948 if (command == NULL || *(command + 1) == '\0') {
949 message(LOG | CONSOLE, "Bad inittab entry: %s", lineAsRead);
956 /* Ok, now process it */
957 for (a = actions; a->name != 0; a++) {
958 if (strcmp(a->name, action) == 0) {
960 if(strncmp(id, "/dev/", 5) == 0)
962 strcpy(tmpConsole, "/dev/");
963 safe_strncpy(tmpConsole + 5, id,
964 CONSOLE_BUFF_SIZE - 5);
967 new_init_action(a->action, command, id);
972 /* Choke on an unknown action */
973 message(LOG | CONSOLE, "Bad inittab entry: %s", lineAsRead);
978 #endif /* CONFIG_FEATURE_USE_INITTAB */
981 #ifdef CONFIG_FEATURE_USE_INITTAB
982 static void reload_signal(int sig ATTRIBUTE_UNUSED)
984 struct init_action *a, *tmp;
986 message(LOG, "Reloading /etc/inittab");
988 /* disable old entrys */
989 for (a = init_action_list; a; a = a->next ) {
995 /* remove unused entrys */
996 for (a = init_action_list; a; a = tmp) {
998 if (a->action & (ONCE | SYSINIT | WAIT ) &&
1000 delete_init_action(a);
1003 run_actions(RESPAWN);
1006 #endif /* CONFIG_FEATURE_USE_INITTAB */
1008 int init_main(int argc, char **argv)
1010 struct init_action *a;
1013 if (argc > 1 && !strcmp(argv[1], "-q")) {
1014 return kill(1,SIGHUP);
1016 #if !ENABLE_DEBUG_INIT
1017 /* Expect to be invoked as init with PID=1 or be invoked as linuxrc */
1018 if (getpid() != 1 &&
1019 (!ENABLE_FEATURE_INITRD || !strstr(bb_applet_name, "linuxrc")))
1023 /* Set up sig handlers -- be sure to
1024 * clear all of these in run() */
1025 signal(SIGHUP, exec_signal);
1026 signal(SIGQUIT, exec_signal);
1027 signal(SIGUSR1, shutdown_signal);
1028 signal(SIGUSR2, shutdown_signal);
1029 signal(SIGINT, ctrlaltdel_signal);
1030 signal(SIGTERM, shutdown_signal);
1031 signal(SIGCONT, cont_handler);
1032 signal(SIGSTOP, stop_handler);
1033 signal(SIGTSTP, stop_handler);
1035 /* Turn off rebooting via CTL-ALT-DEL -- we get a
1036 * SIGINT on CAD so we can shut things down gracefully... */
1037 init_reboot(RB_DISABLE_CAD);
1040 /* Figure out where the default console should be */
1043 /* Close whatever files are open, and reset the console. */
1048 if (device_open(console, O_RDWR | O_NOCTTY) == 0) {
1056 const char * const *e;
1057 /* Make sure environs is set to something sane */
1058 for(e = environment; *e; e++)
1059 putenv((char *) *e);
1062 message(MAYBE_CONSOLE | LOG, "init started: %s", bb_msg_full_version);
1064 /* Make sure there is enough memory to do something useful. */
1065 if (ENABLE_SWAPONOFF) {
1066 struct sysinfo info;
1068 if (!sysinfo(&info) &&
1069 (info.mem_unit ? : 1) * (long long)info.totalram < MEGABYTE)
1071 message(CONSOLE,"Low memory: forcing swapon.");
1072 /* swapon -a requires /proc typically */
1073 new_init_action(SYSINIT, "/bin/mount -t proc proc /proc", "");
1074 /* Try to turn on swap */
1075 new_init_action(SYSINIT, "/sbin/swapon -a", "");
1076 run_actions(SYSINIT); /* wait and removing */
1080 /* Check if we are supposed to be in single user mode */
1081 if (argc > 1 && (!strcmp(argv[1], "single") ||
1082 !strcmp(argv[1], "-s") || !strcmp(argv[1], "1"))) {
1083 /* Start a shell on console */
1084 new_init_action(RESPAWN, bb_default_login_shell, "");
1086 /* Not in single user mode -- see what inittab says */
1088 /* NOTE that if CONFIG_FEATURE_USE_INITTAB is NOT defined,
1089 * then parse_inittab() simply adds in some default
1090 * actions(i.e., runs INIT_SCRIPT and then starts a pair
1091 * of "askfirst" shells */
1095 #ifdef CONFIG_SELINUX
1096 if (getenv("SELINUX_INIT") == NULL) {
1099 putenv("SELINUX_INIT=YES");
1100 if (selinux_init_load_policy(&enforce) == 0) {
1101 execv(argv[0], argv);
1102 } else if (enforce > 0) {
1103 /* SELinux in enforcing mode but load_policy failed */
1104 /* At this point, we probably can't open /dev/console, so log() won't work */
1105 message(CONSOLE,"Unable to load SELinux Policy. Machine is in enforcing mode. Halting now.");
1109 #endif /* CONFIG_SELINUX */
1111 /* Make the command line just say "init" -- thats all, nothing else */
1112 fixup_argv(argc, argv, "init");
1114 /* Now run everything that needs to be run */
1116 /* First run the sysinit command */
1117 run_actions(SYSINIT);
1119 /* Next run anything that wants to block */
1122 /* Next run anything to be run only once */
1125 #ifdef CONFIG_FEATURE_USE_INITTAB
1126 /* Redefine SIGHUP to reread /etc/inittab */
1127 signal(SIGHUP, reload_signal);
1129 signal(SIGHUP, SIG_IGN);
1130 #endif /* CONFIG_FEATURE_USE_INITTAB */
1133 /* Now run the looping stuff for the rest of forever */
1135 /* run the respawn stuff */
1136 run_actions(RESPAWN);
1138 /* run the askfirst stuff */
1139 run_actions(ASKFIRST);
1141 /* Don't consume all CPU time -- sleep a bit */
1144 /* Wait for a child process to exit */
1147 /* Find out who died and clean up their corpse */
1148 for (a = init_action_list; a; a = a->next) {
1149 if (a->pid == wpid) {
1150 /* Set the pid to 0 so that the process gets
1151 * restarted by run_actions() */
1153 message(LOG, "Process '%s' (pid %d) exited. "
1154 "Scheduling it for restart.",
1158 /* see if anyone else is waiting to be reaped */
1159 wpid = waitpid (-1, NULL, WNOHANG);