blob: c9200bd6cdbdd057e6a29eada95485cb48bdc7de [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
Eric Andersenbdfd0d72001-10-24 05:00:29 +000022#ifdef CONFIG_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
Eric Andersenbdfd0d72001-10-24 05:00:29 +000063#if defined CONFIG_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
Eric Andersene7078062002-07-29 08:00:16 +0000124#ifndef CONFIG_SYSLOGD
Mike Frysinger72a4c332005-07-05 02:19:20 +0000125static 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
Eric Andersenb0cc0a62002-03-20 14:57:50 +0000135#if defined CONFIG_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
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}
Eric Andersen7ef1a5b2001-03-20 17:39:08 +0000170
Erik Andersen42094cd2000-03-20 21:34:52 +0000171/* Print a message to the specified device.
172 * Device may be bitwise-or'd from LOG | CONSOLE */
Bernhard Reutner-Fischer35e1a072006-05-29 13:08:35 +0000173#if ENABLE_DEBUG_INIT
174#define messageD message
175#else
176static inline void messageD(int ATTRIBUTE_UNUSED device,
"Vladimir N. Oleynik"1f04c9d2006-02-01 14:47:52 +0000177 const char ATTRIBUTE_UNUSED *fmt, ...)
Glenn L McGrathbaf55a82002-08-22 18:22:10 +0000178{
179}
Eric Andersenfedce062001-11-17 07:27:14 +0000180#endif
Glenn L McGrathb4a1baa2003-01-13 22:09:50 +0000181static void message(int device, const char *fmt, ...)
Glenn L McGrathbaf55a82002-08-22 18:22:10 +0000182 __attribute__ ((format(printf, 2, 3)));
Glenn L McGrathb4a1baa2003-01-13 22:09:50 +0000183static void message(int device, const char *fmt, ...)
Eric Andersen0460ff21999-10-25 23:32:44 +0000184{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000185 va_list arguments;
Glenn L McGrathb4a1baa2003-01-13 22:09:50 +0000186 int l;
Bernhard Reutner-Fischer3ab30802006-05-30 12:10:29 +0000187 RESERVE_CONFIG_BUFFER(msg, 1024);
Eric Andersen22237012003-01-23 07:08:26 +0000188#ifndef CONFIG_SYSLOGD
189 static int log_fd = -1;
190#endif
Eric Andersen4c781471999-11-26 08:12:56 +0000191
Glenn L McGrathb4a1baa2003-01-13 22:09:50 +0000192 msg[0] = '\r';
Erik Andersene49d5ec2000-02-08 19:58:47 +0000193 va_start(arguments, fmt);
Bernhard Reutner-Fischer3ab30802006-05-30 12:10:29 +0000194 l = vsnprintf(msg + 1, 1024 - 2, fmt, arguments) + 1;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000195 va_end(arguments);
Glenn L McGrathb4a1baa2003-01-13 22:09:50 +0000196
197#ifdef CONFIG_SYSLOGD
198 /* Log the message to syslogd */
199 if (device & LOG) {
200 /* don`t out "\r\n" */
Eric Andersen36adca82004-06-22 10:07:17 +0000201 openlog(bb_applet_name, 0, LOG_DAEMON);
"Vladimir N. Oleynik"8e1bd4a2005-09-29 12:55:21 +0000202 syslog(LOG_INFO, "%s", msg + 1);
Eric Andersen36adca82004-06-22 10:07:17 +0000203 closelog();
Eric Andersen3ae0c781999-11-04 01:13:21 +0000204 }
Glenn L McGrathb4a1baa2003-01-13 22:09:50 +0000205
206 msg[l++] = '\n';
207 msg[l] = 0;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000208#else
Erik Andersene49d5ec2000-02-08 19:58:47 +0000209
Glenn L McGrathb4a1baa2003-01-13 22:09:50 +0000210 msg[l++] = '\n';
211 msg[l] = 0;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000212 /* Take full control of the log tty, and never close it.
213 * It's mine, all mine! Muhahahaha! */
214 if (log_fd < 0) {
Mike Frysingere548bdf2005-07-06 04:46:14 +0000215 if ((log_fd = device_open(log_console, O_RDWR | O_NONBLOCK | O_NOCTTY)) < 0) {
Erik Andersene132f4b2000-02-09 04:16:43 +0000216 log_fd = -2;
Mike Frysinger72a4c332005-07-05 02:19:20 +0000217 bb_error_msg("Bummer, can't write to log on %s!", log_console);
Erik Andersene132f4b2000-02-09 04:16:43 +0000218 device = CONSOLE;
Eric Andersen6a979902002-09-30 20:08:53 +0000219 } else {
220 fcntl(log_fd, F_SETFD, FD_CLOEXEC);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000221 }
222 }
223 if ((device & LOG) && (log_fd >= 0)) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000224 bb_full_write(log_fd, msg, l);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000225 }
Eric Andersen4c781471999-11-26 08:12:56 +0000226#endif
227
Erik Andersene49d5ec2000-02-08 19:58:47 +0000228 if (device & CONSOLE) {
Bernhard Reutner-Fischercf1f2ac2006-06-02 10:43:17 +0000229 int fd = device_open(CONSOLE_DEV,
Mike Frysingere548bdf2005-07-06 04:46:14 +0000230 O_WRONLY | O_NOCTTY | O_NONBLOCK);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000231 /* Always send console messages to /dev/console so people will see them. */
Glenn L McGrathb4a1baa2003-01-13 22:09:50 +0000232 if (fd >= 0) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000233 bb_full_write(fd, msg, l);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000234 close(fd);
Bernhard Reutner-Fischer35e1a072006-05-29 13:08:35 +0000235#if ENABLE_DEBUG_INIT
Glenn L McGrathb4a1baa2003-01-13 22:09:50 +0000236 /* all descriptors may be closed */
Erik Andersene49d5ec2000-02-08 19:58:47 +0000237 } else {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000238 bb_error_msg("Bummer, can't print: ");
Erik Andersene49d5ec2000-02-08 19:58:47 +0000239 va_start(arguments, fmt);
240 vfprintf(stderr, fmt, arguments);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000241 va_end(arguments);
Glenn L McGrathb4a1baa2003-01-13 22:09:50 +0000242#endif
Erik Andersene49d5ec2000-02-08 19:58:47 +0000243 }
Eric Andersen3ae0c781999-11-04 01:13:21 +0000244 }
Bernhard Reutner-Fischer3ab30802006-05-30 12:10:29 +0000245 RELEASE_CONFIG_BUFFER(msg);
Eric Andersencc8ed391999-10-05 16:24:54 +0000246}
247
Eric Andersen0460ff21999-10-25 23:32:44 +0000248/* Set terminal settings to reasonable defaults */
Bernhard Reutner-Fischer49e60b92006-05-29 12:57:52 +0000249static void set_term(void)
Eric Andersencc8ed391999-10-05 16:24:54 +0000250{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000251 struct termios tty;
Eric Andersencc8ed391999-10-05 16:24:54 +0000252
Bernhard Reutner-Fischer49e60b92006-05-29 12:57:52 +0000253 tcgetattr(STDIN_FILENO, &tty);
Eric Andersencc8ed391999-10-05 16:24:54 +0000254
Erik Andersene49d5ec2000-02-08 19:58:47 +0000255 /* set control chars */
Glenn L McGrathbaf55a82002-08-22 18:22:10 +0000256 tty.c_cc[VINTR] = 3; /* C-c */
257 tty.c_cc[VQUIT] = 28; /* C-\ */
258 tty.c_cc[VERASE] = 127; /* C-? */
259 tty.c_cc[VKILL] = 21; /* C-u */
260 tty.c_cc[VEOF] = 4; /* C-d */
Eric Andersen3849f9b2000-07-10 19:56:47 +0000261 tty.c_cc[VSTART] = 17; /* C-q */
Glenn L McGrathbaf55a82002-08-22 18:22:10 +0000262 tty.c_cc[VSTOP] = 19; /* C-s */
263 tty.c_cc[VSUSP] = 26; /* C-z */
Eric Andersen02bc25b2000-07-06 21:29:32 +0000264
Erik Andersene49d5ec2000-02-08 19:58:47 +0000265 /* use line dicipline 0 */
266 tty.c_line = 0;
Eric Andersen7f1acfd1999-10-29 23:09:13 +0000267
Erik Andersene49d5ec2000-02-08 19:58:47 +0000268 /* Make it be sane */
Glenn L McGrathbaf55a82002-08-22 18:22:10 +0000269 tty.c_cflag &= CBAUD | CBAUDEX | CSIZE | CSTOPB | PARENB | PARODD;
270 tty.c_cflag |= CREAD | HUPCL | CLOCAL;
Eric Andersend8862922001-04-23 15:14:11 +0000271
Eric Andersen08b10341999-11-19 02:38:58 +0000272
Erik Andersene49d5ec2000-02-08 19:58:47 +0000273 /* input modes */
274 tty.c_iflag = ICRNL | IXON | IXOFF;
Eric Andersen08b10341999-11-19 02:38:58 +0000275
Erik Andersene49d5ec2000-02-08 19:58:47 +0000276 /* output modes */
277 tty.c_oflag = OPOST | ONLCR;
Eric Andersen7f1acfd1999-10-29 23:09:13 +0000278
Erik Andersene49d5ec2000-02-08 19:58:47 +0000279 /* local modes */
280 tty.c_lflag =
281 ISIG | ICANON | ECHO | ECHOE | ECHOK | ECHOCTL | ECHOKE | IEXTEN;
Eric Andersencc8ed391999-10-05 16:24:54 +0000282
Bernhard Reutner-Fischer49e60b92006-05-29 12:57:52 +0000283 tcsetattr(STDIN_FILENO, TCSANOW, &tty);
Eric Andersencc8ed391999-10-05 16:24:54 +0000284}
285
Eric Andersen74400cc2001-10-18 04:11:39 +0000286static void console_init(void)
Eric Andersen0460ff21999-10-25 23:32:44 +0000287{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000288 int fd;
Glenn L McGrathb4a1baa2003-01-13 22:09:50 +0000289 int tried = 0;
Erik Andersen029011b2000-03-04 21:19:32 +0000290 struct vt_stat vt;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000291 struct serial_struct sr;
292 char *s;
Eric Andersen0460ff21999-10-25 23:32:44 +0000293
Glenn L McGrathb4a1baa2003-01-13 22:09:50 +0000294 if ((s = getenv("CONSOLE")) != NULL || (s = getenv("console")) != NULL) {
Matt Kraai18447702001-05-18 21:24:58 +0000295 safe_strncpy(console, s, sizeof(console));
Glenn L McGrathb4a1baa2003-01-13 22:09:50 +0000296 } else {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000297 /* 2.2 kernels: identify the real console backend and try to use it */
298 if (ioctl(0, TIOCGSERIAL, &sr) == 0) {
299 /* this is a serial console */
Matt Kraai439e3df2001-07-23 14:52:08 +0000300 snprintf(console, sizeof(console) - 1, SC_FORMAT, sr.line);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000301 } else if (ioctl(0, VT_GETSTATE, &vt) == 0) {
302 /* this is linux virtual tty */
Matt Kraai439e3df2001-07-23 14:52:08 +0000303 snprintf(console, sizeof(console) - 1, VC_FORMAT, vt.v_active);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000304 } else {
Bernhard Reutner-Fischercf1f2ac2006-06-02 10:43:17 +0000305 safe_strncpy(console, CONSOLE_DEV, sizeof(console));
Glenn L McGrathb4a1baa2003-01-13 22:09:50 +0000306 tried++;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000307 }
Eric Andersen07e52971999-11-07 07:38:08 +0000308 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000309
Glenn L McGrathb4a1baa2003-01-13 22:09:50 +0000310 while ((fd = open(console, O_RDONLY | O_NONBLOCK)) < 0 && tried < 2) {
311 /* Can't open selected console -- try
312 logical system console and VT_MASTER */
Bernhard Reutner-Fischercf1f2ac2006-06-02 10:43:17 +0000313 safe_strncpy(console, (tried == 0 ? CONSOLE_DEV : CURRENT_VC),
Glenn L McGrathb4a1baa2003-01-13 22:09:50 +0000314 sizeof(console));
315 tried++;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000316 }
317 if (fd < 0) {
318 /* Perhaps we should panic here? */
Glenn L McGrathb4a1baa2003-01-13 22:09:50 +0000319#ifndef CONFIG_SYSLOGD
Mike Frysinger72a4c332005-07-05 02:19:20 +0000320 log_console =
Glenn L McGrathb4a1baa2003-01-13 22:09:50 +0000321#endif
"Vladimir N. Oleynik"6c35c7c2005-10-12 15:34:25 +0000322 safe_strncpy(console, bb_dev_null, sizeof(console));
Eric Andersen07e52971999-11-07 07:38:08 +0000323 } else {
Glenn L McGrathb4a1baa2003-01-13 22:09:50 +0000324 s = getenv("TERM");
Eric Andersen3bc2b202002-07-29 06:39:58 +0000325 /* check for serial console */
Glenn L McGrathb4a1baa2003-01-13 22:09:50 +0000326 if (ioctl(fd, TIOCGSERIAL, &sr) == 0) {
Erik Andersenfa4718e2000-02-21 19:25:12 +0000327 /* Force the TERM setting to vt102 for serial console --
Eric Andersen3bc2b202002-07-29 06:39:58 +0000328 * if TERM is set to linux (the default) */
Glenn L McGrathb4a1baa2003-01-13 22:09:50 +0000329 if (s == NULL || strcmp(s, "linux") == 0)
330 putenv("TERM=vt102");
331#ifndef CONFIG_SYSLOGD
Mike Frysinger72a4c332005-07-05 02:19:20 +0000332 log_console = console;
Glenn L McGrathb4a1baa2003-01-13 22:09:50 +0000333#endif
334 } else {
335 if (s == NULL)
336 putenv("TERM=linux");
Erik Andersene49d5ec2000-02-08 19:58:47 +0000337 }
338 close(fd);
Eric Andersen07e52971999-11-07 07:38:08 +0000339 }
Glenn L McGrathb4a1baa2003-01-13 22:09:50 +0000340 messageD(LOG, "console=%s", console);
Eric Andersen0460ff21999-10-25 23:32:44 +0000341}
Glenn L McGrathbaf55a82002-08-22 18:22:10 +0000342
Eric Andersen7ef1a5b2001-03-20 17:39:08 +0000343static void fixup_argv(int argc, char **argv, char *new_argv0)
344{
345 int len;
Glenn L McGrathbaf55a82002-08-22 18:22:10 +0000346
Eric Andersen7ef1a5b2001-03-20 17:39:08 +0000347 /* Fix up argv[0] to be certain we claim to be init */
348 len = strlen(argv[0]);
349 memset(argv[0], 0, len);
Eric Andersen72f9a422001-10-28 05:12:20 +0000350 safe_strncpy(argv[0], new_argv0, len + 1);
Eric Andersen7ef1a5b2001-03-20 17:39:08 +0000351
352 /* Wipe argv[1]-argv[N] so they don't clutter the ps listing */
353 len = 1;
354 while (argc > len) {
355 memset(argv[len], 0, strlen(argv[len]));
356 len++;
357 }
358}
359
Bernhard Reutner-Fischer0da069d2006-05-29 12:54:16 +0000360/* Open the new terminal device */
Bernhard Reutner-Fischercf1f2ac2006-06-02 10:43:17 +0000361static void open_new_terminal(const char * const device, const int fail) {
Bernhard Reutner-Fischer0da069d2006-05-29 12:54:16 +0000362 struct stat sb;
363
364 if ((device_open(device, O_RDWR)) < 0) {
365 if (stat(device, &sb) != 0) {
366 message(LOG | CONSOLE, "device '%s' does not exist.", device);
367 } else {
368 message(LOG | CONSOLE, "Bummer, can't open %s", device);
369 }
370 if (fail)
371 _exit(1);
372 /* else */
Bernhard Reutner-Fischercf1f2ac2006-06-02 10:43:17 +0000373#if !ENABLE_DEBUG_INIT
Paul Foxd112f8f2006-06-01 13:17:49 +0000374 shutdown_signal(SIGUSR1);
Bernhard Reutner-Fischercf1f2ac2006-06-02 10:43:17 +0000375#else
376 _exit(2);
377#endif
Bernhard Reutner-Fischer0da069d2006-05-29 12:54:16 +0000378 }
379}
380
Glenn L McGrathb4a1baa2003-01-13 22:09:50 +0000381static pid_t run(const struct init_action *a)
Eric Andersen0298be82002-03-05 15:12:19 +0000382{
Bernhard Reutner-Fischer3ab30802006-05-30 12:10:29 +0000383 int i;
Mike Frysinger10427ab2005-07-06 05:00:48 +0000384 pid_t pid;
Eric Andersen0826b6b2002-07-03 23:50:16 +0000385 char *s, *tmpCmd, *cmd[INIT_BUFFS_SIZE], *cmdpath;
Glenn L McGrathbaf55a82002-08-22 18:22:10 +0000386 char buf[INIT_BUFFS_SIZE + 6]; /* INIT_BUFFS_SIZE+strlen("exec ")+1 */
Eric Andersen0298be82002-03-05 15:12:19 +0000387 sigset_t nmask, omask;
Eric Andersen0298be82002-03-05 15:12:19 +0000388 static const char press_enter[] =
389#ifdef CUSTOMIZED_BANNER
390#include CUSTOMIZED_BANNER
391#endif
392 "\nPlease press Enter to activate this console. ";
Erik Andersenac6e71f2000-01-08 22:04:33 +0000393
Eric Andersen0298be82002-03-05 15:12:19 +0000394 /* Block sigchild while forking. */
395 sigemptyset(&nmask);
396 sigaddset(&nmask, SIGCHLD);
397 sigprocmask(SIG_BLOCK, &nmask, &omask);
398
Glenn L McGrathbaf55a82002-08-22 18:22:10 +0000399 if ((pid = fork()) == 0) {
Mike Frysinger10427ab2005-07-06 05:00:48 +0000400
Erik Andersene49d5ec2000-02-08 19:58:47 +0000401 /* Clean up */
402 close(0);
403 close(1);
404 close(2);
Eric Andersen0298be82002-03-05 15:12:19 +0000405 sigprocmask(SIG_SETMASK, &omask, NULL);
406
Eric Andersen0298be82002-03-05 15:12:19 +0000407 /* Reset signal handlers that were set by the parent process */
Erik Andersene49d5ec2000-02-08 19:58:47 +0000408 signal(SIGUSR1, SIG_DFL);
409 signal(SIGUSR2, SIG_DFL);
Glenn L McGrathbaf55a82002-08-22 18:22:10 +0000410 signal(SIGINT, SIG_DFL);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000411 signal(SIGTERM, SIG_DFL);
Glenn L McGrathbaf55a82002-08-22 18:22:10 +0000412 signal(SIGHUP, SIG_DFL);
Mike Frysingera77b4f32005-04-16 08:21:34 +0000413 signal(SIGQUIT, SIG_DFL);
Eric Andersened8a9be2001-11-30 19:10:58 +0000414 signal(SIGCONT, SIG_DFL);
415 signal(SIGSTOP, SIG_DFL);
416 signal(SIGTSTP, SIG_DFL);
Eric Andersen2f6c04f1999-11-01 23:59:44 +0000417
Eric Andersen599e3ce2002-07-03 11:08:10 +0000418 /* Create a new session and make ourself the process
Eric Andersene8a90fb2002-10-12 04:05:48 +0000419 * group leader */
420 setsid();
Eric Andersen599e3ce2002-07-03 11:08:10 +0000421
Eric Andersen0298be82002-03-05 15:12:19 +0000422 /* Open the new terminal device */
Bernhard Reutner-Fischer0da069d2006-05-29 12:54:16 +0000423 open_new_terminal(a->terminal, 1);
Eric Andersen599e3ce2002-07-03 11:08:10 +0000424
Eric Andersen0298be82002-03-05 15:12:19 +0000425 /* Make sure the terminal will act fairly normal for us */
Bernhard Reutner-Fischer49e60b92006-05-29 12:57:52 +0000426 set_term();
Eric Andersen0826b6b2002-07-03 23:50:16 +0000427 /* Setup stdout, stderr for the new process so
Eric Andersen599e3ce2002-07-03 11:08:10 +0000428 * they point to the supplied terminal */
Eric Andersen0826b6b2002-07-03 23:50:16 +0000429 dup(0);
430 dup(0);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000431
Eric Andersen599e3ce2002-07-03 11:08:10 +0000432 /* If the init Action requires us to wait, then force the
Eric Andersen0298be82002-03-05 15:12:19 +0000433 * supplied terminal to be the controlling tty. */
Glenn L McGrathbaf55a82002-08-22 18:22:10 +0000434 if (a->action & (SYSINIT | WAIT | CTRLALTDEL | SHUTDOWN | RESTART)) {
Eric Andersen0298be82002-03-05 15:12:19 +0000435
436 /* Now fork off another process to just hang around */
437 if ((pid = fork()) < 0) {
Glenn L McGrathb4a1baa2003-01-13 22:09:50 +0000438 message(LOG | CONSOLE, "Can't fork!");
Eric Andersen0298be82002-03-05 15:12:19 +0000439 _exit(1);
440 }
441
442 if (pid > 0) {
443
444 /* We are the parent -- wait till the child is done */
445 signal(SIGINT, SIG_IGN);
446 signal(SIGTSTP, SIG_IGN);
447 signal(SIGQUIT, SIG_IGN);
448 signal(SIGCHLD, SIG_DFL);
449
Bernhard Reutner-Fischer3ab30802006-05-30 12:10:29 +0000450 waitfor(NULL, pid);
Eric Andersen0298be82002-03-05 15:12:19 +0000451 /* See if stealing the controlling tty back is necessary */
Bernhard Reutner-Fischer3ab30802006-05-30 12:10:29 +0000452 if (tcgetpgrp(0) != getpid())
Eric Andersen0298be82002-03-05 15:12:19 +0000453 _exit(0);
454
455 /* Use a temporary process to steal the controlling tty. */
456 if ((pid = fork()) < 0) {
Glenn L McGrathb4a1baa2003-01-13 22:09:50 +0000457 message(LOG | CONSOLE, "Can't fork!");
Eric Andersen0298be82002-03-05 15:12:19 +0000458 _exit(1);
Glenn L McGrathbaf55a82002-08-22 18:22:10 +0000459 }
Eric Andersen0298be82002-03-05 15:12:19 +0000460 if (pid == 0) {
461 setsid();
Eric Andersen0826b6b2002-07-03 23:50:16 +0000462 ioctl(0, TIOCSCTTY, 1);
Eric Andersen0298be82002-03-05 15:12:19 +0000463 _exit(0);
Glenn L McGrathbaf55a82002-08-22 18:22:10 +0000464 }
Bernhard Reutner-Fischer3ab30802006-05-30 12:10:29 +0000465 waitfor(NULL, pid);
Eric Andersen0298be82002-03-05 15:12:19 +0000466 _exit(0);
467 }
468
469 /* Now fall though to actually execute things */
Eric Andersen0298be82002-03-05 15:12:19 +0000470 }
471
Erik Andersene132f4b2000-02-09 04:16:43 +0000472 /* See if any special /bin/sh requiring characters are present */
Eric Andersen0298be82002-03-05 15:12:19 +0000473 if (strpbrk(a->command, "~`!$^&*()=|\\{}[];\"'<>?") != NULL) {
Glenn L McGrathdc4e75e2003-09-02 02:36:18 +0000474 cmd[0] = (char *)DEFAULT_SHELL;
Erik Andersene132f4b2000-02-09 04:16:43 +0000475 cmd[1] = "-c";
Glenn L McGrathb4a1baa2003-01-13 22:09:50 +0000476 cmd[2] = strcat(strcpy(buf, "exec "), a->command);
Erik Andersene132f4b2000-02-09 04:16:43 +0000477 cmd[3] = NULL;
478 } else {
479 /* Convert command (char*) into cmd (char**, one word per string) */
Eric Andersen0826b6b2002-07-03 23:50:16 +0000480 strcpy(buf, a->command);
Eric Andersen72f9a422001-10-28 05:12:20 +0000481 s = buf;
Glenn L McGrathbaf55a82002-08-22 18:22:10 +0000482 for (tmpCmd = buf, i = 0; (tmpCmd = strsep(&s, " \t")) != NULL;) {
Erik Andersene132f4b2000-02-09 04:16:43 +0000483 if (*tmpCmd != '\0') {
484 cmd[i] = tmpCmd;
Erik Andersene132f4b2000-02-09 04:16:43 +0000485 i++;
486 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000487 }
Erik Andersene132f4b2000-02-09 04:16:43 +0000488 cmd[i] = NULL;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000489 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000490
Eric Andersen7ef1a5b2001-03-20 17:39:08 +0000491 cmdpath = cmd[0];
Eric Andersen7e3bf6e2000-09-14 22:01:31 +0000492
Eric Andersen7ef1a5b2001-03-20 17:39:08 +0000493 /*
494 Interactive shells want to see a dash in argv[0]. This
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000495 typically is handled by login, argv will be setup this
496 way if a dash appears at the front of the command path
Eric Andersen7e3bf6e2000-09-14 22:01:31 +0000497 (like "-/bin/sh").
Eric Andersen7ef1a5b2001-03-20 17:39:08 +0000498 */
Eric Andersen7e3bf6e2000-09-14 22:01:31 +0000499
Eric Andersen7ef1a5b2001-03-20 17:39:08 +0000500 if (*cmdpath == '-') {
Eric Andersen7e3bf6e2000-09-14 22:01:31 +0000501
Eric Andersen7ef1a5b2001-03-20 17:39:08 +0000502 /* skip over the dash */
503 ++cmdpath;
Eric Andersen7e3bf6e2000-09-14 22:01:31 +0000504
Eric Andersen7ef1a5b2001-03-20 17:39:08 +0000505 /* find the last component in the command pathname */
Manuel Novoa III cad53642003-03-19 09:13:01 +0000506 s = bb_get_last_path_component(cmdpath);
Eric Andersen7e3bf6e2000-09-14 22:01:31 +0000507
Eric Andersen7ef1a5b2001-03-20 17:39:08 +0000508 /* make a new argv[0] */
Glenn L McGrathbaf55a82002-08-22 18:22:10 +0000509 if ((cmd[0] = malloc(strlen(s) + 2)) == NULL) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000510 message(LOG | CONSOLE, bb_msg_memory_exhausted);
Eric Andersen7ef1a5b2001-03-20 17:39:08 +0000511 cmd[0] = cmdpath;
512 } else {
513 cmd[0][0] = '-';
Glenn L McGrathbaf55a82002-08-22 18:22:10 +0000514 strcpy(cmd[0] + 1, s);
Eric Andersen7ef1a5b2001-03-20 17:39:08 +0000515 }
Paul Fox41a72ec2005-08-01 16:43:13 +0000516#ifdef CONFIG_FEATURE_INIT_SCTTY
517 /* Establish this process as session leader and
518 * (attempt) to make the tty (if any) a controlling tty.
519 */
520 (void) setsid();
521 (void) ioctl(0, TIOCSCTTY, 0/*don't steal it*/);
522#endif
Eric Andersen7ef1a5b2001-03-20 17:39:08 +0000523 }
524
Eric Andersen1f50e842004-08-16 09:29:42 +0000525#if !defined(__UCLIBC__) || defined(__ARCH_HAS_MMU__)
Eric Andersen0298be82002-03-05 15:12:19 +0000526 if (a->action & ASKFIRST) {
Glenn L McGrathb4a1baa2003-01-13 22:09:50 +0000527 char c;
Eric Andersen7ef1a5b2001-03-20 17:39:08 +0000528 /*
529 * Save memory by not exec-ing anything large (like a shell)
530 * before the user wants it. This is critical if swap is not
531 * enabled and the system has low memory. Generally this will
532 * be run on the second virtual console, and the first will
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000533 * be allowed to start a shell or whatever an init script
Eric Andersen7ef1a5b2001-03-20 17:39:08 +0000534 * specifies.
535 */
Glenn L McGrathb4a1baa2003-01-13 22:09:50 +0000536 messageD(LOG, "Waiting for enter to start '%s'"
537 "(pid %d, terminal %s)\n",
Glenn L McGrathbaf55a82002-08-22 18:22:10 +0000538 cmdpath, getpid(), a->terminal);
Manuel Novoa III cad53642003-03-19 09:13:01 +0000539 bb_full_write(1, press_enter, sizeof(press_enter) - 1);
Glenn L McGrathb4a1baa2003-01-13 22:09:50 +0000540 while(read(0, &c, 1) == 1 && c != '\n')
541 ;
Eric Andersen7ef1a5b2001-03-20 17:39:08 +0000542 }
Eric Andersen1f50e842004-08-16 09:29:42 +0000543#endif
Eric Andersen7ef1a5b2001-03-20 17:39:08 +0000544
Eric Andersen7ef1a5b2001-03-20 17:39:08 +0000545 /* Log the process name and args */
Glenn L McGrathb4a1baa2003-01-13 22:09:50 +0000546 message(LOG, "Starting pid %d, console %s: '%s'",
Glenn L McGrathbaf55a82002-08-22 18:22:10 +0000547 getpid(), a->terminal, cmdpath);
Eric Andersen7e3bf6e2000-09-14 22:01:31 +0000548
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000549#if defined CONFIG_FEATURE_INIT_COREDUMPS
Bernhard Reutner-Fischer0da069d2006-05-29 12:54:16 +0000550 {
551 struct stat sb;
552 if (stat(CORE_ENABLE_FLAG_FILE, &sb) == 0) {
553 struct rlimit limit;
Glenn L McGrathbaf55a82002-08-22 18:22:10 +0000554
Bernhard Reutner-Fischer0da069d2006-05-29 12:54:16 +0000555 limit.rlim_cur = RLIM_INFINITY;
556 limit.rlim_max = RLIM_INFINITY;
557 setrlimit(RLIMIT_CORE, &limit);
558 }
Erik Andersen983b51b2000-04-04 18:14:25 +0000559 }
Erik Andersen183da4a2000-04-04 18:36:37 +0000560#endif
Erik Andersen983b51b2000-04-04 18:14:25 +0000561
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000562 /* Now run it. The new program will take over this PID,
Erik Andersene49d5ec2000-02-08 19:58:47 +0000563 * so nothing further in init.c should be run. */
Glenn L McGrathb4a1baa2003-01-13 22:09:50 +0000564 execv(cmdpath, cmd);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000565
Eric Andersen7ef1a5b2001-03-20 17:39:08 +0000566 /* We're still here? Some error happened. */
Glenn L McGrathb4a1baa2003-01-13 22:09:50 +0000567 message(LOG | CONSOLE, "Bummer, could not run '%s': %m", cmdpath);
Eric Andersen0298be82002-03-05 15:12:19 +0000568 _exit(-1);
Erik Andersen05df2392000-01-13 04:43:48 +0000569 }
Eric Andersen0298be82002-03-05 15:12:19 +0000570 sigprocmask(SIG_SETMASK, &omask, NULL);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000571 return pid;
Eric Andersen0460ff21999-10-25 23:32:44 +0000572}
573
Bernhard Reutner-Fischer3ab30802006-05-30 12:10:29 +0000574static int waitfor(const struct init_action *a, pid_t pid)
Erik Andersen0e3782f2000-01-07 02:54:55 +0000575{
Bernhard Reutner-Fischer3ab30802006-05-30 12:10:29 +0000576 int runpid;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000577 int status, wpid;
Erik Andersen0e3782f2000-01-07 02:54:55 +0000578
Bernhard Reutner-Fischer3ab30802006-05-30 12:10:29 +0000579 runpid = (NULL == a)? pid : run(a);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000580 while (1) {
Bernhard Reutner-Fischer3ab30802006-05-30 12:10:29 +0000581 wpid = waitpid(runpid,&status,0);
582 if (wpid == runpid)
Erik Andersene49d5ec2000-02-08 19:58:47 +0000583 break;
Glenn L McGrathe6ba16f2003-09-26 10:45:55 +0000584 if (wpid == -1 && errno == ECHILD) {
585 /* we missed its termination */
586 break;
587 }
588 /* FIXME other errors should maybe trigger an error, but allow
589 * the program to continue */
Erik Andersen0e3782f2000-01-07 02:54:55 +0000590 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000591 return wpid;
Erik Andersen0e3782f2000-01-07 02:54:55 +0000592}
593
Eric Andersen0298be82002-03-05 15:12:19 +0000594/* Run all commands of a particular type */
595static void run_actions(int action)
Eric Andersen219d6f51999-11-02 19:43:01 +0000596{
Eric Andersen0298be82002-03-05 15:12:19 +0000597 struct init_action *a, *tmp;
Eric Andersen219d6f51999-11-02 19:43:01 +0000598
Eric Andersen0298be82002-03-05 15:12:19 +0000599 for (a = init_action_list; a; a = tmp) {
600 tmp = a->next;
Eric Andersenc97ec342001-04-03 18:01:51 +0000601 if (a->action == action) {
Rob Landley2dd42792006-03-22 17:39:13 +0000602 if (access(a->terminal, R_OK | W_OK)) {
603 delete_init_action(a);
604 } else if (a->action & (SYSINIT | WAIT | CTRLALTDEL | SHUTDOWN | RESTART)) {
Bernhard Reutner-Fischer3ab30802006-05-30 12:10:29 +0000605 waitfor(a, 0);
Eric Andersen0298be82002-03-05 15:12:19 +0000606 delete_init_action(a);
607 } else if (a->action & ONCE) {
608 run(a);
609 delete_init_action(a);
Glenn L McGrathbaf55a82002-08-22 18:22:10 +0000610 } else if (a->action & (RESPAWN | ASKFIRST)) {
Eric Andersen0298be82002-03-05 15:12:19 +0000611 /* Only run stuff with pid==0. If they have
612 * a pid, that means it is still running */
613 if (a->pid == 0) {
614 a->pid = run(a);
615 }
616 }
Erik Andersene132f4b2000-02-09 04:16:43 +0000617 }
618 }
619}
620
Bernhard Reutner-Fischer35e1a072006-05-29 13:08:35 +0000621#if !ENABLE_DEBUG_INIT
Eric Andersen2c1de612003-04-24 11:41:28 +0000622static void init_reboot(unsigned long magic)
623{
624 pid_t pid;
625 /* We have to fork here, since the kernel calls do_exit(0) in
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000626 * linux/kernel/sys.c, which can cause the machine to panic when
Eric Andersen2c1de612003-04-24 11:41:28 +0000627 * the init process is killed.... */
628 if ((pid = fork()) == 0) {
Eric Andersen2c1de612003-04-24 11:41:28 +0000629 reboot(magic);
Eric Andersen2c1de612003-04-24 11:41:28 +0000630 _exit(0);
631 }
632 waitpid (pid, NULL, 0);
633}
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);
Eric Andersen0298be82002-03-05 15:12:19 +0000716 execl(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{
Paul Foxd112f8f2006-06-01 13:17:49 +0000730 char *m;
731 int rb;
732
Erik Andersene49d5ec2000-02-08 19:58:47 +0000733 shutdown_system();
Paul Foxd112f8f2006-06-01 13:17:49 +0000734
735 if (sig == SIGTERM) {
736 m = "reboot";
737 rb = RB_AUTOBOOT;
738 } else if (sig == SIGUSR2) {
739 m = "poweroff";
740 rb = RB_POWER_OFF;
741 } else {
742 m = "halt";
743 rb = RB_HALT_SYSTEM;
744 }
745 message(CONSOLE | LOG, "Requesting system %s.", m);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000746 sync();
Erik Andersen3fe39dc2000-01-25 18:13:53 +0000747
Erik Andersene49d5ec2000-02-08 19:58:47 +0000748 /* allow time for last message to reach serial console */
Erik Andersene132f4b2000-02-09 04:16:43 +0000749 sleep(2);
Erik Andersen3fe39dc2000-01-25 18:13:53 +0000750
Paul Foxd112f8f2006-06-01 13:17:49 +0000751 init_reboot(rb);
Matt Kraai67a46402001-06-03 05:55:52 +0000752
753 loop_forever();
Eric Andersencc8ed391999-10-05 16:24:54 +0000754}
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000755
"Vladimir N. Oleynik"1f04c9d2006-02-01 14:47:52 +0000756static void ctrlaltdel_signal(int sig ATTRIBUTE_UNUSED)
Eric Andersenc97ec342001-04-03 18:01:51 +0000757{
758 run_actions(CTRLALTDEL);
759}
760
Eric Andersen0298be82002-03-05 15:12:19 +0000761/* The SIGSTOP & SIGTSTP handler */
"Vladimir N. Oleynik"1f04c9d2006-02-01 14:47:52 +0000762static void stop_handler(int sig ATTRIBUTE_UNUSED)
Eric Andersened8a9be2001-11-30 19:10:58 +0000763{
Eric Andersen0298be82002-03-05 15:12:19 +0000764 int saved_errno = errno;
Eric Andersened8a9be2001-11-30 19:10:58 +0000765
766 got_cont = 0;
Glenn L McGrathbaf55a82002-08-22 18:22:10 +0000767 while (!got_cont)
768 pause();
Eric Andersened8a9be2001-11-30 19:10:58 +0000769 got_cont = 0;
770 errno = saved_errno;
771}
772
Glenn L McGrathbaf55a82002-08-22 18:22:10 +0000773/* The SIGCONT handler */
"Vladimir N. Oleynik"1f04c9d2006-02-01 14:47:52 +0000774static void cont_handler(int sig ATTRIBUTE_UNUSED)
Eric Andersened8a9be2001-11-30 19:10:58 +0000775{
776 got_cont = 1;
777}
778
Bernhard Reutner-Fischer35e1a072006-05-29 13:08:35 +0000779#endif /* ! ENABLE_DEBUG_INIT */
Erik Andersende552872000-01-23 01:34:05 +0000780
Glenn L McGrathdc4e75e2003-09-02 02:36:18 +0000781static void new_init_action(int action, const char *command, const char *cons)
Erik Andersen7dc16072000-01-04 01:10:25 +0000782{
Eric Andersen22718092004-10-08 08:17:39 +0000783 struct init_action *new_action, *a, *last;
Erik Andersen9e737252000-01-06 01:16:13 +0000784
Erik Andersene49d5ec2000-02-08 19:58:47 +0000785 if (*cons == '\0')
786 cons = console;
Erik Andersen9e737252000-01-06 01:16:13 +0000787
"Vladimir N. Oleynik"6c35c7c2005-10-12 15:34:25 +0000788 if (strcmp(cons, bb_dev_null) == 0 && (action & ASKFIRST))
Eric Andersen1644db92001-09-05 20:18:15 +0000789 return;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000790
Eric Andersen0298be82002-03-05 15:12:19 +0000791 new_action = calloc((size_t) (1), sizeof(struct init_action));
792 if (!new_action) {
Glenn L McGrathb4a1baa2003-01-13 22:09:50 +0000793 message(LOG | CONSOLE, "Memory allocation failure");
Matt Kraai67a46402001-06-03 05:55:52 +0000794 loop_forever();
Erik Andersene49d5ec2000-02-08 19:58:47 +0000795 }
Eric Andersen0298be82002-03-05 15:12:19 +0000796
797 /* Append to the end of the list */
Eric Andersen22718092004-10-08 08:17:39 +0000798 for (a = last = init_action_list; a; a = a->next) {
Eric Andersen82baf632004-10-08 08:21:54 +0000799 /* don't enter action if it's already in the list,
800 * but do overwrite existing actions */
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000801 if ((strcmp(a->command, command) == 0) &&
Eric Andersen6fd0e312003-07-22 09:48:56 +0000802 (strcmp(a->terminal, cons) ==0)) {
Eric Andersen82baf632004-10-08 08:21:54 +0000803 a->action = action;
Eric Andersen6fd0e312003-07-22 09:48:56 +0000804 free(new_action);
805 return;
806 }
Eric Andersen22718092004-10-08 08:17:39 +0000807 last = a;
Eric Andersen6fd0e312003-07-22 09:48:56 +0000808 }
Eric Andersen22718092004-10-08 08:17:39 +0000809 if (last) {
810 last->next = new_action;
Eric Andersen1644db92001-09-05 20:18:15 +0000811 } else {
Eric Andersen0298be82002-03-05 15:12:19 +0000812 init_action_list = new_action;
Eric Andersen1644db92001-09-05 20:18:15 +0000813 }
Eric Andersen0826b6b2002-07-03 23:50:16 +0000814 strcpy(new_action->command, command);
Eric Andersen0298be82002-03-05 15:12:19 +0000815 new_action->action = action;
Eric Andersen0826b6b2002-07-03 23:50:16 +0000816 strcpy(new_action->terminal, cons);
Glenn L McGrathb4a1baa2003-01-13 22:09:50 +0000817 messageD(LOG|CONSOLE, "command='%s' action='%d' terminal='%s'\n",
818 new_action->command, new_action->action, new_action->terminal);
Erik Andersen7dc16072000-01-04 01:10:25 +0000819}
820
Glenn L McGrathbaf55a82002-08-22 18:22:10 +0000821static void delete_init_action(struct init_action *action)
Erik Andersen7dc16072000-01-04 01:10:25 +0000822{
Eric Andersen0298be82002-03-05 15:12:19 +0000823 struct init_action *a, *b = NULL;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000824
Eric Andersen0298be82002-03-05 15:12:19 +0000825 for (a = init_action_list; a; b = a, a = a->next) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000826 if (a == action) {
827 if (b == NULL) {
Eric Andersen0298be82002-03-05 15:12:19 +0000828 init_action_list = a->next;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000829 } else {
Eric Andersen0298be82002-03-05 15:12:19 +0000830 b->next = a->next;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000831 }
832 free(a);
833 break;
834 }
Erik Andersen7dc16072000-01-04 01:10:25 +0000835 }
Erik Andersen7dc16072000-01-04 01:10:25 +0000836}
837
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000838/* NOTE that if CONFIG_FEATURE_USE_INITTAB is NOT defined,
Erik Andersen9e737252000-01-06 01:16:13 +0000839 * then parse_inittab() simply adds in some default
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000840 * actions(i.e., runs INIT_SCRIPT and then starts a pair
841 * of "askfirst" shells). If CONFIG_FEATURE_USE_INITTAB
842 * _is_ defined, but /etc/inittab is missing, this
Erik Andersen812d4662000-01-07 18:30:40 +0000843 * results in the same set of default behaviors.
Glenn L McGrathbaf55a82002-08-22 18:22:10 +0000844 */
Eric Andersen3e6ff902001-03-09 21:24:12 +0000845static void parse_inittab(void)
Erik Andersen7dc16072000-01-04 01:10:25 +0000846{
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000847#ifdef CONFIG_FEATURE_USE_INITTAB
Erik Andersene49d5ec2000-02-08 19:58:47 +0000848 FILE *file;
Glenn L McGrathb4a1baa2003-01-13 22:09:50 +0000849 char buf[INIT_BUFFS_SIZE], lineAsRead[INIT_BUFFS_SIZE];
850 char tmpConsole[CONSOLE_BUFF_SIZE];
Eric Andersen0298be82002-03-05 15:12:19 +0000851 char *id, *runlev, *action, *command, *eol;
852 const struct init_action_type *a = actions;
Erik Andersen7dc16072000-01-04 01:10:25 +0000853
854
Erik Andersene49d5ec2000-02-08 19:58:47 +0000855 file = fopen(INITTAB, "r");
856 if (file == NULL) {
857 /* No inittab file -- set up some default behavior */
Erik Andersen9e737252000-01-06 01:16:13 +0000858#endif
Eric Andersenc97ec342001-04-03 18:01:51 +0000859 /* Reboot on Ctrl-Alt-Del */
Glenn L McGrathb4a1baa2003-01-13 22:09:50 +0000860 new_init_action(CTRLALTDEL, "/sbin/reboot", "");
Eric Andersen1644db92001-09-05 20:18:15 +0000861 /* Umount all filesystems on halt/reboot */
Glenn L McGrathb4a1baa2003-01-13 22:09:50 +0000862 new_init_action(SHUTDOWN, "/bin/umount -a -r", "");
Eric Andersen1644db92001-09-05 20:18:15 +0000863 /* Swapoff on halt/reboot */
Rob Landley7a8f6792005-08-30 18:17:05 +0000864 if(ENABLE_SWAPONOFF) new_init_action(SHUTDOWN, "/sbin/swapoff -a", "");
Eric Andersen730f8262001-12-17 23:13:08 +0000865 /* Prepare to restart init when a HUP is received */
Glenn L McGrathb4a1baa2003-01-13 22:09:50 +0000866 new_init_action(RESTART, "/sbin/init", "");
Eric Andersen3bc2b202002-07-29 06:39:58 +0000867 /* Askfirst shell on tty1-4 */
Glenn L McGrathdc4e75e2003-09-02 02:36:18 +0000868 new_init_action(ASKFIRST, bb_default_login_shell, "");
869 new_init_action(ASKFIRST, bb_default_login_shell, VC_2);
870 new_init_action(ASKFIRST, bb_default_login_shell, VC_3);
871 new_init_action(ASKFIRST, bb_default_login_shell, VC_4);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000872 /* sysinit */
Glenn L McGrathb4a1baa2003-01-13 22:09:50 +0000873 new_init_action(SYSINIT, INIT_SCRIPT, "");
Erik Andersen7dc16072000-01-04 01:10:25 +0000874
Erik Andersene49d5ec2000-02-08 19:58:47 +0000875 return;
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000876#ifdef CONFIG_FEATURE_USE_INITTAB
Erik Andersen9e737252000-01-06 01:16:13 +0000877 }
Erik Andersen7dc16072000-01-04 01:10:25 +0000878
Eric Andersen0826b6b2002-07-03 23:50:16 +0000879 while (fgets(buf, INIT_BUFFS_SIZE, file) != NULL) {
Eric Andersenb5966362000-05-31 20:04:38 +0000880 /* Skip leading spaces */
881 for (id = buf; *id == ' ' || *id == '\t'; id++);
882
883 /* Skip the line if it's a comment */
884 if (*id == '#' || *id == '\n')
Erik Andersene49d5ec2000-02-08 19:58:47 +0000885 continue;
Erik Andersen7dc16072000-01-04 01:10:25 +0000886
Erik Andersene49d5ec2000-02-08 19:58:47 +0000887 /* Trim the trailing \n */
Eric Andersenb5966362000-05-31 20:04:38 +0000888 eol = strrchr(id, '\n');
889 if (eol != NULL)
890 *eol = '\0';
Erik Andersen7dc16072000-01-04 01:10:25 +0000891
Erik Andersene49d5ec2000-02-08 19:58:47 +0000892 /* Keep a copy around for posterity's sake (and error msgs) */
893 strcpy(lineAsRead, buf);
894
Eric Andersenb5966362000-05-31 20:04:38 +0000895 /* Separate the ID field from the runlevels */
896 runlev = strchr(id, ':');
897 if (runlev == NULL || *(runlev + 1) == '\0') {
Glenn L McGrathb4a1baa2003-01-13 22:09:50 +0000898 message(LOG | CONSOLE, "Bad inittab entry: %s", lineAsRead);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000899 continue;
900 } else {
Eric Andersenb5966362000-05-31 20:04:38 +0000901 *runlev = '\0';
902 ++runlev;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000903 }
904
Eric Andersenb5966362000-05-31 20:04:38 +0000905 /* Separate the runlevels from the action */
906 action = strchr(runlev, ':');
907 if (action == NULL || *(action + 1) == '\0') {
Glenn L McGrathb4a1baa2003-01-13 22:09:50 +0000908 message(LOG | CONSOLE, "Bad inittab entry: %s", lineAsRead);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000909 continue;
910 } else {
Eric Andersenb5966362000-05-31 20:04:38 +0000911 *action = '\0';
912 ++action;
913 }
914
Eric Andersen0298be82002-03-05 15:12:19 +0000915 /* Separate the action from the command */
916 command = strchr(action, ':');
917 if (command == NULL || *(command + 1) == '\0') {
Glenn L McGrathb4a1baa2003-01-13 22:09:50 +0000918 message(LOG | CONSOLE, "Bad inittab entry: %s", lineAsRead);
Eric Andersenb5966362000-05-31 20:04:38 +0000919 continue;
920 } else {
Eric Andersen0298be82002-03-05 15:12:19 +0000921 *command = '\0';
922 ++command;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000923 }
924
925 /* Ok, now process it */
Glenn L McGrathb4a1baa2003-01-13 22:09:50 +0000926 for (a = actions; a->name != 0; a++) {
Eric Andersenb5966362000-05-31 20:04:38 +0000927 if (strcmp(a->name, action) == 0) {
928 if (*id != '\0') {
Glenn L McGrathb4a1baa2003-01-13 22:09:50 +0000929 if(strncmp(id, "/dev/", 5) == 0)
930 id += 5;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000931 strcpy(tmpConsole, "/dev/");
Glenn L McGrathb4a1baa2003-01-13 22:09:50 +0000932 safe_strncpy(tmpConsole + 5, id,
933 CONSOLE_BUFF_SIZE - 5);
Eric Andersenb5966362000-05-31 20:04:38 +0000934 id = tmpConsole;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000935 }
Eric Andersen0298be82002-03-05 15:12:19 +0000936 new_init_action(a->action, command, id);
Glenn L McGrathb4a1baa2003-01-13 22:09:50 +0000937 break;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000938 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000939 }
Glenn L McGrathb4a1baa2003-01-13 22:09:50 +0000940 if (a->name == 0) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000941 /* Choke on an unknown action */
Glenn L McGrathb4a1baa2003-01-13 22:09:50 +0000942 message(LOG | CONSOLE, "Bad inittab entry: %s", lineAsRead);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000943 }
Erik Andersen7dc16072000-01-04 01:10:25 +0000944 }
Eric Andersend8636ca2002-05-15 22:19:09 +0000945 fclose(file);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000946 return;
Glenn L McGrathbaf55a82002-08-22 18:22:10 +0000947#endif /* CONFIG_FEATURE_USE_INITTAB */
Erik Andersen7dc16072000-01-04 01:10:25 +0000948}
949
Eric Andersen82baf632004-10-08 08:21:54 +0000950#ifdef CONFIG_FEATURE_USE_INITTAB
"Vladimir N. Oleynik"1f04c9d2006-02-01 14:47:52 +0000951static void reload_signal(int sig ATTRIBUTE_UNUSED)
Eric Andersen6fd0e312003-07-22 09:48:56 +0000952{
Eric Andersen82baf632004-10-08 08:21:54 +0000953 struct init_action *a, *tmp;
954
955 message(LOG, "Reloading /etc/inittab");
956
957 /* disable old entrys */
958 for (a = init_action_list; a; a = a->next ) {
959 a->action = ONCE;
960 }
961
962 parse_inittab();
963
964 /* remove unused entrys */
965 for (a = init_action_list; a; a = tmp) {
966 tmp = a->next;
967 if (a->action & (ONCE | SYSINIT | WAIT ) &&
968 a->pid == 0 ) {
969 delete_init_action(a);
970 }
971 }
Eric Andersen6fd0e312003-07-22 09:48:56 +0000972 run_actions(RESPAWN);
Eric Andersen82baf632004-10-08 08:21:54 +0000973 return;
Eric Andersen6fd0e312003-07-22 09:48:56 +0000974}
Eric Andersen82baf632004-10-08 08:21:54 +0000975#endif /* CONFIG_FEATURE_USE_INITTAB */
976
Rob Landleydfba7412006-03-06 20:47:33 +0000977int init_main(int argc, char **argv)
Eric Andersen0460ff21999-10-25 23:32:44 +0000978{
Eric Andersen0298be82002-03-05 15:12:19 +0000979 struct init_action *a;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000980 pid_t wpid;
Eric Andersen0460ff21999-10-25 23:32:44 +0000981
Eric Andersen730f8262001-12-17 23:13:08 +0000982 if (argc > 1 && !strcmp(argv[1], "-q")) {
Rob Landley2edf5262006-01-22 02:41:51 +0000983 return kill(1,SIGHUP);
Eric Andersen730f8262001-12-17 23:13:08 +0000984 }
Bernhard Reutner-Fischer35e1a072006-05-29 13:08:35 +0000985#if !ENABLE_DEBUG_INIT
Erik Andersena51ecdd2000-02-24 18:09:58 +0000986 /* Expect to be invoked as init with PID=1 or be invoked as linuxrc */
Rob Landley64612912006-01-30 08:31:37 +0000987 if (getpid() != 1 &&
988 (!ENABLE_FEATURE_INITRD || !strstr(bb_applet_name, "linuxrc")))
989 {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000990 bb_show_usage();
Erik Andersene49d5ec2000-02-08 19:58:47 +0000991 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000992 /* Set up sig handlers -- be sure to
993 * clear all of these in run() */
Glenn L McGrathbaf55a82002-08-22 18:22:10 +0000994 signal(SIGHUP, exec_signal);
Mike Frysingera77b4f32005-04-16 08:21:34 +0000995 signal(SIGQUIT, exec_signal);
Paul Foxd112f8f2006-06-01 13:17:49 +0000996 signal(SIGUSR1, shutdown_signal);
997 signal(SIGUSR2, shutdown_signal);
Glenn L McGrathbaf55a82002-08-22 18:22:10 +0000998 signal(SIGINT, ctrlaltdel_signal);
Paul Foxd112f8f2006-06-01 13:17:49 +0000999 signal(SIGTERM, shutdown_signal);
Eric Andersened8a9be2001-11-30 19:10:58 +00001000 signal(SIGCONT, cont_handler);
1001 signal(SIGSTOP, stop_handler);
1002 signal(SIGTSTP, stop_handler);
Eric Andersen0460ff21999-10-25 23:32:44 +00001003
Eric Andersenc7bda1c2004-03-15 08:29:22 +00001004 /* Turn off rebooting via CTL-ALT-DEL -- we get a
Erik Andersene49d5ec2000-02-08 19:58:47 +00001005 * SIGINT on CAD so we can shut things down gracefully... */
Erik Andersen4f3f7572000-04-28 00:18:56 +00001006 init_reboot(RB_DISABLE_CAD);
Erik Andersene49d5ec2000-02-08 19:58:47 +00001007#endif
Erik Andersen05df2392000-01-13 04:43:48 +00001008
Eric Andersen599e3ce2002-07-03 11:08:10 +00001009 /* Figure out where the default console should be */
1010 console_init();
1011
Erik Andersene49d5ec2000-02-08 19:58:47 +00001012 /* Close whatever files are open, and reset the console. */
1013 close(0);
1014 close(1);
1015 close(2);
Eric Andersen72f9a422001-10-28 05:12:20 +00001016
Glenn L McGrathbaf55a82002-08-22 18:22:10 +00001017 if (device_open(console, O_RDWR | O_NOCTTY) == 0) {
Bernhard Reutner-Fischer49e60b92006-05-29 12:57:52 +00001018 set_term();
Eric Andersen599e3ce2002-07-03 11:08:10 +00001019 close(0);
1020 }
1021
Erik Andersen983b51b2000-04-04 18:14:25 +00001022 chdir("/");
Erik Andersene49d5ec2000-02-08 19:58:47 +00001023 setsid();
Glenn L McGrathb4a1baa2003-01-13 22:09:50 +00001024 {
1025 const char * const *e;
1026 /* Make sure environs is set to something sane */
1027 for(e = environment; *e; e++)
Manuel Novoa III cad53642003-03-19 09:13:01 +00001028 putenv((char *) *e);
Glenn L McGrathb4a1baa2003-01-13 22:09:50 +00001029 }
Rob Landleycba1b962006-07-09 17:28:17 +00001030
1031 if (argc > 1) setenv("RUNLEVEL", argv[1], 1);
1032
Erik Andersene49d5ec2000-02-08 19:58:47 +00001033 /* Hello world */
Manuel Novoa III cad53642003-03-19 09:13:01 +00001034 message(MAYBE_CONSOLE | LOG, "init started: %s", bb_msg_full_version);
Eric Andersencc8ed391999-10-05 16:24:54 +00001035
Erik Andersene49d5ec2000-02-08 19:58:47 +00001036 /* Make sure there is enough memory to do something useful. */
Rob Landleyc3386a42005-08-30 18:50:37 +00001037 if (ENABLE_SWAPONOFF) {
1038 struct sysinfo info;
1039
1040 if (!sysinfo(&info) &&
1041 (info.mem_unit ? : 1) * (long long)info.totalram < MEGABYTE)
1042 {
1043 message(CONSOLE,"Low memory: forcing swapon.");
1044 /* swapon -a requires /proc typically */
1045 new_init_action(SYSINIT, "/bin/mount -t proc proc /proc", "");
1046 /* Try to turn on swap */
1047 new_init_action(SYSINIT, "/sbin/swapon -a", "");
1048 run_actions(SYSINIT); /* wait and removing */
1049 }
1050 }
Erik Andersen9e737252000-01-06 01:16:13 +00001051
Erik Andersene49d5ec2000-02-08 19:58:47 +00001052 /* Check if we are supposed to be in single user mode */
1053 if (argc > 1 && (!strcmp(argv[1], "single") ||
1054 !strcmp(argv[1], "-s") || !strcmp(argv[1], "1"))) {
Glenn L McGrathb4a1baa2003-01-13 22:09:50 +00001055 /* Start a shell on console */
Glenn L McGrathdc4e75e2003-09-02 02:36:18 +00001056 new_init_action(RESPAWN, bb_default_login_shell, "");
Erik Andersene49d5ec2000-02-08 19:58:47 +00001057 } else {
1058 /* Not in single user mode -- see what inittab says */
Eric Andersen8a8fbb81999-10-26 00:18:56 +00001059
Eric Andersenbdfd0d72001-10-24 05:00:29 +00001060 /* NOTE that if CONFIG_FEATURE_USE_INITTAB is NOT defined,
Erik Andersene49d5ec2000-02-08 19:58:47 +00001061 * then parse_inittab() simply adds in some default
Eric Andersenc7bda1c2004-03-15 08:29:22 +00001062 * actions(i.e., runs INIT_SCRIPT and then starts a pair
Erik Andersene49d5ec2000-02-08 19:58:47 +00001063 * of "askfirst" shells */
1064 parse_inittab();
Eric Andersend00c2621999-12-07 08:37:31 +00001065 }
Erik Andersen983b51b2000-04-04 18:14:25 +00001066
Rob Landleyb3ede5a2006-03-27 23:09:12 +00001067#ifdef CONFIG_SELINUX
1068 if (getenv("SELINUX_INIT") == NULL) {
1069 int enforce = 0;
1070
1071 putenv("SELINUX_INIT=YES");
1072 if (selinux_init_load_policy(&enforce) == 0) {
1073 execv(argv[0], argv);
1074 } else if (enforce > 0) {
1075 /* SELinux in enforcing mode but load_policy failed */
1076 /* At this point, we probably can't open /dev/console, so log() won't work */
1077 message(CONSOLE,"Unable to load SELinux Policy. Machine is in enforcing mode. Halting now.");
1078 exit(1);
1079 }
1080 }
1081#endif /* CONFIG_SELINUX */
1082
Eric Andersen7ef1a5b2001-03-20 17:39:08 +00001083 /* Make the command line just say "init" -- thats all, nothing else */
1084 fixup_argv(argc, argv, "init");
Eric Andersen219d6f51999-11-02 19:43:01 +00001085
Erik Andersene49d5ec2000-02-08 19:58:47 +00001086 /* Now run everything that needs to be run */
1087
1088 /* First run the sysinit command */
Eric Andersen1644db92001-09-05 20:18:15 +00001089 run_actions(SYSINIT);
Eric Andersen0298be82002-03-05 15:12:19 +00001090
Erik Andersene49d5ec2000-02-08 19:58:47 +00001091 /* Next run anything that wants to block */
Eric Andersen1644db92001-09-05 20:18:15 +00001092 run_actions(WAIT);
Eric Andersen0298be82002-03-05 15:12:19 +00001093
Erik Andersene49d5ec2000-02-08 19:58:47 +00001094 /* Next run anything to be run only once */
Eric Andersen0298be82002-03-05 15:12:19 +00001095 run_actions(ONCE);
1096
Eric Andersen82baf632004-10-08 08:21:54 +00001097#ifdef CONFIG_FEATURE_USE_INITTAB
Eric Andersen6fd0e312003-07-22 09:48:56 +00001098 /* Redefine SIGHUP to reread /etc/inittab */
1099 signal(SIGHUP, reload_signal);
Eric Andersen82baf632004-10-08 08:21:54 +00001100#else
1101 signal(SIGHUP, SIG_IGN);
1102#endif /* CONFIG_FEATURE_USE_INITTAB */
1103
Eric Andersen6fd0e312003-07-22 09:48:56 +00001104
Erik Andersene49d5ec2000-02-08 19:58:47 +00001105 /* Now run the looping stuff for the rest of forever */
1106 while (1) {
Eric Andersen0298be82002-03-05 15:12:19 +00001107 /* run the respawn stuff */
1108 run_actions(RESPAWN);
1109
1110 /* run the askfirst stuff */
1111 run_actions(ASKFIRST);
1112
1113 /* Don't consume all CPU time -- sleep a bit */
1114 sleep(1);
1115
Erik Andersene49d5ec2000-02-08 19:58:47 +00001116 /* Wait for a child process to exit */
Bernhard Reutner-Fischerc58dbf22006-05-30 12:16:54 +00001117 wpid = wait(NULL);
Eric Andersen87812dc2004-04-12 19:21:54 +00001118 while (wpid > 0) {
Erik Andersene49d5ec2000-02-08 19:58:47 +00001119 /* Find out who died and clean up their corpse */
Eric Andersen0298be82002-03-05 15:12:19 +00001120 for (a = init_action_list; a; a = a->next) {
Erik Andersene49d5ec2000-02-08 19:58:47 +00001121 if (a->pid == wpid) {
Eric Andersen0298be82002-03-05 15:12:19 +00001122 /* Set the pid to 0 so that the process gets
1123 * restarted by run_actions() */
Erik Andersene49d5ec2000-02-08 19:58:47 +00001124 a->pid = 0;
Eric Andersen0298be82002-03-05 15:12:19 +00001125 message(LOG, "Process '%s' (pid %d) exited. "
Glenn L McGrathb4a1baa2003-01-13 22:09:50 +00001126 "Scheduling it for restart.",
1127 a->command, wpid);
Erik Andersene49d5ec2000-02-08 19:58:47 +00001128 }
1129 }
Eric Andersencf1fee02002-12-17 09:48:16 +00001130 /* see if anyone else is waiting to be reaped */
Bernhard Reutner-Fischerc58dbf22006-05-30 12:16:54 +00001131 wpid = waitpid (-1, NULL, WNOHANG);
Erik Andersene49d5ec2000-02-08 19:58:47 +00001132 }
Erik Andersene49d5ec2000-02-08 19:58:47 +00001133 }
Eric Andersencc8ed391999-10-05 16:24:54 +00001134}