blob: bc8979859d46d91791f2dc16ed9c444646e9129a [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
Bernhard Reutner-Fischercf1f2ac2006-06-02 10:43:17 +000012#include "busybox.h"
Eric Andersenb186d981999-12-03 09:19:54 +000013#include <paths.h>
Erik Andersen42094cd2000-03-20 21:34:52 +000014#include <signal.h>
Erik Andersen42094cd2000-03-20 21:34:52 +000015#include <sys/ioctl.h>
Erik Andersen42094cd2000-03-20 21:34:52 +000016#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 Vlasenko9b1381f2007-01-03 02:56:00 +000019#if ENABLE_SYSLOGD
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
Eric Andersen41492d62001-02-23 00:05:56 +000027#ifndef _PATH_STDPATH
Erik Andersen4f3f7572000-04-28 00:18:56 +000028#define _PATH_STDPATH "/usr/bin:/bin:/usr/sbin:/sbin"
Eric Andersen41492d62001-02-23 00:05:56 +000029#endif
Eric Andersencc8ed391999-10-05 16:24:54 +000030
Denis Vlasenko9b1381f2007-01-03 02:56:00 +000031#if ENABLE_FEATURE_INIT_COREDUMPS
Erik Andersen983b51b2000-04-04 18:14:25 +000032/*
Eric Andersenc7bda1c2004-03-15 08:29:22 +000033 * When a file named CORE_ENABLE_FLAG_FILE exists, setrlimit is called
Erik Andersen183da4a2000-04-04 18:36:37 +000034 * before processes are spawned to set core file size as unlimited.
35 * This is for debugging only. Don't use this is production, unless
36 * you want core dumps lying about....
Erik Andersen983b51b2000-04-04 18:14:25 +000037 */
38#define CORE_ENABLE_FLAG_FILE "/.init_enable_core"
39#include <sys/resource.h>
Erik Andersen183da4a2000-04-04 18:36:37 +000040#endif
Erik Andersen983b51b2000-04-04 18:14:25 +000041
Glenn L McGrathbaf55a82002-08-22 18:22:10 +000042#define INITTAB "/etc/inittab" /* inittab file location */
Erik Andersen96e2abd2000-01-07 11:40:44 +000043#ifndef INIT_SCRIPT
Glenn L McGrathbaf55a82002-08-22 18:22:10 +000044#define INIT_SCRIPT "/etc/init.d/rcS" /* Default sysinit script. */
Erik Andersen96e2abd2000-01-07 11:40:44 +000045#endif
Erik Andersen9c88cac1999-12-30 09:25:17 +000046
Erik Andersen7dc16072000-01-04 01:10:25 +000047/* Allowed init action types */
Eric Andersen0298be82002-03-05 15:12:19 +000048#define SYSINIT 0x001
49#define RESPAWN 0x002
50#define ASKFIRST 0x004
51#define WAIT 0x008
52#define ONCE 0x010
53#define CTRLALTDEL 0x020
54#define SHUTDOWN 0x040
55#define RESTART 0x080
Erik Andersen7dc16072000-01-04 01:10:25 +000056
Erik Andersen42094cd2000-03-20 21:34:52 +000057/* A mapping between "inittab" action name strings and action type codes. */
Eric Andersen0298be82002-03-05 15:12:19 +000058struct init_action_type {
Erik Andersene49d5ec2000-02-08 19:58:47 +000059 const char *name;
Eric Andersen0298be82002-03-05 15:12:19 +000060 int action;
61};
Erik Andersen7dc16072000-01-04 01:10:25 +000062
Eric Andersen0298be82002-03-05 15:12:19 +000063static const struct init_action_type actions[] = {
Erik Andersene49d5ec2000-02-08 19:58:47 +000064 {"sysinit", SYSINIT},
65 {"respawn", RESPAWN},
66 {"askfirst", ASKFIRST},
67 {"wait", WAIT},
68 {"once", ONCE},
Erik Andersene132f4b2000-02-09 04:16:43 +000069 {"ctrlaltdel", CTRLALTDEL},
Eric Andersenc97ec342001-04-03 18:01:51 +000070 {"shutdown", SHUTDOWN},
Eric Andersen730f8262001-12-17 23:13:08 +000071 {"restart", RESTART},
Pavel Roskin9027bcf2000-07-14 15:44:25 +000072 {0, 0}
Erik Andersen7dc16072000-01-04 01:10:25 +000073};
74
Eric Andersen0298be82002-03-05 15:12:19 +000075/* Set up a linked list of init_actions, to be read from inittab */
76struct init_action {
Eric Andersen0298be82002-03-05 15:12:19 +000077 struct init_action *next;
78 int action;
Denis Vlasenko7a2ca5e2007-02-21 00:15:20 +000079 pid_t pid;
80 char command[INIT_BUFFS_SIZE];
81 char terminal[CONSOLE_NAME_SIZE];
Erik Andersen7dc16072000-01-04 01:10:25 +000082};
Erik Andersen7dc16072000-01-04 01:10:25 +000083
Eric Andersen0298be82002-03-05 15:12:19 +000084/* Static variables */
85static struct init_action *init_action_list = NULL;
Glenn L McGrathbaf55a82002-08-22 18:22:10 +000086
Denis Vlasenko9b1381f2007-01-03 02:56:00 +000087#if !ENABLE_SYSLOGD
Denis Vlasenko4c978632007-02-03 03:31:13 +000088static const char *log_console = VC_5;
Eric Andersene7078062002-07-29 08:00:16 +000089#endif
Bernhard Reutner-Fischercf1f2ac2006-06-02 10:43:17 +000090#if !ENABLE_DEBUG_INIT
Eric Andersen0298be82002-03-05 15:12:19 +000091static sig_atomic_t got_cont = 0;
Bernhard Reutner-Fischercf1f2ac2006-06-02 10:43:17 +000092#endif
Rob Landleybc68cd12006-03-10 19:22:06 +000093
94enum {
Denis Vlasenkoec27feb2007-02-17 15:52:02 +000095 L_LOG = 0x1,
96 L_CONSOLE = 0x2,
Glenn L McGrathbaf55a82002-08-22 18:22:10 +000097
Denis Vlasenko9b1381f2007-01-03 02:56:00 +000098#if ENABLE_FEATURE_EXTRA_QUIET
Rob Landleybc68cd12006-03-10 19:22:06 +000099 MAYBE_CONSOLE = 0x0,
Eric Andersen0298be82002-03-05 15:12:19 +0000100#else
Denis Vlasenkoec27feb2007-02-17 15:52:02 +0000101 MAYBE_CONSOLE = L_CONSOLE,
Eric Andersen0298be82002-03-05 15:12:19 +0000102#endif
Glenn L McGrathbaf55a82002-08-22 18:22:10 +0000103
Rob Landleybc68cd12006-03-10 19:22:06 +0000104#ifndef RB_HALT_SYSTEM
Bernhard Reutner-Fischercf1f2ac2006-06-02 10:43:17 +0000105 RB_HALT_SYSTEM = 0xcdef0123, /* FIXME: this overflows enum */
Rob Landleybc68cd12006-03-10 19:22:06 +0000106 RB_ENABLE_CAD = 0x89abcdef,
107 RB_DISABLE_CAD = 0,
108 RB_POWER_OFF = 0x4321fedc,
109 RB_AUTOBOOT = 0x01234567,
Eric Andersen0298be82002-03-05 15:12:19 +0000110#endif
Rob Landleybc68cd12006-03-10 19:22:06 +0000111};
Erik Andersen42094cd2000-03-20 21:34:52 +0000112
Glenn L McGrathb4a1baa2003-01-13 22:09:50 +0000113static const char * const environment[] = {
114 "HOME=/",
115 "PATH=" _PATH_STDPATH,
Glenn L McGrathdc4e75e2003-09-02 02:36:18 +0000116 "SHELL=/bin/sh",
Glenn L McGrathb4a1baa2003-01-13 22:09:50 +0000117 "USER=root",
118 NULL
119};
120
Eric Andersen0298be82002-03-05 15:12:19 +0000121/* Function prototypes */
122static void delete_init_action(struct init_action *a);
Bernhard Reutner-Fischer3ab30802006-05-30 12:10:29 +0000123static int waitfor(const struct init_action *a, pid_t pid);
Bernhard Reutner-Fischercf1f2ac2006-06-02 10:43:17 +0000124#if !ENABLE_DEBUG_INIT
Paul Foxd112f8f2006-06-01 13:17:49 +0000125static void shutdown_signal(int sig);
Bernhard Reutner-Fischercf1f2ac2006-06-02 10:43:17 +0000126#endif
Eric Andersen0460ff21999-10-25 23:32:44 +0000127
Denis Vlasenko9b1381f2007-01-03 02:56:00 +0000128#if !ENABLE_DEBUG_INIT
Eric Andersen74400cc2001-10-18 04:11:39 +0000129static void loop_forever(void)
Matt Kraai67a46402001-06-03 05:55:52 +0000130{
131 while (1)
Glenn L McGrathbaf55a82002-08-22 18:22:10 +0000132 sleep(1);
Matt Kraai67a46402001-06-03 05:55:52 +0000133}
Denis Vlasenko9b1381f2007-01-03 02:56:00 +0000134#endif
Eric Andersen7ef1a5b2001-03-20 17:39:08 +0000135
Erik Andersen42094cd2000-03-20 21:34:52 +0000136/* Print a message to the specified device.
Denis Vlasenkoec27feb2007-02-17 15:52:02 +0000137 * Device may be bitwise-or'd from L_LOG | L_CONSOLE */
Bernhard Reutner-Fischer35e1a072006-05-29 13:08:35 +0000138#if ENABLE_DEBUG_INIT
139#define messageD message
140#else
Denis Vlasenkobf0a2012006-12-26 10:42:51 +0000141#define messageD(...) do {} while (0)
Eric Andersenfedce062001-11-17 07:27:14 +0000142#endif
Glenn L McGrathb4a1baa2003-01-13 22:09:50 +0000143static void message(int device, const char *fmt, ...)
Glenn L McGrathbaf55a82002-08-22 18:22:10 +0000144 __attribute__ ((format(printf, 2, 3)));
Glenn L McGrathb4a1baa2003-01-13 22:09:50 +0000145static void message(int device, const char *fmt, ...)
Eric Andersen0460ff21999-10-25 23:32:44 +0000146{
Denis Vlasenko9b1381f2007-01-03 02:56:00 +0000147#if !ENABLE_SYSLOGD
Eric Andersen22237012003-01-23 07:08:26 +0000148 static int log_fd = -1;
149#endif
Eric Andersen4c781471999-11-26 08:12:56 +0000150
Denis Vlasenko7a2ca5e2007-02-21 00:15:20 +0000151 va_list arguments;
152 int l;
153 char msg[128];
154
Glenn L McGrathb4a1baa2003-01-13 22:09:50 +0000155 msg[0] = '\r';
Denis Vlasenko7a2ca5e2007-02-21 00:15:20 +0000156 va_start(arguments, fmt);
157 vsnprintf(msg + 1, sizeof(msg) - 2, fmt, arguments);
Denis Vlasenkoec27feb2007-02-17 15:52:02 +0000158 va_end(arguments);
Denis Vlasenko7a2ca5e2007-02-21 00:15:20 +0000159 msg[sizeof(msg) - 2] = '\0';
160 l = strlen(msg);
Glenn L McGrathb4a1baa2003-01-13 22:09:50 +0000161
Denis Vlasenko9b1381f2007-01-03 02:56:00 +0000162#if ENABLE_SYSLOGD
Glenn L McGrathb4a1baa2003-01-13 22:09:50 +0000163 /* Log the message to syslogd */
Denis Vlasenkoec27feb2007-02-17 15:52:02 +0000164 if (device & L_LOG) {
Denis Vlasenko7a2ca5e2007-02-21 00:15:20 +0000165 /* don't out "\r" */
Denis Vlasenko8f8f2682006-10-03 21:00:43 +0000166 openlog(applet_name, 0, LOG_DAEMON);
Denis Vlasenko7a2ca5e2007-02-21 00:15:20 +0000167 syslog(LOG_INFO, "init: %s", msg + 1);
Eric Andersen36adca82004-06-22 10:07:17 +0000168 closelog();
Eric Andersen3ae0c781999-11-04 01:13:21 +0000169 }
Glenn L McGrathb4a1baa2003-01-13 22:09:50 +0000170 msg[l++] = '\n';
Denis Vlasenko7a2ca5e2007-02-21 00:15:20 +0000171 msg[l] = '\0';
Erik Andersene49d5ec2000-02-08 19:58:47 +0000172#else
Glenn L McGrathb4a1baa2003-01-13 22:09:50 +0000173 msg[l++] = '\n';
Denis Vlasenko7a2ca5e2007-02-21 00:15:20 +0000174 msg[l] = '\0';
Erik Andersene49d5ec2000-02-08 19:58:47 +0000175 /* Take full control of the log tty, and never close it.
176 * It's mine, all mine! Muhahahaha! */
177 if (log_fd < 0) {
Denis Vlasenko7a2ca5e2007-02-21 00:15:20 +0000178 if (!log_console) {
179 log_fd = 2;
Eric Andersen6a979902002-09-30 20:08:53 +0000180 } else {
Denis Vlasenko7a2ca5e2007-02-21 00:15:20 +0000181 log_fd = device_open(log_console, O_WRONLY | O_NONBLOCK | O_NOCTTY);
182 if (log_fd < 0) {
183 bb_error_msg("can't log to %s", log_console);
184 device = L_CONSOLE;
185 } else {
186 fcntl(log_fd, F_SETFD, FD_CLOEXEC);
187 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000188 }
189 }
Denis Vlasenkoec27feb2007-02-17 15:52:02 +0000190 if (device & L_LOG) {
Rob Landley53437472006-07-16 08:14:35 +0000191 full_write(log_fd, msg, l);
Denis Vlasenko7a2ca5e2007-02-21 00:15:20 +0000192 if (log_fd == 2)
193 return; /* don't print dup messages */
Erik Andersene49d5ec2000-02-08 19:58:47 +0000194 }
Eric Andersen4c781471999-11-26 08:12:56 +0000195#endif
196
Denis Vlasenkoec27feb2007-02-17 15:52:02 +0000197 if (device & L_CONSOLE) {
Denis Vlasenko7a2ca5e2007-02-21 00:15:20 +0000198 /* Send console messages to console so people will see them. */
199 full_write(2, msg, l);
Eric Andersen3ae0c781999-11-04 01:13:21 +0000200 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000201}
202
Eric Andersen0460ff21999-10-25 23:32:44 +0000203/* Set terminal settings to reasonable defaults */
Bernhard Reutner-Fischer49e60b92006-05-29 12:57:52 +0000204static void set_term(void)
Eric Andersencc8ed391999-10-05 16:24:54 +0000205{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000206 struct termios tty;
Eric Andersencc8ed391999-10-05 16:24:54 +0000207
Bernhard Reutner-Fischer49e60b92006-05-29 12:57:52 +0000208 tcgetattr(STDIN_FILENO, &tty);
Eric Andersencc8ed391999-10-05 16:24:54 +0000209
Erik Andersene49d5ec2000-02-08 19:58:47 +0000210 /* set control chars */
Glenn L McGrathbaf55a82002-08-22 18:22:10 +0000211 tty.c_cc[VINTR] = 3; /* C-c */
212 tty.c_cc[VQUIT] = 28; /* C-\ */
213 tty.c_cc[VERASE] = 127; /* C-? */
214 tty.c_cc[VKILL] = 21; /* C-u */
215 tty.c_cc[VEOF] = 4; /* C-d */
Eric Andersen3849f9b2000-07-10 19:56:47 +0000216 tty.c_cc[VSTART] = 17; /* C-q */
Glenn L McGrathbaf55a82002-08-22 18:22:10 +0000217 tty.c_cc[VSTOP] = 19; /* C-s */
218 tty.c_cc[VSUSP] = 26; /* C-z */
Eric Andersen02bc25b2000-07-06 21:29:32 +0000219
Erik Andersene49d5ec2000-02-08 19:58:47 +0000220 /* use line dicipline 0 */
221 tty.c_line = 0;
Eric Andersen7f1acfd1999-10-29 23:09:13 +0000222
Erik Andersene49d5ec2000-02-08 19:58:47 +0000223 /* Make it be sane */
Glenn L McGrathbaf55a82002-08-22 18:22:10 +0000224 tty.c_cflag &= CBAUD | CBAUDEX | CSIZE | CSTOPB | PARENB | PARODD;
225 tty.c_cflag |= CREAD | HUPCL | CLOCAL;
Eric Andersend8862922001-04-23 15:14:11 +0000226
Erik Andersene49d5ec2000-02-08 19:58:47 +0000227 /* input modes */
228 tty.c_iflag = ICRNL | IXON | IXOFF;
Eric Andersen08b10341999-11-19 02:38:58 +0000229
Erik Andersene49d5ec2000-02-08 19:58:47 +0000230 /* output modes */
231 tty.c_oflag = OPOST | ONLCR;
Eric Andersen7f1acfd1999-10-29 23:09:13 +0000232
Erik Andersene49d5ec2000-02-08 19:58:47 +0000233 /* local modes */
234 tty.c_lflag =
235 ISIG | ICANON | ECHO | ECHOE | ECHOK | ECHOCTL | ECHOKE | IEXTEN;
Eric Andersencc8ed391999-10-05 16:24:54 +0000236
Bernhard Reutner-Fischer49e60b92006-05-29 12:57:52 +0000237 tcsetattr(STDIN_FILENO, TCSANOW, &tty);
Eric Andersencc8ed391999-10-05 16:24:54 +0000238}
239
Denis Vlasenko7a2ca5e2007-02-21 00:15:20 +0000240/* From <linux/serial.h> */
241struct serial_struct {
242 int type;
243 int line;
244 unsigned int port;
245 int irq;
246 int flags;
247 int xmit_fifo_size;
248 int custom_divisor;
249 int baud_base;
250 unsigned short close_delay;
251 char io_type;
252 char reserved_char[1];
253 int hub6;
254 unsigned short closing_wait; /* time to wait before closing */
255 unsigned short closing_wait2; /* no longer used... */
256 unsigned char *iomem_base;
257 unsigned short iomem_reg_shift;
258 unsigned int port_high;
259 unsigned long iomap_base; /* cookie passed into ioremap */
260 int reserved[1];
261 /* Paranoia (imagine 64bit kernel overwriting 32bit userspace stack) */
262 uint32_t bbox_reserved[16];
263};
Eric Andersen74400cc2001-10-18 04:11:39 +0000264static void console_init(void)
Eric Andersen0460ff21999-10-25 23:32:44 +0000265{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000266 struct serial_struct sr;
267 char *s;
Eric Andersen0460ff21999-10-25 23:32:44 +0000268
Denis Vlasenko7a2ca5e2007-02-21 00:15:20 +0000269 s = getenv("CONSOLE");
270 if (!s) s = getenv("console");
271 if (s) {
272 int fd = open(s, O_RDWR | O_NONBLOCK | O_NOCTTY);
273 if (fd >= 0) {
274 dup2(fd, 0);
275 dup2(fd, 1);
276 dup2(fd, 2);
277 while (fd > 2) close(fd--);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000278 }
Denis Vlasenko7a2ca5e2007-02-21 00:15:20 +0000279 messageD(L_LOG, "console='%s'", s);
Eric Andersen07e52971999-11-07 07:38:08 +0000280 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000281
Denis Vlasenko7a2ca5e2007-02-21 00:15:20 +0000282 s = getenv("TERM");
283 if (ioctl(0, TIOCGSERIAL, &sr) == 0) {
284 /* Force the TERM setting to vt102 for serial console --
285 * if TERM is set to linux (the default) */
286 if (!s || strcmp(s, "linux") == 0)
287 putenv((char*)"TERM=vt102");
Denis Vlasenko9b1381f2007-01-03 02:56:00 +0000288#if !ENABLE_SYSLOGD
Denis Vlasenko7a2ca5e2007-02-21 00:15:20 +0000289 log_console = NULL;
Glenn L McGrathb4a1baa2003-01-13 22:09:50 +0000290#endif
Denis Vlasenko7a2ca5e2007-02-21 00:15:20 +0000291 } else if (!s)
292 putenv((char*)"TERM=linux");
Eric Andersen0460ff21999-10-25 23:32:44 +0000293}
Glenn L McGrathbaf55a82002-08-22 18:22:10 +0000294
Denis Vlasenko06c0a712007-01-29 22:51:44 +0000295static void fixup_argv(int argc, char **argv, const char *new_argv0)
Eric Andersen7ef1a5b2001-03-20 17:39:08 +0000296{
297 int len;
Glenn L McGrathbaf55a82002-08-22 18:22:10 +0000298
Eric Andersen7ef1a5b2001-03-20 17:39:08 +0000299 /* Fix up argv[0] to be certain we claim to be init */
300 len = strlen(argv[0]);
Denis Vlasenko06c0a712007-01-29 22:51:44 +0000301 strncpy(argv[0], new_argv0, len);
Eric Andersen7ef1a5b2001-03-20 17:39:08 +0000302
303 /* Wipe argv[1]-argv[N] so they don't clutter the ps listing */
304 len = 1;
305 while (argc > len) {
306 memset(argv[len], 0, strlen(argv[len]));
307 len++;
308 }
309}
310
Bernhard Reutner-Fischer0da069d2006-05-29 12:54:16 +0000311/* Open the new terminal device */
Denis Vlasenko7a2ca5e2007-02-21 00:15:20 +0000312static void open_stdio_to_tty(const char* tty_name, int fail)
313{
314 /* empty tty_name means "use init's tty", else... */
315 if (tty_name[0]) {
316 close(0);
317 if ((device_open(tty_name, O_RDWR)) < 0) {
318 dup2(1, 0); /* restore fd #0 - avoid nasty surprises */
319 message(L_LOG | L_CONSOLE, "can't open %s: %s",
320 tty_name, strerror(errno));
321 if (fail)
322 _exit(1);
Bernhard Reutner-Fischercf1f2ac2006-06-02 10:43:17 +0000323#if !ENABLE_DEBUG_INIT
Denis Vlasenko7a2ca5e2007-02-21 00:15:20 +0000324 shutdown_signal(SIGUSR1);
Bernhard Reutner-Fischercf1f2ac2006-06-02 10:43:17 +0000325#else
Denis Vlasenko7a2ca5e2007-02-21 00:15:20 +0000326 _exit(2);
Bernhard Reutner-Fischercf1f2ac2006-06-02 10:43:17 +0000327#endif
Denis Vlasenko7a2ca5e2007-02-21 00:15:20 +0000328 }
Bernhard Reutner-Fischer0da069d2006-05-29 12:54:16 +0000329 }
Denis Vlasenko7a2ca5e2007-02-21 00:15:20 +0000330 close(1);
331 close(2);
332 set_term();
333 dup(0);
334 dup(0);
Bernhard Reutner-Fischer0da069d2006-05-29 12:54:16 +0000335}
336
Glenn L McGrathb4a1baa2003-01-13 22:09:50 +0000337static pid_t run(const struct init_action *a)
Eric Andersen0298be82002-03-05 15:12:19 +0000338{
Bernhard Reutner-Fischer3ab30802006-05-30 12:10:29 +0000339 int i;
Mike Frysinger10427ab2005-07-06 05:00:48 +0000340 pid_t pid;
Denis Vlasenko06c0a712007-01-29 22:51:44 +0000341 char *s, *tmpCmd, *cmdpath;
342 char *cmd[INIT_BUFFS_SIZE];
Glenn L McGrathbaf55a82002-08-22 18:22:10 +0000343 char buf[INIT_BUFFS_SIZE + 6]; /* INIT_BUFFS_SIZE+strlen("exec ")+1 */
Eric Andersen0298be82002-03-05 15:12:19 +0000344 sigset_t nmask, omask;
Eric Andersen0298be82002-03-05 15:12:19 +0000345 static const char press_enter[] =
346#ifdef CUSTOMIZED_BANNER
347#include CUSTOMIZED_BANNER
348#endif
349 "\nPlease press Enter to activate this console. ";
Erik Andersenac6e71f2000-01-08 22:04:33 +0000350
Eric Andersen0298be82002-03-05 15:12:19 +0000351 /* Block sigchild while forking. */
352 sigemptyset(&nmask);
353 sigaddset(&nmask, SIGCHLD);
354 sigprocmask(SIG_BLOCK, &nmask, &omask);
355
Glenn L McGrathbaf55a82002-08-22 18:22:10 +0000356 if ((pid = fork()) == 0) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000357 /* Clean up */
Eric Andersen0298be82002-03-05 15:12:19 +0000358 sigprocmask(SIG_SETMASK, &omask, NULL);
359
Eric Andersen0298be82002-03-05 15:12:19 +0000360 /* Reset signal handlers that were set by the parent process */
Erik Andersene49d5ec2000-02-08 19:58:47 +0000361 signal(SIGUSR1, SIG_DFL);
362 signal(SIGUSR2, SIG_DFL);
Glenn L McGrathbaf55a82002-08-22 18:22:10 +0000363 signal(SIGINT, SIG_DFL);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000364 signal(SIGTERM, SIG_DFL);
Glenn L McGrathbaf55a82002-08-22 18:22:10 +0000365 signal(SIGHUP, SIG_DFL);
Mike Frysingera77b4f32005-04-16 08:21:34 +0000366 signal(SIGQUIT, SIG_DFL);
Eric Andersened8a9be2001-11-30 19:10:58 +0000367 signal(SIGCONT, SIG_DFL);
368 signal(SIGSTOP, SIG_DFL);
369 signal(SIGTSTP, SIG_DFL);
Eric Andersen2f6c04f1999-11-01 23:59:44 +0000370
Eric Andersen599e3ce2002-07-03 11:08:10 +0000371 /* Create a new session and make ourself the process
Eric Andersene8a90fb2002-10-12 04:05:48 +0000372 * group leader */
373 setsid();
Eric Andersen599e3ce2002-07-03 11:08:10 +0000374
Eric Andersen0298be82002-03-05 15:12:19 +0000375 /* Open the new terminal device */
Denis Vlasenko7a2ca5e2007-02-21 00:15:20 +0000376 open_stdio_to_tty(a->terminal, 1);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000377
Eric Andersen599e3ce2002-07-03 11:08:10 +0000378 /* If the init Action requires us to wait, then force the
Eric Andersen0298be82002-03-05 15:12:19 +0000379 * supplied terminal to be the controlling tty. */
Glenn L McGrathbaf55a82002-08-22 18:22:10 +0000380 if (a->action & (SYSINIT | WAIT | CTRLALTDEL | SHUTDOWN | RESTART)) {
Eric Andersen0298be82002-03-05 15:12:19 +0000381
382 /* Now fork off another process to just hang around */
383 if ((pid = fork()) < 0) {
Denis Vlasenko7a2ca5e2007-02-21 00:15:20 +0000384 message(L_LOG | L_CONSOLE, "can't fork");
Eric Andersen0298be82002-03-05 15:12:19 +0000385 _exit(1);
386 }
387
388 if (pid > 0) {
389
390 /* We are the parent -- wait till the child is done */
391 signal(SIGINT, SIG_IGN);
392 signal(SIGTSTP, SIG_IGN);
393 signal(SIGQUIT, SIG_IGN);
394 signal(SIGCHLD, SIG_DFL);
395
Bernhard Reutner-Fischer3ab30802006-05-30 12:10:29 +0000396 waitfor(NULL, pid);
Eric Andersen0298be82002-03-05 15:12:19 +0000397 /* See if stealing the controlling tty back is necessary */
Bernhard Reutner-Fischer3ab30802006-05-30 12:10:29 +0000398 if (tcgetpgrp(0) != getpid())
Eric Andersen0298be82002-03-05 15:12:19 +0000399 _exit(0);
400
401 /* Use a temporary process to steal the controlling tty. */
402 if ((pid = fork()) < 0) {
Denis Vlasenko7a2ca5e2007-02-21 00:15:20 +0000403 message(L_LOG | L_CONSOLE, "can't fork");
Eric Andersen0298be82002-03-05 15:12:19 +0000404 _exit(1);
Glenn L McGrathbaf55a82002-08-22 18:22:10 +0000405 }
Eric Andersen0298be82002-03-05 15:12:19 +0000406 if (pid == 0) {
407 setsid();
Eric Andersen0826b6b2002-07-03 23:50:16 +0000408 ioctl(0, TIOCSCTTY, 1);
Eric Andersen0298be82002-03-05 15:12:19 +0000409 _exit(0);
Glenn L McGrathbaf55a82002-08-22 18:22:10 +0000410 }
Bernhard Reutner-Fischer3ab30802006-05-30 12:10:29 +0000411 waitfor(NULL, pid);
Eric Andersen0298be82002-03-05 15:12:19 +0000412 _exit(0);
413 }
414
415 /* Now fall though to actually execute things */
Eric Andersen0298be82002-03-05 15:12:19 +0000416 }
417
Erik Andersene132f4b2000-02-09 04:16:43 +0000418 /* See if any special /bin/sh requiring characters are present */
Eric Andersen0298be82002-03-05 15:12:19 +0000419 if (strpbrk(a->command, "~`!$^&*()=|\\{}[];\"'<>?") != NULL) {
Glenn L McGrathdc4e75e2003-09-02 02:36:18 +0000420 cmd[0] = (char *)DEFAULT_SHELL;
Denis Vlasenko06c0a712007-01-29 22:51:44 +0000421 cmd[1] = (char*)"-c";
Glenn L McGrathb4a1baa2003-01-13 22:09:50 +0000422 cmd[2] = strcat(strcpy(buf, "exec "), a->command);
Erik Andersene132f4b2000-02-09 04:16:43 +0000423 cmd[3] = NULL;
424 } else {
425 /* Convert command (char*) into cmd (char**, one word per string) */
Eric Andersen0826b6b2002-07-03 23:50:16 +0000426 strcpy(buf, a->command);
Eric Andersen72f9a422001-10-28 05:12:20 +0000427 s = buf;
Glenn L McGrathbaf55a82002-08-22 18:22:10 +0000428 for (tmpCmd = buf, i = 0; (tmpCmd = strsep(&s, " \t")) != NULL;) {
Erik Andersene132f4b2000-02-09 04:16:43 +0000429 if (*tmpCmd != '\0') {
430 cmd[i] = tmpCmd;
Erik Andersene132f4b2000-02-09 04:16:43 +0000431 i++;
432 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000433 }
Erik Andersene132f4b2000-02-09 04:16:43 +0000434 cmd[i] = NULL;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000435 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000436
Eric Andersen7ef1a5b2001-03-20 17:39:08 +0000437 cmdpath = cmd[0];
Eric Andersen7e3bf6e2000-09-14 22:01:31 +0000438
Eric Andersen7ef1a5b2001-03-20 17:39:08 +0000439 /*
Denis Vlasenko7a2ca5e2007-02-21 00:15:20 +0000440 * Interactive shells want to see a dash in argv[0]. This
441 * typically is handled by login, argv will be setup this
442 * way if a dash appears at the front of the command path
443 * (like "-/bin/sh").
Eric Andersen7ef1a5b2001-03-20 17:39:08 +0000444 */
Eric Andersen7ef1a5b2001-03-20 17:39:08 +0000445 if (*cmdpath == '-') {
Eric Andersen7ef1a5b2001-03-20 17:39:08 +0000446 /* skip over the dash */
447 ++cmdpath;
Eric Andersen7e3bf6e2000-09-14 22:01:31 +0000448
Eric Andersen7ef1a5b2001-03-20 17:39:08 +0000449 /* find the last component in the command pathname */
Manuel Novoa III cad53642003-03-19 09:13:01 +0000450 s = bb_get_last_path_component(cmdpath);
Eric Andersen7e3bf6e2000-09-14 22:01:31 +0000451
Eric Andersen7ef1a5b2001-03-20 17:39:08 +0000452 /* make a new argv[0] */
Glenn L McGrathbaf55a82002-08-22 18:22:10 +0000453 if ((cmd[0] = malloc(strlen(s) + 2)) == NULL) {
Denis Vlasenkoec27feb2007-02-17 15:52:02 +0000454 message(L_LOG | L_CONSOLE, bb_msg_memory_exhausted);
Eric Andersen7ef1a5b2001-03-20 17:39:08 +0000455 cmd[0] = cmdpath;
456 } else {
457 cmd[0][0] = '-';
Glenn L McGrathbaf55a82002-08-22 18:22:10 +0000458 strcpy(cmd[0] + 1, s);
Eric Andersen7ef1a5b2001-03-20 17:39:08 +0000459 }
Denis Vlasenko9b1381f2007-01-03 02:56:00 +0000460#if ENABLE_FEATURE_INIT_SCTTY
Paul Fox41a72ec2005-08-01 16:43:13 +0000461 /* Establish this process as session leader and
462 * (attempt) to make the tty (if any) a controlling tty.
463 */
Denis Vlasenko7a2ca5e2007-02-21 00:15:20 +0000464 setsid();
465 ioctl(0, TIOCSCTTY, 0 /*don't steal it*/);
Paul Fox41a72ec2005-08-01 16:43:13 +0000466#endif
Eric Andersen7ef1a5b2001-03-20 17:39:08 +0000467 }
468
Eric Andersen1f50e842004-08-16 09:29:42 +0000469#if !defined(__UCLIBC__) || defined(__ARCH_HAS_MMU__)
Eric Andersen0298be82002-03-05 15:12:19 +0000470 if (a->action & ASKFIRST) {
Glenn L McGrathb4a1baa2003-01-13 22:09:50 +0000471 char c;
Eric Andersen7ef1a5b2001-03-20 17:39:08 +0000472 /*
473 * Save memory by not exec-ing anything large (like a shell)
474 * before the user wants it. This is critical if swap is not
475 * enabled and the system has low memory. Generally this will
476 * be run on the second virtual console, and the first will
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000477 * be allowed to start a shell or whatever an init script
Eric Andersen7ef1a5b2001-03-20 17:39:08 +0000478 * specifies.
479 */
Denis Vlasenkoec27feb2007-02-17 15:52:02 +0000480 messageD(L_LOG, "Waiting for enter to start '%s'"
Denis Vlasenko7a2ca5e2007-02-21 00:15:20 +0000481 "(pid %d, tty '%s')\n",
Glenn L McGrathbaf55a82002-08-22 18:22:10 +0000482 cmdpath, getpid(), a->terminal);
Rob Landley53437472006-07-16 08:14:35 +0000483 full_write(1, press_enter, sizeof(press_enter) - 1);
Denis Vlasenkobf0a2012006-12-26 10:42:51 +0000484 while (read(0, &c, 1) == 1 && c != '\n')
Glenn L McGrathb4a1baa2003-01-13 22:09:50 +0000485 ;
Eric Andersen7ef1a5b2001-03-20 17:39:08 +0000486 }
Eric Andersen1f50e842004-08-16 09:29:42 +0000487#endif
Eric Andersen7ef1a5b2001-03-20 17:39:08 +0000488
Eric Andersen7ef1a5b2001-03-20 17:39:08 +0000489 /* Log the process name and args */
Denis Vlasenko7a2ca5e2007-02-21 00:15:20 +0000490 message(L_LOG, "starting pid %d, tty '%s': '%s'",
Glenn L McGrathbaf55a82002-08-22 18:22:10 +0000491 getpid(), a->terminal, cmdpath);
Eric Andersen7e3bf6e2000-09-14 22:01:31 +0000492
Denis Vlasenko9b1381f2007-01-03 02:56:00 +0000493#if ENABLE_FEATURE_INIT_COREDUMPS
Bernhard Reutner-Fischer0da069d2006-05-29 12:54:16 +0000494 {
495 struct stat sb;
496 if (stat(CORE_ENABLE_FLAG_FILE, &sb) == 0) {
497 struct rlimit limit;
Glenn L McGrathbaf55a82002-08-22 18:22:10 +0000498
Bernhard Reutner-Fischer0da069d2006-05-29 12:54:16 +0000499 limit.rlim_cur = RLIM_INFINITY;
500 limit.rlim_max = RLIM_INFINITY;
501 setrlimit(RLIMIT_CORE, &limit);
502 }
Erik Andersen983b51b2000-04-04 18:14:25 +0000503 }
Erik Andersen183da4a2000-04-04 18:36:37 +0000504#endif
Erik Andersen983b51b2000-04-04 18:14:25 +0000505
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000506 /* Now run it. The new program will take over this PID,
Erik Andersene49d5ec2000-02-08 19:58:47 +0000507 * so nothing further in init.c should be run. */
Denis Vlasenko4921b542007-02-03 02:17:41 +0000508 BB_EXECVP(cmdpath, cmd);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000509
Eric Andersen7ef1a5b2001-03-20 17:39:08 +0000510 /* We're still here? Some error happened. */
Denis Vlasenko7a2ca5e2007-02-21 00:15:20 +0000511 message(L_LOG | L_CONSOLE, "Cannot run '%s': %s",
512 cmdpath, strerror(errno));
Eric Andersen0298be82002-03-05 15:12:19 +0000513 _exit(-1);
Erik Andersen05df2392000-01-13 04:43:48 +0000514 }
Eric Andersen0298be82002-03-05 15:12:19 +0000515 sigprocmask(SIG_SETMASK, &omask, NULL);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000516 return pid;
Eric Andersen0460ff21999-10-25 23:32:44 +0000517}
518
Bernhard Reutner-Fischer3ab30802006-05-30 12:10:29 +0000519static int waitfor(const struct init_action *a, pid_t pid)
Erik Andersen0e3782f2000-01-07 02:54:55 +0000520{
Bernhard Reutner-Fischer3ab30802006-05-30 12:10:29 +0000521 int runpid;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000522 int status, wpid;
Erik Andersen0e3782f2000-01-07 02:54:55 +0000523
Bernhard Reutner-Fischer3ab30802006-05-30 12:10:29 +0000524 runpid = (NULL == a)? pid : run(a);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000525 while (1) {
Bernhard Reutner-Fischerd818dcc2007-02-16 17:17:07 +0000526 wpid = waitpid(runpid, &status, 0);
Bernhard Reutner-Fischer3ab30802006-05-30 12:10:29 +0000527 if (wpid == runpid)
Erik Andersene49d5ec2000-02-08 19:58:47 +0000528 break;
Glenn L McGrathe6ba16f2003-09-26 10:45:55 +0000529 if (wpid == -1 && errno == ECHILD) {
530 /* we missed its termination */
531 break;
532 }
533 /* FIXME other errors should maybe trigger an error, but allow
534 * the program to continue */
Erik Andersen0e3782f2000-01-07 02:54:55 +0000535 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000536 return wpid;
Erik Andersen0e3782f2000-01-07 02:54:55 +0000537}
538
Eric Andersen0298be82002-03-05 15:12:19 +0000539/* Run all commands of a particular type */
540static void run_actions(int action)
Eric Andersen219d6f51999-11-02 19:43:01 +0000541{
Eric Andersen0298be82002-03-05 15:12:19 +0000542 struct init_action *a, *tmp;
Eric Andersen219d6f51999-11-02 19:43:01 +0000543
Eric Andersen0298be82002-03-05 15:12:19 +0000544 for (a = init_action_list; a; a = tmp) {
545 tmp = a->next;
Eric Andersenc97ec342001-04-03 18:01:51 +0000546 if (a->action == action) {
Denis Vlasenko7a2ca5e2007-02-21 00:15:20 +0000547 /* a->terminal of "" means "init's console" */
548 if (a->terminal[0] && access(a->terminal, R_OK | W_OK)) {
Rob Landley2dd42792006-03-22 17:39:13 +0000549 delete_init_action(a);
550 } else if (a->action & (SYSINIT | WAIT | CTRLALTDEL | SHUTDOWN | RESTART)) {
Bernhard Reutner-Fischer3ab30802006-05-30 12:10:29 +0000551 waitfor(a, 0);
Eric Andersen0298be82002-03-05 15:12:19 +0000552 delete_init_action(a);
553 } else if (a->action & ONCE) {
554 run(a);
555 delete_init_action(a);
Glenn L McGrathbaf55a82002-08-22 18:22:10 +0000556 } else if (a->action & (RESPAWN | ASKFIRST)) {
Eric Andersen0298be82002-03-05 15:12:19 +0000557 /* Only run stuff with pid==0. If they have
558 * a pid, that means it is still running */
559 if (a->pid == 0) {
560 a->pid = run(a);
561 }
562 }
Erik Andersene132f4b2000-02-09 04:16:43 +0000563 }
564 }
565}
566
Bernhard Reutner-Fischer35e1a072006-05-29 13:08:35 +0000567#if !ENABLE_DEBUG_INIT
Eric Andersen2c1de612003-04-24 11:41:28 +0000568static void init_reboot(unsigned long magic)
569{
570 pid_t pid;
571 /* We have to fork here, since the kernel calls do_exit(0) in
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000572 * linux/kernel/sys.c, which can cause the machine to panic when
Eric Andersen2c1de612003-04-24 11:41:28 +0000573 * the init process is killed.... */
Denis Vlasenko9b1381f2007-01-03 02:56:00 +0000574 pid = vfork();
575 if (pid == 0) { /* child */
Eric Andersen2c1de612003-04-24 11:41:28 +0000576 reboot(magic);
Eric Andersen2c1de612003-04-24 11:41:28 +0000577 _exit(0);
578 }
Denis Vlasenko9b1381f2007-01-03 02:56:00 +0000579 waitpid(pid, NULL, 0);
Eric Andersen2c1de612003-04-24 11:41:28 +0000580}
581
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000582static void shutdown_system(void)
Eric Andersencc8ed391999-10-05 16:24:54 +0000583{
Eric Andersen813d88c2001-10-28 22:49:48 +0000584 sigset_t block_signals;
Erik Andersene132f4b2000-02-09 04:16:43 +0000585
Eric Andersena9cc8962002-09-16 06:49:06 +0000586 /* run everything to be run at "shutdown". This is done _prior_
587 * to killing everything, in case people wish to use scripts to
588 * shut things down gracefully... */
589 run_actions(SHUTDOWN);
590
Eric Andersen72f9a422001-10-28 05:12:20 +0000591 /* first disable all our signals */
592 sigemptyset(&block_signals);
593 sigaddset(&block_signals, SIGHUP);
Mike Frysingera77b4f32005-04-16 08:21:34 +0000594 sigaddset(&block_signals, SIGQUIT);
Eric Andersen72f9a422001-10-28 05:12:20 +0000595 sigaddset(&block_signals, SIGCHLD);
596 sigaddset(&block_signals, SIGUSR1);
597 sigaddset(&block_signals, SIGUSR2);
598 sigaddset(&block_signals, SIGINT);
599 sigaddset(&block_signals, SIGTERM);
Eric Andersened8a9be2001-11-30 19:10:58 +0000600 sigaddset(&block_signals, SIGCONT);
601 sigaddset(&block_signals, SIGSTOP);
602 sigaddset(&block_signals, SIGTSTP);
Eric Andersen72f9a422001-10-28 05:12:20 +0000603 sigprocmask(SIG_BLOCK, &block_signals, NULL);
Erik Andersen7dc16072000-01-04 01:10:25 +0000604
Denis Vlasenko7a2ca5e2007-02-21 00:15:20 +0000605 message(L_CONSOLE | L_LOG, "The system is going down NOW!");
606
Erik Andersene49d5ec2000-02-08 19:58:47 +0000607 /* Allow Ctrl-Alt-Del to reboot system. */
Erik Andersen4f3f7572000-04-28 00:18:56 +0000608 init_reboot(RB_ENABLE_CAD);
Erik Andersene132f4b2000-02-09 04:16:43 +0000609
Erik Andersene49d5ec2000-02-08 19:58:47 +0000610 /* Send signals to every process _except_ pid 1 */
Denis Vlasenko7a2ca5e2007-02-21 00:15:20 +0000611 message(L_CONSOLE | L_LOG, "Sending SIG%s to all processes", "TERM");
Erik Andersene49d5ec2000-02-08 19:58:47 +0000612 kill(-1, SIGTERM);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000613 sync();
Denis Vlasenko7a2ca5e2007-02-21 00:15:20 +0000614 sleep(1);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000615
Denis Vlasenko7a2ca5e2007-02-21 00:15:20 +0000616 message(L_CONSOLE | L_LOG, "Sending SIG%s to all processes", "KILL");
Erik Andersene49d5ec2000-02-08 19:58:47 +0000617 kill(-1, SIGKILL);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000618 sync();
Denis Vlasenko7a2ca5e2007-02-21 00:15:20 +0000619 sleep(1);
Eric Andersencc8ed391999-10-05 16:24:54 +0000620}
621
"Vladimir N. Oleynik"1f04c9d2006-02-01 14:47:52 +0000622static void exec_signal(int sig ATTRIBUTE_UNUSED)
Eric Andersen730f8262001-12-17 23:13:08 +0000623{
Eric Andersen0298be82002-03-05 15:12:19 +0000624 struct init_action *a, *tmp;
Eric Andersen5222d312002-07-03 05:15:23 +0000625 sigset_t unblock_signals;
Glenn L McGrathbaf55a82002-08-22 18:22:10 +0000626
Eric Andersen0298be82002-03-05 15:12:19 +0000627 for (a = init_action_list; a; a = tmp) {
628 tmp = a->next;
629 if (a->action & RESTART) {
Eric Andersen730f8262001-12-17 23:13:08 +0000630 shutdown_system();
Eric Andersen5222d312002-07-03 05:15:23 +0000631
632 /* unblock all signals, blocked in shutdown_system() */
633 sigemptyset(&unblock_signals);
634 sigaddset(&unblock_signals, SIGHUP);
Mike Frysingera77b4f32005-04-16 08:21:34 +0000635 sigaddset(&unblock_signals, SIGQUIT);
Eric Andersen5222d312002-07-03 05:15:23 +0000636 sigaddset(&unblock_signals, SIGCHLD);
637 sigaddset(&unblock_signals, SIGUSR1);
638 sigaddset(&unblock_signals, SIGUSR2);
639 sigaddset(&unblock_signals, SIGINT);
640 sigaddset(&unblock_signals, SIGTERM);
641 sigaddset(&unblock_signals, SIGCONT);
642 sigaddset(&unblock_signals, SIGSTOP);
643 sigaddset(&unblock_signals, SIGTSTP);
644 sigprocmask(SIG_UNBLOCK, &unblock_signals, NULL);
645
Eric Andersen3c8064f2003-07-05 08:29:01 +0000646 /* Open the new terminal device */
Denis Vlasenko7a2ca5e2007-02-21 00:15:20 +0000647 open_stdio_to_tty(a->terminal, 0);
Eric Andersen3c8064f2003-07-05 08:29:01 +0000648
Denis Vlasenkoec27feb2007-02-17 15:52:02 +0000649 messageD(L_CONSOLE | L_LOG, "Trying to re-exec %s", a->command);
Denis Vlasenko4921b542007-02-03 02:17:41 +0000650 BB_EXECLP(a->command, a->command, NULL);
Glenn L McGrathbaf55a82002-08-22 18:22:10 +0000651
Denis Vlasenko7a2ca5e2007-02-21 00:15:20 +0000652 message(L_CONSOLE | L_LOG, "Cannot run '%s': %s",
653 a->command, strerror(errno));
Eric Andersen730f8262001-12-17 23:13:08 +0000654 sleep(2);
655 init_reboot(RB_HALT_SYSTEM);
656 loop_forever();
657 }
658 }
659}
660
Paul Foxd112f8f2006-06-01 13:17:49 +0000661static void shutdown_signal(int sig)
Eric Andersencc8ed391999-10-05 16:24:54 +0000662{
Denis Vlasenkoe968fcd2007-02-03 02:42:47 +0000663 const char *m;
Paul Foxd112f8f2006-06-01 13:17:49 +0000664 int rb;
665
Erik Andersene49d5ec2000-02-08 19:58:47 +0000666 shutdown_system();
Paul Foxd112f8f2006-06-01 13:17:49 +0000667
Denis Vlasenkoe968fcd2007-02-03 02:42:47 +0000668 m = "halt";
669 rb = RB_HALT_SYSTEM;
Paul Foxd112f8f2006-06-01 13:17:49 +0000670 if (sig == SIGTERM) {
671 m = "reboot";
672 rb = RB_AUTOBOOT;
673 } else if (sig == SIGUSR2) {
674 m = "poweroff";
675 rb = RB_POWER_OFF;
Paul Foxd112f8f2006-06-01 13:17:49 +0000676 }
Denis Vlasenkoec27feb2007-02-17 15:52:02 +0000677 message(L_CONSOLE | L_LOG, "Requesting system %s", m);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000678 /* allow time for last message to reach serial console */
Erik Andersene132f4b2000-02-09 04:16:43 +0000679 sleep(2);
Paul Foxd112f8f2006-06-01 13:17:49 +0000680 init_reboot(rb);
Matt Kraai67a46402001-06-03 05:55:52 +0000681 loop_forever();
Eric Andersencc8ed391999-10-05 16:24:54 +0000682}
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000683
"Vladimir N. Oleynik"1f04c9d2006-02-01 14:47:52 +0000684static void ctrlaltdel_signal(int sig ATTRIBUTE_UNUSED)
Eric Andersenc97ec342001-04-03 18:01:51 +0000685{
686 run_actions(CTRLALTDEL);
687}
688
Eric Andersen0298be82002-03-05 15:12:19 +0000689/* The SIGSTOP & SIGTSTP handler */
"Vladimir N. Oleynik"1f04c9d2006-02-01 14:47:52 +0000690static void stop_handler(int sig ATTRIBUTE_UNUSED)
Eric Andersened8a9be2001-11-30 19:10:58 +0000691{
Eric Andersen0298be82002-03-05 15:12:19 +0000692 int saved_errno = errno;
Eric Andersened8a9be2001-11-30 19:10:58 +0000693
694 got_cont = 0;
Glenn L McGrathbaf55a82002-08-22 18:22:10 +0000695 while (!got_cont)
696 pause();
Eric Andersened8a9be2001-11-30 19:10:58 +0000697 got_cont = 0;
698 errno = saved_errno;
699}
700
Glenn L McGrathbaf55a82002-08-22 18:22:10 +0000701/* The SIGCONT handler */
"Vladimir N. Oleynik"1f04c9d2006-02-01 14:47:52 +0000702static void cont_handler(int sig ATTRIBUTE_UNUSED)
Eric Andersened8a9be2001-11-30 19:10:58 +0000703{
704 got_cont = 1;
705}
706
Denis Vlasenkoe968fcd2007-02-03 02:42:47 +0000707#endif /* !ENABLE_DEBUG_INIT */
Erik Andersende552872000-01-23 01:34:05 +0000708
Glenn L McGrathdc4e75e2003-09-02 02:36:18 +0000709static void new_init_action(int action, const char *command, const char *cons)
Erik Andersen7dc16072000-01-04 01:10:25 +0000710{
Eric Andersen22718092004-10-08 08:17:39 +0000711 struct init_action *new_action, *a, *last;
Erik Andersen9e737252000-01-06 01:16:13 +0000712
"Vladimir N. Oleynik"6c35c7c2005-10-12 15:34:25 +0000713 if (strcmp(cons, bb_dev_null) == 0 && (action & ASKFIRST))
Eric Andersen1644db92001-09-05 20:18:15 +0000714 return;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000715
Eric Andersen0298be82002-03-05 15:12:19 +0000716 /* Append to the end of the list */
Eric Andersen22718092004-10-08 08:17:39 +0000717 for (a = last = init_action_list; a; a = a->next) {
Eric Andersen82baf632004-10-08 08:21:54 +0000718 /* don't enter action if it's already in the list,
719 * but do overwrite existing actions */
Denis Vlasenkoec27feb2007-02-17 15:52:02 +0000720 if ((strcmp(a->command, command) == 0)
721 && (strcmp(a->terminal, cons) == 0)
722 ) {
Eric Andersen82baf632004-10-08 08:21:54 +0000723 a->action = action;
Eric Andersen6fd0e312003-07-22 09:48:56 +0000724 return;
725 }
Eric Andersen22718092004-10-08 08:17:39 +0000726 last = a;
Eric Andersen6fd0e312003-07-22 09:48:56 +0000727 }
Denis Vlasenko7a2ca5e2007-02-21 00:15:20 +0000728
729 new_action = xzalloc(sizeof(struct init_action));
Eric Andersen22718092004-10-08 08:17:39 +0000730 if (last) {
731 last->next = new_action;
Eric Andersen1644db92001-09-05 20:18:15 +0000732 } else {
Eric Andersen0298be82002-03-05 15:12:19 +0000733 init_action_list = new_action;
Eric Andersen1644db92001-09-05 20:18:15 +0000734 }
Eric Andersen0826b6b2002-07-03 23:50:16 +0000735 strcpy(new_action->command, command);
Eric Andersen0298be82002-03-05 15:12:19 +0000736 new_action->action = action;
Eric Andersen0826b6b2002-07-03 23:50:16 +0000737 strcpy(new_action->terminal, cons);
Denis Vlasenko7a2ca5e2007-02-21 00:15:20 +0000738 messageD(L_LOG | L_CONSOLE, "command='%s' action=%d tty='%s'\n",
Glenn L McGrathb4a1baa2003-01-13 22:09:50 +0000739 new_action->command, new_action->action, new_action->terminal);
Erik Andersen7dc16072000-01-04 01:10:25 +0000740}
741
Glenn L McGrathbaf55a82002-08-22 18:22:10 +0000742static void delete_init_action(struct init_action *action)
Erik Andersen7dc16072000-01-04 01:10:25 +0000743{
Eric Andersen0298be82002-03-05 15:12:19 +0000744 struct init_action *a, *b = NULL;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000745
Eric Andersen0298be82002-03-05 15:12:19 +0000746 for (a = init_action_list; a; b = a, a = a->next) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000747 if (a == action) {
748 if (b == NULL) {
Eric Andersen0298be82002-03-05 15:12:19 +0000749 init_action_list = a->next;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000750 } else {
Eric Andersen0298be82002-03-05 15:12:19 +0000751 b->next = a->next;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000752 }
753 free(a);
754 break;
755 }
Erik Andersen7dc16072000-01-04 01:10:25 +0000756 }
Erik Andersen7dc16072000-01-04 01:10:25 +0000757}
758
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000759/* NOTE that if CONFIG_FEATURE_USE_INITTAB is NOT defined,
Erik Andersen9e737252000-01-06 01:16:13 +0000760 * then parse_inittab() simply adds in some default
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000761 * actions(i.e., runs INIT_SCRIPT and then starts a pair
762 * of "askfirst" shells). If CONFIG_FEATURE_USE_INITTAB
763 * _is_ defined, but /etc/inittab is missing, this
Erik Andersen812d4662000-01-07 18:30:40 +0000764 * results in the same set of default behaviors.
Glenn L McGrathbaf55a82002-08-22 18:22:10 +0000765 */
Eric Andersen3e6ff902001-03-09 21:24:12 +0000766static void parse_inittab(void)
Erik Andersen7dc16072000-01-04 01:10:25 +0000767{
Denis Vlasenko9b1381f2007-01-03 02:56:00 +0000768#if ENABLE_FEATURE_USE_INITTAB
Erik Andersene49d5ec2000-02-08 19:58:47 +0000769 FILE *file;
Glenn L McGrathb4a1baa2003-01-13 22:09:50 +0000770 char buf[INIT_BUFFS_SIZE], lineAsRead[INIT_BUFFS_SIZE];
Denis Vlasenko7a2ca5e2007-02-21 00:15:20 +0000771 char tmpConsole[CONSOLE_NAME_SIZE];
Eric Andersen0298be82002-03-05 15:12:19 +0000772 char *id, *runlev, *action, *command, *eol;
773 const struct init_action_type *a = actions;
Erik Andersen7dc16072000-01-04 01:10:25 +0000774
Erik Andersene49d5ec2000-02-08 19:58:47 +0000775 file = fopen(INITTAB, "r");
776 if (file == NULL) {
777 /* No inittab file -- set up some default behavior */
Erik Andersen9e737252000-01-06 01:16:13 +0000778#endif
Eric Andersenc97ec342001-04-03 18:01:51 +0000779 /* Reboot on Ctrl-Alt-Del */
Denis Vlasenko2f0c0d02007-01-21 00:41:04 +0000780 new_init_action(CTRLALTDEL, "reboot", "");
Eric Andersen1644db92001-09-05 20:18:15 +0000781 /* Umount all filesystems on halt/reboot */
Denis Vlasenko2f0c0d02007-01-21 00:41:04 +0000782 new_init_action(SHUTDOWN, "umount -a -r", "");
Eric Andersen1644db92001-09-05 20:18:15 +0000783 /* Swapoff on halt/reboot */
Bernhard Reutner-Fischerd818dcc2007-02-16 17:17:07 +0000784 if (ENABLE_SWAPONOFF) new_init_action(SHUTDOWN, "swapoff -a", "");
Eric Andersen730f8262001-12-17 23:13:08 +0000785 /* Prepare to restart init when a HUP is received */
Denis Vlasenko2f0c0d02007-01-21 00:41:04 +0000786 new_init_action(RESTART, "init", "");
Eric Andersen3bc2b202002-07-29 06:39:58 +0000787 /* Askfirst shell on tty1-4 */
Glenn L McGrathdc4e75e2003-09-02 02:36:18 +0000788 new_init_action(ASKFIRST, bb_default_login_shell, "");
789 new_init_action(ASKFIRST, bb_default_login_shell, VC_2);
790 new_init_action(ASKFIRST, bb_default_login_shell, VC_3);
791 new_init_action(ASKFIRST, bb_default_login_shell, VC_4);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000792 /* sysinit */
Glenn L McGrathb4a1baa2003-01-13 22:09:50 +0000793 new_init_action(SYSINIT, INIT_SCRIPT, "");
Erik Andersen7dc16072000-01-04 01:10:25 +0000794
Erik Andersene49d5ec2000-02-08 19:58:47 +0000795 return;
Denis Vlasenko9b1381f2007-01-03 02:56:00 +0000796#if ENABLE_FEATURE_USE_INITTAB
Erik Andersen9e737252000-01-06 01:16:13 +0000797 }
Erik Andersen7dc16072000-01-04 01:10:25 +0000798
Eric Andersen0826b6b2002-07-03 23:50:16 +0000799 while (fgets(buf, INIT_BUFFS_SIZE, file) != NULL) {
Eric Andersenb5966362000-05-31 20:04:38 +0000800 /* Skip leading spaces */
801 for (id = buf; *id == ' ' || *id == '\t'; id++);
802
803 /* Skip the line if it's a comment */
804 if (*id == '#' || *id == '\n')
Erik Andersene49d5ec2000-02-08 19:58:47 +0000805 continue;
Erik Andersen7dc16072000-01-04 01:10:25 +0000806
Erik Andersene49d5ec2000-02-08 19:58:47 +0000807 /* Trim the trailing \n */
Bernhard Reutner-Fischerd818dcc2007-02-16 17:17:07 +0000808 //XXX: chomp() ?
Eric Andersenb5966362000-05-31 20:04:38 +0000809 eol = strrchr(id, '\n');
810 if (eol != NULL)
811 *eol = '\0';
Erik Andersen7dc16072000-01-04 01:10:25 +0000812
Erik Andersene49d5ec2000-02-08 19:58:47 +0000813 /* Keep a copy around for posterity's sake (and error msgs) */
814 strcpy(lineAsRead, buf);
815
Eric Andersenb5966362000-05-31 20:04:38 +0000816 /* Separate the ID field from the runlevels */
817 runlev = strchr(id, ':');
818 if (runlev == NULL || *(runlev + 1) == '\0') {
Denis Vlasenkoec27feb2007-02-17 15:52:02 +0000819 message(L_LOG | L_CONSOLE, "Bad inittab entry: %s", lineAsRead);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000820 continue;
821 } else {
Eric Andersenb5966362000-05-31 20:04:38 +0000822 *runlev = '\0';
823 ++runlev;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000824 }
825
Eric Andersenb5966362000-05-31 20:04:38 +0000826 /* Separate the runlevels from the action */
827 action = strchr(runlev, ':');
828 if (action == NULL || *(action + 1) == '\0') {
Denis Vlasenkoec27feb2007-02-17 15:52:02 +0000829 message(L_LOG | L_CONSOLE, "Bad inittab entry: %s", lineAsRead);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000830 continue;
831 } else {
Eric Andersenb5966362000-05-31 20:04:38 +0000832 *action = '\0';
833 ++action;
834 }
835
Eric Andersen0298be82002-03-05 15:12:19 +0000836 /* Separate the action from the command */
837 command = strchr(action, ':');
838 if (command == NULL || *(command + 1) == '\0') {
Denis Vlasenkoec27feb2007-02-17 15:52:02 +0000839 message(L_LOG | L_CONSOLE, "Bad inittab entry: %s", lineAsRead);
Eric Andersenb5966362000-05-31 20:04:38 +0000840 continue;
841 } else {
Eric Andersen0298be82002-03-05 15:12:19 +0000842 *command = '\0';
843 ++command;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000844 }
845
846 /* Ok, now process it */
Glenn L McGrathb4a1baa2003-01-13 22:09:50 +0000847 for (a = actions; a->name != 0; a++) {
Eric Andersenb5966362000-05-31 20:04:38 +0000848 if (strcmp(a->name, action) == 0) {
849 if (*id != '\0') {
Glenn L McGrathb4a1baa2003-01-13 22:09:50 +0000850 if(strncmp(id, "/dev/", 5) == 0)
851 id += 5;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000852 strcpy(tmpConsole, "/dev/");
Glenn L McGrathb4a1baa2003-01-13 22:09:50 +0000853 safe_strncpy(tmpConsole + 5, id,
Denis Vlasenko7a2ca5e2007-02-21 00:15:20 +0000854 sizeof(tmpConsole) - 5);
Eric Andersenb5966362000-05-31 20:04:38 +0000855 id = tmpConsole;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000856 }
Eric Andersen0298be82002-03-05 15:12:19 +0000857 new_init_action(a->action, command, id);
Glenn L McGrathb4a1baa2003-01-13 22:09:50 +0000858 break;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000859 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000860 }
Glenn L McGrathb4a1baa2003-01-13 22:09:50 +0000861 if (a->name == 0) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000862 /* Choke on an unknown action */
Denis Vlasenkoec27feb2007-02-17 15:52:02 +0000863 message(L_LOG | L_CONSOLE, "Bad inittab entry: %s", lineAsRead);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000864 }
Erik Andersen7dc16072000-01-04 01:10:25 +0000865 }
Eric Andersend8636ca2002-05-15 22:19:09 +0000866 fclose(file);
Denis Vlasenko9b1381f2007-01-03 02:56:00 +0000867#endif /* FEATURE_USE_INITTAB */
Erik Andersen7dc16072000-01-04 01:10:25 +0000868}
869
Denis Vlasenko9b1381f2007-01-03 02:56:00 +0000870#if ENABLE_FEATURE_USE_INITTAB
"Vladimir N. Oleynik"1f04c9d2006-02-01 14:47:52 +0000871static void reload_signal(int sig ATTRIBUTE_UNUSED)
Eric Andersen6fd0e312003-07-22 09:48:56 +0000872{
Eric Andersen82baf632004-10-08 08:21:54 +0000873 struct init_action *a, *tmp;
874
Denis Vlasenko7a2ca5e2007-02-21 00:15:20 +0000875 message(L_LOG, "reloading /etc/inittab");
Eric Andersen82baf632004-10-08 08:21:54 +0000876
877 /* disable old entrys */
878 for (a = init_action_list; a; a = a->next ) {
879 a->action = ONCE;
880 }
881
882 parse_inittab();
883
884 /* remove unused entrys */
885 for (a = init_action_list; a; a = tmp) {
886 tmp = a->next;
887 if (a->action & (ONCE | SYSINIT | WAIT ) &&
888 a->pid == 0 ) {
889 delete_init_action(a);
890 }
891 }
Eric Andersen6fd0e312003-07-22 09:48:56 +0000892 run_actions(RESPAWN);
Eric Andersen6fd0e312003-07-22 09:48:56 +0000893}
Denis Vlasenko9b1381f2007-01-03 02:56:00 +0000894#endif /* FEATURE_USE_INITTAB */
Eric Andersen82baf632004-10-08 08:21:54 +0000895
Denis Vlasenko06af2162007-02-03 17:28:39 +0000896int init_main(int argc, char **argv);
Rob Landleydfba7412006-03-06 20:47:33 +0000897int init_main(int argc, char **argv)
Eric Andersen0460ff21999-10-25 23:32:44 +0000898{
Eric Andersen0298be82002-03-05 15:12:19 +0000899 struct init_action *a;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000900 pid_t wpid;
Eric Andersen0460ff21999-10-25 23:32:44 +0000901
Denis Vlasenko9b1381f2007-01-03 02:56:00 +0000902 die_sleep = 30 * 24*60*60; /* if xmalloc will ever die... */
903
Eric Andersen730f8262001-12-17 23:13:08 +0000904 if (argc > 1 && !strcmp(argv[1], "-q")) {
Denis Vlasenko7a2ca5e2007-02-21 00:15:20 +0000905 return kill(1, SIGHUP);
Eric Andersen730f8262001-12-17 23:13:08 +0000906 }
Bernhard Reutner-Fischer35e1a072006-05-29 13:08:35 +0000907#if !ENABLE_DEBUG_INIT
Erik Andersena51ecdd2000-02-24 18:09:58 +0000908 /* Expect to be invoked as init with PID=1 or be invoked as linuxrc */
Denis Vlasenko7a2ca5e2007-02-21 00:15:20 +0000909 if (getpid() != 1
910 && (!ENABLE_FEATURE_INITRD || !strstr(applet_name, "linuxrc"))
911 ) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000912 bb_show_usage();
Erik Andersene49d5ec2000-02-08 19:58:47 +0000913 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000914 /* Set up sig handlers -- be sure to
915 * clear all of these in run() */
Glenn L McGrathbaf55a82002-08-22 18:22:10 +0000916 signal(SIGHUP, exec_signal);
Mike Frysingera77b4f32005-04-16 08:21:34 +0000917 signal(SIGQUIT, exec_signal);
Paul Foxd112f8f2006-06-01 13:17:49 +0000918 signal(SIGUSR1, shutdown_signal);
919 signal(SIGUSR2, shutdown_signal);
Glenn L McGrathbaf55a82002-08-22 18:22:10 +0000920 signal(SIGINT, ctrlaltdel_signal);
Paul Foxd112f8f2006-06-01 13:17:49 +0000921 signal(SIGTERM, shutdown_signal);
Eric Andersened8a9be2001-11-30 19:10:58 +0000922 signal(SIGCONT, cont_handler);
923 signal(SIGSTOP, stop_handler);
924 signal(SIGTSTP, stop_handler);
Eric Andersen0460ff21999-10-25 23:32:44 +0000925
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000926 /* Turn off rebooting via CTL-ALT-DEL -- we get a
Erik Andersene49d5ec2000-02-08 19:58:47 +0000927 * SIGINT on CAD so we can shut things down gracefully... */
Erik Andersen4f3f7572000-04-28 00:18:56 +0000928 init_reboot(RB_DISABLE_CAD);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000929#endif
Erik Andersen05df2392000-01-13 04:43:48 +0000930
Eric Andersen599e3ce2002-07-03 11:08:10 +0000931 /* Figure out where the default console should be */
932 console_init();
Denis Vlasenko7a2ca5e2007-02-21 00:15:20 +0000933 set_term();
Erik Andersen983b51b2000-04-04 18:14:25 +0000934 chdir("/");
Erik Andersene49d5ec2000-02-08 19:58:47 +0000935 setsid();
Glenn L McGrathb4a1baa2003-01-13 22:09:50 +0000936 {
937 const char * const *e;
938 /* Make sure environs is set to something sane */
Denis Vlasenkobf0a2012006-12-26 10:42:51 +0000939 for (e = environment; *e; e++)
Manuel Novoa III cad53642003-03-19 09:13:01 +0000940 putenv((char *) *e);
Glenn L McGrathb4a1baa2003-01-13 22:09:50 +0000941 }
Rob Landleycba1b962006-07-09 17:28:17 +0000942
943 if (argc > 1) setenv("RUNLEVEL", argv[1], 1);
944
Erik Andersene49d5ec2000-02-08 19:58:47 +0000945 /* Hello world */
Denis Vlasenko7a2ca5e2007-02-21 00:15:20 +0000946 message(MAYBE_CONSOLE | L_LOG, "init started: %s", bb_msg_full_version);
Eric Andersencc8ed391999-10-05 16:24:54 +0000947
Erik Andersene49d5ec2000-02-08 19:58:47 +0000948 /* Make sure there is enough memory to do something useful. */
Rob Landleyc3386a42005-08-30 18:50:37 +0000949 if (ENABLE_SWAPONOFF) {
950 struct sysinfo info;
951
952 if (!sysinfo(&info) &&
Denis Vlasenkodca0b702006-10-27 09:05:02 +0000953 (info.mem_unit ? : 1) * (long long)info.totalram < 1024*1024)
Rob Landleyc3386a42005-08-30 18:50:37 +0000954 {
Denis Vlasenko7a2ca5e2007-02-21 00:15:20 +0000955 message(L_CONSOLE, "Low memory, forcing swapon");
Rob Landleyc3386a42005-08-30 18:50:37 +0000956 /* swapon -a requires /proc typically */
Denis Vlasenko2f0c0d02007-01-21 00:41:04 +0000957 new_init_action(SYSINIT, "mount -t proc proc /proc", "");
Rob Landleyc3386a42005-08-30 18:50:37 +0000958 /* Try to turn on swap */
Denis Vlasenko2f0c0d02007-01-21 00:41:04 +0000959 new_init_action(SYSINIT, "swapon -a", "");
Rob Landleyc3386a42005-08-30 18:50:37 +0000960 run_actions(SYSINIT); /* wait and removing */
961 }
962 }
Erik Andersen9e737252000-01-06 01:16:13 +0000963
Erik Andersene49d5ec2000-02-08 19:58:47 +0000964 /* Check if we are supposed to be in single user mode */
Denis Vlasenkobf66fbc2006-12-21 13:23:14 +0000965 if (argc > 1
966 && (!strcmp(argv[1], "single") || !strcmp(argv[1], "-s") || LONE_CHAR(argv[1], '1'))
967 ) {
Glenn L McGrathb4a1baa2003-01-13 22:09:50 +0000968 /* Start a shell on console */
Glenn L McGrathdc4e75e2003-09-02 02:36:18 +0000969 new_init_action(RESPAWN, bb_default_login_shell, "");
Erik Andersene49d5ec2000-02-08 19:58:47 +0000970 } else {
971 /* Not in single user mode -- see what inittab says */
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000972
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000973 /* NOTE that if CONFIG_FEATURE_USE_INITTAB is NOT defined,
Erik Andersene49d5ec2000-02-08 19:58:47 +0000974 * then parse_inittab() simply adds in some default
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000975 * actions(i.e., runs INIT_SCRIPT and then starts a pair
Erik Andersene49d5ec2000-02-08 19:58:47 +0000976 * of "askfirst" shells */
977 parse_inittab();
Eric Andersend00c2621999-12-07 08:37:31 +0000978 }
Erik Andersen983b51b2000-04-04 18:14:25 +0000979
Denis Vlasenko9b1381f2007-01-03 02:56:00 +0000980#if ENABLE_SELINUX
Rob Landleyb3ede5a2006-03-27 23:09:12 +0000981 if (getenv("SELINUX_INIT") == NULL) {
982 int enforce = 0;
983
984 putenv("SELINUX_INIT=YES");
985 if (selinux_init_load_policy(&enforce) == 0) {
Denis Vlasenko4921b542007-02-03 02:17:41 +0000986 BB_EXECVP(argv[0], argv);
Rob Landleyb3ede5a2006-03-27 23:09:12 +0000987 } else if (enforce > 0) {
988 /* SELinux in enforcing mode but load_policy failed */
989 /* At this point, we probably can't open /dev/console, so log() won't work */
Denis Vlasenkob7167542007-02-27 19:20:00 +0000990 message(L_CONSOLE, "Cannot load SELinux Policy. "
991 "Machine is in enforcing mode. Halting now.");
Rob Landleyb3ede5a2006-03-27 23:09:12 +0000992 exit(1);
993 }
994 }
995#endif /* CONFIG_SELINUX */
996
Eric Andersen7ef1a5b2001-03-20 17:39:08 +0000997 /* Make the command line just say "init" -- thats all, nothing else */
998 fixup_argv(argc, argv, "init");
Eric Andersen219d6f51999-11-02 19:43:01 +0000999
Erik Andersene49d5ec2000-02-08 19:58:47 +00001000 /* Now run everything that needs to be run */
1001
1002 /* First run the sysinit command */
Eric Andersen1644db92001-09-05 20:18:15 +00001003 run_actions(SYSINIT);
Eric Andersen0298be82002-03-05 15:12:19 +00001004
Erik Andersene49d5ec2000-02-08 19:58:47 +00001005 /* Next run anything that wants to block */
Eric Andersen1644db92001-09-05 20:18:15 +00001006 run_actions(WAIT);
Eric Andersen0298be82002-03-05 15:12:19 +00001007
Erik Andersene49d5ec2000-02-08 19:58:47 +00001008 /* Next run anything to be run only once */
Eric Andersen0298be82002-03-05 15:12:19 +00001009 run_actions(ONCE);
1010
Denis Vlasenko9b1381f2007-01-03 02:56:00 +00001011#if ENABLE_FEATURE_USE_INITTAB
Eric Andersen6fd0e312003-07-22 09:48:56 +00001012 /* Redefine SIGHUP to reread /etc/inittab */
1013 signal(SIGHUP, reload_signal);
Eric Andersen82baf632004-10-08 08:21:54 +00001014#else
1015 signal(SIGHUP, SIG_IGN);
Denis Vlasenko9b1381f2007-01-03 02:56:00 +00001016#endif /* FEATURE_USE_INITTAB */
Eric Andersen82baf632004-10-08 08:21:54 +00001017
Erik Andersene49d5ec2000-02-08 19:58:47 +00001018 /* Now run the looping stuff for the rest of forever */
1019 while (1) {
Eric Andersen0298be82002-03-05 15:12:19 +00001020 /* run the respawn stuff */
1021 run_actions(RESPAWN);
1022
1023 /* run the askfirst stuff */
1024 run_actions(ASKFIRST);
1025
1026 /* Don't consume all CPU time -- sleep a bit */
1027 sleep(1);
1028
Erik Andersene49d5ec2000-02-08 19:58:47 +00001029 /* Wait for a child process to exit */
Bernhard Reutner-Fischerc58dbf22006-05-30 12:16:54 +00001030 wpid = wait(NULL);
Eric Andersen87812dc2004-04-12 19:21:54 +00001031 while (wpid > 0) {
Erik Andersene49d5ec2000-02-08 19:58:47 +00001032 /* Find out who died and clean up their corpse */
Eric Andersen0298be82002-03-05 15:12:19 +00001033 for (a = init_action_list; a; a = a->next) {
Erik Andersene49d5ec2000-02-08 19:58:47 +00001034 if (a->pid == wpid) {
Eric Andersen0298be82002-03-05 15:12:19 +00001035 /* Set the pid to 0 so that the process gets
1036 * restarted by run_actions() */
Erik Andersene49d5ec2000-02-08 19:58:47 +00001037 a->pid = 0;
Denis Vlasenko7a2ca5e2007-02-21 00:15:20 +00001038 message(L_LOG, "process '%s' (pid %d) exited. "
Glenn L McGrathb4a1baa2003-01-13 22:09:50 +00001039 "Scheduling it for restart.",
1040 a->command, wpid);
Erik Andersene49d5ec2000-02-08 19:58:47 +00001041 }
1042 }
Eric Andersencf1fee02002-12-17 09:48:16 +00001043 /* see if anyone else is waiting to be reaped */
Denis Vlasenkoce979602006-09-27 23:31:08 +00001044 wpid = waitpid(-1, NULL, WNOHANG);
Erik Andersene49d5ec2000-02-08 19:58:47 +00001045 }
Erik Andersene49d5ec2000-02-08 19:58:47 +00001046 }
Eric Andersencc8ed391999-10-05 16:24:54 +00001047}