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>
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 static const int 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>
92 #define KERNEL_VERSION(a,b,c) (((a) << 16) + ((b) << 8) + (c))
94 #define INITTAB "/etc/inittab" /* inittab file location */
96 #define INIT_SCRIPT "/etc/init.d/rcS" /* Default sysinit script. */
99 #define MAXENV 16 /* Number of env. vars */
101 #define CONSOLE_BUFF_SIZE 32
103 /* Allowed init action types */
104 #define SYSINIT 0x001
105 #define RESPAWN 0x002
106 #define ASKFIRST 0x004
109 #define CTRLALTDEL 0x020
110 #define SHUTDOWN 0x040
111 #define RESTART 0x080
113 /* A mapping between "inittab" action name strings and action type codes. */
114 struct init_action_type {
119 static const struct init_action_type actions[] = {
120 {"sysinit", SYSINIT},
121 {"respawn", RESPAWN},
122 {"askfirst", ASKFIRST},
125 {"ctrlaltdel", CTRLALTDEL},
126 {"shutdown", SHUTDOWN},
127 {"restart", RESTART},
131 /* Set up a linked list of init_actions, to be read from inittab */
134 char command[INIT_BUFFS_SIZE];
135 char terminal[CONSOLE_BUFF_SIZE];
136 struct init_action *next;
140 /* Static variables */
141 static struct init_action *init_action_list = NULL;
142 static char console[CONSOLE_BUFF_SIZE] = _PATH_CONSOLE;
144 #ifndef CONFIG_SYSLOGD
145 static char *log_console = VC_5;
147 static sig_atomic_t got_cont = 0;
148 static const int LOG = 0x1;
149 static const int CONSOLE = 0x2;
151 #if defined CONFIG_FEATURE_EXTRA_QUIET
152 static const int MAYBE_CONSOLE = 0x0;
154 #define MAYBE_CONSOLE CONSOLE
156 #ifndef RB_HALT_SYSTEM
157 static const int RB_HALT_SYSTEM = 0xcdef0123;
158 static const int RB_ENABLE_CAD = 0x89abcdef;
159 static const int RB_DISABLE_CAD = 0;
161 #define RB_POWER_OFF 0x4321fedc
162 static const int RB_AUTOBOOT = 0x01234567;
165 static const char * const environment[] = {
167 "PATH=" _PATH_STDPATH,
173 /* Function prototypes */
174 static void delete_init_action(struct init_action *a);
175 static int waitfor(const struct init_action *a);
176 static void halt_signal(int sig);
179 static void loop_forever(void)
185 /* Print a message to the specified device.
186 * Device may be bitwise-or'd from LOG | CONSOLE */
188 static inline void messageD(int ATTRIBUTE_UNUSED device,
189 const char ATTRIBUTE_UNUSED *fmt, ...)
193 #define messageD message
195 static void message(int device, const char *fmt, ...)
196 __attribute__ ((format(printf, 2, 3)));
197 static void message(int device, const char *fmt, ...)
202 #ifndef CONFIG_SYSLOGD
203 static int log_fd = -1;
207 va_start(arguments, fmt);
208 l = vsnprintf(msg + 1, sizeof(msg) - 2, fmt, arguments) + 1;
211 #ifdef CONFIG_SYSLOGD
212 /* Log the message to syslogd */
214 /* don`t out "\r\n" */
215 openlog(bb_applet_name, 0, LOG_DAEMON);
216 syslog(LOG_INFO, "%s", msg + 1);
226 /* Take full control of the log tty, and never close it.
227 * It's mine, all mine! Muhahahaha! */
229 if ((log_fd = device_open(log_console, O_RDWR | O_NONBLOCK | O_NOCTTY)) < 0) {
231 bb_error_msg("Bummer, can't write to log on %s!", log_console);
234 fcntl(log_fd, F_SETFD, FD_CLOEXEC);
237 if ((device & LOG) && (log_fd >= 0)) {
238 bb_full_write(log_fd, msg, l);
242 if (device & CONSOLE) {
243 int fd = device_open(_PATH_CONSOLE,
244 O_WRONLY | O_NOCTTY | O_NONBLOCK);
245 /* Always send console messages to /dev/console so people will see them. */
247 bb_full_write(fd, msg, l);
250 /* all descriptors may be closed */
252 bb_error_msg("Bummer, can't print: ");
253 va_start(arguments, fmt);
254 vfprintf(stderr, fmt, arguments);
261 /* Set terminal settings to reasonable defaults */
262 static void set_term(int fd)
268 /* set control chars */
269 tty.c_cc[VINTR] = 3; /* C-c */
270 tty.c_cc[VQUIT] = 28; /* C-\ */
271 tty.c_cc[VERASE] = 127; /* C-? */
272 tty.c_cc[VKILL] = 21; /* C-u */
273 tty.c_cc[VEOF] = 4; /* C-d */
274 tty.c_cc[VSTART] = 17; /* C-q */
275 tty.c_cc[VSTOP] = 19; /* C-s */
276 tty.c_cc[VSUSP] = 26; /* C-z */
278 /* use line dicipline 0 */
281 /* Make it be sane */
282 tty.c_cflag &= CBAUD | CBAUDEX | CSIZE | CSTOPB | PARENB | PARODD;
283 tty.c_cflag |= CREAD | HUPCL | CLOCAL;
287 tty.c_iflag = ICRNL | IXON | IXOFF;
290 tty.c_oflag = OPOST | ONLCR;
294 ISIG | ICANON | ECHO | ECHOE | ECHOK | ECHOCTL | ECHOKE | IEXTEN;
296 tcsetattr(fd, TCSANOW, &tty);
299 static void console_init(void)
304 struct serial_struct sr;
307 if ((s = getenv("CONSOLE")) != NULL || (s = getenv("console")) != NULL) {
308 safe_strncpy(console, s, sizeof(console));
310 /* sparc kernel supports console=tty[ab] parameter which is also
311 * passed to init, so catch it here */
312 /* remap tty[ab] to /dev/ttyS[01] */
313 if (strcmp(s, "ttya") == 0)
314 safe_strncpy(console, SC_0, sizeof(console));
315 else if (strcmp(s, "ttyb") == 0)
316 safe_strncpy(console, SC_1, sizeof(console));
319 /* 2.2 kernels: identify the real console backend and try to use it */
320 if (ioctl(0, TIOCGSERIAL, &sr) == 0) {
321 /* this is a serial console */
322 snprintf(console, sizeof(console) - 1, SC_FORMAT, sr.line);
323 } else if (ioctl(0, VT_GETSTATE, &vt) == 0) {
324 /* this is linux virtual tty */
325 snprintf(console, sizeof(console) - 1, VC_FORMAT, vt.v_active);
327 safe_strncpy(console, _PATH_CONSOLE, sizeof(console));
332 while ((fd = open(console, O_RDONLY | O_NONBLOCK)) < 0 && tried < 2) {
333 /* Can't open selected console -- try
334 logical system console and VT_MASTER */
335 safe_strncpy(console, (tried == 0 ? _PATH_CONSOLE : CURRENT_VC),
340 /* Perhaps we should panic here? */
341 #ifndef CONFIG_SYSLOGD
344 safe_strncpy(console, bb_dev_null, sizeof(console));
347 /* check for serial console */
348 if (ioctl(fd, TIOCGSERIAL, &sr) == 0) {
349 /* Force the TERM setting to vt102 for serial console --
350 * if TERM is set to linux (the default) */
351 if (s == NULL || strcmp(s, "linux") == 0)
352 putenv("TERM=vt102");
353 #ifndef CONFIG_SYSLOGD
354 log_console = console;
358 putenv("TERM=linux");
362 messageD(LOG, "console=%s", console);
365 static void fixup_argv(int argc, char **argv, char *new_argv0)
369 /* Fix up argv[0] to be certain we claim to be init */
370 len = strlen(argv[0]);
371 memset(argv[0], 0, len);
372 safe_strncpy(argv[0], new_argv0, len + 1);
374 /* Wipe argv[1]-argv[N] so they don't clutter the ps listing */
377 memset(argv[len], 0, strlen(argv[len]));
382 static pid_t run(const struct init_action *a)
386 char *s, *tmpCmd, *cmd[INIT_BUFFS_SIZE], *cmdpath;
387 char buf[INIT_BUFFS_SIZE + 6]; /* INIT_BUFFS_SIZE+strlen("exec ")+1 */
388 sigset_t nmask, omask;
389 static const char press_enter[] =
390 #ifdef CUSTOMIZED_BANNER
391 #include CUSTOMIZED_BANNER
393 "\nPlease press Enter to activate this console. ";
395 /* Block sigchild while forking. */
397 sigaddset(&nmask, SIGCHLD);
398 sigprocmask(SIG_BLOCK, &nmask, &omask);
400 if ((pid = fork()) == 0) {
407 sigprocmask(SIG_SETMASK, &omask, NULL);
409 /* Reset signal handlers that were set by the parent process */
410 signal(SIGUSR1, SIG_DFL);
411 signal(SIGUSR2, SIG_DFL);
412 signal(SIGINT, SIG_DFL);
413 signal(SIGTERM, SIG_DFL);
414 signal(SIGHUP, SIG_DFL);
415 signal(SIGQUIT, SIG_DFL);
416 signal(SIGCONT, SIG_DFL);
417 signal(SIGSTOP, SIG_DFL);
418 signal(SIGTSTP, SIG_DFL);
420 /* Create a new session and make ourself the process
424 /* Open the new terminal device */
425 if ((device_open(a->terminal, O_RDWR)) < 0) {
426 if (stat(a->terminal, &sb) != 0) {
427 message(LOG | CONSOLE, "device '%s' does not exist.", a->terminal);
429 message(LOG | CONSOLE, "Bummer, can't open %s", a->terminal);
434 /* Make sure the terminal will act fairly normal for us */
436 /* Setup stdout, stderr for the new process so
437 * they point to the supplied terminal */
441 /* If the init Action requires us to wait, then force the
442 * supplied terminal to be the controlling tty. */
443 if (a->action & (SYSINIT | WAIT | CTRLALTDEL | SHUTDOWN | RESTART)) {
446 /* Now fork off another process to just hang around */
447 if ((pid = fork()) < 0) {
448 message(LOG | CONSOLE, "Can't fork!");
454 /* We are the parent -- wait till the child is done */
455 signal(SIGINT, SIG_IGN);
456 signal(SIGTSTP, SIG_IGN);
457 signal(SIGQUIT, SIG_IGN);
458 signal(SIGCHLD, SIG_DFL);
460 /* Wait for child to exit */
461 while ((tmp_pid = waitpid(pid, &junk, 0)) != pid) {
462 if (tmp_pid == -1 && errno == ECHILD) {
465 /* FIXME handle other errors */
468 /* See if stealing the controlling tty back is necessary */
470 if (pgrp != getpid())
473 /* Use a temporary process to steal the controlling tty. */
474 if ((pid = fork()) < 0) {
475 message(LOG | CONSOLE, "Can't fork!");
480 ioctl(0, TIOCSCTTY, 1);
483 while ((tmp_pid = waitpid(pid, &junk, 0)) != pid) {
484 if (tmp_pid < 0 && errno == ECHILD)
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
571 if (stat(CORE_ENABLE_FLAG_FILE, &sb) == 0) {
574 limit.rlim_cur = RLIM_INFINITY;
575 limit.rlim_max = RLIM_INFINITY;
576 setrlimit(RLIMIT_CORE, &limit);
580 /* Now run it. The new program will take over this PID,
581 * so nothing further in init.c should be run. */
584 /* We're still here? Some error happened. */
585 message(LOG | CONSOLE, "Bummer, could not run '%s': %m", cmdpath);
588 sigprocmask(SIG_SETMASK, &omask, NULL);
592 static int waitfor(const struct init_action *a)
599 wpid = waitpid(pid,&status,0);
602 if (wpid == -1 && errno == ECHILD) {
603 /* we missed its termination */
606 /* FIXME other errors should maybe trigger an error, but allow
607 * the program to continue */
612 /* Run all commands of a particular type */
613 static void run_actions(int action)
615 struct init_action *a, *tmp;
617 for (a = init_action_list; a; a = tmp) {
619 if (a->action == action) {
620 if (a->action & (SYSINIT | WAIT | CTRLALTDEL | SHUTDOWN | RESTART)) {
622 delete_init_action(a);
623 } else if (a->action & ONCE) {
625 delete_init_action(a);
626 } else if (a->action & (RESPAWN | ASKFIRST)) {
627 /* Only run stuff with pid==0. If they have
628 * a pid, that means it is still running */
638 static void init_reboot(unsigned long magic)
641 /* We have to fork here, since the kernel calls do_exit(0) in
642 * linux/kernel/sys.c, which can cause the machine to panic when
643 * the init process is killed.... */
644 if ((pid = fork()) == 0) {
648 waitpid (pid, NULL, 0);
651 static void shutdown_system(void)
653 sigset_t block_signals;
655 /* run everything to be run at "shutdown". This is done _prior_
656 * to killing everything, in case people wish to use scripts to
657 * shut things down gracefully... */
658 run_actions(SHUTDOWN);
660 /* first disable all our signals */
661 sigemptyset(&block_signals);
662 sigaddset(&block_signals, SIGHUP);
663 sigaddset(&block_signals, SIGQUIT);
664 sigaddset(&block_signals, SIGCHLD);
665 sigaddset(&block_signals, SIGUSR1);
666 sigaddset(&block_signals, SIGUSR2);
667 sigaddset(&block_signals, SIGINT);
668 sigaddset(&block_signals, SIGTERM);
669 sigaddset(&block_signals, SIGCONT);
670 sigaddset(&block_signals, SIGSTOP);
671 sigaddset(&block_signals, SIGTSTP);
672 sigprocmask(SIG_BLOCK, &block_signals, NULL);
674 /* Allow Ctrl-Alt-Del to reboot system. */
675 init_reboot(RB_ENABLE_CAD);
677 message(CONSOLE | LOG, "The system is going down NOW !!");
680 /* Send signals to every process _except_ pid 1 */
681 message(CONSOLE | LOG, "Sending SIGTERM to all processes.");
686 message(CONSOLE | LOG, "Sending SIGKILL to all processes.");
693 static void exec_signal(int sig ATTRIBUTE_UNUSED)
695 struct init_action *a, *tmp;
696 sigset_t unblock_signals;
698 for (a = init_action_list; a; a = tmp) {
700 if (a->action & RESTART) {
703 /* unblock all signals, blocked in shutdown_system() */
704 sigemptyset(&unblock_signals);
705 sigaddset(&unblock_signals, SIGHUP);
706 sigaddset(&unblock_signals, SIGQUIT);
707 sigaddset(&unblock_signals, SIGCHLD);
708 sigaddset(&unblock_signals, SIGUSR1);
709 sigaddset(&unblock_signals, SIGUSR2);
710 sigaddset(&unblock_signals, SIGINT);
711 sigaddset(&unblock_signals, SIGTERM);
712 sigaddset(&unblock_signals, SIGCONT);
713 sigaddset(&unblock_signals, SIGSTOP);
714 sigaddset(&unblock_signals, SIGTSTP);
715 sigprocmask(SIG_UNBLOCK, &unblock_signals, NULL);
717 /* Close whatever files are open. */
722 /* Open the new terminal device */
723 if ((device_open(a->terminal, O_RDWR)) < 0) {
725 if (stat(a->terminal, &sb) != 0) {
726 message(LOG | CONSOLE, "device '%s' does not exist.", a->terminal);
728 message(LOG | CONSOLE, "Bummer, can't open %s", a->terminal);
730 halt_signal(SIGUSR1);
733 /* Make sure the terminal will act fairly normal for us */
735 /* Setup stdout, stderr on the supplied terminal */
739 messageD(CONSOLE | LOG, "Trying to re-exec %s", a->command);
740 execl(a->command, a->command, NULL);
742 message(CONSOLE | LOG, "exec of '%s' failed: %m",
746 init_reboot(RB_HALT_SYSTEM);
752 static void halt_signal(int sig ATTRIBUTE_UNUSED)
755 message(CONSOLE | LOG, "The system is halted.");
758 /* allow time for last message to reach serial console */
762 init_reboot(RB_POWER_OFF);
764 init_reboot(RB_HALT_SYSTEM);
769 static void reboot_signal(int sig ATTRIBUTE_UNUSED)
772 message(CONSOLE | LOG, "Please stand by while rebooting the system.");
775 /* allow time for last message to reach serial console */
778 init_reboot(RB_AUTOBOOT);
783 static void ctrlaltdel_signal(int sig ATTRIBUTE_UNUSED)
785 run_actions(CTRLALTDEL);
788 /* The SIGSTOP & SIGTSTP handler */
789 static void stop_handler(int sig ATTRIBUTE_UNUSED)
791 int saved_errno = errno;
800 /* The SIGCONT handler */
801 static void cont_handler(int sig ATTRIBUTE_UNUSED)
806 #endif /* ! DEBUG_INIT */
808 static void new_init_action(int action, const char *command, const char *cons)
810 struct init_action *new_action, *a, *last;
815 /* do not run entries if console device is not available */
816 if (access(cons, R_OK | W_OK))
818 if (strcmp(cons, bb_dev_null) == 0 && (action & ASKFIRST))
821 new_action = calloc((size_t) (1), sizeof(struct init_action));
823 message(LOG | CONSOLE, "Memory allocation failure");
827 /* Append to the end of the list */
828 for (a = last = init_action_list; a; a = a->next) {
829 /* don't enter action if it's already in the list,
830 * but do overwrite existing actions */
831 if ((strcmp(a->command, command) == 0) &&
832 (strcmp(a->terminal, cons) ==0)) {
840 last->next = new_action;
842 init_action_list = new_action;
844 strcpy(new_action->command, command);
845 new_action->action = action;
846 strcpy(new_action->terminal, cons);
847 #if 0 /* calloc zeroed always */
850 messageD(LOG|CONSOLE, "command='%s' action='%d' terminal='%s'\n",
851 new_action->command, new_action->action, new_action->terminal);
854 static void delete_init_action(struct init_action *action)
856 struct init_action *a, *b = NULL;
858 for (a = init_action_list; a; b = a, a = a->next) {
861 init_action_list = a->next;
871 /* NOTE that if CONFIG_FEATURE_USE_INITTAB is NOT defined,
872 * then parse_inittab() simply adds in some default
873 * actions(i.e., runs INIT_SCRIPT and then starts a pair
874 * of "askfirst" shells). If CONFIG_FEATURE_USE_INITTAB
875 * _is_ defined, but /etc/inittab is missing, this
876 * results in the same set of default behaviors.
878 static void parse_inittab(void)
880 #ifdef CONFIG_FEATURE_USE_INITTAB
882 char buf[INIT_BUFFS_SIZE], lineAsRead[INIT_BUFFS_SIZE];
883 char tmpConsole[CONSOLE_BUFF_SIZE];
884 char *id, *runlev, *action, *command, *eol;
885 const struct init_action_type *a = actions;
888 file = fopen(INITTAB, "r");
890 /* No inittab file -- set up some default behavior */
892 /* Reboot on Ctrl-Alt-Del */
893 new_init_action(CTRLALTDEL, "/sbin/reboot", "");
894 /* Umount all filesystems on halt/reboot */
895 new_init_action(SHUTDOWN, "/bin/umount -a -r", "");
896 /* Swapoff on halt/reboot */
897 if(ENABLE_SWAPONOFF) new_init_action(SHUTDOWN, "/sbin/swapoff -a", "");
898 /* Prepare to restart init when a HUP is received */
899 new_init_action(RESTART, "/sbin/init", "");
900 /* Askfirst shell on tty1-4 */
901 new_init_action(ASKFIRST, bb_default_login_shell, "");
902 new_init_action(ASKFIRST, bb_default_login_shell, VC_2);
903 new_init_action(ASKFIRST, bb_default_login_shell, VC_3);
904 new_init_action(ASKFIRST, bb_default_login_shell, VC_4);
906 new_init_action(SYSINIT, INIT_SCRIPT, "");
909 #ifdef CONFIG_FEATURE_USE_INITTAB
912 while (fgets(buf, INIT_BUFFS_SIZE, file) != NULL) {
913 /* Skip leading spaces */
914 for (id = buf; *id == ' ' || *id == '\t'; id++);
916 /* Skip the line if it's a comment */
917 if (*id == '#' || *id == '\n')
920 /* Trim the trailing \n */
921 eol = strrchr(id, '\n');
925 /* Keep a copy around for posterity's sake (and error msgs) */
926 strcpy(lineAsRead, buf);
928 /* Separate the ID field from the runlevels */
929 runlev = strchr(id, ':');
930 if (runlev == NULL || *(runlev + 1) == '\0') {
931 message(LOG | CONSOLE, "Bad inittab entry: %s", lineAsRead);
938 /* Separate the runlevels from the action */
939 action = strchr(runlev, ':');
940 if (action == NULL || *(action + 1) == '\0') {
941 message(LOG | CONSOLE, "Bad inittab entry: %s", lineAsRead);
948 /* Separate the action from the command */
949 command = strchr(action, ':');
950 if (command == NULL || *(command + 1) == '\0') {
951 message(LOG | CONSOLE, "Bad inittab entry: %s", lineAsRead);
958 /* Ok, now process it */
959 for (a = actions; a->name != 0; a++) {
960 if (strcmp(a->name, action) == 0) {
962 if(strncmp(id, "/dev/", 5) == 0)
964 strcpy(tmpConsole, "/dev/");
965 safe_strncpy(tmpConsole + 5, id,
966 CONSOLE_BUFF_SIZE - 5);
969 new_init_action(a->action, command, id);
974 /* Choke on an unknown action */
975 message(LOG | CONSOLE, "Bad inittab entry: %s", lineAsRead);
980 #endif /* CONFIG_FEATURE_USE_INITTAB */
983 #ifdef CONFIG_FEATURE_USE_INITTAB
984 static void reload_signal(int sig ATTRIBUTE_UNUSED)
986 struct init_action *a, *tmp;
988 message(LOG, "Reloading /etc/inittab");
990 /* disable old entrys */
991 for (a = init_action_list; a; a = a->next ) {
997 /* remove unused entrys */
998 for (a = init_action_list; a; a = tmp) {
1000 if (a->action & (ONCE | SYSINIT | WAIT ) &&
1002 delete_init_action(a);
1005 run_actions(RESPAWN);
1008 #endif /* CONFIG_FEATURE_USE_INITTAB */
1010 extern int init_main(int argc, char **argv)
1012 struct init_action *a;
1016 if (argc > 1 && !strcmp(argv[1], "-q")) {
1017 return kill(1,SIGHUP);
1020 /* Expect to be invoked as init with PID=1 or be invoked as linuxrc */
1021 if (getpid() != 1 &&
1022 (!ENABLE_FEATURE_INITRD || !strstr(bb_applet_name, "linuxrc")))
1026 /* Set up sig handlers -- be sure to
1027 * clear all of these in run() */
1028 signal(SIGHUP, exec_signal);
1029 signal(SIGQUIT, exec_signal);
1030 signal(SIGUSR1, halt_signal);
1031 signal(SIGUSR2, halt_signal);
1032 signal(SIGINT, ctrlaltdel_signal);
1033 signal(SIGTERM, reboot_signal);
1034 signal(SIGCONT, cont_handler);
1035 signal(SIGSTOP, stop_handler);
1036 signal(SIGTSTP, stop_handler);
1038 /* Turn off rebooting via CTL-ALT-DEL -- we get a
1039 * SIGINT on CAD so we can shut things down gracefully... */
1040 init_reboot(RB_DISABLE_CAD);
1043 /* Figure out where the default console should be */
1046 /* Close whatever files are open, and reset the console. */
1051 if (device_open(console, O_RDWR | O_NOCTTY) == 0) {
1059 const char * const *e;
1060 /* Make sure environs is set to something sane */
1061 for(e = environment; *e; e++)
1062 putenv((char *) *e);
1065 message(MAYBE_CONSOLE | LOG, "init started: %s", bb_msg_full_version);
1067 /* Make sure there is enough memory to do something useful. */
1068 if (ENABLE_SWAPONOFF) {
1069 struct sysinfo info;
1071 if (!sysinfo(&info) &&
1072 (info.mem_unit ? : 1) * (long long)info.totalram < MEGABYTE)
1074 message(CONSOLE,"Low memory: forcing swapon.");
1075 /* swapon -a requires /proc typically */
1076 new_init_action(SYSINIT, "/bin/mount -t proc proc /proc", "");
1077 /* Try to turn on swap */
1078 new_init_action(SYSINIT, "/sbin/swapon -a", "");
1079 run_actions(SYSINIT); /* wait and removing */
1083 /* Check if we are supposed to be in single user mode */
1084 if (argc > 1 && (!strcmp(argv[1], "single") ||
1085 !strcmp(argv[1], "-s") || !strcmp(argv[1], "1"))) {
1086 /* Start a shell on console */
1087 new_init_action(RESPAWN, bb_default_login_shell, "");
1089 /* Not in single user mode -- see what inittab says */
1091 /* NOTE that if CONFIG_FEATURE_USE_INITTAB is NOT defined,
1092 * then parse_inittab() simply adds in some default
1093 * actions(i.e., runs INIT_SCRIPT and then starts a pair
1094 * of "askfirst" shells */
1098 /* Make the command line just say "init" -- thats all, nothing else */
1099 fixup_argv(argc, argv, "init");
1101 /* Now run everything that needs to be run */
1103 /* First run the sysinit command */
1104 run_actions(SYSINIT);
1106 /* Next run anything that wants to block */
1109 /* Next run anything to be run only once */
1112 #ifdef CONFIG_FEATURE_USE_INITTAB
1113 /* Redefine SIGHUP to reread /etc/inittab */
1114 signal(SIGHUP, reload_signal);
1116 signal(SIGHUP, SIG_IGN);
1117 #endif /* CONFIG_FEATURE_USE_INITTAB */
1120 /* Now run the looping stuff for the rest of forever */
1122 /* run the respawn stuff */
1123 run_actions(RESPAWN);
1125 /* run the askfirst stuff */
1126 run_actions(ASKFIRST);
1128 /* Don't consume all CPU time -- sleep a bit */
1131 /* Wait for a child process to exit */
1132 wpid = wait(&status);
1134 /* Find out who died and clean up their corpse */
1135 for (a = init_action_list; a; a = a->next) {
1136 if (a->pid == wpid) {
1137 /* Set the pid to 0 so that the process gets
1138 * restarted by run_actions() */
1140 message(LOG, "Process '%s' (pid %d) exited. "
1141 "Scheduling it for restart.",
1145 /* see if anyone else is waiting to be reaped */
1146 wpid = waitpid (-1, &status, WNOHANG);
1153 c-file-style: "linux"