blob: 116d0795446b05c548bd07f6a152ea62b5563438 [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 Andersen0881de72000-01-05 09:34:26 +000064#define SHELL "-sh" /* Default shell */
Erik Andersen7dc16072000-01-04 01:10:25 +000065#define INITTAB "/etc/inittab" /* inittab file location */
66#define INIT_SCRIPT "/etc/init.d/rcS" /* Default sysinit script. */
Erik Andersen9c88cac1999-12-30 09:25:17 +000067
Eric Andersen3ae0c781999-11-04 01:13:21 +000068#define LOG 0x1
69#define CONSOLE 0x2
Erik Andersen7dc16072000-01-04 01:10:25 +000070
71/* Allowed init action types */
72typedef enum {
73 SYSINIT=1,
Erik Andersen7dc16072000-01-04 01:10:25 +000074 RESPAWN,
75 ASKFIRST,
76 WAIT,
77 ONCE
78} initActionEnum;
79
80/* And now a list of the actions we support in the version of init */
81typedef struct initActionType{
82 const char* name;
83 initActionEnum action;
84} initActionType;
85
86static const struct initActionType actions[] = {
87 {"sysinit", SYSINIT},
Erik Andersen7dc16072000-01-04 01:10:25 +000088 {"respawn", RESPAWN},
89 {"askfirst", ASKFIRST},
90 {"wait", WAIT},
91 {"once", ONCE},
92 {0}
93};
94
95/* Set up a linked list of initactions, to be read from inittab */
96typedef struct initActionTag initAction;
97struct initActionTag {
98 pid_t pid;
99 char process[256];
100 char *console;
101 initAction *nextPtr;
102 initActionEnum action;
103};
104initAction* initActionList = NULL;
105
106
Eric Andersenb186d981999-12-03 09:19:54 +0000107static char *console = _PATH_CONSOLE;
Eric Andersenbe971d61999-11-03 16:52:50 +0000108static char *second_console = VT_SECONDARY;
109static char *log = VT_LOG;
Eric Andersene18c75a1999-11-05 04:23:05 +0000110static int kernel_version = 0;
Eric Andersen0460ff21999-10-25 23:32:44 +0000111
112
113/* try to open up the specified device */
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000114int device_open(char *device, int mode)
Eric Andersencc8ed391999-10-05 16:24:54 +0000115{
Eric Andersen0460ff21999-10-25 23:32:44 +0000116 int m, f, fd = -1;
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000117
Eric Andersen7f1acfd1999-10-29 23:09:13 +0000118 m = mode | O_NONBLOCK;
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000119
Eric Andersen0460ff21999-10-25 23:32:44 +0000120 /* Retry up to 5 times */
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000121 for (f = 0; f < 5; f++)
122 if ((fd = open(device, m)) >= 0)
123 break;
124 if (fd < 0)
125 return fd;
Eric Andersen3ae0c781999-11-04 01:13:21 +0000126 /* Reset original flags. */
Eric Andersen0460ff21999-10-25 23:32:44 +0000127 if (m != mode)
128 fcntl(fd, F_SETFL, mode);
129 return fd;
130}
Eric Andersencc8ed391999-10-05 16:24:54 +0000131
Eric Andersen3ae0c781999-11-04 01:13:21 +0000132/* print a message to the specified device:
133 * device may be bitwise-or'd from LOG | CONSOLE */
134void message(int device, char *fmt, ...)
Eric Andersen0460ff21999-10-25 23:32:44 +0000135{
Eric Andersen0460ff21999-10-25 23:32:44 +0000136 va_list arguments;
Erik Andersen7dc16072000-01-04 01:10:25 +0000137 int fd;
138
Eric Andersen4c781471999-11-26 08:12:56 +0000139#ifdef BB_SYSLOGD
140
141 /* Log the message to syslogd */
142 if (device & LOG ) {
143 char msg[1024];
144 va_start(arguments, fmt);
145 vsnprintf(msg, sizeof(msg), fmt, arguments);
146 va_end(arguments);
147 syslog(LOG_DAEMON|LOG_NOTICE, msg);
148 }
149
150#else
151 static int log_fd=-1;
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000152
Eric Andersen3ae0c781999-11-04 01:13:21 +0000153 /* Take full control of the log tty, and never close it.
154 * It's mine, all mine! Muhahahaha! */
Eric Andersen07e52971999-11-07 07:38:08 +0000155 if (log_fd < 0) {
156 if (log == NULL) {
157 /* don't even try to log, because there is no such console */
158 log_fd = -2;
159 /* log to main console instead */
160 device = CONSOLE;
161 }
162 else if ((log_fd = device_open(log, O_RDWR|O_NDELAY)) < 0) {
Eric Andersen3ae0c781999-11-04 01:13:21 +0000163 log_fd=-1;
164 fprintf(stderr, "Bummer, can't write to log on %s!\r\n", log);
165 fflush(stderr);
166 return;
167 }
168 }
Eric Andersen07e52971999-11-07 07:38:08 +0000169 if ( (device & LOG) && (log_fd >= 0) ) {
Eric Andersen0460ff21999-10-25 23:32:44 +0000170 va_start(arguments, fmt);
Eric Andersen3ae0c781999-11-04 01:13:21 +0000171 vdprintf(log_fd, fmt, arguments);
Eric Andersena7456061999-10-27 02:31:32 +0000172 va_end(arguments);
173 }
Eric Andersen4c781471999-11-26 08:12:56 +0000174#endif
175
Eric Andersen3ae0c781999-11-04 01:13:21 +0000176 if (device & CONSOLE) {
Eric Andersended62591999-11-18 00:19:26 +0000177 /* Always send console messages to /dev/console so people will see them. */
Eric Andersenb186d981999-12-03 09:19:54 +0000178 if ((fd = device_open(_PATH_CONSOLE, O_WRONLY|O_NOCTTY|O_NDELAY)) >= 0) {
Eric Andersen3ae0c781999-11-04 01:13:21 +0000179 va_start(arguments, fmt);
180 vdprintf(fd, fmt, arguments);
181 va_end(arguments);
182 close(fd);
183 } else {
184 fprintf(stderr, "Bummer, can't print: ");
185 va_start(arguments, fmt);
186 vfprintf(stderr, fmt, arguments);
187 fflush(stderr);
188 va_end(arguments);
189 }
190 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000191}
192
Eric Andersen3ae0c781999-11-04 01:13:21 +0000193
Eric Andersen0460ff21999-10-25 23:32:44 +0000194/* Set terminal settings to reasonable defaults */
Eric Andersenc7c41d31999-10-28 00:24:35 +0000195void set_term( int fd)
Eric Andersencc8ed391999-10-05 16:24:54 +0000196{
Eric Andersen0460ff21999-10-25 23:32:44 +0000197 struct termios tty;
Eric Andersen08b10341999-11-19 02:38:58 +0000198 static const char control_characters[] = {
199 '\003', '\034', '\177', '\025', '\004', '\0',
200 '\1', '\0', '\021', '\023', '\032', '\0', '\022',
201 '\017', '\027', '\026', '\0'
202 };
Eric Andersencc8ed391999-10-05 16:24:54 +0000203
Eric Andersenc7c41d31999-10-28 00:24:35 +0000204 tcgetattr(fd, &tty);
Eric Andersencc8ed391999-10-05 16:24:54 +0000205
Eric Andersen08b10341999-11-19 02:38:58 +0000206 /* set control chars */
207 memcpy(tty.c_cc, control_characters, sizeof(control_characters));
Eric Andersencc8ed391999-10-05 16:24:54 +0000208
Eric Andersen219d6f51999-11-02 19:43:01 +0000209 /* use line dicipline 0 */
Eric Andersen7f1acfd1999-10-29 23:09:13 +0000210 tty.c_line = 0;
211
Eric Andersen08b10341999-11-19 02:38:58 +0000212 /* Make it be sane */
213 //tty.c_cflag &= CBAUD|CBAUDEX|CSIZE|CSTOPB|PARENB|PARODD;
214 //tty.c_cflag |= HUPCL|CLOCAL;
215
216 /* input modes */
217 tty.c_iflag = ICRNL|IXON|IXOFF;
218
Eric Andersen7f1acfd1999-10-29 23:09:13 +0000219 /* output modes */
Eric Andersenc7c41d31999-10-28 00:24:35 +0000220 tty.c_oflag = OPOST|ONLCR;
Eric Andersen7f1acfd1999-10-29 23:09:13 +0000221
222 /* local modes */
Eric Andersen08b10341999-11-19 02:38:58 +0000223 tty.c_lflag = ISIG|ICANON|ECHO|ECHOE|ECHOK|ECHOCTL|ECHOKE|IEXTEN;
Eric Andersencc8ed391999-10-05 16:24:54 +0000224
Eric Andersenc7c41d31999-10-28 00:24:35 +0000225 tcsetattr(fd, TCSANOW, &tty);
Eric Andersencc8ed391999-10-05 16:24:54 +0000226}
227
Eric Andersen219d6f51999-11-02 19:43:01 +0000228/* How much memory does this machine have? */
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000229static int mem_total()
Eric Andersencc8ed391999-10-05 16:24:54 +0000230{
Eric Andersenabc7d591999-10-19 00:27:50 +0000231 char s[80];
Eric Andersen219d6f51999-11-02 19:43:01 +0000232 char *p = "/proc/meminfo";
Eric Andersenabc7d591999-10-19 00:27:50 +0000233 FILE *f;
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000234 const char pattern[] = "MemTotal:";
Eric Andersencc8ed391999-10-05 16:24:54 +0000235
Eric Andersen219d6f51999-11-02 19:43:01 +0000236 if ((f = fopen(p, "r")) < 0) {
Eric Andersen3ae0c781999-11-04 01:13:21 +0000237 message(LOG, "Error opening %s: %s\n", p, strerror( errno));
Eric Andersen219d6f51999-11-02 19:43:01 +0000238 return -1;
239 }
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000240 while (NULL != fgets(s, 79, f)) {
241 p = strstr(s, pattern);
Eric Andersenabc7d591999-10-19 00:27:50 +0000242 if (NULL != p) {
243 fclose(f);
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000244 return (atoi(p + strlen(pattern)));
Eric Andersenabc7d591999-10-19 00:27:50 +0000245 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000246 }
Eric Andersenabc7d591999-10-19 00:27:50 +0000247 return -1;
Eric Andersencc8ed391999-10-05 16:24:54 +0000248}
249
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000250static void console_init()
Eric Andersen0460ff21999-10-25 23:32:44 +0000251{
252 int fd;
253 int tried_devcons = 0;
Eric Andersen219d6f51999-11-02 19:43:01 +0000254 int tried_vtprimary = 0;
Eric Andersen07e52971999-11-07 07:38:08 +0000255 struct serial_struct sr;
Eric Andersen0460ff21999-10-25 23:32:44 +0000256 char *s;
257
Eric Andersen219d6f51999-11-02 19:43:01 +0000258 if ((s = getenv("CONSOLE")) != NULL) {
Eric Andersen0460ff21999-10-25 23:32:44 +0000259 console = s;
Eric Andersen07e52971999-11-07 07:38:08 +0000260 }
Eric Andersenfbb39c81999-11-08 17:00:52 +0000261#if #cpu(sparc)
Eric Andersen07e52971999-11-07 07:38:08 +0000262 /* sparc kernel supports console=tty[ab] parameter which is also
263 * passed to init, so catch it here */
264 else if ((s = getenv("console")) != NULL) {
265 /* remap tty[ab] to /dev/ttyS[01] */
266 if (strcmp( s, "ttya" )==0)
267 console = SERIAL_CON0;
268 else if (strcmp( s, "ttyb" )==0)
269 console = SERIAL_CON1;
270 }
Eric Andersen219d6f51999-11-02 19:43:01 +0000271#endif
Eric Andersen07e52971999-11-07 07:38:08 +0000272 else {
273 struct vt_stat vt;
274 static char the_console[13];
275
276 console = the_console;
277 /* 2.2 kernels: identify the real console backend and try to use it */
Eric Andersen08b10341999-11-19 02:38:58 +0000278 if (ioctl(0, TIOCGSERIAL, &sr) == 0) {
Eric Andersen07e52971999-11-07 07:38:08 +0000279 /* this is a serial console */
280 snprintf( the_console, sizeof the_console, "/dev/ttyS%d", sr.line );
281 }
282 else if (ioctl(0, VT_GETSTATE, &vt) == 0) {
283 /* this is linux virtual tty */
284 snprintf( the_console, sizeof the_console, "/dev/tty%d", vt.v_active );
285 } else {
Eric Andersenb186d981999-12-03 09:19:54 +0000286 console = _PATH_CONSOLE;
Eric Andersen07e52971999-11-07 07:38:08 +0000287 tried_devcons++;
288 }
Eric Andersen0460ff21999-10-25 23:32:44 +0000289 }
Eric Andersena7456061999-10-27 02:31:32 +0000290
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000291 while ((fd = open(console, O_RDONLY | O_NONBLOCK)) < 0) {
Eric Andersen219d6f51999-11-02 19:43:01 +0000292 /* Can't open selected console -- try /dev/console */
Eric Andersen0460ff21999-10-25 23:32:44 +0000293 if (!tried_devcons) {
294 tried_devcons++;
Eric Andersenb186d981999-12-03 09:19:54 +0000295 console = _PATH_CONSOLE;
Eric Andersen0460ff21999-10-25 23:32:44 +0000296 continue;
297 }
Eric Andersenbe971d61999-11-03 16:52:50 +0000298 /* Can't open selected console -- try vt1 */
299 if (!tried_vtprimary) {
300 tried_vtprimary++;
301 console = VT_PRIMARY;
302 continue;
303 }
Eric Andersen0460ff21999-10-25 23:32:44 +0000304 break;
305 }
306 if (fd < 0)
Eric Andersen219d6f51999-11-02 19:43:01 +0000307 /* Perhaps we should panic here? */
Eric Andersen0460ff21999-10-25 23:32:44 +0000308 console = "/dev/null";
Eric Andersen07e52971999-11-07 07:38:08 +0000309 else {
310 /* check for serial console and disable logging to tty3 & running a
311 * shell to tty2 */
312 if (ioctl(0,TIOCGSERIAL,&sr) == 0) {
Eric Andersen08b10341999-11-19 02:38:58 +0000313 message(LOG|CONSOLE, "serial console detected. Disabling 2nd virtual terminal.\r\n", console );
Eric Andersen07e52971999-11-07 07:38:08 +0000314 log = NULL;
315 second_console = NULL;
316 }
Eric Andersen0460ff21999-10-25 23:32:44 +0000317 close(fd);
Eric Andersen07e52971999-11-07 07:38:08 +0000318 }
Eric Andersen3ae0c781999-11-04 01:13:21 +0000319 message(LOG, "console=%s\n", console );
Eric Andersen0460ff21999-10-25 23:32:44 +0000320}
321
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000322static int waitfor(int pid)
Eric Andersen0460ff21999-10-25 23:32:44 +0000323{
324 int status, wpid;
325
Erik Andersen0881de72000-01-05 09:34:26 +0000326 while (1) {
327 wpid = wait(&status);
328 if (wpid > 0 ) {
Eric Andersen3ae0c781999-11-04 01:13:21 +0000329 message(LOG, "pid %d exited, status=0x%x.\n", wpid, status);
Erik Andersen0881de72000-01-05 09:34:26 +0000330 break;
331 }
332 if (wpid == pid )
333 break;
Eric Andersen0460ff21999-10-25 23:32:44 +0000334 }
335 return wpid;
336}
337
Erik Andersen7dc16072000-01-04 01:10:25 +0000338static pid_t run(char* command,
Eric Andersenc7c41d31999-10-28 00:24:35 +0000339 char *terminal, int get_enter)
Eric Andersen0460ff21999-10-25 23:32:44 +0000340{
Erik Andersen7dc16072000-01-04 01:10:25 +0000341 int i;
Eric Andersenc7c41d31999-10-28 00:24:35 +0000342 pid_t pid;
Erik Andersen7dc16072000-01-04 01:10:25 +0000343 char* tmpCmd;
344 char* cmd[255];
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000345 static const char press_enter[] =
Eric Andersen0460ff21999-10-25 23:32:44 +0000346 "\nPlease press Enter to activate this console. ";
347
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
Erik Andersen0881de72000-01-05 09:34:26 +0000357 if (device_open(terminal, O_RDWR) < 0) {
358 message(LOG|CONSOLE, "Bummer, can't open %s\r\n", terminal);
359 exit(1);
360 }
361 dup(0);
362 dup(0);
363 /* Grab control of the terminal. */
364 if (tcsetpgrp (0, getpgrp()) < 0) {
365 message(LOG|CONSOLE, "tcsetpgrp error: %s\r\n", strerror(errno));
366 }
367 set_term(0);
368
Eric Andersen2f6c04f1999-11-01 23:59:44 +0000369 /* Reset signal handlers set for parent process */
370 signal(SIGUSR1, SIG_DFL);
371 signal(SIGUSR2, SIG_DFL);
372 signal(SIGINT, SIG_DFL);
373 signal(SIGTERM, SIG_DFL);
374
Eric Andersen0460ff21999-10-25 23:32:44 +0000375
Eric Andersen2f6c04f1999-11-01 23:59:44 +0000376 if (get_enter==TRUE) {
Eric Andersen0460ff21999-10-25 23:32:44 +0000377 /*
378 * Save memory by not exec-ing anything large (like a shell)
379 * before the user wants it. This is critical if swap is not
380 * enabled and the system has low memory. Generally this will
381 * be run on the second virtual console, and the first will
382 * be allowed to start a shell or whatever an init script
383 * specifies.
384 */
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000385 char c;
Eric Andersen3ae0c781999-11-04 01:13:21 +0000386 message(LOG, "Waiting for enter to start '%s' (pid %d, console %s)\r\n",
Erik Andersen0881de72000-01-05 09:34:26 +0000387 command, shell_pgid, terminal );
Erik Andersen7dc16072000-01-04 01:10:25 +0000388 write(fileno(stdout), press_enter, sizeof(press_enter) - 1);
389 read(fileno(stdin), &c, 1);
Eric Andersen0460ff21999-10-25 23:32:44 +0000390 }
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;
396 tmpCmd++;
397 i++;
398 }
399 }
400 cmd[i] = NULL;
401
Eric Andersen0460ff21999-10-25 23:32:44 +0000402 /* Log the process name and args */
Erik Andersen7dc16072000-01-04 01:10:25 +0000403 message(LOG, "Starting pid %d, console %s: '%s'\r\n",
Erik Andersen0881de72000-01-05 09:34:26 +0000404 shell_pgid, terminal, cmd[0]);
Erik Andersen7dc16072000-01-04 01:10:25 +0000405
Eric Andersenc7c41d31999-10-28 00:24:35 +0000406 /* Now run it. The new program will take over this PID,
Eric Andersena7456061999-10-27 02:31:32 +0000407 * so nothing further in init.c should be run. */
Erik Andersen7dc16072000-01-04 01:10:25 +0000408 execvp(cmd[0], cmd);
Eric Andersen0460ff21999-10-25 23:32:44 +0000409
Erik Andersen7dc16072000-01-04 01:10:25 +0000410 /* We're still here? Some error happened. */
411 message(LOG|CONSOLE, "Bummer, could not run '%s': %s\n", cmd[0],
412 strerror(errno));
Eric Andersen0460ff21999-10-25 23:32:44 +0000413 exit(-1);
414 }
415 return pid;
416}
417
Eric Andersen219d6f51999-11-02 19:43:01 +0000418/* Make sure there is enough memory to do something useful. *
419 * Calls swapon if needed so be sure /proc is mounted. */
420static void check_memory()
421{
422 struct stat statbuf;
Eric Andersen219d6f51999-11-02 19:43:01 +0000423
424 if (mem_total() > 3500)
425 return;
426
427 if (stat("/etc/fstab", &statbuf) == 0) {
428 /* Try to turn on swap */
Erik Andersen7dc16072000-01-04 01:10:25 +0000429 waitfor(run("/bin/swapon swapon -a", log, FALSE));
Eric Andersen219d6f51999-11-02 19:43:01 +0000430 if (mem_total() < 3500)
431 goto goodnight;
432 } else
433 goto goodnight;
434 return;
435
436goodnight:
Eric Andersen3ae0c781999-11-04 01:13:21 +0000437 message(CONSOLE, "Sorry, your computer does not have enough memory.\r\n");
Eric Andersen219d6f51999-11-02 19:43:01 +0000438 while (1) sleep(1);
439}
440
Erik Andersen7dc16072000-01-04 01:10:25 +0000441#ifndef DEBUG_INIT
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000442static void shutdown_system(void)
Eric Andersencc8ed391999-10-05 16:24:54 +0000443{
Eric Andersen0460ff21999-10-25 23:32:44 +0000444 /* Allow Ctrl-Alt-Del to reboot system. */
445 reboot(RB_ENABLE_CAD);
Eric Andersen08b10341999-11-19 02:38:58 +0000446 message(CONSOLE, "\r\nThe system is going down NOW !!\r\n");
Eric Andersen3ae0c781999-11-04 01:13:21 +0000447 sync();
Erik Andersen7dc16072000-01-04 01:10:25 +0000448
Eric Andersen0460ff21999-10-25 23:32:44 +0000449 /* Send signals to every process _except_ pid 1 */
Eric Andersen3ae0c781999-11-04 01:13:21 +0000450 message(CONSOLE, "Sending SIGHUP to all processes.\r\n");
Eric Andersen0460ff21999-10-25 23:32:44 +0000451 kill(-1, SIGHUP);
452 sleep(2);
453 sync();
Erik Andersen7dc16072000-01-04 01:10:25 +0000454
Eric Andersen3ae0c781999-11-04 01:13:21 +0000455 message(CONSOLE, "Sending SIGKILL to all processes.\r\n");
Eric Andersen0460ff21999-10-25 23:32:44 +0000456 kill(-1, SIGKILL);
457 sleep(1);
Erik Andersen7dc16072000-01-04 01:10:25 +0000458
Eric Andersen08b10341999-11-19 02:38:58 +0000459 message(CONSOLE, "Disabling swap.\r\n");
Erik Andersen7dc16072000-01-04 01:10:25 +0000460 waitfor(run( "swapoff -a", console, FALSE));
Eric Andersen08b10341999-11-19 02:38:58 +0000461 message(CONSOLE, "Unmounting filesystems.\r\n");
Erik Andersen7dc16072000-01-04 01:10:25 +0000462 waitfor(run( "umount -a", console, FALSE));
Eric Andersen0460ff21999-10-25 23:32:44 +0000463 sync();
Eric Andersene18c75a1999-11-05 04:23:05 +0000464 if (kernel_version > 0 && kernel_version <= 2 * 65536 + 2 * 256 + 11) {
Eric Andersencf8c9cf1999-11-05 00:31:46 +0000465 /* bdflush, kupdate not needed for kernels >2.2.11 */
Eric Andersene18c75a1999-11-05 04:23:05 +0000466 message(CONSOLE, "Flushing buffers.\r\n");
Eric Andersencf8c9cf1999-11-05 00:31:46 +0000467 bdflush(1, 0);
468 sync();
469 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000470}
471
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000472static void halt_signal(int sig)
Eric Andersencc8ed391999-10-05 16:24:54 +0000473{
Eric Andersenabc7d591999-10-19 00:27:50 +0000474 shutdown_system();
Eric Andersen3ae0c781999-11-04 01:13:21 +0000475 message(CONSOLE,
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000476 "The system is halted. Press CTRL-ALT-DEL or turn off power\r\n");
Eric Andersen3ae0c781999-11-04 01:13:21 +0000477 sync();
Erik Andersen4d1d0111999-12-17 18:44:15 +0000478#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,2,0)
Eric Andersen2cb55071999-12-10 08:25:07 +0000479 if (sig == SIGUSR2)
480 reboot(RB_POWER_OFF);
481 else
Erik Andersen4d1d0111999-12-17 18:44:15 +0000482#endif
Erik Andersen7dc16072000-01-04 01:10:25 +0000483 reboot(RB_HALT_SYSTEM);
Eric Andersenabc7d591999-10-19 00:27:50 +0000484 exit(0);
Eric Andersencc8ed391999-10-05 16:24:54 +0000485}
486
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000487static void reboot_signal(int sig)
Eric Andersencc8ed391999-10-05 16:24:54 +0000488{
Eric Andersenabc7d591999-10-19 00:27:50 +0000489 shutdown_system();
Eric Andersen3ae0c781999-11-04 01:13:21 +0000490 message(CONSOLE, "Please stand by while rebooting the system.\r\n");
491 sync();
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000492 reboot(RB_AUTOBOOT);
Eric Andersenabc7d591999-10-19 00:27:50 +0000493 exit(0);
Eric Andersencc8ed391999-10-05 16:24:54 +0000494}
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000495
Erik Andersen7dc16072000-01-04 01:10:25 +0000496#endif
497
498void new_initAction (const struct initActionType *a,
499 char* process, char* console)
500{
501 initAction* newAction;
502 newAction = calloc ((size_t)(1), sizeof(initAction));
503 if (!newAction) {
504 fprintf(stderr, "Memory allocation failure\n");
505 while (1) sleep(1);
506 }
507 newAction->nextPtr = initActionList;
508 initActionList = newAction;
509 strncpy( newAction->process, process, 255);
510 newAction->action = a->action;
511 newAction->console = console;
512 newAction->pid = 0;
513}
514
515void delete_initAction (initAction *action)
516{
517 initAction *a, *b=NULL;
518 for( a=initActionList ; a; b=a, a=a->nextPtr) {
519 if (a == action && b != NULL) {
520 b->nextPtr=a->nextPtr;
521 free( a);
522 break;
523 }
524 }
525}
526
527void parse_inittab(void)
528{
529 FILE* file;
530 char buf[256];
531 char *p, *q, *r;
532 const struct initActionType *a = actions;
533 int foundIt;
534
535
536 file = fopen(INITTAB, "r");
537 if (file == NULL) {
538 /* No inittab file -- set up some default behavior */
539
540 /* Askfirst shell on tty1 */
541 new_initAction( &(actions[3]), SHELL, console );
542 /* Askfirst shell on tty2 */
543 if (second_console != NULL)
544 new_initAction( &(actions[3]), SHELL, second_console );
Erik Andersen7dc16072000-01-04 01:10:25 +0000545 /* sysinit */
546 new_initAction( &(actions[0]), INIT_SCRIPT, console );
547
548 return;
549 }
550
551 while ( fgets(buf, 255, file) != NULL) {
552 foundIt=FALSE;
553 for(p = buf; *p == ' ' || *p == '\t'; p++);
554 if (*p == '#' || *p == '\n') continue;
555
556 /* Trim the trailing \n */
557 q = strrchr( p, '\n');
558 if (q != NULL)
559 *q='\0';
560
561 /* Skip past the ID field and the runlevel
562 * field (both are ignored) */
563 p = strchr( p, ':');
564
565 /* Now peal off the process field from the end
566 * of the string */
567 q = strrchr( p, ':');
568 if ( q == NULL || *(q+1) == '\0' ) {
569 fprintf(stderr, "Bad inittab entry: %s\n", buf);
570 continue;
571 } else {
572 *q='\0';
573 ++q;
574 }
575
576 /* Now peal off the action field */
577 r = strrchr( p, ':');
578 if ( r == NULL || *(r+1) == '\0') {
579 fprintf(stderr, "Bad inittab entry: %s\n", buf);
580 continue;
581 } else {
582 ++r;
583 }
584
585 /* Ok, now process it */
586 a = actions;
587 while (a->name != 0) {
588 if (strcmp(a->name, r) == 0) {
589 new_initAction( a, q, NULL);
590 foundIt=TRUE;
591 }
592 a++;
593 }
594 if (foundIt==TRUE)
595 continue;
596 else {
597 /* Choke on an unknown action */
598 fprintf(stderr, "Bad inittab entry: %s\n", buf);
599 }
600 }
601 return;
602}
603
604
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000605extern int init_main(int argc, char **argv)
Eric Andersen0460ff21999-10-25 23:32:44 +0000606{
Erik Andersen7dc16072000-01-04 01:10:25 +0000607 initAction *a;
608 pid_t wpid;
609 int status;
Eric Andersen0460ff21999-10-25 23:32:44 +0000610
Eric Andersend73dc5b1999-11-10 23:13:02 +0000611#ifndef DEBUG_INIT
Eric Andersen07274581999-11-21 21:50:07 +0000612 /* Expect to be PID 1 iff we are run as init (not linuxrc) */
613 if (getpid() != 1 && strstr(argv[0], "init")!=NULL ) {
Eric Andersend73dc5b1999-11-10 23:13:02 +0000614 usage( "init\n\nInit is the parent of all processes.\n\n"
615 "This version of init is designed to be run only by the kernel\n");
616 }
Eric Andersend73dc5b1999-11-10 23:13:02 +0000617
Erik Andersen0881de72000-01-05 09:34:26 +0000618 /* from the controlling terminal */
619 setsid();
620
Erik Andersen7dc16072000-01-04 01:10:25 +0000621 /* Set up sig handlers -- be sure to clear all of these in run() */
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000622 signal(SIGUSR1, halt_signal);
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000623 signal(SIGUSR2, reboot_signal);
Erik Andersen0881de72000-01-05 09:34:26 +0000624 signal(SIGINT, reboot_signal);
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000625 signal(SIGTERM, reboot_signal);
Eric Andersen0460ff21999-10-25 23:32:44 +0000626
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000627 /* Turn off rebooting via CTL-ALT-DEL -- we get a
628 * SIGINT on CAD so we can shut things down gracefully... */
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000629 reboot(RB_DISABLE_CAD);
Eric Andersen7f1acfd1999-10-29 23:09:13 +0000630#endif
Erik Andersen7dc16072000-01-04 01:10:25 +0000631
Eric Andersenc7c41d31999-10-28 00:24:35 +0000632 /* Figure out where the default console should be */
633 console_init();
Eric Andersen0460ff21999-10-25 23:32:44 +0000634
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000635 /* Close whatever files are open, and reset the console. */
636 close(0);
637 close(1);
638 close(2);
Eric Andersen7f1acfd1999-10-29 23:09:13 +0000639 set_term(0);
Eric Andersencc8ed391999-10-05 16:24:54 +0000640
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000641 /* Make sure PATH is set to something sane */
Eric Andersenb186d981999-12-03 09:19:54 +0000642 putenv(_PATH_STDPATH);
Eric Andersencc8ed391999-10-05 16:24:54 +0000643
Eric Andersen219d6f51999-11-02 19:43:01 +0000644
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000645 /* Hello world */
Eric Andersen7f1acfd1999-10-29 23:09:13 +0000646#ifndef DEBUG_INIT
Erik Andersen7dc16072000-01-04 01:10:25 +0000647 message(CONSOLE|LOG,
648 "init started: BusyBox v%s (%s) multi-call binary\r\n",
649 BB_VER, BB_BT);
Eric Andersen7f1acfd1999-10-29 23:09:13 +0000650#else
Erik Andersen7dc16072000-01-04 01:10:25 +0000651 message(CONSOLE|LOG,
652 "init(%d) started: BusyBox v%s (%s) multi-call binary\r\n",
653 getpid(), BB_VER, BB_BT);
Eric Andersen7f1acfd1999-10-29 23:09:13 +0000654#endif
Eric Andersencc8ed391999-10-05 16:24:54 +0000655
Eric Andersenc7c41d31999-10-28 00:24:35 +0000656
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000657 /* Mount /proc */
Eric Andersene18c75a1999-11-05 04:23:05 +0000658 if (mount ("proc", "/proc", "proc", 0, 0) == 0) {
Eric Andersen3ae0c781999-11-04 01:13:21 +0000659 message(CONSOLE|LOG, "Mounting /proc: done.\n");
Eric Andersene18c75a1999-11-05 04:23:05 +0000660 kernel_version = get_kernel_revision();
661 } else
Eric Andersen3ae0c781999-11-04 01:13:21 +0000662 message(CONSOLE|LOG, "Mounting /proc: failed!\n");
Eric Andersencc8ed391999-10-05 16:24:54 +0000663
Eric Andersen219d6f51999-11-02 19:43:01 +0000664 /* Make sure there is enough memory to do something useful. */
665 check_memory();
Eric Andersencc8ed391999-10-05 16:24:54 +0000666
Eric Andersend00c2621999-12-07 08:37:31 +0000667 /* Check if we are supposed to be in single user mode */
668 if ( argc > 1 && (!strcmp(argv[1], "single") ||
Erik Andersen0881de72000-01-05 09:34:26 +0000669 !strcmp(argv[1], "-s") || !strcmp(argv[1], "1")))
670 {
Erik Andersen7dc16072000-01-04 01:10:25 +0000671 /* Ask first then start a shell on tty2 */
672 if (second_console != NULL)
673 new_initAction( &(actions[3]), SHELL, second_console);
674 /* Ask first then start a shell on tty1 */
675 new_initAction( &(actions[3]), SHELL, console);
676 } else {
677 /* Not in single user mode -- see what inittab says */
678 parse_inittab();
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000679 }
680
Erik Andersen7dc16072000-01-04 01:10:25 +0000681 /* Now run everything that needs to be run */
Eric Andersen84b00921999-12-11 04:16:51 +0000682
Erik Andersen0881de72000-01-05 09:34:26 +0000683 /* First run the sysinit command */
Erik Andersen7dc16072000-01-04 01:10:25 +0000684 for( a=initActionList ; a; a=a->nextPtr) {
685 if (a->action == SYSINIT) {
686 waitfor(run(a->process, console, FALSE));
687 /* Now remove the "sysinit" entry from the list */
688 delete_initAction( a);
Eric Andersend00c2621999-12-07 08:37:31 +0000689 }
690 }
Erik Andersen7dc16072000-01-04 01:10:25 +0000691 /* Next run anything that wants to block */
692 for( a=initActionList ; a; a=a->nextPtr) {
693 if (a->action == WAIT) {
694 waitfor(run(a->process, console, FALSE));
695 /* Now remove the "wait" entry from the list */
696 delete_initAction( a);
697 }
698 }
699 /* Next run anything to be run only once */
700 for( a=initActionList ; a; a=a->nextPtr) {
701 if (a->action == ONCE) {
702 run(a->process, console, FALSE);
703 /* Now remove the "once" entry from the list */
704 delete_initAction( a);
705 }
706 }
Eric Andersen219d6f51999-11-02 19:43:01 +0000707
Erik Andersen0881de72000-01-05 09:34:26 +0000708 /* Now run the looping stuff for the rest of forever */
709 while (1) {
Erik Andersen7dc16072000-01-04 01:10:25 +0000710 for( a=initActionList ; a; a=a->nextPtr) {
711 /* Only run stuff with pid==0. If they have
712 * a pid, that means they are still running */
713 if (a->pid == 0) {
714 switch(a->action) {
715 case RESPAWN:
716 /* run the respawn stuff */
717 a->pid = run(a->process, console, FALSE);
718 break;
719 case ASKFIRST:
720 /* run the askfirst stuff */
Erik Andersen0881de72000-01-05 09:34:26 +0000721 a->pid = run(a->process, console, TRUE);
Erik Andersen7dc16072000-01-04 01:10:25 +0000722 break;
Erik Andersen0881de72000-01-05 09:34:26 +0000723 /* silence the compiler's incessant whining */
Erik Andersen7dc16072000-01-04 01:10:25 +0000724 default:
725 break;
726 }
727 }
728 }
Erik Andersen0881de72000-01-05 09:34:26 +0000729 /* Wait for a child process to exit */
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000730 wpid = wait(&status);
Eric Andersena7456061999-10-27 02:31:32 +0000731 if (wpid > 0 ) {
Erik Andersen0881de72000-01-05 09:34:26 +0000732 /* Find out who died and clean up their corpse */
Erik Andersen7dc16072000-01-04 01:10:25 +0000733 for( a=initActionList ; a; a=a->nextPtr) {
734 if (a->pid==wpid) {
735 a->pid=0;
Erik Andersen0881de72000-01-05 09:34:26 +0000736 message(LOG, "Process '%s' (pid %d) exited. Scheduling it for restart.\n",
737 a->process, wpid);
Erik Andersen7dc16072000-01-04 01:10:25 +0000738 }
Eric Andersenb6a44b81999-11-13 04:47:09 +0000739 }
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000740 }
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000741 sleep(1);
742 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000743}
Erik Andersen9c88cac1999-12-30 09:25:17 +0000744