blob: cea30c99fd61526c16372da9cdf25d7732154f78 [file] [log] [blame]
Erik Andersenccc74882000-01-27 02:40:21 +00001/* vi: set sw=4 ts=4: */
Eric Andersenc4996011999-10-20 22:08:37 +00002/*
3 * Mini init implementation for busybox
4 *
Eric Andersenc4996011999-10-20 22:08:37 +00005 * Copyright (C) 1995, 1996 by Bruce Perens <bruce@pixar.com>.
Eric Andersenc7bda1c2004-03-15 08:29:22 +00006 * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org>
Eric Andersenc4996011999-10-20 22:08:37 +00007 * Adjusted by so many folks, it's impossible to keep track.
8 *
Rob Landley2edf5262006-01-22 02:41:51 +00009 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
Eric Andersenc4996011999-10-20 22:08:37 +000010 */
11
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000012#include "libbb.h"
Bernhard Reutner-Fischerf4701962008-01-27 12:50:12 +000013#include <syslog.h>
Eric Andersenb186d981999-12-03 09:19:54 +000014#include <paths.h>
Eric Andersen85e5e722003-07-22 08:56:55 +000015#include <sys/reboot.h>
Denis Vlasenko6c622462009-01-29 02:01:04 +000016#include <sys/resource.h>
Eric Andersen2c1de612003-04-24 11:41:28 +000017
Denis Vlasenko5cb54b52008-10-21 17:14:26 +000018/* 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
Denis Vlasenko6c622462009-01-29 02:01:04 +000022#define COMMAND_SIZE 256
Denis Vlasenko7a2ca5e2007-02-21 00:15:20 +000023#define CONSOLE_NAME_SIZE 32
Eric Andersenbd22ed82000-07-08 18:55:24 +000024
Erik Andersen96e2abd2000-01-07 11:40:44 +000025#ifndef INIT_SCRIPT
Glenn L McGrathbaf55a82002-08-22 18:22:10 +000026#define INIT_SCRIPT "/etc/init.d/rcS" /* Default sysinit script. */
Erik Andersen96e2abd2000-01-07 11:40:44 +000027#endif
Erik Andersen9c88cac1999-12-30 09:25:17 +000028
Erik Andersen7dc16072000-01-04 01:10:25 +000029/* Allowed init action types */
Denis Vlasenkoad4da982008-04-05 04:24:23 +000030#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
Erik Andersen7dc16072000-01-04 01:10:25 +000039
Eric Andersen0298be82002-03-05 15:12:19 +000040/* Set up a linked list of init_actions, to be read from inittab */
41struct init_action {
Eric Andersen0298be82002-03-05 15:12:19 +000042 struct init_action *next;
Denis Vlasenko7a2ca5e2007-02-21 00:15:20 +000043 pid_t pid;
Denis Vlasenkoa37e7132008-02-19 02:57:07 +000044 uint8_t action_type;
Denis Vlasenko7a2ca5e2007-02-21 00:15:20 +000045 char terminal[CONSOLE_NAME_SIZE];
Denis Vlasenkoa37e7132008-02-19 02:57:07 +000046 char command[COMMAND_SIZE];
Erik Andersen7dc16072000-01-04 01:10:25 +000047};
Erik Andersen7dc16072000-01-04 01:10:25 +000048
Eric Andersen0298be82002-03-05 15:12:19 +000049static struct init_action *init_action_list = NULL;
Glenn L McGrathbaf55a82002-08-22 18:22:10 +000050
Denis Vlasenko4c978632007-02-03 03:31:13 +000051static const char *log_console = VC_5;
Rob Landleybc68cd12006-03-10 19:22:06 +000052
53enum {
Denis Vlasenkoec27feb2007-02-17 15:52:02 +000054 L_LOG = 0x1,
55 L_CONSOLE = 0x2,
Denis Vlasenkob2b2c402009-01-28 23:56:31 +000056 MAYBE_CONSOLE = L_CONSOLE * !ENABLE_FEATURE_EXTRA_QUIET,
Rob Landleybc68cd12006-03-10 19:22:06 +000057#ifndef RB_HALT_SYSTEM
Bernhard Reutner-Fischercf1f2ac2006-06-02 10:43:17 +000058 RB_HALT_SYSTEM = 0xcdef0123, /* FIXME: this overflows enum */
Rob Landleybc68cd12006-03-10 19:22:06 +000059 RB_ENABLE_CAD = 0x89abcdef,
60 RB_DISABLE_CAD = 0,
61 RB_POWER_OFF = 0x4321fedc,
62 RB_AUTOBOOT = 0x01234567,
Eric Andersen0298be82002-03-05 15:12:19 +000063#endif
Rob Landleybc68cd12006-03-10 19:22:06 +000064};
Erik Andersen42094cd2000-03-20 21:34:52 +000065
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +000066static void halt_reboot_pwoff(int sig) NORETURN;
Eric Andersen0460ff21999-10-25 23:32:44 +000067
Denis Vlasenko21e20cb2008-01-04 15:10:47 +000068static void waitfor(pid_t pid)
Denis Vlasenkofb0eba72008-01-02 19:55:04 +000069{
Denis Vlasenko21e20cb2008-01-04 15:10:47 +000070 /* 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;
Denis Vlasenkofb0eba72008-01-02 19:55:04 +000078}
79
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +000080static void loop_forever(void) NORETURN;
Eric Andersen74400cc2001-10-18 04:11:39 +000081static void loop_forever(void)
Matt Kraai67a46402001-06-03 05:55:52 +000082{
83 while (1)
Glenn L McGrathbaf55a82002-08-22 18:22:10 +000084 sleep(1);
Matt Kraai67a46402001-06-03 05:55:52 +000085}
Eric Andersen7ef1a5b2001-03-20 17:39:08 +000086
Erik Andersen42094cd2000-03-20 21:34:52 +000087/* Print a message to the specified device.
Denis Vlasenko671ca332008-02-19 14:13:20 +000088 * "where" may be bitwise-or'd from L_LOG | L_CONSOLE
89 * NB: careful, we can be called after vfork!
90 */
Denis Vlasenko5cb54b52008-10-21 17:14:26 +000091#define messageD(...) do { if (DEBUG_INIT) message(__VA_ARGS__); } while (0)
Denis Vlasenko671ca332008-02-19 14:13:20 +000092static void message(int where, const char *fmt, ...)
Glenn L McGrathbaf55a82002-08-22 18:22:10 +000093 __attribute__ ((format(printf, 2, 3)));
Denis Vlasenko671ca332008-02-19 14:13:20 +000094static void message(int where, const char *fmt, ...)
Eric Andersen0460ff21999-10-25 23:32:44 +000095{
Eric Andersen22237012003-01-23 07:08:26 +000096 static int log_fd = -1;
Denis Vlasenko7a2ca5e2007-02-21 00:15:20 +000097 va_list arguments;
Denis Vlasenkob8d1a4c2008-09-20 16:28:59 +000098 unsigned l;
Denis Vlasenko7a2ca5e2007-02-21 00:15:20 +000099 char msg[128];
100
Glenn L McGrathb4a1baa2003-01-13 22:09:50 +0000101 msg[0] = '\r';
Denis Vlasenko7a2ca5e2007-02-21 00:15:20 +0000102 va_start(arguments, fmt);
Denis Vlasenko1bcdcd22008-12-09 21:23:31 +0000103 l = 1 + vsnprintf(msg + 1, sizeof(msg) - 2, fmt, arguments);
104 if (l > sizeof(msg) - 1)
105 l = sizeof(msg) - 1;
Denis Vlasenkob8d1a4c2008-09-20 16:28:59 +0000106 msg[l] = '\0';
Denis Vlasenkoec27feb2007-02-17 15:52:02 +0000107 va_end(arguments);
Glenn L McGrathb4a1baa2003-01-13 22:09:50 +0000108
Mike Frysingerbb50fdf2007-12-25 04:30:14 +0000109 if (ENABLE_FEATURE_INIT_SYSLOG) {
Denis Vlasenko671ca332008-02-19 14:13:20 +0000110 if (where & L_LOG) {
Denis Vlasenko1bcdcd22008-12-09 21:23:31 +0000111 /* Log the message to syslogd */
112 openlog("init", 0, LOG_DAEMON);
113 /* don't print "\r" */
114 syslog(LOG_INFO, "%s", msg + 1);
Mike Frysingerbb50fdf2007-12-25 04:30:14 +0000115 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) {
Bernhard Reutner-Fischerad2fa652008-07-21 11:16:39 +0000126 log_fd = STDERR_FILENO;
Denis Vlasenko7a2ca5e2007-02-21 00:15:20 +0000127 } else {
Mike Frysingerbb50fdf2007-12-25 04:30:14 +0000128 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);
Denis Vlasenko671ca332008-02-19 14:13:20 +0000131 where = L_CONSOLE;
Mike Frysingerbb50fdf2007-12-25 04:30:14 +0000132 } else {
133 close_on_exec_on(log_fd);
134 }
Denis Vlasenko7a2ca5e2007-02-21 00:15:20 +0000135 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000136 }
Denis Vlasenko671ca332008-02-19 14:13:20 +0000137 if (where & L_LOG) {
Mike Frysingerbb50fdf2007-12-25 04:30:14 +0000138 full_write(log_fd, msg, l);
Bernhard Reutner-Fischerad2fa652008-07-21 11:16:39 +0000139 if (log_fd == STDERR_FILENO)
Mike Frysingerbb50fdf2007-12-25 04:30:14 +0000140 return; /* don't print dup messages */
141 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000142 }
Eric Andersen4c781471999-11-26 08:12:56 +0000143
Denis Vlasenko671ca332008-02-19 14:13:20 +0000144 if (where & L_CONSOLE) {
Denis Vlasenko7a2ca5e2007-02-21 00:15:20 +0000145 /* Send console messages to console so people will see them. */
Bernhard Reutner-Fischer5e25ddb2008-05-19 09:48:17 +0000146 full_write(STDERR_FILENO, msg, l);
Eric Andersen3ae0c781999-11-04 01:13:21 +0000147 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000148}
149
Denis Vlasenko7a2ca5e2007-02-21 00:15:20 +0000150/* From <linux/serial.h> */
151struct 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};
Eric Andersen74400cc2001-10-18 04:11:39 +0000174static void console_init(void)
Eric Andersen0460ff21999-10-25 23:32:44 +0000175{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000176 struct serial_struct sr;
177 char *s;
Eric Andersen0460ff21999-10-25 23:32:44 +0000178
Denis Vlasenko7a2ca5e2007-02-21 00:15:20 +0000179 s = getenv("CONSOLE");
Bernhard Reutner-Fischerad2fa652008-07-21 11:16:39 +0000180 if (!s)
181 s = getenv("console");
Denis Vlasenko7a2ca5e2007-02-21 00:15:20 +0000182 if (s) {
183 int fd = open(s, O_RDWR | O_NONBLOCK | O_NOCTTY);
184 if (fd >= 0) {
Bernhard Reutner-Fischerad2fa652008-07-21 11:16:39 +0000185 dup2(fd, STDIN_FILENO);
186 dup2(fd, STDOUT_FILENO);
187 xmove_fd(fd, STDERR_FILENO);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000188 }
Denis Vlasenko7a2ca5e2007-02-21 00:15:20 +0000189 messageD(L_LOG, "console='%s'", s);
Denis Vlasenkod8540f72007-06-14 07:53:06 +0000190 } else {
Denis Vlasenkofb274df2008-03-17 13:26:51 +0000191 /* Make sure fd 0,1,2 are not closed
192 * (so that they won't be used by future opens) */
Denis Vlasenkod48e81f2008-07-06 07:00:11 +0000193 bb_sanitize_stdio();
Denis Vlasenkoa34b8a42008-11-29 23:14:37 +0000194// 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);
Eric Andersen07e52971999-11-07 07:38:08 +0000197 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000198
Denis Vlasenko7a2ca5e2007-02-21 00:15:20 +0000199 s = getenv("TERM");
Denis Vlasenkoe421b5e2008-03-17 22:01:42 +0000200 if (ioctl(STDIN_FILENO, TIOCGSERIAL, &sr) == 0) {
Denis Vlasenko2afabe82007-12-10 07:06:04 +0000201 /* Force the TERM setting to vt102 for serial console
Denis Vlasenko7a2ca5e2007-02-21 00:15:20 +0000202 * if TERM is set to linux (the default) */
203 if (!s || strcmp(s, "linux") == 0)
204 putenv((char*)"TERM=vt102");
Mike Frysingerbb50fdf2007-12-25 04:30:14 +0000205 if (!ENABLE_FEATURE_INIT_SYSLOG)
206 log_console = NULL;
Denis Vlasenko7a2ca5e2007-02-21 00:15:20 +0000207 } else if (!s)
208 putenv((char*)"TERM=linux");
Eric Andersen0460ff21999-10-25 23:32:44 +0000209}
Glenn L McGrathbaf55a82002-08-22 18:22:10 +0000210
Denis Vlasenko671ca332008-02-19 14:13:20 +0000211/* Set terminal settings to reasonable defaults.
212 * NB: careful, we can be called after vfork! */
Denis Vlasenko2afabe82007-12-10 07:06:04 +0000213static void set_sane_term(void)
Eric Andersen7ef1a5b2001-03-20 17:39:08 +0000214{
Denis Vlasenko2afabe82007-12-10 07:06:04 +0000215 struct termios tty;
Eric Andersen7ef1a5b2001-03-20 17:39:08 +0000216
Denis Vlasenko2afabe82007-12-10 07:06:04 +0000217 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
Bernhard Reutner-Fischerad2fa652008-07-21 11:16:39 +0000229 /* use line discipline 0 */
Denis Vlasenko2afabe82007-12-10 07:06:04 +0000230 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
Denis Vlasenko202ac502008-11-05 13:20:58 +0000246 tcsetattr_stdin_TCSANOW(&tty);
Eric Andersen7ef1a5b2001-03-20 17:39:08 +0000247}
248
Denis Vlasenko671ca332008-02-19 14:13:20 +0000249/* Open the new terminal device.
250 * NB: careful, we can be called after vfork! */
Denis Vlasenkod55268d2007-12-26 18:32:58 +0000251static void open_stdio_to_tty(const char* tty_name, int exit_on_failure)
Denis Vlasenko7a2ca5e2007-02-21 00:15:20 +0000252{
253 /* empty tty_name means "use init's tty", else... */
254 if (tty_name[0]) {
Denis Vlasenkod55268d2007-12-26 18:32:58 +0000255 int fd;
Bernhard Reutner-Fischerad2fa652008-07-21 11:16:39 +0000256 close(STDIN_FILENO);
Denis Vlasenkod55268d2007-12-26 18:32:58 +0000257 /* fd can be only < 0 or 0: */
258 fd = device_open(tty_name, O_RDWR);
259 if (fd) {
Bernhard Reutner-Fischera53de7f2008-07-21 13:46:54 +0000260 message(L_LOG | L_CONSOLE, "can't open %s: %s",
Denis Vlasenko7a2ca5e2007-02-21 00:15:20 +0000261 tty_name, strerror(errno));
Denis Vlasenkod55268d2007-12-26 18:32:58 +0000262 if (exit_on_failure)
Bernhard Reutner-Fischer636a1f82008-05-19 09:29:47 +0000263 _exit(EXIT_FAILURE);
Denis Vlasenko5cb54b52008-10-21 17:14:26 +0000264 if (DEBUG_INIT)
Mike Frysingerbb50fdf2007-12-25 04:30:14 +0000265 _exit(2);
Bernhard Reutner-Fischerad2fa652008-07-21 11:16:39 +0000266 /* NB: we don't reach this if we were called after vfork.
267 * Thus halt_reboot_pwoff() itself need not be vfork-safe. */
Denis Vlasenkod55268d2007-12-26 18:32:58 +0000268 halt_reboot_pwoff(SIGUSR1); /* halt the system */
Denis Vlasenko7a2ca5e2007-02-21 00:15:20 +0000269 }
Bernhard Reutner-Fischerad2fa652008-07-21 11:16:39 +0000270 dup2(STDIN_FILENO, STDOUT_FILENO);
271 dup2(STDIN_FILENO, STDERR_FILENO);
Bernhard Reutner-Fischer0da069d2006-05-29 12:54:16 +0000272 }
Denis Vlasenkod238a472007-03-05 19:22:04 +0000273 set_sane_term();
Bernhard Reutner-Fischer0da069d2006-05-29 12:54:16 +0000274}
275
Denis Vlasenko671ca332008-02-19 14:13:20 +0000276/* 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!
Denis Vlasenkoa37e7132008-02-19 02:57:07 +0000282 */
283static void init_exec(const char *command)
284{
285 char *cmd[COMMAND_SIZE / 2];
286 char buf[COMMAND_SIZE + 6]; /* COMMAND_SIZE+strlen("exec ")+1 */
Denis Vlasenko671ca332008-02-19 14:13:20 +0000287 int dash = (command[0] == '-' /* maybe? && command[1] == '/' */);
Denis Vlasenkoa37e7132008-02-19 02:57:07 +0000288
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 "-" */
Denis Vlasenko671ca332008-02-19 14:13:20 +0000293 /* NB: LIBBB_DEFAULT_LOGIN_SHELL define has leading dash */
Denis Vlasenkoa37e7132008-02-19 02:57:07 +0000294 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);
Bernhard Reutner-Fischera53de7f2008-07-21 13:46:54 +0000320 message(L_LOG | L_CONSOLE, "cannot run '%s': %s", cmd[0], strerror(errno));
Denis Vlasenkoa37e7132008-02-19 02:57:07 +0000321 /* returns if execvp fails */
322}
323
Denis Vlasenko2afabe82007-12-10 07:06:04 +0000324/* Used only by run_actions */
Glenn L McGrathb4a1baa2003-01-13 22:09:50 +0000325static pid_t run(const struct init_action *a)
Eric Andersen0298be82002-03-05 15:12:19 +0000326{
Mike Frysinger10427ab2005-07-06 05:00:48 +0000327 pid_t pid;
Eric Andersen0298be82002-03-05 15:12:19 +0000328 sigset_t nmask, omask;
Erik Andersenac6e71f2000-01-08 22:04:33 +0000329
Denis Vlasenko2afabe82007-12-10 07:06:04 +0000330 /* Block sigchild while forking (why?) */
Eric Andersen0298be82002-03-05 15:12:19 +0000331 sigemptyset(&nmask);
332 sigaddset(&nmask, SIGCHLD);
333 sigprocmask(SIG_BLOCK, &nmask, &omask);
Denis Vlasenkoad4da982008-04-05 04:24:23 +0000334 if (BB_MMU && (a->action_type & ASKFIRST))
335 pid = fork();
336 else
337 pid = vfork();
Denis Vlasenko966bb432007-02-27 19:20:33 +0000338 sigprocmask(SIG_SETMASK, &omask, NULL);
Eric Andersen0298be82002-03-05 15:12:19 +0000339
Denis Vlasenko2afabe82007-12-10 07:06:04 +0000340 if (pid < 0)
Bernhard Reutner-Fischera53de7f2008-07-21 13:46:54 +0000341 message(L_LOG | L_CONSOLE, "can't fork");
Denis Vlasenko966bb432007-02-27 19:20:33 +0000342 if (pid)
343 return pid;
Eric Andersen0298be82002-03-05 15:12:19 +0000344
Denis Vlasenko2afabe82007-12-10 07:06:04 +0000345 /* Child */
346
Denis Vlasenko966bb432007-02-27 19:20:33 +0000347 /* Reset signal handlers that were set by the parent process */
Denis Vlasenko25591c32008-02-16 22:58:56 +0000348 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);
Eric Andersen2f6c04f1999-11-01 23:59:44 +0000359
Denis Vlasenko966bb432007-02-27 19:20:33 +0000360 /* Create a new session and make ourself the process
361 * group leader */
362 setsid();
Eric Andersen599e3ce2002-07-03 11:08:10 +0000363
Denis Vlasenko966bb432007-02-27 19:20:33 +0000364 /* Open the new terminal device */
Denis Vlasenko671ca332008-02-19 14:13:20 +0000365 open_stdio_to_tty(a->terminal, 1 /* - exit if open fails */);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000366
Denis Vlasenko671ca332008-02-19 14:13:20 +0000367// NB: do not enable unless you change vfork to fork above
Denis Vlasenko2afabe82007-12-10 07:06:04 +0000368#ifdef BUT_RUN_ACTIONS_ALREADY_DOES_WAITING
Denis Vlasenko966bb432007-02-27 19:20:33 +0000369 /* If the init Action requires us to wait, then force the
370 * supplied terminal to be the controlling tty. */
Denis Vlasenkoa37e7132008-02-19 02:57:07 +0000371 if (a->action_type & (SYSINIT | WAIT | CTRLALTDEL | SHUTDOWN | RESTART)) {
Denis Vlasenko966bb432007-02-27 19:20:33 +0000372 /* Now fork off another process to just hang around */
Denis Vlasenko6bef3d12007-11-06 03:05:54 +0000373 pid = fork();
Denis Vlasenkof6ccc622007-11-10 01:57:35 +0000374 if (pid < 0) {
Bernhard Reutner-Fischera53de7f2008-07-21 13:46:54 +0000375 message(L_LOG | L_CONSOLE, "can't fork");
Bernhard Reutner-Fischer636a1f82008-05-19 09:29:47 +0000376 _exit(EXIT_FAILURE);
Denis Vlasenko966bb432007-02-27 19:20:33 +0000377 }
378
379 if (pid > 0) {
Denis Vlasenko2afabe82007-12-10 07:06:04 +0000380 /* Parent - wait till the child is done */
Denis Vlasenko25591c32008-02-16 22:58:56 +0000381 bb_signals(0
382 + (1 << SIGINT)
383 + (1 << SIGTSTP)
384 + (1 << SIGQUIT)
385 , SIG_IGN);
Denis Vlasenko966bb432007-02-27 19:20:33 +0000386 signal(SIGCHLD, SIG_DFL);
387
Denis Vlasenko2afabe82007-12-10 07:06:04 +0000388 waitfor(pid);
Denis Vlasenko966bb432007-02-27 19:20:33 +0000389 /* See if stealing the controlling tty back is necessary */
390 if (tcgetpgrp(0) != getpid())
Bernhard Reutner-Fischer636a1f82008-05-19 09:29:47 +0000391 _exit(EXIT_SUCCESS);
Denis Vlasenko966bb432007-02-27 19:20:33 +0000392
393 /* Use a temporary process to steal the controlling tty. */
Denis Vlasenko6bef3d12007-11-06 03:05:54 +0000394 pid = fork();
395 if (pid < 0) {
Bernhard Reutner-Fischera53de7f2008-07-21 13:46:54 +0000396 message(L_LOG | L_CONSOLE, "can't fork");
Bernhard Reutner-Fischer636a1f82008-05-19 09:29:47 +0000397 _exit(EXIT_FAILURE);
Eric Andersen0298be82002-03-05 15:12:19 +0000398 }
Denis Vlasenko966bb432007-02-27 19:20:33 +0000399 if (pid == 0) {
400 setsid();
401 ioctl(0, TIOCSCTTY, 1);
Bernhard Reutner-Fischer636a1f82008-05-19 09:29:47 +0000402 _exit(EXIT_SUCCESS);
Eric Andersen0298be82002-03-05 15:12:19 +0000403 }
Denis Vlasenko2afabe82007-12-10 07:06:04 +0000404 waitfor(pid);
Bernhard Reutner-Fischer636a1f82008-05-19 09:29:47 +0000405 _exit(EXIT_SUCCESS);
Eric Andersen0298be82002-03-05 15:12:19 +0000406 }
Denis Vlasenko2afabe82007-12-10 07:06:04 +0000407 /* Child - fall though to actually execute things */
Denis Vlasenko966bb432007-02-27 19:20:33 +0000408 }
Denis Vlasenko5adfa442007-12-25 16:08:53 +0000409#endif
Denis Vlasenko671ca332008-02-19 14:13:20 +0000410
Denis Vlasenkoad4da982008-04-05 04:24:23 +0000411 /* NB: on NOMMU we can't wait for input in child, so
412 * "askfirst" will work the same as "respawn". */
Denis Vlasenko671ca332008-02-19 14:13:20 +0000413 if (BB_MMU && (a->action_type & ASKFIRST)) {
Denis Vlasenko6ca409e2007-08-12 20:58:27 +0000414 static const char press_enter[] ALIGN1 =
Denis Vlasenko966bb432007-02-27 19:20:33 +0000415#ifdef CUSTOMIZED_BANNER
416#include CUSTOMIZED_BANNER
Eric Andersen1f50e842004-08-16 09:29:42 +0000417#endif
Denis Vlasenko966bb432007-02-27 19:20:33 +0000418 "\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",
Denis Vlasenkoa37e7132008-02-19 02:57:07 +0000430 a->command, getpid(), a->terminal);
Bernhard Reutner-Fischer5e25ddb2008-05-19 09:48:17 +0000431 full_write(STDOUT_FILENO, press_enter, sizeof(press_enter) - 1);
432 while (safe_read(STDIN_FILENO, &c, 1) == 1 && c != '\n')
Denis Vlasenko2afabe82007-12-10 07:06:04 +0000433 continue;
Denis Vlasenko966bb432007-02-27 19:20:33 +0000434 }
Denis Vlasenko2afabe82007-12-10 07:06:04 +0000435
Denis Vlasenko6c622462009-01-29 02:01:04 +0000436 /*
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 */
Mike Frysingerbb50fdf2007-12-25 04:30:14 +0000442 if (ENABLE_FEATURE_INIT_COREDUMPS) {
Denis Vlasenko6c622462009-01-29 02:01:04 +0000443 if (access("/.init_enable_core", F_OK) == 0) {
Denis Vlasenko966bb432007-02-27 19:20:33 +0000444 struct rlimit limit;
Denis Vlasenko966bb432007-02-27 19:20:33 +0000445 limit.rlim_cur = RLIM_INFINITY;
446 limit.rlim_max = RLIM_INFINITY;
447 setrlimit(RLIMIT_CORE, &limit);
Erik Andersen983b51b2000-04-04 18:14:25 +0000448 }
Erik Andersen05df2392000-01-13 04:43:48 +0000449 }
Mike Frysingerbb50fdf2007-12-25 04:30:14 +0000450
Denis Vlasenkoa37e7132008-02-19 02:57:07 +0000451 /* Log the process name and args */
452 message(L_LOG, "starting pid %d, tty '%s': '%s'",
453 getpid(), a->terminal, a->command);
454
Denis Vlasenko966bb432007-02-27 19:20:33 +0000455 /* Now run it. The new program will take over this PID,
456 * so nothing further in init.c should be run. */
Denis Vlasenkoa37e7132008-02-19 02:57:07 +0000457 init_exec(a->command);
Denis Vlasenko966bb432007-02-27 19:20:33 +0000458 /* We're still here? Some error happened. */
Denis Vlasenko966bb432007-02-27 19:20:33 +0000459 _exit(-1);
Eric Andersen0460ff21999-10-25 23:32:44 +0000460}
461
Bernhard Reutner-Fischer52593612008-07-21 11:53:04 +0000462static 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
Eric Andersen0298be82002-03-05 15:12:19 +0000479/* Run all commands of a particular type */
Denis Vlasenkoa37e7132008-02-19 02:57:07 +0000480static void run_actions(int action_type)
Eric Andersen219d6f51999-11-02 19:43:01 +0000481{
Eric Andersen0298be82002-03-05 15:12:19 +0000482 struct init_action *a, *tmp;
Eric Andersen219d6f51999-11-02 19:43:01 +0000483
Eric Andersen0298be82002-03-05 15:12:19 +0000484 for (a = init_action_list; a; a = tmp) {
485 tmp = a->next;
Denis Vlasenkoad4da982008-04-05 04:24:23 +0000486 if (a->action_type & action_type) {
Denis Vlasenkod7e2e122007-12-10 08:40:29 +0000487 // 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
Denis Vlasenkoa37e7132008-02-19 02:57:07 +0000493 if (a->action_type & (SYSINIT | WAIT | CTRLALTDEL | SHUTDOWN | RESTART)) {
Denis Vlasenko2afabe82007-12-10 07:06:04 +0000494 waitfor(run(a));
Eric Andersen0298be82002-03-05 15:12:19 +0000495 delete_init_action(a);
Denis Vlasenkoa37e7132008-02-19 02:57:07 +0000496 } else if (a->action_type & ONCE) {
Eric Andersen0298be82002-03-05 15:12:19 +0000497 run(a);
498 delete_init_action(a);
Denis Vlasenkoa37e7132008-02-19 02:57:07 +0000499 } else if (a->action_type & (RESPAWN | ASKFIRST)) {
Eric Andersen0298be82002-03-05 15:12:19 +0000500 /* 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 }
Erik Andersene132f4b2000-02-09 04:16:43 +0000506 }
507 }
508}
509
Eric Andersen2c1de612003-04-24 11:41:28 +0000510static void init_reboot(unsigned long magic)
511{
512 pid_t pid;
Bernhard Reutner-Fischer636a1f82008-05-19 09:29:47 +0000513 /* We have to fork here, since the kernel calls do_exit(EXIT_SUCCESS) in
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000514 * linux/kernel/sys.c, which can cause the machine to panic when
Eric Andersen2c1de612003-04-24 11:41:28 +0000515 * the init process is killed.... */
Denis Vlasenko9b1381f2007-01-03 02:56:00 +0000516 pid = vfork();
517 if (pid == 0) { /* child */
Eric Andersen2c1de612003-04-24 11:41:28 +0000518 reboot(magic);
Bernhard Reutner-Fischer636a1f82008-05-19 09:29:47 +0000519 _exit(EXIT_SUCCESS);
Eric Andersen2c1de612003-04-24 11:41:28 +0000520 }
Denis Vlasenkofb0eba72008-01-02 19:55:04 +0000521 waitfor(pid);
Eric Andersen2c1de612003-04-24 11:41:28 +0000522}
523
Denis Vlasenkod55268d2007-12-26 18:32:58 +0000524static void kill_all_processes(void)
Eric Andersencc8ed391999-10-05 16:24:54 +0000525{
Eric Andersena9cc8962002-09-16 06:49:06 +0000526 /* 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
Eric Andersen72f9a422001-10-28 05:12:20 +0000531 /* first disable all our signals */
Denis Vlasenko3f165fa2008-03-17 08:29:08 +0000532 sigprocmask_allsigs(SIG_BLOCK);
Erik Andersen7dc16072000-01-04 01:10:25 +0000533
Denis Vlasenko7a2ca5e2007-02-21 00:15:20 +0000534 message(L_CONSOLE | L_LOG, "The system is going down NOW!");
535
Erik Andersene49d5ec2000-02-08 19:58:47 +0000536 /* Allow Ctrl-Alt-Del to reboot system. */
Erik Andersen4f3f7572000-04-28 00:18:56 +0000537 init_reboot(RB_ENABLE_CAD);
Erik Andersene132f4b2000-02-09 04:16:43 +0000538
Erik Andersene49d5ec2000-02-08 19:58:47 +0000539 /* Send signals to every process _except_ pid 1 */
Denis Vlasenko7a2ca5e2007-02-21 00:15:20 +0000540 message(L_CONSOLE | L_LOG, "Sending SIG%s to all processes", "TERM");
Erik Andersene49d5ec2000-02-08 19:58:47 +0000541 kill(-1, SIGTERM);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000542 sync();
Denis Vlasenko7a2ca5e2007-02-21 00:15:20 +0000543 sleep(1);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000544
Denis Vlasenko7a2ca5e2007-02-21 00:15:20 +0000545 message(L_CONSOLE | L_LOG, "Sending SIG%s to all processes", "KILL");
Erik Andersene49d5ec2000-02-08 19:58:47 +0000546 kill(-1, SIGKILL);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000547 sync();
Denis Vlasenko7a2ca5e2007-02-21 00:15:20 +0000548 sleep(1);
Eric Andersencc8ed391999-10-05 16:24:54 +0000549}
550
Denis Vlasenkod55268d2007-12-26 18:32:58 +0000551static void halt_reboot_pwoff(int sig)
Denis Vlasenko2afabe82007-12-10 07:06:04 +0000552{
Bernhard Reutner-Fischer54d50a02008-07-17 14:00:42 +0000553 const char *m = "halt";
Denis Vlasenko2afabe82007-12-10 07:06:04 +0000554 int rb;
555
Denis Vlasenkod55268d2007-12-26 18:32:58 +0000556 kill_all_processes();
Denis Vlasenko2afabe82007-12-10 07:06:04 +0000557
Denis Vlasenko2afabe82007-12-10 07:06:04 +0000558 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
Denis Vlasenkoa58a6372008-02-19 12:10:18 +0000573/* Handler for QUIT - exec "restart" action,
Denis Vlasenkod55268d2007-12-26 18:32:58 +0000574 * else (no such action defined) do nothing */
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000575static void exec_restart_action(int sig UNUSED_PARAM)
Eric Andersen730f8262001-12-17 23:13:08 +0000576{
Denis Vlasenkoa37e7132008-02-19 02:57:07 +0000577 struct init_action *a;
Glenn L McGrathbaf55a82002-08-22 18:22:10 +0000578
Denis Vlasenkoa37e7132008-02-19 02:57:07 +0000579 for (a = init_action_list; a; a = a->next) {
580 if (a->action_type & RESTART) {
Denis Vlasenkod55268d2007-12-26 18:32:58 +0000581 kill_all_processes();
Eric Andersen5222d312002-07-03 05:15:23 +0000582
Denis Vlasenkod55268d2007-12-26 18:32:58 +0000583 /* unblock all signals (blocked in kill_all_processes()) */
Denis Vlasenko3f165fa2008-03-17 08:29:08 +0000584 sigprocmask_allsigs(SIG_UNBLOCK);
Eric Andersen5222d312002-07-03 05:15:23 +0000585
Eric Andersen3c8064f2003-07-05 08:29:01 +0000586 /* Open the new terminal device */
Denis Vlasenkod55268d2007-12-26 18:32:58 +0000587 open_stdio_to_tty(a->terminal, 0 /* - halt if open fails */);
Eric Andersen3c8064f2003-07-05 08:29:01 +0000588
Denis Vlasenkoec27feb2007-02-17 15:52:02 +0000589 messageD(L_CONSOLE | L_LOG, "Trying to re-exec %s", a->command);
Denis Vlasenkoa37e7132008-02-19 02:57:07 +0000590 init_exec(a->command);
Eric Andersen730f8262001-12-17 23:13:08 +0000591 sleep(2);
592 init_reboot(RB_HALT_SYSTEM);
593 loop_forever();
594 }
595 }
596}
597
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000598static void ctrlaltdel_signal(int sig UNUSED_PARAM)
Eric Andersenc97ec342001-04-03 18:01:51 +0000599{
600 run_actions(CTRLALTDEL);
601}
602
Denis Vlasenko3fa36e22008-11-09 00:15:11 +0000603/* The SIGCONT handler is set to record_signo().
604 * It just sets bb_got_signal = SIGCONT. */
605
Eric Andersen0298be82002-03-05 15:12:19 +0000606/* The SIGSTOP & SIGTSTP handler */
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000607static void stop_handler(int sig UNUSED_PARAM)
Eric Andersened8a9be2001-11-30 19:10:58 +0000608{
Eric Andersen0298be82002-03-05 15:12:19 +0000609 int saved_errno = errno;
Eric Andersened8a9be2001-11-30 19:10:58 +0000610
Denis Vlasenko3fa36e22008-11-09 00:15:11 +0000611 bb_got_signal = 0;
612 while (bb_got_signal == 0)
Glenn L McGrathbaf55a82002-08-22 18:22:10 +0000613 pause();
Denis Vlasenko2afabe82007-12-10 07:06:04 +0000614
Eric Andersened8a9be2001-11-30 19:10:58 +0000615 errno = saved_errno;
616}
617
Denis Vlasenkoa37e7132008-02-19 02:57:07 +0000618static void new_init_action(uint8_t action_type, const char *command, const char *cons)
Erik Andersen7dc16072000-01-04 01:10:25 +0000619{
Denis Vlasenkoa37e7132008-02-19 02:57:07 +0000620 struct init_action *a, *last;
Erik Andersen9e737252000-01-06 01:16:13 +0000621
Denis Vlasenkoa37e7132008-02-19 02:57:07 +0000622// Why?
623// if (strcmp(cons, bb_dev_null) == 0 && (action & ASKFIRST))
624// return;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000625
Eric Andersen0298be82002-03-05 15:12:19 +0000626 /* Append to the end of the list */
Eric Andersen22718092004-10-08 08:17:39 +0000627 for (a = last = init_action_list; a; a = a->next) {
Eric Andersen82baf632004-10-08 08:21:54 +0000628 /* don't enter action if it's already in the list,
629 * but do overwrite existing actions */
Denis Vlasenkoec27feb2007-02-17 15:52:02 +0000630 if ((strcmp(a->command, command) == 0)
Denis Vlasenkob74a2db2008-07-21 21:34:51 +0000631 && (strcmp(a->terminal, cons) == 0)
632 ) {
Denis Vlasenkoa37e7132008-02-19 02:57:07 +0000633 a->action_type = action_type;
Eric Andersen6fd0e312003-07-22 09:48:56 +0000634 return;
635 }
Eric Andersen22718092004-10-08 08:17:39 +0000636 last = a;
Eric Andersen6fd0e312003-07-22 09:48:56 +0000637 }
Denis Vlasenko7a2ca5e2007-02-21 00:15:20 +0000638
Denis Vlasenkoa37e7132008-02-19 02:57:07 +0000639 a = xzalloc(sizeof(*a));
Eric Andersen22718092004-10-08 08:17:39 +0000640 if (last) {
Denis Vlasenkoa37e7132008-02-19 02:57:07 +0000641 last->next = a;
Eric Andersen1644db92001-09-05 20:18:15 +0000642 } else {
Denis Vlasenkoa37e7132008-02-19 02:57:07 +0000643 init_action_list = a;
Eric Andersen1644db92001-09-05 20:18:15 +0000644 }
Denis Vlasenkoa37e7132008-02-19 02:57:07 +0000645 a->action_type = action_type;
646 safe_strncpy(a->command, command, sizeof(a->command));
647 safe_strncpy(a->terminal, cons, sizeof(a->terminal));
Denis Vlasenko7a2ca5e2007-02-21 00:15:20 +0000648 messageD(L_LOG | L_CONSOLE, "command='%s' action=%d tty='%s'\n",
Denis Vlasenkoa37e7132008-02-19 02:57:07 +0000649 a->command, a->action_type, a->terminal);
Erik Andersen7dc16072000-01-04 01:10:25 +0000650}
651
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000652/* NOTE that if CONFIG_FEATURE_USE_INITTAB is NOT defined,
Erik Andersen9e737252000-01-06 01:16:13 +0000653 * then parse_inittab() simply adds in some default
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000654 * 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
Erik Andersen812d4662000-01-07 18:30:40 +0000657 * results in the same set of default behaviors.
Glenn L McGrathbaf55a82002-08-22 18:22:10 +0000658 */
Eric Andersen3e6ff902001-03-09 21:24:12 +0000659static void parse_inittab(void)
Erik Andersen7dc16072000-01-04 01:10:25 +0000660{
Denis Vlasenko6c622462009-01-29 02:01:04 +0000661#if ENABLE_FEATURE_USE_INITTAB
Bernhard Reutner-Fischer54d50a02008-07-17 14:00:42 +0000662 char *token[4];
Denis Vlasenko6c622462009-01-29 02:01:04 +0000663 parser_t *parser = config_open2("/etc/inittab", fopen_for_read);
Bernhard Reutner-Fischer54d50a02008-07-17 14:00:42 +0000664
Denis Vlasenko6c622462009-01-29 02:01:04 +0000665 if (parser == NULL)
666#endif
667 {
668 /* No inittab file -- set up some default behavior */
Bernhard Reutner-Fischer54d50a02008-07-17 14:00:42 +0000669 /* 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, "");
Denis Vlasenkoa474b682008-07-17 17:58:44 +0000680//TODO: VC_1 instead of ""? "" is console -> ctty problems -> angry users
Bernhard Reutner-Fischer54d50a02008-07-17 14:00:42 +0000681 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, "");
Bernhard Reutner-Fischer54d50a02008-07-17 14:00:42 +0000686 return;
687 }
Denis Vlasenko6c622462009-01-29 02:01:04 +0000688
689#if ENABLE_FEATURE_USE_INITTAB
Bernhard Reutner-Fischer54d50a02008-07-17 14:00:42 +0000690 /* optional_tty:ignored_runlevel:action:command
Denis Vlasenkoa474b682008-07-17 17:58:44 +0000691 * Delims are not to be collapsed and need exactly 4 tokens
Bernhard Reutner-Fischer54d50a02008-07-17 14:00:42 +0000692 */
Denis Vlasenko084266e2008-07-26 23:08:31 +0000693 while (config_read(parser, token, 4, 0, "#:",
694 PARSE_NORMAL & ~(PARSE_TRIM | PARSE_COLLAPSE))) {
Denis Vlasenko6c622462009-01-29 02:01:04 +0000695 /* 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";
Denis Vlasenkoa474b682008-07-17 17:58:44 +0000699 int action;
Bernhard Reutner-Fischer54d50a02008-07-17 14:00:42 +0000700 char *tty = token[0];
Bernhard Reutner-Fischer54d50a02008-07-17 14:00:42 +0000701
Denis Vlasenkoa474b682008-07-17 17:58:44 +0000702 if (!token[3]) /* less than 4 tokens */
Bernhard Reutner-Fischer54d50a02008-07-17 14:00:42 +0000703 goto bad_entry;
Denis Vlasenkoa474b682008-07-17 17:58:44 +0000704 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)
Bernhard Reutner-Fischer54d50a02008-07-17 14:00:42 +0000710 tty += 5;
711 tty = concat_path_file("/dev/", tty);
Denis Vlasenkoa474b682008-07-17 17:58:44 +0000712 }
713 new_init_action(1 << action, token[3], tty);
714 if (tty[0])
Bernhard Reutner-Fischer54d50a02008-07-17 14:00:42 +0000715 free(tty);
716 continue;
717 bad_entry:
Bernhard Reutner-Fischerad2fa652008-07-21 11:16:39 +0000718 message(L_LOG | L_CONSOLE, "Bad inittab entry at line %d",
719 parser->lineno);
Bernhard Reutner-Fischer54d50a02008-07-17 14:00:42 +0000720 }
721 config_close(parser);
Denis Vlasenko6c622462009-01-29 02:01:04 +0000722#endif
Erik Andersen7dc16072000-01-04 01:10:25 +0000723}
724
Denis Vlasenkoad4da982008-04-05 04:24:23 +0000725#if ENABLE_FEATURE_USE_INITTAB
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000726static void reload_signal(int sig UNUSED_PARAM)
Eric Andersen6fd0e312003-07-22 09:48:56 +0000727{
Eric Andersen82baf632004-10-08 08:21:54 +0000728 struct init_action *a, *tmp;
729
Denis Vlasenko7a2ca5e2007-02-21 00:15:20 +0000730 message(L_LOG, "reloading /etc/inittab");
Eric Andersen82baf632004-10-08 08:21:54 +0000731
732 /* disable old entrys */
Denis Vlasenkoec5631b2007-12-25 01:08:58 +0000733 for (a = init_action_list; a; a = a->next) {
Denis Vlasenkoa37e7132008-02-19 02:57:07 +0000734 a->action_type = ONCE;
Eric Andersen82baf632004-10-08 08:21:54 +0000735 }
736
737 parse_inittab();
738
Mike Frysingerbb50fdf2007-12-25 04:30:14 +0000739 if (ENABLE_FEATURE_KILL_REMOVED) {
Denis Vlasenkod55268d2007-12-26 18:32:58 +0000740 /* Be nice and send SIGTERM first */
Mike Frysingerbb50fdf2007-12-25 04:30:14 +0000741 for (a = init_action_list; a; a = a->next) {
742 pid_t pid = a->pid;
Denis Vlasenkoa37e7132008-02-19 02:57:07 +0000743 if ((a->action_type & ONCE) && pid != 0) {
Mike Frysingerbb50fdf2007-12-25 04:30:14 +0000744 kill(pid, SIGTERM);
Denis Vlasenkoec5631b2007-12-25 01:08:58 +0000745 }
Denis Vlasenkoec5631b2007-12-25 01:08:58 +0000746 }
Denis Vlasenkod55268d2007-12-26 18:32:58 +0000747#if CONFIG_FEATURE_KILL_DELAY
Denis Vlasenko671ca332008-02-19 14:13:20 +0000748 /* NB: parent will wait in NOMMU case */
749 if ((BB_MMU ? fork() : vfork()) == 0) { /* child */
Denis Vlasenkod55268d2007-12-26 18:32:58 +0000750 sleep(CONFIG_FEATURE_KILL_DELAY);
751 for (a = init_action_list; a; a = a->next) {
752 pid_t pid = a->pid;
Denis Vlasenkoa37e7132008-02-19 02:57:07 +0000753 if ((a->action_type & ONCE) && pid != 0) {
Denis Vlasenkod55268d2007-12-26 18:32:58 +0000754 kill(pid, SIGKILL);
755 }
756 }
Bernhard Reutner-Fischer636a1f82008-05-19 09:29:47 +0000757 _exit(EXIT_SUCCESS);
Denis Vlasenkod55268d2007-12-26 18:32:58 +0000758 }
759#endif
Denis Vlasenkoec5631b2007-12-25 01:08:58 +0000760 }
Denis Vlasenkoec5631b2007-12-25 01:08:58 +0000761
Eric Andersen82baf632004-10-08 08:21:54 +0000762 /* remove unused entrys */
763 for (a = init_action_list; a; a = tmp) {
764 tmp = a->next;
Denis Vlasenkoa37e7132008-02-19 02:57:07 +0000765 if ((a->action_type & (ONCE | SYSINIT | WAIT)) && a->pid == 0) {
Eric Andersen82baf632004-10-08 08:21:54 +0000766 delete_init_action(a);
767 }
768 }
Denis Vlasenkoad4da982008-04-05 04:24:23 +0000769 run_actions(RESPAWN | ASKFIRST);
Eric Andersen6fd0e312003-07-22 09:48:56 +0000770}
Denis Vlasenkob2b2c402009-01-28 23:56:31 +0000771#else
772void reload_signal(int sig);
Denis Vlasenkoad4da982008-04-05 04:24:23 +0000773#endif
Eric Andersen82baf632004-10-08 08:21:54 +0000774
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +0000775int init_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000776int init_main(int argc UNUSED_PARAM, char **argv)
Eric Andersen0460ff21999-10-25 23:32:44 +0000777{
Eric Andersen0298be82002-03-05 15:12:19 +0000778 struct init_action *a;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000779 pid_t wpid;
Eric Andersen0460ff21999-10-25 23:32:44 +0000780
Denis Vlasenko9b1381f2007-01-03 02:56:00 +0000781 die_sleep = 30 * 24*60*60; /* if xmalloc will ever die... */
782
Denis Vlasenko1d426652008-03-17 09:09:09 +0000783 if (argv[1] && !strcmp(argv[1], "-q")) {
Denis Vlasenko7a2ca5e2007-02-21 00:15:20 +0000784 return kill(1, SIGHUP);
Eric Andersen730f8262001-12-17 23:13:08 +0000785 }
Mike Frysingerbb50fdf2007-12-25 04:30:14 +0000786
Denis Vlasenko5cb54b52008-10-21 17:14:26 +0000787 if (!DEBUG_INIT) {
Mike Frysingerbb50fdf2007-12-25 04:30:14 +0000788 /* 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() */
Denis Vlasenko6c622462009-01-29 02:01:04 +0000796// 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()...
Denis Vlasenko99a61842008-02-19 12:08:38 +0000800 signal(SIGQUIT, exec_restart_action);
Denis Vlasenko25591c32008-02-16 22:58:56 +0000801 bb_signals(0
802 + (1 << SIGUSR1) /* halt */
803 + (1 << SIGUSR2) /* poweroff */
804 + (1 << SIGTERM) /* reboot */
805 , halt_reboot_pwoff);
Mike Frysingerbb50fdf2007-12-25 04:30:14 +0000806 signal(SIGINT, ctrlaltdel_signal);
Denis Vlasenko3fa36e22008-11-09 00:15:11 +0000807 signal(SIGCONT, record_signo);
Denis Vlasenko25591c32008-02-16 22:58:56 +0000808 bb_signals(0
809 + (1 << SIGSTOP)
810 + (1 << SIGTSTP)
811 , stop_handler);
Mike Frysingerbb50fdf2007-12-25 04:30:14 +0000812
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);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000816 }
Denis Vlasenkod8540f72007-06-14 07:53:06 +0000817
Eric Andersen599e3ce2002-07-03 11:08:10 +0000818 /* Figure out where the default console should be */
819 console_init();
Denis Vlasenkod238a472007-03-05 19:22:04 +0000820 set_sane_term();
Bernhard Reutner-Fischer54d50a02008-07-17 14:00:42 +0000821 xchdir("/");
Erik Andersene49d5ec2000-02-08 19:58:47 +0000822 setsid();
Denis Vlasenkob8d1a4c2008-09-20 16:28:59 +0000823
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? */
Rob Landleycba1b962006-07-09 17:28:17 +0000829
Bernhard Reutner-Fischer54d50a02008-07-17 14:00:42 +0000830 if (argv[1])
Denis Vlasenkob8d1a4c2008-09-20 16:28:59 +0000831 xsetenv("RUNLEVEL", argv[1]);
Rob Landleycba1b962006-07-09 17:28:17 +0000832
Erik Andersene49d5ec2000-02-08 19:58:47 +0000833 /* Hello world */
Denis Vlasenkoca525b42007-06-13 12:27:17 +0000834 message(MAYBE_CONSOLE | L_LOG, "init started: %s", bb_banner);
Eric Andersencc8ed391999-10-05 16:24:54 +0000835
Erik Andersene49d5ec2000-02-08 19:58:47 +0000836 /* Make sure there is enough memory to do something useful. */
Rob Landleyc3386a42005-08-30 18:50:37 +0000837 if (ENABLE_SWAPONOFF) {
838 struct sysinfo info;
839
Denis Vlasenko6c622462009-01-29 02:01:04 +0000840 if (sysinfo(&info) == 0
841 && (info.mem_unit ? : 1) * (long long)info.totalram < 1024*1024
842 ) {
Denis Vlasenko7a2ca5e2007-02-21 00:15:20 +0000843 message(L_CONSOLE, "Low memory, forcing swapon");
Rob Landleyc3386a42005-08-30 18:50:37 +0000844 /* swapon -a requires /proc typically */
Denis Vlasenko2f0c0d02007-01-21 00:41:04 +0000845 new_init_action(SYSINIT, "mount -t proc proc /proc", "");
Rob Landleyc3386a42005-08-30 18:50:37 +0000846 /* Try to turn on swap */
Denis Vlasenko2f0c0d02007-01-21 00:41:04 +0000847 new_init_action(SYSINIT, "swapon -a", "");
Rob Landleyc3386a42005-08-30 18:50:37 +0000848 run_actions(SYSINIT); /* wait and removing */
849 }
850 }
Erik Andersen9e737252000-01-06 01:16:13 +0000851
Erik Andersene49d5ec2000-02-08 19:58:47 +0000852 /* Check if we are supposed to be in single user mode */
Denis Vlasenko1d426652008-03-17 09:09:09 +0000853 if (argv[1]
Denis Vlasenkobf66fbc2006-12-21 13:23:14 +0000854 && (!strcmp(argv[1], "single") || !strcmp(argv[1], "-s") || LONE_CHAR(argv[1], '1'))
855 ) {
Bernhard Reutner-Fischer54d50a02008-07-17 14:00:42 +0000856 /* ??? shouldn't we set RUNLEVEL="b" here? */
Glenn L McGrathb4a1baa2003-01-13 22:09:50 +0000857 /* Start a shell on console */
Glenn L McGrathdc4e75e2003-09-02 02:36:18 +0000858 new_init_action(RESPAWN, bb_default_login_shell, "");
Erik Andersene49d5ec2000-02-08 19:58:47 +0000859 } else {
860 /* Not in single user mode -- see what inittab says */
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000861
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000862 /* NOTE that if CONFIG_FEATURE_USE_INITTAB is NOT defined,
Erik Andersene49d5ec2000-02-08 19:58:47 +0000863 * then parse_inittab() simply adds in some default
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000864 * actions(i.e., runs INIT_SCRIPT and then starts a pair
Erik Andersene49d5ec2000-02-08 19:58:47 +0000865 * of "askfirst" shells */
866 parse_inittab();
Eric Andersend00c2621999-12-07 08:37:31 +0000867 }
Erik Andersen983b51b2000-04-04 18:14:25 +0000868
Denis Vlasenko9b1381f2007-01-03 02:56:00 +0000869#if ENABLE_SELINUX
Rob Landleyb3ede5a2006-03-27 23:09:12 +0000870 if (getenv("SELINUX_INIT") == NULL) {
871 int enforce = 0;
872
Denis Vlasenkod8540f72007-06-14 07:53:06 +0000873 putenv((char*)"SELINUX_INIT=YES");
Rob Landleyb3ede5a2006-03-27 23:09:12 +0000874 if (selinux_init_load_policy(&enforce) == 0) {
Denis Vlasenko4921b542007-02-03 02:17:41 +0000875 BB_EXECVP(argv[0], argv);
Rob Landleyb3ede5a2006-03-27 23:09:12 +0000876 } else if (enforce > 0) {
877 /* SELinux in enforcing mode but load_policy failed */
Bernhard Reutner-Fischera53de7f2008-07-21 13:46:54 +0000878 message(L_CONSOLE, "cannot load SELinux Policy. "
Denis Vlasenkob7167542007-02-27 19:20:00 +0000879 "Machine is in enforcing mode. Halting now.");
Bernhard Reutner-Fischer636a1f82008-05-19 09:29:47 +0000880 exit(EXIT_FAILURE);
Rob Landleyb3ede5a2006-03-27 23:09:12 +0000881 }
882 }
Denis Vlasenko6c622462009-01-29 02:01:04 +0000883#endif
Rob Landleyb3ede5a2006-03-27 23:09:12 +0000884
Denis Vlasenko2afabe82007-12-10 07:06:04 +0000885 /* 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));
Eric Andersen219d6f51999-11-02 19:43:01 +0000890
Erik Andersene49d5ec2000-02-08 19:58:47 +0000891 /* Now run everything that needs to be run */
892
893 /* First run the sysinit command */
Eric Andersen1644db92001-09-05 20:18:15 +0000894 run_actions(SYSINIT);
Eric Andersen0298be82002-03-05 15:12:19 +0000895
Erik Andersene49d5ec2000-02-08 19:58:47 +0000896 /* Next run anything that wants to block */
Eric Andersen1644db92001-09-05 20:18:15 +0000897 run_actions(WAIT);
Eric Andersen0298be82002-03-05 15:12:19 +0000898
Erik Andersene49d5ec2000-02-08 19:58:47 +0000899 /* Next run anything to be run only once */
Eric Andersen0298be82002-03-05 15:12:19 +0000900 run_actions(ONCE);
901
Eric Andersen6fd0e312003-07-22 09:48:56 +0000902 /* Redefine SIGHUP to reread /etc/inittab */
Denis Vlasenkob2b2c402009-01-28 23:56:31 +0000903 signal(SIGHUP, ENABLE_FEATURE_USE_INITTAB ? reload_signal : SIG_IGN);
Eric Andersen82baf632004-10-08 08:21:54 +0000904
Erik Andersene49d5ec2000-02-08 19:58:47 +0000905 /* Now run the looping stuff for the rest of forever */
906 while (1) {
Denis Vlasenkoad4da982008-04-05 04:24:23 +0000907 /* run the respawn/askfirst stuff */
908 run_actions(RESPAWN | ASKFIRST);
Eric Andersen0298be82002-03-05 15:12:19 +0000909
910 /* Don't consume all CPU time -- sleep a bit */
911 sleep(1);
912
Denis Vlasenkofb0eba72008-01-02 19:55:04 +0000913 /* Wait for any child process to exit */
Bernhard Reutner-Fischerc58dbf22006-05-30 12:16:54 +0000914 wpid = wait(NULL);
Eric Andersen87812dc2004-04-12 19:21:54 +0000915 while (wpid > 0) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000916 /* Find out who died and clean up their corpse */
Eric Andersen0298be82002-03-05 15:12:19 +0000917 for (a = init_action_list; a; a = a->next) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000918 if (a->pid == wpid) {
Eric Andersen0298be82002-03-05 15:12:19 +0000919 /* Set the pid to 0 so that the process gets
920 * restarted by run_actions() */
Erik Andersene49d5ec2000-02-08 19:58:47 +0000921 a->pid = 0;
Denis Vlasenko7a2ca5e2007-02-21 00:15:20 +0000922 message(L_LOG, "process '%s' (pid %d) exited. "
Denis Vlasenkoa37e7132008-02-19 02:57:07 +0000923 "Scheduling for restart.",
Glenn L McGrathb4a1baa2003-01-13 22:09:50 +0000924 a->command, wpid);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000925 }
926 }
Eric Andersencf1fee02002-12-17 09:48:16 +0000927 /* see if anyone else is waiting to be reaped */
Denis Vlasenkofb0eba72008-01-02 19:55:04 +0000928 wpid = wait_any_nohang(NULL);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000929 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000930 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000931}