blob: f0124a22533a7ba61d77ddc9693e19261326faa1 [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"
31#include <stdio.h>
Erik Andersen7dc16072000-01-04 01:10:25 +000032#include <string.h>
Eric Andersencc8ed391999-10-05 16:24:54 +000033#include <stdlib.h>
34#include <stdarg.h>
35#include <unistd.h>
36#include <errno.h>
37#include <signal.h>
38#include <termios.h>
Eric Andersenb186d981999-12-03 09:19:54 +000039#include <paths.h>
Eric Andersencc8ed391999-10-05 16:24:54 +000040#include <sys/types.h>
41#include <sys/fcntl.h>
42#include <sys/wait.h>
43#include <string.h>
44#include <sys/mount.h>
45#include <sys/reboot.h>
46#include <sys/kdaemon.h>
Eric Andersencc8ed391999-10-05 16:24:54 +000047#include <sys/sysmacros.h>
Erik Andersenfac10d72000-02-07 05:29:42 +000048#include <asm/types.h>
Erik Andersene49d5ec2000-02-08 19:58:47 +000049#include <linux/serial.h> /* for serial_struct */
50#include <sys/vt.h> /* for vt_stat */
Eric Andersenabc7d591999-10-19 00:27:50 +000051#include <sys/ioctl.h>
Erik Andersend07ee462000-02-21 21:26:32 +000052#include <sys/sysinfo.h> /* For check_free_memory() */
Erik Andersen4d1d0111999-12-17 18:44:15 +000053#include <linux/version.h>
Eric Andersen4c781471999-11-26 08:12:56 +000054#ifdef BB_SYSLOGD
55#include <sys/syslog.h>
56#endif
Eric Andersencc8ed391999-10-05 16:24:54 +000057
Erik Andersen4d1d0111999-12-17 18:44:15 +000058#ifndef KERNEL_VERSION
59#define KERNEL_VERSION(a,b,c) (((a) << 16) + ((b) << 8) + (c))
60#endif
61
Eric Andersen0ecb54a1999-12-05 23:24:55 +000062
Erik Andersene49d5ec2000-02-08 19:58:47 +000063#define VT_PRIMARY "/dev/tty1" /* Primary virtual console */
64#define VT_SECONDARY "/dev/tty2" /* Virtual console */
65#define VT_LOG "/dev/tty3" /* Virtual console */
66#define SERIAL_CON0 "/dev/ttyS0" /* Primary serial console */
67#define SERIAL_CON1 "/dev/ttyS1" /* Serial console */
68#define SHELL "/bin/sh" /* Default shell */
69#define INITTAB "/etc/inittab" /* inittab file location */
Erik Andersen96e2abd2000-01-07 11:40:44 +000070#ifndef INIT_SCRIPT
Erik Andersene49d5ec2000-02-08 19:58:47 +000071#define INIT_SCRIPT "/etc/init.d/rcS" /* Default sysinit script. */
Erik Andersen96e2abd2000-01-07 11:40:44 +000072#endif
Erik Andersen9c88cac1999-12-30 09:25:17 +000073
Eric Andersen3ae0c781999-11-04 01:13:21 +000074#define LOG 0x1
75#define CONSOLE 0x2
Erik Andersen7dc16072000-01-04 01:10:25 +000076
77/* Allowed init action types */
78typedef enum {
Erik Andersene49d5ec2000-02-08 19:58:47 +000079 SYSINIT = 1,
80 RESPAWN,
81 ASKFIRST,
82 WAIT,
Erik Andersene132f4b2000-02-09 04:16:43 +000083 ONCE,
84 CTRLALTDEL
Erik Andersen7dc16072000-01-04 01:10:25 +000085} initActionEnum;
86
87/* And now a list of the actions we support in the version of init */
Erik Andersene49d5ec2000-02-08 19:58:47 +000088typedef struct initActionType {
89 const char *name;
90 initActionEnum action;
Erik Andersen7dc16072000-01-04 01:10:25 +000091} initActionType;
92
93static const struct initActionType actions[] = {
Erik Andersene49d5ec2000-02-08 19:58:47 +000094 {"sysinit", SYSINIT},
95 {"respawn", RESPAWN},
96 {"askfirst", ASKFIRST},
97 {"wait", WAIT},
98 {"once", ONCE},
Erik Andersene132f4b2000-02-09 04:16:43 +000099 {"ctrlaltdel", CTRLALTDEL},
Erik Andersene49d5ec2000-02-08 19:58:47 +0000100 {0}
Erik Andersen7dc16072000-01-04 01:10:25 +0000101};
102
103/* Set up a linked list of initactions, to be read from inittab */
104typedef struct initActionTag initAction;
105struct initActionTag {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000106 pid_t pid;
107 char process[256];
108 char console[256];
109 initAction *nextPtr;
110 initActionEnum action;
Erik Andersen7dc16072000-01-04 01:10:25 +0000111};
Erik Andersene49d5ec2000-02-08 19:58:47 +0000112initAction *initActionList = NULL;
Erik Andersen7dc16072000-01-04 01:10:25 +0000113
114
Erik Andersenac6e71f2000-01-08 22:04:33 +0000115static char *secondConsole = VT_SECONDARY;
Eric Andersenbe971d61999-11-03 16:52:50 +0000116static char *log = VT_LOG;
Erik Andersenac6e71f2000-01-08 22:04:33 +0000117static int kernelVersion = 0;
Erik Andersene2729152000-02-18 21:34:17 +0000118static char termType[32] = "TERM=linux";
Erik Andersen31638212000-01-15 22:28:50 +0000119static char console[32] = _PATH_CONSOLE;
Erik Andersene132f4b2000-02-09 04:16:43 +0000120static void delete_initAction(initAction * action);
Eric Andersen0460ff21999-10-25 23:32:44 +0000121
122
Eric Andersen3ae0c781999-11-04 01:13:21 +0000123/* print a message to the specified device:
124 * device may be bitwise-or'd from LOG | CONSOLE */
125void message(int device, char *fmt, ...)
Eric Andersen0460ff21999-10-25 23:32:44 +0000126{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000127 va_list arguments;
128 int fd;
Erik Andersen7dc16072000-01-04 01:10:25 +0000129
Eric Andersen4c781471999-11-26 08:12:56 +0000130#ifdef BB_SYSLOGD
131
Erik Andersene49d5ec2000-02-08 19:58:47 +0000132 /* Log the message to syslogd */
133 if (device & LOG) {
134 char msg[1024];
Eric Andersen4c781471999-11-26 08:12:56 +0000135
Erik Andersene49d5ec2000-02-08 19:58:47 +0000136 va_start(arguments, fmt);
137 vsnprintf(msg, sizeof(msg), fmt, arguments);
138 va_end(arguments);
Erik Andersene132f4b2000-02-09 04:16:43 +0000139 openlog("init", 0, LOG_USER);
140 syslog(LOG_USER|LOG_INFO, msg);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000141 closelog();
Eric Andersen3ae0c781999-11-04 01:13:21 +0000142 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000143#else
144 static int log_fd = -1;
145
146 /* Take full control of the log tty, and never close it.
147 * It's mine, all mine! Muhahahaha! */
148 if (log_fd < 0) {
149 if (log == NULL) {
150 /* don't even try to log, because there is no such console */
151 log_fd = -2;
152 /* log to main console instead */
153 device = CONSOLE;
Erik Andersene132f4b2000-02-09 04:16:43 +0000154 } else if ((log_fd = device_open(log, O_RDWR|O_NDELAY)) < 0) {
155 log_fd = -2;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000156 fprintf(stderr, "Bummer, can't write to log on %s!\r\n", log);
157 fflush(stderr);
Erik Andersene132f4b2000-02-09 04:16:43 +0000158 log = NULL;
159 device = CONSOLE;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000160 }
161 }
162 if ((device & LOG) && (log_fd >= 0)) {
163 va_start(arguments, fmt);
164 vdprintf(log_fd, fmt, arguments);
165 va_end(arguments);
166 }
Eric Andersen4c781471999-11-26 08:12:56 +0000167#endif
168
Erik Andersene49d5ec2000-02-08 19:58:47 +0000169 if (device & CONSOLE) {
170 /* Always send console messages to /dev/console so people will see them. */
171 if (
172 (fd =
173 device_open(_PATH_CONSOLE,
174 O_WRONLY | O_NOCTTY | O_NDELAY)) >= 0) {
175 va_start(arguments, fmt);
176 vdprintf(fd, fmt, arguments);
177 va_end(arguments);
178 close(fd);
179 } else {
180 fprintf(stderr, "Bummer, can't print: ");
181 va_start(arguments, fmt);
182 vfprintf(stderr, fmt, arguments);
183 fflush(stderr);
184 va_end(arguments);
185 }
Eric Andersen3ae0c781999-11-04 01:13:21 +0000186 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000187}
188
Eric Andersen3ae0c781999-11-04 01:13:21 +0000189
Eric Andersen0460ff21999-10-25 23:32:44 +0000190/* Set terminal settings to reasonable defaults */
Erik Andersene49d5ec2000-02-08 19:58:47 +0000191void set_term(int fd)
Eric Andersencc8ed391999-10-05 16:24:54 +0000192{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000193 struct termios tty;
Eric Andersencc8ed391999-10-05 16:24:54 +0000194
Erik Andersene49d5ec2000-02-08 19:58:47 +0000195 tcgetattr(fd, &tty);
Eric Andersencc8ed391999-10-05 16:24:54 +0000196
Erik Andersene49d5ec2000-02-08 19:58:47 +0000197 /* set control chars */
Erik Andersen6c41c442000-03-19 05:13:49 +0000198 tty.c_cc[VINTR] = 3; /* C-c */
199 tty.c_cc[VQUIT] = 28; /* C-\ */
200 tty.c_cc[VERASE] = 127; /* C-? */
201 tty.c_cc[VKILL] = 21; /* C-u */
202 tty.c_cc[VEOF] = 4; /* C-d */
203 tty.c_cc[VSTART] = 17; /* C-q */
204 tty.c_cc[VSTOP] = 19; /* C-s */
205 tty.c_cc[VSUSP] = 26; /* C-z */
Eric Andersencc8ed391999-10-05 16:24:54 +0000206
Erik Andersene49d5ec2000-02-08 19:58:47 +0000207 /* use line dicipline 0 */
208 tty.c_line = 0;
Eric Andersen7f1acfd1999-10-29 23:09:13 +0000209
Erik Andersene49d5ec2000-02-08 19:58:47 +0000210 /* Make it be sane */
Erik Andersen6c41c442000-03-19 05:13:49 +0000211 tty.c_cflag &= CBAUD|CBAUDEX|CSIZE|CSTOPB|PARENB|PARODD;
212 tty.c_cflag |= HUPCL|CLOCAL;
Eric Andersen08b10341999-11-19 02:38:58 +0000213
Erik Andersene49d5ec2000-02-08 19:58:47 +0000214 /* input modes */
215 tty.c_iflag = ICRNL | IXON | IXOFF;
Eric Andersen08b10341999-11-19 02:38:58 +0000216
Erik Andersene49d5ec2000-02-08 19:58:47 +0000217 /* output modes */
218 tty.c_oflag = OPOST | ONLCR;
Eric Andersen7f1acfd1999-10-29 23:09:13 +0000219
Erik Andersene49d5ec2000-02-08 19:58:47 +0000220 /* local modes */
221 tty.c_lflag =
222 ISIG | ICANON | ECHO | ECHOE | ECHOK | ECHOCTL | ECHOKE | IEXTEN;
Eric Andersencc8ed391999-10-05 16:24:54 +0000223
Erik Andersene49d5ec2000-02-08 19:58:47 +0000224 tcsetattr(fd, TCSANOW, &tty);
Eric Andersencc8ed391999-10-05 16:24:54 +0000225}
226
Eric Andersen219d6f51999-11-02 19:43:01 +0000227/* How much memory does this machine have? */
Erik Andersend07ee462000-02-21 21:26:32 +0000228static int check_free_memory()
Eric Andersencc8ed391999-10-05 16:24:54 +0000229{
Erik Andersend07ee462000-02-21 21:26:32 +0000230 struct sysinfo info;
Eric Andersencc8ed391999-10-05 16:24:54 +0000231
Erik Andersend07ee462000-02-21 21:26:32 +0000232 sysinfo(&info);
233 if (sysinfo(&info) != 0) {
234 message(LOG, "Error checking free memory: %s\n", strerror(errno));
Erik Andersene49d5ec2000-02-08 19:58:47 +0000235 return -1;
Eric Andersenabc7d591999-10-19 00:27:50 +0000236 }
Erik Andersend07ee462000-02-21 21:26:32 +0000237
238 return(info.freeram/1024);
239
Eric Andersencc8ed391999-10-05 16:24:54 +0000240}
241
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000242static void console_init()
Eric Andersen0460ff21999-10-25 23:32:44 +0000243{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000244 int fd;
245 int tried_devcons = 0;
246 int tried_vtprimary = 0;
Erik Andersen029011b2000-03-04 21:19:32 +0000247 struct vt_stat vt;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000248 struct serial_struct sr;
249 char *s;
Eric Andersen0460ff21999-10-25 23:32:44 +0000250
Erik Andersene49d5ec2000-02-08 19:58:47 +0000251 if ((s = getenv("TERM")) != NULL) {
252 snprintf(termType, sizeof(termType) - 1, "TERM=%s", s);
253 }
Erik Andersenac6e71f2000-01-08 22:04:33 +0000254
Erik Andersene49d5ec2000-02-08 19:58:47 +0000255 if ((s = getenv("CONSOLE")) != NULL) {
256 snprintf(console, sizeof(console) - 1, "%s", s);
257 }
Eric Andersenfbb39c81999-11-08 17:00:52 +0000258#if #cpu(sparc)
Erik Andersene49d5ec2000-02-08 19:58:47 +0000259 /* sparc kernel supports console=tty[ab] parameter which is also
260 * passed to init, so catch it here */
261 else if ((s = getenv("console")) != NULL) {
262 /* remap tty[ab] to /dev/ttyS[01] */
263 if (strcmp(s, "ttya") == 0)
264 snprintf(console, sizeof(console) - 1, "%s", SERIAL_CON0);
265 else if (strcmp(s, "ttyb") == 0)
266 snprintf(console, sizeof(console) - 1, "%s", SERIAL_CON1);
267 }
Eric Andersen219d6f51999-11-02 19:43:01 +0000268#endif
Erik Andersene49d5ec2000-02-08 19:58:47 +0000269 else {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000270 /* 2.2 kernels: identify the real console backend and try to use it */
271 if (ioctl(0, TIOCGSERIAL, &sr) == 0) {
272 /* this is a serial console */
273 snprintf(console, sizeof(console) - 1, "/dev/ttyS%d", sr.line);
274 } else if (ioctl(0, VT_GETSTATE, &vt) == 0) {
275 /* this is linux virtual tty */
276 snprintf(console, sizeof(console) - 1, "/dev/tty%d",
277 vt.v_active);
278 } else {
279 snprintf(console, sizeof(console) - 1, "%s", _PATH_CONSOLE);
280 tried_devcons++;
281 }
Eric Andersen07e52971999-11-07 07:38:08 +0000282 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000283
284 while ((fd = open(console, O_RDONLY | O_NONBLOCK)) < 0) {
285 /* Can't open selected console -- try /dev/console */
286 if (!tried_devcons) {
287 tried_devcons++;
288 snprintf(console, sizeof(console) - 1, "%s", _PATH_CONSOLE);
289 continue;
290 }
291 /* Can't open selected console -- try vt1 */
292 if (!tried_vtprimary) {
293 tried_vtprimary++;
294 snprintf(console, sizeof(console) - 1, "%s", VT_PRIMARY);
295 continue;
296 }
297 break;
298 }
299 if (fd < 0) {
300 /* Perhaps we should panic here? */
301 snprintf(console, sizeof(console) - 1, "/dev/null");
Eric Andersen07e52971999-11-07 07:38:08 +0000302 } else {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000303 /* check for serial console and disable logging to tty3 & running a
304 * shell to tty2 */
305 if (ioctl(0, TIOCGSERIAL, &sr) == 0) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000306 log = NULL;
307 secondConsole = NULL;
Erik Andersenfa4718e2000-02-21 19:25:12 +0000308 /* Force the TERM setting to vt102 for serial console --
309 * iff TERM is set to linux (the default) */
310 if (strcmp( termType, "TERM=linux" ) == 0)
311 snprintf(termType, sizeof(termType) - 1, "TERM=vt102");
Erik Andersene132f4b2000-02-09 04:16:43 +0000312 message(LOG | CONSOLE,
313 "serial console detected. Disabling virtual terminals.\r\n");
Erik Andersene49d5ec2000-02-08 19:58:47 +0000314 }
315 close(fd);
Eric Andersen07e52971999-11-07 07:38:08 +0000316 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000317 message(LOG, "console=%s\n", console);
Eric Andersen0460ff21999-10-25 23:32:44 +0000318}
319
Erik Andersene49d5ec2000-02-08 19:58:47 +0000320static pid_t run(char *command, char *terminal, int get_enter)
Eric Andersen0460ff21999-10-25 23:32:44 +0000321{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000322 int i, fd;
323 pid_t pid;
324 char *tmpCmd;
325 char *cmd[255];
Erik Andersene132f4b2000-02-09 04:16:43 +0000326 char buf[255];
Erik Andersene49d5ec2000-02-08 19:58:47 +0000327 static const char press_enter[] =
328
329 "\nPlease press Enter to activate this console. ";
330 char *environment[] = {
331 "HOME=/",
332 "PATH=/usr/bin:/bin:/usr/sbin:/sbin",
333 "SHELL=/bin/sh",
334 termType,
335 "USER=root",
336 0
337 };
Erik Andersenac6e71f2000-01-08 22:04:33 +0000338
Eric Andersen0460ff21999-10-25 23:32:44 +0000339
Erik Andersene49d5ec2000-02-08 19:58:47 +0000340 if ((pid = fork()) == 0) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000341 /* Clean up */
342 close(0);
343 close(1);
344 close(2);
345 setsid();
Eric Andersen0460ff21999-10-25 23:32:44 +0000346
Erik Andersene49d5ec2000-02-08 19:58:47 +0000347 /* Reset signal handlers set for parent process */
348 signal(SIGUSR1, SIG_DFL);
349 signal(SIGUSR2, SIG_DFL);
350 signal(SIGINT, SIG_DFL);
351 signal(SIGTERM, SIG_DFL);
352 signal(SIGHUP, SIG_DFL);
Eric Andersen2f6c04f1999-11-01 23:59:44 +0000353
Erik Andersene49d5ec2000-02-08 19:58:47 +0000354 if ((fd = device_open(terminal, O_RDWR)) < 0) {
355 message(LOG | CONSOLE, "Bummer, can't open %s\r\n", terminal);
356 exit(1);
357 }
358 dup2(fd, 0);
359 dup2(fd, 1);
360 dup2(fd, 2);
361 tcsetpgrp(0, getpgrp());
362 set_term(0);
363
364 if (get_enter == TRUE) {
365 /*
366 * Save memory by not exec-ing anything large (like a shell)
367 * before the user wants it. This is critical if swap is not
368 * enabled and the system has low memory. Generally this will
369 * be run on the second virtual console, and the first will
370 * be allowed to start a shell or whatever an init script
371 * specifies.
372 */
373 char c;
Erik Andersene132f4b2000-02-09 04:16:43 +0000374#ifdef DEBUG_INIT
Erik Andersen2ac2fae2000-03-07 23:32:17 +0000375 pid_t shell_pgid = getpid();
Erik Andersene49d5ec2000-02-08 19:58:47 +0000376 message(LOG, "Waiting for enter to start '%s' (pid %d, console %s)\r\n",
377 command, shell_pgid, terminal);
Erik Andersene132f4b2000-02-09 04:16:43 +0000378#endif
Erik Andersene49d5ec2000-02-08 19:58:47 +0000379 write(fileno(stdout), press_enter, sizeof(press_enter) - 1);
380 read(fileno(stdin), &c, 1);
381 }
382
Erik Andersene49d5ec2000-02-08 19:58:47 +0000383#ifdef DEBUG_INIT
Erik Andersene132f4b2000-02-09 04:16:43 +0000384 /* Log the process name and args */
385 message(LOG, "Starting pid %d, console %s: '%s'\r\n",
386 shell_pgid, terminal, command);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000387#endif
Erik Andersene132f4b2000-02-09 04:16:43 +0000388
389 /* See if any special /bin/sh requiring characters are present */
390 if (strpbrk(command, "~`!$^&*()=|\\{}[];\"'<>?") != NULL) {
391 cmd[0] = SHELL;
392 cmd[1] = "-c";
393 strcpy(buf, "exec ");
394 strncat(buf, command, sizeof(buf) - strlen(buf) - 1);
395 cmd[2] = buf;
396 cmd[3] = NULL;
397 } else {
398 /* Convert command (char*) into cmd (char**, one word per string) */
399 for (tmpCmd = command, i = 0;
400 (tmpCmd = strsep(&command, " \t")) != NULL;) {
401 if (*tmpCmd != '\0') {
402 cmd[i] = tmpCmd;
403 tmpCmd++;
404 i++;
405 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000406 }
Erik Andersene132f4b2000-02-09 04:16:43 +0000407 cmd[i] = NULL;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000408 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000409
410 /* Now run it. The new program will take over this PID,
411 * so nothing further in init.c should be run. */
412 execve(cmd[0], cmd, environment);
413
414 /* We're still here? Some error happened. */
415 message(LOG | CONSOLE, "Bummer, could not run '%s': %s\n", cmd[0],
416 strerror(errno));
417 exit(-1);
Erik Andersen05df2392000-01-13 04:43:48 +0000418 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000419 return pid;
Eric Andersen0460ff21999-10-25 23:32:44 +0000420}
421
Erik Andersene49d5ec2000-02-08 19:58:47 +0000422static int waitfor(char *command, char *terminal, int get_enter)
Erik Andersen0e3782f2000-01-07 02:54:55 +0000423{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000424 int status, wpid;
425 int pid = run(command, terminal, get_enter);
Erik Andersen0e3782f2000-01-07 02:54:55 +0000426
Erik Andersene49d5ec2000-02-08 19:58:47 +0000427 while (1) {
428 wpid = wait(&status);
Erik Andersene132f4b2000-02-09 04:16:43 +0000429 if (wpid > 0 && wpid != pid) {
430 continue;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000431 }
432 if (wpid == pid)
433 break;
Erik Andersen0e3782f2000-01-07 02:54:55 +0000434 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000435 return wpid;
Erik Andersen0e3782f2000-01-07 02:54:55 +0000436}
437
Eric Andersen219d6f51999-11-02 19:43:01 +0000438/* Make sure there is enough memory to do something useful. *
Erik Andersene132f4b2000-02-09 04:16:43 +0000439 * Calls "swapon -a" if needed so be sure /etc/fstab is present... */
Eric Andersen219d6f51999-11-02 19:43:01 +0000440static void check_memory()
441{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000442 struct stat statBuf;
Eric Andersen219d6f51999-11-02 19:43:01 +0000443
Erik Andersend07ee462000-02-21 21:26:32 +0000444 if (check_free_memory() > 1000)
Erik Andersene49d5ec2000-02-08 19:58:47 +0000445 return;
446
447 if (stat("/etc/fstab", &statBuf) == 0) {
448 /* Try to turn on swap */
Erik Andersenfb1793f2000-02-09 16:37:08 +0000449 system("/sbin/swapon -a");
Erik Andersend07ee462000-02-21 21:26:32 +0000450 if (check_free_memory() < 1000)
Erik Andersene49d5ec2000-02-08 19:58:47 +0000451 goto goodnight;
452 } else
453 goto goodnight;
Eric Andersen219d6f51999-11-02 19:43:01 +0000454 return;
455
Erik Andersene49d5ec2000-02-08 19:58:47 +0000456 goodnight:
457 message(CONSOLE,
458 "Sorry, your computer does not have enough memory.\r\n");
459 while (1)
460 sleep(1);
Eric Andersen219d6f51999-11-02 19:43:01 +0000461}
462
Erik Andersene132f4b2000-02-09 04:16:43 +0000463/* Run all commands to be run right before halt/reboot */
464static void run_lastAction(void)
465{
466 initAction *a;
467 for (a = initActionList; a; a = a->nextPtr) {
468 if (a->action == CTRLALTDEL) {
469 waitfor(a->process, a->console, FALSE);
470 delete_initAction(a);
471 }
472 }
473}
474
475
Erik Andersen7dc16072000-01-04 01:10:25 +0000476#ifndef DEBUG_INIT
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000477static void shutdown_system(void)
Eric Andersencc8ed391999-10-05 16:24:54 +0000478{
Erik Andersene132f4b2000-02-09 04:16:43 +0000479
Erik Andersene49d5ec2000-02-08 19:58:47 +0000480 /* first disable our SIGHUP signal */
481 signal(SIGHUP, SIG_DFL);
Erik Andersen7dc16072000-01-04 01:10:25 +0000482
Erik Andersene49d5ec2000-02-08 19:58:47 +0000483 /* Allow Ctrl-Alt-Del to reboot system. */
484 reboot(RB_ENABLE_CAD);
Erik Andersene132f4b2000-02-09 04:16:43 +0000485
486 message(CONSOLE|LOG, "\r\nThe system is going down NOW !!\r\n");
Eric Andersencf8c9cf1999-11-05 00:31:46 +0000487 sync();
Erik Andersene49d5ec2000-02-08 19:58:47 +0000488
489 /* Send signals to every process _except_ pid 1 */
Erik Andersene132f4b2000-02-09 04:16:43 +0000490 message(CONSOLE|LOG, "Sending SIGTERM to all processes.\r\n");
Erik Andersene49d5ec2000-02-08 19:58:47 +0000491 kill(-1, SIGTERM);
Erik Andersene132f4b2000-02-09 04:16:43 +0000492 sleep(1);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000493 sync();
494
Erik Andersene132f4b2000-02-09 04:16:43 +0000495 message(CONSOLE|LOG, "Sending SIGKILL to all processes.\r\n");
Erik Andersene49d5ec2000-02-08 19:58:47 +0000496 kill(-1, SIGKILL);
Erik Andersene132f4b2000-02-09 04:16:43 +0000497 sleep(1);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000498
Erik Andersene132f4b2000-02-09 04:16:43 +0000499 /* run everything to be run at "ctrlaltdel" */
500 run_lastAction();
501
Erik Andersene49d5ec2000-02-08 19:58:47 +0000502 sync();
503 if (kernelVersion > 0 && kernelVersion <= 2 * 65536 + 2 * 256 + 11) {
504 /* bdflush, kupdate not needed for kernels >2.2.11 */
505 bdflush(1, 0);
506 sync();
507 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000508}
509
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000510static void halt_signal(int sig)
Eric Andersencc8ed391999-10-05 16:24:54 +0000511{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000512 shutdown_system();
Erik Andersene132f4b2000-02-09 04:16:43 +0000513 message(CONSOLE|LOG,
Erik Andersene49d5ec2000-02-08 19:58:47 +0000514 "The system is halted. Press %s or turn off power\r\n",
515 (secondConsole == NULL) /* serial console */
516 ? "Reset" : "CTRL-ALT-DEL");
517 sync();
Erik Andersen3fe39dc2000-01-25 18:13:53 +0000518
Erik Andersene49d5ec2000-02-08 19:58:47 +0000519 /* allow time for last message to reach serial console */
Erik Andersene132f4b2000-02-09 04:16:43 +0000520 sleep(2);
Erik Andersen3fe39dc2000-01-25 18:13:53 +0000521
Erik Andersen4d1d0111999-12-17 18:44:15 +0000522#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,2,0)
Erik Andersene49d5ec2000-02-08 19:58:47 +0000523 if (sig == SIGUSR2)
524 reboot(RB_POWER_OFF);
525 else
Erik Andersen4d1d0111999-12-17 18:44:15 +0000526#endif
Erik Andersene49d5ec2000-02-08 19:58:47 +0000527 reboot(RB_HALT_SYSTEM);
528 exit(0);
Eric Andersencc8ed391999-10-05 16:24:54 +0000529}
530
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000531static void reboot_signal(int sig)
Eric Andersencc8ed391999-10-05 16:24:54 +0000532{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000533 shutdown_system();
Erik Andersene132f4b2000-02-09 04:16:43 +0000534 message(CONSOLE|LOG, "Please stand by while rebooting the system.\r\n");
Erik Andersene49d5ec2000-02-08 19:58:47 +0000535 sync();
Erik Andersen3fe39dc2000-01-25 18:13:53 +0000536
Erik Andersene49d5ec2000-02-08 19:58:47 +0000537 /* allow time for last message to reach serial console */
538 sleep(2);
Erik Andersen3fe39dc2000-01-25 18:13:53 +0000539
Erik Andersene49d5ec2000-02-08 19:58:47 +0000540 reboot(RB_AUTOBOOT);
541 exit(0);
Eric Andersencc8ed391999-10-05 16:24:54 +0000542}
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000543
Erik Andersende552872000-01-23 01:34:05 +0000544#if defined BB_FEATURE_INIT_CHROOT
Erik Andersend07ee462000-02-21 21:26:32 +0000545
546#if ! defined BB_FEATURE_USE_PROCFS
547#error Sorry, I depend on the /proc filesystem right now.
548#endif
549
Erik Andersende552872000-01-23 01:34:05 +0000550static void check_chroot(int sig)
551{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000552 char *argv_init[2] = { "init", NULL, };
553 char *envp_init[3] = { "HOME=/", "TERM=linux", NULL, };
554 char rootpath[256], *tc;
555 int fd;
Erik Andersende552872000-01-23 01:34:05 +0000556
Erik Andersene49d5ec2000-02-08 19:58:47 +0000557 if ((fd = open("/proc/sys/kernel/init-chroot", O_RDONLY)) == -1) {
558 message(CONSOLE,
559 "SIGHUP recived, but could not open proc file\r\n");
560 sleep(2);
561 return;
562 }
563 if (read(fd, rootpath, sizeof(rootpath)) == -1) {
564 message(CONSOLE,
565 "SIGHUP recived, but could not read proc file\r\n");
566 sleep(2);
567 return;
568 }
569 close(fd);
570
571 if (rootpath[0] == '\0') {
572 message(CONSOLE,
573 "SIGHUP recived, but new root is not valid: %s\r\n",
574 rootpath);
575 sleep(2);
576 return;
577 }
578
579 tc = strrchr(rootpath, '\n');
580 *tc = '\0';
581
582 /* Ok, making it this far means we commit */
583 message(CONSOLE, "Please stand by, changing root to `%s'.\r\n",
584 rootpath);
585
586 /* kill all other programs first */
587 message(CONSOLE, "Sending SIGTERM to all processes.\r\n");
588 kill(-1, SIGTERM);
Erik Andersende552872000-01-23 01:34:05 +0000589 sleep(2);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000590 sync();
591
592 message(CONSOLE, "Sending SIGKILL to all processes.\r\n");
593 kill(-1, SIGKILL);
Erik Andersende552872000-01-23 01:34:05 +0000594 sleep(2);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000595 sync();
596
597 /* ok, we don't need /proc anymore. we also assume that the signaling
598 * process left the rest of the filesystems alone for us */
599 umount("/proc");
600
601 /* Ok, now we chroot. Hopefully we only have two things mounted, the
602 * new chroot'd mount point, and the old "/" mount. s,
603 * we go ahead and unmount the old "/". This should trigger the kernel
604 * to set things up the Right Way(tm). */
605
606 if (!chroot(rootpath))
607 umount("/dev/root");
608
609 /* If the chroot fails, we are already too far to turn back, so we
610 * continue and hope that executing init below will revive the system */
611
612 /* close all of our descriptors and open new ones */
613 close(0);
614 close(1);
615 close(2);
616 open("/dev/console", O_RDWR, 0);
617 dup(0);
618 dup(0);
619
620 message(CONSOLE, "Executing real init...\r\n");
621 /* execute init in the (hopefully) new root */
622 execve("/sbin/init", argv_init, envp_init);
623
624 message(CONSOLE,
625 "ERROR: Could not exec new init. Press %s to reboot.\r\n",
626 (secondConsole == NULL) /* serial console */
627 ? "Reset" : "CTRL-ALT-DEL");
Erik Andersende552872000-01-23 01:34:05 +0000628 return;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000629}
630#endif /* BB_FEATURE_INIT_CHROOT */
Erik Andersende552872000-01-23 01:34:05 +0000631
Erik Andersene49d5ec2000-02-08 19:58:47 +0000632#endif /* ! DEBUG_INIT */
Erik Andersende552872000-01-23 01:34:05 +0000633
Erik Andersene49d5ec2000-02-08 19:58:47 +0000634void new_initAction(initActionEnum action, char *process, char *cons)
Erik Andersen7dc16072000-01-04 01:10:25 +0000635{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000636 initAction *newAction;
Erik Andersen9e737252000-01-06 01:16:13 +0000637
Erik Andersene49d5ec2000-02-08 19:58:47 +0000638 if (*cons == '\0')
639 cons = console;
Erik Andersen9e737252000-01-06 01:16:13 +0000640
Erik Andersene49d5ec2000-02-08 19:58:47 +0000641 /* If BusyBox detects that a serial console is in use,
642 * then entries not refering to the console or null devices will _not_ be run.
643 * The exception to this rule is the null device.
644 */
645 if (secondConsole == NULL && strcmp(cons, console)
646 && strcmp(cons, "/dev/null"))
647 return;
648
649 newAction = calloc((size_t) (1), sizeof(initAction));
650 if (!newAction) {
651 message(LOG | CONSOLE, "Memory allocation failure\n");
652 while (1)
653 sleep(1);
654 }
655 newAction->nextPtr = initActionList;
656 initActionList = newAction;
657 strncpy(newAction->process, process, 255);
658 newAction->action = action;
659 strncpy(newAction->console, cons, 255);
660 newAction->pid = 0;
Erik Andersen31638212000-01-15 22:28:50 +0000661// message(LOG|CONSOLE, "process='%s' action='%d' console='%s'\n",
Erik Andersene49d5ec2000-02-08 19:58:47 +0000662// newAction->process, newAction->action, newAction->console);
Erik Andersen7dc16072000-01-04 01:10:25 +0000663}
664
Erik Andersene132f4b2000-02-09 04:16:43 +0000665static void delete_initAction(initAction * action)
Erik Andersen7dc16072000-01-04 01:10:25 +0000666{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000667 initAction *a, *b = NULL;
668
669 for (a = initActionList; a; b = a, a = a->nextPtr) {
670 if (a == action) {
671 if (b == NULL) {
672 initActionList = a->nextPtr;
673 } else {
674 b->nextPtr = a->nextPtr;
675 }
676 free(a);
677 break;
678 }
Erik Andersen7dc16072000-01-04 01:10:25 +0000679 }
Erik Andersen7dc16072000-01-04 01:10:25 +0000680}
681
Erik Andersen9e737252000-01-06 01:16:13 +0000682/* NOTE that if BB_FEATURE_USE_INITTAB is NOT defined,
683 * then parse_inittab() simply adds in some default
684 * actions(i.e runs INIT_SCRIPT and then starts a pair
Erik Andersen812d4662000-01-07 18:30:40 +0000685 * of "askfirst" shells). If BB_FEATURE_USE_INITTAB
686 * _is_ defined, but /etc/inittab is missing, this
687 * results in the same set of default behaviors.
Erik Andersen9e737252000-01-06 01:16:13 +0000688 * */
Erik Andersene49d5ec2000-02-08 19:58:47 +0000689void parse_inittab(void)
Erik Andersen7dc16072000-01-04 01:10:25 +0000690{
Erik Andersen9e737252000-01-06 01:16:13 +0000691#ifdef BB_FEATURE_USE_INITTAB
Erik Andersene49d5ec2000-02-08 19:58:47 +0000692 FILE *file;
693 char buf[256], lineAsRead[256], tmpConsole[256];
694 char *p, *q, *r, *s;
695 const struct initActionType *a = actions;
696 int foundIt;
Erik Andersen7dc16072000-01-04 01:10:25 +0000697
698
Erik Andersene49d5ec2000-02-08 19:58:47 +0000699 file = fopen(INITTAB, "r");
700 if (file == NULL) {
701 /* No inittab file -- set up some default behavior */
Erik Andersen9e737252000-01-06 01:16:13 +0000702#endif
Erik Andersene132f4b2000-02-09 04:16:43 +0000703 /* Swapoff on halt/reboot */
Erik Andersenfb1793f2000-02-09 16:37:08 +0000704 new_initAction(CTRLALTDEL, "/sbin/swapoff -a > /dev/null 2>&1", console);
Erik Andersene132f4b2000-02-09 04:16:43 +0000705 /* Umount all filesystems on halt/reboot */
706 new_initAction(CTRLALTDEL, "/bin/umount -a -r > /dev/null 2>&1", console);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000707 /* Askfirst shell on tty1 */
708 new_initAction(ASKFIRST, SHELL, console);
709 /* Askfirst shell on tty2 */
710 if (secondConsole != NULL)
711 new_initAction(ASKFIRST, SHELL, secondConsole);
712 /* sysinit */
713 new_initAction(SYSINIT, INIT_SCRIPT, console);
Erik Andersen7dc16072000-01-04 01:10:25 +0000714
Erik Andersene49d5ec2000-02-08 19:58:47 +0000715 return;
Erik Andersen9e737252000-01-06 01:16:13 +0000716#ifdef BB_FEATURE_USE_INITTAB
Erik Andersen9e737252000-01-06 01:16:13 +0000717 }
Erik Andersen7dc16072000-01-04 01:10:25 +0000718
Erik Andersene49d5ec2000-02-08 19:58:47 +0000719 while (fgets(buf, 255, file) != NULL) {
720 foundIt = FALSE;
721 for (p = buf; *p == ' ' || *p == '\t'; p++);
722 if (*p == '#' || *p == '\n')
723 continue;
Erik Andersen7dc16072000-01-04 01:10:25 +0000724
Erik Andersene49d5ec2000-02-08 19:58:47 +0000725 /* Trim the trailing \n */
726 q = strrchr(p, '\n');
727 if (q != NULL)
728 *q = '\0';
Erik Andersen7dc16072000-01-04 01:10:25 +0000729
Erik Andersene49d5ec2000-02-08 19:58:47 +0000730 /* Keep a copy around for posterity's sake (and error msgs) */
731 strcpy(lineAsRead, buf);
732
733 /* Grab the ID field */
734 s = p;
735 p = strchr(p, ':');
736 if (p != NULL || *(p + 1) != '\0') {
737 *p = '\0';
738 ++p;
Erik Andersen0e3782f2000-01-07 02:54:55 +0000739 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000740
741 /* Now peal off the process field from the end
742 * of the string */
743 q = strrchr(p, ':');
744 if (q == NULL || *(q + 1) == '\0') {
745 message(LOG | CONSOLE, "Bad inittab entry: %s\n", lineAsRead);
746 continue;
747 } else {
748 *q = '\0';
749 ++q;
750 }
751
752 /* Now peal off the action field */
753 r = strrchr(p, ':');
754 if (r == NULL || *(r + 1) == '\0') {
755 message(LOG | CONSOLE, "Bad inittab entry: %s\n", lineAsRead);
756 continue;
757 } else {
758 ++r;
759 }
760
761 /* Ok, now process it */
762 a = actions;
763 while (a->name != 0) {
764 if (strcmp(a->name, r) == 0) {
765 if (*s != '\0') {
766 struct stat statBuf;
767
768 strcpy(tmpConsole, "/dev/");
769 strncat(tmpConsole, s, 200);
770 if (stat(tmpConsole, &statBuf) != 0) {
771 message(LOG | CONSOLE,
772 "device '%s' does not exist. Did you read the directions?\n",
773 tmpConsole);
774 break;
775 }
776 s = tmpConsole;
777 }
778 new_initAction(a->action, q, s);
779 foundIt = TRUE;
780 }
781 a++;
782 }
783 if (foundIt == TRUE)
784 continue;
785 else {
786 /* Choke on an unknown action */
787 message(LOG | CONSOLE, "Bad inittab entry: %s\n", lineAsRead);
788 }
Erik Andersen7dc16072000-01-04 01:10:25 +0000789 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000790 return;
Erik Andersen9e737252000-01-06 01:16:13 +0000791#endif
Erik Andersen7dc16072000-01-04 01:10:25 +0000792}
793
Erik Andersene132f4b2000-02-09 04:16:43 +0000794
795
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000796extern int init_main(int argc, char **argv)
Eric Andersen0460ff21999-10-25 23:32:44 +0000797{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000798 initAction *a;
799 pid_t wpid;
800 int status;
Eric Andersen0460ff21999-10-25 23:32:44 +0000801
Eric Andersend73dc5b1999-11-10 23:13:02 +0000802#ifndef DEBUG_INIT
Erik Andersena51ecdd2000-02-24 18:09:58 +0000803 /* Expect to be invoked as init with PID=1 or be invoked as linuxrc */
804 if (getpid() != 1
805#ifdef BB_FEATURE_LINUXRC
806 && strstr(argv[0], "linuxrc") == NULL
807#endif
808 )
809 {
810 usage("init\n\nInit is the parent of all processes.\n\n"
811 "This version of init is designed to be run only "
812 "by the kernel.\n");
Erik Andersene49d5ec2000-02-08 19:58:47 +0000813 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000814 /* Set up sig handlers -- be sure to
815 * clear all of these in run() */
816 signal(SIGUSR1, halt_signal);
817 signal(SIGUSR2, reboot_signal);
818 signal(SIGINT, reboot_signal);
819 signal(SIGTERM, reboot_signal);
Erik Andersende552872000-01-23 01:34:05 +0000820#if defined BB_FEATURE_INIT_CHROOT
Erik Andersene49d5ec2000-02-08 19:58:47 +0000821 signal(SIGHUP, check_chroot);
Erik Andersende552872000-01-23 01:34:05 +0000822#endif
Eric Andersen0460ff21999-10-25 23:32:44 +0000823
Erik Andersene49d5ec2000-02-08 19:58:47 +0000824 /* Turn off rebooting via CTL-ALT-DEL -- we get a
825 * SIGINT on CAD so we can shut things down gracefully... */
826 reboot(RB_DISABLE_CAD);
827#endif
Erik Andersen05df2392000-01-13 04:43:48 +0000828
Erik Andersene49d5ec2000-02-08 19:58:47 +0000829 /* Figure out where the default console should be */
830 console_init();
Eric Andersen0460ff21999-10-25 23:32:44 +0000831
Erik Andersene49d5ec2000-02-08 19:58:47 +0000832 /* Close whatever files are open, and reset the console. */
833 close(0);
834 close(1);
835 close(2);
836 set_term(0);
837 setsid();
Eric Andersencc8ed391999-10-05 16:24:54 +0000838
Erik Andersene49d5ec2000-02-08 19:58:47 +0000839 /* Make sure PATH is set to something sane */
840 putenv(_PATH_STDPATH);
Eric Andersencc8ed391999-10-05 16:24:54 +0000841
Erik Andersene49d5ec2000-02-08 19:58:47 +0000842 /* Hello world */
Eric Andersen7f1acfd1999-10-29 23:09:13 +0000843#ifndef DEBUG_INIT
Erik Andersenea6b67d2000-03-07 07:47:10 +0000844 message(
845#if ! defined BB_FEATURE_EXTRA_QUIET
846 CONSOLE|
847#endif
848 LOG,
Erik Andersene49d5ec2000-02-08 19:58:47 +0000849 "init started: BusyBox v%s (%s) multi-call binary\r\n",
850 BB_VER, BB_BT);
Eric Andersen7f1acfd1999-10-29 23:09:13 +0000851#else
Erik Andersenea6b67d2000-03-07 07:47:10 +0000852 message(
853#if ! defined BB_FEATURE_EXTRA_QUIET
854 CONSOLE|
855#endif
856 LOG,
Erik Andersene49d5ec2000-02-08 19:58:47 +0000857 "init(%d) started: BusyBox v%s (%s) multi-call binary\r\n",
858 getpid(), BB_VER, BB_BT);
Eric Andersen7f1acfd1999-10-29 23:09:13 +0000859#endif
Eric Andersencc8ed391999-10-05 16:24:54 +0000860
Eric Andersencc8ed391999-10-05 16:24:54 +0000861
Erik Andersene49d5ec2000-02-08 19:58:47 +0000862 /* Make sure there is enough memory to do something useful. */
863 check_memory();
Erik Andersen9e737252000-01-06 01:16:13 +0000864
Erik Andersene49d5ec2000-02-08 19:58:47 +0000865 /* Check if we are supposed to be in single user mode */
866 if (argc > 1 && (!strcmp(argv[1], "single") ||
867 !strcmp(argv[1], "-s") || !strcmp(argv[1], "1"))) {
868 /* Ask first then start a shell on tty2 */
869 if (secondConsole != NULL)
870 new_initAction(ASKFIRST, SHELL, secondConsole);
871 /* Start a shell on tty1 */
872 new_initAction(RESPAWN, SHELL, console);
873 } else {
874 /* Not in single user mode -- see what inittab says */
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000875
Erik Andersene49d5ec2000-02-08 19:58:47 +0000876 /* NOTE that if BB_FEATURE_USE_INITTAB is NOT defined,
877 * then parse_inittab() simply adds in some default
878 * actions(i.e runs INIT_SCRIPT and then starts a pair
879 * of "askfirst" shells */
880 parse_inittab();
Eric Andersend00c2621999-12-07 08:37:31 +0000881 }
Erik Andersene132f4b2000-02-09 04:16:43 +0000882
883 /* Fix up argv[0] to be certain we claim to be init */
884 strncpy(argv[0], "init", strlen(argv[0])+1);
Erik Andersen07f56042000-02-09 06:05:01 +0000885 if (argc > 1)
886 strncpy(argv[1], "\0", strlen(argv[1])+1);
Eric Andersen219d6f51999-11-02 19:43:01 +0000887
Erik Andersene49d5ec2000-02-08 19:58:47 +0000888 /* Now run everything that needs to be run */
889
890 /* First run the sysinit command */
891 for (a = initActionList; a; a = a->nextPtr) {
892 if (a->action == SYSINIT) {
893 waitfor(a->process, a->console, FALSE);
894 /* Now remove the "sysinit" entry from the list */
895 delete_initAction(a);
Erik Andersen7dc16072000-01-04 01:10:25 +0000896 }
Erik Andersen7dc16072000-01-04 01:10:25 +0000897 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000898 /* Next run anything that wants to block */
899 for (a = initActionList; a; a = a->nextPtr) {
900 if (a->action == WAIT) {
901 waitfor(a->process, a->console, FALSE);
902 /* Now remove the "wait" entry from the list */
903 delete_initAction(a);
Erik Andersen7dc16072000-01-04 01:10:25 +0000904 }
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000905 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000906 /* Next run anything to be run only once */
907 for (a = initActionList; a; a = a->nextPtr) {
908 if (a->action == ONCE) {
909 run(a->process, a->console, FALSE);
910 /* Now remove the "once" entry from the list */
911 delete_initAction(a);
912 }
913 }
914 /* If there is nothing else to do, stop */
915 if (initActionList == NULL) {
916 message(LOG | CONSOLE,
917 "No more tasks for init -- sleeping forever.\n");
918 while (1)
919 sleep(1);
920 }
921
922 /* Now run the looping stuff for the rest of forever */
923 while (1) {
924 for (a = initActionList; a; a = a->nextPtr) {
925 /* Only run stuff with pid==0. If they have
926 * a pid, that means they are still running */
927 if (a->pid == 0) {
928 switch (a->action) {
929 case RESPAWN:
930 /* run the respawn stuff */
931 a->pid = run(a->process, a->console, FALSE);
932 break;
933 case ASKFIRST:
934 /* run the askfirst stuff */
935 a->pid = run(a->process, a->console, TRUE);
936 break;
937 /* silence the compiler's incessant whining */
938 default:
939 break;
940 }
941 }
942 }
943 /* Wait for a child process to exit */
944 wpid = wait(&status);
945 if (wpid > 0) {
946 /* Find out who died and clean up their corpse */
947 for (a = initActionList; a; a = a->nextPtr) {
948 if (a->pid == wpid) {
949 a->pid = 0;
950 message(LOG,
951 "Process '%s' (pid %d) exited. Scheduling it for restart.\n",
952 a->process, wpid);
953 }
954 }
955 }
956 sleep(1);
957 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000958}
Erik Andersen029011b2000-03-04 21:19:32 +0000959
960/*
961Local Variables:
962c-file-style: "linux"
963c-basic-offset: 4
964tab-width: 4
965End:
966*/