blob: c6052f8cfaf8c5ad905f6dcd39486909bb69697b [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;
194 static const char control_characters[] = {
195 '\003', '\034', '\177', '\025', '\004', '\0',
196 '\1', '\0', '\021', '\023', '\032', '\0', '\022',
197 '\017', '\027', '\026', '\0'
Eric Andersen08b10341999-11-19 02:38:58 +0000198 };
Eric Andersencc8ed391999-10-05 16:24:54 +0000199
Erik Andersene49d5ec2000-02-08 19:58:47 +0000200 tcgetattr(fd, &tty);
Eric Andersencc8ed391999-10-05 16:24:54 +0000201
Erik Andersene49d5ec2000-02-08 19:58:47 +0000202 /* set control chars */
203 memcpy(tty.c_cc, control_characters, sizeof(control_characters));
Eric Andersencc8ed391999-10-05 16:24:54 +0000204
Erik Andersene49d5ec2000-02-08 19:58:47 +0000205 /* use line dicipline 0 */
206 tty.c_line = 0;
Eric Andersen7f1acfd1999-10-29 23:09:13 +0000207
Erik Andersene49d5ec2000-02-08 19:58:47 +0000208 /* Make it be sane */
209 //tty.c_cflag &= CBAUD|CBAUDEX|CSIZE|CSTOPB|PARENB|PARODD;
210 //tty.c_cflag |= HUPCL|CLOCAL;
Eric Andersen08b10341999-11-19 02:38:58 +0000211
Erik Andersene49d5ec2000-02-08 19:58:47 +0000212 /* input modes */
213 tty.c_iflag = ICRNL | IXON | IXOFF;
Eric Andersen08b10341999-11-19 02:38:58 +0000214
Erik Andersene49d5ec2000-02-08 19:58:47 +0000215 /* output modes */
216 tty.c_oflag = OPOST | ONLCR;
Eric Andersen7f1acfd1999-10-29 23:09:13 +0000217
Erik Andersene49d5ec2000-02-08 19:58:47 +0000218 /* local modes */
219 tty.c_lflag =
220 ISIG | ICANON | ECHO | ECHOE | ECHOK | ECHOCTL | ECHOKE | IEXTEN;
Eric Andersencc8ed391999-10-05 16:24:54 +0000221
Erik Andersene49d5ec2000-02-08 19:58:47 +0000222 tcsetattr(fd, TCSANOW, &tty);
Eric Andersencc8ed391999-10-05 16:24:54 +0000223}
224
Eric Andersen219d6f51999-11-02 19:43:01 +0000225/* How much memory does this machine have? */
Erik Andersend07ee462000-02-21 21:26:32 +0000226static int check_free_memory()
Eric Andersencc8ed391999-10-05 16:24:54 +0000227{
Erik Andersend07ee462000-02-21 21:26:32 +0000228 struct sysinfo info;
Eric Andersencc8ed391999-10-05 16:24:54 +0000229
Erik Andersend07ee462000-02-21 21:26:32 +0000230 sysinfo(&info);
231 if (sysinfo(&info) != 0) {
232 message(LOG, "Error checking free memory: %s\n", strerror(errno));
Erik Andersene49d5ec2000-02-08 19:58:47 +0000233 return -1;
Eric Andersenabc7d591999-10-19 00:27:50 +0000234 }
Erik Andersend07ee462000-02-21 21:26:32 +0000235
236 return(info.freeram/1024);
237
Eric Andersencc8ed391999-10-05 16:24:54 +0000238}
239
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000240static void console_init()
Eric Andersen0460ff21999-10-25 23:32:44 +0000241{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000242 int fd;
243 int tried_devcons = 0;
244 int tried_vtprimary = 0;
Erik Andersen029011b2000-03-04 21:19:32 +0000245 struct vt_stat vt;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000246 struct serial_struct sr;
247 char *s;
Eric Andersen0460ff21999-10-25 23:32:44 +0000248
Erik Andersene49d5ec2000-02-08 19:58:47 +0000249 if ((s = getenv("TERM")) != NULL) {
250 snprintf(termType, sizeof(termType) - 1, "TERM=%s", s);
251 }
Erik Andersenac6e71f2000-01-08 22:04:33 +0000252
Erik Andersene49d5ec2000-02-08 19:58:47 +0000253 if ((s = getenv("CONSOLE")) != NULL) {
254 snprintf(console, sizeof(console) - 1, "%s", s);
255 }
Eric Andersenfbb39c81999-11-08 17:00:52 +0000256#if #cpu(sparc)
Erik Andersene49d5ec2000-02-08 19:58:47 +0000257 /* sparc kernel supports console=tty[ab] parameter which is also
258 * passed to init, so catch it here */
259 else if ((s = getenv("console")) != NULL) {
260 /* remap tty[ab] to /dev/ttyS[01] */
261 if (strcmp(s, "ttya") == 0)
262 snprintf(console, sizeof(console) - 1, "%s", SERIAL_CON0);
263 else if (strcmp(s, "ttyb") == 0)
264 snprintf(console, sizeof(console) - 1, "%s", SERIAL_CON1);
265 }
Eric Andersen219d6f51999-11-02 19:43:01 +0000266#endif
Erik Andersene49d5ec2000-02-08 19:58:47 +0000267 else {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000268 /* 2.2 kernels: identify the real console backend and try to use it */
269 if (ioctl(0, TIOCGSERIAL, &sr) == 0) {
270 /* this is a serial console */
271 snprintf(console, sizeof(console) - 1, "/dev/ttyS%d", sr.line);
272 } else if (ioctl(0, VT_GETSTATE, &vt) == 0) {
273 /* this is linux virtual tty */
274 snprintf(console, sizeof(console) - 1, "/dev/tty%d",
275 vt.v_active);
276 } else {
277 snprintf(console, sizeof(console) - 1, "%s", _PATH_CONSOLE);
278 tried_devcons++;
279 }
Eric Andersen07e52971999-11-07 07:38:08 +0000280 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000281
282 while ((fd = open(console, O_RDONLY | O_NONBLOCK)) < 0) {
283 /* Can't open selected console -- try /dev/console */
284 if (!tried_devcons) {
285 tried_devcons++;
286 snprintf(console, sizeof(console) - 1, "%s", _PATH_CONSOLE);
287 continue;
288 }
289 /* Can't open selected console -- try vt1 */
290 if (!tried_vtprimary) {
291 tried_vtprimary++;
292 snprintf(console, sizeof(console) - 1, "%s", VT_PRIMARY);
293 continue;
294 }
295 break;
296 }
297 if (fd < 0) {
298 /* Perhaps we should panic here? */
299 snprintf(console, sizeof(console) - 1, "/dev/null");
Eric Andersen07e52971999-11-07 07:38:08 +0000300 } else {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000301 /* check for serial console and disable logging to tty3 & running a
302 * shell to tty2 */
303 if (ioctl(0, TIOCGSERIAL, &sr) == 0) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000304 log = NULL;
305 secondConsole = NULL;
Erik Andersenfa4718e2000-02-21 19:25:12 +0000306 /* Force the TERM setting to vt102 for serial console --
307 * iff TERM is set to linux (the default) */
308 if (strcmp( termType, "TERM=linux" ) == 0)
309 snprintf(termType, sizeof(termType) - 1, "TERM=vt102");
Erik Andersene132f4b2000-02-09 04:16:43 +0000310 message(LOG | CONSOLE,
311 "serial console detected. Disabling virtual terminals.\r\n");
Erik Andersene49d5ec2000-02-08 19:58:47 +0000312 }
313 close(fd);
Eric Andersen07e52971999-11-07 07:38:08 +0000314 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000315 message(LOG, "console=%s\n", console);
Eric Andersen0460ff21999-10-25 23:32:44 +0000316}
317
Erik Andersene49d5ec2000-02-08 19:58:47 +0000318static pid_t run(char *command, char *terminal, int get_enter)
Eric Andersen0460ff21999-10-25 23:32:44 +0000319{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000320 int i, fd;
321 pid_t pid;
322 char *tmpCmd;
323 char *cmd[255];
Erik Andersene132f4b2000-02-09 04:16:43 +0000324 char buf[255];
Erik Andersene49d5ec2000-02-08 19:58:47 +0000325 static const char press_enter[] =
326
327 "\nPlease press Enter to activate this console. ";
328 char *environment[] = {
329 "HOME=/",
330 "PATH=/usr/bin:/bin:/usr/sbin:/sbin",
331 "SHELL=/bin/sh",
332 termType,
333 "USER=root",
334 0
335 };
Erik Andersenac6e71f2000-01-08 22:04:33 +0000336
Eric Andersen0460ff21999-10-25 23:32:44 +0000337
Erik Andersene49d5ec2000-02-08 19:58:47 +0000338 if ((pid = fork()) == 0) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000339 /* Clean up */
340 close(0);
341 close(1);
342 close(2);
343 setsid();
Eric Andersen0460ff21999-10-25 23:32:44 +0000344
Erik Andersene49d5ec2000-02-08 19:58:47 +0000345 /* Reset signal handlers set for parent process */
346 signal(SIGUSR1, SIG_DFL);
347 signal(SIGUSR2, SIG_DFL);
348 signal(SIGINT, SIG_DFL);
349 signal(SIGTERM, SIG_DFL);
350 signal(SIGHUP, SIG_DFL);
Eric Andersen2f6c04f1999-11-01 23:59:44 +0000351
Erik Andersene49d5ec2000-02-08 19:58:47 +0000352 if ((fd = device_open(terminal, O_RDWR)) < 0) {
353 message(LOG | CONSOLE, "Bummer, can't open %s\r\n", terminal);
354 exit(1);
355 }
356 dup2(fd, 0);
357 dup2(fd, 1);
358 dup2(fd, 2);
359 tcsetpgrp(0, getpgrp());
360 set_term(0);
361
362 if (get_enter == TRUE) {
363 /*
364 * Save memory by not exec-ing anything large (like a shell)
365 * before the user wants it. This is critical if swap is not
366 * enabled and the system has low memory. Generally this will
367 * be run on the second virtual console, and the first will
368 * be allowed to start a shell or whatever an init script
369 * specifies.
370 */
371 char c;
Erik Andersene132f4b2000-02-09 04:16:43 +0000372#ifdef DEBUG_INIT
Erik Andersen2ac2fae2000-03-07 23:32:17 +0000373 pid_t shell_pgid = getpid();
Erik Andersene49d5ec2000-02-08 19:58:47 +0000374 message(LOG, "Waiting for enter to start '%s' (pid %d, console %s)\r\n",
375 command, shell_pgid, terminal);
Erik Andersene132f4b2000-02-09 04:16:43 +0000376#endif
Erik Andersene49d5ec2000-02-08 19:58:47 +0000377 write(fileno(stdout), press_enter, sizeof(press_enter) - 1);
378 read(fileno(stdin), &c, 1);
379 }
380
Erik Andersene49d5ec2000-02-08 19:58:47 +0000381#ifdef DEBUG_INIT
Erik Andersene132f4b2000-02-09 04:16:43 +0000382 /* Log the process name and args */
383 message(LOG, "Starting pid %d, console %s: '%s'\r\n",
384 shell_pgid, terminal, command);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000385#endif
Erik Andersene132f4b2000-02-09 04:16:43 +0000386
387 /* See if any special /bin/sh requiring characters are present */
388 if (strpbrk(command, "~`!$^&*()=|\\{}[];\"'<>?") != NULL) {
389 cmd[0] = SHELL;
390 cmd[1] = "-c";
391 strcpy(buf, "exec ");
392 strncat(buf, command, sizeof(buf) - strlen(buf) - 1);
393 cmd[2] = buf;
394 cmd[3] = NULL;
395 } else {
396 /* Convert command (char*) into cmd (char**, one word per string) */
397 for (tmpCmd = command, i = 0;
398 (tmpCmd = strsep(&command, " \t")) != NULL;) {
399 if (*tmpCmd != '\0') {
400 cmd[i] = tmpCmd;
401 tmpCmd++;
402 i++;
403 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000404 }
Erik Andersene132f4b2000-02-09 04:16:43 +0000405 cmd[i] = NULL;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000406 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000407
408 /* Now run it. The new program will take over this PID,
409 * so nothing further in init.c should be run. */
410 execve(cmd[0], cmd, environment);
411
412 /* We're still here? Some error happened. */
413 message(LOG | CONSOLE, "Bummer, could not run '%s': %s\n", cmd[0],
414 strerror(errno));
415 exit(-1);
Erik Andersen05df2392000-01-13 04:43:48 +0000416 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000417 return pid;
Eric Andersen0460ff21999-10-25 23:32:44 +0000418}
419
Erik Andersene49d5ec2000-02-08 19:58:47 +0000420static int waitfor(char *command, char *terminal, int get_enter)
Erik Andersen0e3782f2000-01-07 02:54:55 +0000421{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000422 int status, wpid;
423 int pid = run(command, terminal, get_enter);
Erik Andersen0e3782f2000-01-07 02:54:55 +0000424
Erik Andersene49d5ec2000-02-08 19:58:47 +0000425 while (1) {
426 wpid = wait(&status);
Erik Andersene132f4b2000-02-09 04:16:43 +0000427 if (wpid > 0 && wpid != pid) {
428 continue;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000429 }
430 if (wpid == pid)
431 break;
Erik Andersen0e3782f2000-01-07 02:54:55 +0000432 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000433 return wpid;
Erik Andersen0e3782f2000-01-07 02:54:55 +0000434}
435
Eric Andersen219d6f51999-11-02 19:43:01 +0000436/* Make sure there is enough memory to do something useful. *
Erik Andersene132f4b2000-02-09 04:16:43 +0000437 * Calls "swapon -a" if needed so be sure /etc/fstab is present... */
Eric Andersen219d6f51999-11-02 19:43:01 +0000438static void check_memory()
439{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000440 struct stat statBuf;
Eric Andersen219d6f51999-11-02 19:43:01 +0000441
Erik Andersend07ee462000-02-21 21:26:32 +0000442 if (check_free_memory() > 1000)
Erik Andersene49d5ec2000-02-08 19:58:47 +0000443 return;
444
445 if (stat("/etc/fstab", &statBuf) == 0) {
446 /* Try to turn on swap */
Erik Andersenfb1793f2000-02-09 16:37:08 +0000447 system("/sbin/swapon -a");
Erik Andersend07ee462000-02-21 21:26:32 +0000448 if (check_free_memory() < 1000)
Erik Andersene49d5ec2000-02-08 19:58:47 +0000449 goto goodnight;
450 } else
451 goto goodnight;
Eric Andersen219d6f51999-11-02 19:43:01 +0000452 return;
453
Erik Andersene49d5ec2000-02-08 19:58:47 +0000454 goodnight:
455 message(CONSOLE,
456 "Sorry, your computer does not have enough memory.\r\n");
457 while (1)
458 sleep(1);
Eric Andersen219d6f51999-11-02 19:43:01 +0000459}
460
Erik Andersene132f4b2000-02-09 04:16:43 +0000461/* Run all commands to be run right before halt/reboot */
462static void run_lastAction(void)
463{
464 initAction *a;
465 for (a = initActionList; a; a = a->nextPtr) {
466 if (a->action == CTRLALTDEL) {
467 waitfor(a->process, a->console, FALSE);
468 delete_initAction(a);
469 }
470 }
471}
472
473
Erik Andersen7dc16072000-01-04 01:10:25 +0000474#ifndef DEBUG_INIT
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000475static void shutdown_system(void)
Eric Andersencc8ed391999-10-05 16:24:54 +0000476{
Erik Andersene132f4b2000-02-09 04:16:43 +0000477
Erik Andersene49d5ec2000-02-08 19:58:47 +0000478 /* first disable our SIGHUP signal */
479 signal(SIGHUP, SIG_DFL);
Erik Andersen7dc16072000-01-04 01:10:25 +0000480
Erik Andersene49d5ec2000-02-08 19:58:47 +0000481 /* Allow Ctrl-Alt-Del to reboot system. */
482 reboot(RB_ENABLE_CAD);
Erik Andersene132f4b2000-02-09 04:16:43 +0000483
484 message(CONSOLE|LOG, "\r\nThe system is going down NOW !!\r\n");
Eric Andersencf8c9cf1999-11-05 00:31:46 +0000485 sync();
Erik Andersene49d5ec2000-02-08 19:58:47 +0000486
487 /* Send signals to every process _except_ pid 1 */
Erik Andersene132f4b2000-02-09 04:16:43 +0000488 message(CONSOLE|LOG, "Sending SIGTERM to all processes.\r\n");
Erik Andersene49d5ec2000-02-08 19:58:47 +0000489 kill(-1, SIGTERM);
Erik Andersene132f4b2000-02-09 04:16:43 +0000490 sleep(1);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000491 sync();
492
Erik Andersene132f4b2000-02-09 04:16:43 +0000493 message(CONSOLE|LOG, "Sending SIGKILL to all processes.\r\n");
Erik Andersene49d5ec2000-02-08 19:58:47 +0000494 kill(-1, SIGKILL);
Erik Andersene132f4b2000-02-09 04:16:43 +0000495 sleep(1);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000496
Erik Andersene132f4b2000-02-09 04:16:43 +0000497 /* run everything to be run at "ctrlaltdel" */
498 run_lastAction();
499
Erik Andersene49d5ec2000-02-08 19:58:47 +0000500 sync();
501 if (kernelVersion > 0 && kernelVersion <= 2 * 65536 + 2 * 256 + 11) {
502 /* bdflush, kupdate not needed for kernels >2.2.11 */
503 bdflush(1, 0);
504 sync();
505 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000506}
507
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000508static void halt_signal(int sig)
Eric Andersencc8ed391999-10-05 16:24:54 +0000509{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000510 shutdown_system();
Erik Andersene132f4b2000-02-09 04:16:43 +0000511 message(CONSOLE|LOG,
Erik Andersene49d5ec2000-02-08 19:58:47 +0000512 "The system is halted. Press %s or turn off power\r\n",
513 (secondConsole == NULL) /* serial console */
514 ? "Reset" : "CTRL-ALT-DEL");
515 sync();
Erik Andersen3fe39dc2000-01-25 18:13:53 +0000516
Erik Andersene49d5ec2000-02-08 19:58:47 +0000517 /* allow time for last message to reach serial console */
Erik Andersene132f4b2000-02-09 04:16:43 +0000518 sleep(2);
Erik Andersen3fe39dc2000-01-25 18:13:53 +0000519
Erik Andersen4d1d0111999-12-17 18:44:15 +0000520#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,2,0)
Erik Andersene49d5ec2000-02-08 19:58:47 +0000521 if (sig == SIGUSR2)
522 reboot(RB_POWER_OFF);
523 else
Erik Andersen4d1d0111999-12-17 18:44:15 +0000524#endif
Erik Andersene49d5ec2000-02-08 19:58:47 +0000525 reboot(RB_HALT_SYSTEM);
526 exit(0);
Eric Andersencc8ed391999-10-05 16:24:54 +0000527}
528
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000529static void reboot_signal(int sig)
Eric Andersencc8ed391999-10-05 16:24:54 +0000530{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000531 shutdown_system();
Erik Andersene132f4b2000-02-09 04:16:43 +0000532 message(CONSOLE|LOG, "Please stand by while rebooting the system.\r\n");
Erik Andersene49d5ec2000-02-08 19:58:47 +0000533 sync();
Erik Andersen3fe39dc2000-01-25 18:13:53 +0000534
Erik Andersene49d5ec2000-02-08 19:58:47 +0000535 /* allow time for last message to reach serial console */
536 sleep(2);
Erik Andersen3fe39dc2000-01-25 18:13:53 +0000537
Erik Andersene49d5ec2000-02-08 19:58:47 +0000538 reboot(RB_AUTOBOOT);
539 exit(0);
Eric Andersencc8ed391999-10-05 16:24:54 +0000540}
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000541
Erik Andersende552872000-01-23 01:34:05 +0000542#if defined BB_FEATURE_INIT_CHROOT
Erik Andersend07ee462000-02-21 21:26:32 +0000543
544#if ! defined BB_FEATURE_USE_PROCFS
545#error Sorry, I depend on the /proc filesystem right now.
546#endif
547
Erik Andersende552872000-01-23 01:34:05 +0000548static void check_chroot(int sig)
549{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000550 char *argv_init[2] = { "init", NULL, };
551 char *envp_init[3] = { "HOME=/", "TERM=linux", NULL, };
552 char rootpath[256], *tc;
553 int fd;
Erik Andersende552872000-01-23 01:34:05 +0000554
Erik Andersene49d5ec2000-02-08 19:58:47 +0000555 if ((fd = open("/proc/sys/kernel/init-chroot", O_RDONLY)) == -1) {
556 message(CONSOLE,
557 "SIGHUP recived, but could not open proc file\r\n");
558 sleep(2);
559 return;
560 }
561 if (read(fd, rootpath, sizeof(rootpath)) == -1) {
562 message(CONSOLE,
563 "SIGHUP recived, but could not read proc file\r\n");
564 sleep(2);
565 return;
566 }
567 close(fd);
568
569 if (rootpath[0] == '\0') {
570 message(CONSOLE,
571 "SIGHUP recived, but new root is not valid: %s\r\n",
572 rootpath);
573 sleep(2);
574 return;
575 }
576
577 tc = strrchr(rootpath, '\n');
578 *tc = '\0';
579
580 /* Ok, making it this far means we commit */
581 message(CONSOLE, "Please stand by, changing root to `%s'.\r\n",
582 rootpath);
583
584 /* kill all other programs first */
585 message(CONSOLE, "Sending SIGTERM to all processes.\r\n");
586 kill(-1, SIGTERM);
Erik Andersende552872000-01-23 01:34:05 +0000587 sleep(2);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000588 sync();
589
590 message(CONSOLE, "Sending SIGKILL to all processes.\r\n");
591 kill(-1, SIGKILL);
Erik Andersende552872000-01-23 01:34:05 +0000592 sleep(2);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000593 sync();
594
595 /* ok, we don't need /proc anymore. we also assume that the signaling
596 * process left the rest of the filesystems alone for us */
597 umount("/proc");
598
599 /* Ok, now we chroot. Hopefully we only have two things mounted, the
600 * new chroot'd mount point, and the old "/" mount. s,
601 * we go ahead and unmount the old "/". This should trigger the kernel
602 * to set things up the Right Way(tm). */
603
604 if (!chroot(rootpath))
605 umount("/dev/root");
606
607 /* If the chroot fails, we are already too far to turn back, so we
608 * continue and hope that executing init below will revive the system */
609
610 /* close all of our descriptors and open new ones */
611 close(0);
612 close(1);
613 close(2);
614 open("/dev/console", O_RDWR, 0);
615 dup(0);
616 dup(0);
617
618 message(CONSOLE, "Executing real init...\r\n");
619 /* execute init in the (hopefully) new root */
620 execve("/sbin/init", argv_init, envp_init);
621
622 message(CONSOLE,
623 "ERROR: Could not exec new init. Press %s to reboot.\r\n",
624 (secondConsole == NULL) /* serial console */
625 ? "Reset" : "CTRL-ALT-DEL");
Erik Andersende552872000-01-23 01:34:05 +0000626 return;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000627}
628#endif /* BB_FEATURE_INIT_CHROOT */
Erik Andersende552872000-01-23 01:34:05 +0000629
Erik Andersene49d5ec2000-02-08 19:58:47 +0000630#endif /* ! DEBUG_INIT */
Erik Andersende552872000-01-23 01:34:05 +0000631
Erik Andersene49d5ec2000-02-08 19:58:47 +0000632void new_initAction(initActionEnum action, char *process, char *cons)
Erik Andersen7dc16072000-01-04 01:10:25 +0000633{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000634 initAction *newAction;
Erik Andersen9e737252000-01-06 01:16:13 +0000635
Erik Andersene49d5ec2000-02-08 19:58:47 +0000636 if (*cons == '\0')
637 cons = console;
Erik Andersen9e737252000-01-06 01:16:13 +0000638
Erik Andersene49d5ec2000-02-08 19:58:47 +0000639 /* If BusyBox detects that a serial console is in use,
640 * then entries not refering to the console or null devices will _not_ be run.
641 * The exception to this rule is the null device.
642 */
643 if (secondConsole == NULL && strcmp(cons, console)
644 && strcmp(cons, "/dev/null"))
645 return;
646
647 newAction = calloc((size_t) (1), sizeof(initAction));
648 if (!newAction) {
649 message(LOG | CONSOLE, "Memory allocation failure\n");
650 while (1)
651 sleep(1);
652 }
653 newAction->nextPtr = initActionList;
654 initActionList = newAction;
655 strncpy(newAction->process, process, 255);
656 newAction->action = action;
657 strncpy(newAction->console, cons, 255);
658 newAction->pid = 0;
Erik Andersen31638212000-01-15 22:28:50 +0000659// message(LOG|CONSOLE, "process='%s' action='%d' console='%s'\n",
Erik Andersene49d5ec2000-02-08 19:58:47 +0000660// newAction->process, newAction->action, newAction->console);
Erik Andersen7dc16072000-01-04 01:10:25 +0000661}
662
Erik Andersene132f4b2000-02-09 04:16:43 +0000663static void delete_initAction(initAction * action)
Erik Andersen7dc16072000-01-04 01:10:25 +0000664{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000665 initAction *a, *b = NULL;
666
667 for (a = initActionList; a; b = a, a = a->nextPtr) {
668 if (a == action) {
669 if (b == NULL) {
670 initActionList = a->nextPtr;
671 } else {
672 b->nextPtr = a->nextPtr;
673 }
674 free(a);
675 break;
676 }
Erik Andersen7dc16072000-01-04 01:10:25 +0000677 }
Erik Andersen7dc16072000-01-04 01:10:25 +0000678}
679
Erik Andersen9e737252000-01-06 01:16:13 +0000680/* NOTE that if BB_FEATURE_USE_INITTAB is NOT defined,
681 * then parse_inittab() simply adds in some default
682 * actions(i.e runs INIT_SCRIPT and then starts a pair
Erik Andersen812d4662000-01-07 18:30:40 +0000683 * of "askfirst" shells). If BB_FEATURE_USE_INITTAB
684 * _is_ defined, but /etc/inittab is missing, this
685 * results in the same set of default behaviors.
Erik Andersen9e737252000-01-06 01:16:13 +0000686 * */
Erik Andersene49d5ec2000-02-08 19:58:47 +0000687void parse_inittab(void)
Erik Andersen7dc16072000-01-04 01:10:25 +0000688{
Erik Andersen9e737252000-01-06 01:16:13 +0000689#ifdef BB_FEATURE_USE_INITTAB
Erik Andersene49d5ec2000-02-08 19:58:47 +0000690 FILE *file;
691 char buf[256], lineAsRead[256], tmpConsole[256];
692 char *p, *q, *r, *s;
693 const struct initActionType *a = actions;
694 int foundIt;
Erik Andersen7dc16072000-01-04 01:10:25 +0000695
696
Erik Andersene49d5ec2000-02-08 19:58:47 +0000697 file = fopen(INITTAB, "r");
698 if (file == NULL) {
699 /* No inittab file -- set up some default behavior */
Erik Andersen9e737252000-01-06 01:16:13 +0000700#endif
Erik Andersene132f4b2000-02-09 04:16:43 +0000701 /* Swapoff on halt/reboot */
Erik Andersenfb1793f2000-02-09 16:37:08 +0000702 new_initAction(CTRLALTDEL, "/sbin/swapoff -a > /dev/null 2>&1", console);
Erik Andersene132f4b2000-02-09 04:16:43 +0000703 /* Umount all filesystems on halt/reboot */
704 new_initAction(CTRLALTDEL, "/bin/umount -a -r > /dev/null 2>&1", console);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000705 /* Askfirst shell on tty1 */
706 new_initAction(ASKFIRST, SHELL, console);
707 /* Askfirst shell on tty2 */
708 if (secondConsole != NULL)
709 new_initAction(ASKFIRST, SHELL, secondConsole);
710 /* sysinit */
711 new_initAction(SYSINIT, INIT_SCRIPT, console);
Erik Andersen7dc16072000-01-04 01:10:25 +0000712
Erik Andersene49d5ec2000-02-08 19:58:47 +0000713 return;
Erik Andersen9e737252000-01-06 01:16:13 +0000714#ifdef BB_FEATURE_USE_INITTAB
Erik Andersen9e737252000-01-06 01:16:13 +0000715 }
Erik Andersen7dc16072000-01-04 01:10:25 +0000716
Erik Andersene49d5ec2000-02-08 19:58:47 +0000717 while (fgets(buf, 255, file) != NULL) {
718 foundIt = FALSE;
719 for (p = buf; *p == ' ' || *p == '\t'; p++);
720 if (*p == '#' || *p == '\n')
721 continue;
Erik Andersen7dc16072000-01-04 01:10:25 +0000722
Erik Andersene49d5ec2000-02-08 19:58:47 +0000723 /* Trim the trailing \n */
724 q = strrchr(p, '\n');
725 if (q != NULL)
726 *q = '\0';
Erik Andersen7dc16072000-01-04 01:10:25 +0000727
Erik Andersene49d5ec2000-02-08 19:58:47 +0000728 /* Keep a copy around for posterity's sake (and error msgs) */
729 strcpy(lineAsRead, buf);
730
731 /* Grab the ID field */
732 s = p;
733 p = strchr(p, ':');
734 if (p != NULL || *(p + 1) != '\0') {
735 *p = '\0';
736 ++p;
Erik Andersen0e3782f2000-01-07 02:54:55 +0000737 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000738
739 /* Now peal off the process field from the end
740 * of the string */
741 q = strrchr(p, ':');
742 if (q == NULL || *(q + 1) == '\0') {
743 message(LOG | CONSOLE, "Bad inittab entry: %s\n", lineAsRead);
744 continue;
745 } else {
746 *q = '\0';
747 ++q;
748 }
749
750 /* Now peal off the action field */
751 r = strrchr(p, ':');
752 if (r == NULL || *(r + 1) == '\0') {
753 message(LOG | CONSOLE, "Bad inittab entry: %s\n", lineAsRead);
754 continue;
755 } else {
756 ++r;
757 }
758
759 /* Ok, now process it */
760 a = actions;
761 while (a->name != 0) {
762 if (strcmp(a->name, r) == 0) {
763 if (*s != '\0') {
764 struct stat statBuf;
765
766 strcpy(tmpConsole, "/dev/");
767 strncat(tmpConsole, s, 200);
768 if (stat(tmpConsole, &statBuf) != 0) {
769 message(LOG | CONSOLE,
770 "device '%s' does not exist. Did you read the directions?\n",
771 tmpConsole);
772 break;
773 }
774 s = tmpConsole;
775 }
776 new_initAction(a->action, q, s);
777 foundIt = TRUE;
778 }
779 a++;
780 }
781 if (foundIt == TRUE)
782 continue;
783 else {
784 /* Choke on an unknown action */
785 message(LOG | CONSOLE, "Bad inittab entry: %s\n", lineAsRead);
786 }
Erik Andersen7dc16072000-01-04 01:10:25 +0000787 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000788 return;
Erik Andersen9e737252000-01-06 01:16:13 +0000789#endif
Erik Andersen7dc16072000-01-04 01:10:25 +0000790}
791
Erik Andersene132f4b2000-02-09 04:16:43 +0000792
793
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000794extern int init_main(int argc, char **argv)
Eric Andersen0460ff21999-10-25 23:32:44 +0000795{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000796 initAction *a;
797 pid_t wpid;
798 int status;
Eric Andersen0460ff21999-10-25 23:32:44 +0000799
Eric Andersend73dc5b1999-11-10 23:13:02 +0000800#ifndef DEBUG_INIT
Erik Andersena51ecdd2000-02-24 18:09:58 +0000801 /* Expect to be invoked as init with PID=1 or be invoked as linuxrc */
802 if (getpid() != 1
803#ifdef BB_FEATURE_LINUXRC
804 && strstr(argv[0], "linuxrc") == NULL
805#endif
806 )
807 {
808 usage("init\n\nInit is the parent of all processes.\n\n"
809 "This version of init is designed to be run only "
810 "by the kernel.\n");
Erik Andersene49d5ec2000-02-08 19:58:47 +0000811 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000812 /* Set up sig handlers -- be sure to
813 * clear all of these in run() */
814 signal(SIGUSR1, halt_signal);
815 signal(SIGUSR2, reboot_signal);
816 signal(SIGINT, reboot_signal);
817 signal(SIGTERM, reboot_signal);
Erik Andersende552872000-01-23 01:34:05 +0000818#if defined BB_FEATURE_INIT_CHROOT
Erik Andersene49d5ec2000-02-08 19:58:47 +0000819 signal(SIGHUP, check_chroot);
Erik Andersende552872000-01-23 01:34:05 +0000820#endif
Eric Andersen0460ff21999-10-25 23:32:44 +0000821
Erik Andersene49d5ec2000-02-08 19:58:47 +0000822 /* Turn off rebooting via CTL-ALT-DEL -- we get a
823 * SIGINT on CAD so we can shut things down gracefully... */
824 reboot(RB_DISABLE_CAD);
825#endif
Erik Andersen05df2392000-01-13 04:43:48 +0000826
Erik Andersene49d5ec2000-02-08 19:58:47 +0000827 /* Figure out where the default console should be */
828 console_init();
Eric Andersen0460ff21999-10-25 23:32:44 +0000829
Erik Andersene49d5ec2000-02-08 19:58:47 +0000830 /* Close whatever files are open, and reset the console. */
831 close(0);
832 close(1);
833 close(2);
834 set_term(0);
835 setsid();
Eric Andersencc8ed391999-10-05 16:24:54 +0000836
Erik Andersene49d5ec2000-02-08 19:58:47 +0000837 /* Make sure PATH is set to something sane */
838 putenv(_PATH_STDPATH);
Eric Andersencc8ed391999-10-05 16:24:54 +0000839
Erik Andersene49d5ec2000-02-08 19:58:47 +0000840 /* Hello world */
Eric Andersen7f1acfd1999-10-29 23:09:13 +0000841#ifndef DEBUG_INIT
Erik Andersenea6b67d2000-03-07 07:47:10 +0000842 message(
843#if ! defined BB_FEATURE_EXTRA_QUIET
844 CONSOLE|
845#endif
846 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 Andersenea6b67d2000-03-07 07:47:10 +0000850 message(
851#if ! defined BB_FEATURE_EXTRA_QUIET
852 CONSOLE|
853#endif
854 LOG,
Erik Andersene49d5ec2000-02-08 19:58:47 +0000855 "init(%d) started: BusyBox v%s (%s) multi-call binary\r\n",
856 getpid(), BB_VER, BB_BT);
Eric Andersen7f1acfd1999-10-29 23:09:13 +0000857#endif
Eric Andersencc8ed391999-10-05 16:24:54 +0000858
Eric Andersencc8ed391999-10-05 16:24:54 +0000859
Erik Andersene49d5ec2000-02-08 19:58:47 +0000860 /* Make sure there is enough memory to do something useful. */
861 check_memory();
Erik Andersen9e737252000-01-06 01:16:13 +0000862
Erik Andersene49d5ec2000-02-08 19:58:47 +0000863 /* Check if we are supposed to be in single user mode */
864 if (argc > 1 && (!strcmp(argv[1], "single") ||
865 !strcmp(argv[1], "-s") || !strcmp(argv[1], "1"))) {
866 /* Ask first then start a shell on tty2 */
867 if (secondConsole != NULL)
868 new_initAction(ASKFIRST, SHELL, secondConsole);
869 /* Start a shell on tty1 */
870 new_initAction(RESPAWN, SHELL, console);
871 } else {
872 /* Not in single user mode -- see what inittab says */
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000873
Erik Andersene49d5ec2000-02-08 19:58:47 +0000874 /* NOTE that if BB_FEATURE_USE_INITTAB is NOT defined,
875 * then parse_inittab() simply adds in some default
876 * actions(i.e runs INIT_SCRIPT and then starts a pair
877 * of "askfirst" shells */
878 parse_inittab();
Eric Andersend00c2621999-12-07 08:37:31 +0000879 }
Erik Andersene132f4b2000-02-09 04:16:43 +0000880
881 /* Fix up argv[0] to be certain we claim to be init */
882 strncpy(argv[0], "init", strlen(argv[0])+1);
Erik Andersen07f56042000-02-09 06:05:01 +0000883 if (argc > 1)
884 strncpy(argv[1], "\0", strlen(argv[1])+1);
Eric Andersen219d6f51999-11-02 19:43:01 +0000885
Erik Andersene49d5ec2000-02-08 19:58:47 +0000886 /* Now run everything that needs to be run */
887
888 /* First run the sysinit command */
889 for (a = initActionList; a; a = a->nextPtr) {
890 if (a->action == SYSINIT) {
891 waitfor(a->process, a->console, FALSE);
892 /* Now remove the "sysinit" entry from the list */
893 delete_initAction(a);
Erik Andersen7dc16072000-01-04 01:10:25 +0000894 }
Erik Andersen7dc16072000-01-04 01:10:25 +0000895 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000896 /* Next run anything that wants to block */
897 for (a = initActionList; a; a = a->nextPtr) {
898 if (a->action == WAIT) {
899 waitfor(a->process, a->console, FALSE);
900 /* Now remove the "wait" entry from the list */
901 delete_initAction(a);
Erik Andersen7dc16072000-01-04 01:10:25 +0000902 }
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000903 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000904 /* Next run anything to be run only once */
905 for (a = initActionList; a; a = a->nextPtr) {
906 if (a->action == ONCE) {
907 run(a->process, a->console, FALSE);
908 /* Now remove the "once" entry from the list */
909 delete_initAction(a);
910 }
911 }
912 /* If there is nothing else to do, stop */
913 if (initActionList == NULL) {
914 message(LOG | CONSOLE,
915 "No more tasks for init -- sleeping forever.\n");
916 while (1)
917 sleep(1);
918 }
919
920 /* Now run the looping stuff for the rest of forever */
921 while (1) {
922 for (a = initActionList; a; a = a->nextPtr) {
923 /* Only run stuff with pid==0. If they have
924 * a pid, that means they are still running */
925 if (a->pid == 0) {
926 switch (a->action) {
927 case RESPAWN:
928 /* run the respawn stuff */
929 a->pid = run(a->process, a->console, FALSE);
930 break;
931 case ASKFIRST:
932 /* run the askfirst stuff */
933 a->pid = run(a->process, a->console, TRUE);
934 break;
935 /* silence the compiler's incessant whining */
936 default:
937 break;
938 }
939 }
940 }
941 /* Wait for a child process to exit */
942 wpid = wait(&status);
943 if (wpid > 0) {
944 /* Find out who died and clean up their corpse */
945 for (a = initActionList; a; a = a->nextPtr) {
946 if (a->pid == wpid) {
947 a->pid = 0;
948 message(LOG,
949 "Process '%s' (pid %d) exited. Scheduling it for restart.\n",
950 a->process, wpid);
951 }
952 }
953 }
954 sleep(1);
955 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000956}
Erik Andersen029011b2000-03-04 21:19:32 +0000957
958/*
959Local Variables:
960c-file-style: "linux"
961c-basic-offset: 4
962tab-width: 4
963End:
964*/