blob: 213a5c149ccc36f83c0e236f6cbb12cf68979e1a [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
Rob Landley88621d72006-08-29 19:41:06 +0000176#define messageD(...) do {;} while(0);
Eric Andersenfedce062001-11-17 07:27:14 +0000177#endif
Glenn L McGrathb4a1baa2003-01-13 22:09:50 +0000178static void message(int device, const char *fmt, ...)
Glenn L McGrathbaf55a82002-08-22 18:22:10 +0000179 __attribute__ ((format(printf, 2, 3)));
Glenn L McGrathb4a1baa2003-01-13 22:09:50 +0000180static void message(int device, const char *fmt, ...)
Eric Andersen0460ff21999-10-25 23:32:44 +0000181{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000182 va_list arguments;
Glenn L McGrathb4a1baa2003-01-13 22:09:50 +0000183 int l;
Bernhard Reutner-Fischer3ab30802006-05-30 12:10:29 +0000184 RESERVE_CONFIG_BUFFER(msg, 1024);
Eric Andersen22237012003-01-23 07:08:26 +0000185#ifndef CONFIG_SYSLOGD
186 static int log_fd = -1;
187#endif
Eric Andersen4c781471999-11-26 08:12:56 +0000188
Glenn L McGrathb4a1baa2003-01-13 22:09:50 +0000189 msg[0] = '\r';
Erik Andersene49d5ec2000-02-08 19:58:47 +0000190 va_start(arguments, fmt);
Bernhard Reutner-Fischer3ab30802006-05-30 12:10:29 +0000191 l = vsnprintf(msg + 1, 1024 - 2, fmt, arguments) + 1;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000192 va_end(arguments);
Glenn L McGrathb4a1baa2003-01-13 22:09:50 +0000193
194#ifdef CONFIG_SYSLOGD
195 /* Log the message to syslogd */
196 if (device & LOG) {
197 /* don`t out "\r\n" */
Denis Vlasenko8f8f2682006-10-03 21:00:43 +0000198 openlog(applet_name, 0, LOG_DAEMON);
"Vladimir N. Oleynik"8e1bd4a2005-09-29 12:55:21 +0000199 syslog(LOG_INFO, "%s", msg + 1);
Eric Andersen36adca82004-06-22 10:07:17 +0000200 closelog();
Eric Andersen3ae0c781999-11-04 01:13:21 +0000201 }
Glenn L McGrathb4a1baa2003-01-13 22:09:50 +0000202
203 msg[l++] = '\n';
204 msg[l] = 0;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000205#else
Erik Andersene49d5ec2000-02-08 19:58:47 +0000206
Glenn L McGrathb4a1baa2003-01-13 22:09:50 +0000207 msg[l++] = '\n';
208 msg[l] = 0;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000209 /* Take full control of the log tty, and never close it.
210 * It's mine, all mine! Muhahahaha! */
211 if (log_fd < 0) {
Mike Frysingere548bdf2005-07-06 04:46:14 +0000212 if ((log_fd = device_open(log_console, O_RDWR | O_NONBLOCK | O_NOCTTY)) < 0) {
Erik Andersene132f4b2000-02-09 04:16:43 +0000213 log_fd = -2;
Denis Vlasenkoe1a0d482006-10-20 13:28:22 +0000214 bb_error_msg("bummer, can't write to log on %s!", log_console);
Erik Andersene132f4b2000-02-09 04:16:43 +0000215 device = CONSOLE;
Eric Andersen6a979902002-09-30 20:08:53 +0000216 } else {
217 fcntl(log_fd, F_SETFD, FD_CLOEXEC);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000218 }
219 }
220 if ((device & LOG) && (log_fd >= 0)) {
Rob Landley53437472006-07-16 08:14:35 +0000221 full_write(log_fd, msg, l);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000222 }
Eric Andersen4c781471999-11-26 08:12:56 +0000223#endif
224
Erik Andersene49d5ec2000-02-08 19:58:47 +0000225 if (device & CONSOLE) {
Bernhard Reutner-Fischercf1f2ac2006-06-02 10:43:17 +0000226 int fd = device_open(CONSOLE_DEV,
Mike Frysingere548bdf2005-07-06 04:46:14 +0000227 O_WRONLY | O_NOCTTY | O_NONBLOCK);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000228 /* Always send console messages to /dev/console so people will see them. */
Glenn L McGrathb4a1baa2003-01-13 22:09:50 +0000229 if (fd >= 0) {
Rob Landley53437472006-07-16 08:14:35 +0000230 full_write(fd, msg, l);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000231 close(fd);
Bernhard Reutner-Fischer35e1a072006-05-29 13:08:35 +0000232#if ENABLE_DEBUG_INIT
Glenn L McGrathb4a1baa2003-01-13 22:09:50 +0000233 /* all descriptors may be closed */
Erik Andersene49d5ec2000-02-08 19:58:47 +0000234 } else {
Denis Vlasenkoe1a0d482006-10-20 13:28:22 +0000235 bb_error_msg("bummer, can't print: ");
Erik Andersene49d5ec2000-02-08 19:58:47 +0000236 va_start(arguments, fmt);
237 vfprintf(stderr, fmt, arguments);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000238 va_end(arguments);
Glenn L McGrathb4a1baa2003-01-13 22:09:50 +0000239#endif
Erik Andersene49d5ec2000-02-08 19:58:47 +0000240 }
Eric Andersen3ae0c781999-11-04 01:13:21 +0000241 }
Bernhard Reutner-Fischer3ab30802006-05-30 12:10:29 +0000242 RELEASE_CONFIG_BUFFER(msg);
Eric Andersencc8ed391999-10-05 16:24:54 +0000243}
244
Eric Andersen0460ff21999-10-25 23:32:44 +0000245/* Set terminal settings to reasonable defaults */
Bernhard Reutner-Fischer49e60b92006-05-29 12:57:52 +0000246static void set_term(void)
Eric Andersencc8ed391999-10-05 16:24:54 +0000247{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000248 struct termios tty;
Eric Andersencc8ed391999-10-05 16:24:54 +0000249
Bernhard Reutner-Fischer49e60b92006-05-29 12:57:52 +0000250 tcgetattr(STDIN_FILENO, &tty);
Eric Andersencc8ed391999-10-05 16:24:54 +0000251
Erik Andersene49d5ec2000-02-08 19:58:47 +0000252 /* set control chars */
Glenn L McGrathbaf55a82002-08-22 18:22:10 +0000253 tty.c_cc[VINTR] = 3; /* C-c */
254 tty.c_cc[VQUIT] = 28; /* C-\ */
255 tty.c_cc[VERASE] = 127; /* C-? */
256 tty.c_cc[VKILL] = 21; /* C-u */
257 tty.c_cc[VEOF] = 4; /* C-d */
Eric Andersen3849f9b2000-07-10 19:56:47 +0000258 tty.c_cc[VSTART] = 17; /* C-q */
Glenn L McGrathbaf55a82002-08-22 18:22:10 +0000259 tty.c_cc[VSTOP] = 19; /* C-s */
260 tty.c_cc[VSUSP] = 26; /* C-z */
Eric Andersen02bc25b2000-07-06 21:29:32 +0000261
Erik Andersene49d5ec2000-02-08 19:58:47 +0000262 /* use line dicipline 0 */
263 tty.c_line = 0;
Eric Andersen7f1acfd1999-10-29 23:09:13 +0000264
Erik Andersene49d5ec2000-02-08 19:58:47 +0000265 /* Make it be sane */
Glenn L McGrathbaf55a82002-08-22 18:22:10 +0000266 tty.c_cflag &= CBAUD | CBAUDEX | CSIZE | CSTOPB | PARENB | PARODD;
267 tty.c_cflag |= CREAD | HUPCL | CLOCAL;
Eric Andersend8862922001-04-23 15:14:11 +0000268
Eric Andersen08b10341999-11-19 02:38:58 +0000269
Erik Andersene49d5ec2000-02-08 19:58:47 +0000270 /* input modes */
271 tty.c_iflag = ICRNL | IXON | IXOFF;
Eric Andersen08b10341999-11-19 02:38:58 +0000272
Erik Andersene49d5ec2000-02-08 19:58:47 +0000273 /* output modes */
274 tty.c_oflag = OPOST | ONLCR;
Eric Andersen7f1acfd1999-10-29 23:09:13 +0000275
Erik Andersene49d5ec2000-02-08 19:58:47 +0000276 /* local modes */
277 tty.c_lflag =
278 ISIG | ICANON | ECHO | ECHOE | ECHOK | ECHOCTL | ECHOKE | IEXTEN;
Eric Andersencc8ed391999-10-05 16:24:54 +0000279
Bernhard Reutner-Fischer49e60b92006-05-29 12:57:52 +0000280 tcsetattr(STDIN_FILENO, TCSANOW, &tty);
Eric Andersencc8ed391999-10-05 16:24:54 +0000281}
282
Eric Andersen74400cc2001-10-18 04:11:39 +0000283static void console_init(void)
Eric Andersen0460ff21999-10-25 23:32:44 +0000284{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000285 int fd;
Glenn L McGrathb4a1baa2003-01-13 22:09:50 +0000286 int tried = 0;
Erik Andersen029011b2000-03-04 21:19:32 +0000287 struct vt_stat vt;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000288 struct serial_struct sr;
289 char *s;
Eric Andersen0460ff21999-10-25 23:32:44 +0000290
Glenn L McGrathb4a1baa2003-01-13 22:09:50 +0000291 if ((s = getenv("CONSOLE")) != NULL || (s = getenv("console")) != NULL) {
Matt Kraai18447702001-05-18 21:24:58 +0000292 safe_strncpy(console, s, sizeof(console));
Glenn L McGrathb4a1baa2003-01-13 22:09:50 +0000293 } else {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000294 /* 2.2 kernels: identify the real console backend and try to use it */
295 if (ioctl(0, TIOCGSERIAL, &sr) == 0) {
296 /* this is a serial console */
Matt Kraai439e3df2001-07-23 14:52:08 +0000297 snprintf(console, sizeof(console) - 1, SC_FORMAT, sr.line);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000298 } else if (ioctl(0, VT_GETSTATE, &vt) == 0) {
299 /* this is linux virtual tty */
Matt Kraai439e3df2001-07-23 14:52:08 +0000300 snprintf(console, sizeof(console) - 1, VC_FORMAT, vt.v_active);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000301 } else {
Bernhard Reutner-Fischercf1f2ac2006-06-02 10:43:17 +0000302 safe_strncpy(console, CONSOLE_DEV, sizeof(console));
Glenn L McGrathb4a1baa2003-01-13 22:09:50 +0000303 tried++;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000304 }
Eric Andersen07e52971999-11-07 07:38:08 +0000305 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000306
Glenn L McGrathb4a1baa2003-01-13 22:09:50 +0000307 while ((fd = open(console, O_RDONLY | O_NONBLOCK)) < 0 && tried < 2) {
308 /* Can't open selected console -- try
309 logical system console and VT_MASTER */
Bernhard Reutner-Fischercf1f2ac2006-06-02 10:43:17 +0000310 safe_strncpy(console, (tried == 0 ? CONSOLE_DEV : CURRENT_VC),
Glenn L McGrathb4a1baa2003-01-13 22:09:50 +0000311 sizeof(console));
312 tried++;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000313 }
314 if (fd < 0) {
315 /* Perhaps we should panic here? */
Glenn L McGrathb4a1baa2003-01-13 22:09:50 +0000316#ifndef CONFIG_SYSLOGD
Mike Frysinger72a4c332005-07-05 02:19:20 +0000317 log_console =
Glenn L McGrathb4a1baa2003-01-13 22:09:50 +0000318#endif
"Vladimir N. Oleynik"6c35c7c2005-10-12 15:34:25 +0000319 safe_strncpy(console, bb_dev_null, sizeof(console));
Eric Andersen07e52971999-11-07 07:38:08 +0000320 } else {
Glenn L McGrathb4a1baa2003-01-13 22:09:50 +0000321 s = getenv("TERM");
Eric Andersen3bc2b202002-07-29 06:39:58 +0000322 /* check for serial console */
Glenn L McGrathb4a1baa2003-01-13 22:09:50 +0000323 if (ioctl(fd, TIOCGSERIAL, &sr) == 0) {
Erik Andersenfa4718e2000-02-21 19:25:12 +0000324 /* Force the TERM setting to vt102 for serial console --
Eric Andersen3bc2b202002-07-29 06:39:58 +0000325 * if TERM is set to linux (the default) */
Glenn L McGrathb4a1baa2003-01-13 22:09:50 +0000326 if (s == NULL || strcmp(s, "linux") == 0)
327 putenv("TERM=vt102");
328#ifndef CONFIG_SYSLOGD
Mike Frysinger72a4c332005-07-05 02:19:20 +0000329 log_console = console;
Glenn L McGrathb4a1baa2003-01-13 22:09:50 +0000330#endif
331 } else {
332 if (s == NULL)
333 putenv("TERM=linux");
Erik Andersene49d5ec2000-02-08 19:58:47 +0000334 }
335 close(fd);
Eric Andersen07e52971999-11-07 07:38:08 +0000336 }
Glenn L McGrathb4a1baa2003-01-13 22:09:50 +0000337 messageD(LOG, "console=%s", console);
Eric Andersen0460ff21999-10-25 23:32:44 +0000338}
Glenn L McGrathbaf55a82002-08-22 18:22:10 +0000339
Eric Andersen7ef1a5b2001-03-20 17:39:08 +0000340static void fixup_argv(int argc, char **argv, char *new_argv0)
341{
342 int len;
Glenn L McGrathbaf55a82002-08-22 18:22:10 +0000343
Eric Andersen7ef1a5b2001-03-20 17:39:08 +0000344 /* Fix up argv[0] to be certain we claim to be init */
345 len = strlen(argv[0]);
346 memset(argv[0], 0, len);
Eric Andersen72f9a422001-10-28 05:12:20 +0000347 safe_strncpy(argv[0], new_argv0, len + 1);
Eric Andersen7ef1a5b2001-03-20 17:39:08 +0000348
349 /* Wipe argv[1]-argv[N] so they don't clutter the ps listing */
350 len = 1;
351 while (argc > len) {
352 memset(argv[len], 0, strlen(argv[len]));
353 len++;
354 }
355}
356
Bernhard Reutner-Fischer0da069d2006-05-29 12:54:16 +0000357/* Open the new terminal device */
Bernhard Reutner-Fischercf1f2ac2006-06-02 10:43:17 +0000358static void open_new_terminal(const char * const device, const int fail) {
Bernhard Reutner-Fischer0da069d2006-05-29 12:54:16 +0000359 struct stat sb;
360
361 if ((device_open(device, O_RDWR)) < 0) {
362 if (stat(device, &sb) != 0) {
363 message(LOG | CONSOLE, "device '%s' does not exist.", device);
364 } else {
365 message(LOG | CONSOLE, "Bummer, can't open %s", device);
366 }
367 if (fail)
368 _exit(1);
369 /* else */
Bernhard Reutner-Fischercf1f2ac2006-06-02 10:43:17 +0000370#if !ENABLE_DEBUG_INIT
Paul Foxd112f8f2006-06-01 13:17:49 +0000371 shutdown_signal(SIGUSR1);
Bernhard Reutner-Fischercf1f2ac2006-06-02 10:43:17 +0000372#else
373 _exit(2);
374#endif
Bernhard Reutner-Fischer0da069d2006-05-29 12:54:16 +0000375 }
376}
377
Glenn L McGrathb4a1baa2003-01-13 22:09:50 +0000378static pid_t run(const struct init_action *a)
Eric Andersen0298be82002-03-05 15:12:19 +0000379{
Bernhard Reutner-Fischer3ab30802006-05-30 12:10:29 +0000380 int i;
Mike Frysinger10427ab2005-07-06 05:00:48 +0000381 pid_t pid;
Eric Andersen0826b6b2002-07-03 23:50:16 +0000382 char *s, *tmpCmd, *cmd[INIT_BUFFS_SIZE], *cmdpath;
Glenn L McGrathbaf55a82002-08-22 18:22:10 +0000383 char buf[INIT_BUFFS_SIZE + 6]; /* INIT_BUFFS_SIZE+strlen("exec ")+1 */
Eric Andersen0298be82002-03-05 15:12:19 +0000384 sigset_t nmask, omask;
Eric Andersen0298be82002-03-05 15:12:19 +0000385 static const char press_enter[] =
386#ifdef CUSTOMIZED_BANNER
387#include CUSTOMIZED_BANNER
388#endif
389 "\nPlease press Enter to activate this console. ";
Erik Andersenac6e71f2000-01-08 22:04:33 +0000390
Eric Andersen0298be82002-03-05 15:12:19 +0000391 /* Block sigchild while forking. */
392 sigemptyset(&nmask);
393 sigaddset(&nmask, SIGCHLD);
394 sigprocmask(SIG_BLOCK, &nmask, &omask);
395
Glenn L McGrathbaf55a82002-08-22 18:22:10 +0000396 if ((pid = fork()) == 0) {
Mike Frysinger10427ab2005-07-06 05:00:48 +0000397
Erik Andersene49d5ec2000-02-08 19:58:47 +0000398 /* Clean up */
399 close(0);
400 close(1);
401 close(2);
Eric Andersen0298be82002-03-05 15:12:19 +0000402 sigprocmask(SIG_SETMASK, &omask, NULL);
403
Eric Andersen0298be82002-03-05 15:12:19 +0000404 /* Reset signal handlers that were set by the parent process */
Erik Andersene49d5ec2000-02-08 19:58:47 +0000405 signal(SIGUSR1, SIG_DFL);
406 signal(SIGUSR2, SIG_DFL);
Glenn L McGrathbaf55a82002-08-22 18:22:10 +0000407 signal(SIGINT, SIG_DFL);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000408 signal(SIGTERM, SIG_DFL);
Glenn L McGrathbaf55a82002-08-22 18:22:10 +0000409 signal(SIGHUP, SIG_DFL);
Mike Frysingera77b4f32005-04-16 08:21:34 +0000410 signal(SIGQUIT, SIG_DFL);
Eric Andersened8a9be2001-11-30 19:10:58 +0000411 signal(SIGCONT, SIG_DFL);
412 signal(SIGSTOP, SIG_DFL);
413 signal(SIGTSTP, SIG_DFL);
Eric Andersen2f6c04f1999-11-01 23:59:44 +0000414
Eric Andersen599e3ce2002-07-03 11:08:10 +0000415 /* Create a new session and make ourself the process
Eric Andersene8a90fb2002-10-12 04:05:48 +0000416 * group leader */
417 setsid();
Eric Andersen599e3ce2002-07-03 11:08:10 +0000418
Eric Andersen0298be82002-03-05 15:12:19 +0000419 /* Open the new terminal device */
Bernhard Reutner-Fischer0da069d2006-05-29 12:54:16 +0000420 open_new_terminal(a->terminal, 1);
Eric Andersen599e3ce2002-07-03 11:08:10 +0000421
Eric Andersen0298be82002-03-05 15:12:19 +0000422 /* Make sure the terminal will act fairly normal for us */
Bernhard Reutner-Fischer49e60b92006-05-29 12:57:52 +0000423 set_term();
Eric Andersen0826b6b2002-07-03 23:50:16 +0000424 /* Setup stdout, stderr for the new process so
Eric Andersen599e3ce2002-07-03 11:08:10 +0000425 * they point to the supplied terminal */
Eric Andersen0826b6b2002-07-03 23:50:16 +0000426 dup(0);
427 dup(0);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000428
Eric Andersen599e3ce2002-07-03 11:08:10 +0000429 /* If the init Action requires us to wait, then force the
Eric Andersen0298be82002-03-05 15:12:19 +0000430 * supplied terminal to be the controlling tty. */
Glenn L McGrathbaf55a82002-08-22 18:22:10 +0000431 if (a->action & (SYSINIT | WAIT | CTRLALTDEL | SHUTDOWN | RESTART)) {
Eric Andersen0298be82002-03-05 15:12:19 +0000432
433 /* Now fork off another process to just hang around */
434 if ((pid = fork()) < 0) {
Glenn L McGrathb4a1baa2003-01-13 22:09:50 +0000435 message(LOG | CONSOLE, "Can't fork!");
Eric Andersen0298be82002-03-05 15:12:19 +0000436 _exit(1);
437 }
438
439 if (pid > 0) {
440
441 /* We are the parent -- wait till the child is done */
442 signal(SIGINT, SIG_IGN);
443 signal(SIGTSTP, SIG_IGN);
444 signal(SIGQUIT, SIG_IGN);
445 signal(SIGCHLD, SIG_DFL);
446
Bernhard Reutner-Fischer3ab30802006-05-30 12:10:29 +0000447 waitfor(NULL, pid);
Eric Andersen0298be82002-03-05 15:12:19 +0000448 /* See if stealing the controlling tty back is necessary */
Bernhard Reutner-Fischer3ab30802006-05-30 12:10:29 +0000449 if (tcgetpgrp(0) != getpid())
Eric Andersen0298be82002-03-05 15:12:19 +0000450 _exit(0);
451
452 /* Use a temporary process to steal the controlling tty. */
453 if ((pid = fork()) < 0) {
Glenn L McGrathb4a1baa2003-01-13 22:09:50 +0000454 message(LOG | CONSOLE, "Can't fork!");
Eric Andersen0298be82002-03-05 15:12:19 +0000455 _exit(1);
Glenn L McGrathbaf55a82002-08-22 18:22:10 +0000456 }
Eric Andersen0298be82002-03-05 15:12:19 +0000457 if (pid == 0) {
458 setsid();
Eric Andersen0826b6b2002-07-03 23:50:16 +0000459 ioctl(0, TIOCSCTTY, 1);
Eric Andersen0298be82002-03-05 15:12:19 +0000460 _exit(0);
Glenn L McGrathbaf55a82002-08-22 18:22:10 +0000461 }
Bernhard Reutner-Fischer3ab30802006-05-30 12:10:29 +0000462 waitfor(NULL, pid);
Eric Andersen0298be82002-03-05 15:12:19 +0000463 _exit(0);
464 }
465
466 /* Now fall though to actually execute things */
Eric Andersen0298be82002-03-05 15:12:19 +0000467 }
468
Erik Andersene132f4b2000-02-09 04:16:43 +0000469 /* See if any special /bin/sh requiring characters are present */
Eric Andersen0298be82002-03-05 15:12:19 +0000470 if (strpbrk(a->command, "~`!$^&*()=|\\{}[];\"'<>?") != NULL) {
Glenn L McGrathdc4e75e2003-09-02 02:36:18 +0000471 cmd[0] = (char *)DEFAULT_SHELL;
Erik Andersene132f4b2000-02-09 04:16:43 +0000472 cmd[1] = "-c";
Glenn L McGrathb4a1baa2003-01-13 22:09:50 +0000473 cmd[2] = strcat(strcpy(buf, "exec "), a->command);
Erik Andersene132f4b2000-02-09 04:16:43 +0000474 cmd[3] = NULL;
475 } else {
476 /* Convert command (char*) into cmd (char**, one word per string) */
Eric Andersen0826b6b2002-07-03 23:50:16 +0000477 strcpy(buf, a->command);
Eric Andersen72f9a422001-10-28 05:12:20 +0000478 s = buf;
Glenn L McGrathbaf55a82002-08-22 18:22:10 +0000479 for (tmpCmd = buf, i = 0; (tmpCmd = strsep(&s, " \t")) != NULL;) {
Erik Andersene132f4b2000-02-09 04:16:43 +0000480 if (*tmpCmd != '\0') {
481 cmd[i] = tmpCmd;
Erik Andersene132f4b2000-02-09 04:16:43 +0000482 i++;
483 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000484 }
Erik Andersene132f4b2000-02-09 04:16:43 +0000485 cmd[i] = NULL;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000486 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000487
Eric Andersen7ef1a5b2001-03-20 17:39:08 +0000488 cmdpath = cmd[0];
Eric Andersen7e3bf6e2000-09-14 22:01:31 +0000489
Eric Andersen7ef1a5b2001-03-20 17:39:08 +0000490 /*
491 Interactive shells want to see a dash in argv[0]. This
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000492 typically is handled by login, argv will be setup this
493 way if a dash appears at the front of the command path
Eric Andersen7e3bf6e2000-09-14 22:01:31 +0000494 (like "-/bin/sh").
Eric Andersen7ef1a5b2001-03-20 17:39:08 +0000495 */
Eric Andersen7e3bf6e2000-09-14 22:01:31 +0000496
Eric Andersen7ef1a5b2001-03-20 17:39:08 +0000497 if (*cmdpath == '-') {
Eric Andersen7e3bf6e2000-09-14 22:01:31 +0000498
Eric Andersen7ef1a5b2001-03-20 17:39:08 +0000499 /* skip over the dash */
500 ++cmdpath;
Eric Andersen7e3bf6e2000-09-14 22:01:31 +0000501
Eric Andersen7ef1a5b2001-03-20 17:39:08 +0000502 /* find the last component in the command pathname */
Manuel Novoa III cad53642003-03-19 09:13:01 +0000503 s = bb_get_last_path_component(cmdpath);
Eric Andersen7e3bf6e2000-09-14 22:01:31 +0000504
Eric Andersen7ef1a5b2001-03-20 17:39:08 +0000505 /* make a new argv[0] */
Glenn L McGrathbaf55a82002-08-22 18:22:10 +0000506 if ((cmd[0] = malloc(strlen(s) + 2)) == NULL) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000507 message(LOG | CONSOLE, bb_msg_memory_exhausted);
Eric Andersen7ef1a5b2001-03-20 17:39:08 +0000508 cmd[0] = cmdpath;
509 } else {
510 cmd[0][0] = '-';
Glenn L McGrathbaf55a82002-08-22 18:22:10 +0000511 strcpy(cmd[0] + 1, s);
Eric Andersen7ef1a5b2001-03-20 17:39:08 +0000512 }
Paul Fox41a72ec2005-08-01 16:43:13 +0000513#ifdef CONFIG_FEATURE_INIT_SCTTY
514 /* Establish this process as session leader and
515 * (attempt) to make the tty (if any) a controlling tty.
516 */
517 (void) setsid();
518 (void) ioctl(0, TIOCSCTTY, 0/*don't steal it*/);
519#endif
Eric Andersen7ef1a5b2001-03-20 17:39:08 +0000520 }
521
Eric Andersen1f50e842004-08-16 09:29:42 +0000522#if !defined(__UCLIBC__) || defined(__ARCH_HAS_MMU__)
Eric Andersen0298be82002-03-05 15:12:19 +0000523 if (a->action & ASKFIRST) {
Glenn L McGrathb4a1baa2003-01-13 22:09:50 +0000524 char c;
Eric Andersen7ef1a5b2001-03-20 17:39:08 +0000525 /*
526 * Save memory by not exec-ing anything large (like a shell)
527 * before the user wants it. This is critical if swap is not
528 * enabled and the system has low memory. Generally this will
529 * be run on the second virtual console, and the first will
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000530 * be allowed to start a shell or whatever an init script
Eric Andersen7ef1a5b2001-03-20 17:39:08 +0000531 * specifies.
532 */
Glenn L McGrathb4a1baa2003-01-13 22:09:50 +0000533 messageD(LOG, "Waiting for enter to start '%s'"
534 "(pid %d, terminal %s)\n",
Glenn L McGrathbaf55a82002-08-22 18:22:10 +0000535 cmdpath, getpid(), a->terminal);
Rob Landley53437472006-07-16 08:14:35 +0000536 full_write(1, press_enter, sizeof(press_enter) - 1);
Glenn L McGrathb4a1baa2003-01-13 22:09:50 +0000537 while(read(0, &c, 1) == 1 && c != '\n')
538 ;
Eric Andersen7ef1a5b2001-03-20 17:39:08 +0000539 }
Eric Andersen1f50e842004-08-16 09:29:42 +0000540#endif
Eric Andersen7ef1a5b2001-03-20 17:39:08 +0000541
Eric Andersen7ef1a5b2001-03-20 17:39:08 +0000542 /* Log the process name and args */
Glenn L McGrathb4a1baa2003-01-13 22:09:50 +0000543 message(LOG, "Starting pid %d, console %s: '%s'",
Glenn L McGrathbaf55a82002-08-22 18:22:10 +0000544 getpid(), a->terminal, cmdpath);
Eric Andersen7e3bf6e2000-09-14 22:01:31 +0000545
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000546#if defined CONFIG_FEATURE_INIT_COREDUMPS
Bernhard Reutner-Fischer0da069d2006-05-29 12:54:16 +0000547 {
548 struct stat sb;
549 if (stat(CORE_ENABLE_FLAG_FILE, &sb) == 0) {
550 struct rlimit limit;
Glenn L McGrathbaf55a82002-08-22 18:22:10 +0000551
Bernhard Reutner-Fischer0da069d2006-05-29 12:54:16 +0000552 limit.rlim_cur = RLIM_INFINITY;
553 limit.rlim_max = RLIM_INFINITY;
554 setrlimit(RLIMIT_CORE, &limit);
555 }
Erik Andersen983b51b2000-04-04 18:14:25 +0000556 }
Erik Andersen183da4a2000-04-04 18:36:37 +0000557#endif
Erik Andersen983b51b2000-04-04 18:14:25 +0000558
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000559 /* Now run it. The new program will take over this PID,
Erik Andersene49d5ec2000-02-08 19:58:47 +0000560 * so nothing further in init.c should be run. */
Glenn L McGrathb4a1baa2003-01-13 22:09:50 +0000561 execv(cmdpath, cmd);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000562
Eric Andersen7ef1a5b2001-03-20 17:39:08 +0000563 /* We're still here? Some error happened. */
Denis Vlasenkoea620772006-10-14 02:23:43 +0000564 message(LOG | CONSOLE, "Bummer, cannot run '%s': %m", cmdpath);
Eric Andersen0298be82002-03-05 15:12:19 +0000565 _exit(-1);
Erik Andersen05df2392000-01-13 04:43:48 +0000566 }
Eric Andersen0298be82002-03-05 15:12:19 +0000567 sigprocmask(SIG_SETMASK, &omask, NULL);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000568 return pid;
Eric Andersen0460ff21999-10-25 23:32:44 +0000569}
570
Bernhard Reutner-Fischer3ab30802006-05-30 12:10:29 +0000571static int waitfor(const struct init_action *a, pid_t pid)
Erik Andersen0e3782f2000-01-07 02:54:55 +0000572{
Bernhard Reutner-Fischer3ab30802006-05-30 12:10:29 +0000573 int runpid;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000574 int status, wpid;
Erik Andersen0e3782f2000-01-07 02:54:55 +0000575
Bernhard Reutner-Fischer3ab30802006-05-30 12:10:29 +0000576 runpid = (NULL == a)? pid : run(a);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000577 while (1) {
Bernhard Reutner-Fischer3ab30802006-05-30 12:10:29 +0000578 wpid = waitpid(runpid,&status,0);
579 if (wpid == runpid)
Erik Andersene49d5ec2000-02-08 19:58:47 +0000580 break;
Glenn L McGrathe6ba16f2003-09-26 10:45:55 +0000581 if (wpid == -1 && errno == ECHILD) {
582 /* we missed its termination */
583 break;
584 }
585 /* FIXME other errors should maybe trigger an error, but allow
586 * the program to continue */
Erik Andersen0e3782f2000-01-07 02:54:55 +0000587 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000588 return wpid;
Erik Andersen0e3782f2000-01-07 02:54:55 +0000589}
590
Eric Andersen0298be82002-03-05 15:12:19 +0000591/* Run all commands of a particular type */
592static void run_actions(int action)
Eric Andersen219d6f51999-11-02 19:43:01 +0000593{
Eric Andersen0298be82002-03-05 15:12:19 +0000594 struct init_action *a, *tmp;
Eric Andersen219d6f51999-11-02 19:43:01 +0000595
Eric Andersen0298be82002-03-05 15:12:19 +0000596 for (a = init_action_list; a; a = tmp) {
597 tmp = a->next;
Eric Andersenc97ec342001-04-03 18:01:51 +0000598 if (a->action == action) {
Rob Landley2dd42792006-03-22 17:39:13 +0000599 if (access(a->terminal, R_OK | W_OK)) {
600 delete_init_action(a);
601 } else if (a->action & (SYSINIT | WAIT | CTRLALTDEL | SHUTDOWN | RESTART)) {
Bernhard Reutner-Fischer3ab30802006-05-30 12:10:29 +0000602 waitfor(a, 0);
Eric Andersen0298be82002-03-05 15:12:19 +0000603 delete_init_action(a);
604 } else if (a->action & ONCE) {
605 run(a);
606 delete_init_action(a);
Glenn L McGrathbaf55a82002-08-22 18:22:10 +0000607 } else if (a->action & (RESPAWN | ASKFIRST)) {
Eric Andersen0298be82002-03-05 15:12:19 +0000608 /* Only run stuff with pid==0. If they have
609 * a pid, that means it is still running */
610 if (a->pid == 0) {
611 a->pid = run(a);
612 }
613 }
Erik Andersene132f4b2000-02-09 04:16:43 +0000614 }
615 }
616}
617
Bernhard Reutner-Fischer35e1a072006-05-29 13:08:35 +0000618#if !ENABLE_DEBUG_INIT
Eric Andersen2c1de612003-04-24 11:41:28 +0000619static void init_reboot(unsigned long magic)
620{
621 pid_t pid;
622 /* We have to fork here, since the kernel calls do_exit(0) in
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000623 * linux/kernel/sys.c, which can cause the machine to panic when
Eric Andersen2c1de612003-04-24 11:41:28 +0000624 * the init process is killed.... */
625 if ((pid = fork()) == 0) {
Eric Andersen2c1de612003-04-24 11:41:28 +0000626 reboot(magic);
Eric Andersen2c1de612003-04-24 11:41:28 +0000627 _exit(0);
628 }
629 waitpid (pid, NULL, 0);
630}
631
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000632static void shutdown_system(void)
Eric Andersencc8ed391999-10-05 16:24:54 +0000633{
Eric Andersen813d88c2001-10-28 22:49:48 +0000634 sigset_t block_signals;
Erik Andersene132f4b2000-02-09 04:16:43 +0000635
Eric Andersena9cc8962002-09-16 06:49:06 +0000636 /* run everything to be run at "shutdown". This is done _prior_
637 * to killing everything, in case people wish to use scripts to
638 * shut things down gracefully... */
639 run_actions(SHUTDOWN);
640
Eric Andersen72f9a422001-10-28 05:12:20 +0000641 /* first disable all our signals */
642 sigemptyset(&block_signals);
643 sigaddset(&block_signals, SIGHUP);
Mike Frysingera77b4f32005-04-16 08:21:34 +0000644 sigaddset(&block_signals, SIGQUIT);
Eric Andersen72f9a422001-10-28 05:12:20 +0000645 sigaddset(&block_signals, SIGCHLD);
646 sigaddset(&block_signals, SIGUSR1);
647 sigaddset(&block_signals, SIGUSR2);
648 sigaddset(&block_signals, SIGINT);
649 sigaddset(&block_signals, SIGTERM);
Eric Andersened8a9be2001-11-30 19:10:58 +0000650 sigaddset(&block_signals, SIGCONT);
651 sigaddset(&block_signals, SIGSTOP);
652 sigaddset(&block_signals, SIGTSTP);
Eric Andersen72f9a422001-10-28 05:12:20 +0000653 sigprocmask(SIG_BLOCK, &block_signals, NULL);
Erik Andersen7dc16072000-01-04 01:10:25 +0000654
Erik Andersene49d5ec2000-02-08 19:58:47 +0000655 /* Allow Ctrl-Alt-Del to reboot system. */
Erik Andersen4f3f7572000-04-28 00:18:56 +0000656 init_reboot(RB_ENABLE_CAD);
Erik Andersene132f4b2000-02-09 04:16:43 +0000657
Glenn L McGrathb4a1baa2003-01-13 22:09:50 +0000658 message(CONSOLE | LOG, "The system is going down NOW !!");
Eric Andersencf8c9cf1999-11-05 00:31:46 +0000659 sync();
Erik Andersene49d5ec2000-02-08 19:58:47 +0000660
661 /* Send signals to every process _except_ pid 1 */
Bernhard Reutner-Fischer7ae15532006-05-30 18:17:21 +0000662 message(CONSOLE | LOG, init_sending_format, "TERM");
Erik Andersene49d5ec2000-02-08 19:58:47 +0000663 kill(-1, SIGTERM);
Erik Andersene132f4b2000-02-09 04:16:43 +0000664 sleep(1);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000665 sync();
666
Bernhard Reutner-Fischer7ae15532006-05-30 18:17:21 +0000667 message(CONSOLE | LOG, init_sending_format, "KILL");
Erik Andersene49d5ec2000-02-08 19:58:47 +0000668 kill(-1, SIGKILL);
Erik Andersene132f4b2000-02-09 04:16:43 +0000669 sleep(1);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000670
Erik Andersene49d5ec2000-02-08 19:58:47 +0000671 sync();
Eric Andersencc8ed391999-10-05 16:24:54 +0000672}
673
"Vladimir N. Oleynik"1f04c9d2006-02-01 14:47:52 +0000674static void exec_signal(int sig ATTRIBUTE_UNUSED)
Eric Andersen730f8262001-12-17 23:13:08 +0000675{
Eric Andersen0298be82002-03-05 15:12:19 +0000676 struct init_action *a, *tmp;
Eric Andersen5222d312002-07-03 05:15:23 +0000677 sigset_t unblock_signals;
Glenn L McGrathbaf55a82002-08-22 18:22:10 +0000678
Eric Andersen0298be82002-03-05 15:12:19 +0000679 for (a = init_action_list; a; a = tmp) {
680 tmp = a->next;
681 if (a->action & RESTART) {
Eric Andersen730f8262001-12-17 23:13:08 +0000682 shutdown_system();
Eric Andersen5222d312002-07-03 05:15:23 +0000683
684 /* unblock all signals, blocked in shutdown_system() */
685 sigemptyset(&unblock_signals);
686 sigaddset(&unblock_signals, SIGHUP);
Mike Frysingera77b4f32005-04-16 08:21:34 +0000687 sigaddset(&unblock_signals, SIGQUIT);
Eric Andersen5222d312002-07-03 05:15:23 +0000688 sigaddset(&unblock_signals, SIGCHLD);
689 sigaddset(&unblock_signals, SIGUSR1);
690 sigaddset(&unblock_signals, SIGUSR2);
691 sigaddset(&unblock_signals, SIGINT);
692 sigaddset(&unblock_signals, SIGTERM);
693 sigaddset(&unblock_signals, SIGCONT);
694 sigaddset(&unblock_signals, SIGSTOP);
695 sigaddset(&unblock_signals, SIGTSTP);
696 sigprocmask(SIG_UNBLOCK, &unblock_signals, NULL);
697
Eric Andersen9cdef5d2003-07-29 06:33:12 +0000698 /* Close whatever files are open. */
699 close(0);
700 close(1);
701 close(2);
702
Eric Andersen3c8064f2003-07-05 08:29:01 +0000703 /* Open the new terminal device */
Bernhard Reutner-Fischer0da069d2006-05-29 12:54:16 +0000704 open_new_terminal(a->terminal, 0);
Eric Andersen3c8064f2003-07-05 08:29:01 +0000705
706 /* Make sure the terminal will act fairly normal for us */
Bernhard Reutner-Fischer49e60b92006-05-29 12:57:52 +0000707 set_term();
Eric Andersen3c8064f2003-07-05 08:29:01 +0000708 /* Setup stdout, stderr on the supplied terminal */
709 dup(0);
710 dup(0);
711
Glenn L McGrathb4a1baa2003-01-13 22:09:50 +0000712 messageD(CONSOLE | LOG, "Trying to re-exec %s", a->command);
Eric Andersen0298be82002-03-05 15:12:19 +0000713 execl(a->command, a->command, NULL);
Glenn L McGrathbaf55a82002-08-22 18:22:10 +0000714
Glenn L McGrathb4a1baa2003-01-13 22:09:50 +0000715 message(CONSOLE | LOG, "exec of '%s' failed: %m",
Eric Andersen71ae64b2002-10-10 04:20:21 +0000716 a->command);
Eric Andersen730f8262001-12-17 23:13:08 +0000717 sync();
718 sleep(2);
719 init_reboot(RB_HALT_SYSTEM);
720 loop_forever();
721 }
722 }
723}
724
Paul Foxd112f8f2006-06-01 13:17:49 +0000725static void shutdown_signal(int sig)
Eric Andersencc8ed391999-10-05 16:24:54 +0000726{
Paul Foxd112f8f2006-06-01 13:17:49 +0000727 char *m;
728 int rb;
729
Erik Andersene49d5ec2000-02-08 19:58:47 +0000730 shutdown_system();
Paul Foxd112f8f2006-06-01 13:17:49 +0000731
732 if (sig == SIGTERM) {
733 m = "reboot";
734 rb = RB_AUTOBOOT;
735 } else if (sig == SIGUSR2) {
736 m = "poweroff";
737 rb = RB_POWER_OFF;
738 } else {
739 m = "halt";
740 rb = RB_HALT_SYSTEM;
741 }
742 message(CONSOLE | LOG, "Requesting system %s.", m);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000743 sync();
Erik Andersen3fe39dc2000-01-25 18:13:53 +0000744
Erik Andersene49d5ec2000-02-08 19:58:47 +0000745 /* allow time for last message to reach serial console */
Erik Andersene132f4b2000-02-09 04:16:43 +0000746 sleep(2);
Erik Andersen3fe39dc2000-01-25 18:13:53 +0000747
Paul Foxd112f8f2006-06-01 13:17:49 +0000748 init_reboot(rb);
Matt Kraai67a46402001-06-03 05:55:52 +0000749
750 loop_forever();
Eric Andersencc8ed391999-10-05 16:24:54 +0000751}
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000752
"Vladimir N. Oleynik"1f04c9d2006-02-01 14:47:52 +0000753static void ctrlaltdel_signal(int sig ATTRIBUTE_UNUSED)
Eric Andersenc97ec342001-04-03 18:01:51 +0000754{
755 run_actions(CTRLALTDEL);
756}
757
Eric Andersen0298be82002-03-05 15:12:19 +0000758/* The SIGSTOP & SIGTSTP handler */
"Vladimir N. Oleynik"1f04c9d2006-02-01 14:47:52 +0000759static void stop_handler(int sig ATTRIBUTE_UNUSED)
Eric Andersened8a9be2001-11-30 19:10:58 +0000760{
Eric Andersen0298be82002-03-05 15:12:19 +0000761 int saved_errno = errno;
Eric Andersened8a9be2001-11-30 19:10:58 +0000762
763 got_cont = 0;
Glenn L McGrathbaf55a82002-08-22 18:22:10 +0000764 while (!got_cont)
765 pause();
Eric Andersened8a9be2001-11-30 19:10:58 +0000766 got_cont = 0;
767 errno = saved_errno;
768}
769
Glenn L McGrathbaf55a82002-08-22 18:22:10 +0000770/* The SIGCONT handler */
"Vladimir N. Oleynik"1f04c9d2006-02-01 14:47:52 +0000771static void cont_handler(int sig ATTRIBUTE_UNUSED)
Eric Andersened8a9be2001-11-30 19:10:58 +0000772{
773 got_cont = 1;
774}
775
Bernhard Reutner-Fischer35e1a072006-05-29 13:08:35 +0000776#endif /* ! ENABLE_DEBUG_INIT */
Erik Andersende552872000-01-23 01:34:05 +0000777
Glenn L McGrathdc4e75e2003-09-02 02:36:18 +0000778static void new_init_action(int action, const char *command, const char *cons)
Erik Andersen7dc16072000-01-04 01:10:25 +0000779{
Eric Andersen22718092004-10-08 08:17:39 +0000780 struct init_action *new_action, *a, *last;
Erik Andersen9e737252000-01-06 01:16:13 +0000781
Erik Andersene49d5ec2000-02-08 19:58:47 +0000782 if (*cons == '\0')
783 cons = console;
Erik Andersen9e737252000-01-06 01:16:13 +0000784
"Vladimir N. Oleynik"6c35c7c2005-10-12 15:34:25 +0000785 if (strcmp(cons, bb_dev_null) == 0 && (action & ASKFIRST))
Eric Andersen1644db92001-09-05 20:18:15 +0000786 return;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000787
Eric Andersen0298be82002-03-05 15:12:19 +0000788 new_action = calloc((size_t) (1), sizeof(struct init_action));
789 if (!new_action) {
Glenn L McGrathb4a1baa2003-01-13 22:09:50 +0000790 message(LOG | CONSOLE, "Memory allocation failure");
Matt Kraai67a46402001-06-03 05:55:52 +0000791 loop_forever();
Erik Andersene49d5ec2000-02-08 19:58:47 +0000792 }
Eric Andersen0298be82002-03-05 15:12:19 +0000793
794 /* Append to the end of the list */
Eric Andersen22718092004-10-08 08:17:39 +0000795 for (a = last = init_action_list; a; a = a->next) {
Eric Andersen82baf632004-10-08 08:21:54 +0000796 /* don't enter action if it's already in the list,
797 * but do overwrite existing actions */
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000798 if ((strcmp(a->command, command) == 0) &&
Eric Andersen6fd0e312003-07-22 09:48:56 +0000799 (strcmp(a->terminal, cons) ==0)) {
Eric Andersen82baf632004-10-08 08:21:54 +0000800 a->action = action;
Eric Andersen6fd0e312003-07-22 09:48:56 +0000801 free(new_action);
802 return;
803 }
Eric Andersen22718092004-10-08 08:17:39 +0000804 last = a;
Eric Andersen6fd0e312003-07-22 09:48:56 +0000805 }
Eric Andersen22718092004-10-08 08:17:39 +0000806 if (last) {
807 last->next = new_action;
Eric Andersen1644db92001-09-05 20:18:15 +0000808 } else {
Eric Andersen0298be82002-03-05 15:12:19 +0000809 init_action_list = new_action;
Eric Andersen1644db92001-09-05 20:18:15 +0000810 }
Eric Andersen0826b6b2002-07-03 23:50:16 +0000811 strcpy(new_action->command, command);
Eric Andersen0298be82002-03-05 15:12:19 +0000812 new_action->action = action;
Eric Andersen0826b6b2002-07-03 23:50:16 +0000813 strcpy(new_action->terminal, cons);
Glenn L McGrathb4a1baa2003-01-13 22:09:50 +0000814 messageD(LOG|CONSOLE, "command='%s' action='%d' terminal='%s'\n",
815 new_action->command, new_action->action, new_action->terminal);
Erik Andersen7dc16072000-01-04 01:10:25 +0000816}
817
Glenn L McGrathbaf55a82002-08-22 18:22:10 +0000818static void delete_init_action(struct init_action *action)
Erik Andersen7dc16072000-01-04 01:10:25 +0000819{
Eric Andersen0298be82002-03-05 15:12:19 +0000820 struct init_action *a, *b = NULL;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000821
Eric Andersen0298be82002-03-05 15:12:19 +0000822 for (a = init_action_list; a; b = a, a = a->next) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000823 if (a == action) {
824 if (b == NULL) {
Eric Andersen0298be82002-03-05 15:12:19 +0000825 init_action_list = a->next;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000826 } else {
Eric Andersen0298be82002-03-05 15:12:19 +0000827 b->next = a->next;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000828 }
829 free(a);
830 break;
831 }
Erik Andersen7dc16072000-01-04 01:10:25 +0000832 }
Erik Andersen7dc16072000-01-04 01:10:25 +0000833}
834
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000835/* NOTE that if CONFIG_FEATURE_USE_INITTAB is NOT defined,
Erik Andersen9e737252000-01-06 01:16:13 +0000836 * then parse_inittab() simply adds in some default
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000837 * actions(i.e., runs INIT_SCRIPT and then starts a pair
838 * of "askfirst" shells). If CONFIG_FEATURE_USE_INITTAB
839 * _is_ defined, but /etc/inittab is missing, this
Erik Andersen812d4662000-01-07 18:30:40 +0000840 * results in the same set of default behaviors.
Glenn L McGrathbaf55a82002-08-22 18:22:10 +0000841 */
Eric Andersen3e6ff902001-03-09 21:24:12 +0000842static void parse_inittab(void)
Erik Andersen7dc16072000-01-04 01:10:25 +0000843{
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000844#ifdef CONFIG_FEATURE_USE_INITTAB
Erik Andersene49d5ec2000-02-08 19:58:47 +0000845 FILE *file;
Glenn L McGrathb4a1baa2003-01-13 22:09:50 +0000846 char buf[INIT_BUFFS_SIZE], lineAsRead[INIT_BUFFS_SIZE];
847 char tmpConsole[CONSOLE_BUFF_SIZE];
Eric Andersen0298be82002-03-05 15:12:19 +0000848 char *id, *runlev, *action, *command, *eol;
849 const struct init_action_type *a = actions;
Erik Andersen7dc16072000-01-04 01:10:25 +0000850
851
Erik Andersene49d5ec2000-02-08 19:58:47 +0000852 file = fopen(INITTAB, "r");
853 if (file == NULL) {
854 /* No inittab file -- set up some default behavior */
Erik Andersen9e737252000-01-06 01:16:13 +0000855#endif
Eric Andersenc97ec342001-04-03 18:01:51 +0000856 /* Reboot on Ctrl-Alt-Del */
Glenn L McGrathb4a1baa2003-01-13 22:09:50 +0000857 new_init_action(CTRLALTDEL, "/sbin/reboot", "");
Eric Andersen1644db92001-09-05 20:18:15 +0000858 /* Umount all filesystems on halt/reboot */
Glenn L McGrathb4a1baa2003-01-13 22:09:50 +0000859 new_init_action(SHUTDOWN, "/bin/umount -a -r", "");
Eric Andersen1644db92001-09-05 20:18:15 +0000860 /* Swapoff on halt/reboot */
Rob Landley7a8f6792005-08-30 18:17:05 +0000861 if(ENABLE_SWAPONOFF) new_init_action(SHUTDOWN, "/sbin/swapoff -a", "");
Eric Andersen730f8262001-12-17 23:13:08 +0000862 /* Prepare to restart init when a HUP is received */
Glenn L McGrathb4a1baa2003-01-13 22:09:50 +0000863 new_init_action(RESTART, "/sbin/init", "");
Eric Andersen3bc2b202002-07-29 06:39:58 +0000864 /* Askfirst shell on tty1-4 */
Glenn L McGrathdc4e75e2003-09-02 02:36:18 +0000865 new_init_action(ASKFIRST, bb_default_login_shell, "");
866 new_init_action(ASKFIRST, bb_default_login_shell, VC_2);
867 new_init_action(ASKFIRST, bb_default_login_shell, VC_3);
868 new_init_action(ASKFIRST, bb_default_login_shell, VC_4);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000869 /* sysinit */
Glenn L McGrathb4a1baa2003-01-13 22:09:50 +0000870 new_init_action(SYSINIT, INIT_SCRIPT, "");
Erik Andersen7dc16072000-01-04 01:10:25 +0000871
Erik Andersene49d5ec2000-02-08 19:58:47 +0000872 return;
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000873#ifdef CONFIG_FEATURE_USE_INITTAB
Erik Andersen9e737252000-01-06 01:16:13 +0000874 }
Erik Andersen7dc16072000-01-04 01:10:25 +0000875
Eric Andersen0826b6b2002-07-03 23:50:16 +0000876 while (fgets(buf, INIT_BUFFS_SIZE, file) != NULL) {
Eric Andersenb5966362000-05-31 20:04:38 +0000877 /* Skip leading spaces */
878 for (id = buf; *id == ' ' || *id == '\t'; id++);
879
880 /* Skip the line if it's a comment */
881 if (*id == '#' || *id == '\n')
Erik Andersene49d5ec2000-02-08 19:58:47 +0000882 continue;
Erik Andersen7dc16072000-01-04 01:10:25 +0000883
Erik Andersene49d5ec2000-02-08 19:58:47 +0000884 /* Trim the trailing \n */
Eric Andersenb5966362000-05-31 20:04:38 +0000885 eol = strrchr(id, '\n');
886 if (eol != NULL)
887 *eol = '\0';
Erik Andersen7dc16072000-01-04 01:10:25 +0000888
Erik Andersene49d5ec2000-02-08 19:58:47 +0000889 /* Keep a copy around for posterity's sake (and error msgs) */
890 strcpy(lineAsRead, buf);
891
Eric Andersenb5966362000-05-31 20:04:38 +0000892 /* Separate the ID field from the runlevels */
893 runlev = strchr(id, ':');
894 if (runlev == NULL || *(runlev + 1) == '\0') {
Glenn L McGrathb4a1baa2003-01-13 22:09:50 +0000895 message(LOG | CONSOLE, "Bad inittab entry: %s", lineAsRead);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000896 continue;
897 } else {
Eric Andersenb5966362000-05-31 20:04:38 +0000898 *runlev = '\0';
899 ++runlev;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000900 }
901
Eric Andersenb5966362000-05-31 20:04:38 +0000902 /* Separate the runlevels from the action */
903 action = strchr(runlev, ':');
904 if (action == NULL || *(action + 1) == '\0') {
Glenn L McGrathb4a1baa2003-01-13 22:09:50 +0000905 message(LOG | CONSOLE, "Bad inittab entry: %s", lineAsRead);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000906 continue;
907 } else {
Eric Andersenb5966362000-05-31 20:04:38 +0000908 *action = '\0';
909 ++action;
910 }
911
Eric Andersen0298be82002-03-05 15:12:19 +0000912 /* Separate the action from the command */
913 command = strchr(action, ':');
914 if (command == NULL || *(command + 1) == '\0') {
Glenn L McGrathb4a1baa2003-01-13 22:09:50 +0000915 message(LOG | CONSOLE, "Bad inittab entry: %s", lineAsRead);
Eric Andersenb5966362000-05-31 20:04:38 +0000916 continue;
917 } else {
Eric Andersen0298be82002-03-05 15:12:19 +0000918 *command = '\0';
919 ++command;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000920 }
921
922 /* Ok, now process it */
Glenn L McGrathb4a1baa2003-01-13 22:09:50 +0000923 for (a = actions; a->name != 0; a++) {
Eric Andersenb5966362000-05-31 20:04:38 +0000924 if (strcmp(a->name, action) == 0) {
925 if (*id != '\0') {
Glenn L McGrathb4a1baa2003-01-13 22:09:50 +0000926 if(strncmp(id, "/dev/", 5) == 0)
927 id += 5;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000928 strcpy(tmpConsole, "/dev/");
Glenn L McGrathb4a1baa2003-01-13 22:09:50 +0000929 safe_strncpy(tmpConsole + 5, id,
930 CONSOLE_BUFF_SIZE - 5);
Eric Andersenb5966362000-05-31 20:04:38 +0000931 id = tmpConsole;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000932 }
Eric Andersen0298be82002-03-05 15:12:19 +0000933 new_init_action(a->action, command, id);
Glenn L McGrathb4a1baa2003-01-13 22:09:50 +0000934 break;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000935 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000936 }
Glenn L McGrathb4a1baa2003-01-13 22:09:50 +0000937 if (a->name == 0) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000938 /* Choke on an unknown action */
Glenn L McGrathb4a1baa2003-01-13 22:09:50 +0000939 message(LOG | CONSOLE, "Bad inittab entry: %s", lineAsRead);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000940 }
Erik Andersen7dc16072000-01-04 01:10:25 +0000941 }
Eric Andersend8636ca2002-05-15 22:19:09 +0000942 fclose(file);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000943 return;
Glenn L McGrathbaf55a82002-08-22 18:22:10 +0000944#endif /* CONFIG_FEATURE_USE_INITTAB */
Erik Andersen7dc16072000-01-04 01:10:25 +0000945}
946
Eric Andersen82baf632004-10-08 08:21:54 +0000947#ifdef CONFIG_FEATURE_USE_INITTAB
"Vladimir N. Oleynik"1f04c9d2006-02-01 14:47:52 +0000948static void reload_signal(int sig ATTRIBUTE_UNUSED)
Eric Andersen6fd0e312003-07-22 09:48:56 +0000949{
Eric Andersen82baf632004-10-08 08:21:54 +0000950 struct init_action *a, *tmp;
951
952 message(LOG, "Reloading /etc/inittab");
953
954 /* disable old entrys */
955 for (a = init_action_list; a; a = a->next ) {
956 a->action = ONCE;
957 }
958
959 parse_inittab();
960
961 /* remove unused entrys */
962 for (a = init_action_list; a; a = tmp) {
963 tmp = a->next;
964 if (a->action & (ONCE | SYSINIT | WAIT ) &&
965 a->pid == 0 ) {
966 delete_init_action(a);
967 }
968 }
Eric Andersen6fd0e312003-07-22 09:48:56 +0000969 run_actions(RESPAWN);
Eric Andersen82baf632004-10-08 08:21:54 +0000970 return;
Eric Andersen6fd0e312003-07-22 09:48:56 +0000971}
Eric Andersen82baf632004-10-08 08:21:54 +0000972#endif /* CONFIG_FEATURE_USE_INITTAB */
973
Rob Landleydfba7412006-03-06 20:47:33 +0000974int init_main(int argc, char **argv)
Eric Andersen0460ff21999-10-25 23:32:44 +0000975{
Eric Andersen0298be82002-03-05 15:12:19 +0000976 struct init_action *a;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000977 pid_t wpid;
Eric Andersen0460ff21999-10-25 23:32:44 +0000978
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 */
1024 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 */
1042 new_init_action(SYSINIT, "/bin/mount -t proc proc /proc", "");
1043 /* Try to turn on swap */
1044 new_init_action(SYSINIT, "/sbin/swapon -a", "");
1045 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 */
1050 if (argc > 1 && (!strcmp(argv[1], "single") ||
1051 !strcmp(argv[1], "-s") || !strcmp(argv[1], "1"))) {
Glenn L McGrathb4a1baa2003-01-13 22:09:50 +00001052 /* Start a shell on console */
Glenn L McGrathdc4e75e2003-09-02 02:36:18 +00001053 new_init_action(RESPAWN, bb_default_login_shell, "");
Erik Andersene49d5ec2000-02-08 19:58:47 +00001054 } else {
1055 /* Not in single user mode -- see what inittab says */
Eric Andersen8a8fbb81999-10-26 00:18:56 +00001056
Eric Andersenbdfd0d72001-10-24 05:00:29 +00001057 /* NOTE that if CONFIG_FEATURE_USE_INITTAB is NOT defined,
Erik Andersene49d5ec2000-02-08 19:58:47 +00001058 * then parse_inittab() simply adds in some default
Eric Andersenc7bda1c2004-03-15 08:29:22 +00001059 * actions(i.e., runs INIT_SCRIPT and then starts a pair
Erik Andersene49d5ec2000-02-08 19:58:47 +00001060 * of "askfirst" shells */
1061 parse_inittab();
Eric Andersend00c2621999-12-07 08:37:31 +00001062 }
Erik Andersen983b51b2000-04-04 18:14:25 +00001063
Rob Landleyb3ede5a2006-03-27 23:09:12 +00001064#ifdef CONFIG_SELINUX
1065 if (getenv("SELINUX_INIT") == NULL) {
1066 int enforce = 0;
1067
1068 putenv("SELINUX_INIT=YES");
1069 if (selinux_init_load_policy(&enforce) == 0) {
1070 execv(argv[0], argv);
1071 } else if (enforce > 0) {
1072 /* SELinux in enforcing mode but load_policy failed */
1073 /* At this point, we probably can't open /dev/console, so log() won't work */
1074 message(CONSOLE,"Unable to load SELinux Policy. Machine is in enforcing mode. Halting now.");
1075 exit(1);
1076 }
1077 }
1078#endif /* CONFIG_SELINUX */
1079
Eric Andersen7ef1a5b2001-03-20 17:39:08 +00001080 /* Make the command line just say "init" -- thats all, nothing else */
1081 fixup_argv(argc, argv, "init");
Eric Andersen219d6f51999-11-02 19:43:01 +00001082
Erik Andersene49d5ec2000-02-08 19:58:47 +00001083 /* Now run everything that needs to be run */
1084
1085 /* First run the sysinit command */
Eric Andersen1644db92001-09-05 20:18:15 +00001086 run_actions(SYSINIT);
Eric Andersen0298be82002-03-05 15:12:19 +00001087
Erik Andersene49d5ec2000-02-08 19:58:47 +00001088 /* Next run anything that wants to block */
Eric Andersen1644db92001-09-05 20:18:15 +00001089 run_actions(WAIT);
Eric Andersen0298be82002-03-05 15:12:19 +00001090
Erik Andersene49d5ec2000-02-08 19:58:47 +00001091 /* Next run anything to be run only once */
Eric Andersen0298be82002-03-05 15:12:19 +00001092 run_actions(ONCE);
1093
Eric Andersen82baf632004-10-08 08:21:54 +00001094#ifdef CONFIG_FEATURE_USE_INITTAB
Eric Andersen6fd0e312003-07-22 09:48:56 +00001095 /* Redefine SIGHUP to reread /etc/inittab */
1096 signal(SIGHUP, reload_signal);
Eric Andersen82baf632004-10-08 08:21:54 +00001097#else
1098 signal(SIGHUP, SIG_IGN);
1099#endif /* CONFIG_FEATURE_USE_INITTAB */
1100
Eric Andersen6fd0e312003-07-22 09:48:56 +00001101
Erik Andersene49d5ec2000-02-08 19:58:47 +00001102 /* Now run the looping stuff for the rest of forever */
1103 while (1) {
Eric Andersen0298be82002-03-05 15:12:19 +00001104 /* run the respawn stuff */
1105 run_actions(RESPAWN);
1106
1107 /* run the askfirst stuff */
1108 run_actions(ASKFIRST);
1109
1110 /* Don't consume all CPU time -- sleep a bit */
1111 sleep(1);
1112
Erik Andersene49d5ec2000-02-08 19:58:47 +00001113 /* Wait for a child process to exit */
Bernhard Reutner-Fischerc58dbf22006-05-30 12:16:54 +00001114 wpid = wait(NULL);
Eric Andersen87812dc2004-04-12 19:21:54 +00001115 while (wpid > 0) {
Erik Andersene49d5ec2000-02-08 19:58:47 +00001116 /* Find out who died and clean up their corpse */
Eric Andersen0298be82002-03-05 15:12:19 +00001117 for (a = init_action_list; a; a = a->next) {
Erik Andersene49d5ec2000-02-08 19:58:47 +00001118 if (a->pid == wpid) {
Eric Andersen0298be82002-03-05 15:12:19 +00001119 /* Set the pid to 0 so that the process gets
1120 * restarted by run_actions() */
Erik Andersene49d5ec2000-02-08 19:58:47 +00001121 a->pid = 0;
Eric Andersen0298be82002-03-05 15:12:19 +00001122 message(LOG, "Process '%s' (pid %d) exited. "
Glenn L McGrathb4a1baa2003-01-13 22:09:50 +00001123 "Scheduling it for restart.",
1124 a->command, wpid);
Erik Andersene49d5ec2000-02-08 19:58:47 +00001125 }
1126 }
Eric Andersencf1fee02002-12-17 09:48:16 +00001127 /* see if anyone else is waiting to be reaped */
Denis Vlasenkoce979602006-09-27 23:31:08 +00001128 wpid = waitpid(-1, NULL, WNOHANG);
Erik Andersene49d5ec2000-02-08 19:58:47 +00001129 }
Erik Andersene49d5ec2000-02-08 19:58:47 +00001130 }
Eric Andersencc8ed391999-10-05 16:24:54 +00001131}