blob: e8a5f74d31782e3f6660a8613801198db02c6957 [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 *
5 *
6 * Copyright (C) 1995, 1996 by Bruce Perens <bruce@pixar.com>.
7 * 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
Eric Andersencc8ed391999-10-05 16:24:54 +000030#include "internal.h"
Erik Andersen42094cd2000-03-20 21:34:52 +000031#include <asm/types.h>
Eric Andersencc8ed391999-10-05 16:24:54 +000032#include <errno.h>
Erik Andersen42094cd2000-03-20 21:34:52 +000033#include <linux/serial.h> /* for serial_struct */
34#include <linux/version.h>
Eric Andersenb186d981999-12-03 09:19:54 +000035#include <paths.h>
Erik Andersen42094cd2000-03-20 21:34:52 +000036#include <signal.h>
37#include <stdarg.h>
38#include <stdio.h>
39#include <stdlib.h>
Eric Andersencc8ed391999-10-05 16:24:54 +000040#include <string.h>
Erik Andersen42094cd2000-03-20 21:34:52 +000041#include <sys/fcntl.h>
42#include <sys/ioctl.h>
43#include <sys/kdaemon.h>
Eric Andersencc8ed391999-10-05 16:24:54 +000044#include <sys/mount.h>
45#include <sys/reboot.h>
Erik Andersend07ee462000-02-21 21:26:32 +000046#include <sys/sysinfo.h> /* For check_free_memory() */
Eric Andersen4c781471999-11-26 08:12:56 +000047#ifdef BB_SYSLOGD
Erik Andersen983b51b2000-04-04 18:14:25 +000048# include <sys/syslog.h>
Eric Andersen4c781471999-11-26 08:12:56 +000049#endif
Erik Andersen42094cd2000-03-20 21:34:52 +000050#include <sys/sysmacros.h>
51#include <sys/types.h>
52#include <sys/vt.h> /* for vt_stat */
53#include <sys/wait.h>
54#include <termios.h>
55#include <unistd.h>
Eric Andersencc8ed391999-10-05 16:24:54 +000056
Erik Andersen983b51b2000-04-04 18:14:25 +000057
Erik Andersen183da4a2000-04-04 18:36:37 +000058#if defined BB_FEATURE_INIT_COREDUMPS
Erik Andersen983b51b2000-04-04 18:14:25 +000059/*
Erik Andersen183da4a2000-04-04 18:36:37 +000060 * When a file named CORE_ENABLE_FLAG_FILE exists, setrlimit is called
61 * before processes are spawned to set core file size as unlimited.
62 * This is for debugging only. Don't use this is production, unless
63 * you want core dumps lying about....
Erik Andersen983b51b2000-04-04 18:14:25 +000064 */
65#define CORE_ENABLE_FLAG_FILE "/.init_enable_core"
66#include <sys/resource.h>
67#include <sys/time.h>
Erik Andersen183da4a2000-04-04 18:36:37 +000068#endif
Erik Andersen983b51b2000-04-04 18:14:25 +000069
Erik Andersen4d1d0111999-12-17 18:44:15 +000070#ifndef KERNEL_VERSION
71#define KERNEL_VERSION(a,b,c) (((a) << 16) + ((b) << 8) + (c))
72#endif
73
Eric Andersen0ecb54a1999-12-05 23:24:55 +000074
Erik Andersen42094cd2000-03-20 21:34:52 +000075#define VT_PRIMARY "/dev/tty1" /* Primary virtual console */
76#define VT_SECONDARY "/dev/tty2" /* Virtual console */
77#define VT_LOG "/dev/tty3" /* Virtual console */
78#define SERIAL_CON0 "/dev/ttyS0" /* Primary serial console */
79#define SERIAL_CON1 "/dev/ttyS1" /* Serial console */
80#define SHELL "/bin/sh" /* Default shell */
81#define INITTAB "/etc/inittab" /* inittab file location */
Erik Andersen96e2abd2000-01-07 11:40:44 +000082#ifndef INIT_SCRIPT
Erik Andersen42094cd2000-03-20 21:34:52 +000083#define INIT_SCRIPT "/etc/init.d/rcS" /* Default sysinit script. */
Erik Andersen96e2abd2000-01-07 11:40:44 +000084#endif
Erik Andersen9c88cac1999-12-30 09:25:17 +000085
Erik Andersen42094cd2000-03-20 21:34:52 +000086#define LOG 0x1
87#define CONSOLE 0x2
Erik Andersen7dc16072000-01-04 01:10:25 +000088
89/* Allowed init action types */
90typedef enum {
Erik Andersene49d5ec2000-02-08 19:58:47 +000091 SYSINIT = 1,
92 RESPAWN,
93 ASKFIRST,
94 WAIT,
Erik Andersene132f4b2000-02-09 04:16:43 +000095 ONCE,
96 CTRLALTDEL
Erik Andersen7dc16072000-01-04 01:10:25 +000097} initActionEnum;
98
Erik Andersen42094cd2000-03-20 21:34:52 +000099/* A mapping between "inittab" action name strings and action type codes. */
Erik Andersene49d5ec2000-02-08 19:58:47 +0000100typedef struct initActionType {
101 const char *name;
102 initActionEnum action;
Erik Andersen7dc16072000-01-04 01:10:25 +0000103} initActionType;
104
105static const struct initActionType actions[] = {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000106 {"sysinit", SYSINIT},
107 {"respawn", RESPAWN},
108 {"askfirst", ASKFIRST},
109 {"wait", WAIT},
110 {"once", ONCE},
Erik Andersene132f4b2000-02-09 04:16:43 +0000111 {"ctrlaltdel", CTRLALTDEL},
Erik Andersene49d5ec2000-02-08 19:58:47 +0000112 {0}
Erik Andersen7dc16072000-01-04 01:10:25 +0000113};
114
Erik Andersen42094cd2000-03-20 21:34:52 +0000115/* Set up a linked list of initActions, to be read from inittab */
Erik Andersen7dc16072000-01-04 01:10:25 +0000116typedef struct initActionTag initAction;
117struct initActionTag {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000118 pid_t pid;
119 char process[256];
120 char console[256];
121 initAction *nextPtr;
122 initActionEnum action;
Erik Andersen7dc16072000-01-04 01:10:25 +0000123};
Erik Andersene49d5ec2000-02-08 19:58:47 +0000124initAction *initActionList = NULL;
Erik Andersen7dc16072000-01-04 01:10:25 +0000125
126
Erik Andersenac6e71f2000-01-08 22:04:33 +0000127static char *secondConsole = VT_SECONDARY;
Erik Andersen42094cd2000-03-20 21:34:52 +0000128static char *log = VT_LOG;
129static int kernelVersion = 0;
130static char termType[32] = "TERM=linux";
131static char console[32] = _PATH_CONSOLE;
132
Erik Andersene132f4b2000-02-09 04:16:43 +0000133static void delete_initAction(initAction * action);
Eric Andersen0460ff21999-10-25 23:32:44 +0000134
135
Erik Andersen42094cd2000-03-20 21:34:52 +0000136/* Print a message to the specified device.
137 * Device may be bitwise-or'd from LOG | CONSOLE */
138static void message(int device, char *fmt, ...)
Erik Andersen93d65132000-04-06 08:06:36 +0000139 __attribute__ ((format (printf, 2, 3)));
140static void message(int device, char *fmt, ...)
Eric Andersen0460ff21999-10-25 23:32:44 +0000141{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000142 va_list arguments;
143 int fd;
Erik Andersen7dc16072000-01-04 01:10:25 +0000144
Eric Andersen4c781471999-11-26 08:12:56 +0000145#ifdef BB_SYSLOGD
146
Erik Andersene49d5ec2000-02-08 19:58:47 +0000147 /* Log the message to syslogd */
148 if (device & LOG) {
149 char msg[1024];
Eric Andersen4c781471999-11-26 08:12:56 +0000150
Erik Andersene49d5ec2000-02-08 19:58:47 +0000151 va_start(arguments, fmt);
152 vsnprintf(msg, sizeof(msg), fmt, arguments);
153 va_end(arguments);
Erik Andersene132f4b2000-02-09 04:16:43 +0000154 openlog("init", 0, LOG_USER);
155 syslog(LOG_USER|LOG_INFO, msg);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000156 closelog();
Eric Andersen3ae0c781999-11-04 01:13:21 +0000157 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000158#else
159 static int log_fd = -1;
160
161 /* Take full control of the log tty, and never close it.
162 * It's mine, all mine! Muhahahaha! */
163 if (log_fd < 0) {
164 if (log == NULL) {
165 /* don't even try to log, because there is no such console */
166 log_fd = -2;
167 /* log to main console instead */
168 device = CONSOLE;
Erik Andersene132f4b2000-02-09 04:16:43 +0000169 } else if ((log_fd = device_open(log, O_RDWR|O_NDELAY)) < 0) {
170 log_fd = -2;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000171 fprintf(stderr, "Bummer, can't write to log on %s!\r\n", log);
172 fflush(stderr);
Erik Andersene132f4b2000-02-09 04:16:43 +0000173 log = NULL;
174 device = CONSOLE;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000175 }
176 }
177 if ((device & LOG) && (log_fd >= 0)) {
178 va_start(arguments, fmt);
179 vdprintf(log_fd, fmt, arguments);
180 va_end(arguments);
181 }
Eric Andersen4c781471999-11-26 08:12:56 +0000182#endif
183
Erik Andersene49d5ec2000-02-08 19:58:47 +0000184 if (device & CONSOLE) {
185 /* Always send console messages to /dev/console so people will see them. */
186 if (
187 (fd =
188 device_open(_PATH_CONSOLE,
189 O_WRONLY | O_NOCTTY | O_NDELAY)) >= 0) {
190 va_start(arguments, fmt);
191 vdprintf(fd, fmt, arguments);
192 va_end(arguments);
193 close(fd);
194 } else {
195 fprintf(stderr, "Bummer, can't print: ");
196 va_start(arguments, fmt);
197 vfprintf(stderr, fmt, arguments);
198 fflush(stderr);
199 va_end(arguments);
200 }
Eric Andersen3ae0c781999-11-04 01:13:21 +0000201 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000202}
203
Eric Andersen3ae0c781999-11-04 01:13:21 +0000204
Eric Andersen0460ff21999-10-25 23:32:44 +0000205/* Set terminal settings to reasonable defaults */
Erik Andersene49d5ec2000-02-08 19:58:47 +0000206void set_term(int fd)
Eric Andersencc8ed391999-10-05 16:24:54 +0000207{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000208 struct termios tty;
Eric Andersencc8ed391999-10-05 16:24:54 +0000209
Erik Andersene49d5ec2000-02-08 19:58:47 +0000210 tcgetattr(fd, &tty);
Eric Andersencc8ed391999-10-05 16:24:54 +0000211
Erik Andersene49d5ec2000-02-08 19:58:47 +0000212 /* set control chars */
Erik Andersen6c41c442000-03-19 05:13:49 +0000213 tty.c_cc[VINTR] = 3; /* C-c */
214 tty.c_cc[VQUIT] = 28; /* C-\ */
215 tty.c_cc[VERASE] = 127; /* C-? */
216 tty.c_cc[VKILL] = 21; /* C-u */
217 tty.c_cc[VEOF] = 4; /* C-d */
218 tty.c_cc[VSTART] = 17; /* C-q */
219 tty.c_cc[VSTOP] = 19; /* C-s */
220 tty.c_cc[VSUSP] = 26; /* C-z */
Eric Andersencc8ed391999-10-05 16:24:54 +0000221
Erik Andersene49d5ec2000-02-08 19:58:47 +0000222 /* use line dicipline 0 */
223 tty.c_line = 0;
Eric Andersen7f1acfd1999-10-29 23:09:13 +0000224
Erik Andersene49d5ec2000-02-08 19:58:47 +0000225 /* Make it be sane */
Erik Andersen6c41c442000-03-19 05:13:49 +0000226 tty.c_cflag &= CBAUD|CBAUDEX|CSIZE|CSTOPB|PARENB|PARODD;
227 tty.c_cflag |= HUPCL|CLOCAL;
Eric Andersen08b10341999-11-19 02:38:58 +0000228
Erik Andersene49d5ec2000-02-08 19:58:47 +0000229 /* input modes */
230 tty.c_iflag = ICRNL | IXON | IXOFF;
Eric Andersen08b10341999-11-19 02:38:58 +0000231
Erik Andersene49d5ec2000-02-08 19:58:47 +0000232 /* output modes */
233 tty.c_oflag = OPOST | ONLCR;
Eric Andersen7f1acfd1999-10-29 23:09:13 +0000234
Erik Andersene49d5ec2000-02-08 19:58:47 +0000235 /* local modes */
236 tty.c_lflag =
237 ISIG | ICANON | ECHO | ECHOE | ECHOK | ECHOCTL | ECHOKE | IEXTEN;
Eric Andersencc8ed391999-10-05 16:24:54 +0000238
Erik Andersene49d5ec2000-02-08 19:58:47 +0000239 tcsetattr(fd, TCSANOW, &tty);
Eric Andersencc8ed391999-10-05 16:24:54 +0000240}
241
Eric Andersen219d6f51999-11-02 19:43:01 +0000242/* How much memory does this machine have? */
Erik Andersend07ee462000-02-21 21:26:32 +0000243static int check_free_memory()
Eric Andersencc8ed391999-10-05 16:24:54 +0000244{
Erik Andersend07ee462000-02-21 21:26:32 +0000245 struct sysinfo info;
Eric Andersencc8ed391999-10-05 16:24:54 +0000246
Erik Andersend07ee462000-02-21 21:26:32 +0000247 sysinfo(&info);
248 if (sysinfo(&info) != 0) {
249 message(LOG, "Error checking free memory: %s\n", strerror(errno));
Erik Andersene49d5ec2000-02-08 19:58:47 +0000250 return -1;
Eric Andersenabc7d591999-10-19 00:27:50 +0000251 }
Erik Andersend07ee462000-02-21 21:26:32 +0000252
Erik Andersen42094cd2000-03-20 21:34:52 +0000253 return((info.totalram+info.totalswap)/1024);
Eric Andersencc8ed391999-10-05 16:24:54 +0000254}
255
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000256static void console_init()
Eric Andersen0460ff21999-10-25 23:32:44 +0000257{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000258 int fd;
259 int tried_devcons = 0;
260 int tried_vtprimary = 0;
Erik Andersen029011b2000-03-04 21:19:32 +0000261 struct vt_stat vt;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000262 struct serial_struct sr;
263 char *s;
Eric Andersen0460ff21999-10-25 23:32:44 +0000264
Erik Andersene49d5ec2000-02-08 19:58:47 +0000265 if ((s = getenv("TERM")) != NULL) {
266 snprintf(termType, sizeof(termType) - 1, "TERM=%s", s);
267 }
Erik Andersenac6e71f2000-01-08 22:04:33 +0000268
Erik Andersene49d5ec2000-02-08 19:58:47 +0000269 if ((s = getenv("CONSOLE")) != NULL) {
270 snprintf(console, sizeof(console) - 1, "%s", s);
271 }
Eric Andersenfbb39c81999-11-08 17:00:52 +0000272#if #cpu(sparc)
Erik Andersene49d5ec2000-02-08 19:58:47 +0000273 /* sparc kernel supports console=tty[ab] parameter which is also
274 * passed to init, so catch it here */
275 else if ((s = getenv("console")) != NULL) {
276 /* remap tty[ab] to /dev/ttyS[01] */
277 if (strcmp(s, "ttya") == 0)
278 snprintf(console, sizeof(console) - 1, "%s", SERIAL_CON0);
279 else if (strcmp(s, "ttyb") == 0)
280 snprintf(console, sizeof(console) - 1, "%s", SERIAL_CON1);
281 }
Eric Andersen219d6f51999-11-02 19:43:01 +0000282#endif
Erik Andersene49d5ec2000-02-08 19:58:47 +0000283 else {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000284 /* 2.2 kernels: identify the real console backend and try to use it */
285 if (ioctl(0, TIOCGSERIAL, &sr) == 0) {
286 /* this is a serial console */
287 snprintf(console, sizeof(console) - 1, "/dev/ttyS%d", sr.line);
288 } else if (ioctl(0, VT_GETSTATE, &vt) == 0) {
289 /* this is linux virtual tty */
290 snprintf(console, sizeof(console) - 1, "/dev/tty%d",
291 vt.v_active);
292 } else {
293 snprintf(console, sizeof(console) - 1, "%s", _PATH_CONSOLE);
294 tried_devcons++;
295 }
Eric Andersen07e52971999-11-07 07:38:08 +0000296 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000297
298 while ((fd = open(console, O_RDONLY | O_NONBLOCK)) < 0) {
299 /* Can't open selected console -- try /dev/console */
300 if (!tried_devcons) {
301 tried_devcons++;
302 snprintf(console, sizeof(console) - 1, "%s", _PATH_CONSOLE);
303 continue;
304 }
305 /* Can't open selected console -- try vt1 */
306 if (!tried_vtprimary) {
307 tried_vtprimary++;
308 snprintf(console, sizeof(console) - 1, "%s", VT_PRIMARY);
309 continue;
310 }
311 break;
312 }
313 if (fd < 0) {
314 /* Perhaps we should panic here? */
315 snprintf(console, sizeof(console) - 1, "/dev/null");
Eric Andersen07e52971999-11-07 07:38:08 +0000316 } else {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000317 /* check for serial console and disable logging to tty3 & running a
318 * shell to tty2 */
319 if (ioctl(0, TIOCGSERIAL, &sr) == 0) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000320 log = NULL;
321 secondConsole = NULL;
Erik Andersenfa4718e2000-02-21 19:25:12 +0000322 /* Force the TERM setting to vt102 for serial console --
323 * iff TERM is set to linux (the default) */
324 if (strcmp( termType, "TERM=linux" ) == 0)
325 snprintf(termType, sizeof(termType) - 1, "TERM=vt102");
Erik Andersene132f4b2000-02-09 04:16:43 +0000326 message(LOG | CONSOLE,
327 "serial console detected. Disabling virtual terminals.\r\n");
Erik Andersene49d5ec2000-02-08 19:58:47 +0000328 }
329 close(fd);
Eric Andersen07e52971999-11-07 07:38:08 +0000330 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000331 message(LOG, "console=%s\n", console);
Eric Andersen0460ff21999-10-25 23:32:44 +0000332}
333
Erik Andersene49d5ec2000-02-08 19:58:47 +0000334static pid_t run(char *command, char *terminal, int get_enter)
Eric Andersen0460ff21999-10-25 23:32:44 +0000335{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000336 int i, fd;
337 pid_t pid;
338 char *tmpCmd;
339 char *cmd[255];
Erik Andersene132f4b2000-02-09 04:16:43 +0000340 char buf[255];
Erik Andersene49d5ec2000-02-08 19:58:47 +0000341 static const char press_enter[] =
342
343 "\nPlease press Enter to activate this console. ";
344 char *environment[] = {
345 "HOME=/",
346 "PATH=/usr/bin:/bin:/usr/sbin:/sbin",
347 "SHELL=/bin/sh",
348 termType,
349 "USER=root",
350 0
351 };
Erik Andersenac6e71f2000-01-08 22:04:33 +0000352
Eric Andersen0460ff21999-10-25 23:32:44 +0000353
Erik Andersene49d5ec2000-02-08 19:58:47 +0000354 if ((pid = fork()) == 0) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000355 /* Clean up */
356 close(0);
357 close(1);
358 close(2);
359 setsid();
Eric Andersen0460ff21999-10-25 23:32:44 +0000360
Erik Andersene49d5ec2000-02-08 19:58:47 +0000361 /* Reset signal handlers set for parent process */
362 signal(SIGUSR1, SIG_DFL);
363 signal(SIGUSR2, SIG_DFL);
364 signal(SIGINT, SIG_DFL);
365 signal(SIGTERM, SIG_DFL);
366 signal(SIGHUP, SIG_DFL);
Eric Andersen2f6c04f1999-11-01 23:59:44 +0000367
Erik Andersene49d5ec2000-02-08 19:58:47 +0000368 if ((fd = device_open(terminal, O_RDWR)) < 0) {
369 message(LOG | CONSOLE, "Bummer, can't open %s\r\n", terminal);
370 exit(1);
371 }
372 dup2(fd, 0);
373 dup2(fd, 1);
374 dup2(fd, 2);
375 tcsetpgrp(0, getpgrp());
376 set_term(0);
377
378 if (get_enter == TRUE) {
379 /*
380 * Save memory by not exec-ing anything large (like a shell)
381 * before the user wants it. This is critical if swap is not
382 * enabled and the system has low memory. Generally this will
383 * be run on the second virtual console, and the first will
384 * be allowed to start a shell or whatever an init script
385 * specifies.
386 */
387 char c;
Erik Andersene132f4b2000-02-09 04:16:43 +0000388#ifdef DEBUG_INIT
Erik Andersen2ac2fae2000-03-07 23:32:17 +0000389 pid_t shell_pgid = getpid();
Erik Andersene49d5ec2000-02-08 19:58:47 +0000390 message(LOG, "Waiting for enter to start '%s' (pid %d, console %s)\r\n",
391 command, shell_pgid, terminal);
Erik Andersene132f4b2000-02-09 04:16:43 +0000392#endif
Erik Andersene49d5ec2000-02-08 19:58:47 +0000393 write(fileno(stdout), press_enter, sizeof(press_enter) - 1);
394 read(fileno(stdin), &c, 1);
395 }
396
Erik Andersene49d5ec2000-02-08 19:58:47 +0000397#ifdef DEBUG_INIT
Erik Andersene132f4b2000-02-09 04:16:43 +0000398 /* Log the process name and args */
399 message(LOG, "Starting pid %d, console %s: '%s'\r\n",
400 shell_pgid, terminal, command);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000401#endif
Erik Andersene132f4b2000-02-09 04:16:43 +0000402
403 /* See if any special /bin/sh requiring characters are present */
404 if (strpbrk(command, "~`!$^&*()=|\\{}[];\"'<>?") != NULL) {
405 cmd[0] = SHELL;
406 cmd[1] = "-c";
407 strcpy(buf, "exec ");
408 strncat(buf, command, sizeof(buf) - strlen(buf) - 1);
409 cmd[2] = buf;
410 cmd[3] = NULL;
411 } else {
412 /* Convert command (char*) into cmd (char**, one word per string) */
413 for (tmpCmd = command, i = 0;
414 (tmpCmd = strsep(&command, " \t")) != NULL;) {
415 if (*tmpCmd != '\0') {
416 cmd[i] = tmpCmd;
417 tmpCmd++;
418 i++;
419 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000420 }
Erik Andersene132f4b2000-02-09 04:16:43 +0000421 cmd[i] = NULL;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000422 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000423
Erik Andersen183da4a2000-04-04 18:36:37 +0000424#if defined BB_FEATURE_INIT_COREDUMPS
Erik Andersen983b51b2000-04-04 18:14:25 +0000425 {
426 struct stat sb;
427 if (stat (CORE_ENABLE_FLAG_FILE, &sb) == 0) {
428 struct rlimit limit;
429 limit.rlim_cur = RLIM_INFINITY;
430 limit.rlim_max = RLIM_INFINITY;
431 setrlimit(RLIMIT_CORE, &limit);
432 }
433 }
Erik Andersen183da4a2000-04-04 18:36:37 +0000434#endif
Erik Andersen983b51b2000-04-04 18:14:25 +0000435
Erik Andersene49d5ec2000-02-08 19:58:47 +0000436 /* Now run it. The new program will take over this PID,
437 * so nothing further in init.c should be run. */
438 execve(cmd[0], cmd, environment);
439
440 /* We're still here? Some error happened. */
441 message(LOG | CONSOLE, "Bummer, could not run '%s': %s\n", cmd[0],
442 strerror(errno));
443 exit(-1);
Erik Andersen05df2392000-01-13 04:43:48 +0000444 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000445 return pid;
Eric Andersen0460ff21999-10-25 23:32:44 +0000446}
447
Erik Andersene49d5ec2000-02-08 19:58:47 +0000448static int waitfor(char *command, char *terminal, int get_enter)
Erik Andersen0e3782f2000-01-07 02:54:55 +0000449{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000450 int status, wpid;
451 int pid = run(command, terminal, get_enter);
Erik Andersen0e3782f2000-01-07 02:54:55 +0000452
Erik Andersene49d5ec2000-02-08 19:58:47 +0000453 while (1) {
454 wpid = wait(&status);
Erik Andersene132f4b2000-02-09 04:16:43 +0000455 if (wpid > 0 && wpid != pid) {
456 continue;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000457 }
458 if (wpid == pid)
459 break;
Erik Andersen0e3782f2000-01-07 02:54:55 +0000460 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000461 return wpid;
Erik Andersen0e3782f2000-01-07 02:54:55 +0000462}
463
Eric Andersen219d6f51999-11-02 19:43:01 +0000464/* Make sure there is enough memory to do something useful. *
Erik Andersene132f4b2000-02-09 04:16:43 +0000465 * Calls "swapon -a" if needed so be sure /etc/fstab is present... */
Eric Andersen219d6f51999-11-02 19:43:01 +0000466static void check_memory()
467{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000468 struct stat statBuf;
Eric Andersen219d6f51999-11-02 19:43:01 +0000469
Erik Andersend07ee462000-02-21 21:26:32 +0000470 if (check_free_memory() > 1000)
Erik Andersene49d5ec2000-02-08 19:58:47 +0000471 return;
472
473 if (stat("/etc/fstab", &statBuf) == 0) {
Erik Andersen61677fe2000-04-13 01:18:56 +0000474 /* swapon -a requires /proc typically */
475 waitfor("mount proc /proc -t proc", console, FALSE);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000476 /* Try to turn on swap */
Erik Andersen61677fe2000-04-13 01:18:56 +0000477 waitfor("swapon -a", console, FALSE);
Erik Andersend07ee462000-02-21 21:26:32 +0000478 if (check_free_memory() < 1000)
Erik Andersene49d5ec2000-02-08 19:58:47 +0000479 goto goodnight;
480 } else
481 goto goodnight;
Eric Andersen219d6f51999-11-02 19:43:01 +0000482 return;
483
Erik Andersene49d5ec2000-02-08 19:58:47 +0000484 goodnight:
485 message(CONSOLE,
486 "Sorry, your computer does not have enough memory.\r\n");
487 while (1)
488 sleep(1);
Eric Andersen219d6f51999-11-02 19:43:01 +0000489}
490
Erik Andersene132f4b2000-02-09 04:16:43 +0000491/* Run all commands to be run right before halt/reboot */
492static void run_lastAction(void)
493{
494 initAction *a;
495 for (a = initActionList; a; a = a->nextPtr) {
496 if (a->action == CTRLALTDEL) {
497 waitfor(a->process, a->console, FALSE);
498 delete_initAction(a);
499 }
500 }
501}
502
503
Erik Andersen7dc16072000-01-04 01:10:25 +0000504#ifndef DEBUG_INIT
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000505static void shutdown_system(void)
Eric Andersencc8ed391999-10-05 16:24:54 +0000506{
Erik Andersene132f4b2000-02-09 04:16:43 +0000507
Erik Andersene49d5ec2000-02-08 19:58:47 +0000508 /* first disable our SIGHUP signal */
509 signal(SIGHUP, SIG_DFL);
Erik Andersen7dc16072000-01-04 01:10:25 +0000510
Erik Andersene49d5ec2000-02-08 19:58:47 +0000511 /* Allow Ctrl-Alt-Del to reboot system. */
512 reboot(RB_ENABLE_CAD);
Erik Andersene132f4b2000-02-09 04:16:43 +0000513
514 message(CONSOLE|LOG, "\r\nThe system is going down NOW !!\r\n");
Eric Andersencf8c9cf1999-11-05 00:31:46 +0000515 sync();
Erik Andersene49d5ec2000-02-08 19:58:47 +0000516
517 /* Send signals to every process _except_ pid 1 */
Erik Andersene132f4b2000-02-09 04:16:43 +0000518 message(CONSOLE|LOG, "Sending SIGTERM to all processes.\r\n");
Erik Andersene49d5ec2000-02-08 19:58:47 +0000519 kill(-1, SIGTERM);
Erik Andersene132f4b2000-02-09 04:16:43 +0000520 sleep(1);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000521 sync();
522
Erik Andersene132f4b2000-02-09 04:16:43 +0000523 message(CONSOLE|LOG, "Sending SIGKILL to all processes.\r\n");
Erik Andersene49d5ec2000-02-08 19:58:47 +0000524 kill(-1, SIGKILL);
Erik Andersene132f4b2000-02-09 04:16:43 +0000525 sleep(1);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000526
Erik Andersene132f4b2000-02-09 04:16:43 +0000527 /* run everything to be run at "ctrlaltdel" */
528 run_lastAction();
529
Erik Andersene49d5ec2000-02-08 19:58:47 +0000530 sync();
531 if (kernelVersion > 0 && kernelVersion <= 2 * 65536 + 2 * 256 + 11) {
532 /* bdflush, kupdate not needed for kernels >2.2.11 */
533 bdflush(1, 0);
534 sync();
535 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000536}
537
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000538static void halt_signal(int sig)
Eric Andersencc8ed391999-10-05 16:24:54 +0000539{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000540 shutdown_system();
Erik Andersene132f4b2000-02-09 04:16:43 +0000541 message(CONSOLE|LOG,
Erik Andersene49d5ec2000-02-08 19:58:47 +0000542 "The system is halted. Press %s or turn off power\r\n",
543 (secondConsole == NULL) /* serial console */
544 ? "Reset" : "CTRL-ALT-DEL");
545 sync();
Erik Andersen3fe39dc2000-01-25 18:13:53 +0000546
Erik Andersene49d5ec2000-02-08 19:58:47 +0000547 /* allow time for last message to reach serial console */
Erik Andersene132f4b2000-02-09 04:16:43 +0000548 sleep(2);
Erik Andersen3fe39dc2000-01-25 18:13:53 +0000549
Erik Andersen4d1d0111999-12-17 18:44:15 +0000550#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,2,0)
Erik Andersene49d5ec2000-02-08 19:58:47 +0000551 if (sig == SIGUSR2)
552 reboot(RB_POWER_OFF);
553 else
Erik Andersen4d1d0111999-12-17 18:44:15 +0000554#endif
Erik Andersene49d5ec2000-02-08 19:58:47 +0000555 reboot(RB_HALT_SYSTEM);
556 exit(0);
Eric Andersencc8ed391999-10-05 16:24:54 +0000557}
558
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000559static void reboot_signal(int sig)
Eric Andersencc8ed391999-10-05 16:24:54 +0000560{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000561 shutdown_system();
Erik Andersene132f4b2000-02-09 04:16:43 +0000562 message(CONSOLE|LOG, "Please stand by while rebooting the system.\r\n");
Erik Andersene49d5ec2000-02-08 19:58:47 +0000563 sync();
Erik Andersen3fe39dc2000-01-25 18:13:53 +0000564
Erik Andersene49d5ec2000-02-08 19:58:47 +0000565 /* allow time for last message to reach serial console */
566 sleep(2);
Erik Andersen3fe39dc2000-01-25 18:13:53 +0000567
Erik Andersene49d5ec2000-02-08 19:58:47 +0000568 reboot(RB_AUTOBOOT);
569 exit(0);
Eric Andersencc8ed391999-10-05 16:24:54 +0000570}
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000571
Erik Andersende552872000-01-23 01:34:05 +0000572#if defined BB_FEATURE_INIT_CHROOT
Erik Andersend07ee462000-02-21 21:26:32 +0000573
574#if ! defined BB_FEATURE_USE_PROCFS
575#error Sorry, I depend on the /proc filesystem right now.
576#endif
577
Erik Andersende552872000-01-23 01:34:05 +0000578static void check_chroot(int sig)
579{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000580 char *argv_init[2] = { "init", NULL, };
581 char *envp_init[3] = { "HOME=/", "TERM=linux", NULL, };
582 char rootpath[256], *tc;
583 int fd;
Erik Andersende552872000-01-23 01:34:05 +0000584
Erik Andersene49d5ec2000-02-08 19:58:47 +0000585 if ((fd = open("/proc/sys/kernel/init-chroot", O_RDONLY)) == -1) {
586 message(CONSOLE,
587 "SIGHUP recived, but could not open proc file\r\n");
588 sleep(2);
589 return;
590 }
591 if (read(fd, rootpath, sizeof(rootpath)) == -1) {
592 message(CONSOLE,
593 "SIGHUP recived, but could not read proc file\r\n");
594 sleep(2);
595 return;
596 }
597 close(fd);
598
599 if (rootpath[0] == '\0') {
600 message(CONSOLE,
601 "SIGHUP recived, but new root is not valid: %s\r\n",
602 rootpath);
603 sleep(2);
604 return;
605 }
606
607 tc = strrchr(rootpath, '\n');
608 *tc = '\0';
609
610 /* Ok, making it this far means we commit */
611 message(CONSOLE, "Please stand by, changing root to `%s'.\r\n",
612 rootpath);
613
614 /* kill all other programs first */
615 message(CONSOLE, "Sending SIGTERM to all processes.\r\n");
616 kill(-1, SIGTERM);
Erik Andersende552872000-01-23 01:34:05 +0000617 sleep(2);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000618 sync();
619
620 message(CONSOLE, "Sending SIGKILL to all processes.\r\n");
621 kill(-1, SIGKILL);
Erik Andersende552872000-01-23 01:34:05 +0000622 sleep(2);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000623 sync();
624
625 /* ok, we don't need /proc anymore. we also assume that the signaling
626 * process left the rest of the filesystems alone for us */
627 umount("/proc");
628
629 /* Ok, now we chroot. Hopefully we only have two things mounted, the
630 * new chroot'd mount point, and the old "/" mount. s,
631 * we go ahead and unmount the old "/". This should trigger the kernel
632 * to set things up the Right Way(tm). */
633
634 if (!chroot(rootpath))
635 umount("/dev/root");
636
637 /* If the chroot fails, we are already too far to turn back, so we
638 * continue and hope that executing init below will revive the system */
639
640 /* close all of our descriptors and open new ones */
641 close(0);
642 close(1);
643 close(2);
644 open("/dev/console", O_RDWR, 0);
645 dup(0);
646 dup(0);
647
648 message(CONSOLE, "Executing real init...\r\n");
649 /* execute init in the (hopefully) new root */
650 execve("/sbin/init", argv_init, envp_init);
651
652 message(CONSOLE,
653 "ERROR: Could not exec new init. Press %s to reboot.\r\n",
654 (secondConsole == NULL) /* serial console */
655 ? "Reset" : "CTRL-ALT-DEL");
Erik Andersende552872000-01-23 01:34:05 +0000656 return;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000657}
658#endif /* BB_FEATURE_INIT_CHROOT */
Erik Andersende552872000-01-23 01:34:05 +0000659
Erik Andersene49d5ec2000-02-08 19:58:47 +0000660#endif /* ! DEBUG_INIT */
Erik Andersende552872000-01-23 01:34:05 +0000661
Erik Andersene49d5ec2000-02-08 19:58:47 +0000662void new_initAction(initActionEnum action, char *process, char *cons)
Erik Andersen7dc16072000-01-04 01:10:25 +0000663{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000664 initAction *newAction;
Erik Andersen9e737252000-01-06 01:16:13 +0000665
Erik Andersene49d5ec2000-02-08 19:58:47 +0000666 if (*cons == '\0')
667 cons = console;
Erik Andersen9e737252000-01-06 01:16:13 +0000668
Erik Andersene49d5ec2000-02-08 19:58:47 +0000669 /* If BusyBox detects that a serial console is in use,
670 * then entries not refering to the console or null devices will _not_ be run.
671 * The exception to this rule is the null device.
672 */
673 if (secondConsole == NULL && strcmp(cons, console)
674 && strcmp(cons, "/dev/null"))
675 return;
676
677 newAction = calloc((size_t) (1), sizeof(initAction));
678 if (!newAction) {
679 message(LOG | CONSOLE, "Memory allocation failure\n");
680 while (1)
681 sleep(1);
682 }
683 newAction->nextPtr = initActionList;
684 initActionList = newAction;
685 strncpy(newAction->process, process, 255);
686 newAction->action = action;
687 strncpy(newAction->console, cons, 255);
688 newAction->pid = 0;
Erik Andersen31638212000-01-15 22:28:50 +0000689// message(LOG|CONSOLE, "process='%s' action='%d' console='%s'\n",
Erik Andersene49d5ec2000-02-08 19:58:47 +0000690// newAction->process, newAction->action, newAction->console);
Erik Andersen7dc16072000-01-04 01:10:25 +0000691}
692
Erik Andersene132f4b2000-02-09 04:16:43 +0000693static void delete_initAction(initAction * action)
Erik Andersen7dc16072000-01-04 01:10:25 +0000694{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000695 initAction *a, *b = NULL;
696
697 for (a = initActionList; a; b = a, a = a->nextPtr) {
698 if (a == action) {
699 if (b == NULL) {
700 initActionList = a->nextPtr;
701 } else {
702 b->nextPtr = a->nextPtr;
703 }
704 free(a);
705 break;
706 }
Erik Andersen7dc16072000-01-04 01:10:25 +0000707 }
Erik Andersen7dc16072000-01-04 01:10:25 +0000708}
709
Erik Andersen9e737252000-01-06 01:16:13 +0000710/* NOTE that if BB_FEATURE_USE_INITTAB is NOT defined,
711 * then parse_inittab() simply adds in some default
712 * actions(i.e runs INIT_SCRIPT and then starts a pair
Erik Andersen812d4662000-01-07 18:30:40 +0000713 * of "askfirst" shells). If BB_FEATURE_USE_INITTAB
714 * _is_ defined, but /etc/inittab is missing, this
715 * results in the same set of default behaviors.
Erik Andersen9e737252000-01-06 01:16:13 +0000716 * */
Erik Andersene49d5ec2000-02-08 19:58:47 +0000717void parse_inittab(void)
Erik Andersen7dc16072000-01-04 01:10:25 +0000718{
Erik Andersen9e737252000-01-06 01:16:13 +0000719#ifdef BB_FEATURE_USE_INITTAB
Erik Andersene49d5ec2000-02-08 19:58:47 +0000720 FILE *file;
721 char buf[256], lineAsRead[256], tmpConsole[256];
722 char *p, *q, *r, *s;
723 const struct initActionType *a = actions;
724 int foundIt;
Erik Andersen7dc16072000-01-04 01:10:25 +0000725
726
Erik Andersene49d5ec2000-02-08 19:58:47 +0000727 file = fopen(INITTAB, "r");
728 if (file == NULL) {
729 /* No inittab file -- set up some default behavior */
Erik Andersen9e737252000-01-06 01:16:13 +0000730#endif
Erik Andersene132f4b2000-02-09 04:16:43 +0000731 /* Swapoff on halt/reboot */
Erik Andersenfb1793f2000-02-09 16:37:08 +0000732 new_initAction(CTRLALTDEL, "/sbin/swapoff -a > /dev/null 2>&1", console);
Erik Andersene132f4b2000-02-09 04:16:43 +0000733 /* Umount all filesystems on halt/reboot */
734 new_initAction(CTRLALTDEL, "/bin/umount -a -r > /dev/null 2>&1", console);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000735 /* Askfirst shell on tty1 */
736 new_initAction(ASKFIRST, SHELL, console);
737 /* Askfirst shell on tty2 */
738 if (secondConsole != NULL)
739 new_initAction(ASKFIRST, SHELL, secondConsole);
740 /* sysinit */
741 new_initAction(SYSINIT, INIT_SCRIPT, console);
Erik Andersen7dc16072000-01-04 01:10:25 +0000742
Erik Andersene49d5ec2000-02-08 19:58:47 +0000743 return;
Erik Andersen9e737252000-01-06 01:16:13 +0000744#ifdef BB_FEATURE_USE_INITTAB
Erik Andersen9e737252000-01-06 01:16:13 +0000745 }
Erik Andersen7dc16072000-01-04 01:10:25 +0000746
Erik Andersene49d5ec2000-02-08 19:58:47 +0000747 while (fgets(buf, 255, file) != NULL) {
748 foundIt = FALSE;
749 for (p = buf; *p == ' ' || *p == '\t'; p++);
750 if (*p == '#' || *p == '\n')
751 continue;
Erik Andersen7dc16072000-01-04 01:10:25 +0000752
Erik Andersene49d5ec2000-02-08 19:58:47 +0000753 /* Trim the trailing \n */
754 q = strrchr(p, '\n');
755 if (q != NULL)
756 *q = '\0';
Erik Andersen7dc16072000-01-04 01:10:25 +0000757
Erik Andersene49d5ec2000-02-08 19:58:47 +0000758 /* Keep a copy around for posterity's sake (and error msgs) */
759 strcpy(lineAsRead, buf);
760
761 /* Grab the ID field */
762 s = p;
763 p = strchr(p, ':');
764 if (p != NULL || *(p + 1) != '\0') {
765 *p = '\0';
766 ++p;
Erik Andersen0e3782f2000-01-07 02:54:55 +0000767 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000768
Erik Andersen42094cd2000-03-20 21:34:52 +0000769 /* Now peel off the process field from the end
Erik Andersene49d5ec2000-02-08 19:58:47 +0000770 * of the string */
771 q = strrchr(p, ':');
772 if (q == NULL || *(q + 1) == '\0') {
773 message(LOG | CONSOLE, "Bad inittab entry: %s\n", lineAsRead);
774 continue;
775 } else {
776 *q = '\0';
777 ++q;
778 }
779
Erik Andersen42094cd2000-03-20 21:34:52 +0000780 /* Now peel off the action field */
Erik Andersene49d5ec2000-02-08 19:58:47 +0000781 r = strrchr(p, ':');
782 if (r == NULL || *(r + 1) == '\0') {
783 message(LOG | CONSOLE, "Bad inittab entry: %s\n", lineAsRead);
784 continue;
785 } else {
786 ++r;
787 }
788
789 /* Ok, now process it */
790 a = actions;
791 while (a->name != 0) {
792 if (strcmp(a->name, r) == 0) {
793 if (*s != '\0') {
794 struct stat statBuf;
795
796 strcpy(tmpConsole, "/dev/");
797 strncat(tmpConsole, s, 200);
798 if (stat(tmpConsole, &statBuf) != 0) {
799 message(LOG | CONSOLE,
800 "device '%s' does not exist. Did you read the directions?\n",
801 tmpConsole);
802 break;
803 }
804 s = tmpConsole;
805 }
806 new_initAction(a->action, q, s);
807 foundIt = TRUE;
808 }
809 a++;
810 }
811 if (foundIt == TRUE)
812 continue;
813 else {
814 /* Choke on an unknown action */
815 message(LOG | CONSOLE, "Bad inittab entry: %s\n", lineAsRead);
816 }
Erik Andersen7dc16072000-01-04 01:10:25 +0000817 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000818 return;
Erik Andersen42094cd2000-03-20 21:34:52 +0000819#endif /* BB_FEATURE_USE_INITTAB */
Erik Andersen7dc16072000-01-04 01:10:25 +0000820}
821
Erik Andersene132f4b2000-02-09 04:16:43 +0000822
823
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000824extern int init_main(int argc, char **argv)
Eric Andersen0460ff21999-10-25 23:32:44 +0000825{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000826 initAction *a;
827 pid_t wpid;
828 int status;
Eric Andersen0460ff21999-10-25 23:32:44 +0000829
Eric Andersend73dc5b1999-11-10 23:13:02 +0000830#ifndef DEBUG_INIT
Erik Andersena51ecdd2000-02-24 18:09:58 +0000831 /* Expect to be invoked as init with PID=1 or be invoked as linuxrc */
832 if (getpid() != 1
833#ifdef BB_FEATURE_LINUXRC
834 && strstr(argv[0], "linuxrc") == NULL
835#endif
836 )
837 {
838 usage("init\n\nInit is the parent of all processes.\n\n"
839 "This version of init is designed to be run only "
840 "by the kernel.\n");
Erik Andersene49d5ec2000-02-08 19:58:47 +0000841 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000842 /* Set up sig handlers -- be sure to
843 * clear all of these in run() */
844 signal(SIGUSR1, halt_signal);
845 signal(SIGUSR2, reboot_signal);
846 signal(SIGINT, reboot_signal);
847 signal(SIGTERM, reboot_signal);
Erik Andersende552872000-01-23 01:34:05 +0000848#if defined BB_FEATURE_INIT_CHROOT
Erik Andersene49d5ec2000-02-08 19:58:47 +0000849 signal(SIGHUP, check_chroot);
Erik Andersende552872000-01-23 01:34:05 +0000850#endif
Eric Andersen0460ff21999-10-25 23:32:44 +0000851
Erik Andersene49d5ec2000-02-08 19:58:47 +0000852 /* Turn off rebooting via CTL-ALT-DEL -- we get a
853 * SIGINT on CAD so we can shut things down gracefully... */
854 reboot(RB_DISABLE_CAD);
855#endif
Erik Andersen05df2392000-01-13 04:43:48 +0000856
Erik Andersen298854f2000-03-23 01:09:18 +0000857 /* Figure out what kernel this is running */
858 kernelVersion = get_kernel_revision();
859
Erik Andersene49d5ec2000-02-08 19:58:47 +0000860 /* Figure out where the default console should be */
861 console_init();
Eric Andersen0460ff21999-10-25 23:32:44 +0000862
Erik Andersene49d5ec2000-02-08 19:58:47 +0000863 /* Close whatever files are open, and reset the console. */
864 close(0);
865 close(1);
866 close(2);
867 set_term(0);
Erik Andersen983b51b2000-04-04 18:14:25 +0000868 chdir("/");
Erik Andersene49d5ec2000-02-08 19:58:47 +0000869 setsid();
Eric Andersencc8ed391999-10-05 16:24:54 +0000870
Erik Andersene49d5ec2000-02-08 19:58:47 +0000871 /* Make sure PATH is set to something sane */
872 putenv(_PATH_STDPATH);
Eric Andersencc8ed391999-10-05 16:24:54 +0000873
Erik Andersene49d5ec2000-02-08 19:58:47 +0000874 /* Hello world */
Eric Andersen7f1acfd1999-10-29 23:09:13 +0000875#ifndef DEBUG_INIT
Erik Andersenea6b67d2000-03-07 07:47:10 +0000876 message(
877#if ! defined BB_FEATURE_EXTRA_QUIET
878 CONSOLE|
879#endif
880 LOG,
Erik Andersene49d5ec2000-02-08 19:58:47 +0000881 "init started: BusyBox v%s (%s) multi-call binary\r\n",
882 BB_VER, BB_BT);
Eric Andersen7f1acfd1999-10-29 23:09:13 +0000883#else
Erik Andersenea6b67d2000-03-07 07:47:10 +0000884 message(
885#if ! defined BB_FEATURE_EXTRA_QUIET
886 CONSOLE|
887#endif
888 LOG,
Erik Andersene49d5ec2000-02-08 19:58:47 +0000889 "init(%d) started: BusyBox v%s (%s) multi-call binary\r\n",
890 getpid(), BB_VER, BB_BT);
Eric Andersen7f1acfd1999-10-29 23:09:13 +0000891#endif
Eric Andersencc8ed391999-10-05 16:24:54 +0000892
Eric Andersencc8ed391999-10-05 16:24:54 +0000893
Erik Andersene49d5ec2000-02-08 19:58:47 +0000894 /* Make sure there is enough memory to do something useful. */
895 check_memory();
Erik Andersen9e737252000-01-06 01:16:13 +0000896
Erik Andersene49d5ec2000-02-08 19:58:47 +0000897 /* Check if we are supposed to be in single user mode */
898 if (argc > 1 && (!strcmp(argv[1], "single") ||
899 !strcmp(argv[1], "-s") || !strcmp(argv[1], "1"))) {
900 /* Ask first then start a shell on tty2 */
901 if (secondConsole != NULL)
902 new_initAction(ASKFIRST, SHELL, secondConsole);
903 /* Start a shell on tty1 */
904 new_initAction(RESPAWN, SHELL, console);
905 } else {
906 /* Not in single user mode -- see what inittab says */
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000907
Erik Andersene49d5ec2000-02-08 19:58:47 +0000908 /* NOTE that if BB_FEATURE_USE_INITTAB is NOT defined,
909 * then parse_inittab() simply adds in some default
910 * actions(i.e runs INIT_SCRIPT and then starts a pair
911 * of "askfirst" shells */
912 parse_inittab();
Eric Andersend00c2621999-12-07 08:37:31 +0000913 }
Erik Andersen983b51b2000-04-04 18:14:25 +0000914
Erik Andersene132f4b2000-02-09 04:16:43 +0000915 /* Fix up argv[0] to be certain we claim to be init */
916 strncpy(argv[0], "init", strlen(argv[0])+1);
Erik Andersen07f56042000-02-09 06:05:01 +0000917 if (argc > 1)
918 strncpy(argv[1], "\0", strlen(argv[1])+1);
Eric Andersen219d6f51999-11-02 19:43:01 +0000919
Erik Andersene49d5ec2000-02-08 19:58:47 +0000920 /* Now run everything that needs to be run */
921
922 /* First run the sysinit command */
923 for (a = initActionList; a; a = a->nextPtr) {
924 if (a->action == SYSINIT) {
925 waitfor(a->process, a->console, FALSE);
926 /* Now remove the "sysinit" entry from the list */
927 delete_initAction(a);
Erik Andersen7dc16072000-01-04 01:10:25 +0000928 }
Erik Andersen7dc16072000-01-04 01:10:25 +0000929 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000930 /* Next run anything that wants to block */
931 for (a = initActionList; a; a = a->nextPtr) {
932 if (a->action == WAIT) {
933 waitfor(a->process, a->console, FALSE);
934 /* Now remove the "wait" entry from the list */
935 delete_initAction(a);
Erik Andersen7dc16072000-01-04 01:10:25 +0000936 }
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000937 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000938 /* Next run anything to be run only once */
939 for (a = initActionList; a; a = a->nextPtr) {
940 if (a->action == ONCE) {
941 run(a->process, a->console, FALSE);
942 /* Now remove the "once" entry from the list */
943 delete_initAction(a);
944 }
945 }
946 /* If there is nothing else to do, stop */
947 if (initActionList == NULL) {
948 message(LOG | CONSOLE,
949 "No more tasks for init -- sleeping forever.\n");
950 while (1)
951 sleep(1);
952 }
953
954 /* Now run the looping stuff for the rest of forever */
955 while (1) {
956 for (a = initActionList; a; a = a->nextPtr) {
957 /* Only run stuff with pid==0. If they have
958 * a pid, that means they are still running */
959 if (a->pid == 0) {
960 switch (a->action) {
961 case RESPAWN:
962 /* run the respawn stuff */
963 a->pid = run(a->process, a->console, FALSE);
964 break;
965 case ASKFIRST:
966 /* run the askfirst stuff */
967 a->pid = run(a->process, a->console, TRUE);
968 break;
969 /* silence the compiler's incessant whining */
970 default:
971 break;
972 }
973 }
974 }
975 /* Wait for a child process to exit */
976 wpid = wait(&status);
977 if (wpid > 0) {
978 /* Find out who died and clean up their corpse */
979 for (a = initActionList; a; a = a->nextPtr) {
980 if (a->pid == wpid) {
981 a->pid = 0;
982 message(LOG,
983 "Process '%s' (pid %d) exited. Scheduling it for restart.\n",
984 a->process, wpid);
985 }
986 }
987 }
988 sleep(1);
989 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000990}
Erik Andersen029011b2000-03-04 21:19:32 +0000991
992/*
993Local Variables:
994c-file-style: "linux"
995c-basic-offset: 4
996tab-width: 4
997End:
998*/