blob: ce1f7391a2be4e5bcf6cdae0d91d9075d76b8092 [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 Andersencc8ed391999-10-05 16:24:54 +000013#include <errno.h>
Eric Andersenb186d981999-12-03 09:19:54 +000014#include <paths.h>
Erik Andersen42094cd2000-03-20 21:34:52 +000015#include <signal.h>
Erik Andersen42094cd2000-03-20 21:34:52 +000016#include <sys/ioctl.h>
Erik Andersen42094cd2000-03-20 21:34:52 +000017#include <sys/wait.h>
Eric Andersen85e5e722003-07-22 08:56:55 +000018#include <sys/reboot.h>
Eric Andersenb01ed652003-06-27 17:08:15 +000019
20#include "init_shared.h"
21
Denis Vlasenko9b1381f2007-01-03 02:56:00 +000022#if ENABLE_SYSLOGD
Erik Andersen4f3f7572000-04-28 00:18:56 +000023# include <sys/syslog.h>
24#endif
Eric Andersen2c1de612003-04-24 11:41:28 +000025
Eric Andersen0826b6b2002-07-03 23:50:16 +000026#define INIT_BUFFS_SIZE 256
27
Eric Andersenbd22ed82000-07-08 18:55:24 +000028/* From <linux/vt.h> */
29struct vt_stat {
Glenn L McGrathbaf55a82002-08-22 18:22:10 +000030 unsigned short v_active; /* active vt */
31 unsigned short v_signal; /* signal to send */
32 unsigned short v_state; /* vt bitmask */
Eric Andersenbd22ed82000-07-08 18:55:24 +000033};
Rob Landleybc68cd12006-03-10 19:22:06 +000034enum { VT_GETSTATE = 0x5603 }; /* get global vt state info */
Eric Andersenbd22ed82000-07-08 18:55:24 +000035
36/* From <linux/serial.h> */
37struct serial_struct {
Eric Andersen81155f82003-09-11 08:52:22 +000038 int type;
39 int line;
40 unsigned int port;
41 int irq;
42 int flags;
43 int xmit_fifo_size;
44 int custom_divisor;
45 int baud_base;
46 unsigned short close_delay;
47 char io_type;
48 char reserved_char[1];
49 int hub6;
50 unsigned short closing_wait; /* time to wait before closing */
51 unsigned short closing_wait2; /* no longer used... */
52 unsigned char *iomem_base;
53 unsigned short iomem_reg_shift;
54 unsigned int port_high;
55 unsigned long iomap_base; /* cookie passed into ioremap */
56 int reserved[1];
Eric Andersenbd22ed82000-07-08 18:55:24 +000057};
58
Eric Andersen41492d62001-02-23 00:05:56 +000059#ifndef _PATH_STDPATH
Erik Andersen4f3f7572000-04-28 00:18:56 +000060#define _PATH_STDPATH "/usr/bin:/bin:/usr/sbin:/sbin"
Eric Andersen41492d62001-02-23 00:05:56 +000061#endif
Eric Andersencc8ed391999-10-05 16:24:54 +000062
Denis Vlasenko9b1381f2007-01-03 02:56:00 +000063#if ENABLE_FEATURE_INIT_COREDUMPS
Erik Andersen983b51b2000-04-04 18:14:25 +000064/*
Eric Andersenc7bda1c2004-03-15 08:29:22 +000065 * When a file named CORE_ENABLE_FLAG_FILE exists, setrlimit is called
Erik Andersen183da4a2000-04-04 18:36:37 +000066 * before processes are spawned to set core file size as unlimited.
67 * This is for debugging only. Don't use this is production, unless
68 * you want core dumps lying about....
Erik Andersen983b51b2000-04-04 18:14:25 +000069 */
70#define CORE_ENABLE_FLAG_FILE "/.init_enable_core"
71#include <sys/resource.h>
Erik Andersen183da4a2000-04-04 18:36:37 +000072#endif
Erik Andersen983b51b2000-04-04 18:14:25 +000073
Glenn L McGrathbaf55a82002-08-22 18:22:10 +000074#define INITTAB "/etc/inittab" /* inittab file location */
Erik Andersen96e2abd2000-01-07 11:40:44 +000075#ifndef INIT_SCRIPT
Glenn L McGrathbaf55a82002-08-22 18:22:10 +000076#define INIT_SCRIPT "/etc/init.d/rcS" /* Default sysinit script. */
Erik Andersen96e2abd2000-01-07 11:40:44 +000077#endif
Erik Andersen9c88cac1999-12-30 09:25:17 +000078
Eric Andersen41492d62001-02-23 00:05:56 +000079#define MAXENV 16 /* Number of env. vars */
Erik Andersen7dc16072000-01-04 01:10:25 +000080
Glenn L McGrathb4a1baa2003-01-13 22:09:50 +000081#define CONSOLE_BUFF_SIZE 32
82
Erik Andersen7dc16072000-01-04 01:10:25 +000083/* Allowed init action types */
Eric Andersen0298be82002-03-05 15:12:19 +000084#define SYSINIT 0x001
85#define RESPAWN 0x002
86#define ASKFIRST 0x004
87#define WAIT 0x008
88#define ONCE 0x010
89#define CTRLALTDEL 0x020
90#define SHUTDOWN 0x040
91#define RESTART 0x080
Erik Andersen7dc16072000-01-04 01:10:25 +000092
Erik Andersen42094cd2000-03-20 21:34:52 +000093/* A mapping between "inittab" action name strings and action type codes. */
Eric Andersen0298be82002-03-05 15:12:19 +000094struct init_action_type {
Erik Andersene49d5ec2000-02-08 19:58:47 +000095 const char *name;
Eric Andersen0298be82002-03-05 15:12:19 +000096 int action;
97};
Erik Andersen7dc16072000-01-04 01:10:25 +000098
Eric Andersen0298be82002-03-05 15:12:19 +000099static const struct init_action_type actions[] = {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000100 {"sysinit", SYSINIT},
101 {"respawn", RESPAWN},
102 {"askfirst", ASKFIRST},
103 {"wait", WAIT},
104 {"once", ONCE},
Erik Andersene132f4b2000-02-09 04:16:43 +0000105 {"ctrlaltdel", CTRLALTDEL},
Eric Andersenc97ec342001-04-03 18:01:51 +0000106 {"shutdown", SHUTDOWN},
Eric Andersen730f8262001-12-17 23:13:08 +0000107 {"restart", RESTART},
Pavel Roskin9027bcf2000-07-14 15:44:25 +0000108 {0, 0}
Erik Andersen7dc16072000-01-04 01:10:25 +0000109};
110
Eric Andersen0298be82002-03-05 15:12:19 +0000111/* Set up a linked list of init_actions, to be read from inittab */
112struct init_action {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000113 pid_t pid;
Eric Andersen0826b6b2002-07-03 23:50:16 +0000114 char command[INIT_BUFFS_SIZE];
Glenn L McGrathb4a1baa2003-01-13 22:09:50 +0000115 char terminal[CONSOLE_BUFF_SIZE];
Eric Andersen0298be82002-03-05 15:12:19 +0000116 struct init_action *next;
117 int action;
Erik Andersen7dc16072000-01-04 01:10:25 +0000118};
Erik Andersen7dc16072000-01-04 01:10:25 +0000119
Eric Andersen0298be82002-03-05 15:12:19 +0000120/* Static variables */
121static struct init_action *init_action_list = NULL;
Bernhard Reutner-Fischercf1f2ac2006-06-02 10:43:17 +0000122static char console[CONSOLE_BUFF_SIZE] = CONSOLE_DEV;
Glenn L McGrathbaf55a82002-08-22 18:22:10 +0000123
Denis Vlasenko9b1381f2007-01-03 02:56:00 +0000124#if !ENABLE_SYSLOGD
Denis Vlasenko4c978632007-02-03 03:31:13 +0000125static const char *log_console = VC_5;
Eric Andersene7078062002-07-29 08:00:16 +0000126#endif
Bernhard Reutner-Fischercf1f2ac2006-06-02 10:43:17 +0000127#if !ENABLE_DEBUG_INIT
Eric Andersen0298be82002-03-05 15:12:19 +0000128static sig_atomic_t got_cont = 0;
Bernhard Reutner-Fischercf1f2ac2006-06-02 10:43:17 +0000129#endif
Rob Landleybc68cd12006-03-10 19:22:06 +0000130
131enum {
132 LOG = 0x1,
133 CONSOLE = 0x2,
Glenn L McGrathbaf55a82002-08-22 18:22:10 +0000134
Denis Vlasenko9b1381f2007-01-03 02:56:00 +0000135#if ENABLE_FEATURE_EXTRA_QUIET
Rob Landleybc68cd12006-03-10 19:22:06 +0000136 MAYBE_CONSOLE = 0x0,
Eric Andersen0298be82002-03-05 15:12:19 +0000137#else
Rob Landleybc68cd12006-03-10 19:22:06 +0000138 MAYBE_CONSOLE = CONSOLE,
Eric Andersen0298be82002-03-05 15:12:19 +0000139#endif
Glenn L McGrathbaf55a82002-08-22 18:22:10 +0000140
Rob Landleybc68cd12006-03-10 19:22:06 +0000141#ifndef RB_HALT_SYSTEM
Bernhard Reutner-Fischercf1f2ac2006-06-02 10:43:17 +0000142 RB_HALT_SYSTEM = 0xcdef0123, /* FIXME: this overflows enum */
Rob Landleybc68cd12006-03-10 19:22:06 +0000143 RB_ENABLE_CAD = 0x89abcdef,
144 RB_DISABLE_CAD = 0,
145 RB_POWER_OFF = 0x4321fedc,
146 RB_AUTOBOOT = 0x01234567,
Eric Andersen0298be82002-03-05 15:12:19 +0000147#endif
Rob Landleybc68cd12006-03-10 19:22:06 +0000148};
Erik Andersen42094cd2000-03-20 21:34:52 +0000149
Glenn L McGrathb4a1baa2003-01-13 22:09:50 +0000150static const char * const environment[] = {
151 "HOME=/",
152 "PATH=" _PATH_STDPATH,
Glenn L McGrathdc4e75e2003-09-02 02:36:18 +0000153 "SHELL=/bin/sh",
Glenn L McGrathb4a1baa2003-01-13 22:09:50 +0000154 "USER=root",
155 NULL
156};
157
Eric Andersen0298be82002-03-05 15:12:19 +0000158/* Function prototypes */
159static void delete_init_action(struct init_action *a);
Bernhard Reutner-Fischer3ab30802006-05-30 12:10:29 +0000160static int waitfor(const struct init_action *a, pid_t pid);
Bernhard Reutner-Fischercf1f2ac2006-06-02 10:43:17 +0000161#if !ENABLE_DEBUG_INIT
Paul Foxd112f8f2006-06-01 13:17:49 +0000162static void shutdown_signal(int sig);
Bernhard Reutner-Fischercf1f2ac2006-06-02 10:43:17 +0000163#endif
Eric Andersen0460ff21999-10-25 23:32:44 +0000164
Denis Vlasenko9b1381f2007-01-03 02:56:00 +0000165#if !ENABLE_DEBUG_INIT
Eric Andersen74400cc2001-10-18 04:11:39 +0000166static void loop_forever(void)
Matt Kraai67a46402001-06-03 05:55:52 +0000167{
168 while (1)
Glenn L McGrathbaf55a82002-08-22 18:22:10 +0000169 sleep(1);
Matt Kraai67a46402001-06-03 05:55:52 +0000170}
Denis Vlasenko9b1381f2007-01-03 02:56:00 +0000171#endif
Eric Andersen7ef1a5b2001-03-20 17:39:08 +0000172
Erik Andersen42094cd2000-03-20 21:34:52 +0000173/* Print a message to the specified device.
174 * Device may be bitwise-or'd from LOG | CONSOLE */
Bernhard Reutner-Fischer35e1a072006-05-29 13:08:35 +0000175#if ENABLE_DEBUG_INIT
176#define messageD message
177#else
Denis Vlasenkobf0a2012006-12-26 10:42:51 +0000178#define messageD(...) do {} while (0)
Eric Andersenfedce062001-11-17 07:27:14 +0000179#endif
Glenn L McGrathb4a1baa2003-01-13 22:09:50 +0000180static void message(int device, const char *fmt, ...)
Glenn L McGrathbaf55a82002-08-22 18:22:10 +0000181 __attribute__ ((format(printf, 2, 3)));
Glenn L McGrathb4a1baa2003-01-13 22:09:50 +0000182static void message(int device, const char *fmt, ...)
Eric Andersen0460ff21999-10-25 23:32:44 +0000183{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000184 va_list arguments;
Glenn L McGrathb4a1baa2003-01-13 22:09:50 +0000185 int l;
Bernhard Reutner-Fischer3ab30802006-05-30 12:10:29 +0000186 RESERVE_CONFIG_BUFFER(msg, 1024);
Denis Vlasenko9b1381f2007-01-03 02:56:00 +0000187#if !ENABLE_SYSLOGD
Eric Andersen22237012003-01-23 07:08:26 +0000188 static int log_fd = -1;
189#endif
Eric Andersen4c781471999-11-26 08:12:56 +0000190
Glenn L McGrathb4a1baa2003-01-13 22:09:50 +0000191 msg[0] = '\r';
Erik Andersene49d5ec2000-02-08 19:58:47 +0000192 va_start(arguments, fmt);
Bernhard Reutner-Fischer3ab30802006-05-30 12:10:29 +0000193 l = vsnprintf(msg + 1, 1024 - 2, fmt, arguments) + 1;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000194 va_end(arguments);
Glenn L McGrathb4a1baa2003-01-13 22:09:50 +0000195
Denis Vlasenko9b1381f2007-01-03 02:56:00 +0000196#if ENABLE_SYSLOGD
Glenn L McGrathb4a1baa2003-01-13 22:09:50 +0000197 /* Log the message to syslogd */
198 if (device & LOG) {
199 /* don`t out "\r\n" */
Denis Vlasenko8f8f2682006-10-03 21:00:43 +0000200 openlog(applet_name, 0, LOG_DAEMON);
"Vladimir N. Oleynik"8e1bd4a2005-09-29 12:55:21 +0000201 syslog(LOG_INFO, "%s", msg + 1);
Eric Andersen36adca82004-06-22 10:07:17 +0000202 closelog();
Eric Andersen3ae0c781999-11-04 01:13:21 +0000203 }
Glenn L McGrathb4a1baa2003-01-13 22:09:50 +0000204
205 msg[l++] = '\n';
206 msg[l] = 0;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000207#else
Erik Andersene49d5ec2000-02-08 19:58:47 +0000208
Glenn L McGrathb4a1baa2003-01-13 22:09:50 +0000209 msg[l++] = '\n';
210 msg[l] = 0;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000211 /* Take full control of the log tty, and never close it.
212 * It's mine, all mine! Muhahahaha! */
213 if (log_fd < 0) {
Mike Frysingere548bdf2005-07-06 04:46:14 +0000214 if ((log_fd = device_open(log_console, O_RDWR | O_NONBLOCK | O_NOCTTY)) < 0) {
Erik Andersene132f4b2000-02-09 04:16:43 +0000215 log_fd = -2;
Denis Vlasenkoe1a0d482006-10-20 13:28:22 +0000216 bb_error_msg("bummer, can't write to log on %s!", log_console);
Erik Andersene132f4b2000-02-09 04:16:43 +0000217 device = 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 }
222 if ((device & LOG) && (log_fd >= 0)) {
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
Erik Andersene49d5ec2000-02-08 19:58:47 +0000227 if (device & CONSOLE) {
Bernhard Reutner-Fischercf1f2ac2006-06-02 10:43:17 +0000228 int fd = device_open(CONSOLE_DEV,
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) {
Matt Kraai18447702001-05-18 21:24:58 +0000294 safe_strncpy(console, s, sizeof(console));
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 */
Matt Kraai439e3df2001-07-23 14:52:08 +0000299 snprintf(console, sizeof(console) - 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 */
Matt Kraai439e3df2001-07-23 14:52:08 +0000302 snprintf(console, sizeof(console) - 1, VC_FORMAT, vt.v_active);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000303 } else {
Bernhard Reutner-Fischercf1f2ac2006-06-02 10:43:17 +0000304 safe_strncpy(console, CONSOLE_DEV, sizeof(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
Glenn L McGrathb4a1baa2003-01-13 22:09:50 +0000309 while ((fd = open(console, O_RDONLY | O_NONBLOCK)) < 0 && tried < 2) {
310 /* Can't open selected console -- try
311 logical system console and VT_MASTER */
Bernhard Reutner-Fischercf1f2ac2006-06-02 10:43:17 +0000312 safe_strncpy(console, (tried == 0 ? CONSOLE_DEV : CURRENT_VC),
Glenn L McGrathb4a1baa2003-01-13 22:09:50 +0000313 sizeof(console));
314 tried++;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000315 }
316 if (fd < 0) {
317 /* Perhaps we should panic here? */
Denis Vlasenko9b1381f2007-01-03 02:56:00 +0000318#if !ENABLE_SYSLOGD
Mike Frysinger72a4c332005-07-05 02:19:20 +0000319 log_console =
Glenn L McGrathb4a1baa2003-01-13 22:09:50 +0000320#endif
"Vladimir N. Oleynik"6c35c7c2005-10-12 15:34:25 +0000321 safe_strncpy(console, bb_dev_null, sizeof(console));
Eric Andersen07e52971999-11-07 07:38:08 +0000322 } else {
Glenn L McGrathb4a1baa2003-01-13 22:09:50 +0000323 s = getenv("TERM");
Eric Andersen3bc2b202002-07-29 06:39:58 +0000324 /* check for serial console */
Glenn L McGrathb4a1baa2003-01-13 22:09:50 +0000325 if (ioctl(fd, TIOCGSERIAL, &sr) == 0) {
Erik Andersenfa4718e2000-02-21 19:25:12 +0000326 /* Force the TERM setting to vt102 for serial console --
Eric Andersen3bc2b202002-07-29 06:39:58 +0000327 * if TERM is set to linux (the default) */
Glenn L McGrathb4a1baa2003-01-13 22:09:50 +0000328 if (s == NULL || strcmp(s, "linux") == 0)
Denis Vlasenko06c0a712007-01-29 22:51:44 +0000329 putenv((char*)"TERM=vt102");
Denis Vlasenko9b1381f2007-01-03 02:56:00 +0000330#if !ENABLE_SYSLOGD
Mike Frysinger72a4c332005-07-05 02:19:20 +0000331 log_console = console;
Glenn L McGrathb4a1baa2003-01-13 22:09:50 +0000332#endif
333 } else {
334 if (s == NULL)
Denis Vlasenko06c0a712007-01-29 22:51:44 +0000335 putenv((char*)"TERM=linux");
Erik Andersene49d5ec2000-02-08 19:58:47 +0000336 }
337 close(fd);
Eric Andersen07e52971999-11-07 07:38:08 +0000338 }
Glenn L McGrathb4a1baa2003-01-13 22:09:50 +0000339 messageD(LOG, "console=%s", console);
Eric Andersen0460ff21999-10-25 23:32:44 +0000340}
Glenn L McGrathbaf55a82002-08-22 18:22:10 +0000341
Denis Vlasenko06c0a712007-01-29 22:51:44 +0000342static void fixup_argv(int argc, char **argv, const char *new_argv0)
Eric Andersen7ef1a5b2001-03-20 17:39:08 +0000343{
344 int len;
Glenn L McGrathbaf55a82002-08-22 18:22:10 +0000345
Eric Andersen7ef1a5b2001-03-20 17:39:08 +0000346 /* Fix up argv[0] to be certain we claim to be init */
347 len = strlen(argv[0]);
Denis Vlasenko06c0a712007-01-29 22:51:44 +0000348 strncpy(argv[0], new_argv0, len);
Eric Andersen7ef1a5b2001-03-20 17:39:08 +0000349
350 /* Wipe argv[1]-argv[N] so they don't clutter the ps listing */
351 len = 1;
352 while (argc > len) {
353 memset(argv[len], 0, strlen(argv[len]));
354 len++;
355 }
356}
357
Bernhard Reutner-Fischer0da069d2006-05-29 12:54:16 +0000358/* Open the new terminal device */
Bernhard Reutner-Fischercf1f2ac2006-06-02 10:43:17 +0000359static void open_new_terminal(const char * const device, const int fail) {
Bernhard Reutner-Fischer0da069d2006-05-29 12:54:16 +0000360 struct stat sb;
361
362 if ((device_open(device, O_RDWR)) < 0) {
363 if (stat(device, &sb) != 0) {
364 message(LOG | CONSOLE, "device '%s' does not exist.", device);
365 } else {
366 message(LOG | CONSOLE, "Bummer, can't open %s", device);
367 }
368 if (fail)
369 _exit(1);
370 /* else */
Bernhard Reutner-Fischercf1f2ac2006-06-02 10:43:17 +0000371#if !ENABLE_DEBUG_INIT
Paul Foxd112f8f2006-06-01 13:17:49 +0000372 shutdown_signal(SIGUSR1);
Bernhard Reutner-Fischercf1f2ac2006-06-02 10:43:17 +0000373#else
374 _exit(2);
375#endif
Bernhard Reutner-Fischer0da069d2006-05-29 12:54:16 +0000376 }
377}
378
Glenn L McGrathb4a1baa2003-01-13 22:09:50 +0000379static pid_t run(const struct init_action *a)
Eric Andersen0298be82002-03-05 15:12:19 +0000380{
Bernhard Reutner-Fischer3ab30802006-05-30 12:10:29 +0000381 int i;
Mike Frysinger10427ab2005-07-06 05:00:48 +0000382 pid_t pid;
Denis Vlasenko06c0a712007-01-29 22:51:44 +0000383 char *s, *tmpCmd, *cmdpath;
384 char *cmd[INIT_BUFFS_SIZE];
Glenn L McGrathbaf55a82002-08-22 18:22:10 +0000385 char buf[INIT_BUFFS_SIZE + 6]; /* INIT_BUFFS_SIZE+strlen("exec ")+1 */
Eric Andersen0298be82002-03-05 15:12:19 +0000386 sigset_t nmask, omask;
Eric Andersen0298be82002-03-05 15:12:19 +0000387 static const char press_enter[] =
388#ifdef CUSTOMIZED_BANNER
389#include CUSTOMIZED_BANNER
390#endif
391 "\nPlease press Enter to activate this console. ";
Erik Andersenac6e71f2000-01-08 22:04:33 +0000392
Eric Andersen0298be82002-03-05 15:12:19 +0000393 /* Block sigchild while forking. */
394 sigemptyset(&nmask);
395 sigaddset(&nmask, SIGCHLD);
396 sigprocmask(SIG_BLOCK, &nmask, &omask);
397
Glenn L McGrathbaf55a82002-08-22 18:22:10 +0000398 if ((pid = fork()) == 0) {
Mike Frysinger10427ab2005-07-06 05:00:48 +0000399
Erik Andersene49d5ec2000-02-08 19:58:47 +0000400 /* Clean up */
401 close(0);
402 close(1);
403 close(2);
Eric Andersen0298be82002-03-05 15:12:19 +0000404 sigprocmask(SIG_SETMASK, &omask, NULL);
405
Eric Andersen0298be82002-03-05 15:12:19 +0000406 /* Reset signal handlers that were set by the parent process */
Erik Andersene49d5ec2000-02-08 19:58:47 +0000407 signal(SIGUSR1, SIG_DFL);
408 signal(SIGUSR2, SIG_DFL);
Glenn L McGrathbaf55a82002-08-22 18:22:10 +0000409 signal(SIGINT, SIG_DFL);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000410 signal(SIGTERM, SIG_DFL);
Glenn L McGrathbaf55a82002-08-22 18:22:10 +0000411 signal(SIGHUP, SIG_DFL);
Mike Frysingera77b4f32005-04-16 08:21:34 +0000412 signal(SIGQUIT, SIG_DFL);
Eric Andersened8a9be2001-11-30 19:10:58 +0000413 signal(SIGCONT, SIG_DFL);
414 signal(SIGSTOP, SIG_DFL);
415 signal(SIGTSTP, SIG_DFL);
Eric Andersen2f6c04f1999-11-01 23:59:44 +0000416
Eric Andersen599e3ce2002-07-03 11:08:10 +0000417 /* Create a new session and make ourself the process
Eric Andersene8a90fb2002-10-12 04:05:48 +0000418 * group leader */
419 setsid();
Eric Andersen599e3ce2002-07-03 11:08:10 +0000420
Eric Andersen0298be82002-03-05 15:12:19 +0000421 /* Open the new terminal device */
Bernhard Reutner-Fischer0da069d2006-05-29 12:54:16 +0000422 open_new_terminal(a->terminal, 1);
Eric Andersen599e3ce2002-07-03 11:08:10 +0000423
Eric Andersen0298be82002-03-05 15:12:19 +0000424 /* Make sure the terminal will act fairly normal for us */
Bernhard Reutner-Fischer49e60b92006-05-29 12:57:52 +0000425 set_term();
Eric Andersen0826b6b2002-07-03 23:50:16 +0000426 /* Setup stdout, stderr for the new process so
Eric Andersen599e3ce2002-07-03 11:08:10 +0000427 * they point to the supplied terminal */
Eric Andersen0826b6b2002-07-03 23:50:16 +0000428 dup(0);
429 dup(0);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000430
Eric Andersen599e3ce2002-07-03 11:08:10 +0000431 /* If the init Action requires us to wait, then force the
Eric Andersen0298be82002-03-05 15:12:19 +0000432 * supplied terminal to be the controlling tty. */
Glenn L McGrathbaf55a82002-08-22 18:22:10 +0000433 if (a->action & (SYSINIT | WAIT | CTRLALTDEL | SHUTDOWN | RESTART)) {
Eric Andersen0298be82002-03-05 15:12:19 +0000434
435 /* Now fork off another process to just hang around */
436 if ((pid = fork()) < 0) {
Glenn L McGrathb4a1baa2003-01-13 22:09:50 +0000437 message(LOG | CONSOLE, "Can't fork!");
Eric Andersen0298be82002-03-05 15:12:19 +0000438 _exit(1);
439 }
440
441 if (pid > 0) {
442
443 /* We are the parent -- wait till the child is done */
444 signal(SIGINT, SIG_IGN);
445 signal(SIGTSTP, SIG_IGN);
446 signal(SIGQUIT, SIG_IGN);
447 signal(SIGCHLD, SIG_DFL);
448
Bernhard Reutner-Fischer3ab30802006-05-30 12:10:29 +0000449 waitfor(NULL, pid);
Eric Andersen0298be82002-03-05 15:12:19 +0000450 /* See if stealing the controlling tty back is necessary */
Bernhard Reutner-Fischer3ab30802006-05-30 12:10:29 +0000451 if (tcgetpgrp(0) != getpid())
Eric Andersen0298be82002-03-05 15:12:19 +0000452 _exit(0);
453
454 /* Use a temporary process to steal the controlling tty. */
455 if ((pid = fork()) < 0) {
Glenn L McGrathb4a1baa2003-01-13 22:09:50 +0000456 message(LOG | CONSOLE, "Can't fork!");
Eric Andersen0298be82002-03-05 15:12:19 +0000457 _exit(1);
Glenn L McGrathbaf55a82002-08-22 18:22:10 +0000458 }
Eric Andersen0298be82002-03-05 15:12:19 +0000459 if (pid == 0) {
460 setsid();
Eric Andersen0826b6b2002-07-03 23:50:16 +0000461 ioctl(0, TIOCSCTTY, 1);
Eric Andersen0298be82002-03-05 15:12:19 +0000462 _exit(0);
Glenn L McGrathbaf55a82002-08-22 18:22:10 +0000463 }
Bernhard Reutner-Fischer3ab30802006-05-30 12:10:29 +0000464 waitfor(NULL, pid);
Eric Andersen0298be82002-03-05 15:12:19 +0000465 _exit(0);
466 }
467
468 /* Now fall though to actually execute things */
Eric Andersen0298be82002-03-05 15:12:19 +0000469 }
470
Erik Andersene132f4b2000-02-09 04:16:43 +0000471 /* See if any special /bin/sh requiring characters are present */
Eric Andersen0298be82002-03-05 15:12:19 +0000472 if (strpbrk(a->command, "~`!$^&*()=|\\{}[];\"'<>?") != NULL) {
Glenn L McGrathdc4e75e2003-09-02 02:36:18 +0000473 cmd[0] = (char *)DEFAULT_SHELL;
Denis Vlasenko06c0a712007-01-29 22:51:44 +0000474 cmd[1] = (char*)"-c";
Glenn L McGrathb4a1baa2003-01-13 22:09:50 +0000475 cmd[2] = strcat(strcpy(buf, "exec "), a->command);
Erik Andersene132f4b2000-02-09 04:16:43 +0000476 cmd[3] = NULL;
477 } else {
478 /* Convert command (char*) into cmd (char**, one word per string) */
Eric Andersen0826b6b2002-07-03 23:50:16 +0000479 strcpy(buf, a->command);
Eric Andersen72f9a422001-10-28 05:12:20 +0000480 s = buf;
Glenn L McGrathbaf55a82002-08-22 18:22:10 +0000481 for (tmpCmd = buf, i = 0; (tmpCmd = strsep(&s, " \t")) != NULL;) {
Erik Andersene132f4b2000-02-09 04:16:43 +0000482 if (*tmpCmd != '\0') {
483 cmd[i] = tmpCmd;
Erik Andersene132f4b2000-02-09 04:16:43 +0000484 i++;
485 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000486 }
Erik Andersene132f4b2000-02-09 04:16:43 +0000487 cmd[i] = NULL;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000488 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000489
Eric Andersen7ef1a5b2001-03-20 17:39:08 +0000490 cmdpath = cmd[0];
Eric Andersen7e3bf6e2000-09-14 22:01:31 +0000491
Eric Andersen7ef1a5b2001-03-20 17:39:08 +0000492 /*
493 Interactive shells want to see a dash in argv[0]. This
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000494 typically is handled by login, argv will be setup this
495 way if a dash appears at the front of the command path
Eric Andersen7e3bf6e2000-09-14 22:01:31 +0000496 (like "-/bin/sh").
Eric Andersen7ef1a5b2001-03-20 17:39:08 +0000497 */
Eric Andersen7e3bf6e2000-09-14 22:01:31 +0000498
Eric Andersen7ef1a5b2001-03-20 17:39:08 +0000499 if (*cmdpath == '-') {
Eric Andersen7e3bf6e2000-09-14 22:01:31 +0000500
Eric Andersen7ef1a5b2001-03-20 17:39:08 +0000501 /* skip over the dash */
502 ++cmdpath;
Eric Andersen7e3bf6e2000-09-14 22:01:31 +0000503
Eric Andersen7ef1a5b2001-03-20 17:39:08 +0000504 /* find the last component in the command pathname */
Manuel Novoa III cad53642003-03-19 09:13:01 +0000505 s = bb_get_last_path_component(cmdpath);
Eric Andersen7e3bf6e2000-09-14 22:01:31 +0000506
Eric Andersen7ef1a5b2001-03-20 17:39:08 +0000507 /* make a new argv[0] */
Glenn L McGrathbaf55a82002-08-22 18:22:10 +0000508 if ((cmd[0] = malloc(strlen(s) + 2)) == NULL) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000509 message(LOG | CONSOLE, bb_msg_memory_exhausted);
Eric Andersen7ef1a5b2001-03-20 17:39:08 +0000510 cmd[0] = cmdpath;
511 } else {
512 cmd[0][0] = '-';
Glenn L McGrathbaf55a82002-08-22 18:22:10 +0000513 strcpy(cmd[0] + 1, s);
Eric Andersen7ef1a5b2001-03-20 17:39:08 +0000514 }
Denis Vlasenko9b1381f2007-01-03 02:56:00 +0000515#if ENABLE_FEATURE_INIT_SCTTY
Paul Fox41a72ec2005-08-01 16:43:13 +0000516 /* Establish this process as session leader and
517 * (attempt) to make the tty (if any) a controlling tty.
518 */
519 (void) setsid();
520 (void) ioctl(0, TIOCSCTTY, 0/*don't steal it*/);
521#endif
Eric Andersen7ef1a5b2001-03-20 17:39:08 +0000522 }
523
Eric Andersen1f50e842004-08-16 09:29:42 +0000524#if !defined(__UCLIBC__) || defined(__ARCH_HAS_MMU__)
Eric Andersen0298be82002-03-05 15:12:19 +0000525 if (a->action & ASKFIRST) {
Glenn L McGrathb4a1baa2003-01-13 22:09:50 +0000526 char c;
Eric Andersen7ef1a5b2001-03-20 17:39:08 +0000527 /*
528 * Save memory by not exec-ing anything large (like a shell)
529 * before the user wants it. This is critical if swap is not
530 * enabled and the system has low memory. Generally this will
531 * be run on the second virtual console, and the first will
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000532 * be allowed to start a shell or whatever an init script
Eric Andersen7ef1a5b2001-03-20 17:39:08 +0000533 * specifies.
534 */
Glenn L McGrathb4a1baa2003-01-13 22:09:50 +0000535 messageD(LOG, "Waiting for enter to start '%s'"
536 "(pid %d, terminal %s)\n",
Glenn L McGrathbaf55a82002-08-22 18:22:10 +0000537 cmdpath, getpid(), a->terminal);
Rob Landley53437472006-07-16 08:14:35 +0000538 full_write(1, press_enter, sizeof(press_enter) - 1);
Denis Vlasenkobf0a2012006-12-26 10:42:51 +0000539 while (read(0, &c, 1) == 1 && c != '\n')
Glenn L McGrathb4a1baa2003-01-13 22:09:50 +0000540 ;
Eric Andersen7ef1a5b2001-03-20 17:39:08 +0000541 }
Eric Andersen1f50e842004-08-16 09:29:42 +0000542#endif
Eric Andersen7ef1a5b2001-03-20 17:39:08 +0000543
Eric Andersen7ef1a5b2001-03-20 17:39:08 +0000544 /* Log the process name and args */
Glenn L McGrathb4a1baa2003-01-13 22:09:50 +0000545 message(LOG, "Starting pid %d, console %s: '%s'",
Glenn L McGrathbaf55a82002-08-22 18:22:10 +0000546 getpid(), a->terminal, cmdpath);
Eric Andersen7e3bf6e2000-09-14 22:01:31 +0000547
Denis Vlasenko9b1381f2007-01-03 02:56:00 +0000548#if ENABLE_FEATURE_INIT_COREDUMPS
Bernhard Reutner-Fischer0da069d2006-05-29 12:54:16 +0000549 {
550 struct stat sb;
551 if (stat(CORE_ENABLE_FLAG_FILE, &sb) == 0) {
552 struct rlimit limit;
Glenn L McGrathbaf55a82002-08-22 18:22:10 +0000553
Bernhard Reutner-Fischer0da069d2006-05-29 12:54:16 +0000554 limit.rlim_cur = RLIM_INFINITY;
555 limit.rlim_max = RLIM_INFINITY;
556 setrlimit(RLIMIT_CORE, &limit);
557 }
Erik Andersen983b51b2000-04-04 18:14:25 +0000558 }
Erik Andersen183da4a2000-04-04 18:36:37 +0000559#endif
Erik Andersen983b51b2000-04-04 18:14:25 +0000560
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000561 /* Now run it. The new program will take over this PID,
Erik Andersene49d5ec2000-02-08 19:58:47 +0000562 * so nothing further in init.c should be run. */
Denis Vlasenko4921b542007-02-03 02:17:41 +0000563 BB_EXECVP(cmdpath, cmd);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000564
Eric Andersen7ef1a5b2001-03-20 17:39:08 +0000565 /* We're still here? Some error happened. */
Denis Vlasenkoea620772006-10-14 02:23:43 +0000566 message(LOG | CONSOLE, "Bummer, cannot run '%s': %m", cmdpath);
Eric Andersen0298be82002-03-05 15:12:19 +0000567 _exit(-1);
Erik Andersen05df2392000-01-13 04:43:48 +0000568 }
Eric Andersen0298be82002-03-05 15:12:19 +0000569 sigprocmask(SIG_SETMASK, &omask, NULL);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000570 return pid;
Eric Andersen0460ff21999-10-25 23:32:44 +0000571}
572
Bernhard Reutner-Fischer3ab30802006-05-30 12:10:29 +0000573static int waitfor(const struct init_action *a, pid_t pid)
Erik Andersen0e3782f2000-01-07 02:54:55 +0000574{
Bernhard Reutner-Fischer3ab30802006-05-30 12:10:29 +0000575 int runpid;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000576 int status, wpid;
Erik Andersen0e3782f2000-01-07 02:54:55 +0000577
Bernhard Reutner-Fischer3ab30802006-05-30 12:10:29 +0000578 runpid = (NULL == a)? pid : run(a);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000579 while (1) {
Bernhard Reutner-Fischer3ab30802006-05-30 12:10:29 +0000580 wpid = waitpid(runpid,&status,0);
581 if (wpid == runpid)
Erik Andersene49d5ec2000-02-08 19:58:47 +0000582 break;
Glenn L McGrathe6ba16f2003-09-26 10:45:55 +0000583 if (wpid == -1 && errno == ECHILD) {
584 /* we missed its termination */
585 break;
586 }
587 /* FIXME other errors should maybe trigger an error, but allow
588 * the program to continue */
Erik Andersen0e3782f2000-01-07 02:54:55 +0000589 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000590 return wpid;
Erik Andersen0e3782f2000-01-07 02:54:55 +0000591}
592
Eric Andersen0298be82002-03-05 15:12:19 +0000593/* Run all commands of a particular type */
594static void run_actions(int action)
Eric Andersen219d6f51999-11-02 19:43:01 +0000595{
Eric Andersen0298be82002-03-05 15:12:19 +0000596 struct init_action *a, *tmp;
Eric Andersen219d6f51999-11-02 19:43:01 +0000597
Eric Andersen0298be82002-03-05 15:12:19 +0000598 for (a = init_action_list; a; a = tmp) {
599 tmp = a->next;
Eric Andersenc97ec342001-04-03 18:01:51 +0000600 if (a->action == action) {
Rob Landley2dd42792006-03-22 17:39:13 +0000601 if (access(a->terminal, R_OK | W_OK)) {
602 delete_init_action(a);
603 } else if (a->action & (SYSINIT | WAIT | CTRLALTDEL | SHUTDOWN | RESTART)) {
Bernhard Reutner-Fischer3ab30802006-05-30 12:10:29 +0000604 waitfor(a, 0);
Eric Andersen0298be82002-03-05 15:12:19 +0000605 delete_init_action(a);
606 } else if (a->action & ONCE) {
607 run(a);
608 delete_init_action(a);
Glenn L McGrathbaf55a82002-08-22 18:22:10 +0000609 } else if (a->action & (RESPAWN | ASKFIRST)) {
Eric Andersen0298be82002-03-05 15:12:19 +0000610 /* Only run stuff with pid==0. If they have
611 * a pid, that means it is still running */
612 if (a->pid == 0) {
613 a->pid = run(a);
614 }
615 }
Erik Andersene132f4b2000-02-09 04:16:43 +0000616 }
617 }
618}
619
Bernhard Reutner-Fischer35e1a072006-05-29 13:08:35 +0000620#if !ENABLE_DEBUG_INIT
Eric Andersen2c1de612003-04-24 11:41:28 +0000621static void init_reboot(unsigned long magic)
622{
623 pid_t pid;
624 /* We have to fork here, since the kernel calls do_exit(0) in
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000625 * linux/kernel/sys.c, which can cause the machine to panic when
Eric Andersen2c1de612003-04-24 11:41:28 +0000626 * the init process is killed.... */
Denis Vlasenko9b1381f2007-01-03 02:56:00 +0000627 pid = vfork();
628 if (pid == 0) { /* child */
Eric Andersen2c1de612003-04-24 11:41:28 +0000629 reboot(magic);
Eric Andersen2c1de612003-04-24 11:41:28 +0000630 _exit(0);
631 }
Denis Vlasenko9b1381f2007-01-03 02:56:00 +0000632 waitpid(pid, NULL, 0);
Eric Andersen2c1de612003-04-24 11:41:28 +0000633}
634
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000635static void shutdown_system(void)
Eric Andersencc8ed391999-10-05 16:24:54 +0000636{
Eric Andersen813d88c2001-10-28 22:49:48 +0000637 sigset_t block_signals;
Erik Andersene132f4b2000-02-09 04:16:43 +0000638
Eric Andersena9cc8962002-09-16 06:49:06 +0000639 /* run everything to be run at "shutdown". This is done _prior_
640 * to killing everything, in case people wish to use scripts to
641 * shut things down gracefully... */
642 run_actions(SHUTDOWN);
643
Eric Andersen72f9a422001-10-28 05:12:20 +0000644 /* first disable all our signals */
645 sigemptyset(&block_signals);
646 sigaddset(&block_signals, SIGHUP);
Mike Frysingera77b4f32005-04-16 08:21:34 +0000647 sigaddset(&block_signals, SIGQUIT);
Eric Andersen72f9a422001-10-28 05:12:20 +0000648 sigaddset(&block_signals, SIGCHLD);
649 sigaddset(&block_signals, SIGUSR1);
650 sigaddset(&block_signals, SIGUSR2);
651 sigaddset(&block_signals, SIGINT);
652 sigaddset(&block_signals, SIGTERM);
Eric Andersened8a9be2001-11-30 19:10:58 +0000653 sigaddset(&block_signals, SIGCONT);
654 sigaddset(&block_signals, SIGSTOP);
655 sigaddset(&block_signals, SIGTSTP);
Eric Andersen72f9a422001-10-28 05:12:20 +0000656 sigprocmask(SIG_BLOCK, &block_signals, NULL);
Erik Andersen7dc16072000-01-04 01:10:25 +0000657
Erik Andersene49d5ec2000-02-08 19:58:47 +0000658 /* Allow Ctrl-Alt-Del to reboot system. */
Erik Andersen4f3f7572000-04-28 00:18:56 +0000659 init_reboot(RB_ENABLE_CAD);
Erik Andersene132f4b2000-02-09 04:16:43 +0000660
Glenn L McGrathb4a1baa2003-01-13 22:09:50 +0000661 message(CONSOLE | LOG, "The system is going down NOW !!");
Eric Andersencf8c9cf1999-11-05 00:31:46 +0000662 sync();
Erik Andersene49d5ec2000-02-08 19:58:47 +0000663
664 /* Send signals to every process _except_ pid 1 */
Bernhard Reutner-Fischer7ae15532006-05-30 18:17:21 +0000665 message(CONSOLE | LOG, init_sending_format, "TERM");
Erik Andersene49d5ec2000-02-08 19:58:47 +0000666 kill(-1, SIGTERM);
Erik Andersene132f4b2000-02-09 04:16:43 +0000667 sleep(1);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000668 sync();
669
Bernhard Reutner-Fischer7ae15532006-05-30 18:17:21 +0000670 message(CONSOLE | LOG, init_sending_format, "KILL");
Erik Andersene49d5ec2000-02-08 19:58:47 +0000671 kill(-1, SIGKILL);
Erik Andersene132f4b2000-02-09 04:16:43 +0000672 sleep(1);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000673
Erik Andersene49d5ec2000-02-08 19:58:47 +0000674 sync();
Eric Andersencc8ed391999-10-05 16:24:54 +0000675}
676
"Vladimir N. Oleynik"1f04c9d2006-02-01 14:47:52 +0000677static void exec_signal(int sig ATTRIBUTE_UNUSED)
Eric Andersen730f8262001-12-17 23:13:08 +0000678{
Eric Andersen0298be82002-03-05 15:12:19 +0000679 struct init_action *a, *tmp;
Eric Andersen5222d312002-07-03 05:15:23 +0000680 sigset_t unblock_signals;
Glenn L McGrathbaf55a82002-08-22 18:22:10 +0000681
Eric Andersen0298be82002-03-05 15:12:19 +0000682 for (a = init_action_list; a; a = tmp) {
683 tmp = a->next;
684 if (a->action & RESTART) {
Eric Andersen730f8262001-12-17 23:13:08 +0000685 shutdown_system();
Eric Andersen5222d312002-07-03 05:15:23 +0000686
687 /* unblock all signals, blocked in shutdown_system() */
688 sigemptyset(&unblock_signals);
689 sigaddset(&unblock_signals, SIGHUP);
Mike Frysingera77b4f32005-04-16 08:21:34 +0000690 sigaddset(&unblock_signals, SIGQUIT);
Eric Andersen5222d312002-07-03 05:15:23 +0000691 sigaddset(&unblock_signals, SIGCHLD);
692 sigaddset(&unblock_signals, SIGUSR1);
693 sigaddset(&unblock_signals, SIGUSR2);
694 sigaddset(&unblock_signals, SIGINT);
695 sigaddset(&unblock_signals, SIGTERM);
696 sigaddset(&unblock_signals, SIGCONT);
697 sigaddset(&unblock_signals, SIGSTOP);
698 sigaddset(&unblock_signals, SIGTSTP);
699 sigprocmask(SIG_UNBLOCK, &unblock_signals, NULL);
700
Eric Andersen9cdef5d2003-07-29 06:33:12 +0000701 /* Close whatever files are open. */
702 close(0);
703 close(1);
704 close(2);
705
Eric Andersen3c8064f2003-07-05 08:29:01 +0000706 /* Open the new terminal device */
Bernhard Reutner-Fischer0da069d2006-05-29 12:54:16 +0000707 open_new_terminal(a->terminal, 0);
Eric Andersen3c8064f2003-07-05 08:29:01 +0000708
709 /* Make sure the terminal will act fairly normal for us */
Bernhard Reutner-Fischer49e60b92006-05-29 12:57:52 +0000710 set_term();
Eric Andersen3c8064f2003-07-05 08:29:01 +0000711 /* Setup stdout, stderr on the supplied terminal */
712 dup(0);
713 dup(0);
714
Glenn L McGrathb4a1baa2003-01-13 22:09:50 +0000715 messageD(CONSOLE | LOG, "Trying to re-exec %s", a->command);
Denis Vlasenko4921b542007-02-03 02:17:41 +0000716 BB_EXECLP(a->command, a->command, NULL);
Glenn L McGrathbaf55a82002-08-22 18:22:10 +0000717
Glenn L McGrathb4a1baa2003-01-13 22:09:50 +0000718 message(CONSOLE | LOG, "exec of '%s' failed: %m",
Eric Andersen71ae64b2002-10-10 04:20:21 +0000719 a->command);
Eric Andersen730f8262001-12-17 23:13:08 +0000720 sync();
721 sleep(2);
722 init_reboot(RB_HALT_SYSTEM);
723 loop_forever();
724 }
725 }
726}
727
Paul Foxd112f8f2006-06-01 13:17:49 +0000728static void shutdown_signal(int sig)
Eric Andersencc8ed391999-10-05 16:24:54 +0000729{
Denis Vlasenkoe968fcd2007-02-03 02:42:47 +0000730 const char *m;
Paul Foxd112f8f2006-06-01 13:17:49 +0000731 int rb;
732
Erik Andersene49d5ec2000-02-08 19:58:47 +0000733 shutdown_system();
Paul Foxd112f8f2006-06-01 13:17:49 +0000734
Denis Vlasenkoe968fcd2007-02-03 02:42:47 +0000735 m = "halt";
736 rb = RB_HALT_SYSTEM;
Paul Foxd112f8f2006-06-01 13:17:49 +0000737 if (sig == SIGTERM) {
738 m = "reboot";
739 rb = RB_AUTOBOOT;
740 } else if (sig == SIGUSR2) {
741 m = "poweroff";
742 rb = RB_POWER_OFF;
Paul Foxd112f8f2006-06-01 13:17:49 +0000743 }
744 message(CONSOLE | LOG, "Requesting system %s.", m);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000745 sync();
Erik Andersen3fe39dc2000-01-25 18:13:53 +0000746
Erik Andersene49d5ec2000-02-08 19:58:47 +0000747 /* allow time for last message to reach serial console */
Erik Andersene132f4b2000-02-09 04:16:43 +0000748 sleep(2);
Erik Andersen3fe39dc2000-01-25 18:13:53 +0000749
Paul Foxd112f8f2006-06-01 13:17:49 +0000750 init_reboot(rb);
Matt Kraai67a46402001-06-03 05:55:52 +0000751 loop_forever();
Eric Andersencc8ed391999-10-05 16:24:54 +0000752}
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000753
"Vladimir N. Oleynik"1f04c9d2006-02-01 14:47:52 +0000754static void ctrlaltdel_signal(int sig ATTRIBUTE_UNUSED)
Eric Andersenc97ec342001-04-03 18:01:51 +0000755{
756 run_actions(CTRLALTDEL);
757}
758
Eric Andersen0298be82002-03-05 15:12:19 +0000759/* The SIGSTOP & SIGTSTP handler */
"Vladimir N. Oleynik"1f04c9d2006-02-01 14:47:52 +0000760static void stop_handler(int sig ATTRIBUTE_UNUSED)
Eric Andersened8a9be2001-11-30 19:10:58 +0000761{
Eric Andersen0298be82002-03-05 15:12:19 +0000762 int saved_errno = errno;
Eric Andersened8a9be2001-11-30 19:10:58 +0000763
764 got_cont = 0;
Glenn L McGrathbaf55a82002-08-22 18:22:10 +0000765 while (!got_cont)
766 pause();
Eric Andersened8a9be2001-11-30 19:10:58 +0000767 got_cont = 0;
768 errno = saved_errno;
769}
770
Glenn L McGrathbaf55a82002-08-22 18:22:10 +0000771/* The SIGCONT handler */
"Vladimir N. Oleynik"1f04c9d2006-02-01 14:47:52 +0000772static void cont_handler(int sig ATTRIBUTE_UNUSED)
Eric Andersened8a9be2001-11-30 19:10:58 +0000773{
774 got_cont = 1;
775}
776
Denis Vlasenkoe968fcd2007-02-03 02:42:47 +0000777#endif /* !ENABLE_DEBUG_INIT */
Erik Andersende552872000-01-23 01:34:05 +0000778
Glenn L McGrathdc4e75e2003-09-02 02:36:18 +0000779static void new_init_action(int action, const char *command, const char *cons)
Erik Andersen7dc16072000-01-04 01:10:25 +0000780{
Eric Andersen22718092004-10-08 08:17:39 +0000781 struct init_action *new_action, *a, *last;
Erik Andersen9e737252000-01-06 01:16:13 +0000782
Erik Andersene49d5ec2000-02-08 19:58:47 +0000783 if (*cons == '\0')
784 cons = console;
Erik Andersen9e737252000-01-06 01:16:13 +0000785
"Vladimir N. Oleynik"6c35c7c2005-10-12 15:34:25 +0000786 if (strcmp(cons, bb_dev_null) == 0 && (action & ASKFIRST))
Eric Andersen1644db92001-09-05 20:18:15 +0000787 return;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000788
Denis Vlasenko9b1381f2007-01-03 02:56:00 +0000789 new_action = xzalloc(sizeof(struct init_action));
Eric Andersen0298be82002-03-05 15:12:19 +0000790
791 /* Append to the end of the list */
Eric Andersen22718092004-10-08 08:17:39 +0000792 for (a = last = init_action_list; a; a = a->next) {
Eric Andersen82baf632004-10-08 08:21:54 +0000793 /* don't enter action if it's already in the list,
794 * but do overwrite existing actions */
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000795 if ((strcmp(a->command, command) == 0) &&
Eric Andersen6fd0e312003-07-22 09:48:56 +0000796 (strcmp(a->terminal, cons) ==0)) {
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);
Glenn L McGrathb4a1baa2003-01-13 22:09:50 +0000811 messageD(LOG|CONSOLE, "command='%s' action='%d' terminal='%s'\n",
812 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 */
Denis Vlasenko2f0c0d02007-01-21 00:41:04 +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 */
Eric Andersenb5966362000-05-31 20:04:38 +0000882 eol = strrchr(id, '\n');
883 if (eol != NULL)
884 *eol = '\0';
Erik Andersen7dc16072000-01-04 01:10:25 +0000885
Erik Andersene49d5ec2000-02-08 19:58:47 +0000886 /* Keep a copy around for posterity's sake (and error msgs) */
887 strcpy(lineAsRead, buf);
888
Eric Andersenb5966362000-05-31 20:04:38 +0000889 /* Separate the ID field from the runlevels */
890 runlev = strchr(id, ':');
891 if (runlev == NULL || *(runlev + 1) == '\0') {
Glenn L McGrathb4a1baa2003-01-13 22:09:50 +0000892 message(LOG | CONSOLE, "Bad inittab entry: %s", lineAsRead);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000893 continue;
894 } else {
Eric Andersenb5966362000-05-31 20:04:38 +0000895 *runlev = '\0';
896 ++runlev;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000897 }
898
Eric Andersenb5966362000-05-31 20:04:38 +0000899 /* Separate the runlevels from the action */
900 action = strchr(runlev, ':');
901 if (action == NULL || *(action + 1) == '\0') {
Glenn L McGrathb4a1baa2003-01-13 22:09:50 +0000902 message(LOG | CONSOLE, "Bad inittab entry: %s", lineAsRead);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000903 continue;
904 } else {
Eric Andersenb5966362000-05-31 20:04:38 +0000905 *action = '\0';
906 ++action;
907 }
908
Eric Andersen0298be82002-03-05 15:12:19 +0000909 /* Separate the action from the command */
910 command = strchr(action, ':');
911 if (command == NULL || *(command + 1) == '\0') {
Glenn L McGrathb4a1baa2003-01-13 22:09:50 +0000912 message(LOG | CONSOLE, "Bad inittab entry: %s", lineAsRead);
Eric Andersenb5966362000-05-31 20:04:38 +0000913 continue;
914 } else {
Eric Andersen0298be82002-03-05 15:12:19 +0000915 *command = '\0';
916 ++command;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000917 }
918
919 /* Ok, now process it */
Glenn L McGrathb4a1baa2003-01-13 22:09:50 +0000920 for (a = actions; a->name != 0; a++) {
Eric Andersenb5966362000-05-31 20:04:38 +0000921 if (strcmp(a->name, action) == 0) {
922 if (*id != '\0') {
Glenn L McGrathb4a1baa2003-01-13 22:09:50 +0000923 if(strncmp(id, "/dev/", 5) == 0)
924 id += 5;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000925 strcpy(tmpConsole, "/dev/");
Glenn L McGrathb4a1baa2003-01-13 22:09:50 +0000926 safe_strncpy(tmpConsole + 5, id,
927 CONSOLE_BUFF_SIZE - 5);
Eric Andersenb5966362000-05-31 20:04:38 +0000928 id = tmpConsole;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000929 }
Eric Andersen0298be82002-03-05 15:12:19 +0000930 new_init_action(a->action, command, id);
Glenn L McGrathb4a1baa2003-01-13 22:09:50 +0000931 break;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000932 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000933 }
Glenn L McGrathb4a1baa2003-01-13 22:09:50 +0000934 if (a->name == 0) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000935 /* Choke on an unknown action */
Glenn L McGrathb4a1baa2003-01-13 22:09:50 +0000936 message(LOG | CONSOLE, "Bad inittab entry: %s", lineAsRead);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000937 }
Erik Andersen7dc16072000-01-04 01:10:25 +0000938 }
Eric Andersend8636ca2002-05-15 22:19:09 +0000939 fclose(file);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000940 return;
Denis Vlasenko9b1381f2007-01-03 02:56:00 +0000941#endif /* FEATURE_USE_INITTAB */
Erik Andersen7dc16072000-01-04 01:10:25 +0000942}
943
Denis Vlasenko9b1381f2007-01-03 02:56:00 +0000944#if ENABLE_FEATURE_USE_INITTAB
"Vladimir N. Oleynik"1f04c9d2006-02-01 14:47:52 +0000945static void reload_signal(int sig ATTRIBUTE_UNUSED)
Eric Andersen6fd0e312003-07-22 09:48:56 +0000946{
Eric Andersen82baf632004-10-08 08:21:54 +0000947 struct init_action *a, *tmp;
948
949 message(LOG, "Reloading /etc/inittab");
950
951 /* disable old entrys */
952 for (a = init_action_list; a; a = a->next ) {
953 a->action = ONCE;
954 }
955
956 parse_inittab();
957
958 /* remove unused entrys */
959 for (a = init_action_list; a; a = tmp) {
960 tmp = a->next;
961 if (a->action & (ONCE | SYSINIT | WAIT ) &&
962 a->pid == 0 ) {
963 delete_init_action(a);
964 }
965 }
Eric Andersen6fd0e312003-07-22 09:48:56 +0000966 run_actions(RESPAWN);
Eric Andersen82baf632004-10-08 08:21:54 +0000967 return;
Eric Andersen6fd0e312003-07-22 09:48:56 +0000968}
Denis Vlasenko9b1381f2007-01-03 02:56:00 +0000969#endif /* FEATURE_USE_INITTAB */
Eric Andersen82baf632004-10-08 08:21:54 +0000970
Denis Vlasenko06af2162007-02-03 17:28:39 +0000971int init_main(int argc, char **argv);
Rob Landleydfba7412006-03-06 20:47:33 +0000972int init_main(int argc, char **argv)
Eric Andersen0460ff21999-10-25 23:32:44 +0000973{
Eric Andersen0298be82002-03-05 15:12:19 +0000974 struct init_action *a;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000975 pid_t wpid;
Eric Andersen0460ff21999-10-25 23:32:44 +0000976
Denis Vlasenko9b1381f2007-01-03 02:56:00 +0000977 die_sleep = 30 * 24*60*60; /* if xmalloc will ever die... */
978
Eric Andersen730f8262001-12-17 23:13:08 +0000979 if (argc > 1 && !strcmp(argv[1], "-q")) {
Rob Landley2edf5262006-01-22 02:41:51 +0000980 return kill(1,SIGHUP);
Eric Andersen730f8262001-12-17 23:13:08 +0000981 }
Bernhard Reutner-Fischer35e1a072006-05-29 13:08:35 +0000982#if !ENABLE_DEBUG_INIT
Erik Andersena51ecdd2000-02-24 18:09:58 +0000983 /* Expect to be invoked as init with PID=1 or be invoked as linuxrc */
Rob Landley64612912006-01-30 08:31:37 +0000984 if (getpid() != 1 &&
Denis Vlasenko8f8f2682006-10-03 21:00:43 +0000985 (!ENABLE_FEATURE_INITRD || !strstr(applet_name, "linuxrc")))
Rob Landley64612912006-01-30 08:31:37 +0000986 {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000987 bb_show_usage();
Erik Andersene49d5ec2000-02-08 19:58:47 +0000988 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000989 /* Set up sig handlers -- be sure to
990 * clear all of these in run() */
Glenn L McGrathbaf55a82002-08-22 18:22:10 +0000991 signal(SIGHUP, exec_signal);
Mike Frysingera77b4f32005-04-16 08:21:34 +0000992 signal(SIGQUIT, exec_signal);
Paul Foxd112f8f2006-06-01 13:17:49 +0000993 signal(SIGUSR1, shutdown_signal);
994 signal(SIGUSR2, shutdown_signal);
Glenn L McGrathbaf55a82002-08-22 18:22:10 +0000995 signal(SIGINT, ctrlaltdel_signal);
Paul Foxd112f8f2006-06-01 13:17:49 +0000996 signal(SIGTERM, shutdown_signal);
Eric Andersened8a9be2001-11-30 19:10:58 +0000997 signal(SIGCONT, cont_handler);
998 signal(SIGSTOP, stop_handler);
999 signal(SIGTSTP, stop_handler);
Eric Andersen0460ff21999-10-25 23:32:44 +00001000
Eric Andersenc7bda1c2004-03-15 08:29:22 +00001001 /* Turn off rebooting via CTL-ALT-DEL -- we get a
Erik Andersene49d5ec2000-02-08 19:58:47 +00001002 * SIGINT on CAD so we can shut things down gracefully... */
Erik Andersen4f3f7572000-04-28 00:18:56 +00001003 init_reboot(RB_DISABLE_CAD);
Erik Andersene49d5ec2000-02-08 19:58:47 +00001004#endif
Erik Andersen05df2392000-01-13 04:43:48 +00001005
Eric Andersen599e3ce2002-07-03 11:08:10 +00001006 /* Figure out where the default console should be */
1007 console_init();
1008
Erik Andersene49d5ec2000-02-08 19:58:47 +00001009 /* Close whatever files are open, and reset the console. */
1010 close(0);
1011 close(1);
1012 close(2);
Eric Andersen72f9a422001-10-28 05:12:20 +00001013
Glenn L McGrathbaf55a82002-08-22 18:22:10 +00001014 if (device_open(console, O_RDWR | O_NOCTTY) == 0) {
Bernhard Reutner-Fischer49e60b92006-05-29 12:57:52 +00001015 set_term();
Eric Andersen599e3ce2002-07-03 11:08:10 +00001016 close(0);
1017 }
1018
Erik Andersen983b51b2000-04-04 18:14:25 +00001019 chdir("/");
Erik Andersene49d5ec2000-02-08 19:58:47 +00001020 setsid();
Glenn L McGrathb4a1baa2003-01-13 22:09:50 +00001021 {
1022 const char * const *e;
1023 /* Make sure environs is set to something sane */
Denis Vlasenkobf0a2012006-12-26 10:42:51 +00001024 for (e = environment; *e; e++)
Manuel Novoa III cad53642003-03-19 09:13:01 +00001025 putenv((char *) *e);
Glenn L McGrathb4a1baa2003-01-13 22:09:50 +00001026 }
Rob Landleycba1b962006-07-09 17:28:17 +00001027
1028 if (argc > 1) setenv("RUNLEVEL", argv[1], 1);
1029
Erik Andersene49d5ec2000-02-08 19:58:47 +00001030 /* Hello world */
Manuel Novoa III cad53642003-03-19 09:13:01 +00001031 message(MAYBE_CONSOLE | LOG, "init started: %s", bb_msg_full_version);
Eric Andersencc8ed391999-10-05 16:24:54 +00001032
Erik Andersene49d5ec2000-02-08 19:58:47 +00001033 /* Make sure there is enough memory to do something useful. */
Rob Landleyc3386a42005-08-30 18:50:37 +00001034 if (ENABLE_SWAPONOFF) {
1035 struct sysinfo info;
1036
1037 if (!sysinfo(&info) &&
Denis Vlasenkodca0b702006-10-27 09:05:02 +00001038 (info.mem_unit ? : 1) * (long long)info.totalram < 1024*1024)
Rob Landleyc3386a42005-08-30 18:50:37 +00001039 {
1040 message(CONSOLE,"Low memory: forcing swapon.");
1041 /* swapon -a requires /proc typically */
Denis Vlasenko2f0c0d02007-01-21 00:41:04 +00001042 new_init_action(SYSINIT, "mount -t proc proc /proc", "");
Rob Landleyc3386a42005-08-30 18:50:37 +00001043 /* Try to turn on swap */
Denis Vlasenko2f0c0d02007-01-21 00:41:04 +00001044 new_init_action(SYSINIT, "swapon -a", "");
Rob Landleyc3386a42005-08-30 18:50:37 +00001045 run_actions(SYSINIT); /* wait and removing */
1046 }
1047 }
Erik Andersen9e737252000-01-06 01:16:13 +00001048
Erik Andersene49d5ec2000-02-08 19:58:47 +00001049 /* Check if we are supposed to be in single user mode */
Denis Vlasenkobf66fbc2006-12-21 13:23:14 +00001050 if (argc > 1
1051 && (!strcmp(argv[1], "single") || !strcmp(argv[1], "-s") || LONE_CHAR(argv[1], '1'))
1052 ) {
Glenn L McGrathb4a1baa2003-01-13 22:09:50 +00001053 /* Start a shell on console */
Glenn L McGrathdc4e75e2003-09-02 02:36:18 +00001054 new_init_action(RESPAWN, bb_default_login_shell, "");
Erik Andersene49d5ec2000-02-08 19:58:47 +00001055 } else {
1056 /* Not in single user mode -- see what inittab says */
Eric Andersen8a8fbb81999-10-26 00:18:56 +00001057
Eric Andersenbdfd0d72001-10-24 05:00:29 +00001058 /* NOTE that if CONFIG_FEATURE_USE_INITTAB is NOT defined,
Erik Andersene49d5ec2000-02-08 19:58:47 +00001059 * then parse_inittab() simply adds in some default
Eric Andersenc7bda1c2004-03-15 08:29:22 +00001060 * actions(i.e., runs INIT_SCRIPT and then starts a pair
Erik Andersene49d5ec2000-02-08 19:58:47 +00001061 * of "askfirst" shells */
1062 parse_inittab();
Eric Andersend00c2621999-12-07 08:37:31 +00001063 }
Erik Andersen983b51b2000-04-04 18:14:25 +00001064
Denis Vlasenko9b1381f2007-01-03 02:56:00 +00001065#if ENABLE_SELINUX
Rob Landleyb3ede5a2006-03-27 23:09:12 +00001066 if (getenv("SELINUX_INIT") == NULL) {
1067 int enforce = 0;
1068
1069 putenv("SELINUX_INIT=YES");
1070 if (selinux_init_load_policy(&enforce) == 0) {
Denis Vlasenko4921b542007-02-03 02:17:41 +00001071 BB_EXECVP(argv[0], argv);
Rob Landleyb3ede5a2006-03-27 23:09:12 +00001072 } else if (enforce > 0) {
1073 /* SELinux in enforcing mode but load_policy failed */
1074 /* At this point, we probably can't open /dev/console, so log() won't work */
1075 message(CONSOLE,"Unable to load SELinux Policy. Machine is in enforcing mode. Halting now.");
1076 exit(1);
1077 }
1078 }
1079#endif /* CONFIG_SELINUX */
1080
Eric Andersen7ef1a5b2001-03-20 17:39:08 +00001081 /* Make the command line just say "init" -- thats all, nothing else */
1082 fixup_argv(argc, argv, "init");
Eric Andersen219d6f51999-11-02 19:43:01 +00001083
Erik Andersene49d5ec2000-02-08 19:58:47 +00001084 /* Now run everything that needs to be run */
1085
1086 /* First run the sysinit command */
Eric Andersen1644db92001-09-05 20:18:15 +00001087 run_actions(SYSINIT);
Eric Andersen0298be82002-03-05 15:12:19 +00001088
Erik Andersene49d5ec2000-02-08 19:58:47 +00001089 /* Next run anything that wants to block */
Eric Andersen1644db92001-09-05 20:18:15 +00001090 run_actions(WAIT);
Eric Andersen0298be82002-03-05 15:12:19 +00001091
Erik Andersene49d5ec2000-02-08 19:58:47 +00001092 /* Next run anything to be run only once */
Eric Andersen0298be82002-03-05 15:12:19 +00001093 run_actions(ONCE);
1094
Denis Vlasenko9b1381f2007-01-03 02:56:00 +00001095#if ENABLE_FEATURE_USE_INITTAB
Eric Andersen6fd0e312003-07-22 09:48:56 +00001096 /* Redefine SIGHUP to reread /etc/inittab */
1097 signal(SIGHUP, reload_signal);
Eric Andersen82baf632004-10-08 08:21:54 +00001098#else
1099 signal(SIGHUP, SIG_IGN);
Denis Vlasenko9b1381f2007-01-03 02:56:00 +00001100#endif /* FEATURE_USE_INITTAB */
Eric Andersen82baf632004-10-08 08:21:54 +00001101
Eric Andersen6fd0e312003-07-22 09:48:56 +00001102
Erik Andersene49d5ec2000-02-08 19:58:47 +00001103 /* Now run the looping stuff for the rest of forever */
1104 while (1) {
Eric Andersen0298be82002-03-05 15:12:19 +00001105 /* run the respawn stuff */
1106 run_actions(RESPAWN);
1107
1108 /* run the askfirst stuff */
1109 run_actions(ASKFIRST);
1110
1111 /* Don't consume all CPU time -- sleep a bit */
1112 sleep(1);
1113
Erik Andersene49d5ec2000-02-08 19:58:47 +00001114 /* Wait for a child process to exit */
Bernhard Reutner-Fischerc58dbf22006-05-30 12:16:54 +00001115 wpid = wait(NULL);
Eric Andersen87812dc2004-04-12 19:21:54 +00001116 while (wpid > 0) {
Erik Andersene49d5ec2000-02-08 19:58:47 +00001117 /* Find out who died and clean up their corpse */
Eric Andersen0298be82002-03-05 15:12:19 +00001118 for (a = init_action_list; a; a = a->next) {
Erik Andersene49d5ec2000-02-08 19:58:47 +00001119 if (a->pid == wpid) {
Eric Andersen0298be82002-03-05 15:12:19 +00001120 /* Set the pid to 0 so that the process gets
1121 * restarted by run_actions() */
Erik Andersene49d5ec2000-02-08 19:58:47 +00001122 a->pid = 0;
Eric Andersen0298be82002-03-05 15:12:19 +00001123 message(LOG, "Process '%s' (pid %d) exited. "
Glenn L McGrathb4a1baa2003-01-13 22:09:50 +00001124 "Scheduling it for restart.",
1125 a->command, wpid);
Erik Andersene49d5ec2000-02-08 19:58:47 +00001126 }
1127 }
Eric Andersencf1fee02002-12-17 09:48:16 +00001128 /* see if anyone else is waiting to be reaped */
Denis Vlasenkoce979602006-09-27 23:31:08 +00001129 wpid = waitpid(-1, NULL, WNOHANG);
Erik Andersene49d5ec2000-02-08 19:58:47 +00001130 }
Erik Andersene49d5ec2000-02-08 19:58:47 +00001131 }
Eric Andersencc8ed391999-10-05 16:24:54 +00001132}