blob: b488f649fd58d6e706af41ca12d919546536f712 [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
19#include "init_shared.h"
20
Denis Vlasenko9b1381f2007-01-03 02:56:00 +000021#if ENABLE_SYSLOGD
Erik Andersen4f3f7572000-04-28 00:18:56 +000022# include <sys/syslog.h>
23#endif
Eric Andersen2c1de612003-04-24 11:41:28 +000024
Eric Andersen0826b6b2002-07-03 23:50:16 +000025#define INIT_BUFFS_SIZE 256
26
Eric Andersenbd22ed82000-07-08 18:55:24 +000027/* From <linux/vt.h> */
28struct vt_stat {
Glenn L McGrathbaf55a82002-08-22 18:22:10 +000029 unsigned short v_active; /* active vt */
30 unsigned short v_signal; /* signal to send */
31 unsigned short v_state; /* vt bitmask */
Eric Andersenbd22ed82000-07-08 18:55:24 +000032};
Rob Landleybc68cd12006-03-10 19:22:06 +000033enum { VT_GETSTATE = 0x5603 }; /* get global vt state info */
Eric Andersenbd22ed82000-07-08 18:55:24 +000034
35/* From <linux/serial.h> */
36struct serial_struct {
Eric Andersen81155f82003-09-11 08:52:22 +000037 int type;
38 int line;
39 unsigned int port;
40 int irq;
41 int flags;
42 int xmit_fifo_size;
43 int custom_divisor;
44 int baud_base;
45 unsigned short close_delay;
46 char io_type;
47 char reserved_char[1];
48 int hub6;
49 unsigned short closing_wait; /* time to wait before closing */
50 unsigned short closing_wait2; /* no longer used... */
51 unsigned char *iomem_base;
52 unsigned short iomem_reg_shift;
53 unsigned int port_high;
54 unsigned long iomap_base; /* cookie passed into ioremap */
55 int reserved[1];
Eric Andersenbd22ed82000-07-08 18:55:24 +000056};
57
Eric Andersen41492d62001-02-23 00:05:56 +000058#ifndef _PATH_STDPATH
Erik Andersen4f3f7572000-04-28 00:18:56 +000059#define _PATH_STDPATH "/usr/bin:/bin:/usr/sbin:/sbin"
Eric Andersen41492d62001-02-23 00:05:56 +000060#endif
Eric Andersencc8ed391999-10-05 16:24:54 +000061
Denis Vlasenko9b1381f2007-01-03 02:56:00 +000062#if ENABLE_FEATURE_INIT_COREDUMPS
Erik Andersen983b51b2000-04-04 18:14:25 +000063/*
Eric Andersenc7bda1c2004-03-15 08:29:22 +000064 * When a file named CORE_ENABLE_FLAG_FILE exists, setrlimit is called
Erik Andersen183da4a2000-04-04 18:36:37 +000065 * before processes are spawned to set core file size as unlimited.
66 * This is for debugging only. Don't use this is production, unless
67 * you want core dumps lying about....
Erik Andersen983b51b2000-04-04 18:14:25 +000068 */
69#define CORE_ENABLE_FLAG_FILE "/.init_enable_core"
70#include <sys/resource.h>
Erik Andersen183da4a2000-04-04 18:36:37 +000071#endif
Erik Andersen983b51b2000-04-04 18:14:25 +000072
Glenn L McGrathbaf55a82002-08-22 18:22:10 +000073#define INITTAB "/etc/inittab" /* inittab file location */
Erik Andersen96e2abd2000-01-07 11:40:44 +000074#ifndef INIT_SCRIPT
Glenn L McGrathbaf55a82002-08-22 18:22:10 +000075#define INIT_SCRIPT "/etc/init.d/rcS" /* Default sysinit script. */
Erik Andersen96e2abd2000-01-07 11:40:44 +000076#endif
Erik Andersen9c88cac1999-12-30 09:25:17 +000077
Eric Andersen41492d62001-02-23 00:05:56 +000078#define MAXENV 16 /* Number of env. vars */
Erik Andersen7dc16072000-01-04 01:10:25 +000079
Glenn L McGrathb4a1baa2003-01-13 22:09:50 +000080#define CONSOLE_BUFF_SIZE 32
81
Erik Andersen7dc16072000-01-04 01:10:25 +000082/* Allowed init action types */
Eric Andersen0298be82002-03-05 15:12:19 +000083#define SYSINIT 0x001
84#define RESPAWN 0x002
85#define ASKFIRST 0x004
86#define WAIT 0x008
87#define ONCE 0x010
88#define CTRLALTDEL 0x020
89#define SHUTDOWN 0x040
90#define RESTART 0x080
Erik Andersen7dc16072000-01-04 01:10:25 +000091
Erik Andersen42094cd2000-03-20 21:34:52 +000092/* A mapping between "inittab" action name strings and action type codes. */
Eric Andersen0298be82002-03-05 15:12:19 +000093struct init_action_type {
Erik Andersene49d5ec2000-02-08 19:58:47 +000094 const char *name;
Eric Andersen0298be82002-03-05 15:12:19 +000095 int action;
96};
Erik Andersen7dc16072000-01-04 01:10:25 +000097
Eric Andersen0298be82002-03-05 15:12:19 +000098static const struct init_action_type actions[] = {
Erik Andersene49d5ec2000-02-08 19:58:47 +000099 {"sysinit", SYSINIT},
100 {"respawn", RESPAWN},
101 {"askfirst", ASKFIRST},
102 {"wait", WAIT},
103 {"once", ONCE},
Erik Andersene132f4b2000-02-09 04:16:43 +0000104 {"ctrlaltdel", CTRLALTDEL},
Eric Andersenc97ec342001-04-03 18:01:51 +0000105 {"shutdown", SHUTDOWN},
Eric Andersen730f8262001-12-17 23:13:08 +0000106 {"restart", RESTART},
Pavel Roskin9027bcf2000-07-14 15:44:25 +0000107 {0, 0}
Erik Andersen7dc16072000-01-04 01:10:25 +0000108};
109
Eric Andersen0298be82002-03-05 15:12:19 +0000110/* Set up a linked list of init_actions, to be read from inittab */
111struct init_action {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000112 pid_t pid;
Eric Andersen0826b6b2002-07-03 23:50:16 +0000113 char command[INIT_BUFFS_SIZE];
Glenn L McGrathb4a1baa2003-01-13 22:09:50 +0000114 char terminal[CONSOLE_BUFF_SIZE];
Eric Andersen0298be82002-03-05 15:12:19 +0000115 struct init_action *next;
116 int action;
Erik Andersen7dc16072000-01-04 01:10:25 +0000117};
Erik Andersen7dc16072000-01-04 01:10:25 +0000118
Eric Andersen0298be82002-03-05 15:12:19 +0000119/* Static variables */
120static struct init_action *init_action_list = NULL;
Denis Vlasenkoec27feb2007-02-17 15:52:02 +0000121static char console_name[CONSOLE_BUFF_SIZE] = DEV_CONSOLE;
Glenn L McGrathbaf55a82002-08-22 18:22:10 +0000122
Denis Vlasenko9b1381f2007-01-03 02:56:00 +0000123#if !ENABLE_SYSLOGD
Denis Vlasenko4c978632007-02-03 03:31:13 +0000124static const char *log_console = VC_5;
Eric Andersene7078062002-07-29 08:00:16 +0000125#endif
Bernhard Reutner-Fischercf1f2ac2006-06-02 10:43:17 +0000126#if !ENABLE_DEBUG_INIT
Eric Andersen0298be82002-03-05 15:12:19 +0000127static sig_atomic_t got_cont = 0;
Bernhard Reutner-Fischercf1f2ac2006-06-02 10:43:17 +0000128#endif
Rob Landleybc68cd12006-03-10 19:22:06 +0000129
130enum {
Denis Vlasenkoec27feb2007-02-17 15:52:02 +0000131 L_LOG = 0x1,
132 L_CONSOLE = 0x2,
Glenn L McGrathbaf55a82002-08-22 18:22:10 +0000133
Denis Vlasenko9b1381f2007-01-03 02:56:00 +0000134#if ENABLE_FEATURE_EXTRA_QUIET
Rob Landleybc68cd12006-03-10 19:22:06 +0000135 MAYBE_CONSOLE = 0x0,
Eric Andersen0298be82002-03-05 15:12:19 +0000136#else
Denis Vlasenkoec27feb2007-02-17 15:52:02 +0000137 MAYBE_CONSOLE = L_CONSOLE,
Eric Andersen0298be82002-03-05 15:12:19 +0000138#endif
Glenn L McGrathbaf55a82002-08-22 18:22:10 +0000139
Rob Landleybc68cd12006-03-10 19:22:06 +0000140#ifndef RB_HALT_SYSTEM
Bernhard Reutner-Fischercf1f2ac2006-06-02 10:43:17 +0000141 RB_HALT_SYSTEM = 0xcdef0123, /* FIXME: this overflows enum */
Rob Landleybc68cd12006-03-10 19:22:06 +0000142 RB_ENABLE_CAD = 0x89abcdef,
143 RB_DISABLE_CAD = 0,
144 RB_POWER_OFF = 0x4321fedc,
145 RB_AUTOBOOT = 0x01234567,
Eric Andersen0298be82002-03-05 15:12:19 +0000146#endif
Rob Landleybc68cd12006-03-10 19:22:06 +0000147};
Erik Andersen42094cd2000-03-20 21:34:52 +0000148
Glenn L McGrathb4a1baa2003-01-13 22:09:50 +0000149static const char * const environment[] = {
150 "HOME=/",
151 "PATH=" _PATH_STDPATH,
Glenn L McGrathdc4e75e2003-09-02 02:36:18 +0000152 "SHELL=/bin/sh",
Glenn L McGrathb4a1baa2003-01-13 22:09:50 +0000153 "USER=root",
154 NULL
155};
156
Eric Andersen0298be82002-03-05 15:12:19 +0000157/* Function prototypes */
158static void delete_init_action(struct init_action *a);
Bernhard Reutner-Fischer3ab30802006-05-30 12:10:29 +0000159static int waitfor(const struct init_action *a, pid_t pid);
Bernhard Reutner-Fischercf1f2ac2006-06-02 10:43:17 +0000160#if !ENABLE_DEBUG_INIT
Paul Foxd112f8f2006-06-01 13:17:49 +0000161static void shutdown_signal(int sig);
Bernhard Reutner-Fischercf1f2ac2006-06-02 10:43:17 +0000162#endif
Eric Andersen0460ff21999-10-25 23:32:44 +0000163
Denis Vlasenko9b1381f2007-01-03 02:56:00 +0000164#if !ENABLE_DEBUG_INIT
Eric Andersen74400cc2001-10-18 04:11:39 +0000165static void loop_forever(void)
Matt Kraai67a46402001-06-03 05:55:52 +0000166{
167 while (1)
Glenn L McGrathbaf55a82002-08-22 18:22:10 +0000168 sleep(1);
Matt Kraai67a46402001-06-03 05:55:52 +0000169}
Denis Vlasenko9b1381f2007-01-03 02:56:00 +0000170#endif
Eric Andersen7ef1a5b2001-03-20 17:39:08 +0000171
Erik Andersen42094cd2000-03-20 21:34:52 +0000172/* Print a message to the specified device.
Denis Vlasenkoec27feb2007-02-17 15:52:02 +0000173 * Device may be bitwise-or'd from L_LOG | L_CONSOLE */
Bernhard Reutner-Fischer35e1a072006-05-29 13:08:35 +0000174#if ENABLE_DEBUG_INIT
175#define messageD message
176#else
Denis Vlasenkobf0a2012006-12-26 10:42:51 +0000177#define messageD(...) do {} while (0)
Eric Andersenfedce062001-11-17 07:27:14 +0000178#endif
Glenn L McGrathb4a1baa2003-01-13 22:09:50 +0000179static void message(int device, const char *fmt, ...)
Glenn L McGrathbaf55a82002-08-22 18:22:10 +0000180 __attribute__ ((format(printf, 2, 3)));
Glenn L McGrathb4a1baa2003-01-13 22:09:50 +0000181static void message(int device, const char *fmt, ...)
Eric Andersen0460ff21999-10-25 23:32:44 +0000182{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000183 va_list arguments;
Glenn L McGrathb4a1baa2003-01-13 22:09:50 +0000184 int l;
Bernhard Reutner-Fischer3ab30802006-05-30 12:10:29 +0000185 RESERVE_CONFIG_BUFFER(msg, 1024);
Denis Vlasenko9b1381f2007-01-03 02:56:00 +0000186#if !ENABLE_SYSLOGD
Eric Andersen22237012003-01-23 07:08:26 +0000187 static int log_fd = -1;
188#endif
Eric Andersen4c781471999-11-26 08:12:56 +0000189
Glenn L McGrathb4a1baa2003-01-13 22:09:50 +0000190 msg[0] = '\r';
Erik Andersene49d5ec2000-02-08 19:58:47 +0000191 va_start(arguments, fmt);
Bernhard Reutner-Fischer3ab30802006-05-30 12:10:29 +0000192 l = vsnprintf(msg + 1, 1024 - 2, fmt, arguments) + 1;
Denis Vlasenkoec27feb2007-02-17 15:52:02 +0000193 va_end(arguments);
Glenn L McGrathb4a1baa2003-01-13 22:09:50 +0000194
Denis Vlasenko9b1381f2007-01-03 02:56:00 +0000195#if ENABLE_SYSLOGD
Glenn L McGrathb4a1baa2003-01-13 22:09:50 +0000196 /* Log the message to syslogd */
Denis Vlasenkoec27feb2007-02-17 15:52:02 +0000197 if (device & L_LOG) {
Glenn L McGrathb4a1baa2003-01-13 22:09:50 +0000198 /* don`t out "\r\n" */
Denis Vlasenko8f8f2682006-10-03 21:00:43 +0000199 openlog(applet_name, 0, LOG_DAEMON);
"Vladimir N. Oleynik"8e1bd4a2005-09-29 12:55:21 +0000200 syslog(LOG_INFO, "%s", msg + 1);
Eric Andersen36adca82004-06-22 10:07:17 +0000201 closelog();
Eric Andersen3ae0c781999-11-04 01:13:21 +0000202 }
Glenn L McGrathb4a1baa2003-01-13 22:09:50 +0000203
204 msg[l++] = '\n';
205 msg[l] = 0;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000206#else
Erik Andersene49d5ec2000-02-08 19:58:47 +0000207
Glenn L McGrathb4a1baa2003-01-13 22:09:50 +0000208 msg[l++] = '\n';
209 msg[l] = 0;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000210 /* Take full control of the log tty, and never close it.
211 * It's mine, all mine! Muhahahaha! */
212 if (log_fd < 0) {
Denis Vlasenkoec27feb2007-02-17 15:52:02 +0000213 log_fd = device_open(log_console, O_WRONLY | O_NONBLOCK | O_NOCTTY);
214 if (log_fd < 0) {
Erik Andersene132f4b2000-02-09 04:16:43 +0000215 log_fd = -2;
Denis Vlasenkoec27feb2007-02-17 15:52:02 +0000216 bb_error_msg("bummer, can't log to %s!", log_console);
217 device = L_CONSOLE;
Eric Andersen6a979902002-09-30 20:08:53 +0000218 } else {
219 fcntl(log_fd, F_SETFD, FD_CLOEXEC);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000220 }
221 }
Denis Vlasenkoec27feb2007-02-17 15:52:02 +0000222 if (device & L_LOG) {
Rob Landley53437472006-07-16 08:14:35 +0000223 full_write(log_fd, msg, l);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000224 }
Eric Andersen4c781471999-11-26 08:12:56 +0000225#endif
226
Denis Vlasenkoec27feb2007-02-17 15:52:02 +0000227 if (device & L_CONSOLE) {
228 int fd = device_open(DEV_CONSOLE,
Mike Frysingere548bdf2005-07-06 04:46:14 +0000229 O_WRONLY | O_NOCTTY | O_NONBLOCK);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000230 /* Always send console messages to /dev/console so people will see them. */
Glenn L McGrathb4a1baa2003-01-13 22:09:50 +0000231 if (fd >= 0) {
Rob Landley53437472006-07-16 08:14:35 +0000232 full_write(fd, msg, l);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000233 close(fd);
Bernhard Reutner-Fischer35e1a072006-05-29 13:08:35 +0000234#if ENABLE_DEBUG_INIT
Glenn L McGrathb4a1baa2003-01-13 22:09:50 +0000235 /* all descriptors may be closed */
Erik Andersene49d5ec2000-02-08 19:58:47 +0000236 } else {
Denis Vlasenkoe1a0d482006-10-20 13:28:22 +0000237 bb_error_msg("bummer, can't print: ");
Erik Andersene49d5ec2000-02-08 19:58:47 +0000238 va_start(arguments, fmt);
239 vfprintf(stderr, fmt, arguments);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000240 va_end(arguments);
Glenn L McGrathb4a1baa2003-01-13 22:09:50 +0000241#endif
Erik Andersene49d5ec2000-02-08 19:58:47 +0000242 }
Eric Andersen3ae0c781999-11-04 01:13:21 +0000243 }
Bernhard Reutner-Fischer3ab30802006-05-30 12:10:29 +0000244 RELEASE_CONFIG_BUFFER(msg);
Eric Andersencc8ed391999-10-05 16:24:54 +0000245}
246
Eric Andersen0460ff21999-10-25 23:32:44 +0000247/* Set terminal settings to reasonable defaults */
Bernhard Reutner-Fischer49e60b92006-05-29 12:57:52 +0000248static void set_term(void)
Eric Andersencc8ed391999-10-05 16:24:54 +0000249{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000250 struct termios tty;
Eric Andersencc8ed391999-10-05 16:24:54 +0000251
Bernhard Reutner-Fischer49e60b92006-05-29 12:57:52 +0000252 tcgetattr(STDIN_FILENO, &tty);
Eric Andersencc8ed391999-10-05 16:24:54 +0000253
Erik Andersene49d5ec2000-02-08 19:58:47 +0000254 /* set control chars */
Glenn L McGrathbaf55a82002-08-22 18:22:10 +0000255 tty.c_cc[VINTR] = 3; /* C-c */
256 tty.c_cc[VQUIT] = 28; /* C-\ */
257 tty.c_cc[VERASE] = 127; /* C-? */
258 tty.c_cc[VKILL] = 21; /* C-u */
259 tty.c_cc[VEOF] = 4; /* C-d */
Eric Andersen3849f9b2000-07-10 19:56:47 +0000260 tty.c_cc[VSTART] = 17; /* C-q */
Glenn L McGrathbaf55a82002-08-22 18:22:10 +0000261 tty.c_cc[VSTOP] = 19; /* C-s */
262 tty.c_cc[VSUSP] = 26; /* C-z */
Eric Andersen02bc25b2000-07-06 21:29:32 +0000263
Erik Andersene49d5ec2000-02-08 19:58:47 +0000264 /* use line dicipline 0 */
265 tty.c_line = 0;
Eric Andersen7f1acfd1999-10-29 23:09:13 +0000266
Erik Andersene49d5ec2000-02-08 19:58:47 +0000267 /* Make it be sane */
Glenn L McGrathbaf55a82002-08-22 18:22:10 +0000268 tty.c_cflag &= CBAUD | CBAUDEX | CSIZE | CSTOPB | PARENB | PARODD;
269 tty.c_cflag |= CREAD | HUPCL | CLOCAL;
Eric Andersend8862922001-04-23 15:14:11 +0000270
Eric Andersen08b10341999-11-19 02:38:58 +0000271
Erik Andersene49d5ec2000-02-08 19:58:47 +0000272 /* input modes */
273 tty.c_iflag = ICRNL | IXON | IXOFF;
Eric Andersen08b10341999-11-19 02:38:58 +0000274
Erik Andersene49d5ec2000-02-08 19:58:47 +0000275 /* output modes */
276 tty.c_oflag = OPOST | ONLCR;
Eric Andersen7f1acfd1999-10-29 23:09:13 +0000277
Erik Andersene49d5ec2000-02-08 19:58:47 +0000278 /* local modes */
279 tty.c_lflag =
280 ISIG | ICANON | ECHO | ECHOE | ECHOK | ECHOCTL | ECHOKE | IEXTEN;
Eric Andersencc8ed391999-10-05 16:24:54 +0000281
Bernhard Reutner-Fischer49e60b92006-05-29 12:57:52 +0000282 tcsetattr(STDIN_FILENO, TCSANOW, &tty);
Eric Andersencc8ed391999-10-05 16:24:54 +0000283}
284
Eric Andersen74400cc2001-10-18 04:11:39 +0000285static void console_init(void)
Eric Andersen0460ff21999-10-25 23:32:44 +0000286{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000287 int fd;
Glenn L McGrathb4a1baa2003-01-13 22:09:50 +0000288 int tried = 0;
Erik Andersen029011b2000-03-04 21:19:32 +0000289 struct vt_stat vt;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000290 struct serial_struct sr;
291 char *s;
Eric Andersen0460ff21999-10-25 23:32:44 +0000292
Glenn L McGrathb4a1baa2003-01-13 22:09:50 +0000293 if ((s = getenv("CONSOLE")) != NULL || (s = getenv("console")) != NULL) {
Denis Vlasenkoec27feb2007-02-17 15:52:02 +0000294 safe_strncpy(console_name, s, sizeof(console_name));
Glenn L McGrathb4a1baa2003-01-13 22:09:50 +0000295 } else {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000296 /* 2.2 kernels: identify the real console backend and try to use it */
297 if (ioctl(0, TIOCGSERIAL, &sr) == 0) {
298 /* this is a serial console */
Denis Vlasenkoec27feb2007-02-17 15:52:02 +0000299 snprintf(console_name, sizeof(console_name) - 1, SC_FORMAT, sr.line);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000300 } else if (ioctl(0, VT_GETSTATE, &vt) == 0) {
301 /* this is linux virtual tty */
Denis Vlasenkoec27feb2007-02-17 15:52:02 +0000302 snprintf(console_name, sizeof(console_name) - 1, VC_FORMAT, vt.v_active);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000303 } else {
Denis Vlasenkoec27feb2007-02-17 15:52:02 +0000304 strcpy(console_name, DEV_CONSOLE);
Glenn L McGrathb4a1baa2003-01-13 22:09:50 +0000305 tried++;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000306 }
Eric Andersen07e52971999-11-07 07:38:08 +0000307 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000308
Denis Vlasenkoec27feb2007-02-17 15:52:02 +0000309 while ((fd = open(console_name, O_RDONLY | O_NONBLOCK)) < 0 && tried < 2) {
Glenn L McGrathb4a1baa2003-01-13 22:09:50 +0000310 /* Can't open selected console -- try
311 logical system console and VT_MASTER */
Denis Vlasenkoec27feb2007-02-17 15:52:02 +0000312 strcpy(console_name, (tried == 0 ? DEV_CONSOLE : CURRENT_VC));
Glenn L McGrathb4a1baa2003-01-13 22:09:50 +0000313 tried++;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000314 }
315 if (fd < 0) {
316 /* Perhaps we should panic here? */
Denis Vlasenko9b1381f2007-01-03 02:56:00 +0000317#if !ENABLE_SYSLOGD
Mike Frysinger72a4c332005-07-05 02:19:20 +0000318 log_console =
Glenn L McGrathb4a1baa2003-01-13 22:09:50 +0000319#endif
Denis Vlasenkoec27feb2007-02-17 15:52:02 +0000320 strcpy(console_name, bb_dev_null);
Eric Andersen07e52971999-11-07 07:38:08 +0000321 } else {
Glenn L McGrathb4a1baa2003-01-13 22:09:50 +0000322 s = getenv("TERM");
Eric Andersen3bc2b202002-07-29 06:39:58 +0000323 /* check for serial console */
Glenn L McGrathb4a1baa2003-01-13 22:09:50 +0000324 if (ioctl(fd, TIOCGSERIAL, &sr) == 0) {
Erik Andersenfa4718e2000-02-21 19:25:12 +0000325 /* Force the TERM setting to vt102 for serial console --
Eric Andersen3bc2b202002-07-29 06:39:58 +0000326 * if TERM is set to linux (the default) */
Glenn L McGrathb4a1baa2003-01-13 22:09:50 +0000327 if (s == NULL || strcmp(s, "linux") == 0)
Denis Vlasenko06c0a712007-01-29 22:51:44 +0000328 putenv((char*)"TERM=vt102");
Denis Vlasenko9b1381f2007-01-03 02:56:00 +0000329#if !ENABLE_SYSLOGD
Denis Vlasenkoec27feb2007-02-17 15:52:02 +0000330 log_console = console_name;
Glenn L McGrathb4a1baa2003-01-13 22:09:50 +0000331#endif
332 } else {
333 if (s == NULL)
Denis Vlasenko06c0a712007-01-29 22:51:44 +0000334 putenv((char*)"TERM=linux");
Erik Andersene49d5ec2000-02-08 19:58:47 +0000335 }
336 close(fd);
Eric Andersen07e52971999-11-07 07:38:08 +0000337 }
Denis Vlasenkoec27feb2007-02-17 15:52:02 +0000338 messageD(L_LOG, "console=%s", console_name);
Eric Andersen0460ff21999-10-25 23:32:44 +0000339}
Glenn L McGrathbaf55a82002-08-22 18:22:10 +0000340
Denis Vlasenko06c0a712007-01-29 22:51:44 +0000341static void fixup_argv(int argc, char **argv, const char *new_argv0)
Eric Andersen7ef1a5b2001-03-20 17:39:08 +0000342{
343 int len;
Glenn L McGrathbaf55a82002-08-22 18:22:10 +0000344
Eric Andersen7ef1a5b2001-03-20 17:39:08 +0000345 /* Fix up argv[0] to be certain we claim to be init */
346 len = strlen(argv[0]);
Denis Vlasenko06c0a712007-01-29 22:51:44 +0000347 strncpy(argv[0], new_argv0, len);
Eric Andersen7ef1a5b2001-03-20 17:39:08 +0000348
349 /* Wipe argv[1]-argv[N] so they don't clutter the ps listing */
350 len = 1;
351 while (argc > len) {
352 memset(argv[len], 0, strlen(argv[len]));
353 len++;
354 }
355}
356
Bernhard Reutner-Fischer0da069d2006-05-29 12:54:16 +0000357/* Open the new terminal device */
Denis Vlasenkoec27feb2007-02-17 15:52:02 +0000358static void open_new_terminal(const char* device, int fail) {
Bernhard Reutner-Fischer0da069d2006-05-29 12:54:16 +0000359 struct stat sb;
360
361 if ((device_open(device, O_RDWR)) < 0) {
362 if (stat(device, &sb) != 0) {
Denis Vlasenkoec27feb2007-02-17 15:52:02 +0000363 message(L_LOG | L_CONSOLE, "device '%s' does not exist", device);
Bernhard Reutner-Fischer0da069d2006-05-29 12:54:16 +0000364 } else {
Denis Vlasenkoec27feb2007-02-17 15:52:02 +0000365 message(L_LOG | L_CONSOLE, "Bummer, can't open %s", device);
Bernhard Reutner-Fischer0da069d2006-05-29 12:54:16 +0000366 }
367 if (fail)
368 _exit(1);
369 /* else */
Bernhard Reutner-Fischercf1f2ac2006-06-02 10:43:17 +0000370#if !ENABLE_DEBUG_INIT
Paul Foxd112f8f2006-06-01 13:17:49 +0000371 shutdown_signal(SIGUSR1);
Bernhard Reutner-Fischercf1f2ac2006-06-02 10:43:17 +0000372#else
373 _exit(2);
374#endif
Bernhard Reutner-Fischer0da069d2006-05-29 12:54:16 +0000375 }
376}
377
Glenn L McGrathb4a1baa2003-01-13 22:09:50 +0000378static pid_t run(const struct init_action *a)
Eric Andersen0298be82002-03-05 15:12:19 +0000379{
Bernhard Reutner-Fischer3ab30802006-05-30 12:10:29 +0000380 int i;
Mike Frysinger10427ab2005-07-06 05:00:48 +0000381 pid_t pid;
Denis Vlasenko06c0a712007-01-29 22:51:44 +0000382 char *s, *tmpCmd, *cmdpath;
383 char *cmd[INIT_BUFFS_SIZE];
Glenn L McGrathbaf55a82002-08-22 18:22:10 +0000384 char buf[INIT_BUFFS_SIZE + 6]; /* INIT_BUFFS_SIZE+strlen("exec ")+1 */
Eric Andersen0298be82002-03-05 15:12:19 +0000385 sigset_t nmask, omask;
Eric Andersen0298be82002-03-05 15:12:19 +0000386 static const char press_enter[] =
387#ifdef CUSTOMIZED_BANNER
388#include CUSTOMIZED_BANNER
389#endif
390 "\nPlease press Enter to activate this console. ";
Erik Andersenac6e71f2000-01-08 22:04:33 +0000391
Eric Andersen0298be82002-03-05 15:12:19 +0000392 /* Block sigchild while forking. */
393 sigemptyset(&nmask);
394 sigaddset(&nmask, SIGCHLD);
395 sigprocmask(SIG_BLOCK, &nmask, &omask);
396
Glenn L McGrathbaf55a82002-08-22 18:22:10 +0000397 if ((pid = fork()) == 0) {
Mike Frysinger10427ab2005-07-06 05:00:48 +0000398
Erik Andersene49d5ec2000-02-08 19:58:47 +0000399 /* Clean up */
400 close(0);
401 close(1);
402 close(2);
Eric Andersen0298be82002-03-05 15:12:19 +0000403 sigprocmask(SIG_SETMASK, &omask, NULL);
404
Eric Andersen0298be82002-03-05 15:12:19 +0000405 /* Reset signal handlers that were set by the parent process */
Erik Andersene49d5ec2000-02-08 19:58:47 +0000406 signal(SIGUSR1, SIG_DFL);
407 signal(SIGUSR2, SIG_DFL);
Glenn L McGrathbaf55a82002-08-22 18:22:10 +0000408 signal(SIGINT, SIG_DFL);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000409 signal(SIGTERM, SIG_DFL);
Glenn L McGrathbaf55a82002-08-22 18:22:10 +0000410 signal(SIGHUP, SIG_DFL);
Mike Frysingera77b4f32005-04-16 08:21:34 +0000411 signal(SIGQUIT, SIG_DFL);
Eric Andersened8a9be2001-11-30 19:10:58 +0000412 signal(SIGCONT, SIG_DFL);
413 signal(SIGSTOP, SIG_DFL);
414 signal(SIGTSTP, SIG_DFL);
Eric Andersen2f6c04f1999-11-01 23:59:44 +0000415
Eric Andersen599e3ce2002-07-03 11:08:10 +0000416 /* Create a new session and make ourself the process
Eric Andersene8a90fb2002-10-12 04:05:48 +0000417 * group leader */
418 setsid();
Eric Andersen599e3ce2002-07-03 11:08:10 +0000419
Eric Andersen0298be82002-03-05 15:12:19 +0000420 /* Open the new terminal device */
Bernhard Reutner-Fischer0da069d2006-05-29 12:54:16 +0000421 open_new_terminal(a->terminal, 1);
Eric Andersen599e3ce2002-07-03 11:08:10 +0000422
Eric Andersen0298be82002-03-05 15:12:19 +0000423 /* Make sure the terminal will act fairly normal for us */
Bernhard Reutner-Fischer49e60b92006-05-29 12:57:52 +0000424 set_term();
Eric Andersen0826b6b2002-07-03 23:50:16 +0000425 /* Setup stdout, stderr for the new process so
Eric Andersen599e3ce2002-07-03 11:08:10 +0000426 * they point to the supplied terminal */
Eric Andersen0826b6b2002-07-03 23:50:16 +0000427 dup(0);
428 dup(0);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000429
Eric Andersen599e3ce2002-07-03 11:08:10 +0000430 /* If the init Action requires us to wait, then force the
Eric Andersen0298be82002-03-05 15:12:19 +0000431 * supplied terminal to be the controlling tty. */
Glenn L McGrathbaf55a82002-08-22 18:22:10 +0000432 if (a->action & (SYSINIT | WAIT | CTRLALTDEL | SHUTDOWN | RESTART)) {
Eric Andersen0298be82002-03-05 15:12:19 +0000433
434 /* Now fork off another process to just hang around */
435 if ((pid = fork()) < 0) {
Denis Vlasenkoec27feb2007-02-17 15:52:02 +0000436 message(L_LOG | L_CONSOLE, "Can't fork!");
Eric Andersen0298be82002-03-05 15:12:19 +0000437 _exit(1);
438 }
439
440 if (pid > 0) {
441
442 /* We are the parent -- wait till the child is done */
443 signal(SIGINT, SIG_IGN);
444 signal(SIGTSTP, SIG_IGN);
445 signal(SIGQUIT, SIG_IGN);
446 signal(SIGCHLD, SIG_DFL);
447
Bernhard Reutner-Fischer3ab30802006-05-30 12:10:29 +0000448 waitfor(NULL, pid);
Eric Andersen0298be82002-03-05 15:12:19 +0000449 /* See if stealing the controlling tty back is necessary */
Bernhard Reutner-Fischer3ab30802006-05-30 12:10:29 +0000450 if (tcgetpgrp(0) != getpid())
Eric Andersen0298be82002-03-05 15:12:19 +0000451 _exit(0);
452
453 /* Use a temporary process to steal the controlling tty. */
454 if ((pid = fork()) < 0) {
Denis Vlasenkoec27feb2007-02-17 15:52:02 +0000455 message(L_LOG | L_CONSOLE, "Can't fork!");
Eric Andersen0298be82002-03-05 15:12:19 +0000456 _exit(1);
Glenn L McGrathbaf55a82002-08-22 18:22:10 +0000457 }
Eric Andersen0298be82002-03-05 15:12:19 +0000458 if (pid == 0) {
459 setsid();
Eric Andersen0826b6b2002-07-03 23:50:16 +0000460 ioctl(0, TIOCSCTTY, 1);
Eric Andersen0298be82002-03-05 15:12:19 +0000461 _exit(0);
Glenn L McGrathbaf55a82002-08-22 18:22:10 +0000462 }
Bernhard Reutner-Fischer3ab30802006-05-30 12:10:29 +0000463 waitfor(NULL, pid);
Eric Andersen0298be82002-03-05 15:12:19 +0000464 _exit(0);
465 }
466
467 /* Now fall though to actually execute things */
Eric Andersen0298be82002-03-05 15:12:19 +0000468 }
469
Erik Andersene132f4b2000-02-09 04:16:43 +0000470 /* See if any special /bin/sh requiring characters are present */
Eric Andersen0298be82002-03-05 15:12:19 +0000471 if (strpbrk(a->command, "~`!$^&*()=|\\{}[];\"'<>?") != NULL) {
Glenn L McGrathdc4e75e2003-09-02 02:36:18 +0000472 cmd[0] = (char *)DEFAULT_SHELL;
Denis Vlasenko06c0a712007-01-29 22:51:44 +0000473 cmd[1] = (char*)"-c";
Glenn L McGrathb4a1baa2003-01-13 22:09:50 +0000474 cmd[2] = strcat(strcpy(buf, "exec "), a->command);
Erik Andersene132f4b2000-02-09 04:16:43 +0000475 cmd[3] = NULL;
476 } else {
477 /* Convert command (char*) into cmd (char**, one word per string) */
Eric Andersen0826b6b2002-07-03 23:50:16 +0000478 strcpy(buf, a->command);
Eric Andersen72f9a422001-10-28 05:12:20 +0000479 s = buf;
Glenn L McGrathbaf55a82002-08-22 18:22:10 +0000480 for (tmpCmd = buf, i = 0; (tmpCmd = strsep(&s, " \t")) != NULL;) {
Erik Andersene132f4b2000-02-09 04:16:43 +0000481 if (*tmpCmd != '\0') {
482 cmd[i] = tmpCmd;
Erik Andersene132f4b2000-02-09 04:16:43 +0000483 i++;
484 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000485 }
Erik Andersene132f4b2000-02-09 04:16:43 +0000486 cmd[i] = NULL;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000487 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000488
Eric Andersen7ef1a5b2001-03-20 17:39:08 +0000489 cmdpath = cmd[0];
Eric Andersen7e3bf6e2000-09-14 22:01:31 +0000490
Eric Andersen7ef1a5b2001-03-20 17:39:08 +0000491 /*
492 Interactive shells want to see a dash in argv[0]. This
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000493 typically is handled by login, argv will be setup this
494 way if a dash appears at the front of the command path
Eric Andersen7e3bf6e2000-09-14 22:01:31 +0000495 (like "-/bin/sh").
Eric Andersen7ef1a5b2001-03-20 17:39:08 +0000496 */
Eric Andersen7e3bf6e2000-09-14 22:01:31 +0000497
Eric Andersen7ef1a5b2001-03-20 17:39:08 +0000498 if (*cmdpath == '-') {
Eric Andersen7e3bf6e2000-09-14 22:01:31 +0000499
Eric Andersen7ef1a5b2001-03-20 17:39:08 +0000500 /* skip over the dash */
501 ++cmdpath;
Eric Andersen7e3bf6e2000-09-14 22:01:31 +0000502
Eric Andersen7ef1a5b2001-03-20 17:39:08 +0000503 /* find the last component in the command pathname */
Manuel Novoa III cad53642003-03-19 09:13:01 +0000504 s = bb_get_last_path_component(cmdpath);
Eric Andersen7e3bf6e2000-09-14 22:01:31 +0000505
Eric Andersen7ef1a5b2001-03-20 17:39:08 +0000506 /* make a new argv[0] */
Glenn L McGrathbaf55a82002-08-22 18:22:10 +0000507 if ((cmd[0] = malloc(strlen(s) + 2)) == NULL) {
Denis Vlasenkoec27feb2007-02-17 15:52:02 +0000508 message(L_LOG | L_CONSOLE, bb_msg_memory_exhausted);
Eric Andersen7ef1a5b2001-03-20 17:39:08 +0000509 cmd[0] = cmdpath;
510 } else {
511 cmd[0][0] = '-';
Glenn L McGrathbaf55a82002-08-22 18:22:10 +0000512 strcpy(cmd[0] + 1, s);
Eric Andersen7ef1a5b2001-03-20 17:39:08 +0000513 }
Denis Vlasenko9b1381f2007-01-03 02:56:00 +0000514#if ENABLE_FEATURE_INIT_SCTTY
Paul Fox41a72ec2005-08-01 16:43:13 +0000515 /* Establish this process as session leader and
516 * (attempt) to make the tty (if any) a controlling tty.
517 */
518 (void) setsid();
519 (void) ioctl(0, TIOCSCTTY, 0/*don't steal it*/);
520#endif
Eric Andersen7ef1a5b2001-03-20 17:39:08 +0000521 }
522
Eric Andersen1f50e842004-08-16 09:29:42 +0000523#if !defined(__UCLIBC__) || defined(__ARCH_HAS_MMU__)
Eric Andersen0298be82002-03-05 15:12:19 +0000524 if (a->action & ASKFIRST) {
Glenn L McGrathb4a1baa2003-01-13 22:09:50 +0000525 char c;
Eric Andersen7ef1a5b2001-03-20 17:39:08 +0000526 /*
527 * Save memory by not exec-ing anything large (like a shell)
528 * before the user wants it. This is critical if swap is not
529 * enabled and the system has low memory. Generally this will
530 * be run on the second virtual console, and the first will
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000531 * be allowed to start a shell or whatever an init script
Eric Andersen7ef1a5b2001-03-20 17:39:08 +0000532 * specifies.
533 */
Denis Vlasenkoec27feb2007-02-17 15:52:02 +0000534 messageD(L_LOG, "Waiting for enter to start '%s'"
Glenn L McGrathb4a1baa2003-01-13 22:09:50 +0000535 "(pid %d, terminal %s)\n",
Glenn L McGrathbaf55a82002-08-22 18:22:10 +0000536 cmdpath, getpid(), a->terminal);
Rob Landley53437472006-07-16 08:14:35 +0000537 full_write(1, press_enter, sizeof(press_enter) - 1);
Denis Vlasenkobf0a2012006-12-26 10:42:51 +0000538 while (read(0, &c, 1) == 1 && c != '\n')
Glenn L McGrathb4a1baa2003-01-13 22:09:50 +0000539 ;
Eric Andersen7ef1a5b2001-03-20 17:39:08 +0000540 }
Eric Andersen1f50e842004-08-16 09:29:42 +0000541#endif
Eric Andersen7ef1a5b2001-03-20 17:39:08 +0000542
Eric Andersen7ef1a5b2001-03-20 17:39:08 +0000543 /* Log the process name and args */
Denis Vlasenkoec27feb2007-02-17 15:52:02 +0000544 message(L_LOG, "Starting pid %d, console %s: '%s'",
Glenn L McGrathbaf55a82002-08-22 18:22:10 +0000545 getpid(), a->terminal, cmdpath);
Eric Andersen7e3bf6e2000-09-14 22:01:31 +0000546
Denis Vlasenko9b1381f2007-01-03 02:56:00 +0000547#if ENABLE_FEATURE_INIT_COREDUMPS
Bernhard Reutner-Fischer0da069d2006-05-29 12:54:16 +0000548 {
549 struct stat sb;
550 if (stat(CORE_ENABLE_FLAG_FILE, &sb) == 0) {
551 struct rlimit limit;
Glenn L McGrathbaf55a82002-08-22 18:22:10 +0000552
Bernhard Reutner-Fischer0da069d2006-05-29 12:54:16 +0000553 limit.rlim_cur = RLIM_INFINITY;
554 limit.rlim_max = RLIM_INFINITY;
555 setrlimit(RLIMIT_CORE, &limit);
556 }
Erik Andersen983b51b2000-04-04 18:14:25 +0000557 }
Erik Andersen183da4a2000-04-04 18:36:37 +0000558#endif
Erik Andersen983b51b2000-04-04 18:14:25 +0000559
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000560 /* Now run it. The new program will take over this PID,
Erik Andersene49d5ec2000-02-08 19:58:47 +0000561 * so nothing further in init.c should be run. */
Denis Vlasenko4921b542007-02-03 02:17:41 +0000562 BB_EXECVP(cmdpath, cmd);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000563
Eric Andersen7ef1a5b2001-03-20 17:39:08 +0000564 /* We're still here? Some error happened. */
Denis Vlasenkoec27feb2007-02-17 15:52:02 +0000565 message(L_LOG | L_CONSOLE, "Bummer, cannot run '%s': %m", cmdpath);
Eric Andersen0298be82002-03-05 15:12:19 +0000566 _exit(-1);
Erik Andersen05df2392000-01-13 04:43:48 +0000567 }
Eric Andersen0298be82002-03-05 15:12:19 +0000568 sigprocmask(SIG_SETMASK, &omask, NULL);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000569 return pid;
Eric Andersen0460ff21999-10-25 23:32:44 +0000570}
571
Bernhard Reutner-Fischer3ab30802006-05-30 12:10:29 +0000572static int waitfor(const struct init_action *a, pid_t pid)
Erik Andersen0e3782f2000-01-07 02:54:55 +0000573{
Bernhard Reutner-Fischer3ab30802006-05-30 12:10:29 +0000574 int runpid;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000575 int status, wpid;
Erik Andersen0e3782f2000-01-07 02:54:55 +0000576
Bernhard Reutner-Fischer3ab30802006-05-30 12:10:29 +0000577 runpid = (NULL == a)? pid : run(a);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000578 while (1) {
Bernhard Reutner-Fischerd818dcc2007-02-16 17:17:07 +0000579 wpid = waitpid(runpid, &status, 0);
Bernhard Reutner-Fischer3ab30802006-05-30 12:10:29 +0000580 if (wpid == runpid)
Erik Andersene49d5ec2000-02-08 19:58:47 +0000581 break;
Glenn L McGrathe6ba16f2003-09-26 10:45:55 +0000582 if (wpid == -1 && errno == ECHILD) {
583 /* we missed its termination */
584 break;
585 }
586 /* FIXME other errors should maybe trigger an error, but allow
587 * the program to continue */
Erik Andersen0e3782f2000-01-07 02:54:55 +0000588 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000589 return wpid;
Erik Andersen0e3782f2000-01-07 02:54:55 +0000590}
591
Eric Andersen0298be82002-03-05 15:12:19 +0000592/* Run all commands of a particular type */
593static void run_actions(int action)
Eric Andersen219d6f51999-11-02 19:43:01 +0000594{
Eric Andersen0298be82002-03-05 15:12:19 +0000595 struct init_action *a, *tmp;
Eric Andersen219d6f51999-11-02 19:43:01 +0000596
Eric Andersen0298be82002-03-05 15:12:19 +0000597 for (a = init_action_list; a; a = tmp) {
598 tmp = a->next;
Eric Andersenc97ec342001-04-03 18:01:51 +0000599 if (a->action == action) {
Rob Landley2dd42792006-03-22 17:39:13 +0000600 if (access(a->terminal, R_OK | W_OK)) {
601 delete_init_action(a);
602 } else if (a->action & (SYSINIT | WAIT | CTRLALTDEL | SHUTDOWN | RESTART)) {
Bernhard Reutner-Fischer3ab30802006-05-30 12:10:29 +0000603 waitfor(a, 0);
Eric Andersen0298be82002-03-05 15:12:19 +0000604 delete_init_action(a);
605 } else if (a->action & ONCE) {
606 run(a);
607 delete_init_action(a);
Glenn L McGrathbaf55a82002-08-22 18:22:10 +0000608 } else if (a->action & (RESPAWN | ASKFIRST)) {
Eric Andersen0298be82002-03-05 15:12:19 +0000609 /* Only run stuff with pid==0. If they have
610 * a pid, that means it is still running */
611 if (a->pid == 0) {
612 a->pid = run(a);
613 }
614 }
Erik Andersene132f4b2000-02-09 04:16:43 +0000615 }
616 }
617}
618
Bernhard Reutner-Fischer35e1a072006-05-29 13:08:35 +0000619#if !ENABLE_DEBUG_INIT
Eric Andersen2c1de612003-04-24 11:41:28 +0000620static void init_reboot(unsigned long magic)
621{
622 pid_t pid;
623 /* We have to fork here, since the kernel calls do_exit(0) in
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000624 * linux/kernel/sys.c, which can cause the machine to panic when
Eric Andersen2c1de612003-04-24 11:41:28 +0000625 * the init process is killed.... */
Denis Vlasenko9b1381f2007-01-03 02:56:00 +0000626 pid = vfork();
627 if (pid == 0) { /* child */
Eric Andersen2c1de612003-04-24 11:41:28 +0000628 reboot(magic);
Eric Andersen2c1de612003-04-24 11:41:28 +0000629 _exit(0);
630 }
Denis Vlasenko9b1381f2007-01-03 02:56:00 +0000631 waitpid(pid, NULL, 0);
Eric Andersen2c1de612003-04-24 11:41:28 +0000632}
633
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000634static void shutdown_system(void)
Eric Andersencc8ed391999-10-05 16:24:54 +0000635{
Eric Andersen813d88c2001-10-28 22:49:48 +0000636 sigset_t block_signals;
Erik Andersene132f4b2000-02-09 04:16:43 +0000637
Eric Andersena9cc8962002-09-16 06:49:06 +0000638 /* run everything to be run at "shutdown". This is done _prior_
639 * to killing everything, in case people wish to use scripts to
640 * shut things down gracefully... */
641 run_actions(SHUTDOWN);
642
Eric Andersen72f9a422001-10-28 05:12:20 +0000643 /* first disable all our signals */
644 sigemptyset(&block_signals);
645 sigaddset(&block_signals, SIGHUP);
Mike Frysingera77b4f32005-04-16 08:21:34 +0000646 sigaddset(&block_signals, SIGQUIT);
Eric Andersen72f9a422001-10-28 05:12:20 +0000647 sigaddset(&block_signals, SIGCHLD);
648 sigaddset(&block_signals, SIGUSR1);
649 sigaddset(&block_signals, SIGUSR2);
650 sigaddset(&block_signals, SIGINT);
651 sigaddset(&block_signals, SIGTERM);
Eric Andersened8a9be2001-11-30 19:10:58 +0000652 sigaddset(&block_signals, SIGCONT);
653 sigaddset(&block_signals, SIGSTOP);
654 sigaddset(&block_signals, SIGTSTP);
Eric Andersen72f9a422001-10-28 05:12:20 +0000655 sigprocmask(SIG_BLOCK, &block_signals, NULL);
Erik Andersen7dc16072000-01-04 01:10:25 +0000656
Erik Andersene49d5ec2000-02-08 19:58:47 +0000657 /* Allow Ctrl-Alt-Del to reboot system. */
Erik Andersen4f3f7572000-04-28 00:18:56 +0000658 init_reboot(RB_ENABLE_CAD);
Erik Andersene132f4b2000-02-09 04:16:43 +0000659
Denis Vlasenkoec27feb2007-02-17 15:52:02 +0000660 message(L_CONSOLE | L_LOG, "The system is going down NOW !!");
Eric Andersencf8c9cf1999-11-05 00:31:46 +0000661 sync();
Erik Andersene49d5ec2000-02-08 19:58:47 +0000662
663 /* Send signals to every process _except_ pid 1 */
Denis Vlasenkoec27feb2007-02-17 15:52:02 +0000664 message(L_CONSOLE | L_LOG, init_sending_format, "TERM");
Erik Andersene49d5ec2000-02-08 19:58:47 +0000665 kill(-1, SIGTERM);
Erik Andersene132f4b2000-02-09 04:16:43 +0000666 sleep(1);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000667 sync();
668
Denis Vlasenkoec27feb2007-02-17 15:52:02 +0000669 message(L_CONSOLE | L_LOG, init_sending_format, "KILL");
Erik Andersene49d5ec2000-02-08 19:58:47 +0000670 kill(-1, SIGKILL);
Erik Andersene132f4b2000-02-09 04:16:43 +0000671 sleep(1);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000672
Erik Andersene49d5ec2000-02-08 19:58:47 +0000673 sync();
Eric Andersencc8ed391999-10-05 16:24:54 +0000674}
675
"Vladimir N. Oleynik"1f04c9d2006-02-01 14:47:52 +0000676static void exec_signal(int sig ATTRIBUTE_UNUSED)
Eric Andersen730f8262001-12-17 23:13:08 +0000677{
Eric Andersen0298be82002-03-05 15:12:19 +0000678 struct init_action *a, *tmp;
Eric Andersen5222d312002-07-03 05:15:23 +0000679 sigset_t unblock_signals;
Glenn L McGrathbaf55a82002-08-22 18:22:10 +0000680
Eric Andersen0298be82002-03-05 15:12:19 +0000681 for (a = init_action_list; a; a = tmp) {
682 tmp = a->next;
683 if (a->action & RESTART) {
Eric Andersen730f8262001-12-17 23:13:08 +0000684 shutdown_system();
Eric Andersen5222d312002-07-03 05:15:23 +0000685
686 /* unblock all signals, blocked in shutdown_system() */
687 sigemptyset(&unblock_signals);
688 sigaddset(&unblock_signals, SIGHUP);
Mike Frysingera77b4f32005-04-16 08:21:34 +0000689 sigaddset(&unblock_signals, SIGQUIT);
Eric Andersen5222d312002-07-03 05:15:23 +0000690 sigaddset(&unblock_signals, SIGCHLD);
691 sigaddset(&unblock_signals, SIGUSR1);
692 sigaddset(&unblock_signals, SIGUSR2);
693 sigaddset(&unblock_signals, SIGINT);
694 sigaddset(&unblock_signals, SIGTERM);
695 sigaddset(&unblock_signals, SIGCONT);
696 sigaddset(&unblock_signals, SIGSTOP);
697 sigaddset(&unblock_signals, SIGTSTP);
698 sigprocmask(SIG_UNBLOCK, &unblock_signals, NULL);
699
Eric Andersen9cdef5d2003-07-29 06:33:12 +0000700 /* Close whatever files are open. */
701 close(0);
702 close(1);
703 close(2);
704
Eric Andersen3c8064f2003-07-05 08:29:01 +0000705 /* Open the new terminal device */
Bernhard Reutner-Fischer0da069d2006-05-29 12:54:16 +0000706 open_new_terminal(a->terminal, 0);
Eric Andersen3c8064f2003-07-05 08:29:01 +0000707
708 /* Make sure the terminal will act fairly normal for us */
Bernhard Reutner-Fischer49e60b92006-05-29 12:57:52 +0000709 set_term();
Eric Andersen3c8064f2003-07-05 08:29:01 +0000710 /* Setup stdout, stderr on the supplied terminal */
711 dup(0);
712 dup(0);
713
Denis Vlasenkoec27feb2007-02-17 15:52:02 +0000714 messageD(L_CONSOLE | L_LOG, "Trying to re-exec %s", a->command);
Denis Vlasenko4921b542007-02-03 02:17:41 +0000715 BB_EXECLP(a->command, a->command, NULL);
Glenn L McGrathbaf55a82002-08-22 18:22:10 +0000716
Denis Vlasenkoec27feb2007-02-17 15:52:02 +0000717 message(L_CONSOLE | L_LOG, "exec of '%s' failed: %m",
Eric Andersen71ae64b2002-10-10 04:20:21 +0000718 a->command);
Eric Andersen730f8262001-12-17 23:13:08 +0000719 sync();
720 sleep(2);
721 init_reboot(RB_HALT_SYSTEM);
722 loop_forever();
723 }
724 }
725}
726
Paul Foxd112f8f2006-06-01 13:17:49 +0000727static void shutdown_signal(int sig)
Eric Andersencc8ed391999-10-05 16:24:54 +0000728{
Denis Vlasenkoe968fcd2007-02-03 02:42:47 +0000729 const char *m;
Paul Foxd112f8f2006-06-01 13:17:49 +0000730 int rb;
731
Erik Andersene49d5ec2000-02-08 19:58:47 +0000732 shutdown_system();
Paul Foxd112f8f2006-06-01 13:17:49 +0000733
Denis Vlasenkoe968fcd2007-02-03 02:42:47 +0000734 m = "halt";
735 rb = RB_HALT_SYSTEM;
Paul Foxd112f8f2006-06-01 13:17:49 +0000736 if (sig == SIGTERM) {
737 m = "reboot";
738 rb = RB_AUTOBOOT;
739 } else if (sig == SIGUSR2) {
740 m = "poweroff";
741 rb = RB_POWER_OFF;
Paul Foxd112f8f2006-06-01 13:17:49 +0000742 }
Denis Vlasenkoec27feb2007-02-17 15:52:02 +0000743 message(L_CONSOLE | L_LOG, "Requesting system %s", m);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000744 sync();
Erik Andersen3fe39dc2000-01-25 18:13:53 +0000745
Erik Andersene49d5ec2000-02-08 19:58:47 +0000746 /* allow time for last message to reach serial console */
Erik Andersene132f4b2000-02-09 04:16:43 +0000747 sleep(2);
Erik Andersen3fe39dc2000-01-25 18:13:53 +0000748
Paul Foxd112f8f2006-06-01 13:17:49 +0000749 init_reboot(rb);
Matt Kraai67a46402001-06-03 05:55:52 +0000750 loop_forever();
Eric Andersencc8ed391999-10-05 16:24:54 +0000751}
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000752
"Vladimir N. Oleynik"1f04c9d2006-02-01 14:47:52 +0000753static void ctrlaltdel_signal(int sig ATTRIBUTE_UNUSED)
Eric Andersenc97ec342001-04-03 18:01:51 +0000754{
755 run_actions(CTRLALTDEL);
756}
757
Eric Andersen0298be82002-03-05 15:12:19 +0000758/* The SIGSTOP & SIGTSTP handler */
"Vladimir N. Oleynik"1f04c9d2006-02-01 14:47:52 +0000759static void stop_handler(int sig ATTRIBUTE_UNUSED)
Eric Andersened8a9be2001-11-30 19:10:58 +0000760{
Eric Andersen0298be82002-03-05 15:12:19 +0000761 int saved_errno = errno;
Eric Andersened8a9be2001-11-30 19:10:58 +0000762
763 got_cont = 0;
Glenn L McGrathbaf55a82002-08-22 18:22:10 +0000764 while (!got_cont)
765 pause();
Eric Andersened8a9be2001-11-30 19:10:58 +0000766 got_cont = 0;
767 errno = saved_errno;
768}
769
Glenn L McGrathbaf55a82002-08-22 18:22:10 +0000770/* The SIGCONT handler */
"Vladimir N. Oleynik"1f04c9d2006-02-01 14:47:52 +0000771static void cont_handler(int sig ATTRIBUTE_UNUSED)
Eric Andersened8a9be2001-11-30 19:10:58 +0000772{
773 got_cont = 1;
774}
775
Denis Vlasenkoe968fcd2007-02-03 02:42:47 +0000776#endif /* !ENABLE_DEBUG_INIT */
Erik Andersende552872000-01-23 01:34:05 +0000777
Glenn L McGrathdc4e75e2003-09-02 02:36:18 +0000778static void new_init_action(int action, const char *command, const char *cons)
Erik Andersen7dc16072000-01-04 01:10:25 +0000779{
Eric Andersen22718092004-10-08 08:17:39 +0000780 struct init_action *new_action, *a, *last;
Erik Andersen9e737252000-01-06 01:16:13 +0000781
Erik Andersene49d5ec2000-02-08 19:58:47 +0000782 if (*cons == '\0')
Denis Vlasenkoec27feb2007-02-17 15:52:02 +0000783 cons = console_name;
Erik Andersen9e737252000-01-06 01:16:13 +0000784
"Vladimir N. Oleynik"6c35c7c2005-10-12 15:34:25 +0000785 if (strcmp(cons, bb_dev_null) == 0 && (action & ASKFIRST))
Eric Andersen1644db92001-09-05 20:18:15 +0000786 return;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000787
Denis Vlasenko9b1381f2007-01-03 02:56:00 +0000788 new_action = xzalloc(sizeof(struct init_action));
Eric Andersen0298be82002-03-05 15:12:19 +0000789
790 /* Append to the end of the list */
Eric Andersen22718092004-10-08 08:17:39 +0000791 for (a = last = init_action_list; a; a = a->next) {
Eric Andersen82baf632004-10-08 08:21:54 +0000792 /* don't enter action if it's already in the list,
793 * but do overwrite existing actions */
Denis Vlasenkoec27feb2007-02-17 15:52:02 +0000794 if ((strcmp(a->command, command) == 0)
795 && (strcmp(a->terminal, cons) == 0)
796 ) {
Eric Andersen82baf632004-10-08 08:21:54 +0000797 a->action = action;
Eric Andersen6fd0e312003-07-22 09:48:56 +0000798 free(new_action);
799 return;
800 }
Eric Andersen22718092004-10-08 08:17:39 +0000801 last = a;
Eric Andersen6fd0e312003-07-22 09:48:56 +0000802 }
Eric Andersen22718092004-10-08 08:17:39 +0000803 if (last) {
804 last->next = new_action;
Eric Andersen1644db92001-09-05 20:18:15 +0000805 } else {
Eric Andersen0298be82002-03-05 15:12:19 +0000806 init_action_list = new_action;
Eric Andersen1644db92001-09-05 20:18:15 +0000807 }
Eric Andersen0826b6b2002-07-03 23:50:16 +0000808 strcpy(new_action->command, command);
Eric Andersen0298be82002-03-05 15:12:19 +0000809 new_action->action = action;
Eric Andersen0826b6b2002-07-03 23:50:16 +0000810 strcpy(new_action->terminal, cons);
Denis Vlasenkoec27feb2007-02-17 15:52:02 +0000811 messageD(L_LOG | L_CONSOLE, "command='%s' action='%d' terminal='%s'\n",
Glenn L McGrathb4a1baa2003-01-13 22:09:50 +0000812 new_action->command, new_action->action, new_action->terminal);
Erik Andersen7dc16072000-01-04 01:10:25 +0000813}
814
Glenn L McGrathbaf55a82002-08-22 18:22:10 +0000815static void delete_init_action(struct init_action *action)
Erik Andersen7dc16072000-01-04 01:10:25 +0000816{
Eric Andersen0298be82002-03-05 15:12:19 +0000817 struct init_action *a, *b = NULL;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000818
Eric Andersen0298be82002-03-05 15:12:19 +0000819 for (a = init_action_list; a; b = a, a = a->next) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000820 if (a == action) {
821 if (b == NULL) {
Eric Andersen0298be82002-03-05 15:12:19 +0000822 init_action_list = a->next;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000823 } else {
Eric Andersen0298be82002-03-05 15:12:19 +0000824 b->next = a->next;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000825 }
826 free(a);
827 break;
828 }
Erik Andersen7dc16072000-01-04 01:10:25 +0000829 }
Erik Andersen7dc16072000-01-04 01:10:25 +0000830}
831
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000832/* NOTE that if CONFIG_FEATURE_USE_INITTAB is NOT defined,
Erik Andersen9e737252000-01-06 01:16:13 +0000833 * then parse_inittab() simply adds in some default
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000834 * actions(i.e., runs INIT_SCRIPT and then starts a pair
835 * of "askfirst" shells). If CONFIG_FEATURE_USE_INITTAB
836 * _is_ defined, but /etc/inittab is missing, this
Erik Andersen812d4662000-01-07 18:30:40 +0000837 * results in the same set of default behaviors.
Glenn L McGrathbaf55a82002-08-22 18:22:10 +0000838 */
Eric Andersen3e6ff902001-03-09 21:24:12 +0000839static void parse_inittab(void)
Erik Andersen7dc16072000-01-04 01:10:25 +0000840{
Denis Vlasenko9b1381f2007-01-03 02:56:00 +0000841#if ENABLE_FEATURE_USE_INITTAB
Erik Andersene49d5ec2000-02-08 19:58:47 +0000842 FILE *file;
Glenn L McGrathb4a1baa2003-01-13 22:09:50 +0000843 char buf[INIT_BUFFS_SIZE], lineAsRead[INIT_BUFFS_SIZE];
844 char tmpConsole[CONSOLE_BUFF_SIZE];
Eric Andersen0298be82002-03-05 15:12:19 +0000845 char *id, *runlev, *action, *command, *eol;
846 const struct init_action_type *a = actions;
Erik Andersen7dc16072000-01-04 01:10:25 +0000847
848
Erik Andersene49d5ec2000-02-08 19:58:47 +0000849 file = fopen(INITTAB, "r");
850 if (file == NULL) {
851 /* No inittab file -- set up some default behavior */
Erik Andersen9e737252000-01-06 01:16:13 +0000852#endif
Eric Andersenc97ec342001-04-03 18:01:51 +0000853 /* Reboot on Ctrl-Alt-Del */
Denis Vlasenko2f0c0d02007-01-21 00:41:04 +0000854 new_init_action(CTRLALTDEL, "reboot", "");
Eric Andersen1644db92001-09-05 20:18:15 +0000855 /* Umount all filesystems on halt/reboot */
Denis Vlasenko2f0c0d02007-01-21 00:41:04 +0000856 new_init_action(SHUTDOWN, "umount -a -r", "");
Eric Andersen1644db92001-09-05 20:18:15 +0000857 /* Swapoff on halt/reboot */
Bernhard Reutner-Fischerd818dcc2007-02-16 17:17:07 +0000858 if (ENABLE_SWAPONOFF) new_init_action(SHUTDOWN, "swapoff -a", "");
Eric Andersen730f8262001-12-17 23:13:08 +0000859 /* Prepare to restart init when a HUP is received */
Denis Vlasenko2f0c0d02007-01-21 00:41:04 +0000860 new_init_action(RESTART, "init", "");
Eric Andersen3bc2b202002-07-29 06:39:58 +0000861 /* Askfirst shell on tty1-4 */
Glenn L McGrathdc4e75e2003-09-02 02:36:18 +0000862 new_init_action(ASKFIRST, bb_default_login_shell, "");
863 new_init_action(ASKFIRST, bb_default_login_shell, VC_2);
864 new_init_action(ASKFIRST, bb_default_login_shell, VC_3);
865 new_init_action(ASKFIRST, bb_default_login_shell, VC_4);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000866 /* sysinit */
Glenn L McGrathb4a1baa2003-01-13 22:09:50 +0000867 new_init_action(SYSINIT, INIT_SCRIPT, "");
Erik Andersen7dc16072000-01-04 01:10:25 +0000868
Erik Andersene49d5ec2000-02-08 19:58:47 +0000869 return;
Denis Vlasenko9b1381f2007-01-03 02:56:00 +0000870#if ENABLE_FEATURE_USE_INITTAB
Erik Andersen9e737252000-01-06 01:16:13 +0000871 }
Erik Andersen7dc16072000-01-04 01:10:25 +0000872
Eric Andersen0826b6b2002-07-03 23:50:16 +0000873 while (fgets(buf, INIT_BUFFS_SIZE, file) != NULL) {
Eric Andersenb5966362000-05-31 20:04:38 +0000874 /* Skip leading spaces */
875 for (id = buf; *id == ' ' || *id == '\t'; id++);
876
877 /* Skip the line if it's a comment */
878 if (*id == '#' || *id == '\n')
Erik Andersene49d5ec2000-02-08 19:58:47 +0000879 continue;
Erik Andersen7dc16072000-01-04 01:10:25 +0000880
Erik Andersene49d5ec2000-02-08 19:58:47 +0000881 /* Trim the trailing \n */
Bernhard Reutner-Fischerd818dcc2007-02-16 17:17:07 +0000882 //XXX: chomp() ?
Eric Andersenb5966362000-05-31 20:04:38 +0000883 eol = strrchr(id, '\n');
884 if (eol != NULL)
885 *eol = '\0';
Erik Andersen7dc16072000-01-04 01:10:25 +0000886
Erik Andersene49d5ec2000-02-08 19:58:47 +0000887 /* Keep a copy around for posterity's sake (and error msgs) */
888 strcpy(lineAsRead, buf);
889
Eric Andersenb5966362000-05-31 20:04:38 +0000890 /* Separate the ID field from the runlevels */
891 runlev = strchr(id, ':');
892 if (runlev == NULL || *(runlev + 1) == '\0') {
Denis Vlasenkoec27feb2007-02-17 15:52:02 +0000893 message(L_LOG | L_CONSOLE, "Bad inittab entry: %s", lineAsRead);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000894 continue;
895 } else {
Eric Andersenb5966362000-05-31 20:04:38 +0000896 *runlev = '\0';
897 ++runlev;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000898 }
899
Eric Andersenb5966362000-05-31 20:04:38 +0000900 /* Separate the runlevels from the action */
901 action = strchr(runlev, ':');
902 if (action == NULL || *(action + 1) == '\0') {
Denis Vlasenkoec27feb2007-02-17 15:52:02 +0000903 message(L_LOG | L_CONSOLE, "Bad inittab entry: %s", lineAsRead);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000904 continue;
905 } else {
Eric Andersenb5966362000-05-31 20:04:38 +0000906 *action = '\0';
907 ++action;
908 }
909
Eric Andersen0298be82002-03-05 15:12:19 +0000910 /* Separate the action from the command */
911 command = strchr(action, ':');
912 if (command == NULL || *(command + 1) == '\0') {
Denis Vlasenkoec27feb2007-02-17 15:52:02 +0000913 message(L_LOG | L_CONSOLE, "Bad inittab entry: %s", lineAsRead);
Eric Andersenb5966362000-05-31 20:04:38 +0000914 continue;
915 } else {
Eric Andersen0298be82002-03-05 15:12:19 +0000916 *command = '\0';
917 ++command;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000918 }
919
920 /* Ok, now process it */
Glenn L McGrathb4a1baa2003-01-13 22:09:50 +0000921 for (a = actions; a->name != 0; a++) {
Eric Andersenb5966362000-05-31 20:04:38 +0000922 if (strcmp(a->name, action) == 0) {
923 if (*id != '\0') {
Glenn L McGrathb4a1baa2003-01-13 22:09:50 +0000924 if(strncmp(id, "/dev/", 5) == 0)
925 id += 5;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000926 strcpy(tmpConsole, "/dev/");
Glenn L McGrathb4a1baa2003-01-13 22:09:50 +0000927 safe_strncpy(tmpConsole + 5, id,
928 CONSOLE_BUFF_SIZE - 5);
Eric Andersenb5966362000-05-31 20:04:38 +0000929 id = tmpConsole;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000930 }
Eric Andersen0298be82002-03-05 15:12:19 +0000931 new_init_action(a->action, command, id);
Glenn L McGrathb4a1baa2003-01-13 22:09:50 +0000932 break;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000933 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000934 }
Glenn L McGrathb4a1baa2003-01-13 22:09:50 +0000935 if (a->name == 0) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000936 /* Choke on an unknown action */
Denis Vlasenkoec27feb2007-02-17 15:52:02 +0000937 message(L_LOG | L_CONSOLE, "Bad inittab entry: %s", lineAsRead);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000938 }
Erik Andersen7dc16072000-01-04 01:10:25 +0000939 }
Eric Andersend8636ca2002-05-15 22:19:09 +0000940 fclose(file);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000941 return;
Denis Vlasenko9b1381f2007-01-03 02:56:00 +0000942#endif /* FEATURE_USE_INITTAB */
Erik Andersen7dc16072000-01-04 01:10:25 +0000943}
944
Denis Vlasenko9b1381f2007-01-03 02:56:00 +0000945#if ENABLE_FEATURE_USE_INITTAB
"Vladimir N. Oleynik"1f04c9d2006-02-01 14:47:52 +0000946static void reload_signal(int sig ATTRIBUTE_UNUSED)
Eric Andersen6fd0e312003-07-22 09:48:56 +0000947{
Eric Andersen82baf632004-10-08 08:21:54 +0000948 struct init_action *a, *tmp;
949
Denis Vlasenkoec27feb2007-02-17 15:52:02 +0000950 message(L_LOG, "Reloading /etc/inittab");
Eric Andersen82baf632004-10-08 08:21:54 +0000951
952 /* disable old entrys */
953 for (a = init_action_list; a; a = a->next ) {
954 a->action = ONCE;
955 }
956
957 parse_inittab();
958
959 /* remove unused entrys */
960 for (a = init_action_list; a; a = tmp) {
961 tmp = a->next;
962 if (a->action & (ONCE | SYSINIT | WAIT ) &&
963 a->pid == 0 ) {
964 delete_init_action(a);
965 }
966 }
Eric Andersen6fd0e312003-07-22 09:48:56 +0000967 run_actions(RESPAWN);
Eric Andersen82baf632004-10-08 08:21:54 +0000968 return;
Eric Andersen6fd0e312003-07-22 09:48:56 +0000969}
Denis Vlasenko9b1381f2007-01-03 02:56:00 +0000970#endif /* FEATURE_USE_INITTAB */
Eric Andersen82baf632004-10-08 08:21:54 +0000971
Denis Vlasenko06af2162007-02-03 17:28:39 +0000972int init_main(int argc, char **argv);
Rob Landleydfba7412006-03-06 20:47:33 +0000973int init_main(int argc, char **argv)
Eric Andersen0460ff21999-10-25 23:32:44 +0000974{
Eric Andersen0298be82002-03-05 15:12:19 +0000975 struct init_action *a;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000976 pid_t wpid;
Eric Andersen0460ff21999-10-25 23:32:44 +0000977
Denis Vlasenko9b1381f2007-01-03 02:56:00 +0000978 die_sleep = 30 * 24*60*60; /* if xmalloc will ever die... */
979
Eric Andersen730f8262001-12-17 23:13:08 +0000980 if (argc > 1 && !strcmp(argv[1], "-q")) {
Rob Landley2edf5262006-01-22 02:41:51 +0000981 return kill(1,SIGHUP);
Eric Andersen730f8262001-12-17 23:13:08 +0000982 }
Bernhard Reutner-Fischer35e1a072006-05-29 13:08:35 +0000983#if !ENABLE_DEBUG_INIT
Erik Andersena51ecdd2000-02-24 18:09:58 +0000984 /* Expect to be invoked as init with PID=1 or be invoked as linuxrc */
Rob Landley64612912006-01-30 08:31:37 +0000985 if (getpid() != 1 &&
Denis Vlasenko8f8f2682006-10-03 21:00:43 +0000986 (!ENABLE_FEATURE_INITRD || !strstr(applet_name, "linuxrc")))
Rob Landley64612912006-01-30 08:31:37 +0000987 {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000988 bb_show_usage();
Erik Andersene49d5ec2000-02-08 19:58:47 +0000989 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000990 /* Set up sig handlers -- be sure to
991 * clear all of these in run() */
Glenn L McGrathbaf55a82002-08-22 18:22:10 +0000992 signal(SIGHUP, exec_signal);
Mike Frysingera77b4f32005-04-16 08:21:34 +0000993 signal(SIGQUIT, exec_signal);
Paul Foxd112f8f2006-06-01 13:17:49 +0000994 signal(SIGUSR1, shutdown_signal);
995 signal(SIGUSR2, shutdown_signal);
Glenn L McGrathbaf55a82002-08-22 18:22:10 +0000996 signal(SIGINT, ctrlaltdel_signal);
Paul Foxd112f8f2006-06-01 13:17:49 +0000997 signal(SIGTERM, shutdown_signal);
Eric Andersened8a9be2001-11-30 19:10:58 +0000998 signal(SIGCONT, cont_handler);
999 signal(SIGSTOP, stop_handler);
1000 signal(SIGTSTP, stop_handler);
Eric Andersen0460ff21999-10-25 23:32:44 +00001001
Eric Andersenc7bda1c2004-03-15 08:29:22 +00001002 /* Turn off rebooting via CTL-ALT-DEL -- we get a
Erik Andersene49d5ec2000-02-08 19:58:47 +00001003 * SIGINT on CAD so we can shut things down gracefully... */
Erik Andersen4f3f7572000-04-28 00:18:56 +00001004 init_reboot(RB_DISABLE_CAD);
Erik Andersene49d5ec2000-02-08 19:58:47 +00001005#endif
Erik Andersen05df2392000-01-13 04:43:48 +00001006
Eric Andersen599e3ce2002-07-03 11:08:10 +00001007 /* Figure out where the default console should be */
1008 console_init();
1009
Erik Andersene49d5ec2000-02-08 19:58:47 +00001010 /* Close whatever files are open, and reset the console. */
1011 close(0);
1012 close(1);
1013 close(2);
Eric Andersen72f9a422001-10-28 05:12:20 +00001014
Denis Vlasenkoec27feb2007-02-17 15:52:02 +00001015 if (device_open(console_name, O_RDWR | O_NOCTTY) == 0) {
Bernhard Reutner-Fischer49e60b92006-05-29 12:57:52 +00001016 set_term();
Eric Andersen599e3ce2002-07-03 11:08:10 +00001017 close(0);
1018 }
1019
Erik Andersen983b51b2000-04-04 18:14:25 +00001020 chdir("/");
Erik Andersene49d5ec2000-02-08 19:58:47 +00001021 setsid();
Glenn L McGrathb4a1baa2003-01-13 22:09:50 +00001022 {
1023 const char * const *e;
1024 /* Make sure environs is set to something sane */
Denis Vlasenkobf0a2012006-12-26 10:42:51 +00001025 for (e = environment; *e; e++)
Manuel Novoa III cad53642003-03-19 09:13:01 +00001026 putenv((char *) *e);
Glenn L McGrathb4a1baa2003-01-13 22:09:50 +00001027 }
Rob Landleycba1b962006-07-09 17:28:17 +00001028
1029 if (argc > 1) setenv("RUNLEVEL", argv[1], 1);
1030
Erik Andersene49d5ec2000-02-08 19:58:47 +00001031 /* Hello world */
Denis Vlasenkoec27feb2007-02-17 15:52:02 +00001032 message(MAYBE_CONSOLE | L_LOG, "init started: %s", bb_msg_full_version);
Eric Andersencc8ed391999-10-05 16:24:54 +00001033
Erik Andersene49d5ec2000-02-08 19:58:47 +00001034 /* Make sure there is enough memory to do something useful. */
Rob Landleyc3386a42005-08-30 18:50:37 +00001035 if (ENABLE_SWAPONOFF) {
1036 struct sysinfo info;
1037
1038 if (!sysinfo(&info) &&
Denis Vlasenkodca0b702006-10-27 09:05:02 +00001039 (info.mem_unit ? : 1) * (long long)info.totalram < 1024*1024)
Rob Landleyc3386a42005-08-30 18:50:37 +00001040 {
Denis Vlasenkoec27feb2007-02-17 15:52:02 +00001041 message(L_CONSOLE, "Low memory: forcing swapon.");
Rob Landleyc3386a42005-08-30 18:50:37 +00001042 /* swapon -a requires /proc typically */
Denis Vlasenko2f0c0d02007-01-21 00:41:04 +00001043 new_init_action(SYSINIT, "mount -t proc proc /proc", "");
Rob Landleyc3386a42005-08-30 18:50:37 +00001044 /* Try to turn on swap */
Denis Vlasenko2f0c0d02007-01-21 00:41:04 +00001045 new_init_action(SYSINIT, "swapon -a", "");
Rob Landleyc3386a42005-08-30 18:50:37 +00001046 run_actions(SYSINIT); /* wait and removing */
1047 }
1048 }
Erik Andersen9e737252000-01-06 01:16:13 +00001049
Erik Andersene49d5ec2000-02-08 19:58:47 +00001050 /* Check if we are supposed to be in single user mode */
Denis Vlasenkobf66fbc2006-12-21 13:23:14 +00001051 if (argc > 1
1052 && (!strcmp(argv[1], "single") || !strcmp(argv[1], "-s") || LONE_CHAR(argv[1], '1'))
1053 ) {
Glenn L McGrathb4a1baa2003-01-13 22:09:50 +00001054 /* Start a shell on console */
Glenn L McGrathdc4e75e2003-09-02 02:36:18 +00001055 new_init_action(RESPAWN, bb_default_login_shell, "");
Erik Andersene49d5ec2000-02-08 19:58:47 +00001056 } else {
1057 /* Not in single user mode -- see what inittab says */
Eric Andersen8a8fbb81999-10-26 00:18:56 +00001058
Eric Andersenbdfd0d72001-10-24 05:00:29 +00001059 /* NOTE that if CONFIG_FEATURE_USE_INITTAB is NOT defined,
Erik Andersene49d5ec2000-02-08 19:58:47 +00001060 * then parse_inittab() simply adds in some default
Eric Andersenc7bda1c2004-03-15 08:29:22 +00001061 * actions(i.e., runs INIT_SCRIPT and then starts a pair
Erik Andersene49d5ec2000-02-08 19:58:47 +00001062 * of "askfirst" shells */
1063 parse_inittab();
Eric Andersend00c2621999-12-07 08:37:31 +00001064 }
Erik Andersen983b51b2000-04-04 18:14:25 +00001065
Denis Vlasenko9b1381f2007-01-03 02:56:00 +00001066#if ENABLE_SELINUX
Rob Landleyb3ede5a2006-03-27 23:09:12 +00001067 if (getenv("SELINUX_INIT") == NULL) {
1068 int enforce = 0;
1069
1070 putenv("SELINUX_INIT=YES");
1071 if (selinux_init_load_policy(&enforce) == 0) {
Denis Vlasenko4921b542007-02-03 02:17:41 +00001072 BB_EXECVP(argv[0], argv);
Rob Landleyb3ede5a2006-03-27 23:09:12 +00001073 } else if (enforce > 0) {
1074 /* SELinux in enforcing mode but load_policy failed */
1075 /* At this point, we probably can't open /dev/console, so log() won't work */
1076 message(CONSOLE,"Unable to load SELinux Policy. Machine is in enforcing mode. Halting now.");
1077 exit(1);
1078 }
1079 }
1080#endif /* CONFIG_SELINUX */
1081
Eric Andersen7ef1a5b2001-03-20 17:39:08 +00001082 /* Make the command line just say "init" -- thats all, nothing else */
1083 fixup_argv(argc, argv, "init");
Eric Andersen219d6f51999-11-02 19:43:01 +00001084
Erik Andersene49d5ec2000-02-08 19:58:47 +00001085 /* Now run everything that needs to be run */
1086
1087 /* First run the sysinit command */
Eric Andersen1644db92001-09-05 20:18:15 +00001088 run_actions(SYSINIT);
Eric Andersen0298be82002-03-05 15:12:19 +00001089
Erik Andersene49d5ec2000-02-08 19:58:47 +00001090 /* Next run anything that wants to block */
Eric Andersen1644db92001-09-05 20:18:15 +00001091 run_actions(WAIT);
Eric Andersen0298be82002-03-05 15:12:19 +00001092
Erik Andersene49d5ec2000-02-08 19:58:47 +00001093 /* Next run anything to be run only once */
Eric Andersen0298be82002-03-05 15:12:19 +00001094 run_actions(ONCE);
1095
Denis Vlasenko9b1381f2007-01-03 02:56:00 +00001096#if ENABLE_FEATURE_USE_INITTAB
Eric Andersen6fd0e312003-07-22 09:48:56 +00001097 /* Redefine SIGHUP to reread /etc/inittab */
1098 signal(SIGHUP, reload_signal);
Eric Andersen82baf632004-10-08 08:21:54 +00001099#else
1100 signal(SIGHUP, SIG_IGN);
Denis Vlasenko9b1381f2007-01-03 02:56:00 +00001101#endif /* FEATURE_USE_INITTAB */
Eric Andersen82baf632004-10-08 08:21:54 +00001102
Eric Andersen6fd0e312003-07-22 09:48:56 +00001103
Erik Andersene49d5ec2000-02-08 19:58:47 +00001104 /* Now run the looping stuff for the rest of forever */
1105 while (1) {
Eric Andersen0298be82002-03-05 15:12:19 +00001106 /* run the respawn stuff */
1107 run_actions(RESPAWN);
1108
1109 /* run the askfirst stuff */
1110 run_actions(ASKFIRST);
1111
1112 /* Don't consume all CPU time -- sleep a bit */
1113 sleep(1);
1114
Erik Andersene49d5ec2000-02-08 19:58:47 +00001115 /* Wait for a child process to exit */
Bernhard Reutner-Fischerc58dbf22006-05-30 12:16:54 +00001116 wpid = wait(NULL);
Eric Andersen87812dc2004-04-12 19:21:54 +00001117 while (wpid > 0) {
Erik Andersene49d5ec2000-02-08 19:58:47 +00001118 /* Find out who died and clean up their corpse */
Eric Andersen0298be82002-03-05 15:12:19 +00001119 for (a = init_action_list; a; a = a->next) {
Erik Andersene49d5ec2000-02-08 19:58:47 +00001120 if (a->pid == wpid) {
Eric Andersen0298be82002-03-05 15:12:19 +00001121 /* Set the pid to 0 so that the process gets
1122 * restarted by run_actions() */
Erik Andersene49d5ec2000-02-08 19:58:47 +00001123 a->pid = 0;
Denis Vlasenkoec27feb2007-02-17 15:52:02 +00001124 message(L_LOG, "Process '%s' (pid %d) exited. "
Glenn L McGrathb4a1baa2003-01-13 22:09:50 +00001125 "Scheduling it for restart.",
1126 a->command, wpid);
Erik Andersene49d5ec2000-02-08 19:58:47 +00001127 }
1128 }
Eric Andersencf1fee02002-12-17 09:48:16 +00001129 /* see if anyone else is waiting to be reaped */
Denis Vlasenkoce979602006-09-27 23:31:08 +00001130 wpid = waitpid(-1, NULL, WNOHANG);
Erik Andersene49d5ec2000-02-08 19:58:47 +00001131 }
Erik Andersene49d5ec2000-02-08 19:58:47 +00001132 }
Eric Andersencc8ed391999-10-05 16:24:54 +00001133}