blob: 8a48380e93a9d3e46e139d1a5bf0a877b5489e0d [file] [log] [blame]
Eric Andersenc4996011999-10-20 22:08:37 +00001/*
2 * Mini init implementation for busybox
3 *
4 *
5 * Copyright (C) 1995, 1996 by Bruce Perens <bruce@pixar.com>.
6 * Adjusted by so many folks, it's impossible to keep track.
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 *
22 */
23
Eric Andersencc8ed391999-10-05 16:24:54 +000024#include "internal.h"
25#include <stdio.h>
Erik Andersen7dc16072000-01-04 01:10:25 +000026#include <string.h>
Eric Andersencc8ed391999-10-05 16:24:54 +000027#include <stdlib.h>
28#include <stdarg.h>
29#include <unistd.h>
30#include <errno.h>
31#include <signal.h>
32#include <termios.h>
Eric Andersenb186d981999-12-03 09:19:54 +000033#include <paths.h>
Eric Andersencc8ed391999-10-05 16:24:54 +000034#include <sys/types.h>
35#include <sys/fcntl.h>
36#include <sys/wait.h>
37#include <string.h>
38#include <sys/mount.h>
39#include <sys/reboot.h>
40#include <sys/kdaemon.h>
Eric Andersencc8ed391999-10-05 16:24:54 +000041#include <sys/sysmacros.h>
Eric Andersen8a8fbb81999-10-26 00:18:56 +000042#include <linux/serial.h> /* for serial_struct */
43#include <sys/vt.h> /* for vt_stat */
Eric Andersenabc7d591999-10-19 00:27:50 +000044#include <sys/ioctl.h>
Erik Andersen4d1d0111999-12-17 18:44:15 +000045#include <linux/version.h>
Eric Andersen4c781471999-11-26 08:12:56 +000046#ifdef BB_SYSLOGD
47#include <sys/syslog.h>
48#endif
Eric Andersencc8ed391999-10-05 16:24:54 +000049
Eric Andersen0ecb54a1999-12-05 23:24:55 +000050#if ! defined BB_FEATURE_USE_PROCFS
51#error Sorry, I depend on the /proc filesystem right now.
52#endif
53
Erik Andersen4d1d0111999-12-17 18:44:15 +000054#ifndef KERNEL_VERSION
55#define KERNEL_VERSION(a,b,c) (((a) << 16) + ((b) << 8) + (c))
56#endif
57
Eric Andersen0ecb54a1999-12-05 23:24:55 +000058
Erik Andersen7dc16072000-01-04 01:10:25 +000059#define VT_PRIMARY "/dev/tty1" /* Primary virtual console */
60#define VT_SECONDARY "/dev/tty2" /* Virtual console */
61#define VT_LOG "/dev/tty3" /* Virtual console */
62#define SERIAL_CON0 "/dev/ttyS0" /* Primary serial console */
63#define SERIAL_CON1 "/dev/ttyS1" /* Serial console */
Erik Andersen96e2abd2000-01-07 11:40:44 +000064#define SHELL "/bin/sh" /* Default shell */
Erik Andersen7dc16072000-01-04 01:10:25 +000065#define INITTAB "/etc/inittab" /* inittab file location */
Erik Andersen96e2abd2000-01-07 11:40:44 +000066#ifndef INIT_SCRIPT
Erik Andersen7dc16072000-01-04 01:10:25 +000067#define INIT_SCRIPT "/etc/init.d/rcS" /* Default sysinit script. */
Erik Andersen96e2abd2000-01-07 11:40:44 +000068#endif
Erik Andersen9c88cac1999-12-30 09:25:17 +000069
Eric Andersen3ae0c781999-11-04 01:13:21 +000070#define LOG 0x1
71#define CONSOLE 0x2
Erik Andersen7dc16072000-01-04 01:10:25 +000072
73/* Allowed init action types */
74typedef enum {
75 SYSINIT=1,
Erik Andersen7dc16072000-01-04 01:10:25 +000076 RESPAWN,
77 ASKFIRST,
78 WAIT,
79 ONCE
80} initActionEnum;
81
82/* And now a list of the actions we support in the version of init */
83typedef struct initActionType{
84 const char* name;
85 initActionEnum action;
86} initActionType;
87
88static const struct initActionType actions[] = {
89 {"sysinit", SYSINIT},
Erik Andersen7dc16072000-01-04 01:10:25 +000090 {"respawn", RESPAWN},
91 {"askfirst", ASKFIRST},
92 {"wait", WAIT},
93 {"once", ONCE},
94 {0}
95};
96
97/* Set up a linked list of initactions, to be read from inittab */
98typedef struct initActionTag initAction;
99struct initActionTag {
100 pid_t pid;
101 char process[256];
Erik Andersen9e737252000-01-06 01:16:13 +0000102 char console[256];
Erik Andersen7dc16072000-01-04 01:10:25 +0000103 initAction *nextPtr;
104 initActionEnum action;
105};
106initAction* initActionList = NULL;
107
108
Eric Andersenb186d981999-12-03 09:19:54 +0000109static char *console = _PATH_CONSOLE;
Erik Andersenac6e71f2000-01-08 22:04:33 +0000110static char *secondConsole = VT_SECONDARY;
Eric Andersenbe971d61999-11-03 16:52:50 +0000111static char *log = VT_LOG;
Erik Andersenac6e71f2000-01-08 22:04:33 +0000112static int kernelVersion = 0;
113static char *termType = NULL;
Eric Andersen0460ff21999-10-25 23:32:44 +0000114
115
116/* try to open up the specified device */
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000117int device_open(char *device, int mode)
Eric Andersencc8ed391999-10-05 16:24:54 +0000118{
Eric Andersen0460ff21999-10-25 23:32:44 +0000119 int m, f, fd = -1;
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000120
Eric Andersen7f1acfd1999-10-29 23:09:13 +0000121 m = mode | O_NONBLOCK;
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000122
Eric Andersen0460ff21999-10-25 23:32:44 +0000123 /* Retry up to 5 times */
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000124 for (f = 0; f < 5; f++)
125 if ((fd = open(device, m)) >= 0)
126 break;
127 if (fd < 0)
128 return fd;
Eric Andersen3ae0c781999-11-04 01:13:21 +0000129 /* Reset original flags. */
Eric Andersen0460ff21999-10-25 23:32:44 +0000130 if (m != mode)
131 fcntl(fd, F_SETFL, mode);
132 return fd;
133}
Eric Andersencc8ed391999-10-05 16:24:54 +0000134
Eric Andersen3ae0c781999-11-04 01:13:21 +0000135/* print a message to the specified device:
136 * device may be bitwise-or'd from LOG | CONSOLE */
137void message(int device, char *fmt, ...)
Eric Andersen0460ff21999-10-25 23:32:44 +0000138{
Eric Andersen0460ff21999-10-25 23:32:44 +0000139 va_list arguments;
Erik Andersen7dc16072000-01-04 01:10:25 +0000140 int fd;
141
Eric Andersen4c781471999-11-26 08:12:56 +0000142#ifdef BB_SYSLOGD
143
144 /* Log the message to syslogd */
145 if (device & LOG ) {
146 char msg[1024];
147 va_start(arguments, fmt);
148 vsnprintf(msg, sizeof(msg), fmt, arguments);
149 va_end(arguments);
150 syslog(LOG_DAEMON|LOG_NOTICE, msg);
151 }
152
153#else
154 static int log_fd=-1;
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000155
Eric Andersen3ae0c781999-11-04 01:13:21 +0000156 /* Take full control of the log tty, and never close it.
157 * It's mine, all mine! Muhahahaha! */
Eric Andersen07e52971999-11-07 07:38:08 +0000158 if (log_fd < 0) {
159 if (log == NULL) {
160 /* don't even try to log, because there is no such console */
161 log_fd = -2;
162 /* log to main console instead */
163 device = CONSOLE;
164 }
165 else if ((log_fd = device_open(log, O_RDWR|O_NDELAY)) < 0) {
Eric Andersen3ae0c781999-11-04 01:13:21 +0000166 log_fd=-1;
167 fprintf(stderr, "Bummer, can't write to log on %s!\r\n", log);
168 fflush(stderr);
169 return;
170 }
171 }
Eric Andersen07e52971999-11-07 07:38:08 +0000172 if ( (device & LOG) && (log_fd >= 0) ) {
Eric Andersen0460ff21999-10-25 23:32:44 +0000173 va_start(arguments, fmt);
Eric Andersen3ae0c781999-11-04 01:13:21 +0000174 vdprintf(log_fd, fmt, arguments);
Eric Andersena7456061999-10-27 02:31:32 +0000175 va_end(arguments);
176 }
Eric Andersen4c781471999-11-26 08:12:56 +0000177#endif
178
Eric Andersen3ae0c781999-11-04 01:13:21 +0000179 if (device & CONSOLE) {
Eric Andersended62591999-11-18 00:19:26 +0000180 /* Always send console messages to /dev/console so people will see them. */
Eric Andersenb186d981999-12-03 09:19:54 +0000181 if ((fd = device_open(_PATH_CONSOLE, O_WRONLY|O_NOCTTY|O_NDELAY)) >= 0) {
Eric Andersen3ae0c781999-11-04 01:13:21 +0000182 va_start(arguments, fmt);
183 vdprintf(fd, fmt, arguments);
184 va_end(arguments);
185 close(fd);
186 } else {
187 fprintf(stderr, "Bummer, can't print: ");
188 va_start(arguments, fmt);
189 vfprintf(stderr, fmt, arguments);
190 fflush(stderr);
191 va_end(arguments);
192 }
193 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000194}
195
Eric Andersen3ae0c781999-11-04 01:13:21 +0000196
Eric Andersen0460ff21999-10-25 23:32:44 +0000197/* Set terminal settings to reasonable defaults */
Eric Andersenc7c41d31999-10-28 00:24:35 +0000198void set_term( int fd)
Eric Andersencc8ed391999-10-05 16:24:54 +0000199{
Eric Andersen0460ff21999-10-25 23:32:44 +0000200 struct termios tty;
Eric Andersen08b10341999-11-19 02:38:58 +0000201 static const char control_characters[] = {
202 '\003', '\034', '\177', '\025', '\004', '\0',
203 '\1', '\0', '\021', '\023', '\032', '\0', '\022',
204 '\017', '\027', '\026', '\0'
205 };
Eric Andersencc8ed391999-10-05 16:24:54 +0000206
Eric Andersenc7c41d31999-10-28 00:24:35 +0000207 tcgetattr(fd, &tty);
Eric Andersencc8ed391999-10-05 16:24:54 +0000208
Eric Andersen08b10341999-11-19 02:38:58 +0000209 /* set control chars */
210 memcpy(tty.c_cc, control_characters, sizeof(control_characters));
Eric Andersencc8ed391999-10-05 16:24:54 +0000211
Eric Andersen219d6f51999-11-02 19:43:01 +0000212 /* use line dicipline 0 */
Eric Andersen7f1acfd1999-10-29 23:09:13 +0000213 tty.c_line = 0;
214
Eric Andersen08b10341999-11-19 02:38:58 +0000215 /* Make it be sane */
216 //tty.c_cflag &= CBAUD|CBAUDEX|CSIZE|CSTOPB|PARENB|PARODD;
217 //tty.c_cflag |= HUPCL|CLOCAL;
218
219 /* input modes */
220 tty.c_iflag = ICRNL|IXON|IXOFF;
221
Eric Andersen7f1acfd1999-10-29 23:09:13 +0000222 /* output modes */
Eric Andersenc7c41d31999-10-28 00:24:35 +0000223 tty.c_oflag = OPOST|ONLCR;
Eric Andersen7f1acfd1999-10-29 23:09:13 +0000224
225 /* local modes */
Eric Andersen08b10341999-11-19 02:38:58 +0000226 tty.c_lflag = ISIG|ICANON|ECHO|ECHOE|ECHOK|ECHOCTL|ECHOKE|IEXTEN;
Eric Andersencc8ed391999-10-05 16:24:54 +0000227
Eric Andersenc7c41d31999-10-28 00:24:35 +0000228 tcsetattr(fd, TCSANOW, &tty);
Eric Andersencc8ed391999-10-05 16:24:54 +0000229}
230
Eric Andersen219d6f51999-11-02 19:43:01 +0000231/* How much memory does this machine have? */
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000232static int mem_total()
Eric Andersencc8ed391999-10-05 16:24:54 +0000233{
Eric Andersenabc7d591999-10-19 00:27:50 +0000234 char s[80];
Eric Andersen219d6f51999-11-02 19:43:01 +0000235 char *p = "/proc/meminfo";
Eric Andersenabc7d591999-10-19 00:27:50 +0000236 FILE *f;
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000237 const char pattern[] = "MemTotal:";
Eric Andersencc8ed391999-10-05 16:24:54 +0000238
Eric Andersen219d6f51999-11-02 19:43:01 +0000239 if ((f = fopen(p, "r")) < 0) {
Eric Andersen3ae0c781999-11-04 01:13:21 +0000240 message(LOG, "Error opening %s: %s\n", p, strerror( errno));
Eric Andersen219d6f51999-11-02 19:43:01 +0000241 return -1;
242 }
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000243 while (NULL != fgets(s, 79, f)) {
244 p = strstr(s, pattern);
Eric Andersenabc7d591999-10-19 00:27:50 +0000245 if (NULL != p) {
246 fclose(f);
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000247 return (atoi(p + strlen(pattern)));
Eric Andersenabc7d591999-10-19 00:27:50 +0000248 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000249 }
Eric Andersenabc7d591999-10-19 00:27:50 +0000250 return -1;
Eric Andersencc8ed391999-10-05 16:24:54 +0000251}
252
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000253static void console_init()
Eric Andersen0460ff21999-10-25 23:32:44 +0000254{
255 int fd;
256 int tried_devcons = 0;
Eric Andersen219d6f51999-11-02 19:43:01 +0000257 int tried_vtprimary = 0;
Eric Andersen07e52971999-11-07 07:38:08 +0000258 struct serial_struct sr;
Eric Andersen0460ff21999-10-25 23:32:44 +0000259 char *s;
260
Eric Andersen219d6f51999-11-02 19:43:01 +0000261 if ((s = getenv("CONSOLE")) != NULL) {
Erik Andersenac6e71f2000-01-08 22:04:33 +0000262 termType = s;
263 } else {
264 termType = "TERM=vt100";
265 }
266
267 if ((s = getenv("CONSOLE")) != NULL) {
Eric Andersen0460ff21999-10-25 23:32:44 +0000268 console = s;
Eric Andersen07e52971999-11-07 07:38:08 +0000269 }
Eric Andersenfbb39c81999-11-08 17:00:52 +0000270#if #cpu(sparc)
Eric Andersen07e52971999-11-07 07:38:08 +0000271 /* sparc kernel supports console=tty[ab] parameter which is also
272 * passed to init, so catch it here */
273 else if ((s = getenv("console")) != NULL) {
274 /* remap tty[ab] to /dev/ttyS[01] */
275 if (strcmp( s, "ttya" )==0)
276 console = SERIAL_CON0;
277 else if (strcmp( s, "ttyb" )==0)
278 console = SERIAL_CON1;
279 }
Eric Andersen219d6f51999-11-02 19:43:01 +0000280#endif
Eric Andersen07e52971999-11-07 07:38:08 +0000281 else {
282 struct vt_stat vt;
283 static char the_console[13];
284
285 console = the_console;
286 /* 2.2 kernels: identify the real console backend and try to use it */
Eric Andersen08b10341999-11-19 02:38:58 +0000287 if (ioctl(0, TIOCGSERIAL, &sr) == 0) {
Eric Andersen07e52971999-11-07 07:38:08 +0000288 /* this is a serial console */
289 snprintf( the_console, sizeof the_console, "/dev/ttyS%d", sr.line );
290 }
291 else if (ioctl(0, VT_GETSTATE, &vt) == 0) {
292 /* this is linux virtual tty */
293 snprintf( the_console, sizeof the_console, "/dev/tty%d", vt.v_active );
294 } else {
Eric Andersenb186d981999-12-03 09:19:54 +0000295 console = _PATH_CONSOLE;
Eric Andersen07e52971999-11-07 07:38:08 +0000296 tried_devcons++;
297 }
Eric Andersen0460ff21999-10-25 23:32:44 +0000298 }
Eric Andersena7456061999-10-27 02:31:32 +0000299
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000300 while ((fd = open(console, O_RDONLY | O_NONBLOCK)) < 0) {
Eric Andersen219d6f51999-11-02 19:43:01 +0000301 /* Can't open selected console -- try /dev/console */
Eric Andersen0460ff21999-10-25 23:32:44 +0000302 if (!tried_devcons) {
303 tried_devcons++;
Eric Andersenb186d981999-12-03 09:19:54 +0000304 console = _PATH_CONSOLE;
Eric Andersen0460ff21999-10-25 23:32:44 +0000305 continue;
306 }
Eric Andersenbe971d61999-11-03 16:52:50 +0000307 /* Can't open selected console -- try vt1 */
308 if (!tried_vtprimary) {
309 tried_vtprimary++;
310 console = VT_PRIMARY;
311 continue;
312 }
Eric Andersen0460ff21999-10-25 23:32:44 +0000313 break;
314 }
315 if (fd < 0)
Eric Andersen219d6f51999-11-02 19:43:01 +0000316 /* Perhaps we should panic here? */
Eric Andersen0460ff21999-10-25 23:32:44 +0000317 console = "/dev/null";
Eric Andersen07e52971999-11-07 07:38:08 +0000318 else {
319 /* check for serial console and disable logging to tty3 & running a
320 * shell to tty2 */
321 if (ioctl(0,TIOCGSERIAL,&sr) == 0) {
Erik Andersen812d4662000-01-07 18:30:40 +0000322 message(LOG|CONSOLE, "serial console detected. Disabling virtual terminals.\r\n", console );
Eric Andersen07e52971999-11-07 07:38:08 +0000323 log = NULL;
Erik Andersenac6e71f2000-01-08 22:04:33 +0000324 secondConsole = NULL;
Eric Andersen07e52971999-11-07 07:38:08 +0000325 }
Eric Andersen0460ff21999-10-25 23:32:44 +0000326 close(fd);
Eric Andersen07e52971999-11-07 07:38:08 +0000327 }
Eric Andersen3ae0c781999-11-04 01:13:21 +0000328 message(LOG, "console=%s\n", console );
Eric Andersen0460ff21999-10-25 23:32:44 +0000329}
330
Erik Andersen7dc16072000-01-04 01:10:25 +0000331static pid_t run(char* command,
Eric Andersenc7c41d31999-10-28 00:24:35 +0000332 char *terminal, int get_enter)
Eric Andersen0460ff21999-10-25 23:32:44 +0000333{
Erik Andersen7dc16072000-01-04 01:10:25 +0000334 int i;
Eric Andersenc7c41d31999-10-28 00:24:35 +0000335 pid_t pid;
Erik Andersen7dc16072000-01-04 01:10:25 +0000336 char* tmpCmd;
337 char* cmd[255];
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000338 static const char press_enter[] =
Eric Andersen0460ff21999-10-25 23:32:44 +0000339 "\nPlease press Enter to activate this console. ";
Erik Andersenac6e71f2000-01-08 22:04:33 +0000340 static char * environment[] = {
341 "HOME=/",
342 "PATH=/usr/bin:/bin:/usr/sbin:/sbin",
343 "SHELL=/bin/sh",
344 0,
345 "USER=root",
346 0
347 };
348 environment[3]=termType;
349
Eric Andersen0460ff21999-10-25 23:32:44 +0000350
Eric Andersen0460ff21999-10-25 23:32:44 +0000351 if ((pid = fork()) == 0) {
Erik Andersen0881de72000-01-05 09:34:26 +0000352 pid_t shell_pgid = getpid ();
353
Eric Andersen0460ff21999-10-25 23:32:44 +0000354 /* Clean up */
355 close(0);
356 close(1);
357 close(2);
358 setsid();
359
Erik Andersenac6e71f2000-01-08 22:04:33 +0000360 if (device_open(terminal, O_RDWR) < 0) {
Erik Andersen0881de72000-01-05 09:34:26 +0000361 message(LOG|CONSOLE, "Bummer, can't open %s\r\n", terminal);
362 exit(1);
363 }
Erik Andersenac6e71f2000-01-08 22:04:33 +0000364 dup(0);
365 dup(0);
366 tcsetpgrp (0, getpgrp());
367 set_term(0);
Erik Andersen0881de72000-01-05 09:34:26 +0000368
Eric Andersen2f6c04f1999-11-01 23:59:44 +0000369 /* Reset signal handlers set for parent process */
370 signal(SIGUSR1, SIG_DFL);
371 signal(SIGUSR2, SIG_DFL);
372 signal(SIGINT, SIG_DFL);
373 signal(SIGTERM, SIG_DFL);
374
Eric Andersen0460ff21999-10-25 23:32:44 +0000375
Eric Andersen2f6c04f1999-11-01 23:59:44 +0000376 if (get_enter==TRUE) {
Eric Andersen0460ff21999-10-25 23:32:44 +0000377 /*
378 * Save memory by not exec-ing anything large (like a shell)
379 * before the user wants it. This is critical if swap is not
380 * enabled and the system has low memory. Generally this will
381 * be run on the second virtual console, and the first will
382 * be allowed to start a shell or whatever an init script
383 * specifies.
384 */
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000385 char c;
Eric Andersen3ae0c781999-11-04 01:13:21 +0000386 message(LOG, "Waiting for enter to start '%s' (pid %d, console %s)\r\n",
Erik Andersen0881de72000-01-05 09:34:26 +0000387 command, shell_pgid, terminal );
Erik Andersen7dc16072000-01-04 01:10:25 +0000388 write(fileno(stdout), press_enter, sizeof(press_enter) - 1);
389 read(fileno(stdin), &c, 1);
Eric Andersen0460ff21999-10-25 23:32:44 +0000390 }
391
Erik Andersen7dc16072000-01-04 01:10:25 +0000392 /* Convert command (char*) into cmd (char**, one word per string) */
393 for (tmpCmd=command, i=0; (tmpCmd=strsep(&command, " \t")) != NULL;) {
394 if (*tmpCmd != '\0') {
395 cmd[i] = tmpCmd;
396 tmpCmd++;
397 i++;
398 }
399 }
400 cmd[i] = NULL;
401
Eric Andersen0460ff21999-10-25 23:32:44 +0000402 /* Log the process name and args */
Erik Andersen7dc16072000-01-04 01:10:25 +0000403 message(LOG, "Starting pid %d, console %s: '%s'\r\n",
Erik Andersen0881de72000-01-05 09:34:26 +0000404 shell_pgid, terminal, cmd[0]);
Erik Andersen7dc16072000-01-04 01:10:25 +0000405
Eric Andersenc7c41d31999-10-28 00:24:35 +0000406 /* Now run it. The new program will take over this PID,
Eric Andersena7456061999-10-27 02:31:32 +0000407 * so nothing further in init.c should be run. */
Erik Andersenac6e71f2000-01-08 22:04:33 +0000408 //execvp(cmd[0], cmd);
409 execve(cmd[0], cmd, environment);
Eric Andersen0460ff21999-10-25 23:32:44 +0000410
Erik Andersen7dc16072000-01-04 01:10:25 +0000411 /* We're still here? Some error happened. */
412 message(LOG|CONSOLE, "Bummer, could not run '%s': %s\n", cmd[0],
413 strerror(errno));
Eric Andersen0460ff21999-10-25 23:32:44 +0000414 exit(-1);
415 }
416 return pid;
417}
418
Erik Andersen0e3782f2000-01-07 02:54:55 +0000419static int waitfor(char* command,
420 char *terminal, int get_enter)
421{
422 int status, wpid;
423 int pid = run( command, terminal, get_enter);
424
425 while (1) {
426 wpid = wait(&status);
427 if (wpid > 0 ) {
428 message(LOG, "Process '%s' (pid %d) exited.\n",
429 command, wpid);
430 break;
431 }
432 if (wpid == pid )
433 break;
434 }
435 return wpid;
436}
437
Eric Andersen219d6f51999-11-02 19:43:01 +0000438/* Make sure there is enough memory to do something useful. *
439 * Calls swapon if needed so be sure /proc is mounted. */
440static void check_memory()
441{
Erik Andersen0e3782f2000-01-07 02:54:55 +0000442 struct stat statBuf;
Eric Andersen219d6f51999-11-02 19:43:01 +0000443
444 if (mem_total() > 3500)
445 return;
446
Erik Andersen0e3782f2000-01-07 02:54:55 +0000447 if (stat("/etc/fstab", &statBuf) == 0) {
Eric Andersen219d6f51999-11-02 19:43:01 +0000448 /* Try to turn on swap */
Erik Andersen0e3782f2000-01-07 02:54:55 +0000449 waitfor("/bin/swapon swapon -a", log, FALSE);
Eric Andersen219d6f51999-11-02 19:43:01 +0000450 if (mem_total() < 3500)
451 goto goodnight;
452 } else
453 goto goodnight;
454 return;
455
456goodnight:
Eric Andersen3ae0c781999-11-04 01:13:21 +0000457 message(CONSOLE, "Sorry, your computer does not have enough memory.\r\n");
Eric Andersen219d6f51999-11-02 19:43:01 +0000458 while (1) sleep(1);
459}
460
Erik Andersen7dc16072000-01-04 01:10:25 +0000461#ifndef DEBUG_INIT
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000462static void shutdown_system(void)
Eric Andersencc8ed391999-10-05 16:24:54 +0000463{
Eric Andersen0460ff21999-10-25 23:32:44 +0000464 /* Allow Ctrl-Alt-Del to reboot system. */
465 reboot(RB_ENABLE_CAD);
Eric Andersen08b10341999-11-19 02:38:58 +0000466 message(CONSOLE, "\r\nThe system is going down NOW !!\r\n");
Eric Andersen3ae0c781999-11-04 01:13:21 +0000467 sync();
Erik Andersen7dc16072000-01-04 01:10:25 +0000468
Eric Andersen0460ff21999-10-25 23:32:44 +0000469 /* Send signals to every process _except_ pid 1 */
Eric Andersen3ae0c781999-11-04 01:13:21 +0000470 message(CONSOLE, "Sending SIGHUP to all processes.\r\n");
Eric Andersen0460ff21999-10-25 23:32:44 +0000471 kill(-1, SIGHUP);
472 sleep(2);
473 sync();
Erik Andersen7dc16072000-01-04 01:10:25 +0000474
Eric Andersen3ae0c781999-11-04 01:13:21 +0000475 message(CONSOLE, "Sending SIGKILL to all processes.\r\n");
Eric Andersen0460ff21999-10-25 23:32:44 +0000476 kill(-1, SIGKILL);
477 sleep(1);
Erik Andersen7dc16072000-01-04 01:10:25 +0000478
Eric Andersen08b10341999-11-19 02:38:58 +0000479 message(CONSOLE, "Disabling swap.\r\n");
Erik Andersen0e3782f2000-01-07 02:54:55 +0000480 waitfor( "swapoff -a", console, FALSE);
Eric Andersen08b10341999-11-19 02:38:58 +0000481 message(CONSOLE, "Unmounting filesystems.\r\n");
Erik Andersen0e3782f2000-01-07 02:54:55 +0000482 waitfor("umount -a", console, FALSE);
Eric Andersen0460ff21999-10-25 23:32:44 +0000483 sync();
Erik Andersenac6e71f2000-01-08 22:04:33 +0000484 if (kernelVersion > 0 && kernelVersion <= 2 * 65536 + 2 * 256 + 11) {
Eric Andersencf8c9cf1999-11-05 00:31:46 +0000485 /* bdflush, kupdate not needed for kernels >2.2.11 */
486 bdflush(1, 0);
487 sync();
488 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000489}
490
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000491static void halt_signal(int sig)
Eric Andersencc8ed391999-10-05 16:24:54 +0000492{
Eric Andersenabc7d591999-10-19 00:27:50 +0000493 shutdown_system();
Eric Andersen3ae0c781999-11-04 01:13:21 +0000494 message(CONSOLE,
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000495 "The system is halted. Press CTRL-ALT-DEL or turn off power\r\n");
Eric Andersen3ae0c781999-11-04 01:13:21 +0000496 sync();
Erik Andersen4d1d0111999-12-17 18:44:15 +0000497#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,2,0)
Eric Andersen2cb55071999-12-10 08:25:07 +0000498 if (sig == SIGUSR2)
499 reboot(RB_POWER_OFF);
500 else
Erik Andersen4d1d0111999-12-17 18:44:15 +0000501#endif
Erik Andersen7dc16072000-01-04 01:10:25 +0000502 reboot(RB_HALT_SYSTEM);
Eric Andersenabc7d591999-10-19 00:27:50 +0000503 exit(0);
Eric Andersencc8ed391999-10-05 16:24:54 +0000504}
505
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000506static void reboot_signal(int sig)
Eric Andersencc8ed391999-10-05 16:24:54 +0000507{
Eric Andersenabc7d591999-10-19 00:27:50 +0000508 shutdown_system();
Eric Andersen3ae0c781999-11-04 01:13:21 +0000509 message(CONSOLE, "Please stand by while rebooting the system.\r\n");
510 sync();
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000511 reboot(RB_AUTOBOOT);
Eric Andersenabc7d591999-10-19 00:27:50 +0000512 exit(0);
Eric Andersencc8ed391999-10-05 16:24:54 +0000513}
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000514
Erik Andersen7dc16072000-01-04 01:10:25 +0000515#endif
516
Erik Andersen96e2abd2000-01-07 11:40:44 +0000517void new_initAction (initActionEnum action,
Erik Andersen9e737252000-01-06 01:16:13 +0000518 char* process, char* cons)
Erik Andersen7dc16072000-01-04 01:10:25 +0000519{
520 initAction* newAction;
Erik Andersen9e737252000-01-06 01:16:13 +0000521
522 /* If BusyBox detects that a serial console is in use,
523 * then entries containing non-empty id fields will _not_ be run.
524 */
Erik Andersenac6e71f2000-01-08 22:04:33 +0000525 if (secondConsole == NULL && *cons != '\0') {
Erik Andersen9e737252000-01-06 01:16:13 +0000526 return;
Erik Andersen0e3782f2000-01-07 02:54:55 +0000527 }
Erik Andersen9e737252000-01-06 01:16:13 +0000528
Erik Andersen7dc16072000-01-04 01:10:25 +0000529 newAction = calloc ((size_t)(1), sizeof(initAction));
530 if (!newAction) {
Erik Andersen0e3782f2000-01-07 02:54:55 +0000531 message(LOG|CONSOLE,"Memory allocation failure\n");
Erik Andersen7dc16072000-01-04 01:10:25 +0000532 while (1) sleep(1);
533 }
534 newAction->nextPtr = initActionList;
535 initActionList = newAction;
536 strncpy( newAction->process, process, 255);
Erik Andersen96e2abd2000-01-07 11:40:44 +0000537 newAction->action = action;
Erik Andersen0e3782f2000-01-07 02:54:55 +0000538 if (*cons != '\0') {
Erik Andersen9e737252000-01-06 01:16:13 +0000539 strncpy(newAction->console, cons, 255);
Erik Andersen0e3782f2000-01-07 02:54:55 +0000540 } else
Erik Andersen9e737252000-01-06 01:16:13 +0000541 strncpy(newAction->console, console, 255);
Erik Andersen7dc16072000-01-04 01:10:25 +0000542 newAction->pid = 0;
Erik Andersen812d4662000-01-07 18:30:40 +0000543// message(LOG|CONSOLE, "process='%s' action='%d' console='%s'\n",
544// newAction->process, newAction->action, newAction->console);
Erik Andersen7dc16072000-01-04 01:10:25 +0000545}
546
547void delete_initAction (initAction *action)
548{
549 initAction *a, *b=NULL;
550 for( a=initActionList ; a; b=a, a=a->nextPtr) {
551 if (a == action && b != NULL) {
552 b->nextPtr=a->nextPtr;
553 free( a);
554 break;
555 }
556 }
557}
558
Erik Andersen9e737252000-01-06 01:16:13 +0000559/* NOTE that if BB_FEATURE_USE_INITTAB is NOT defined,
560 * then parse_inittab() simply adds in some default
561 * actions(i.e runs INIT_SCRIPT and then starts a pair
Erik Andersen812d4662000-01-07 18:30:40 +0000562 * of "askfirst" shells). If BB_FEATURE_USE_INITTAB
563 * _is_ defined, but /etc/inittab is missing, this
564 * results in the same set of default behaviors.
Erik Andersen9e737252000-01-06 01:16:13 +0000565 * */
Erik Andersen7dc16072000-01-04 01:10:25 +0000566void parse_inittab(void)
567{
Erik Andersen9e737252000-01-06 01:16:13 +0000568#ifdef BB_FEATURE_USE_INITTAB
Erik Andersen7dc16072000-01-04 01:10:25 +0000569 FILE* file;
Erik Andersen0e3782f2000-01-07 02:54:55 +0000570 char buf[256], lineAsRead[256], tmpConsole[256];
Erik Andersen9e737252000-01-06 01:16:13 +0000571 char *p, *q, *r, *s;
Erik Andersen7dc16072000-01-04 01:10:25 +0000572 const struct initActionType *a = actions;
573 int foundIt;
574
575
576 file = fopen(INITTAB, "r");
577 if (file == NULL) {
578 /* No inittab file -- set up some default behavior */
Erik Andersen9e737252000-01-06 01:16:13 +0000579#endif
Erik Andersen7dc16072000-01-04 01:10:25 +0000580 /* Askfirst shell on tty1 */
Erik Andersen96e2abd2000-01-07 11:40:44 +0000581 new_initAction( ASKFIRST, SHELL, console );
Erik Andersen7dc16072000-01-04 01:10:25 +0000582 /* Askfirst shell on tty2 */
Erik Andersenac6e71f2000-01-08 22:04:33 +0000583 if (secondConsole != NULL)
584 new_initAction( ASKFIRST, SHELL, secondConsole );
Erik Andersen7dc16072000-01-04 01:10:25 +0000585 /* sysinit */
Erik Andersen96e2abd2000-01-07 11:40:44 +0000586 new_initAction( SYSINIT, INIT_SCRIPT, console );
Erik Andersen7dc16072000-01-04 01:10:25 +0000587
588 return;
Erik Andersen9e737252000-01-06 01:16:13 +0000589#ifdef BB_FEATURE_USE_INITTAB
Erik Andersen7dc16072000-01-04 01:10:25 +0000590 }
591
592 while ( fgets(buf, 255, file) != NULL) {
593 foundIt=FALSE;
594 for(p = buf; *p == ' ' || *p == '\t'; p++);
595 if (*p == '#' || *p == '\n') continue;
596
597 /* Trim the trailing \n */
598 q = strrchr( p, '\n');
599 if (q != NULL)
600 *q='\0';
601
Erik Andersen9e737252000-01-06 01:16:13 +0000602 /* Keep a copy around for posterity's sake (and error msgs) */
Erik Andersen0e3782f2000-01-07 02:54:55 +0000603 strcpy(lineAsRead, buf);
Erik Andersen9e737252000-01-06 01:16:13 +0000604
605 /* Grab the ID field */
606 s=p;
Erik Andersen7dc16072000-01-04 01:10:25 +0000607 p = strchr( p, ':');
Erik Andersen9e737252000-01-06 01:16:13 +0000608 if ( p != NULL || *(p+1) != '\0' ) {
609 *p='\0';
610 ++p;
611 }
Erik Andersen7dc16072000-01-04 01:10:25 +0000612
613 /* Now peal off the process field from the end
614 * of the string */
615 q = strrchr( p, ':');
616 if ( q == NULL || *(q+1) == '\0' ) {
Erik Andersen0e3782f2000-01-07 02:54:55 +0000617 message(LOG|CONSOLE,"Bad inittab entry: %s\n", lineAsRead);
Erik Andersen7dc16072000-01-04 01:10:25 +0000618 continue;
619 } else {
620 *q='\0';
621 ++q;
622 }
623
624 /* Now peal off the action field */
625 r = strrchr( p, ':');
626 if ( r == NULL || *(r+1) == '\0') {
Erik Andersen0e3782f2000-01-07 02:54:55 +0000627 message(LOG|CONSOLE,"Bad inittab entry: %s\n", lineAsRead);
Erik Andersen7dc16072000-01-04 01:10:25 +0000628 continue;
629 } else {
630 ++r;
631 }
632
633 /* Ok, now process it */
634 a = actions;
635 while (a->name != 0) {
636 if (strcmp(a->name, r) == 0) {
Erik Andersen0e3782f2000-01-07 02:54:55 +0000637 if (*s != '\0') {
638 struct stat statBuf;
639 strcpy(tmpConsole, "/dev/");
640 strncat(tmpConsole, s, 200);
641 if (stat(tmpConsole, &statBuf) != 0) {
642 message(LOG|CONSOLE, "device '%s' does not exist. Did you read the directions?\n", tmpConsole);
643 break;
644 }
645 s = tmpConsole;
646 }
Erik Andersen96e2abd2000-01-07 11:40:44 +0000647 new_initAction( a->action, q, s);
Erik Andersen7dc16072000-01-04 01:10:25 +0000648 foundIt=TRUE;
649 }
650 a++;
651 }
652 if (foundIt==TRUE)
653 continue;
654 else {
655 /* Choke on an unknown action */
Erik Andersen0e3782f2000-01-07 02:54:55 +0000656 message(LOG|CONSOLE, "Bad inittab entry: %s\n", lineAsRead);
Erik Andersen7dc16072000-01-04 01:10:25 +0000657 }
658 }
659 return;
Erik Andersen9e737252000-01-06 01:16:13 +0000660#endif
Erik Andersen7dc16072000-01-04 01:10:25 +0000661}
662
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000663extern int init_main(int argc, char **argv)
Eric Andersen0460ff21999-10-25 23:32:44 +0000664{
Erik Andersen7dc16072000-01-04 01:10:25 +0000665 initAction *a;
666 pid_t wpid;
667 int status;
Eric Andersen0460ff21999-10-25 23:32:44 +0000668
Eric Andersend73dc5b1999-11-10 23:13:02 +0000669#ifndef DEBUG_INIT
Eric Andersen07274581999-11-21 21:50:07 +0000670 /* Expect to be PID 1 iff we are run as init (not linuxrc) */
671 if (getpid() != 1 && strstr(argv[0], "init")!=NULL ) {
Eric Andersend73dc5b1999-11-10 23:13:02 +0000672 usage( "init\n\nInit is the parent of all processes.\n\n"
673 "This version of init is designed to be run only by the kernel\n");
674 }
Eric Andersend73dc5b1999-11-10 23:13:02 +0000675
Erik Andersen0881de72000-01-05 09:34:26 +0000676 /* from the controlling terminal */
677 setsid();
678
Erik Andersen7dc16072000-01-04 01:10:25 +0000679 /* Set up sig handlers -- be sure to clear all of these in run() */
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000680 signal(SIGUSR1, halt_signal);
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000681 signal(SIGUSR2, reboot_signal);
Erik Andersen0881de72000-01-05 09:34:26 +0000682 signal(SIGINT, reboot_signal);
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000683 signal(SIGTERM, reboot_signal);
Eric Andersen0460ff21999-10-25 23:32:44 +0000684
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000685 /* Turn off rebooting via CTL-ALT-DEL -- we get a
686 * SIGINT on CAD so we can shut things down gracefully... */
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000687 reboot(RB_DISABLE_CAD);
Eric Andersen7f1acfd1999-10-29 23:09:13 +0000688#endif
Erik Andersen7dc16072000-01-04 01:10:25 +0000689
Eric Andersenc7c41d31999-10-28 00:24:35 +0000690 /* Figure out where the default console should be */
691 console_init();
Eric Andersen0460ff21999-10-25 23:32:44 +0000692
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000693 /* Close whatever files are open, and reset the console. */
694 close(0);
695 close(1);
696 close(2);
Eric Andersen7f1acfd1999-10-29 23:09:13 +0000697 set_term(0);
Eric Andersencc8ed391999-10-05 16:24:54 +0000698
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000699 /* Make sure PATH is set to something sane */
Eric Andersenb186d981999-12-03 09:19:54 +0000700 putenv(_PATH_STDPATH);
Eric Andersencc8ed391999-10-05 16:24:54 +0000701
Eric Andersen219d6f51999-11-02 19:43:01 +0000702
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000703 /* Hello world */
Eric Andersen7f1acfd1999-10-29 23:09:13 +0000704#ifndef DEBUG_INIT
Erik Andersen0e3782f2000-01-07 02:54:55 +0000705 message(LOG|CONSOLE,
Erik Andersen7dc16072000-01-04 01:10:25 +0000706 "init started: BusyBox v%s (%s) multi-call binary\r\n",
707 BB_VER, BB_BT);
Eric Andersen7f1acfd1999-10-29 23:09:13 +0000708#else
Erik Andersen0e3782f2000-01-07 02:54:55 +0000709 message(LOG|CONSOLE,
Erik Andersen7dc16072000-01-04 01:10:25 +0000710 "init(%d) started: BusyBox v%s (%s) multi-call binary\r\n",
711 getpid(), BB_VER, BB_BT);
Eric Andersen7f1acfd1999-10-29 23:09:13 +0000712#endif
Eric Andersencc8ed391999-10-05 16:24:54 +0000713
Eric Andersenc7c41d31999-10-28 00:24:35 +0000714
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000715 /* Mount /proc */
Eric Andersene18c75a1999-11-05 04:23:05 +0000716 if (mount ("proc", "/proc", "proc", 0, 0) == 0) {
Erik Andersen0e3782f2000-01-07 02:54:55 +0000717 message(LOG|CONSOLE, "Mounting /proc: done.\n");
Erik Andersenac6e71f2000-01-08 22:04:33 +0000718 kernelVersion = get_kernel_revision();
Eric Andersene18c75a1999-11-05 04:23:05 +0000719 } else
Erik Andersen0e3782f2000-01-07 02:54:55 +0000720 message(LOG|CONSOLE, "Mounting /proc: failed!\n");
Eric Andersencc8ed391999-10-05 16:24:54 +0000721
Eric Andersen219d6f51999-11-02 19:43:01 +0000722 /* Make sure there is enough memory to do something useful. */
723 check_memory();
Eric Andersencc8ed391999-10-05 16:24:54 +0000724
Eric Andersend00c2621999-12-07 08:37:31 +0000725 /* Check if we are supposed to be in single user mode */
726 if ( argc > 1 && (!strcmp(argv[1], "single") ||
Erik Andersen0881de72000-01-05 09:34:26 +0000727 !strcmp(argv[1], "-s") || !strcmp(argv[1], "1")))
728 {
Erik Andersen7dc16072000-01-04 01:10:25 +0000729 /* Ask first then start a shell on tty2 */
Erik Andersenac6e71f2000-01-08 22:04:33 +0000730 if (secondConsole != NULL)
731 new_initAction( ASKFIRST, SHELL, secondConsole);
Erik Andersen7dc16072000-01-04 01:10:25 +0000732 /* Ask first then start a shell on tty1 */
Erik Andersen96e2abd2000-01-07 11:40:44 +0000733 new_initAction( ASKFIRST, SHELL, console);
Erik Andersen7dc16072000-01-04 01:10:25 +0000734 } else {
735 /* Not in single user mode -- see what inittab says */
Erik Andersen9e737252000-01-06 01:16:13 +0000736
737 /* NOTE that if BB_FEATURE_USE_INITTAB is NOT defined,
738 * then parse_inittab() simply adds in some default
739 * actions(i.e runs INIT_SCRIPT and then starts a pair
740 * of "askfirst" shells */
Erik Andersen7dc16072000-01-04 01:10:25 +0000741 parse_inittab();
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000742 }
743
Erik Andersen7dc16072000-01-04 01:10:25 +0000744 /* Now run everything that needs to be run */
Eric Andersen84b00921999-12-11 04:16:51 +0000745
Erik Andersen0881de72000-01-05 09:34:26 +0000746 /* First run the sysinit command */
Erik Andersen7dc16072000-01-04 01:10:25 +0000747 for( a=initActionList ; a; a=a->nextPtr) {
748 if (a->action == SYSINIT) {
Erik Andersen96e2abd2000-01-07 11:40:44 +0000749 waitfor(a->process, a->console, FALSE);
Erik Andersen7dc16072000-01-04 01:10:25 +0000750 /* Now remove the "sysinit" entry from the list */
751 delete_initAction( a);
Eric Andersend00c2621999-12-07 08:37:31 +0000752 }
753 }
Erik Andersen7dc16072000-01-04 01:10:25 +0000754 /* Next run anything that wants to block */
755 for( a=initActionList ; a; a=a->nextPtr) {
756 if (a->action == WAIT) {
Erik Andersen96e2abd2000-01-07 11:40:44 +0000757 waitfor(a->process, a->console, FALSE);
Erik Andersen7dc16072000-01-04 01:10:25 +0000758 /* Now remove the "wait" entry from the list */
759 delete_initAction( a);
760 }
761 }
762 /* Next run anything to be run only once */
763 for( a=initActionList ; a; a=a->nextPtr) {
764 if (a->action == ONCE) {
Erik Andersen96e2abd2000-01-07 11:40:44 +0000765 run(a->process, a->console, FALSE);
Erik Andersen7dc16072000-01-04 01:10:25 +0000766 /* Now remove the "once" entry from the list */
767 delete_initAction( a);
768 }
769 }
Erik Andersen0e3782f2000-01-07 02:54:55 +0000770 /* If there is nothing else to do, stop */
771 if (initActionList == NULL) {
772 message(LOG|CONSOLE, "No more tasks for init -- sleeping forever.\n");
773 while (1) sleep(1);
774 }
Eric Andersen219d6f51999-11-02 19:43:01 +0000775
Erik Andersen0881de72000-01-05 09:34:26 +0000776 /* Now run the looping stuff for the rest of forever */
777 while (1) {
Erik Andersen7dc16072000-01-04 01:10:25 +0000778 for( a=initActionList ; a; a=a->nextPtr) {
779 /* Only run stuff with pid==0. If they have
780 * a pid, that means they are still running */
781 if (a->pid == 0) {
782 switch(a->action) {
783 case RESPAWN:
784 /* run the respawn stuff */
Erik Andersen96e2abd2000-01-07 11:40:44 +0000785 a->pid = run(a->process, a->console, FALSE);
Erik Andersen7dc16072000-01-04 01:10:25 +0000786 break;
787 case ASKFIRST:
788 /* run the askfirst stuff */
Erik Andersen96e2abd2000-01-07 11:40:44 +0000789 a->pid = run(a->process, a->console, TRUE);
Erik Andersen7dc16072000-01-04 01:10:25 +0000790 break;
Erik Andersen0881de72000-01-05 09:34:26 +0000791 /* silence the compiler's incessant whining */
Erik Andersen7dc16072000-01-04 01:10:25 +0000792 default:
793 break;
794 }
795 }
796 }
Erik Andersen0881de72000-01-05 09:34:26 +0000797 /* Wait for a child process to exit */
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000798 wpid = wait(&status);
Eric Andersena7456061999-10-27 02:31:32 +0000799 if (wpid > 0 ) {
Erik Andersen0881de72000-01-05 09:34:26 +0000800 /* Find out who died and clean up their corpse */
Erik Andersen7dc16072000-01-04 01:10:25 +0000801 for( a=initActionList ; a; a=a->nextPtr) {
802 if (a->pid==wpid) {
803 a->pid=0;
Erik Andersen0881de72000-01-05 09:34:26 +0000804 message(LOG, "Process '%s' (pid %d) exited. Scheduling it for restart.\n",
805 a->process, wpid);
Erik Andersen7dc16072000-01-04 01:10:25 +0000806 }
Eric Andersenb6a44b81999-11-13 04:47:09 +0000807 }
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000808 }
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000809 sleep(1);
810 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000811}
Erik Andersen9c88cac1999-12-30 09:25:17 +0000812