blob: 0e4a8f1e587348ebb3888a5900134177eece20b1 [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
Bernhard Reutner-Fischer54d50a02008-07-17 14:00:42 +000046/*
Denis Vlasenko2afabe82007-12-10 07:06:04 +000047#define STR_SYSINIT "\x01"
48#define STR_RESPAWN "\x02"
49#define STR_ASKFIRST "\x04"
50#define STR_WAIT "\x08"
51#define STR_ONCE "\x10"
52#define STR_CTRLALTDEL "\x20"
53#define STR_SHUTDOWN "\x40"
54#define STR_RESTART "\x80"
Bernhard Reutner-Fischer54d50a02008-07-17 14:00:42 +000055*/
Eric Andersen0298be82002-03-05 15:12:19 +000056/* Set up a linked list of init_actions, to be read from inittab */
57struct init_action {
Eric Andersen0298be82002-03-05 15:12:19 +000058 struct init_action *next;
Denis Vlasenko7a2ca5e2007-02-21 00:15:20 +000059 pid_t pid;
Denis Vlasenkoa37e7132008-02-19 02:57:07 +000060 uint8_t action_type;
Denis Vlasenko7a2ca5e2007-02-21 00:15:20 +000061 char terminal[CONSOLE_NAME_SIZE];
Denis Vlasenkoa37e7132008-02-19 02:57:07 +000062 char command[COMMAND_SIZE];
Erik Andersen7dc16072000-01-04 01:10:25 +000063};
Erik Andersen7dc16072000-01-04 01:10:25 +000064
Eric Andersen0298be82002-03-05 15:12:19 +000065/* Static variables */
66static struct init_action *init_action_list = NULL;
Glenn L McGrathbaf55a82002-08-22 18:22:10 +000067
Denis Vlasenko4c978632007-02-03 03:31:13 +000068static const char *log_console = VC_5;
Eric Andersen0298be82002-03-05 15:12:19 +000069static sig_atomic_t got_cont = 0;
Rob Landleybc68cd12006-03-10 19:22:06 +000070
71enum {
Denis Vlasenkoec27feb2007-02-17 15:52:02 +000072 L_LOG = 0x1,
73 L_CONSOLE = 0x2,
Glenn L McGrathbaf55a82002-08-22 18:22:10 +000074
Denis Vlasenko9b1381f2007-01-03 02:56:00 +000075#if ENABLE_FEATURE_EXTRA_QUIET
Rob Landleybc68cd12006-03-10 19:22:06 +000076 MAYBE_CONSOLE = 0x0,
Eric Andersen0298be82002-03-05 15:12:19 +000077#else
Denis Vlasenkoec27feb2007-02-17 15:52:02 +000078 MAYBE_CONSOLE = L_CONSOLE,
Eric Andersen0298be82002-03-05 15:12:19 +000079#endif
Glenn L McGrathbaf55a82002-08-22 18:22:10 +000080
Rob Landleybc68cd12006-03-10 19:22:06 +000081#ifndef RB_HALT_SYSTEM
Bernhard Reutner-Fischercf1f2ac2006-06-02 10:43:17 +000082 RB_HALT_SYSTEM = 0xcdef0123, /* FIXME: this overflows enum */
Rob Landleybc68cd12006-03-10 19:22:06 +000083 RB_ENABLE_CAD = 0x89abcdef,
84 RB_DISABLE_CAD = 0,
85 RB_POWER_OFF = 0x4321fedc,
86 RB_AUTOBOOT = 0x01234567,
Eric Andersen0298be82002-03-05 15:12:19 +000087#endif
Rob Landleybc68cd12006-03-10 19:22:06 +000088};
Erik Andersen42094cd2000-03-20 21:34:52 +000089
Denis Vlasenko6ca409e2007-08-12 20:58:27 +000090static const char *const environment[] = {
Glenn L McGrathb4a1baa2003-01-13 22:09:50 +000091 "HOME=/",
Denis Vlasenkof5f75c52007-06-12 22:35:19 +000092 bb_PATH_root_path,
Glenn L McGrathdc4e75e2003-09-02 02:36:18 +000093 "SHELL=/bin/sh",
Glenn L McGrathb4a1baa2003-01-13 22:09:50 +000094 "USER=root",
95 NULL
96};
97
Eric Andersen0298be82002-03-05 15:12:19 +000098/* Function prototypes */
99static void delete_init_action(struct init_action *a);
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000100static void halt_reboot_pwoff(int sig) NORETURN;
Eric Andersen0460ff21999-10-25 23:32:44 +0000101
Denis Vlasenko21e20cb2008-01-04 15:10:47 +0000102static void waitfor(pid_t pid)
Denis Vlasenkofb0eba72008-01-02 19:55:04 +0000103{
Denis Vlasenko21e20cb2008-01-04 15:10:47 +0000104 /* waitfor(run(x)): protect against failed fork inside run() */
105 if (pid <= 0)
106 return;
107
108 /* Wait for any child (prevent zombies from exiting orphaned processes)
109 * but exit the loop only when specified one has exited. */
110 while (wait(NULL) != pid)
111 continue;
Denis Vlasenkofb0eba72008-01-02 19:55:04 +0000112}
113
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000114static void loop_forever(void) NORETURN;
Eric Andersen74400cc2001-10-18 04:11:39 +0000115static void loop_forever(void)
Matt Kraai67a46402001-06-03 05:55:52 +0000116{
117 while (1)
Glenn L McGrathbaf55a82002-08-22 18:22:10 +0000118 sleep(1);
Matt Kraai67a46402001-06-03 05:55:52 +0000119}
Eric Andersen7ef1a5b2001-03-20 17:39:08 +0000120
Erik Andersen42094cd2000-03-20 21:34:52 +0000121/* Print a message to the specified device.
Denis Vlasenko671ca332008-02-19 14:13:20 +0000122 * "where" may be bitwise-or'd from L_LOG | L_CONSOLE
123 * NB: careful, we can be called after vfork!
124 */
Mike Frysingerbb50fdf2007-12-25 04:30:14 +0000125#define messageD(...) do { if (ENABLE_DEBUG_INIT) message(__VA_ARGS__); } while (0)
Denis Vlasenko671ca332008-02-19 14:13:20 +0000126static void message(int where, const char *fmt, ...)
Glenn L McGrathbaf55a82002-08-22 18:22:10 +0000127 __attribute__ ((format(printf, 2, 3)));
Denis Vlasenko671ca332008-02-19 14:13:20 +0000128static void message(int where, const char *fmt, ...)
Eric Andersen0460ff21999-10-25 23:32:44 +0000129{
Eric Andersen22237012003-01-23 07:08:26 +0000130 static int log_fd = -1;
Denis Vlasenko7a2ca5e2007-02-21 00:15:20 +0000131 va_list arguments;
132 int l;
133 char msg[128];
134
Glenn L McGrathb4a1baa2003-01-13 22:09:50 +0000135 msg[0] = '\r';
Denis Vlasenko7a2ca5e2007-02-21 00:15:20 +0000136 va_start(arguments, fmt);
137 vsnprintf(msg + 1, sizeof(msg) - 2, fmt, arguments);
Denis Vlasenkoec27feb2007-02-17 15:52:02 +0000138 va_end(arguments);
Denis Vlasenko7a2ca5e2007-02-21 00:15:20 +0000139 msg[sizeof(msg) - 2] = '\0';
140 l = strlen(msg);
Glenn L McGrathb4a1baa2003-01-13 22:09:50 +0000141
Mike Frysingerbb50fdf2007-12-25 04:30:14 +0000142 if (ENABLE_FEATURE_INIT_SYSLOG) {
143 /* Log the message to syslogd */
Denis Vlasenko671ca332008-02-19 14:13:20 +0000144 if (where & L_LOG) {
Mike Frysingerbb50fdf2007-12-25 04:30:14 +0000145 /* don't out "\r" */
146 openlog(applet_name, 0, LOG_DAEMON);
147 syslog(LOG_INFO, "init: %s", msg + 1);
148 closelog();
149 }
150 msg[l++] = '\n';
151 msg[l] = '\0';
152 } else {
153 msg[l++] = '\n';
154 msg[l] = '\0';
155 /* Take full control of the log tty, and never close it.
156 * It's mine, all mine! Muhahahaha! */
157 if (log_fd < 0) {
158 if (!log_console) {
159 log_fd = 2;
Denis Vlasenko7a2ca5e2007-02-21 00:15:20 +0000160 } else {
Mike Frysingerbb50fdf2007-12-25 04:30:14 +0000161 log_fd = device_open(log_console, O_WRONLY | O_NONBLOCK | O_NOCTTY);
162 if (log_fd < 0) {
163 bb_error_msg("can't log to %s", log_console);
Denis Vlasenko671ca332008-02-19 14:13:20 +0000164 where = L_CONSOLE;
Mike Frysingerbb50fdf2007-12-25 04:30:14 +0000165 } else {
166 close_on_exec_on(log_fd);
167 }
Denis Vlasenko7a2ca5e2007-02-21 00:15:20 +0000168 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000169 }
Denis Vlasenko671ca332008-02-19 14:13:20 +0000170 if (where & L_LOG) {
Mike Frysingerbb50fdf2007-12-25 04:30:14 +0000171 full_write(log_fd, msg, l);
172 if (log_fd == 2)
173 return; /* don't print dup messages */
174 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000175 }
Eric Andersen4c781471999-11-26 08:12:56 +0000176
Denis Vlasenko671ca332008-02-19 14:13:20 +0000177 if (where & L_CONSOLE) {
Denis Vlasenko7a2ca5e2007-02-21 00:15:20 +0000178 /* Send console messages to console so people will see them. */
Bernhard Reutner-Fischer5e25ddb2008-05-19 09:48:17 +0000179 full_write(STDERR_FILENO, msg, l);
Eric Andersen3ae0c781999-11-04 01:13:21 +0000180 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000181}
182
Denis Vlasenko7a2ca5e2007-02-21 00:15:20 +0000183/* From <linux/serial.h> */
184struct serial_struct {
185 int type;
186 int line;
187 unsigned int port;
188 int irq;
189 int flags;
190 int xmit_fifo_size;
191 int custom_divisor;
192 int baud_base;
193 unsigned short close_delay;
194 char io_type;
195 char reserved_char[1];
196 int hub6;
197 unsigned short closing_wait; /* time to wait before closing */
198 unsigned short closing_wait2; /* no longer used... */
199 unsigned char *iomem_base;
200 unsigned short iomem_reg_shift;
201 unsigned int port_high;
202 unsigned long iomap_base; /* cookie passed into ioremap */
203 int reserved[1];
204 /* Paranoia (imagine 64bit kernel overwriting 32bit userspace stack) */
205 uint32_t bbox_reserved[16];
206};
Eric Andersen74400cc2001-10-18 04:11:39 +0000207static void console_init(void)
Eric Andersen0460ff21999-10-25 23:32:44 +0000208{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000209 struct serial_struct sr;
210 char *s;
Eric Andersen0460ff21999-10-25 23:32:44 +0000211
Denis Vlasenko7a2ca5e2007-02-21 00:15:20 +0000212 s = getenv("CONSOLE");
213 if (!s) s = getenv("console");
214 if (s) {
215 int fd = open(s, O_RDWR | O_NONBLOCK | O_NOCTTY);
216 if (fd >= 0) {
217 dup2(fd, 0);
218 dup2(fd, 1);
Denis Vlasenkoe421b5e2008-03-17 22:01:42 +0000219 xmove_fd(fd, 2);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000220 }
Denis Vlasenko7a2ca5e2007-02-21 00:15:20 +0000221 messageD(L_LOG, "console='%s'", s);
Denis Vlasenkod8540f72007-06-14 07:53:06 +0000222 } else {
Denis Vlasenkofb274df2008-03-17 13:26:51 +0000223 /* Make sure fd 0,1,2 are not closed
224 * (so that they won't be used by future opens) */
Denis Vlasenkod48e81f2008-07-06 07:00:11 +0000225 bb_sanitize_stdio();
Eric Andersen07e52971999-11-07 07:38:08 +0000226 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000227
Denis Vlasenko7a2ca5e2007-02-21 00:15:20 +0000228 s = getenv("TERM");
Denis Vlasenkoe421b5e2008-03-17 22:01:42 +0000229 if (ioctl(STDIN_FILENO, TIOCGSERIAL, &sr) == 0) {
Denis Vlasenko2afabe82007-12-10 07:06:04 +0000230 /* Force the TERM setting to vt102 for serial console
Denis Vlasenko7a2ca5e2007-02-21 00:15:20 +0000231 * if TERM is set to linux (the default) */
232 if (!s || strcmp(s, "linux") == 0)
233 putenv((char*)"TERM=vt102");
Mike Frysingerbb50fdf2007-12-25 04:30:14 +0000234 if (!ENABLE_FEATURE_INIT_SYSLOG)
235 log_console = NULL;
Denis Vlasenko7a2ca5e2007-02-21 00:15:20 +0000236 } else if (!s)
237 putenv((char*)"TERM=linux");
Eric Andersen0460ff21999-10-25 23:32:44 +0000238}
Glenn L McGrathbaf55a82002-08-22 18:22:10 +0000239
Denis Vlasenko671ca332008-02-19 14:13:20 +0000240/* Set terminal settings to reasonable defaults.
241 * NB: careful, we can be called after vfork! */
Denis Vlasenko2afabe82007-12-10 07:06:04 +0000242static void set_sane_term(void)
Eric Andersen7ef1a5b2001-03-20 17:39:08 +0000243{
Denis Vlasenko2afabe82007-12-10 07:06:04 +0000244 struct termios tty;
Eric Andersen7ef1a5b2001-03-20 17:39:08 +0000245
Denis Vlasenko2afabe82007-12-10 07:06:04 +0000246 tcgetattr(STDIN_FILENO, &tty);
247
248 /* set control chars */
249 tty.c_cc[VINTR] = 3; /* C-c */
250 tty.c_cc[VQUIT] = 28; /* C-\ */
251 tty.c_cc[VERASE] = 127; /* C-? */
252 tty.c_cc[VKILL] = 21; /* C-u */
253 tty.c_cc[VEOF] = 4; /* C-d */
254 tty.c_cc[VSTART] = 17; /* C-q */
255 tty.c_cc[VSTOP] = 19; /* C-s */
256 tty.c_cc[VSUSP] = 26; /* C-z */
257
258 /* use line dicipline 0 */
259 tty.c_line = 0;
260
261 /* Make it be sane */
262 tty.c_cflag &= CBAUD | CBAUDEX | CSIZE | CSTOPB | PARENB | PARODD;
263 tty.c_cflag |= CREAD | HUPCL | CLOCAL;
264
265 /* input modes */
266 tty.c_iflag = ICRNL | IXON | IXOFF;
267
268 /* output modes */
269 tty.c_oflag = OPOST | ONLCR;
270
271 /* local modes */
272 tty.c_lflag =
273 ISIG | ICANON | ECHO | ECHOE | ECHOK | ECHOCTL | ECHOKE | IEXTEN;
274
275 tcsetattr(STDIN_FILENO, TCSANOW, &tty);
Eric Andersen7ef1a5b2001-03-20 17:39:08 +0000276}
277
Denis Vlasenko671ca332008-02-19 14:13:20 +0000278/* Open the new terminal device.
279 * NB: careful, we can be called after vfork! */
Denis Vlasenkod55268d2007-12-26 18:32:58 +0000280static void open_stdio_to_tty(const char* tty_name, int exit_on_failure)
Denis Vlasenko7a2ca5e2007-02-21 00:15:20 +0000281{
282 /* empty tty_name means "use init's tty", else... */
283 if (tty_name[0]) {
Denis Vlasenkod55268d2007-12-26 18:32:58 +0000284 int fd;
285 close(0);
286 /* fd can be only < 0 or 0: */
287 fd = device_open(tty_name, O_RDWR);
288 if (fd) {
Denis Vlasenko966bb432007-02-27 19:20:33 +0000289 message(L_LOG | L_CONSOLE, "Can't open %s: %s",
Denis Vlasenko7a2ca5e2007-02-21 00:15:20 +0000290 tty_name, strerror(errno));
Denis Vlasenkod55268d2007-12-26 18:32:58 +0000291 if (exit_on_failure)
Bernhard Reutner-Fischer636a1f82008-05-19 09:29:47 +0000292 _exit(EXIT_FAILURE);
Denis Vlasenkod55268d2007-12-26 18:32:58 +0000293 if (ENABLE_DEBUG_INIT)
Mike Frysingerbb50fdf2007-12-25 04:30:14 +0000294 _exit(2);
Denis Vlasenko671ca332008-02-19 14:13:20 +0000295 /* NB: we don't reach this if we were called after vfork.
296 * Thus halt_reboot_pwoff() itself need not be vfork-safe. */
Denis Vlasenkod55268d2007-12-26 18:32:58 +0000297 halt_reboot_pwoff(SIGUSR1); /* halt the system */
Denis Vlasenko7a2ca5e2007-02-21 00:15:20 +0000298 }
Denis Vlasenkod55268d2007-12-26 18:32:58 +0000299 dup2(0, 1);
300 dup2(0, 2);
Bernhard Reutner-Fischer0da069d2006-05-29 12:54:16 +0000301 }
Denis Vlasenkod238a472007-03-05 19:22:04 +0000302 set_sane_term();
Bernhard Reutner-Fischer0da069d2006-05-29 12:54:16 +0000303}
304
Denis Vlasenko671ca332008-02-19 14:13:20 +0000305/* Wrapper around exec:
306 * Takes string (max COMMAND_SIZE chars).
307 * If chars like '>' detected, execs '[-]/bin/sh -c "exec ......."'.
308 * Otherwise splits words on whitespace, deals with leading dash,
309 * and uses plain exec().
310 * NB: careful, we can be called after vfork!
Denis Vlasenkoa37e7132008-02-19 02:57:07 +0000311 */
312static void init_exec(const char *command)
313{
314 char *cmd[COMMAND_SIZE / 2];
315 char buf[COMMAND_SIZE + 6]; /* COMMAND_SIZE+strlen("exec ")+1 */
Denis Vlasenko671ca332008-02-19 14:13:20 +0000316 int dash = (command[0] == '-' /* maybe? && command[1] == '/' */);
Denis Vlasenkoa37e7132008-02-19 02:57:07 +0000317
318 /* See if any special /bin/sh requiring characters are present */
319 if (strpbrk(command, "~`!$^&*()=|\\{}[];\"'<>?") != NULL) {
320 strcpy(buf, "exec ");
321 strcpy(buf + 5, command + dash); /* excluding "-" */
Denis Vlasenko671ca332008-02-19 14:13:20 +0000322 /* NB: LIBBB_DEFAULT_LOGIN_SHELL define has leading dash */
Denis Vlasenkoa37e7132008-02-19 02:57:07 +0000323 cmd[0] = (char*)(LIBBB_DEFAULT_LOGIN_SHELL + !dash);
324 cmd[1] = (char*)"-c";
325 cmd[2] = buf;
326 cmd[3] = NULL;
327 } else {
328 /* Convert command (char*) into cmd (char**, one word per string) */
329 char *word, *next;
330 int i = 0;
331 next = strcpy(buf, command); /* including "-" */
332 while ((word = strsep(&next, " \t")) != NULL) {
333 if (*word != '\0') { /* not two spaces/tabs together? */
334 cmd[i] = word;
335 i++;
336 }
337 }
338 cmd[i] = NULL;
339 }
340 /* If we saw leading "-", it is interactive shell.
341 * Try harder to give it a controlling tty.
342 * And skip "-" in actual exec call. */
343 if (dash) {
344 /* _Attempt_ to make stdin a controlling tty. */
345 if (ENABLE_FEATURE_INIT_SCTTY)
346 ioctl(STDIN_FILENO, TIOCSCTTY, 0 /*only try, don't steal*/);
347 }
348 BB_EXECVP(cmd[0] + dash, cmd);
349 message(L_LOG | L_CONSOLE, "Cannot run '%s': %s",
350 cmd[0], strerror(errno));
351 /* returns if execvp fails */
352}
353
Denis Vlasenko2afabe82007-12-10 07:06:04 +0000354/* Used only by run_actions */
Glenn L McGrathb4a1baa2003-01-13 22:09:50 +0000355static pid_t run(const struct init_action *a)
Eric Andersen0298be82002-03-05 15:12:19 +0000356{
Mike Frysinger10427ab2005-07-06 05:00:48 +0000357 pid_t pid;
Eric Andersen0298be82002-03-05 15:12:19 +0000358 sigset_t nmask, omask;
Erik Andersenac6e71f2000-01-08 22:04:33 +0000359
Denis Vlasenko2afabe82007-12-10 07:06:04 +0000360 /* Block sigchild while forking (why?) */
Eric Andersen0298be82002-03-05 15:12:19 +0000361 sigemptyset(&nmask);
362 sigaddset(&nmask, SIGCHLD);
363 sigprocmask(SIG_BLOCK, &nmask, &omask);
Denis Vlasenkoad4da982008-04-05 04:24:23 +0000364 if (BB_MMU && (a->action_type & ASKFIRST))
365 pid = fork();
366 else
367 pid = vfork();
Denis Vlasenko966bb432007-02-27 19:20:33 +0000368 sigprocmask(SIG_SETMASK, &omask, NULL);
Eric Andersen0298be82002-03-05 15:12:19 +0000369
Denis Vlasenko2afabe82007-12-10 07:06:04 +0000370 if (pid < 0)
371 message(L_LOG | L_CONSOLE, "Can't fork");
Denis Vlasenko966bb432007-02-27 19:20:33 +0000372 if (pid)
373 return pid;
Eric Andersen0298be82002-03-05 15:12:19 +0000374
Denis Vlasenko2afabe82007-12-10 07:06:04 +0000375 /* Child */
376
Denis Vlasenko966bb432007-02-27 19:20:33 +0000377 /* Reset signal handlers that were set by the parent process */
Denis Vlasenko25591c32008-02-16 22:58:56 +0000378 bb_signals(0
379 + (1 << SIGUSR1)
380 + (1 << SIGUSR2)
381 + (1 << SIGINT)
382 + (1 << SIGTERM)
383 + (1 << SIGHUP)
384 + (1 << SIGQUIT)
385 + (1 << SIGCONT)
386 + (1 << SIGSTOP)
387 + (1 << SIGTSTP)
388 , SIG_DFL);
Eric Andersen2f6c04f1999-11-01 23:59:44 +0000389
Denis Vlasenko966bb432007-02-27 19:20:33 +0000390 /* Create a new session and make ourself the process
391 * group leader */
392 setsid();
Eric Andersen599e3ce2002-07-03 11:08:10 +0000393
Denis Vlasenko966bb432007-02-27 19:20:33 +0000394 /* Open the new terminal device */
Denis Vlasenko671ca332008-02-19 14:13:20 +0000395 open_stdio_to_tty(a->terminal, 1 /* - exit if open fails */);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000396
Denis Vlasenko671ca332008-02-19 14:13:20 +0000397// NB: do not enable unless you change vfork to fork above
Denis Vlasenko2afabe82007-12-10 07:06:04 +0000398#ifdef BUT_RUN_ACTIONS_ALREADY_DOES_WAITING
Denis Vlasenko966bb432007-02-27 19:20:33 +0000399 /* If the init Action requires us to wait, then force the
400 * supplied terminal to be the controlling tty. */
Denis Vlasenkoa37e7132008-02-19 02:57:07 +0000401 if (a->action_type & (SYSINIT | WAIT | CTRLALTDEL | SHUTDOWN | RESTART)) {
Denis Vlasenko966bb432007-02-27 19:20:33 +0000402 /* Now fork off another process to just hang around */
Denis Vlasenko6bef3d12007-11-06 03:05:54 +0000403 pid = fork();
Denis Vlasenkof6ccc622007-11-10 01:57:35 +0000404 if (pid < 0) {
Denis Vlasenko966bb432007-02-27 19:20:33 +0000405 message(L_LOG | L_CONSOLE, "Can't fork");
Bernhard Reutner-Fischer636a1f82008-05-19 09:29:47 +0000406 _exit(EXIT_FAILURE);
Denis Vlasenko966bb432007-02-27 19:20:33 +0000407 }
408
409 if (pid > 0) {
Denis Vlasenko2afabe82007-12-10 07:06:04 +0000410 /* Parent - wait till the child is done */
Denis Vlasenko25591c32008-02-16 22:58:56 +0000411 bb_signals(0
412 + (1 << SIGINT)
413 + (1 << SIGTSTP)
414 + (1 << SIGQUIT)
415 , SIG_IGN);
Denis Vlasenko966bb432007-02-27 19:20:33 +0000416 signal(SIGCHLD, SIG_DFL);
417
Denis Vlasenko2afabe82007-12-10 07:06:04 +0000418 waitfor(pid);
Denis Vlasenko966bb432007-02-27 19:20:33 +0000419 /* See if stealing the controlling tty back is necessary */
420 if (tcgetpgrp(0) != getpid())
Bernhard Reutner-Fischer636a1f82008-05-19 09:29:47 +0000421 _exit(EXIT_SUCCESS);
Denis Vlasenko966bb432007-02-27 19:20:33 +0000422
423 /* Use a temporary process to steal the controlling tty. */
Denis Vlasenko6bef3d12007-11-06 03:05:54 +0000424 pid = fork();
425 if (pid < 0) {
Denis Vlasenko966bb432007-02-27 19:20:33 +0000426 message(L_LOG | L_CONSOLE, "Can't fork");
Bernhard Reutner-Fischer636a1f82008-05-19 09:29:47 +0000427 _exit(EXIT_FAILURE);
Eric Andersen0298be82002-03-05 15:12:19 +0000428 }
Denis Vlasenko966bb432007-02-27 19:20:33 +0000429 if (pid == 0) {
430 setsid();
431 ioctl(0, TIOCSCTTY, 1);
Bernhard Reutner-Fischer636a1f82008-05-19 09:29:47 +0000432 _exit(EXIT_SUCCESS);
Eric Andersen0298be82002-03-05 15:12:19 +0000433 }
Denis Vlasenko2afabe82007-12-10 07:06:04 +0000434 waitfor(pid);
Bernhard Reutner-Fischer636a1f82008-05-19 09:29:47 +0000435 _exit(EXIT_SUCCESS);
Eric Andersen0298be82002-03-05 15:12:19 +0000436 }
437
Denis Vlasenko2afabe82007-12-10 07:06:04 +0000438 /* Child - fall though to actually execute things */
Denis Vlasenko966bb432007-02-27 19:20:33 +0000439 }
Denis Vlasenko5adfa442007-12-25 16:08:53 +0000440#endif
Denis Vlasenko671ca332008-02-19 14:13:20 +0000441
Denis Vlasenkoad4da982008-04-05 04:24:23 +0000442 /* NB: on NOMMU we can't wait for input in child, so
443 * "askfirst" will work the same as "respawn". */
Denis Vlasenko671ca332008-02-19 14:13:20 +0000444 if (BB_MMU && (a->action_type & ASKFIRST)) {
Denis Vlasenko6ca409e2007-08-12 20:58:27 +0000445 static const char press_enter[] ALIGN1 =
Denis Vlasenko966bb432007-02-27 19:20:33 +0000446#ifdef CUSTOMIZED_BANNER
447#include CUSTOMIZED_BANNER
Eric Andersen1f50e842004-08-16 09:29:42 +0000448#endif
Denis Vlasenko966bb432007-02-27 19:20:33 +0000449 "\nPlease press Enter to activate this console. ";
450 char c;
451 /*
452 * Save memory by not exec-ing anything large (like a shell)
453 * before the user wants it. This is critical if swap is not
454 * enabled and the system has low memory. Generally this will
455 * be run on the second virtual console, and the first will
456 * be allowed to start a shell or whatever an init script
457 * specifies.
458 */
459 messageD(L_LOG, "waiting for enter to start '%s'"
460 "(pid %d, tty '%s')\n",
Denis Vlasenkoa37e7132008-02-19 02:57:07 +0000461 a->command, getpid(), a->terminal);
Bernhard Reutner-Fischer5e25ddb2008-05-19 09:48:17 +0000462 full_write(STDOUT_FILENO, press_enter, sizeof(press_enter) - 1);
463 while (safe_read(STDIN_FILENO, &c, 1) == 1 && c != '\n')
Denis Vlasenko2afabe82007-12-10 07:06:04 +0000464 continue;
Denis Vlasenko966bb432007-02-27 19:20:33 +0000465 }
Denis Vlasenko2afabe82007-12-10 07:06:04 +0000466
Mike Frysingerbb50fdf2007-12-25 04:30:14 +0000467 if (ENABLE_FEATURE_INIT_COREDUMPS) {
Denis Vlasenko966bb432007-02-27 19:20:33 +0000468 struct stat sb;
469 if (stat(CORE_ENABLE_FLAG_FILE, &sb) == 0) {
470 struct rlimit limit;
Denis Vlasenko966bb432007-02-27 19:20:33 +0000471 limit.rlim_cur = RLIM_INFINITY;
472 limit.rlim_max = RLIM_INFINITY;
473 setrlimit(RLIMIT_CORE, &limit);
Erik Andersen983b51b2000-04-04 18:14:25 +0000474 }
Erik Andersen05df2392000-01-13 04:43:48 +0000475 }
Mike Frysingerbb50fdf2007-12-25 04:30:14 +0000476
Denis Vlasenkoa37e7132008-02-19 02:57:07 +0000477 /* Log the process name and args */
478 message(L_LOG, "starting pid %d, tty '%s': '%s'",
479 getpid(), a->terminal, a->command);
480
Denis Vlasenko966bb432007-02-27 19:20:33 +0000481 /* Now run it. The new program will take over this PID,
482 * so nothing further in init.c should be run. */
Denis Vlasenkoa37e7132008-02-19 02:57:07 +0000483 init_exec(a->command);
Denis Vlasenko966bb432007-02-27 19:20:33 +0000484 /* We're still here? Some error happened. */
Denis Vlasenko966bb432007-02-27 19:20:33 +0000485 _exit(-1);
Eric Andersen0460ff21999-10-25 23:32:44 +0000486}
487
Eric Andersen0298be82002-03-05 15:12:19 +0000488/* Run all commands of a particular type */
Denis Vlasenkoa37e7132008-02-19 02:57:07 +0000489static void run_actions(int action_type)
Eric Andersen219d6f51999-11-02 19:43:01 +0000490{
Eric Andersen0298be82002-03-05 15:12:19 +0000491 struct init_action *a, *tmp;
Eric Andersen219d6f51999-11-02 19:43:01 +0000492
Eric Andersen0298be82002-03-05 15:12:19 +0000493 for (a = init_action_list; a; a = tmp) {
494 tmp = a->next;
Denis Vlasenkoad4da982008-04-05 04:24:23 +0000495 if (a->action_type & action_type) {
Denis Vlasenkod7e2e122007-12-10 08:40:29 +0000496 // Pointless: run() will error out if open of device fails.
497 ///* a->terminal of "" means "init's console" */
498 //if (a->terminal[0] && access(a->terminal, R_OK | W_OK)) {
499 // //message(L_LOG | L_CONSOLE, "Device %s cannot be opened in RW mode", a->terminal /*, strerror(errno)*/);
500 // delete_init_action(a);
501 //} else
Denis Vlasenkoa37e7132008-02-19 02:57:07 +0000502 if (a->action_type & (SYSINIT | WAIT | CTRLALTDEL | SHUTDOWN | RESTART)) {
Denis Vlasenko2afabe82007-12-10 07:06:04 +0000503 waitfor(run(a));
Eric Andersen0298be82002-03-05 15:12:19 +0000504 delete_init_action(a);
Denis Vlasenkoa37e7132008-02-19 02:57:07 +0000505 } else if (a->action_type & ONCE) {
Eric Andersen0298be82002-03-05 15:12:19 +0000506 run(a);
507 delete_init_action(a);
Denis Vlasenkoa37e7132008-02-19 02:57:07 +0000508 } else if (a->action_type & (RESPAWN | ASKFIRST)) {
Eric Andersen0298be82002-03-05 15:12:19 +0000509 /* Only run stuff with pid==0. If they have
510 * a pid, that means it is still running */
511 if (a->pid == 0) {
512 a->pid = run(a);
513 }
514 }
Erik Andersene132f4b2000-02-09 04:16:43 +0000515 }
516 }
517}
518
Eric Andersen2c1de612003-04-24 11:41:28 +0000519static void init_reboot(unsigned long magic)
520{
521 pid_t pid;
Bernhard Reutner-Fischer636a1f82008-05-19 09:29:47 +0000522 /* We have to fork here, since the kernel calls do_exit(EXIT_SUCCESS) in
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000523 * linux/kernel/sys.c, which can cause the machine to panic when
Eric Andersen2c1de612003-04-24 11:41:28 +0000524 * the init process is killed.... */
Denis Vlasenko9b1381f2007-01-03 02:56:00 +0000525 pid = vfork();
526 if (pid == 0) { /* child */
Eric Andersen2c1de612003-04-24 11:41:28 +0000527 reboot(magic);
Bernhard Reutner-Fischer636a1f82008-05-19 09:29:47 +0000528 _exit(EXIT_SUCCESS);
Eric Andersen2c1de612003-04-24 11:41:28 +0000529 }
Denis Vlasenkofb0eba72008-01-02 19:55:04 +0000530 waitfor(pid);
Eric Andersen2c1de612003-04-24 11:41:28 +0000531}
532
Denis Vlasenkod55268d2007-12-26 18:32:58 +0000533static void kill_all_processes(void)
Eric Andersencc8ed391999-10-05 16:24:54 +0000534{
Eric Andersena9cc8962002-09-16 06:49:06 +0000535 /* run everything to be run at "shutdown". This is done _prior_
536 * to killing everything, in case people wish to use scripts to
537 * shut things down gracefully... */
538 run_actions(SHUTDOWN);
539
Eric Andersen72f9a422001-10-28 05:12:20 +0000540 /* first disable all our signals */
Denis Vlasenko3f165fa2008-03-17 08:29:08 +0000541 sigprocmask_allsigs(SIG_BLOCK);
Erik Andersen7dc16072000-01-04 01:10:25 +0000542
Denis Vlasenko7a2ca5e2007-02-21 00:15:20 +0000543 message(L_CONSOLE | L_LOG, "The system is going down NOW!");
544
Erik Andersene49d5ec2000-02-08 19:58:47 +0000545 /* Allow Ctrl-Alt-Del to reboot system. */
Erik Andersen4f3f7572000-04-28 00:18:56 +0000546 init_reboot(RB_ENABLE_CAD);
Erik Andersene132f4b2000-02-09 04:16:43 +0000547
Erik Andersene49d5ec2000-02-08 19:58:47 +0000548 /* Send signals to every process _except_ pid 1 */
Denis Vlasenko7a2ca5e2007-02-21 00:15:20 +0000549 message(L_CONSOLE | L_LOG, "Sending SIG%s to all processes", "TERM");
Erik Andersene49d5ec2000-02-08 19:58:47 +0000550 kill(-1, SIGTERM);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000551 sync();
Denis Vlasenko7a2ca5e2007-02-21 00:15:20 +0000552 sleep(1);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000553
Denis Vlasenko7a2ca5e2007-02-21 00:15:20 +0000554 message(L_CONSOLE | L_LOG, "Sending SIG%s to all processes", "KILL");
Erik Andersene49d5ec2000-02-08 19:58:47 +0000555 kill(-1, SIGKILL);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000556 sync();
Denis Vlasenko7a2ca5e2007-02-21 00:15:20 +0000557 sleep(1);
Eric Andersencc8ed391999-10-05 16:24:54 +0000558}
559
Denis Vlasenkod55268d2007-12-26 18:32:58 +0000560static void halt_reboot_pwoff(int sig)
Denis Vlasenko2afabe82007-12-10 07:06:04 +0000561{
Bernhard Reutner-Fischer54d50a02008-07-17 14:00:42 +0000562 const char *m = "halt";
Denis Vlasenko2afabe82007-12-10 07:06:04 +0000563 int rb;
564
Denis Vlasenkod55268d2007-12-26 18:32:58 +0000565 kill_all_processes();
Denis Vlasenko2afabe82007-12-10 07:06:04 +0000566
Denis Vlasenko2afabe82007-12-10 07:06:04 +0000567 rb = RB_HALT_SYSTEM;
568 if (sig == SIGTERM) {
569 m = "reboot";
570 rb = RB_AUTOBOOT;
571 } else if (sig == SIGUSR2) {
572 m = "poweroff";
573 rb = RB_POWER_OFF;
574 }
575 message(L_CONSOLE | L_LOG, "Requesting system %s", m);
576 /* allow time for last message to reach serial console */
577 sleep(2);
578 init_reboot(rb);
579 loop_forever();
580}
581
Denis Vlasenkoa58a6372008-02-19 12:10:18 +0000582/* Handler for QUIT - exec "restart" action,
Denis Vlasenkod55268d2007-12-26 18:32:58 +0000583 * else (no such action defined) do nothing */
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000584static void exec_restart_action(int sig UNUSED_PARAM)
Eric Andersen730f8262001-12-17 23:13:08 +0000585{
Denis Vlasenkoa37e7132008-02-19 02:57:07 +0000586 struct init_action *a;
Glenn L McGrathbaf55a82002-08-22 18:22:10 +0000587
Denis Vlasenkoa37e7132008-02-19 02:57:07 +0000588 for (a = init_action_list; a; a = a->next) {
589 if (a->action_type & RESTART) {
Denis Vlasenkod55268d2007-12-26 18:32:58 +0000590 kill_all_processes();
Eric Andersen5222d312002-07-03 05:15:23 +0000591
Denis Vlasenkod55268d2007-12-26 18:32:58 +0000592 /* unblock all signals (blocked in kill_all_processes()) */
Denis Vlasenko3f165fa2008-03-17 08:29:08 +0000593 sigprocmask_allsigs(SIG_UNBLOCK);
Eric Andersen5222d312002-07-03 05:15:23 +0000594
Eric Andersen3c8064f2003-07-05 08:29:01 +0000595 /* Open the new terminal device */
Denis Vlasenkod55268d2007-12-26 18:32:58 +0000596 open_stdio_to_tty(a->terminal, 0 /* - halt if open fails */);
Eric Andersen3c8064f2003-07-05 08:29:01 +0000597
Denis Vlasenkoec27feb2007-02-17 15:52:02 +0000598 messageD(L_CONSOLE | L_LOG, "Trying to re-exec %s", a->command);
Denis Vlasenkoa37e7132008-02-19 02:57:07 +0000599 init_exec(a->command);
Eric Andersen730f8262001-12-17 23:13:08 +0000600 sleep(2);
601 init_reboot(RB_HALT_SYSTEM);
602 loop_forever();
603 }
604 }
605}
606
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000607static void ctrlaltdel_signal(int sig UNUSED_PARAM)
Eric Andersenc97ec342001-04-03 18:01:51 +0000608{
609 run_actions(CTRLALTDEL);
610}
611
Eric Andersen0298be82002-03-05 15:12:19 +0000612/* The SIGSTOP & SIGTSTP handler */
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000613static void stop_handler(int sig UNUSED_PARAM)
Eric Andersened8a9be2001-11-30 19:10:58 +0000614{
Eric Andersen0298be82002-03-05 15:12:19 +0000615 int saved_errno = errno;
Eric Andersened8a9be2001-11-30 19:10:58 +0000616
617 got_cont = 0;
Glenn L McGrathbaf55a82002-08-22 18:22:10 +0000618 while (!got_cont)
619 pause();
Denis Vlasenko2afabe82007-12-10 07:06:04 +0000620
Eric Andersened8a9be2001-11-30 19:10:58 +0000621 errno = saved_errno;
622}
623
Glenn L McGrathbaf55a82002-08-22 18:22:10 +0000624/* The SIGCONT handler */
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000625static void cont_handler(int sig UNUSED_PARAM)
Eric Andersened8a9be2001-11-30 19:10:58 +0000626{
627 got_cont = 1;
628}
629
Denis Vlasenkoa37e7132008-02-19 02:57:07 +0000630static void new_init_action(uint8_t action_type, const char *command, const char *cons)
Erik Andersen7dc16072000-01-04 01:10:25 +0000631{
Denis Vlasenkoa37e7132008-02-19 02:57:07 +0000632 struct init_action *a, *last;
Erik Andersen9e737252000-01-06 01:16:13 +0000633
Denis Vlasenkoa37e7132008-02-19 02:57:07 +0000634// Why?
635// if (strcmp(cons, bb_dev_null) == 0 && (action & ASKFIRST))
636// return;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000637
Eric Andersen0298be82002-03-05 15:12:19 +0000638 /* Append to the end of the list */
Eric Andersen22718092004-10-08 08:17:39 +0000639 for (a = last = init_action_list; a; a = a->next) {
Eric Andersen82baf632004-10-08 08:21:54 +0000640 /* don't enter action if it's already in the list,
641 * but do overwrite existing actions */
Denis Vlasenkoec27feb2007-02-17 15:52:02 +0000642 if ((strcmp(a->command, command) == 0)
643 && (strcmp(a->terminal, cons) == 0)
644 ) {
Denis Vlasenkoa37e7132008-02-19 02:57:07 +0000645 a->action_type = action_type;
Eric Andersen6fd0e312003-07-22 09:48:56 +0000646 return;
647 }
Eric Andersen22718092004-10-08 08:17:39 +0000648 last = a;
Eric Andersen6fd0e312003-07-22 09:48:56 +0000649 }
Denis Vlasenko7a2ca5e2007-02-21 00:15:20 +0000650
Denis Vlasenkoa37e7132008-02-19 02:57:07 +0000651 a = xzalloc(sizeof(*a));
Eric Andersen22718092004-10-08 08:17:39 +0000652 if (last) {
Denis Vlasenkoa37e7132008-02-19 02:57:07 +0000653 last->next = a;
Eric Andersen1644db92001-09-05 20:18:15 +0000654 } else {
Denis Vlasenkoa37e7132008-02-19 02:57:07 +0000655 init_action_list = a;
Eric Andersen1644db92001-09-05 20:18:15 +0000656 }
Denis Vlasenkoa37e7132008-02-19 02:57:07 +0000657 a->action_type = action_type;
658 safe_strncpy(a->command, command, sizeof(a->command));
659 safe_strncpy(a->terminal, cons, sizeof(a->terminal));
Denis Vlasenko7a2ca5e2007-02-21 00:15:20 +0000660 messageD(L_LOG | L_CONSOLE, "command='%s' action=%d tty='%s'\n",
Denis Vlasenkoa37e7132008-02-19 02:57:07 +0000661 a->command, a->action_type, a->terminal);
Erik Andersen7dc16072000-01-04 01:10:25 +0000662}
663
Glenn L McGrathbaf55a82002-08-22 18:22:10 +0000664static void delete_init_action(struct init_action *action)
Erik Andersen7dc16072000-01-04 01:10:25 +0000665{
Eric Andersen0298be82002-03-05 15:12:19 +0000666 struct init_action *a, *b = NULL;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000667
Eric Andersen0298be82002-03-05 15:12:19 +0000668 for (a = init_action_list; a; b = a, a = a->next) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000669 if (a == action) {
670 if (b == NULL) {
Eric Andersen0298be82002-03-05 15:12:19 +0000671 init_action_list = a->next;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000672 } else {
Eric Andersen0298be82002-03-05 15:12:19 +0000673 b->next = a->next;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000674 }
675 free(a);
676 break;
677 }
Erik Andersen7dc16072000-01-04 01:10:25 +0000678 }
Erik Andersen7dc16072000-01-04 01:10:25 +0000679}
680
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000681/* NOTE that if CONFIG_FEATURE_USE_INITTAB is NOT defined,
Erik Andersen9e737252000-01-06 01:16:13 +0000682 * then parse_inittab() simply adds in some default
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000683 * actions(i.e., runs INIT_SCRIPT and then starts a pair
684 * of "askfirst" shells). If CONFIG_FEATURE_USE_INITTAB
685 * _is_ defined, but /etc/inittab is missing, this
Erik Andersen812d4662000-01-07 18:30:40 +0000686 * results in the same set of default behaviors.
Glenn L McGrathbaf55a82002-08-22 18:22:10 +0000687 */
Eric Andersen3e6ff902001-03-09 21:24:12 +0000688static void parse_inittab(void)
Erik Andersen7dc16072000-01-04 01:10:25 +0000689{
Bernhard Reutner-Fischer54d50a02008-07-17 14:00:42 +0000690#if 0
Erik Andersene49d5ec2000-02-08 19:58:47 +0000691 FILE *file;
Denis Vlasenkoa37e7132008-02-19 02:57:07 +0000692 char buf[COMMAND_SIZE];
Erik Andersen7dc16072000-01-04 01:10:25 +0000693
Mike Frysingerbb50fdf2007-12-25 04:30:14 +0000694 if (ENABLE_FEATURE_USE_INITTAB)
695 file = fopen(INITTAB, "r");
696 else
697 file = NULL;
698
699 /* No inittab file -- set up some default behavior */
Erik Andersene49d5ec2000-02-08 19:58:47 +0000700 if (file == NULL) {
Eric Andersenc97ec342001-04-03 18:01:51 +0000701 /* Reboot on Ctrl-Alt-Del */
Denis Vlasenko2f0c0d02007-01-21 00:41:04 +0000702 new_init_action(CTRLALTDEL, "reboot", "");
Eric Andersen1644db92001-09-05 20:18:15 +0000703 /* Umount all filesystems on halt/reboot */
Denis Vlasenko2f0c0d02007-01-21 00:41:04 +0000704 new_init_action(SHUTDOWN, "umount -a -r", "");
Eric Andersen1644db92001-09-05 20:18:15 +0000705 /* Swapoff on halt/reboot */
Denis Vlasenko2afabe82007-12-10 07:06:04 +0000706 if (ENABLE_SWAPONOFF)
707 new_init_action(SHUTDOWN, "swapoff -a", "");
Denis Vlasenkoa58a6372008-02-19 12:10:18 +0000708 /* Prepare to restart init when a QUIT is received */
Denis Vlasenko2f0c0d02007-01-21 00:41:04 +0000709 new_init_action(RESTART, "init", "");
Eric Andersen3bc2b202002-07-29 06:39:58 +0000710 /* Askfirst shell on tty1-4 */
Glenn L McGrathdc4e75e2003-09-02 02:36:18 +0000711 new_init_action(ASKFIRST, bb_default_login_shell, "");
712 new_init_action(ASKFIRST, bb_default_login_shell, VC_2);
713 new_init_action(ASKFIRST, bb_default_login_shell, VC_3);
714 new_init_action(ASKFIRST, bb_default_login_shell, VC_4);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000715 /* sysinit */
Glenn L McGrathb4a1baa2003-01-13 22:09:50 +0000716 new_init_action(SYSINIT, INIT_SCRIPT, "");
Erik Andersen7dc16072000-01-04 01:10:25 +0000717
Erik Andersene49d5ec2000-02-08 19:58:47 +0000718 return;
Erik Andersen9e737252000-01-06 01:16:13 +0000719 }
Erik Andersen7dc16072000-01-04 01:10:25 +0000720
Denis Vlasenkoa37e7132008-02-19 02:57:07 +0000721 while (fgets(buf, COMMAND_SIZE, file) != NULL) {
Denis Vlasenkoec5631b2007-12-25 01:08:58 +0000722 static const char actions[] =
723 STR_SYSINIT "sysinit\0"
724 STR_RESPAWN "respawn\0"
725 STR_ASKFIRST "askfirst\0"
726 STR_WAIT "wait\0"
727 STR_ONCE "once\0"
728 STR_CTRLALTDEL "ctrlaltdel\0"
729 STR_SHUTDOWN "shutdown\0"
730 STR_RESTART "restart\0"
731 ;
732 char tmpConsole[CONSOLE_NAME_SIZE];
733 char *id, *runlev, *action, *command;
Denis Vlasenko2afabe82007-12-10 07:06:04 +0000734 const char *a;
735
Eric Andersenb5966362000-05-31 20:04:38 +0000736 /* Skip leading spaces */
Denis Vlasenkoec5631b2007-12-25 01:08:58 +0000737 id = skip_whitespace(buf);
Denis Vlasenkoec5631b2007-12-25 01:08:58 +0000738 /* Trim the trailing '\n' */
739 *strchrnul(id, '\n') = '\0';
Denis Vlasenkoc882f342008-01-29 09:56:21 +0000740 /* Skip the line if it is a comment */
741 if (*id == '#' || *id == '\0')
742 continue;
Erik Andersen7dc16072000-01-04 01:10:25 +0000743
Denis Vlasenkoec5631b2007-12-25 01:08:58 +0000744 /* Line is: "id:runlevel_ignored:action:command" */
Eric Andersenb5966362000-05-31 20:04:38 +0000745 runlev = strchr(id, ':');
Denis Vlasenkoec5631b2007-12-25 01:08:58 +0000746 if (runlev == NULL /*|| runlev[1] == '\0' - not needed */)
747 goto bad_entry;
748 action = strchr(runlev + 1, ':');
749 if (action == NULL /*|| action[1] == '\0' - not needed */)
750 goto bad_entry;
751 command = strchr(action + 1, ':');
752 if (command == NULL || command[1] == '\0')
753 goto bad_entry;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000754
Denis Vlasenkoec5631b2007-12-25 01:08:58 +0000755 *command = '\0'; /* action => ":action\0" now */
Denis Vlasenko2afabe82007-12-10 07:06:04 +0000756 for (a = actions; a[0]; a += strlen(a) + 1) {
Denis Vlasenkoec5631b2007-12-25 01:08:58 +0000757 if (strcmp(a + 1, action + 1) == 0) {
758 *runlev = '\0';
Eric Andersenb5966362000-05-31 20:04:38 +0000759 if (*id != '\0') {
Denis Vlasenko51742f42007-04-12 00:32:05 +0000760 if (strncmp(id, "/dev/", 5) == 0)
Glenn L McGrathb4a1baa2003-01-13 22:09:50 +0000761 id += 5;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000762 strcpy(tmpConsole, "/dev/");
Glenn L McGrathb4a1baa2003-01-13 22:09:50 +0000763 safe_strncpy(tmpConsole + 5, id,
Denis Vlasenko7a2ca5e2007-02-21 00:15:20 +0000764 sizeof(tmpConsole) - 5);
Eric Andersenb5966362000-05-31 20:04:38 +0000765 id = tmpConsole;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000766 }
Denis Vlasenkoec5631b2007-12-25 01:08:58 +0000767 new_init_action((uint8_t)a[0], command + 1, id);
768 goto next_line;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000769 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000770 }
Denis Vlasenkoec5631b2007-12-25 01:08:58 +0000771 *command = ':';
772 /* Choke on an unknown action */
773 bad_entry:
774 message(L_LOG | L_CONSOLE, "Bad inittab entry: %s", id);
775 next_line: ;
Erik Andersen7dc16072000-01-04 01:10:25 +0000776 }
Eric Andersend8636ca2002-05-15 22:19:09 +0000777 fclose(file);
Bernhard Reutner-Fischer54d50a02008-07-17 14:00:42 +0000778#else
779 char *token[4];
Denis Vlasenkoa474b682008-07-17 17:58:44 +0000780 /* order must correspond to SYSINIT..RESTART constants */
781 static const char actions[] ALIGN1 =
782 "sysinit\0""respawn\0""askfirst\0""wait\0""once\0"
783 "ctrlaltdel\0""shutdown\0""restart\0";
Bernhard Reutner-Fischer54d50a02008-07-17 14:00:42 +0000784
785 parser_t *parser = config_open(INITTAB);
786 /* No inittab file -- set up some default behavior */
787 if (parser == NULL) {
788 /* Reboot on Ctrl-Alt-Del */
789 new_init_action(CTRLALTDEL, "reboot", "");
790 /* Umount all filesystems on halt/reboot */
791 new_init_action(SHUTDOWN, "umount -a -r", "");
792 /* Swapoff on halt/reboot */
793 if (ENABLE_SWAPONOFF)
794 new_init_action(SHUTDOWN, "swapoff -a", "");
795 /* Prepare to restart init when a QUIT is received */
796 new_init_action(RESTART, "init", "");
797 /* Askfirst shell on tty1-4 */
798 new_init_action(ASKFIRST, bb_default_login_shell, "");
Denis Vlasenkoa474b682008-07-17 17:58:44 +0000799//TODO: VC_1 instead of ""? "" is console -> ctty problems -> angry users
Bernhard Reutner-Fischer54d50a02008-07-17 14:00:42 +0000800 new_init_action(ASKFIRST, bb_default_login_shell, VC_2);
801 new_init_action(ASKFIRST, bb_default_login_shell, VC_3);
802 new_init_action(ASKFIRST, bb_default_login_shell, VC_4);
803 /* sysinit */
804 new_init_action(SYSINIT, INIT_SCRIPT, "");
805
806 return;
807 }
808 /* optional_tty:ignored_runlevel:action:command
Denis Vlasenkoa474b682008-07-17 17:58:44 +0000809 * Delims are not to be collapsed and need exactly 4 tokens
Bernhard Reutner-Fischer54d50a02008-07-17 14:00:42 +0000810 */
Denis Vlasenko2e157dd2008-07-19 09:27:19 +0000811 while (config_read(parser, token, 4, 0, "#:", PARSE_DONT_TRIM|PARSE_DONT_REDUCE|PARSE_LAST_IS_GREEDY)) {
Denis Vlasenkoa474b682008-07-17 17:58:44 +0000812 int action;
Bernhard Reutner-Fischer54d50a02008-07-17 14:00:42 +0000813 char *tty = token[0];
Bernhard Reutner-Fischer54d50a02008-07-17 14:00:42 +0000814
Denis Vlasenkoa474b682008-07-17 17:58:44 +0000815 if (!token[3]) /* less than 4 tokens */
Bernhard Reutner-Fischer54d50a02008-07-17 14:00:42 +0000816 goto bad_entry;
Denis Vlasenkoa474b682008-07-17 17:58:44 +0000817 action = index_in_strings(actions, token[2]);
818 if (action < 0 || !token[3][0]) /* token[3]: command */
819 goto bad_entry;
820 /* turn .*TTY -> /dev/TTY */
821 if (tty[0]) {
822 if (strncmp(tty, "/dev/", 5) == 0)
Bernhard Reutner-Fischer54d50a02008-07-17 14:00:42 +0000823 tty += 5;
824 tty = concat_path_file("/dev/", tty);
Denis Vlasenkoa474b682008-07-17 17:58:44 +0000825 }
826 new_init_action(1 << action, token[3], tty);
827 if (tty[0])
Bernhard Reutner-Fischer54d50a02008-07-17 14:00:42 +0000828 free(tty);
829 continue;
830 bad_entry:
Denis Vlasenko2e157dd2008-07-19 09:27:19 +0000831 message(L_LOG | L_CONSOLE, "Bad inittab entry at line %d", parser->lineno);
Bernhard Reutner-Fischer54d50a02008-07-17 14:00:42 +0000832 }
833 config_close(parser);
834#endif
Erik Andersen7dc16072000-01-04 01:10:25 +0000835}
836
Denis Vlasenkoad4da982008-04-05 04:24:23 +0000837#if ENABLE_FEATURE_USE_INITTAB
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000838static void reload_signal(int sig UNUSED_PARAM)
Eric Andersen6fd0e312003-07-22 09:48:56 +0000839{
Eric Andersen82baf632004-10-08 08:21:54 +0000840 struct init_action *a, *tmp;
841
Denis Vlasenko7a2ca5e2007-02-21 00:15:20 +0000842 message(L_LOG, "reloading /etc/inittab");
Eric Andersen82baf632004-10-08 08:21:54 +0000843
844 /* disable old entrys */
Denis Vlasenkoec5631b2007-12-25 01:08:58 +0000845 for (a = init_action_list; a; a = a->next) {
Denis Vlasenkoa37e7132008-02-19 02:57:07 +0000846 a->action_type = ONCE;
Eric Andersen82baf632004-10-08 08:21:54 +0000847 }
848
849 parse_inittab();
850
Mike Frysingerbb50fdf2007-12-25 04:30:14 +0000851 if (ENABLE_FEATURE_KILL_REMOVED) {
Denis Vlasenkod55268d2007-12-26 18:32:58 +0000852 /* Be nice and send SIGTERM first */
Mike Frysingerbb50fdf2007-12-25 04:30:14 +0000853 for (a = init_action_list; a; a = a->next) {
854 pid_t pid = a->pid;
Denis Vlasenkoa37e7132008-02-19 02:57:07 +0000855 if ((a->action_type & ONCE) && pid != 0) {
Mike Frysingerbb50fdf2007-12-25 04:30:14 +0000856 kill(pid, SIGTERM);
Denis Vlasenkoec5631b2007-12-25 01:08:58 +0000857 }
Denis Vlasenkoec5631b2007-12-25 01:08:58 +0000858 }
Denis Vlasenkod55268d2007-12-26 18:32:58 +0000859#if CONFIG_FEATURE_KILL_DELAY
Denis Vlasenko671ca332008-02-19 14:13:20 +0000860 /* NB: parent will wait in NOMMU case */
861 if ((BB_MMU ? fork() : vfork()) == 0) { /* child */
Denis Vlasenkod55268d2007-12-26 18:32:58 +0000862 sleep(CONFIG_FEATURE_KILL_DELAY);
863 for (a = init_action_list; a; a = a->next) {
864 pid_t pid = a->pid;
Denis Vlasenkoa37e7132008-02-19 02:57:07 +0000865 if ((a->action_type & ONCE) && pid != 0) {
Denis Vlasenkod55268d2007-12-26 18:32:58 +0000866 kill(pid, SIGKILL);
867 }
868 }
Bernhard Reutner-Fischer636a1f82008-05-19 09:29:47 +0000869 _exit(EXIT_SUCCESS);
Denis Vlasenkod55268d2007-12-26 18:32:58 +0000870 }
871#endif
Denis Vlasenkoec5631b2007-12-25 01:08:58 +0000872 }
Denis Vlasenkoec5631b2007-12-25 01:08:58 +0000873
Eric Andersen82baf632004-10-08 08:21:54 +0000874 /* remove unused entrys */
875 for (a = init_action_list; a; a = tmp) {
876 tmp = a->next;
Denis Vlasenkoa37e7132008-02-19 02:57:07 +0000877 if ((a->action_type & (ONCE | SYSINIT | WAIT)) && a->pid == 0) {
Eric Andersen82baf632004-10-08 08:21:54 +0000878 delete_init_action(a);
879 }
880 }
Denis Vlasenkoad4da982008-04-05 04:24:23 +0000881 run_actions(RESPAWN | ASKFIRST);
Eric Andersen6fd0e312003-07-22 09:48:56 +0000882}
Denis Vlasenkoad4da982008-04-05 04:24:23 +0000883#endif
Eric Andersen82baf632004-10-08 08:21:54 +0000884
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +0000885int init_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000886int init_main(int argc UNUSED_PARAM, char **argv)
Eric Andersen0460ff21999-10-25 23:32:44 +0000887{
Eric Andersen0298be82002-03-05 15:12:19 +0000888 struct init_action *a;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000889 pid_t wpid;
Eric Andersen0460ff21999-10-25 23:32:44 +0000890
Denis Vlasenko9b1381f2007-01-03 02:56:00 +0000891 die_sleep = 30 * 24*60*60; /* if xmalloc will ever die... */
892
Denis Vlasenko1d426652008-03-17 09:09:09 +0000893 if (argv[1] && !strcmp(argv[1], "-q")) {
Denis Vlasenko7a2ca5e2007-02-21 00:15:20 +0000894 return kill(1, SIGHUP);
Eric Andersen730f8262001-12-17 23:13:08 +0000895 }
Mike Frysingerbb50fdf2007-12-25 04:30:14 +0000896
897 if (!ENABLE_DEBUG_INIT) {
898 /* Expect to be invoked as init with PID=1 or be invoked as linuxrc */
899 if (getpid() != 1
900 && (!ENABLE_FEATURE_INITRD || !strstr(applet_name, "linuxrc"))
901 ) {
902 bb_show_usage();
903 }
904 /* Set up sig handlers -- be sure to
905 * clear all of these in run() */
Denis Vlasenko99a61842008-02-19 12:08:38 +0000906 signal(SIGQUIT, exec_restart_action);
Denis Vlasenko25591c32008-02-16 22:58:56 +0000907 bb_signals(0
908 + (1 << SIGUSR1) /* halt */
909 + (1 << SIGUSR2) /* poweroff */
910 + (1 << SIGTERM) /* reboot */
911 , halt_reboot_pwoff);
Mike Frysingerbb50fdf2007-12-25 04:30:14 +0000912 signal(SIGINT, ctrlaltdel_signal);
Mike Frysingerbb50fdf2007-12-25 04:30:14 +0000913 signal(SIGCONT, cont_handler);
Denis Vlasenko25591c32008-02-16 22:58:56 +0000914 bb_signals(0
915 + (1 << SIGSTOP)
916 + (1 << SIGTSTP)
917 , stop_handler);
Mike Frysingerbb50fdf2007-12-25 04:30:14 +0000918
919 /* Turn off rebooting via CTL-ALT-DEL -- we get a
920 * SIGINT on CAD so we can shut things down gracefully... */
921 init_reboot(RB_DISABLE_CAD);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000922 }
Denis Vlasenkod8540f72007-06-14 07:53:06 +0000923
Eric Andersen599e3ce2002-07-03 11:08:10 +0000924 /* Figure out where the default console should be */
925 console_init();
Denis Vlasenkod238a472007-03-05 19:22:04 +0000926 set_sane_term();
Bernhard Reutner-Fischer54d50a02008-07-17 14:00:42 +0000927 xchdir("/");
Erik Andersene49d5ec2000-02-08 19:58:47 +0000928 setsid();
Glenn L McGrathb4a1baa2003-01-13 22:09:50 +0000929 {
Denis Vlasenko6ca409e2007-08-12 20:58:27 +0000930 const char *const *e;
Glenn L McGrathb4a1baa2003-01-13 22:09:50 +0000931 /* Make sure environs is set to something sane */
Denis Vlasenkobf0a2012006-12-26 10:42:51 +0000932 for (e = environment; *e; e++)
Manuel Novoa III cad53642003-03-19 09:13:01 +0000933 putenv((char *) *e);
Glenn L McGrathb4a1baa2003-01-13 22:09:50 +0000934 }
Rob Landleycba1b962006-07-09 17:28:17 +0000935
Bernhard Reutner-Fischer54d50a02008-07-17 14:00:42 +0000936 if (argv[1])
937 setenv("RUNLEVEL", argv[1], 1);
Rob Landleycba1b962006-07-09 17:28:17 +0000938
Erik Andersene49d5ec2000-02-08 19:58:47 +0000939 /* Hello world */
Denis Vlasenkoca525b42007-06-13 12:27:17 +0000940 message(MAYBE_CONSOLE | L_LOG, "init started: %s", bb_banner);
Eric Andersencc8ed391999-10-05 16:24:54 +0000941
Erik Andersene49d5ec2000-02-08 19:58:47 +0000942 /* Make sure there is enough memory to do something useful. */
Rob Landleyc3386a42005-08-30 18:50:37 +0000943 if (ENABLE_SWAPONOFF) {
944 struct sysinfo info;
945
946 if (!sysinfo(&info) &&
Denis Vlasenkodca0b702006-10-27 09:05:02 +0000947 (info.mem_unit ? : 1) * (long long)info.totalram < 1024*1024)
Rob Landleyc3386a42005-08-30 18:50:37 +0000948 {
Denis Vlasenko7a2ca5e2007-02-21 00:15:20 +0000949 message(L_CONSOLE, "Low memory, forcing swapon");
Rob Landleyc3386a42005-08-30 18:50:37 +0000950 /* swapon -a requires /proc typically */
Denis Vlasenko2f0c0d02007-01-21 00:41:04 +0000951 new_init_action(SYSINIT, "mount -t proc proc /proc", "");
Rob Landleyc3386a42005-08-30 18:50:37 +0000952 /* Try to turn on swap */
Denis Vlasenko2f0c0d02007-01-21 00:41:04 +0000953 new_init_action(SYSINIT, "swapon -a", "");
Rob Landleyc3386a42005-08-30 18:50:37 +0000954 run_actions(SYSINIT); /* wait and removing */
955 }
956 }
Erik Andersen9e737252000-01-06 01:16:13 +0000957
Erik Andersene49d5ec2000-02-08 19:58:47 +0000958 /* Check if we are supposed to be in single user mode */
Denis Vlasenko1d426652008-03-17 09:09:09 +0000959 if (argv[1]
Denis Vlasenkobf66fbc2006-12-21 13:23:14 +0000960 && (!strcmp(argv[1], "single") || !strcmp(argv[1], "-s") || LONE_CHAR(argv[1], '1'))
961 ) {
Bernhard Reutner-Fischer54d50a02008-07-17 14:00:42 +0000962 /* ??? shouldn't we set RUNLEVEL="b" here? */
Glenn L McGrathb4a1baa2003-01-13 22:09:50 +0000963 /* Start a shell on console */
Glenn L McGrathdc4e75e2003-09-02 02:36:18 +0000964 new_init_action(RESPAWN, bb_default_login_shell, "");
Erik Andersene49d5ec2000-02-08 19:58:47 +0000965 } else {
966 /* Not in single user mode -- see what inittab says */
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000967
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000968 /* NOTE that if CONFIG_FEATURE_USE_INITTAB is NOT defined,
Erik Andersene49d5ec2000-02-08 19:58:47 +0000969 * then parse_inittab() simply adds in some default
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000970 * actions(i.e., runs INIT_SCRIPT and then starts a pair
Erik Andersene49d5ec2000-02-08 19:58:47 +0000971 * of "askfirst" shells */
972 parse_inittab();
Eric Andersend00c2621999-12-07 08:37:31 +0000973 }
Erik Andersen983b51b2000-04-04 18:14:25 +0000974
Denis Vlasenko9b1381f2007-01-03 02:56:00 +0000975#if ENABLE_SELINUX
Rob Landleyb3ede5a2006-03-27 23:09:12 +0000976 if (getenv("SELINUX_INIT") == NULL) {
977 int enforce = 0;
978
Denis Vlasenkod8540f72007-06-14 07:53:06 +0000979 putenv((char*)"SELINUX_INIT=YES");
Rob Landleyb3ede5a2006-03-27 23:09:12 +0000980 if (selinux_init_load_policy(&enforce) == 0) {
Denis Vlasenko4921b542007-02-03 02:17:41 +0000981 BB_EXECVP(argv[0], argv);
Rob Landleyb3ede5a2006-03-27 23:09:12 +0000982 } else if (enforce > 0) {
983 /* SELinux in enforcing mode but load_policy failed */
Denis Vlasenkob7167542007-02-27 19:20:00 +0000984 message(L_CONSOLE, "Cannot load SELinux Policy. "
985 "Machine is in enforcing mode. Halting now.");
Bernhard Reutner-Fischer636a1f82008-05-19 09:29:47 +0000986 exit(EXIT_FAILURE);
Rob Landleyb3ede5a2006-03-27 23:09:12 +0000987 }
988 }
989#endif /* CONFIG_SELINUX */
990
Denis Vlasenko2afabe82007-12-10 07:06:04 +0000991 /* Make the command line just say "init" - thats all, nothing else */
992 strncpy(argv[0], "init", strlen(argv[0]));
993 /* Wipe argv[1]-argv[N] so they don't clutter the ps listing */
994 while (*++argv)
995 memset(*argv, 0, strlen(*argv));
Eric Andersen219d6f51999-11-02 19:43:01 +0000996
Erik Andersene49d5ec2000-02-08 19:58:47 +0000997 /* Now run everything that needs to be run */
998
999 /* First run the sysinit command */
Eric Andersen1644db92001-09-05 20:18:15 +00001000 run_actions(SYSINIT);
Eric Andersen0298be82002-03-05 15:12:19 +00001001
Erik Andersene49d5ec2000-02-08 19:58:47 +00001002 /* Next run anything that wants to block */
Eric Andersen1644db92001-09-05 20:18:15 +00001003 run_actions(WAIT);
Eric Andersen0298be82002-03-05 15:12:19 +00001004
Erik Andersene49d5ec2000-02-08 19:58:47 +00001005 /* Next run anything to be run only once */
Eric Andersen0298be82002-03-05 15:12:19 +00001006 run_actions(ONCE);
1007
Eric Andersen6fd0e312003-07-22 09:48:56 +00001008 /* Redefine SIGHUP to reread /etc/inittab */
Denis Vlasenkoad4da982008-04-05 04:24:23 +00001009#if ENABLE_FEATURE_USE_INITTAB
1010 signal(SIGHUP, reload_signal);
1011#else
1012 signal(SIGHUP, SIG_IGN);
1013#endif
Eric Andersen82baf632004-10-08 08:21:54 +00001014
Erik Andersene49d5ec2000-02-08 19:58:47 +00001015 /* Now run the looping stuff for the rest of forever */
1016 while (1) {
Denis Vlasenkoad4da982008-04-05 04:24:23 +00001017 /* run the respawn/askfirst stuff */
1018 run_actions(RESPAWN | ASKFIRST);
Eric Andersen0298be82002-03-05 15:12:19 +00001019
1020 /* Don't consume all CPU time -- sleep a bit */
1021 sleep(1);
1022
Denis Vlasenkofb0eba72008-01-02 19:55:04 +00001023 /* Wait for any child process to exit */
Bernhard Reutner-Fischerc58dbf22006-05-30 12:16:54 +00001024 wpid = wait(NULL);
Eric Andersen87812dc2004-04-12 19:21:54 +00001025 while (wpid > 0) {
Erik Andersene49d5ec2000-02-08 19:58:47 +00001026 /* Find out who died and clean up their corpse */
Eric Andersen0298be82002-03-05 15:12:19 +00001027 for (a = init_action_list; a; a = a->next) {
Erik Andersene49d5ec2000-02-08 19:58:47 +00001028 if (a->pid == wpid) {
Eric Andersen0298be82002-03-05 15:12:19 +00001029 /* Set the pid to 0 so that the process gets
1030 * restarted by run_actions() */
Erik Andersene49d5ec2000-02-08 19:58:47 +00001031 a->pid = 0;
Denis Vlasenko7a2ca5e2007-02-21 00:15:20 +00001032 message(L_LOG, "process '%s' (pid %d) exited. "
Denis Vlasenkoa37e7132008-02-19 02:57:07 +00001033 "Scheduling for restart.",
Glenn L McGrathb4a1baa2003-01-13 22:09:50 +00001034 a->command, wpid);
Erik Andersene49d5ec2000-02-08 19:58:47 +00001035 }
1036 }
Eric Andersencf1fee02002-12-17 09:48:16 +00001037 /* see if anyone else is waiting to be reaped */
Denis Vlasenkofb0eba72008-01-02 19:55:04 +00001038 wpid = wait_any_nohang(NULL);
Erik Andersene49d5ec2000-02-08 19:58:47 +00001039 }
Erik Andersene49d5ec2000-02-08 19:58:47 +00001040 }
Eric Andersencc8ed391999-10-05 16:24:54 +00001041}