blob: 6dad7181b47376599a38626f465612861fe47b36 [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
Eric Andersencc8ed391999-10-05 16:24:54 +000025#include "internal.h"
26#include <stdio.h>
Erik Andersen7dc16072000-01-04 01:10:25 +000027#include <string.h>
Eric Andersencc8ed391999-10-05 16:24:54 +000028#include <stdlib.h>
29#include <stdarg.h>
30#include <unistd.h>
31#include <errno.h>
32#include <signal.h>
33#include <termios.h>
Eric Andersenb186d981999-12-03 09:19:54 +000034#include <paths.h>
Eric Andersencc8ed391999-10-05 16:24:54 +000035#include <sys/types.h>
36#include <sys/fcntl.h>
37#include <sys/wait.h>
38#include <string.h>
39#include <sys/mount.h>
40#include <sys/reboot.h>
41#include <sys/kdaemon.h>
Eric Andersencc8ed391999-10-05 16:24:54 +000042#include <sys/sysmacros.h>
Erik Andersenfac10d72000-02-07 05:29:42 +000043#include <asm/types.h>
Eric Andersen8a8fbb81999-10-26 00:18:56 +000044#include <linux/serial.h> /* for serial_struct */
45#include <sys/vt.h> /* for vt_stat */
Eric Andersenabc7d591999-10-19 00:27:50 +000046#include <sys/ioctl.h>
Erik Andersen4d1d0111999-12-17 18:44:15 +000047#include <linux/version.h>
Eric Andersen4c781471999-11-26 08:12:56 +000048#ifdef BB_SYSLOGD
49#include <sys/syslog.h>
50#endif
Eric Andersencc8ed391999-10-05 16:24:54 +000051
Eric Andersen0ecb54a1999-12-05 23:24:55 +000052#if ! defined BB_FEATURE_USE_PROCFS
53#error Sorry, I depend on the /proc filesystem right now.
54#endif
55
Erik Andersen4d1d0111999-12-17 18:44:15 +000056#ifndef KERNEL_VERSION
57#define KERNEL_VERSION(a,b,c) (((a) << 16) + ((b) << 8) + (c))
58#endif
59
Eric Andersen0ecb54a1999-12-05 23:24:55 +000060
Erik Andersen7dc16072000-01-04 01:10:25 +000061#define VT_PRIMARY "/dev/tty1" /* Primary virtual console */
62#define VT_SECONDARY "/dev/tty2" /* Virtual console */
63#define VT_LOG "/dev/tty3" /* Virtual console */
64#define SERIAL_CON0 "/dev/ttyS0" /* Primary serial console */
65#define SERIAL_CON1 "/dev/ttyS1" /* Serial console */
Erik Andersen96e2abd2000-01-07 11:40:44 +000066#define SHELL "/bin/sh" /* Default shell */
Erik Andersen7dc16072000-01-04 01:10:25 +000067#define INITTAB "/etc/inittab" /* inittab file location */
Erik Andersen96e2abd2000-01-07 11:40:44 +000068#ifndef INIT_SCRIPT
Erik Andersen7dc16072000-01-04 01:10:25 +000069#define INIT_SCRIPT "/etc/init.d/rcS" /* Default sysinit script. */
Erik Andersen96e2abd2000-01-07 11:40:44 +000070#endif
Erik Andersen9c88cac1999-12-30 09:25:17 +000071
Eric Andersen3ae0c781999-11-04 01:13:21 +000072#define LOG 0x1
73#define CONSOLE 0x2
Erik Andersen7dc16072000-01-04 01:10:25 +000074
75/* Allowed init action types */
76typedef enum {
77 SYSINIT=1,
Erik Andersen7dc16072000-01-04 01:10:25 +000078 RESPAWN,
79 ASKFIRST,
80 WAIT,
81 ONCE
82} initActionEnum;
83
84/* And now a list of the actions we support in the version of init */
85typedef struct initActionType{
86 const char* name;
87 initActionEnum action;
88} initActionType;
89
90static const struct initActionType actions[] = {
91 {"sysinit", SYSINIT},
Erik Andersen7dc16072000-01-04 01:10:25 +000092 {"respawn", RESPAWN},
93 {"askfirst", ASKFIRST},
94 {"wait", WAIT},
95 {"once", ONCE},
96 {0}
97};
98
99/* Set up a linked list of initactions, to be read from inittab */
100typedef struct initActionTag initAction;
101struct initActionTag {
102 pid_t pid;
103 char process[256];
Erik Andersen9e737252000-01-06 01:16:13 +0000104 char console[256];
Erik Andersen7dc16072000-01-04 01:10:25 +0000105 initAction *nextPtr;
106 initActionEnum action;
107};
108initAction* initActionList = NULL;
109
110
Erik Andersenac6e71f2000-01-08 22:04:33 +0000111static char *secondConsole = VT_SECONDARY;
Eric Andersenbe971d61999-11-03 16:52:50 +0000112static char *log = VT_LOG;
Erik Andersenac6e71f2000-01-08 22:04:33 +0000113static int kernelVersion = 0;
Erik Andersen31638212000-01-15 22:28:50 +0000114static char termType[32] = "TERM=ansi";
115static char console[32] = _PATH_CONSOLE;
Eric Andersen0460ff21999-10-25 23:32:44 +0000116
117
Eric Andersen3ae0c781999-11-04 01:13:21 +0000118/* print a message to the specified device:
119 * device may be bitwise-or'd from LOG | CONSOLE */
120void message(int device, char *fmt, ...)
Eric Andersen0460ff21999-10-25 23:32:44 +0000121{
Eric Andersen0460ff21999-10-25 23:32:44 +0000122 va_list arguments;
Erik Andersen7dc16072000-01-04 01:10:25 +0000123 int fd;
124
Eric Andersen4c781471999-11-26 08:12:56 +0000125#ifdef BB_SYSLOGD
126
127 /* Log the message to syslogd */
128 if (device & LOG ) {
129 char msg[1024];
130 va_start(arguments, fmt);
131 vsnprintf(msg, sizeof(msg), fmt, arguments);
132 va_end(arguments);
Erik Andersende552872000-01-23 01:34:05 +0000133 openlog( "init", 0, LOG_DAEMON);
Eric Andersen4c781471999-11-26 08:12:56 +0000134 syslog(LOG_DAEMON|LOG_NOTICE, msg);
Erik Andersende552872000-01-23 01:34:05 +0000135 closelog();
Eric Andersen4c781471999-11-26 08:12:56 +0000136 }
137
138#else
Erik Andersen04e97022000-01-29 07:06:24 +0000139 static int log_fd = -1;
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000140
Eric Andersen3ae0c781999-11-04 01:13:21 +0000141 /* Take full control of the log tty, and never close it.
142 * It's mine, all mine! Muhahahaha! */
Eric Andersen07e52971999-11-07 07:38:08 +0000143 if (log_fd < 0) {
144 if (log == NULL) {
145 /* don't even try to log, because there is no such console */
146 log_fd = -2;
147 /* log to main console instead */
148 device = CONSOLE;
149 }
150 else if ((log_fd = device_open(log, O_RDWR|O_NDELAY)) < 0) {
Erik Andersen04e97022000-01-29 07:06:24 +0000151 log_fd = -1;
Eric Andersen3ae0c781999-11-04 01:13:21 +0000152 fprintf(stderr, "Bummer, can't write to log on %s!\r\n", log);
153 fflush(stderr);
154 return;
155 }
156 }
Eric Andersen07e52971999-11-07 07:38:08 +0000157 if ( (device & LOG) && (log_fd >= 0) ) {
Eric Andersen0460ff21999-10-25 23:32:44 +0000158 va_start(arguments, fmt);
Eric Andersen3ae0c781999-11-04 01:13:21 +0000159 vdprintf(log_fd, fmt, arguments);
Eric Andersena7456061999-10-27 02:31:32 +0000160 va_end(arguments);
161 }
Eric Andersen4c781471999-11-26 08:12:56 +0000162#endif
163
Eric Andersen3ae0c781999-11-04 01:13:21 +0000164 if (device & CONSOLE) {
Eric Andersended62591999-11-18 00:19:26 +0000165 /* Always send console messages to /dev/console so people will see them. */
Eric Andersenb186d981999-12-03 09:19:54 +0000166 if ((fd = device_open(_PATH_CONSOLE, O_WRONLY|O_NOCTTY|O_NDELAY)) >= 0) {
Eric Andersen3ae0c781999-11-04 01:13:21 +0000167 va_start(arguments, fmt);
168 vdprintf(fd, fmt, arguments);
169 va_end(arguments);
170 close(fd);
171 } else {
172 fprintf(stderr, "Bummer, can't print: ");
173 va_start(arguments, fmt);
174 vfprintf(stderr, fmt, arguments);
175 fflush(stderr);
176 va_end(arguments);
177 }
178 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000179}
180
Eric Andersen3ae0c781999-11-04 01:13:21 +0000181
Eric Andersen0460ff21999-10-25 23:32:44 +0000182/* Set terminal settings to reasonable defaults */
Eric Andersenc7c41d31999-10-28 00:24:35 +0000183void set_term( int fd)
Eric Andersencc8ed391999-10-05 16:24:54 +0000184{
Eric Andersen0460ff21999-10-25 23:32:44 +0000185 struct termios tty;
Eric Andersen08b10341999-11-19 02:38:58 +0000186 static const char control_characters[] = {
187 '\003', '\034', '\177', '\025', '\004', '\0',
188 '\1', '\0', '\021', '\023', '\032', '\0', '\022',
189 '\017', '\027', '\026', '\0'
190 };
Eric Andersencc8ed391999-10-05 16:24:54 +0000191
Eric Andersenc7c41d31999-10-28 00:24:35 +0000192 tcgetattr(fd, &tty);
Eric Andersencc8ed391999-10-05 16:24:54 +0000193
Eric Andersen08b10341999-11-19 02:38:58 +0000194 /* set control chars */
195 memcpy(tty.c_cc, control_characters, sizeof(control_characters));
Eric Andersencc8ed391999-10-05 16:24:54 +0000196
Eric Andersen219d6f51999-11-02 19:43:01 +0000197 /* use line dicipline 0 */
Eric Andersen7f1acfd1999-10-29 23:09:13 +0000198 tty.c_line = 0;
199
Eric Andersen08b10341999-11-19 02:38:58 +0000200 /* Make it be sane */
201 //tty.c_cflag &= CBAUD|CBAUDEX|CSIZE|CSTOPB|PARENB|PARODD;
202 //tty.c_cflag |= HUPCL|CLOCAL;
203
204 /* input modes */
205 tty.c_iflag = ICRNL|IXON|IXOFF;
206
Eric Andersen7f1acfd1999-10-29 23:09:13 +0000207 /* output modes */
Eric Andersenc7c41d31999-10-28 00:24:35 +0000208 tty.c_oflag = OPOST|ONLCR;
Eric Andersen7f1acfd1999-10-29 23:09:13 +0000209
210 /* local modes */
Eric Andersen08b10341999-11-19 02:38:58 +0000211 tty.c_lflag = ISIG|ICANON|ECHO|ECHOE|ECHOK|ECHOCTL|ECHOKE|IEXTEN;
Eric Andersencc8ed391999-10-05 16:24:54 +0000212
Eric Andersenc7c41d31999-10-28 00:24:35 +0000213 tcsetattr(fd, TCSANOW, &tty);
Eric Andersencc8ed391999-10-05 16:24:54 +0000214}
215
Eric Andersen219d6f51999-11-02 19:43:01 +0000216/* How much memory does this machine have? */
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000217static int mem_total()
Eric Andersencc8ed391999-10-05 16:24:54 +0000218{
Eric Andersenabc7d591999-10-19 00:27:50 +0000219 char s[80];
Eric Andersen219d6f51999-11-02 19:43:01 +0000220 char *p = "/proc/meminfo";
Eric Andersenabc7d591999-10-19 00:27:50 +0000221 FILE *f;
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000222 const char pattern[] = "MemTotal:";
Eric Andersencc8ed391999-10-05 16:24:54 +0000223
Eric Andersen219d6f51999-11-02 19:43:01 +0000224 if ((f = fopen(p, "r")) < 0) {
Eric Andersen3ae0c781999-11-04 01:13:21 +0000225 message(LOG, "Error opening %s: %s\n", p, strerror( errno));
Eric Andersen219d6f51999-11-02 19:43:01 +0000226 return -1;
227 }
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000228 while (NULL != fgets(s, 79, f)) {
229 p = strstr(s, pattern);
Eric Andersenabc7d591999-10-19 00:27:50 +0000230 if (NULL != p) {
231 fclose(f);
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000232 return (atoi(p + strlen(pattern)));
Eric Andersenabc7d591999-10-19 00:27:50 +0000233 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000234 }
Eric Andersenabc7d591999-10-19 00:27:50 +0000235 return -1;
Eric Andersencc8ed391999-10-05 16:24:54 +0000236}
237
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000238static void console_init()
Eric Andersen0460ff21999-10-25 23:32:44 +0000239{
240 int fd;
241 int tried_devcons = 0;
Eric Andersen219d6f51999-11-02 19:43:01 +0000242 int tried_vtprimary = 0;
Eric Andersen07e52971999-11-07 07:38:08 +0000243 struct serial_struct sr;
Eric Andersen0460ff21999-10-25 23:32:44 +0000244 char *s;
245
Erik Andersen31638212000-01-15 22:28:50 +0000246 if ((s = getenv("TERM")) != NULL) {
247 snprintf(termType,sizeof(termType)-1,"TERM=%s",s);
Erik Andersenac6e71f2000-01-08 22:04:33 +0000248 }
249
250 if ((s = getenv("CONSOLE")) != NULL) {
Erik Andersen31638212000-01-15 22:28:50 +0000251 snprintf(console, sizeof(console)-1, "%s",s);
Eric Andersen07e52971999-11-07 07:38:08 +0000252 }
Eric Andersenfbb39c81999-11-08 17:00:52 +0000253#if #cpu(sparc)
Eric Andersen07e52971999-11-07 07:38:08 +0000254 /* sparc kernel supports console=tty[ab] parameter which is also
255 * passed to init, so catch it here */
Erik Andersende552872000-01-23 01:34:05 +0000256 else if ((s = getenv("console")) != NULL) {
Eric Andersen07e52971999-11-07 07:38:08 +0000257 /* remap tty[ab] to /dev/ttyS[01] */
258 if (strcmp( s, "ttya" )==0)
Erik Andersen31638212000-01-15 22:28:50 +0000259 snprintf(console, sizeof(console)-1, "%s", SERIAL_CON0);
Eric Andersen07e52971999-11-07 07:38:08 +0000260 else if (strcmp( s, "ttyb" )==0)
Erik Andersen31638212000-01-15 22:28:50 +0000261 snprintf(console, sizeof(console)-1, "%s", SERIAL_CON1);
Eric Andersen07e52971999-11-07 07:38:08 +0000262 }
Eric Andersen219d6f51999-11-02 19:43:01 +0000263#endif
Eric Andersen07e52971999-11-07 07:38:08 +0000264 else {
265 struct vt_stat vt;
Eric Andersen07e52971999-11-07 07:38:08 +0000266
Eric Andersen07e52971999-11-07 07:38:08 +0000267 /* 2.2 kernels: identify the real console backend and try to use it */
Eric Andersen08b10341999-11-19 02:38:58 +0000268 if (ioctl(0, TIOCGSERIAL, &sr) == 0) {
Eric Andersen07e52971999-11-07 07:38:08 +0000269 /* this is a serial console */
Erik Andersen31638212000-01-15 22:28:50 +0000270 snprintf(console, sizeof(console)-1, "/dev/ttyS%d", sr.line);
Eric Andersen07e52971999-11-07 07:38:08 +0000271 }
272 else if (ioctl(0, VT_GETSTATE, &vt) == 0) {
273 /* this is linux virtual tty */
Erik Andersen31638212000-01-15 22:28:50 +0000274 snprintf(console, sizeof(console)-1, "/dev/tty%d", vt.v_active);
Eric Andersen07e52971999-11-07 07:38:08 +0000275 } else {
Erik Andersen31638212000-01-15 22:28:50 +0000276 snprintf(console, sizeof(console)-1, "%s", _PATH_CONSOLE);
Eric Andersen07e52971999-11-07 07:38:08 +0000277 tried_devcons++;
278 }
Eric Andersen0460ff21999-10-25 23:32:44 +0000279 }
Eric Andersena7456061999-10-27 02:31:32 +0000280
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000281 while ((fd = open(console, O_RDONLY | O_NONBLOCK)) < 0) {
Eric Andersen219d6f51999-11-02 19:43:01 +0000282 /* Can't open selected console -- try /dev/console */
Eric Andersen0460ff21999-10-25 23:32:44 +0000283 if (!tried_devcons) {
284 tried_devcons++;
Erik Andersen31638212000-01-15 22:28:50 +0000285 snprintf(console, sizeof(console)-1, "%s", _PATH_CONSOLE);
Eric Andersen0460ff21999-10-25 23:32:44 +0000286 continue;
287 }
Eric Andersenbe971d61999-11-03 16:52:50 +0000288 /* Can't open selected console -- try vt1 */
289 if (!tried_vtprimary) {
290 tried_vtprimary++;
Erik Andersen31638212000-01-15 22:28:50 +0000291 snprintf(console, sizeof(console)-1, "%s", VT_PRIMARY);
Eric Andersenbe971d61999-11-03 16:52:50 +0000292 continue;
293 }
Eric Andersen0460ff21999-10-25 23:32:44 +0000294 break;
295 }
Erik Andersen31638212000-01-15 22:28:50 +0000296 if (fd < 0) {
Eric Andersen219d6f51999-11-02 19:43:01 +0000297 /* Perhaps we should panic here? */
Erik Andersen31638212000-01-15 22:28:50 +0000298 snprintf(console, sizeof(console)-1, "/dev/null");
299 } else {
Eric Andersen07e52971999-11-07 07:38:08 +0000300 /* check for serial console and disable logging to tty3 & running a
301 * shell to tty2 */
302 if (ioctl(0,TIOCGSERIAL,&sr) == 0) {
Erik Andersen31638212000-01-15 22:28:50 +0000303 message(LOG|CONSOLE, "serial console detected. Disabling virtual terminals.\r\n" );
Eric Andersen07e52971999-11-07 07:38:08 +0000304 log = NULL;
Erik Andersenac6e71f2000-01-08 22:04:33 +0000305 secondConsole = NULL;
Eric Andersen07e52971999-11-07 07:38:08 +0000306 }
Eric Andersen0460ff21999-10-25 23:32:44 +0000307 close(fd);
Eric Andersen07e52971999-11-07 07:38:08 +0000308 }
Eric Andersen3ae0c781999-11-04 01:13:21 +0000309 message(LOG, "console=%s\n", console );
Eric Andersen0460ff21999-10-25 23:32:44 +0000310}
311
Erik Andersen7dc16072000-01-04 01:10:25 +0000312static pid_t run(char* command,
Eric Andersenc7c41d31999-10-28 00:24:35 +0000313 char *terminal, int get_enter)
Eric Andersen0460ff21999-10-25 23:32:44 +0000314{
Erik Andersen05df2392000-01-13 04:43:48 +0000315 int i, fd;
Eric Andersenc7c41d31999-10-28 00:24:35 +0000316 pid_t pid;
Erik Andersen7dc16072000-01-04 01:10:25 +0000317 char* tmpCmd;
318 char* cmd[255];
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000319 static const char press_enter[] =
Eric Andersen0460ff21999-10-25 23:32:44 +0000320 "\nPlease press Enter to activate this console. ";
Erik Andersen31638212000-01-15 22:28:50 +0000321 char* environment[] = {
Erik Andersenac6e71f2000-01-08 22:04:33 +0000322 "HOME=/",
323 "PATH=/usr/bin:/bin:/usr/sbin:/sbin",
324 "SHELL=/bin/sh",
Erik Andersen31638212000-01-15 22:28:50 +0000325 termType,
Erik Andersenac6e71f2000-01-08 22:04:33 +0000326 "USER=root",
327 0
328 };
Erik Andersenac6e71f2000-01-08 22:04:33 +0000329
Eric Andersen0460ff21999-10-25 23:32:44 +0000330
Eric Andersen0460ff21999-10-25 23:32:44 +0000331 if ((pid = fork()) == 0) {
Erik Andersen0881de72000-01-05 09:34:26 +0000332 pid_t shell_pgid = getpid ();
333
Eric Andersen0460ff21999-10-25 23:32:44 +0000334 /* Clean up */
335 close(0);
336 close(1);
337 close(2);
338 setsid();
339
Eric Andersen2f6c04f1999-11-01 23:59:44 +0000340 /* Reset signal handlers set for parent process */
341 signal(SIGUSR1, SIG_DFL);
342 signal(SIGUSR2, SIG_DFL);
343 signal(SIGINT, SIG_DFL);
344 signal(SIGTERM, SIG_DFL);
Erik Andersen5cbdd712000-01-26 20:06:48 +0000345 signal(SIGHUP, SIG_DFL);
Eric Andersen2f6c04f1999-11-01 23:59:44 +0000346
Erik Andersen05df2392000-01-13 04:43:48 +0000347 if ((fd = device_open(terminal, O_RDWR)) < 0) {
348 message(LOG|CONSOLE, "Bummer, can't open %s\r\n", terminal);
349 exit(1);
350 }
Erik Andersena6d0dbc2000-01-29 06:29:32 +0000351 dup2(fd, 0);
352 dup2(fd, 1);
353 dup2(fd, 2);
Erik Andersen05df2392000-01-13 04:43:48 +0000354 tcsetpgrp (0, getpgrp());
355 set_term(0);
Eric Andersen0460ff21999-10-25 23:32:44 +0000356
Eric Andersen2f6c04f1999-11-01 23:59:44 +0000357 if (get_enter==TRUE) {
Eric Andersen0460ff21999-10-25 23:32:44 +0000358 /*
359 * Save memory by not exec-ing anything large (like a shell)
360 * before the user wants it. This is critical if swap is not
361 * enabled and the system has low memory. Generally this will
362 * be run on the second virtual console, and the first will
363 * be allowed to start a shell or whatever an init script
364 * specifies.
365 */
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000366 char c;
Eric Andersen3ae0c781999-11-04 01:13:21 +0000367 message(LOG, "Waiting for enter to start '%s' (pid %d, console %s)\r\n",
Erik Andersen0881de72000-01-05 09:34:26 +0000368 command, shell_pgid, terminal );
Erik Andersen7dc16072000-01-04 01:10:25 +0000369 write(fileno(stdout), press_enter, sizeof(press_enter) - 1);
370 read(fileno(stdin), &c, 1);
Eric Andersen0460ff21999-10-25 23:32:44 +0000371 }
372
Erik Andersen05df2392000-01-13 04:43:48 +0000373 /* Log the process name and args */
Erik Andersen31638212000-01-15 22:28:50 +0000374 message(LOG, "Starting pid %d, console %s: '",
Erik Andersen05df2392000-01-13 04:43:48 +0000375 shell_pgid, terminal, command);
376
Erik Andersen7dc16072000-01-04 01:10:25 +0000377 /* Convert command (char*) into cmd (char**, one word per string) */
378 for (tmpCmd=command, i=0; (tmpCmd=strsep(&command, " \t")) != NULL;) {
379 if (*tmpCmd != '\0') {
380 cmd[i] = tmpCmd;
Erik Andersen31638212000-01-15 22:28:50 +0000381 message(LOG, "%s ", tmpCmd);
Erik Andersen7dc16072000-01-04 01:10:25 +0000382 tmpCmd++;
383 i++;
384 }
385 }
386 cmd[i] = NULL;
Erik Andersen31638212000-01-15 22:28:50 +0000387 message(LOG, "'\r\n");
Erik Andersen7dc16072000-01-04 01:10:25 +0000388
Eric Andersenc7c41d31999-10-28 00:24:35 +0000389 /* Now run it. The new program will take over this PID,
Eric Andersena7456061999-10-27 02:31:32 +0000390 * so nothing further in init.c should be run. */
Erik Andersenac6e71f2000-01-08 22:04:33 +0000391 execve(cmd[0], cmd, environment);
Eric Andersen0460ff21999-10-25 23:32:44 +0000392
Erik Andersen7dc16072000-01-04 01:10:25 +0000393 /* We're still here? Some error happened. */
394 message(LOG|CONSOLE, "Bummer, could not run '%s': %s\n", cmd[0],
395 strerror(errno));
Eric Andersen0460ff21999-10-25 23:32:44 +0000396 exit(-1);
397 }
398 return pid;
399}
400
Erik Andersen0e3782f2000-01-07 02:54:55 +0000401static int waitfor(char* command,
402 char *terminal, int get_enter)
403{
404 int status, wpid;
405 int pid = run( command, terminal, get_enter);
406
407 while (1) {
408 wpid = wait(&status);
409 if (wpid > 0 ) {
410 message(LOG, "Process '%s' (pid %d) exited.\n",
411 command, wpid);
412 break;
413 }
414 if (wpid == pid )
415 break;
416 }
417 return wpid;
418}
419
Eric Andersen219d6f51999-11-02 19:43:01 +0000420/* Make sure there is enough memory to do something useful. *
421 * Calls swapon if needed so be sure /proc is mounted. */
422static void check_memory()
423{
Erik Andersen0e3782f2000-01-07 02:54:55 +0000424 struct stat statBuf;
Eric Andersen219d6f51999-11-02 19:43:01 +0000425
426 if (mem_total() > 3500)
427 return;
428
Erik Andersen0e3782f2000-01-07 02:54:55 +0000429 if (stat("/etc/fstab", &statBuf) == 0) {
Eric Andersen219d6f51999-11-02 19:43:01 +0000430 /* Try to turn on swap */
Erik Andersen0e3782f2000-01-07 02:54:55 +0000431 waitfor("/bin/swapon swapon -a", log, FALSE);
Eric Andersen219d6f51999-11-02 19:43:01 +0000432 if (mem_total() < 3500)
433 goto goodnight;
434 } else
435 goto goodnight;
436 return;
437
438goodnight:
Eric Andersen3ae0c781999-11-04 01:13:21 +0000439 message(CONSOLE, "Sorry, your computer does not have enough memory.\r\n");
Eric Andersen219d6f51999-11-02 19:43:01 +0000440 while (1) sleep(1);
441}
442
Erik Andersen7dc16072000-01-04 01:10:25 +0000443#ifndef DEBUG_INIT
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000444static void shutdown_system(void)
Eric Andersencc8ed391999-10-05 16:24:54 +0000445{
Erik Andersen5cbdd712000-01-26 20:06:48 +0000446 /* first disable our SIGHUP signal */
447 signal(SIGHUP, SIG_DFL);
448
Eric Andersen0460ff21999-10-25 23:32:44 +0000449 /* Allow Ctrl-Alt-Del to reboot system. */
450 reboot(RB_ENABLE_CAD);
Eric Andersen08b10341999-11-19 02:38:58 +0000451 message(CONSOLE, "\r\nThe system is going down NOW !!\r\n");
Eric Andersen3ae0c781999-11-04 01:13:21 +0000452 sync();
Erik Andersen7dc16072000-01-04 01:10:25 +0000453
Eric Andersen0460ff21999-10-25 23:32:44 +0000454 /* Send signals to every process _except_ pid 1 */
Erik Andersenfac10d72000-02-07 05:29:42 +0000455 message(CONSOLE, "Sending SIGTERM to all processes.\r\n");
456 kill(-1, SIGTERM);
457 sleep(5);
Eric Andersen0460ff21999-10-25 23:32:44 +0000458 sync();
Erik Andersen7dc16072000-01-04 01:10:25 +0000459
Eric Andersen3ae0c781999-11-04 01:13:21 +0000460 message(CONSOLE, "Sending SIGKILL to all processes.\r\n");
Eric Andersen0460ff21999-10-25 23:32:44 +0000461 kill(-1, SIGKILL);
Erik Andersenfac10d72000-02-07 05:29:42 +0000462 sleep(5);
Erik Andersen7dc16072000-01-04 01:10:25 +0000463
Eric Andersen08b10341999-11-19 02:38:58 +0000464 message(CONSOLE, "Disabling swap.\r\n");
Erik Andersenfac10d72000-02-07 05:29:42 +0000465 waitfor( "swapoff -a", console, FALSE);
Eric Andersen08b10341999-11-19 02:38:58 +0000466 message(CONSOLE, "Unmounting filesystems.\r\n");
Erik Andersenfac10d72000-02-07 05:29:42 +0000467 waitfor("umount -a -r", console, FALSE);
Eric Andersen0460ff21999-10-25 23:32:44 +0000468 sync();
Erik Andersenac6e71f2000-01-08 22:04:33 +0000469 if (kernelVersion > 0 && kernelVersion <= 2 * 65536 + 2 * 256 + 11) {
Eric Andersencf8c9cf1999-11-05 00:31:46 +0000470 /* bdflush, kupdate not needed for kernels >2.2.11 */
471 bdflush(1, 0);
472 sync();
473 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000474}
475
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000476static void halt_signal(int sig)
Eric Andersencc8ed391999-10-05 16:24:54 +0000477{
Eric Andersenabc7d591999-10-19 00:27:50 +0000478 shutdown_system();
Erik Andersen3fe39dc2000-01-25 18:13:53 +0000479 message(CONSOLE, "The system is halted. Press %s or turn off power\r\n",
480 (secondConsole == NULL) /* serial console */
481 ? "Reset" : "CTRL-ALT-DEL");
Eric Andersen3ae0c781999-11-04 01:13:21 +0000482 sync();
Erik Andersen3fe39dc2000-01-25 18:13:53 +0000483
484 /* allow time for last message to reach serial console */
Erik Andersenfac10d72000-02-07 05:29:42 +0000485 sleep(5);
Erik Andersen3fe39dc2000-01-25 18:13:53 +0000486
Erik Andersen4d1d0111999-12-17 18:44:15 +0000487#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,2,0)
Eric Andersen2cb55071999-12-10 08:25:07 +0000488 if (sig == SIGUSR2)
489 reboot(RB_POWER_OFF);
490 else
Erik Andersen4d1d0111999-12-17 18:44:15 +0000491#endif
Erik Andersen7dc16072000-01-04 01:10:25 +0000492 reboot(RB_HALT_SYSTEM);
Eric Andersenabc7d591999-10-19 00:27:50 +0000493 exit(0);
Eric Andersencc8ed391999-10-05 16:24:54 +0000494}
495
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000496static void reboot_signal(int sig)
Eric Andersencc8ed391999-10-05 16:24:54 +0000497{
Eric Andersenabc7d591999-10-19 00:27:50 +0000498 shutdown_system();
Eric Andersen3ae0c781999-11-04 01:13:21 +0000499 message(CONSOLE, "Please stand by while rebooting the system.\r\n");
500 sync();
Erik Andersen3fe39dc2000-01-25 18:13:53 +0000501
502 /* allow time for last message to reach serial console */
503 sleep(2);
504
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000505 reboot(RB_AUTOBOOT);
Eric Andersenabc7d591999-10-19 00:27:50 +0000506 exit(0);
Eric Andersencc8ed391999-10-05 16:24:54 +0000507}
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000508
Erik Andersende552872000-01-23 01:34:05 +0000509#if defined BB_FEATURE_INIT_CHROOT
510static void check_chroot(int sig)
511{
512 char *argv_init[2] = { "init", NULL, };
513 char *envp_init[3] = { "HOME=/", "TERM=linux", NULL, };
514 char rootpath[256], *tc;
515 int fd;
516
517 if ((fd = open("/proc/sys/kernel/init-chroot", O_RDONLY)) == -1) {
518 message(CONSOLE, "SIGHUP recived, but could not open proc file\r\n");
519 sleep(2);
520 return;
521 }
522 if (read(fd, rootpath, sizeof(rootpath)) == -1) {
523 message(CONSOLE, "SIGHUP recived, but could not read proc file\r\n");
524 sleep(2);
525 return;
526 }
527 close(fd);
528
529 if (rootpath[0] == '\0') {
530 message(CONSOLE, "SIGHUP recived, but new root is not valid: %s\r\n",
531 rootpath);
532 sleep(2);
533 return;
534 }
535
536 tc = strrchr(rootpath, '\n');
537 *tc = '\0';
538
539 /* Ok, making it this far means we commit */
540 message(CONSOLE, "Please stand by, changing root to `%s'.\r\n", rootpath);
541
542 /* kill all other programs first */
543 message(CONSOLE, "Sending SIGTERM to all processes.\r\n");
544 kill(-1, SIGTERM);
545 sleep(2);
546 sync();
547
548 message(CONSOLE, "Sending SIGKILL to all processes.\r\n");
549 kill(-1, SIGKILL);
550 sleep(2);
551 sync();
552
553 /* ok, we don't need /proc anymore. we also assume that the signaling
554 * process left the rest of the filesystems alone for us */
555 umount("/proc");
556
557 /* Ok, now we chroot. Hopefully we only have two things mounted, the
558 * new chroot'd mount point, and the old "/" mount. s,
559 * we go ahead and unmount the old "/". This should trigger the kernel
560 * to set things up the Right Way(tm). */
561
562 if (!chroot(rootpath))
563 umount("/dev/root");
564
565 /* If the chroot fails, we are already too far to turn back, so we
566 * continue and hope that executing init below will revive the system */
567
568 /* close all of our descriptors and open new ones */
569 close(0);
570 close(1);
571 close(2);
572 open("/dev/console", O_RDWR, 0);
573 dup(0);
574 dup(0);
575
576 message(CONSOLE, "Executing real init...\r\n");
577 /* execute init in the (hopefully) new root */
578 execve("/sbin/init",argv_init,envp_init);
579
Erik Andersen3fe39dc2000-01-25 18:13:53 +0000580 message(CONSOLE, "ERROR: Could not exec new init. Press %s to reboot.\r\n",
581 (secondConsole == NULL) /* serial console */
582 ? "Reset" : "CTRL-ALT-DEL");
Erik Andersende552872000-01-23 01:34:05 +0000583 return;
584}
585#endif /* BB_FEATURE_INIT_CHROOT */
586
587#endif /* ! DEBUG_INIT */
Erik Andersen7dc16072000-01-04 01:10:25 +0000588
Erik Andersen96e2abd2000-01-07 11:40:44 +0000589void new_initAction (initActionEnum action,
Erik Andersen9e737252000-01-06 01:16:13 +0000590 char* process, char* cons)
Erik Andersen7dc16072000-01-04 01:10:25 +0000591{
592 initAction* newAction;
Erik Andersen9e737252000-01-06 01:16:13 +0000593
Erik Andersen3fe39dc2000-01-25 18:13:53 +0000594 if (*cons == '\0')
595 cons = console;
596
Erik Andersen9e737252000-01-06 01:16:13 +0000597 /* If BusyBox detects that a serial console is in use,
Erik Andersen3fe39dc2000-01-25 18:13:53 +0000598 * then entries not refering to the console or null devices will _not_ be run.
Erik Andersende552872000-01-23 01:34:05 +0000599 * The exception to this rule is the null device.
Erik Andersen9e737252000-01-06 01:16:13 +0000600 */
Erik Andersen3fe39dc2000-01-25 18:13:53 +0000601 if (secondConsole == NULL && strcmp(cons, console) && strcmp(cons, "/dev/null"))
Erik Andersen9e737252000-01-06 01:16:13 +0000602 return;
603
Erik Andersen7dc16072000-01-04 01:10:25 +0000604 newAction = calloc ((size_t)(1), sizeof(initAction));
605 if (!newAction) {
Erik Andersen0e3782f2000-01-07 02:54:55 +0000606 message(LOG|CONSOLE,"Memory allocation failure\n");
Erik Andersen7dc16072000-01-04 01:10:25 +0000607 while (1) sleep(1);
608 }
609 newAction->nextPtr = initActionList;
610 initActionList = newAction;
611 strncpy( newAction->process, process, 255);
Erik Andersen96e2abd2000-01-07 11:40:44 +0000612 newAction->action = action;
Erik Andersen3fe39dc2000-01-25 18:13:53 +0000613 strncpy(newAction->console, cons, 255);
Erik Andersen7dc16072000-01-04 01:10:25 +0000614 newAction->pid = 0;
Erik Andersen31638212000-01-15 22:28:50 +0000615// message(LOG|CONSOLE, "process='%s' action='%d' console='%s'\n",
616// newAction->process, newAction->action, newAction->console);
Erik Andersen7dc16072000-01-04 01:10:25 +0000617}
618
619void delete_initAction (initAction *action)
620{
621 initAction *a, *b=NULL;
Erik Andersen3fe39dc2000-01-25 18:13:53 +0000622 for( a=initActionList ; a ; b=a, a=a->nextPtr) {
623 if (a == action) {
624 if (b==NULL) {
625 initActionList=a->nextPtr;
626 } else {
627 b->nextPtr=a->nextPtr;
628 }
Erik Andersen7dc16072000-01-04 01:10:25 +0000629 free( a);
630 break;
631 }
632 }
633}
634
Erik Andersen9e737252000-01-06 01:16:13 +0000635/* NOTE that if BB_FEATURE_USE_INITTAB is NOT defined,
636 * then parse_inittab() simply adds in some default
637 * actions(i.e runs INIT_SCRIPT and then starts a pair
Erik Andersen812d4662000-01-07 18:30:40 +0000638 * of "askfirst" shells). If BB_FEATURE_USE_INITTAB
639 * _is_ defined, but /etc/inittab is missing, this
640 * results in the same set of default behaviors.
Erik Andersen9e737252000-01-06 01:16:13 +0000641 * */
Erik Andersen7dc16072000-01-04 01:10:25 +0000642void parse_inittab(void)
643{
Erik Andersen9e737252000-01-06 01:16:13 +0000644#ifdef BB_FEATURE_USE_INITTAB
Erik Andersen7dc16072000-01-04 01:10:25 +0000645 FILE* file;
Erik Andersen0e3782f2000-01-07 02:54:55 +0000646 char buf[256], lineAsRead[256], tmpConsole[256];
Erik Andersen9e737252000-01-06 01:16:13 +0000647 char *p, *q, *r, *s;
Erik Andersen7dc16072000-01-04 01:10:25 +0000648 const struct initActionType *a = actions;
649 int foundIt;
650
651
652 file = fopen(INITTAB, "r");
653 if (file == NULL) {
654 /* No inittab file -- set up some default behavior */
Erik Andersen9e737252000-01-06 01:16:13 +0000655#endif
Erik Andersen7dc16072000-01-04 01:10:25 +0000656 /* Askfirst shell on tty1 */
Erik Andersen96e2abd2000-01-07 11:40:44 +0000657 new_initAction( ASKFIRST, SHELL, console );
Erik Andersen7dc16072000-01-04 01:10:25 +0000658 /* Askfirst shell on tty2 */
Erik Andersenac6e71f2000-01-08 22:04:33 +0000659 if (secondConsole != NULL)
660 new_initAction( ASKFIRST, SHELL, secondConsole );
Erik Andersen7dc16072000-01-04 01:10:25 +0000661 /* sysinit */
Erik Andersen96e2abd2000-01-07 11:40:44 +0000662 new_initAction( SYSINIT, INIT_SCRIPT, console );
Erik Andersen7dc16072000-01-04 01:10:25 +0000663
664 return;
Erik Andersen9e737252000-01-06 01:16:13 +0000665#ifdef BB_FEATURE_USE_INITTAB
Erik Andersen7dc16072000-01-04 01:10:25 +0000666 }
667
668 while ( fgets(buf, 255, file) != NULL) {
669 foundIt=FALSE;
670 for(p = buf; *p == ' ' || *p == '\t'; p++);
671 if (*p == '#' || *p == '\n') continue;
672
673 /* Trim the trailing \n */
674 q = strrchr( p, '\n');
675 if (q != NULL)
676 *q='\0';
677
Erik Andersen9e737252000-01-06 01:16:13 +0000678 /* Keep a copy around for posterity's sake (and error msgs) */
Erik Andersen0e3782f2000-01-07 02:54:55 +0000679 strcpy(lineAsRead, buf);
Erik Andersen9e737252000-01-06 01:16:13 +0000680
681 /* Grab the ID field */
682 s=p;
Erik Andersen7dc16072000-01-04 01:10:25 +0000683 p = strchr( p, ':');
Erik Andersen9e737252000-01-06 01:16:13 +0000684 if ( p != NULL || *(p+1) != '\0' ) {
685 *p='\0';
686 ++p;
687 }
Erik Andersen7dc16072000-01-04 01:10:25 +0000688
689 /* Now peal off the process field from the end
690 * of the string */
691 q = strrchr( p, ':');
692 if ( q == NULL || *(q+1) == '\0' ) {
Erik Andersen0e3782f2000-01-07 02:54:55 +0000693 message(LOG|CONSOLE,"Bad inittab entry: %s\n", lineAsRead);
Erik Andersen7dc16072000-01-04 01:10:25 +0000694 continue;
695 } else {
696 *q='\0';
697 ++q;
698 }
699
700 /* Now peal off the action field */
701 r = strrchr( p, ':');
702 if ( r == NULL || *(r+1) == '\0') {
Erik Andersen0e3782f2000-01-07 02:54:55 +0000703 message(LOG|CONSOLE,"Bad inittab entry: %s\n", lineAsRead);
Erik Andersen7dc16072000-01-04 01:10:25 +0000704 continue;
705 } else {
706 ++r;
707 }
708
709 /* Ok, now process it */
710 a = actions;
711 while (a->name != 0) {
712 if (strcmp(a->name, r) == 0) {
Erik Andersen0e3782f2000-01-07 02:54:55 +0000713 if (*s != '\0') {
714 struct stat statBuf;
715 strcpy(tmpConsole, "/dev/");
716 strncat(tmpConsole, s, 200);
717 if (stat(tmpConsole, &statBuf) != 0) {
718 message(LOG|CONSOLE, "device '%s' does not exist. Did you read the directions?\n", tmpConsole);
719 break;
720 }
721 s = tmpConsole;
722 }
Erik Andersen96e2abd2000-01-07 11:40:44 +0000723 new_initAction( a->action, q, s);
Erik Andersen7dc16072000-01-04 01:10:25 +0000724 foundIt=TRUE;
725 }
726 a++;
727 }
728 if (foundIt==TRUE)
729 continue;
730 else {
731 /* Choke on an unknown action */
Erik Andersen0e3782f2000-01-07 02:54:55 +0000732 message(LOG|CONSOLE, "Bad inittab entry: %s\n", lineAsRead);
Erik Andersen7dc16072000-01-04 01:10:25 +0000733 }
734 }
735 return;
Erik Andersen9e737252000-01-06 01:16:13 +0000736#endif
Erik Andersen7dc16072000-01-04 01:10:25 +0000737}
738
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000739extern int init_main(int argc, char **argv)
Eric Andersen0460ff21999-10-25 23:32:44 +0000740{
Erik Andersen7dc16072000-01-04 01:10:25 +0000741 initAction *a;
742 pid_t wpid;
743 int status;
Eric Andersen0460ff21999-10-25 23:32:44 +0000744
Eric Andersend73dc5b1999-11-10 23:13:02 +0000745#ifndef DEBUG_INIT
Erik Andersende552872000-01-23 01:34:05 +0000746 /* Expect to be PID 1 if we are run as init (not linuxrc) */
Eric Andersen07274581999-11-21 21:50:07 +0000747 if (getpid() != 1 && strstr(argv[0], "init")!=NULL ) {
Eric Andersend73dc5b1999-11-10 23:13:02 +0000748 usage( "init\n\nInit is the parent of all processes.\n\n"
749 "This version of init is designed to be run only by the kernel\n");
750 }
Erik Andersen31638212000-01-15 22:28:50 +0000751 /* Fix up argv[0] to be certain we claim to be init */
752 strncpy(argv[0], "init", strlen(argv[0]));
753
Erik Andersen05df2392000-01-13 04:43:48 +0000754 /* Set up sig handlers -- be sure to
755 * clear all of these in run() */
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000756 signal(SIGUSR1, halt_signal);
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000757 signal(SIGUSR2, reboot_signal);
Erik Andersen0881de72000-01-05 09:34:26 +0000758 signal(SIGINT, reboot_signal);
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000759 signal(SIGTERM, reboot_signal);
Erik Andersende552872000-01-23 01:34:05 +0000760#if defined BB_FEATURE_INIT_CHROOT
761 signal(SIGHUP, check_chroot);
762#endif
Eric Andersen0460ff21999-10-25 23:32:44 +0000763
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000764 /* Turn off rebooting via CTL-ALT-DEL -- we get a
765 * SIGINT on CAD so we can shut things down gracefully... */
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000766 reboot(RB_DISABLE_CAD);
Eric Andersen7f1acfd1999-10-29 23:09:13 +0000767#endif
Erik Andersen05df2392000-01-13 04:43:48 +0000768
Eric Andersenc7c41d31999-10-28 00:24:35 +0000769 /* Figure out where the default console should be */
770 console_init();
Eric Andersen0460ff21999-10-25 23:32:44 +0000771
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000772 /* Close whatever files are open, and reset the console. */
773 close(0);
774 close(1);
775 close(2);
Eric Andersen7f1acfd1999-10-29 23:09:13 +0000776 set_term(0);
Erik Andersen05df2392000-01-13 04:43:48 +0000777 setsid();
Eric Andersencc8ed391999-10-05 16:24:54 +0000778
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000779 /* Make sure PATH is set to something sane */
Eric Andersenb186d981999-12-03 09:19:54 +0000780 putenv(_PATH_STDPATH);
Eric Andersencc8ed391999-10-05 16:24:54 +0000781
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000782 /* Hello world */
Eric Andersen7f1acfd1999-10-29 23:09:13 +0000783#ifndef DEBUG_INIT
Erik Andersen0e3782f2000-01-07 02:54:55 +0000784 message(LOG|CONSOLE,
Erik Andersen7dc16072000-01-04 01:10:25 +0000785 "init started: BusyBox v%s (%s) multi-call binary\r\n",
786 BB_VER, BB_BT);
Eric Andersen7f1acfd1999-10-29 23:09:13 +0000787#else
Erik Andersen0e3782f2000-01-07 02:54:55 +0000788 message(LOG|CONSOLE,
Erik Andersen7dc16072000-01-04 01:10:25 +0000789 "init(%d) started: BusyBox v%s (%s) multi-call binary\r\n",
790 getpid(), BB_VER, BB_BT);
Eric Andersen7f1acfd1999-10-29 23:09:13 +0000791#endif
Eric Andersencc8ed391999-10-05 16:24:54 +0000792
Eric Andersenc7c41d31999-10-28 00:24:35 +0000793
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000794 /* Mount /proc */
Eric Andersene18c75a1999-11-05 04:23:05 +0000795 if (mount ("proc", "/proc", "proc", 0, 0) == 0) {
Erik Andersen0e3782f2000-01-07 02:54:55 +0000796 message(LOG|CONSOLE, "Mounting /proc: done.\n");
Erik Andersenac6e71f2000-01-08 22:04:33 +0000797 kernelVersion = get_kernel_revision();
Eric Andersene18c75a1999-11-05 04:23:05 +0000798 } else
Erik Andersen0e3782f2000-01-07 02:54:55 +0000799 message(LOG|CONSOLE, "Mounting /proc: failed!\n");
Eric Andersencc8ed391999-10-05 16:24:54 +0000800
Eric Andersen219d6f51999-11-02 19:43:01 +0000801 /* Make sure there is enough memory to do something useful. */
802 check_memory();
Eric Andersencc8ed391999-10-05 16:24:54 +0000803
Eric Andersend00c2621999-12-07 08:37:31 +0000804 /* Check if we are supposed to be in single user mode */
805 if ( argc > 1 && (!strcmp(argv[1], "single") ||
Erik Andersen0881de72000-01-05 09:34:26 +0000806 !strcmp(argv[1], "-s") || !strcmp(argv[1], "1")))
807 {
Erik Andersen7dc16072000-01-04 01:10:25 +0000808 /* Ask first then start a shell on tty2 */
Erik Andersenac6e71f2000-01-08 22:04:33 +0000809 if (secondConsole != NULL)
810 new_initAction( ASKFIRST, SHELL, secondConsole);
Erik Andersen3fe39dc2000-01-25 18:13:53 +0000811 /* Start a shell on tty1 */
812 new_initAction( RESPAWN, SHELL, console);
Erik Andersen7dc16072000-01-04 01:10:25 +0000813 } else {
814 /* Not in single user mode -- see what inittab says */
Erik Andersen9e737252000-01-06 01:16:13 +0000815
816 /* NOTE that if BB_FEATURE_USE_INITTAB is NOT defined,
817 * then parse_inittab() simply adds in some default
818 * actions(i.e runs INIT_SCRIPT and then starts a pair
819 * of "askfirst" shells */
Erik Andersen7dc16072000-01-04 01:10:25 +0000820 parse_inittab();
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000821 }
822
Erik Andersen7dc16072000-01-04 01:10:25 +0000823 /* Now run everything that needs to be run */
Eric Andersen84b00921999-12-11 04:16:51 +0000824
Erik Andersen0881de72000-01-05 09:34:26 +0000825 /* First run the sysinit command */
Erik Andersen7dc16072000-01-04 01:10:25 +0000826 for( a=initActionList ; a; a=a->nextPtr) {
827 if (a->action == SYSINIT) {
Erik Andersen96e2abd2000-01-07 11:40:44 +0000828 waitfor(a->process, a->console, FALSE);
Erik Andersen7dc16072000-01-04 01:10:25 +0000829 /* Now remove the "sysinit" entry from the list */
830 delete_initAction( a);
Eric Andersend00c2621999-12-07 08:37:31 +0000831 }
832 }
Erik Andersen7dc16072000-01-04 01:10:25 +0000833 /* Next run anything that wants to block */
834 for( a=initActionList ; a; a=a->nextPtr) {
835 if (a->action == WAIT) {
Erik Andersen96e2abd2000-01-07 11:40:44 +0000836 waitfor(a->process, a->console, FALSE);
Erik Andersen7dc16072000-01-04 01:10:25 +0000837 /* Now remove the "wait" entry from the list */
838 delete_initAction( a);
839 }
840 }
841 /* Next run anything to be run only once */
842 for( a=initActionList ; a; a=a->nextPtr) {
843 if (a->action == ONCE) {
Erik Andersen96e2abd2000-01-07 11:40:44 +0000844 run(a->process, a->console, FALSE);
Erik Andersen7dc16072000-01-04 01:10:25 +0000845 /* Now remove the "once" entry from the list */
846 delete_initAction( a);
847 }
848 }
Erik Andersen0e3782f2000-01-07 02:54:55 +0000849 /* If there is nothing else to do, stop */
850 if (initActionList == NULL) {
851 message(LOG|CONSOLE, "No more tasks for init -- sleeping forever.\n");
852 while (1) sleep(1);
853 }
Eric Andersen219d6f51999-11-02 19:43:01 +0000854
Erik Andersen0881de72000-01-05 09:34:26 +0000855 /* Now run the looping stuff for the rest of forever */
856 while (1) {
Erik Andersen7dc16072000-01-04 01:10:25 +0000857 for( a=initActionList ; a; a=a->nextPtr) {
858 /* Only run stuff with pid==0. If they have
859 * a pid, that means they are still running */
860 if (a->pid == 0) {
861 switch(a->action) {
862 case RESPAWN:
863 /* run the respawn stuff */
Erik Andersen96e2abd2000-01-07 11:40:44 +0000864 a->pid = run(a->process, a->console, FALSE);
Erik Andersen7dc16072000-01-04 01:10:25 +0000865 break;
866 case ASKFIRST:
867 /* run the askfirst stuff */
Erik Andersen96e2abd2000-01-07 11:40:44 +0000868 a->pid = run(a->process, a->console, TRUE);
Erik Andersen7dc16072000-01-04 01:10:25 +0000869 break;
Erik Andersen0881de72000-01-05 09:34:26 +0000870 /* silence the compiler's incessant whining */
Erik Andersen7dc16072000-01-04 01:10:25 +0000871 default:
872 break;
873 }
874 }
875 }
Erik Andersen0881de72000-01-05 09:34:26 +0000876 /* Wait for a child process to exit */
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000877 wpid = wait(&status);
Eric Andersena7456061999-10-27 02:31:32 +0000878 if (wpid > 0 ) {
Erik Andersen0881de72000-01-05 09:34:26 +0000879 /* Find out who died and clean up their corpse */
Erik Andersen7dc16072000-01-04 01:10:25 +0000880 for( a=initActionList ; a; a=a->nextPtr) {
881 if (a->pid==wpid) {
882 a->pid=0;
Erik Andersen0881de72000-01-05 09:34:26 +0000883 message(LOG, "Process '%s' (pid %d) exited. Scheduling it for restart.\n",
884 a->process, wpid);
Erik Andersen7dc16072000-01-04 01:10:25 +0000885 }
Eric Andersenb6a44b81999-11-13 04:47:09 +0000886 }
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000887 }
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000888 sleep(1);
889 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000890}
Erik Andersen9c88cac1999-12-30 09:25:17 +0000891