blob: 722ccd21701fa45d54a5da12eef862dae25a60cb [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>
Eric Andersen8a8fbb81999-10-26 00:18:56 +000043#include <linux/serial.h> /* for serial_struct */
44#include <sys/vt.h> /* for vt_stat */
Eric Andersenabc7d591999-10-19 00:27:50 +000045#include <sys/ioctl.h>
Erik Andersen4d1d0111999-12-17 18:44:15 +000046#include <linux/version.h>
Eric Andersen4c781471999-11-26 08:12:56 +000047#ifdef BB_SYSLOGD
48#include <sys/syslog.h>
49#endif
Eric Andersencc8ed391999-10-05 16:24:54 +000050
Eric Andersen0ecb54a1999-12-05 23:24:55 +000051#if ! defined BB_FEATURE_USE_PROCFS
52#error Sorry, I depend on the /proc filesystem right now.
53#endif
54
Erik Andersen4d1d0111999-12-17 18:44:15 +000055#ifndef KERNEL_VERSION
56#define KERNEL_VERSION(a,b,c) (((a) << 16) + ((b) << 8) + (c))
57#endif
58
Eric Andersen0ecb54a1999-12-05 23:24:55 +000059
Erik Andersen7dc16072000-01-04 01:10:25 +000060#define VT_PRIMARY "/dev/tty1" /* Primary virtual console */
61#define VT_SECONDARY "/dev/tty2" /* Virtual console */
62#define VT_LOG "/dev/tty3" /* Virtual console */
63#define SERIAL_CON0 "/dev/ttyS0" /* Primary serial console */
64#define SERIAL_CON1 "/dev/ttyS1" /* Serial console */
Erik Andersen96e2abd2000-01-07 11:40:44 +000065#define SHELL "/bin/sh" /* Default shell */
Erik Andersen7dc16072000-01-04 01:10:25 +000066#define INITTAB "/etc/inittab" /* inittab file location */
Erik Andersen96e2abd2000-01-07 11:40:44 +000067#ifndef INIT_SCRIPT
Erik Andersen7dc16072000-01-04 01:10:25 +000068#define INIT_SCRIPT "/etc/init.d/rcS" /* Default sysinit script. */
Erik Andersen96e2abd2000-01-07 11:40:44 +000069#endif
Erik Andersen9c88cac1999-12-30 09:25:17 +000070
Eric Andersen3ae0c781999-11-04 01:13:21 +000071#define LOG 0x1
72#define CONSOLE 0x2
Erik Andersen7dc16072000-01-04 01:10:25 +000073
74/* Allowed init action types */
75typedef enum {
76 SYSINIT=1,
Erik Andersen7dc16072000-01-04 01:10:25 +000077 RESPAWN,
78 ASKFIRST,
79 WAIT,
80 ONCE
81} initActionEnum;
82
83/* And now a list of the actions we support in the version of init */
84typedef struct initActionType{
85 const char* name;
86 initActionEnum action;
87} initActionType;
88
89static const struct initActionType actions[] = {
90 {"sysinit", SYSINIT},
Erik Andersen7dc16072000-01-04 01:10:25 +000091 {"respawn", RESPAWN},
92 {"askfirst", ASKFIRST},
93 {"wait", WAIT},
94 {"once", ONCE},
95 {0}
96};
97
98/* Set up a linked list of initactions, to be read from inittab */
99typedef struct initActionTag initAction;
100struct initActionTag {
101 pid_t pid;
102 char process[256];
Erik Andersen9e737252000-01-06 01:16:13 +0000103 char console[256];
Erik Andersen7dc16072000-01-04 01:10:25 +0000104 initAction *nextPtr;
105 initActionEnum action;
106};
107initAction* initActionList = NULL;
108
109
Erik Andersenac6e71f2000-01-08 22:04:33 +0000110static char *secondConsole = VT_SECONDARY;
Eric Andersenbe971d61999-11-03 16:52:50 +0000111static char *log = VT_LOG;
Erik Andersenac6e71f2000-01-08 22:04:33 +0000112static int kernelVersion = 0;
Erik Andersen31638212000-01-15 22:28:50 +0000113static char termType[32] = "TERM=ansi";
114static char console[32] = _PATH_CONSOLE;
Eric Andersen0460ff21999-10-25 23:32:44 +0000115
116
117/* try to open up the specified device */
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000118int device_open(char *device, int mode)
Eric Andersencc8ed391999-10-05 16:24:54 +0000119{
Eric Andersen0460ff21999-10-25 23:32:44 +0000120 int m, f, fd = -1;
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000121
Eric Andersen7f1acfd1999-10-29 23:09:13 +0000122 m = mode | O_NONBLOCK;
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000123
Eric Andersen0460ff21999-10-25 23:32:44 +0000124 /* Retry up to 5 times */
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000125 for (f = 0; f < 5; f++)
126 if ((fd = open(device, m)) >= 0)
127 break;
128 if (fd < 0)
129 return fd;
Eric Andersen3ae0c781999-11-04 01:13:21 +0000130 /* Reset original flags. */
Eric Andersen0460ff21999-10-25 23:32:44 +0000131 if (m != mode)
132 fcntl(fd, F_SETFL, mode);
133 return fd;
134}
Eric Andersencc8ed391999-10-05 16:24:54 +0000135
Eric Andersen3ae0c781999-11-04 01:13:21 +0000136/* print a message to the specified device:
137 * device may be bitwise-or'd from LOG | CONSOLE */
138void message(int device, char *fmt, ...)
Eric Andersen0460ff21999-10-25 23:32:44 +0000139{
Eric Andersen0460ff21999-10-25 23:32:44 +0000140 va_list arguments;
Erik Andersen7dc16072000-01-04 01:10:25 +0000141 int fd;
142
Eric Andersen4c781471999-11-26 08:12:56 +0000143#ifdef BB_SYSLOGD
144
145 /* Log the message to syslogd */
146 if (device & LOG ) {
147 char msg[1024];
148 va_start(arguments, fmt);
149 vsnprintf(msg, sizeof(msg), fmt, arguments);
150 va_end(arguments);
Erik Andersende552872000-01-23 01:34:05 +0000151 openlog( "init", 0, LOG_DAEMON);
Eric Andersen4c781471999-11-26 08:12:56 +0000152 syslog(LOG_DAEMON|LOG_NOTICE, msg);
Erik Andersende552872000-01-23 01:34:05 +0000153 closelog();
Eric Andersen4c781471999-11-26 08:12:56 +0000154 }
155
156#else
157 static int log_fd=-1;
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000158
Eric Andersen3ae0c781999-11-04 01:13:21 +0000159 /* Take full control of the log tty, and never close it.
160 * It's mine, all mine! Muhahahaha! */
Eric Andersen07e52971999-11-07 07:38:08 +0000161 if (log_fd < 0) {
162 if (log == NULL) {
163 /* don't even try to log, because there is no such console */
164 log_fd = -2;
165 /* log to main console instead */
166 device = CONSOLE;
167 }
168 else if ((log_fd = device_open(log, O_RDWR|O_NDELAY)) < 0) {
Eric Andersen3ae0c781999-11-04 01:13:21 +0000169 log_fd=-1;
170 fprintf(stderr, "Bummer, can't write to log on %s!\r\n", log);
171 fflush(stderr);
172 return;
173 }
174 }
Eric Andersen07e52971999-11-07 07:38:08 +0000175 if ( (device & LOG) && (log_fd >= 0) ) {
Eric Andersen0460ff21999-10-25 23:32:44 +0000176 va_start(arguments, fmt);
Eric Andersen3ae0c781999-11-04 01:13:21 +0000177 vdprintf(log_fd, fmt, arguments);
Eric Andersena7456061999-10-27 02:31:32 +0000178 va_end(arguments);
179 }
Eric Andersen4c781471999-11-26 08:12:56 +0000180#endif
181
Eric Andersen3ae0c781999-11-04 01:13:21 +0000182 if (device & CONSOLE) {
Eric Andersended62591999-11-18 00:19:26 +0000183 /* Always send console messages to /dev/console so people will see them. */
Eric Andersenb186d981999-12-03 09:19:54 +0000184 if ((fd = device_open(_PATH_CONSOLE, O_WRONLY|O_NOCTTY|O_NDELAY)) >= 0) {
Eric Andersen3ae0c781999-11-04 01:13:21 +0000185 va_start(arguments, fmt);
186 vdprintf(fd, fmt, arguments);
187 va_end(arguments);
188 close(fd);
189 } else {
190 fprintf(stderr, "Bummer, can't print: ");
191 va_start(arguments, fmt);
192 vfprintf(stderr, fmt, arguments);
193 fflush(stderr);
194 va_end(arguments);
195 }
196 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000197}
198
Eric Andersen3ae0c781999-11-04 01:13:21 +0000199
Eric Andersen0460ff21999-10-25 23:32:44 +0000200/* Set terminal settings to reasonable defaults */
Eric Andersenc7c41d31999-10-28 00:24:35 +0000201void set_term( int fd)
Eric Andersencc8ed391999-10-05 16:24:54 +0000202{
Eric Andersen0460ff21999-10-25 23:32:44 +0000203 struct termios tty;
Eric Andersen08b10341999-11-19 02:38:58 +0000204 static const char control_characters[] = {
205 '\003', '\034', '\177', '\025', '\004', '\0',
206 '\1', '\0', '\021', '\023', '\032', '\0', '\022',
207 '\017', '\027', '\026', '\0'
208 };
Eric Andersencc8ed391999-10-05 16:24:54 +0000209
Eric Andersenc7c41d31999-10-28 00:24:35 +0000210 tcgetattr(fd, &tty);
Eric Andersencc8ed391999-10-05 16:24:54 +0000211
Eric Andersen08b10341999-11-19 02:38:58 +0000212 /* set control chars */
213 memcpy(tty.c_cc, control_characters, sizeof(control_characters));
Eric Andersencc8ed391999-10-05 16:24:54 +0000214
Eric Andersen219d6f51999-11-02 19:43:01 +0000215 /* use line dicipline 0 */
Eric Andersen7f1acfd1999-10-29 23:09:13 +0000216 tty.c_line = 0;
217
Eric Andersen08b10341999-11-19 02:38:58 +0000218 /* Make it be sane */
219 //tty.c_cflag &= CBAUD|CBAUDEX|CSIZE|CSTOPB|PARENB|PARODD;
220 //tty.c_cflag |= HUPCL|CLOCAL;
221
222 /* input modes */
223 tty.c_iflag = ICRNL|IXON|IXOFF;
224
Eric Andersen7f1acfd1999-10-29 23:09:13 +0000225 /* output modes */
Eric Andersenc7c41d31999-10-28 00:24:35 +0000226 tty.c_oflag = OPOST|ONLCR;
Eric Andersen7f1acfd1999-10-29 23:09:13 +0000227
228 /* local modes */
Eric Andersen08b10341999-11-19 02:38:58 +0000229 tty.c_lflag = ISIG|ICANON|ECHO|ECHOE|ECHOK|ECHOCTL|ECHOKE|IEXTEN;
Eric Andersencc8ed391999-10-05 16:24:54 +0000230
Eric Andersenc7c41d31999-10-28 00:24:35 +0000231 tcsetattr(fd, TCSANOW, &tty);
Eric Andersencc8ed391999-10-05 16:24:54 +0000232}
233
Eric Andersen219d6f51999-11-02 19:43:01 +0000234/* How much memory does this machine have? */
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000235static int mem_total()
Eric Andersencc8ed391999-10-05 16:24:54 +0000236{
Eric Andersenabc7d591999-10-19 00:27:50 +0000237 char s[80];
Eric Andersen219d6f51999-11-02 19:43:01 +0000238 char *p = "/proc/meminfo";
Eric Andersenabc7d591999-10-19 00:27:50 +0000239 FILE *f;
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000240 const char pattern[] = "MemTotal:";
Eric Andersencc8ed391999-10-05 16:24:54 +0000241
Eric Andersen219d6f51999-11-02 19:43:01 +0000242 if ((f = fopen(p, "r")) < 0) {
Eric Andersen3ae0c781999-11-04 01:13:21 +0000243 message(LOG, "Error opening %s: %s\n", p, strerror( errno));
Eric Andersen219d6f51999-11-02 19:43:01 +0000244 return -1;
245 }
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000246 while (NULL != fgets(s, 79, f)) {
247 p = strstr(s, pattern);
Eric Andersenabc7d591999-10-19 00:27:50 +0000248 if (NULL != p) {
249 fclose(f);
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000250 return (atoi(p + strlen(pattern)));
Eric Andersenabc7d591999-10-19 00:27:50 +0000251 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000252 }
Eric Andersenabc7d591999-10-19 00:27:50 +0000253 return -1;
Eric Andersencc8ed391999-10-05 16:24:54 +0000254}
255
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000256static void console_init()
Eric Andersen0460ff21999-10-25 23:32:44 +0000257{
258 int fd;
259 int tried_devcons = 0;
Eric Andersen219d6f51999-11-02 19:43:01 +0000260 int tried_vtprimary = 0;
Eric Andersen07e52971999-11-07 07:38:08 +0000261 struct serial_struct sr;
Eric Andersen0460ff21999-10-25 23:32:44 +0000262 char *s;
263
Erik Andersen31638212000-01-15 22:28:50 +0000264 if ((s = getenv("TERM")) != NULL) {
265 snprintf(termType,sizeof(termType)-1,"TERM=%s",s);
Erik Andersenac6e71f2000-01-08 22:04:33 +0000266 }
267
268 if ((s = getenv("CONSOLE")) != NULL) {
Erik Andersen31638212000-01-15 22:28:50 +0000269 snprintf(console, sizeof(console)-1, "%s",s);
Eric Andersen07e52971999-11-07 07:38:08 +0000270 }
Eric Andersenfbb39c81999-11-08 17:00:52 +0000271#if #cpu(sparc)
Eric Andersen07e52971999-11-07 07:38:08 +0000272 /* sparc kernel supports console=tty[ab] parameter which is also
273 * passed to init, so catch it here */
Erik Andersende552872000-01-23 01:34:05 +0000274 else if ((s = getenv("console")) != NULL) {
Eric Andersen07e52971999-11-07 07:38:08 +0000275 /* remap tty[ab] to /dev/ttyS[01] */
276 if (strcmp( s, "ttya" )==0)
Erik Andersen31638212000-01-15 22:28:50 +0000277 snprintf(console, sizeof(console)-1, "%s", SERIAL_CON0);
Eric Andersen07e52971999-11-07 07:38:08 +0000278 else if (strcmp( s, "ttyb" )==0)
Erik Andersen31638212000-01-15 22:28:50 +0000279 snprintf(console, sizeof(console)-1, "%s", SERIAL_CON1);
Eric Andersen07e52971999-11-07 07:38:08 +0000280 }
Eric Andersen219d6f51999-11-02 19:43:01 +0000281#endif
Eric Andersen07e52971999-11-07 07:38:08 +0000282 else {
283 struct vt_stat vt;
Eric Andersen07e52971999-11-07 07:38:08 +0000284
Eric Andersen07e52971999-11-07 07:38:08 +0000285 /* 2.2 kernels: identify the real console backend and try to use it */
Eric Andersen08b10341999-11-19 02:38:58 +0000286 if (ioctl(0, TIOCGSERIAL, &sr) == 0) {
Eric Andersen07e52971999-11-07 07:38:08 +0000287 /* this is a serial console */
Erik Andersen31638212000-01-15 22:28:50 +0000288 snprintf(console, sizeof(console)-1, "/dev/ttyS%d", sr.line);
Eric Andersen07e52971999-11-07 07:38:08 +0000289 }
290 else if (ioctl(0, VT_GETSTATE, &vt) == 0) {
291 /* this is linux virtual tty */
Erik Andersen31638212000-01-15 22:28:50 +0000292 snprintf(console, sizeof(console)-1, "/dev/tty%d", vt.v_active);
Eric Andersen07e52971999-11-07 07:38:08 +0000293 } else {
Erik Andersen31638212000-01-15 22:28:50 +0000294 snprintf(console, sizeof(console)-1, "%s", _PATH_CONSOLE);
Eric Andersen07e52971999-11-07 07:38:08 +0000295 tried_devcons++;
296 }
Eric Andersen0460ff21999-10-25 23:32:44 +0000297 }
Eric Andersena7456061999-10-27 02:31:32 +0000298
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000299 while ((fd = open(console, O_RDONLY | O_NONBLOCK)) < 0) {
Eric Andersen219d6f51999-11-02 19:43:01 +0000300 /* Can't open selected console -- try /dev/console */
Eric Andersen0460ff21999-10-25 23:32:44 +0000301 if (!tried_devcons) {
302 tried_devcons++;
Erik Andersen31638212000-01-15 22:28:50 +0000303 snprintf(console, sizeof(console)-1, "%s", _PATH_CONSOLE);
Eric Andersen0460ff21999-10-25 23:32:44 +0000304 continue;
305 }
Eric Andersenbe971d61999-11-03 16:52:50 +0000306 /* Can't open selected console -- try vt1 */
307 if (!tried_vtprimary) {
308 tried_vtprimary++;
Erik Andersen31638212000-01-15 22:28:50 +0000309 snprintf(console, sizeof(console)-1, "%s", VT_PRIMARY);
Eric Andersenbe971d61999-11-03 16:52:50 +0000310 continue;
311 }
Eric Andersen0460ff21999-10-25 23:32:44 +0000312 break;
313 }
Erik Andersen31638212000-01-15 22:28:50 +0000314 if (fd < 0) {
Eric Andersen219d6f51999-11-02 19:43:01 +0000315 /* Perhaps we should panic here? */
Erik Andersen31638212000-01-15 22:28:50 +0000316 snprintf(console, sizeof(console)-1, "/dev/null");
317 } else {
Eric Andersen07e52971999-11-07 07:38:08 +0000318 /* check for serial console and disable logging to tty3 & running a
319 * shell to tty2 */
320 if (ioctl(0,TIOCGSERIAL,&sr) == 0) {
Erik Andersen31638212000-01-15 22:28:50 +0000321 message(LOG|CONSOLE, "serial console detected. Disabling virtual terminals.\r\n" );
Eric Andersen07e52971999-11-07 07:38:08 +0000322 log = NULL;
Erik Andersenac6e71f2000-01-08 22:04:33 +0000323 secondConsole = NULL;
Eric Andersen07e52971999-11-07 07:38:08 +0000324 }
Eric Andersen0460ff21999-10-25 23:32:44 +0000325 close(fd);
Eric Andersen07e52971999-11-07 07:38:08 +0000326 }
Eric Andersen3ae0c781999-11-04 01:13:21 +0000327 message(LOG, "console=%s\n", console );
Eric Andersen0460ff21999-10-25 23:32:44 +0000328}
329
Erik Andersen7dc16072000-01-04 01:10:25 +0000330static pid_t run(char* command,
Eric Andersenc7c41d31999-10-28 00:24:35 +0000331 char *terminal, int get_enter)
Eric Andersen0460ff21999-10-25 23:32:44 +0000332{
Erik Andersen05df2392000-01-13 04:43:48 +0000333 int i, fd;
Eric Andersenc7c41d31999-10-28 00:24:35 +0000334 pid_t pid;
Erik Andersen7dc16072000-01-04 01:10:25 +0000335 char* tmpCmd;
336 char* cmd[255];
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000337 static const char press_enter[] =
Eric Andersen0460ff21999-10-25 23:32:44 +0000338 "\nPlease press Enter to activate this console. ";
Erik Andersen31638212000-01-15 22:28:50 +0000339 char* environment[] = {
Erik Andersenac6e71f2000-01-08 22:04:33 +0000340 "HOME=/",
341 "PATH=/usr/bin:/bin:/usr/sbin:/sbin",
342 "SHELL=/bin/sh",
Erik Andersen31638212000-01-15 22:28:50 +0000343 termType,
Erik Andersenac6e71f2000-01-08 22:04:33 +0000344 "USER=root",
345 0
346 };
Erik Andersenac6e71f2000-01-08 22:04:33 +0000347
Eric Andersen0460ff21999-10-25 23:32:44 +0000348
Eric Andersen0460ff21999-10-25 23:32:44 +0000349 if ((pid = fork()) == 0) {
Erik Andersen0881de72000-01-05 09:34:26 +0000350 pid_t shell_pgid = getpid ();
351
Eric Andersen0460ff21999-10-25 23:32:44 +0000352 /* Clean up */
353 close(0);
354 close(1);
355 close(2);
356 setsid();
357
Eric Andersen2f6c04f1999-11-01 23:59:44 +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);
Erik Andersen5cbdd712000-01-26 20:06:48 +0000363 signal(SIGHUP, SIG_DFL);
Eric Andersen2f6c04f1999-11-01 23:59:44 +0000364
Erik Andersen05df2392000-01-13 04:43:48 +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 }
Erik Andersena6d0dbc2000-01-29 06:29:32 +0000369 dup2(fd, 0);
370 dup2(fd, 1);
371 dup2(fd, 2);
Erik Andersen05df2392000-01-13 04:43:48 +0000372 tcsetpgrp (0, getpgrp());
373 set_term(0);
Eric Andersen0460ff21999-10-25 23:32:44 +0000374
Eric Andersen2f6c04f1999-11-01 23:59:44 +0000375 if (get_enter==TRUE) {
Eric Andersen0460ff21999-10-25 23:32:44 +0000376 /*
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 */
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000384 char c;
Eric Andersen3ae0c781999-11-04 01:13:21 +0000385 message(LOG, "Waiting for enter to start '%s' (pid %d, console %s)\r\n",
Erik Andersen0881de72000-01-05 09:34:26 +0000386 command, shell_pgid, terminal );
Erik Andersen7dc16072000-01-04 01:10:25 +0000387 write(fileno(stdout), press_enter, sizeof(press_enter) - 1);
388 read(fileno(stdin), &c, 1);
Eric Andersen0460ff21999-10-25 23:32:44 +0000389 }
390
Erik Andersen05df2392000-01-13 04:43:48 +0000391 /* Log the process name and args */
Erik Andersen31638212000-01-15 22:28:50 +0000392 message(LOG, "Starting pid %d, console %s: '",
Erik Andersen05df2392000-01-13 04:43:48 +0000393 shell_pgid, terminal, command);
394
Erik Andersen7dc16072000-01-04 01:10:25 +0000395 /* Convert command (char*) into cmd (char**, one word per string) */
396 for (tmpCmd=command, i=0; (tmpCmd=strsep(&command, " \t")) != NULL;) {
397 if (*tmpCmd != '\0') {
398 cmd[i] = tmpCmd;
Erik Andersen31638212000-01-15 22:28:50 +0000399 message(LOG, "%s ", tmpCmd);
Erik Andersen7dc16072000-01-04 01:10:25 +0000400 tmpCmd++;
401 i++;
402 }
403 }
404 cmd[i] = NULL;
Erik Andersen31638212000-01-15 22:28:50 +0000405 message(LOG, "'\r\n");
Erik Andersen7dc16072000-01-04 01:10:25 +0000406
Eric Andersenc7c41d31999-10-28 00:24:35 +0000407 /* Now run it. The new program will take over this PID,
Eric Andersena7456061999-10-27 02:31:32 +0000408 * so nothing further in init.c should be run. */
Erik Andersenac6e71f2000-01-08 22:04:33 +0000409 execve(cmd[0], cmd, environment);
Eric Andersen0460ff21999-10-25 23:32:44 +0000410
Erik Andersen7dc16072000-01-04 01:10:25 +0000411 /* We're still here? Some error happened. */
412 message(LOG|CONSOLE, "Bummer, could not run '%s': %s\n", cmd[0],
413 strerror(errno));
Eric Andersen0460ff21999-10-25 23:32:44 +0000414 exit(-1);
415 }
416 return pid;
417}
418
Erik Andersen0e3782f2000-01-07 02:54:55 +0000419static int waitfor(char* command,
420 char *terminal, int get_enter)
421{
422 int status, wpid;
423 int pid = run( command, terminal, get_enter);
424
425 while (1) {
426 wpid = wait(&status);
427 if (wpid > 0 ) {
428 message(LOG, "Process '%s' (pid %d) exited.\n",
429 command, wpid);
430 break;
431 }
432 if (wpid == pid )
433 break;
434 }
435 return wpid;
436}
437
Eric Andersen219d6f51999-11-02 19:43:01 +0000438/* Make sure there is enough memory to do something useful. *
439 * Calls swapon if needed so be sure /proc is mounted. */
440static void check_memory()
441{
Erik Andersen0e3782f2000-01-07 02:54:55 +0000442 struct stat statBuf;
Eric Andersen219d6f51999-11-02 19:43:01 +0000443
444 if (mem_total() > 3500)
445 return;
446
Erik Andersen0e3782f2000-01-07 02:54:55 +0000447 if (stat("/etc/fstab", &statBuf) == 0) {
Eric Andersen219d6f51999-11-02 19:43:01 +0000448 /* Try to turn on swap */
Erik Andersen0e3782f2000-01-07 02:54:55 +0000449 waitfor("/bin/swapon swapon -a", log, FALSE);
Eric Andersen219d6f51999-11-02 19:43:01 +0000450 if (mem_total() < 3500)
451 goto goodnight;
452 } else
453 goto goodnight;
454 return;
455
456goodnight:
Eric Andersen3ae0c781999-11-04 01:13:21 +0000457 message(CONSOLE, "Sorry, your computer does not have enough memory.\r\n");
Eric Andersen219d6f51999-11-02 19:43:01 +0000458 while (1) sleep(1);
459}
460
Erik Andersen7dc16072000-01-04 01:10:25 +0000461#ifndef DEBUG_INIT
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000462static void shutdown_system(void)
Eric Andersencc8ed391999-10-05 16:24:54 +0000463{
Erik Andersen5cbdd712000-01-26 20:06:48 +0000464 /* first disable our SIGHUP signal */
465 signal(SIGHUP, SIG_DFL);
466
Eric Andersen0460ff21999-10-25 23:32:44 +0000467 /* Allow Ctrl-Alt-Del to reboot system. */
468 reboot(RB_ENABLE_CAD);
Eric Andersen08b10341999-11-19 02:38:58 +0000469 message(CONSOLE, "\r\nThe system is going down NOW !!\r\n");
Eric Andersen3ae0c781999-11-04 01:13:21 +0000470 sync();
Erik Andersen7dc16072000-01-04 01:10:25 +0000471
Eric Andersen0460ff21999-10-25 23:32:44 +0000472 /* Send signals to every process _except_ pid 1 */
Eric Andersen3ae0c781999-11-04 01:13:21 +0000473 message(CONSOLE, "Sending SIGHUP to all processes.\r\n");
Eric Andersen0460ff21999-10-25 23:32:44 +0000474 kill(-1, SIGHUP);
475 sleep(2);
476 sync();
Erik Andersen7dc16072000-01-04 01:10:25 +0000477
Eric Andersen3ae0c781999-11-04 01:13:21 +0000478 message(CONSOLE, "Sending SIGKILL to all processes.\r\n");
Eric Andersen0460ff21999-10-25 23:32:44 +0000479 kill(-1, SIGKILL);
480 sleep(1);
Erik Andersen7dc16072000-01-04 01:10:25 +0000481
Eric Andersen08b10341999-11-19 02:38:58 +0000482 message(CONSOLE, "Disabling swap.\r\n");
Erik Andersen0e3782f2000-01-07 02:54:55 +0000483 waitfor( "swapoff -a", console, FALSE);
Eric Andersen08b10341999-11-19 02:38:58 +0000484 message(CONSOLE, "Unmounting filesystems.\r\n");
Erik Andersen0e3782f2000-01-07 02:54:55 +0000485 waitfor("umount -a", console, FALSE);
Eric Andersen0460ff21999-10-25 23:32:44 +0000486 sync();
Erik Andersenac6e71f2000-01-08 22:04:33 +0000487 if (kernelVersion > 0 && kernelVersion <= 2 * 65536 + 2 * 256 + 11) {
Eric Andersencf8c9cf1999-11-05 00:31:46 +0000488 /* bdflush, kupdate not needed for kernels >2.2.11 */
489 bdflush(1, 0);
490 sync();
491 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000492}
493
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000494static void halt_signal(int sig)
Eric Andersencc8ed391999-10-05 16:24:54 +0000495{
Eric Andersenabc7d591999-10-19 00:27:50 +0000496 shutdown_system();
Erik Andersen3fe39dc2000-01-25 18:13:53 +0000497 message(CONSOLE, "The system is halted. Press %s or turn off power\r\n",
498 (secondConsole == NULL) /* serial console */
499 ? "Reset" : "CTRL-ALT-DEL");
Eric Andersen3ae0c781999-11-04 01:13:21 +0000500 sync();
Erik Andersen3fe39dc2000-01-25 18:13:53 +0000501
502 /* allow time for last message to reach serial console */
503 sleep(2);
504
Erik Andersen4d1d0111999-12-17 18:44:15 +0000505#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,2,0)
Eric Andersen2cb55071999-12-10 08:25:07 +0000506 if (sig == SIGUSR2)
507 reboot(RB_POWER_OFF);
508 else
Erik Andersen4d1d0111999-12-17 18:44:15 +0000509#endif
Erik Andersen7dc16072000-01-04 01:10:25 +0000510 reboot(RB_HALT_SYSTEM);
Eric Andersenabc7d591999-10-19 00:27:50 +0000511 exit(0);
Eric Andersencc8ed391999-10-05 16:24:54 +0000512}
513
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000514static void reboot_signal(int sig)
Eric Andersencc8ed391999-10-05 16:24:54 +0000515{
Eric Andersenabc7d591999-10-19 00:27:50 +0000516 shutdown_system();
Eric Andersen3ae0c781999-11-04 01:13:21 +0000517 message(CONSOLE, "Please stand by while rebooting the system.\r\n");
518 sync();
Erik Andersen3fe39dc2000-01-25 18:13:53 +0000519
520 /* allow time for last message to reach serial console */
521 sleep(2);
522
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000523 reboot(RB_AUTOBOOT);
Eric Andersenabc7d591999-10-19 00:27:50 +0000524 exit(0);
Eric Andersencc8ed391999-10-05 16:24:54 +0000525}
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000526
Erik Andersende552872000-01-23 01:34:05 +0000527#if defined BB_FEATURE_INIT_CHROOT
528static void check_chroot(int sig)
529{
530 char *argv_init[2] = { "init", NULL, };
531 char *envp_init[3] = { "HOME=/", "TERM=linux", NULL, };
532 char rootpath[256], *tc;
533 int fd;
534
535 if ((fd = open("/proc/sys/kernel/init-chroot", O_RDONLY)) == -1) {
536 message(CONSOLE, "SIGHUP recived, but could not open proc file\r\n");
537 sleep(2);
538 return;
539 }
540 if (read(fd, rootpath, sizeof(rootpath)) == -1) {
541 message(CONSOLE, "SIGHUP recived, but could not read proc file\r\n");
542 sleep(2);
543 return;
544 }
545 close(fd);
546
547 if (rootpath[0] == '\0') {
548 message(CONSOLE, "SIGHUP recived, but new root is not valid: %s\r\n",
549 rootpath);
550 sleep(2);
551 return;
552 }
553
554 tc = strrchr(rootpath, '\n');
555 *tc = '\0';
556
557 /* Ok, making it this far means we commit */
558 message(CONSOLE, "Please stand by, changing root to `%s'.\r\n", rootpath);
559
560 /* kill all other programs first */
561 message(CONSOLE, "Sending SIGTERM to all processes.\r\n");
562 kill(-1, SIGTERM);
563 sleep(2);
564 sync();
565
566 message(CONSOLE, "Sending SIGKILL to all processes.\r\n");
567 kill(-1, SIGKILL);
568 sleep(2);
569 sync();
570
571 /* ok, we don't need /proc anymore. we also assume that the signaling
572 * process left the rest of the filesystems alone for us */
573 umount("/proc");
574
575 /* Ok, now we chroot. Hopefully we only have two things mounted, the
576 * new chroot'd mount point, and the old "/" mount. s,
577 * we go ahead and unmount the old "/". This should trigger the kernel
578 * to set things up the Right Way(tm). */
579
580 if (!chroot(rootpath))
581 umount("/dev/root");
582
583 /* If the chroot fails, we are already too far to turn back, so we
584 * continue and hope that executing init below will revive the system */
585
586 /* close all of our descriptors and open new ones */
587 close(0);
588 close(1);
589 close(2);
590 open("/dev/console", O_RDWR, 0);
591 dup(0);
592 dup(0);
593
594 message(CONSOLE, "Executing real init...\r\n");
595 /* execute init in the (hopefully) new root */
596 execve("/sbin/init",argv_init,envp_init);
597
Erik Andersen3fe39dc2000-01-25 18:13:53 +0000598 message(CONSOLE, "ERROR: Could not exec new init. Press %s to reboot.\r\n",
599 (secondConsole == NULL) /* serial console */
600 ? "Reset" : "CTRL-ALT-DEL");
Erik Andersende552872000-01-23 01:34:05 +0000601 return;
602}
603#endif /* BB_FEATURE_INIT_CHROOT */
604
605#endif /* ! DEBUG_INIT */
Erik Andersen7dc16072000-01-04 01:10:25 +0000606
Erik Andersen96e2abd2000-01-07 11:40:44 +0000607void new_initAction (initActionEnum action,
Erik Andersen9e737252000-01-06 01:16:13 +0000608 char* process, char* cons)
Erik Andersen7dc16072000-01-04 01:10:25 +0000609{
610 initAction* newAction;
Erik Andersen9e737252000-01-06 01:16:13 +0000611
Erik Andersen3fe39dc2000-01-25 18:13:53 +0000612 if (*cons == '\0')
613 cons = console;
614
Erik Andersen9e737252000-01-06 01:16:13 +0000615 /* If BusyBox detects that a serial console is in use,
Erik Andersen3fe39dc2000-01-25 18:13:53 +0000616 * then entries not refering to the console or null devices will _not_ be run.
Erik Andersende552872000-01-23 01:34:05 +0000617 * The exception to this rule is the null device.
Erik Andersen9e737252000-01-06 01:16:13 +0000618 */
Erik Andersen3fe39dc2000-01-25 18:13:53 +0000619 if (secondConsole == NULL && strcmp(cons, console) && strcmp(cons, "/dev/null"))
Erik Andersen9e737252000-01-06 01:16:13 +0000620 return;
621
Erik Andersen7dc16072000-01-04 01:10:25 +0000622 newAction = calloc ((size_t)(1), sizeof(initAction));
623 if (!newAction) {
Erik Andersen0e3782f2000-01-07 02:54:55 +0000624 message(LOG|CONSOLE,"Memory allocation failure\n");
Erik Andersen7dc16072000-01-04 01:10:25 +0000625 while (1) sleep(1);
626 }
627 newAction->nextPtr = initActionList;
628 initActionList = newAction;
629 strncpy( newAction->process, process, 255);
Erik Andersen96e2abd2000-01-07 11:40:44 +0000630 newAction->action = action;
Erik Andersen3fe39dc2000-01-25 18:13:53 +0000631 strncpy(newAction->console, cons, 255);
Erik Andersen7dc16072000-01-04 01:10:25 +0000632 newAction->pid = 0;
Erik Andersen31638212000-01-15 22:28:50 +0000633// message(LOG|CONSOLE, "process='%s' action='%d' console='%s'\n",
634// newAction->process, newAction->action, newAction->console);
Erik Andersen7dc16072000-01-04 01:10:25 +0000635}
636
637void delete_initAction (initAction *action)
638{
639 initAction *a, *b=NULL;
Erik Andersen3fe39dc2000-01-25 18:13:53 +0000640 for( a=initActionList ; a ; b=a, a=a->nextPtr) {
641 if (a == action) {
642 if (b==NULL) {
643 initActionList=a->nextPtr;
644 } else {
645 b->nextPtr=a->nextPtr;
646 }
Erik Andersen7dc16072000-01-04 01:10:25 +0000647 free( a);
648 break;
649 }
650 }
651}
652
Erik Andersen9e737252000-01-06 01:16:13 +0000653/* NOTE that if BB_FEATURE_USE_INITTAB is NOT defined,
654 * then parse_inittab() simply adds in some default
655 * actions(i.e runs INIT_SCRIPT and then starts a pair
Erik Andersen812d4662000-01-07 18:30:40 +0000656 * of "askfirst" shells). If BB_FEATURE_USE_INITTAB
657 * _is_ defined, but /etc/inittab is missing, this
658 * results in the same set of default behaviors.
Erik Andersen9e737252000-01-06 01:16:13 +0000659 * */
Erik Andersen7dc16072000-01-04 01:10:25 +0000660void parse_inittab(void)
661{
Erik Andersen9e737252000-01-06 01:16:13 +0000662#ifdef BB_FEATURE_USE_INITTAB
Erik Andersen7dc16072000-01-04 01:10:25 +0000663 FILE* file;
Erik Andersen0e3782f2000-01-07 02:54:55 +0000664 char buf[256], lineAsRead[256], tmpConsole[256];
Erik Andersen9e737252000-01-06 01:16:13 +0000665 char *p, *q, *r, *s;
Erik Andersen7dc16072000-01-04 01:10:25 +0000666 const struct initActionType *a = actions;
667 int foundIt;
668
669
670 file = fopen(INITTAB, "r");
671 if (file == NULL) {
672 /* No inittab file -- set up some default behavior */
Erik Andersen9e737252000-01-06 01:16:13 +0000673#endif
Erik Andersen7dc16072000-01-04 01:10:25 +0000674 /* Askfirst shell on tty1 */
Erik Andersen96e2abd2000-01-07 11:40:44 +0000675 new_initAction( ASKFIRST, SHELL, console );
Erik Andersen7dc16072000-01-04 01:10:25 +0000676 /* Askfirst shell on tty2 */
Erik Andersenac6e71f2000-01-08 22:04:33 +0000677 if (secondConsole != NULL)
678 new_initAction( ASKFIRST, SHELL, secondConsole );
Erik Andersen7dc16072000-01-04 01:10:25 +0000679 /* sysinit */
Erik Andersen96e2abd2000-01-07 11:40:44 +0000680 new_initAction( SYSINIT, INIT_SCRIPT, console );
Erik Andersen7dc16072000-01-04 01:10:25 +0000681
682 return;
Erik Andersen9e737252000-01-06 01:16:13 +0000683#ifdef BB_FEATURE_USE_INITTAB
Erik Andersen7dc16072000-01-04 01:10:25 +0000684 }
685
686 while ( fgets(buf, 255, file) != NULL) {
687 foundIt=FALSE;
688 for(p = buf; *p == ' ' || *p == '\t'; p++);
689 if (*p == '#' || *p == '\n') continue;
690
691 /* Trim the trailing \n */
692 q = strrchr( p, '\n');
693 if (q != NULL)
694 *q='\0';
695
Erik Andersen9e737252000-01-06 01:16:13 +0000696 /* Keep a copy around for posterity's sake (and error msgs) */
Erik Andersen0e3782f2000-01-07 02:54:55 +0000697 strcpy(lineAsRead, buf);
Erik Andersen9e737252000-01-06 01:16:13 +0000698
699 /* Grab the ID field */
700 s=p;
Erik Andersen7dc16072000-01-04 01:10:25 +0000701 p = strchr( p, ':');
Erik Andersen9e737252000-01-06 01:16:13 +0000702 if ( p != NULL || *(p+1) != '\0' ) {
703 *p='\0';
704 ++p;
705 }
Erik Andersen7dc16072000-01-04 01:10:25 +0000706
707 /* Now peal off the process field from the end
708 * of the string */
709 q = strrchr( p, ':');
710 if ( q == NULL || *(q+1) == '\0' ) {
Erik Andersen0e3782f2000-01-07 02:54:55 +0000711 message(LOG|CONSOLE,"Bad inittab entry: %s\n", lineAsRead);
Erik Andersen7dc16072000-01-04 01:10:25 +0000712 continue;
713 } else {
714 *q='\0';
715 ++q;
716 }
717
718 /* Now peal off the action field */
719 r = strrchr( p, ':');
720 if ( r == NULL || *(r+1) == '\0') {
Erik Andersen0e3782f2000-01-07 02:54:55 +0000721 message(LOG|CONSOLE,"Bad inittab entry: %s\n", lineAsRead);
Erik Andersen7dc16072000-01-04 01:10:25 +0000722 continue;
723 } else {
724 ++r;
725 }
726
727 /* Ok, now process it */
728 a = actions;
729 while (a->name != 0) {
730 if (strcmp(a->name, r) == 0) {
Erik Andersen0e3782f2000-01-07 02:54:55 +0000731 if (*s != '\0') {
732 struct stat statBuf;
733 strcpy(tmpConsole, "/dev/");
734 strncat(tmpConsole, s, 200);
735 if (stat(tmpConsole, &statBuf) != 0) {
736 message(LOG|CONSOLE, "device '%s' does not exist. Did you read the directions?\n", tmpConsole);
737 break;
738 }
739 s = tmpConsole;
740 }
Erik Andersen96e2abd2000-01-07 11:40:44 +0000741 new_initAction( a->action, q, s);
Erik Andersen7dc16072000-01-04 01:10:25 +0000742 foundIt=TRUE;
743 }
744 a++;
745 }
746 if (foundIt==TRUE)
747 continue;
748 else {
749 /* Choke on an unknown action */
Erik Andersen0e3782f2000-01-07 02:54:55 +0000750 message(LOG|CONSOLE, "Bad inittab entry: %s\n", lineAsRead);
Erik Andersen7dc16072000-01-04 01:10:25 +0000751 }
752 }
753 return;
Erik Andersen9e737252000-01-06 01:16:13 +0000754#endif
Erik Andersen7dc16072000-01-04 01:10:25 +0000755}
756
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000757extern int init_main(int argc, char **argv)
Eric Andersen0460ff21999-10-25 23:32:44 +0000758{
Erik Andersen7dc16072000-01-04 01:10:25 +0000759 initAction *a;
760 pid_t wpid;
761 int status;
Eric Andersen0460ff21999-10-25 23:32:44 +0000762
Eric Andersend73dc5b1999-11-10 23:13:02 +0000763#ifndef DEBUG_INIT
Erik Andersende552872000-01-23 01:34:05 +0000764 /* Expect to be PID 1 if we are run as init (not linuxrc) */
Eric Andersen07274581999-11-21 21:50:07 +0000765 if (getpid() != 1 && strstr(argv[0], "init")!=NULL ) {
Eric Andersend73dc5b1999-11-10 23:13:02 +0000766 usage( "init\n\nInit is the parent of all processes.\n\n"
767 "This version of init is designed to be run only by the kernel\n");
768 }
Erik Andersen31638212000-01-15 22:28:50 +0000769 /* Fix up argv[0] to be certain we claim to be init */
770 strncpy(argv[0], "init", strlen(argv[0]));
771
Erik Andersen05df2392000-01-13 04:43:48 +0000772 /* Set up sig handlers -- be sure to
773 * clear all of these in run() */
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000774 signal(SIGUSR1, halt_signal);
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000775 signal(SIGUSR2, reboot_signal);
Erik Andersen0881de72000-01-05 09:34:26 +0000776 signal(SIGINT, reboot_signal);
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000777 signal(SIGTERM, reboot_signal);
Erik Andersende552872000-01-23 01:34:05 +0000778#if defined BB_FEATURE_INIT_CHROOT
779 signal(SIGHUP, check_chroot);
780#endif
Eric Andersen0460ff21999-10-25 23:32:44 +0000781
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000782 /* Turn off rebooting via CTL-ALT-DEL -- we get a
783 * SIGINT on CAD so we can shut things down gracefully... */
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000784 reboot(RB_DISABLE_CAD);
Eric Andersen7f1acfd1999-10-29 23:09:13 +0000785#endif
Erik Andersen05df2392000-01-13 04:43:48 +0000786
Eric Andersenc7c41d31999-10-28 00:24:35 +0000787 /* Figure out where the default console should be */
788 console_init();
Eric Andersen0460ff21999-10-25 23:32:44 +0000789
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000790 /* Close whatever files are open, and reset the console. */
791 close(0);
792 close(1);
793 close(2);
Eric Andersen7f1acfd1999-10-29 23:09:13 +0000794 set_term(0);
Erik Andersen05df2392000-01-13 04:43:48 +0000795 setsid();
Eric Andersencc8ed391999-10-05 16:24:54 +0000796
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000797 /* Make sure PATH is set to something sane */
Eric Andersenb186d981999-12-03 09:19:54 +0000798 putenv(_PATH_STDPATH);
Eric Andersencc8ed391999-10-05 16:24:54 +0000799
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000800 /* Hello world */
Eric Andersen7f1acfd1999-10-29 23:09:13 +0000801#ifndef DEBUG_INIT
Erik Andersen0e3782f2000-01-07 02:54:55 +0000802 message(LOG|CONSOLE,
Erik Andersen7dc16072000-01-04 01:10:25 +0000803 "init started: BusyBox v%s (%s) multi-call binary\r\n",
804 BB_VER, BB_BT);
Eric Andersen7f1acfd1999-10-29 23:09:13 +0000805#else
Erik Andersen0e3782f2000-01-07 02:54:55 +0000806 message(LOG|CONSOLE,
Erik Andersen7dc16072000-01-04 01:10:25 +0000807 "init(%d) started: BusyBox v%s (%s) multi-call binary\r\n",
808 getpid(), BB_VER, BB_BT);
Eric Andersen7f1acfd1999-10-29 23:09:13 +0000809#endif
Eric Andersencc8ed391999-10-05 16:24:54 +0000810
Eric Andersenc7c41d31999-10-28 00:24:35 +0000811
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000812 /* Mount /proc */
Eric Andersene18c75a1999-11-05 04:23:05 +0000813 if (mount ("proc", "/proc", "proc", 0, 0) == 0) {
Erik Andersen0e3782f2000-01-07 02:54:55 +0000814 message(LOG|CONSOLE, "Mounting /proc: done.\n");
Erik Andersenac6e71f2000-01-08 22:04:33 +0000815 kernelVersion = get_kernel_revision();
Eric Andersene18c75a1999-11-05 04:23:05 +0000816 } else
Erik Andersen0e3782f2000-01-07 02:54:55 +0000817 message(LOG|CONSOLE, "Mounting /proc: failed!\n");
Eric Andersencc8ed391999-10-05 16:24:54 +0000818
Eric Andersen219d6f51999-11-02 19:43:01 +0000819 /* Make sure there is enough memory to do something useful. */
820 check_memory();
Eric Andersencc8ed391999-10-05 16:24:54 +0000821
Eric Andersend00c2621999-12-07 08:37:31 +0000822 /* Check if we are supposed to be in single user mode */
823 if ( argc > 1 && (!strcmp(argv[1], "single") ||
Erik Andersen0881de72000-01-05 09:34:26 +0000824 !strcmp(argv[1], "-s") || !strcmp(argv[1], "1")))
825 {
Erik Andersen7dc16072000-01-04 01:10:25 +0000826 /* Ask first then start a shell on tty2 */
Erik Andersenac6e71f2000-01-08 22:04:33 +0000827 if (secondConsole != NULL)
828 new_initAction( ASKFIRST, SHELL, secondConsole);
Erik Andersen3fe39dc2000-01-25 18:13:53 +0000829 /* Start a shell on tty1 */
830 new_initAction( RESPAWN, SHELL, console);
Erik Andersen7dc16072000-01-04 01:10:25 +0000831 } else {
832 /* Not in single user mode -- see what inittab says */
Erik Andersen9e737252000-01-06 01:16:13 +0000833
834 /* NOTE that if BB_FEATURE_USE_INITTAB is NOT defined,
835 * then parse_inittab() simply adds in some default
836 * actions(i.e runs INIT_SCRIPT and then starts a pair
837 * of "askfirst" shells */
Erik Andersen7dc16072000-01-04 01:10:25 +0000838 parse_inittab();
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000839 }
840
Erik Andersen7dc16072000-01-04 01:10:25 +0000841 /* Now run everything that needs to be run */
Eric Andersen84b00921999-12-11 04:16:51 +0000842
Erik Andersen0881de72000-01-05 09:34:26 +0000843 /* First run the sysinit command */
Erik Andersen7dc16072000-01-04 01:10:25 +0000844 for( a=initActionList ; a; a=a->nextPtr) {
845 if (a->action == SYSINIT) {
Erik Andersen96e2abd2000-01-07 11:40:44 +0000846 waitfor(a->process, a->console, FALSE);
Erik Andersen7dc16072000-01-04 01:10:25 +0000847 /* Now remove the "sysinit" entry from the list */
848 delete_initAction( a);
Eric Andersend00c2621999-12-07 08:37:31 +0000849 }
850 }
Erik Andersen7dc16072000-01-04 01:10:25 +0000851 /* Next run anything that wants to block */
852 for( a=initActionList ; a; a=a->nextPtr) {
853 if (a->action == WAIT) {
Erik Andersen96e2abd2000-01-07 11:40:44 +0000854 waitfor(a->process, a->console, FALSE);
Erik Andersen7dc16072000-01-04 01:10:25 +0000855 /* Now remove the "wait" entry from the list */
856 delete_initAction( a);
857 }
858 }
859 /* Next run anything to be run only once */
860 for( a=initActionList ; a; a=a->nextPtr) {
861 if (a->action == ONCE) {
Erik Andersen96e2abd2000-01-07 11:40:44 +0000862 run(a->process, a->console, FALSE);
Erik Andersen7dc16072000-01-04 01:10:25 +0000863 /* Now remove the "once" entry from the list */
864 delete_initAction( a);
865 }
866 }
Erik Andersen0e3782f2000-01-07 02:54:55 +0000867 /* If there is nothing else to do, stop */
868 if (initActionList == NULL) {
869 message(LOG|CONSOLE, "No more tasks for init -- sleeping forever.\n");
870 while (1) sleep(1);
871 }
Eric Andersen219d6f51999-11-02 19:43:01 +0000872
Erik Andersen0881de72000-01-05 09:34:26 +0000873 /* Now run the looping stuff for the rest of forever */
874 while (1) {
Erik Andersen7dc16072000-01-04 01:10:25 +0000875 for( a=initActionList ; a; a=a->nextPtr) {
876 /* Only run stuff with pid==0. If they have
877 * a pid, that means they are still running */
878 if (a->pid == 0) {
879 switch(a->action) {
880 case RESPAWN:
881 /* run the respawn stuff */
Erik Andersen96e2abd2000-01-07 11:40:44 +0000882 a->pid = run(a->process, a->console, FALSE);
Erik Andersen7dc16072000-01-04 01:10:25 +0000883 break;
884 case ASKFIRST:
885 /* run the askfirst stuff */
Erik Andersen96e2abd2000-01-07 11:40:44 +0000886 a->pid = run(a->process, a->console, TRUE);
Erik Andersen7dc16072000-01-04 01:10:25 +0000887 break;
Erik Andersen0881de72000-01-05 09:34:26 +0000888 /* silence the compiler's incessant whining */
Erik Andersen7dc16072000-01-04 01:10:25 +0000889 default:
890 break;
891 }
892 }
893 }
Erik Andersen0881de72000-01-05 09:34:26 +0000894 /* Wait for a child process to exit */
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000895 wpid = wait(&status);
Eric Andersena7456061999-10-27 02:31:32 +0000896 if (wpid > 0 ) {
Erik Andersen0881de72000-01-05 09:34:26 +0000897 /* Find out who died and clean up their corpse */
Erik Andersen7dc16072000-01-04 01:10:25 +0000898 for( a=initActionList ; a; a=a->nextPtr) {
899 if (a->pid==wpid) {
900 a->pid=0;
Erik Andersen0881de72000-01-05 09:34:26 +0000901 message(LOG, "Process '%s' (pid %d) exited. Scheduling it for restart.\n",
902 a->process, wpid);
Erik Andersen7dc16072000-01-04 01:10:25 +0000903 }
Eric Andersenb6a44b81999-11-13 04:47:09 +0000904 }
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000905 }
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000906 sleep(1);
907 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000908}
Erik Andersen9c88cac1999-12-30 09:25:17 +0000909