init: reinstate proper handling of !ENABLE_FEATURE_USE_INITTAB
[oweals/busybox.git] / init / init.c
1 /* vi: set sw=4 ts=4: */
2 /*
3  * Mini init implementation for busybox
4  *
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.
8  *
9  * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
10  */
11
12 #include "libbb.h"
13 #include <syslog.h>
14 #include <paths.h>
15 #include <sys/reboot.h>
16 #include <sys/resource.h>
17
18 /* Was a CONFIG_xxx option. A lot of people were building
19  * not fully functional init by switching it on! */
20 #define DEBUG_INIT 0
21
22 #define COMMAND_SIZE      256
23 #define CONSOLE_NAME_SIZE 32
24
25 #ifndef INIT_SCRIPT
26 #define INIT_SCRIPT  "/etc/init.d/rcS"  /* Default sysinit script. */
27 #endif
28
29 /* Allowed init action types */
30 #define SYSINIT     0x01
31 #define RESPAWN     0x02
32 /* like respawn, but wait for <Enter> to be pressed on tty: */
33 #define ASKFIRST    0x04
34 #define WAIT        0x08
35 #define ONCE        0x10
36 #define CTRLALTDEL  0x20
37 #define SHUTDOWN    0x40
38 #define RESTART     0x80
39
40 /* Set up a linked list of init_actions, to be read from inittab */
41 struct init_action {
42         struct init_action *next;
43         pid_t pid;
44         uint8_t action_type;
45         char terminal[CONSOLE_NAME_SIZE];
46         char command[COMMAND_SIZE];
47 };
48
49 static struct init_action *init_action_list = NULL;
50
51 static const char *log_console = VC_5;
52
53 enum {
54         L_LOG = 0x1,
55         L_CONSOLE = 0x2,
56         MAYBE_CONSOLE = L_CONSOLE * !ENABLE_FEATURE_EXTRA_QUIET,
57 #ifndef RB_HALT_SYSTEM
58         RB_HALT_SYSTEM = 0xcdef0123, /* FIXME: this overflows enum */
59         RB_ENABLE_CAD = 0x89abcdef,
60         RB_DISABLE_CAD = 0,
61         RB_POWER_OFF = 0x4321fedc,
62         RB_AUTOBOOT = 0x01234567,
63 #endif
64 };
65
66 static void halt_reboot_pwoff(int sig) NORETURN;
67
68 static void waitfor(pid_t pid)
69 {
70         /* waitfor(run(x)): protect against failed fork inside run() */
71         if (pid <= 0)
72                 return;
73
74         /* Wait for any child (prevent zombies from exiting orphaned processes)
75          * but exit the loop only when specified one has exited. */
76         while (wait(NULL) != pid)
77                 continue;
78 }
79
80 static void loop_forever(void) NORETURN;
81 static void loop_forever(void)
82 {
83         while (1)
84                 sleep(1);
85 }
86
87 /* Print a message to the specified device.
88  * "where" may be bitwise-or'd from L_LOG | L_CONSOLE
89  * NB: careful, we can be called after vfork!
90  */
91 #define messageD(...) do { if (DEBUG_INIT) message(__VA_ARGS__); } while (0)
92 static void message(int where, const char *fmt, ...)
93         __attribute__ ((format(printf, 2, 3)));
94 static void message(int where, const char *fmt, ...)
95 {
96         static int log_fd = -1;
97         va_list arguments;
98         unsigned l;
99         char msg[128];
100
101         msg[0] = '\r';
102         va_start(arguments, fmt);
103         l = 1 + vsnprintf(msg + 1, sizeof(msg) - 2, fmt, arguments);
104         if (l > sizeof(msg) - 1)
105                 l = sizeof(msg) - 1;
106         msg[l] = '\0';
107         va_end(arguments);
108
109         if (ENABLE_FEATURE_INIT_SYSLOG) {
110                 if (where & L_LOG) {
111                         /* Log the message to syslogd */
112                         openlog("init", 0, LOG_DAEMON);
113                         /* don't print "\r" */
114                         syslog(LOG_INFO, "%s", msg + 1);
115                         closelog();
116                 }
117                 msg[l++] = '\n';
118                 msg[l] = '\0';
119         } else {
120                 msg[l++] = '\n';
121                 msg[l] = '\0';
122                 /* Take full control of the log tty, and never close it.
123                  * It's mine, all mine!  Muhahahaha! */
124                 if (log_fd < 0) {
125                         if (!log_console) {
126                                 log_fd = STDERR_FILENO;
127                         } else {
128                                 log_fd = device_open(log_console, O_WRONLY | O_NONBLOCK | O_NOCTTY);
129                                 if (log_fd < 0) {
130                                         bb_error_msg("can't log to %s", log_console);
131                                         where = L_CONSOLE;
132                                 } else {
133                                         close_on_exec_on(log_fd);
134                                 }
135                         }
136                 }
137                 if (where & L_LOG) {
138                         full_write(log_fd, msg, l);
139                         if (log_fd == STDERR_FILENO)
140                                 return; /* don't print dup messages */
141                 }
142         }
143
144         if (where & L_CONSOLE) {
145                 /* Send console messages to console so people will see them. */
146                 full_write(STDERR_FILENO, msg, l);
147         }
148 }
149
150 /* From <linux/serial.h> */
151 struct serial_struct {
152         int     type;
153         int     line;
154         unsigned int    port;
155         int     irq;
156         int     flags;
157         int     xmit_fifo_size;
158         int     custom_divisor;
159         int     baud_base;
160         unsigned short  close_delay;
161         char    io_type;
162         char    reserved_char[1];
163         int     hub6;
164         unsigned short  closing_wait; /* time to wait before closing */
165         unsigned short  closing_wait2; /* no longer used... */
166         unsigned char   *iomem_base;
167         unsigned short  iomem_reg_shift;
168         unsigned int    port_high;
169         unsigned long   iomap_base;     /* cookie passed into ioremap */
170         int     reserved[1];
171         /* Paranoia (imagine 64bit kernel overwriting 32bit userspace stack) */
172         uint32_t bbox_reserved[16];
173 };
174 static void console_init(void)
175 {
176         struct serial_struct sr;
177         char *s;
178
179         s = getenv("CONSOLE");
180         if (!s)
181                 s = getenv("console");
182         if (s) {
183                 int fd = open(s, O_RDWR | O_NONBLOCK | O_NOCTTY);
184                 if (fd >= 0) {
185                         dup2(fd, STDIN_FILENO);
186                         dup2(fd, STDOUT_FILENO);
187                         xmove_fd(fd, STDERR_FILENO);
188                 }
189                 messageD(L_LOG, "console='%s'", s);
190         } else {
191                 /* Make sure fd 0,1,2 are not closed
192                  * (so that they won't be used by future opens) */
193                 bb_sanitize_stdio();
194 // Users report problems
195 //              /* Make sure init can't be blocked by writing to stderr */
196 //              fcntl(STDERR_FILENO, F_SETFL, fcntl(STDERR_FILENO, F_GETFL) | O_NONBLOCK);
197         }
198
199         s = getenv("TERM");
200         if (ioctl(STDIN_FILENO, TIOCGSERIAL, &sr) == 0) {
201                 /* Force the TERM setting to vt102 for serial console
202                  * if TERM is set to linux (the default) */
203                 if (!s || strcmp(s, "linux") == 0)
204                         putenv((char*)"TERM=vt102");
205                 if (!ENABLE_FEATURE_INIT_SYSLOG)
206                         log_console = NULL;
207         } else if (!s)
208                 putenv((char*)"TERM=linux");
209 }
210
211 /* Set terminal settings to reasonable defaults.
212  * NB: careful, we can be called after vfork! */
213 static void set_sane_term(void)
214 {
215         struct termios tty;
216
217         tcgetattr(STDIN_FILENO, &tty);
218
219         /* set control chars */
220         tty.c_cc[VINTR] = 3;    /* C-c */
221         tty.c_cc[VQUIT] = 28;   /* C-\ */
222         tty.c_cc[VERASE] = 127; /* C-? */
223         tty.c_cc[VKILL] = 21;   /* C-u */
224         tty.c_cc[VEOF] = 4;     /* C-d */
225         tty.c_cc[VSTART] = 17;  /* C-q */
226         tty.c_cc[VSTOP] = 19;   /* C-s */
227         tty.c_cc[VSUSP] = 26;   /* C-z */
228
229         /* use line discipline 0 */
230         tty.c_line = 0;
231
232         /* Make it be sane */
233         tty.c_cflag &= CBAUD | CBAUDEX | CSIZE | CSTOPB | PARENB | PARODD;
234         tty.c_cflag |= CREAD | HUPCL | CLOCAL;
235
236         /* input modes */
237         tty.c_iflag = ICRNL | IXON | IXOFF;
238
239         /* output modes */
240         tty.c_oflag = OPOST | ONLCR;
241
242         /* local modes */
243         tty.c_lflag =
244                 ISIG | ICANON | ECHO | ECHOE | ECHOK | ECHOCTL | ECHOKE | IEXTEN;
245
246         tcsetattr_stdin_TCSANOW(&tty);
247 }
248
249 /* Open the new terminal device.
250  * NB: careful, we can be called after vfork! */
251 static void open_stdio_to_tty(const char* tty_name, int exit_on_failure)
252 {
253         /* empty tty_name means "use init's tty", else... */
254         if (tty_name[0]) {
255                 int fd;
256                 close(STDIN_FILENO);
257                 /* fd can be only < 0 or 0: */
258                 fd = device_open(tty_name, O_RDWR);
259                 if (fd) {
260                         message(L_LOG | L_CONSOLE, "can't open %s: %s",
261                                 tty_name, strerror(errno));
262                         if (exit_on_failure)
263                                 _exit(EXIT_FAILURE);
264                         if (DEBUG_INIT)
265                                 _exit(2);
266                         /* NB: we don't reach this if we were called after vfork.
267                          * Thus halt_reboot_pwoff() itself need not be vfork-safe. */
268                         halt_reboot_pwoff(SIGUSR1); /* halt the system */
269                 }
270                 dup2(STDIN_FILENO, STDOUT_FILENO);
271                 dup2(STDIN_FILENO, STDERR_FILENO);
272         }
273         set_sane_term();
274 }
275
276 /* Wrapper around exec:
277  * Takes string (max COMMAND_SIZE chars).
278  * If chars like '>' detected, execs '[-]/bin/sh -c "exec ......."'.
279  * Otherwise splits words on whitespace, deals with leading dash,
280  * and uses plain exec().
281  * NB: careful, we can be called after vfork!
282  */
283 static void init_exec(const char *command)
284 {
285         char *cmd[COMMAND_SIZE / 2];
286         char buf[COMMAND_SIZE + 6];  /* COMMAND_SIZE+strlen("exec ")+1 */
287         int dash = (command[0] == '-' /* maybe? && command[1] == '/' */);
288
289         /* See if any special /bin/sh requiring characters are present */
290         if (strpbrk(command, "~`!$^&*()=|\\{}[];\"'<>?") != NULL) {
291                 strcpy(buf, "exec ");
292                 strcpy(buf + 5, command + dash); /* excluding "-" */
293                 /* NB: LIBBB_DEFAULT_LOGIN_SHELL define has leading dash */
294                 cmd[0] = (char*)(LIBBB_DEFAULT_LOGIN_SHELL + !dash);
295                 cmd[1] = (char*)"-c";
296                 cmd[2] = buf;
297                 cmd[3] = NULL;
298         } else {
299                 /* Convert command (char*) into cmd (char**, one word per string) */
300                 char *word, *next;
301                 int i = 0;
302                 next = strcpy(buf, command); /* including "-" */
303                 while ((word = strsep(&next, " \t")) != NULL) {
304                         if (*word != '\0') { /* not two spaces/tabs together? */
305                                 cmd[i] = word;
306                                 i++;
307                         }
308                 }
309                 cmd[i] = NULL;
310         }
311         /* If we saw leading "-", it is interactive shell.
312          * Try harder to give it a controlling tty.
313          * And skip "-" in actual exec call. */
314         if (dash) {
315                 /* _Attempt_ to make stdin a controlling tty. */
316                 if (ENABLE_FEATURE_INIT_SCTTY)
317                         ioctl(STDIN_FILENO, TIOCSCTTY, 0 /*only try, don't steal*/);
318         }
319         BB_EXECVP(cmd[0] + dash, cmd);
320         message(L_LOG | L_CONSOLE, "cannot run '%s': %s", cmd[0], strerror(errno));
321         /* returns if execvp fails */
322 }
323
324 /* Used only by run_actions */
325 static pid_t run(const struct init_action *a)
326 {
327         pid_t pid;
328         sigset_t nmask, omask;
329
330         /* Block sigchild while forking (why?) */
331         sigemptyset(&nmask);
332         sigaddset(&nmask, SIGCHLD);
333         sigprocmask(SIG_BLOCK, &nmask, &omask);
334         if (BB_MMU && (a->action_type & ASKFIRST))
335                 pid = fork();
336         else
337                 pid = vfork();
338         sigprocmask(SIG_SETMASK, &omask, NULL);
339
340         if (pid < 0)
341                 message(L_LOG | L_CONSOLE, "can't fork");
342         if (pid)
343                 return pid;
344
345         /* Child */
346
347         /* Reset signal handlers that were set by the parent process */
348         bb_signals(0
349                 + (1 << SIGUSR1)
350                 + (1 << SIGUSR2)
351                 + (1 << SIGINT)
352                 + (1 << SIGTERM)
353                 + (1 << SIGHUP)
354                 + (1 << SIGQUIT)
355                 + (1 << SIGCONT)
356                 + (1 << SIGSTOP)
357                 + (1 << SIGTSTP)
358                 , SIG_DFL);
359
360         /* Create a new session and make ourself the process
361          * group leader */
362         setsid();
363
364         /* Open the new terminal device */
365         open_stdio_to_tty(a->terminal, 1 /* - exit if open fails */);
366
367 // NB: do not enable unless you change vfork to fork above
368 #ifdef BUT_RUN_ACTIONS_ALREADY_DOES_WAITING
369         /* If the init Action requires us to wait, then force the
370          * supplied terminal to be the controlling tty. */
371         if (a->action_type & (SYSINIT | WAIT | CTRLALTDEL | SHUTDOWN | RESTART)) {
372                 /* Now fork off another process to just hang around */
373                 pid = fork();
374                 if (pid < 0) {
375                         message(L_LOG | L_CONSOLE, "can't fork");
376                         _exit(EXIT_FAILURE);
377                 }
378
379                 if (pid > 0) {
380                         /* Parent - wait till the child is done */
381                         bb_signals(0
382                                 + (1 << SIGINT)
383                                 + (1 << SIGTSTP)
384                                 + (1 << SIGQUIT)
385                                 , SIG_IGN);
386                         signal(SIGCHLD, SIG_DFL);
387
388                         waitfor(pid);
389                         /* See if stealing the controlling tty back is necessary */
390                         if (tcgetpgrp(0) != getpid())
391                                 _exit(EXIT_SUCCESS);
392
393                         /* Use a temporary process to steal the controlling tty. */
394                         pid = fork();
395                         if (pid < 0) {
396                                 message(L_LOG | L_CONSOLE, "can't fork");
397                                 _exit(EXIT_FAILURE);
398                         }
399                         if (pid == 0) {
400                                 setsid();
401                                 ioctl(0, TIOCSCTTY, 1);
402                                 _exit(EXIT_SUCCESS);
403                         }
404                         waitfor(pid);
405                         _exit(EXIT_SUCCESS);
406                 }
407                 /* Child - fall though to actually execute things */
408         }
409 #endif
410
411         /* NB: on NOMMU we can't wait for input in child, so
412          * "askfirst" will work the same as "respawn". */
413         if (BB_MMU && (a->action_type & ASKFIRST)) {
414                 static const char press_enter[] ALIGN1 =
415 #ifdef CUSTOMIZED_BANNER
416 #include CUSTOMIZED_BANNER
417 #endif
418                         "\nPlease press Enter to activate this console. ";
419                 char c;
420                 /*
421                  * Save memory by not exec-ing anything large (like a shell)
422                  * before the user wants it. This is critical if swap is not
423                  * enabled and the system has low memory. Generally this will
424                  * be run on the second virtual console, and the first will
425                  * be allowed to start a shell or whatever an init script
426                  * specifies.
427                  */
428                 messageD(L_LOG, "waiting for enter to start '%s'"
429                                         "(pid %d, tty '%s')\n",
430                                 a->command, getpid(), a->terminal);
431                 full_write(STDOUT_FILENO, press_enter, sizeof(press_enter) - 1);
432                 while (safe_read(STDIN_FILENO, &c, 1) == 1 && c != '\n')
433                         continue;
434         }
435
436         /*
437          * When a file named /.init_enable_core exists, setrlimit is called
438          * before processes are spawned to set core file size as unlimited.
439          * This is for debugging only.  Don't use this is production, unless
440          * you want core dumps lying about....
441          */
442         if (ENABLE_FEATURE_INIT_COREDUMPS) {
443                 if (access("/.init_enable_core", F_OK) == 0) {
444                         struct rlimit limit;
445                         limit.rlim_cur = RLIM_INFINITY;
446                         limit.rlim_max = RLIM_INFINITY;
447                         setrlimit(RLIMIT_CORE, &limit);
448                 }
449         }
450
451         /* Log the process name and args */
452         message(L_LOG, "starting pid %d, tty '%s': '%s'",
453                           getpid(), a->terminal, a->command);
454
455         /* Now run it.  The new program will take over this PID,
456          * so nothing further in init.c should be run. */
457         init_exec(a->command);
458         /* We're still here?  Some error happened. */
459         _exit(-1);
460 }
461
462 static void delete_init_action(struct init_action *action)
463 {
464         struct init_action *a, *b = NULL;
465
466         for (a = init_action_list; a; b = a, a = a->next) {
467                 if (a == action) {
468                         if (b == NULL) {
469                                 init_action_list = a->next;
470                         } else {
471                                 b->next = a->next;
472                         }
473                         free(a);
474                         break;
475                 }
476         }
477 }
478
479 /* Run all commands of a particular type */
480 static void run_actions(int action_type)
481 {
482         struct init_action *a, *tmp;
483
484         for (a = init_action_list; a; a = tmp) {
485                 tmp = a->next;
486                 if (a->action_type & action_type) {
487                         // Pointless: run() will error out if open of device fails.
488                         ///* a->terminal of "" means "init's console" */
489                         //if (a->terminal[0] && access(a->terminal, R_OK | W_OK)) {
490                         //      //message(L_LOG | L_CONSOLE, "Device %s cannot be opened in RW mode", a->terminal /*, strerror(errno)*/);
491                         //      delete_init_action(a);
492                         //} else
493                         if (a->action_type & (SYSINIT | WAIT | CTRLALTDEL | SHUTDOWN | RESTART)) {
494                                 waitfor(run(a));
495                                 delete_init_action(a);
496                         } else if (a->action_type & ONCE) {
497                                 run(a);
498                                 delete_init_action(a);
499                         } else if (a->action_type & (RESPAWN | ASKFIRST)) {
500                                 /* Only run stuff with pid==0.  If they have
501                                  * a pid, that means it is still running */
502                                 if (a->pid == 0) {
503                                         a->pid = run(a);
504                                 }
505                         }
506                 }
507         }
508 }
509
510 static void init_reboot(unsigned long magic)
511 {
512         pid_t pid;
513         /* We have to fork here, since the kernel calls do_exit(EXIT_SUCCESS) in
514          * linux/kernel/sys.c, which can cause the machine to panic when
515          * the init process is killed.... */
516         pid = vfork();
517         if (pid == 0) { /* child */
518                 reboot(magic);
519                 _exit(EXIT_SUCCESS);
520         }
521         waitfor(pid);
522 }
523
524 static void kill_all_processes(void)
525 {
526         /* run everything to be run at "shutdown".  This is done _prior_
527          * to killing everything, in case people wish to use scripts to
528          * shut things down gracefully... */
529         run_actions(SHUTDOWN);
530
531         /* first disable all our signals */
532         sigprocmask_allsigs(SIG_BLOCK);
533
534         message(L_CONSOLE | L_LOG, "The system is going down NOW!");
535
536         /* Allow Ctrl-Alt-Del to reboot system. */
537         init_reboot(RB_ENABLE_CAD);
538
539         /* Send signals to every process _except_ pid 1 */
540         message(L_CONSOLE | L_LOG, "Sending SIG%s to all processes", "TERM");
541         kill(-1, SIGTERM);
542         sync();
543         sleep(1);
544
545         message(L_CONSOLE | L_LOG, "Sending SIG%s to all processes", "KILL");
546         kill(-1, SIGKILL);
547         sync();
548         sleep(1);
549 }
550
551 static void halt_reboot_pwoff(int sig)
552 {
553         const char *m = "halt";
554         int rb;
555
556         kill_all_processes();
557
558         rb = RB_HALT_SYSTEM;
559         if (sig == SIGTERM) {
560                 m = "reboot";
561                 rb = RB_AUTOBOOT;
562         } else if (sig == SIGUSR2) {
563                 m = "poweroff";
564                 rb = RB_POWER_OFF;
565         }
566         message(L_CONSOLE | L_LOG, "Requesting system %s", m);
567         /* allow time for last message to reach serial console */
568         sleep(2);
569         init_reboot(rb);
570         loop_forever();
571 }
572
573 /* Handler for QUIT - exec "restart" action,
574  * else (no such action defined) do nothing */
575 static void exec_restart_action(int sig UNUSED_PARAM)
576 {
577         struct init_action *a;
578
579         for (a = init_action_list; a; a = a->next) {
580                 if (a->action_type & RESTART) {
581                         kill_all_processes();
582
583                         /* unblock all signals (blocked in kill_all_processes()) */
584                         sigprocmask_allsigs(SIG_UNBLOCK);
585
586                         /* Open the new terminal device */
587                         open_stdio_to_tty(a->terminal, 0 /* - halt if open fails */);
588
589                         messageD(L_CONSOLE | L_LOG, "Trying to re-exec %s", a->command);
590                         init_exec(a->command);
591                         sleep(2);
592                         init_reboot(RB_HALT_SYSTEM);
593                         loop_forever();
594                 }
595         }
596 }
597
598 static void ctrlaltdel_signal(int sig UNUSED_PARAM)
599 {
600         run_actions(CTRLALTDEL);
601 }
602
603 /* The SIGCONT handler is set to record_signo().
604  * It just sets bb_got_signal = SIGCONT.  */
605
606 /* The SIGSTOP & SIGTSTP handler */
607 static void stop_handler(int sig UNUSED_PARAM)
608 {
609         int saved_errno = errno;
610
611         bb_got_signal = 0;
612         while (bb_got_signal == 0)
613                 pause();
614
615         errno = saved_errno;
616 }
617
618 static void new_init_action(uint8_t action_type, const char *command, const char *cons)
619 {
620         struct init_action *a, *last;
621
622 // Why?
623 //      if (strcmp(cons, bb_dev_null) == 0 && (action & ASKFIRST))
624 //              return;
625
626         /* Append to the end of the list */
627         for (a = last = init_action_list; a; a = a->next) {
628                 /* don't enter action if it's already in the list,
629                  * but do overwrite existing actions */
630                 if ((strcmp(a->command, command) == 0)
631                  && (strcmp(a->terminal, cons) == 0)
632                 ) {
633                         a->action_type = action_type;
634                         return;
635                 }
636                 last = a;
637         }
638
639         a = xzalloc(sizeof(*a));
640         if (last) {
641                 last->next = a;
642         } else {
643                 init_action_list = a;
644         }
645         a->action_type = action_type;
646         safe_strncpy(a->command, command, sizeof(a->command));
647         safe_strncpy(a->terminal, cons, sizeof(a->terminal));
648         messageD(L_LOG | L_CONSOLE, "command='%s' action=%d tty='%s'\n",
649                 a->command, a->action_type, a->terminal);
650 }
651
652 /* NOTE that if CONFIG_FEATURE_USE_INITTAB is NOT defined,
653  * then parse_inittab() simply adds in some default
654  * actions(i.e., runs INIT_SCRIPT and then starts a pair
655  * of "askfirst" shells).  If CONFIG_FEATURE_USE_INITTAB
656  * _is_ defined, but /etc/inittab is missing, this
657  * results in the same set of default behaviors.
658  */
659 static void parse_inittab(void)
660 {
661 #if ENABLE_FEATURE_USE_INITTAB
662         char *token[4];
663         parser_t *parser = config_open2("/etc/inittab", fopen_for_read);
664
665         if (parser == NULL)
666 #endif
667         {
668                 /* No inittab file -- set up some default behavior */
669                 /* Reboot on Ctrl-Alt-Del */
670                 new_init_action(CTRLALTDEL, "reboot", "");
671                 /* Umount all filesystems on halt/reboot */
672                 new_init_action(SHUTDOWN, "umount -a -r", "");
673                 /* Swapoff on halt/reboot */
674                 if (ENABLE_SWAPONOFF)
675                         new_init_action(SHUTDOWN, "swapoff -a", "");
676                 /* Prepare to restart init when a QUIT is received */
677                 new_init_action(RESTART, "init", "");
678                 /* Askfirst shell on tty1-4 */
679                 new_init_action(ASKFIRST, bb_default_login_shell, "");
680 //TODO: VC_1 instead of ""? "" is console -> ctty problems -> angry users
681                 new_init_action(ASKFIRST, bb_default_login_shell, VC_2);
682                 new_init_action(ASKFIRST, bb_default_login_shell, VC_3);
683                 new_init_action(ASKFIRST, bb_default_login_shell, VC_4);
684                 /* sysinit */
685                 new_init_action(SYSINIT, INIT_SCRIPT, "");
686                 return;
687         }
688
689 #if ENABLE_FEATURE_USE_INITTAB
690         /* optional_tty:ignored_runlevel:action:command
691          * Delims are not to be collapsed and need exactly 4 tokens
692          */
693         while (config_read(parser, token, 4, 0, "#:",
694                                 PARSE_NORMAL & ~(PARSE_TRIM | PARSE_COLLAPSE))) {
695                 /* order must correspond to SYSINIT..RESTART constants */
696                 static const char actions[] ALIGN1 =
697                         "sysinit\0""respawn\0""askfirst\0""wait\0""once\0"
698                         "ctrlaltdel\0""shutdown\0""restart\0";
699                 int action;
700                 char *tty = token[0];
701
702                 if (!token[3]) /* less than 4 tokens */
703                         goto bad_entry;
704                 action = index_in_strings(actions, token[2]);
705                 if (action < 0 || !token[3][0]) /* token[3]: command */
706                         goto bad_entry;
707                 /* turn .*TTY -> /dev/TTY */
708                 if (tty[0]) {
709                         if (strncmp(tty, "/dev/", 5) == 0)
710                                 tty += 5;
711                         tty = concat_path_file("/dev/", tty);
712                 }
713                 new_init_action(1 << action, token[3], tty);
714                 if (tty[0])
715                         free(tty);
716                 continue;
717  bad_entry:
718                 message(L_LOG | L_CONSOLE, "Bad inittab entry at line %d",
719                                 parser->lineno);
720         }
721         config_close(parser);
722 #endif
723 }
724
725 #if ENABLE_FEATURE_USE_INITTAB
726 static void reload_signal(int sig UNUSED_PARAM)
727 {
728         struct init_action *a, *tmp;
729
730         message(L_LOG, "reloading /etc/inittab");
731
732         /* disable old entrys */
733         for (a = init_action_list; a; a = a->next) {
734                 a->action_type = ONCE;
735         }
736
737         parse_inittab();
738
739         if (ENABLE_FEATURE_KILL_REMOVED) {
740                 /* Be nice and send SIGTERM first */
741                 for (a = init_action_list; a; a = a->next) {
742                         pid_t pid = a->pid;
743                         if ((a->action_type & ONCE) && pid != 0) {
744                                 kill(pid, SIGTERM);
745                         }
746                 }
747 #if CONFIG_FEATURE_KILL_DELAY
748                 /* NB: parent will wait in NOMMU case */
749                 if ((BB_MMU ? fork() : vfork()) == 0) { /* child */
750                         sleep(CONFIG_FEATURE_KILL_DELAY);
751                         for (a = init_action_list; a; a = a->next) {
752                                 pid_t pid = a->pid;
753                                 if ((a->action_type & ONCE) && pid != 0) {
754                                         kill(pid, SIGKILL);
755                                 }
756                         }
757                         _exit(EXIT_SUCCESS);
758                 }
759 #endif
760         }
761
762         /* remove unused entrys */
763         for (a = init_action_list; a; a = tmp) {
764                 tmp = a->next;
765                 if ((a->action_type & (ONCE | SYSINIT | WAIT)) && a->pid == 0) {
766                         delete_init_action(a);
767                 }
768         }
769         run_actions(RESPAWN | ASKFIRST);
770 }
771 #else
772 void reload_signal(int sig);
773 #endif
774
775 int init_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
776 int init_main(int argc UNUSED_PARAM, char **argv)
777 {
778         struct init_action *a;
779         pid_t wpid;
780
781         die_sleep = 30 * 24*60*60; /* if xmalloc will ever die... */
782
783         if (argv[1] && !strcmp(argv[1], "-q")) {
784                 return kill(1, SIGHUP);
785         }
786
787         if (!DEBUG_INIT) {
788                 /* Expect to be invoked as init with PID=1 or be invoked as linuxrc */
789                 if (getpid() != 1
790                  && (!ENABLE_FEATURE_INITRD || !strstr(applet_name, "linuxrc"))
791                 ) {
792                         bb_show_usage();
793                 }
794                 /* Set up sig handlers  -- be sure to
795                  * clear all of these in run() */
796 // TODO: handlers should just set a flag variable.
797 // Move signal handling from handlers to main loop -
798 // we have bad races otherwise.
799 // E.g. parse_inittab() vs. delete_init_action()...
800                 signal(SIGQUIT, exec_restart_action);
801                 bb_signals(0
802                         + (1 << SIGUSR1)  /* halt */
803                         + (1 << SIGUSR2)  /* poweroff */
804                         + (1 << SIGTERM)  /* reboot */
805                         , halt_reboot_pwoff);
806                 signal(SIGINT, ctrlaltdel_signal);
807                 signal(SIGCONT, record_signo);
808                 bb_signals(0
809                         + (1 << SIGSTOP)
810                         + (1 << SIGTSTP)
811                         , stop_handler);
812
813                 /* Turn off rebooting via CTL-ALT-DEL -- we get a
814                  * SIGINT on CAD so we can shut things down gracefully... */
815                 init_reboot(RB_DISABLE_CAD);
816         }
817
818         /* Figure out where the default console should be */
819         console_init();
820         set_sane_term();
821         xchdir("/");
822         setsid();
823
824         /* Make sure environs is set to something sane */
825         putenv((char *) "HOME=/");
826         putenv((char *) bb_PATH_root_path);
827         putenv((char *) "SHELL=/bin/sh");
828         putenv((char *) "USER=root"); /* needed? why? */
829
830         if (argv[1])
831                 xsetenv("RUNLEVEL", argv[1]);
832
833         /* Hello world */
834         message(MAYBE_CONSOLE | L_LOG, "init started: %s", bb_banner);
835
836         /* Make sure there is enough memory to do something useful. */
837         if (ENABLE_SWAPONOFF) {
838                 struct sysinfo info;
839
840                 if (sysinfo(&info) == 0
841                  && (info.mem_unit ? : 1) * (long long)info.totalram < 1024*1024
842                 ) {
843                         message(L_CONSOLE, "Low memory, forcing swapon");
844                         /* swapon -a requires /proc typically */
845                         new_init_action(SYSINIT, "mount -t proc proc /proc", "");
846                         /* Try to turn on swap */
847                         new_init_action(SYSINIT, "swapon -a", "");
848                         run_actions(SYSINIT);   /* wait and removing */
849                 }
850         }
851
852         /* Check if we are supposed to be in single user mode */
853         if (argv[1]
854          && (!strcmp(argv[1], "single") || !strcmp(argv[1], "-s") || LONE_CHAR(argv[1], '1'))
855         ) {
856                 /* ??? shouldn't we set RUNLEVEL="b" here? */
857                 /* Start a shell on console */
858                 new_init_action(RESPAWN, bb_default_login_shell, "");
859         } else {
860                 /* Not in single user mode -- see what inittab says */
861
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 */
866                 parse_inittab();
867         }
868
869 #if ENABLE_SELINUX
870         if (getenv("SELINUX_INIT") == NULL) {
871                 int enforce = 0;
872
873                 putenv((char*)"SELINUX_INIT=YES");
874                 if (selinux_init_load_policy(&enforce) == 0) {
875                         BB_EXECVP(argv[0], argv);
876                 } else if (enforce > 0) {
877                         /* SELinux in enforcing mode but load_policy failed */
878                         message(L_CONSOLE, "cannot load SELinux Policy. "
879                                 "Machine is in enforcing mode. Halting now.");
880                         exit(EXIT_FAILURE);
881                 }
882         }
883 #endif
884
885         /* Make the command line just say "init"  - thats all, nothing else */
886         strncpy(argv[0], "init", strlen(argv[0]));
887         /* Wipe argv[1]-argv[N] so they don't clutter the ps listing */
888         while (*++argv)
889                 memset(*argv, 0, strlen(*argv));
890
891         /* Now run everything that needs to be run */
892
893         /* First run the sysinit command */
894         run_actions(SYSINIT);
895
896         /* Next run anything that wants to block */
897         run_actions(WAIT);
898
899         /* Next run anything to be run only once */
900         run_actions(ONCE);
901
902         /* Redefine SIGHUP to reread /etc/inittab */
903         signal(SIGHUP, ENABLE_FEATURE_USE_INITTAB ? reload_signal : SIG_IGN);
904
905         /* Now run the looping stuff for the rest of forever */
906         while (1) {
907                 /* run the respawn/askfirst stuff */
908                 run_actions(RESPAWN | ASKFIRST);
909
910                 /* Don't consume all CPU time -- sleep a bit */
911                 sleep(1);
912
913                 /* Wait for any child process to exit */
914                 wpid = wait(NULL);
915                 while (wpid > 0) {
916                         /* Find out who died and clean up their corpse */
917                         for (a = init_action_list; a; a = a->next) {
918                                 if (a->pid == wpid) {
919                                         /* Set the pid to 0 so that the process gets
920                                          * restarted by run_actions() */
921                                         a->pid = 0;
922                                         message(L_LOG, "process '%s' (pid %d) exited. "
923                                                         "Scheduling for restart.",
924                                                         a->command, wpid);
925                                 }
926                         }
927                         /* see if anyone else is waiting to be reaped */
928                         wpid = wait_any_nohang(NULL);
929                 }
930         }
931 }