blob: a3e165ecce3bf4317f39509a23f61381c16d724a [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 Andersene2729152000-02-18 21:34:17 +0000317 /* Force the TERM setting to vt102 for serial console */
318 snprintf(termType, sizeof(termType) - 1, "TERM=vt102");
Erik Andersene132f4b2000-02-09 04:16:43 +0000319 message(LOG | CONSOLE,
320 "serial console detected. Disabling virtual terminals.\r\n");
Erik Andersene49d5ec2000-02-08 19:58:47 +0000321 }
322 close(fd);
Eric Andersen07e52971999-11-07 07:38:08 +0000323 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000324 message(LOG, "console=%s\n", console);
Eric Andersen0460ff21999-10-25 23:32:44 +0000325}
326
Erik Andersene49d5ec2000-02-08 19:58:47 +0000327static pid_t run(char *command, char *terminal, int get_enter)
Eric Andersen0460ff21999-10-25 23:32:44 +0000328{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000329 int i, fd;
330 pid_t pid;
331 char *tmpCmd;
332 char *cmd[255];
Erik Andersene132f4b2000-02-09 04:16:43 +0000333 char buf[255];
Erik Andersene49d5ec2000-02-08 19:58:47 +0000334 static const char press_enter[] =
335
336 "\nPlease press Enter to activate this console. ";
337 char *environment[] = {
338 "HOME=/",
339 "PATH=/usr/bin:/bin:/usr/sbin:/sbin",
340 "SHELL=/bin/sh",
341 termType,
342 "USER=root",
343 0
344 };
Erik Andersenac6e71f2000-01-08 22:04:33 +0000345
Eric Andersen0460ff21999-10-25 23:32:44 +0000346
Erik Andersene49d5ec2000-02-08 19:58:47 +0000347 if ((pid = fork()) == 0) {
Erik Andersene132f4b2000-02-09 04:16:43 +0000348#ifdef DEBUG_INIT
Erik Andersene49d5ec2000-02-08 19:58:47 +0000349 pid_t shell_pgid = getpid();
Erik Andersene132f4b2000-02-09 04:16:43 +0000350#endif
Erik Andersen0881de72000-01-05 09:34:26 +0000351
Erik Andersene49d5ec2000-02-08 19:58:47 +0000352 /* Clean up */
353 close(0);
354 close(1);
355 close(2);
356 setsid();
Eric Andersen0460ff21999-10-25 23:32:44 +0000357
Erik Andersene49d5ec2000-02-08 19:58:47 +0000358 /* Reset signal handlers set for parent process */
359 signal(SIGUSR1, SIG_DFL);
360 signal(SIGUSR2, SIG_DFL);
361 signal(SIGINT, SIG_DFL);
362 signal(SIGTERM, SIG_DFL);
363 signal(SIGHUP, SIG_DFL);
Eric Andersen2f6c04f1999-11-01 23:59:44 +0000364
Erik Andersene49d5ec2000-02-08 19:58:47 +0000365 if ((fd = device_open(terminal, O_RDWR)) < 0) {
366 message(LOG | CONSOLE, "Bummer, can't open %s\r\n", terminal);
367 exit(1);
368 }
369 dup2(fd, 0);
370 dup2(fd, 1);
371 dup2(fd, 2);
372 tcsetpgrp(0, getpgrp());
373 set_term(0);
374
375 if (get_enter == TRUE) {
376 /*
377 * Save memory by not exec-ing anything large (like a shell)
378 * before the user wants it. This is critical if swap is not
379 * enabled and the system has low memory. Generally this will
380 * be run on the second virtual console, and the first will
381 * be allowed to start a shell or whatever an init script
382 * specifies.
383 */
384 char c;
385
Erik Andersene132f4b2000-02-09 04:16:43 +0000386#ifdef DEBUG_INIT
Erik Andersene49d5ec2000-02-08 19:58:47 +0000387 message(LOG, "Waiting for enter to start '%s' (pid %d, console %s)\r\n",
388 command, shell_pgid, terminal);
Erik Andersene132f4b2000-02-09 04:16:43 +0000389#endif
Erik Andersene49d5ec2000-02-08 19:58:47 +0000390 write(fileno(stdout), press_enter, sizeof(press_enter) - 1);
391 read(fileno(stdin), &c, 1);
392 }
393
Erik Andersene49d5ec2000-02-08 19:58:47 +0000394#ifdef DEBUG_INIT
Erik Andersene132f4b2000-02-09 04:16:43 +0000395 /* Log the process name and args */
396 message(LOG, "Starting pid %d, console %s: '%s'\r\n",
397 shell_pgid, terminal, command);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000398#endif
Erik Andersene132f4b2000-02-09 04:16:43 +0000399
400 /* See if any special /bin/sh requiring characters are present */
401 if (strpbrk(command, "~`!$^&*()=|\\{}[];\"'<>?") != NULL) {
402 cmd[0] = SHELL;
403 cmd[1] = "-c";
404 strcpy(buf, "exec ");
405 strncat(buf, command, sizeof(buf) - strlen(buf) - 1);
406 cmd[2] = buf;
407 cmd[3] = NULL;
408 } else {
409 /* Convert command (char*) into cmd (char**, one word per string) */
410 for (tmpCmd = command, i = 0;
411 (tmpCmd = strsep(&command, " \t")) != NULL;) {
412 if (*tmpCmd != '\0') {
413 cmd[i] = tmpCmd;
414 tmpCmd++;
415 i++;
416 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000417 }
Erik Andersene132f4b2000-02-09 04:16:43 +0000418 cmd[i] = NULL;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000419 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000420
421 /* Now run it. The new program will take over this PID,
422 * so nothing further in init.c should be run. */
423 execve(cmd[0], cmd, environment);
424
425 /* We're still here? Some error happened. */
426 message(LOG | CONSOLE, "Bummer, could not run '%s': %s\n", cmd[0],
427 strerror(errno));
428 exit(-1);
Erik Andersen05df2392000-01-13 04:43:48 +0000429 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000430 return pid;
Eric Andersen0460ff21999-10-25 23:32:44 +0000431}
432
Erik Andersene49d5ec2000-02-08 19:58:47 +0000433static int waitfor(char *command, char *terminal, int get_enter)
Erik Andersen0e3782f2000-01-07 02:54:55 +0000434{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000435 int status, wpid;
436 int pid = run(command, terminal, get_enter);
Erik Andersen0e3782f2000-01-07 02:54:55 +0000437
Erik Andersene49d5ec2000-02-08 19:58:47 +0000438 while (1) {
439 wpid = wait(&status);
Erik Andersene132f4b2000-02-09 04:16:43 +0000440 if (wpid > 0 && wpid != pid) {
441 continue;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000442 }
443 if (wpid == pid)
444 break;
Erik Andersen0e3782f2000-01-07 02:54:55 +0000445 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000446 return wpid;
Erik Andersen0e3782f2000-01-07 02:54:55 +0000447}
448
Eric Andersen219d6f51999-11-02 19:43:01 +0000449/* Make sure there is enough memory to do something useful. *
Erik Andersene132f4b2000-02-09 04:16:43 +0000450 * Calls "swapon -a" if needed so be sure /etc/fstab is present... */
Eric Andersen219d6f51999-11-02 19:43:01 +0000451static void check_memory()
452{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000453 struct stat statBuf;
Eric Andersen219d6f51999-11-02 19:43:01 +0000454
Erik Andersene49d5ec2000-02-08 19:58:47 +0000455 if (mem_total() > 3500)
456 return;
457
458 if (stat("/etc/fstab", &statBuf) == 0) {
459 /* Try to turn on swap */
Erik Andersenfb1793f2000-02-09 16:37:08 +0000460 system("/sbin/swapon -a");
Erik Andersene49d5ec2000-02-08 19:58:47 +0000461 if (mem_total() < 3500)
462 goto goodnight;
463 } else
464 goto goodnight;
Eric Andersen219d6f51999-11-02 19:43:01 +0000465 return;
466
Erik Andersene49d5ec2000-02-08 19:58:47 +0000467 goodnight:
468 message(CONSOLE,
469 "Sorry, your computer does not have enough memory.\r\n");
470 while (1)
471 sleep(1);
Eric Andersen219d6f51999-11-02 19:43:01 +0000472}
473
Erik Andersene132f4b2000-02-09 04:16:43 +0000474/* Run all commands to be run right before halt/reboot */
475static void run_lastAction(void)
476{
477 initAction *a;
478 for (a = initActionList; a; a = a->nextPtr) {
479 if (a->action == CTRLALTDEL) {
480 waitfor(a->process, a->console, FALSE);
481 delete_initAction(a);
482 }
483 }
484}
485
486
Erik Andersen7dc16072000-01-04 01:10:25 +0000487#ifndef DEBUG_INIT
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000488static void shutdown_system(void)
Eric Andersencc8ed391999-10-05 16:24:54 +0000489{
Erik Andersene132f4b2000-02-09 04:16:43 +0000490
Erik Andersene49d5ec2000-02-08 19:58:47 +0000491 /* first disable our SIGHUP signal */
492 signal(SIGHUP, SIG_DFL);
Erik Andersen7dc16072000-01-04 01:10:25 +0000493
Erik Andersene49d5ec2000-02-08 19:58:47 +0000494 /* Allow Ctrl-Alt-Del to reboot system. */
495 reboot(RB_ENABLE_CAD);
Erik Andersene132f4b2000-02-09 04:16:43 +0000496
497 message(CONSOLE|LOG, "\r\nThe system is going down NOW !!\r\n");
Eric Andersencf8c9cf1999-11-05 00:31:46 +0000498 sync();
Erik Andersene49d5ec2000-02-08 19:58:47 +0000499
500 /* Send signals to every process _except_ pid 1 */
Erik Andersene132f4b2000-02-09 04:16:43 +0000501 message(CONSOLE|LOG, "Sending SIGTERM to all processes.\r\n");
Erik Andersene49d5ec2000-02-08 19:58:47 +0000502 kill(-1, SIGTERM);
Erik Andersene132f4b2000-02-09 04:16:43 +0000503 sleep(1);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000504 sync();
505
Erik Andersene132f4b2000-02-09 04:16:43 +0000506 message(CONSOLE|LOG, "Sending SIGKILL to all processes.\r\n");
Erik Andersene49d5ec2000-02-08 19:58:47 +0000507 kill(-1, SIGKILL);
Erik Andersene132f4b2000-02-09 04:16:43 +0000508 sleep(1);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000509
Erik Andersene132f4b2000-02-09 04:16:43 +0000510 /* run everything to be run at "ctrlaltdel" */
511 run_lastAction();
512
Erik Andersene49d5ec2000-02-08 19:58:47 +0000513 sync();
514 if (kernelVersion > 0 && kernelVersion <= 2 * 65536 + 2 * 256 + 11) {
515 /* bdflush, kupdate not needed for kernels >2.2.11 */
516 bdflush(1, 0);
517 sync();
518 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000519}
520
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000521static void halt_signal(int sig)
Eric Andersencc8ed391999-10-05 16:24:54 +0000522{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000523 shutdown_system();
Erik Andersene132f4b2000-02-09 04:16:43 +0000524 message(CONSOLE|LOG,
Erik Andersene49d5ec2000-02-08 19:58:47 +0000525 "The system is halted. Press %s or turn off power\r\n",
526 (secondConsole == NULL) /* serial console */
527 ? "Reset" : "CTRL-ALT-DEL");
528 sync();
Erik Andersen3fe39dc2000-01-25 18:13:53 +0000529
Erik Andersene49d5ec2000-02-08 19:58:47 +0000530 /* allow time for last message to reach serial console */
Erik Andersene132f4b2000-02-09 04:16:43 +0000531 sleep(2);
Erik Andersen3fe39dc2000-01-25 18:13:53 +0000532
Erik Andersen4d1d0111999-12-17 18:44:15 +0000533#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,2,0)
Erik Andersene49d5ec2000-02-08 19:58:47 +0000534 if (sig == SIGUSR2)
535 reboot(RB_POWER_OFF);
536 else
Erik Andersen4d1d0111999-12-17 18:44:15 +0000537#endif
Erik Andersene49d5ec2000-02-08 19:58:47 +0000538 reboot(RB_HALT_SYSTEM);
539 exit(0);
Eric Andersencc8ed391999-10-05 16:24:54 +0000540}
541
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000542static void reboot_signal(int sig)
Eric Andersencc8ed391999-10-05 16:24:54 +0000543{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000544 shutdown_system();
Erik Andersene132f4b2000-02-09 04:16:43 +0000545 message(CONSOLE|LOG, "Please stand by while rebooting the system.\r\n");
Erik Andersene49d5ec2000-02-08 19:58:47 +0000546 sync();
Erik Andersen3fe39dc2000-01-25 18:13:53 +0000547
Erik Andersene49d5ec2000-02-08 19:58:47 +0000548 /* allow time for last message to reach serial console */
549 sleep(2);
Erik Andersen3fe39dc2000-01-25 18:13:53 +0000550
Erik Andersene49d5ec2000-02-08 19:58:47 +0000551 reboot(RB_AUTOBOOT);
552 exit(0);
Eric Andersencc8ed391999-10-05 16:24:54 +0000553}
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000554
Erik Andersende552872000-01-23 01:34:05 +0000555#if defined BB_FEATURE_INIT_CHROOT
556static void check_chroot(int sig)
557{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000558 char *argv_init[2] = { "init", NULL, };
559 char *envp_init[3] = { "HOME=/", "TERM=linux", NULL, };
560 char rootpath[256], *tc;
561 int fd;
Erik Andersende552872000-01-23 01:34:05 +0000562
Erik Andersene49d5ec2000-02-08 19:58:47 +0000563 if ((fd = open("/proc/sys/kernel/init-chroot", O_RDONLY)) == -1) {
564 message(CONSOLE,
565 "SIGHUP recived, but could not open proc file\r\n");
566 sleep(2);
567 return;
568 }
569 if (read(fd, rootpath, sizeof(rootpath)) == -1) {
570 message(CONSOLE,
571 "SIGHUP recived, but could not read proc file\r\n");
572 sleep(2);
573 return;
574 }
575 close(fd);
576
577 if (rootpath[0] == '\0') {
578 message(CONSOLE,
579 "SIGHUP recived, but new root is not valid: %s\r\n",
580 rootpath);
581 sleep(2);
582 return;
583 }
584
585 tc = strrchr(rootpath, '\n');
586 *tc = '\0';
587
588 /* Ok, making it this far means we commit */
589 message(CONSOLE, "Please stand by, changing root to `%s'.\r\n",
590 rootpath);
591
592 /* kill all other programs first */
593 message(CONSOLE, "Sending SIGTERM to all processes.\r\n");
594 kill(-1, SIGTERM);
Erik Andersende552872000-01-23 01:34:05 +0000595 sleep(2);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000596 sync();
597
598 message(CONSOLE, "Sending SIGKILL to all processes.\r\n");
599 kill(-1, SIGKILL);
Erik Andersende552872000-01-23 01:34:05 +0000600 sleep(2);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000601 sync();
602
603 /* ok, we don't need /proc anymore. we also assume that the signaling
604 * process left the rest of the filesystems alone for us */
605 umount("/proc");
606
607 /* Ok, now we chroot. Hopefully we only have two things mounted, the
608 * new chroot'd mount point, and the old "/" mount. s,
609 * we go ahead and unmount the old "/". This should trigger the kernel
610 * to set things up the Right Way(tm). */
611
612 if (!chroot(rootpath))
613 umount("/dev/root");
614
615 /* If the chroot fails, we are already too far to turn back, so we
616 * continue and hope that executing init below will revive the system */
617
618 /* close all of our descriptors and open new ones */
619 close(0);
620 close(1);
621 close(2);
622 open("/dev/console", O_RDWR, 0);
623 dup(0);
624 dup(0);
625
626 message(CONSOLE, "Executing real init...\r\n");
627 /* execute init in the (hopefully) new root */
628 execve("/sbin/init", argv_init, envp_init);
629
630 message(CONSOLE,
631 "ERROR: Could not exec new init. Press %s to reboot.\r\n",
632 (secondConsole == NULL) /* serial console */
633 ? "Reset" : "CTRL-ALT-DEL");
Erik Andersende552872000-01-23 01:34:05 +0000634 return;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000635}
636#endif /* BB_FEATURE_INIT_CHROOT */
Erik Andersende552872000-01-23 01:34:05 +0000637
Erik Andersene49d5ec2000-02-08 19:58:47 +0000638#endif /* ! DEBUG_INIT */
Erik Andersende552872000-01-23 01:34:05 +0000639
Erik Andersene49d5ec2000-02-08 19:58:47 +0000640void new_initAction(initActionEnum action, char *process, char *cons)
Erik Andersen7dc16072000-01-04 01:10:25 +0000641{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000642 initAction *newAction;
Erik Andersen9e737252000-01-06 01:16:13 +0000643
Erik Andersene49d5ec2000-02-08 19:58:47 +0000644 if (*cons == '\0')
645 cons = console;
Erik Andersen9e737252000-01-06 01:16:13 +0000646
Erik Andersene49d5ec2000-02-08 19:58:47 +0000647 /* If BusyBox detects that a serial console is in use,
648 * then entries not refering to the console or null devices will _not_ be run.
649 * The exception to this rule is the null device.
650 */
651 if (secondConsole == NULL && strcmp(cons, console)
652 && strcmp(cons, "/dev/null"))
653 return;
654
655 newAction = calloc((size_t) (1), sizeof(initAction));
656 if (!newAction) {
657 message(LOG | CONSOLE, "Memory allocation failure\n");
658 while (1)
659 sleep(1);
660 }
661 newAction->nextPtr = initActionList;
662 initActionList = newAction;
663 strncpy(newAction->process, process, 255);
664 newAction->action = action;
665 strncpy(newAction->console, cons, 255);
666 newAction->pid = 0;
Erik Andersen31638212000-01-15 22:28:50 +0000667// message(LOG|CONSOLE, "process='%s' action='%d' console='%s'\n",
Erik Andersene49d5ec2000-02-08 19:58:47 +0000668// newAction->process, newAction->action, newAction->console);
Erik Andersen7dc16072000-01-04 01:10:25 +0000669}
670
Erik Andersene132f4b2000-02-09 04:16:43 +0000671static void delete_initAction(initAction * action)
Erik Andersen7dc16072000-01-04 01:10:25 +0000672{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000673 initAction *a, *b = NULL;
674
675 for (a = initActionList; a; b = a, a = a->nextPtr) {
676 if (a == action) {
677 if (b == NULL) {
678 initActionList = a->nextPtr;
679 } else {
680 b->nextPtr = a->nextPtr;
681 }
682 free(a);
683 break;
684 }
Erik Andersen7dc16072000-01-04 01:10:25 +0000685 }
Erik Andersen7dc16072000-01-04 01:10:25 +0000686}
687
Erik Andersen9e737252000-01-06 01:16:13 +0000688/* NOTE that if BB_FEATURE_USE_INITTAB is NOT defined,
689 * then parse_inittab() simply adds in some default
690 * actions(i.e runs INIT_SCRIPT and then starts a pair
Erik Andersen812d4662000-01-07 18:30:40 +0000691 * of "askfirst" shells). If BB_FEATURE_USE_INITTAB
692 * _is_ defined, but /etc/inittab is missing, this
693 * results in the same set of default behaviors.
Erik Andersen9e737252000-01-06 01:16:13 +0000694 * */
Erik Andersene49d5ec2000-02-08 19:58:47 +0000695void parse_inittab(void)
Erik Andersen7dc16072000-01-04 01:10:25 +0000696{
Erik Andersen9e737252000-01-06 01:16:13 +0000697#ifdef BB_FEATURE_USE_INITTAB
Erik Andersene49d5ec2000-02-08 19:58:47 +0000698 FILE *file;
699 char buf[256], lineAsRead[256], tmpConsole[256];
700 char *p, *q, *r, *s;
701 const struct initActionType *a = actions;
702 int foundIt;
Erik Andersen7dc16072000-01-04 01:10:25 +0000703
704
Erik Andersene49d5ec2000-02-08 19:58:47 +0000705 file = fopen(INITTAB, "r");
706 if (file == NULL) {
707 /* No inittab file -- set up some default behavior */
Erik Andersen9e737252000-01-06 01:16:13 +0000708#endif
Erik Andersene132f4b2000-02-09 04:16:43 +0000709 /* Swapoff on halt/reboot */
Erik Andersenfb1793f2000-02-09 16:37:08 +0000710 new_initAction(CTRLALTDEL, "/sbin/swapoff -a > /dev/null 2>&1", console);
Erik Andersene132f4b2000-02-09 04:16:43 +0000711 /* Umount all filesystems on halt/reboot */
712 new_initAction(CTRLALTDEL, "/bin/umount -a -r > /dev/null 2>&1", console);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000713 /* Askfirst shell on tty1 */
714 new_initAction(ASKFIRST, SHELL, console);
715 /* Askfirst shell on tty2 */
716 if (secondConsole != NULL)
717 new_initAction(ASKFIRST, SHELL, secondConsole);
718 /* sysinit */
719 new_initAction(SYSINIT, INIT_SCRIPT, console);
Erik Andersen7dc16072000-01-04 01:10:25 +0000720
Erik Andersene49d5ec2000-02-08 19:58:47 +0000721 return;
Erik Andersen9e737252000-01-06 01:16:13 +0000722#ifdef BB_FEATURE_USE_INITTAB
Erik Andersen9e737252000-01-06 01:16:13 +0000723 }
Erik Andersen7dc16072000-01-04 01:10:25 +0000724
Erik Andersene49d5ec2000-02-08 19:58:47 +0000725 while (fgets(buf, 255, file) != NULL) {
726 foundIt = FALSE;
727 for (p = buf; *p == ' ' || *p == '\t'; p++);
728 if (*p == '#' || *p == '\n')
729 continue;
Erik Andersen7dc16072000-01-04 01:10:25 +0000730
Erik Andersene49d5ec2000-02-08 19:58:47 +0000731 /* Trim the trailing \n */
732 q = strrchr(p, '\n');
733 if (q != NULL)
734 *q = '\0';
Erik Andersen7dc16072000-01-04 01:10:25 +0000735
Erik Andersene49d5ec2000-02-08 19:58:47 +0000736 /* Keep a copy around for posterity's sake (and error msgs) */
737 strcpy(lineAsRead, buf);
738
739 /* Grab the ID field */
740 s = p;
741 p = strchr(p, ':');
742 if (p != NULL || *(p + 1) != '\0') {
743 *p = '\0';
744 ++p;
Erik Andersen0e3782f2000-01-07 02:54:55 +0000745 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000746
747 /* Now peal off the process field from the end
748 * of the string */
749 q = strrchr(p, ':');
750 if (q == NULL || *(q + 1) == '\0') {
751 message(LOG | CONSOLE, "Bad inittab entry: %s\n", lineAsRead);
752 continue;
753 } else {
754 *q = '\0';
755 ++q;
756 }
757
758 /* Now peal off the action field */
759 r = strrchr(p, ':');
760 if (r == NULL || *(r + 1) == '\0') {
761 message(LOG | CONSOLE, "Bad inittab entry: %s\n", lineAsRead);
762 continue;
763 } else {
764 ++r;
765 }
766
767 /* Ok, now process it */
768 a = actions;
769 while (a->name != 0) {
770 if (strcmp(a->name, r) == 0) {
771 if (*s != '\0') {
772 struct stat statBuf;
773
774 strcpy(tmpConsole, "/dev/");
775 strncat(tmpConsole, s, 200);
776 if (stat(tmpConsole, &statBuf) != 0) {
777 message(LOG | CONSOLE,
778 "device '%s' does not exist. Did you read the directions?\n",
779 tmpConsole);
780 break;
781 }
782 s = tmpConsole;
783 }
784 new_initAction(a->action, q, s);
785 foundIt = TRUE;
786 }
787 a++;
788 }
789 if (foundIt == TRUE)
790 continue;
791 else {
792 /* Choke on an unknown action */
793 message(LOG | CONSOLE, "Bad inittab entry: %s\n", lineAsRead);
794 }
Erik Andersen7dc16072000-01-04 01:10:25 +0000795 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000796 return;
Erik Andersen9e737252000-01-06 01:16:13 +0000797#endif
Erik Andersen7dc16072000-01-04 01:10:25 +0000798}
799
Erik Andersene132f4b2000-02-09 04:16:43 +0000800
801
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000802extern int init_main(int argc, char **argv)
Eric Andersen0460ff21999-10-25 23:32:44 +0000803{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000804 initAction *a;
805 pid_t wpid;
806 int status;
Eric Andersen0460ff21999-10-25 23:32:44 +0000807
Eric Andersend73dc5b1999-11-10 23:13:02 +0000808#ifndef DEBUG_INIT
Erik Andersene49d5ec2000-02-08 19:58:47 +0000809 /* Expect to be PID 1 if we are run as init (not linuxrc) */
810 if (getpid() != 1 && strstr(argv[0], "init") != NULL) {
811 usage("init\n\nInit is the parent of all processes.\n\n"
812 "This version of init is designed to be run only by the kernel\n");
813 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000814 /* Set up sig handlers -- be sure to
815 * clear all of these in run() */
816 signal(SIGUSR1, halt_signal);
817 signal(SIGUSR2, reboot_signal);
818 signal(SIGINT, reboot_signal);
819 signal(SIGTERM, reboot_signal);
Erik Andersende552872000-01-23 01:34:05 +0000820#if defined BB_FEATURE_INIT_CHROOT
Erik Andersene49d5ec2000-02-08 19:58:47 +0000821 signal(SIGHUP, check_chroot);
Erik Andersende552872000-01-23 01:34:05 +0000822#endif
Eric Andersen0460ff21999-10-25 23:32:44 +0000823
Erik Andersene49d5ec2000-02-08 19:58:47 +0000824 /* Turn off rebooting via CTL-ALT-DEL -- we get a
825 * SIGINT on CAD so we can shut things down gracefully... */
826 reboot(RB_DISABLE_CAD);
827#endif
Erik Andersen05df2392000-01-13 04:43:48 +0000828
Erik Andersene49d5ec2000-02-08 19:58:47 +0000829 /* Figure out where the default console should be */
830 console_init();
Eric Andersen0460ff21999-10-25 23:32:44 +0000831
Erik Andersene49d5ec2000-02-08 19:58:47 +0000832 /* Close whatever files are open, and reset the console. */
833 close(0);
834 close(1);
835 close(2);
836 set_term(0);
837 setsid();
Eric Andersencc8ed391999-10-05 16:24:54 +0000838
Erik Andersene49d5ec2000-02-08 19:58:47 +0000839 /* Make sure PATH is set to something sane */
840 putenv(_PATH_STDPATH);
Eric Andersencc8ed391999-10-05 16:24:54 +0000841
Erik Andersene49d5ec2000-02-08 19:58:47 +0000842 /* Hello world */
Eric Andersen7f1acfd1999-10-29 23:09:13 +0000843#ifndef DEBUG_INIT
Erik Andersene2729152000-02-18 21:34:17 +0000844 message(LOG,
Erik Andersene49d5ec2000-02-08 19:58:47 +0000845 "init started: BusyBox v%s (%s) multi-call binary\r\n",
846 BB_VER, BB_BT);
Eric Andersen7f1acfd1999-10-29 23:09:13 +0000847#else
Erik Andersene2729152000-02-18 21:34:17 +0000848 message(LOG,
Erik Andersene49d5ec2000-02-08 19:58:47 +0000849 "init(%d) started: BusyBox v%s (%s) multi-call binary\r\n",
850 getpid(), BB_VER, BB_BT);
Eric Andersen7f1acfd1999-10-29 23:09:13 +0000851#endif
Eric Andersencc8ed391999-10-05 16:24:54 +0000852
Eric Andersencc8ed391999-10-05 16:24:54 +0000853
Erik Andersene49d5ec2000-02-08 19:58:47 +0000854 /* Mount /proc */
855 if (mount("proc", "/proc", "proc", 0, 0) == 0) {
Erik Andersene2729152000-02-18 21:34:17 +0000856 message(LOG, "Mounting /proc: done.\n");
Erik Andersene49d5ec2000-02-08 19:58:47 +0000857 kernelVersion = get_kernel_revision();
858 } else
859 message(LOG | CONSOLE, "Mounting /proc: failed!\n");
Eric Andersencc8ed391999-10-05 16:24:54 +0000860
Erik Andersene49d5ec2000-02-08 19:58:47 +0000861 /* Make sure there is enough memory to do something useful. */
862 check_memory();
Erik Andersen9e737252000-01-06 01:16:13 +0000863
Erik Andersene49d5ec2000-02-08 19:58:47 +0000864 /* Check if we are supposed to be in single user mode */
865 if (argc > 1 && (!strcmp(argv[1], "single") ||
866 !strcmp(argv[1], "-s") || !strcmp(argv[1], "1"))) {
867 /* Ask first then start a shell on tty2 */
868 if (secondConsole != NULL)
869 new_initAction(ASKFIRST, SHELL, secondConsole);
870 /* Start a shell on tty1 */
871 new_initAction(RESPAWN, SHELL, console);
872 } else {
873 /* Not in single user mode -- see what inittab says */
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000874
Erik Andersene49d5ec2000-02-08 19:58:47 +0000875 /* NOTE that if BB_FEATURE_USE_INITTAB is NOT defined,
876 * then parse_inittab() simply adds in some default
877 * actions(i.e runs INIT_SCRIPT and then starts a pair
878 * of "askfirst" shells */
879 parse_inittab();
Eric Andersend00c2621999-12-07 08:37:31 +0000880 }
Erik Andersene132f4b2000-02-09 04:16:43 +0000881
882 /* Fix up argv[0] to be certain we claim to be init */
883 strncpy(argv[0], "init", strlen(argv[0])+1);
Erik Andersen07f56042000-02-09 06:05:01 +0000884 if (argc > 1)
885 strncpy(argv[1], "\0", strlen(argv[1])+1);
Eric Andersen219d6f51999-11-02 19:43:01 +0000886
Erik Andersene49d5ec2000-02-08 19:58:47 +0000887 /* Now run everything that needs to be run */
888
889 /* First run the sysinit command */
890 for (a = initActionList; a; a = a->nextPtr) {
891 if (a->action == SYSINIT) {
892 waitfor(a->process, a->console, FALSE);
893 /* Now remove the "sysinit" entry from the list */
894 delete_initAction(a);
Erik Andersen7dc16072000-01-04 01:10:25 +0000895 }
Erik Andersen7dc16072000-01-04 01:10:25 +0000896 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000897 /* Next run anything that wants to block */
898 for (a = initActionList; a; a = a->nextPtr) {
899 if (a->action == WAIT) {
900 waitfor(a->process, a->console, FALSE);
901 /* Now remove the "wait" entry from the list */
902 delete_initAction(a);
Erik Andersen7dc16072000-01-04 01:10:25 +0000903 }
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000904 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000905 /* Next run anything to be run only once */
906 for (a = initActionList; a; a = a->nextPtr) {
907 if (a->action == ONCE) {
908 run(a->process, a->console, FALSE);
909 /* Now remove the "once" entry from the list */
910 delete_initAction(a);
911 }
912 }
913 /* If there is nothing else to do, stop */
914 if (initActionList == NULL) {
915 message(LOG | CONSOLE,
916 "No more tasks for init -- sleeping forever.\n");
917 while (1)
918 sleep(1);
919 }
920
921 /* Now run the looping stuff for the rest of forever */
922 while (1) {
923 for (a = initActionList; a; a = a->nextPtr) {
924 /* Only run stuff with pid==0. If they have
925 * a pid, that means they are still running */
926 if (a->pid == 0) {
927 switch (a->action) {
928 case RESPAWN:
929 /* run the respawn stuff */
930 a->pid = run(a->process, a->console, FALSE);
931 break;
932 case ASKFIRST:
933 /* run the askfirst stuff */
934 a->pid = run(a->process, a->console, TRUE);
935 break;
936 /* silence the compiler's incessant whining */
937 default:
938 break;
939 }
940 }
941 }
942 /* Wait for a child process to exit */
943 wpid = wait(&status);
944 if (wpid > 0) {
945 /* Find out who died and clean up their corpse */
946 for (a = initActionList; a; a = a->nextPtr) {
947 if (a->pid == wpid) {
948 a->pid = 0;
949 message(LOG,
950 "Process '%s' (pid %d) exited. Scheduling it for restart.\n",
951 a->process, wpid);
952 }
953 }
954 }
955 sleep(1);
956 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000957}