blob: 877b08122817af26140c5cdd638e4611335dd47e [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"
Erik Andersen4f3f7572000-04-28 00:18:56 +000013#include <stdio.h>
14#include <stdlib.h>
Eric Andersencc8ed391999-10-05 16:24:54 +000015#include <errno.h>
Eric Andersenb186d981999-12-03 09:19:54 +000016#include <paths.h>
Erik Andersen42094cd2000-03-20 21:34:52 +000017#include <signal.h>
18#include <stdarg.h>
Eric Andersencc8ed391999-10-05 16:24:54 +000019#include <string.h>
Erik Andersen4f3f7572000-04-28 00:18:56 +000020#include <termios.h>
21#include <unistd.h>
Eric Andersened3ef502001-01-27 08:24:39 +000022#include <limits.h>
Mike Frysinger706f8f62006-04-29 04:43:31 +000023#include <fcntl.h>
Erik Andersen42094cd2000-03-20 21:34:52 +000024#include <sys/ioctl.h>
Erik Andersen42094cd2000-03-20 21:34:52 +000025#include <sys/types.h>
Erik Andersen42094cd2000-03-20 21:34:52 +000026#include <sys/wait.h>
Eric Andersen85e5e722003-07-22 08:56:55 +000027#include <sys/reboot.h>
Eric Andersenb01ed652003-06-27 17:08:15 +000028
29#include "init_shared.h"
30
31
Eric Andersenbdfd0d72001-10-24 05:00:29 +000032#ifdef CONFIG_SYSLOGD
Erik Andersen4f3f7572000-04-28 00:18:56 +000033# include <sys/syslog.h>
34#endif
Eric Andersen2c1de612003-04-24 11:41:28 +000035
Erik Andersen4f3f7572000-04-28 00:18:56 +000036
Rob Landleyb3ede5a2006-03-27 23:09:12 +000037#ifdef CONFIG_SELINUX
38# include <selinux/selinux.h>
39#endif /* CONFIG_SELINUX */
40
41
Eric Andersen0826b6b2002-07-03 23:50:16 +000042#define INIT_BUFFS_SIZE 256
43
Eric Andersenbd22ed82000-07-08 18:55:24 +000044/* From <linux/vt.h> */
45struct vt_stat {
Glenn L McGrathbaf55a82002-08-22 18:22:10 +000046 unsigned short v_active; /* active vt */
47 unsigned short v_signal; /* signal to send */
48 unsigned short v_state; /* vt bitmask */
Eric Andersenbd22ed82000-07-08 18:55:24 +000049};
Rob Landleybc68cd12006-03-10 19:22:06 +000050enum { VT_GETSTATE = 0x5603 }; /* get global vt state info */
Eric Andersenbd22ed82000-07-08 18:55:24 +000051
52/* From <linux/serial.h> */
53struct serial_struct {
Eric Andersen81155f82003-09-11 08:52:22 +000054 int type;
55 int line;
56 unsigned int port;
57 int irq;
58 int flags;
59 int xmit_fifo_size;
60 int custom_divisor;
61 int baud_base;
62 unsigned short close_delay;
63 char io_type;
64 char reserved_char[1];
65 int hub6;
66 unsigned short closing_wait; /* time to wait before closing */
67 unsigned short closing_wait2; /* no longer used... */
68 unsigned char *iomem_base;
69 unsigned short iomem_reg_shift;
70 unsigned int port_high;
71 unsigned long iomap_base; /* cookie passed into ioremap */
72 int reserved[1];
Eric Andersenbd22ed82000-07-08 18:55:24 +000073};
74
75
Eric Andersen41492d62001-02-23 00:05:56 +000076#ifndef _PATH_STDPATH
Erik Andersen4f3f7572000-04-28 00:18:56 +000077#define _PATH_STDPATH "/usr/bin:/bin:/usr/sbin:/sbin"
Eric Andersen41492d62001-02-23 00:05:56 +000078#endif
Eric Andersencc8ed391999-10-05 16:24:54 +000079
Eric Andersenbdfd0d72001-10-24 05:00:29 +000080#if defined CONFIG_FEATURE_INIT_COREDUMPS
Erik Andersen983b51b2000-04-04 18:14:25 +000081/*
Eric Andersenc7bda1c2004-03-15 08:29:22 +000082 * When a file named CORE_ENABLE_FLAG_FILE exists, setrlimit is called
Erik Andersen183da4a2000-04-04 18:36:37 +000083 * before processes are spawned to set core file size as unlimited.
84 * This is for debugging only. Don't use this is production, unless
85 * you want core dumps lying about....
Erik Andersen983b51b2000-04-04 18:14:25 +000086 */
87#define CORE_ENABLE_FLAG_FILE "/.init_enable_core"
88#include <sys/resource.h>
Erik Andersen183da4a2000-04-04 18:36:37 +000089#endif
Erik Andersen983b51b2000-04-04 18:14:25 +000090
Erik Andersen4d1d0111999-12-17 18:44:15 +000091#define KERNEL_VERSION(a,b,c) (((a) << 16) + ((b) << 8) + (c))
Erik Andersen4d1d0111999-12-17 18:44:15 +000092
Glenn L McGrathbaf55a82002-08-22 18:22:10 +000093#define INITTAB "/etc/inittab" /* inittab file location */
Erik Andersen96e2abd2000-01-07 11:40:44 +000094#ifndef INIT_SCRIPT
Glenn L McGrathbaf55a82002-08-22 18:22:10 +000095#define INIT_SCRIPT "/etc/init.d/rcS" /* Default sysinit script. */
Erik Andersen96e2abd2000-01-07 11:40:44 +000096#endif
Erik Andersen9c88cac1999-12-30 09:25:17 +000097
Eric Andersen41492d62001-02-23 00:05:56 +000098#define MAXENV 16 /* Number of env. vars */
Erik Andersen7dc16072000-01-04 01:10:25 +000099
Glenn L McGrathb4a1baa2003-01-13 22:09:50 +0000100#define CONSOLE_BUFF_SIZE 32
101
Erik Andersen7dc16072000-01-04 01:10:25 +0000102/* Allowed init action types */
Eric Andersen0298be82002-03-05 15:12:19 +0000103#define SYSINIT 0x001
104#define RESPAWN 0x002
105#define ASKFIRST 0x004
106#define WAIT 0x008
107#define ONCE 0x010
108#define CTRLALTDEL 0x020
109#define SHUTDOWN 0x040
110#define RESTART 0x080
Erik Andersen7dc16072000-01-04 01:10:25 +0000111
Erik Andersen42094cd2000-03-20 21:34:52 +0000112/* A mapping between "inittab" action name strings and action type codes. */
Eric Andersen0298be82002-03-05 15:12:19 +0000113struct init_action_type {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000114 const char *name;
Eric Andersen0298be82002-03-05 15:12:19 +0000115 int action;
116};
Erik Andersen7dc16072000-01-04 01:10:25 +0000117
Eric Andersen0298be82002-03-05 15:12:19 +0000118static const struct init_action_type actions[] = {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000119 {"sysinit", SYSINIT},
120 {"respawn", RESPAWN},
121 {"askfirst", ASKFIRST},
122 {"wait", WAIT},
123 {"once", ONCE},
Erik Andersene132f4b2000-02-09 04:16:43 +0000124 {"ctrlaltdel", CTRLALTDEL},
Eric Andersenc97ec342001-04-03 18:01:51 +0000125 {"shutdown", SHUTDOWN},
Eric Andersen730f8262001-12-17 23:13:08 +0000126 {"restart", RESTART},
Pavel Roskin9027bcf2000-07-14 15:44:25 +0000127 {0, 0}
Erik Andersen7dc16072000-01-04 01:10:25 +0000128};
129
Eric Andersen0298be82002-03-05 15:12:19 +0000130/* Set up a linked list of init_actions, to be read from inittab */
131struct init_action {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000132 pid_t pid;
Eric Andersen0826b6b2002-07-03 23:50:16 +0000133 char command[INIT_BUFFS_SIZE];
Glenn L McGrathb4a1baa2003-01-13 22:09:50 +0000134 char terminal[CONSOLE_BUFF_SIZE];
Eric Andersen0298be82002-03-05 15:12:19 +0000135 struct init_action *next;
136 int action;
Erik Andersen7dc16072000-01-04 01:10:25 +0000137};
Erik Andersen7dc16072000-01-04 01:10:25 +0000138
Eric Andersen0298be82002-03-05 15:12:19 +0000139/* Static variables */
140static struct init_action *init_action_list = NULL;
Bernhard Reutner-Fischercf1f2ac2006-06-02 10:43:17 +0000141static char console[CONSOLE_BUFF_SIZE] = CONSOLE_DEV;
Glenn L McGrathbaf55a82002-08-22 18:22:10 +0000142
Eric Andersene7078062002-07-29 08:00:16 +0000143#ifndef CONFIG_SYSLOGD
Mike Frysinger72a4c332005-07-05 02:19:20 +0000144static char *log_console = VC_5;
Eric Andersene7078062002-07-29 08:00:16 +0000145#endif
Bernhard Reutner-Fischercf1f2ac2006-06-02 10:43:17 +0000146#if !ENABLE_DEBUG_INIT
Eric Andersen0298be82002-03-05 15:12:19 +0000147static sig_atomic_t got_cont = 0;
Bernhard Reutner-Fischercf1f2ac2006-06-02 10:43:17 +0000148#endif
Rob Landleybc68cd12006-03-10 19:22:06 +0000149
150enum {
151 LOG = 0x1,
152 CONSOLE = 0x2,
Glenn L McGrathbaf55a82002-08-22 18:22:10 +0000153
Eric Andersenb0cc0a62002-03-20 14:57:50 +0000154#if defined CONFIG_FEATURE_EXTRA_QUIET
Rob Landleybc68cd12006-03-10 19:22:06 +0000155 MAYBE_CONSOLE = 0x0,
Eric Andersen0298be82002-03-05 15:12:19 +0000156#else
Rob Landleybc68cd12006-03-10 19:22:06 +0000157 MAYBE_CONSOLE = CONSOLE,
Eric Andersen0298be82002-03-05 15:12:19 +0000158#endif
Glenn L McGrathbaf55a82002-08-22 18:22:10 +0000159
Rob Landleybc68cd12006-03-10 19:22:06 +0000160#ifndef RB_HALT_SYSTEM
Bernhard Reutner-Fischercf1f2ac2006-06-02 10:43:17 +0000161 RB_HALT_SYSTEM = 0xcdef0123, /* FIXME: this overflows enum */
Rob Landleybc68cd12006-03-10 19:22:06 +0000162 RB_ENABLE_CAD = 0x89abcdef,
163 RB_DISABLE_CAD = 0,
164 RB_POWER_OFF = 0x4321fedc,
165 RB_AUTOBOOT = 0x01234567,
Eric Andersen0298be82002-03-05 15:12:19 +0000166#endif
Rob Landleybc68cd12006-03-10 19:22:06 +0000167};
Erik Andersen42094cd2000-03-20 21:34:52 +0000168
Glenn L McGrathb4a1baa2003-01-13 22:09:50 +0000169static const char * const environment[] = {
170 "HOME=/",
171 "PATH=" _PATH_STDPATH,
Glenn L McGrathdc4e75e2003-09-02 02:36:18 +0000172 "SHELL=/bin/sh",
Glenn L McGrathb4a1baa2003-01-13 22:09:50 +0000173 "USER=root",
174 NULL
175};
176
Eric Andersen0298be82002-03-05 15:12:19 +0000177/* Function prototypes */
178static void delete_init_action(struct init_action *a);
Bernhard Reutner-Fischer3ab30802006-05-30 12:10:29 +0000179static int waitfor(const struct init_action *a, pid_t pid);
Bernhard Reutner-Fischercf1f2ac2006-06-02 10:43:17 +0000180#if !ENABLE_DEBUG_INIT
Paul Foxd112f8f2006-06-01 13:17:49 +0000181static void shutdown_signal(int sig);
Bernhard Reutner-Fischercf1f2ac2006-06-02 10:43:17 +0000182#endif
Eric Andersen0460ff21999-10-25 23:32:44 +0000183
Eric Andersen74400cc2001-10-18 04:11:39 +0000184static void loop_forever(void)
Matt Kraai67a46402001-06-03 05:55:52 +0000185{
186 while (1)
Glenn L McGrathbaf55a82002-08-22 18:22:10 +0000187 sleep(1);
Matt Kraai67a46402001-06-03 05:55:52 +0000188}
Eric Andersen7ef1a5b2001-03-20 17:39:08 +0000189
Erik Andersen42094cd2000-03-20 21:34:52 +0000190/* Print a message to the specified device.
191 * Device may be bitwise-or'd from LOG | CONSOLE */
Bernhard Reutner-Fischer35e1a072006-05-29 13:08:35 +0000192#if ENABLE_DEBUG_INIT
193#define messageD message
194#else
195static inline void messageD(int ATTRIBUTE_UNUSED device,
"Vladimir N. Oleynik"1f04c9d2006-02-01 14:47:52 +0000196 const char ATTRIBUTE_UNUSED *fmt, ...)
Glenn L McGrathbaf55a82002-08-22 18:22:10 +0000197{
198}
Eric Andersenfedce062001-11-17 07:27:14 +0000199#endif
Glenn L McGrathb4a1baa2003-01-13 22:09:50 +0000200static void message(int device, const char *fmt, ...)
Glenn L McGrathbaf55a82002-08-22 18:22:10 +0000201 __attribute__ ((format(printf, 2, 3)));
Glenn L McGrathb4a1baa2003-01-13 22:09:50 +0000202static void message(int device, const char *fmt, ...)
Eric Andersen0460ff21999-10-25 23:32:44 +0000203{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000204 va_list arguments;
Glenn L McGrathb4a1baa2003-01-13 22:09:50 +0000205 int l;
Bernhard Reutner-Fischer3ab30802006-05-30 12:10:29 +0000206 RESERVE_CONFIG_BUFFER(msg, 1024);
Eric Andersen22237012003-01-23 07:08:26 +0000207#ifndef CONFIG_SYSLOGD
208 static int log_fd = -1;
209#endif
Eric Andersen4c781471999-11-26 08:12:56 +0000210
Glenn L McGrathb4a1baa2003-01-13 22:09:50 +0000211 msg[0] = '\r';
Erik Andersene49d5ec2000-02-08 19:58:47 +0000212 va_start(arguments, fmt);
Bernhard Reutner-Fischer3ab30802006-05-30 12:10:29 +0000213 l = vsnprintf(msg + 1, 1024 - 2, fmt, arguments) + 1;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000214 va_end(arguments);
Glenn L McGrathb4a1baa2003-01-13 22:09:50 +0000215
216#ifdef CONFIG_SYSLOGD
217 /* Log the message to syslogd */
218 if (device & LOG) {
219 /* don`t out "\r\n" */
Eric Andersen36adca82004-06-22 10:07:17 +0000220 openlog(bb_applet_name, 0, LOG_DAEMON);
"Vladimir N. Oleynik"8e1bd4a2005-09-29 12:55:21 +0000221 syslog(LOG_INFO, "%s", msg + 1);
Eric Andersen36adca82004-06-22 10:07:17 +0000222 closelog();
Eric Andersen3ae0c781999-11-04 01:13:21 +0000223 }
Glenn L McGrathb4a1baa2003-01-13 22:09:50 +0000224
225 msg[l++] = '\n';
226 msg[l] = 0;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000227#else
Erik Andersene49d5ec2000-02-08 19:58:47 +0000228
Glenn L McGrathb4a1baa2003-01-13 22:09:50 +0000229 msg[l++] = '\n';
230 msg[l] = 0;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000231 /* Take full control of the log tty, and never close it.
232 * It's mine, all mine! Muhahahaha! */
233 if (log_fd < 0) {
Mike Frysingere548bdf2005-07-06 04:46:14 +0000234 if ((log_fd = device_open(log_console, O_RDWR | O_NONBLOCK | O_NOCTTY)) < 0) {
Erik Andersene132f4b2000-02-09 04:16:43 +0000235 log_fd = -2;
Mike Frysinger72a4c332005-07-05 02:19:20 +0000236 bb_error_msg("Bummer, can't write to log on %s!", log_console);
Erik Andersene132f4b2000-02-09 04:16:43 +0000237 device = CONSOLE;
Eric Andersen6a979902002-09-30 20:08:53 +0000238 } else {
239 fcntl(log_fd, F_SETFD, FD_CLOEXEC);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000240 }
241 }
242 if ((device & LOG) && (log_fd >= 0)) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000243 bb_full_write(log_fd, msg, l);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000244 }
Eric Andersen4c781471999-11-26 08:12:56 +0000245#endif
246
Erik Andersene49d5ec2000-02-08 19:58:47 +0000247 if (device & CONSOLE) {
Bernhard Reutner-Fischercf1f2ac2006-06-02 10:43:17 +0000248 int fd = device_open(CONSOLE_DEV,
Mike Frysingere548bdf2005-07-06 04:46:14 +0000249 O_WRONLY | O_NOCTTY | O_NONBLOCK);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000250 /* Always send console messages to /dev/console so people will see them. */
Glenn L McGrathb4a1baa2003-01-13 22:09:50 +0000251 if (fd >= 0) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000252 bb_full_write(fd, msg, l);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000253 close(fd);
Bernhard Reutner-Fischer35e1a072006-05-29 13:08:35 +0000254#if ENABLE_DEBUG_INIT
Glenn L McGrathb4a1baa2003-01-13 22:09:50 +0000255 /* all descriptors may be closed */
Erik Andersene49d5ec2000-02-08 19:58:47 +0000256 } else {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000257 bb_error_msg("Bummer, can't print: ");
Erik Andersene49d5ec2000-02-08 19:58:47 +0000258 va_start(arguments, fmt);
259 vfprintf(stderr, fmt, arguments);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000260 va_end(arguments);
Glenn L McGrathb4a1baa2003-01-13 22:09:50 +0000261#endif
Erik Andersene49d5ec2000-02-08 19:58:47 +0000262 }
Eric Andersen3ae0c781999-11-04 01:13:21 +0000263 }
Bernhard Reutner-Fischer3ab30802006-05-30 12:10:29 +0000264 RELEASE_CONFIG_BUFFER(msg);
Eric Andersencc8ed391999-10-05 16:24:54 +0000265}
266
Eric Andersen0460ff21999-10-25 23:32:44 +0000267/* Set terminal settings to reasonable defaults */
Bernhard Reutner-Fischer49e60b92006-05-29 12:57:52 +0000268static void set_term(void)
Eric Andersencc8ed391999-10-05 16:24:54 +0000269{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000270 struct termios tty;
Eric Andersencc8ed391999-10-05 16:24:54 +0000271
Bernhard Reutner-Fischer49e60b92006-05-29 12:57:52 +0000272 tcgetattr(STDIN_FILENO, &tty);
Eric Andersencc8ed391999-10-05 16:24:54 +0000273
Erik Andersene49d5ec2000-02-08 19:58:47 +0000274 /* set control chars */
Glenn L McGrathbaf55a82002-08-22 18:22:10 +0000275 tty.c_cc[VINTR] = 3; /* C-c */
276 tty.c_cc[VQUIT] = 28; /* C-\ */
277 tty.c_cc[VERASE] = 127; /* C-? */
278 tty.c_cc[VKILL] = 21; /* C-u */
279 tty.c_cc[VEOF] = 4; /* C-d */
Eric Andersen3849f9b2000-07-10 19:56:47 +0000280 tty.c_cc[VSTART] = 17; /* C-q */
Glenn L McGrathbaf55a82002-08-22 18:22:10 +0000281 tty.c_cc[VSTOP] = 19; /* C-s */
282 tty.c_cc[VSUSP] = 26; /* C-z */
Eric Andersen02bc25b2000-07-06 21:29:32 +0000283
Erik Andersene49d5ec2000-02-08 19:58:47 +0000284 /* use line dicipline 0 */
285 tty.c_line = 0;
Eric Andersen7f1acfd1999-10-29 23:09:13 +0000286
Erik Andersene49d5ec2000-02-08 19:58:47 +0000287 /* Make it be sane */
Glenn L McGrathbaf55a82002-08-22 18:22:10 +0000288 tty.c_cflag &= CBAUD | CBAUDEX | CSIZE | CSTOPB | PARENB | PARODD;
289 tty.c_cflag |= CREAD | HUPCL | CLOCAL;
Eric Andersend8862922001-04-23 15:14:11 +0000290
Eric Andersen08b10341999-11-19 02:38:58 +0000291
Erik Andersene49d5ec2000-02-08 19:58:47 +0000292 /* input modes */
293 tty.c_iflag = ICRNL | IXON | IXOFF;
Eric Andersen08b10341999-11-19 02:38:58 +0000294
Erik Andersene49d5ec2000-02-08 19:58:47 +0000295 /* output modes */
296 tty.c_oflag = OPOST | ONLCR;
Eric Andersen7f1acfd1999-10-29 23:09:13 +0000297
Erik Andersene49d5ec2000-02-08 19:58:47 +0000298 /* local modes */
299 tty.c_lflag =
300 ISIG | ICANON | ECHO | ECHOE | ECHOK | ECHOCTL | ECHOKE | IEXTEN;
Eric Andersencc8ed391999-10-05 16:24:54 +0000301
Bernhard Reutner-Fischer49e60b92006-05-29 12:57:52 +0000302 tcsetattr(STDIN_FILENO, TCSANOW, &tty);
Eric Andersencc8ed391999-10-05 16:24:54 +0000303}
304
Eric Andersen74400cc2001-10-18 04:11:39 +0000305static void console_init(void)
Eric Andersen0460ff21999-10-25 23:32:44 +0000306{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000307 int fd;
Glenn L McGrathb4a1baa2003-01-13 22:09:50 +0000308 int tried = 0;
Erik Andersen029011b2000-03-04 21:19:32 +0000309 struct vt_stat vt;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000310 struct serial_struct sr;
311 char *s;
Eric Andersen0460ff21999-10-25 23:32:44 +0000312
Glenn L McGrathb4a1baa2003-01-13 22:09:50 +0000313 if ((s = getenv("CONSOLE")) != NULL || (s = getenv("console")) != NULL) {
Matt Kraai18447702001-05-18 21:24:58 +0000314 safe_strncpy(console, s, sizeof(console));
Glenn L McGrathb4a1baa2003-01-13 22:09:50 +0000315 } else {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000316 /* 2.2 kernels: identify the real console backend and try to use it */
317 if (ioctl(0, TIOCGSERIAL, &sr) == 0) {
318 /* this is a serial console */
Matt Kraai439e3df2001-07-23 14:52:08 +0000319 snprintf(console, sizeof(console) - 1, SC_FORMAT, sr.line);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000320 } else if (ioctl(0, VT_GETSTATE, &vt) == 0) {
321 /* this is linux virtual tty */
Matt Kraai439e3df2001-07-23 14:52:08 +0000322 snprintf(console, sizeof(console) - 1, VC_FORMAT, vt.v_active);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000323 } else {
Bernhard Reutner-Fischercf1f2ac2006-06-02 10:43:17 +0000324 safe_strncpy(console, CONSOLE_DEV, sizeof(console));
Glenn L McGrathb4a1baa2003-01-13 22:09:50 +0000325 tried++;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000326 }
Eric Andersen07e52971999-11-07 07:38:08 +0000327 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000328
Glenn L McGrathb4a1baa2003-01-13 22:09:50 +0000329 while ((fd = open(console, O_RDONLY | O_NONBLOCK)) < 0 && tried < 2) {
330 /* Can't open selected console -- try
331 logical system console and VT_MASTER */
Bernhard Reutner-Fischercf1f2ac2006-06-02 10:43:17 +0000332 safe_strncpy(console, (tried == 0 ? CONSOLE_DEV : CURRENT_VC),
Glenn L McGrathb4a1baa2003-01-13 22:09:50 +0000333 sizeof(console));
334 tried++;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000335 }
336 if (fd < 0) {
337 /* Perhaps we should panic here? */
Glenn L McGrathb4a1baa2003-01-13 22:09:50 +0000338#ifndef CONFIG_SYSLOGD
Mike Frysinger72a4c332005-07-05 02:19:20 +0000339 log_console =
Glenn L McGrathb4a1baa2003-01-13 22:09:50 +0000340#endif
"Vladimir N. Oleynik"6c35c7c2005-10-12 15:34:25 +0000341 safe_strncpy(console, bb_dev_null, sizeof(console));
Eric Andersen07e52971999-11-07 07:38:08 +0000342 } else {
Glenn L McGrathb4a1baa2003-01-13 22:09:50 +0000343 s = getenv("TERM");
Eric Andersen3bc2b202002-07-29 06:39:58 +0000344 /* check for serial console */
Glenn L McGrathb4a1baa2003-01-13 22:09:50 +0000345 if (ioctl(fd, TIOCGSERIAL, &sr) == 0) {
Erik Andersenfa4718e2000-02-21 19:25:12 +0000346 /* Force the TERM setting to vt102 for serial console --
Eric Andersen3bc2b202002-07-29 06:39:58 +0000347 * if TERM is set to linux (the default) */
Glenn L McGrathb4a1baa2003-01-13 22:09:50 +0000348 if (s == NULL || strcmp(s, "linux") == 0)
349 putenv("TERM=vt102");
350#ifndef CONFIG_SYSLOGD
Mike Frysinger72a4c332005-07-05 02:19:20 +0000351 log_console = console;
Glenn L McGrathb4a1baa2003-01-13 22:09:50 +0000352#endif
353 } else {
354 if (s == NULL)
355 putenv("TERM=linux");
Erik Andersene49d5ec2000-02-08 19:58:47 +0000356 }
357 close(fd);
Eric Andersen07e52971999-11-07 07:38:08 +0000358 }
Glenn L McGrathb4a1baa2003-01-13 22:09:50 +0000359 messageD(LOG, "console=%s", console);
Eric Andersen0460ff21999-10-25 23:32:44 +0000360}
Glenn L McGrathbaf55a82002-08-22 18:22:10 +0000361
Eric Andersen7ef1a5b2001-03-20 17:39:08 +0000362static void fixup_argv(int argc, char **argv, char *new_argv0)
363{
364 int len;
Glenn L McGrathbaf55a82002-08-22 18:22:10 +0000365
Eric Andersen7ef1a5b2001-03-20 17:39:08 +0000366 /* Fix up argv[0] to be certain we claim to be init */
367 len = strlen(argv[0]);
368 memset(argv[0], 0, len);
Eric Andersen72f9a422001-10-28 05:12:20 +0000369 safe_strncpy(argv[0], new_argv0, len + 1);
Eric Andersen7ef1a5b2001-03-20 17:39:08 +0000370
371 /* Wipe argv[1]-argv[N] so they don't clutter the ps listing */
372 len = 1;
373 while (argc > len) {
374 memset(argv[len], 0, strlen(argv[len]));
375 len++;
376 }
377}
378
Bernhard Reutner-Fischer0da069d2006-05-29 12:54:16 +0000379/* Open the new terminal device */
Bernhard Reutner-Fischercf1f2ac2006-06-02 10:43:17 +0000380static void open_new_terminal(const char * const device, const int fail) {
Bernhard Reutner-Fischer0da069d2006-05-29 12:54:16 +0000381 struct stat sb;
382
383 if ((device_open(device, O_RDWR)) < 0) {
384 if (stat(device, &sb) != 0) {
385 message(LOG | CONSOLE, "device '%s' does not exist.", device);
386 } else {
387 message(LOG | CONSOLE, "Bummer, can't open %s", device);
388 }
389 if (fail)
390 _exit(1);
391 /* else */
Bernhard Reutner-Fischercf1f2ac2006-06-02 10:43:17 +0000392#if !ENABLE_DEBUG_INIT
Paul Foxd112f8f2006-06-01 13:17:49 +0000393 shutdown_signal(SIGUSR1);
Bernhard Reutner-Fischercf1f2ac2006-06-02 10:43:17 +0000394#else
395 _exit(2);
396#endif
Bernhard Reutner-Fischer0da069d2006-05-29 12:54:16 +0000397 }
398}
399
Glenn L McGrathb4a1baa2003-01-13 22:09:50 +0000400static pid_t run(const struct init_action *a)
Eric Andersen0298be82002-03-05 15:12:19 +0000401{
Bernhard Reutner-Fischer3ab30802006-05-30 12:10:29 +0000402 int i;
Mike Frysinger10427ab2005-07-06 05:00:48 +0000403 pid_t pid;
Eric Andersen0826b6b2002-07-03 23:50:16 +0000404 char *s, *tmpCmd, *cmd[INIT_BUFFS_SIZE], *cmdpath;
Glenn L McGrathbaf55a82002-08-22 18:22:10 +0000405 char buf[INIT_BUFFS_SIZE + 6]; /* INIT_BUFFS_SIZE+strlen("exec ")+1 */
Eric Andersen0298be82002-03-05 15:12:19 +0000406 sigset_t nmask, omask;
Eric Andersen0298be82002-03-05 15:12:19 +0000407 static const char press_enter[] =
408#ifdef CUSTOMIZED_BANNER
409#include CUSTOMIZED_BANNER
410#endif
411 "\nPlease press Enter to activate this console. ";
Erik Andersenac6e71f2000-01-08 22:04:33 +0000412
Eric Andersen0298be82002-03-05 15:12:19 +0000413 /* Block sigchild while forking. */
414 sigemptyset(&nmask);
415 sigaddset(&nmask, SIGCHLD);
416 sigprocmask(SIG_BLOCK, &nmask, &omask);
417
Glenn L McGrathbaf55a82002-08-22 18:22:10 +0000418 if ((pid = fork()) == 0) {
Mike Frysinger10427ab2005-07-06 05:00:48 +0000419
Erik Andersene49d5ec2000-02-08 19:58:47 +0000420 /* Clean up */
421 close(0);
422 close(1);
423 close(2);
Eric Andersen0298be82002-03-05 15:12:19 +0000424 sigprocmask(SIG_SETMASK, &omask, NULL);
425
Eric Andersen0298be82002-03-05 15:12:19 +0000426 /* Reset signal handlers that were set by the parent process */
Erik Andersene49d5ec2000-02-08 19:58:47 +0000427 signal(SIGUSR1, SIG_DFL);
428 signal(SIGUSR2, SIG_DFL);
Glenn L McGrathbaf55a82002-08-22 18:22:10 +0000429 signal(SIGINT, SIG_DFL);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000430 signal(SIGTERM, SIG_DFL);
Glenn L McGrathbaf55a82002-08-22 18:22:10 +0000431 signal(SIGHUP, SIG_DFL);
Mike Frysingera77b4f32005-04-16 08:21:34 +0000432 signal(SIGQUIT, SIG_DFL);
Eric Andersened8a9be2001-11-30 19:10:58 +0000433 signal(SIGCONT, SIG_DFL);
434 signal(SIGSTOP, SIG_DFL);
435 signal(SIGTSTP, SIG_DFL);
Eric Andersen2f6c04f1999-11-01 23:59:44 +0000436
Eric Andersen599e3ce2002-07-03 11:08:10 +0000437 /* Create a new session and make ourself the process
Eric Andersene8a90fb2002-10-12 04:05:48 +0000438 * group leader */
439 setsid();
Eric Andersen599e3ce2002-07-03 11:08:10 +0000440
Eric Andersen0298be82002-03-05 15:12:19 +0000441 /* Open the new terminal device */
Bernhard Reutner-Fischer0da069d2006-05-29 12:54:16 +0000442 open_new_terminal(a->terminal, 1);
Eric Andersen599e3ce2002-07-03 11:08:10 +0000443
Eric Andersen0298be82002-03-05 15:12:19 +0000444 /* Make sure the terminal will act fairly normal for us */
Bernhard Reutner-Fischer49e60b92006-05-29 12:57:52 +0000445 set_term();
Eric Andersen0826b6b2002-07-03 23:50:16 +0000446 /* Setup stdout, stderr for the new process so
Eric Andersen599e3ce2002-07-03 11:08:10 +0000447 * they point to the supplied terminal */
Eric Andersen0826b6b2002-07-03 23:50:16 +0000448 dup(0);
449 dup(0);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000450
Eric Andersen599e3ce2002-07-03 11:08:10 +0000451 /* If the init Action requires us to wait, then force the
Eric Andersen0298be82002-03-05 15:12:19 +0000452 * supplied terminal to be the controlling tty. */
Glenn L McGrathbaf55a82002-08-22 18:22:10 +0000453 if (a->action & (SYSINIT | WAIT | CTRLALTDEL | SHUTDOWN | RESTART)) {
Eric Andersen0298be82002-03-05 15:12:19 +0000454
455 /* Now fork off another process to just hang around */
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);
459 }
460
461 if (pid > 0) {
462
463 /* We are the parent -- wait till the child is done */
464 signal(SIGINT, SIG_IGN);
465 signal(SIGTSTP, SIG_IGN);
466 signal(SIGQUIT, SIG_IGN);
467 signal(SIGCHLD, SIG_DFL);
468
Bernhard Reutner-Fischer3ab30802006-05-30 12:10:29 +0000469 waitfor(NULL, pid);
Eric Andersen0298be82002-03-05 15:12:19 +0000470 /* See if stealing the controlling tty back is necessary */
Bernhard Reutner-Fischer3ab30802006-05-30 12:10:29 +0000471 if (tcgetpgrp(0) != getpid())
Eric Andersen0298be82002-03-05 15:12:19 +0000472 _exit(0);
473
474 /* Use a temporary process to steal the controlling tty. */
475 if ((pid = fork()) < 0) {
Glenn L McGrathb4a1baa2003-01-13 22:09:50 +0000476 message(LOG | CONSOLE, "Can't fork!");
Eric Andersen0298be82002-03-05 15:12:19 +0000477 _exit(1);
Glenn L McGrathbaf55a82002-08-22 18:22:10 +0000478 }
Eric Andersen0298be82002-03-05 15:12:19 +0000479 if (pid == 0) {
480 setsid();
Eric Andersen0826b6b2002-07-03 23:50:16 +0000481 ioctl(0, TIOCSCTTY, 1);
Eric Andersen0298be82002-03-05 15:12:19 +0000482 _exit(0);
Glenn L McGrathbaf55a82002-08-22 18:22:10 +0000483 }
Bernhard Reutner-Fischer3ab30802006-05-30 12:10:29 +0000484 waitfor(NULL, pid);
Eric Andersen0298be82002-03-05 15:12:19 +0000485 _exit(0);
486 }
487
488 /* Now fall though to actually execute things */
Eric Andersen0298be82002-03-05 15:12:19 +0000489 }
490
Erik Andersene132f4b2000-02-09 04:16:43 +0000491 /* See if any special /bin/sh requiring characters are present */
Eric Andersen0298be82002-03-05 15:12:19 +0000492 if (strpbrk(a->command, "~`!$^&*()=|\\{}[];\"'<>?") != NULL) {
Glenn L McGrathdc4e75e2003-09-02 02:36:18 +0000493 cmd[0] = (char *)DEFAULT_SHELL;
Erik Andersene132f4b2000-02-09 04:16:43 +0000494 cmd[1] = "-c";
Glenn L McGrathb4a1baa2003-01-13 22:09:50 +0000495 cmd[2] = strcat(strcpy(buf, "exec "), a->command);
Erik Andersene132f4b2000-02-09 04:16:43 +0000496 cmd[3] = NULL;
497 } else {
498 /* Convert command (char*) into cmd (char**, one word per string) */
Eric Andersen0826b6b2002-07-03 23:50:16 +0000499 strcpy(buf, a->command);
Eric Andersen72f9a422001-10-28 05:12:20 +0000500 s = buf;
Glenn L McGrathbaf55a82002-08-22 18:22:10 +0000501 for (tmpCmd = buf, i = 0; (tmpCmd = strsep(&s, " \t")) != NULL;) {
Erik Andersene132f4b2000-02-09 04:16:43 +0000502 if (*tmpCmd != '\0') {
503 cmd[i] = tmpCmd;
Erik Andersene132f4b2000-02-09 04:16:43 +0000504 i++;
505 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000506 }
Erik Andersene132f4b2000-02-09 04:16:43 +0000507 cmd[i] = NULL;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000508 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000509
Eric Andersen7ef1a5b2001-03-20 17:39:08 +0000510 cmdpath = cmd[0];
Eric Andersen7e3bf6e2000-09-14 22:01:31 +0000511
Eric Andersen7ef1a5b2001-03-20 17:39:08 +0000512 /*
513 Interactive shells want to see a dash in argv[0]. This
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000514 typically is handled by login, argv will be setup this
515 way if a dash appears at the front of the command path
Eric Andersen7e3bf6e2000-09-14 22:01:31 +0000516 (like "-/bin/sh").
Eric Andersen7ef1a5b2001-03-20 17:39:08 +0000517 */
Eric Andersen7e3bf6e2000-09-14 22:01:31 +0000518
Eric Andersen7ef1a5b2001-03-20 17:39:08 +0000519 if (*cmdpath == '-') {
Eric Andersen7e3bf6e2000-09-14 22:01:31 +0000520
Eric Andersen7ef1a5b2001-03-20 17:39:08 +0000521 /* skip over the dash */
522 ++cmdpath;
Eric Andersen7e3bf6e2000-09-14 22:01:31 +0000523
Eric Andersen7ef1a5b2001-03-20 17:39:08 +0000524 /* find the last component in the command pathname */
Manuel Novoa III cad53642003-03-19 09:13:01 +0000525 s = bb_get_last_path_component(cmdpath);
Eric Andersen7e3bf6e2000-09-14 22:01:31 +0000526
Eric Andersen7ef1a5b2001-03-20 17:39:08 +0000527 /* make a new argv[0] */
Glenn L McGrathbaf55a82002-08-22 18:22:10 +0000528 if ((cmd[0] = malloc(strlen(s) + 2)) == NULL) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000529 message(LOG | CONSOLE, bb_msg_memory_exhausted);
Eric Andersen7ef1a5b2001-03-20 17:39:08 +0000530 cmd[0] = cmdpath;
531 } else {
532 cmd[0][0] = '-';
Glenn L McGrathbaf55a82002-08-22 18:22:10 +0000533 strcpy(cmd[0] + 1, s);
Eric Andersen7ef1a5b2001-03-20 17:39:08 +0000534 }
Paul Fox41a72ec2005-08-01 16:43:13 +0000535#ifdef CONFIG_FEATURE_INIT_SCTTY
536 /* Establish this process as session leader and
537 * (attempt) to make the tty (if any) a controlling tty.
538 */
539 (void) setsid();
540 (void) ioctl(0, TIOCSCTTY, 0/*don't steal it*/);
541#endif
Eric Andersen7ef1a5b2001-03-20 17:39:08 +0000542 }
543
Eric Andersen1f50e842004-08-16 09:29:42 +0000544#if !defined(__UCLIBC__) || defined(__ARCH_HAS_MMU__)
Eric Andersen0298be82002-03-05 15:12:19 +0000545 if (a->action & ASKFIRST) {
Glenn L McGrathb4a1baa2003-01-13 22:09:50 +0000546 char c;
Eric Andersen7ef1a5b2001-03-20 17:39:08 +0000547 /*
548 * Save memory by not exec-ing anything large (like a shell)
549 * before the user wants it. This is critical if swap is not
550 * enabled and the system has low memory. Generally this will
551 * be run on the second virtual console, and the first will
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000552 * be allowed to start a shell or whatever an init script
Eric Andersen7ef1a5b2001-03-20 17:39:08 +0000553 * specifies.
554 */
Glenn L McGrathb4a1baa2003-01-13 22:09:50 +0000555 messageD(LOG, "Waiting for enter to start '%s'"
556 "(pid %d, terminal %s)\n",
Glenn L McGrathbaf55a82002-08-22 18:22:10 +0000557 cmdpath, getpid(), a->terminal);
Manuel Novoa III cad53642003-03-19 09:13:01 +0000558 bb_full_write(1, press_enter, sizeof(press_enter) - 1);
Glenn L McGrathb4a1baa2003-01-13 22:09:50 +0000559 while(read(0, &c, 1) == 1 && c != '\n')
560 ;
Eric Andersen7ef1a5b2001-03-20 17:39:08 +0000561 }
Eric Andersen1f50e842004-08-16 09:29:42 +0000562#endif
Eric Andersen7ef1a5b2001-03-20 17:39:08 +0000563
Eric Andersen7ef1a5b2001-03-20 17:39:08 +0000564 /* Log the process name and args */
Glenn L McGrathb4a1baa2003-01-13 22:09:50 +0000565 message(LOG, "Starting pid %d, console %s: '%s'",
Glenn L McGrathbaf55a82002-08-22 18:22:10 +0000566 getpid(), a->terminal, cmdpath);
Eric Andersen7e3bf6e2000-09-14 22:01:31 +0000567
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000568#if defined CONFIG_FEATURE_INIT_COREDUMPS
Bernhard Reutner-Fischer0da069d2006-05-29 12:54:16 +0000569 {
570 struct stat sb;
571 if (stat(CORE_ENABLE_FLAG_FILE, &sb) == 0) {
572 struct rlimit limit;
Glenn L McGrathbaf55a82002-08-22 18:22:10 +0000573
Bernhard Reutner-Fischer0da069d2006-05-29 12:54:16 +0000574 limit.rlim_cur = RLIM_INFINITY;
575 limit.rlim_max = RLIM_INFINITY;
576 setrlimit(RLIMIT_CORE, &limit);
577 }
Erik Andersen983b51b2000-04-04 18:14:25 +0000578 }
Erik Andersen183da4a2000-04-04 18:36:37 +0000579#endif
Erik Andersen983b51b2000-04-04 18:14:25 +0000580
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000581 /* Now run it. The new program will take over this PID,
Erik Andersene49d5ec2000-02-08 19:58:47 +0000582 * so nothing further in init.c should be run. */
Glenn L McGrathb4a1baa2003-01-13 22:09:50 +0000583 execv(cmdpath, cmd);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000584
Eric Andersen7ef1a5b2001-03-20 17:39:08 +0000585 /* We're still here? Some error happened. */
Glenn L McGrathb4a1baa2003-01-13 22:09:50 +0000586 message(LOG | CONSOLE, "Bummer, could not run '%s': %m", cmdpath);
Eric Andersen0298be82002-03-05 15:12:19 +0000587 _exit(-1);
Erik Andersen05df2392000-01-13 04:43:48 +0000588 }
Eric Andersen0298be82002-03-05 15:12:19 +0000589 sigprocmask(SIG_SETMASK, &omask, NULL);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000590 return pid;
Eric Andersen0460ff21999-10-25 23:32:44 +0000591}
592
Bernhard Reutner-Fischer3ab30802006-05-30 12:10:29 +0000593static int waitfor(const struct init_action *a, pid_t pid)
Erik Andersen0e3782f2000-01-07 02:54:55 +0000594{
Bernhard Reutner-Fischer3ab30802006-05-30 12:10:29 +0000595 int runpid;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000596 int status, wpid;
Erik Andersen0e3782f2000-01-07 02:54:55 +0000597
Bernhard Reutner-Fischer3ab30802006-05-30 12:10:29 +0000598 runpid = (NULL == a)? pid : run(a);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000599 while (1) {
Bernhard Reutner-Fischer3ab30802006-05-30 12:10:29 +0000600 wpid = waitpid(runpid,&status,0);
601 if (wpid == runpid)
Erik Andersene49d5ec2000-02-08 19:58:47 +0000602 break;
Glenn L McGrathe6ba16f2003-09-26 10:45:55 +0000603 if (wpid == -1 && errno == ECHILD) {
604 /* we missed its termination */
605 break;
606 }
607 /* FIXME other errors should maybe trigger an error, but allow
608 * the program to continue */
Erik Andersen0e3782f2000-01-07 02:54:55 +0000609 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000610 return wpid;
Erik Andersen0e3782f2000-01-07 02:54:55 +0000611}
612
Eric Andersen0298be82002-03-05 15:12:19 +0000613/* Run all commands of a particular type */
614static void run_actions(int action)
Eric Andersen219d6f51999-11-02 19:43:01 +0000615{
Eric Andersen0298be82002-03-05 15:12:19 +0000616 struct init_action *a, *tmp;
Eric Andersen219d6f51999-11-02 19:43:01 +0000617
Eric Andersen0298be82002-03-05 15:12:19 +0000618 for (a = init_action_list; a; a = tmp) {
619 tmp = a->next;
Eric Andersenc97ec342001-04-03 18:01:51 +0000620 if (a->action == action) {
Rob Landley2dd42792006-03-22 17:39:13 +0000621 if (access(a->terminal, R_OK | W_OK)) {
622 delete_init_action(a);
623 } else if (a->action & (SYSINIT | WAIT | CTRLALTDEL | SHUTDOWN | RESTART)) {
Bernhard Reutner-Fischer3ab30802006-05-30 12:10:29 +0000624 waitfor(a, 0);
Eric Andersen0298be82002-03-05 15:12:19 +0000625 delete_init_action(a);
626 } else if (a->action & ONCE) {
627 run(a);
628 delete_init_action(a);
Glenn L McGrathbaf55a82002-08-22 18:22:10 +0000629 } else if (a->action & (RESPAWN | ASKFIRST)) {
Eric Andersen0298be82002-03-05 15:12:19 +0000630 /* Only run stuff with pid==0. If they have
631 * a pid, that means it is still running */
632 if (a->pid == 0) {
633 a->pid = run(a);
634 }
635 }
Erik Andersene132f4b2000-02-09 04:16:43 +0000636 }
637 }
638}
639
Bernhard Reutner-Fischer35e1a072006-05-29 13:08:35 +0000640#if !ENABLE_DEBUG_INIT
Eric Andersen2c1de612003-04-24 11:41:28 +0000641static void init_reboot(unsigned long magic)
642{
643 pid_t pid;
644 /* We have to fork here, since the kernel calls do_exit(0) in
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000645 * linux/kernel/sys.c, which can cause the machine to panic when
Eric Andersen2c1de612003-04-24 11:41:28 +0000646 * the init process is killed.... */
647 if ((pid = fork()) == 0) {
Eric Andersen2c1de612003-04-24 11:41:28 +0000648 reboot(magic);
Eric Andersen2c1de612003-04-24 11:41:28 +0000649 _exit(0);
650 }
651 waitpid (pid, NULL, 0);
652}
653
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000654static void shutdown_system(void)
Eric Andersencc8ed391999-10-05 16:24:54 +0000655{
Eric Andersen813d88c2001-10-28 22:49:48 +0000656 sigset_t block_signals;
Erik Andersene132f4b2000-02-09 04:16:43 +0000657
Eric Andersena9cc8962002-09-16 06:49:06 +0000658 /* run everything to be run at "shutdown". This is done _prior_
659 * to killing everything, in case people wish to use scripts to
660 * shut things down gracefully... */
661 run_actions(SHUTDOWN);
662
Eric Andersen72f9a422001-10-28 05:12:20 +0000663 /* first disable all our signals */
664 sigemptyset(&block_signals);
665 sigaddset(&block_signals, SIGHUP);
Mike Frysingera77b4f32005-04-16 08:21:34 +0000666 sigaddset(&block_signals, SIGQUIT);
Eric Andersen72f9a422001-10-28 05:12:20 +0000667 sigaddset(&block_signals, SIGCHLD);
668 sigaddset(&block_signals, SIGUSR1);
669 sigaddset(&block_signals, SIGUSR2);
670 sigaddset(&block_signals, SIGINT);
671 sigaddset(&block_signals, SIGTERM);
Eric Andersened8a9be2001-11-30 19:10:58 +0000672 sigaddset(&block_signals, SIGCONT);
673 sigaddset(&block_signals, SIGSTOP);
674 sigaddset(&block_signals, SIGTSTP);
Eric Andersen72f9a422001-10-28 05:12:20 +0000675 sigprocmask(SIG_BLOCK, &block_signals, NULL);
Erik Andersen7dc16072000-01-04 01:10:25 +0000676
Erik Andersene49d5ec2000-02-08 19:58:47 +0000677 /* Allow Ctrl-Alt-Del to reboot system. */
Erik Andersen4f3f7572000-04-28 00:18:56 +0000678 init_reboot(RB_ENABLE_CAD);
Erik Andersene132f4b2000-02-09 04:16:43 +0000679
Glenn L McGrathb4a1baa2003-01-13 22:09:50 +0000680 message(CONSOLE | LOG, "The system is going down NOW !!");
Eric Andersencf8c9cf1999-11-05 00:31:46 +0000681 sync();
Erik Andersene49d5ec2000-02-08 19:58:47 +0000682
683 /* Send signals to every process _except_ pid 1 */
Bernhard Reutner-Fischer7ae15532006-05-30 18:17:21 +0000684 message(CONSOLE | LOG, init_sending_format, "TERM");
Erik Andersene49d5ec2000-02-08 19:58:47 +0000685 kill(-1, SIGTERM);
Erik Andersene132f4b2000-02-09 04:16:43 +0000686 sleep(1);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000687 sync();
688
Bernhard Reutner-Fischer7ae15532006-05-30 18:17:21 +0000689 message(CONSOLE | LOG, init_sending_format, "KILL");
Erik Andersene49d5ec2000-02-08 19:58:47 +0000690 kill(-1, SIGKILL);
Erik Andersene132f4b2000-02-09 04:16:43 +0000691 sleep(1);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000692
Erik Andersene49d5ec2000-02-08 19:58:47 +0000693 sync();
Eric Andersencc8ed391999-10-05 16:24:54 +0000694}
695
"Vladimir N. Oleynik"1f04c9d2006-02-01 14:47:52 +0000696static void exec_signal(int sig ATTRIBUTE_UNUSED)
Eric Andersen730f8262001-12-17 23:13:08 +0000697{
Eric Andersen0298be82002-03-05 15:12:19 +0000698 struct init_action *a, *tmp;
Eric Andersen5222d312002-07-03 05:15:23 +0000699 sigset_t unblock_signals;
Glenn L McGrathbaf55a82002-08-22 18:22:10 +0000700
Eric Andersen0298be82002-03-05 15:12:19 +0000701 for (a = init_action_list; a; a = tmp) {
702 tmp = a->next;
703 if (a->action & RESTART) {
Eric Andersen730f8262001-12-17 23:13:08 +0000704 shutdown_system();
Eric Andersen5222d312002-07-03 05:15:23 +0000705
706 /* unblock all signals, blocked in shutdown_system() */
707 sigemptyset(&unblock_signals);
708 sigaddset(&unblock_signals, SIGHUP);
Mike Frysingera77b4f32005-04-16 08:21:34 +0000709 sigaddset(&unblock_signals, SIGQUIT);
Eric Andersen5222d312002-07-03 05:15:23 +0000710 sigaddset(&unblock_signals, SIGCHLD);
711 sigaddset(&unblock_signals, SIGUSR1);
712 sigaddset(&unblock_signals, SIGUSR2);
713 sigaddset(&unblock_signals, SIGINT);
714 sigaddset(&unblock_signals, SIGTERM);
715 sigaddset(&unblock_signals, SIGCONT);
716 sigaddset(&unblock_signals, SIGSTOP);
717 sigaddset(&unblock_signals, SIGTSTP);
718 sigprocmask(SIG_UNBLOCK, &unblock_signals, NULL);
719
Eric Andersen9cdef5d2003-07-29 06:33:12 +0000720 /* Close whatever files are open. */
721 close(0);
722 close(1);
723 close(2);
724
Eric Andersen3c8064f2003-07-05 08:29:01 +0000725 /* Open the new terminal device */
Bernhard Reutner-Fischer0da069d2006-05-29 12:54:16 +0000726 open_new_terminal(a->terminal, 0);
Eric Andersen3c8064f2003-07-05 08:29:01 +0000727
728 /* Make sure the terminal will act fairly normal for us */
Bernhard Reutner-Fischer49e60b92006-05-29 12:57:52 +0000729 set_term();
Eric Andersen3c8064f2003-07-05 08:29:01 +0000730 /* Setup stdout, stderr on the supplied terminal */
731 dup(0);
732 dup(0);
733
Glenn L McGrathb4a1baa2003-01-13 22:09:50 +0000734 messageD(CONSOLE | LOG, "Trying to re-exec %s", a->command);
Eric Andersen0298be82002-03-05 15:12:19 +0000735 execl(a->command, a->command, NULL);
Glenn L McGrathbaf55a82002-08-22 18:22:10 +0000736
Glenn L McGrathb4a1baa2003-01-13 22:09:50 +0000737 message(CONSOLE | LOG, "exec of '%s' failed: %m",
Eric Andersen71ae64b2002-10-10 04:20:21 +0000738 a->command);
Eric Andersen730f8262001-12-17 23:13:08 +0000739 sync();
740 sleep(2);
741 init_reboot(RB_HALT_SYSTEM);
742 loop_forever();
743 }
744 }
745}
746
Paul Foxd112f8f2006-06-01 13:17:49 +0000747static void shutdown_signal(int sig)
Eric Andersencc8ed391999-10-05 16:24:54 +0000748{
Paul Foxd112f8f2006-06-01 13:17:49 +0000749 char *m;
750 int rb;
751
Erik Andersene49d5ec2000-02-08 19:58:47 +0000752 shutdown_system();
Paul Foxd112f8f2006-06-01 13:17:49 +0000753
754 if (sig == SIGTERM) {
755 m = "reboot";
756 rb = RB_AUTOBOOT;
757 } else if (sig == SIGUSR2) {
758 m = "poweroff";
759 rb = RB_POWER_OFF;
760 } else {
761 m = "halt";
762 rb = RB_HALT_SYSTEM;
763 }
764 message(CONSOLE | LOG, "Requesting system %s.", m);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000765 sync();
Erik Andersen3fe39dc2000-01-25 18:13:53 +0000766
Erik Andersene49d5ec2000-02-08 19:58:47 +0000767 /* allow time for last message to reach serial console */
Erik Andersene132f4b2000-02-09 04:16:43 +0000768 sleep(2);
Erik Andersen3fe39dc2000-01-25 18:13:53 +0000769
Paul Foxd112f8f2006-06-01 13:17:49 +0000770 init_reboot(rb);
Matt Kraai67a46402001-06-03 05:55:52 +0000771
772 loop_forever();
Eric Andersencc8ed391999-10-05 16:24:54 +0000773}
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000774
"Vladimir N. Oleynik"1f04c9d2006-02-01 14:47:52 +0000775static void ctrlaltdel_signal(int sig ATTRIBUTE_UNUSED)
Eric Andersenc97ec342001-04-03 18:01:51 +0000776{
777 run_actions(CTRLALTDEL);
778}
779
Eric Andersen0298be82002-03-05 15:12:19 +0000780/* The SIGSTOP & SIGTSTP handler */
"Vladimir N. Oleynik"1f04c9d2006-02-01 14:47:52 +0000781static void stop_handler(int sig ATTRIBUTE_UNUSED)
Eric Andersened8a9be2001-11-30 19:10:58 +0000782{
Eric Andersen0298be82002-03-05 15:12:19 +0000783 int saved_errno = errno;
Eric Andersened8a9be2001-11-30 19:10:58 +0000784
785 got_cont = 0;
Glenn L McGrathbaf55a82002-08-22 18:22:10 +0000786 while (!got_cont)
787 pause();
Eric Andersened8a9be2001-11-30 19:10:58 +0000788 got_cont = 0;
789 errno = saved_errno;
790}
791
Glenn L McGrathbaf55a82002-08-22 18:22:10 +0000792/* The SIGCONT handler */
"Vladimir N. Oleynik"1f04c9d2006-02-01 14:47:52 +0000793static void cont_handler(int sig ATTRIBUTE_UNUSED)
Eric Andersened8a9be2001-11-30 19:10:58 +0000794{
795 got_cont = 1;
796}
797
Bernhard Reutner-Fischer35e1a072006-05-29 13:08:35 +0000798#endif /* ! ENABLE_DEBUG_INIT */
Erik Andersende552872000-01-23 01:34:05 +0000799
Glenn L McGrathdc4e75e2003-09-02 02:36:18 +0000800static void new_init_action(int action, const char *command, const char *cons)
Erik Andersen7dc16072000-01-04 01:10:25 +0000801{
Eric Andersen22718092004-10-08 08:17:39 +0000802 struct init_action *new_action, *a, *last;
Erik Andersen9e737252000-01-06 01:16:13 +0000803
Erik Andersene49d5ec2000-02-08 19:58:47 +0000804 if (*cons == '\0')
805 cons = console;
Erik Andersen9e737252000-01-06 01:16:13 +0000806
"Vladimir N. Oleynik"6c35c7c2005-10-12 15:34:25 +0000807 if (strcmp(cons, bb_dev_null) == 0 && (action & ASKFIRST))
Eric Andersen1644db92001-09-05 20:18:15 +0000808 return;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000809
Eric Andersen0298be82002-03-05 15:12:19 +0000810 new_action = calloc((size_t) (1), sizeof(struct init_action));
811 if (!new_action) {
Glenn L McGrathb4a1baa2003-01-13 22:09:50 +0000812 message(LOG | CONSOLE, "Memory allocation failure");
Matt Kraai67a46402001-06-03 05:55:52 +0000813 loop_forever();
Erik Andersene49d5ec2000-02-08 19:58:47 +0000814 }
Eric Andersen0298be82002-03-05 15:12:19 +0000815
816 /* Append to the end of the list */
Eric Andersen22718092004-10-08 08:17:39 +0000817 for (a = last = init_action_list; a; a = a->next) {
Eric Andersen82baf632004-10-08 08:21:54 +0000818 /* don't enter action if it's already in the list,
819 * but do overwrite existing actions */
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000820 if ((strcmp(a->command, command) == 0) &&
Eric Andersen6fd0e312003-07-22 09:48:56 +0000821 (strcmp(a->terminal, cons) ==0)) {
Eric Andersen82baf632004-10-08 08:21:54 +0000822 a->action = action;
Eric Andersen6fd0e312003-07-22 09:48:56 +0000823 free(new_action);
824 return;
825 }
Eric Andersen22718092004-10-08 08:17:39 +0000826 last = a;
Eric Andersen6fd0e312003-07-22 09:48:56 +0000827 }
Eric Andersen22718092004-10-08 08:17:39 +0000828 if (last) {
829 last->next = new_action;
Eric Andersen1644db92001-09-05 20:18:15 +0000830 } else {
Eric Andersen0298be82002-03-05 15:12:19 +0000831 init_action_list = new_action;
Eric Andersen1644db92001-09-05 20:18:15 +0000832 }
Eric Andersen0826b6b2002-07-03 23:50:16 +0000833 strcpy(new_action->command, command);
Eric Andersen0298be82002-03-05 15:12:19 +0000834 new_action->action = action;
Eric Andersen0826b6b2002-07-03 23:50:16 +0000835 strcpy(new_action->terminal, cons);
Glenn L McGrathb4a1baa2003-01-13 22:09:50 +0000836 messageD(LOG|CONSOLE, "command='%s' action='%d' terminal='%s'\n",
837 new_action->command, new_action->action, new_action->terminal);
Erik Andersen7dc16072000-01-04 01:10:25 +0000838}
839
Glenn L McGrathbaf55a82002-08-22 18:22:10 +0000840static void delete_init_action(struct init_action *action)
Erik Andersen7dc16072000-01-04 01:10:25 +0000841{
Eric Andersen0298be82002-03-05 15:12:19 +0000842 struct init_action *a, *b = NULL;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000843
Eric Andersen0298be82002-03-05 15:12:19 +0000844 for (a = init_action_list; a; b = a, a = a->next) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000845 if (a == action) {
846 if (b == NULL) {
Eric Andersen0298be82002-03-05 15:12:19 +0000847 init_action_list = a->next;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000848 } else {
Eric Andersen0298be82002-03-05 15:12:19 +0000849 b->next = a->next;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000850 }
851 free(a);
852 break;
853 }
Erik Andersen7dc16072000-01-04 01:10:25 +0000854 }
Erik Andersen7dc16072000-01-04 01:10:25 +0000855}
856
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000857/* NOTE that if CONFIG_FEATURE_USE_INITTAB is NOT defined,
Erik Andersen9e737252000-01-06 01:16:13 +0000858 * then parse_inittab() simply adds in some default
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000859 * actions(i.e., runs INIT_SCRIPT and then starts a pair
860 * of "askfirst" shells). If CONFIG_FEATURE_USE_INITTAB
861 * _is_ defined, but /etc/inittab is missing, this
Erik Andersen812d4662000-01-07 18:30:40 +0000862 * results in the same set of default behaviors.
Glenn L McGrathbaf55a82002-08-22 18:22:10 +0000863 */
Eric Andersen3e6ff902001-03-09 21:24:12 +0000864static void parse_inittab(void)
Erik Andersen7dc16072000-01-04 01:10:25 +0000865{
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000866#ifdef CONFIG_FEATURE_USE_INITTAB
Erik Andersene49d5ec2000-02-08 19:58:47 +0000867 FILE *file;
Glenn L McGrathb4a1baa2003-01-13 22:09:50 +0000868 char buf[INIT_BUFFS_SIZE], lineAsRead[INIT_BUFFS_SIZE];
869 char tmpConsole[CONSOLE_BUFF_SIZE];
Eric Andersen0298be82002-03-05 15:12:19 +0000870 char *id, *runlev, *action, *command, *eol;
871 const struct init_action_type *a = actions;
Erik Andersen7dc16072000-01-04 01:10:25 +0000872
873
Erik Andersene49d5ec2000-02-08 19:58:47 +0000874 file = fopen(INITTAB, "r");
875 if (file == NULL) {
876 /* No inittab file -- set up some default behavior */
Erik Andersen9e737252000-01-06 01:16:13 +0000877#endif
Eric Andersenc97ec342001-04-03 18:01:51 +0000878 /* Reboot on Ctrl-Alt-Del */
Glenn L McGrathb4a1baa2003-01-13 22:09:50 +0000879 new_init_action(CTRLALTDEL, "/sbin/reboot", "");
Eric Andersen1644db92001-09-05 20:18:15 +0000880 /* Umount all filesystems on halt/reboot */
Glenn L McGrathb4a1baa2003-01-13 22:09:50 +0000881 new_init_action(SHUTDOWN, "/bin/umount -a -r", "");
Eric Andersen1644db92001-09-05 20:18:15 +0000882 /* Swapoff on halt/reboot */
Rob Landley7a8f6792005-08-30 18:17:05 +0000883 if(ENABLE_SWAPONOFF) new_init_action(SHUTDOWN, "/sbin/swapoff -a", "");
Eric Andersen730f8262001-12-17 23:13:08 +0000884 /* Prepare to restart init when a HUP is received */
Glenn L McGrathb4a1baa2003-01-13 22:09:50 +0000885 new_init_action(RESTART, "/sbin/init", "");
Eric Andersen3bc2b202002-07-29 06:39:58 +0000886 /* Askfirst shell on tty1-4 */
Glenn L McGrathdc4e75e2003-09-02 02:36:18 +0000887 new_init_action(ASKFIRST, bb_default_login_shell, "");
888 new_init_action(ASKFIRST, bb_default_login_shell, VC_2);
889 new_init_action(ASKFIRST, bb_default_login_shell, VC_3);
890 new_init_action(ASKFIRST, bb_default_login_shell, VC_4);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000891 /* sysinit */
Glenn L McGrathb4a1baa2003-01-13 22:09:50 +0000892 new_init_action(SYSINIT, INIT_SCRIPT, "");
Erik Andersen7dc16072000-01-04 01:10:25 +0000893
Erik Andersene49d5ec2000-02-08 19:58:47 +0000894 return;
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000895#ifdef CONFIG_FEATURE_USE_INITTAB
Erik Andersen9e737252000-01-06 01:16:13 +0000896 }
Erik Andersen7dc16072000-01-04 01:10:25 +0000897
Eric Andersen0826b6b2002-07-03 23:50:16 +0000898 while (fgets(buf, INIT_BUFFS_SIZE, file) != NULL) {
Eric Andersenb5966362000-05-31 20:04:38 +0000899 /* Skip leading spaces */
900 for (id = buf; *id == ' ' || *id == '\t'; id++);
901
902 /* Skip the line if it's a comment */
903 if (*id == '#' || *id == '\n')
Erik Andersene49d5ec2000-02-08 19:58:47 +0000904 continue;
Erik Andersen7dc16072000-01-04 01:10:25 +0000905
Erik Andersene49d5ec2000-02-08 19:58:47 +0000906 /* Trim the trailing \n */
Eric Andersenb5966362000-05-31 20:04:38 +0000907 eol = strrchr(id, '\n');
908 if (eol != NULL)
909 *eol = '\0';
Erik Andersen7dc16072000-01-04 01:10:25 +0000910
Erik Andersene49d5ec2000-02-08 19:58:47 +0000911 /* Keep a copy around for posterity's sake (and error msgs) */
912 strcpy(lineAsRead, buf);
913
Eric Andersenb5966362000-05-31 20:04:38 +0000914 /* Separate the ID field from the runlevels */
915 runlev = strchr(id, ':');
916 if (runlev == NULL || *(runlev + 1) == '\0') {
Glenn L McGrathb4a1baa2003-01-13 22:09:50 +0000917 message(LOG | CONSOLE, "Bad inittab entry: %s", lineAsRead);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000918 continue;
919 } else {
Eric Andersenb5966362000-05-31 20:04:38 +0000920 *runlev = '\0';
921 ++runlev;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000922 }
923
Eric Andersenb5966362000-05-31 20:04:38 +0000924 /* Separate the runlevels from the action */
925 action = strchr(runlev, ':');
926 if (action == NULL || *(action + 1) == '\0') {
Glenn L McGrathb4a1baa2003-01-13 22:09:50 +0000927 message(LOG | CONSOLE, "Bad inittab entry: %s", lineAsRead);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000928 continue;
929 } else {
Eric Andersenb5966362000-05-31 20:04:38 +0000930 *action = '\0';
931 ++action;
932 }
933
Eric Andersen0298be82002-03-05 15:12:19 +0000934 /* Separate the action from the command */
935 command = strchr(action, ':');
936 if (command == NULL || *(command + 1) == '\0') {
Glenn L McGrathb4a1baa2003-01-13 22:09:50 +0000937 message(LOG | CONSOLE, "Bad inittab entry: %s", lineAsRead);
Eric Andersenb5966362000-05-31 20:04:38 +0000938 continue;
939 } else {
Eric Andersen0298be82002-03-05 15:12:19 +0000940 *command = '\0';
941 ++command;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000942 }
943
944 /* Ok, now process it */
Glenn L McGrathb4a1baa2003-01-13 22:09:50 +0000945 for (a = actions; a->name != 0; a++) {
Eric Andersenb5966362000-05-31 20:04:38 +0000946 if (strcmp(a->name, action) == 0) {
947 if (*id != '\0') {
Glenn L McGrathb4a1baa2003-01-13 22:09:50 +0000948 if(strncmp(id, "/dev/", 5) == 0)
949 id += 5;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000950 strcpy(tmpConsole, "/dev/");
Glenn L McGrathb4a1baa2003-01-13 22:09:50 +0000951 safe_strncpy(tmpConsole + 5, id,
952 CONSOLE_BUFF_SIZE - 5);
Eric Andersenb5966362000-05-31 20:04:38 +0000953 id = tmpConsole;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000954 }
Eric Andersen0298be82002-03-05 15:12:19 +0000955 new_init_action(a->action, command, id);
Glenn L McGrathb4a1baa2003-01-13 22:09:50 +0000956 break;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000957 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000958 }
Glenn L McGrathb4a1baa2003-01-13 22:09:50 +0000959 if (a->name == 0) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000960 /* Choke on an unknown action */
Glenn L McGrathb4a1baa2003-01-13 22:09:50 +0000961 message(LOG | CONSOLE, "Bad inittab entry: %s", lineAsRead);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000962 }
Erik Andersen7dc16072000-01-04 01:10:25 +0000963 }
Eric Andersend8636ca2002-05-15 22:19:09 +0000964 fclose(file);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000965 return;
Glenn L McGrathbaf55a82002-08-22 18:22:10 +0000966#endif /* CONFIG_FEATURE_USE_INITTAB */
Erik Andersen7dc16072000-01-04 01:10:25 +0000967}
968
Eric Andersen82baf632004-10-08 08:21:54 +0000969#ifdef CONFIG_FEATURE_USE_INITTAB
"Vladimir N. Oleynik"1f04c9d2006-02-01 14:47:52 +0000970static void reload_signal(int sig ATTRIBUTE_UNUSED)
Eric Andersen6fd0e312003-07-22 09:48:56 +0000971{
Eric Andersen82baf632004-10-08 08:21:54 +0000972 struct init_action *a, *tmp;
973
974 message(LOG, "Reloading /etc/inittab");
975
976 /* disable old entrys */
977 for (a = init_action_list; a; a = a->next ) {
978 a->action = ONCE;
979 }
980
981 parse_inittab();
982
983 /* remove unused entrys */
984 for (a = init_action_list; a; a = tmp) {
985 tmp = a->next;
986 if (a->action & (ONCE | SYSINIT | WAIT ) &&
987 a->pid == 0 ) {
988 delete_init_action(a);
989 }
990 }
Eric Andersen6fd0e312003-07-22 09:48:56 +0000991 run_actions(RESPAWN);
Eric Andersen82baf632004-10-08 08:21:54 +0000992 return;
Eric Andersen6fd0e312003-07-22 09:48:56 +0000993}
Eric Andersen82baf632004-10-08 08:21:54 +0000994#endif /* CONFIG_FEATURE_USE_INITTAB */
995
Rob Landleydfba7412006-03-06 20:47:33 +0000996int init_main(int argc, char **argv)
Eric Andersen0460ff21999-10-25 23:32:44 +0000997{
Eric Andersen0298be82002-03-05 15:12:19 +0000998 struct init_action *a;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000999 pid_t wpid;
Eric Andersen0460ff21999-10-25 23:32:44 +00001000
Eric Andersen730f8262001-12-17 23:13:08 +00001001 if (argc > 1 && !strcmp(argv[1], "-q")) {
Rob Landley2edf5262006-01-22 02:41:51 +00001002 return kill(1,SIGHUP);
Eric Andersen730f8262001-12-17 23:13:08 +00001003 }
Bernhard Reutner-Fischer35e1a072006-05-29 13:08:35 +00001004#if !ENABLE_DEBUG_INIT
Erik Andersena51ecdd2000-02-24 18:09:58 +00001005 /* Expect to be invoked as init with PID=1 or be invoked as linuxrc */
Rob Landley64612912006-01-30 08:31:37 +00001006 if (getpid() != 1 &&
1007 (!ENABLE_FEATURE_INITRD || !strstr(bb_applet_name, "linuxrc")))
1008 {
Manuel Novoa III cad53642003-03-19 09:13:01 +00001009 bb_show_usage();
Erik Andersene49d5ec2000-02-08 19:58:47 +00001010 }
Erik Andersene49d5ec2000-02-08 19:58:47 +00001011 /* Set up sig handlers -- be sure to
1012 * clear all of these in run() */
Glenn L McGrathbaf55a82002-08-22 18:22:10 +00001013 signal(SIGHUP, exec_signal);
Mike Frysingera77b4f32005-04-16 08:21:34 +00001014 signal(SIGQUIT, exec_signal);
Paul Foxd112f8f2006-06-01 13:17:49 +00001015 signal(SIGUSR1, shutdown_signal);
1016 signal(SIGUSR2, shutdown_signal);
Glenn L McGrathbaf55a82002-08-22 18:22:10 +00001017 signal(SIGINT, ctrlaltdel_signal);
Paul Foxd112f8f2006-06-01 13:17:49 +00001018 signal(SIGTERM, shutdown_signal);
Eric Andersened8a9be2001-11-30 19:10:58 +00001019 signal(SIGCONT, cont_handler);
1020 signal(SIGSTOP, stop_handler);
1021 signal(SIGTSTP, stop_handler);
Eric Andersen0460ff21999-10-25 23:32:44 +00001022
Eric Andersenc7bda1c2004-03-15 08:29:22 +00001023 /* Turn off rebooting via CTL-ALT-DEL -- we get a
Erik Andersene49d5ec2000-02-08 19:58:47 +00001024 * SIGINT on CAD so we can shut things down gracefully... */
Erik Andersen4f3f7572000-04-28 00:18:56 +00001025 init_reboot(RB_DISABLE_CAD);
Erik Andersene49d5ec2000-02-08 19:58:47 +00001026#endif
Erik Andersen05df2392000-01-13 04:43:48 +00001027
Eric Andersen599e3ce2002-07-03 11:08:10 +00001028 /* Figure out where the default console should be */
1029 console_init();
1030
Erik Andersene49d5ec2000-02-08 19:58:47 +00001031 /* Close whatever files are open, and reset the console. */
1032 close(0);
1033 close(1);
1034 close(2);
Eric Andersen72f9a422001-10-28 05:12:20 +00001035
Glenn L McGrathbaf55a82002-08-22 18:22:10 +00001036 if (device_open(console, O_RDWR | O_NOCTTY) == 0) {
Bernhard Reutner-Fischer49e60b92006-05-29 12:57:52 +00001037 set_term();
Eric Andersen599e3ce2002-07-03 11:08:10 +00001038 close(0);
1039 }
1040
Erik Andersen983b51b2000-04-04 18:14:25 +00001041 chdir("/");
Erik Andersene49d5ec2000-02-08 19:58:47 +00001042 setsid();
Glenn L McGrathb4a1baa2003-01-13 22:09:50 +00001043 {
1044 const char * const *e;
1045 /* Make sure environs is set to something sane */
1046 for(e = environment; *e; e++)
Manuel Novoa III cad53642003-03-19 09:13:01 +00001047 putenv((char *) *e);
Glenn L McGrathb4a1baa2003-01-13 22:09:50 +00001048 }
Erik Andersene49d5ec2000-02-08 19:58:47 +00001049 /* Hello world */
Manuel Novoa III cad53642003-03-19 09:13:01 +00001050 message(MAYBE_CONSOLE | LOG, "init started: %s", bb_msg_full_version);
Eric Andersencc8ed391999-10-05 16:24:54 +00001051
Erik Andersene49d5ec2000-02-08 19:58:47 +00001052 /* Make sure there is enough memory to do something useful. */
Rob Landleyc3386a42005-08-30 18:50:37 +00001053 if (ENABLE_SWAPONOFF) {
1054 struct sysinfo info;
1055
1056 if (!sysinfo(&info) &&
1057 (info.mem_unit ? : 1) * (long long)info.totalram < MEGABYTE)
1058 {
1059 message(CONSOLE,"Low memory: forcing swapon.");
1060 /* swapon -a requires /proc typically */
1061 new_init_action(SYSINIT, "/bin/mount -t proc proc /proc", "");
1062 /* Try to turn on swap */
1063 new_init_action(SYSINIT, "/sbin/swapon -a", "");
1064 run_actions(SYSINIT); /* wait and removing */
1065 }
1066 }
Erik Andersen9e737252000-01-06 01:16:13 +00001067
Erik Andersene49d5ec2000-02-08 19:58:47 +00001068 /* Check if we are supposed to be in single user mode */
1069 if (argc > 1 && (!strcmp(argv[1], "single") ||
1070 !strcmp(argv[1], "-s") || !strcmp(argv[1], "1"))) {
Glenn L McGrathb4a1baa2003-01-13 22:09:50 +00001071 /* Start a shell on console */
Glenn L McGrathdc4e75e2003-09-02 02:36:18 +00001072 new_init_action(RESPAWN, bb_default_login_shell, "");
Erik Andersene49d5ec2000-02-08 19:58:47 +00001073 } else {
1074 /* Not in single user mode -- see what inittab says */
Eric Andersen8a8fbb81999-10-26 00:18:56 +00001075
Eric Andersenbdfd0d72001-10-24 05:00:29 +00001076 /* NOTE that if CONFIG_FEATURE_USE_INITTAB is NOT defined,
Erik Andersene49d5ec2000-02-08 19:58:47 +00001077 * then parse_inittab() simply adds in some default
Eric Andersenc7bda1c2004-03-15 08:29:22 +00001078 * actions(i.e., runs INIT_SCRIPT and then starts a pair
Erik Andersene49d5ec2000-02-08 19:58:47 +00001079 * of "askfirst" shells */
1080 parse_inittab();
Eric Andersend00c2621999-12-07 08:37:31 +00001081 }
Erik Andersen983b51b2000-04-04 18:14:25 +00001082
Rob Landleyb3ede5a2006-03-27 23:09:12 +00001083#ifdef CONFIG_SELINUX
1084 if (getenv("SELINUX_INIT") == NULL) {
1085 int enforce = 0;
1086
1087 putenv("SELINUX_INIT=YES");
1088 if (selinux_init_load_policy(&enforce) == 0) {
1089 execv(argv[0], argv);
1090 } else if (enforce > 0) {
1091 /* SELinux in enforcing mode but load_policy failed */
1092 /* At this point, we probably can't open /dev/console, so log() won't work */
1093 message(CONSOLE,"Unable to load SELinux Policy. Machine is in enforcing mode. Halting now.");
1094 exit(1);
1095 }
1096 }
1097#endif /* CONFIG_SELINUX */
1098
Eric Andersen7ef1a5b2001-03-20 17:39:08 +00001099 /* Make the command line just say "init" -- thats all, nothing else */
1100 fixup_argv(argc, argv, "init");
Eric Andersen219d6f51999-11-02 19:43:01 +00001101
Erik Andersene49d5ec2000-02-08 19:58:47 +00001102 /* Now run everything that needs to be run */
1103
1104 /* First run the sysinit command */
Eric Andersen1644db92001-09-05 20:18:15 +00001105 run_actions(SYSINIT);
Eric Andersen0298be82002-03-05 15:12:19 +00001106
Erik Andersene49d5ec2000-02-08 19:58:47 +00001107 /* Next run anything that wants to block */
Eric Andersen1644db92001-09-05 20:18:15 +00001108 run_actions(WAIT);
Eric Andersen0298be82002-03-05 15:12:19 +00001109
Erik Andersene49d5ec2000-02-08 19:58:47 +00001110 /* Next run anything to be run only once */
Eric Andersen0298be82002-03-05 15:12:19 +00001111 run_actions(ONCE);
1112
Eric Andersen82baf632004-10-08 08:21:54 +00001113#ifdef CONFIG_FEATURE_USE_INITTAB
Eric Andersen6fd0e312003-07-22 09:48:56 +00001114 /* Redefine SIGHUP to reread /etc/inittab */
1115 signal(SIGHUP, reload_signal);
Eric Andersen82baf632004-10-08 08:21:54 +00001116#else
1117 signal(SIGHUP, SIG_IGN);
1118#endif /* CONFIG_FEATURE_USE_INITTAB */
1119
Eric Andersen6fd0e312003-07-22 09:48:56 +00001120
Erik Andersene49d5ec2000-02-08 19:58:47 +00001121 /* Now run the looping stuff for the rest of forever */
1122 while (1) {
Eric Andersen0298be82002-03-05 15:12:19 +00001123 /* run the respawn stuff */
1124 run_actions(RESPAWN);
1125
1126 /* run the askfirst stuff */
1127 run_actions(ASKFIRST);
1128
1129 /* Don't consume all CPU time -- sleep a bit */
1130 sleep(1);
1131
Erik Andersene49d5ec2000-02-08 19:58:47 +00001132 /* Wait for a child process to exit */
Bernhard Reutner-Fischerc58dbf22006-05-30 12:16:54 +00001133 wpid = wait(NULL);
Eric Andersen87812dc2004-04-12 19:21:54 +00001134 while (wpid > 0) {
Erik Andersene49d5ec2000-02-08 19:58:47 +00001135 /* Find out who died and clean up their corpse */
Eric Andersen0298be82002-03-05 15:12:19 +00001136 for (a = init_action_list; a; a = a->next) {
Erik Andersene49d5ec2000-02-08 19:58:47 +00001137 if (a->pid == wpid) {
Eric Andersen0298be82002-03-05 15:12:19 +00001138 /* Set the pid to 0 so that the process gets
1139 * restarted by run_actions() */
Erik Andersene49d5ec2000-02-08 19:58:47 +00001140 a->pid = 0;
Eric Andersen0298be82002-03-05 15:12:19 +00001141 message(LOG, "Process '%s' (pid %d) exited. "
Glenn L McGrathb4a1baa2003-01-13 22:09:50 +00001142 "Scheduling it for restart.",
1143 a->command, wpid);
Erik Andersene49d5ec2000-02-08 19:58:47 +00001144 }
1145 }
Eric Andersencf1fee02002-12-17 09:48:16 +00001146 /* see if anyone else is waiting to be reaped */
Bernhard Reutner-Fischerc58dbf22006-05-30 12:16:54 +00001147 wpid = waitpid (-1, NULL, WNOHANG);
Erik Andersene49d5ec2000-02-08 19:58:47 +00001148 }
Erik Andersene49d5ec2000-02-08 19:58:47 +00001149 }
Eric Andersencc8ed391999-10-05 16:24:54 +00001150}