blob: 088a890cc6132cc6944283cbb06729d0cc8917d5 [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>
26#include <stdlib.h>
27#include <stdarg.h>
28#include <unistd.h>
29#include <errno.h>
30#include <signal.h>
31#include <termios.h>
Eric Andersenb186d981999-12-03 09:19:54 +000032#include <paths.h>
Eric Andersencc8ed391999-10-05 16:24:54 +000033#include <sys/types.h>
34#include <sys/fcntl.h>
35#include <sys/wait.h>
36#include <string.h>
37#include <sys/mount.h>
38#include <sys/reboot.h>
39#include <sys/kdaemon.h>
Eric Andersencc8ed391999-10-05 16:24:54 +000040#include <sys/sysmacros.h>
Eric Andersen8a8fbb81999-10-26 00:18:56 +000041#include <linux/serial.h> /* for serial_struct */
42#include <sys/vt.h> /* for vt_stat */
Eric Andersenabc7d591999-10-19 00:27:50 +000043#include <sys/ioctl.h>
Eric Andersen4c781471999-11-26 08:12:56 +000044#ifdef BB_SYSLOGD
45#include <sys/syslog.h>
46#endif
Eric Andersencc8ed391999-10-05 16:24:54 +000047
Eric Andersenbe971d61999-11-03 16:52:50 +000048#define VT_PRIMARY "/dev/tty1" /* Primary virtual console */
49#define VT_SECONDARY "/dev/tty2" /* Virtual console */
50#define VT_LOG "/dev/tty3" /* Virtual console */
Eric Andersen219d6f51999-11-02 19:43:01 +000051#define SERIAL_CON0 "/dev/ttyS0" /* Primary serial console */
52#define SERIAL_CON1 "/dev/ttyS1" /* Serial console */
Eric Andersen8a8fbb81999-10-26 00:18:56 +000053#define SHELL "/bin/sh" /* Default shell */
Eric Andersen219d6f51999-11-02 19:43:01 +000054#define INITSCRIPT "/etc/init.d/rcS" /* Initscript. */
Eric Andersen0460ff21999-10-25 23:32:44 +000055
Eric Andersen3ae0c781999-11-04 01:13:21 +000056#define LOG 0x1
57#define CONSOLE 0x2
Eric Andersenb186d981999-12-03 09:19:54 +000058static char *console = _PATH_CONSOLE;
Eric Andersenbe971d61999-11-03 16:52:50 +000059static char *second_console = VT_SECONDARY;
60static char *log = VT_LOG;
Eric Andersene18c75a1999-11-05 04:23:05 +000061static int kernel_version = 0;
Eric Andersen0460ff21999-10-25 23:32:44 +000062
63
64/* try to open up the specified device */
Eric Andersen8a8fbb81999-10-26 00:18:56 +000065int device_open(char *device, int mode)
Eric Andersencc8ed391999-10-05 16:24:54 +000066{
Eric Andersen0460ff21999-10-25 23:32:44 +000067 int m, f, fd = -1;
Eric Andersen8a8fbb81999-10-26 00:18:56 +000068
Eric Andersen7f1acfd1999-10-29 23:09:13 +000069 m = mode | O_NONBLOCK;
Eric Andersen8a8fbb81999-10-26 00:18:56 +000070
Eric Andersen0460ff21999-10-25 23:32:44 +000071 /* Retry up to 5 times */
Eric Andersen8a8fbb81999-10-26 00:18:56 +000072 for (f = 0; f < 5; f++)
73 if ((fd = open(device, m)) >= 0)
74 break;
75 if (fd < 0)
76 return fd;
Eric Andersen3ae0c781999-11-04 01:13:21 +000077 /* Reset original flags. */
Eric Andersen0460ff21999-10-25 23:32:44 +000078 if (m != mode)
79 fcntl(fd, F_SETFL, mode);
80 return fd;
81}
Eric Andersencc8ed391999-10-05 16:24:54 +000082
Eric Andersen3ae0c781999-11-04 01:13:21 +000083/* print a message to the specified device:
84 * device may be bitwise-or'd from LOG | CONSOLE */
85void message(int device, char *fmt, ...)
Eric Andersen0460ff21999-10-25 23:32:44 +000086{
87 int fd;
88 va_list arguments;
Eric Andersen4c781471999-11-26 08:12:56 +000089#ifdef BB_SYSLOGD
90
91 /* Log the message to syslogd */
92 if (device & LOG ) {
93 char msg[1024];
94 va_start(arguments, fmt);
95 vsnprintf(msg, sizeof(msg), fmt, arguments);
96 va_end(arguments);
97 syslog(LOG_DAEMON|LOG_NOTICE, msg);
98 }
99
100#else
101 static int log_fd=-1;
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000102
Eric Andersen3ae0c781999-11-04 01:13:21 +0000103 /* Take full control of the log tty, and never close it.
104 * It's mine, all mine! Muhahahaha! */
Eric Andersen07e52971999-11-07 07:38:08 +0000105 if (log_fd < 0) {
106 if (log == NULL) {
107 /* don't even try to log, because there is no such console */
108 log_fd = -2;
109 /* log to main console instead */
110 device = CONSOLE;
111 }
112 else if ((log_fd = device_open(log, O_RDWR|O_NDELAY)) < 0) {
Eric Andersen3ae0c781999-11-04 01:13:21 +0000113 log_fd=-1;
114 fprintf(stderr, "Bummer, can't write to log on %s!\r\n", log);
115 fflush(stderr);
116 return;
117 }
118 }
Eric Andersen07e52971999-11-07 07:38:08 +0000119 if ( (device & LOG) && (log_fd >= 0) ) {
Eric Andersen0460ff21999-10-25 23:32:44 +0000120 va_start(arguments, fmt);
Eric Andersen3ae0c781999-11-04 01:13:21 +0000121 vdprintf(log_fd, fmt, arguments);
Eric Andersena7456061999-10-27 02:31:32 +0000122 va_end(arguments);
123 }
Eric Andersen4c781471999-11-26 08:12:56 +0000124#endif
125
Eric Andersen3ae0c781999-11-04 01:13:21 +0000126 if (device & CONSOLE) {
Eric Andersended62591999-11-18 00:19:26 +0000127 /* Always send console messages to /dev/console so people will see them. */
Eric Andersenb186d981999-12-03 09:19:54 +0000128 if ((fd = device_open(_PATH_CONSOLE, O_WRONLY|O_NOCTTY|O_NDELAY)) >= 0) {
Eric Andersen3ae0c781999-11-04 01:13:21 +0000129 va_start(arguments, fmt);
130 vdprintf(fd, fmt, arguments);
131 va_end(arguments);
132 close(fd);
133 } else {
134 fprintf(stderr, "Bummer, can't print: ");
135 va_start(arguments, fmt);
136 vfprintf(stderr, fmt, arguments);
137 fflush(stderr);
138 va_end(arguments);
139 }
140 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000141}
142
Eric Andersen3ae0c781999-11-04 01:13:21 +0000143
Eric Andersen0460ff21999-10-25 23:32:44 +0000144/* Set terminal settings to reasonable defaults */
Eric Andersenc7c41d31999-10-28 00:24:35 +0000145void set_term( int fd)
Eric Andersencc8ed391999-10-05 16:24:54 +0000146{
Eric Andersen0460ff21999-10-25 23:32:44 +0000147 struct termios tty;
Eric Andersen08b10341999-11-19 02:38:58 +0000148#if 0
Eric Andersen3ae0c781999-11-04 01:13:21 +0000149 static const char control_characters[] = {
150 '\003', '\034', '\177', '\030', '\004', '\0',
151 '\1', '\0', '\021', '\023', '\032', '\0', '\022',
152 '\017', '\027', '\026', '\0'
153 };
Eric Andersen08b10341999-11-19 02:38:58 +0000154#else
155 static const char control_characters[] = {
156 '\003', '\034', '\177', '\025', '\004', '\0',
157 '\1', '\0', '\021', '\023', '\032', '\0', '\022',
158 '\017', '\027', '\026', '\0'
159 };
160#endif
Eric Andersencc8ed391999-10-05 16:24:54 +0000161
Eric Andersenc7c41d31999-10-28 00:24:35 +0000162 tcgetattr(fd, &tty);
Eric Andersencc8ed391999-10-05 16:24:54 +0000163
Eric Andersen08b10341999-11-19 02:38:58 +0000164 /* set control chars */
165 memcpy(tty.c_cc, control_characters, sizeof(control_characters));
Eric Andersencc8ed391999-10-05 16:24:54 +0000166
Eric Andersen219d6f51999-11-02 19:43:01 +0000167 /* use line dicipline 0 */
Eric Andersen7f1acfd1999-10-29 23:09:13 +0000168 tty.c_line = 0;
169
Eric Andersen08b10341999-11-19 02:38:58 +0000170 /* Make it be sane */
171 //tty.c_cflag &= CBAUD|CBAUDEX|CSIZE|CSTOPB|PARENB|PARODD;
172 //tty.c_cflag |= HUPCL|CLOCAL;
173
174 /* input modes */
175 tty.c_iflag = ICRNL|IXON|IXOFF;
176
Eric Andersen7f1acfd1999-10-29 23:09:13 +0000177 /* output modes */
Eric Andersenc7c41d31999-10-28 00:24:35 +0000178 tty.c_oflag = OPOST|ONLCR;
Eric Andersen7f1acfd1999-10-29 23:09:13 +0000179
180 /* local modes */
Eric Andersen08b10341999-11-19 02:38:58 +0000181 tty.c_lflag = ISIG|ICANON|ECHO|ECHOE|ECHOK|ECHOCTL|ECHOKE|IEXTEN;
Eric Andersencc8ed391999-10-05 16:24:54 +0000182
Eric Andersenc7c41d31999-10-28 00:24:35 +0000183 tcsetattr(fd, TCSANOW, &tty);
Eric Andersencc8ed391999-10-05 16:24:54 +0000184}
185
Eric Andersen219d6f51999-11-02 19:43:01 +0000186/* How much memory does this machine have? */
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000187static int mem_total()
Eric Andersencc8ed391999-10-05 16:24:54 +0000188{
Eric Andersenabc7d591999-10-19 00:27:50 +0000189 char s[80];
Eric Andersen219d6f51999-11-02 19:43:01 +0000190 char *p = "/proc/meminfo";
Eric Andersenabc7d591999-10-19 00:27:50 +0000191 FILE *f;
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000192 const char pattern[] = "MemTotal:";
Eric Andersencc8ed391999-10-05 16:24:54 +0000193
Eric Andersen219d6f51999-11-02 19:43:01 +0000194 if ((f = fopen(p, "r")) < 0) {
Eric Andersen3ae0c781999-11-04 01:13:21 +0000195 message(LOG, "Error opening %s: %s\n", p, strerror( errno));
Eric Andersen219d6f51999-11-02 19:43:01 +0000196 return -1;
197 }
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000198 while (NULL != fgets(s, 79, f)) {
199 p = strstr(s, pattern);
Eric Andersenabc7d591999-10-19 00:27:50 +0000200 if (NULL != p) {
201 fclose(f);
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000202 return (atoi(p + strlen(pattern)));
Eric Andersenabc7d591999-10-19 00:27:50 +0000203 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000204 }
Eric Andersenabc7d591999-10-19 00:27:50 +0000205 return -1;
Eric Andersencc8ed391999-10-05 16:24:54 +0000206}
207
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000208static void console_init()
Eric Andersen0460ff21999-10-25 23:32:44 +0000209{
210 int fd;
211 int tried_devcons = 0;
Eric Andersen219d6f51999-11-02 19:43:01 +0000212 int tried_vtprimary = 0;
Eric Andersen07e52971999-11-07 07:38:08 +0000213 struct serial_struct sr;
Eric Andersen0460ff21999-10-25 23:32:44 +0000214 char *s;
215
Eric Andersen219d6f51999-11-02 19:43:01 +0000216 if ((s = getenv("CONSOLE")) != NULL) {
Eric Andersen0460ff21999-10-25 23:32:44 +0000217 console = s;
Eric Andersen07e52971999-11-07 07:38:08 +0000218 }
Eric Andersenfbb39c81999-11-08 17:00:52 +0000219#if #cpu(sparc)
Eric Andersen07e52971999-11-07 07:38:08 +0000220 /* sparc kernel supports console=tty[ab] parameter which is also
221 * passed to init, so catch it here */
222 else if ((s = getenv("console")) != NULL) {
223 /* remap tty[ab] to /dev/ttyS[01] */
224 if (strcmp( s, "ttya" )==0)
225 console = SERIAL_CON0;
226 else if (strcmp( s, "ttyb" )==0)
227 console = SERIAL_CON1;
228 }
Eric Andersen219d6f51999-11-02 19:43:01 +0000229#endif
Eric Andersen07e52971999-11-07 07:38:08 +0000230 else {
231 struct vt_stat vt;
232 static char the_console[13];
233
234 console = the_console;
235 /* 2.2 kernels: identify the real console backend and try to use it */
Eric Andersen08b10341999-11-19 02:38:58 +0000236 if (ioctl(0, TIOCGSERIAL, &sr) == 0) {
Eric Andersen07e52971999-11-07 07:38:08 +0000237 /* this is a serial console */
238 snprintf( the_console, sizeof the_console, "/dev/ttyS%d", sr.line );
239 }
240 else if (ioctl(0, VT_GETSTATE, &vt) == 0) {
241 /* this is linux virtual tty */
242 snprintf( the_console, sizeof the_console, "/dev/tty%d", vt.v_active );
243 } else {
Eric Andersenb186d981999-12-03 09:19:54 +0000244 console = _PATH_CONSOLE;
Eric Andersen07e52971999-11-07 07:38:08 +0000245 tried_devcons++;
246 }
Eric Andersen0460ff21999-10-25 23:32:44 +0000247 }
Eric Andersena7456061999-10-27 02:31:32 +0000248
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000249 while ((fd = open(console, O_RDONLY | O_NONBLOCK)) < 0) {
Eric Andersen219d6f51999-11-02 19:43:01 +0000250 /* Can't open selected console -- try /dev/console */
Eric Andersen0460ff21999-10-25 23:32:44 +0000251 if (!tried_devcons) {
252 tried_devcons++;
Eric Andersenb186d981999-12-03 09:19:54 +0000253 console = _PATH_CONSOLE;
Eric Andersen0460ff21999-10-25 23:32:44 +0000254 continue;
255 }
Eric Andersenbe971d61999-11-03 16:52:50 +0000256 /* Can't open selected console -- try vt1 */
257 if (!tried_vtprimary) {
258 tried_vtprimary++;
259 console = VT_PRIMARY;
260 continue;
261 }
Eric Andersen0460ff21999-10-25 23:32:44 +0000262 break;
263 }
264 if (fd < 0)
Eric Andersen219d6f51999-11-02 19:43:01 +0000265 /* Perhaps we should panic here? */
Eric Andersen0460ff21999-10-25 23:32:44 +0000266 console = "/dev/null";
Eric Andersen07e52971999-11-07 07:38:08 +0000267 else {
268 /* check for serial console and disable logging to tty3 & running a
269 * shell to tty2 */
270 if (ioctl(0,TIOCGSERIAL,&sr) == 0) {
Eric Andersen08b10341999-11-19 02:38:58 +0000271 message(LOG|CONSOLE, "serial console detected. Disabling 2nd virtual terminal.\r\n", console );
Eric Andersen07e52971999-11-07 07:38:08 +0000272 log = NULL;
273 second_console = NULL;
274 }
Eric Andersen0460ff21999-10-25 23:32:44 +0000275 close(fd);
Eric Andersen07e52971999-11-07 07:38:08 +0000276 }
Eric Andersen3ae0c781999-11-04 01:13:21 +0000277 message(LOG, "console=%s\n", console );
Eric Andersen0460ff21999-10-25 23:32:44 +0000278}
279
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000280static int waitfor(int pid)
Eric Andersen0460ff21999-10-25 23:32:44 +0000281{
282 int status, wpid;
283
Eric Andersen3ae0c781999-11-04 01:13:21 +0000284 message(LOG, "Waiting for process %d.\n", pid);
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000285 while ((wpid = wait(&status)) != pid) {
286 if (wpid > 0)
Eric Andersen3ae0c781999-11-04 01:13:21 +0000287 message(LOG, "pid %d exited, status=0x%x.\n", wpid, status);
Eric Andersen0460ff21999-10-25 23:32:44 +0000288 }
289 return wpid;
290}
291
Eric Andersena7456061999-10-27 02:31:32 +0000292
Eric Andersenc7c41d31999-10-28 00:24:35 +0000293static pid_t run(const char * const* command,
294 char *terminal, int get_enter)
Eric Andersen0460ff21999-10-25 23:32:44 +0000295{
Eric Andersen2f6c04f1999-11-01 23:59:44 +0000296 int fd;
Eric Andersenc7c41d31999-10-28 00:24:35 +0000297 pid_t pid;
Eric Andersen219d6f51999-11-02 19:43:01 +0000298 const char * const* cmd = command+1;
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000299 static const char press_enter[] =
Eric Andersen0460ff21999-10-25 23:32:44 +0000300 "\nPlease press Enter to activate this console. ";
301
Eric Andersen0460ff21999-10-25 23:32:44 +0000302 if ((pid = fork()) == 0) {
303 /* Clean up */
304 close(0);
305 close(1);
306 close(2);
307 setsid();
308
Eric Andersen2f6c04f1999-11-01 23:59:44 +0000309 /* Reset signal handlers set for parent process */
310 signal(SIGUSR1, SIG_DFL);
311 signal(SIGUSR2, SIG_DFL);
312 signal(SIGINT, SIG_DFL);
313 signal(SIGTERM, SIG_DFL);
314
315 if ((fd = device_open(terminal, O_RDWR)) < 0) {
Eric Andersen3ae0c781999-11-04 01:13:21 +0000316 message(LOG, "Bummer, can't open %s\r\n", terminal);
Eric Andersen2f6c04f1999-11-01 23:59:44 +0000317 exit(-1);
318 }
319 dup(fd);
320 dup(fd);
Eric Andersenc7c41d31999-10-28 00:24:35 +0000321 tcsetpgrp(0, getpgrp());
322 set_term(0);
Eric Andersen0460ff21999-10-25 23:32:44 +0000323
Eric Andersen2f6c04f1999-11-01 23:59:44 +0000324 if (get_enter==TRUE) {
Eric Andersen0460ff21999-10-25 23:32:44 +0000325 /*
326 * Save memory by not exec-ing anything large (like a shell)
327 * before the user wants it. This is critical if swap is not
328 * enabled and the system has low memory. Generally this will
329 * be run on the second virtual console, and the first will
330 * be allowed to start a shell or whatever an init script
331 * specifies.
332 */
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000333 char c;
Eric Andersen3ae0c781999-11-04 01:13:21 +0000334 message(LOG, "Waiting for enter to start '%s' (pid %d, console %s)\r\n",
335 *cmd, getpid(), terminal );
Eric Andersen7f1acfd1999-10-29 23:09:13 +0000336 write(1, press_enter, sizeof(press_enter) - 1);
Eric Andersen0460ff21999-10-25 23:32:44 +0000337 read(0, &c, 1);
338 }
339
340 /* Log the process name and args */
Eric Andersen3ae0c781999-11-04 01:13:21 +0000341 message(LOG, "Starting pid %d, console %s: '", getpid(), terminal);
342 while ( *cmd) message(LOG, "%s ", *cmd++);
343 message(LOG, "'\r\n");
Eric Andersen2f6c04f1999-11-01 23:59:44 +0000344
Eric Andersenc7c41d31999-10-28 00:24:35 +0000345 /* Now run it. The new program will take over this PID,
Eric Andersena7456061999-10-27 02:31:32 +0000346 * so nothing further in init.c should be run. */
Eric Andersenc7c41d31999-10-28 00:24:35 +0000347 execvp(*command, (char**)command+1);
Eric Andersen0460ff21999-10-25 23:32:44 +0000348
Eric Andersen3ae0c781999-11-04 01:13:21 +0000349 message(LOG, "Bummer, could not run '%s'\n", command);
Eric Andersen0460ff21999-10-25 23:32:44 +0000350 exit(-1);
351 }
352 return pid;
353}
354
Eric Andersen219d6f51999-11-02 19:43:01 +0000355/* Make sure there is enough memory to do something useful. *
356 * Calls swapon if needed so be sure /proc is mounted. */
357static void check_memory()
358{
359 struct stat statbuf;
360 const char* const swap_on_cmd[] =
361 { "/bin/swapon", "swapon", "-a", 0};
Eric Andersen219d6f51999-11-02 19:43:01 +0000362
363 if (mem_total() > 3500)
364 return;
365
366 if (stat("/etc/fstab", &statbuf) == 0) {
367 /* Try to turn on swap */
368 waitfor(run(swap_on_cmd, log, FALSE));
369 if (mem_total() < 3500)
370 goto goodnight;
371 } else
372 goto goodnight;
373 return;
374
375goodnight:
Eric Andersen3ae0c781999-11-04 01:13:21 +0000376 message(CONSOLE, "Sorry, your computer does not have enough memory.\r\n");
Eric Andersen219d6f51999-11-02 19:43:01 +0000377 while (1) sleep(1);
378}
379
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000380static void shutdown_system(void)
Eric Andersencc8ed391999-10-05 16:24:54 +0000381{
Eric Andersen2f6c04f1999-11-01 23:59:44 +0000382 const char* const swap_off_cmd[] = { "swapoff", "swapoff", "-a", 0};
383 const char* const umount_cmd[] = { "umount", "umount", "-a", "-n", 0};
Eric Andersencc8ed391999-10-05 16:24:54 +0000384
Eric Andersen2f6c04f1999-11-01 23:59:44 +0000385#ifndef DEBUG_INIT
Eric Andersen0460ff21999-10-25 23:32:44 +0000386 /* Allow Ctrl-Alt-Del to reboot system. */
387 reboot(RB_ENABLE_CAD);
Eric Andersen2f6c04f1999-11-01 23:59:44 +0000388#endif
Eric Andersen08b10341999-11-19 02:38:58 +0000389 message(CONSOLE, "\r\nThe system is going down NOW !!\r\n");
Eric Andersen3ae0c781999-11-04 01:13:21 +0000390 sync();
Eric Andersen0460ff21999-10-25 23:32:44 +0000391 /* Send signals to every process _except_ pid 1 */
Eric Andersen3ae0c781999-11-04 01:13:21 +0000392 message(CONSOLE, "Sending SIGHUP to all processes.\r\n");
Eric Andersen2f6c04f1999-11-01 23:59:44 +0000393#ifndef DEBUG_INIT
Eric Andersen0460ff21999-10-25 23:32:44 +0000394 kill(-1, SIGHUP);
Eric Andersen2f6c04f1999-11-01 23:59:44 +0000395#endif
Eric Andersen0460ff21999-10-25 23:32:44 +0000396 sleep(2);
397 sync();
Eric Andersen3ae0c781999-11-04 01:13:21 +0000398 message(CONSOLE, "Sending SIGKILL to all processes.\r\n");
Eric Andersen2f6c04f1999-11-01 23:59:44 +0000399#ifndef DEBUG_INIT
Eric Andersen0460ff21999-10-25 23:32:44 +0000400 kill(-1, SIGKILL);
Eric Andersen2f6c04f1999-11-01 23:59:44 +0000401#endif
Eric Andersen0460ff21999-10-25 23:32:44 +0000402 sleep(1);
Eric Andersen08b10341999-11-19 02:38:58 +0000403 message(CONSOLE, "Disabling swap.\r\n");
Eric Andersen3ae0c781999-11-04 01:13:21 +0000404 waitfor(run( swap_off_cmd, console, FALSE));
Eric Andersen08b10341999-11-19 02:38:58 +0000405 message(CONSOLE, "Unmounting filesystems.\r\n");
Eric Andersen3ae0c781999-11-04 01:13:21 +0000406 waitfor(run( umount_cmd, console, FALSE));
Eric Andersen0460ff21999-10-25 23:32:44 +0000407 sync();
Eric Andersene18c75a1999-11-05 04:23:05 +0000408 if (kernel_version > 0 && kernel_version <= 2 * 65536 + 2 * 256 + 11) {
Eric Andersencf8c9cf1999-11-05 00:31:46 +0000409 /* bdflush, kupdate not needed for kernels >2.2.11 */
Eric Andersene18c75a1999-11-05 04:23:05 +0000410 message(CONSOLE, "Flushing buffers.\r\n");
Eric Andersencf8c9cf1999-11-05 00:31:46 +0000411 bdflush(1, 0);
412 sync();
413 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000414}
415
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000416static void halt_signal(int sig)
Eric Andersencc8ed391999-10-05 16:24:54 +0000417{
Eric Andersenabc7d591999-10-19 00:27:50 +0000418 shutdown_system();
Eric Andersen3ae0c781999-11-04 01:13:21 +0000419 message(CONSOLE,
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000420 "The system is halted. Press CTRL-ALT-DEL or turn off power\r\n");
Eric Andersen3ae0c781999-11-04 01:13:21 +0000421 sync();
Eric Andersen2f6c04f1999-11-01 23:59:44 +0000422#ifndef DEBUG_INIT
Eric Andersenb186d981999-12-03 09:19:54 +0000423 while (1) sleep(1);
Eric Andersen3ae0c781999-11-04 01:13:21 +0000424 reboot(RB_HALT_SYSTEM);
425 //reboot(RB_POWER_OFF);
Eric Andersen2f6c04f1999-11-01 23:59:44 +0000426#endif
Eric Andersenabc7d591999-10-19 00:27:50 +0000427 exit(0);
Eric Andersencc8ed391999-10-05 16:24:54 +0000428}
429
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000430static void reboot_signal(int sig)
Eric Andersencc8ed391999-10-05 16:24:54 +0000431{
Eric Andersenabc7d591999-10-19 00:27:50 +0000432 shutdown_system();
Eric Andersen3ae0c781999-11-04 01:13:21 +0000433 message(CONSOLE, "Please stand by while rebooting the system.\r\n");
434 sync();
Eric Andersenb186d981999-12-03 09:19:54 +0000435 while (1) sleep(1);
Eric Andersen2f6c04f1999-11-01 23:59:44 +0000436#ifndef DEBUG_INIT
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000437 reboot(RB_AUTOBOOT);
Eric Andersen2f6c04f1999-11-01 23:59:44 +0000438#endif
Eric Andersenabc7d591999-10-19 00:27:50 +0000439 exit(0);
Eric Andersencc8ed391999-10-05 16:24:54 +0000440}
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000441
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000442extern int init_main(int argc, char **argv)
Eric Andersen0460ff21999-10-25 23:32:44 +0000443{
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000444 int run_rc = TRUE;
Eric Andersen219d6f51999-11-02 19:43:01 +0000445 int wait_for_enter = TRUE;
Eric Andersenc7c41d31999-10-28 00:24:35 +0000446 pid_t pid1 = 0;
447 pid_t pid2 = 0;
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000448 struct stat statbuf;
Eric Andersenb6a44b81999-11-13 04:47:09 +0000449 const char* const rc_script_command[] = { INITSCRIPT, INITSCRIPT, 0};
450 const char* const shell_command[] = { SHELL, "-" SHELL, 0};
451 const char* const* tty0_command = shell_command;
452 const char* const* tty1_command = shell_command;
Eric Andersen4c781471999-11-26 08:12:56 +0000453#ifdef BB_INIT_CMD_IF_RC_SCRIPT_EXITS
454 const char* const rc_exit_command[] = { "BB_INIT_CMD_IF_RC_SCRIPT_EXITS",
455 "BB_INIT_CMD_IF_RC_SCRIPT_EXITS", 0 };
Eric Andersenb6a44b81999-11-13 04:47:09 +0000456#endif
457
Eric Andersen2f6c04f1999-11-01 23:59:44 +0000458#ifdef DEBUG_INIT
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000459 char *hello_msg_format =
Eric Andersen04579781999-10-29 23:12:50 +0000460 "init(%d) started: BusyBox v%s (%s) multi-call binary\r\n";
Eric Andersen2f6c04f1999-11-01 23:59:44 +0000461#else
462 char *hello_msg_format =
463 "init started: BusyBox v%s (%s) multi-call binary\r\n";
Eric Andersen7f1acfd1999-10-29 23:09:13 +0000464#endif
Eric Andersen0460ff21999-10-25 23:32:44 +0000465
Eric Andersen219d6f51999-11-02 19:43:01 +0000466
Eric Andersend73dc5b1999-11-10 23:13:02 +0000467#ifndef DEBUG_INIT
Eric Andersen07274581999-11-21 21:50:07 +0000468 /* Expect to be PID 1 iff we are run as init (not linuxrc) */
469 if (getpid() != 1 && strstr(argv[0], "init")!=NULL ) {
Eric Andersend73dc5b1999-11-10 23:13:02 +0000470 usage( "init\n\nInit is the parent of all processes.\n\n"
471 "This version of init is designed to be run only by the kernel\n");
472 }
473#endif
474
Eric Andersen219d6f51999-11-02 19:43:01 +0000475 /* Check if we are supposed to be in single user mode */
476 if ( argc > 1 && (!strcmp(argv[1], "single") ||
477 !strcmp(argv[1], "-s") || !strcmp(argv[1], "1"))) {
478 run_rc = FALSE;
479 }
480
481
Eric Andersen2f6c04f1999-11-01 23:59:44 +0000482 /* Set up sig handlers -- be sure to
483 * clear all of these in run() */
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000484 signal(SIGUSR1, halt_signal);
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000485 signal(SIGUSR2, reboot_signal);
486 signal(SIGINT, reboot_signal);
487 signal(SIGTERM, reboot_signal);
Eric Andersen0460ff21999-10-25 23:32:44 +0000488
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000489 /* Turn off rebooting via CTL-ALT-DEL -- we get a
490 * SIGINT on CAD so we can shut things down gracefully... */
Eric Andersen2f6c04f1999-11-01 23:59:44 +0000491#ifndef DEBUG_INIT
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000492 reboot(RB_DISABLE_CAD);
Eric Andersen7f1acfd1999-10-29 23:09:13 +0000493#endif
Eric Andersen219d6f51999-11-02 19:43:01 +0000494
Eric Andersenc7c41d31999-10-28 00:24:35 +0000495 /* Figure out where the default console should be */
496 console_init();
Eric Andersen0460ff21999-10-25 23:32:44 +0000497
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000498 /* Close whatever files are open, and reset the console. */
499 close(0);
500 close(1);
501 close(2);
Eric Andersen7f1acfd1999-10-29 23:09:13 +0000502 set_term(0);
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000503 setsid();
Eric Andersencc8ed391999-10-05 16:24:54 +0000504
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000505 /* Make sure PATH is set to something sane */
Eric Andersenb186d981999-12-03 09:19:54 +0000506 putenv(_PATH_STDPATH);
Eric Andersencc8ed391999-10-05 16:24:54 +0000507
Eric Andersen219d6f51999-11-02 19:43:01 +0000508
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000509 /* Hello world */
Eric Andersen7f1acfd1999-10-29 23:09:13 +0000510#ifndef DEBUG_INIT
Eric Andersen3ae0c781999-11-04 01:13:21 +0000511 message(CONSOLE|LOG, hello_msg_format, BB_VER, BB_BT);
Eric Andersen7f1acfd1999-10-29 23:09:13 +0000512#else
Eric Andersen3ae0c781999-11-04 01:13:21 +0000513 message(CONSOLE|LOG, hello_msg_format, getpid(), BB_VER, BB_BT);
Eric Andersen7f1acfd1999-10-29 23:09:13 +0000514#endif
Eric Andersencc8ed391999-10-05 16:24:54 +0000515
Eric Andersenc7c41d31999-10-28 00:24:35 +0000516
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000517 /* Mount /proc */
Eric Andersene18c75a1999-11-05 04:23:05 +0000518 if (mount ("proc", "/proc", "proc", 0, 0) == 0) {
Eric Andersen3ae0c781999-11-04 01:13:21 +0000519 message(CONSOLE|LOG, "Mounting /proc: done.\n");
Eric Andersene18c75a1999-11-05 04:23:05 +0000520 kernel_version = get_kernel_revision();
521 } else
Eric Andersen3ae0c781999-11-04 01:13:21 +0000522 message(CONSOLE|LOG, "Mounting /proc: failed!\n");
Eric Andersencc8ed391999-10-05 16:24:54 +0000523
Eric Andersen219d6f51999-11-02 19:43:01 +0000524 /* Make sure there is enough memory to do something useful. */
525 check_memory();
Eric Andersencc8ed391999-10-05 16:24:54 +0000526
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000527
528 /* Make sure an init script exists before trying to run it */
Eric Andersen219d6f51999-11-02 19:43:01 +0000529 if (run_rc == TRUE && stat(INITSCRIPT, &statbuf)==0) {
530 wait_for_enter = FALSE;
Eric Andersenb6a44b81999-11-13 04:47:09 +0000531 tty0_command = rc_script_command;
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000532 }
533
Eric Andersen219d6f51999-11-02 19:43:01 +0000534
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000535 /* Ok, now launch the rc script and/or prepare to
536 * start up some VTs if somebody hits enter...
537 */
538 for (;;) {
Eric Andersenc7c41d31999-10-28 00:24:35 +0000539 pid_t wpid;
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000540 int status;
541
Eric Andersenb6a44b81999-11-13 04:47:09 +0000542 if (pid1 == 0 && tty0_command) {
543 pid1 = run(tty0_command, console, wait_for_enter);
Eric Andersencc8ed391999-10-05 16:24:54 +0000544 }
Eric Andersenb6a44b81999-11-13 04:47:09 +0000545 if (pid2 == 0 && tty1_command && second_console) {
546 pid2 = run(tty1_command, second_console, TRUE);
Eric Andersencc8ed391999-10-05 16:24:54 +0000547 }
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000548 wpid = wait(&status);
Eric Andersena7456061999-10-27 02:31:32 +0000549 if (wpid > 0 ) {
Eric Andersen3ae0c781999-11-04 01:13:21 +0000550 message(LOG, "pid %d exited, status=%x.\n", wpid, status);
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000551 }
Eric Andersencf8c9cf1999-11-05 00:31:46 +0000552 /* Don't respawn init script if it exits */
Eric Andersen219d6f51999-11-02 19:43:01 +0000553 if (wpid == pid1) {
Eric Andersencf8c9cf1999-11-05 00:31:46 +0000554 if (run_rc == FALSE) {
555 pid1 = 0;
Eric Andersenb6a44b81999-11-13 04:47:09 +0000556 }
Eric Andersen4c781471999-11-26 08:12:56 +0000557#ifdef BB_INIT_CMD_IF_RC_SCRIPT_EXITS
Eric Andersencf8c9cf1999-11-05 00:31:46 +0000558 else {
Eric Andersenb6a44b81999-11-13 04:47:09 +0000559 pid1 = 0;
Eric Andersen219d6f51999-11-02 19:43:01 +0000560 run_rc=FALSE;
561 wait_for_enter=TRUE;
Eric Andersenb6a44b81999-11-13 04:47:09 +0000562 tty0_command=rc_exit_command;
Eric Andersen219d6f51999-11-02 19:43:01 +0000563 }
Eric Andersencf8c9cf1999-11-05 00:31:46 +0000564#endif
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000565 }
566 if (wpid == pid2) {
567 pid2 = 0;
568 }
569 sleep(1);
570 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000571}