blob: 9891087f54e25a9d9cd70079c00543adde0093ec [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"
Eric Andersenb186d981999-12-03 09:19:54 +000013#include <paths.h>
Denis Vlasenko9a7d38f2007-05-31 22:42:12 +000014//#include <signal.h>
15//#include <sys/ioctl.h>
16//#include <sys/wait.h>
Eric Andersen85e5e722003-07-22 08:56:55 +000017#include <sys/reboot.h>
Eric Andersenb01ed652003-06-27 17:08:15 +000018
Denis Vlasenkoccd412d2007-03-05 19:55:30 +000019#if ENABLE_FEATURE_INIT_SYSLOG
Erik Andersen4f3f7572000-04-28 00:18:56 +000020# include <sys/syslog.h>
21#endif
Eric Andersen2c1de612003-04-24 11:41:28 +000022
Eric Andersen0826b6b2002-07-03 23:50:16 +000023#define INIT_BUFFS_SIZE 256
Denis Vlasenko7a2ca5e2007-02-21 00:15:20 +000024#define CONSOLE_NAME_SIZE 32
25#define MAXENV 16 /* Number of env. vars */
Eric Andersenbd22ed82000-07-08 18:55:24 +000026
Denis Vlasenko9b1381f2007-01-03 02:56:00 +000027#if ENABLE_FEATURE_INIT_COREDUMPS
Erik Andersen983b51b2000-04-04 18:14:25 +000028/*
Eric Andersenc7bda1c2004-03-15 08:29:22 +000029 * When a file named CORE_ENABLE_FLAG_FILE exists, setrlimit is called
Erik Andersen183da4a2000-04-04 18:36:37 +000030 * before processes are spawned to set core file size as unlimited.
31 * This is for debugging only. Don't use this is production, unless
32 * you want core dumps lying about....
Erik Andersen983b51b2000-04-04 18:14:25 +000033 */
34#define CORE_ENABLE_FLAG_FILE "/.init_enable_core"
35#include <sys/resource.h>
Erik Andersen183da4a2000-04-04 18:36:37 +000036#endif
Erik Andersen983b51b2000-04-04 18:14:25 +000037
Glenn L McGrathbaf55a82002-08-22 18:22:10 +000038#define INITTAB "/etc/inittab" /* inittab file location */
Erik Andersen96e2abd2000-01-07 11:40:44 +000039#ifndef INIT_SCRIPT
Glenn L McGrathbaf55a82002-08-22 18:22:10 +000040#define INIT_SCRIPT "/etc/init.d/rcS" /* Default sysinit script. */
Erik Andersen96e2abd2000-01-07 11:40:44 +000041#endif
Erik Andersen9c88cac1999-12-30 09:25:17 +000042
Erik Andersen7dc16072000-01-04 01:10:25 +000043/* Allowed init action types */
Eric Andersen0298be82002-03-05 15:12:19 +000044#define SYSINIT 0x001
45#define RESPAWN 0x002
46#define ASKFIRST 0x004
47#define WAIT 0x008
48#define ONCE 0x010
49#define CTRLALTDEL 0x020
50#define SHUTDOWN 0x040
51#define RESTART 0x080
Erik Andersen7dc16072000-01-04 01:10:25 +000052
Erik Andersen42094cd2000-03-20 21:34:52 +000053/* A mapping between "inittab" action name strings and action type codes. */
Eric Andersen0298be82002-03-05 15:12:19 +000054struct init_action_type {
Erik Andersene49d5ec2000-02-08 19:58:47 +000055 const char *name;
Eric Andersen0298be82002-03-05 15:12:19 +000056 int action;
57};
Erik Andersen7dc16072000-01-04 01:10:25 +000058
Eric Andersen0298be82002-03-05 15:12:19 +000059static const struct init_action_type actions[] = {
Erik Andersene49d5ec2000-02-08 19:58:47 +000060 {"sysinit", SYSINIT},
61 {"respawn", RESPAWN},
62 {"askfirst", ASKFIRST},
63 {"wait", WAIT},
64 {"once", ONCE},
Erik Andersene132f4b2000-02-09 04:16:43 +000065 {"ctrlaltdel", CTRLALTDEL},
Eric Andersenc97ec342001-04-03 18:01:51 +000066 {"shutdown", SHUTDOWN},
Eric Andersen730f8262001-12-17 23:13:08 +000067 {"restart", RESTART},
Pavel Roskin9027bcf2000-07-14 15:44:25 +000068 {0, 0}
Erik Andersen7dc16072000-01-04 01:10:25 +000069};
70
Eric Andersen0298be82002-03-05 15:12:19 +000071/* Set up a linked list of init_actions, to be read from inittab */
72struct init_action {
Eric Andersen0298be82002-03-05 15:12:19 +000073 struct init_action *next;
74 int action;
Denis Vlasenko7a2ca5e2007-02-21 00:15:20 +000075 pid_t pid;
76 char command[INIT_BUFFS_SIZE];
77 char terminal[CONSOLE_NAME_SIZE];
Erik Andersen7dc16072000-01-04 01:10:25 +000078};
Erik Andersen7dc16072000-01-04 01:10:25 +000079
Eric Andersen0298be82002-03-05 15:12:19 +000080/* Static variables */
81static struct init_action *init_action_list = NULL;
Glenn L McGrathbaf55a82002-08-22 18:22:10 +000082
Denis Vlasenkoccd412d2007-03-05 19:55:30 +000083#if !ENABLE_FEATURE_INIT_SYSLOG
Denis Vlasenko4c978632007-02-03 03:31:13 +000084static const char *log_console = VC_5;
Eric Andersene7078062002-07-29 08:00:16 +000085#endif
Bernhard Reutner-Fischercf1f2ac2006-06-02 10:43:17 +000086#if !ENABLE_DEBUG_INIT
Eric Andersen0298be82002-03-05 15:12:19 +000087static sig_atomic_t got_cont = 0;
Bernhard Reutner-Fischercf1f2ac2006-06-02 10:43:17 +000088#endif
Rob Landleybc68cd12006-03-10 19:22:06 +000089
90enum {
Denis Vlasenkoec27feb2007-02-17 15:52:02 +000091 L_LOG = 0x1,
92 L_CONSOLE = 0x2,
Glenn L McGrathbaf55a82002-08-22 18:22:10 +000093
Denis Vlasenko9b1381f2007-01-03 02:56:00 +000094#if ENABLE_FEATURE_EXTRA_QUIET
Rob Landleybc68cd12006-03-10 19:22:06 +000095 MAYBE_CONSOLE = 0x0,
Eric Andersen0298be82002-03-05 15:12:19 +000096#else
Denis Vlasenkoec27feb2007-02-17 15:52:02 +000097 MAYBE_CONSOLE = L_CONSOLE,
Eric Andersen0298be82002-03-05 15:12:19 +000098#endif
Glenn L McGrathbaf55a82002-08-22 18:22:10 +000099
Rob Landleybc68cd12006-03-10 19:22:06 +0000100#ifndef RB_HALT_SYSTEM
Bernhard Reutner-Fischercf1f2ac2006-06-02 10:43:17 +0000101 RB_HALT_SYSTEM = 0xcdef0123, /* FIXME: this overflows enum */
Rob Landleybc68cd12006-03-10 19:22:06 +0000102 RB_ENABLE_CAD = 0x89abcdef,
103 RB_DISABLE_CAD = 0,
104 RB_POWER_OFF = 0x4321fedc,
105 RB_AUTOBOOT = 0x01234567,
Eric Andersen0298be82002-03-05 15:12:19 +0000106#endif
Rob Landleybc68cd12006-03-10 19:22:06 +0000107};
Erik Andersen42094cd2000-03-20 21:34:52 +0000108
Denis Vlasenko6ca409e2007-08-12 20:58:27 +0000109static const char *const environment[] = {
Glenn L McGrathb4a1baa2003-01-13 22:09:50 +0000110 "HOME=/",
Denis Vlasenkof5f75c52007-06-12 22:35:19 +0000111 bb_PATH_root_path,
Glenn L McGrathdc4e75e2003-09-02 02:36:18 +0000112 "SHELL=/bin/sh",
Glenn L McGrathb4a1baa2003-01-13 22:09:50 +0000113 "USER=root",
114 NULL
115};
116
Eric Andersen0298be82002-03-05 15:12:19 +0000117/* Function prototypes */
118static void delete_init_action(struct init_action *a);
Bernhard Reutner-Fischer3ab30802006-05-30 12:10:29 +0000119static int waitfor(const struct init_action *a, pid_t pid);
Bernhard Reutner-Fischercf1f2ac2006-06-02 10:43:17 +0000120#if !ENABLE_DEBUG_INIT
Paul Foxd112f8f2006-06-01 13:17:49 +0000121static void shutdown_signal(int sig);
Bernhard Reutner-Fischercf1f2ac2006-06-02 10:43:17 +0000122#endif
Eric Andersen0460ff21999-10-25 23:32:44 +0000123
Denis Vlasenko9b1381f2007-01-03 02:56:00 +0000124#if !ENABLE_DEBUG_INIT
Eric Andersen74400cc2001-10-18 04:11:39 +0000125static void loop_forever(void)
Matt Kraai67a46402001-06-03 05:55:52 +0000126{
127 while (1)
Glenn L McGrathbaf55a82002-08-22 18:22:10 +0000128 sleep(1);
Matt Kraai67a46402001-06-03 05:55:52 +0000129}
Denis Vlasenko9b1381f2007-01-03 02:56:00 +0000130#endif
Eric Andersen7ef1a5b2001-03-20 17:39:08 +0000131
Erik Andersen42094cd2000-03-20 21:34:52 +0000132/* Print a message to the specified device.
Denis Vlasenkoec27feb2007-02-17 15:52:02 +0000133 * Device may be bitwise-or'd from L_LOG | L_CONSOLE */
Bernhard Reutner-Fischer35e1a072006-05-29 13:08:35 +0000134#if ENABLE_DEBUG_INIT
135#define messageD message
136#else
Denis Vlasenkobf0a2012006-12-26 10:42:51 +0000137#define messageD(...) do {} while (0)
Eric Andersenfedce062001-11-17 07:27:14 +0000138#endif
Glenn L McGrathb4a1baa2003-01-13 22:09:50 +0000139static void message(int device, const char *fmt, ...)
Glenn L McGrathbaf55a82002-08-22 18:22:10 +0000140 __attribute__ ((format(printf, 2, 3)));
Glenn L McGrathb4a1baa2003-01-13 22:09:50 +0000141static void message(int device, const char *fmt, ...)
Eric Andersen0460ff21999-10-25 23:32:44 +0000142{
Denis Vlasenkoccd412d2007-03-05 19:55:30 +0000143#if !ENABLE_FEATURE_INIT_SYSLOG
Eric Andersen22237012003-01-23 07:08:26 +0000144 static int log_fd = -1;
145#endif
Eric Andersen4c781471999-11-26 08:12:56 +0000146
Denis Vlasenko7a2ca5e2007-02-21 00:15:20 +0000147 va_list arguments;
148 int l;
149 char msg[128];
150
Glenn L McGrathb4a1baa2003-01-13 22:09:50 +0000151 msg[0] = '\r';
Denis Vlasenko7a2ca5e2007-02-21 00:15:20 +0000152 va_start(arguments, fmt);
153 vsnprintf(msg + 1, sizeof(msg) - 2, fmt, arguments);
Denis Vlasenkoec27feb2007-02-17 15:52:02 +0000154 va_end(arguments);
Denis Vlasenko7a2ca5e2007-02-21 00:15:20 +0000155 msg[sizeof(msg) - 2] = '\0';
156 l = strlen(msg);
Glenn L McGrathb4a1baa2003-01-13 22:09:50 +0000157
Denis Vlasenkoccd412d2007-03-05 19:55:30 +0000158#if ENABLE_FEATURE_INIT_SYSLOG
Glenn L McGrathb4a1baa2003-01-13 22:09:50 +0000159 /* Log the message to syslogd */
Denis Vlasenkoec27feb2007-02-17 15:52:02 +0000160 if (device & L_LOG) {
Denis Vlasenko7a2ca5e2007-02-21 00:15:20 +0000161 /* don't out "\r" */
Denis Vlasenko8f8f2682006-10-03 21:00:43 +0000162 openlog(applet_name, 0, LOG_DAEMON);
Denis Vlasenko7a2ca5e2007-02-21 00:15:20 +0000163 syslog(LOG_INFO, "init: %s", msg + 1);
Eric Andersen36adca82004-06-22 10:07:17 +0000164 closelog();
Eric Andersen3ae0c781999-11-04 01:13:21 +0000165 }
Glenn L McGrathb4a1baa2003-01-13 22:09:50 +0000166 msg[l++] = '\n';
Denis Vlasenko7a2ca5e2007-02-21 00:15:20 +0000167 msg[l] = '\0';
Erik Andersene49d5ec2000-02-08 19:58:47 +0000168#else
Glenn L McGrathb4a1baa2003-01-13 22:09:50 +0000169 msg[l++] = '\n';
Denis Vlasenko7a2ca5e2007-02-21 00:15:20 +0000170 msg[l] = '\0';
Erik Andersene49d5ec2000-02-08 19:58:47 +0000171 /* Take full control of the log tty, and never close it.
172 * It's mine, all mine! Muhahahaha! */
173 if (log_fd < 0) {
Denis Vlasenko7a2ca5e2007-02-21 00:15:20 +0000174 if (!log_console) {
175 log_fd = 2;
Eric Andersen6a979902002-09-30 20:08:53 +0000176 } else {
Denis Vlasenko7a2ca5e2007-02-21 00:15:20 +0000177 log_fd = device_open(log_console, O_WRONLY | O_NONBLOCK | O_NOCTTY);
178 if (log_fd < 0) {
179 bb_error_msg("can't log to %s", log_console);
180 device = L_CONSOLE;
181 } else {
Denis Vlasenko96e1b382007-09-30 23:50:48 +0000182 close_on_exec_on(log_fd);
Denis Vlasenko7a2ca5e2007-02-21 00:15:20 +0000183 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000184 }
185 }
Denis Vlasenkoec27feb2007-02-17 15:52:02 +0000186 if (device & L_LOG) {
Rob Landley53437472006-07-16 08:14:35 +0000187 full_write(log_fd, msg, l);
Denis Vlasenko7a2ca5e2007-02-21 00:15:20 +0000188 if (log_fd == 2)
189 return; /* don't print dup messages */
Erik Andersene49d5ec2000-02-08 19:58:47 +0000190 }
Eric Andersen4c781471999-11-26 08:12:56 +0000191#endif
192
Denis Vlasenkoec27feb2007-02-17 15:52:02 +0000193 if (device & L_CONSOLE) {
Denis Vlasenko7a2ca5e2007-02-21 00:15:20 +0000194 /* Send console messages to console so people will see them. */
195 full_write(2, msg, l);
Eric Andersen3ae0c781999-11-04 01:13:21 +0000196 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000197}
198
Eric Andersen0460ff21999-10-25 23:32:44 +0000199/* Set terminal settings to reasonable defaults */
Denis Vlasenkod238a472007-03-05 19:22:04 +0000200static void set_sane_term(void)
Eric Andersencc8ed391999-10-05 16:24:54 +0000201{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000202 struct termios tty;
Eric Andersencc8ed391999-10-05 16:24:54 +0000203
Bernhard Reutner-Fischer49e60b92006-05-29 12:57:52 +0000204 tcgetattr(STDIN_FILENO, &tty);
Eric Andersencc8ed391999-10-05 16:24:54 +0000205
Erik Andersene49d5ec2000-02-08 19:58:47 +0000206 /* set control chars */
Glenn L McGrathbaf55a82002-08-22 18:22:10 +0000207 tty.c_cc[VINTR] = 3; /* C-c */
208 tty.c_cc[VQUIT] = 28; /* C-\ */
209 tty.c_cc[VERASE] = 127; /* C-? */
210 tty.c_cc[VKILL] = 21; /* C-u */
211 tty.c_cc[VEOF] = 4; /* C-d */
Eric Andersen3849f9b2000-07-10 19:56:47 +0000212 tty.c_cc[VSTART] = 17; /* C-q */
Glenn L McGrathbaf55a82002-08-22 18:22:10 +0000213 tty.c_cc[VSTOP] = 19; /* C-s */
214 tty.c_cc[VSUSP] = 26; /* C-z */
Eric Andersen02bc25b2000-07-06 21:29:32 +0000215
Erik Andersene49d5ec2000-02-08 19:58:47 +0000216 /* use line dicipline 0 */
217 tty.c_line = 0;
Eric Andersen7f1acfd1999-10-29 23:09:13 +0000218
Erik Andersene49d5ec2000-02-08 19:58:47 +0000219 /* Make it be sane */
Glenn L McGrathbaf55a82002-08-22 18:22:10 +0000220 tty.c_cflag &= CBAUD | CBAUDEX | CSIZE | CSTOPB | PARENB | PARODD;
221 tty.c_cflag |= CREAD | HUPCL | CLOCAL;
Eric Andersend8862922001-04-23 15:14:11 +0000222
Erik Andersene49d5ec2000-02-08 19:58:47 +0000223 /* input modes */
224 tty.c_iflag = ICRNL | IXON | IXOFF;
Eric Andersen08b10341999-11-19 02:38:58 +0000225
Erik Andersene49d5ec2000-02-08 19:58:47 +0000226 /* output modes */
227 tty.c_oflag = OPOST | ONLCR;
Eric Andersen7f1acfd1999-10-29 23:09:13 +0000228
Erik Andersene49d5ec2000-02-08 19:58:47 +0000229 /* local modes */
230 tty.c_lflag =
231 ISIG | ICANON | ECHO | ECHOE | ECHOK | ECHOCTL | ECHOKE | IEXTEN;
Eric Andersencc8ed391999-10-05 16:24:54 +0000232
Bernhard Reutner-Fischer49e60b92006-05-29 12:57:52 +0000233 tcsetattr(STDIN_FILENO, TCSANOW, &tty);
Eric Andersencc8ed391999-10-05 16:24:54 +0000234}
235
Denis Vlasenko7a2ca5e2007-02-21 00:15:20 +0000236/* From <linux/serial.h> */
237struct serial_struct {
238 int type;
239 int line;
240 unsigned int port;
241 int irq;
242 int flags;
243 int xmit_fifo_size;
244 int custom_divisor;
245 int baud_base;
246 unsigned short close_delay;
247 char io_type;
248 char reserved_char[1];
249 int hub6;
250 unsigned short closing_wait; /* time to wait before closing */
251 unsigned short closing_wait2; /* no longer used... */
252 unsigned char *iomem_base;
253 unsigned short iomem_reg_shift;
254 unsigned int port_high;
255 unsigned long iomap_base; /* cookie passed into ioremap */
256 int reserved[1];
257 /* Paranoia (imagine 64bit kernel overwriting 32bit userspace stack) */
258 uint32_t bbox_reserved[16];
259};
Eric Andersen74400cc2001-10-18 04:11:39 +0000260static void console_init(void)
Eric Andersen0460ff21999-10-25 23:32:44 +0000261{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000262 struct serial_struct sr;
263 char *s;
Eric Andersen0460ff21999-10-25 23:32:44 +0000264
Denis Vlasenko7a2ca5e2007-02-21 00:15:20 +0000265 s = getenv("CONSOLE");
266 if (!s) s = getenv("console");
267 if (s) {
268 int fd = open(s, O_RDWR | O_NONBLOCK | O_NOCTTY);
269 if (fd >= 0) {
270 dup2(fd, 0);
271 dup2(fd, 1);
272 dup2(fd, 2);
273 while (fd > 2) close(fd--);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000274 }
Denis Vlasenko7a2ca5e2007-02-21 00:15:20 +0000275 messageD(L_LOG, "console='%s'", s);
Denis Vlasenkod8540f72007-06-14 07:53:06 +0000276 } else {
277 /* Make sure fd 0,1,2 are not closed */
278 bb_sanitize_stdio();
Eric Andersen07e52971999-11-07 07:38:08 +0000279 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000280
Denis Vlasenko7a2ca5e2007-02-21 00:15:20 +0000281 s = getenv("TERM");
282 if (ioctl(0, TIOCGSERIAL, &sr) == 0) {
283 /* Force the TERM setting to vt102 for serial console --
284 * if TERM is set to linux (the default) */
285 if (!s || strcmp(s, "linux") == 0)
286 putenv((char*)"TERM=vt102");
Denis Vlasenkoccd412d2007-03-05 19:55:30 +0000287#if !ENABLE_FEATURE_INIT_SYSLOG
Denis Vlasenko7a2ca5e2007-02-21 00:15:20 +0000288 log_console = NULL;
Glenn L McGrathb4a1baa2003-01-13 22:09:50 +0000289#endif
Denis Vlasenko7a2ca5e2007-02-21 00:15:20 +0000290 } else if (!s)
291 putenv((char*)"TERM=linux");
Eric Andersen0460ff21999-10-25 23:32:44 +0000292}
Glenn L McGrathbaf55a82002-08-22 18:22:10 +0000293
Denis Vlasenkod8540f72007-06-14 07:53:06 +0000294static void fixup_argv(char **argv)
Eric Andersen7ef1a5b2001-03-20 17:39:08 +0000295{
Eric Andersen7ef1a5b2001-03-20 17:39:08 +0000296 /* Fix up argv[0] to be certain we claim to be init */
Denis Vlasenkod8540f72007-06-14 07:53:06 +0000297 strncpy(argv[0], "init", strlen(argv[0]));
Eric Andersen7ef1a5b2001-03-20 17:39:08 +0000298
299 /* Wipe argv[1]-argv[N] so they don't clutter the ps listing */
Denis Vlasenkod8540f72007-06-14 07:53:06 +0000300 while (*++argv)
301 memset(*argv, 0, strlen(*argv));
Eric Andersen7ef1a5b2001-03-20 17:39:08 +0000302}
303
Bernhard Reutner-Fischer0da069d2006-05-29 12:54:16 +0000304/* Open the new terminal device */
Denis Vlasenko7a2ca5e2007-02-21 00:15:20 +0000305static void open_stdio_to_tty(const char* tty_name, int fail)
306{
307 /* empty tty_name means "use init's tty", else... */
308 if (tty_name[0]) {
Denis Vlasenkod238a472007-03-05 19:22:04 +0000309 int fd = device_open(tty_name, O_RDWR);
310 if (fd < 0) {
Denis Vlasenko966bb432007-02-27 19:20:33 +0000311 message(L_LOG | L_CONSOLE, "Can't open %s: %s",
Denis Vlasenko7a2ca5e2007-02-21 00:15:20 +0000312 tty_name, strerror(errno));
313 if (fail)
314 _exit(1);
Bernhard Reutner-Fischercf1f2ac2006-06-02 10:43:17 +0000315#if !ENABLE_DEBUG_INIT
Denis Vlasenko7a2ca5e2007-02-21 00:15:20 +0000316 shutdown_signal(SIGUSR1);
Bernhard Reutner-Fischercf1f2ac2006-06-02 10:43:17 +0000317#else
Denis Vlasenko7a2ca5e2007-02-21 00:15:20 +0000318 _exit(2);
Bernhard Reutner-Fischercf1f2ac2006-06-02 10:43:17 +0000319#endif
Denis Vlasenkod238a472007-03-05 19:22:04 +0000320 } else {
321 dup2(fd, 0);
322 dup2(fd, 1);
323 dup2(fd, 2);
324 if (fd > 2) close(fd);
Denis Vlasenko7a2ca5e2007-02-21 00:15:20 +0000325 }
Bernhard Reutner-Fischer0da069d2006-05-29 12:54:16 +0000326 }
Denis Vlasenkod238a472007-03-05 19:22:04 +0000327 set_sane_term();
Bernhard Reutner-Fischer0da069d2006-05-29 12:54:16 +0000328}
329
Glenn L McGrathb4a1baa2003-01-13 22:09:50 +0000330static pid_t run(const struct init_action *a)
Eric Andersen0298be82002-03-05 15:12:19 +0000331{
Bernhard Reutner-Fischer3ab30802006-05-30 12:10:29 +0000332 int i;
Mike Frysinger10427ab2005-07-06 05:00:48 +0000333 pid_t pid;
Denis Vlasenko06c0a712007-01-29 22:51:44 +0000334 char *s, *tmpCmd, *cmdpath;
335 char *cmd[INIT_BUFFS_SIZE];
Glenn L McGrathbaf55a82002-08-22 18:22:10 +0000336 char buf[INIT_BUFFS_SIZE + 6]; /* INIT_BUFFS_SIZE+strlen("exec ")+1 */
Eric Andersen0298be82002-03-05 15:12:19 +0000337 sigset_t nmask, omask;
Erik Andersenac6e71f2000-01-08 22:04:33 +0000338
Eric Andersen0298be82002-03-05 15:12:19 +0000339 /* Block sigchild while forking. */
340 sigemptyset(&nmask);
341 sigaddset(&nmask, SIGCHLD);
342 sigprocmask(SIG_BLOCK, &nmask, &omask);
Denis Vlasenko966bb432007-02-27 19:20:33 +0000343 pid = fork();
344 sigprocmask(SIG_SETMASK, &omask, NULL);
Eric Andersen0298be82002-03-05 15:12:19 +0000345
Denis Vlasenko966bb432007-02-27 19:20:33 +0000346 if (pid)
347 return pid;
Eric Andersen0298be82002-03-05 15:12:19 +0000348
Denis Vlasenko966bb432007-02-27 19:20:33 +0000349 /* Reset signal handlers that were set by the parent process */
350 signal(SIGUSR1, SIG_DFL);
351 signal(SIGUSR2, SIG_DFL);
352 signal(SIGINT, SIG_DFL);
353 signal(SIGTERM, SIG_DFL);
354 signal(SIGHUP, SIG_DFL);
355 signal(SIGQUIT, SIG_DFL);
356 signal(SIGCONT, SIG_DFL);
357 signal(SIGSTOP, SIG_DFL);
358 signal(SIGTSTP, SIG_DFL);
Eric Andersen2f6c04f1999-11-01 23:59:44 +0000359
Denis Vlasenko966bb432007-02-27 19:20:33 +0000360 /* Create a new session and make ourself the process
361 * group leader */
362 setsid();
Eric Andersen599e3ce2002-07-03 11:08:10 +0000363
Denis Vlasenko966bb432007-02-27 19:20:33 +0000364 /* Open the new terminal device */
365 open_stdio_to_tty(a->terminal, 1);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000366
Denis Vlasenko966bb432007-02-27 19:20:33 +0000367 /* If the init Action requires us to wait, then force the
368 * supplied terminal to be the controlling tty. */
369 if (a->action & (SYSINIT | WAIT | CTRLALTDEL | SHUTDOWN | RESTART)) {
Eric Andersen0298be82002-03-05 15:12:19 +0000370
Denis Vlasenko966bb432007-02-27 19:20:33 +0000371 /* Now fork off another process to just hang around */
Denis Vlasenko6bef3d12007-11-06 03:05:54 +0000372 pid = fork();
Denis Vlasenkof6ccc622007-11-10 01:57:35 +0000373 if (pid < 0) {
Denis Vlasenko966bb432007-02-27 19:20:33 +0000374 message(L_LOG | L_CONSOLE, "Can't fork");
375 _exit(1);
376 }
377
378 if (pid > 0) {
Denis Vlasenko966bb432007-02-27 19:20:33 +0000379 /* We are the parent -- wait till the child is done */
380 signal(SIGINT, SIG_IGN);
381 signal(SIGTSTP, SIG_IGN);
382 signal(SIGQUIT, SIG_IGN);
383 signal(SIGCHLD, SIG_DFL);
384
385 waitfor(NULL, pid);
386 /* See if stealing the controlling tty back is necessary */
387 if (tcgetpgrp(0) != getpid())
388 _exit(0);
389
390 /* Use a temporary process to steal the controlling tty. */
Denis Vlasenko6bef3d12007-11-06 03:05:54 +0000391 pid = fork();
392 if (pid < 0) {
Denis Vlasenko966bb432007-02-27 19:20:33 +0000393 message(L_LOG | L_CONSOLE, "Can't fork");
Eric Andersen0298be82002-03-05 15:12:19 +0000394 _exit(1);
395 }
Denis Vlasenko966bb432007-02-27 19:20:33 +0000396 if (pid == 0) {
397 setsid();
398 ioctl(0, TIOCSCTTY, 1);
Eric Andersen0298be82002-03-05 15:12:19 +0000399 _exit(0);
400 }
Denis Vlasenko966bb432007-02-27 19:20:33 +0000401 waitfor(NULL, pid);
402 _exit(0);
Eric Andersen0298be82002-03-05 15:12:19 +0000403 }
404
Denis Vlasenko966bb432007-02-27 19:20:33 +0000405 /* Now fall though to actually execute things */
406 }
407
408 /* See if any special /bin/sh requiring characters are present */
409 if (strpbrk(a->command, "~`!$^&*()=|\\{}[];\"'<>?") != NULL) {
410 cmd[0] = (char*)DEFAULT_SHELL;
411 cmd[1] = (char*)"-c";
412 cmd[2] = strcat(strcpy(buf, "exec "), a->command);
413 cmd[3] = NULL;
414 } else {
415 /* Convert command (char*) into cmd (char**, one word per string) */
416 strcpy(buf, a->command);
417 s = buf;
418 for (tmpCmd = buf, i = 0; (tmpCmd = strsep(&s, " \t")) != NULL;) {
419 if (*tmpCmd != '\0') {
420 cmd[i] = tmpCmd;
421 i++;
422 }
423 }
424 cmd[i] = NULL;
425 }
426
427 cmdpath = cmd[0];
428
429 /*
430 * Interactive shells want to see a dash in argv[0]. This
431 * typically is handled by login, argv will be setup this
432 * way if a dash appears at the front of the command path
433 * (like "-/bin/sh").
434 */
435 if (*cmdpath == '-') {
436 /* skip over the dash */
437 ++cmdpath;
438
439 /* find the last component in the command pathname */
Denis Vlasenko818322b2007-09-24 18:27:04 +0000440 s = bb_get_last_path_component_nostrip(cmdpath);
Denis Vlasenko966bb432007-02-27 19:20:33 +0000441
442 /* make a new argv[0] */
Denis Vlasenko818322b2007-09-24 18:27:04 +0000443 cmd[0] = malloc(strlen(s) + 2);
444 if (cmd[0] == NULL) {
Denis Vlasenko966bb432007-02-27 19:20:33 +0000445 message(L_LOG | L_CONSOLE, bb_msg_memory_exhausted);
446 cmd[0] = cmdpath;
Erik Andersene132f4b2000-02-09 04:16:43 +0000447 } else {
Denis Vlasenko966bb432007-02-27 19:20:33 +0000448 cmd[0][0] = '-';
449 strcpy(cmd[0] + 1, s);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000450 }
Denis Vlasenko9b1381f2007-01-03 02:56:00 +0000451#if ENABLE_FEATURE_INIT_SCTTY
Denis Vlasenko966bb432007-02-27 19:20:33 +0000452 /* Establish this process as session leader and
453 * (attempt) to make the tty (if any) a controlling tty.
454 */
455 setsid();
456 ioctl(0, TIOCSCTTY, 0 /*don't steal it*/);
Paul Fox41a72ec2005-08-01 16:43:13 +0000457#endif
Denis Vlasenko966bb432007-02-27 19:20:33 +0000458 }
Eric Andersen7ef1a5b2001-03-20 17:39:08 +0000459
Eric Andersen1f50e842004-08-16 09:29:42 +0000460#if !defined(__UCLIBC__) || defined(__ARCH_HAS_MMU__)
Denis Vlasenko966bb432007-02-27 19:20:33 +0000461 if (a->action & ASKFIRST) {
Denis Vlasenko6ca409e2007-08-12 20:58:27 +0000462 static const char press_enter[] ALIGN1 =
Denis Vlasenko966bb432007-02-27 19:20:33 +0000463#ifdef CUSTOMIZED_BANNER
464#include CUSTOMIZED_BANNER
Eric Andersen1f50e842004-08-16 09:29:42 +0000465#endif
Denis Vlasenko966bb432007-02-27 19:20:33 +0000466 "\nPlease press Enter to activate this console. ";
467 char c;
468 /*
469 * Save memory by not exec-ing anything large (like a shell)
470 * before the user wants it. This is critical if swap is not
471 * enabled and the system has low memory. Generally this will
472 * be run on the second virtual console, and the first will
473 * be allowed to start a shell or whatever an init script
474 * specifies.
475 */
476 messageD(L_LOG, "waiting for enter to start '%s'"
477 "(pid %d, tty '%s')\n",
478 cmdpath, getpid(), a->terminal);
479 full_write(1, press_enter, sizeof(press_enter) - 1);
480 while (read(0, &c, 1) == 1 && c != '\n')
481 ;
482 }
483#endif
484 /* Log the process name and args */
485 message(L_LOG, "starting pid %d, tty '%s': '%s'",
486 getpid(), a->terminal, cmdpath);
Eric Andersen7e3bf6e2000-09-14 22:01:31 +0000487
Denis Vlasenko9b1381f2007-01-03 02:56:00 +0000488#if ENABLE_FEATURE_INIT_COREDUMPS
Denis Vlasenko966bb432007-02-27 19:20:33 +0000489 {
490 struct stat sb;
491 if (stat(CORE_ENABLE_FLAG_FILE, &sb) == 0) {
492 struct rlimit limit;
Glenn L McGrathbaf55a82002-08-22 18:22:10 +0000493
Denis Vlasenko966bb432007-02-27 19:20:33 +0000494 limit.rlim_cur = RLIM_INFINITY;
495 limit.rlim_max = RLIM_INFINITY;
496 setrlimit(RLIMIT_CORE, &limit);
Erik Andersen983b51b2000-04-04 18:14:25 +0000497 }
Erik Andersen05df2392000-01-13 04:43:48 +0000498 }
Denis Vlasenko966bb432007-02-27 19:20:33 +0000499#endif
500 /* Now run it. The new program will take over this PID,
501 * so nothing further in init.c should be run. */
502 BB_EXECVP(cmdpath, cmd);
503
504 /* We're still here? Some error happened. */
505 message(L_LOG | L_CONSOLE, "Cannot run '%s': %s",
506 cmdpath, strerror(errno));
507 _exit(-1);
Eric Andersen0460ff21999-10-25 23:32:44 +0000508}
509
Bernhard Reutner-Fischer3ab30802006-05-30 12:10:29 +0000510static int waitfor(const struct init_action *a, pid_t pid)
Erik Andersen0e3782f2000-01-07 02:54:55 +0000511{
Bernhard Reutner-Fischer3ab30802006-05-30 12:10:29 +0000512 int runpid;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000513 int status, wpid;
Erik Andersen0e3782f2000-01-07 02:54:55 +0000514
Bernhard Reutner-Fischer3ab30802006-05-30 12:10:29 +0000515 runpid = (NULL == a)? pid : run(a);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000516 while (1) {
Bernhard Reutner-Fischerd818dcc2007-02-16 17:17:07 +0000517 wpid = waitpid(runpid, &status, 0);
Bernhard Reutner-Fischer3ab30802006-05-30 12:10:29 +0000518 if (wpid == runpid)
Erik Andersene49d5ec2000-02-08 19:58:47 +0000519 break;
Glenn L McGrathe6ba16f2003-09-26 10:45:55 +0000520 if (wpid == -1 && errno == ECHILD) {
521 /* we missed its termination */
522 break;
523 }
524 /* FIXME other errors should maybe trigger an error, but allow
525 * the program to continue */
Erik Andersen0e3782f2000-01-07 02:54:55 +0000526 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000527 return wpid;
Erik Andersen0e3782f2000-01-07 02:54:55 +0000528}
529
Eric Andersen0298be82002-03-05 15:12:19 +0000530/* Run all commands of a particular type */
531static void run_actions(int action)
Eric Andersen219d6f51999-11-02 19:43:01 +0000532{
Eric Andersen0298be82002-03-05 15:12:19 +0000533 struct init_action *a, *tmp;
Eric Andersen219d6f51999-11-02 19:43:01 +0000534
Eric Andersen0298be82002-03-05 15:12:19 +0000535 for (a = init_action_list; a; a = tmp) {
536 tmp = a->next;
Eric Andersenc97ec342001-04-03 18:01:51 +0000537 if (a->action == action) {
Denis Vlasenko7a2ca5e2007-02-21 00:15:20 +0000538 /* a->terminal of "" means "init's console" */
539 if (a->terminal[0] && access(a->terminal, R_OK | W_OK)) {
Rob Landley2dd42792006-03-22 17:39:13 +0000540 delete_init_action(a);
541 } else if (a->action & (SYSINIT | WAIT | CTRLALTDEL | SHUTDOWN | RESTART)) {
Bernhard Reutner-Fischer3ab30802006-05-30 12:10:29 +0000542 waitfor(a, 0);
Eric Andersen0298be82002-03-05 15:12:19 +0000543 delete_init_action(a);
544 } else if (a->action & ONCE) {
545 run(a);
546 delete_init_action(a);
Glenn L McGrathbaf55a82002-08-22 18:22:10 +0000547 } else if (a->action & (RESPAWN | ASKFIRST)) {
Eric Andersen0298be82002-03-05 15:12:19 +0000548 /* Only run stuff with pid==0. If they have
549 * a pid, that means it is still running */
550 if (a->pid == 0) {
551 a->pid = run(a);
552 }
553 }
Erik Andersene132f4b2000-02-09 04:16:43 +0000554 }
555 }
556}
557
Bernhard Reutner-Fischer35e1a072006-05-29 13:08:35 +0000558#if !ENABLE_DEBUG_INIT
Eric Andersen2c1de612003-04-24 11:41:28 +0000559static void init_reboot(unsigned long magic)
560{
561 pid_t pid;
562 /* We have to fork here, since the kernel calls do_exit(0) in
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000563 * linux/kernel/sys.c, which can cause the machine to panic when
Eric Andersen2c1de612003-04-24 11:41:28 +0000564 * the init process is killed.... */
Denis Vlasenko9b1381f2007-01-03 02:56:00 +0000565 pid = vfork();
566 if (pid == 0) { /* child */
Eric Andersen2c1de612003-04-24 11:41:28 +0000567 reboot(magic);
Eric Andersen2c1de612003-04-24 11:41:28 +0000568 _exit(0);
569 }
Denis Vlasenko9b1381f2007-01-03 02:56:00 +0000570 waitpid(pid, NULL, 0);
Eric Andersen2c1de612003-04-24 11:41:28 +0000571}
572
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000573static void shutdown_system(void)
Eric Andersencc8ed391999-10-05 16:24:54 +0000574{
Eric Andersen813d88c2001-10-28 22:49:48 +0000575 sigset_t block_signals;
Erik Andersene132f4b2000-02-09 04:16:43 +0000576
Eric Andersena9cc8962002-09-16 06:49:06 +0000577 /* run everything to be run at "shutdown". This is done _prior_
578 * to killing everything, in case people wish to use scripts to
579 * shut things down gracefully... */
580 run_actions(SHUTDOWN);
581
Eric Andersen72f9a422001-10-28 05:12:20 +0000582 /* first disable all our signals */
583 sigemptyset(&block_signals);
584 sigaddset(&block_signals, SIGHUP);
Mike Frysingera77b4f32005-04-16 08:21:34 +0000585 sigaddset(&block_signals, SIGQUIT);
Eric Andersen72f9a422001-10-28 05:12:20 +0000586 sigaddset(&block_signals, SIGCHLD);
587 sigaddset(&block_signals, SIGUSR1);
588 sigaddset(&block_signals, SIGUSR2);
589 sigaddset(&block_signals, SIGINT);
590 sigaddset(&block_signals, SIGTERM);
Eric Andersened8a9be2001-11-30 19:10:58 +0000591 sigaddset(&block_signals, SIGCONT);
592 sigaddset(&block_signals, SIGSTOP);
593 sigaddset(&block_signals, SIGTSTP);
Eric Andersen72f9a422001-10-28 05:12:20 +0000594 sigprocmask(SIG_BLOCK, &block_signals, NULL);
Erik Andersen7dc16072000-01-04 01:10:25 +0000595
Denis Vlasenko7a2ca5e2007-02-21 00:15:20 +0000596 message(L_CONSOLE | L_LOG, "The system is going down NOW!");
597
Erik Andersene49d5ec2000-02-08 19:58:47 +0000598 /* Allow Ctrl-Alt-Del to reboot system. */
Erik Andersen4f3f7572000-04-28 00:18:56 +0000599 init_reboot(RB_ENABLE_CAD);
Erik Andersene132f4b2000-02-09 04:16:43 +0000600
Erik Andersene49d5ec2000-02-08 19:58:47 +0000601 /* Send signals to every process _except_ pid 1 */
Denis Vlasenko7a2ca5e2007-02-21 00:15:20 +0000602 message(L_CONSOLE | L_LOG, "Sending SIG%s to all processes", "TERM");
Erik Andersene49d5ec2000-02-08 19:58:47 +0000603 kill(-1, SIGTERM);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000604 sync();
Denis Vlasenko7a2ca5e2007-02-21 00:15:20 +0000605 sleep(1);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000606
Denis Vlasenko7a2ca5e2007-02-21 00:15:20 +0000607 message(L_CONSOLE | L_LOG, "Sending SIG%s to all processes", "KILL");
Erik Andersene49d5ec2000-02-08 19:58:47 +0000608 kill(-1, SIGKILL);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000609 sync();
Denis Vlasenko7a2ca5e2007-02-21 00:15:20 +0000610 sleep(1);
Eric Andersencc8ed391999-10-05 16:24:54 +0000611}
612
"Vladimir N. Oleynik"1f04c9d2006-02-01 14:47:52 +0000613static void exec_signal(int sig ATTRIBUTE_UNUSED)
Eric Andersen730f8262001-12-17 23:13:08 +0000614{
Eric Andersen0298be82002-03-05 15:12:19 +0000615 struct init_action *a, *tmp;
Eric Andersen5222d312002-07-03 05:15:23 +0000616 sigset_t unblock_signals;
Glenn L McGrathbaf55a82002-08-22 18:22:10 +0000617
Eric Andersen0298be82002-03-05 15:12:19 +0000618 for (a = init_action_list; a; a = tmp) {
619 tmp = a->next;
620 if (a->action & RESTART) {
Eric Andersen730f8262001-12-17 23:13:08 +0000621 shutdown_system();
Eric Andersen5222d312002-07-03 05:15:23 +0000622
623 /* unblock all signals, blocked in shutdown_system() */
624 sigemptyset(&unblock_signals);
625 sigaddset(&unblock_signals, SIGHUP);
Mike Frysingera77b4f32005-04-16 08:21:34 +0000626 sigaddset(&unblock_signals, SIGQUIT);
Eric Andersen5222d312002-07-03 05:15:23 +0000627 sigaddset(&unblock_signals, SIGCHLD);
628 sigaddset(&unblock_signals, SIGUSR1);
629 sigaddset(&unblock_signals, SIGUSR2);
630 sigaddset(&unblock_signals, SIGINT);
631 sigaddset(&unblock_signals, SIGTERM);
632 sigaddset(&unblock_signals, SIGCONT);
633 sigaddset(&unblock_signals, SIGSTOP);
634 sigaddset(&unblock_signals, SIGTSTP);
635 sigprocmask(SIG_UNBLOCK, &unblock_signals, NULL);
636
Eric Andersen3c8064f2003-07-05 08:29:01 +0000637 /* Open the new terminal device */
Denis Vlasenko7a2ca5e2007-02-21 00:15:20 +0000638 open_stdio_to_tty(a->terminal, 0);
Eric Andersen3c8064f2003-07-05 08:29:01 +0000639
Denis Vlasenkoec27feb2007-02-17 15:52:02 +0000640 messageD(L_CONSOLE | L_LOG, "Trying to re-exec %s", a->command);
Denis Vlasenko4921b542007-02-03 02:17:41 +0000641 BB_EXECLP(a->command, a->command, NULL);
Glenn L McGrathbaf55a82002-08-22 18:22:10 +0000642
Denis Vlasenko7a2ca5e2007-02-21 00:15:20 +0000643 message(L_CONSOLE | L_LOG, "Cannot run '%s': %s",
644 a->command, strerror(errno));
Eric Andersen730f8262001-12-17 23:13:08 +0000645 sleep(2);
646 init_reboot(RB_HALT_SYSTEM);
647 loop_forever();
648 }
649 }
650}
651
Paul Foxd112f8f2006-06-01 13:17:49 +0000652static void shutdown_signal(int sig)
Eric Andersencc8ed391999-10-05 16:24:54 +0000653{
Denis Vlasenkoe968fcd2007-02-03 02:42:47 +0000654 const char *m;
Paul Foxd112f8f2006-06-01 13:17:49 +0000655 int rb;
656
Erik Andersene49d5ec2000-02-08 19:58:47 +0000657 shutdown_system();
Paul Foxd112f8f2006-06-01 13:17:49 +0000658
Denis Vlasenkoe968fcd2007-02-03 02:42:47 +0000659 m = "halt";
660 rb = RB_HALT_SYSTEM;
Paul Foxd112f8f2006-06-01 13:17:49 +0000661 if (sig == SIGTERM) {
662 m = "reboot";
663 rb = RB_AUTOBOOT;
664 } else if (sig == SIGUSR2) {
665 m = "poweroff";
666 rb = RB_POWER_OFF;
Paul Foxd112f8f2006-06-01 13:17:49 +0000667 }
Denis Vlasenkoec27feb2007-02-17 15:52:02 +0000668 message(L_CONSOLE | L_LOG, "Requesting system %s", m);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000669 /* allow time for last message to reach serial console */
Erik Andersene132f4b2000-02-09 04:16:43 +0000670 sleep(2);
Paul Foxd112f8f2006-06-01 13:17:49 +0000671 init_reboot(rb);
Matt Kraai67a46402001-06-03 05:55:52 +0000672 loop_forever();
Eric Andersencc8ed391999-10-05 16:24:54 +0000673}
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000674
"Vladimir N. Oleynik"1f04c9d2006-02-01 14:47:52 +0000675static void ctrlaltdel_signal(int sig ATTRIBUTE_UNUSED)
Eric Andersenc97ec342001-04-03 18:01:51 +0000676{
677 run_actions(CTRLALTDEL);
678}
679
Eric Andersen0298be82002-03-05 15:12:19 +0000680/* The SIGSTOP & SIGTSTP handler */
"Vladimir N. Oleynik"1f04c9d2006-02-01 14:47:52 +0000681static void stop_handler(int sig ATTRIBUTE_UNUSED)
Eric Andersened8a9be2001-11-30 19:10:58 +0000682{
Eric Andersen0298be82002-03-05 15:12:19 +0000683 int saved_errno = errno;
Eric Andersened8a9be2001-11-30 19:10:58 +0000684
685 got_cont = 0;
Glenn L McGrathbaf55a82002-08-22 18:22:10 +0000686 while (!got_cont)
687 pause();
Eric Andersened8a9be2001-11-30 19:10:58 +0000688 got_cont = 0;
689 errno = saved_errno;
690}
691
Glenn L McGrathbaf55a82002-08-22 18:22:10 +0000692/* The SIGCONT handler */
"Vladimir N. Oleynik"1f04c9d2006-02-01 14:47:52 +0000693static void cont_handler(int sig ATTRIBUTE_UNUSED)
Eric Andersened8a9be2001-11-30 19:10:58 +0000694{
695 got_cont = 1;
696}
697
Denis Vlasenkoe968fcd2007-02-03 02:42:47 +0000698#endif /* !ENABLE_DEBUG_INIT */
Erik Andersende552872000-01-23 01:34:05 +0000699
Glenn L McGrathdc4e75e2003-09-02 02:36:18 +0000700static void new_init_action(int action, const char *command, const char *cons)
Erik Andersen7dc16072000-01-04 01:10:25 +0000701{
Eric Andersen22718092004-10-08 08:17:39 +0000702 struct init_action *new_action, *a, *last;
Erik Andersen9e737252000-01-06 01:16:13 +0000703
"Vladimir N. Oleynik"6c35c7c2005-10-12 15:34:25 +0000704 if (strcmp(cons, bb_dev_null) == 0 && (action & ASKFIRST))
Eric Andersen1644db92001-09-05 20:18:15 +0000705 return;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000706
Eric Andersen0298be82002-03-05 15:12:19 +0000707 /* Append to the end of the list */
Eric Andersen22718092004-10-08 08:17:39 +0000708 for (a = last = init_action_list; a; a = a->next) {
Eric Andersen82baf632004-10-08 08:21:54 +0000709 /* don't enter action if it's already in the list,
710 * but do overwrite existing actions */
Denis Vlasenkoec27feb2007-02-17 15:52:02 +0000711 if ((strcmp(a->command, command) == 0)
712 && (strcmp(a->terminal, cons) == 0)
713 ) {
Eric Andersen82baf632004-10-08 08:21:54 +0000714 a->action = action;
Eric Andersen6fd0e312003-07-22 09:48:56 +0000715 return;
716 }
Eric Andersen22718092004-10-08 08:17:39 +0000717 last = a;
Eric Andersen6fd0e312003-07-22 09:48:56 +0000718 }
Denis Vlasenko7a2ca5e2007-02-21 00:15:20 +0000719
720 new_action = xzalloc(sizeof(struct init_action));
Eric Andersen22718092004-10-08 08:17:39 +0000721 if (last) {
722 last->next = new_action;
Eric Andersen1644db92001-09-05 20:18:15 +0000723 } else {
Eric Andersen0298be82002-03-05 15:12:19 +0000724 init_action_list = new_action;
Eric Andersen1644db92001-09-05 20:18:15 +0000725 }
Eric Andersen0826b6b2002-07-03 23:50:16 +0000726 strcpy(new_action->command, command);
Eric Andersen0298be82002-03-05 15:12:19 +0000727 new_action->action = action;
Eric Andersen0826b6b2002-07-03 23:50:16 +0000728 strcpy(new_action->terminal, cons);
Denis Vlasenko7a2ca5e2007-02-21 00:15:20 +0000729 messageD(L_LOG | L_CONSOLE, "command='%s' action=%d tty='%s'\n",
Glenn L McGrathb4a1baa2003-01-13 22:09:50 +0000730 new_action->command, new_action->action, new_action->terminal);
Erik Andersen7dc16072000-01-04 01:10:25 +0000731}
732
Glenn L McGrathbaf55a82002-08-22 18:22:10 +0000733static void delete_init_action(struct init_action *action)
Erik Andersen7dc16072000-01-04 01:10:25 +0000734{
Eric Andersen0298be82002-03-05 15:12:19 +0000735 struct init_action *a, *b = NULL;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000736
Eric Andersen0298be82002-03-05 15:12:19 +0000737 for (a = init_action_list; a; b = a, a = a->next) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000738 if (a == action) {
739 if (b == NULL) {
Eric Andersen0298be82002-03-05 15:12:19 +0000740 init_action_list = a->next;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000741 } else {
Eric Andersen0298be82002-03-05 15:12:19 +0000742 b->next = a->next;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000743 }
744 free(a);
745 break;
746 }
Erik Andersen7dc16072000-01-04 01:10:25 +0000747 }
Erik Andersen7dc16072000-01-04 01:10:25 +0000748}
749
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000750/* NOTE that if CONFIG_FEATURE_USE_INITTAB is NOT defined,
Erik Andersen9e737252000-01-06 01:16:13 +0000751 * then parse_inittab() simply adds in some default
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000752 * actions(i.e., runs INIT_SCRIPT and then starts a pair
753 * of "askfirst" shells). If CONFIG_FEATURE_USE_INITTAB
754 * _is_ defined, but /etc/inittab is missing, this
Erik Andersen812d4662000-01-07 18:30:40 +0000755 * results in the same set of default behaviors.
Glenn L McGrathbaf55a82002-08-22 18:22:10 +0000756 */
Eric Andersen3e6ff902001-03-09 21:24:12 +0000757static void parse_inittab(void)
Erik Andersen7dc16072000-01-04 01:10:25 +0000758{
Denis Vlasenko9b1381f2007-01-03 02:56:00 +0000759#if ENABLE_FEATURE_USE_INITTAB
Erik Andersene49d5ec2000-02-08 19:58:47 +0000760 FILE *file;
Glenn L McGrathb4a1baa2003-01-13 22:09:50 +0000761 char buf[INIT_BUFFS_SIZE], lineAsRead[INIT_BUFFS_SIZE];
Denis Vlasenko7a2ca5e2007-02-21 00:15:20 +0000762 char tmpConsole[CONSOLE_NAME_SIZE];
Eric Andersen0298be82002-03-05 15:12:19 +0000763 char *id, *runlev, *action, *command, *eol;
764 const struct init_action_type *a = actions;
Erik Andersen7dc16072000-01-04 01:10:25 +0000765
Erik Andersene49d5ec2000-02-08 19:58:47 +0000766 file = fopen(INITTAB, "r");
767 if (file == NULL) {
768 /* No inittab file -- set up some default behavior */
Erik Andersen9e737252000-01-06 01:16:13 +0000769#endif
Eric Andersenc97ec342001-04-03 18:01:51 +0000770 /* Reboot on Ctrl-Alt-Del */
Denis Vlasenko2f0c0d02007-01-21 00:41:04 +0000771 new_init_action(CTRLALTDEL, "reboot", "");
Eric Andersen1644db92001-09-05 20:18:15 +0000772 /* Umount all filesystems on halt/reboot */
Denis Vlasenko2f0c0d02007-01-21 00:41:04 +0000773 new_init_action(SHUTDOWN, "umount -a -r", "");
Eric Andersen1644db92001-09-05 20:18:15 +0000774 /* Swapoff on halt/reboot */
Bernhard Reutner-Fischerd818dcc2007-02-16 17:17:07 +0000775 if (ENABLE_SWAPONOFF) new_init_action(SHUTDOWN, "swapoff -a", "");
Eric Andersen730f8262001-12-17 23:13:08 +0000776 /* Prepare to restart init when a HUP is received */
Denis Vlasenko2f0c0d02007-01-21 00:41:04 +0000777 new_init_action(RESTART, "init", "");
Eric Andersen3bc2b202002-07-29 06:39:58 +0000778 /* Askfirst shell on tty1-4 */
Glenn L McGrathdc4e75e2003-09-02 02:36:18 +0000779 new_init_action(ASKFIRST, bb_default_login_shell, "");
780 new_init_action(ASKFIRST, bb_default_login_shell, VC_2);
781 new_init_action(ASKFIRST, bb_default_login_shell, VC_3);
782 new_init_action(ASKFIRST, bb_default_login_shell, VC_4);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000783 /* sysinit */
Glenn L McGrathb4a1baa2003-01-13 22:09:50 +0000784 new_init_action(SYSINIT, INIT_SCRIPT, "");
Erik Andersen7dc16072000-01-04 01:10:25 +0000785
Erik Andersene49d5ec2000-02-08 19:58:47 +0000786 return;
Denis Vlasenko9b1381f2007-01-03 02:56:00 +0000787#if ENABLE_FEATURE_USE_INITTAB
Erik Andersen9e737252000-01-06 01:16:13 +0000788 }
Erik Andersen7dc16072000-01-04 01:10:25 +0000789
Eric Andersen0826b6b2002-07-03 23:50:16 +0000790 while (fgets(buf, INIT_BUFFS_SIZE, file) != NULL) {
Eric Andersenb5966362000-05-31 20:04:38 +0000791 /* Skip leading spaces */
792 for (id = buf; *id == ' ' || *id == '\t'; id++);
793
794 /* Skip the line if it's a comment */
795 if (*id == '#' || *id == '\n')
Erik Andersene49d5ec2000-02-08 19:58:47 +0000796 continue;
Erik Andersen7dc16072000-01-04 01:10:25 +0000797
Erik Andersene49d5ec2000-02-08 19:58:47 +0000798 /* Trim the trailing \n */
Bernhard Reutner-Fischerd818dcc2007-02-16 17:17:07 +0000799 //XXX: chomp() ?
Eric Andersenb5966362000-05-31 20:04:38 +0000800 eol = strrchr(id, '\n');
801 if (eol != NULL)
802 *eol = '\0';
Erik Andersen7dc16072000-01-04 01:10:25 +0000803
Erik Andersene49d5ec2000-02-08 19:58:47 +0000804 /* Keep a copy around for posterity's sake (and error msgs) */
805 strcpy(lineAsRead, buf);
806
Eric Andersenb5966362000-05-31 20:04:38 +0000807 /* Separate the ID field from the runlevels */
808 runlev = strchr(id, ':');
809 if (runlev == NULL || *(runlev + 1) == '\0') {
Denis Vlasenkoec27feb2007-02-17 15:52:02 +0000810 message(L_LOG | L_CONSOLE, "Bad inittab entry: %s", lineAsRead);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000811 continue;
812 } else {
Eric Andersenb5966362000-05-31 20:04:38 +0000813 *runlev = '\0';
814 ++runlev;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000815 }
816
Eric Andersenb5966362000-05-31 20:04:38 +0000817 /* Separate the runlevels from the action */
818 action = strchr(runlev, ':');
819 if (action == NULL || *(action + 1) == '\0') {
Denis Vlasenkoec27feb2007-02-17 15:52:02 +0000820 message(L_LOG | L_CONSOLE, "Bad inittab entry: %s", lineAsRead);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000821 continue;
822 } else {
Eric Andersenb5966362000-05-31 20:04:38 +0000823 *action = '\0';
824 ++action;
825 }
826
Eric Andersen0298be82002-03-05 15:12:19 +0000827 /* Separate the action from the command */
828 command = strchr(action, ':');
829 if (command == NULL || *(command + 1) == '\0') {
Denis Vlasenkoec27feb2007-02-17 15:52:02 +0000830 message(L_LOG | L_CONSOLE, "Bad inittab entry: %s", lineAsRead);
Eric Andersenb5966362000-05-31 20:04:38 +0000831 continue;
832 } else {
Eric Andersen0298be82002-03-05 15:12:19 +0000833 *command = '\0';
834 ++command;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000835 }
836
837 /* Ok, now process it */
Glenn L McGrathb4a1baa2003-01-13 22:09:50 +0000838 for (a = actions; a->name != 0; a++) {
Eric Andersenb5966362000-05-31 20:04:38 +0000839 if (strcmp(a->name, action) == 0) {
840 if (*id != '\0') {
Denis Vlasenko51742f42007-04-12 00:32:05 +0000841 if (strncmp(id, "/dev/", 5) == 0)
Glenn L McGrathb4a1baa2003-01-13 22:09:50 +0000842 id += 5;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000843 strcpy(tmpConsole, "/dev/");
Glenn L McGrathb4a1baa2003-01-13 22:09:50 +0000844 safe_strncpy(tmpConsole + 5, id,
Denis Vlasenko7a2ca5e2007-02-21 00:15:20 +0000845 sizeof(tmpConsole) - 5);
Eric Andersenb5966362000-05-31 20:04:38 +0000846 id = tmpConsole;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000847 }
Eric Andersen0298be82002-03-05 15:12:19 +0000848 new_init_action(a->action, command, id);
Glenn L McGrathb4a1baa2003-01-13 22:09:50 +0000849 break;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000850 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000851 }
Glenn L McGrathb4a1baa2003-01-13 22:09:50 +0000852 if (a->name == 0) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000853 /* Choke on an unknown action */
Denis Vlasenkoec27feb2007-02-17 15:52:02 +0000854 message(L_LOG | L_CONSOLE, "Bad inittab entry: %s", lineAsRead);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000855 }
Erik Andersen7dc16072000-01-04 01:10:25 +0000856 }
Eric Andersend8636ca2002-05-15 22:19:09 +0000857 fclose(file);
Denis Vlasenko9b1381f2007-01-03 02:56:00 +0000858#endif /* FEATURE_USE_INITTAB */
Erik Andersen7dc16072000-01-04 01:10:25 +0000859}
860
Denis Vlasenko9b1381f2007-01-03 02:56:00 +0000861#if ENABLE_FEATURE_USE_INITTAB
"Vladimir N. Oleynik"1f04c9d2006-02-01 14:47:52 +0000862static void reload_signal(int sig ATTRIBUTE_UNUSED)
Eric Andersen6fd0e312003-07-22 09:48:56 +0000863{
Eric Andersen82baf632004-10-08 08:21:54 +0000864 struct init_action *a, *tmp;
865
Denis Vlasenko7a2ca5e2007-02-21 00:15:20 +0000866 message(L_LOG, "reloading /etc/inittab");
Eric Andersen82baf632004-10-08 08:21:54 +0000867
868 /* disable old entrys */
869 for (a = init_action_list; a; a = a->next ) {
870 a->action = ONCE;
871 }
872
873 parse_inittab();
874
875 /* remove unused entrys */
876 for (a = init_action_list; a; a = tmp) {
877 tmp = a->next;
Denis Vlasenko219d14d2007-03-24 15:40:16 +0000878 if ((a->action & (ONCE | SYSINIT | WAIT)) && a->pid == 0) {
Eric Andersen82baf632004-10-08 08:21:54 +0000879 delete_init_action(a);
880 }
881 }
Eric Andersen6fd0e312003-07-22 09:48:56 +0000882 run_actions(RESPAWN);
Eric Andersen6fd0e312003-07-22 09:48:56 +0000883}
Denis Vlasenko9b1381f2007-01-03 02:56:00 +0000884#endif /* FEATURE_USE_INITTAB */
Eric Andersen82baf632004-10-08 08:21:54 +0000885
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +0000886int init_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Rob Landleydfba7412006-03-06 20:47:33 +0000887int init_main(int argc, char **argv)
Eric Andersen0460ff21999-10-25 23:32:44 +0000888{
Eric Andersen0298be82002-03-05 15:12:19 +0000889 struct init_action *a;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000890 pid_t wpid;
Eric Andersen0460ff21999-10-25 23:32:44 +0000891
Denis Vlasenko9b1381f2007-01-03 02:56:00 +0000892 die_sleep = 30 * 24*60*60; /* if xmalloc will ever die... */
893
Eric Andersen730f8262001-12-17 23:13:08 +0000894 if (argc > 1 && !strcmp(argv[1], "-q")) {
Denis Vlasenko7a2ca5e2007-02-21 00:15:20 +0000895 return kill(1, SIGHUP);
Eric Andersen730f8262001-12-17 23:13:08 +0000896 }
Bernhard Reutner-Fischer35e1a072006-05-29 13:08:35 +0000897#if !ENABLE_DEBUG_INIT
Erik Andersena51ecdd2000-02-24 18:09:58 +0000898 /* Expect to be invoked as init with PID=1 or be invoked as linuxrc */
Denis Vlasenko7a2ca5e2007-02-21 00:15:20 +0000899 if (getpid() != 1
900 && (!ENABLE_FEATURE_INITRD || !strstr(applet_name, "linuxrc"))
901 ) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000902 bb_show_usage();
Erik Andersene49d5ec2000-02-08 19:58:47 +0000903 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000904 /* Set up sig handlers -- be sure to
905 * clear all of these in run() */
Glenn L McGrathbaf55a82002-08-22 18:22:10 +0000906 signal(SIGHUP, exec_signal);
Mike Frysingera77b4f32005-04-16 08:21:34 +0000907 signal(SIGQUIT, exec_signal);
Paul Foxd112f8f2006-06-01 13:17:49 +0000908 signal(SIGUSR1, shutdown_signal);
909 signal(SIGUSR2, shutdown_signal);
Glenn L McGrathbaf55a82002-08-22 18:22:10 +0000910 signal(SIGINT, ctrlaltdel_signal);
Paul Foxd112f8f2006-06-01 13:17:49 +0000911 signal(SIGTERM, shutdown_signal);
Eric Andersened8a9be2001-11-30 19:10:58 +0000912 signal(SIGCONT, cont_handler);
913 signal(SIGSTOP, stop_handler);
914 signal(SIGTSTP, stop_handler);
Eric Andersen0460ff21999-10-25 23:32:44 +0000915
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000916 /* Turn off rebooting via CTL-ALT-DEL -- we get a
Erik Andersene49d5ec2000-02-08 19:58:47 +0000917 * SIGINT on CAD so we can shut things down gracefully... */
Erik Andersen4f3f7572000-04-28 00:18:56 +0000918 init_reboot(RB_DISABLE_CAD);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000919#endif
Erik Andersen05df2392000-01-13 04:43:48 +0000920
Denis Vlasenkod8540f72007-06-14 07:53:06 +0000921
Eric Andersen599e3ce2002-07-03 11:08:10 +0000922 /* Figure out where the default console should be */
923 console_init();
Denis Vlasenkod238a472007-03-05 19:22:04 +0000924 set_sane_term();
Erik Andersen983b51b2000-04-04 18:14:25 +0000925 chdir("/");
Erik Andersene49d5ec2000-02-08 19:58:47 +0000926 setsid();
Glenn L McGrathb4a1baa2003-01-13 22:09:50 +0000927 {
Denis Vlasenko6ca409e2007-08-12 20:58:27 +0000928 const char *const *e;
Glenn L McGrathb4a1baa2003-01-13 22:09:50 +0000929 /* Make sure environs is set to something sane */
Denis Vlasenkobf0a2012006-12-26 10:42:51 +0000930 for (e = environment; *e; e++)
Manuel Novoa III cad53642003-03-19 09:13:01 +0000931 putenv((char *) *e);
Glenn L McGrathb4a1baa2003-01-13 22:09:50 +0000932 }
Rob Landleycba1b962006-07-09 17:28:17 +0000933
934 if (argc > 1) setenv("RUNLEVEL", argv[1], 1);
935
Erik Andersene49d5ec2000-02-08 19:58:47 +0000936 /* Hello world */
Denis Vlasenkoca525b42007-06-13 12:27:17 +0000937 message(MAYBE_CONSOLE | L_LOG, "init started: %s", bb_banner);
Eric Andersencc8ed391999-10-05 16:24:54 +0000938
Erik Andersene49d5ec2000-02-08 19:58:47 +0000939 /* Make sure there is enough memory to do something useful. */
Rob Landleyc3386a42005-08-30 18:50:37 +0000940 if (ENABLE_SWAPONOFF) {
941 struct sysinfo info;
942
943 if (!sysinfo(&info) &&
Denis Vlasenkodca0b702006-10-27 09:05:02 +0000944 (info.mem_unit ? : 1) * (long long)info.totalram < 1024*1024)
Rob Landleyc3386a42005-08-30 18:50:37 +0000945 {
Denis Vlasenko7a2ca5e2007-02-21 00:15:20 +0000946 message(L_CONSOLE, "Low memory, forcing swapon");
Rob Landleyc3386a42005-08-30 18:50:37 +0000947 /* swapon -a requires /proc typically */
Denis Vlasenko2f0c0d02007-01-21 00:41:04 +0000948 new_init_action(SYSINIT, "mount -t proc proc /proc", "");
Rob Landleyc3386a42005-08-30 18:50:37 +0000949 /* Try to turn on swap */
Denis Vlasenko2f0c0d02007-01-21 00:41:04 +0000950 new_init_action(SYSINIT, "swapon -a", "");
Rob Landleyc3386a42005-08-30 18:50:37 +0000951 run_actions(SYSINIT); /* wait and removing */
952 }
953 }
Erik Andersen9e737252000-01-06 01:16:13 +0000954
Erik Andersene49d5ec2000-02-08 19:58:47 +0000955 /* Check if we are supposed to be in single user mode */
Denis Vlasenkobf66fbc2006-12-21 13:23:14 +0000956 if (argc > 1
957 && (!strcmp(argv[1], "single") || !strcmp(argv[1], "-s") || LONE_CHAR(argv[1], '1'))
958 ) {
Glenn L McGrathb4a1baa2003-01-13 22:09:50 +0000959 /* Start a shell on console */
Glenn L McGrathdc4e75e2003-09-02 02:36:18 +0000960 new_init_action(RESPAWN, bb_default_login_shell, "");
Erik Andersene49d5ec2000-02-08 19:58:47 +0000961 } else {
962 /* Not in single user mode -- see what inittab says */
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000963
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000964 /* NOTE that if CONFIG_FEATURE_USE_INITTAB is NOT defined,
Erik Andersene49d5ec2000-02-08 19:58:47 +0000965 * then parse_inittab() simply adds in some default
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000966 * actions(i.e., runs INIT_SCRIPT and then starts a pair
Erik Andersene49d5ec2000-02-08 19:58:47 +0000967 * of "askfirst" shells */
968 parse_inittab();
Eric Andersend00c2621999-12-07 08:37:31 +0000969 }
Erik Andersen983b51b2000-04-04 18:14:25 +0000970
Denis Vlasenko9b1381f2007-01-03 02:56:00 +0000971#if ENABLE_SELINUX
Rob Landleyb3ede5a2006-03-27 23:09:12 +0000972 if (getenv("SELINUX_INIT") == NULL) {
973 int enforce = 0;
974
Denis Vlasenkod8540f72007-06-14 07:53:06 +0000975 putenv((char*)"SELINUX_INIT=YES");
Rob Landleyb3ede5a2006-03-27 23:09:12 +0000976 if (selinux_init_load_policy(&enforce) == 0) {
Denis Vlasenko4921b542007-02-03 02:17:41 +0000977 BB_EXECVP(argv[0], argv);
Rob Landleyb3ede5a2006-03-27 23:09:12 +0000978 } else if (enforce > 0) {
979 /* SELinux in enforcing mode but load_policy failed */
980 /* At this point, we probably can't open /dev/console, so log() won't work */
Denis Vlasenkob7167542007-02-27 19:20:00 +0000981 message(L_CONSOLE, "Cannot load SELinux Policy. "
982 "Machine is in enforcing mode. Halting now.");
Rob Landleyb3ede5a2006-03-27 23:09:12 +0000983 exit(1);
984 }
985 }
986#endif /* CONFIG_SELINUX */
987
Eric Andersen7ef1a5b2001-03-20 17:39:08 +0000988 /* Make the command line just say "init" -- thats all, nothing else */
Denis Vlasenkod8540f72007-06-14 07:53:06 +0000989 fixup_argv(argv);
Eric Andersen219d6f51999-11-02 19:43:01 +0000990
Erik Andersene49d5ec2000-02-08 19:58:47 +0000991 /* Now run everything that needs to be run */
992
993 /* First run the sysinit command */
Eric Andersen1644db92001-09-05 20:18:15 +0000994 run_actions(SYSINIT);
Eric Andersen0298be82002-03-05 15:12:19 +0000995
Erik Andersene49d5ec2000-02-08 19:58:47 +0000996 /* Next run anything that wants to block */
Eric Andersen1644db92001-09-05 20:18:15 +0000997 run_actions(WAIT);
Eric Andersen0298be82002-03-05 15:12:19 +0000998
Erik Andersene49d5ec2000-02-08 19:58:47 +0000999 /* Next run anything to be run only once */
Eric Andersen0298be82002-03-05 15:12:19 +00001000 run_actions(ONCE);
1001
Denis Vlasenko9b1381f2007-01-03 02:56:00 +00001002#if ENABLE_FEATURE_USE_INITTAB
Eric Andersen6fd0e312003-07-22 09:48:56 +00001003 /* Redefine SIGHUP to reread /etc/inittab */
1004 signal(SIGHUP, reload_signal);
Eric Andersen82baf632004-10-08 08:21:54 +00001005#else
1006 signal(SIGHUP, SIG_IGN);
Denis Vlasenko9b1381f2007-01-03 02:56:00 +00001007#endif /* FEATURE_USE_INITTAB */
Eric Andersen82baf632004-10-08 08:21:54 +00001008
Erik Andersene49d5ec2000-02-08 19:58:47 +00001009 /* Now run the looping stuff for the rest of forever */
1010 while (1) {
Eric Andersen0298be82002-03-05 15:12:19 +00001011 /* run the respawn stuff */
1012 run_actions(RESPAWN);
1013
1014 /* run the askfirst stuff */
1015 run_actions(ASKFIRST);
1016
1017 /* Don't consume all CPU time -- sleep a bit */
1018 sleep(1);
1019
Erik Andersene49d5ec2000-02-08 19:58:47 +00001020 /* Wait for a child process to exit */
Bernhard Reutner-Fischerc58dbf22006-05-30 12:16:54 +00001021 wpid = wait(NULL);
Eric Andersen87812dc2004-04-12 19:21:54 +00001022 while (wpid > 0) {
Erik Andersene49d5ec2000-02-08 19:58:47 +00001023 /* Find out who died and clean up their corpse */
Eric Andersen0298be82002-03-05 15:12:19 +00001024 for (a = init_action_list; a; a = a->next) {
Erik Andersene49d5ec2000-02-08 19:58:47 +00001025 if (a->pid == wpid) {
Eric Andersen0298be82002-03-05 15:12:19 +00001026 /* Set the pid to 0 so that the process gets
1027 * restarted by run_actions() */
Erik Andersene49d5ec2000-02-08 19:58:47 +00001028 a->pid = 0;
Denis Vlasenko7a2ca5e2007-02-21 00:15:20 +00001029 message(L_LOG, "process '%s' (pid %d) exited. "
Glenn L McGrathb4a1baa2003-01-13 22:09:50 +00001030 "Scheduling it for restart.",
1031 a->command, wpid);
Erik Andersene49d5ec2000-02-08 19:58:47 +00001032 }
1033 }
Eric Andersencf1fee02002-12-17 09:48:16 +00001034 /* see if anyone else is waiting to be reaped */
Denis Vlasenkoce979602006-09-27 23:31:08 +00001035 wpid = waitpid(-1, NULL, WNOHANG);
Erik Andersene49d5ec2000-02-08 19:58:47 +00001036 }
Erik Andersene49d5ec2000-02-08 19:58:47 +00001037 }
Eric Andersencc8ed391999-10-05 16:24:54 +00001038}