blob: 5b80cc56180418606451013c6cd0cdd972e6cfbd [file] [log] [blame]
Eric Andersenc4996011999-10-20 22:08:37 +00001/*
2 * Mini init implementation for busybox
3 *
4 *
5 * Copyright (C) 1995, 1996 by Bruce Perens <bruce@pixar.com>.
6 * Adjusted by so many folks, it's impossible to keep track.
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 *
22 */
23
Eric Andersencc8ed391999-10-05 16:24:54 +000024#include "internal.h"
25#include <stdio.h>
Erik Andersen7dc16072000-01-04 01:10:25 +000026#include <string.h>
Eric Andersencc8ed391999-10-05 16:24:54 +000027#include <stdlib.h>
28#include <stdarg.h>
29#include <unistd.h>
30#include <errno.h>
31#include <signal.h>
32#include <termios.h>
Eric Andersenb186d981999-12-03 09:19:54 +000033#include <paths.h>
Eric Andersencc8ed391999-10-05 16:24:54 +000034#include <sys/types.h>
35#include <sys/fcntl.h>
36#include <sys/wait.h>
37#include <string.h>
38#include <sys/mount.h>
39#include <sys/reboot.h>
40#include <sys/kdaemon.h>
Eric Andersencc8ed391999-10-05 16:24:54 +000041#include <sys/sysmacros.h>
Eric Andersen8a8fbb81999-10-26 00:18:56 +000042#include <linux/serial.h> /* for serial_struct */
43#include <sys/vt.h> /* for vt_stat */
Eric Andersenabc7d591999-10-19 00:27:50 +000044#include <sys/ioctl.h>
Erik Andersen4d1d0111999-12-17 18:44:15 +000045#include <linux/version.h>
Eric Andersen4c781471999-11-26 08:12:56 +000046#ifdef BB_SYSLOGD
47#include <sys/syslog.h>
48#endif
Eric Andersencc8ed391999-10-05 16:24:54 +000049
Eric Andersen0ecb54a1999-12-05 23:24:55 +000050#if ! defined BB_FEATURE_USE_PROCFS
51#error Sorry, I depend on the /proc filesystem right now.
52#endif
53
Erik Andersen4d1d0111999-12-17 18:44:15 +000054#ifndef KERNEL_VERSION
55#define KERNEL_VERSION(a,b,c) (((a) << 16) + ((b) << 8) + (c))
56#endif
57
Eric Andersen0ecb54a1999-12-05 23:24:55 +000058
Erik Andersen7dc16072000-01-04 01:10:25 +000059#define VT_PRIMARY "/dev/tty1" /* Primary virtual console */
60#define VT_SECONDARY "/dev/tty2" /* Virtual console */
61#define VT_LOG "/dev/tty3" /* Virtual console */
62#define SERIAL_CON0 "/dev/ttyS0" /* Primary serial console */
63#define SERIAL_CON1 "/dev/ttyS1" /* Serial console */
Erik Andersen96e2abd2000-01-07 11:40:44 +000064#define SHELL "/bin/sh" /* Default shell */
Erik Andersen7dc16072000-01-04 01:10:25 +000065#define INITTAB "/etc/inittab" /* inittab file location */
Erik Andersen96e2abd2000-01-07 11:40:44 +000066#ifndef INIT_SCRIPT
Erik Andersen7dc16072000-01-04 01:10:25 +000067#define INIT_SCRIPT "/etc/init.d/rcS" /* Default sysinit script. */
Erik Andersen96e2abd2000-01-07 11:40:44 +000068#endif
Erik Andersen9c88cac1999-12-30 09:25:17 +000069
Eric Andersen3ae0c781999-11-04 01:13:21 +000070#define LOG 0x1
71#define CONSOLE 0x2
Erik Andersen7dc16072000-01-04 01:10:25 +000072
73/* Allowed init action types */
74typedef enum {
75 SYSINIT=1,
Erik Andersen7dc16072000-01-04 01:10:25 +000076 RESPAWN,
77 ASKFIRST,
78 WAIT,
79 ONCE
80} initActionEnum;
81
82/* And now a list of the actions we support in the version of init */
83typedef struct initActionType{
84 const char* name;
85 initActionEnum action;
86} initActionType;
87
88static const struct initActionType actions[] = {
89 {"sysinit", SYSINIT},
Erik Andersen7dc16072000-01-04 01:10:25 +000090 {"respawn", RESPAWN},
91 {"askfirst", ASKFIRST},
92 {"wait", WAIT},
93 {"once", ONCE},
94 {0}
95};
96
97/* Set up a linked list of initactions, to be read from inittab */
98typedef struct initActionTag initAction;
99struct initActionTag {
100 pid_t pid;
101 char process[256];
Erik Andersen9e737252000-01-06 01:16:13 +0000102 char console[256];
Erik Andersen7dc16072000-01-04 01:10:25 +0000103 initAction *nextPtr;
104 initActionEnum action;
105};
106initAction* initActionList = NULL;
107
108
Erik Andersenac6e71f2000-01-08 22:04:33 +0000109static char *secondConsole = VT_SECONDARY;
Eric Andersenbe971d61999-11-03 16:52:50 +0000110static char *log = VT_LOG;
Erik Andersenac6e71f2000-01-08 22:04:33 +0000111static int kernelVersion = 0;
Erik Andersen31638212000-01-15 22:28:50 +0000112static char termType[32] = "TERM=ansi";
113static char console[32] = _PATH_CONSOLE;
Eric Andersen0460ff21999-10-25 23:32:44 +0000114
115
116/* try to open up the specified device */
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000117int device_open(char *device, int mode)
Eric Andersencc8ed391999-10-05 16:24:54 +0000118{
Eric Andersen0460ff21999-10-25 23:32:44 +0000119 int m, f, fd = -1;
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000120
Eric Andersen7f1acfd1999-10-29 23:09:13 +0000121 m = mode | O_NONBLOCK;
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000122
Eric Andersen0460ff21999-10-25 23:32:44 +0000123 /* Retry up to 5 times */
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000124 for (f = 0; f < 5; f++)
125 if ((fd = open(device, m)) >= 0)
126 break;
127 if (fd < 0)
128 return fd;
Eric Andersen3ae0c781999-11-04 01:13:21 +0000129 /* Reset original flags. */
Eric Andersen0460ff21999-10-25 23:32:44 +0000130 if (m != mode)
131 fcntl(fd, F_SETFL, mode);
132 return fd;
133}
Eric Andersencc8ed391999-10-05 16:24:54 +0000134
Eric Andersen3ae0c781999-11-04 01:13:21 +0000135/* print a message to the specified device:
136 * device may be bitwise-or'd from LOG | CONSOLE */
137void message(int device, char *fmt, ...)
Eric Andersen0460ff21999-10-25 23:32:44 +0000138{
Eric Andersen0460ff21999-10-25 23:32:44 +0000139 va_list arguments;
Erik Andersen7dc16072000-01-04 01:10:25 +0000140 int fd;
141
Eric Andersen4c781471999-11-26 08:12:56 +0000142#ifdef BB_SYSLOGD
143
144 /* Log the message to syslogd */
145 if (device & LOG ) {
146 char msg[1024];
147 va_start(arguments, fmt);
148 vsnprintf(msg, sizeof(msg), fmt, arguments);
149 va_end(arguments);
Erik Andersende552872000-01-23 01:34:05 +0000150 openlog( "init", 0, LOG_DAEMON);
Eric Andersen4c781471999-11-26 08:12:56 +0000151 syslog(LOG_DAEMON|LOG_NOTICE, msg);
Erik Andersende552872000-01-23 01:34:05 +0000152 closelog();
Eric Andersen4c781471999-11-26 08:12:56 +0000153 }
154
155#else
156 static int log_fd=-1;
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000157
Eric Andersen3ae0c781999-11-04 01:13:21 +0000158 /* Take full control of the log tty, and never close it.
159 * It's mine, all mine! Muhahahaha! */
Eric Andersen07e52971999-11-07 07:38:08 +0000160 if (log_fd < 0) {
161 if (log == NULL) {
162 /* don't even try to log, because there is no such console */
163 log_fd = -2;
164 /* log to main console instead */
165 device = CONSOLE;
166 }
167 else if ((log_fd = device_open(log, O_RDWR|O_NDELAY)) < 0) {
Eric Andersen3ae0c781999-11-04 01:13:21 +0000168 log_fd=-1;
169 fprintf(stderr, "Bummer, can't write to log on %s!\r\n", log);
170 fflush(stderr);
171 return;
172 }
173 }
Eric Andersen07e52971999-11-07 07:38:08 +0000174 if ( (device & LOG) && (log_fd >= 0) ) {
Eric Andersen0460ff21999-10-25 23:32:44 +0000175 va_start(arguments, fmt);
Eric Andersen3ae0c781999-11-04 01:13:21 +0000176 vdprintf(log_fd, fmt, arguments);
Eric Andersena7456061999-10-27 02:31:32 +0000177 va_end(arguments);
178 }
Eric Andersen4c781471999-11-26 08:12:56 +0000179#endif
180
Eric Andersen3ae0c781999-11-04 01:13:21 +0000181 if (device & CONSOLE) {
Eric Andersended62591999-11-18 00:19:26 +0000182 /* Always send console messages to /dev/console so people will see them. */
Eric Andersenb186d981999-12-03 09:19:54 +0000183 if ((fd = device_open(_PATH_CONSOLE, O_WRONLY|O_NOCTTY|O_NDELAY)) >= 0) {
Eric Andersen3ae0c781999-11-04 01:13:21 +0000184 va_start(arguments, fmt);
185 vdprintf(fd, fmt, arguments);
186 va_end(arguments);
187 close(fd);
188 } else {
189 fprintf(stderr, "Bummer, can't print: ");
190 va_start(arguments, fmt);
191 vfprintf(stderr, fmt, arguments);
192 fflush(stderr);
193 va_end(arguments);
194 }
195 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000196}
197
Eric Andersen3ae0c781999-11-04 01:13:21 +0000198
Eric Andersen0460ff21999-10-25 23:32:44 +0000199/* Set terminal settings to reasonable defaults */
Eric Andersenc7c41d31999-10-28 00:24:35 +0000200void set_term( int fd)
Eric Andersencc8ed391999-10-05 16:24:54 +0000201{
Eric Andersen0460ff21999-10-25 23:32:44 +0000202 struct termios tty;
Eric Andersen08b10341999-11-19 02:38:58 +0000203 static const char control_characters[] = {
204 '\003', '\034', '\177', '\025', '\004', '\0',
205 '\1', '\0', '\021', '\023', '\032', '\0', '\022',
206 '\017', '\027', '\026', '\0'
207 };
Eric Andersencc8ed391999-10-05 16:24:54 +0000208
Eric Andersenc7c41d31999-10-28 00:24:35 +0000209 tcgetattr(fd, &tty);
Eric Andersencc8ed391999-10-05 16:24:54 +0000210
Eric Andersen08b10341999-11-19 02:38:58 +0000211 /* set control chars */
212 memcpy(tty.c_cc, control_characters, sizeof(control_characters));
Eric Andersencc8ed391999-10-05 16:24:54 +0000213
Eric Andersen219d6f51999-11-02 19:43:01 +0000214 /* use line dicipline 0 */
Eric Andersen7f1acfd1999-10-29 23:09:13 +0000215 tty.c_line = 0;
216
Eric Andersen08b10341999-11-19 02:38:58 +0000217 /* Make it be sane */
218 //tty.c_cflag &= CBAUD|CBAUDEX|CSIZE|CSTOPB|PARENB|PARODD;
219 //tty.c_cflag |= HUPCL|CLOCAL;
220
221 /* input modes */
222 tty.c_iflag = ICRNL|IXON|IXOFF;
223
Eric Andersen7f1acfd1999-10-29 23:09:13 +0000224 /* output modes */
Eric Andersenc7c41d31999-10-28 00:24:35 +0000225 tty.c_oflag = OPOST|ONLCR;
Eric Andersen7f1acfd1999-10-29 23:09:13 +0000226
227 /* local modes */
Eric Andersen08b10341999-11-19 02:38:58 +0000228 tty.c_lflag = ISIG|ICANON|ECHO|ECHOE|ECHOK|ECHOCTL|ECHOKE|IEXTEN;
Eric Andersencc8ed391999-10-05 16:24:54 +0000229
Eric Andersenc7c41d31999-10-28 00:24:35 +0000230 tcsetattr(fd, TCSANOW, &tty);
Eric Andersencc8ed391999-10-05 16:24:54 +0000231}
232
Eric Andersen219d6f51999-11-02 19:43:01 +0000233/* How much memory does this machine have? */
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000234static int mem_total()
Eric Andersencc8ed391999-10-05 16:24:54 +0000235{
Eric Andersenabc7d591999-10-19 00:27:50 +0000236 char s[80];
Eric Andersen219d6f51999-11-02 19:43:01 +0000237 char *p = "/proc/meminfo";
Eric Andersenabc7d591999-10-19 00:27:50 +0000238 FILE *f;
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000239 const char pattern[] = "MemTotal:";
Eric Andersencc8ed391999-10-05 16:24:54 +0000240
Eric Andersen219d6f51999-11-02 19:43:01 +0000241 if ((f = fopen(p, "r")) < 0) {
Eric Andersen3ae0c781999-11-04 01:13:21 +0000242 message(LOG, "Error opening %s: %s\n", p, strerror( errno));
Eric Andersen219d6f51999-11-02 19:43:01 +0000243 return -1;
244 }
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000245 while (NULL != fgets(s, 79, f)) {
246 p = strstr(s, pattern);
Eric Andersenabc7d591999-10-19 00:27:50 +0000247 if (NULL != p) {
248 fclose(f);
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000249 return (atoi(p + strlen(pattern)));
Eric Andersenabc7d591999-10-19 00:27:50 +0000250 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000251 }
Eric Andersenabc7d591999-10-19 00:27:50 +0000252 return -1;
Eric Andersencc8ed391999-10-05 16:24:54 +0000253}
254
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000255static void console_init()
Eric Andersen0460ff21999-10-25 23:32:44 +0000256{
257 int fd;
258 int tried_devcons = 0;
Eric Andersen219d6f51999-11-02 19:43:01 +0000259 int tried_vtprimary = 0;
Eric Andersen07e52971999-11-07 07:38:08 +0000260 struct serial_struct sr;
Eric Andersen0460ff21999-10-25 23:32:44 +0000261 char *s;
262
Erik Andersen31638212000-01-15 22:28:50 +0000263 if ((s = getenv("TERM")) != NULL) {
264 snprintf(termType,sizeof(termType)-1,"TERM=%s",s);
Erik Andersenac6e71f2000-01-08 22:04:33 +0000265 }
266
267 if ((s = getenv("CONSOLE")) != NULL) {
Erik Andersen31638212000-01-15 22:28:50 +0000268 snprintf(console, sizeof(console)-1, "%s",s);
Eric Andersen07e52971999-11-07 07:38:08 +0000269 }
Eric Andersenfbb39c81999-11-08 17:00:52 +0000270#if #cpu(sparc)
Eric Andersen07e52971999-11-07 07:38:08 +0000271 /* sparc kernel supports console=tty[ab] parameter which is also
272 * passed to init, so catch it here */
Erik Andersende552872000-01-23 01:34:05 +0000273 else if ((s = getenv("console")) != NULL) {
Eric Andersen07e52971999-11-07 07:38:08 +0000274 /* remap tty[ab] to /dev/ttyS[01] */
275 if (strcmp( s, "ttya" )==0)
Erik Andersen31638212000-01-15 22:28:50 +0000276 snprintf(console, sizeof(console)-1, "%s", SERIAL_CON0);
Eric Andersen07e52971999-11-07 07:38:08 +0000277 else if (strcmp( s, "ttyb" )==0)
Erik Andersen31638212000-01-15 22:28:50 +0000278 snprintf(console, sizeof(console)-1, "%s", SERIAL_CON1);
Eric Andersen07e52971999-11-07 07:38:08 +0000279 }
Eric Andersen219d6f51999-11-02 19:43:01 +0000280#endif
Eric Andersen07e52971999-11-07 07:38:08 +0000281 else {
282 struct vt_stat vt;
Eric Andersen07e52971999-11-07 07:38:08 +0000283
Eric Andersen07e52971999-11-07 07:38:08 +0000284 /* 2.2 kernels: identify the real console backend and try to use it */
Eric Andersen08b10341999-11-19 02:38:58 +0000285 if (ioctl(0, TIOCGSERIAL, &sr) == 0) {
Eric Andersen07e52971999-11-07 07:38:08 +0000286 /* this is a serial console */
Erik Andersen31638212000-01-15 22:28:50 +0000287 snprintf(console, sizeof(console)-1, "/dev/ttyS%d", sr.line);
Eric Andersen07e52971999-11-07 07:38:08 +0000288 }
289 else if (ioctl(0, VT_GETSTATE, &vt) == 0) {
290 /* this is linux virtual tty */
Erik Andersen31638212000-01-15 22:28:50 +0000291 snprintf(console, sizeof(console)-1, "/dev/tty%d", vt.v_active);
Eric Andersen07e52971999-11-07 07:38:08 +0000292 } else {
Erik Andersen31638212000-01-15 22:28:50 +0000293 snprintf(console, sizeof(console)-1, "%s", _PATH_CONSOLE);
Eric Andersen07e52971999-11-07 07:38:08 +0000294 tried_devcons++;
295 }
Eric Andersen0460ff21999-10-25 23:32:44 +0000296 }
Eric Andersena7456061999-10-27 02:31:32 +0000297
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000298 while ((fd = open(console, O_RDONLY | O_NONBLOCK)) < 0) {
Eric Andersen219d6f51999-11-02 19:43:01 +0000299 /* Can't open selected console -- try /dev/console */
Eric Andersen0460ff21999-10-25 23:32:44 +0000300 if (!tried_devcons) {
301 tried_devcons++;
Erik Andersen31638212000-01-15 22:28:50 +0000302 snprintf(console, sizeof(console)-1, "%s", _PATH_CONSOLE);
Eric Andersen0460ff21999-10-25 23:32:44 +0000303 continue;
304 }
Eric Andersenbe971d61999-11-03 16:52:50 +0000305 /* Can't open selected console -- try vt1 */
306 if (!tried_vtprimary) {
307 tried_vtprimary++;
Erik Andersen31638212000-01-15 22:28:50 +0000308 snprintf(console, sizeof(console)-1, "%s", VT_PRIMARY);
Eric Andersenbe971d61999-11-03 16:52:50 +0000309 continue;
310 }
Eric Andersen0460ff21999-10-25 23:32:44 +0000311 break;
312 }
Erik Andersen31638212000-01-15 22:28:50 +0000313 if (fd < 0) {
Eric Andersen219d6f51999-11-02 19:43:01 +0000314 /* Perhaps we should panic here? */
Erik Andersen31638212000-01-15 22:28:50 +0000315 snprintf(console, sizeof(console)-1, "/dev/null");
316 } else {
Eric Andersen07e52971999-11-07 07:38:08 +0000317 /* check for serial console and disable logging to tty3 & running a
318 * shell to tty2 */
319 if (ioctl(0,TIOCGSERIAL,&sr) == 0) {
Erik Andersen31638212000-01-15 22:28:50 +0000320 message(LOG|CONSOLE, "serial console detected. Disabling virtual terminals.\r\n" );
Eric Andersen07e52971999-11-07 07:38:08 +0000321 log = NULL;
Erik Andersenac6e71f2000-01-08 22:04:33 +0000322 secondConsole = NULL;
Eric Andersen07e52971999-11-07 07:38:08 +0000323 }
Eric Andersen0460ff21999-10-25 23:32:44 +0000324 close(fd);
Eric Andersen07e52971999-11-07 07:38:08 +0000325 }
Eric Andersen3ae0c781999-11-04 01:13:21 +0000326 message(LOG, "console=%s\n", console );
Eric Andersen0460ff21999-10-25 23:32:44 +0000327}
328
Erik Andersen7dc16072000-01-04 01:10:25 +0000329static pid_t run(char* command,
Eric Andersenc7c41d31999-10-28 00:24:35 +0000330 char *terminal, int get_enter)
Eric Andersen0460ff21999-10-25 23:32:44 +0000331{
Erik Andersen05df2392000-01-13 04:43:48 +0000332 int i, fd;
Eric Andersenc7c41d31999-10-28 00:24:35 +0000333 pid_t pid;
Erik Andersen7dc16072000-01-04 01:10:25 +0000334 char* tmpCmd;
335 char* cmd[255];
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000336 static const char press_enter[] =
Eric Andersen0460ff21999-10-25 23:32:44 +0000337 "\nPlease press Enter to activate this console. ";
Erik Andersen31638212000-01-15 22:28:50 +0000338 char* environment[] = {
Erik Andersenac6e71f2000-01-08 22:04:33 +0000339 "HOME=/",
340 "PATH=/usr/bin:/bin:/usr/sbin:/sbin",
341 "SHELL=/bin/sh",
Erik Andersen31638212000-01-15 22:28:50 +0000342 termType,
Erik Andersenac6e71f2000-01-08 22:04:33 +0000343 "USER=root",
344 0
345 };
Erik Andersenac6e71f2000-01-08 22:04:33 +0000346
Eric Andersen0460ff21999-10-25 23:32:44 +0000347
Eric Andersen0460ff21999-10-25 23:32:44 +0000348 if ((pid = fork()) == 0) {
Erik Andersen0881de72000-01-05 09:34:26 +0000349 pid_t shell_pgid = getpid ();
350
Eric Andersen0460ff21999-10-25 23:32:44 +0000351 /* Clean up */
352 close(0);
353 close(1);
354 close(2);
355 setsid();
356
Eric Andersen2f6c04f1999-11-01 23:59:44 +0000357 /* Reset signal handlers set for parent process */
358 signal(SIGUSR1, SIG_DFL);
359 signal(SIGUSR2, SIG_DFL);
360 signal(SIGINT, SIG_DFL);
361 signal(SIGTERM, SIG_DFL);
362
Erik Andersen05df2392000-01-13 04:43:48 +0000363 if ((fd = device_open(terminal, O_RDWR)) < 0) {
364 message(LOG|CONSOLE, "Bummer, can't open %s\r\n", terminal);
365 exit(1);
366 }
367 dup(fd);
368 dup(fd);
369 tcsetpgrp (0, getpgrp());
370 set_term(0);
Eric Andersen0460ff21999-10-25 23:32:44 +0000371
Eric Andersen2f6c04f1999-11-01 23:59:44 +0000372 if (get_enter==TRUE) {
Eric Andersen0460ff21999-10-25 23:32:44 +0000373 /*
374 * Save memory by not exec-ing anything large (like a shell)
375 * before the user wants it. This is critical if swap is not
376 * enabled and the system has low memory. Generally this will
377 * be run on the second virtual console, and the first will
378 * be allowed to start a shell or whatever an init script
379 * specifies.
380 */
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000381 char c;
Eric Andersen3ae0c781999-11-04 01:13:21 +0000382 message(LOG, "Waiting for enter to start '%s' (pid %d, console %s)\r\n",
Erik Andersen0881de72000-01-05 09:34:26 +0000383 command, shell_pgid, terminal );
Erik Andersen7dc16072000-01-04 01:10:25 +0000384 write(fileno(stdout), press_enter, sizeof(press_enter) - 1);
385 read(fileno(stdin), &c, 1);
Eric Andersen0460ff21999-10-25 23:32:44 +0000386 }
387
Erik Andersen05df2392000-01-13 04:43:48 +0000388 /* Log the process name and args */
Erik Andersen31638212000-01-15 22:28:50 +0000389 message(LOG, "Starting pid %d, console %s: '",
Erik Andersen05df2392000-01-13 04:43:48 +0000390 shell_pgid, terminal, command);
391
Erik Andersen7dc16072000-01-04 01:10:25 +0000392 /* Convert command (char*) into cmd (char**, one word per string) */
393 for (tmpCmd=command, i=0; (tmpCmd=strsep(&command, " \t")) != NULL;) {
394 if (*tmpCmd != '\0') {
395 cmd[i] = tmpCmd;
Erik Andersen31638212000-01-15 22:28:50 +0000396 message(LOG, "%s ", tmpCmd);
Erik Andersen7dc16072000-01-04 01:10:25 +0000397 tmpCmd++;
398 i++;
399 }
400 }
401 cmd[i] = NULL;
Erik Andersen31638212000-01-15 22:28:50 +0000402 message(LOG, "'\r\n");
Erik Andersen7dc16072000-01-04 01:10:25 +0000403
Eric Andersenc7c41d31999-10-28 00:24:35 +0000404 /* Now run it. The new program will take over this PID,
Eric Andersena7456061999-10-27 02:31:32 +0000405 * so nothing further in init.c should be run. */
Erik Andersenac6e71f2000-01-08 22:04:33 +0000406 execve(cmd[0], cmd, environment);
Eric Andersen0460ff21999-10-25 23:32:44 +0000407
Erik Andersen7dc16072000-01-04 01:10:25 +0000408 /* We're still here? Some error happened. */
409 message(LOG|CONSOLE, "Bummer, could not run '%s': %s\n", cmd[0],
410 strerror(errno));
Eric Andersen0460ff21999-10-25 23:32:44 +0000411 exit(-1);
412 }
413 return pid;
414}
415
Erik Andersen0e3782f2000-01-07 02:54:55 +0000416static int waitfor(char* command,
417 char *terminal, int get_enter)
418{
419 int status, wpid;
420 int pid = run( command, terminal, get_enter);
421
422 while (1) {
423 wpid = wait(&status);
424 if (wpid > 0 ) {
425 message(LOG, "Process '%s' (pid %d) exited.\n",
426 command, wpid);
427 break;
428 }
429 if (wpid == pid )
430 break;
431 }
432 return wpid;
433}
434
Eric Andersen219d6f51999-11-02 19:43:01 +0000435/* Make sure there is enough memory to do something useful. *
436 * Calls swapon if needed so be sure /proc is mounted. */
437static void check_memory()
438{
Erik Andersen0e3782f2000-01-07 02:54:55 +0000439 struct stat statBuf;
Eric Andersen219d6f51999-11-02 19:43:01 +0000440
441 if (mem_total() > 3500)
442 return;
443
Erik Andersen0e3782f2000-01-07 02:54:55 +0000444 if (stat("/etc/fstab", &statBuf) == 0) {
Eric Andersen219d6f51999-11-02 19:43:01 +0000445 /* Try to turn on swap */
Erik Andersen0e3782f2000-01-07 02:54:55 +0000446 waitfor("/bin/swapon swapon -a", log, FALSE);
Eric Andersen219d6f51999-11-02 19:43:01 +0000447 if (mem_total() < 3500)
448 goto goodnight;
449 } else
450 goto goodnight;
451 return;
452
453goodnight:
Eric Andersen3ae0c781999-11-04 01:13:21 +0000454 message(CONSOLE, "Sorry, your computer does not have enough memory.\r\n");
Eric Andersen219d6f51999-11-02 19:43:01 +0000455 while (1) sleep(1);
456}
457
Erik Andersen7dc16072000-01-04 01:10:25 +0000458#ifndef DEBUG_INIT
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000459static void shutdown_system(void)
Eric Andersencc8ed391999-10-05 16:24:54 +0000460{
Eric Andersen0460ff21999-10-25 23:32:44 +0000461 /* Allow Ctrl-Alt-Del to reboot system. */
462 reboot(RB_ENABLE_CAD);
Eric Andersen08b10341999-11-19 02:38:58 +0000463 message(CONSOLE, "\r\nThe system is going down NOW !!\r\n");
Eric Andersen3ae0c781999-11-04 01:13:21 +0000464 sync();
Erik Andersen7dc16072000-01-04 01:10:25 +0000465
Eric Andersen0460ff21999-10-25 23:32:44 +0000466 /* Send signals to every process _except_ pid 1 */
Eric Andersen3ae0c781999-11-04 01:13:21 +0000467 message(CONSOLE, "Sending SIGHUP to all processes.\r\n");
Eric Andersen0460ff21999-10-25 23:32:44 +0000468 kill(-1, SIGHUP);
469 sleep(2);
470 sync();
Erik Andersen7dc16072000-01-04 01:10:25 +0000471
Eric Andersen3ae0c781999-11-04 01:13:21 +0000472 message(CONSOLE, "Sending SIGKILL to all processes.\r\n");
Eric Andersen0460ff21999-10-25 23:32:44 +0000473 kill(-1, SIGKILL);
474 sleep(1);
Erik Andersen7dc16072000-01-04 01:10:25 +0000475
Eric Andersen08b10341999-11-19 02:38:58 +0000476 message(CONSOLE, "Disabling swap.\r\n");
Erik Andersen0e3782f2000-01-07 02:54:55 +0000477 waitfor( "swapoff -a", console, FALSE);
Eric Andersen08b10341999-11-19 02:38:58 +0000478 message(CONSOLE, "Unmounting filesystems.\r\n");
Erik Andersen0e3782f2000-01-07 02:54:55 +0000479 waitfor("umount -a", console, FALSE);
Eric Andersen0460ff21999-10-25 23:32:44 +0000480 sync();
Erik Andersenac6e71f2000-01-08 22:04:33 +0000481 if (kernelVersion > 0 && kernelVersion <= 2 * 65536 + 2 * 256 + 11) {
Eric Andersencf8c9cf1999-11-05 00:31:46 +0000482 /* bdflush, kupdate not needed for kernels >2.2.11 */
483 bdflush(1, 0);
484 sync();
485 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000486}
487
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000488static void halt_signal(int sig)
Eric Andersencc8ed391999-10-05 16:24:54 +0000489{
Eric Andersenabc7d591999-10-19 00:27:50 +0000490 shutdown_system();
Erik Andersen3fe39dc2000-01-25 18:13:53 +0000491 message(CONSOLE, "The system is halted. Press %s or turn off power\r\n",
492 (secondConsole == NULL) /* serial console */
493 ? "Reset" : "CTRL-ALT-DEL");
Eric Andersen3ae0c781999-11-04 01:13:21 +0000494 sync();
Erik Andersen3fe39dc2000-01-25 18:13:53 +0000495
496 /* allow time for last message to reach serial console */
497 sleep(2);
498
Erik Andersen4d1d0111999-12-17 18:44:15 +0000499#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,2,0)
Eric Andersen2cb55071999-12-10 08:25:07 +0000500 if (sig == SIGUSR2)
501 reboot(RB_POWER_OFF);
502 else
Erik Andersen4d1d0111999-12-17 18:44:15 +0000503#endif
Erik Andersen7dc16072000-01-04 01:10:25 +0000504 reboot(RB_HALT_SYSTEM);
Eric Andersenabc7d591999-10-19 00:27:50 +0000505 exit(0);
Eric Andersencc8ed391999-10-05 16:24:54 +0000506}
507
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000508static void reboot_signal(int sig)
Eric Andersencc8ed391999-10-05 16:24:54 +0000509{
Eric Andersenabc7d591999-10-19 00:27:50 +0000510 shutdown_system();
Eric Andersen3ae0c781999-11-04 01:13:21 +0000511 message(CONSOLE, "Please stand by while rebooting the system.\r\n");
512 sync();
Erik Andersen3fe39dc2000-01-25 18:13:53 +0000513
514 /* allow time for last message to reach serial console */
515 sleep(2);
516
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000517 reboot(RB_AUTOBOOT);
Eric Andersenabc7d591999-10-19 00:27:50 +0000518 exit(0);
Eric Andersencc8ed391999-10-05 16:24:54 +0000519}
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000520
Erik Andersende552872000-01-23 01:34:05 +0000521#if defined BB_FEATURE_INIT_CHROOT
522static void check_chroot(int sig)
523{
524 char *argv_init[2] = { "init", NULL, };
525 char *envp_init[3] = { "HOME=/", "TERM=linux", NULL, };
526 char rootpath[256], *tc;
527 int fd;
528
529 if ((fd = open("/proc/sys/kernel/init-chroot", O_RDONLY)) == -1) {
530 message(CONSOLE, "SIGHUP recived, but could not open proc file\r\n");
531 sleep(2);
532 return;
533 }
534 if (read(fd, rootpath, sizeof(rootpath)) == -1) {
535 message(CONSOLE, "SIGHUP recived, but could not read proc file\r\n");
536 sleep(2);
537 return;
538 }
539 close(fd);
540
541 if (rootpath[0] == '\0') {
542 message(CONSOLE, "SIGHUP recived, but new root is not valid: %s\r\n",
543 rootpath);
544 sleep(2);
545 return;
546 }
547
548 tc = strrchr(rootpath, '\n');
549 *tc = '\0';
550
551 /* Ok, making it this far means we commit */
552 message(CONSOLE, "Please stand by, changing root to `%s'.\r\n", rootpath);
553
554 /* kill all other programs first */
555 message(CONSOLE, "Sending SIGTERM to all processes.\r\n");
556 kill(-1, SIGTERM);
557 sleep(2);
558 sync();
559
560 message(CONSOLE, "Sending SIGKILL to all processes.\r\n");
561 kill(-1, SIGKILL);
562 sleep(2);
563 sync();
564
565 /* ok, we don't need /proc anymore. we also assume that the signaling
566 * process left the rest of the filesystems alone for us */
567 umount("/proc");
568
569 /* Ok, now we chroot. Hopefully we only have two things mounted, the
570 * new chroot'd mount point, and the old "/" mount. s,
571 * we go ahead and unmount the old "/". This should trigger the kernel
572 * to set things up the Right Way(tm). */
573
574 if (!chroot(rootpath))
575 umount("/dev/root");
576
577 /* If the chroot fails, we are already too far to turn back, so we
578 * continue and hope that executing init below will revive the system */
579
580 /* close all of our descriptors and open new ones */
581 close(0);
582 close(1);
583 close(2);
584 open("/dev/console", O_RDWR, 0);
585 dup(0);
586 dup(0);
587
588 message(CONSOLE, "Executing real init...\r\n");
589 /* execute init in the (hopefully) new root */
590 execve("/sbin/init",argv_init,envp_init);
591
Erik Andersen3fe39dc2000-01-25 18:13:53 +0000592 message(CONSOLE, "ERROR: Could not exec new init. Press %s to reboot.\r\n",
593 (secondConsole == NULL) /* serial console */
594 ? "Reset" : "CTRL-ALT-DEL");
Erik Andersende552872000-01-23 01:34:05 +0000595 return;
596}
597#endif /* BB_FEATURE_INIT_CHROOT */
598
599#endif /* ! DEBUG_INIT */
Erik Andersen7dc16072000-01-04 01:10:25 +0000600
Erik Andersen96e2abd2000-01-07 11:40:44 +0000601void new_initAction (initActionEnum action,
Erik Andersen9e737252000-01-06 01:16:13 +0000602 char* process, char* cons)
Erik Andersen7dc16072000-01-04 01:10:25 +0000603{
604 initAction* newAction;
Erik Andersen9e737252000-01-06 01:16:13 +0000605
Erik Andersen3fe39dc2000-01-25 18:13:53 +0000606 if (*cons == '\0')
607 cons = console;
608
Erik Andersen9e737252000-01-06 01:16:13 +0000609 /* If BusyBox detects that a serial console is in use,
Erik Andersen3fe39dc2000-01-25 18:13:53 +0000610 * then entries not refering to the console or null devices will _not_ be run.
Erik Andersende552872000-01-23 01:34:05 +0000611 * The exception to this rule is the null device.
Erik Andersen9e737252000-01-06 01:16:13 +0000612 */
Erik Andersen3fe39dc2000-01-25 18:13:53 +0000613 if (secondConsole == NULL && strcmp(cons, console) && strcmp(cons, "/dev/null"))
Erik Andersen9e737252000-01-06 01:16:13 +0000614 return;
615
Erik Andersen7dc16072000-01-04 01:10:25 +0000616 newAction = calloc ((size_t)(1), sizeof(initAction));
617 if (!newAction) {
Erik Andersen0e3782f2000-01-07 02:54:55 +0000618 message(LOG|CONSOLE,"Memory allocation failure\n");
Erik Andersen7dc16072000-01-04 01:10:25 +0000619 while (1) sleep(1);
620 }
621 newAction->nextPtr = initActionList;
622 initActionList = newAction;
623 strncpy( newAction->process, process, 255);
Erik Andersen96e2abd2000-01-07 11:40:44 +0000624 newAction->action = action;
Erik Andersen3fe39dc2000-01-25 18:13:53 +0000625 strncpy(newAction->console, cons, 255);
Erik Andersen7dc16072000-01-04 01:10:25 +0000626 newAction->pid = 0;
Erik Andersen31638212000-01-15 22:28:50 +0000627// message(LOG|CONSOLE, "process='%s' action='%d' console='%s'\n",
628// newAction->process, newAction->action, newAction->console);
Erik Andersen7dc16072000-01-04 01:10:25 +0000629}
630
631void delete_initAction (initAction *action)
632{
633 initAction *a, *b=NULL;
Erik Andersen3fe39dc2000-01-25 18:13:53 +0000634 for( a=initActionList ; a ; b=a, a=a->nextPtr) {
635 if (a == action) {
636 if (b==NULL) {
637 initActionList=a->nextPtr;
638 } else {
639 b->nextPtr=a->nextPtr;
640 }
Erik Andersen7dc16072000-01-04 01:10:25 +0000641 free( a);
642 break;
643 }
644 }
645}
646
Erik Andersen9e737252000-01-06 01:16:13 +0000647/* NOTE that if BB_FEATURE_USE_INITTAB is NOT defined,
648 * then parse_inittab() simply adds in some default
649 * actions(i.e runs INIT_SCRIPT and then starts a pair
Erik Andersen812d4662000-01-07 18:30:40 +0000650 * of "askfirst" shells). If BB_FEATURE_USE_INITTAB
651 * _is_ defined, but /etc/inittab is missing, this
652 * results in the same set of default behaviors.
Erik Andersen9e737252000-01-06 01:16:13 +0000653 * */
Erik Andersen7dc16072000-01-04 01:10:25 +0000654void parse_inittab(void)
655{
Erik Andersen9e737252000-01-06 01:16:13 +0000656#ifdef BB_FEATURE_USE_INITTAB
Erik Andersen7dc16072000-01-04 01:10:25 +0000657 FILE* file;
Erik Andersen0e3782f2000-01-07 02:54:55 +0000658 char buf[256], lineAsRead[256], tmpConsole[256];
Erik Andersen9e737252000-01-06 01:16:13 +0000659 char *p, *q, *r, *s;
Erik Andersen7dc16072000-01-04 01:10:25 +0000660 const struct initActionType *a = actions;
661 int foundIt;
662
663
664 file = fopen(INITTAB, "r");
665 if (file == NULL) {
666 /* No inittab file -- set up some default behavior */
Erik Andersen9e737252000-01-06 01:16:13 +0000667#endif
Erik Andersen7dc16072000-01-04 01:10:25 +0000668 /* Askfirst shell on tty1 */
Erik Andersen96e2abd2000-01-07 11:40:44 +0000669 new_initAction( ASKFIRST, SHELL, console );
Erik Andersen7dc16072000-01-04 01:10:25 +0000670 /* Askfirst shell on tty2 */
Erik Andersenac6e71f2000-01-08 22:04:33 +0000671 if (secondConsole != NULL)
672 new_initAction( ASKFIRST, SHELL, secondConsole );
Erik Andersen7dc16072000-01-04 01:10:25 +0000673 /* sysinit */
Erik Andersen96e2abd2000-01-07 11:40:44 +0000674 new_initAction( SYSINIT, INIT_SCRIPT, console );
Erik Andersen7dc16072000-01-04 01:10:25 +0000675
676 return;
Erik Andersen9e737252000-01-06 01:16:13 +0000677#ifdef BB_FEATURE_USE_INITTAB
Erik Andersen7dc16072000-01-04 01:10:25 +0000678 }
679
680 while ( fgets(buf, 255, file) != NULL) {
681 foundIt=FALSE;
682 for(p = buf; *p == ' ' || *p == '\t'; p++);
683 if (*p == '#' || *p == '\n') continue;
684
685 /* Trim the trailing \n */
686 q = strrchr( p, '\n');
687 if (q != NULL)
688 *q='\0';
689
Erik Andersen9e737252000-01-06 01:16:13 +0000690 /* Keep a copy around for posterity's sake (and error msgs) */
Erik Andersen0e3782f2000-01-07 02:54:55 +0000691 strcpy(lineAsRead, buf);
Erik Andersen9e737252000-01-06 01:16:13 +0000692
693 /* Grab the ID field */
694 s=p;
Erik Andersen7dc16072000-01-04 01:10:25 +0000695 p = strchr( p, ':');
Erik Andersen9e737252000-01-06 01:16:13 +0000696 if ( p != NULL || *(p+1) != '\0' ) {
697 *p='\0';
698 ++p;
699 }
Erik Andersen7dc16072000-01-04 01:10:25 +0000700
701 /* Now peal off the process field from the end
702 * of the string */
703 q = strrchr( p, ':');
704 if ( q == NULL || *(q+1) == '\0' ) {
Erik Andersen0e3782f2000-01-07 02:54:55 +0000705 message(LOG|CONSOLE,"Bad inittab entry: %s\n", lineAsRead);
Erik Andersen7dc16072000-01-04 01:10:25 +0000706 continue;
707 } else {
708 *q='\0';
709 ++q;
710 }
711
712 /* Now peal off the action field */
713 r = strrchr( p, ':');
714 if ( r == NULL || *(r+1) == '\0') {
Erik Andersen0e3782f2000-01-07 02:54:55 +0000715 message(LOG|CONSOLE,"Bad inittab entry: %s\n", lineAsRead);
Erik Andersen7dc16072000-01-04 01:10:25 +0000716 continue;
717 } else {
718 ++r;
719 }
720
721 /* Ok, now process it */
722 a = actions;
723 while (a->name != 0) {
724 if (strcmp(a->name, r) == 0) {
Erik Andersen0e3782f2000-01-07 02:54:55 +0000725 if (*s != '\0') {
726 struct stat statBuf;
727 strcpy(tmpConsole, "/dev/");
728 strncat(tmpConsole, s, 200);
729 if (stat(tmpConsole, &statBuf) != 0) {
730 message(LOG|CONSOLE, "device '%s' does not exist. Did you read the directions?\n", tmpConsole);
731 break;
732 }
733 s = tmpConsole;
734 }
Erik Andersen96e2abd2000-01-07 11:40:44 +0000735 new_initAction( a->action, q, s);
Erik Andersen7dc16072000-01-04 01:10:25 +0000736 foundIt=TRUE;
737 }
738 a++;
739 }
740 if (foundIt==TRUE)
741 continue;
742 else {
743 /* Choke on an unknown action */
Erik Andersen0e3782f2000-01-07 02:54:55 +0000744 message(LOG|CONSOLE, "Bad inittab entry: %s\n", lineAsRead);
Erik Andersen7dc16072000-01-04 01:10:25 +0000745 }
746 }
747 return;
Erik Andersen9e737252000-01-06 01:16:13 +0000748#endif
Erik Andersen7dc16072000-01-04 01:10:25 +0000749}
750
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000751extern int init_main(int argc, char **argv)
Eric Andersen0460ff21999-10-25 23:32:44 +0000752{
Erik Andersen7dc16072000-01-04 01:10:25 +0000753 initAction *a;
754 pid_t wpid;
755 int status;
Eric Andersen0460ff21999-10-25 23:32:44 +0000756
Eric Andersend73dc5b1999-11-10 23:13:02 +0000757#ifndef DEBUG_INIT
Erik Andersende552872000-01-23 01:34:05 +0000758 /* Expect to be PID 1 if we are run as init (not linuxrc) */
Eric Andersen07274581999-11-21 21:50:07 +0000759 if (getpid() != 1 && strstr(argv[0], "init")!=NULL ) {
Eric Andersend73dc5b1999-11-10 23:13:02 +0000760 usage( "init\n\nInit is the parent of all processes.\n\n"
761 "This version of init is designed to be run only by the kernel\n");
762 }
Erik Andersen31638212000-01-15 22:28:50 +0000763 /* Fix up argv[0] to be certain we claim to be init */
764 strncpy(argv[0], "init", strlen(argv[0]));
765
Erik Andersen05df2392000-01-13 04:43:48 +0000766 /* Set up sig handlers -- be sure to
767 * clear all of these in run() */
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000768 signal(SIGUSR1, halt_signal);
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000769 signal(SIGUSR2, reboot_signal);
Erik Andersen0881de72000-01-05 09:34:26 +0000770 signal(SIGINT, reboot_signal);
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000771 signal(SIGTERM, reboot_signal);
Erik Andersende552872000-01-23 01:34:05 +0000772#if defined BB_FEATURE_INIT_CHROOT
773 signal(SIGHUP, check_chroot);
774#endif
Eric Andersen0460ff21999-10-25 23:32:44 +0000775
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000776 /* Turn off rebooting via CTL-ALT-DEL -- we get a
777 * SIGINT on CAD so we can shut things down gracefully... */
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000778 reboot(RB_DISABLE_CAD);
Eric Andersen7f1acfd1999-10-29 23:09:13 +0000779#endif
Erik Andersen05df2392000-01-13 04:43:48 +0000780
Eric Andersenc7c41d31999-10-28 00:24:35 +0000781 /* Figure out where the default console should be */
782 console_init();
Eric Andersen0460ff21999-10-25 23:32:44 +0000783
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000784 /* Close whatever files are open, and reset the console. */
785 close(0);
786 close(1);
787 close(2);
Eric Andersen7f1acfd1999-10-29 23:09:13 +0000788 set_term(0);
Erik Andersen05df2392000-01-13 04:43:48 +0000789 setsid();
Eric Andersencc8ed391999-10-05 16:24:54 +0000790
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000791 /* Make sure PATH is set to something sane */
Eric Andersenb186d981999-12-03 09:19:54 +0000792 putenv(_PATH_STDPATH);
Eric Andersencc8ed391999-10-05 16:24:54 +0000793
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000794 /* Hello world */
Eric Andersen7f1acfd1999-10-29 23:09:13 +0000795#ifndef DEBUG_INIT
Erik Andersen0e3782f2000-01-07 02:54:55 +0000796 message(LOG|CONSOLE,
Erik Andersen7dc16072000-01-04 01:10:25 +0000797 "init started: BusyBox v%s (%s) multi-call binary\r\n",
798 BB_VER, BB_BT);
Eric Andersen7f1acfd1999-10-29 23:09:13 +0000799#else
Erik Andersen0e3782f2000-01-07 02:54:55 +0000800 message(LOG|CONSOLE,
Erik Andersen7dc16072000-01-04 01:10:25 +0000801 "init(%d) started: BusyBox v%s (%s) multi-call binary\r\n",
802 getpid(), BB_VER, BB_BT);
Eric Andersen7f1acfd1999-10-29 23:09:13 +0000803#endif
Eric Andersencc8ed391999-10-05 16:24:54 +0000804
Eric Andersenc7c41d31999-10-28 00:24:35 +0000805
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000806 /* Mount /proc */
Eric Andersene18c75a1999-11-05 04:23:05 +0000807 if (mount ("proc", "/proc", "proc", 0, 0) == 0) {
Erik Andersen0e3782f2000-01-07 02:54:55 +0000808 message(LOG|CONSOLE, "Mounting /proc: done.\n");
Erik Andersenac6e71f2000-01-08 22:04:33 +0000809 kernelVersion = get_kernel_revision();
Eric Andersene18c75a1999-11-05 04:23:05 +0000810 } else
Erik Andersen0e3782f2000-01-07 02:54:55 +0000811 message(LOG|CONSOLE, "Mounting /proc: failed!\n");
Eric Andersencc8ed391999-10-05 16:24:54 +0000812
Eric Andersen219d6f51999-11-02 19:43:01 +0000813 /* Make sure there is enough memory to do something useful. */
814 check_memory();
Eric Andersencc8ed391999-10-05 16:24:54 +0000815
Eric Andersend00c2621999-12-07 08:37:31 +0000816 /* Check if we are supposed to be in single user mode */
817 if ( argc > 1 && (!strcmp(argv[1], "single") ||
Erik Andersen0881de72000-01-05 09:34:26 +0000818 !strcmp(argv[1], "-s") || !strcmp(argv[1], "1")))
819 {
Erik Andersen7dc16072000-01-04 01:10:25 +0000820 /* Ask first then start a shell on tty2 */
Erik Andersenac6e71f2000-01-08 22:04:33 +0000821 if (secondConsole != NULL)
822 new_initAction( ASKFIRST, SHELL, secondConsole);
Erik Andersen3fe39dc2000-01-25 18:13:53 +0000823 /* Start a shell on tty1 */
824 new_initAction( RESPAWN, SHELL, console);
Erik Andersen7dc16072000-01-04 01:10:25 +0000825 } else {
826 /* Not in single user mode -- see what inittab says */
Erik Andersen9e737252000-01-06 01:16:13 +0000827
828 /* NOTE that if BB_FEATURE_USE_INITTAB is NOT defined,
829 * then parse_inittab() simply adds in some default
830 * actions(i.e runs INIT_SCRIPT and then starts a pair
831 * of "askfirst" shells */
Erik Andersen7dc16072000-01-04 01:10:25 +0000832 parse_inittab();
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000833 }
834
Erik Andersen7dc16072000-01-04 01:10:25 +0000835 /* Now run everything that needs to be run */
Eric Andersen84b00921999-12-11 04:16:51 +0000836
Erik Andersen0881de72000-01-05 09:34:26 +0000837 /* First run the sysinit command */
Erik Andersen7dc16072000-01-04 01:10:25 +0000838 for( a=initActionList ; a; a=a->nextPtr) {
839 if (a->action == SYSINIT) {
Erik Andersen96e2abd2000-01-07 11:40:44 +0000840 waitfor(a->process, a->console, FALSE);
Erik Andersen7dc16072000-01-04 01:10:25 +0000841 /* Now remove the "sysinit" entry from the list */
842 delete_initAction( a);
Eric Andersend00c2621999-12-07 08:37:31 +0000843 }
844 }
Erik Andersen7dc16072000-01-04 01:10:25 +0000845 /* Next run anything that wants to block */
846 for( a=initActionList ; a; a=a->nextPtr) {
847 if (a->action == WAIT) {
Erik Andersen96e2abd2000-01-07 11:40:44 +0000848 waitfor(a->process, a->console, FALSE);
Erik Andersen7dc16072000-01-04 01:10:25 +0000849 /* Now remove the "wait" entry from the list */
850 delete_initAction( a);
851 }
852 }
853 /* Next run anything to be run only once */
854 for( a=initActionList ; a; a=a->nextPtr) {
855 if (a->action == ONCE) {
Erik Andersen96e2abd2000-01-07 11:40:44 +0000856 run(a->process, a->console, FALSE);
Erik Andersen7dc16072000-01-04 01:10:25 +0000857 /* Now remove the "once" entry from the list */
858 delete_initAction( a);
859 }
860 }
Erik Andersen0e3782f2000-01-07 02:54:55 +0000861 /* If there is nothing else to do, stop */
862 if (initActionList == NULL) {
863 message(LOG|CONSOLE, "No more tasks for init -- sleeping forever.\n");
864 while (1) sleep(1);
865 }
Eric Andersen219d6f51999-11-02 19:43:01 +0000866
Erik Andersen0881de72000-01-05 09:34:26 +0000867 /* Now run the looping stuff for the rest of forever */
868 while (1) {
Erik Andersen7dc16072000-01-04 01:10:25 +0000869 for( a=initActionList ; a; a=a->nextPtr) {
870 /* Only run stuff with pid==0. If they have
871 * a pid, that means they are still running */
872 if (a->pid == 0) {
873 switch(a->action) {
874 case RESPAWN:
875 /* run the respawn stuff */
Erik Andersen96e2abd2000-01-07 11:40:44 +0000876 a->pid = run(a->process, a->console, FALSE);
Erik Andersen7dc16072000-01-04 01:10:25 +0000877 break;
878 case ASKFIRST:
879 /* run the askfirst stuff */
Erik Andersen96e2abd2000-01-07 11:40:44 +0000880 a->pid = run(a->process, a->console, TRUE);
Erik Andersen7dc16072000-01-04 01:10:25 +0000881 break;
Erik Andersen0881de72000-01-05 09:34:26 +0000882 /* silence the compiler's incessant whining */
Erik Andersen7dc16072000-01-04 01:10:25 +0000883 default:
884 break;
885 }
886 }
887 }
Erik Andersen0881de72000-01-05 09:34:26 +0000888 /* Wait for a child process to exit */
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000889 wpid = wait(&status);
Eric Andersena7456061999-10-27 02:31:32 +0000890 if (wpid > 0 ) {
Erik Andersen0881de72000-01-05 09:34:26 +0000891 /* Find out who died and clean up their corpse */
Erik Andersen7dc16072000-01-04 01:10:25 +0000892 for( a=initActionList ; a; a=a->nextPtr) {
893 if (a->pid==wpid) {
894 a->pid=0;
Erik Andersen0881de72000-01-05 09:34:26 +0000895 message(LOG, "Process '%s' (pid %d) exited. Scheduling it for restart.\n",
896 a->process, wpid);
Erik Andersen7dc16072000-01-04 01:10:25 +0000897 }
Eric Andersenb6a44b81999-11-13 04:47:09 +0000898 }
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000899 }
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000900 sleep(1);
901 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000902}
Erik Andersen9c88cac1999-12-30 09:25:17 +0000903