blob: ab2d0ea91efb79ad0e4377f4af8a34c1e3414a99 [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;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000235 }
236 }
237 if ((device & LOG) && (log_fd >= 0)) {
238 va_start(arguments, fmt);
239 vdprintf(log_fd, fmt, arguments);
240 va_end(arguments);
241 }
Eric Andersen4c781471999-11-26 08:12:56 +0000242#endif
243
Erik Andersene49d5ec2000-02-08 19:58:47 +0000244 if (device & CONSOLE) {
245 /* Always send console messages to /dev/console so people will see them. */
Glenn L McGrathbaf55a82002-08-22 18:22:10 +0000246 if ((fd =
247 device_open(_PATH_CONSOLE,
248 O_WRONLY | O_NOCTTY | O_NDELAY)) >= 0) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000249 va_start(arguments, fmt);
250 vdprintf(fd, fmt, arguments);
251 va_end(arguments);
252 close(fd);
253 } else {
254 fprintf(stderr, "Bummer, can't print: ");
255 va_start(arguments, fmt);
256 vfprintf(stderr, fmt, arguments);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000257 va_end(arguments);
258 }
Eric Andersen3ae0c781999-11-04 01:13:21 +0000259 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000260}
261
Eric Andersen0460ff21999-10-25 23:32:44 +0000262/* Set terminal settings to reasonable defaults */
Eric Andersen3e6ff902001-03-09 21:24:12 +0000263static void set_term(int fd)
Eric Andersencc8ed391999-10-05 16:24:54 +0000264{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000265 struct termios tty;
Eric Andersencc8ed391999-10-05 16:24:54 +0000266
Erik Andersene49d5ec2000-02-08 19:58:47 +0000267 tcgetattr(fd, &tty);
Eric Andersencc8ed391999-10-05 16:24:54 +0000268
Erik Andersene49d5ec2000-02-08 19:58:47 +0000269 /* set control chars */
Glenn L McGrathbaf55a82002-08-22 18:22:10 +0000270 tty.c_cc[VINTR] = 3; /* C-c */
271 tty.c_cc[VQUIT] = 28; /* C-\ */
272 tty.c_cc[VERASE] = 127; /* C-? */
273 tty.c_cc[VKILL] = 21; /* C-u */
274 tty.c_cc[VEOF] = 4; /* C-d */
Eric Andersen3849f9b2000-07-10 19:56:47 +0000275 tty.c_cc[VSTART] = 17; /* C-q */
Glenn L McGrathbaf55a82002-08-22 18:22:10 +0000276 tty.c_cc[VSTOP] = 19; /* C-s */
277 tty.c_cc[VSUSP] = 26; /* C-z */
Eric Andersen02bc25b2000-07-06 21:29:32 +0000278
Erik Andersene49d5ec2000-02-08 19:58:47 +0000279 /* use line dicipline 0 */
280 tty.c_line = 0;
Eric Andersen7f1acfd1999-10-29 23:09:13 +0000281
Erik Andersene49d5ec2000-02-08 19:58:47 +0000282 /* Make it be sane */
Glenn L McGrathbaf55a82002-08-22 18:22:10 +0000283 tty.c_cflag &= CBAUD | CBAUDEX | CSIZE | CSTOPB | PARENB | PARODD;
284 tty.c_cflag |= CREAD | HUPCL | CLOCAL;
Eric Andersend8862922001-04-23 15:14:11 +0000285
Eric Andersen08b10341999-11-19 02:38:58 +0000286
Erik Andersene49d5ec2000-02-08 19:58:47 +0000287 /* input modes */
288 tty.c_iflag = ICRNL | IXON | IXOFF;
Eric Andersen08b10341999-11-19 02:38:58 +0000289
Erik Andersene49d5ec2000-02-08 19:58:47 +0000290 /* output modes */
291 tty.c_oflag = OPOST | ONLCR;
Eric Andersen7f1acfd1999-10-29 23:09:13 +0000292
Erik Andersene49d5ec2000-02-08 19:58:47 +0000293 /* local modes */
294 tty.c_lflag =
295 ISIG | ICANON | ECHO | ECHOE | ECHOK | ECHOCTL | ECHOKE | IEXTEN;
Eric Andersencc8ed391999-10-05 16:24:54 +0000296
Erik Andersene49d5ec2000-02-08 19:58:47 +0000297 tcsetattr(fd, TCSANOW, &tty);
Eric Andersencc8ed391999-10-05 16:24:54 +0000298}
299
Eric Andersenbc5941a2000-12-06 23:17:37 +0000300/* How much memory does this machine have?
301 Units are kBytes to avoid overflow on 4GB machines */
Eric Andersen74400cc2001-10-18 04:11:39 +0000302static int check_free_memory(void)
Eric Andersencc8ed391999-10-05 16:24:54 +0000303{
Erik Andersend07ee462000-02-21 21:26:32 +0000304 struct sysinfo info;
Glenn L McGrathbaf55a82002-08-22 18:22:10 +0000305 unsigned int result, u, s = 10;
Eric Andersencc8ed391999-10-05 16:24:54 +0000306
Erik Andersend07ee462000-02-21 21:26:32 +0000307 if (sysinfo(&info) != 0) {
Eric Andersen53cfb7e2001-01-31 17:29:47 +0000308 perror_msg("Error checking free memory");
Erik Andersene49d5ec2000-02-08 19:58:47 +0000309 return -1;
Eric Andersenabc7d591999-10-19 00:27:50 +0000310 }
Eric Andersenbc5941a2000-12-06 23:17:37 +0000311
312 /* Kernels 2.0.x and 2.2.x return info.mem_unit==0 with values in bytes.
313 * Kernels 2.4.0 return info.mem_unit in bytes. */
314 u = info.mem_unit;
Glenn L McGrathbaf55a82002-08-22 18:22:10 +0000315 if (u == 0)
316 u = 1;
317 while ((u & 1) == 0 && s > 0) {
318 u >>= 1;
319 s--;
320 }
321 result = (info.totalram >> s) + (info.totalswap >> s);
322 result = result * u;
323 if (result < 0)
324 result = INT_MAX;
Eric Andersenbc5941a2000-12-06 23:17:37 +0000325 return result;
Eric Andersencc8ed391999-10-05 16:24:54 +0000326}
327
Eric Andersen74400cc2001-10-18 04:11:39 +0000328static void console_init(void)
Eric Andersen0460ff21999-10-25 23:32:44 +0000329{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000330 int fd;
331 int tried_devcons = 0;
332 int tried_vtprimary = 0;
Erik Andersen029011b2000-03-04 21:19:32 +0000333 struct vt_stat vt;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000334 struct serial_struct sr;
335 char *s;
Eric Andersen0460ff21999-10-25 23:32:44 +0000336
Erik Andersene49d5ec2000-02-08 19:58:47 +0000337 if ((s = getenv("TERM")) != NULL) {
338 snprintf(termType, sizeof(termType) - 1, "TERM=%s", s);
339 }
Erik Andersenac6e71f2000-01-08 22:04:33 +0000340
Erik Andersene49d5ec2000-02-08 19:58:47 +0000341 if ((s = getenv("CONSOLE")) != NULL) {
Matt Kraai18447702001-05-18 21:24:58 +0000342 safe_strncpy(console, s, sizeof(console));
Erik Andersene49d5ec2000-02-08 19:58:47 +0000343 }
Eric Andersenfbb39c81999-11-08 17:00:52 +0000344#if #cpu(sparc)
Erik Andersene49d5ec2000-02-08 19:58:47 +0000345 /* sparc kernel supports console=tty[ab] parameter which is also
346 * passed to init, so catch it here */
347 else if ((s = getenv("console")) != NULL) {
348 /* remap tty[ab] to /dev/ttyS[01] */
349 if (strcmp(s, "ttya") == 0)
Matt Kraai439e3df2001-07-23 14:52:08 +0000350 safe_strncpy(console, SC_0, sizeof(console));
Erik Andersene49d5ec2000-02-08 19:58:47 +0000351 else if (strcmp(s, "ttyb") == 0)
Matt Kraai439e3df2001-07-23 14:52:08 +0000352 safe_strncpy(console, SC_1, sizeof(console));
Erik Andersene49d5ec2000-02-08 19:58:47 +0000353 }
Eric Andersen219d6f51999-11-02 19:43:01 +0000354#endif
Erik Andersene49d5ec2000-02-08 19:58:47 +0000355 else {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000356 /* 2.2 kernels: identify the real console backend and try to use it */
357 if (ioctl(0, TIOCGSERIAL, &sr) == 0) {
358 /* this is a serial console */
Matt Kraai439e3df2001-07-23 14:52:08 +0000359 snprintf(console, sizeof(console) - 1, SC_FORMAT, sr.line);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000360 } else if (ioctl(0, VT_GETSTATE, &vt) == 0) {
361 /* this is linux virtual tty */
Matt Kraai439e3df2001-07-23 14:52:08 +0000362 snprintf(console, sizeof(console) - 1, VC_FORMAT, vt.v_active);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000363 } else {
Matt Kraai18447702001-05-18 21:24:58 +0000364 safe_strncpy(console, _PATH_CONSOLE, sizeof(console));
Erik Andersene49d5ec2000-02-08 19:58:47 +0000365 tried_devcons++;
366 }
Eric Andersen07e52971999-11-07 07:38:08 +0000367 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000368
369 while ((fd = open(console, O_RDONLY | O_NONBLOCK)) < 0) {
370 /* Can't open selected console -- try /dev/console */
371 if (!tried_devcons) {
372 tried_devcons++;
Matt Kraai18447702001-05-18 21:24:58 +0000373 safe_strncpy(console, _PATH_CONSOLE, sizeof(console));
Erik Andersene49d5ec2000-02-08 19:58:47 +0000374 continue;
375 }
376 /* Can't open selected console -- try vt1 */
377 if (!tried_vtprimary) {
378 tried_vtprimary++;
Matt Kraai439e3df2001-07-23 14:52:08 +0000379 safe_strncpy(console, VC_1, sizeof(console));
Erik Andersene49d5ec2000-02-08 19:58:47 +0000380 continue;
381 }
382 break;
383 }
384 if (fd < 0) {
385 /* Perhaps we should panic here? */
Matt Kraai18447702001-05-18 21:24:58 +0000386 safe_strncpy(console, "/dev/null", sizeof(console));
Eric Andersen07e52971999-11-07 07:38:08 +0000387 } else {
Eric Andersen3bc2b202002-07-29 06:39:58 +0000388 /* check for serial console */
Erik Andersene49d5ec2000-02-08 19:58:47 +0000389 if (ioctl(0, TIOCGSERIAL, &sr) == 0) {
Erik Andersenfa4718e2000-02-21 19:25:12 +0000390 /* Force the TERM setting to vt102 for serial console --
Eric Andersen3bc2b202002-07-29 06:39:58 +0000391 * if TERM is set to linux (the default) */
Glenn L McGrathbaf55a82002-08-22 18:22:10 +0000392 if (strcmp(termType, "TERM=linux") == 0)
Matt Kraai18447702001-05-18 21:24:58 +0000393 safe_strncpy(termType, "TERM=vt102", sizeof(termType));
Erik Andersene49d5ec2000-02-08 19:58:47 +0000394 }
395 close(fd);
Eric Andersen07e52971999-11-07 07:38:08 +0000396 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000397 message(LOG, "console=%s\n", console);
Eric Andersen0460ff21999-10-25 23:32:44 +0000398}
Glenn L McGrathbaf55a82002-08-22 18:22:10 +0000399
Eric Andersen7ef1a5b2001-03-20 17:39:08 +0000400static void fixup_argv(int argc, char **argv, char *new_argv0)
401{
402 int len;
Glenn L McGrathbaf55a82002-08-22 18:22:10 +0000403
Eric Andersen7ef1a5b2001-03-20 17:39:08 +0000404 /* Fix up argv[0] to be certain we claim to be init */
405 len = strlen(argv[0]);
406 memset(argv[0], 0, len);
Eric Andersen72f9a422001-10-28 05:12:20 +0000407 safe_strncpy(argv[0], new_argv0, len + 1);
Eric Andersen7ef1a5b2001-03-20 17:39:08 +0000408
409 /* Wipe argv[1]-argv[N] so they don't clutter the ps listing */
410 len = 1;
411 while (argc > len) {
412 memset(argv[len], 0, strlen(argv[len]));
413 len++;
414 }
415}
416
Eric Andersen0298be82002-03-05 15:12:19 +0000417/* Make sure there is enough memory to do something useful. *
418 * Calls "swapon -a" if needed so be sure /etc/fstab is present... */
419static void check_memory(void)
Eric Andersen0460ff21999-10-25 23:32:44 +0000420{
Eric Andersen0298be82002-03-05 15:12:19 +0000421 struct stat statBuf;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000422
Eric Andersen0298be82002-03-05 15:12:19 +0000423 if (check_free_memory() > 1000)
424 return;
425
426#if !defined(__UCLIBC__) || defined(__UCLIBC_HAS_MMU__)
427 if (stat("/etc/fstab", &statBuf) == 0) {
428 /* swapon -a requires /proc typically */
429 system("/bin/mount -t proc proc /proc");
430 /* Try to turn on swap */
431 system("/sbin/swapon -a");
432 if (check_free_memory() < 1000)
433 goto goodnight;
434 } else
435 goto goodnight;
436 return;
Eric Andersen38624232001-01-25 00:04:16 +0000437#endif
438
Eric Andersen0298be82002-03-05 15:12:19 +0000439 goodnight:
Glenn L McGrathbaf55a82002-08-22 18:22:10 +0000440 message(CONSOLE, "\rSorry, your computer does not have enough memory.\n");
Eric Andersen0298be82002-03-05 15:12:19 +0000441 loop_forever();
442}
443
444static pid_t run(struct init_action *a)
445{
446 struct stat sb;
Eric Andersen0826b6b2002-07-03 23:50:16 +0000447 int i, j, junk;
Eric Andersen0298be82002-03-05 15:12:19 +0000448 pid_t pid, pgrp, tmp_pid;
Eric Andersen0826b6b2002-07-03 23:50:16 +0000449 char *s, *tmpCmd, *cmd[INIT_BUFFS_SIZE], *cmdpath;
Glenn L McGrathbaf55a82002-08-22 18:22:10 +0000450 char buf[INIT_BUFFS_SIZE + 6]; /* INIT_BUFFS_SIZE+strlen("exec ")+1 */
Eric Andersen0298be82002-03-05 15:12:19 +0000451 sigset_t nmask, omask;
Glenn L McGrathbaf55a82002-08-22 18:22:10 +0000452 char *environment[MAXENV + 1] = {
Eric Andersen7f197852001-03-16 01:14:04 +0000453 termType,
Erik Andersene49d5ec2000-02-08 19:58:47 +0000454 "HOME=/",
Eric Andersen72f9a422001-10-28 05:12:20 +0000455 "PATH=" _PATH_STDPATH,
Matt Kraai7bd773c2001-06-12 20:55:02 +0000456 "SHELL=" SHELL,
Eric Andersen7f197852001-03-16 01:14:04 +0000457 "USER=root",
458 NULL
Erik Andersene49d5ec2000-02-08 19:58:47 +0000459 };
Eric Andersen0298be82002-03-05 15:12:19 +0000460 static const char press_enter[] =
461#ifdef CUSTOMIZED_BANNER
462#include CUSTOMIZED_BANNER
463#endif
464 "\nPlease press Enter to activate this console. ";
Erik Andersenac6e71f2000-01-08 22:04:33 +0000465
Eric Andersen7f197852001-03-16 01:14:04 +0000466 /* inherit environment to the child, merging our values -andy */
Glenn L McGrathbaf55a82002-08-22 18:22:10 +0000467 for (i = 0; environ[i]; i++) {
468 for (j = 0; environment[j]; j++) {
Eric Andersen7f197852001-03-16 01:14:04 +0000469 s = strchr(environment[j], '=');
470 if (!strncmp(environ[i], environment[j], s - environment[j]))
471 break;
472 }
473 if (!environment[j]) {
474 environment[j++] = environ[i];
475 environment[j] = NULL;
476 }
Eric Andersen477aedd2001-02-20 18:01:50 +0000477 }
478
Eric Andersen0298be82002-03-05 15:12:19 +0000479 /* Block sigchild while forking. */
480 sigemptyset(&nmask);
481 sigaddset(&nmask, SIGCHLD);
482 sigprocmask(SIG_BLOCK, &nmask, &omask);
483
Glenn L McGrathbaf55a82002-08-22 18:22:10 +0000484 if ((pid = fork()) == 0) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000485 /* Clean up */
486 close(0);
487 close(1);
488 close(2);
Eric Andersen0298be82002-03-05 15:12:19 +0000489 sigprocmask(SIG_SETMASK, &omask, NULL);
490
Eric Andersen0298be82002-03-05 15:12:19 +0000491 /* Reset signal handlers that were set by the parent process */
Erik Andersene49d5ec2000-02-08 19:58:47 +0000492 signal(SIGUSR1, SIG_DFL);
493 signal(SIGUSR2, SIG_DFL);
Glenn L McGrathbaf55a82002-08-22 18:22:10 +0000494 signal(SIGINT, SIG_DFL);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000495 signal(SIGTERM, SIG_DFL);
Glenn L McGrathbaf55a82002-08-22 18:22:10 +0000496 signal(SIGHUP, SIG_DFL);
Eric Andersened8a9be2001-11-30 19:10:58 +0000497 signal(SIGCONT, SIG_DFL);
498 signal(SIGSTOP, SIG_DFL);
499 signal(SIGTSTP, SIG_DFL);
Eric Andersen2f6c04f1999-11-01 23:59:44 +0000500
Eric Andersen599e3ce2002-07-03 11:08:10 +0000501 /* Create a new session and make ourself the process
502 * group leader for non-interactive jobs */
Glenn L McGrathbaf55a82002-08-22 18:22:10 +0000503 if ((a->action & (RESPAWN)) == 0)
Eric Andersen599e3ce2002-07-03 11:08:10 +0000504 setsid();
505
Eric Andersen0298be82002-03-05 15:12:19 +0000506 /* Open the new terminal device */
Glenn L McGrathbaf55a82002-08-22 18:22:10 +0000507 if ((device_open(a->terminal, O_RDWR | O_NOCTTY)) < 0) {
Eric Andersen0298be82002-03-05 15:12:19 +0000508 if (stat(a->terminal, &sb) != 0) {
Eric Andersenb0cc0a62002-03-20 14:57:50 +0000509 message(LOG | CONSOLE, "\rdevice '%s' does not exist.\n",
Eric Andersen0298be82002-03-05 15:12:19 +0000510 a->terminal);
511 _exit(1);
Eric Andersen53f50612001-03-14 09:01:11 +0000512 }
Eric Andersenb0cc0a62002-03-20 14:57:50 +0000513 message(LOG | CONSOLE, "\rBummer, can't open %s\n", a->terminal);
Eric Andersen0298be82002-03-05 15:12:19 +0000514 _exit(1);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000515 }
Eric Andersen599e3ce2002-07-03 11:08:10 +0000516
517 /* Non-interactive jobs should not get a controling tty */
Glenn L McGrathbaf55a82002-08-22 18:22:10 +0000518 if ((a->action & (RESPAWN)) == 0)
519 (void) ioctl(0, TIOCSCTTY, 0);
Eric Andersen599e3ce2002-07-03 11:08:10 +0000520
Eric Andersen0298be82002-03-05 15:12:19 +0000521 /* Make sure the terminal will act fairly normal for us */
Erik Andersene49d5ec2000-02-08 19:58:47 +0000522 set_term(0);
Eric Andersen0826b6b2002-07-03 23:50:16 +0000523 /* Setup stdout, stderr for the new process so
Eric Andersen599e3ce2002-07-03 11:08:10 +0000524 * they point to the supplied terminal */
Eric Andersen0826b6b2002-07-03 23:50:16 +0000525 dup(0);
526 dup(0);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000527
Eric Andersen599e3ce2002-07-03 11:08:10 +0000528 /* For interactive jobs, create a new session
529 * and become the process group leader */
530 if ((a->action & (RESPAWN)))
531 setsid();
532
533 /* If the init Action requires us to wait, then force the
Eric Andersen0298be82002-03-05 15:12:19 +0000534 * supplied terminal to be the controlling tty. */
Glenn L McGrathbaf55a82002-08-22 18:22:10 +0000535 if (a->action & (SYSINIT | WAIT | CTRLALTDEL | SHUTDOWN | RESTART)) {
Eric Andersen0298be82002-03-05 15:12:19 +0000536
537 /* Now fork off another process to just hang around */
538 if ((pid = fork()) < 0) {
539 message(LOG | CONSOLE, "Can't fork!\n");
540 _exit(1);
541 }
542
543 if (pid > 0) {
544
545 /* We are the parent -- wait till the child is done */
546 signal(SIGINT, SIG_IGN);
547 signal(SIGTSTP, SIG_IGN);
548 signal(SIGQUIT, SIG_IGN);
549 signal(SIGCHLD, SIG_DFL);
550
551 /* Wait for child to exit */
Glenn L McGrathbaf55a82002-08-22 18:22:10 +0000552 while ((tmp_pid = waitpid(pid, &junk, 0)) != pid);
Eric Andersen0298be82002-03-05 15:12:19 +0000553
554 /* See if stealing the controlling tty back is necessary */
Eric Andersen0826b6b2002-07-03 23:50:16 +0000555 pgrp = tcgetpgrp(0);
Eric Andersen0298be82002-03-05 15:12:19 +0000556 if (pgrp != getpid())
557 _exit(0);
558
559 /* Use a temporary process to steal the controlling tty. */
560 if ((pid = fork()) < 0) {
Eric Andersenb0cc0a62002-03-20 14:57:50 +0000561 message(LOG | CONSOLE, "\rCan't fork!\n");
Eric Andersen0298be82002-03-05 15:12:19 +0000562 _exit(1);
Glenn L McGrathbaf55a82002-08-22 18:22:10 +0000563 }
Eric Andersen0298be82002-03-05 15:12:19 +0000564 if (pid == 0) {
565 setsid();
Eric Andersen0826b6b2002-07-03 23:50:16 +0000566 ioctl(0, TIOCSCTTY, 1);
Eric Andersen0298be82002-03-05 15:12:19 +0000567 _exit(0);
Glenn L McGrathbaf55a82002-08-22 18:22:10 +0000568 }
569 while ((tmp_pid = waitpid(pid, &junk, 0)) != pid) {
Eric Andersen0298be82002-03-05 15:12:19 +0000570 if (tmp_pid < 0 && errno == ECHILD)
571 break;
572 }
573 _exit(0);
574 }
575
576 /* Now fall though to actually execute things */
Eric Andersen0298be82002-03-05 15:12:19 +0000577 }
578
Erik Andersene132f4b2000-02-09 04:16:43 +0000579 /* See if any special /bin/sh requiring characters are present */
Eric Andersen0298be82002-03-05 15:12:19 +0000580 if (strpbrk(a->command, "~`!$^&*()=|\\{}[];\"'<>?") != NULL) {
Erik Andersene132f4b2000-02-09 04:16:43 +0000581 cmd[0] = SHELL;
582 cmd[1] = "-c";
Eric Andersen0826b6b2002-07-03 23:50:16 +0000583 strcat(strcpy(buf, "exec "), a->command);
Erik Andersene132f4b2000-02-09 04:16:43 +0000584 cmd[2] = buf;
585 cmd[3] = NULL;
586 } else {
587 /* Convert command (char*) into cmd (char**, one word per string) */
Eric Andersen0826b6b2002-07-03 23:50:16 +0000588 strcpy(buf, a->command);
Eric Andersen72f9a422001-10-28 05:12:20 +0000589 s = buf;
Glenn L McGrathbaf55a82002-08-22 18:22:10 +0000590 for (tmpCmd = buf, i = 0; (tmpCmd = strsep(&s, " \t")) != NULL;) {
Erik Andersene132f4b2000-02-09 04:16:43 +0000591 if (*tmpCmd != '\0') {
592 cmd[i] = tmpCmd;
Erik Andersene132f4b2000-02-09 04:16:43 +0000593 i++;
594 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000595 }
Erik Andersene132f4b2000-02-09 04:16:43 +0000596 cmd[i] = NULL;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000597 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000598
Eric Andersen7ef1a5b2001-03-20 17:39:08 +0000599 cmdpath = cmd[0];
Eric Andersen7e3bf6e2000-09-14 22:01:31 +0000600
Eric Andersen7ef1a5b2001-03-20 17:39:08 +0000601 /*
602 Interactive shells want to see a dash in argv[0]. This
603 typically is handled by login, argv will be setup this
604 way if a dash appears at the front of the command path
Eric Andersen7e3bf6e2000-09-14 22:01:31 +0000605 (like "-/bin/sh").
Eric Andersen7ef1a5b2001-03-20 17:39:08 +0000606 */
Eric Andersen7e3bf6e2000-09-14 22:01:31 +0000607
Eric Andersen7ef1a5b2001-03-20 17:39:08 +0000608 if (*cmdpath == '-') {
Eric Andersen7e3bf6e2000-09-14 22:01:31 +0000609
Eric Andersen7ef1a5b2001-03-20 17:39:08 +0000610 /* skip over the dash */
611 ++cmdpath;
Eric Andersen7e3bf6e2000-09-14 22:01:31 +0000612
Eric Andersen7ef1a5b2001-03-20 17:39:08 +0000613 /* find the last component in the command pathname */
614 s = get_last_path_component(cmdpath);
Eric Andersen7e3bf6e2000-09-14 22:01:31 +0000615
Eric Andersen7ef1a5b2001-03-20 17:39:08 +0000616 /* make a new argv[0] */
Glenn L McGrathbaf55a82002-08-22 18:22:10 +0000617 if ((cmd[0] = malloc(strlen(s) + 2)) == NULL) {
Eric Andersen7ef1a5b2001-03-20 17:39:08 +0000618 message(LOG | CONSOLE, "malloc failed");
619 cmd[0] = cmdpath;
620 } else {
621 cmd[0][0] = '-';
Glenn L McGrathbaf55a82002-08-22 18:22:10 +0000622 strcpy(cmd[0] + 1, s);
Eric Andersen7ef1a5b2001-03-20 17:39:08 +0000623 }
624 }
625
Eric Andersen0298be82002-03-05 15:12:19 +0000626 if (a->action & ASKFIRST) {
Eric Andersen7ef1a5b2001-03-20 17:39:08 +0000627 /*
628 * Save memory by not exec-ing anything large (like a shell)
629 * before the user wants it. This is critical if swap is not
630 * enabled and the system has low memory. Generally this will
631 * be run on the second virtual console, and the first will
632 * be allowed to start a shell or whatever an init script
633 * specifies.
634 */
Glenn L McGrathbaf55a82002-08-22 18:22:10 +0000635 messageND(LOG,
636 "Waiting for enter to start '%s' (pid %d, terminal %s)\n",
637 cmdpath, getpid(), a->terminal);
Eric Andersen7ef1a5b2001-03-20 17:39:08 +0000638 write(fileno(stdout), press_enter, sizeof(press_enter) - 1);
639 getc(stdin);
640 }
641
Eric Andersen7ef1a5b2001-03-20 17:39:08 +0000642 /* Log the process name and args */
Eric Andersenfedce062001-11-17 07:27:14 +0000643 messageND(LOG, "Starting pid %d, console %s: '%s'\n",
Glenn L McGrathbaf55a82002-08-22 18:22:10 +0000644 getpid(), a->terminal, cmdpath);
Eric Andersen7e3bf6e2000-09-14 22:01:31 +0000645
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000646#if defined CONFIG_FEATURE_INIT_COREDUMPS
Glenn L McGrathbaf55a82002-08-22 18:22:10 +0000647 if (stat(CORE_ENABLE_FLAG_FILE, &sb) == 0) {
Eric Andersen7ef1a5b2001-03-20 17:39:08 +0000648 struct rlimit limit;
Glenn L McGrathbaf55a82002-08-22 18:22:10 +0000649
Eric Andersen7ef1a5b2001-03-20 17:39:08 +0000650 limit.rlim_cur = RLIM_INFINITY;
651 limit.rlim_max = RLIM_INFINITY;
652 setrlimit(RLIMIT_CORE, &limit);
Erik Andersen983b51b2000-04-04 18:14:25 +0000653 }
Erik Andersen183da4a2000-04-04 18:36:37 +0000654#endif
Erik Andersen983b51b2000-04-04 18:14:25 +0000655
Erik Andersene49d5ec2000-02-08 19:58:47 +0000656 /* Now run it. The new program will take over this PID,
657 * so nothing further in init.c should be run. */
Eric Andersen7ef1a5b2001-03-20 17:39:08 +0000658 execve(cmdpath, cmd, environment);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000659
Eric Andersen7ef1a5b2001-03-20 17:39:08 +0000660 /* We're still here? Some error happened. */
Eric Andersenb0cc0a62002-03-20 14:57:50 +0000661 message(LOG | CONSOLE, "\rBummer, could not run '%s': %s\n", cmdpath,
Eric Andersen7ef1a5b2001-03-20 17:39:08 +0000662 strerror(errno));
Eric Andersen0298be82002-03-05 15:12:19 +0000663 _exit(-1);
Erik Andersen05df2392000-01-13 04:43:48 +0000664 }
Eric Andersen0298be82002-03-05 15:12:19 +0000665 sigprocmask(SIG_SETMASK, &omask, NULL);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000666 return pid;
Eric Andersen0460ff21999-10-25 23:32:44 +0000667}
668
Eric Andersen0298be82002-03-05 15:12:19 +0000669static int waitfor(struct init_action *a)
Erik Andersen0e3782f2000-01-07 02:54:55 +0000670{
Glenn L McGrathbaf55a82002-08-22 18:22:10 +0000671 int pid;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000672 int status, wpid;
Erik Andersen0e3782f2000-01-07 02:54:55 +0000673
Eric Andersen0298be82002-03-05 15:12:19 +0000674 pid = run(a);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000675 while (1) {
676 wpid = wait(&status);
Erik Andersene132f4b2000-02-09 04:16:43 +0000677 if (wpid > 0 && wpid != pid) {
678 continue;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000679 }
680 if (wpid == pid)
681 break;
Erik Andersen0e3782f2000-01-07 02:54:55 +0000682 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000683 return wpid;
Erik Andersen0e3782f2000-01-07 02:54:55 +0000684}
685
Eric Andersen0298be82002-03-05 15:12:19 +0000686/* Run all commands of a particular type */
687static void run_actions(int action)
Eric Andersen219d6f51999-11-02 19:43:01 +0000688{
Eric Andersen0298be82002-03-05 15:12:19 +0000689 struct init_action *a, *tmp;
Eric Andersen219d6f51999-11-02 19:43:01 +0000690
Eric Andersen0298be82002-03-05 15:12:19 +0000691 for (a = init_action_list; a; a = tmp) {
692 tmp = a->next;
Eric Andersenc97ec342001-04-03 18:01:51 +0000693 if (a->action == action) {
Glenn L McGrathbaf55a82002-08-22 18:22:10 +0000694 if (a->
695 action & (SYSINIT | WAIT | CTRLALTDEL | SHUTDOWN | RESTART)) {
Eric Andersen0298be82002-03-05 15:12:19 +0000696 waitfor(a);
697 delete_init_action(a);
698 } else if (a->action & ONCE) {
699 run(a);
700 delete_init_action(a);
Glenn L McGrathbaf55a82002-08-22 18:22:10 +0000701 } else if (a->action & (RESPAWN | ASKFIRST)) {
Eric Andersen0298be82002-03-05 15:12:19 +0000702 /* Only run stuff with pid==0. If they have
703 * a pid, that means it is still running */
704 if (a->pid == 0) {
705 a->pid = run(a);
706 }
707 }
Erik Andersene132f4b2000-02-09 04:16:43 +0000708 }
709 }
710}
711
712
Erik Andersen7dc16072000-01-04 01:10:25 +0000713#ifndef DEBUG_INIT
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000714static void shutdown_system(void)
Eric Andersencc8ed391999-10-05 16:24:54 +0000715{
Eric Andersen813d88c2001-10-28 22:49:48 +0000716 sigset_t block_signals;
Erik Andersene132f4b2000-02-09 04:16:43 +0000717
Eric Andersena9cc8962002-09-16 06:49:06 +0000718 /* run everything to be run at "shutdown". This is done _prior_
719 * to killing everything, in case people wish to use scripts to
720 * shut things down gracefully... */
721 run_actions(SHUTDOWN);
722
Eric Andersen72f9a422001-10-28 05:12:20 +0000723 /* first disable all our signals */
724 sigemptyset(&block_signals);
725 sigaddset(&block_signals, SIGHUP);
726 sigaddset(&block_signals, SIGCHLD);
727 sigaddset(&block_signals, SIGUSR1);
728 sigaddset(&block_signals, SIGUSR2);
729 sigaddset(&block_signals, SIGINT);
730 sigaddset(&block_signals, SIGTERM);
Eric Andersened8a9be2001-11-30 19:10:58 +0000731 sigaddset(&block_signals, SIGCONT);
732 sigaddset(&block_signals, SIGSTOP);
733 sigaddset(&block_signals, SIGTSTP);
Eric Andersen72f9a422001-10-28 05:12:20 +0000734 sigprocmask(SIG_BLOCK, &block_signals, NULL);
Erik Andersen7dc16072000-01-04 01:10:25 +0000735
Erik Andersene49d5ec2000-02-08 19:58:47 +0000736 /* Allow Ctrl-Alt-Del to reboot system. */
Erik Andersen4f3f7572000-04-28 00:18:56 +0000737 init_reboot(RB_ENABLE_CAD);
Erik Andersene132f4b2000-02-09 04:16:43 +0000738
Glenn L McGrathbaf55a82002-08-22 18:22:10 +0000739 message(CONSOLE | LOG, "\n\rThe system is going down NOW !!\n");
Eric Andersencf8c9cf1999-11-05 00:31:46 +0000740 sync();
Erik Andersene49d5ec2000-02-08 19:58:47 +0000741
742 /* Send signals to every process _except_ pid 1 */
Glenn L McGrathbaf55a82002-08-22 18:22:10 +0000743 message(CONSOLE | LOG, "\rSending SIGTERM to all processes.\n");
Erik Andersene49d5ec2000-02-08 19:58:47 +0000744 kill(-1, SIGTERM);
Erik Andersene132f4b2000-02-09 04:16:43 +0000745 sleep(1);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000746 sync();
747
Glenn L McGrathbaf55a82002-08-22 18:22:10 +0000748 message(CONSOLE | LOG, "\rSending SIGKILL to all processes.\n");
Erik Andersene49d5ec2000-02-08 19:58:47 +0000749 kill(-1, SIGKILL);
Erik Andersene132f4b2000-02-09 04:16:43 +0000750 sleep(1);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000751
Erik Andersene49d5ec2000-02-08 19:58:47 +0000752 sync();
Glenn L McGrathbaf55a82002-08-22 18:22:10 +0000753 if (kernelVersion > 0 && kernelVersion <= KERNEL_VERSION(2, 2, 11)) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000754 /* bdflush, kupdate not needed for kernels >2.2.11 */
755 bdflush(1, 0);
756 sync();
757 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000758}
759
Eric Andersen730f8262001-12-17 23:13:08 +0000760static void exec_signal(int sig)
761{
Eric Andersen0298be82002-03-05 15:12:19 +0000762 struct init_action *a, *tmp;
Eric Andersen5222d312002-07-03 05:15:23 +0000763 sigset_t unblock_signals;
Glenn L McGrathbaf55a82002-08-22 18:22:10 +0000764
Eric Andersen0298be82002-03-05 15:12:19 +0000765 for (a = init_action_list; a; a = tmp) {
766 tmp = a->next;
767 if (a->action & RESTART) {
Eric Andersen730f8262001-12-17 23:13:08 +0000768 shutdown_system();
Eric Andersen5222d312002-07-03 05:15:23 +0000769
770 /* unblock all signals, blocked in shutdown_system() */
771 sigemptyset(&unblock_signals);
772 sigaddset(&unblock_signals, SIGHUP);
773 sigaddset(&unblock_signals, SIGCHLD);
774 sigaddset(&unblock_signals, SIGUSR1);
775 sigaddset(&unblock_signals, SIGUSR2);
776 sigaddset(&unblock_signals, SIGINT);
777 sigaddset(&unblock_signals, SIGTERM);
778 sigaddset(&unblock_signals, SIGCONT);
779 sigaddset(&unblock_signals, SIGSTOP);
780 sigaddset(&unblock_signals, SIGTSTP);
781 sigprocmask(SIG_UNBLOCK, &unblock_signals, NULL);
782
Glenn L McGrathbaf55a82002-08-22 18:22:10 +0000783 message(CONSOLE | LOG, "\rTrying to re-exec %s\n", a->command);
Eric Andersen0298be82002-03-05 15:12:19 +0000784 execl(a->command, a->command, NULL);
Glenn L McGrathbaf55a82002-08-22 18:22:10 +0000785
786 message(CONSOLE | LOG, "\rexec of '%s' failed: %s\n",
Eric Andersen11309762002-08-26 21:36:32 +0000787 a->command, strerror(errno));
Eric Andersen730f8262001-12-17 23:13:08 +0000788 sync();
789 sleep(2);
790 init_reboot(RB_HALT_SYSTEM);
791 loop_forever();
792 }
793 }
794}
795
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000796static void halt_signal(int sig)
Eric Andersencc8ed391999-10-05 16:24:54 +0000797{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000798 shutdown_system();
Glenn L McGrathbaf55a82002-08-22 18:22:10 +0000799 message(CONSOLE | LOG,
Eric Andersenf435a912001-11-20 05:42:57 +0000800#if #cpu(s390)
801 /* Seems the s390 console is Wierd(tm). */
Eric Andersenb0cc0a62002-03-20 14:57:50 +0000802 "\rThe system is halted. You may reboot now.\n"
Eric Andersenf435a912001-11-20 05:42:57 +0000803#else
Eric Andersen3bc2b202002-07-29 06:39:58 +0000804 "\rThe system is halted. Press Reset or turn off power\n"
Eric Andersenf435a912001-11-20 05:42:57 +0000805#endif
Glenn L McGrathbaf55a82002-08-22 18:22:10 +0000806 );
Erik Andersene49d5ec2000-02-08 19:58:47 +0000807 sync();
Erik Andersen3fe39dc2000-01-25 18:13:53 +0000808
Erik Andersene49d5ec2000-02-08 19:58:47 +0000809 /* allow time for last message to reach serial console */
Erik Andersene132f4b2000-02-09 04:16:43 +0000810 sleep(2);
Erik Andersen3fe39dc2000-01-25 18:13:53 +0000811
Glenn L McGrathbaf55a82002-08-22 18:22:10 +0000812 if (sig == SIGUSR2 && kernelVersion >= KERNEL_VERSION(2, 2, 0))
Erik Andersen4f3f7572000-04-28 00:18:56 +0000813 init_reboot(RB_POWER_OFF);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000814 else
Erik Andersen4f3f7572000-04-28 00:18:56 +0000815 init_reboot(RB_HALT_SYSTEM);
Matt Kraai67a46402001-06-03 05:55:52 +0000816
817 loop_forever();
Eric Andersencc8ed391999-10-05 16:24:54 +0000818}
819
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000820static void reboot_signal(int sig)
Eric Andersencc8ed391999-10-05 16:24:54 +0000821{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000822 shutdown_system();
Glenn L McGrathbaf55a82002-08-22 18:22:10 +0000823 message(CONSOLE | LOG, "\rPlease stand by while rebooting the system.\n");
Erik Andersene49d5ec2000-02-08 19:58:47 +0000824 sync();
Erik Andersen3fe39dc2000-01-25 18:13:53 +0000825
Erik Andersene49d5ec2000-02-08 19:58:47 +0000826 /* allow time for last message to reach serial console */
827 sleep(2);
Erik Andersen3fe39dc2000-01-25 18:13:53 +0000828
Erik Andersen4f3f7572000-04-28 00:18:56 +0000829 init_reboot(RB_AUTOBOOT);
Matt Kraai67a46402001-06-03 05:55:52 +0000830
831 loop_forever();
Eric Andersencc8ed391999-10-05 16:24:54 +0000832}
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000833
Eric Andersenc97ec342001-04-03 18:01:51 +0000834static void ctrlaltdel_signal(int sig)
835{
836 run_actions(CTRLALTDEL);
837}
838
Eric Andersen0298be82002-03-05 15:12:19 +0000839/* The SIGSTOP & SIGTSTP handler */
Eric Andersened8a9be2001-11-30 19:10:58 +0000840static void stop_handler(int sig)
841{
Eric Andersen0298be82002-03-05 15:12:19 +0000842 int saved_errno = errno;
Eric Andersened8a9be2001-11-30 19:10:58 +0000843
844 got_cont = 0;
Glenn L McGrathbaf55a82002-08-22 18:22:10 +0000845 while (!got_cont)
846 pause();
Eric Andersened8a9be2001-11-30 19:10:58 +0000847 got_cont = 0;
848 errno = saved_errno;
849}
850
Glenn L McGrathbaf55a82002-08-22 18:22:10 +0000851/* The SIGCONT handler */
Eric Andersened8a9be2001-11-30 19:10:58 +0000852static void cont_handler(int sig)
853{
854 got_cont = 1;
855}
856
Erik Andersene49d5ec2000-02-08 19:58:47 +0000857#endif /* ! DEBUG_INIT */
Erik Andersende552872000-01-23 01:34:05 +0000858
Eric Andersen0298be82002-03-05 15:12:19 +0000859static void new_init_action(int action, char *command, char *cons)
Erik Andersen7dc16072000-01-04 01:10:25 +0000860{
Eric Andersen0298be82002-03-05 15:12:19 +0000861 struct init_action *new_action, *a;
Erik Andersen9e737252000-01-06 01:16:13 +0000862
Erik Andersene49d5ec2000-02-08 19:58:47 +0000863 if (*cons == '\0')
864 cons = console;
Erik Andersen9e737252000-01-06 01:16:13 +0000865
Eric Andersen3bc2b202002-07-29 06:39:58 +0000866 /* do not run entries if console device is not available */
Glenn L McGrathbaf55a82002-08-22 18:22:10 +0000867 if (access(cons, R_OK | W_OK))
Erik Andersene49d5ec2000-02-08 19:58:47 +0000868 return;
Eric Andersen0298be82002-03-05 15:12:19 +0000869 if (strcmp(cons, "/dev/null") == 0 && (action & ASKFIRST))
Eric Andersen1644db92001-09-05 20:18:15 +0000870 return;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000871
Eric Andersen0298be82002-03-05 15:12:19 +0000872 new_action = calloc((size_t) (1), sizeof(struct init_action));
873 if (!new_action) {
Eric Andersenb0cc0a62002-03-20 14:57:50 +0000874 message(LOG | CONSOLE, "\rMemory allocation failure\n");
Matt Kraai67a46402001-06-03 05:55:52 +0000875 loop_forever();
Erik Andersene49d5ec2000-02-08 19:58:47 +0000876 }
Eric Andersen0298be82002-03-05 15:12:19 +0000877
878 /* Append to the end of the list */
Glenn L McGrathbaf55a82002-08-22 18:22:10 +0000879 for (a = init_action_list; a && a->next; a = a->next);
Eric Andersen1644db92001-09-05 20:18:15 +0000880 if (a) {
Eric Andersen0298be82002-03-05 15:12:19 +0000881 a->next = new_action;
Eric Andersen1644db92001-09-05 20:18:15 +0000882 } else {
Eric Andersen0298be82002-03-05 15:12:19 +0000883 init_action_list = new_action;
Eric Andersen1644db92001-09-05 20:18:15 +0000884 }
Eric Andersen0826b6b2002-07-03 23:50:16 +0000885 strcpy(new_action->command, command);
Eric Andersen0298be82002-03-05 15:12:19 +0000886 new_action->action = action;
Eric Andersen0826b6b2002-07-03 23:50:16 +0000887 strcpy(new_action->terminal, cons);
Eric Andersen0298be82002-03-05 15:12:19 +0000888 new_action->pid = 0;
Glenn L McGrathbaf55a82002-08-22 18:22:10 +0000889/* message(LOG|CONSOLE, "command='%s' action='%d' terminal='%s'\n",
890 new_action->command, new_action->action, new_action->terminal); */
Erik Andersen7dc16072000-01-04 01:10:25 +0000891}
892
Glenn L McGrathbaf55a82002-08-22 18:22:10 +0000893static void delete_init_action(struct init_action *action)
Erik Andersen7dc16072000-01-04 01:10:25 +0000894{
Eric Andersen0298be82002-03-05 15:12:19 +0000895 struct init_action *a, *b = NULL;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000896
Eric Andersen0298be82002-03-05 15:12:19 +0000897 for (a = init_action_list; a; b = a, a = a->next) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000898 if (a == action) {
899 if (b == NULL) {
Eric Andersen0298be82002-03-05 15:12:19 +0000900 init_action_list = a->next;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000901 } else {
Eric Andersen0298be82002-03-05 15:12:19 +0000902 b->next = a->next;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000903 }
904 free(a);
905 break;
906 }
Erik Andersen7dc16072000-01-04 01:10:25 +0000907 }
Erik Andersen7dc16072000-01-04 01:10:25 +0000908}
909
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000910/* NOTE that if CONFIG_FEATURE_USE_INITTAB is NOT defined,
Erik Andersen9e737252000-01-06 01:16:13 +0000911 * then parse_inittab() simply adds in some default
Eric Andersen77d92682001-05-23 20:32:09 +0000912 * actions(i.e., runs INIT_SCRIPT and then starts a pair
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000913 * of "askfirst" shells). If CONFIG_FEATURE_USE_INITTAB
Erik Andersen812d4662000-01-07 18:30:40 +0000914 * _is_ defined, but /etc/inittab is missing, this
915 * results in the same set of default behaviors.
Glenn L McGrathbaf55a82002-08-22 18:22:10 +0000916 */
Eric Andersen3e6ff902001-03-09 21:24:12 +0000917static void parse_inittab(void)
Erik Andersen7dc16072000-01-04 01:10:25 +0000918{
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000919#ifdef CONFIG_FEATURE_USE_INITTAB
Erik Andersene49d5ec2000-02-08 19:58:47 +0000920 FILE *file;
Glenn L McGrathbaf55a82002-08-22 18:22:10 +0000921 char buf[INIT_BUFFS_SIZE], lineAsRead[INIT_BUFFS_SIZE],
922 tmpConsole[INIT_BUFFS_SIZE];
Eric Andersen0298be82002-03-05 15:12:19 +0000923 char *id, *runlev, *action, *command, *eol;
924 const struct init_action_type *a = actions;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000925 int foundIt;
Erik Andersen7dc16072000-01-04 01:10:25 +0000926
927
Erik Andersene49d5ec2000-02-08 19:58:47 +0000928 file = fopen(INITTAB, "r");
929 if (file == NULL) {
930 /* No inittab file -- set up some default behavior */
Erik Andersen9e737252000-01-06 01:16:13 +0000931#endif
Eric Andersenc97ec342001-04-03 18:01:51 +0000932 /* Reboot on Ctrl-Alt-Del */
Eric Andersen0298be82002-03-05 15:12:19 +0000933 new_init_action(CTRLALTDEL, "/sbin/reboot", console);
Eric Andersen1644db92001-09-05 20:18:15 +0000934 /* Umount all filesystems on halt/reboot */
Eric Andersen0298be82002-03-05 15:12:19 +0000935 new_init_action(SHUTDOWN, "/bin/umount -a -r", console);
Eric Andersen72f9a422001-10-28 05:12:20 +0000936#if !defined(__UCLIBC__) || defined(__UCLIBC_HAS_MMU__)
Eric Andersen1644db92001-09-05 20:18:15 +0000937 /* Swapoff on halt/reboot */
Eric Andersen0298be82002-03-05 15:12:19 +0000938 new_init_action(SHUTDOWN, "/sbin/swapoff -a", console);
Eric Andersen1644db92001-09-05 20:18:15 +0000939#endif
Eric Andersen730f8262001-12-17 23:13:08 +0000940 /* Prepare to restart init when a HUP is received */
Eric Andersen0298be82002-03-05 15:12:19 +0000941 new_init_action(RESTART, "/sbin/init", console);
Eric Andersen3bc2b202002-07-29 06:39:58 +0000942 /* Askfirst shell on tty1-4 */
Eric Andersen0298be82002-03-05 15:12:19 +0000943 new_init_action(ASKFIRST, LOGIN_SHELL, console);
Eric Andersen3bc2b202002-07-29 06:39:58 +0000944 new_init_action(ASKFIRST, LOGIN_SHELL, VC_2);
945 new_init_action(ASKFIRST, LOGIN_SHELL, VC_3);
946 new_init_action(ASKFIRST, LOGIN_SHELL, VC_4);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000947 /* sysinit */
Eric Andersen0298be82002-03-05 15:12:19 +0000948 new_init_action(SYSINIT, INIT_SCRIPT, console);
Erik Andersen7dc16072000-01-04 01:10:25 +0000949
Erik Andersene49d5ec2000-02-08 19:58:47 +0000950 return;
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000951#ifdef CONFIG_FEATURE_USE_INITTAB
Erik Andersen9e737252000-01-06 01:16:13 +0000952 }
Erik Andersen7dc16072000-01-04 01:10:25 +0000953
Eric Andersen0826b6b2002-07-03 23:50:16 +0000954 while (fgets(buf, INIT_BUFFS_SIZE, file) != NULL) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000955 foundIt = FALSE;
Eric Andersenb5966362000-05-31 20:04:38 +0000956 /* Skip leading spaces */
957 for (id = buf; *id == ' ' || *id == '\t'; id++);
958
959 /* Skip the line if it's a comment */
960 if (*id == '#' || *id == '\n')
Erik Andersene49d5ec2000-02-08 19:58:47 +0000961 continue;
Erik Andersen7dc16072000-01-04 01:10:25 +0000962
Erik Andersene49d5ec2000-02-08 19:58:47 +0000963 /* Trim the trailing \n */
Eric Andersenb5966362000-05-31 20:04:38 +0000964 eol = strrchr(id, '\n');
965 if (eol != NULL)
966 *eol = '\0';
Erik Andersen7dc16072000-01-04 01:10:25 +0000967
Erik Andersene49d5ec2000-02-08 19:58:47 +0000968 /* Keep a copy around for posterity's sake (and error msgs) */
969 strcpy(lineAsRead, buf);
970
Eric Andersenb5966362000-05-31 20:04:38 +0000971 /* Separate the ID field from the runlevels */
972 runlev = strchr(id, ':');
973 if (runlev == NULL || *(runlev + 1) == '\0') {
Eric Andersenb0cc0a62002-03-20 14:57:50 +0000974 message(LOG | CONSOLE, "\rBad inittab entry: %s\n", lineAsRead);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000975 continue;
976 } else {
Eric Andersenb5966362000-05-31 20:04:38 +0000977 *runlev = '\0';
978 ++runlev;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000979 }
980
Eric Andersenb5966362000-05-31 20:04:38 +0000981 /* Separate the runlevels from the action */
982 action = strchr(runlev, ':');
983 if (action == NULL || *(action + 1) == '\0') {
Eric Andersenb0cc0a62002-03-20 14:57:50 +0000984 message(LOG | CONSOLE, "\rBad inittab entry: %s\n", lineAsRead);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000985 continue;
986 } else {
Eric Andersenb5966362000-05-31 20:04:38 +0000987 *action = '\0';
988 ++action;
989 }
990
Eric Andersen0298be82002-03-05 15:12:19 +0000991 /* Separate the action from the command */
992 command = strchr(action, ':');
993 if (command == NULL || *(command + 1) == '\0') {
Eric Andersenb0cc0a62002-03-20 14:57:50 +0000994 message(LOG | CONSOLE, "\rBad inittab entry: %s\n", lineAsRead);
Eric Andersenb5966362000-05-31 20:04:38 +0000995 continue;
996 } else {
Eric Andersen0298be82002-03-05 15:12:19 +0000997 *command = '\0';
998 ++command;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000999 }
1000
1001 /* Ok, now process it */
1002 a = actions;
1003 while (a->name != 0) {
Eric Andersenb5966362000-05-31 20:04:38 +00001004 if (strcmp(a->name, action) == 0) {
1005 if (*id != '\0') {
Erik Andersene49d5ec2000-02-08 19:58:47 +00001006 strcpy(tmpConsole, "/dev/");
Glenn L McGrathbaf55a82002-08-22 18:22:10 +00001007 strncat(tmpConsole, id, INIT_BUFFS_SIZE - 6);
Eric Andersenb5966362000-05-31 20:04:38 +00001008 id = tmpConsole;
Erik Andersene49d5ec2000-02-08 19:58:47 +00001009 }
Eric Andersen0298be82002-03-05 15:12:19 +00001010 new_init_action(a->action, command, id);
Erik Andersene49d5ec2000-02-08 19:58:47 +00001011 foundIt = TRUE;
1012 }
1013 a++;
1014 }
Eric Andersen0298be82002-03-05 15:12:19 +00001015 if (foundIt == TRUE)
Erik Andersene49d5ec2000-02-08 19:58:47 +00001016 continue;
1017 else {
1018 /* Choke on an unknown action */
Eric Andersenb0cc0a62002-03-20 14:57:50 +00001019 message(LOG | CONSOLE, "\rBad inittab entry: %s\n", lineAsRead);
Erik Andersene49d5ec2000-02-08 19:58:47 +00001020 }
Erik Andersen7dc16072000-01-04 01:10:25 +00001021 }
Eric Andersend8636ca2002-05-15 22:19:09 +00001022 fclose(file);
Erik Andersene49d5ec2000-02-08 19:58:47 +00001023 return;
Glenn L McGrathbaf55a82002-08-22 18:22:10 +00001024#endif /* CONFIG_FEATURE_USE_INITTAB */
Erik Andersen7dc16072000-01-04 01:10:25 +00001025}
1026
Erik Andersene132f4b2000-02-09 04:16:43 +00001027
1028
Eric Andersen8a8fbb81999-10-26 00:18:56 +00001029extern int init_main(int argc, char **argv)
Eric Andersen0460ff21999-10-25 23:32:44 +00001030{
Eric Andersen0298be82002-03-05 15:12:19 +00001031 struct init_action *a;
Erik Andersene49d5ec2000-02-08 19:58:47 +00001032 pid_t wpid;
1033 int status;
Eric Andersen0460ff21999-10-25 23:32:44 +00001034
Eric Andersen730f8262001-12-17 23:13:08 +00001035 if (argc > 1 && !strcmp(argv[1], "-q")) {
Eric Andersen467a18b2002-01-25 23:13:06 +00001036 /* don't assume init's pid == 1 */
1037 long *pid = find_pid_by_name("init");
Glenn L McGrathbaf55a82002-08-22 18:22:10 +00001038
1039 if (!pid || *pid <= 0) {
Eric Andersen467a18b2002-01-25 23:13:06 +00001040 pid = find_pid_by_name("linuxrc");
Glenn L McGrathbaf55a82002-08-22 18:22:10 +00001041 if (!pid || *pid <= 0)
Eric Andersen467a18b2002-01-25 23:13:06 +00001042 error_msg_and_die("no process killed");
1043 }
1044 kill(*pid, SIGHUP);
Eric Andersen730f8262001-12-17 23:13:08 +00001045 exit(0);
1046 }
Eric Andersend73dc5b1999-11-10 23:13:02 +00001047#ifndef DEBUG_INIT
Erik Andersena51ecdd2000-02-24 18:09:58 +00001048 /* Expect to be invoked as init with PID=1 or be invoked as linuxrc */
1049 if (getpid() != 1
Eric Andersenbdfd0d72001-10-24 05:00:29 +00001050#ifdef CONFIG_FEATURE_INITRD
Glenn L McGrathbaf55a82002-08-22 18:22:10 +00001051 && strstr(applet_name, "linuxrc") == NULL
Erik Andersena51ecdd2000-02-24 18:09:58 +00001052#endif
Glenn L McGrathbaf55a82002-08-22 18:22:10 +00001053 ) {
1054 show_usage();
Erik Andersene49d5ec2000-02-08 19:58:47 +00001055 }
Erik Andersene49d5ec2000-02-08 19:58:47 +00001056 /* Set up sig handlers -- be sure to
1057 * clear all of these in run() */
Glenn L McGrathbaf55a82002-08-22 18:22:10 +00001058 signal(SIGHUP, exec_signal);
Erik Andersene49d5ec2000-02-08 19:58:47 +00001059 signal(SIGUSR1, halt_signal);
Eric Andersen4c95a282000-07-07 19:30:28 +00001060 signal(SIGUSR2, halt_signal);
Glenn L McGrathbaf55a82002-08-22 18:22:10 +00001061 signal(SIGINT, ctrlaltdel_signal);
Erik Andersene49d5ec2000-02-08 19:58:47 +00001062 signal(SIGTERM, reboot_signal);
Eric Andersened8a9be2001-11-30 19:10:58 +00001063 signal(SIGCONT, cont_handler);
1064 signal(SIGSTOP, stop_handler);
1065 signal(SIGTSTP, stop_handler);
Eric Andersen186685d2002-09-12 15:44:53 +00001066 signal(SIGCHLD, SIG_IGN);
Eric Andersen0460ff21999-10-25 23:32:44 +00001067
Erik Andersene49d5ec2000-02-08 19:58:47 +00001068 /* Turn off rebooting via CTL-ALT-DEL -- we get a
1069 * SIGINT on CAD so we can shut things down gracefully... */
Erik Andersen4f3f7572000-04-28 00:18:56 +00001070 init_reboot(RB_DISABLE_CAD);
Erik Andersene49d5ec2000-02-08 19:58:47 +00001071#endif
Erik Andersen05df2392000-01-13 04:43:48 +00001072
Erik Andersen298854f2000-03-23 01:09:18 +00001073 /* Figure out what kernel this is running */
1074 kernelVersion = get_kernel_revision();
1075
Eric Andersen599e3ce2002-07-03 11:08:10 +00001076 /* Figure out where the default console should be */
1077 console_init();
1078
Erik Andersene49d5ec2000-02-08 19:58:47 +00001079 /* Close whatever files are open, and reset the console. */
1080 close(0);
1081 close(1);
1082 close(2);
Eric Andersen72f9a422001-10-28 05:12:20 +00001083
Glenn L McGrathbaf55a82002-08-22 18:22:10 +00001084 if (device_open(console, O_RDWR | O_NOCTTY) == 0) {
1085 set_term(0);
Eric Andersen599e3ce2002-07-03 11:08:10 +00001086 close(0);
1087 }
1088
Erik Andersen983b51b2000-04-04 18:14:25 +00001089 chdir("/");
Erik Andersene49d5ec2000-02-08 19:58:47 +00001090 setsid();
Eric Andersencc8ed391999-10-05 16:24:54 +00001091
Erik Andersene49d5ec2000-02-08 19:58:47 +00001092 /* Make sure PATH is set to something sane */
Glenn L McGrathbaf55a82002-08-22 18:22:10 +00001093 putenv("PATH=" _PATH_STDPATH);
Eric Andersencc8ed391999-10-05 16:24:54 +00001094
Erik Andersene49d5ec2000-02-08 19:58:47 +00001095 /* Hello world */
Glenn L McGrathbaf55a82002-08-22 18:22:10 +00001096 message(MAYBE_CONSOLE | LOG, "\rinit started: %s\n", full_version);
Eric Andersencc8ed391999-10-05 16:24:54 +00001097
Erik Andersene49d5ec2000-02-08 19:58:47 +00001098 /* Make sure there is enough memory to do something useful. */
1099 check_memory();
Erik Andersen9e737252000-01-06 01:16:13 +00001100
Erik Andersene49d5ec2000-02-08 19:58:47 +00001101 /* Check if we are supposed to be in single user mode */
1102 if (argc > 1 && (!strcmp(argv[1], "single") ||
1103 !strcmp(argv[1], "-s") || !strcmp(argv[1], "1"))) {
Eric Andersen7e3bf6e2000-09-14 22:01:31 +00001104 /* Ask first then start a shell on tty2-4 */
Eric Andersen3bc2b202002-07-29 06:39:58 +00001105 new_init_action(ASKFIRST, LOGIN_SHELL, VC_2);
1106 new_init_action(ASKFIRST, LOGIN_SHELL, VC_3);
1107 new_init_action(ASKFIRST, LOGIN_SHELL, VC_4);
Erik Andersene49d5ec2000-02-08 19:58:47 +00001108 /* Start a shell on tty1 */
Eric Andersen0298be82002-03-05 15:12:19 +00001109 new_init_action(RESPAWN, LOGIN_SHELL, console);
Erik Andersene49d5ec2000-02-08 19:58:47 +00001110 } else {
1111 /* Not in single user mode -- see what inittab says */
Eric Andersen8a8fbb81999-10-26 00:18:56 +00001112
Eric Andersenbdfd0d72001-10-24 05:00:29 +00001113 /* NOTE that if CONFIG_FEATURE_USE_INITTAB is NOT defined,
Erik Andersene49d5ec2000-02-08 19:58:47 +00001114 * then parse_inittab() simply adds in some default
Eric Andersen77d92682001-05-23 20:32:09 +00001115 * actions(i.e., runs INIT_SCRIPT and then starts a pair
Erik Andersene49d5ec2000-02-08 19:58:47 +00001116 * of "askfirst" shells */
1117 parse_inittab();
Eric Andersend00c2621999-12-07 08:37:31 +00001118 }
Erik Andersen983b51b2000-04-04 18:14:25 +00001119
Eric Andersen7ef1a5b2001-03-20 17:39:08 +00001120 /* Make the command line just say "init" -- thats all, nothing else */
1121 fixup_argv(argc, argv, "init");
Eric Andersen219d6f51999-11-02 19:43:01 +00001122
Erik Andersene49d5ec2000-02-08 19:58:47 +00001123 /* Now run everything that needs to be run */
1124
1125 /* First run the sysinit command */
Eric Andersen1644db92001-09-05 20:18:15 +00001126 run_actions(SYSINIT);
Eric Andersen0298be82002-03-05 15:12:19 +00001127
Erik Andersene49d5ec2000-02-08 19:58:47 +00001128 /* Next run anything that wants to block */
Eric Andersen1644db92001-09-05 20:18:15 +00001129 run_actions(WAIT);
Eric Andersen0298be82002-03-05 15:12:19 +00001130
Erik Andersene49d5ec2000-02-08 19:58:47 +00001131 /* Next run anything to be run only once */
Eric Andersen0298be82002-03-05 15:12:19 +00001132 run_actions(ONCE);
1133
Erik Andersene49d5ec2000-02-08 19:58:47 +00001134 /* If there is nothing else to do, stop */
Eric Andersen0298be82002-03-05 15:12:19 +00001135 if (init_action_list == NULL) {
Glenn L McGrathbaf55a82002-08-22 18:22:10 +00001136 message(LOG | CONSOLE,
1137 "\rNo more tasks for init -- sleeping forever.\n");
Matt Kraai67a46402001-06-03 05:55:52 +00001138 loop_forever();
Erik Andersene49d5ec2000-02-08 19:58:47 +00001139 }
1140
1141 /* Now run the looping stuff for the rest of forever */
1142 while (1) {
Eric Andersen0298be82002-03-05 15:12:19 +00001143 /* run the respawn stuff */
1144 run_actions(RESPAWN);
1145
1146 /* run the askfirst stuff */
1147 run_actions(ASKFIRST);
1148
1149 /* Don't consume all CPU time -- sleep a bit */
1150 sleep(1);
1151
Erik Andersene49d5ec2000-02-08 19:58:47 +00001152 /* Wait for a child process to exit */
1153 wpid = wait(&status);
1154 if (wpid > 0) {
1155 /* Find out who died and clean up their corpse */
Eric Andersen0298be82002-03-05 15:12:19 +00001156 for (a = init_action_list; a; a = a->next) {
Erik Andersene49d5ec2000-02-08 19:58:47 +00001157 if (a->pid == wpid) {
Eric Andersen0298be82002-03-05 15:12:19 +00001158 /* Set the pid to 0 so that the process gets
1159 * restarted by run_actions() */
Erik Andersene49d5ec2000-02-08 19:58:47 +00001160 a->pid = 0;
Eric Andersen0298be82002-03-05 15:12:19 +00001161 message(LOG, "Process '%s' (pid %d) exited. "
1162 "Scheduling it for restart.\n", a->command, wpid);
Erik Andersene49d5ec2000-02-08 19:58:47 +00001163 }
1164 }
1165 }
Erik Andersene49d5ec2000-02-08 19:58:47 +00001166 }
Eric Andersencc8ed391999-10-05 16:24:54 +00001167}
Erik Andersen029011b2000-03-04 21:19:32 +00001168
1169/*
1170Local Variables:
1171c-file-style: "linux"
1172c-basic-offset: 4
1173tab-width: 4
1174End:
1175*/