blob: 4a19822ae2d79ae32bf71df89429d96221c8e6d9 [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 Andersen4d1d0111999-12-17 18:44:15 +000052#include <linux/version.h>
Eric Andersen4c781471999-11-26 08:12:56 +000053#ifdef BB_SYSLOGD
54#include <sys/syslog.h>
55#endif
Eric Andersencc8ed391999-10-05 16:24:54 +000056
Eric Andersen0ecb54a1999-12-05 23:24:55 +000057#if ! defined BB_FEATURE_USE_PROCFS
58#error Sorry, I depend on the /proc filesystem right now.
59#endif
60
Erik Andersen4d1d0111999-12-17 18:44:15 +000061#ifndef KERNEL_VERSION
62#define KERNEL_VERSION(a,b,c) (((a) << 16) + ((b) << 8) + (c))
63#endif
64
Eric Andersen0ecb54a1999-12-05 23:24:55 +000065
Erik Andersene49d5ec2000-02-08 19:58:47 +000066#define VT_PRIMARY "/dev/tty1" /* Primary virtual console */
67#define VT_SECONDARY "/dev/tty2" /* Virtual console */
68#define VT_LOG "/dev/tty3" /* Virtual console */
69#define SERIAL_CON0 "/dev/ttyS0" /* Primary serial console */
70#define SERIAL_CON1 "/dev/ttyS1" /* Serial console */
71#define SHELL "/bin/sh" /* Default shell */
72#define INITTAB "/etc/inittab" /* inittab file location */
Erik Andersen96e2abd2000-01-07 11:40:44 +000073#ifndef INIT_SCRIPT
Erik Andersene49d5ec2000-02-08 19:58:47 +000074#define INIT_SCRIPT "/etc/init.d/rcS" /* Default sysinit script. */
Erik Andersen96e2abd2000-01-07 11:40:44 +000075#endif
Erik Andersen9c88cac1999-12-30 09:25:17 +000076
Eric Andersen3ae0c781999-11-04 01:13:21 +000077#define LOG 0x1
78#define CONSOLE 0x2
Erik Andersen7dc16072000-01-04 01:10:25 +000079
80/* Allowed init action types */
81typedef enum {
Erik Andersene49d5ec2000-02-08 19:58:47 +000082 SYSINIT = 1,
83 RESPAWN,
84 ASKFIRST,
85 WAIT,
Erik Andersene132f4b2000-02-09 04:16:43 +000086 ONCE,
87 CTRLALTDEL
Erik Andersen7dc16072000-01-04 01:10:25 +000088} initActionEnum;
89
90/* And now a list of the actions we support in the version of init */
Erik Andersene49d5ec2000-02-08 19:58:47 +000091typedef struct initActionType {
92 const char *name;
93 initActionEnum action;
Erik Andersen7dc16072000-01-04 01:10:25 +000094} initActionType;
95
96static const struct initActionType actions[] = {
Erik Andersene49d5ec2000-02-08 19:58:47 +000097 {"sysinit", SYSINIT},
98 {"respawn", RESPAWN},
99 {"askfirst", ASKFIRST},
100 {"wait", WAIT},
101 {"once", ONCE},
Erik Andersene132f4b2000-02-09 04:16:43 +0000102 {"ctrlaltdel", CTRLALTDEL},
Erik Andersene49d5ec2000-02-08 19:58:47 +0000103 {0}
Erik Andersen7dc16072000-01-04 01:10:25 +0000104};
105
106/* Set up a linked list of initactions, to be read from inittab */
107typedef struct initActionTag initAction;
108struct initActionTag {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000109 pid_t pid;
110 char process[256];
111 char console[256];
112 initAction *nextPtr;
113 initActionEnum action;
Erik Andersen7dc16072000-01-04 01:10:25 +0000114};
Erik Andersene49d5ec2000-02-08 19:58:47 +0000115initAction *initActionList = NULL;
Erik Andersen7dc16072000-01-04 01:10:25 +0000116
117
Erik Andersenac6e71f2000-01-08 22:04:33 +0000118static char *secondConsole = VT_SECONDARY;
Eric Andersenbe971d61999-11-03 16:52:50 +0000119static char *log = VT_LOG;
Erik Andersenac6e71f2000-01-08 22:04:33 +0000120static int kernelVersion = 0;
Erik Andersene2729152000-02-18 21:34:17 +0000121static char termType[32] = "TERM=linux";
Erik Andersen31638212000-01-15 22:28:50 +0000122static char console[32] = _PATH_CONSOLE;
Erik Andersene132f4b2000-02-09 04:16:43 +0000123static void delete_initAction(initAction * action);
Eric Andersen0460ff21999-10-25 23:32:44 +0000124
125
Eric Andersen3ae0c781999-11-04 01:13:21 +0000126/* print a message to the specified device:
127 * device may be bitwise-or'd from LOG | CONSOLE */
128void message(int device, char *fmt, ...)
Eric Andersen0460ff21999-10-25 23:32:44 +0000129{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000130 va_list arguments;
131 int fd;
Erik Andersen7dc16072000-01-04 01:10:25 +0000132
Eric Andersen4c781471999-11-26 08:12:56 +0000133#ifdef BB_SYSLOGD
134
Erik Andersene49d5ec2000-02-08 19:58:47 +0000135 /* Log the message to syslogd */
136 if (device & LOG) {
137 char msg[1024];
Eric Andersen4c781471999-11-26 08:12:56 +0000138
Erik Andersene49d5ec2000-02-08 19:58:47 +0000139 va_start(arguments, fmt);
140 vsnprintf(msg, sizeof(msg), fmt, arguments);
141 va_end(arguments);
Erik Andersene132f4b2000-02-09 04:16:43 +0000142 openlog("init", 0, LOG_USER);
143 syslog(LOG_USER|LOG_INFO, msg);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000144 closelog();
Eric Andersen3ae0c781999-11-04 01:13:21 +0000145 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000146#else
147 static int log_fd = -1;
148
149 /* Take full control of the log tty, and never close it.
150 * It's mine, all mine! Muhahahaha! */
151 if (log_fd < 0) {
152 if (log == NULL) {
153 /* don't even try to log, because there is no such console */
154 log_fd = -2;
155 /* log to main console instead */
156 device = CONSOLE;
Erik Andersene132f4b2000-02-09 04:16:43 +0000157 } else if ((log_fd = device_open(log, O_RDWR|O_NDELAY)) < 0) {
158 log_fd = -2;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000159 fprintf(stderr, "Bummer, can't write to log on %s!\r\n", log);
160 fflush(stderr);
Erik Andersene132f4b2000-02-09 04:16:43 +0000161 log = NULL;
162 device = CONSOLE;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000163 }
164 }
165 if ((device & LOG) && (log_fd >= 0)) {
166 va_start(arguments, fmt);
167 vdprintf(log_fd, fmt, arguments);
168 va_end(arguments);
169 }
Eric Andersen4c781471999-11-26 08:12:56 +0000170#endif
171
Erik Andersene49d5ec2000-02-08 19:58:47 +0000172 if (device & CONSOLE) {
173 /* Always send console messages to /dev/console so people will see them. */
174 if (
175 (fd =
176 device_open(_PATH_CONSOLE,
177 O_WRONLY | O_NOCTTY | O_NDELAY)) >= 0) {
178 va_start(arguments, fmt);
179 vdprintf(fd, fmt, arguments);
180 va_end(arguments);
181 close(fd);
182 } else {
183 fprintf(stderr, "Bummer, can't print: ");
184 va_start(arguments, fmt);
185 vfprintf(stderr, fmt, arguments);
186 fflush(stderr);
187 va_end(arguments);
188 }
Eric Andersen3ae0c781999-11-04 01:13:21 +0000189 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000190}
191
Eric Andersen3ae0c781999-11-04 01:13:21 +0000192
Eric Andersen0460ff21999-10-25 23:32:44 +0000193/* Set terminal settings to reasonable defaults */
Erik Andersene49d5ec2000-02-08 19:58:47 +0000194void set_term(int fd)
Eric Andersencc8ed391999-10-05 16:24:54 +0000195{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000196 struct termios tty;
197 static const char control_characters[] = {
198 '\003', '\034', '\177', '\025', '\004', '\0',
199 '\1', '\0', '\021', '\023', '\032', '\0', '\022',
200 '\017', '\027', '\026', '\0'
Eric Andersen08b10341999-11-19 02:38:58 +0000201 };
Eric Andersencc8ed391999-10-05 16:24:54 +0000202
Erik Andersene49d5ec2000-02-08 19:58:47 +0000203 tcgetattr(fd, &tty);
Eric Andersencc8ed391999-10-05 16:24:54 +0000204
Erik Andersene49d5ec2000-02-08 19:58:47 +0000205 /* set control chars */
206 memcpy(tty.c_cc, control_characters, sizeof(control_characters));
Eric Andersencc8ed391999-10-05 16:24:54 +0000207
Erik Andersene49d5ec2000-02-08 19:58:47 +0000208 /* use line dicipline 0 */
209 tty.c_line = 0;
Eric Andersen7f1acfd1999-10-29 23:09:13 +0000210
Erik Andersene49d5ec2000-02-08 19:58:47 +0000211 /* Make it be sane */
212 //tty.c_cflag &= CBAUD|CBAUDEX|CSIZE|CSTOPB|PARENB|PARODD;
213 //tty.c_cflag |= HUPCL|CLOCAL;
Eric Andersen08b10341999-11-19 02:38:58 +0000214
Erik Andersene49d5ec2000-02-08 19:58:47 +0000215 /* input modes */
216 tty.c_iflag = ICRNL | IXON | IXOFF;
Eric Andersen08b10341999-11-19 02:38:58 +0000217
Erik Andersene49d5ec2000-02-08 19:58:47 +0000218 /* output modes */
219 tty.c_oflag = OPOST | ONLCR;
Eric Andersen7f1acfd1999-10-29 23:09:13 +0000220
Erik Andersene49d5ec2000-02-08 19:58:47 +0000221 /* local modes */
222 tty.c_lflag =
223 ISIG | ICANON | ECHO | ECHOE | ECHOK | ECHOCTL | ECHOKE | IEXTEN;
Eric Andersencc8ed391999-10-05 16:24:54 +0000224
Erik Andersene49d5ec2000-02-08 19:58:47 +0000225 tcsetattr(fd, TCSANOW, &tty);
Eric Andersencc8ed391999-10-05 16:24:54 +0000226}
227
Eric Andersen219d6f51999-11-02 19:43:01 +0000228/* How much memory does this machine have? */
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000229static int mem_total()
Eric Andersencc8ed391999-10-05 16:24:54 +0000230{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000231 char s[80];
232 char *p = "/proc/meminfo";
233 FILE *f;
234 const char pattern[] = "MemTotal:";
Eric Andersencc8ed391999-10-05 16:24:54 +0000235
Erik Andersene49d5ec2000-02-08 19:58:47 +0000236 if ((f = fopen(p, "r")) < 0) {
237 message(LOG, "Error opening %s: %s\n", p, strerror(errno));
238 return -1;
Eric Andersenabc7d591999-10-19 00:27:50 +0000239 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000240 while (NULL != fgets(s, 79, f)) {
241 p = strstr(s, pattern);
242 if (NULL != p) {
243 fclose(f);
244 return (atoi(p + strlen(pattern)));
245 }
246 }
247 return -1;
Eric Andersencc8ed391999-10-05 16:24:54 +0000248}
249
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000250static void console_init()
Eric Andersen0460ff21999-10-25 23:32:44 +0000251{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000252 int fd;
253 int tried_devcons = 0;
254 int tried_vtprimary = 0;
255 struct serial_struct sr;
256 char *s;
Eric Andersen0460ff21999-10-25 23:32:44 +0000257
Erik Andersene49d5ec2000-02-08 19:58:47 +0000258 if ((s = getenv("TERM")) != NULL) {
259 snprintf(termType, sizeof(termType) - 1, "TERM=%s", s);
260 }
Erik Andersenac6e71f2000-01-08 22:04:33 +0000261
Erik Andersene49d5ec2000-02-08 19:58:47 +0000262 if ((s = getenv("CONSOLE")) != NULL) {
263 snprintf(console, sizeof(console) - 1, "%s", s);
264 }
Eric Andersenfbb39c81999-11-08 17:00:52 +0000265#if #cpu(sparc)
Erik Andersene49d5ec2000-02-08 19:58:47 +0000266 /* sparc kernel supports console=tty[ab] parameter which is also
267 * passed to init, so catch it here */
268 else if ((s = getenv("console")) != NULL) {
269 /* remap tty[ab] to /dev/ttyS[01] */
270 if (strcmp(s, "ttya") == 0)
271 snprintf(console, sizeof(console) - 1, "%s", SERIAL_CON0);
272 else if (strcmp(s, "ttyb") == 0)
273 snprintf(console, sizeof(console) - 1, "%s", SERIAL_CON1);
274 }
Eric Andersen219d6f51999-11-02 19:43:01 +0000275#endif
Erik Andersene49d5ec2000-02-08 19:58:47 +0000276 else {
277 struct vt_stat vt;
Eric Andersen07e52971999-11-07 07:38:08 +0000278
Erik Andersene49d5ec2000-02-08 19:58:47 +0000279 /* 2.2 kernels: identify the real console backend and try to use it */
280 if (ioctl(0, TIOCGSERIAL, &sr) == 0) {
281 /* this is a serial console */
282 snprintf(console, sizeof(console) - 1, "/dev/ttyS%d", sr.line);
283 } else if (ioctl(0, VT_GETSTATE, &vt) == 0) {
284 /* this is linux virtual tty */
285 snprintf(console, sizeof(console) - 1, "/dev/tty%d",
286 vt.v_active);
287 } else {
288 snprintf(console, sizeof(console) - 1, "%s", _PATH_CONSOLE);
289 tried_devcons++;
290 }
Eric Andersen07e52971999-11-07 07:38:08 +0000291 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000292
293 while ((fd = open(console, O_RDONLY | O_NONBLOCK)) < 0) {
294 /* Can't open selected console -- try /dev/console */
295 if (!tried_devcons) {
296 tried_devcons++;
297 snprintf(console, sizeof(console) - 1, "%s", _PATH_CONSOLE);
298 continue;
299 }
300 /* Can't open selected console -- try vt1 */
301 if (!tried_vtprimary) {
302 tried_vtprimary++;
303 snprintf(console, sizeof(console) - 1, "%s", VT_PRIMARY);
304 continue;
305 }
306 break;
307 }
308 if (fd < 0) {
309 /* Perhaps we should panic here? */
310 snprintf(console, sizeof(console) - 1, "/dev/null");
Eric Andersen07e52971999-11-07 07:38:08 +0000311 } else {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000312 /* check for serial console and disable logging to tty3 & running a
313 * shell to tty2 */
314 if (ioctl(0, TIOCGSERIAL, &sr) == 0) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000315 log = NULL;
316 secondConsole = NULL;
Erik Andersenfa4718e2000-02-21 19:25:12 +0000317 /* Force the TERM setting to vt102 for serial console --
318 * iff TERM is set to linux (the default) */
319 if (strcmp( termType, "TERM=linux" ) == 0)
320 snprintf(termType, sizeof(termType) - 1, "TERM=vt102");
Erik Andersene132f4b2000-02-09 04:16:43 +0000321 message(LOG | CONSOLE,
322 "serial console detected. Disabling virtual terminals.\r\n");
Erik Andersene49d5ec2000-02-08 19:58:47 +0000323 }
324 close(fd);
Eric Andersen07e52971999-11-07 07:38:08 +0000325 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000326 message(LOG, "console=%s\n", console);
Eric Andersen0460ff21999-10-25 23:32:44 +0000327}
328
Erik Andersene49d5ec2000-02-08 19:58:47 +0000329static pid_t run(char *command, char *terminal, int get_enter)
Eric Andersen0460ff21999-10-25 23:32:44 +0000330{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000331 int i, fd;
332 pid_t pid;
333 char *tmpCmd;
334 char *cmd[255];
Erik Andersene132f4b2000-02-09 04:16:43 +0000335 char buf[255];
Erik Andersene49d5ec2000-02-08 19:58:47 +0000336 static const char press_enter[] =
337
338 "\nPlease press Enter to activate this console. ";
339 char *environment[] = {
340 "HOME=/",
341 "PATH=/usr/bin:/bin:/usr/sbin:/sbin",
342 "SHELL=/bin/sh",
343 termType,
344 "USER=root",
345 0
346 };
Erik Andersenac6e71f2000-01-08 22:04:33 +0000347
Eric Andersen0460ff21999-10-25 23:32:44 +0000348
Erik Andersene49d5ec2000-02-08 19:58:47 +0000349 if ((pid = fork()) == 0) {
Erik Andersene132f4b2000-02-09 04:16:43 +0000350#ifdef DEBUG_INIT
Erik Andersene49d5ec2000-02-08 19:58:47 +0000351 pid_t shell_pgid = getpid();
Erik Andersene132f4b2000-02-09 04:16:43 +0000352#endif
Erik Andersen0881de72000-01-05 09:34:26 +0000353
Erik Andersene49d5ec2000-02-08 19:58:47 +0000354 /* Clean up */
355 close(0);
356 close(1);
357 close(2);
358 setsid();
Eric Andersen0460ff21999-10-25 23:32:44 +0000359
Erik Andersene49d5ec2000-02-08 19:58:47 +0000360 /* Reset signal handlers set for parent process */
361 signal(SIGUSR1, SIG_DFL);
362 signal(SIGUSR2, SIG_DFL);
363 signal(SIGINT, SIG_DFL);
364 signal(SIGTERM, SIG_DFL);
365 signal(SIGHUP, SIG_DFL);
Eric Andersen2f6c04f1999-11-01 23:59:44 +0000366
Erik Andersene49d5ec2000-02-08 19:58:47 +0000367 if ((fd = device_open(terminal, O_RDWR)) < 0) {
368 message(LOG | CONSOLE, "Bummer, can't open %s\r\n", terminal);
369 exit(1);
370 }
371 dup2(fd, 0);
372 dup2(fd, 1);
373 dup2(fd, 2);
374 tcsetpgrp(0, getpgrp());
375 set_term(0);
376
377 if (get_enter == TRUE) {
378 /*
379 * Save memory by not exec-ing anything large (like a shell)
380 * before the user wants it. This is critical if swap is not
381 * enabled and the system has low memory. Generally this will
382 * be run on the second virtual console, and the first will
383 * be allowed to start a shell or whatever an init script
384 * specifies.
385 */
386 char c;
387
Erik Andersene132f4b2000-02-09 04:16:43 +0000388#ifdef DEBUG_INIT
Erik Andersene49d5ec2000-02-08 19:58:47 +0000389 message(LOG, "Waiting for enter to start '%s' (pid %d, console %s)\r\n",
390 command, shell_pgid, terminal);
Erik Andersene132f4b2000-02-09 04:16:43 +0000391#endif
Erik Andersene49d5ec2000-02-08 19:58:47 +0000392 write(fileno(stdout), press_enter, sizeof(press_enter) - 1);
393 read(fileno(stdin), &c, 1);
394 }
395
Erik Andersene49d5ec2000-02-08 19:58:47 +0000396#ifdef DEBUG_INIT
Erik Andersene132f4b2000-02-09 04:16:43 +0000397 /* Log the process name and args */
398 message(LOG, "Starting pid %d, console %s: '%s'\r\n",
399 shell_pgid, terminal, command);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000400#endif
Erik Andersene132f4b2000-02-09 04:16:43 +0000401
402 /* See if any special /bin/sh requiring characters are present */
403 if (strpbrk(command, "~`!$^&*()=|\\{}[];\"'<>?") != NULL) {
404 cmd[0] = SHELL;
405 cmd[1] = "-c";
406 strcpy(buf, "exec ");
407 strncat(buf, command, sizeof(buf) - strlen(buf) - 1);
408 cmd[2] = buf;
409 cmd[3] = NULL;
410 } else {
411 /* Convert command (char*) into cmd (char**, one word per string) */
412 for (tmpCmd = command, i = 0;
413 (tmpCmd = strsep(&command, " \t")) != NULL;) {
414 if (*tmpCmd != '\0') {
415 cmd[i] = tmpCmd;
416 tmpCmd++;
417 i++;
418 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000419 }
Erik Andersene132f4b2000-02-09 04:16:43 +0000420 cmd[i] = NULL;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000421 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000422
423 /* Now run it. The new program will take over this PID,
424 * so nothing further in init.c should be run. */
425 execve(cmd[0], cmd, environment);
426
427 /* We're still here? Some error happened. */
428 message(LOG | CONSOLE, "Bummer, could not run '%s': %s\n", cmd[0],
429 strerror(errno));
430 exit(-1);
Erik Andersen05df2392000-01-13 04:43:48 +0000431 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000432 return pid;
Eric Andersen0460ff21999-10-25 23:32:44 +0000433}
434
Erik Andersene49d5ec2000-02-08 19:58:47 +0000435static int waitfor(char *command, char *terminal, int get_enter)
Erik Andersen0e3782f2000-01-07 02:54:55 +0000436{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000437 int status, wpid;
438 int pid = run(command, terminal, get_enter);
Erik Andersen0e3782f2000-01-07 02:54:55 +0000439
Erik Andersene49d5ec2000-02-08 19:58:47 +0000440 while (1) {
441 wpid = wait(&status);
Erik Andersene132f4b2000-02-09 04:16:43 +0000442 if (wpid > 0 && wpid != pid) {
443 continue;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000444 }
445 if (wpid == pid)
446 break;
Erik Andersen0e3782f2000-01-07 02:54:55 +0000447 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000448 return wpid;
Erik Andersen0e3782f2000-01-07 02:54:55 +0000449}
450
Eric Andersen219d6f51999-11-02 19:43:01 +0000451/* Make sure there is enough memory to do something useful. *
Erik Andersene132f4b2000-02-09 04:16:43 +0000452 * Calls "swapon -a" if needed so be sure /etc/fstab is present... */
Eric Andersen219d6f51999-11-02 19:43:01 +0000453static void check_memory()
454{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000455 struct stat statBuf;
Eric Andersen219d6f51999-11-02 19:43:01 +0000456
Erik Andersene49d5ec2000-02-08 19:58:47 +0000457 if (mem_total() > 3500)
458 return;
459
460 if (stat("/etc/fstab", &statBuf) == 0) {
461 /* Try to turn on swap */
Erik Andersenfb1793f2000-02-09 16:37:08 +0000462 system("/sbin/swapon -a");
Erik Andersene49d5ec2000-02-08 19:58:47 +0000463 if (mem_total() < 3500)
464 goto goodnight;
465 } else
466 goto goodnight;
Eric Andersen219d6f51999-11-02 19:43:01 +0000467 return;
468
Erik Andersene49d5ec2000-02-08 19:58:47 +0000469 goodnight:
470 message(CONSOLE,
471 "Sorry, your computer does not have enough memory.\r\n");
472 while (1)
473 sleep(1);
Eric Andersen219d6f51999-11-02 19:43:01 +0000474}
475
Erik Andersene132f4b2000-02-09 04:16:43 +0000476/* Run all commands to be run right before halt/reboot */
477static void run_lastAction(void)
478{
479 initAction *a;
480 for (a = initActionList; a; a = a->nextPtr) {
481 if (a->action == CTRLALTDEL) {
482 waitfor(a->process, a->console, FALSE);
483 delete_initAction(a);
484 }
485 }
486}
487
488
Erik Andersen7dc16072000-01-04 01:10:25 +0000489#ifndef DEBUG_INIT
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000490static void shutdown_system(void)
Eric Andersencc8ed391999-10-05 16:24:54 +0000491{
Erik Andersene132f4b2000-02-09 04:16:43 +0000492
Erik Andersene49d5ec2000-02-08 19:58:47 +0000493 /* first disable our SIGHUP signal */
494 signal(SIGHUP, SIG_DFL);
Erik Andersen7dc16072000-01-04 01:10:25 +0000495
Erik Andersene49d5ec2000-02-08 19:58:47 +0000496 /* Allow Ctrl-Alt-Del to reboot system. */
497 reboot(RB_ENABLE_CAD);
Erik Andersene132f4b2000-02-09 04:16:43 +0000498
499 message(CONSOLE|LOG, "\r\nThe system is going down NOW !!\r\n");
Eric Andersencf8c9cf1999-11-05 00:31:46 +0000500 sync();
Erik Andersene49d5ec2000-02-08 19:58:47 +0000501
502 /* Send signals to every process _except_ pid 1 */
Erik Andersene132f4b2000-02-09 04:16:43 +0000503 message(CONSOLE|LOG, "Sending SIGTERM to all processes.\r\n");
Erik Andersene49d5ec2000-02-08 19:58:47 +0000504 kill(-1, SIGTERM);
Erik Andersene132f4b2000-02-09 04:16:43 +0000505 sleep(1);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000506 sync();
507
Erik Andersene132f4b2000-02-09 04:16:43 +0000508 message(CONSOLE|LOG, "Sending SIGKILL to all processes.\r\n");
Erik Andersene49d5ec2000-02-08 19:58:47 +0000509 kill(-1, SIGKILL);
Erik Andersene132f4b2000-02-09 04:16:43 +0000510 sleep(1);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000511
Erik Andersene132f4b2000-02-09 04:16:43 +0000512 /* run everything to be run at "ctrlaltdel" */
513 run_lastAction();
514
Erik Andersene49d5ec2000-02-08 19:58:47 +0000515 sync();
516 if (kernelVersion > 0 && kernelVersion <= 2 * 65536 + 2 * 256 + 11) {
517 /* bdflush, kupdate not needed for kernels >2.2.11 */
518 bdflush(1, 0);
519 sync();
520 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000521}
522
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000523static void halt_signal(int sig)
Eric Andersencc8ed391999-10-05 16:24:54 +0000524{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000525 shutdown_system();
Erik Andersene132f4b2000-02-09 04:16:43 +0000526 message(CONSOLE|LOG,
Erik Andersene49d5ec2000-02-08 19:58:47 +0000527 "The system is halted. Press %s or turn off power\r\n",
528 (secondConsole == NULL) /* serial console */
529 ? "Reset" : "CTRL-ALT-DEL");
530 sync();
Erik Andersen3fe39dc2000-01-25 18:13:53 +0000531
Erik Andersene49d5ec2000-02-08 19:58:47 +0000532 /* allow time for last message to reach serial console */
Erik Andersene132f4b2000-02-09 04:16:43 +0000533 sleep(2);
Erik Andersen3fe39dc2000-01-25 18:13:53 +0000534
Erik Andersen4d1d0111999-12-17 18:44:15 +0000535#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,2,0)
Erik Andersene49d5ec2000-02-08 19:58:47 +0000536 if (sig == SIGUSR2)
537 reboot(RB_POWER_OFF);
538 else
Erik Andersen4d1d0111999-12-17 18:44:15 +0000539#endif
Erik Andersene49d5ec2000-02-08 19:58:47 +0000540 reboot(RB_HALT_SYSTEM);
541 exit(0);
Eric Andersencc8ed391999-10-05 16:24:54 +0000542}
543
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000544static void reboot_signal(int sig)
Eric Andersencc8ed391999-10-05 16:24:54 +0000545{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000546 shutdown_system();
Erik Andersene132f4b2000-02-09 04:16:43 +0000547 message(CONSOLE|LOG, "Please stand by while rebooting the system.\r\n");
Erik Andersene49d5ec2000-02-08 19:58:47 +0000548 sync();
Erik Andersen3fe39dc2000-01-25 18:13:53 +0000549
Erik Andersene49d5ec2000-02-08 19:58:47 +0000550 /* allow time for last message to reach serial console */
551 sleep(2);
Erik Andersen3fe39dc2000-01-25 18:13:53 +0000552
Erik Andersene49d5ec2000-02-08 19:58:47 +0000553 reboot(RB_AUTOBOOT);
554 exit(0);
Eric Andersencc8ed391999-10-05 16:24:54 +0000555}
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000556
Erik Andersende552872000-01-23 01:34:05 +0000557#if defined BB_FEATURE_INIT_CHROOT
558static void check_chroot(int sig)
559{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000560 char *argv_init[2] = { "init", NULL, };
561 char *envp_init[3] = { "HOME=/", "TERM=linux", NULL, };
562 char rootpath[256], *tc;
563 int fd;
Erik Andersende552872000-01-23 01:34:05 +0000564
Erik Andersene49d5ec2000-02-08 19:58:47 +0000565 if ((fd = open("/proc/sys/kernel/init-chroot", O_RDONLY)) == -1) {
566 message(CONSOLE,
567 "SIGHUP recived, but could not open proc file\r\n");
568 sleep(2);
569 return;
570 }
571 if (read(fd, rootpath, sizeof(rootpath)) == -1) {
572 message(CONSOLE,
573 "SIGHUP recived, but could not read proc file\r\n");
574 sleep(2);
575 return;
576 }
577 close(fd);
578
579 if (rootpath[0] == '\0') {
580 message(CONSOLE,
581 "SIGHUP recived, but new root is not valid: %s\r\n",
582 rootpath);
583 sleep(2);
584 return;
585 }
586
587 tc = strrchr(rootpath, '\n');
588 *tc = '\0';
589
590 /* Ok, making it this far means we commit */
591 message(CONSOLE, "Please stand by, changing root to `%s'.\r\n",
592 rootpath);
593
594 /* kill all other programs first */
595 message(CONSOLE, "Sending SIGTERM to all processes.\r\n");
596 kill(-1, SIGTERM);
Erik Andersende552872000-01-23 01:34:05 +0000597 sleep(2);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000598 sync();
599
600 message(CONSOLE, "Sending SIGKILL to all processes.\r\n");
601 kill(-1, SIGKILL);
Erik Andersende552872000-01-23 01:34:05 +0000602 sleep(2);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000603 sync();
604
605 /* ok, we don't need /proc anymore. we also assume that the signaling
606 * process left the rest of the filesystems alone for us */
607 umount("/proc");
608
609 /* Ok, now we chroot. Hopefully we only have two things mounted, the
610 * new chroot'd mount point, and the old "/" mount. s,
611 * we go ahead and unmount the old "/". This should trigger the kernel
612 * to set things up the Right Way(tm). */
613
614 if (!chroot(rootpath))
615 umount("/dev/root");
616
617 /* If the chroot fails, we are already too far to turn back, so we
618 * continue and hope that executing init below will revive the system */
619
620 /* close all of our descriptors and open new ones */
621 close(0);
622 close(1);
623 close(2);
624 open("/dev/console", O_RDWR, 0);
625 dup(0);
626 dup(0);
627
628 message(CONSOLE, "Executing real init...\r\n");
629 /* execute init in the (hopefully) new root */
630 execve("/sbin/init", argv_init, envp_init);
631
632 message(CONSOLE,
633 "ERROR: Could not exec new init. Press %s to reboot.\r\n",
634 (secondConsole == NULL) /* serial console */
635 ? "Reset" : "CTRL-ALT-DEL");
Erik Andersende552872000-01-23 01:34:05 +0000636 return;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000637}
638#endif /* BB_FEATURE_INIT_CHROOT */
Erik Andersende552872000-01-23 01:34:05 +0000639
Erik Andersene49d5ec2000-02-08 19:58:47 +0000640#endif /* ! DEBUG_INIT */
Erik Andersende552872000-01-23 01:34:05 +0000641
Erik Andersene49d5ec2000-02-08 19:58:47 +0000642void new_initAction(initActionEnum action, char *process, char *cons)
Erik Andersen7dc16072000-01-04 01:10:25 +0000643{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000644 initAction *newAction;
Erik Andersen9e737252000-01-06 01:16:13 +0000645
Erik Andersene49d5ec2000-02-08 19:58:47 +0000646 if (*cons == '\0')
647 cons = console;
Erik Andersen9e737252000-01-06 01:16:13 +0000648
Erik Andersene49d5ec2000-02-08 19:58:47 +0000649 /* If BusyBox detects that a serial console is in use,
650 * then entries not refering to the console or null devices will _not_ be run.
651 * The exception to this rule is the null device.
652 */
653 if (secondConsole == NULL && strcmp(cons, console)
654 && strcmp(cons, "/dev/null"))
655 return;
656
657 newAction = calloc((size_t) (1), sizeof(initAction));
658 if (!newAction) {
659 message(LOG | CONSOLE, "Memory allocation failure\n");
660 while (1)
661 sleep(1);
662 }
663 newAction->nextPtr = initActionList;
664 initActionList = newAction;
665 strncpy(newAction->process, process, 255);
666 newAction->action = action;
667 strncpy(newAction->console, cons, 255);
668 newAction->pid = 0;
Erik Andersen31638212000-01-15 22:28:50 +0000669// message(LOG|CONSOLE, "process='%s' action='%d' console='%s'\n",
Erik Andersene49d5ec2000-02-08 19:58:47 +0000670// newAction->process, newAction->action, newAction->console);
Erik Andersen7dc16072000-01-04 01:10:25 +0000671}
672
Erik Andersene132f4b2000-02-09 04:16:43 +0000673static void delete_initAction(initAction * action)
Erik Andersen7dc16072000-01-04 01:10:25 +0000674{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000675 initAction *a, *b = NULL;
676
677 for (a = initActionList; a; b = a, a = a->nextPtr) {
678 if (a == action) {
679 if (b == NULL) {
680 initActionList = a->nextPtr;
681 } else {
682 b->nextPtr = a->nextPtr;
683 }
684 free(a);
685 break;
686 }
Erik Andersen7dc16072000-01-04 01:10:25 +0000687 }
Erik Andersen7dc16072000-01-04 01:10:25 +0000688}
689
Erik Andersen9e737252000-01-06 01:16:13 +0000690/* NOTE that if BB_FEATURE_USE_INITTAB is NOT defined,
691 * then parse_inittab() simply adds in some default
692 * actions(i.e runs INIT_SCRIPT and then starts a pair
Erik Andersen812d4662000-01-07 18:30:40 +0000693 * of "askfirst" shells). If BB_FEATURE_USE_INITTAB
694 * _is_ defined, but /etc/inittab is missing, this
695 * results in the same set of default behaviors.
Erik Andersen9e737252000-01-06 01:16:13 +0000696 * */
Erik Andersene49d5ec2000-02-08 19:58:47 +0000697void parse_inittab(void)
Erik Andersen7dc16072000-01-04 01:10:25 +0000698{
Erik Andersen9e737252000-01-06 01:16:13 +0000699#ifdef BB_FEATURE_USE_INITTAB
Erik Andersene49d5ec2000-02-08 19:58:47 +0000700 FILE *file;
701 char buf[256], lineAsRead[256], tmpConsole[256];
702 char *p, *q, *r, *s;
703 const struct initActionType *a = actions;
704 int foundIt;
Erik Andersen7dc16072000-01-04 01:10:25 +0000705
706
Erik Andersene49d5ec2000-02-08 19:58:47 +0000707 file = fopen(INITTAB, "r");
708 if (file == NULL) {
709 /* No inittab file -- set up some default behavior */
Erik Andersen9e737252000-01-06 01:16:13 +0000710#endif
Erik Andersene132f4b2000-02-09 04:16:43 +0000711 /* Swapoff on halt/reboot */
Erik Andersenfb1793f2000-02-09 16:37:08 +0000712 new_initAction(CTRLALTDEL, "/sbin/swapoff -a > /dev/null 2>&1", console);
Erik Andersene132f4b2000-02-09 04:16:43 +0000713 /* Umount all filesystems on halt/reboot */
714 new_initAction(CTRLALTDEL, "/bin/umount -a -r > /dev/null 2>&1", console);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000715 /* Askfirst shell on tty1 */
716 new_initAction(ASKFIRST, SHELL, console);
717 /* Askfirst shell on tty2 */
718 if (secondConsole != NULL)
719 new_initAction(ASKFIRST, SHELL, secondConsole);
720 /* sysinit */
721 new_initAction(SYSINIT, INIT_SCRIPT, console);
Erik Andersen7dc16072000-01-04 01:10:25 +0000722
Erik Andersene49d5ec2000-02-08 19:58:47 +0000723 return;
Erik Andersen9e737252000-01-06 01:16:13 +0000724#ifdef BB_FEATURE_USE_INITTAB
Erik Andersen9e737252000-01-06 01:16:13 +0000725 }
Erik Andersen7dc16072000-01-04 01:10:25 +0000726
Erik Andersene49d5ec2000-02-08 19:58:47 +0000727 while (fgets(buf, 255, file) != NULL) {
728 foundIt = FALSE;
729 for (p = buf; *p == ' ' || *p == '\t'; p++);
730 if (*p == '#' || *p == '\n')
731 continue;
Erik Andersen7dc16072000-01-04 01:10:25 +0000732
Erik Andersene49d5ec2000-02-08 19:58:47 +0000733 /* Trim the trailing \n */
734 q = strrchr(p, '\n');
735 if (q != NULL)
736 *q = '\0';
Erik Andersen7dc16072000-01-04 01:10:25 +0000737
Erik Andersene49d5ec2000-02-08 19:58:47 +0000738 /* Keep a copy around for posterity's sake (and error msgs) */
739 strcpy(lineAsRead, buf);
740
741 /* Grab the ID field */
742 s = p;
743 p = strchr(p, ':');
744 if (p != NULL || *(p + 1) != '\0') {
745 *p = '\0';
746 ++p;
Erik Andersen0e3782f2000-01-07 02:54:55 +0000747 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000748
749 /* Now peal off the process field from the end
750 * of the string */
751 q = strrchr(p, ':');
752 if (q == NULL || *(q + 1) == '\0') {
753 message(LOG | CONSOLE, "Bad inittab entry: %s\n", lineAsRead);
754 continue;
755 } else {
756 *q = '\0';
757 ++q;
758 }
759
760 /* Now peal off the action field */
761 r = strrchr(p, ':');
762 if (r == NULL || *(r + 1) == '\0') {
763 message(LOG | CONSOLE, "Bad inittab entry: %s\n", lineAsRead);
764 continue;
765 } else {
766 ++r;
767 }
768
769 /* Ok, now process it */
770 a = actions;
771 while (a->name != 0) {
772 if (strcmp(a->name, r) == 0) {
773 if (*s != '\0') {
774 struct stat statBuf;
775
776 strcpy(tmpConsole, "/dev/");
777 strncat(tmpConsole, s, 200);
778 if (stat(tmpConsole, &statBuf) != 0) {
779 message(LOG | CONSOLE,
780 "device '%s' does not exist. Did you read the directions?\n",
781 tmpConsole);
782 break;
783 }
784 s = tmpConsole;
785 }
786 new_initAction(a->action, q, s);
787 foundIt = TRUE;
788 }
789 a++;
790 }
791 if (foundIt == TRUE)
792 continue;
793 else {
794 /* Choke on an unknown action */
795 message(LOG | CONSOLE, "Bad inittab entry: %s\n", lineAsRead);
796 }
Erik Andersen7dc16072000-01-04 01:10:25 +0000797 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000798 return;
Erik Andersen9e737252000-01-06 01:16:13 +0000799#endif
Erik Andersen7dc16072000-01-04 01:10:25 +0000800}
801
Erik Andersene132f4b2000-02-09 04:16:43 +0000802
803
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000804extern int init_main(int argc, char **argv)
Eric Andersen0460ff21999-10-25 23:32:44 +0000805{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000806 initAction *a;
807 pid_t wpid;
808 int status;
Eric Andersen0460ff21999-10-25 23:32:44 +0000809
Eric Andersend73dc5b1999-11-10 23:13:02 +0000810#ifndef DEBUG_INIT
Erik Andersene49d5ec2000-02-08 19:58:47 +0000811 /* Expect to be PID 1 if we are run as init (not linuxrc) */
812 if (getpid() != 1 && strstr(argv[0], "init") != NULL) {
813 usage("init\n\nInit is the parent of all processes.\n\n"
814 "This version of init is designed to be run only by the kernel\n");
815 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000816 /* Set up sig handlers -- be sure to
817 * clear all of these in run() */
818 signal(SIGUSR1, halt_signal);
819 signal(SIGUSR2, reboot_signal);
820 signal(SIGINT, reboot_signal);
821 signal(SIGTERM, reboot_signal);
Erik Andersende552872000-01-23 01:34:05 +0000822#if defined BB_FEATURE_INIT_CHROOT
Erik Andersene49d5ec2000-02-08 19:58:47 +0000823 signal(SIGHUP, check_chroot);
Erik Andersende552872000-01-23 01:34:05 +0000824#endif
Eric Andersen0460ff21999-10-25 23:32:44 +0000825
Erik Andersene49d5ec2000-02-08 19:58:47 +0000826 /* Turn off rebooting via CTL-ALT-DEL -- we get a
827 * SIGINT on CAD so we can shut things down gracefully... */
828 reboot(RB_DISABLE_CAD);
829#endif
Erik Andersen05df2392000-01-13 04:43:48 +0000830
Erik Andersene49d5ec2000-02-08 19:58:47 +0000831 /* Figure out where the default console should be */
832 console_init();
Eric Andersen0460ff21999-10-25 23:32:44 +0000833
Erik Andersene49d5ec2000-02-08 19:58:47 +0000834 /* Close whatever files are open, and reset the console. */
835 close(0);
836 close(1);
837 close(2);
838 set_term(0);
839 setsid();
Eric Andersencc8ed391999-10-05 16:24:54 +0000840
Erik Andersene49d5ec2000-02-08 19:58:47 +0000841 /* Make sure PATH is set to something sane */
842 putenv(_PATH_STDPATH);
Eric Andersencc8ed391999-10-05 16:24:54 +0000843
Erik Andersene49d5ec2000-02-08 19:58:47 +0000844 /* Hello world */
Eric Andersen7f1acfd1999-10-29 23:09:13 +0000845#ifndef DEBUG_INIT
Erik Andersene2729152000-02-18 21:34:17 +0000846 message(LOG,
Erik Andersene49d5ec2000-02-08 19:58:47 +0000847 "init started: BusyBox v%s (%s) multi-call binary\r\n",
848 BB_VER, BB_BT);
Eric Andersen7f1acfd1999-10-29 23:09:13 +0000849#else
Erik Andersene2729152000-02-18 21:34:17 +0000850 message(LOG,
Erik Andersene49d5ec2000-02-08 19:58:47 +0000851 "init(%d) started: BusyBox v%s (%s) multi-call binary\r\n",
852 getpid(), BB_VER, BB_BT);
Eric Andersen7f1acfd1999-10-29 23:09:13 +0000853#endif
Eric Andersencc8ed391999-10-05 16:24:54 +0000854
Eric Andersencc8ed391999-10-05 16:24:54 +0000855
Erik Andersene49d5ec2000-02-08 19:58:47 +0000856 /* Mount /proc */
857 if (mount("proc", "/proc", "proc", 0, 0) == 0) {
Erik Andersene2729152000-02-18 21:34:17 +0000858 message(LOG, "Mounting /proc: done.\n");
Erik Andersene49d5ec2000-02-08 19:58:47 +0000859 kernelVersion = get_kernel_revision();
860 } else
861 message(LOG | CONSOLE, "Mounting /proc: failed!\n");
Eric Andersencc8ed391999-10-05 16:24:54 +0000862
Erik Andersene49d5ec2000-02-08 19:58:47 +0000863 /* Make sure there is enough memory to do something useful. */
864 check_memory();
Erik Andersen9e737252000-01-06 01:16:13 +0000865
Erik Andersene49d5ec2000-02-08 19:58:47 +0000866 /* Check if we are supposed to be in single user mode */
867 if (argc > 1 && (!strcmp(argv[1], "single") ||
868 !strcmp(argv[1], "-s") || !strcmp(argv[1], "1"))) {
869 /* Ask first then start a shell on tty2 */
870 if (secondConsole != NULL)
871 new_initAction(ASKFIRST, SHELL, secondConsole);
872 /* Start a shell on tty1 */
873 new_initAction(RESPAWN, SHELL, console);
874 } else {
875 /* Not in single user mode -- see what inittab says */
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000876
Erik Andersene49d5ec2000-02-08 19:58:47 +0000877 /* NOTE that if BB_FEATURE_USE_INITTAB is NOT defined,
878 * then parse_inittab() simply adds in some default
879 * actions(i.e runs INIT_SCRIPT and then starts a pair
880 * of "askfirst" shells */
881 parse_inittab();
Eric Andersend00c2621999-12-07 08:37:31 +0000882 }
Erik Andersene132f4b2000-02-09 04:16:43 +0000883
884 /* Fix up argv[0] to be certain we claim to be init */
885 strncpy(argv[0], "init", strlen(argv[0])+1);
Erik Andersen07f56042000-02-09 06:05:01 +0000886 if (argc > 1)
887 strncpy(argv[1], "\0", strlen(argv[1])+1);
Eric Andersen219d6f51999-11-02 19:43:01 +0000888
Erik Andersene49d5ec2000-02-08 19:58:47 +0000889 /* Now run everything that needs to be run */
890
891 /* First run the sysinit command */
892 for (a = initActionList; a; a = a->nextPtr) {
893 if (a->action == SYSINIT) {
894 waitfor(a->process, a->console, FALSE);
895 /* Now remove the "sysinit" entry from the list */
896 delete_initAction(a);
Erik Andersen7dc16072000-01-04 01:10:25 +0000897 }
Erik Andersen7dc16072000-01-04 01:10:25 +0000898 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000899 /* Next run anything that wants to block */
900 for (a = initActionList; a; a = a->nextPtr) {
901 if (a->action == WAIT) {
902 waitfor(a->process, a->console, FALSE);
903 /* Now remove the "wait" entry from the list */
904 delete_initAction(a);
Erik Andersen7dc16072000-01-04 01:10:25 +0000905 }
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000906 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000907 /* Next run anything to be run only once */
908 for (a = initActionList; a; a = a->nextPtr) {
909 if (a->action == ONCE) {
910 run(a->process, a->console, FALSE);
911 /* Now remove the "once" entry from the list */
912 delete_initAction(a);
913 }
914 }
915 /* If there is nothing else to do, stop */
916 if (initActionList == NULL) {
917 message(LOG | CONSOLE,
918 "No more tasks for init -- sleeping forever.\n");
919 while (1)
920 sleep(1);
921 }
922
923 /* Now run the looping stuff for the rest of forever */
924 while (1) {
925 for (a = initActionList; a; a = a->nextPtr) {
926 /* Only run stuff with pid==0. If they have
927 * a pid, that means they are still running */
928 if (a->pid == 0) {
929 switch (a->action) {
930 case RESPAWN:
931 /* run the respawn stuff */
932 a->pid = run(a->process, a->console, FALSE);
933 break;
934 case ASKFIRST:
935 /* run the askfirst stuff */
936 a->pid = run(a->process, a->console, TRUE);
937 break;
938 /* silence the compiler's incessant whining */
939 default:
940 break;
941 }
942 }
943 }
944 /* Wait for a child process to exit */
945 wpid = wait(&status);
946 if (wpid > 0) {
947 /* Find out who died and clean up their corpse */
948 for (a = initActionList; a; a = a->nextPtr) {
949 if (a->pid == wpid) {
950 a->pid = 0;
951 message(LOG,
952 "Process '%s' (pid %d) exited. Scheduling it for restart.\n",
953 a->process, wpid);
954 }
955 }
956 }
957 sleep(1);
958 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000959}