blob: e02773cc0dd483ed62a33257fb10d73d73883063 [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>
Eric Andersen2c1de612003-04-24 11:41:28 +000016
Denis Vlasenkoa37e7132008-02-19 02:57:07 +000017#define COMMAND_SIZE 256
Denis Vlasenko7a2ca5e2007-02-21 00:15:20 +000018#define CONSOLE_NAME_SIZE 32
19#define MAXENV 16 /* Number of env. vars */
Eric Andersenbd22ed82000-07-08 18:55:24 +000020
Erik Andersen983b51b2000-04-04 18:14:25 +000021/*
Eric Andersenc7bda1c2004-03-15 08:29:22 +000022 * When a file named CORE_ENABLE_FLAG_FILE exists, setrlimit is called
Erik Andersen183da4a2000-04-04 18:36:37 +000023 * before processes are spawned to set core file size as unlimited.
24 * This is for debugging only. Don't use this is production, unless
25 * you want core dumps lying about....
Erik Andersen983b51b2000-04-04 18:14:25 +000026 */
27#define CORE_ENABLE_FLAG_FILE "/.init_enable_core"
28#include <sys/resource.h>
Erik Andersen983b51b2000-04-04 18:14:25 +000029
Glenn L McGrathbaf55a82002-08-22 18:22:10 +000030#define INITTAB "/etc/inittab" /* inittab file location */
Erik Andersen96e2abd2000-01-07 11:40:44 +000031#ifndef INIT_SCRIPT
Glenn L McGrathbaf55a82002-08-22 18:22:10 +000032#define INIT_SCRIPT "/etc/init.d/rcS" /* Default sysinit script. */
Erik Andersen96e2abd2000-01-07 11:40:44 +000033#endif
Erik Andersen9c88cac1999-12-30 09:25:17 +000034
Erik Andersen7dc16072000-01-04 01:10:25 +000035/* Allowed init action types */
Denis Vlasenkoad4da982008-04-05 04:24:23 +000036#define SYSINIT 0x01
37#define RESPAWN 0x02
38/* like respawn, but wait for <Enter> to be pressed on tty: */
39#define ASKFIRST 0x04
40#define WAIT 0x08
41#define ONCE 0x10
42#define CTRLALTDEL 0x20
43#define SHUTDOWN 0x40
44#define RESTART 0x80
Erik Andersen7dc16072000-01-04 01:10:25 +000045
Eric Andersen0298be82002-03-05 15:12:19 +000046/* Set up a linked list of init_actions, to be read from inittab */
47struct init_action {
Eric Andersen0298be82002-03-05 15:12:19 +000048 struct init_action *next;
Denis Vlasenko7a2ca5e2007-02-21 00:15:20 +000049 pid_t pid;
Denis Vlasenkoa37e7132008-02-19 02:57:07 +000050 uint8_t action_type;
Denis Vlasenko7a2ca5e2007-02-21 00:15:20 +000051 char terminal[CONSOLE_NAME_SIZE];
Denis Vlasenkoa37e7132008-02-19 02:57:07 +000052 char command[COMMAND_SIZE];
Erik Andersen7dc16072000-01-04 01:10:25 +000053};
Erik Andersen7dc16072000-01-04 01:10:25 +000054
Eric Andersen0298be82002-03-05 15:12:19 +000055/* Static variables */
56static struct init_action *init_action_list = NULL;
Glenn L McGrathbaf55a82002-08-22 18:22:10 +000057
Denis Vlasenko4c978632007-02-03 03:31:13 +000058static const char *log_console = VC_5;
Eric Andersen0298be82002-03-05 15:12:19 +000059static sig_atomic_t got_cont = 0;
Rob Landleybc68cd12006-03-10 19:22:06 +000060
61enum {
Denis Vlasenkoec27feb2007-02-17 15:52:02 +000062 L_LOG = 0x1,
63 L_CONSOLE = 0x2,
Glenn L McGrathbaf55a82002-08-22 18:22:10 +000064
Denis Vlasenko9b1381f2007-01-03 02:56:00 +000065#if ENABLE_FEATURE_EXTRA_QUIET
Rob Landleybc68cd12006-03-10 19:22:06 +000066 MAYBE_CONSOLE = 0x0,
Eric Andersen0298be82002-03-05 15:12:19 +000067#else
Denis Vlasenkoec27feb2007-02-17 15:52:02 +000068 MAYBE_CONSOLE = L_CONSOLE,
Eric Andersen0298be82002-03-05 15:12:19 +000069#endif
Glenn L McGrathbaf55a82002-08-22 18:22:10 +000070
Rob Landleybc68cd12006-03-10 19:22:06 +000071#ifndef RB_HALT_SYSTEM
Bernhard Reutner-Fischercf1f2ac2006-06-02 10:43:17 +000072 RB_HALT_SYSTEM = 0xcdef0123, /* FIXME: this overflows enum */
Rob Landleybc68cd12006-03-10 19:22:06 +000073 RB_ENABLE_CAD = 0x89abcdef,
74 RB_DISABLE_CAD = 0,
75 RB_POWER_OFF = 0x4321fedc,
76 RB_AUTOBOOT = 0x01234567,
Eric Andersen0298be82002-03-05 15:12:19 +000077#endif
Rob Landleybc68cd12006-03-10 19:22:06 +000078};
Erik Andersen42094cd2000-03-20 21:34:52 +000079
Denis Vlasenko6ca409e2007-08-12 20:58:27 +000080static const char *const environment[] = {
Glenn L McGrathb4a1baa2003-01-13 22:09:50 +000081 "HOME=/",
Denis Vlasenkof5f75c52007-06-12 22:35:19 +000082 bb_PATH_root_path,
Glenn L McGrathdc4e75e2003-09-02 02:36:18 +000083 "SHELL=/bin/sh",
Glenn L McGrathb4a1baa2003-01-13 22:09:50 +000084 "USER=root",
85 NULL
86};
87
Eric Andersen0298be82002-03-05 15:12:19 +000088/* Function prototypes */
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +000089static void halt_reboot_pwoff(int sig) NORETURN;
Eric Andersen0460ff21999-10-25 23:32:44 +000090
Denis Vlasenko21e20cb2008-01-04 15:10:47 +000091static void waitfor(pid_t pid)
Denis Vlasenkofb0eba72008-01-02 19:55:04 +000092{
Denis Vlasenko21e20cb2008-01-04 15:10:47 +000093 /* waitfor(run(x)): protect against failed fork inside run() */
94 if (pid <= 0)
95 return;
96
97 /* Wait for any child (prevent zombies from exiting orphaned processes)
98 * but exit the loop only when specified one has exited. */
99 while (wait(NULL) != pid)
100 continue;
Denis Vlasenkofb0eba72008-01-02 19:55:04 +0000101}
102
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000103static void loop_forever(void) NORETURN;
Eric Andersen74400cc2001-10-18 04:11:39 +0000104static void loop_forever(void)
Matt Kraai67a46402001-06-03 05:55:52 +0000105{
106 while (1)
Glenn L McGrathbaf55a82002-08-22 18:22:10 +0000107 sleep(1);
Matt Kraai67a46402001-06-03 05:55:52 +0000108}
Eric Andersen7ef1a5b2001-03-20 17:39:08 +0000109
Erik Andersen42094cd2000-03-20 21:34:52 +0000110/* Print a message to the specified device.
Denis Vlasenko671ca332008-02-19 14:13:20 +0000111 * "where" may be bitwise-or'd from L_LOG | L_CONSOLE
112 * NB: careful, we can be called after vfork!
113 */
Mike Frysingerbb50fdf2007-12-25 04:30:14 +0000114#define messageD(...) do { if (ENABLE_DEBUG_INIT) message(__VA_ARGS__); } while (0)
Denis Vlasenko671ca332008-02-19 14:13:20 +0000115static void message(int where, const char *fmt, ...)
Glenn L McGrathbaf55a82002-08-22 18:22:10 +0000116 __attribute__ ((format(printf, 2, 3)));
Denis Vlasenko671ca332008-02-19 14:13:20 +0000117static void message(int where, const char *fmt, ...)
Eric Andersen0460ff21999-10-25 23:32:44 +0000118{
Eric Andersen22237012003-01-23 07:08:26 +0000119 static int log_fd = -1;
Denis Vlasenko7a2ca5e2007-02-21 00:15:20 +0000120 va_list arguments;
121 int l;
122 char msg[128];
123
Glenn L McGrathb4a1baa2003-01-13 22:09:50 +0000124 msg[0] = '\r';
Denis Vlasenko7a2ca5e2007-02-21 00:15:20 +0000125 va_start(arguments, fmt);
126 vsnprintf(msg + 1, sizeof(msg) - 2, fmt, arguments);
Denis Vlasenkoec27feb2007-02-17 15:52:02 +0000127 va_end(arguments);
Denis Vlasenko7a2ca5e2007-02-21 00:15:20 +0000128 msg[sizeof(msg) - 2] = '\0';
129 l = strlen(msg);
Glenn L McGrathb4a1baa2003-01-13 22:09:50 +0000130
Mike Frysingerbb50fdf2007-12-25 04:30:14 +0000131 if (ENABLE_FEATURE_INIT_SYSLOG) {
132 /* Log the message to syslogd */
Denis Vlasenko671ca332008-02-19 14:13:20 +0000133 if (where & L_LOG) {
Bernhard Reutner-Fischerad2fa652008-07-21 11:16:39 +0000134 /* don't print out "\r" */
Mike Frysingerbb50fdf2007-12-25 04:30:14 +0000135 openlog(applet_name, 0, LOG_DAEMON);
136 syslog(LOG_INFO, "init: %s", msg + 1);
137 closelog();
138 }
139 msg[l++] = '\n';
140 msg[l] = '\0';
141 } else {
142 msg[l++] = '\n';
143 msg[l] = '\0';
144 /* Take full control of the log tty, and never close it.
145 * It's mine, all mine! Muhahahaha! */
146 if (log_fd < 0) {
147 if (!log_console) {
Bernhard Reutner-Fischerad2fa652008-07-21 11:16:39 +0000148 log_fd = STDERR_FILENO;
Denis Vlasenko7a2ca5e2007-02-21 00:15:20 +0000149 } else {
Mike Frysingerbb50fdf2007-12-25 04:30:14 +0000150 log_fd = device_open(log_console, O_WRONLY | O_NONBLOCK | O_NOCTTY);
151 if (log_fd < 0) {
152 bb_error_msg("can't log to %s", log_console);
Denis Vlasenko671ca332008-02-19 14:13:20 +0000153 where = L_CONSOLE;
Mike Frysingerbb50fdf2007-12-25 04:30:14 +0000154 } else {
155 close_on_exec_on(log_fd);
156 }
Denis Vlasenko7a2ca5e2007-02-21 00:15:20 +0000157 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000158 }
Denis Vlasenko671ca332008-02-19 14:13:20 +0000159 if (where & L_LOG) {
Mike Frysingerbb50fdf2007-12-25 04:30:14 +0000160 full_write(log_fd, msg, l);
Bernhard Reutner-Fischerad2fa652008-07-21 11:16:39 +0000161 if (log_fd == STDERR_FILENO)
Mike Frysingerbb50fdf2007-12-25 04:30:14 +0000162 return; /* don't print dup messages */
163 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000164 }
Eric Andersen4c781471999-11-26 08:12:56 +0000165
Denis Vlasenko671ca332008-02-19 14:13:20 +0000166 if (where & L_CONSOLE) {
Denis Vlasenko7a2ca5e2007-02-21 00:15:20 +0000167 /* Send console messages to console so people will see them. */
Bernhard Reutner-Fischer5e25ddb2008-05-19 09:48:17 +0000168 full_write(STDERR_FILENO, msg, l);
Eric Andersen3ae0c781999-11-04 01:13:21 +0000169 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000170}
171
Denis Vlasenko7a2ca5e2007-02-21 00:15:20 +0000172/* From <linux/serial.h> */
173struct serial_struct {
174 int type;
175 int line;
176 unsigned int port;
177 int irq;
178 int flags;
179 int xmit_fifo_size;
180 int custom_divisor;
181 int baud_base;
182 unsigned short close_delay;
183 char io_type;
184 char reserved_char[1];
185 int hub6;
186 unsigned short closing_wait; /* time to wait before closing */
187 unsigned short closing_wait2; /* no longer used... */
188 unsigned char *iomem_base;
189 unsigned short iomem_reg_shift;
190 unsigned int port_high;
191 unsigned long iomap_base; /* cookie passed into ioremap */
192 int reserved[1];
193 /* Paranoia (imagine 64bit kernel overwriting 32bit userspace stack) */
194 uint32_t bbox_reserved[16];
195};
Eric Andersen74400cc2001-10-18 04:11:39 +0000196static void console_init(void)
Eric Andersen0460ff21999-10-25 23:32:44 +0000197{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000198 struct serial_struct sr;
199 char *s;
Eric Andersen0460ff21999-10-25 23:32:44 +0000200
Denis Vlasenko7a2ca5e2007-02-21 00:15:20 +0000201 s = getenv("CONSOLE");
Bernhard Reutner-Fischerad2fa652008-07-21 11:16:39 +0000202 if (!s)
203 s = getenv("console");
Denis Vlasenko7a2ca5e2007-02-21 00:15:20 +0000204 if (s) {
205 int fd = open(s, O_RDWR | O_NONBLOCK | O_NOCTTY);
206 if (fd >= 0) {
Bernhard Reutner-Fischerad2fa652008-07-21 11:16:39 +0000207 dup2(fd, STDIN_FILENO);
208 dup2(fd, STDOUT_FILENO);
209 xmove_fd(fd, STDERR_FILENO);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000210 }
Denis Vlasenko7a2ca5e2007-02-21 00:15:20 +0000211 messageD(L_LOG, "console='%s'", s);
Denis Vlasenkod8540f72007-06-14 07:53:06 +0000212 } else {
Denis Vlasenkofb274df2008-03-17 13:26:51 +0000213 /* Make sure fd 0,1,2 are not closed
214 * (so that they won't be used by future opens) */
Denis Vlasenkod48e81f2008-07-06 07:00:11 +0000215 bb_sanitize_stdio();
Eric Andersen07e52971999-11-07 07:38:08 +0000216 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000217
Denis Vlasenko7a2ca5e2007-02-21 00:15:20 +0000218 s = getenv("TERM");
Denis Vlasenkoe421b5e2008-03-17 22:01:42 +0000219 if (ioctl(STDIN_FILENO, TIOCGSERIAL, &sr) == 0) {
Denis Vlasenko2afabe82007-12-10 07:06:04 +0000220 /* Force the TERM setting to vt102 for serial console
Denis Vlasenko7a2ca5e2007-02-21 00:15:20 +0000221 * if TERM is set to linux (the default) */
222 if (!s || strcmp(s, "linux") == 0)
223 putenv((char*)"TERM=vt102");
Mike Frysingerbb50fdf2007-12-25 04:30:14 +0000224 if (!ENABLE_FEATURE_INIT_SYSLOG)
225 log_console = NULL;
Denis Vlasenko7a2ca5e2007-02-21 00:15:20 +0000226 } else if (!s)
227 putenv((char*)"TERM=linux");
Eric Andersen0460ff21999-10-25 23:32:44 +0000228}
Glenn L McGrathbaf55a82002-08-22 18:22:10 +0000229
Denis Vlasenko671ca332008-02-19 14:13:20 +0000230/* Set terminal settings to reasonable defaults.
231 * NB: careful, we can be called after vfork! */
Denis Vlasenko2afabe82007-12-10 07:06:04 +0000232static void set_sane_term(void)
Eric Andersen7ef1a5b2001-03-20 17:39:08 +0000233{
Denis Vlasenko2afabe82007-12-10 07:06:04 +0000234 struct termios tty;
Eric Andersen7ef1a5b2001-03-20 17:39:08 +0000235
Denis Vlasenko2afabe82007-12-10 07:06:04 +0000236 tcgetattr(STDIN_FILENO, &tty);
237
238 /* set control chars */
239 tty.c_cc[VINTR] = 3; /* C-c */
240 tty.c_cc[VQUIT] = 28; /* C-\ */
241 tty.c_cc[VERASE] = 127; /* C-? */
242 tty.c_cc[VKILL] = 21; /* C-u */
243 tty.c_cc[VEOF] = 4; /* C-d */
244 tty.c_cc[VSTART] = 17; /* C-q */
245 tty.c_cc[VSTOP] = 19; /* C-s */
246 tty.c_cc[VSUSP] = 26; /* C-z */
247
Bernhard Reutner-Fischerad2fa652008-07-21 11:16:39 +0000248 /* use line discipline 0 */
Denis Vlasenko2afabe82007-12-10 07:06:04 +0000249 tty.c_line = 0;
250
251 /* Make it be sane */
252 tty.c_cflag &= CBAUD | CBAUDEX | CSIZE | CSTOPB | PARENB | PARODD;
253 tty.c_cflag |= CREAD | HUPCL | CLOCAL;
254
255 /* input modes */
256 tty.c_iflag = ICRNL | IXON | IXOFF;
257
258 /* output modes */
259 tty.c_oflag = OPOST | ONLCR;
260
261 /* local modes */
262 tty.c_lflag =
263 ISIG | ICANON | ECHO | ECHOE | ECHOK | ECHOCTL | ECHOKE | IEXTEN;
264
265 tcsetattr(STDIN_FILENO, TCSANOW, &tty);
Eric Andersen7ef1a5b2001-03-20 17:39:08 +0000266}
267
Denis Vlasenko671ca332008-02-19 14:13:20 +0000268/* Open the new terminal device.
269 * NB: careful, we can be called after vfork! */
Denis Vlasenkod55268d2007-12-26 18:32:58 +0000270static void open_stdio_to_tty(const char* tty_name, int exit_on_failure)
Denis Vlasenko7a2ca5e2007-02-21 00:15:20 +0000271{
272 /* empty tty_name means "use init's tty", else... */
273 if (tty_name[0]) {
Denis Vlasenkod55268d2007-12-26 18:32:58 +0000274 int fd;
Bernhard Reutner-Fischerad2fa652008-07-21 11:16:39 +0000275 close(STDIN_FILENO);
Denis Vlasenkod55268d2007-12-26 18:32:58 +0000276 /* fd can be only < 0 or 0: */
277 fd = device_open(tty_name, O_RDWR);
278 if (fd) {
Bernhard Reutner-Fischera53de7f2008-07-21 13:46:54 +0000279 message(L_LOG | L_CONSOLE, "can't open %s: %s",
Denis Vlasenko7a2ca5e2007-02-21 00:15:20 +0000280 tty_name, strerror(errno));
Denis Vlasenkod55268d2007-12-26 18:32:58 +0000281 if (exit_on_failure)
Bernhard Reutner-Fischer636a1f82008-05-19 09:29:47 +0000282 _exit(EXIT_FAILURE);
Denis Vlasenkod55268d2007-12-26 18:32:58 +0000283 if (ENABLE_DEBUG_INIT)
Mike Frysingerbb50fdf2007-12-25 04:30:14 +0000284 _exit(2);
Bernhard Reutner-Fischerad2fa652008-07-21 11:16:39 +0000285 /* NB: we don't reach this if we were called after vfork.
286 * Thus halt_reboot_pwoff() itself need not be vfork-safe. */
Denis Vlasenkod55268d2007-12-26 18:32:58 +0000287 halt_reboot_pwoff(SIGUSR1); /* halt the system */
Denis Vlasenko7a2ca5e2007-02-21 00:15:20 +0000288 }
Bernhard Reutner-Fischerad2fa652008-07-21 11:16:39 +0000289 dup2(STDIN_FILENO, STDOUT_FILENO);
290 dup2(STDIN_FILENO, STDERR_FILENO);
Bernhard Reutner-Fischer0da069d2006-05-29 12:54:16 +0000291 }
Denis Vlasenkod238a472007-03-05 19:22:04 +0000292 set_sane_term();
Bernhard Reutner-Fischer0da069d2006-05-29 12:54:16 +0000293}
294
Denis Vlasenko671ca332008-02-19 14:13:20 +0000295/* Wrapper around exec:
296 * Takes string (max COMMAND_SIZE chars).
297 * If chars like '>' detected, execs '[-]/bin/sh -c "exec ......."'.
298 * Otherwise splits words on whitespace, deals with leading dash,
299 * and uses plain exec().
300 * NB: careful, we can be called after vfork!
Denis Vlasenkoa37e7132008-02-19 02:57:07 +0000301 */
302static void init_exec(const char *command)
303{
304 char *cmd[COMMAND_SIZE / 2];
305 char buf[COMMAND_SIZE + 6]; /* COMMAND_SIZE+strlen("exec ")+1 */
Denis Vlasenko671ca332008-02-19 14:13:20 +0000306 int dash = (command[0] == '-' /* maybe? && command[1] == '/' */);
Denis Vlasenkoa37e7132008-02-19 02:57:07 +0000307
308 /* See if any special /bin/sh requiring characters are present */
309 if (strpbrk(command, "~`!$^&*()=|\\{}[];\"'<>?") != NULL) {
310 strcpy(buf, "exec ");
311 strcpy(buf + 5, command + dash); /* excluding "-" */
Denis Vlasenko671ca332008-02-19 14:13:20 +0000312 /* NB: LIBBB_DEFAULT_LOGIN_SHELL define has leading dash */
Denis Vlasenkoa37e7132008-02-19 02:57:07 +0000313 cmd[0] = (char*)(LIBBB_DEFAULT_LOGIN_SHELL + !dash);
314 cmd[1] = (char*)"-c";
315 cmd[2] = buf;
316 cmd[3] = NULL;
317 } else {
318 /* Convert command (char*) into cmd (char**, one word per string) */
319 char *word, *next;
320 int i = 0;
321 next = strcpy(buf, command); /* including "-" */
322 while ((word = strsep(&next, " \t")) != NULL) {
323 if (*word != '\0') { /* not two spaces/tabs together? */
324 cmd[i] = word;
325 i++;
326 }
327 }
328 cmd[i] = NULL;
329 }
330 /* If we saw leading "-", it is interactive shell.
331 * Try harder to give it a controlling tty.
332 * And skip "-" in actual exec call. */
333 if (dash) {
334 /* _Attempt_ to make stdin a controlling tty. */
335 if (ENABLE_FEATURE_INIT_SCTTY)
336 ioctl(STDIN_FILENO, TIOCSCTTY, 0 /*only try, don't steal*/);
337 }
338 BB_EXECVP(cmd[0] + dash, cmd);
Bernhard Reutner-Fischera53de7f2008-07-21 13:46:54 +0000339 message(L_LOG | L_CONSOLE, "cannot run '%s': %s", cmd[0], strerror(errno));
Denis Vlasenkoa37e7132008-02-19 02:57:07 +0000340 /* returns if execvp fails */
341}
342
Denis Vlasenko2afabe82007-12-10 07:06:04 +0000343/* Used only by run_actions */
Glenn L McGrathb4a1baa2003-01-13 22:09:50 +0000344static pid_t run(const struct init_action *a)
Eric Andersen0298be82002-03-05 15:12:19 +0000345{
Mike Frysinger10427ab2005-07-06 05:00:48 +0000346 pid_t pid;
Eric Andersen0298be82002-03-05 15:12:19 +0000347 sigset_t nmask, omask;
Erik Andersenac6e71f2000-01-08 22:04:33 +0000348
Denis Vlasenko2afabe82007-12-10 07:06:04 +0000349 /* Block sigchild while forking (why?) */
Eric Andersen0298be82002-03-05 15:12:19 +0000350 sigemptyset(&nmask);
351 sigaddset(&nmask, SIGCHLD);
352 sigprocmask(SIG_BLOCK, &nmask, &omask);
Denis Vlasenkoad4da982008-04-05 04:24:23 +0000353 if (BB_MMU && (a->action_type & ASKFIRST))
354 pid = fork();
355 else
356 pid = vfork();
Denis Vlasenko966bb432007-02-27 19:20:33 +0000357 sigprocmask(SIG_SETMASK, &omask, NULL);
Eric Andersen0298be82002-03-05 15:12:19 +0000358
Denis Vlasenko2afabe82007-12-10 07:06:04 +0000359 if (pid < 0)
Bernhard Reutner-Fischera53de7f2008-07-21 13:46:54 +0000360 message(L_LOG | L_CONSOLE, "can't fork");
Denis Vlasenko966bb432007-02-27 19:20:33 +0000361 if (pid)
362 return pid;
Eric Andersen0298be82002-03-05 15:12:19 +0000363
Denis Vlasenko2afabe82007-12-10 07:06:04 +0000364 /* Child */
365
Denis Vlasenko966bb432007-02-27 19:20:33 +0000366 /* Reset signal handlers that were set by the parent process */
Denis Vlasenko25591c32008-02-16 22:58:56 +0000367 bb_signals(0
368 + (1 << SIGUSR1)
369 + (1 << SIGUSR2)
370 + (1 << SIGINT)
371 + (1 << SIGTERM)
372 + (1 << SIGHUP)
373 + (1 << SIGQUIT)
374 + (1 << SIGCONT)
375 + (1 << SIGSTOP)
376 + (1 << SIGTSTP)
377 , SIG_DFL);
Eric Andersen2f6c04f1999-11-01 23:59:44 +0000378
Denis Vlasenko966bb432007-02-27 19:20:33 +0000379 /* Create a new session and make ourself the process
380 * group leader */
381 setsid();
Eric Andersen599e3ce2002-07-03 11:08:10 +0000382
Denis Vlasenko966bb432007-02-27 19:20:33 +0000383 /* Open the new terminal device */
Denis Vlasenko671ca332008-02-19 14:13:20 +0000384 open_stdio_to_tty(a->terminal, 1 /* - exit if open fails */);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000385
Denis Vlasenko671ca332008-02-19 14:13:20 +0000386// NB: do not enable unless you change vfork to fork above
Denis Vlasenko2afabe82007-12-10 07:06:04 +0000387#ifdef BUT_RUN_ACTIONS_ALREADY_DOES_WAITING
Denis Vlasenko966bb432007-02-27 19:20:33 +0000388 /* If the init Action requires us to wait, then force the
389 * supplied terminal to be the controlling tty. */
Denis Vlasenkoa37e7132008-02-19 02:57:07 +0000390 if (a->action_type & (SYSINIT | WAIT | CTRLALTDEL | SHUTDOWN | RESTART)) {
Denis Vlasenko966bb432007-02-27 19:20:33 +0000391 /* Now fork off another process to just hang around */
Denis Vlasenko6bef3d12007-11-06 03:05:54 +0000392 pid = fork();
Denis Vlasenkof6ccc622007-11-10 01:57:35 +0000393 if (pid < 0) {
Bernhard Reutner-Fischera53de7f2008-07-21 13:46:54 +0000394 message(L_LOG | L_CONSOLE, "can't fork");
Bernhard Reutner-Fischer636a1f82008-05-19 09:29:47 +0000395 _exit(EXIT_FAILURE);
Denis Vlasenko966bb432007-02-27 19:20:33 +0000396 }
397
398 if (pid > 0) {
Denis Vlasenko2afabe82007-12-10 07:06:04 +0000399 /* Parent - wait till the child is done */
Denis Vlasenko25591c32008-02-16 22:58:56 +0000400 bb_signals(0
401 + (1 << SIGINT)
402 + (1 << SIGTSTP)
403 + (1 << SIGQUIT)
404 , SIG_IGN);
Denis Vlasenko966bb432007-02-27 19:20:33 +0000405 signal(SIGCHLD, SIG_DFL);
406
Denis Vlasenko2afabe82007-12-10 07:06:04 +0000407 waitfor(pid);
Denis Vlasenko966bb432007-02-27 19:20:33 +0000408 /* See if stealing the controlling tty back is necessary */
409 if (tcgetpgrp(0) != getpid())
Bernhard Reutner-Fischer636a1f82008-05-19 09:29:47 +0000410 _exit(EXIT_SUCCESS);
Denis Vlasenko966bb432007-02-27 19:20:33 +0000411
412 /* Use a temporary process to steal the controlling tty. */
Denis Vlasenko6bef3d12007-11-06 03:05:54 +0000413 pid = fork();
414 if (pid < 0) {
Bernhard Reutner-Fischera53de7f2008-07-21 13:46:54 +0000415 message(L_LOG | L_CONSOLE, "can't fork");
Bernhard Reutner-Fischer636a1f82008-05-19 09:29:47 +0000416 _exit(EXIT_FAILURE);
Eric Andersen0298be82002-03-05 15:12:19 +0000417 }
Denis Vlasenko966bb432007-02-27 19:20:33 +0000418 if (pid == 0) {
419 setsid();
420 ioctl(0, TIOCSCTTY, 1);
Bernhard Reutner-Fischer636a1f82008-05-19 09:29:47 +0000421 _exit(EXIT_SUCCESS);
Eric Andersen0298be82002-03-05 15:12:19 +0000422 }
Denis Vlasenko2afabe82007-12-10 07:06:04 +0000423 waitfor(pid);
Bernhard Reutner-Fischer636a1f82008-05-19 09:29:47 +0000424 _exit(EXIT_SUCCESS);
Eric Andersen0298be82002-03-05 15:12:19 +0000425 }
Denis Vlasenko2afabe82007-12-10 07:06:04 +0000426 /* Child - fall though to actually execute things */
Denis Vlasenko966bb432007-02-27 19:20:33 +0000427 }
Denis Vlasenko5adfa442007-12-25 16:08:53 +0000428#endif
Denis Vlasenko671ca332008-02-19 14:13:20 +0000429
Denis Vlasenkoad4da982008-04-05 04:24:23 +0000430 /* NB: on NOMMU we can't wait for input in child, so
431 * "askfirst" will work the same as "respawn". */
Denis Vlasenko671ca332008-02-19 14:13:20 +0000432 if (BB_MMU && (a->action_type & ASKFIRST)) {
Denis Vlasenko6ca409e2007-08-12 20:58:27 +0000433 static const char press_enter[] ALIGN1 =
Denis Vlasenko966bb432007-02-27 19:20:33 +0000434#ifdef CUSTOMIZED_BANNER
435#include CUSTOMIZED_BANNER
Eric Andersen1f50e842004-08-16 09:29:42 +0000436#endif
Denis Vlasenko966bb432007-02-27 19:20:33 +0000437 "\nPlease press Enter to activate this console. ";
438 char c;
439 /*
440 * Save memory by not exec-ing anything large (like a shell)
441 * before the user wants it. This is critical if swap is not
442 * enabled and the system has low memory. Generally this will
443 * be run on the second virtual console, and the first will
444 * be allowed to start a shell or whatever an init script
445 * specifies.
446 */
447 messageD(L_LOG, "waiting for enter to start '%s'"
448 "(pid %d, tty '%s')\n",
Denis Vlasenkoa37e7132008-02-19 02:57:07 +0000449 a->command, getpid(), a->terminal);
Bernhard Reutner-Fischer5e25ddb2008-05-19 09:48:17 +0000450 full_write(STDOUT_FILENO, press_enter, sizeof(press_enter) - 1);
451 while (safe_read(STDIN_FILENO, &c, 1) == 1 && c != '\n')
Denis Vlasenko2afabe82007-12-10 07:06:04 +0000452 continue;
Denis Vlasenko966bb432007-02-27 19:20:33 +0000453 }
Denis Vlasenko2afabe82007-12-10 07:06:04 +0000454
Mike Frysingerbb50fdf2007-12-25 04:30:14 +0000455 if (ENABLE_FEATURE_INIT_COREDUMPS) {
Denis Vlasenko966bb432007-02-27 19:20:33 +0000456 struct stat sb;
457 if (stat(CORE_ENABLE_FLAG_FILE, &sb) == 0) {
458 struct rlimit limit;
Denis Vlasenko966bb432007-02-27 19:20:33 +0000459 limit.rlim_cur = RLIM_INFINITY;
460 limit.rlim_max = RLIM_INFINITY;
461 setrlimit(RLIMIT_CORE, &limit);
Erik Andersen983b51b2000-04-04 18:14:25 +0000462 }
Erik Andersen05df2392000-01-13 04:43:48 +0000463 }
Mike Frysingerbb50fdf2007-12-25 04:30:14 +0000464
Denis Vlasenkoa37e7132008-02-19 02:57:07 +0000465 /* Log the process name and args */
466 message(L_LOG, "starting pid %d, tty '%s': '%s'",
467 getpid(), a->terminal, a->command);
468
Denis Vlasenko966bb432007-02-27 19:20:33 +0000469 /* Now run it. The new program will take over this PID,
470 * so nothing further in init.c should be run. */
Denis Vlasenkoa37e7132008-02-19 02:57:07 +0000471 init_exec(a->command);
Denis Vlasenko966bb432007-02-27 19:20:33 +0000472 /* We're still here? Some error happened. */
Denis Vlasenko966bb432007-02-27 19:20:33 +0000473 _exit(-1);
Eric Andersen0460ff21999-10-25 23:32:44 +0000474}
475
Bernhard Reutner-Fischer52593612008-07-21 11:53:04 +0000476static void delete_init_action(struct init_action *action)
477{
478 struct init_action *a, *b = NULL;
479
480 for (a = init_action_list; a; b = a, a = a->next) {
481 if (a == action) {
482 if (b == NULL) {
483 init_action_list = a->next;
484 } else {
485 b->next = a->next;
486 }
487 free(a);
488 break;
489 }
490 }
491}
492
Eric Andersen0298be82002-03-05 15:12:19 +0000493/* Run all commands of a particular type */
Denis Vlasenkoa37e7132008-02-19 02:57:07 +0000494static void run_actions(int action_type)
Eric Andersen219d6f51999-11-02 19:43:01 +0000495{
Eric Andersen0298be82002-03-05 15:12:19 +0000496 struct init_action *a, *tmp;
Eric Andersen219d6f51999-11-02 19:43:01 +0000497
Eric Andersen0298be82002-03-05 15:12:19 +0000498 for (a = init_action_list; a; a = tmp) {
499 tmp = a->next;
Denis Vlasenkoad4da982008-04-05 04:24:23 +0000500 if (a->action_type & action_type) {
Denis Vlasenkod7e2e122007-12-10 08:40:29 +0000501 // Pointless: run() will error out if open of device fails.
502 ///* a->terminal of "" means "init's console" */
503 //if (a->terminal[0] && access(a->terminal, R_OK | W_OK)) {
504 // //message(L_LOG | L_CONSOLE, "Device %s cannot be opened in RW mode", a->terminal /*, strerror(errno)*/);
505 // delete_init_action(a);
506 //} else
Denis Vlasenkoa37e7132008-02-19 02:57:07 +0000507 if (a->action_type & (SYSINIT | WAIT | CTRLALTDEL | SHUTDOWN | RESTART)) {
Denis Vlasenko2afabe82007-12-10 07:06:04 +0000508 waitfor(run(a));
Eric Andersen0298be82002-03-05 15:12:19 +0000509 delete_init_action(a);
Denis Vlasenkoa37e7132008-02-19 02:57:07 +0000510 } else if (a->action_type & ONCE) {
Eric Andersen0298be82002-03-05 15:12:19 +0000511 run(a);
512 delete_init_action(a);
Denis Vlasenkoa37e7132008-02-19 02:57:07 +0000513 } else if (a->action_type & (RESPAWN | ASKFIRST)) {
Eric Andersen0298be82002-03-05 15:12:19 +0000514 /* Only run stuff with pid==0. If they have
515 * a pid, that means it is still running */
516 if (a->pid == 0) {
517 a->pid = run(a);
518 }
519 }
Erik Andersene132f4b2000-02-09 04:16:43 +0000520 }
521 }
522}
523
Eric Andersen2c1de612003-04-24 11:41:28 +0000524static void init_reboot(unsigned long magic)
525{
526 pid_t pid;
Bernhard Reutner-Fischer636a1f82008-05-19 09:29:47 +0000527 /* We have to fork here, since the kernel calls do_exit(EXIT_SUCCESS) in
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000528 * linux/kernel/sys.c, which can cause the machine to panic when
Eric Andersen2c1de612003-04-24 11:41:28 +0000529 * the init process is killed.... */
Denis Vlasenko9b1381f2007-01-03 02:56:00 +0000530 pid = vfork();
531 if (pid == 0) { /* child */
Eric Andersen2c1de612003-04-24 11:41:28 +0000532 reboot(magic);
Bernhard Reutner-Fischer636a1f82008-05-19 09:29:47 +0000533 _exit(EXIT_SUCCESS);
Eric Andersen2c1de612003-04-24 11:41:28 +0000534 }
Denis Vlasenkofb0eba72008-01-02 19:55:04 +0000535 waitfor(pid);
Eric Andersen2c1de612003-04-24 11:41:28 +0000536}
537
Denis Vlasenkod55268d2007-12-26 18:32:58 +0000538static void kill_all_processes(void)
Eric Andersencc8ed391999-10-05 16:24:54 +0000539{
Eric Andersena9cc8962002-09-16 06:49:06 +0000540 /* run everything to be run at "shutdown". This is done _prior_
541 * to killing everything, in case people wish to use scripts to
542 * shut things down gracefully... */
543 run_actions(SHUTDOWN);
544
Eric Andersen72f9a422001-10-28 05:12:20 +0000545 /* first disable all our signals */
Denis Vlasenko3f165fa2008-03-17 08:29:08 +0000546 sigprocmask_allsigs(SIG_BLOCK);
Erik Andersen7dc16072000-01-04 01:10:25 +0000547
Denis Vlasenko7a2ca5e2007-02-21 00:15:20 +0000548 message(L_CONSOLE | L_LOG, "The system is going down NOW!");
549
Erik Andersene49d5ec2000-02-08 19:58:47 +0000550 /* Allow Ctrl-Alt-Del to reboot system. */
Erik Andersen4f3f7572000-04-28 00:18:56 +0000551 init_reboot(RB_ENABLE_CAD);
Erik Andersene132f4b2000-02-09 04:16:43 +0000552
Erik Andersene49d5ec2000-02-08 19:58:47 +0000553 /* Send signals to every process _except_ pid 1 */
Denis Vlasenko7a2ca5e2007-02-21 00:15:20 +0000554 message(L_CONSOLE | L_LOG, "Sending SIG%s to all processes", "TERM");
Erik Andersene49d5ec2000-02-08 19:58:47 +0000555 kill(-1, SIGTERM);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000556 sync();
Denis Vlasenko7a2ca5e2007-02-21 00:15:20 +0000557 sleep(1);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000558
Denis Vlasenko7a2ca5e2007-02-21 00:15:20 +0000559 message(L_CONSOLE | L_LOG, "Sending SIG%s to all processes", "KILL");
Erik Andersene49d5ec2000-02-08 19:58:47 +0000560 kill(-1, SIGKILL);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000561 sync();
Denis Vlasenko7a2ca5e2007-02-21 00:15:20 +0000562 sleep(1);
Eric Andersencc8ed391999-10-05 16:24:54 +0000563}
564
Denis Vlasenkod55268d2007-12-26 18:32:58 +0000565static void halt_reboot_pwoff(int sig)
Denis Vlasenko2afabe82007-12-10 07:06:04 +0000566{
Bernhard Reutner-Fischer54d50a02008-07-17 14:00:42 +0000567 const char *m = "halt";
Denis Vlasenko2afabe82007-12-10 07:06:04 +0000568 int rb;
569
Denis Vlasenkod55268d2007-12-26 18:32:58 +0000570 kill_all_processes();
Denis Vlasenko2afabe82007-12-10 07:06:04 +0000571
Denis Vlasenko2afabe82007-12-10 07:06:04 +0000572 rb = RB_HALT_SYSTEM;
573 if (sig == SIGTERM) {
574 m = "reboot";
575 rb = RB_AUTOBOOT;
576 } else if (sig == SIGUSR2) {
577 m = "poweroff";
578 rb = RB_POWER_OFF;
579 }
580 message(L_CONSOLE | L_LOG, "Requesting system %s", m);
581 /* allow time for last message to reach serial console */
582 sleep(2);
583 init_reboot(rb);
584 loop_forever();
585}
586
Denis Vlasenkoa58a6372008-02-19 12:10:18 +0000587/* Handler for QUIT - exec "restart" action,
Denis Vlasenkod55268d2007-12-26 18:32:58 +0000588 * else (no such action defined) do nothing */
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000589static void exec_restart_action(int sig UNUSED_PARAM)
Eric Andersen730f8262001-12-17 23:13:08 +0000590{
Denis Vlasenkoa37e7132008-02-19 02:57:07 +0000591 struct init_action *a;
Glenn L McGrathbaf55a82002-08-22 18:22:10 +0000592
Denis Vlasenkoa37e7132008-02-19 02:57:07 +0000593 for (a = init_action_list; a; a = a->next) {
594 if (a->action_type & RESTART) {
Denis Vlasenkod55268d2007-12-26 18:32:58 +0000595 kill_all_processes();
Eric Andersen5222d312002-07-03 05:15:23 +0000596
Denis Vlasenkod55268d2007-12-26 18:32:58 +0000597 /* unblock all signals (blocked in kill_all_processes()) */
Denis Vlasenko3f165fa2008-03-17 08:29:08 +0000598 sigprocmask_allsigs(SIG_UNBLOCK);
Eric Andersen5222d312002-07-03 05:15:23 +0000599
Eric Andersen3c8064f2003-07-05 08:29:01 +0000600 /* Open the new terminal device */
Denis Vlasenkod55268d2007-12-26 18:32:58 +0000601 open_stdio_to_tty(a->terminal, 0 /* - halt if open fails */);
Eric Andersen3c8064f2003-07-05 08:29:01 +0000602
Denis Vlasenkoec27feb2007-02-17 15:52:02 +0000603 messageD(L_CONSOLE | L_LOG, "Trying to re-exec %s", a->command);
Denis Vlasenkoa37e7132008-02-19 02:57:07 +0000604 init_exec(a->command);
Eric Andersen730f8262001-12-17 23:13:08 +0000605 sleep(2);
606 init_reboot(RB_HALT_SYSTEM);
607 loop_forever();
608 }
609 }
610}
611
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000612static void ctrlaltdel_signal(int sig UNUSED_PARAM)
Eric Andersenc97ec342001-04-03 18:01:51 +0000613{
614 run_actions(CTRLALTDEL);
615}
616
Eric Andersen0298be82002-03-05 15:12:19 +0000617/* The SIGSTOP & SIGTSTP handler */
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000618static void stop_handler(int sig UNUSED_PARAM)
Eric Andersened8a9be2001-11-30 19:10:58 +0000619{
Eric Andersen0298be82002-03-05 15:12:19 +0000620 int saved_errno = errno;
Eric Andersened8a9be2001-11-30 19:10:58 +0000621
622 got_cont = 0;
Glenn L McGrathbaf55a82002-08-22 18:22:10 +0000623 while (!got_cont)
624 pause();
Denis Vlasenko2afabe82007-12-10 07:06:04 +0000625
Eric Andersened8a9be2001-11-30 19:10:58 +0000626 errno = saved_errno;
627}
628
Glenn L McGrathbaf55a82002-08-22 18:22:10 +0000629/* The SIGCONT handler */
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000630static void cont_handler(int sig UNUSED_PARAM)
Eric Andersened8a9be2001-11-30 19:10:58 +0000631{
632 got_cont = 1;
633}
634
Denis Vlasenkoa37e7132008-02-19 02:57:07 +0000635static void new_init_action(uint8_t action_type, const char *command, const char *cons)
Erik Andersen7dc16072000-01-04 01:10:25 +0000636{
Denis Vlasenkoa37e7132008-02-19 02:57:07 +0000637 struct init_action *a, *last;
Erik Andersen9e737252000-01-06 01:16:13 +0000638
Denis Vlasenkoa37e7132008-02-19 02:57:07 +0000639// Why?
640// if (strcmp(cons, bb_dev_null) == 0 && (action & ASKFIRST))
641// return;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000642
Eric Andersen0298be82002-03-05 15:12:19 +0000643 /* Append to the end of the list */
Eric Andersen22718092004-10-08 08:17:39 +0000644 for (a = last = init_action_list; a; a = a->next) {
Eric Andersen82baf632004-10-08 08:21:54 +0000645 /* don't enter action if it's already in the list,
646 * but do overwrite existing actions */
Denis Vlasenkoec27feb2007-02-17 15:52:02 +0000647 if ((strcmp(a->command, command) == 0)
Denis Vlasenkob74a2db2008-07-21 21:34:51 +0000648 && (strcmp(a->terminal, cons) == 0)
649 ) {
Denis Vlasenkoa37e7132008-02-19 02:57:07 +0000650 a->action_type = action_type;
Eric Andersen6fd0e312003-07-22 09:48:56 +0000651 return;
652 }
Eric Andersen22718092004-10-08 08:17:39 +0000653 last = a;
Eric Andersen6fd0e312003-07-22 09:48:56 +0000654 }
Denis Vlasenko7a2ca5e2007-02-21 00:15:20 +0000655
Denis Vlasenkoa37e7132008-02-19 02:57:07 +0000656 a = xzalloc(sizeof(*a));
Eric Andersen22718092004-10-08 08:17:39 +0000657 if (last) {
Denis Vlasenkoa37e7132008-02-19 02:57:07 +0000658 last->next = a;
Eric Andersen1644db92001-09-05 20:18:15 +0000659 } else {
Denis Vlasenkoa37e7132008-02-19 02:57:07 +0000660 init_action_list = a;
Eric Andersen1644db92001-09-05 20:18:15 +0000661 }
Denis Vlasenkoa37e7132008-02-19 02:57:07 +0000662 a->action_type = action_type;
663 safe_strncpy(a->command, command, sizeof(a->command));
664 safe_strncpy(a->terminal, cons, sizeof(a->terminal));
Denis Vlasenko7a2ca5e2007-02-21 00:15:20 +0000665 messageD(L_LOG | L_CONSOLE, "command='%s' action=%d tty='%s'\n",
Denis Vlasenkoa37e7132008-02-19 02:57:07 +0000666 a->command, a->action_type, a->terminal);
Erik Andersen7dc16072000-01-04 01:10:25 +0000667}
668
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000669/* NOTE that if CONFIG_FEATURE_USE_INITTAB is NOT defined,
Erik Andersen9e737252000-01-06 01:16:13 +0000670 * then parse_inittab() simply adds in some default
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000671 * actions(i.e., runs INIT_SCRIPT and then starts a pair
672 * of "askfirst" shells). If CONFIG_FEATURE_USE_INITTAB
673 * _is_ defined, but /etc/inittab is missing, this
Erik Andersen812d4662000-01-07 18:30:40 +0000674 * results in the same set of default behaviors.
Glenn L McGrathbaf55a82002-08-22 18:22:10 +0000675 */
Eric Andersen3e6ff902001-03-09 21:24:12 +0000676static void parse_inittab(void)
Erik Andersen7dc16072000-01-04 01:10:25 +0000677{
Bernhard Reutner-Fischer54d50a02008-07-17 14:00:42 +0000678 char *token[4];
Denis Vlasenkoa474b682008-07-17 17:58:44 +0000679 /* order must correspond to SYSINIT..RESTART constants */
680 static const char actions[] ALIGN1 =
681 "sysinit\0""respawn\0""askfirst\0""wait\0""once\0"
682 "ctrlaltdel\0""shutdown\0""restart\0";
Bernhard Reutner-Fischer54d50a02008-07-17 14:00:42 +0000683
Denis Vlasenko5415c852008-07-21 23:05:26 +0000684 parser_t *parser = config_open2(INITTAB, fopen_for_read);
Bernhard Reutner-Fischer54d50a02008-07-17 14:00:42 +0000685 /* No inittab file -- set up some default behavior */
686 if (parser == NULL) {
687 /* Reboot on Ctrl-Alt-Del */
688 new_init_action(CTRLALTDEL, "reboot", "");
689 /* Umount all filesystems on halt/reboot */
690 new_init_action(SHUTDOWN, "umount -a -r", "");
691 /* Swapoff on halt/reboot */
692 if (ENABLE_SWAPONOFF)
693 new_init_action(SHUTDOWN, "swapoff -a", "");
694 /* Prepare to restart init when a QUIT is received */
695 new_init_action(RESTART, "init", "");
696 /* Askfirst shell on tty1-4 */
697 new_init_action(ASKFIRST, bb_default_login_shell, "");
Denis Vlasenkoa474b682008-07-17 17:58:44 +0000698//TODO: VC_1 instead of ""? "" is console -> ctty problems -> angry users
Bernhard Reutner-Fischer54d50a02008-07-17 14:00:42 +0000699 new_init_action(ASKFIRST, bb_default_login_shell, VC_2);
700 new_init_action(ASKFIRST, bb_default_login_shell, VC_3);
701 new_init_action(ASKFIRST, bb_default_login_shell, VC_4);
702 /* sysinit */
703 new_init_action(SYSINIT, INIT_SCRIPT, "");
Bernhard Reutner-Fischer54d50a02008-07-17 14:00:42 +0000704 return;
705 }
706 /* optional_tty:ignored_runlevel:action:command
Denis Vlasenkoa474b682008-07-17 17:58:44 +0000707 * Delims are not to be collapsed and need exactly 4 tokens
Bernhard Reutner-Fischer54d50a02008-07-17 14:00:42 +0000708 */
Denis Vlasenko084266e2008-07-26 23:08:31 +0000709 while (config_read(parser, token, 4, 0, "#:",
710 PARSE_NORMAL & ~(PARSE_TRIM | PARSE_COLLAPSE))) {
Denis Vlasenkoa474b682008-07-17 17:58:44 +0000711 int action;
Bernhard Reutner-Fischer54d50a02008-07-17 14:00:42 +0000712 char *tty = token[0];
Bernhard Reutner-Fischer54d50a02008-07-17 14:00:42 +0000713
Denis Vlasenkoa474b682008-07-17 17:58:44 +0000714 if (!token[3]) /* less than 4 tokens */
Bernhard Reutner-Fischer54d50a02008-07-17 14:00:42 +0000715 goto bad_entry;
Denis Vlasenkoa474b682008-07-17 17:58:44 +0000716 action = index_in_strings(actions, token[2]);
717 if (action < 0 || !token[3][0]) /* token[3]: command */
718 goto bad_entry;
719 /* turn .*TTY -> /dev/TTY */
720 if (tty[0]) {
721 if (strncmp(tty, "/dev/", 5) == 0)
Bernhard Reutner-Fischer54d50a02008-07-17 14:00:42 +0000722 tty += 5;
723 tty = concat_path_file("/dev/", tty);
Denis Vlasenkoa474b682008-07-17 17:58:44 +0000724 }
725 new_init_action(1 << action, token[3], tty);
726 if (tty[0])
Bernhard Reutner-Fischer54d50a02008-07-17 14:00:42 +0000727 free(tty);
728 continue;
729 bad_entry:
Bernhard Reutner-Fischerad2fa652008-07-21 11:16:39 +0000730 message(L_LOG | L_CONSOLE, "Bad inittab entry at line %d",
731 parser->lineno);
Bernhard Reutner-Fischer54d50a02008-07-17 14:00:42 +0000732 }
733 config_close(parser);
Erik Andersen7dc16072000-01-04 01:10:25 +0000734}
735
Denis Vlasenkoad4da982008-04-05 04:24:23 +0000736#if ENABLE_FEATURE_USE_INITTAB
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000737static void reload_signal(int sig UNUSED_PARAM)
Eric Andersen6fd0e312003-07-22 09:48:56 +0000738{
Eric Andersen82baf632004-10-08 08:21:54 +0000739 struct init_action *a, *tmp;
740
Denis Vlasenko7a2ca5e2007-02-21 00:15:20 +0000741 message(L_LOG, "reloading /etc/inittab");
Eric Andersen82baf632004-10-08 08:21:54 +0000742
743 /* disable old entrys */
Denis Vlasenkoec5631b2007-12-25 01:08:58 +0000744 for (a = init_action_list; a; a = a->next) {
Denis Vlasenkoa37e7132008-02-19 02:57:07 +0000745 a->action_type = ONCE;
Eric Andersen82baf632004-10-08 08:21:54 +0000746 }
747
748 parse_inittab();
749
Mike Frysingerbb50fdf2007-12-25 04:30:14 +0000750 if (ENABLE_FEATURE_KILL_REMOVED) {
Denis Vlasenkod55268d2007-12-26 18:32:58 +0000751 /* Be nice and send SIGTERM first */
Mike Frysingerbb50fdf2007-12-25 04:30:14 +0000752 for (a = init_action_list; a; a = a->next) {
753 pid_t pid = a->pid;
Denis Vlasenkoa37e7132008-02-19 02:57:07 +0000754 if ((a->action_type & ONCE) && pid != 0) {
Mike Frysingerbb50fdf2007-12-25 04:30:14 +0000755 kill(pid, SIGTERM);
Denis Vlasenkoec5631b2007-12-25 01:08:58 +0000756 }
Denis Vlasenkoec5631b2007-12-25 01:08:58 +0000757 }
Denis Vlasenkod55268d2007-12-26 18:32:58 +0000758#if CONFIG_FEATURE_KILL_DELAY
Denis Vlasenko671ca332008-02-19 14:13:20 +0000759 /* NB: parent will wait in NOMMU case */
760 if ((BB_MMU ? fork() : vfork()) == 0) { /* child */
Denis Vlasenkod55268d2007-12-26 18:32:58 +0000761 sleep(CONFIG_FEATURE_KILL_DELAY);
762 for (a = init_action_list; a; a = a->next) {
763 pid_t pid = a->pid;
Denis Vlasenkoa37e7132008-02-19 02:57:07 +0000764 if ((a->action_type & ONCE) && pid != 0) {
Denis Vlasenkod55268d2007-12-26 18:32:58 +0000765 kill(pid, SIGKILL);
766 }
767 }
Bernhard Reutner-Fischer636a1f82008-05-19 09:29:47 +0000768 _exit(EXIT_SUCCESS);
Denis Vlasenkod55268d2007-12-26 18:32:58 +0000769 }
770#endif
Denis Vlasenkoec5631b2007-12-25 01:08:58 +0000771 }
Denis Vlasenkoec5631b2007-12-25 01:08:58 +0000772
Eric Andersen82baf632004-10-08 08:21:54 +0000773 /* remove unused entrys */
774 for (a = init_action_list; a; a = tmp) {
775 tmp = a->next;
Denis Vlasenkoa37e7132008-02-19 02:57:07 +0000776 if ((a->action_type & (ONCE | SYSINIT | WAIT)) && a->pid == 0) {
Eric Andersen82baf632004-10-08 08:21:54 +0000777 delete_init_action(a);
778 }
779 }
Denis Vlasenkoad4da982008-04-05 04:24:23 +0000780 run_actions(RESPAWN | ASKFIRST);
Eric Andersen6fd0e312003-07-22 09:48:56 +0000781}
Denis Vlasenkoad4da982008-04-05 04:24:23 +0000782#endif
Eric Andersen82baf632004-10-08 08:21:54 +0000783
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +0000784int init_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000785int init_main(int argc UNUSED_PARAM, char **argv)
Eric Andersen0460ff21999-10-25 23:32:44 +0000786{
Eric Andersen0298be82002-03-05 15:12:19 +0000787 struct init_action *a;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000788 pid_t wpid;
Eric Andersen0460ff21999-10-25 23:32:44 +0000789
Denis Vlasenko9b1381f2007-01-03 02:56:00 +0000790 die_sleep = 30 * 24*60*60; /* if xmalloc will ever die... */
791
Denis Vlasenko1d426652008-03-17 09:09:09 +0000792 if (argv[1] && !strcmp(argv[1], "-q")) {
Denis Vlasenko7a2ca5e2007-02-21 00:15:20 +0000793 return kill(1, SIGHUP);
Eric Andersen730f8262001-12-17 23:13:08 +0000794 }
Mike Frysingerbb50fdf2007-12-25 04:30:14 +0000795
796 if (!ENABLE_DEBUG_INIT) {
797 /* Expect to be invoked as init with PID=1 or be invoked as linuxrc */
798 if (getpid() != 1
799 && (!ENABLE_FEATURE_INITRD || !strstr(applet_name, "linuxrc"))
800 ) {
801 bb_show_usage();
802 }
803 /* Set up sig handlers -- be sure to
804 * clear all of these in run() */
Denis Vlasenko99a61842008-02-19 12:08:38 +0000805 signal(SIGQUIT, exec_restart_action);
Denis Vlasenko25591c32008-02-16 22:58:56 +0000806 bb_signals(0
807 + (1 << SIGUSR1) /* halt */
808 + (1 << SIGUSR2) /* poweroff */
809 + (1 << SIGTERM) /* reboot */
810 , halt_reboot_pwoff);
Mike Frysingerbb50fdf2007-12-25 04:30:14 +0000811 signal(SIGINT, ctrlaltdel_signal);
Mike Frysingerbb50fdf2007-12-25 04:30:14 +0000812 signal(SIGCONT, cont_handler);
Denis Vlasenko25591c32008-02-16 22:58:56 +0000813 bb_signals(0
814 + (1 << SIGSTOP)
815 + (1 << SIGTSTP)
816 , stop_handler);
Mike Frysingerbb50fdf2007-12-25 04:30:14 +0000817
818 /* Turn off rebooting via CTL-ALT-DEL -- we get a
819 * SIGINT on CAD so we can shut things down gracefully... */
820 init_reboot(RB_DISABLE_CAD);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000821 }
Denis Vlasenkod8540f72007-06-14 07:53:06 +0000822
Eric Andersen599e3ce2002-07-03 11:08:10 +0000823 /* Figure out where the default console should be */
824 console_init();
Denis Vlasenkod238a472007-03-05 19:22:04 +0000825 set_sane_term();
Bernhard Reutner-Fischer54d50a02008-07-17 14:00:42 +0000826 xchdir("/");
Erik Andersene49d5ec2000-02-08 19:58:47 +0000827 setsid();
Glenn L McGrathb4a1baa2003-01-13 22:09:50 +0000828 {
Denis Vlasenko6ca409e2007-08-12 20:58:27 +0000829 const char *const *e;
Glenn L McGrathb4a1baa2003-01-13 22:09:50 +0000830 /* Make sure environs is set to something sane */
Denis Vlasenkobf0a2012006-12-26 10:42:51 +0000831 for (e = environment; *e; e++)
Manuel Novoa III cad53642003-03-19 09:13:01 +0000832 putenv((char *) *e);
Glenn L McGrathb4a1baa2003-01-13 22:09:50 +0000833 }
Rob Landleycba1b962006-07-09 17:28:17 +0000834
Bernhard Reutner-Fischer54d50a02008-07-17 14:00:42 +0000835 if (argv[1])
836 setenv("RUNLEVEL", argv[1], 1);
Rob Landleycba1b962006-07-09 17:28:17 +0000837
Erik Andersene49d5ec2000-02-08 19:58:47 +0000838 /* Hello world */
Denis Vlasenkoca525b42007-06-13 12:27:17 +0000839 message(MAYBE_CONSOLE | L_LOG, "init started: %s", bb_banner);
Eric Andersencc8ed391999-10-05 16:24:54 +0000840
Erik Andersene49d5ec2000-02-08 19:58:47 +0000841 /* Make sure there is enough memory to do something useful. */
Rob Landleyc3386a42005-08-30 18:50:37 +0000842 if (ENABLE_SWAPONOFF) {
843 struct sysinfo info;
844
845 if (!sysinfo(&info) &&
Denis Vlasenkodca0b702006-10-27 09:05:02 +0000846 (info.mem_unit ? : 1) * (long long)info.totalram < 1024*1024)
Rob Landleyc3386a42005-08-30 18:50:37 +0000847 {
Denis Vlasenko7a2ca5e2007-02-21 00:15:20 +0000848 message(L_CONSOLE, "Low memory, forcing swapon");
Rob Landleyc3386a42005-08-30 18:50:37 +0000849 /* swapon -a requires /proc typically */
Denis Vlasenko2f0c0d02007-01-21 00:41:04 +0000850 new_init_action(SYSINIT, "mount -t proc proc /proc", "");
Rob Landleyc3386a42005-08-30 18:50:37 +0000851 /* Try to turn on swap */
Denis Vlasenko2f0c0d02007-01-21 00:41:04 +0000852 new_init_action(SYSINIT, "swapon -a", "");
Rob Landleyc3386a42005-08-30 18:50:37 +0000853 run_actions(SYSINIT); /* wait and removing */
854 }
855 }
Erik Andersen9e737252000-01-06 01:16:13 +0000856
Erik Andersene49d5ec2000-02-08 19:58:47 +0000857 /* Check if we are supposed to be in single user mode */
Denis Vlasenko1d426652008-03-17 09:09:09 +0000858 if (argv[1]
Denis Vlasenkobf66fbc2006-12-21 13:23:14 +0000859 && (!strcmp(argv[1], "single") || !strcmp(argv[1], "-s") || LONE_CHAR(argv[1], '1'))
860 ) {
Bernhard Reutner-Fischer54d50a02008-07-17 14:00:42 +0000861 /* ??? shouldn't we set RUNLEVEL="b" here? */
Glenn L McGrathb4a1baa2003-01-13 22:09:50 +0000862 /* Start a shell on console */
Glenn L McGrathdc4e75e2003-09-02 02:36:18 +0000863 new_init_action(RESPAWN, bb_default_login_shell, "");
Erik Andersene49d5ec2000-02-08 19:58:47 +0000864 } else {
865 /* Not in single user mode -- see what inittab says */
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000866
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000867 /* NOTE that if CONFIG_FEATURE_USE_INITTAB is NOT defined,
Erik Andersene49d5ec2000-02-08 19:58:47 +0000868 * then parse_inittab() simply adds in some default
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000869 * actions(i.e., runs INIT_SCRIPT and then starts a pair
Erik Andersene49d5ec2000-02-08 19:58:47 +0000870 * of "askfirst" shells */
871 parse_inittab();
Eric Andersend00c2621999-12-07 08:37:31 +0000872 }
Erik Andersen983b51b2000-04-04 18:14:25 +0000873
Denis Vlasenko9b1381f2007-01-03 02:56:00 +0000874#if ENABLE_SELINUX
Rob Landleyb3ede5a2006-03-27 23:09:12 +0000875 if (getenv("SELINUX_INIT") == NULL) {
876 int enforce = 0;
877
Denis Vlasenkod8540f72007-06-14 07:53:06 +0000878 putenv((char*)"SELINUX_INIT=YES");
Rob Landleyb3ede5a2006-03-27 23:09:12 +0000879 if (selinux_init_load_policy(&enforce) == 0) {
Denis Vlasenko4921b542007-02-03 02:17:41 +0000880 BB_EXECVP(argv[0], argv);
Rob Landleyb3ede5a2006-03-27 23:09:12 +0000881 } else if (enforce > 0) {
882 /* SELinux in enforcing mode but load_policy failed */
Bernhard Reutner-Fischera53de7f2008-07-21 13:46:54 +0000883 message(L_CONSOLE, "cannot load SELinux Policy. "
Denis Vlasenkob7167542007-02-27 19:20:00 +0000884 "Machine is in enforcing mode. Halting now.");
Bernhard Reutner-Fischer636a1f82008-05-19 09:29:47 +0000885 exit(EXIT_FAILURE);
Rob Landleyb3ede5a2006-03-27 23:09:12 +0000886 }
887 }
888#endif /* CONFIG_SELINUX */
889
Denis Vlasenko2afabe82007-12-10 07:06:04 +0000890 /* Make the command line just say "init" - thats all, nothing else */
891 strncpy(argv[0], "init", strlen(argv[0]));
892 /* Wipe argv[1]-argv[N] so they don't clutter the ps listing */
893 while (*++argv)
894 memset(*argv, 0, strlen(*argv));
Eric Andersen219d6f51999-11-02 19:43:01 +0000895
Erik Andersene49d5ec2000-02-08 19:58:47 +0000896 /* Now run everything that needs to be run */
897
898 /* First run the sysinit command */
Eric Andersen1644db92001-09-05 20:18:15 +0000899 run_actions(SYSINIT);
Eric Andersen0298be82002-03-05 15:12:19 +0000900
Erik Andersene49d5ec2000-02-08 19:58:47 +0000901 /* Next run anything that wants to block */
Eric Andersen1644db92001-09-05 20:18:15 +0000902 run_actions(WAIT);
Eric Andersen0298be82002-03-05 15:12:19 +0000903
Erik Andersene49d5ec2000-02-08 19:58:47 +0000904 /* Next run anything to be run only once */
Eric Andersen0298be82002-03-05 15:12:19 +0000905 run_actions(ONCE);
906
Eric Andersen6fd0e312003-07-22 09:48:56 +0000907 /* Redefine SIGHUP to reread /etc/inittab */
Denis Vlasenkoad4da982008-04-05 04:24:23 +0000908#if ENABLE_FEATURE_USE_INITTAB
909 signal(SIGHUP, reload_signal);
910#else
911 signal(SIGHUP, SIG_IGN);
912#endif
Eric Andersen82baf632004-10-08 08:21:54 +0000913
Erik Andersene49d5ec2000-02-08 19:58:47 +0000914 /* Now run the looping stuff for the rest of forever */
915 while (1) {
Denis Vlasenkoad4da982008-04-05 04:24:23 +0000916 /* run the respawn/askfirst stuff */
917 run_actions(RESPAWN | ASKFIRST);
Eric Andersen0298be82002-03-05 15:12:19 +0000918
919 /* Don't consume all CPU time -- sleep a bit */
920 sleep(1);
921
Denis Vlasenkofb0eba72008-01-02 19:55:04 +0000922 /* Wait for any child process to exit */
Bernhard Reutner-Fischerc58dbf22006-05-30 12:16:54 +0000923 wpid = wait(NULL);
Eric Andersen87812dc2004-04-12 19:21:54 +0000924 while (wpid > 0) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000925 /* Find out who died and clean up their corpse */
Eric Andersen0298be82002-03-05 15:12:19 +0000926 for (a = init_action_list; a; a = a->next) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000927 if (a->pid == wpid) {
Eric Andersen0298be82002-03-05 15:12:19 +0000928 /* Set the pid to 0 so that the process gets
929 * restarted by run_actions() */
Erik Andersene49d5ec2000-02-08 19:58:47 +0000930 a->pid = 0;
Denis Vlasenko7a2ca5e2007-02-21 00:15:20 +0000931 message(L_LOG, "process '%s' (pid %d) exited. "
Denis Vlasenkoa37e7132008-02-19 02:57:07 +0000932 "Scheduling for restart.",
Glenn L McGrathb4a1baa2003-01-13 22:09:50 +0000933 a->command, wpid);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000934 }
935 }
Eric Andersencf1fee02002-12-17 09:48:16 +0000936 /* see if anyone else is waiting to be reaped */
Denis Vlasenkofb0eba72008-01-02 19:55:04 +0000937 wpid = wait_any_nohang(NULL);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000938 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000939 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000940}