blob: ec1bacc83ea80c888245928b10352de241fe73f0 [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 Andersen1d1d2f92002-04-13 08:31:59 +00006 * Copyright (C) 1999-2002 by Erik Andersen <andersee@debian.org>
Eric Andersenc4996011999-10-20 22:08:37 +00007 * Adjusted by so many folks, it's impossible to keep track.
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 *
23 */
24
Erik Andersene132f4b2000-02-09 04:16:43 +000025/* Turn this on to disable all the dangerous
26 rebooting stuff when debugging.
27#define DEBUG_INIT
28*/
29
Erik Andersen4f3f7572000-04-28 00:18:56 +000030#include <stdio.h>
31#include <stdlib.h>
Eric Andersencc8ed391999-10-05 16:24:54 +000032#include <errno.h>
Eric Andersenb186d981999-12-03 09:19:54 +000033#include <paths.h>
Erik Andersen42094cd2000-03-20 21:34:52 +000034#include <signal.h>
35#include <stdarg.h>
Eric Andersencc8ed391999-10-05 16:24:54 +000036#include <string.h>
Erik Andersen4f3f7572000-04-28 00:18:56 +000037#include <termios.h>
38#include <unistd.h>
Eric Andersened3ef502001-01-27 08:24:39 +000039#include <limits.h>
Erik Andersen42094cd2000-03-20 21:34:52 +000040#include <sys/fcntl.h>
41#include <sys/ioctl.h>
Eric Andersencc8ed391999-10-05 16:24:54 +000042#include <sys/mount.h>
Erik Andersen42094cd2000-03-20 21:34:52 +000043#include <sys/types.h>
Erik Andersen42094cd2000-03-20 21:34:52 +000044#include <sys/wait.h>
Eric Andersencbe31da2001-02-20 06:14:08 +000045#include "busybox.h"
Eric Andersenbdfd0d72001-10-24 05:00:29 +000046#ifdef CONFIG_SYSLOGD
Erik Andersen4f3f7572000-04-28 00:18:56 +000047# include <sys/syslog.h>
48#endif
49
Eric Andersen0298be82002-03-05 15:12:19 +000050#if defined(__UCLIBC__) && !defined(__UCLIBC_HAS_MMU__)
51#define fork vfork
52#endif
Pavel Roskin9c5fcc32000-07-17 23:45:12 +000053
Eric Andersen0826b6b2002-07-03 23:50:16 +000054#define INIT_BUFFS_SIZE 256
55
Eric Andersenbd22ed82000-07-08 18:55:24 +000056/* From <linux/vt.h> */
57struct vt_stat {
Glenn L McGrathbaf55a82002-08-22 18:22:10 +000058 unsigned short v_active; /* active vt */
59 unsigned short v_signal; /* signal to send */
60 unsigned short v_state; /* vt bitmask */
Eric Andersenbd22ed82000-07-08 18:55:24 +000061};
Glenn L McGrathbaf55a82002-08-22 18:22:10 +000062static const int VT_GETSTATE = 0x5603; /* get global vt state info */
Eric Andersenbd22ed82000-07-08 18:55:24 +000063
64/* From <linux/serial.h> */
65struct serial_struct {
Glenn L McGrathbaf55a82002-08-22 18:22:10 +000066 int type;
67 int line;
68 int port;
69 int irq;
70 int flags;
71 int xmit_fifo_size;
72 int custom_divisor;
73 int baud_base;
74 unsigned short close_delay;
75 char reserved_char[2];
76 int hub6;
77 unsigned short closing_wait; /* time to wait before closing */
78 unsigned short closing_wait2; /* no longer used... */
79 int reserved[4];
Eric Andersenbd22ed82000-07-08 18:55:24 +000080};
81
82
Glenn L McGrathbaf55a82002-08-22 18:22:10 +000083#if (__GNU_LIBRARY__ > 5) || defined(__dietlibc__)
84#include <sys/reboot.h>
85#define init_reboot(magic) reboot(magic)
Eric Andersen72f9a422001-10-28 05:12:20 +000086#else
Glenn L McGrathbaf55a82002-08-22 18:22:10 +000087#define init_reboot(magic) reboot(0xfee1dead, 672274793, magic)
Eric Andersen72f9a422001-10-28 05:12:20 +000088#endif
Erik Andersen4f3f7572000-04-28 00:18:56 +000089
Eric Andersen41492d62001-02-23 00:05:56 +000090#ifndef _PATH_STDPATH
Erik Andersen4f3f7572000-04-28 00:18:56 +000091#define _PATH_STDPATH "/usr/bin:/bin:/usr/sbin:/sbin"
Eric Andersen41492d62001-02-23 00:05:56 +000092#endif
Eric Andersencc8ed391999-10-05 16:24:54 +000093
Eric Andersenbdfd0d72001-10-24 05:00:29 +000094#if defined CONFIG_FEATURE_INIT_COREDUMPS
Erik Andersen983b51b2000-04-04 18:14:25 +000095/*
Erik Andersen183da4a2000-04-04 18:36:37 +000096 * When a file named CORE_ENABLE_FLAG_FILE exists, setrlimit is called
97 * before processes are spawned to set core file size as unlimited.
98 * This is for debugging only. Don't use this is production, unless
99 * you want core dumps lying about....
Erik Andersen983b51b2000-04-04 18:14:25 +0000100 */
101#define CORE_ENABLE_FLAG_FILE "/.init_enable_core"
102#include <sys/resource.h>
103#include <sys/time.h>
Erik Andersen183da4a2000-04-04 18:36:37 +0000104#endif
Erik Andersen983b51b2000-04-04 18:14:25 +0000105
Erik Andersen4d1d0111999-12-17 18:44:15 +0000106#define KERNEL_VERSION(a,b,c) (((a) << 16) + ((b) << 8) + (c))
Erik Andersen4d1d0111999-12-17 18:44:15 +0000107
Eric Andersenb6b519b2001-04-09 23:52:18 +0000108#if __GNU_LIBRARY__ > 5
Glenn L McGrathbaf55a82002-08-22 18:22:10 +0000109#include <sys/kdaemon.h>
Erik Andersen4f3f7572000-04-28 00:18:56 +0000110#else
Glenn L McGrathbaf55a82002-08-22 18:22:10 +0000111extern int bdflush(int func, long int data);
Eric Andersenb6b519b2001-04-09 23:52:18 +0000112#endif
Erik Andersen4f3f7572000-04-28 00:18:56 +0000113
Glenn L McGrathbaf55a82002-08-22 18:22:10 +0000114#define SHELL "/bin/sh" /* Default shell */
115#define LOGIN_SHELL "-" SHELL /* Default login shell */
116#define INITTAB "/etc/inittab" /* inittab file location */
Erik Andersen96e2abd2000-01-07 11:40:44 +0000117#ifndef INIT_SCRIPT
Glenn L McGrathbaf55a82002-08-22 18:22:10 +0000118#define INIT_SCRIPT "/etc/init.d/rcS" /* Default sysinit script. */
Erik Andersen96e2abd2000-01-07 11:40:44 +0000119#endif
Erik Andersen9c88cac1999-12-30 09:25:17 +0000120
Eric Andersen41492d62001-02-23 00:05:56 +0000121#define MAXENV 16 /* Number of env. vars */
Erik Andersen7dc16072000-01-04 01:10:25 +0000122
123/* Allowed init action types */
Eric Andersen0298be82002-03-05 15:12:19 +0000124#define SYSINIT 0x001
125#define RESPAWN 0x002
126#define ASKFIRST 0x004
127#define WAIT 0x008
128#define ONCE 0x010
129#define CTRLALTDEL 0x020
130#define SHUTDOWN 0x040
131#define RESTART 0x080
Erik Andersen7dc16072000-01-04 01:10:25 +0000132
Erik Andersen42094cd2000-03-20 21:34:52 +0000133/* A mapping between "inittab" action name strings and action type codes. */
Eric Andersen0298be82002-03-05 15:12:19 +0000134struct init_action_type {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000135 const char *name;
Eric Andersen0298be82002-03-05 15:12:19 +0000136 int action;
137};
Erik Andersen7dc16072000-01-04 01:10:25 +0000138
Eric Andersen0298be82002-03-05 15:12:19 +0000139static const struct init_action_type actions[] = {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000140 {"sysinit", SYSINIT},
141 {"respawn", RESPAWN},
142 {"askfirst", ASKFIRST},
143 {"wait", WAIT},
144 {"once", ONCE},
Erik Andersene132f4b2000-02-09 04:16:43 +0000145 {"ctrlaltdel", CTRLALTDEL},
Eric Andersenc97ec342001-04-03 18:01:51 +0000146 {"shutdown", SHUTDOWN},
Eric Andersen730f8262001-12-17 23:13:08 +0000147 {"restart", RESTART},
Pavel Roskin9027bcf2000-07-14 15:44:25 +0000148 {0, 0}
Erik Andersen7dc16072000-01-04 01:10:25 +0000149};
150
Eric Andersen0298be82002-03-05 15:12:19 +0000151/* Set up a linked list of init_actions, to be read from inittab */
152struct init_action {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000153 pid_t pid;
Eric Andersen0826b6b2002-07-03 23:50:16 +0000154 char command[INIT_BUFFS_SIZE];
155 char terminal[INIT_BUFFS_SIZE];
Eric Andersen0298be82002-03-05 15:12:19 +0000156 struct init_action *next;
157 int action;
Erik Andersen7dc16072000-01-04 01:10:25 +0000158};
Erik Andersen7dc16072000-01-04 01:10:25 +0000159
Eric Andersen0298be82002-03-05 15:12:19 +0000160/* Static variables */
161static struct init_action *init_action_list = NULL;
Glenn L McGrathbaf55a82002-08-22 18:22:10 +0000162static int kernelVersion = 0;
163static char termType[32] = "TERM=linux";
164static char console[32] = _PATH_CONSOLE;
165
Eric Andersene7078062002-07-29 08:00:16 +0000166#ifndef CONFIG_SYSLOGD
Glenn L McGrathbaf55a82002-08-22 18:22:10 +0000167static char *log = VC_5;
Eric Andersene7078062002-07-29 08:00:16 +0000168#endif
Eric Andersen0298be82002-03-05 15:12:19 +0000169static sig_atomic_t got_cont = 0;
170static const int LOG = 0x1;
171static const int CONSOLE = 0x2;
Glenn L McGrathbaf55a82002-08-22 18:22:10 +0000172
Eric Andersenb0cc0a62002-03-20 14:57:50 +0000173#if defined CONFIG_FEATURE_EXTRA_QUIET
Eric Andersen0298be82002-03-05 15:12:19 +0000174static const int MAYBE_CONSOLE = 0x0;
175#else
176#define MAYBE_CONSOLE CONSOLE
177#endif
178#ifndef RB_HALT_SYSTEM
179static const int RB_HALT_SYSTEM = 0xcdef0123;
180static const int RB_ENABLE_CAD = 0x89abcdef;
181static const int RB_DISABLE_CAD = 0;
Glenn L McGrathbaf55a82002-08-22 18:22:10 +0000182
Eric Andersen0298be82002-03-05 15:12:19 +0000183#define RB_POWER_OFF 0x4321fedc
184static const int RB_AUTOBOOT = 0x01234567;
185#endif
Erik Andersen42094cd2000-03-20 21:34:52 +0000186
Eric Andersen0298be82002-03-05 15:12:19 +0000187/* Function prototypes */
188static void delete_init_action(struct init_action *a);
189static int waitfor(struct init_action *a);
190
Eric Andersen0460ff21999-10-25 23:32:44 +0000191
Eric Andersen74400cc2001-10-18 04:11:39 +0000192static void loop_forever(void)
Matt Kraai67a46402001-06-03 05:55:52 +0000193{
194 while (1)
Glenn L McGrathbaf55a82002-08-22 18:22:10 +0000195 sleep(1);
Matt Kraai67a46402001-06-03 05:55:52 +0000196}
Eric Andersen7ef1a5b2001-03-20 17:39:08 +0000197
Erik Andersen42094cd2000-03-20 21:34:52 +0000198/* Print a message to the specified device.
199 * Device may be bitwise-or'd from LOG | CONSOLE */
Eric Andersenfedce062001-11-17 07:27:14 +0000200#ifdef DEBUG_INIT
Glenn L McGrathbaf55a82002-08-22 18:22:10 +0000201static inline messageND(int device, char *fmt, ...)
202{
203}
204#else
Eric Andersenfedce062001-11-17 07:27:14 +0000205#define messageND message
206#endif
Glenn L McGrathbaf55a82002-08-22 18:22:10 +0000207static void message(int device, char *fmt, ...)
208 __attribute__ ((format(printf, 2, 3)));
Erik Andersen93d65132000-04-06 08:06:36 +0000209static void message(int device, char *fmt, ...)
Eric Andersen0460ff21999-10-25 23:32:44 +0000210{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000211 va_list arguments;
212 int fd;
Erik Andersen7dc16072000-01-04 01:10:25 +0000213
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000214#ifdef CONFIG_SYSLOGD
Eric Andersen4c781471999-11-26 08:12:56 +0000215
Erik Andersene49d5ec2000-02-08 19:58:47 +0000216 /* Log the message to syslogd */
217 if (device & LOG) {
218 char msg[1024];
Eric Andersen4c781471999-11-26 08:12:56 +0000219
Erik Andersene49d5ec2000-02-08 19:58:47 +0000220 va_start(arguments, fmt);
221 vsnprintf(msg, sizeof(msg), fmt, arguments);
222 va_end(arguments);
Eric Andersen0298be82002-03-05 15:12:19 +0000223 syslog_msg(LOG_USER, LOG_INFO, msg);
Eric Andersen3ae0c781999-11-04 01:13:21 +0000224 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000225#else
226 static int log_fd = -1;
227
228 /* Take full control of the log tty, and never close it.
229 * It's mine, all mine! Muhahahaha! */
230 if (log_fd < 0) {
Glenn L McGrathbaf55a82002-08-22 18:22:10 +0000231 if ((log_fd = device_open(log, O_RDWR | O_NDELAY)) < 0) {
Erik Andersene132f4b2000-02-09 04:16:43 +0000232 log_fd = -2;
Eric Andersen72f9a422001-10-28 05:12:20 +0000233 fprintf(stderr, "Bummer, can't write to log on %s!\n", log);
Erik Andersene132f4b2000-02-09 04:16:43 +0000234 device = CONSOLE;
Eric Andersen6a979902002-09-30 20:08:53 +0000235 } else {
236 fcntl(log_fd, F_SETFD, FD_CLOEXEC);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000237 }
238 }
239 if ((device & LOG) && (log_fd >= 0)) {
240 va_start(arguments, fmt);
241 vdprintf(log_fd, fmt, arguments);
242 va_end(arguments);
243 }
Eric Andersen4c781471999-11-26 08:12:56 +0000244#endif
245
Erik Andersene49d5ec2000-02-08 19:58:47 +0000246 if (device & CONSOLE) {
247 /* Always send console messages to /dev/console so people will see them. */
Glenn L McGrathbaf55a82002-08-22 18:22:10 +0000248 if ((fd =
249 device_open(_PATH_CONSOLE,
250 O_WRONLY | O_NOCTTY | O_NDELAY)) >= 0) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000251 va_start(arguments, fmt);
252 vdprintf(fd, fmt, arguments);
253 va_end(arguments);
254 close(fd);
255 } else {
256 fprintf(stderr, "Bummer, can't print: ");
257 va_start(arguments, fmt);
258 vfprintf(stderr, fmt, arguments);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000259 va_end(arguments);
260 }
Eric Andersen3ae0c781999-11-04 01:13:21 +0000261 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000262}
263
Eric Andersen0460ff21999-10-25 23:32:44 +0000264/* Set terminal settings to reasonable defaults */
Eric Andersen3e6ff902001-03-09 21:24:12 +0000265static void set_term(int fd)
Eric Andersencc8ed391999-10-05 16:24:54 +0000266{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000267 struct termios tty;
Eric Andersencc8ed391999-10-05 16:24:54 +0000268
Erik Andersene49d5ec2000-02-08 19:58:47 +0000269 tcgetattr(fd, &tty);
Eric Andersencc8ed391999-10-05 16:24:54 +0000270
Erik Andersene49d5ec2000-02-08 19:58:47 +0000271 /* set control chars */
Glenn L McGrathbaf55a82002-08-22 18:22:10 +0000272 tty.c_cc[VINTR] = 3; /* C-c */
273 tty.c_cc[VQUIT] = 28; /* C-\ */
274 tty.c_cc[VERASE] = 127; /* C-? */
275 tty.c_cc[VKILL] = 21; /* C-u */
276 tty.c_cc[VEOF] = 4; /* C-d */
Eric Andersen3849f9b2000-07-10 19:56:47 +0000277 tty.c_cc[VSTART] = 17; /* C-q */
Glenn L McGrathbaf55a82002-08-22 18:22:10 +0000278 tty.c_cc[VSTOP] = 19; /* C-s */
279 tty.c_cc[VSUSP] = 26; /* C-z */
Eric Andersen02bc25b2000-07-06 21:29:32 +0000280
Erik Andersene49d5ec2000-02-08 19:58:47 +0000281 /* use line dicipline 0 */
282 tty.c_line = 0;
Eric Andersen7f1acfd1999-10-29 23:09:13 +0000283
Erik Andersene49d5ec2000-02-08 19:58:47 +0000284 /* Make it be sane */
Glenn L McGrathbaf55a82002-08-22 18:22:10 +0000285 tty.c_cflag &= CBAUD | CBAUDEX | CSIZE | CSTOPB | PARENB | PARODD;
286 tty.c_cflag |= CREAD | HUPCL | CLOCAL;
Eric Andersend8862922001-04-23 15:14:11 +0000287
Eric Andersen08b10341999-11-19 02:38:58 +0000288
Erik Andersene49d5ec2000-02-08 19:58:47 +0000289 /* input modes */
290 tty.c_iflag = ICRNL | IXON | IXOFF;
Eric Andersen08b10341999-11-19 02:38:58 +0000291
Erik Andersene49d5ec2000-02-08 19:58:47 +0000292 /* output modes */
293 tty.c_oflag = OPOST | ONLCR;
Eric Andersen7f1acfd1999-10-29 23:09:13 +0000294
Erik Andersene49d5ec2000-02-08 19:58:47 +0000295 /* local modes */
296 tty.c_lflag =
297 ISIG | ICANON | ECHO | ECHOE | ECHOK | ECHOCTL | ECHOKE | IEXTEN;
Eric Andersencc8ed391999-10-05 16:24:54 +0000298
Erik Andersene49d5ec2000-02-08 19:58:47 +0000299 tcsetattr(fd, TCSANOW, &tty);
Eric Andersencc8ed391999-10-05 16:24:54 +0000300}
301
Eric Andersenbc5941a2000-12-06 23:17:37 +0000302/* How much memory does this machine have?
303 Units are kBytes to avoid overflow on 4GB machines */
Eric Andersen74400cc2001-10-18 04:11:39 +0000304static int check_free_memory(void)
Eric Andersencc8ed391999-10-05 16:24:54 +0000305{
Erik Andersend07ee462000-02-21 21:26:32 +0000306 struct sysinfo info;
Glenn L McGrathbaf55a82002-08-22 18:22:10 +0000307 unsigned int result, u, s = 10;
Eric Andersencc8ed391999-10-05 16:24:54 +0000308
Erik Andersend07ee462000-02-21 21:26:32 +0000309 if (sysinfo(&info) != 0) {
Eric Andersen53cfb7e2001-01-31 17:29:47 +0000310 perror_msg("Error checking free memory");
Erik Andersene49d5ec2000-02-08 19:58:47 +0000311 return -1;
Eric Andersenabc7d591999-10-19 00:27:50 +0000312 }
Eric Andersenbc5941a2000-12-06 23:17:37 +0000313
314 /* Kernels 2.0.x and 2.2.x return info.mem_unit==0 with values in bytes.
315 * Kernels 2.4.0 return info.mem_unit in bytes. */
316 u = info.mem_unit;
Glenn L McGrathbaf55a82002-08-22 18:22:10 +0000317 if (u == 0)
318 u = 1;
319 while ((u & 1) == 0 && s > 0) {
320 u >>= 1;
321 s--;
322 }
323 result = (info.totalram >> s) + (info.totalswap >> s);
324 result = result * u;
325 if (result < 0)
326 result = INT_MAX;
Eric Andersenbc5941a2000-12-06 23:17:37 +0000327 return result;
Eric Andersencc8ed391999-10-05 16:24:54 +0000328}
329
Eric Andersen74400cc2001-10-18 04:11:39 +0000330static void console_init(void)
Eric Andersen0460ff21999-10-25 23:32:44 +0000331{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000332 int fd;
333 int tried_devcons = 0;
334 int tried_vtprimary = 0;
Erik Andersen029011b2000-03-04 21:19:32 +0000335 struct vt_stat vt;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000336 struct serial_struct sr;
337 char *s;
Eric Andersen0460ff21999-10-25 23:32:44 +0000338
Erik Andersene49d5ec2000-02-08 19:58:47 +0000339 if ((s = getenv("TERM")) != NULL) {
340 snprintf(termType, sizeof(termType) - 1, "TERM=%s", s);
341 }
Erik Andersenac6e71f2000-01-08 22:04:33 +0000342
Erik Andersene49d5ec2000-02-08 19:58:47 +0000343 if ((s = getenv("CONSOLE")) != NULL) {
Matt Kraai18447702001-05-18 21:24:58 +0000344 safe_strncpy(console, s, sizeof(console));
Erik Andersene49d5ec2000-02-08 19:58:47 +0000345 }
Eric Andersenfbb39c81999-11-08 17:00:52 +0000346#if #cpu(sparc)
Erik Andersene49d5ec2000-02-08 19:58:47 +0000347 /* sparc kernel supports console=tty[ab] parameter which is also
348 * passed to init, so catch it here */
349 else if ((s = getenv("console")) != NULL) {
350 /* remap tty[ab] to /dev/ttyS[01] */
351 if (strcmp(s, "ttya") == 0)
Matt Kraai439e3df2001-07-23 14:52:08 +0000352 safe_strncpy(console, SC_0, sizeof(console));
Erik Andersene49d5ec2000-02-08 19:58:47 +0000353 else if (strcmp(s, "ttyb") == 0)
Matt Kraai439e3df2001-07-23 14:52:08 +0000354 safe_strncpy(console, SC_1, sizeof(console));
Erik Andersene49d5ec2000-02-08 19:58:47 +0000355 }
Eric Andersen219d6f51999-11-02 19:43:01 +0000356#endif
Erik Andersene49d5ec2000-02-08 19:58:47 +0000357 else {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000358 /* 2.2 kernels: identify the real console backend and try to use it */
359 if (ioctl(0, TIOCGSERIAL, &sr) == 0) {
360 /* this is a serial console */
Matt Kraai439e3df2001-07-23 14:52:08 +0000361 snprintf(console, sizeof(console) - 1, SC_FORMAT, sr.line);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000362 } else if (ioctl(0, VT_GETSTATE, &vt) == 0) {
363 /* this is linux virtual tty */
Matt Kraai439e3df2001-07-23 14:52:08 +0000364 snprintf(console, sizeof(console) - 1, VC_FORMAT, vt.v_active);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000365 } else {
Matt Kraai18447702001-05-18 21:24:58 +0000366 safe_strncpy(console, _PATH_CONSOLE, sizeof(console));
Erik Andersene49d5ec2000-02-08 19:58:47 +0000367 tried_devcons++;
368 }
Eric Andersen07e52971999-11-07 07:38:08 +0000369 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000370
371 while ((fd = open(console, O_RDONLY | O_NONBLOCK)) < 0) {
372 /* Can't open selected console -- try /dev/console */
373 if (!tried_devcons) {
374 tried_devcons++;
Matt Kraai18447702001-05-18 21:24:58 +0000375 safe_strncpy(console, _PATH_CONSOLE, sizeof(console));
Erik Andersene49d5ec2000-02-08 19:58:47 +0000376 continue;
377 }
378 /* Can't open selected console -- try vt1 */
379 if (!tried_vtprimary) {
380 tried_vtprimary++;
Matt Kraai439e3df2001-07-23 14:52:08 +0000381 safe_strncpy(console, VC_1, sizeof(console));
Erik Andersene49d5ec2000-02-08 19:58:47 +0000382 continue;
383 }
384 break;
385 }
386 if (fd < 0) {
387 /* Perhaps we should panic here? */
Matt Kraai18447702001-05-18 21:24:58 +0000388 safe_strncpy(console, "/dev/null", sizeof(console));
Eric Andersen07e52971999-11-07 07:38:08 +0000389 } else {
Eric Andersen3bc2b202002-07-29 06:39:58 +0000390 /* check for serial console */
Erik Andersene49d5ec2000-02-08 19:58:47 +0000391 if (ioctl(0, TIOCGSERIAL, &sr) == 0) {
Erik Andersenfa4718e2000-02-21 19:25:12 +0000392 /* Force the TERM setting to vt102 for serial console --
Eric Andersen3bc2b202002-07-29 06:39:58 +0000393 * if TERM is set to linux (the default) */
Glenn L McGrathbaf55a82002-08-22 18:22:10 +0000394 if (strcmp(termType, "TERM=linux") == 0)
Matt Kraai18447702001-05-18 21:24:58 +0000395 safe_strncpy(termType, "TERM=vt102", sizeof(termType));
Erik Andersene49d5ec2000-02-08 19:58:47 +0000396 }
397 close(fd);
Eric Andersen07e52971999-11-07 07:38:08 +0000398 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000399 message(LOG, "console=%s\n", console);
Eric Andersen0460ff21999-10-25 23:32:44 +0000400}
Glenn L McGrathbaf55a82002-08-22 18:22:10 +0000401
Eric Andersen7ef1a5b2001-03-20 17:39:08 +0000402static void fixup_argv(int argc, char **argv, char *new_argv0)
403{
404 int len;
Glenn L McGrathbaf55a82002-08-22 18:22:10 +0000405
Eric Andersen7ef1a5b2001-03-20 17:39:08 +0000406 /* Fix up argv[0] to be certain we claim to be init */
407 len = strlen(argv[0]);
408 memset(argv[0], 0, len);
Eric Andersen72f9a422001-10-28 05:12:20 +0000409 safe_strncpy(argv[0], new_argv0, len + 1);
Eric Andersen7ef1a5b2001-03-20 17:39:08 +0000410
411 /* Wipe argv[1]-argv[N] so they don't clutter the ps listing */
412 len = 1;
413 while (argc > len) {
414 memset(argv[len], 0, strlen(argv[len]));
415 len++;
416 }
417}
418
Eric Andersen0298be82002-03-05 15:12:19 +0000419/* Make sure there is enough memory to do something useful. *
420 * Calls "swapon -a" if needed so be sure /etc/fstab is present... */
421static void check_memory(void)
Eric Andersen0460ff21999-10-25 23:32:44 +0000422{
Eric Andersen0298be82002-03-05 15:12:19 +0000423 struct stat statBuf;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000424
Eric Andersen0298be82002-03-05 15:12:19 +0000425 if (check_free_memory() > 1000)
426 return;
427
428#if !defined(__UCLIBC__) || defined(__UCLIBC_HAS_MMU__)
429 if (stat("/etc/fstab", &statBuf) == 0) {
430 /* swapon -a requires /proc typically */
431 system("/bin/mount -t proc proc /proc");
432 /* Try to turn on swap */
433 system("/sbin/swapon -a");
434 if (check_free_memory() < 1000)
435 goto goodnight;
436 } else
437 goto goodnight;
438 return;
Eric Andersen38624232001-01-25 00:04:16 +0000439#endif
440
Eric Andersen0298be82002-03-05 15:12:19 +0000441 goodnight:
Glenn L McGrathbaf55a82002-08-22 18:22:10 +0000442 message(CONSOLE, "\rSorry, your computer does not have enough memory.\n");
Eric Andersen0298be82002-03-05 15:12:19 +0000443 loop_forever();
444}
445
446static pid_t run(struct init_action *a)
447{
448 struct stat sb;
Eric Andersen0826b6b2002-07-03 23:50:16 +0000449 int i, j, junk;
Eric Andersen0298be82002-03-05 15:12:19 +0000450 pid_t pid, pgrp, tmp_pid;
Eric Andersen0826b6b2002-07-03 23:50:16 +0000451 char *s, *tmpCmd, *cmd[INIT_BUFFS_SIZE], *cmdpath;
Glenn L McGrathbaf55a82002-08-22 18:22:10 +0000452 char buf[INIT_BUFFS_SIZE + 6]; /* INIT_BUFFS_SIZE+strlen("exec ")+1 */
Eric Andersen0298be82002-03-05 15:12:19 +0000453 sigset_t nmask, omask;
Glenn L McGrathbaf55a82002-08-22 18:22:10 +0000454 char *environment[MAXENV + 1] = {
Eric Andersen7f197852001-03-16 01:14:04 +0000455 termType,
Erik Andersene49d5ec2000-02-08 19:58:47 +0000456 "HOME=/",
Eric Andersen72f9a422001-10-28 05:12:20 +0000457 "PATH=" _PATH_STDPATH,
Matt Kraai7bd773c2001-06-12 20:55:02 +0000458 "SHELL=" SHELL,
Eric Andersen7f197852001-03-16 01:14:04 +0000459 "USER=root",
460 NULL
Erik Andersene49d5ec2000-02-08 19:58:47 +0000461 };
Eric Andersen0298be82002-03-05 15:12:19 +0000462 static const char press_enter[] =
463#ifdef CUSTOMIZED_BANNER
464#include CUSTOMIZED_BANNER
465#endif
466 "\nPlease press Enter to activate this console. ";
Erik Andersenac6e71f2000-01-08 22:04:33 +0000467
Eric Andersen7f197852001-03-16 01:14:04 +0000468 /* inherit environment to the child, merging our values -andy */
Glenn L McGrathbaf55a82002-08-22 18:22:10 +0000469 for (i = 0; environ[i]; i++) {
470 for (j = 0; environment[j]; j++) {
Eric Andersen7f197852001-03-16 01:14:04 +0000471 s = strchr(environment[j], '=');
472 if (!strncmp(environ[i], environment[j], s - environment[j]))
473 break;
474 }
475 if (!environment[j]) {
476 environment[j++] = environ[i];
477 environment[j] = NULL;
478 }
Eric Andersen477aedd2001-02-20 18:01:50 +0000479 }
480
Eric Andersen0298be82002-03-05 15:12:19 +0000481 /* Block sigchild while forking. */
482 sigemptyset(&nmask);
483 sigaddset(&nmask, SIGCHLD);
484 sigprocmask(SIG_BLOCK, &nmask, &omask);
485
Glenn L McGrathbaf55a82002-08-22 18:22:10 +0000486 if ((pid = fork()) == 0) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000487 /* Clean up */
488 close(0);
489 close(1);
490 close(2);
Eric Andersen0298be82002-03-05 15:12:19 +0000491 sigprocmask(SIG_SETMASK, &omask, NULL);
492
Eric Andersen0298be82002-03-05 15:12:19 +0000493 /* Reset signal handlers that were set by the parent process */
Erik Andersene49d5ec2000-02-08 19:58:47 +0000494 signal(SIGUSR1, SIG_DFL);
495 signal(SIGUSR2, SIG_DFL);
Glenn L McGrathbaf55a82002-08-22 18:22:10 +0000496 signal(SIGINT, SIG_DFL);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000497 signal(SIGTERM, SIG_DFL);
Glenn L McGrathbaf55a82002-08-22 18:22:10 +0000498 signal(SIGHUP, SIG_DFL);
Eric Andersened8a9be2001-11-30 19:10:58 +0000499 signal(SIGCONT, SIG_DFL);
500 signal(SIGSTOP, SIG_DFL);
501 signal(SIGTSTP, SIG_DFL);
Eric Andersen2f6c04f1999-11-01 23:59:44 +0000502
Eric Andersen599e3ce2002-07-03 11:08:10 +0000503 /* Create a new session and make ourself the process
Eric Andersene8a90fb2002-10-12 04:05:48 +0000504 * group leader */
505 setsid();
Eric Andersen599e3ce2002-07-03 11:08:10 +0000506
Eric Andersen0298be82002-03-05 15:12:19 +0000507 /* Open the new terminal device */
Eric Andersene8a90fb2002-10-12 04:05:48 +0000508 if ((device_open(a->terminal, O_RDWR)) < 0) {
Eric Andersen0298be82002-03-05 15:12:19 +0000509 if (stat(a->terminal, &sb) != 0) {
Eric Andersenb0cc0a62002-03-20 14:57:50 +0000510 message(LOG | CONSOLE, "\rdevice '%s' does not exist.\n",
Eric Andersen0298be82002-03-05 15:12:19 +0000511 a->terminal);
512 _exit(1);
Eric Andersen53f50612001-03-14 09:01:11 +0000513 }
Eric Andersenb0cc0a62002-03-20 14:57:50 +0000514 message(LOG | CONSOLE, "\rBummer, can't open %s\n", a->terminal);
Eric Andersen0298be82002-03-05 15:12:19 +0000515 _exit(1);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000516 }
Eric Andersen599e3ce2002-07-03 11:08:10 +0000517
Eric Andersen0298be82002-03-05 15:12:19 +0000518 /* Make sure the terminal will act fairly normal for us */
Erik Andersene49d5ec2000-02-08 19:58:47 +0000519 set_term(0);
Eric Andersen0826b6b2002-07-03 23:50:16 +0000520 /* Setup stdout, stderr for the new process so
Eric Andersen599e3ce2002-07-03 11:08:10 +0000521 * they point to the supplied terminal */
Eric Andersen0826b6b2002-07-03 23:50:16 +0000522 dup(0);
523 dup(0);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000524
Eric Andersen599e3ce2002-07-03 11:08:10 +0000525 /* If the init Action requires us to wait, then force the
Eric Andersen0298be82002-03-05 15:12:19 +0000526 * supplied terminal to be the controlling tty. */
Glenn L McGrathbaf55a82002-08-22 18:22:10 +0000527 if (a->action & (SYSINIT | WAIT | CTRLALTDEL | SHUTDOWN | RESTART)) {
Eric Andersen0298be82002-03-05 15:12:19 +0000528
529 /* Now fork off another process to just hang around */
530 if ((pid = fork()) < 0) {
531 message(LOG | CONSOLE, "Can't fork!\n");
532 _exit(1);
533 }
534
535 if (pid > 0) {
536
537 /* We are the parent -- wait till the child is done */
538 signal(SIGINT, SIG_IGN);
539 signal(SIGTSTP, SIG_IGN);
540 signal(SIGQUIT, SIG_IGN);
541 signal(SIGCHLD, SIG_DFL);
542
543 /* Wait for child to exit */
Glenn L McGrathbaf55a82002-08-22 18:22:10 +0000544 while ((tmp_pid = waitpid(pid, &junk, 0)) != pid);
Eric Andersen0298be82002-03-05 15:12:19 +0000545
546 /* See if stealing the controlling tty back is necessary */
Eric Andersen0826b6b2002-07-03 23:50:16 +0000547 pgrp = tcgetpgrp(0);
Eric Andersen0298be82002-03-05 15:12:19 +0000548 if (pgrp != getpid())
549 _exit(0);
550
551 /* Use a temporary process to steal the controlling tty. */
552 if ((pid = fork()) < 0) {
Eric Andersenb0cc0a62002-03-20 14:57:50 +0000553 message(LOG | CONSOLE, "\rCan't fork!\n");
Eric Andersen0298be82002-03-05 15:12:19 +0000554 _exit(1);
Glenn L McGrathbaf55a82002-08-22 18:22:10 +0000555 }
Eric Andersen0298be82002-03-05 15:12:19 +0000556 if (pid == 0) {
557 setsid();
Eric Andersen0826b6b2002-07-03 23:50:16 +0000558 ioctl(0, TIOCSCTTY, 1);
Eric Andersen0298be82002-03-05 15:12:19 +0000559 _exit(0);
Glenn L McGrathbaf55a82002-08-22 18:22:10 +0000560 }
561 while ((tmp_pid = waitpid(pid, &junk, 0)) != pid) {
Eric Andersen0298be82002-03-05 15:12:19 +0000562 if (tmp_pid < 0 && errno == ECHILD)
563 break;
564 }
565 _exit(0);
566 }
567
568 /* Now fall though to actually execute things */
Eric Andersen0298be82002-03-05 15:12:19 +0000569 }
570
Erik Andersene132f4b2000-02-09 04:16:43 +0000571 /* See if any special /bin/sh requiring characters are present */
Eric Andersen0298be82002-03-05 15:12:19 +0000572 if (strpbrk(a->command, "~`!$^&*()=|\\{}[];\"'<>?") != NULL) {
Erik Andersene132f4b2000-02-09 04:16:43 +0000573 cmd[0] = SHELL;
574 cmd[1] = "-c";
Eric Andersen0826b6b2002-07-03 23:50:16 +0000575 strcat(strcpy(buf, "exec "), a->command);
Erik Andersene132f4b2000-02-09 04:16:43 +0000576 cmd[2] = buf;
577 cmd[3] = NULL;
578 } else {
579 /* Convert command (char*) into cmd (char**, one word per string) */
Eric Andersen0826b6b2002-07-03 23:50:16 +0000580 strcpy(buf, a->command);
Eric Andersen72f9a422001-10-28 05:12:20 +0000581 s = buf;
Glenn L McGrathbaf55a82002-08-22 18:22:10 +0000582 for (tmpCmd = buf, i = 0; (tmpCmd = strsep(&s, " \t")) != NULL;) {
Erik Andersene132f4b2000-02-09 04:16:43 +0000583 if (*tmpCmd != '\0') {
584 cmd[i] = tmpCmd;
Erik Andersene132f4b2000-02-09 04:16:43 +0000585 i++;
586 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000587 }
Erik Andersene132f4b2000-02-09 04:16:43 +0000588 cmd[i] = NULL;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000589 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000590
Eric Andersen7ef1a5b2001-03-20 17:39:08 +0000591 cmdpath = cmd[0];
Eric Andersen7e3bf6e2000-09-14 22:01:31 +0000592
Eric Andersen7ef1a5b2001-03-20 17:39:08 +0000593 /*
594 Interactive shells want to see a dash in argv[0]. This
595 typically is handled by login, argv will be setup this
596 way if a dash appears at the front of the command path
Eric Andersen7e3bf6e2000-09-14 22:01:31 +0000597 (like "-/bin/sh").
Eric Andersen7ef1a5b2001-03-20 17:39:08 +0000598 */
Eric Andersen7e3bf6e2000-09-14 22:01:31 +0000599
Eric Andersen7ef1a5b2001-03-20 17:39:08 +0000600 if (*cmdpath == '-') {
Eric Andersen7e3bf6e2000-09-14 22:01:31 +0000601
Eric Andersen7ef1a5b2001-03-20 17:39:08 +0000602 /* skip over the dash */
603 ++cmdpath;
Eric Andersen7e3bf6e2000-09-14 22:01:31 +0000604
Eric Andersen7ef1a5b2001-03-20 17:39:08 +0000605 /* find the last component in the command pathname */
606 s = get_last_path_component(cmdpath);
Eric Andersen7e3bf6e2000-09-14 22:01:31 +0000607
Eric Andersen7ef1a5b2001-03-20 17:39:08 +0000608 /* make a new argv[0] */
Glenn L McGrathbaf55a82002-08-22 18:22:10 +0000609 if ((cmd[0] = malloc(strlen(s) + 2)) == NULL) {
Eric Andersen7ef1a5b2001-03-20 17:39:08 +0000610 message(LOG | CONSOLE, "malloc failed");
611 cmd[0] = cmdpath;
612 } else {
613 cmd[0][0] = '-';
Glenn L McGrathbaf55a82002-08-22 18:22:10 +0000614 strcpy(cmd[0] + 1, s);
Eric Andersen7ef1a5b2001-03-20 17:39:08 +0000615 }
616 }
617
Eric Andersen0298be82002-03-05 15:12:19 +0000618 if (a->action & ASKFIRST) {
Eric Andersen7ef1a5b2001-03-20 17:39:08 +0000619 /*
620 * Save memory by not exec-ing anything large (like a shell)
621 * before the user wants it. This is critical if swap is not
622 * enabled and the system has low memory. Generally this will
623 * be run on the second virtual console, and the first will
624 * be allowed to start a shell or whatever an init script
625 * specifies.
626 */
Glenn L McGrathbaf55a82002-08-22 18:22:10 +0000627 messageND(LOG,
628 "Waiting for enter to start '%s' (pid %d, terminal %s)\n",
629 cmdpath, getpid(), a->terminal);
Eric Andersen7ef1a5b2001-03-20 17:39:08 +0000630 write(fileno(stdout), press_enter, sizeof(press_enter) - 1);
631 getc(stdin);
632 }
633
Eric Andersen7ef1a5b2001-03-20 17:39:08 +0000634 /* Log the process name and args */
Eric Andersenfedce062001-11-17 07:27:14 +0000635 messageND(LOG, "Starting pid %d, console %s: '%s'\n",
Glenn L McGrathbaf55a82002-08-22 18:22:10 +0000636 getpid(), a->terminal, cmdpath);
Eric Andersen7e3bf6e2000-09-14 22:01:31 +0000637
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000638#if defined CONFIG_FEATURE_INIT_COREDUMPS
Glenn L McGrathbaf55a82002-08-22 18:22:10 +0000639 if (stat(CORE_ENABLE_FLAG_FILE, &sb) == 0) {
Eric Andersen7ef1a5b2001-03-20 17:39:08 +0000640 struct rlimit limit;
Glenn L McGrathbaf55a82002-08-22 18:22:10 +0000641
Eric Andersen7ef1a5b2001-03-20 17:39:08 +0000642 limit.rlim_cur = RLIM_INFINITY;
643 limit.rlim_max = RLIM_INFINITY;
644 setrlimit(RLIMIT_CORE, &limit);
Erik Andersen983b51b2000-04-04 18:14:25 +0000645 }
Erik Andersen183da4a2000-04-04 18:36:37 +0000646#endif
Erik Andersen983b51b2000-04-04 18:14:25 +0000647
Erik Andersene49d5ec2000-02-08 19:58:47 +0000648 /* Now run it. The new program will take over this PID,
649 * so nothing further in init.c should be run. */
Eric Andersen7ef1a5b2001-03-20 17:39:08 +0000650 execve(cmdpath, cmd, environment);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000651
Eric Andersen7ef1a5b2001-03-20 17:39:08 +0000652 /* We're still here? Some error happened. */
Eric Andersen71ae64b2002-10-10 04:20:21 +0000653 message(LOG | CONSOLE, "\rBummer, could not run '%s': %m\n", cmdpath);
Eric Andersen0298be82002-03-05 15:12:19 +0000654 _exit(-1);
Erik Andersen05df2392000-01-13 04:43:48 +0000655 }
Eric Andersen0298be82002-03-05 15:12:19 +0000656 sigprocmask(SIG_SETMASK, &omask, NULL);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000657 return pid;
Eric Andersen0460ff21999-10-25 23:32:44 +0000658}
659
Eric Andersen0298be82002-03-05 15:12:19 +0000660static int waitfor(struct init_action *a)
Erik Andersen0e3782f2000-01-07 02:54:55 +0000661{
Glenn L McGrathbaf55a82002-08-22 18:22:10 +0000662 int pid;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000663 int status, wpid;
Erik Andersen0e3782f2000-01-07 02:54:55 +0000664
Eric Andersen0298be82002-03-05 15:12:19 +0000665 pid = run(a);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000666 while (1) {
667 wpid = wait(&status);
Erik Andersene132f4b2000-02-09 04:16:43 +0000668 if (wpid > 0 && wpid != pid) {
669 continue;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000670 }
671 if (wpid == pid)
672 break;
Erik Andersen0e3782f2000-01-07 02:54:55 +0000673 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000674 return wpid;
Erik Andersen0e3782f2000-01-07 02:54:55 +0000675}
676
Eric Andersen0298be82002-03-05 15:12:19 +0000677/* Run all commands of a particular type */
678static void run_actions(int action)
Eric Andersen219d6f51999-11-02 19:43:01 +0000679{
Eric Andersen0298be82002-03-05 15:12:19 +0000680 struct init_action *a, *tmp;
Eric Andersen219d6f51999-11-02 19:43:01 +0000681
Eric Andersen0298be82002-03-05 15:12:19 +0000682 for (a = init_action_list; a; a = tmp) {
683 tmp = a->next;
Eric Andersenc97ec342001-04-03 18:01:51 +0000684 if (a->action == action) {
Glenn L McGrathbaf55a82002-08-22 18:22:10 +0000685 if (a->
686 action & (SYSINIT | WAIT | CTRLALTDEL | SHUTDOWN | RESTART)) {
Eric Andersen0298be82002-03-05 15:12:19 +0000687 waitfor(a);
688 delete_init_action(a);
689 } else if (a->action & ONCE) {
690 run(a);
691 delete_init_action(a);
Glenn L McGrathbaf55a82002-08-22 18:22:10 +0000692 } else if (a->action & (RESPAWN | ASKFIRST)) {
Eric Andersen0298be82002-03-05 15:12:19 +0000693 /* Only run stuff with pid==0. If they have
694 * a pid, that means it is still running */
695 if (a->pid == 0) {
696 a->pid = run(a);
697 }
698 }
Erik Andersene132f4b2000-02-09 04:16:43 +0000699 }
700 }
701}
702
703
Erik Andersen7dc16072000-01-04 01:10:25 +0000704#ifndef DEBUG_INIT
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000705static void shutdown_system(void)
Eric Andersencc8ed391999-10-05 16:24:54 +0000706{
Eric Andersen813d88c2001-10-28 22:49:48 +0000707 sigset_t block_signals;
Erik Andersene132f4b2000-02-09 04:16:43 +0000708
Eric Andersena9cc8962002-09-16 06:49:06 +0000709 /* run everything to be run at "shutdown". This is done _prior_
710 * to killing everything, in case people wish to use scripts to
711 * shut things down gracefully... */
712 run_actions(SHUTDOWN);
713
Eric Andersen72f9a422001-10-28 05:12:20 +0000714 /* first disable all our signals */
715 sigemptyset(&block_signals);
716 sigaddset(&block_signals, SIGHUP);
717 sigaddset(&block_signals, SIGCHLD);
718 sigaddset(&block_signals, SIGUSR1);
719 sigaddset(&block_signals, SIGUSR2);
720 sigaddset(&block_signals, SIGINT);
721 sigaddset(&block_signals, SIGTERM);
Eric Andersened8a9be2001-11-30 19:10:58 +0000722 sigaddset(&block_signals, SIGCONT);
723 sigaddset(&block_signals, SIGSTOP);
724 sigaddset(&block_signals, SIGTSTP);
Eric Andersen72f9a422001-10-28 05:12:20 +0000725 sigprocmask(SIG_BLOCK, &block_signals, NULL);
Erik Andersen7dc16072000-01-04 01:10:25 +0000726
Erik Andersene49d5ec2000-02-08 19:58:47 +0000727 /* Allow Ctrl-Alt-Del to reboot system. */
Erik Andersen4f3f7572000-04-28 00:18:56 +0000728 init_reboot(RB_ENABLE_CAD);
Erik Andersene132f4b2000-02-09 04:16:43 +0000729
Glenn L McGrathbaf55a82002-08-22 18:22:10 +0000730 message(CONSOLE | LOG, "\n\rThe system is going down NOW !!\n");
Eric Andersencf8c9cf1999-11-05 00:31:46 +0000731 sync();
Erik Andersene49d5ec2000-02-08 19:58:47 +0000732
733 /* Send signals to every process _except_ pid 1 */
Glenn L McGrathbaf55a82002-08-22 18:22:10 +0000734 message(CONSOLE | LOG, "\rSending SIGTERM to all processes.\n");
Erik Andersene49d5ec2000-02-08 19:58:47 +0000735 kill(-1, SIGTERM);
Erik Andersene132f4b2000-02-09 04:16:43 +0000736 sleep(1);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000737 sync();
738
Glenn L McGrathbaf55a82002-08-22 18:22:10 +0000739 message(CONSOLE | LOG, "\rSending SIGKILL to all processes.\n");
Erik Andersene49d5ec2000-02-08 19:58:47 +0000740 kill(-1, SIGKILL);
Erik Andersene132f4b2000-02-09 04:16:43 +0000741 sleep(1);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000742
Erik Andersene49d5ec2000-02-08 19:58:47 +0000743 sync();
Glenn L McGrathbaf55a82002-08-22 18:22:10 +0000744 if (kernelVersion > 0 && kernelVersion <= KERNEL_VERSION(2, 2, 11)) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000745 /* bdflush, kupdate not needed for kernels >2.2.11 */
746 bdflush(1, 0);
747 sync();
748 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000749}
750
Eric Andersen730f8262001-12-17 23:13:08 +0000751static void exec_signal(int sig)
752{
Eric Andersen0298be82002-03-05 15:12:19 +0000753 struct init_action *a, *tmp;
Eric Andersen5222d312002-07-03 05:15:23 +0000754 sigset_t unblock_signals;
Glenn L McGrathbaf55a82002-08-22 18:22:10 +0000755
Eric Andersen0298be82002-03-05 15:12:19 +0000756 for (a = init_action_list; a; a = tmp) {
757 tmp = a->next;
758 if (a->action & RESTART) {
Eric Andersen730f8262001-12-17 23:13:08 +0000759 shutdown_system();
Eric Andersen5222d312002-07-03 05:15:23 +0000760
761 /* unblock all signals, blocked in shutdown_system() */
762 sigemptyset(&unblock_signals);
763 sigaddset(&unblock_signals, SIGHUP);
764 sigaddset(&unblock_signals, SIGCHLD);
765 sigaddset(&unblock_signals, SIGUSR1);
766 sigaddset(&unblock_signals, SIGUSR2);
767 sigaddset(&unblock_signals, SIGINT);
768 sigaddset(&unblock_signals, SIGTERM);
769 sigaddset(&unblock_signals, SIGCONT);
770 sigaddset(&unblock_signals, SIGSTOP);
771 sigaddset(&unblock_signals, SIGTSTP);
772 sigprocmask(SIG_UNBLOCK, &unblock_signals, NULL);
773
Glenn L McGrathbaf55a82002-08-22 18:22:10 +0000774 message(CONSOLE | LOG, "\rTrying to re-exec %s\n", a->command);
Eric Andersen0298be82002-03-05 15:12:19 +0000775 execl(a->command, a->command, NULL);
Glenn L McGrathbaf55a82002-08-22 18:22:10 +0000776
Eric Andersen71ae64b2002-10-10 04:20:21 +0000777 message(CONSOLE | LOG, "\rexec of '%s' failed: %m\n",
778 a->command);
Eric Andersen730f8262001-12-17 23:13:08 +0000779 sync();
780 sleep(2);
781 init_reboot(RB_HALT_SYSTEM);
782 loop_forever();
783 }
784 }
785}
786
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000787static void halt_signal(int sig)
Eric Andersencc8ed391999-10-05 16:24:54 +0000788{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000789 shutdown_system();
Glenn L McGrathbaf55a82002-08-22 18:22:10 +0000790 message(CONSOLE | LOG,
Eric Andersenf435a912001-11-20 05:42:57 +0000791#if #cpu(s390)
792 /* Seems the s390 console is Wierd(tm). */
Eric Andersenb0cc0a62002-03-20 14:57:50 +0000793 "\rThe system is halted. You may reboot now.\n"
Eric Andersenf435a912001-11-20 05:42:57 +0000794#else
Eric Andersen3bc2b202002-07-29 06:39:58 +0000795 "\rThe system is halted. Press Reset or turn off power\n"
Eric Andersenf435a912001-11-20 05:42:57 +0000796#endif
Glenn L McGrathbaf55a82002-08-22 18:22:10 +0000797 );
Erik Andersene49d5ec2000-02-08 19:58:47 +0000798 sync();
Erik Andersen3fe39dc2000-01-25 18:13:53 +0000799
Erik Andersene49d5ec2000-02-08 19:58:47 +0000800 /* allow time for last message to reach serial console */
Erik Andersene132f4b2000-02-09 04:16:43 +0000801 sleep(2);
Erik Andersen3fe39dc2000-01-25 18:13:53 +0000802
Glenn L McGrathbaf55a82002-08-22 18:22:10 +0000803 if (sig == SIGUSR2 && kernelVersion >= KERNEL_VERSION(2, 2, 0))
Erik Andersen4f3f7572000-04-28 00:18:56 +0000804 init_reboot(RB_POWER_OFF);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000805 else
Erik Andersen4f3f7572000-04-28 00:18:56 +0000806 init_reboot(RB_HALT_SYSTEM);
Matt Kraai67a46402001-06-03 05:55:52 +0000807
808 loop_forever();
Eric Andersencc8ed391999-10-05 16:24:54 +0000809}
810
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000811static void reboot_signal(int sig)
Eric Andersencc8ed391999-10-05 16:24:54 +0000812{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000813 shutdown_system();
Glenn L McGrathbaf55a82002-08-22 18:22:10 +0000814 message(CONSOLE | LOG, "\rPlease stand by while rebooting the system.\n");
Erik Andersene49d5ec2000-02-08 19:58:47 +0000815 sync();
Erik Andersen3fe39dc2000-01-25 18:13:53 +0000816
Erik Andersene49d5ec2000-02-08 19:58:47 +0000817 /* allow time for last message to reach serial console */
818 sleep(2);
Erik Andersen3fe39dc2000-01-25 18:13:53 +0000819
Erik Andersen4f3f7572000-04-28 00:18:56 +0000820 init_reboot(RB_AUTOBOOT);
Matt Kraai67a46402001-06-03 05:55:52 +0000821
822 loop_forever();
Eric Andersencc8ed391999-10-05 16:24:54 +0000823}
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000824
Eric Andersenc97ec342001-04-03 18:01:51 +0000825static void ctrlaltdel_signal(int sig)
826{
827 run_actions(CTRLALTDEL);
828}
829
Eric Andersen0298be82002-03-05 15:12:19 +0000830/* The SIGSTOP & SIGTSTP handler */
Eric Andersened8a9be2001-11-30 19:10:58 +0000831static void stop_handler(int sig)
832{
Eric Andersen0298be82002-03-05 15:12:19 +0000833 int saved_errno = errno;
Eric Andersened8a9be2001-11-30 19:10:58 +0000834
835 got_cont = 0;
Glenn L McGrathbaf55a82002-08-22 18:22:10 +0000836 while (!got_cont)
837 pause();
Eric Andersened8a9be2001-11-30 19:10:58 +0000838 got_cont = 0;
839 errno = saved_errno;
840}
841
Glenn L McGrathbaf55a82002-08-22 18:22:10 +0000842/* The SIGCONT handler */
Eric Andersened8a9be2001-11-30 19:10:58 +0000843static void cont_handler(int sig)
844{
845 got_cont = 1;
846}
847
Erik Andersene49d5ec2000-02-08 19:58:47 +0000848#endif /* ! DEBUG_INIT */
Erik Andersende552872000-01-23 01:34:05 +0000849
Eric Andersen0298be82002-03-05 15:12:19 +0000850static void new_init_action(int action, char *command, char *cons)
Erik Andersen7dc16072000-01-04 01:10:25 +0000851{
Eric Andersen0298be82002-03-05 15:12:19 +0000852 struct init_action *new_action, *a;
Erik Andersen9e737252000-01-06 01:16:13 +0000853
Erik Andersene49d5ec2000-02-08 19:58:47 +0000854 if (*cons == '\0')
855 cons = console;
Erik Andersen9e737252000-01-06 01:16:13 +0000856
Eric Andersen3bc2b202002-07-29 06:39:58 +0000857 /* do not run entries if console device is not available */
Glenn L McGrathbaf55a82002-08-22 18:22:10 +0000858 if (access(cons, R_OK | W_OK))
Erik Andersene49d5ec2000-02-08 19:58:47 +0000859 return;
Eric Andersen0298be82002-03-05 15:12:19 +0000860 if (strcmp(cons, "/dev/null") == 0 && (action & ASKFIRST))
Eric Andersen1644db92001-09-05 20:18:15 +0000861 return;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000862
Eric Andersen0298be82002-03-05 15:12:19 +0000863 new_action = calloc((size_t) (1), sizeof(struct init_action));
864 if (!new_action) {
Eric Andersenb0cc0a62002-03-20 14:57:50 +0000865 message(LOG | CONSOLE, "\rMemory allocation failure\n");
Matt Kraai67a46402001-06-03 05:55:52 +0000866 loop_forever();
Erik Andersene49d5ec2000-02-08 19:58:47 +0000867 }
Eric Andersen0298be82002-03-05 15:12:19 +0000868
869 /* Append to the end of the list */
Glenn L McGrathbaf55a82002-08-22 18:22:10 +0000870 for (a = init_action_list; a && a->next; a = a->next);
Eric Andersen1644db92001-09-05 20:18:15 +0000871 if (a) {
Eric Andersen0298be82002-03-05 15:12:19 +0000872 a->next = new_action;
Eric Andersen1644db92001-09-05 20:18:15 +0000873 } else {
Eric Andersen0298be82002-03-05 15:12:19 +0000874 init_action_list = new_action;
Eric Andersen1644db92001-09-05 20:18:15 +0000875 }
Eric Andersen0826b6b2002-07-03 23:50:16 +0000876 strcpy(new_action->command, command);
Eric Andersen0298be82002-03-05 15:12:19 +0000877 new_action->action = action;
Eric Andersen0826b6b2002-07-03 23:50:16 +0000878 strcpy(new_action->terminal, cons);
Eric Andersen0298be82002-03-05 15:12:19 +0000879 new_action->pid = 0;
Glenn L McGrathbaf55a82002-08-22 18:22:10 +0000880/* message(LOG|CONSOLE, "command='%s' action='%d' terminal='%s'\n",
881 new_action->command, new_action->action, new_action->terminal); */
Erik Andersen7dc16072000-01-04 01:10:25 +0000882}
883
Glenn L McGrathbaf55a82002-08-22 18:22:10 +0000884static void delete_init_action(struct init_action *action)
Erik Andersen7dc16072000-01-04 01:10:25 +0000885{
Eric Andersen0298be82002-03-05 15:12:19 +0000886 struct init_action *a, *b = NULL;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000887
Eric Andersen0298be82002-03-05 15:12:19 +0000888 for (a = init_action_list; a; b = a, a = a->next) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000889 if (a == action) {
890 if (b == NULL) {
Eric Andersen0298be82002-03-05 15:12:19 +0000891 init_action_list = a->next;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000892 } else {
Eric Andersen0298be82002-03-05 15:12:19 +0000893 b->next = a->next;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000894 }
895 free(a);
896 break;
897 }
Erik Andersen7dc16072000-01-04 01:10:25 +0000898 }
Erik Andersen7dc16072000-01-04 01:10:25 +0000899}
900
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000901/* NOTE that if CONFIG_FEATURE_USE_INITTAB is NOT defined,
Erik Andersen9e737252000-01-06 01:16:13 +0000902 * then parse_inittab() simply adds in some default
Eric Andersen77d92682001-05-23 20:32:09 +0000903 * actions(i.e., runs INIT_SCRIPT and then starts a pair
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000904 * of "askfirst" shells). If CONFIG_FEATURE_USE_INITTAB
Erik Andersen812d4662000-01-07 18:30:40 +0000905 * _is_ defined, but /etc/inittab is missing, this
906 * results in the same set of default behaviors.
Glenn L McGrathbaf55a82002-08-22 18:22:10 +0000907 */
Eric Andersen3e6ff902001-03-09 21:24:12 +0000908static void parse_inittab(void)
Erik Andersen7dc16072000-01-04 01:10:25 +0000909{
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000910#ifdef CONFIG_FEATURE_USE_INITTAB
Erik Andersene49d5ec2000-02-08 19:58:47 +0000911 FILE *file;
Glenn L McGrathbaf55a82002-08-22 18:22:10 +0000912 char buf[INIT_BUFFS_SIZE], lineAsRead[INIT_BUFFS_SIZE],
913 tmpConsole[INIT_BUFFS_SIZE];
Eric Andersen0298be82002-03-05 15:12:19 +0000914 char *id, *runlev, *action, *command, *eol;
915 const struct init_action_type *a = actions;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000916 int foundIt;
Erik Andersen7dc16072000-01-04 01:10:25 +0000917
918
Erik Andersene49d5ec2000-02-08 19:58:47 +0000919 file = fopen(INITTAB, "r");
920 if (file == NULL) {
921 /* No inittab file -- set up some default behavior */
Erik Andersen9e737252000-01-06 01:16:13 +0000922#endif
Eric Andersenc97ec342001-04-03 18:01:51 +0000923 /* Reboot on Ctrl-Alt-Del */
Eric Andersen0298be82002-03-05 15:12:19 +0000924 new_init_action(CTRLALTDEL, "/sbin/reboot", console);
Eric Andersen1644db92001-09-05 20:18:15 +0000925 /* Umount all filesystems on halt/reboot */
Eric Andersen0298be82002-03-05 15:12:19 +0000926 new_init_action(SHUTDOWN, "/bin/umount -a -r", console);
Eric Andersen72f9a422001-10-28 05:12:20 +0000927#if !defined(__UCLIBC__) || defined(__UCLIBC_HAS_MMU__)
Eric Andersen1644db92001-09-05 20:18:15 +0000928 /* Swapoff on halt/reboot */
Eric Andersen0298be82002-03-05 15:12:19 +0000929 new_init_action(SHUTDOWN, "/sbin/swapoff -a", console);
Eric Andersen1644db92001-09-05 20:18:15 +0000930#endif
Eric Andersen730f8262001-12-17 23:13:08 +0000931 /* Prepare to restart init when a HUP is received */
Eric Andersen0298be82002-03-05 15:12:19 +0000932 new_init_action(RESTART, "/sbin/init", console);
Eric Andersen3bc2b202002-07-29 06:39:58 +0000933 /* Askfirst shell on tty1-4 */
Eric Andersen0298be82002-03-05 15:12:19 +0000934 new_init_action(ASKFIRST, LOGIN_SHELL, console);
Eric Andersen3bc2b202002-07-29 06:39:58 +0000935 new_init_action(ASKFIRST, LOGIN_SHELL, VC_2);
936 new_init_action(ASKFIRST, LOGIN_SHELL, VC_3);
937 new_init_action(ASKFIRST, LOGIN_SHELL, VC_4);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000938 /* sysinit */
Eric Andersen0298be82002-03-05 15:12:19 +0000939 new_init_action(SYSINIT, INIT_SCRIPT, console);
Erik Andersen7dc16072000-01-04 01:10:25 +0000940
Erik Andersene49d5ec2000-02-08 19:58:47 +0000941 return;
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000942#ifdef CONFIG_FEATURE_USE_INITTAB
Erik Andersen9e737252000-01-06 01:16:13 +0000943 }
Erik Andersen7dc16072000-01-04 01:10:25 +0000944
Eric Andersen0826b6b2002-07-03 23:50:16 +0000945 while (fgets(buf, INIT_BUFFS_SIZE, file) != NULL) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000946 foundIt = FALSE;
Eric Andersenb5966362000-05-31 20:04:38 +0000947 /* Skip leading spaces */
948 for (id = buf; *id == ' ' || *id == '\t'; id++);
949
950 /* Skip the line if it's a comment */
951 if (*id == '#' || *id == '\n')
Erik Andersene49d5ec2000-02-08 19:58:47 +0000952 continue;
Erik Andersen7dc16072000-01-04 01:10:25 +0000953
Erik Andersene49d5ec2000-02-08 19:58:47 +0000954 /* Trim the trailing \n */
Eric Andersenb5966362000-05-31 20:04:38 +0000955 eol = strrchr(id, '\n');
956 if (eol != NULL)
957 *eol = '\0';
Erik Andersen7dc16072000-01-04 01:10:25 +0000958
Erik Andersene49d5ec2000-02-08 19:58:47 +0000959 /* Keep a copy around for posterity's sake (and error msgs) */
960 strcpy(lineAsRead, buf);
961
Eric Andersenb5966362000-05-31 20:04:38 +0000962 /* Separate the ID field from the runlevels */
963 runlev = strchr(id, ':');
964 if (runlev == NULL || *(runlev + 1) == '\0') {
Eric Andersenb0cc0a62002-03-20 14:57:50 +0000965 message(LOG | CONSOLE, "\rBad inittab entry: %s\n", lineAsRead);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000966 continue;
967 } else {
Eric Andersenb5966362000-05-31 20:04:38 +0000968 *runlev = '\0';
969 ++runlev;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000970 }
971
Eric Andersenb5966362000-05-31 20:04:38 +0000972 /* Separate the runlevels from the action */
973 action = strchr(runlev, ':');
974 if (action == NULL || *(action + 1) == '\0') {
Eric Andersenb0cc0a62002-03-20 14:57:50 +0000975 message(LOG | CONSOLE, "\rBad inittab entry: %s\n", lineAsRead);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000976 continue;
977 } else {
Eric Andersenb5966362000-05-31 20:04:38 +0000978 *action = '\0';
979 ++action;
980 }
981
Eric Andersen0298be82002-03-05 15:12:19 +0000982 /* Separate the action from the command */
983 command = strchr(action, ':');
984 if (command == NULL || *(command + 1) == '\0') {
Eric Andersenb0cc0a62002-03-20 14:57:50 +0000985 message(LOG | CONSOLE, "\rBad inittab entry: %s\n", lineAsRead);
Eric Andersenb5966362000-05-31 20:04:38 +0000986 continue;
987 } else {
Eric Andersen0298be82002-03-05 15:12:19 +0000988 *command = '\0';
989 ++command;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000990 }
991
992 /* Ok, now process it */
993 a = actions;
994 while (a->name != 0) {
Eric Andersenb5966362000-05-31 20:04:38 +0000995 if (strcmp(a->name, action) == 0) {
996 if (*id != '\0') {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000997 strcpy(tmpConsole, "/dev/");
Glenn L McGrathbaf55a82002-08-22 18:22:10 +0000998 strncat(tmpConsole, id, INIT_BUFFS_SIZE - 6);
Eric Andersenb5966362000-05-31 20:04:38 +0000999 id = tmpConsole;
Erik Andersene49d5ec2000-02-08 19:58:47 +00001000 }
Eric Andersen0298be82002-03-05 15:12:19 +00001001 new_init_action(a->action, command, id);
Erik Andersene49d5ec2000-02-08 19:58:47 +00001002 foundIt = TRUE;
1003 }
1004 a++;
1005 }
Eric Andersen0298be82002-03-05 15:12:19 +00001006 if (foundIt == TRUE)
Erik Andersene49d5ec2000-02-08 19:58:47 +00001007 continue;
1008 else {
1009 /* Choke on an unknown action */
Eric Andersenb0cc0a62002-03-20 14:57:50 +00001010 message(LOG | CONSOLE, "\rBad inittab entry: %s\n", lineAsRead);
Erik Andersene49d5ec2000-02-08 19:58:47 +00001011 }
Erik Andersen7dc16072000-01-04 01:10:25 +00001012 }
Eric Andersend8636ca2002-05-15 22:19:09 +00001013 fclose(file);
Erik Andersene49d5ec2000-02-08 19:58:47 +00001014 return;
Glenn L McGrathbaf55a82002-08-22 18:22:10 +00001015#endif /* CONFIG_FEATURE_USE_INITTAB */
Erik Andersen7dc16072000-01-04 01:10:25 +00001016}
1017
Erik Andersene132f4b2000-02-09 04:16:43 +00001018
1019
Eric Andersen8a8fbb81999-10-26 00:18:56 +00001020extern int init_main(int argc, char **argv)
Eric Andersen0460ff21999-10-25 23:32:44 +00001021{
Eric Andersen0298be82002-03-05 15:12:19 +00001022 struct init_action *a;
Erik Andersene49d5ec2000-02-08 19:58:47 +00001023 pid_t wpid;
1024 int status;
Eric Andersen0460ff21999-10-25 23:32:44 +00001025
Eric Andersen730f8262001-12-17 23:13:08 +00001026 if (argc > 1 && !strcmp(argv[1], "-q")) {
Eric Andersen467a18b2002-01-25 23:13:06 +00001027 /* don't assume init's pid == 1 */
1028 long *pid = find_pid_by_name("init");
Glenn L McGrathbaf55a82002-08-22 18:22:10 +00001029
1030 if (!pid || *pid <= 0) {
Eric Andersen467a18b2002-01-25 23:13:06 +00001031 pid = find_pid_by_name("linuxrc");
Glenn L McGrathbaf55a82002-08-22 18:22:10 +00001032 if (!pid || *pid <= 0)
Eric Andersen467a18b2002-01-25 23:13:06 +00001033 error_msg_and_die("no process killed");
1034 }
1035 kill(*pid, SIGHUP);
Eric Andersen730f8262001-12-17 23:13:08 +00001036 exit(0);
1037 }
Eric Andersend73dc5b1999-11-10 23:13:02 +00001038#ifndef DEBUG_INIT
Erik Andersena51ecdd2000-02-24 18:09:58 +00001039 /* Expect to be invoked as init with PID=1 or be invoked as linuxrc */
1040 if (getpid() != 1
Eric Andersenbdfd0d72001-10-24 05:00:29 +00001041#ifdef CONFIG_FEATURE_INITRD
Glenn L McGrathbaf55a82002-08-22 18:22:10 +00001042 && strstr(applet_name, "linuxrc") == NULL
Erik Andersena51ecdd2000-02-24 18:09:58 +00001043#endif
Glenn L McGrathbaf55a82002-08-22 18:22:10 +00001044 ) {
1045 show_usage();
Erik Andersene49d5ec2000-02-08 19:58:47 +00001046 }
Erik Andersene49d5ec2000-02-08 19:58:47 +00001047 /* Set up sig handlers -- be sure to
1048 * clear all of these in run() */
Glenn L McGrathbaf55a82002-08-22 18:22:10 +00001049 signal(SIGHUP, exec_signal);
Erik Andersene49d5ec2000-02-08 19:58:47 +00001050 signal(SIGUSR1, halt_signal);
Eric Andersen4c95a282000-07-07 19:30:28 +00001051 signal(SIGUSR2, halt_signal);
Glenn L McGrathbaf55a82002-08-22 18:22:10 +00001052 signal(SIGINT, ctrlaltdel_signal);
Erik Andersene49d5ec2000-02-08 19:58:47 +00001053 signal(SIGTERM, reboot_signal);
Eric Andersened8a9be2001-11-30 19:10:58 +00001054 signal(SIGCONT, cont_handler);
1055 signal(SIGSTOP, stop_handler);
1056 signal(SIGTSTP, stop_handler);
Eric Andersen0460ff21999-10-25 23:32:44 +00001057
Erik Andersene49d5ec2000-02-08 19:58:47 +00001058 /* Turn off rebooting via CTL-ALT-DEL -- we get a
1059 * SIGINT on CAD so we can shut things down gracefully... */
Erik Andersen4f3f7572000-04-28 00:18:56 +00001060 init_reboot(RB_DISABLE_CAD);
Erik Andersene49d5ec2000-02-08 19:58:47 +00001061#endif
Erik Andersen05df2392000-01-13 04:43:48 +00001062
Erik Andersen298854f2000-03-23 01:09:18 +00001063 /* Figure out what kernel this is running */
1064 kernelVersion = get_kernel_revision();
1065
Eric Andersen599e3ce2002-07-03 11:08:10 +00001066 /* Figure out where the default console should be */
1067 console_init();
1068
Erik Andersene49d5ec2000-02-08 19:58:47 +00001069 /* Close whatever files are open, and reset the console. */
1070 close(0);
1071 close(1);
1072 close(2);
Eric Andersen72f9a422001-10-28 05:12:20 +00001073
Glenn L McGrathbaf55a82002-08-22 18:22:10 +00001074 if (device_open(console, O_RDWR | O_NOCTTY) == 0) {
1075 set_term(0);
Eric Andersen599e3ce2002-07-03 11:08:10 +00001076 close(0);
1077 }
1078
Erik Andersen983b51b2000-04-04 18:14:25 +00001079 chdir("/");
Erik Andersene49d5ec2000-02-08 19:58:47 +00001080 setsid();
Eric Andersencc8ed391999-10-05 16:24:54 +00001081
Erik Andersene49d5ec2000-02-08 19:58:47 +00001082 /* Make sure PATH is set to something sane */
Glenn L McGrathbaf55a82002-08-22 18:22:10 +00001083 putenv("PATH=" _PATH_STDPATH);
Eric Andersencc8ed391999-10-05 16:24:54 +00001084
Erik Andersene49d5ec2000-02-08 19:58:47 +00001085 /* Hello world */
Glenn L McGrathbaf55a82002-08-22 18:22:10 +00001086 message(MAYBE_CONSOLE | LOG, "\rinit started: %s\n", full_version);
Eric Andersencc8ed391999-10-05 16:24:54 +00001087
Erik Andersene49d5ec2000-02-08 19:58:47 +00001088 /* Make sure there is enough memory to do something useful. */
1089 check_memory();
Erik Andersen9e737252000-01-06 01:16:13 +00001090
Erik Andersene49d5ec2000-02-08 19:58:47 +00001091 /* Check if we are supposed to be in single user mode */
1092 if (argc > 1 && (!strcmp(argv[1], "single") ||
1093 !strcmp(argv[1], "-s") || !strcmp(argv[1], "1"))) {
Eric Andersen7e3bf6e2000-09-14 22:01:31 +00001094 /* Ask first then start a shell on tty2-4 */
Eric Andersen3bc2b202002-07-29 06:39:58 +00001095 new_init_action(ASKFIRST, LOGIN_SHELL, VC_2);
1096 new_init_action(ASKFIRST, LOGIN_SHELL, VC_3);
1097 new_init_action(ASKFIRST, LOGIN_SHELL, VC_4);
Erik Andersene49d5ec2000-02-08 19:58:47 +00001098 /* Start a shell on tty1 */
Eric Andersen0298be82002-03-05 15:12:19 +00001099 new_init_action(RESPAWN, LOGIN_SHELL, console);
Erik Andersene49d5ec2000-02-08 19:58:47 +00001100 } else {
1101 /* Not in single user mode -- see what inittab says */
Eric Andersen8a8fbb81999-10-26 00:18:56 +00001102
Eric Andersenbdfd0d72001-10-24 05:00:29 +00001103 /* NOTE that if CONFIG_FEATURE_USE_INITTAB is NOT defined,
Erik Andersene49d5ec2000-02-08 19:58:47 +00001104 * then parse_inittab() simply adds in some default
Eric Andersen77d92682001-05-23 20:32:09 +00001105 * actions(i.e., runs INIT_SCRIPT and then starts a pair
Erik Andersene49d5ec2000-02-08 19:58:47 +00001106 * of "askfirst" shells */
1107 parse_inittab();
Eric Andersend00c2621999-12-07 08:37:31 +00001108 }
Erik Andersen983b51b2000-04-04 18:14:25 +00001109
Eric Andersen7ef1a5b2001-03-20 17:39:08 +00001110 /* Make the command line just say "init" -- thats all, nothing else */
1111 fixup_argv(argc, argv, "init");
Eric Andersen219d6f51999-11-02 19:43:01 +00001112
Erik Andersene49d5ec2000-02-08 19:58:47 +00001113 /* Now run everything that needs to be run */
1114
1115 /* First run the sysinit command */
Eric Andersen1644db92001-09-05 20:18:15 +00001116 run_actions(SYSINIT);
Eric Andersen0298be82002-03-05 15:12:19 +00001117
Erik Andersene49d5ec2000-02-08 19:58:47 +00001118 /* Next run anything that wants to block */
Eric Andersen1644db92001-09-05 20:18:15 +00001119 run_actions(WAIT);
Eric Andersen0298be82002-03-05 15:12:19 +00001120
Erik Andersene49d5ec2000-02-08 19:58:47 +00001121 /* Next run anything to be run only once */
Eric Andersen0298be82002-03-05 15:12:19 +00001122 run_actions(ONCE);
1123
Erik Andersene49d5ec2000-02-08 19:58:47 +00001124 /* If there is nothing else to do, stop */
Eric Andersen0298be82002-03-05 15:12:19 +00001125 if (init_action_list == NULL) {
Glenn L McGrathbaf55a82002-08-22 18:22:10 +00001126 message(LOG | CONSOLE,
1127 "\rNo more tasks for init -- sleeping forever.\n");
Matt Kraai67a46402001-06-03 05:55:52 +00001128 loop_forever();
Erik Andersene49d5ec2000-02-08 19:58:47 +00001129 }
1130
1131 /* Now run the looping stuff for the rest of forever */
1132 while (1) {
Eric Andersen0298be82002-03-05 15:12:19 +00001133 /* run the respawn stuff */
1134 run_actions(RESPAWN);
1135
1136 /* run the askfirst stuff */
1137 run_actions(ASKFIRST);
1138
1139 /* Don't consume all CPU time -- sleep a bit */
1140 sleep(1);
1141
Erik Andersene49d5ec2000-02-08 19:58:47 +00001142 /* Wait for a child process to exit */
1143 wpid = wait(&status);
1144 if (wpid > 0) {
1145 /* Find out who died and clean up their corpse */
Eric Andersen0298be82002-03-05 15:12:19 +00001146 for (a = init_action_list; a; a = a->next) {
Erik Andersene49d5ec2000-02-08 19:58:47 +00001147 if (a->pid == wpid) {
Eric Andersen0298be82002-03-05 15:12:19 +00001148 /* Set the pid to 0 so that the process gets
1149 * restarted by run_actions() */
Erik Andersene49d5ec2000-02-08 19:58:47 +00001150 a->pid = 0;
Eric Andersen0298be82002-03-05 15:12:19 +00001151 message(LOG, "Process '%s' (pid %d) exited. "
1152 "Scheduling it for restart.\n", a->command, wpid);
Erik Andersene49d5ec2000-02-08 19:58:47 +00001153 }
1154 }
1155 }
Erik Andersene49d5ec2000-02-08 19:58:47 +00001156 }
Eric Andersencc8ed391999-10-05 16:24:54 +00001157}
Erik Andersen029011b2000-03-04 21:19:32 +00001158
1159/*
1160Local Variables:
1161c-file-style: "linux"
1162c-basic-offset: 4
1163tab-width: 4
1164End:
1165*/