blob: 6ff9953b8bc9d517309c9205abe63c0098ee315a [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 Vlasenko5cb54b52008-10-21 17:14:26 +000017/* Was a CONFIG_xxx option. A lot of people were building
18 * not fully functional init by switching it on! */
19#define DEBUG_INIT 0
20
Denis Vlasenkoa37e7132008-02-19 02:57:07 +000021#define COMMAND_SIZE 256
Denis Vlasenko7a2ca5e2007-02-21 00:15:20 +000022#define CONSOLE_NAME_SIZE 32
23#define MAXENV 16 /* Number of env. vars */
Eric Andersenbd22ed82000-07-08 18:55:24 +000024
Erik Andersen983b51b2000-04-04 18:14:25 +000025/*
Eric Andersenc7bda1c2004-03-15 08:29:22 +000026 * When a file named CORE_ENABLE_FLAG_FILE exists, setrlimit is called
Erik Andersen183da4a2000-04-04 18:36:37 +000027 * before processes are spawned to set core file size as unlimited.
28 * This is for debugging only. Don't use this is production, unless
29 * you want core dumps lying about....
Erik Andersen983b51b2000-04-04 18:14:25 +000030 */
31#define CORE_ENABLE_FLAG_FILE "/.init_enable_core"
32#include <sys/resource.h>
Erik Andersen983b51b2000-04-04 18:14:25 +000033
Glenn L McGrathbaf55a82002-08-22 18:22:10 +000034#define INITTAB "/etc/inittab" /* inittab file location */
Erik Andersen96e2abd2000-01-07 11:40:44 +000035#ifndef INIT_SCRIPT
Glenn L McGrathbaf55a82002-08-22 18:22:10 +000036#define INIT_SCRIPT "/etc/init.d/rcS" /* Default sysinit script. */
Erik Andersen96e2abd2000-01-07 11:40:44 +000037#endif
Erik Andersen9c88cac1999-12-30 09:25:17 +000038
Erik Andersen7dc16072000-01-04 01:10:25 +000039/* Allowed init action types */
Denis Vlasenkoad4da982008-04-05 04:24:23 +000040#define SYSINIT 0x01
41#define RESPAWN 0x02
42/* like respawn, but wait for <Enter> to be pressed on tty: */
43#define ASKFIRST 0x04
44#define WAIT 0x08
45#define ONCE 0x10
46#define CTRLALTDEL 0x20
47#define SHUTDOWN 0x40
48#define RESTART 0x80
Erik Andersen7dc16072000-01-04 01:10:25 +000049
Eric Andersen0298be82002-03-05 15:12:19 +000050/* Set up a linked list of init_actions, to be read from inittab */
51struct init_action {
Eric Andersen0298be82002-03-05 15:12:19 +000052 struct init_action *next;
Denis Vlasenko7a2ca5e2007-02-21 00:15:20 +000053 pid_t pid;
Denis Vlasenkoa37e7132008-02-19 02:57:07 +000054 uint8_t action_type;
Denis Vlasenko7a2ca5e2007-02-21 00:15:20 +000055 char terminal[CONSOLE_NAME_SIZE];
Denis Vlasenkoa37e7132008-02-19 02:57:07 +000056 char command[COMMAND_SIZE];
Erik Andersen7dc16072000-01-04 01:10:25 +000057};
Erik Andersen7dc16072000-01-04 01:10:25 +000058
Eric Andersen0298be82002-03-05 15:12:19 +000059/* Static variables */
60static struct init_action *init_action_list = NULL;
Glenn L McGrathbaf55a82002-08-22 18:22:10 +000061
Denis Vlasenko4c978632007-02-03 03:31:13 +000062static const char *log_console = VC_5;
Rob Landleybc68cd12006-03-10 19:22:06 +000063
64enum {
Denis Vlasenkoec27feb2007-02-17 15:52:02 +000065 L_LOG = 0x1,
66 L_CONSOLE = 0x2,
Denis Vlasenkob2b2c402009-01-28 23:56:31 +000067 MAYBE_CONSOLE = L_CONSOLE * !ENABLE_FEATURE_EXTRA_QUIET,
Rob Landleybc68cd12006-03-10 19:22:06 +000068#ifndef RB_HALT_SYSTEM
Bernhard Reutner-Fischercf1f2ac2006-06-02 10:43:17 +000069 RB_HALT_SYSTEM = 0xcdef0123, /* FIXME: this overflows enum */
Rob Landleybc68cd12006-03-10 19:22:06 +000070 RB_ENABLE_CAD = 0x89abcdef,
71 RB_DISABLE_CAD = 0,
72 RB_POWER_OFF = 0x4321fedc,
73 RB_AUTOBOOT = 0x01234567,
Eric Andersen0298be82002-03-05 15:12:19 +000074#endif
Rob Landleybc68cd12006-03-10 19:22:06 +000075};
Erik Andersen42094cd2000-03-20 21:34:52 +000076
Eric Andersen0298be82002-03-05 15:12:19 +000077/* Function prototypes */
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +000078static void halt_reboot_pwoff(int sig) NORETURN;
Eric Andersen0460ff21999-10-25 23:32:44 +000079
Denis Vlasenko21e20cb2008-01-04 15:10:47 +000080static void waitfor(pid_t pid)
Denis Vlasenkofb0eba72008-01-02 19:55:04 +000081{
Denis Vlasenko21e20cb2008-01-04 15:10:47 +000082 /* waitfor(run(x)): protect against failed fork inside run() */
83 if (pid <= 0)
84 return;
85
86 /* Wait for any child (prevent zombies from exiting orphaned processes)
87 * but exit the loop only when specified one has exited. */
88 while (wait(NULL) != pid)
89 continue;
Denis Vlasenkofb0eba72008-01-02 19:55:04 +000090}
91
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +000092static void loop_forever(void) NORETURN;
Eric Andersen74400cc2001-10-18 04:11:39 +000093static void loop_forever(void)
Matt Kraai67a46402001-06-03 05:55:52 +000094{
95 while (1)
Glenn L McGrathbaf55a82002-08-22 18:22:10 +000096 sleep(1);
Matt Kraai67a46402001-06-03 05:55:52 +000097}
Eric Andersen7ef1a5b2001-03-20 17:39:08 +000098
Erik Andersen42094cd2000-03-20 21:34:52 +000099/* Print a message to the specified device.
Denis Vlasenko671ca332008-02-19 14:13:20 +0000100 * "where" may be bitwise-or'd from L_LOG | L_CONSOLE
101 * NB: careful, we can be called after vfork!
102 */
Denis Vlasenko5cb54b52008-10-21 17:14:26 +0000103#define messageD(...) do { if (DEBUG_INIT) message(__VA_ARGS__); } while (0)
Denis Vlasenko671ca332008-02-19 14:13:20 +0000104static void message(int where, const char *fmt, ...)
Glenn L McGrathbaf55a82002-08-22 18:22:10 +0000105 __attribute__ ((format(printf, 2, 3)));
Denis Vlasenko671ca332008-02-19 14:13:20 +0000106static void message(int where, const char *fmt, ...)
Eric Andersen0460ff21999-10-25 23:32:44 +0000107{
Eric Andersen22237012003-01-23 07:08:26 +0000108 static int log_fd = -1;
Denis Vlasenko7a2ca5e2007-02-21 00:15:20 +0000109 va_list arguments;
Denis Vlasenkob8d1a4c2008-09-20 16:28:59 +0000110 unsigned l;
Denis Vlasenko7a2ca5e2007-02-21 00:15:20 +0000111 char msg[128];
112
Glenn L McGrathb4a1baa2003-01-13 22:09:50 +0000113 msg[0] = '\r';
Denis Vlasenko7a2ca5e2007-02-21 00:15:20 +0000114 va_start(arguments, fmt);
Denis Vlasenko1bcdcd22008-12-09 21:23:31 +0000115 l = 1 + vsnprintf(msg + 1, sizeof(msg) - 2, fmt, arguments);
116 if (l > sizeof(msg) - 1)
117 l = sizeof(msg) - 1;
Denis Vlasenkob8d1a4c2008-09-20 16:28:59 +0000118 msg[l] = '\0';
Denis Vlasenkoec27feb2007-02-17 15:52:02 +0000119 va_end(arguments);
Glenn L McGrathb4a1baa2003-01-13 22:09:50 +0000120
Mike Frysingerbb50fdf2007-12-25 04:30:14 +0000121 if (ENABLE_FEATURE_INIT_SYSLOG) {
Denis Vlasenko671ca332008-02-19 14:13:20 +0000122 if (where & L_LOG) {
Denis Vlasenko1bcdcd22008-12-09 21:23:31 +0000123 /* Log the message to syslogd */
124 openlog("init", 0, LOG_DAEMON);
125 /* don't print "\r" */
126 syslog(LOG_INFO, "%s", msg + 1);
Mike Frysingerbb50fdf2007-12-25 04:30:14 +0000127 closelog();
128 }
129 msg[l++] = '\n';
130 msg[l] = '\0';
131 } else {
132 msg[l++] = '\n';
133 msg[l] = '\0';
134 /* Take full control of the log tty, and never close it.
135 * It's mine, all mine! Muhahahaha! */
136 if (log_fd < 0) {
137 if (!log_console) {
Bernhard Reutner-Fischerad2fa652008-07-21 11:16:39 +0000138 log_fd = STDERR_FILENO;
Denis Vlasenko7a2ca5e2007-02-21 00:15:20 +0000139 } else {
Mike Frysingerbb50fdf2007-12-25 04:30:14 +0000140 log_fd = device_open(log_console, O_WRONLY | O_NONBLOCK | O_NOCTTY);
141 if (log_fd < 0) {
142 bb_error_msg("can't log to %s", log_console);
Denis Vlasenko671ca332008-02-19 14:13:20 +0000143 where = L_CONSOLE;
Mike Frysingerbb50fdf2007-12-25 04:30:14 +0000144 } else {
145 close_on_exec_on(log_fd);
146 }
Denis Vlasenko7a2ca5e2007-02-21 00:15:20 +0000147 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000148 }
Denis Vlasenko671ca332008-02-19 14:13:20 +0000149 if (where & L_LOG) {
Mike Frysingerbb50fdf2007-12-25 04:30:14 +0000150 full_write(log_fd, msg, l);
Bernhard Reutner-Fischerad2fa652008-07-21 11:16:39 +0000151 if (log_fd == STDERR_FILENO)
Mike Frysingerbb50fdf2007-12-25 04:30:14 +0000152 return; /* don't print dup messages */
153 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000154 }
Eric Andersen4c781471999-11-26 08:12:56 +0000155
Denis Vlasenko671ca332008-02-19 14:13:20 +0000156 if (where & L_CONSOLE) {
Denis Vlasenko7a2ca5e2007-02-21 00:15:20 +0000157 /* Send console messages to console so people will see them. */
Bernhard Reutner-Fischer5e25ddb2008-05-19 09:48:17 +0000158 full_write(STDERR_FILENO, msg, l);
Eric Andersen3ae0c781999-11-04 01:13:21 +0000159 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000160}
161
Denis Vlasenko7a2ca5e2007-02-21 00:15:20 +0000162/* From <linux/serial.h> */
163struct serial_struct {
164 int type;
165 int line;
166 unsigned int port;
167 int irq;
168 int flags;
169 int xmit_fifo_size;
170 int custom_divisor;
171 int baud_base;
172 unsigned short close_delay;
173 char io_type;
174 char reserved_char[1];
175 int hub6;
176 unsigned short closing_wait; /* time to wait before closing */
177 unsigned short closing_wait2; /* no longer used... */
178 unsigned char *iomem_base;
179 unsigned short iomem_reg_shift;
180 unsigned int port_high;
181 unsigned long iomap_base; /* cookie passed into ioremap */
182 int reserved[1];
183 /* Paranoia (imagine 64bit kernel overwriting 32bit userspace stack) */
184 uint32_t bbox_reserved[16];
185};
Eric Andersen74400cc2001-10-18 04:11:39 +0000186static void console_init(void)
Eric Andersen0460ff21999-10-25 23:32:44 +0000187{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000188 struct serial_struct sr;
189 char *s;
Eric Andersen0460ff21999-10-25 23:32:44 +0000190
Denis Vlasenko7a2ca5e2007-02-21 00:15:20 +0000191 s = getenv("CONSOLE");
Bernhard Reutner-Fischerad2fa652008-07-21 11:16:39 +0000192 if (!s)
193 s = getenv("console");
Denis Vlasenko7a2ca5e2007-02-21 00:15:20 +0000194 if (s) {
195 int fd = open(s, O_RDWR | O_NONBLOCK | O_NOCTTY);
196 if (fd >= 0) {
Bernhard Reutner-Fischerad2fa652008-07-21 11:16:39 +0000197 dup2(fd, STDIN_FILENO);
198 dup2(fd, STDOUT_FILENO);
199 xmove_fd(fd, STDERR_FILENO);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000200 }
Denis Vlasenko7a2ca5e2007-02-21 00:15:20 +0000201 messageD(L_LOG, "console='%s'", s);
Denis Vlasenkod8540f72007-06-14 07:53:06 +0000202 } else {
Denis Vlasenkofb274df2008-03-17 13:26:51 +0000203 /* Make sure fd 0,1,2 are not closed
204 * (so that they won't be used by future opens) */
Denis Vlasenkod48e81f2008-07-06 07:00:11 +0000205 bb_sanitize_stdio();
Denis Vlasenkoa34b8a42008-11-29 23:14:37 +0000206// Users report problems
207// /* Make sure init can't be blocked by writing to stderr */
208// fcntl(STDERR_FILENO, F_SETFL, fcntl(STDERR_FILENO, F_GETFL) | O_NONBLOCK);
Eric Andersen07e52971999-11-07 07:38:08 +0000209 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000210
Denis Vlasenko7a2ca5e2007-02-21 00:15:20 +0000211 s = getenv("TERM");
Denis Vlasenkoe421b5e2008-03-17 22:01:42 +0000212 if (ioctl(STDIN_FILENO, TIOCGSERIAL, &sr) == 0) {
Denis Vlasenko2afabe82007-12-10 07:06:04 +0000213 /* Force the TERM setting to vt102 for serial console
Denis Vlasenko7a2ca5e2007-02-21 00:15:20 +0000214 * if TERM is set to linux (the default) */
215 if (!s || strcmp(s, "linux") == 0)
216 putenv((char*)"TERM=vt102");
Mike Frysingerbb50fdf2007-12-25 04:30:14 +0000217 if (!ENABLE_FEATURE_INIT_SYSLOG)
218 log_console = NULL;
Denis Vlasenko7a2ca5e2007-02-21 00:15:20 +0000219 } else if (!s)
220 putenv((char*)"TERM=linux");
Eric Andersen0460ff21999-10-25 23:32:44 +0000221}
Glenn L McGrathbaf55a82002-08-22 18:22:10 +0000222
Denis Vlasenko671ca332008-02-19 14:13:20 +0000223/* Set terminal settings to reasonable defaults.
224 * NB: careful, we can be called after vfork! */
Denis Vlasenko2afabe82007-12-10 07:06:04 +0000225static void set_sane_term(void)
Eric Andersen7ef1a5b2001-03-20 17:39:08 +0000226{
Denis Vlasenko2afabe82007-12-10 07:06:04 +0000227 struct termios tty;
Eric Andersen7ef1a5b2001-03-20 17:39:08 +0000228
Denis Vlasenko2afabe82007-12-10 07:06:04 +0000229 tcgetattr(STDIN_FILENO, &tty);
230
231 /* set control chars */
232 tty.c_cc[VINTR] = 3; /* C-c */
233 tty.c_cc[VQUIT] = 28; /* C-\ */
234 tty.c_cc[VERASE] = 127; /* C-? */
235 tty.c_cc[VKILL] = 21; /* C-u */
236 tty.c_cc[VEOF] = 4; /* C-d */
237 tty.c_cc[VSTART] = 17; /* C-q */
238 tty.c_cc[VSTOP] = 19; /* C-s */
239 tty.c_cc[VSUSP] = 26; /* C-z */
240
Bernhard Reutner-Fischerad2fa652008-07-21 11:16:39 +0000241 /* use line discipline 0 */
Denis Vlasenko2afabe82007-12-10 07:06:04 +0000242 tty.c_line = 0;
243
244 /* Make it be sane */
245 tty.c_cflag &= CBAUD | CBAUDEX | CSIZE | CSTOPB | PARENB | PARODD;
246 tty.c_cflag |= CREAD | HUPCL | CLOCAL;
247
248 /* input modes */
249 tty.c_iflag = ICRNL | IXON | IXOFF;
250
251 /* output modes */
252 tty.c_oflag = OPOST | ONLCR;
253
254 /* local modes */
255 tty.c_lflag =
256 ISIG | ICANON | ECHO | ECHOE | ECHOK | ECHOCTL | ECHOKE | IEXTEN;
257
Denis Vlasenko202ac502008-11-05 13:20:58 +0000258 tcsetattr_stdin_TCSANOW(&tty);
Eric Andersen7ef1a5b2001-03-20 17:39:08 +0000259}
260
Denis Vlasenko671ca332008-02-19 14:13:20 +0000261/* Open the new terminal device.
262 * NB: careful, we can be called after vfork! */
Denis Vlasenkod55268d2007-12-26 18:32:58 +0000263static void open_stdio_to_tty(const char* tty_name, int exit_on_failure)
Denis Vlasenko7a2ca5e2007-02-21 00:15:20 +0000264{
265 /* empty tty_name means "use init's tty", else... */
266 if (tty_name[0]) {
Denis Vlasenkod55268d2007-12-26 18:32:58 +0000267 int fd;
Bernhard Reutner-Fischerad2fa652008-07-21 11:16:39 +0000268 close(STDIN_FILENO);
Denis Vlasenkod55268d2007-12-26 18:32:58 +0000269 /* fd can be only < 0 or 0: */
270 fd = device_open(tty_name, O_RDWR);
271 if (fd) {
Bernhard Reutner-Fischera53de7f2008-07-21 13:46:54 +0000272 message(L_LOG | L_CONSOLE, "can't open %s: %s",
Denis Vlasenko7a2ca5e2007-02-21 00:15:20 +0000273 tty_name, strerror(errno));
Denis Vlasenkod55268d2007-12-26 18:32:58 +0000274 if (exit_on_failure)
Bernhard Reutner-Fischer636a1f82008-05-19 09:29:47 +0000275 _exit(EXIT_FAILURE);
Denis Vlasenko5cb54b52008-10-21 17:14:26 +0000276 if (DEBUG_INIT)
Mike Frysingerbb50fdf2007-12-25 04:30:14 +0000277 _exit(2);
Bernhard Reutner-Fischerad2fa652008-07-21 11:16:39 +0000278 /* NB: we don't reach this if we were called after vfork.
279 * Thus halt_reboot_pwoff() itself need not be vfork-safe. */
Denis Vlasenkod55268d2007-12-26 18:32:58 +0000280 halt_reboot_pwoff(SIGUSR1); /* halt the system */
Denis Vlasenko7a2ca5e2007-02-21 00:15:20 +0000281 }
Bernhard Reutner-Fischerad2fa652008-07-21 11:16:39 +0000282 dup2(STDIN_FILENO, STDOUT_FILENO);
283 dup2(STDIN_FILENO, STDERR_FILENO);
Bernhard Reutner-Fischer0da069d2006-05-29 12:54:16 +0000284 }
Denis Vlasenkod238a472007-03-05 19:22:04 +0000285 set_sane_term();
Bernhard Reutner-Fischer0da069d2006-05-29 12:54:16 +0000286}
287
Denis Vlasenko671ca332008-02-19 14:13:20 +0000288/* Wrapper around exec:
289 * Takes string (max COMMAND_SIZE chars).
290 * If chars like '>' detected, execs '[-]/bin/sh -c "exec ......."'.
291 * Otherwise splits words on whitespace, deals with leading dash,
292 * and uses plain exec().
293 * NB: careful, we can be called after vfork!
Denis Vlasenkoa37e7132008-02-19 02:57:07 +0000294 */
295static void init_exec(const char *command)
296{
297 char *cmd[COMMAND_SIZE / 2];
298 char buf[COMMAND_SIZE + 6]; /* COMMAND_SIZE+strlen("exec ")+1 */
Denis Vlasenko671ca332008-02-19 14:13:20 +0000299 int dash = (command[0] == '-' /* maybe? && command[1] == '/' */);
Denis Vlasenkoa37e7132008-02-19 02:57:07 +0000300
301 /* See if any special /bin/sh requiring characters are present */
302 if (strpbrk(command, "~`!$^&*()=|\\{}[];\"'<>?") != NULL) {
303 strcpy(buf, "exec ");
304 strcpy(buf + 5, command + dash); /* excluding "-" */
Denis Vlasenko671ca332008-02-19 14:13:20 +0000305 /* NB: LIBBB_DEFAULT_LOGIN_SHELL define has leading dash */
Denis Vlasenkoa37e7132008-02-19 02:57:07 +0000306 cmd[0] = (char*)(LIBBB_DEFAULT_LOGIN_SHELL + !dash);
307 cmd[1] = (char*)"-c";
308 cmd[2] = buf;
309 cmd[3] = NULL;
310 } else {
311 /* Convert command (char*) into cmd (char**, one word per string) */
312 char *word, *next;
313 int i = 0;
314 next = strcpy(buf, command); /* including "-" */
315 while ((word = strsep(&next, " \t")) != NULL) {
316 if (*word != '\0') { /* not two spaces/tabs together? */
317 cmd[i] = word;
318 i++;
319 }
320 }
321 cmd[i] = NULL;
322 }
323 /* If we saw leading "-", it is interactive shell.
324 * Try harder to give it a controlling tty.
325 * And skip "-" in actual exec call. */
326 if (dash) {
327 /* _Attempt_ to make stdin a controlling tty. */
328 if (ENABLE_FEATURE_INIT_SCTTY)
329 ioctl(STDIN_FILENO, TIOCSCTTY, 0 /*only try, don't steal*/);
330 }
331 BB_EXECVP(cmd[0] + dash, cmd);
Bernhard Reutner-Fischera53de7f2008-07-21 13:46:54 +0000332 message(L_LOG | L_CONSOLE, "cannot run '%s': %s", cmd[0], strerror(errno));
Denis Vlasenkoa37e7132008-02-19 02:57:07 +0000333 /* returns if execvp fails */
334}
335
Denis Vlasenko2afabe82007-12-10 07:06:04 +0000336/* Used only by run_actions */
Glenn L McGrathb4a1baa2003-01-13 22:09:50 +0000337static pid_t run(const struct init_action *a)
Eric Andersen0298be82002-03-05 15:12:19 +0000338{
Mike Frysinger10427ab2005-07-06 05:00:48 +0000339 pid_t pid;
Eric Andersen0298be82002-03-05 15:12:19 +0000340 sigset_t nmask, omask;
Erik Andersenac6e71f2000-01-08 22:04:33 +0000341
Denis Vlasenko2afabe82007-12-10 07:06:04 +0000342 /* Block sigchild while forking (why?) */
Eric Andersen0298be82002-03-05 15:12:19 +0000343 sigemptyset(&nmask);
344 sigaddset(&nmask, SIGCHLD);
345 sigprocmask(SIG_BLOCK, &nmask, &omask);
Denis Vlasenkoad4da982008-04-05 04:24:23 +0000346 if (BB_MMU && (a->action_type & ASKFIRST))
347 pid = fork();
348 else
349 pid = vfork();
Denis Vlasenko966bb432007-02-27 19:20:33 +0000350 sigprocmask(SIG_SETMASK, &omask, NULL);
Eric Andersen0298be82002-03-05 15:12:19 +0000351
Denis Vlasenko2afabe82007-12-10 07:06:04 +0000352 if (pid < 0)
Bernhard Reutner-Fischera53de7f2008-07-21 13:46:54 +0000353 message(L_LOG | L_CONSOLE, "can't fork");
Denis Vlasenko966bb432007-02-27 19:20:33 +0000354 if (pid)
355 return pid;
Eric Andersen0298be82002-03-05 15:12:19 +0000356
Denis Vlasenko2afabe82007-12-10 07:06:04 +0000357 /* Child */
358
Denis Vlasenko966bb432007-02-27 19:20:33 +0000359 /* Reset signal handlers that were set by the parent process */
Denis Vlasenko25591c32008-02-16 22:58:56 +0000360 bb_signals(0
361 + (1 << SIGUSR1)
362 + (1 << SIGUSR2)
363 + (1 << SIGINT)
364 + (1 << SIGTERM)
365 + (1 << SIGHUP)
366 + (1 << SIGQUIT)
367 + (1 << SIGCONT)
368 + (1 << SIGSTOP)
369 + (1 << SIGTSTP)
370 , SIG_DFL);
Eric Andersen2f6c04f1999-11-01 23:59:44 +0000371
Denis Vlasenko966bb432007-02-27 19:20:33 +0000372 /* Create a new session and make ourself the process
373 * group leader */
374 setsid();
Eric Andersen599e3ce2002-07-03 11:08:10 +0000375
Denis Vlasenko966bb432007-02-27 19:20:33 +0000376 /* Open the new terminal device */
Denis Vlasenko671ca332008-02-19 14:13:20 +0000377 open_stdio_to_tty(a->terminal, 1 /* - exit if open fails */);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000378
Denis Vlasenko671ca332008-02-19 14:13:20 +0000379// NB: do not enable unless you change vfork to fork above
Denis Vlasenko2afabe82007-12-10 07:06:04 +0000380#ifdef BUT_RUN_ACTIONS_ALREADY_DOES_WAITING
Denis Vlasenko966bb432007-02-27 19:20:33 +0000381 /* If the init Action requires us to wait, then force the
382 * supplied terminal to be the controlling tty. */
Denis Vlasenkoa37e7132008-02-19 02:57:07 +0000383 if (a->action_type & (SYSINIT | WAIT | CTRLALTDEL | SHUTDOWN | RESTART)) {
Denis Vlasenko966bb432007-02-27 19:20:33 +0000384 /* Now fork off another process to just hang around */
Denis Vlasenko6bef3d12007-11-06 03:05:54 +0000385 pid = fork();
Denis Vlasenkof6ccc622007-11-10 01:57:35 +0000386 if (pid < 0) {
Bernhard Reutner-Fischera53de7f2008-07-21 13:46:54 +0000387 message(L_LOG | L_CONSOLE, "can't fork");
Bernhard Reutner-Fischer636a1f82008-05-19 09:29:47 +0000388 _exit(EXIT_FAILURE);
Denis Vlasenko966bb432007-02-27 19:20:33 +0000389 }
390
391 if (pid > 0) {
Denis Vlasenko2afabe82007-12-10 07:06:04 +0000392 /* Parent - wait till the child is done */
Denis Vlasenko25591c32008-02-16 22:58:56 +0000393 bb_signals(0
394 + (1 << SIGINT)
395 + (1 << SIGTSTP)
396 + (1 << SIGQUIT)
397 , SIG_IGN);
Denis Vlasenko966bb432007-02-27 19:20:33 +0000398 signal(SIGCHLD, SIG_DFL);
399
Denis Vlasenko2afabe82007-12-10 07:06:04 +0000400 waitfor(pid);
Denis Vlasenko966bb432007-02-27 19:20:33 +0000401 /* See if stealing the controlling tty back is necessary */
402 if (tcgetpgrp(0) != getpid())
Bernhard Reutner-Fischer636a1f82008-05-19 09:29:47 +0000403 _exit(EXIT_SUCCESS);
Denis Vlasenko966bb432007-02-27 19:20:33 +0000404
405 /* Use a temporary process to steal the controlling tty. */
Denis Vlasenko6bef3d12007-11-06 03:05:54 +0000406 pid = fork();
407 if (pid < 0) {
Bernhard Reutner-Fischera53de7f2008-07-21 13:46:54 +0000408 message(L_LOG | L_CONSOLE, "can't fork");
Bernhard Reutner-Fischer636a1f82008-05-19 09:29:47 +0000409 _exit(EXIT_FAILURE);
Eric Andersen0298be82002-03-05 15:12:19 +0000410 }
Denis Vlasenko966bb432007-02-27 19:20:33 +0000411 if (pid == 0) {
412 setsid();
413 ioctl(0, TIOCSCTTY, 1);
Bernhard Reutner-Fischer636a1f82008-05-19 09:29:47 +0000414 _exit(EXIT_SUCCESS);
Eric Andersen0298be82002-03-05 15:12:19 +0000415 }
Denis Vlasenko2afabe82007-12-10 07:06:04 +0000416 waitfor(pid);
Bernhard Reutner-Fischer636a1f82008-05-19 09:29:47 +0000417 _exit(EXIT_SUCCESS);
Eric Andersen0298be82002-03-05 15:12:19 +0000418 }
Denis Vlasenko2afabe82007-12-10 07:06:04 +0000419 /* Child - fall though to actually execute things */
Denis Vlasenko966bb432007-02-27 19:20:33 +0000420 }
Denis Vlasenko5adfa442007-12-25 16:08:53 +0000421#endif
Denis Vlasenko671ca332008-02-19 14:13:20 +0000422
Denis Vlasenkoad4da982008-04-05 04:24:23 +0000423 /* NB: on NOMMU we can't wait for input in child, so
424 * "askfirst" will work the same as "respawn". */
Denis Vlasenko671ca332008-02-19 14:13:20 +0000425 if (BB_MMU && (a->action_type & ASKFIRST)) {
Denis Vlasenko6ca409e2007-08-12 20:58:27 +0000426 static const char press_enter[] ALIGN1 =
Denis Vlasenko966bb432007-02-27 19:20:33 +0000427#ifdef CUSTOMIZED_BANNER
428#include CUSTOMIZED_BANNER
Eric Andersen1f50e842004-08-16 09:29:42 +0000429#endif
Denis Vlasenko966bb432007-02-27 19:20:33 +0000430 "\nPlease press Enter to activate this console. ";
431 char c;
432 /*
433 * Save memory by not exec-ing anything large (like a shell)
434 * before the user wants it. This is critical if swap is not
435 * enabled and the system has low memory. Generally this will
436 * be run on the second virtual console, and the first will
437 * be allowed to start a shell or whatever an init script
438 * specifies.
439 */
440 messageD(L_LOG, "waiting for enter to start '%s'"
441 "(pid %d, tty '%s')\n",
Denis Vlasenkoa37e7132008-02-19 02:57:07 +0000442 a->command, getpid(), a->terminal);
Bernhard Reutner-Fischer5e25ddb2008-05-19 09:48:17 +0000443 full_write(STDOUT_FILENO, press_enter, sizeof(press_enter) - 1);
444 while (safe_read(STDIN_FILENO, &c, 1) == 1 && c != '\n')
Denis Vlasenko2afabe82007-12-10 07:06:04 +0000445 continue;
Denis Vlasenko966bb432007-02-27 19:20:33 +0000446 }
Denis Vlasenko2afabe82007-12-10 07:06:04 +0000447
Mike Frysingerbb50fdf2007-12-25 04:30:14 +0000448 if (ENABLE_FEATURE_INIT_COREDUMPS) {
Denis Vlasenko966bb432007-02-27 19:20:33 +0000449 struct stat sb;
450 if (stat(CORE_ENABLE_FLAG_FILE, &sb) == 0) {
451 struct rlimit limit;
Denis Vlasenko966bb432007-02-27 19:20:33 +0000452 limit.rlim_cur = RLIM_INFINITY;
453 limit.rlim_max = RLIM_INFINITY;
454 setrlimit(RLIMIT_CORE, &limit);
Erik Andersen983b51b2000-04-04 18:14:25 +0000455 }
Erik Andersen05df2392000-01-13 04:43:48 +0000456 }
Mike Frysingerbb50fdf2007-12-25 04:30:14 +0000457
Denis Vlasenkoa37e7132008-02-19 02:57:07 +0000458 /* Log the process name and args */
459 message(L_LOG, "starting pid %d, tty '%s': '%s'",
460 getpid(), a->terminal, a->command);
461
Denis Vlasenko966bb432007-02-27 19:20:33 +0000462 /* Now run it. The new program will take over this PID,
463 * so nothing further in init.c should be run. */
Denis Vlasenkoa37e7132008-02-19 02:57:07 +0000464 init_exec(a->command);
Denis Vlasenko966bb432007-02-27 19:20:33 +0000465 /* We're still here? Some error happened. */
Denis Vlasenko966bb432007-02-27 19:20:33 +0000466 _exit(-1);
Eric Andersen0460ff21999-10-25 23:32:44 +0000467}
468
Bernhard Reutner-Fischer52593612008-07-21 11:53:04 +0000469static void delete_init_action(struct init_action *action)
470{
471 struct init_action *a, *b = NULL;
472
473 for (a = init_action_list; a; b = a, a = a->next) {
474 if (a == action) {
475 if (b == NULL) {
476 init_action_list = a->next;
477 } else {
478 b->next = a->next;
479 }
480 free(a);
481 break;
482 }
483 }
484}
485
Eric Andersen0298be82002-03-05 15:12:19 +0000486/* Run all commands of a particular type */
Denis Vlasenkoa37e7132008-02-19 02:57:07 +0000487static void run_actions(int action_type)
Eric Andersen219d6f51999-11-02 19:43:01 +0000488{
Eric Andersen0298be82002-03-05 15:12:19 +0000489 struct init_action *a, *tmp;
Eric Andersen219d6f51999-11-02 19:43:01 +0000490
Eric Andersen0298be82002-03-05 15:12:19 +0000491 for (a = init_action_list; a; a = tmp) {
492 tmp = a->next;
Denis Vlasenkoad4da982008-04-05 04:24:23 +0000493 if (a->action_type & action_type) {
Denis Vlasenkod7e2e122007-12-10 08:40:29 +0000494 // Pointless: run() will error out if open of device fails.
495 ///* a->terminal of "" means "init's console" */
496 //if (a->terminal[0] && access(a->terminal, R_OK | W_OK)) {
497 // //message(L_LOG | L_CONSOLE, "Device %s cannot be opened in RW mode", a->terminal /*, strerror(errno)*/);
498 // delete_init_action(a);
499 //} else
Denis Vlasenkoa37e7132008-02-19 02:57:07 +0000500 if (a->action_type & (SYSINIT | WAIT | CTRLALTDEL | SHUTDOWN | RESTART)) {
Denis Vlasenko2afabe82007-12-10 07:06:04 +0000501 waitfor(run(a));
Eric Andersen0298be82002-03-05 15:12:19 +0000502 delete_init_action(a);
Denis Vlasenkoa37e7132008-02-19 02:57:07 +0000503 } else if (a->action_type & ONCE) {
Eric Andersen0298be82002-03-05 15:12:19 +0000504 run(a);
505 delete_init_action(a);
Denis Vlasenkoa37e7132008-02-19 02:57:07 +0000506 } else if (a->action_type & (RESPAWN | ASKFIRST)) {
Eric Andersen0298be82002-03-05 15:12:19 +0000507 /* Only run stuff with pid==0. If they have
508 * a pid, that means it is still running */
509 if (a->pid == 0) {
510 a->pid = run(a);
511 }
512 }
Erik Andersene132f4b2000-02-09 04:16:43 +0000513 }
514 }
515}
516
Eric Andersen2c1de612003-04-24 11:41:28 +0000517static void init_reboot(unsigned long magic)
518{
519 pid_t pid;
Bernhard Reutner-Fischer636a1f82008-05-19 09:29:47 +0000520 /* We have to fork here, since the kernel calls do_exit(EXIT_SUCCESS) in
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000521 * linux/kernel/sys.c, which can cause the machine to panic when
Eric Andersen2c1de612003-04-24 11:41:28 +0000522 * the init process is killed.... */
Denis Vlasenko9b1381f2007-01-03 02:56:00 +0000523 pid = vfork();
524 if (pid == 0) { /* child */
Eric Andersen2c1de612003-04-24 11:41:28 +0000525 reboot(magic);
Bernhard Reutner-Fischer636a1f82008-05-19 09:29:47 +0000526 _exit(EXIT_SUCCESS);
Eric Andersen2c1de612003-04-24 11:41:28 +0000527 }
Denis Vlasenkofb0eba72008-01-02 19:55:04 +0000528 waitfor(pid);
Eric Andersen2c1de612003-04-24 11:41:28 +0000529}
530
Denis Vlasenkod55268d2007-12-26 18:32:58 +0000531static void kill_all_processes(void)
Eric Andersencc8ed391999-10-05 16:24:54 +0000532{
Eric Andersena9cc8962002-09-16 06:49:06 +0000533 /* run everything to be run at "shutdown". This is done _prior_
534 * to killing everything, in case people wish to use scripts to
535 * shut things down gracefully... */
536 run_actions(SHUTDOWN);
537
Eric Andersen72f9a422001-10-28 05:12:20 +0000538 /* first disable all our signals */
Denis Vlasenko3f165fa2008-03-17 08:29:08 +0000539 sigprocmask_allsigs(SIG_BLOCK);
Erik Andersen7dc16072000-01-04 01:10:25 +0000540
Denis Vlasenko7a2ca5e2007-02-21 00:15:20 +0000541 message(L_CONSOLE | L_LOG, "The system is going down NOW!");
542
Erik Andersene49d5ec2000-02-08 19:58:47 +0000543 /* Allow Ctrl-Alt-Del to reboot system. */
Erik Andersen4f3f7572000-04-28 00:18:56 +0000544 init_reboot(RB_ENABLE_CAD);
Erik Andersene132f4b2000-02-09 04:16:43 +0000545
Erik Andersene49d5ec2000-02-08 19:58:47 +0000546 /* Send signals to every process _except_ pid 1 */
Denis Vlasenko7a2ca5e2007-02-21 00:15:20 +0000547 message(L_CONSOLE | L_LOG, "Sending SIG%s to all processes", "TERM");
Erik Andersene49d5ec2000-02-08 19:58:47 +0000548 kill(-1, SIGTERM);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000549 sync();
Denis Vlasenko7a2ca5e2007-02-21 00:15:20 +0000550 sleep(1);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000551
Denis Vlasenko7a2ca5e2007-02-21 00:15:20 +0000552 message(L_CONSOLE | L_LOG, "Sending SIG%s to all processes", "KILL");
Erik Andersene49d5ec2000-02-08 19:58:47 +0000553 kill(-1, SIGKILL);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000554 sync();
Denis Vlasenko7a2ca5e2007-02-21 00:15:20 +0000555 sleep(1);
Eric Andersencc8ed391999-10-05 16:24:54 +0000556}
557
Denis Vlasenkod55268d2007-12-26 18:32:58 +0000558static void halt_reboot_pwoff(int sig)
Denis Vlasenko2afabe82007-12-10 07:06:04 +0000559{
Bernhard Reutner-Fischer54d50a02008-07-17 14:00:42 +0000560 const char *m = "halt";
Denis Vlasenko2afabe82007-12-10 07:06:04 +0000561 int rb;
562
Denis Vlasenkod55268d2007-12-26 18:32:58 +0000563 kill_all_processes();
Denis Vlasenko2afabe82007-12-10 07:06:04 +0000564
Denis Vlasenko2afabe82007-12-10 07:06:04 +0000565 rb = RB_HALT_SYSTEM;
566 if (sig == SIGTERM) {
567 m = "reboot";
568 rb = RB_AUTOBOOT;
569 } else if (sig == SIGUSR2) {
570 m = "poweroff";
571 rb = RB_POWER_OFF;
572 }
573 message(L_CONSOLE | L_LOG, "Requesting system %s", m);
574 /* allow time for last message to reach serial console */
575 sleep(2);
576 init_reboot(rb);
577 loop_forever();
578}
579
Denis Vlasenkoa58a6372008-02-19 12:10:18 +0000580/* Handler for QUIT - exec "restart" action,
Denis Vlasenkod55268d2007-12-26 18:32:58 +0000581 * else (no such action defined) do nothing */
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000582static void exec_restart_action(int sig UNUSED_PARAM)
Eric Andersen730f8262001-12-17 23:13:08 +0000583{
Denis Vlasenkoa37e7132008-02-19 02:57:07 +0000584 struct init_action *a;
Glenn L McGrathbaf55a82002-08-22 18:22:10 +0000585
Denis Vlasenkoa37e7132008-02-19 02:57:07 +0000586 for (a = init_action_list; a; a = a->next) {
587 if (a->action_type & RESTART) {
Denis Vlasenkod55268d2007-12-26 18:32:58 +0000588 kill_all_processes();
Eric Andersen5222d312002-07-03 05:15:23 +0000589
Denis Vlasenkod55268d2007-12-26 18:32:58 +0000590 /* unblock all signals (blocked in kill_all_processes()) */
Denis Vlasenko3f165fa2008-03-17 08:29:08 +0000591 sigprocmask_allsigs(SIG_UNBLOCK);
Eric Andersen5222d312002-07-03 05:15:23 +0000592
Eric Andersen3c8064f2003-07-05 08:29:01 +0000593 /* Open the new terminal device */
Denis Vlasenkod55268d2007-12-26 18:32:58 +0000594 open_stdio_to_tty(a->terminal, 0 /* - halt if open fails */);
Eric Andersen3c8064f2003-07-05 08:29:01 +0000595
Denis Vlasenkoec27feb2007-02-17 15:52:02 +0000596 messageD(L_CONSOLE | L_LOG, "Trying to re-exec %s", a->command);
Denis Vlasenkoa37e7132008-02-19 02:57:07 +0000597 init_exec(a->command);
Eric Andersen730f8262001-12-17 23:13:08 +0000598 sleep(2);
599 init_reboot(RB_HALT_SYSTEM);
600 loop_forever();
601 }
602 }
603}
604
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000605static void ctrlaltdel_signal(int sig UNUSED_PARAM)
Eric Andersenc97ec342001-04-03 18:01:51 +0000606{
607 run_actions(CTRLALTDEL);
608}
609
Denis Vlasenko3fa36e22008-11-09 00:15:11 +0000610/* The SIGCONT handler is set to record_signo().
611 * It just sets bb_got_signal = SIGCONT. */
612
Eric Andersen0298be82002-03-05 15:12:19 +0000613/* The SIGSTOP & SIGTSTP handler */
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000614static void stop_handler(int sig UNUSED_PARAM)
Eric Andersened8a9be2001-11-30 19:10:58 +0000615{
Eric Andersen0298be82002-03-05 15:12:19 +0000616 int saved_errno = errno;
Eric Andersened8a9be2001-11-30 19:10:58 +0000617
Denis Vlasenko3fa36e22008-11-09 00:15:11 +0000618 bb_got_signal = 0;
619 while (bb_got_signal == 0)
Glenn L McGrathbaf55a82002-08-22 18:22:10 +0000620 pause();
Denis Vlasenko2afabe82007-12-10 07:06:04 +0000621
Eric Andersened8a9be2001-11-30 19:10:58 +0000622 errno = saved_errno;
623}
624
Denis Vlasenkoa37e7132008-02-19 02:57:07 +0000625static void new_init_action(uint8_t action_type, const char *command, const char *cons)
Erik Andersen7dc16072000-01-04 01:10:25 +0000626{
Denis Vlasenkoa37e7132008-02-19 02:57:07 +0000627 struct init_action *a, *last;
Erik Andersen9e737252000-01-06 01:16:13 +0000628
Denis Vlasenkoa37e7132008-02-19 02:57:07 +0000629// Why?
630// if (strcmp(cons, bb_dev_null) == 0 && (action & ASKFIRST))
631// return;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000632
Eric Andersen0298be82002-03-05 15:12:19 +0000633 /* Append to the end of the list */
Eric Andersen22718092004-10-08 08:17:39 +0000634 for (a = last = init_action_list; a; a = a->next) {
Eric Andersen82baf632004-10-08 08:21:54 +0000635 /* don't enter action if it's already in the list,
636 * but do overwrite existing actions */
Denis Vlasenkoec27feb2007-02-17 15:52:02 +0000637 if ((strcmp(a->command, command) == 0)
Denis Vlasenkob74a2db2008-07-21 21:34:51 +0000638 && (strcmp(a->terminal, cons) == 0)
639 ) {
Denis Vlasenkoa37e7132008-02-19 02:57:07 +0000640 a->action_type = action_type;
Eric Andersen6fd0e312003-07-22 09:48:56 +0000641 return;
642 }
Eric Andersen22718092004-10-08 08:17:39 +0000643 last = a;
Eric Andersen6fd0e312003-07-22 09:48:56 +0000644 }
Denis Vlasenko7a2ca5e2007-02-21 00:15:20 +0000645
Denis Vlasenkoa37e7132008-02-19 02:57:07 +0000646 a = xzalloc(sizeof(*a));
Eric Andersen22718092004-10-08 08:17:39 +0000647 if (last) {
Denis Vlasenkoa37e7132008-02-19 02:57:07 +0000648 last->next = a;
Eric Andersen1644db92001-09-05 20:18:15 +0000649 } else {
Denis Vlasenkoa37e7132008-02-19 02:57:07 +0000650 init_action_list = a;
Eric Andersen1644db92001-09-05 20:18:15 +0000651 }
Denis Vlasenkoa37e7132008-02-19 02:57:07 +0000652 a->action_type = action_type;
653 safe_strncpy(a->command, command, sizeof(a->command));
654 safe_strncpy(a->terminal, cons, sizeof(a->terminal));
Denis Vlasenko7a2ca5e2007-02-21 00:15:20 +0000655 messageD(L_LOG | L_CONSOLE, "command='%s' action=%d tty='%s'\n",
Denis Vlasenkoa37e7132008-02-19 02:57:07 +0000656 a->command, a->action_type, a->terminal);
Erik Andersen7dc16072000-01-04 01:10:25 +0000657}
658
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000659/* NOTE that if CONFIG_FEATURE_USE_INITTAB is NOT defined,
Erik Andersen9e737252000-01-06 01:16:13 +0000660 * then parse_inittab() simply adds in some default
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000661 * actions(i.e., runs INIT_SCRIPT and then starts a pair
662 * of "askfirst" shells). If CONFIG_FEATURE_USE_INITTAB
663 * _is_ defined, but /etc/inittab is missing, this
Erik Andersen812d4662000-01-07 18:30:40 +0000664 * results in the same set of default behaviors.
Glenn L McGrathbaf55a82002-08-22 18:22:10 +0000665 */
Eric Andersen3e6ff902001-03-09 21:24:12 +0000666static void parse_inittab(void)
Erik Andersen7dc16072000-01-04 01:10:25 +0000667{
Bernhard Reutner-Fischer54d50a02008-07-17 14:00:42 +0000668 char *token[4];
Denis Vlasenkoa474b682008-07-17 17:58:44 +0000669 /* order must correspond to SYSINIT..RESTART constants */
670 static const char actions[] ALIGN1 =
671 "sysinit\0""respawn\0""askfirst\0""wait\0""once\0"
672 "ctrlaltdel\0""shutdown\0""restart\0";
Bernhard Reutner-Fischer54d50a02008-07-17 14:00:42 +0000673
Denis Vlasenko5415c852008-07-21 23:05:26 +0000674 parser_t *parser = config_open2(INITTAB, fopen_for_read);
Bernhard Reutner-Fischer54d50a02008-07-17 14:00:42 +0000675 /* No inittab file -- set up some default behavior */
676 if (parser == NULL) {
677 /* Reboot on Ctrl-Alt-Del */
678 new_init_action(CTRLALTDEL, "reboot", "");
679 /* Umount all filesystems on halt/reboot */
680 new_init_action(SHUTDOWN, "umount -a -r", "");
681 /* Swapoff on halt/reboot */
682 if (ENABLE_SWAPONOFF)
683 new_init_action(SHUTDOWN, "swapoff -a", "");
684 /* Prepare to restart init when a QUIT is received */
685 new_init_action(RESTART, "init", "");
686 /* Askfirst shell on tty1-4 */
687 new_init_action(ASKFIRST, bb_default_login_shell, "");
Denis Vlasenkoa474b682008-07-17 17:58:44 +0000688//TODO: VC_1 instead of ""? "" is console -> ctty problems -> angry users
Bernhard Reutner-Fischer54d50a02008-07-17 14:00:42 +0000689 new_init_action(ASKFIRST, bb_default_login_shell, VC_2);
690 new_init_action(ASKFIRST, bb_default_login_shell, VC_3);
691 new_init_action(ASKFIRST, bb_default_login_shell, VC_4);
692 /* sysinit */
693 new_init_action(SYSINIT, INIT_SCRIPT, "");
Bernhard Reutner-Fischer54d50a02008-07-17 14:00:42 +0000694 return;
695 }
696 /* optional_tty:ignored_runlevel:action:command
Denis Vlasenkoa474b682008-07-17 17:58:44 +0000697 * Delims are not to be collapsed and need exactly 4 tokens
Bernhard Reutner-Fischer54d50a02008-07-17 14:00:42 +0000698 */
Denis Vlasenko084266e2008-07-26 23:08:31 +0000699 while (config_read(parser, token, 4, 0, "#:",
700 PARSE_NORMAL & ~(PARSE_TRIM | PARSE_COLLAPSE))) {
Denis Vlasenkoa474b682008-07-17 17:58:44 +0000701 int action;
Bernhard Reutner-Fischer54d50a02008-07-17 14:00:42 +0000702 char *tty = token[0];
Bernhard Reutner-Fischer54d50a02008-07-17 14:00:42 +0000703
Denis Vlasenkoa474b682008-07-17 17:58:44 +0000704 if (!token[3]) /* less than 4 tokens */
Bernhard Reutner-Fischer54d50a02008-07-17 14:00:42 +0000705 goto bad_entry;
Denis Vlasenkoa474b682008-07-17 17:58:44 +0000706 action = index_in_strings(actions, token[2]);
707 if (action < 0 || !token[3][0]) /* token[3]: command */
708 goto bad_entry;
709 /* turn .*TTY -> /dev/TTY */
710 if (tty[0]) {
711 if (strncmp(tty, "/dev/", 5) == 0)
Bernhard Reutner-Fischer54d50a02008-07-17 14:00:42 +0000712 tty += 5;
713 tty = concat_path_file("/dev/", tty);
Denis Vlasenkoa474b682008-07-17 17:58:44 +0000714 }
715 new_init_action(1 << action, token[3], tty);
716 if (tty[0])
Bernhard Reutner-Fischer54d50a02008-07-17 14:00:42 +0000717 free(tty);
718 continue;
719 bad_entry:
Bernhard Reutner-Fischerad2fa652008-07-21 11:16:39 +0000720 message(L_LOG | L_CONSOLE, "Bad inittab entry at line %d",
721 parser->lineno);
Bernhard Reutner-Fischer54d50a02008-07-17 14:00:42 +0000722 }
723 config_close(parser);
Erik Andersen7dc16072000-01-04 01:10:25 +0000724}
725
Denis Vlasenkoad4da982008-04-05 04:24:23 +0000726#if ENABLE_FEATURE_USE_INITTAB
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000727static void reload_signal(int sig UNUSED_PARAM)
Eric Andersen6fd0e312003-07-22 09:48:56 +0000728{
Eric Andersen82baf632004-10-08 08:21:54 +0000729 struct init_action *a, *tmp;
730
Denis Vlasenko7a2ca5e2007-02-21 00:15:20 +0000731 message(L_LOG, "reloading /etc/inittab");
Eric Andersen82baf632004-10-08 08:21:54 +0000732
733 /* disable old entrys */
Denis Vlasenkoec5631b2007-12-25 01:08:58 +0000734 for (a = init_action_list; a; a = a->next) {
Denis Vlasenkoa37e7132008-02-19 02:57:07 +0000735 a->action_type = ONCE;
Eric Andersen82baf632004-10-08 08:21:54 +0000736 }
737
738 parse_inittab();
739
Mike Frysingerbb50fdf2007-12-25 04:30:14 +0000740 if (ENABLE_FEATURE_KILL_REMOVED) {
Denis Vlasenkod55268d2007-12-26 18:32:58 +0000741 /* Be nice and send SIGTERM first */
Mike Frysingerbb50fdf2007-12-25 04:30:14 +0000742 for (a = init_action_list; a; a = a->next) {
743 pid_t pid = a->pid;
Denis Vlasenkoa37e7132008-02-19 02:57:07 +0000744 if ((a->action_type & ONCE) && pid != 0) {
Mike Frysingerbb50fdf2007-12-25 04:30:14 +0000745 kill(pid, SIGTERM);
Denis Vlasenkoec5631b2007-12-25 01:08:58 +0000746 }
Denis Vlasenkoec5631b2007-12-25 01:08:58 +0000747 }
Denis Vlasenkod55268d2007-12-26 18:32:58 +0000748#if CONFIG_FEATURE_KILL_DELAY
Denis Vlasenko671ca332008-02-19 14:13:20 +0000749 /* NB: parent will wait in NOMMU case */
750 if ((BB_MMU ? fork() : vfork()) == 0) { /* child */
Denis Vlasenkod55268d2007-12-26 18:32:58 +0000751 sleep(CONFIG_FEATURE_KILL_DELAY);
752 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) {
Denis Vlasenkod55268d2007-12-26 18:32:58 +0000755 kill(pid, SIGKILL);
756 }
757 }
Bernhard Reutner-Fischer636a1f82008-05-19 09:29:47 +0000758 _exit(EXIT_SUCCESS);
Denis Vlasenkod55268d2007-12-26 18:32:58 +0000759 }
760#endif
Denis Vlasenkoec5631b2007-12-25 01:08:58 +0000761 }
Denis Vlasenkoec5631b2007-12-25 01:08:58 +0000762
Eric Andersen82baf632004-10-08 08:21:54 +0000763 /* remove unused entrys */
764 for (a = init_action_list; a; a = tmp) {
765 tmp = a->next;
Denis Vlasenkoa37e7132008-02-19 02:57:07 +0000766 if ((a->action_type & (ONCE | SYSINIT | WAIT)) && a->pid == 0) {
Eric Andersen82baf632004-10-08 08:21:54 +0000767 delete_init_action(a);
768 }
769 }
Denis Vlasenkoad4da982008-04-05 04:24:23 +0000770 run_actions(RESPAWN | ASKFIRST);
Eric Andersen6fd0e312003-07-22 09:48:56 +0000771}
Denis Vlasenkob2b2c402009-01-28 23:56:31 +0000772#else
773void reload_signal(int sig);
Denis Vlasenkoad4da982008-04-05 04:24:23 +0000774#endif
Eric Andersen82baf632004-10-08 08:21:54 +0000775
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +0000776int init_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000777int init_main(int argc UNUSED_PARAM, char **argv)
Eric Andersen0460ff21999-10-25 23:32:44 +0000778{
Eric Andersen0298be82002-03-05 15:12:19 +0000779 struct init_action *a;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000780 pid_t wpid;
Eric Andersen0460ff21999-10-25 23:32:44 +0000781
Denis Vlasenko9b1381f2007-01-03 02:56:00 +0000782 die_sleep = 30 * 24*60*60; /* if xmalloc will ever die... */
783
Denis Vlasenko1d426652008-03-17 09:09:09 +0000784 if (argv[1] && !strcmp(argv[1], "-q")) {
Denis Vlasenko7a2ca5e2007-02-21 00:15:20 +0000785 return kill(1, SIGHUP);
Eric Andersen730f8262001-12-17 23:13:08 +0000786 }
Mike Frysingerbb50fdf2007-12-25 04:30:14 +0000787
Denis Vlasenko5cb54b52008-10-21 17:14:26 +0000788 if (!DEBUG_INIT) {
Mike Frysingerbb50fdf2007-12-25 04:30:14 +0000789 /* Expect to be invoked as init with PID=1 or be invoked as linuxrc */
790 if (getpid() != 1
791 && (!ENABLE_FEATURE_INITRD || !strstr(applet_name, "linuxrc"))
792 ) {
793 bb_show_usage();
794 }
795 /* Set up sig handlers -- be sure to
796 * clear all of these in run() */
Denis Vlasenko99a61842008-02-19 12:08:38 +0000797 signal(SIGQUIT, exec_restart_action);
Denis Vlasenko25591c32008-02-16 22:58:56 +0000798 bb_signals(0
799 + (1 << SIGUSR1) /* halt */
800 + (1 << SIGUSR2) /* poweroff */
801 + (1 << SIGTERM) /* reboot */
802 , halt_reboot_pwoff);
Mike Frysingerbb50fdf2007-12-25 04:30:14 +0000803 signal(SIGINT, ctrlaltdel_signal);
Denis Vlasenko3fa36e22008-11-09 00:15:11 +0000804 signal(SIGCONT, record_signo);
Denis Vlasenko25591c32008-02-16 22:58:56 +0000805 bb_signals(0
806 + (1 << SIGSTOP)
807 + (1 << SIGTSTP)
808 , stop_handler);
Mike Frysingerbb50fdf2007-12-25 04:30:14 +0000809
810 /* Turn off rebooting via CTL-ALT-DEL -- we get a
811 * SIGINT on CAD so we can shut things down gracefully... */
812 init_reboot(RB_DISABLE_CAD);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000813 }
Denis Vlasenkod8540f72007-06-14 07:53:06 +0000814
Eric Andersen599e3ce2002-07-03 11:08:10 +0000815 /* Figure out where the default console should be */
816 console_init();
Denis Vlasenkod238a472007-03-05 19:22:04 +0000817 set_sane_term();
Bernhard Reutner-Fischer54d50a02008-07-17 14:00:42 +0000818 xchdir("/");
Erik Andersene49d5ec2000-02-08 19:58:47 +0000819 setsid();
Denis Vlasenkob8d1a4c2008-09-20 16:28:59 +0000820
821 /* Make sure environs is set to something sane */
822 putenv((char *) "HOME=/");
823 putenv((char *) bb_PATH_root_path);
824 putenv((char *) "SHELL=/bin/sh");
825 putenv((char *) "USER=root"); /* needed? why? */
Rob Landleycba1b962006-07-09 17:28:17 +0000826
Bernhard Reutner-Fischer54d50a02008-07-17 14:00:42 +0000827 if (argv[1])
Denis Vlasenkob8d1a4c2008-09-20 16:28:59 +0000828 xsetenv("RUNLEVEL", argv[1]);
Rob Landleycba1b962006-07-09 17:28:17 +0000829
Erik Andersene49d5ec2000-02-08 19:58:47 +0000830 /* Hello world */
Denis Vlasenkoca525b42007-06-13 12:27:17 +0000831 message(MAYBE_CONSOLE | L_LOG, "init started: %s", bb_banner);
Eric Andersencc8ed391999-10-05 16:24:54 +0000832
Erik Andersene49d5ec2000-02-08 19:58:47 +0000833 /* Make sure there is enough memory to do something useful. */
Rob Landleyc3386a42005-08-30 18:50:37 +0000834 if (ENABLE_SWAPONOFF) {
835 struct sysinfo info;
836
837 if (!sysinfo(&info) &&
Denis Vlasenkodca0b702006-10-27 09:05:02 +0000838 (info.mem_unit ? : 1) * (long long)info.totalram < 1024*1024)
Rob Landleyc3386a42005-08-30 18:50:37 +0000839 {
Denis Vlasenko7a2ca5e2007-02-21 00:15:20 +0000840 message(L_CONSOLE, "Low memory, forcing swapon");
Rob Landleyc3386a42005-08-30 18:50:37 +0000841 /* swapon -a requires /proc typically */
Denis Vlasenko2f0c0d02007-01-21 00:41:04 +0000842 new_init_action(SYSINIT, "mount -t proc proc /proc", "");
Rob Landleyc3386a42005-08-30 18:50:37 +0000843 /* Try to turn on swap */
Denis Vlasenko2f0c0d02007-01-21 00:41:04 +0000844 new_init_action(SYSINIT, "swapon -a", "");
Rob Landleyc3386a42005-08-30 18:50:37 +0000845 run_actions(SYSINIT); /* wait and removing */
846 }
847 }
Erik Andersen9e737252000-01-06 01:16:13 +0000848
Erik Andersene49d5ec2000-02-08 19:58:47 +0000849 /* Check if we are supposed to be in single user mode */
Denis Vlasenko1d426652008-03-17 09:09:09 +0000850 if (argv[1]
Denis Vlasenkobf66fbc2006-12-21 13:23:14 +0000851 && (!strcmp(argv[1], "single") || !strcmp(argv[1], "-s") || LONE_CHAR(argv[1], '1'))
852 ) {
Bernhard Reutner-Fischer54d50a02008-07-17 14:00:42 +0000853 /* ??? shouldn't we set RUNLEVEL="b" here? */
Glenn L McGrathb4a1baa2003-01-13 22:09:50 +0000854 /* Start a shell on console */
Glenn L McGrathdc4e75e2003-09-02 02:36:18 +0000855 new_init_action(RESPAWN, bb_default_login_shell, "");
Erik Andersene49d5ec2000-02-08 19:58:47 +0000856 } else {
857 /* Not in single user mode -- see what inittab says */
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000858
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000859 /* NOTE that if CONFIG_FEATURE_USE_INITTAB is NOT defined,
Erik Andersene49d5ec2000-02-08 19:58:47 +0000860 * then parse_inittab() simply adds in some default
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000861 * actions(i.e., runs INIT_SCRIPT and then starts a pair
Erik Andersene49d5ec2000-02-08 19:58:47 +0000862 * of "askfirst" shells */
863 parse_inittab();
Eric Andersend00c2621999-12-07 08:37:31 +0000864 }
Erik Andersen983b51b2000-04-04 18:14:25 +0000865
Denis Vlasenko9b1381f2007-01-03 02:56:00 +0000866#if ENABLE_SELINUX
Rob Landleyb3ede5a2006-03-27 23:09:12 +0000867 if (getenv("SELINUX_INIT") == NULL) {
868 int enforce = 0;
869
Denis Vlasenkod8540f72007-06-14 07:53:06 +0000870 putenv((char*)"SELINUX_INIT=YES");
Rob Landleyb3ede5a2006-03-27 23:09:12 +0000871 if (selinux_init_load_policy(&enforce) == 0) {
Denis Vlasenko4921b542007-02-03 02:17:41 +0000872 BB_EXECVP(argv[0], argv);
Rob Landleyb3ede5a2006-03-27 23:09:12 +0000873 } else if (enforce > 0) {
874 /* SELinux in enforcing mode but load_policy failed */
Bernhard Reutner-Fischera53de7f2008-07-21 13:46:54 +0000875 message(L_CONSOLE, "cannot load SELinux Policy. "
Denis Vlasenkob7167542007-02-27 19:20:00 +0000876 "Machine is in enforcing mode. Halting now.");
Bernhard Reutner-Fischer636a1f82008-05-19 09:29:47 +0000877 exit(EXIT_FAILURE);
Rob Landleyb3ede5a2006-03-27 23:09:12 +0000878 }
879 }
880#endif /* CONFIG_SELINUX */
881
Denis Vlasenko2afabe82007-12-10 07:06:04 +0000882 /* Make the command line just say "init" - thats all, nothing else */
883 strncpy(argv[0], "init", strlen(argv[0]));
884 /* Wipe argv[1]-argv[N] so they don't clutter the ps listing */
885 while (*++argv)
886 memset(*argv, 0, strlen(*argv));
Eric Andersen219d6f51999-11-02 19:43:01 +0000887
Erik Andersene49d5ec2000-02-08 19:58:47 +0000888 /* Now run everything that needs to be run */
889
890 /* First run the sysinit command */
Eric Andersen1644db92001-09-05 20:18:15 +0000891 run_actions(SYSINIT);
Eric Andersen0298be82002-03-05 15:12:19 +0000892
Erik Andersene49d5ec2000-02-08 19:58:47 +0000893 /* Next run anything that wants to block */
Eric Andersen1644db92001-09-05 20:18:15 +0000894 run_actions(WAIT);
Eric Andersen0298be82002-03-05 15:12:19 +0000895
Erik Andersene49d5ec2000-02-08 19:58:47 +0000896 /* Next run anything to be run only once */
Eric Andersen0298be82002-03-05 15:12:19 +0000897 run_actions(ONCE);
898
Eric Andersen6fd0e312003-07-22 09:48:56 +0000899 /* Redefine SIGHUP to reread /etc/inittab */
Denis Vlasenkob2b2c402009-01-28 23:56:31 +0000900 signal(SIGHUP, ENABLE_FEATURE_USE_INITTAB ? reload_signal : SIG_IGN);
Eric Andersen82baf632004-10-08 08:21:54 +0000901
Erik Andersene49d5ec2000-02-08 19:58:47 +0000902 /* Now run the looping stuff for the rest of forever */
903 while (1) {
Denis Vlasenkoad4da982008-04-05 04:24:23 +0000904 /* run the respawn/askfirst stuff */
905 run_actions(RESPAWN | ASKFIRST);
Eric Andersen0298be82002-03-05 15:12:19 +0000906
907 /* Don't consume all CPU time -- sleep a bit */
908 sleep(1);
909
Denis Vlasenkofb0eba72008-01-02 19:55:04 +0000910 /* Wait for any child process to exit */
Bernhard Reutner-Fischerc58dbf22006-05-30 12:16:54 +0000911 wpid = wait(NULL);
Eric Andersen87812dc2004-04-12 19:21:54 +0000912 while (wpid > 0) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000913 /* Find out who died and clean up their corpse */
Eric Andersen0298be82002-03-05 15:12:19 +0000914 for (a = init_action_list; a; a = a->next) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000915 if (a->pid == wpid) {
Eric Andersen0298be82002-03-05 15:12:19 +0000916 /* Set the pid to 0 so that the process gets
917 * restarted by run_actions() */
Erik Andersene49d5ec2000-02-08 19:58:47 +0000918 a->pid = 0;
Denis Vlasenko7a2ca5e2007-02-21 00:15:20 +0000919 message(L_LOG, "process '%s' (pid %d) exited. "
Denis Vlasenkoa37e7132008-02-19 02:57:07 +0000920 "Scheduling for restart.",
Glenn L McGrathb4a1baa2003-01-13 22:09:50 +0000921 a->command, wpid);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000922 }
923 }
Eric Andersencf1fee02002-12-17 09:48:16 +0000924 /* see if anyone else is waiting to be reaped */
Denis Vlasenkofb0eba72008-01-02 19:55:04 +0000925 wpid = wait_any_nohang(NULL);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000926 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000927 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000928}