blob: 899e430400e22e582bc968fcff0c68c8f623eca4 [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>
32#include <sys/types.h>
33#include <sys/fcntl.h>
34#include <sys/wait.h>
35#include <string.h>
36#include <sys/mount.h>
37#include <sys/reboot.h>
38#include <sys/kdaemon.h>
Eric Andersencc8ed391999-10-05 16:24:54 +000039#include <sys/sysmacros.h>
Eric Andersen8a8fbb81999-10-26 00:18:56 +000040#include <linux/serial.h> /* for serial_struct */
41#include <sys/vt.h> /* for vt_stat */
Eric Andersenabc7d591999-10-19 00:27:50 +000042#include <sys/ioctl.h>
Eric Andersencc8ed391999-10-05 16:24:54 +000043
Eric Andersen7f1acfd1999-10-29 23:09:13 +000044/* Turn this on to debug init so it won't reboot when killed */
Eric Andersen2f6c04f1999-11-01 23:59:44 +000045//#define DEBUG_INIT
Eric Andersen7f1acfd1999-10-29 23:09:13 +000046
Eric Andersen8a8fbb81999-10-26 00:18:56 +000047#define CONSOLE "/dev/console" /* Logical system console */
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#define PATH_DEFAULT "PATH=/usr/local/sbin:/sbin:/bin:/usr/sbin:/usr/bin"
56
Eric Andersenbe971d61999-11-03 16:52:50 +000057static char *console = CONSOLE;
58static char *second_console = VT_SECONDARY;
59static char *log = VT_LOG;
Eric Andersen0460ff21999-10-25 23:32:44 +000060
61
62
63/* try to open up the specified device */
Eric Andersen8a8fbb81999-10-26 00:18:56 +000064int device_open(char *device, int mode)
Eric Andersencc8ed391999-10-05 16:24:54 +000065{
Eric Andersen0460ff21999-10-25 23:32:44 +000066 int m, f, fd = -1;
Eric Andersen8a8fbb81999-10-26 00:18:56 +000067
Eric Andersen7f1acfd1999-10-29 23:09:13 +000068 m = mode | O_NONBLOCK;
Eric Andersen8a8fbb81999-10-26 00:18:56 +000069
Eric Andersen0460ff21999-10-25 23:32:44 +000070 /* Retry up to 5 times */
Eric Andersen8a8fbb81999-10-26 00:18:56 +000071 for (f = 0; f < 5; f++)
72 if ((fd = open(device, m)) >= 0)
73 break;
74 if (fd < 0)
75 return fd;
Eric Andersen0460ff21999-10-25 23:32:44 +000076 /* Set original flags. */
77 if (m != mode)
78 fcntl(fd, F_SETFL, mode);
79 return fd;
80}
Eric Andersencc8ed391999-10-05 16:24:54 +000081
Eric Andersen0460ff21999-10-25 23:32:44 +000082/* print a message to the specified device */
Eric Andersen8a8fbb81999-10-26 00:18:56 +000083void message(char *device, char *fmt, ...)
Eric Andersen0460ff21999-10-25 23:32:44 +000084{
85 int fd;
86 va_list arguments;
Eric Andersen8a8fbb81999-10-26 00:18:56 +000087
Eric Andersenc7c41d31999-10-28 00:24:35 +000088 if ((fd = device_open(device, O_WRONLY|O_NOCTTY|O_NDELAY)) >= 0) {
Eric Andersen0460ff21999-10-25 23:32:44 +000089 va_start(arguments, fmt);
90 vdprintf(fd, fmt, arguments);
Eric Andersencc8ed391999-10-05 16:24:54 +000091 va_end(arguments);
Eric Andersen8a8fbb81999-10-26 00:18:56 +000092 close(fd);
Eric Andersena7456061999-10-27 02:31:32 +000093 } else {
94 fprintf(stderr, "Bummer, can't print: ");
95 va_start(arguments, fmt);
96 vfprintf(stderr, fmt, arguments);
97 fflush(stderr);
98 va_end(arguments);
99 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000100}
101
Eric Andersen0460ff21999-10-25 23:32:44 +0000102/* Set terminal settings to reasonable defaults */
Eric Andersenc7c41d31999-10-28 00:24:35 +0000103void set_term( int fd)
Eric Andersencc8ed391999-10-05 16:24:54 +0000104{
Eric Andersen0460ff21999-10-25 23:32:44 +0000105 struct termios tty;
Eric Andersen7f1acfd1999-10-29 23:09:13 +0000106 static const char control_characters[] = {
107 '\003', '\034', '\177', '\025', '\004', '\0',
108 '\1', '\0', '\021', '\023', '\032', '\0', '\022',
109 '\017', '\027', '\026', '\0'
110 };
Eric Andersencc8ed391999-10-05 16:24:54 +0000111
Eric Andersenc7c41d31999-10-28 00:24:35 +0000112 tcgetattr(fd, &tty);
Eric Andersencc8ed391999-10-05 16:24:54 +0000113
Eric Andersen7f1acfd1999-10-29 23:09:13 +0000114 /* input modes */
115 tty.c_iflag = IGNPAR|ICRNL|IXON|IXOFF|IXANY;
Eric Andersencc8ed391999-10-05 16:24:54 +0000116
Eric Andersen219d6f51999-11-02 19:43:01 +0000117 /* use line dicipline 0 */
Eric Andersen7f1acfd1999-10-29 23:09:13 +0000118 tty.c_line = 0;
119
120 /* output modes */
Eric Andersenc7c41d31999-10-28 00:24:35 +0000121 tty.c_oflag = OPOST|ONLCR;
Eric Andersen7f1acfd1999-10-29 23:09:13 +0000122
123 /* local modes */
124 tty.c_lflag = ISIG|ICANON|ECHO|ECHOE|ECHOK|ECHOCTL|ECHOPRT|ECHOKE|IEXTEN;
125
126 /* control chars */
127 memcpy(tty.c_cc, control_characters, sizeof(control_characters));
Eric Andersencc8ed391999-10-05 16:24:54 +0000128
Eric Andersenc7c41d31999-10-28 00:24:35 +0000129 tcsetattr(fd, TCSANOW, &tty);
Eric Andersencc8ed391999-10-05 16:24:54 +0000130}
131
Eric Andersen219d6f51999-11-02 19:43:01 +0000132/* How much memory does this machine have? */
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000133static int mem_total()
Eric Andersencc8ed391999-10-05 16:24:54 +0000134{
Eric Andersenabc7d591999-10-19 00:27:50 +0000135 char s[80];
Eric Andersen219d6f51999-11-02 19:43:01 +0000136 char *p = "/proc/meminfo";
Eric Andersenabc7d591999-10-19 00:27:50 +0000137 FILE *f;
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000138 const char pattern[] = "MemTotal:";
Eric Andersencc8ed391999-10-05 16:24:54 +0000139
Eric Andersen219d6f51999-11-02 19:43:01 +0000140 if ((f = fopen(p, "r")) < 0) {
141 message(log, "Error opening %s: %s\n", p, strerror( errno));
142 return -1;
143 }
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000144 while (NULL != fgets(s, 79, f)) {
145 p = strstr(s, pattern);
Eric Andersenabc7d591999-10-19 00:27:50 +0000146 if (NULL != p) {
147 fclose(f);
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000148 return (atoi(p + strlen(pattern)));
Eric Andersenabc7d591999-10-19 00:27:50 +0000149 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000150 }
Eric Andersenabc7d591999-10-19 00:27:50 +0000151 return -1;
Eric Andersencc8ed391999-10-05 16:24:54 +0000152}
153
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000154static void console_init()
Eric Andersen0460ff21999-10-25 23:32:44 +0000155{
156 int fd;
157 int tried_devcons = 0;
Eric Andersen219d6f51999-11-02 19:43:01 +0000158 int tried_vtprimary = 0;
Eric Andersen0460ff21999-10-25 23:32:44 +0000159 char *s;
160
Eric Andersen219d6f51999-11-02 19:43:01 +0000161 if ((s = getenv("CONSOLE")) != NULL) {
Eric Andersen0460ff21999-10-25 23:32:44 +0000162 console = s;
Eric Andersen219d6f51999-11-02 19:43:01 +0000163/* Apparently the sparc does wierd things... */
164#if defined (__sparc__)
165 if (strncmp( s, "/dev/tty", 8 )==0) {
166 switch( s[8]) {
167 case 'a':
168 s=SERIAL_CON0;
169 break;
170 case 'b':
171 s=SERIAL_CON1;
172 }
173 }
174#endif
175 } else {
Eric Andersenbe971d61999-11-03 16:52:50 +0000176 console = CONSOLE;
177 tried_devcons++;
Eric Andersen0460ff21999-10-25 23:32:44 +0000178 }
Eric Andersena7456061999-10-27 02:31:32 +0000179
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000180 while ((fd = open(console, O_RDONLY | O_NONBLOCK)) < 0) {
Eric Andersen219d6f51999-11-02 19:43:01 +0000181 /* Can't open selected console -- try /dev/console */
Eric Andersen0460ff21999-10-25 23:32:44 +0000182 if (!tried_devcons) {
183 tried_devcons++;
184 console = CONSOLE;
185 continue;
186 }
Eric Andersenbe971d61999-11-03 16:52:50 +0000187 /* Can't open selected console -- try vt1 */
188 if (!tried_vtprimary) {
189 tried_vtprimary++;
190 console = VT_PRIMARY;
191 continue;
192 }
Eric Andersen0460ff21999-10-25 23:32:44 +0000193 break;
194 }
195 if (fd < 0)
Eric Andersen219d6f51999-11-02 19:43:01 +0000196 /* Perhaps we should panic here? */
Eric Andersen0460ff21999-10-25 23:32:44 +0000197 console = "/dev/null";
198 else
199 close(fd);
Eric Andersen219d6f51999-11-02 19:43:01 +0000200 message(log, "console=%s\n", console );
Eric Andersen0460ff21999-10-25 23:32:44 +0000201}
202
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000203static int waitfor(int pid)
Eric Andersen0460ff21999-10-25 23:32:44 +0000204{
205 int status, wpid;
206
207 message(log, "Waiting for process %d.\n", pid);
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000208 while ((wpid = wait(&status)) != pid) {
209 if (wpid > 0)
210 message(log, "pid %d exited, status=%x.\n", wpid, status);
Eric Andersen0460ff21999-10-25 23:32:44 +0000211 }
212 return wpid;
213}
214
Eric Andersena7456061999-10-27 02:31:32 +0000215
Eric Andersenc7c41d31999-10-28 00:24:35 +0000216static pid_t run(const char * const* command,
217 char *terminal, int get_enter)
Eric Andersen0460ff21999-10-25 23:32:44 +0000218{
Eric Andersen2f6c04f1999-11-01 23:59:44 +0000219 int fd;
Eric Andersenc7c41d31999-10-28 00:24:35 +0000220 pid_t pid;
Eric Andersen219d6f51999-11-02 19:43:01 +0000221 const char * const* cmd = command+1;
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000222 static const char press_enter[] =
Eric Andersen0460ff21999-10-25 23:32:44 +0000223 "\nPlease press Enter to activate this console. ";
224
Eric Andersen0460ff21999-10-25 23:32:44 +0000225 if ((pid = fork()) == 0) {
226 /* Clean up */
227 close(0);
228 close(1);
229 close(2);
230 setsid();
231
Eric Andersen2f6c04f1999-11-01 23:59:44 +0000232 /* Reset signal handlers set for parent process */
233 signal(SIGUSR1, SIG_DFL);
234 signal(SIGUSR2, SIG_DFL);
235 signal(SIGINT, SIG_DFL);
236 signal(SIGTERM, SIG_DFL);
237
238 if ((fd = device_open(terminal, O_RDWR)) < 0) {
239 message(log, "Bummer, can't open %s\r\n", terminal);
240 exit(-1);
241 }
242 dup(fd);
243 dup(fd);
Eric Andersenc7c41d31999-10-28 00:24:35 +0000244 tcsetpgrp(0, getpgrp());
245 set_term(0);
Eric Andersen0460ff21999-10-25 23:32:44 +0000246
Eric Andersen2f6c04f1999-11-01 23:59:44 +0000247 if (get_enter==TRUE) {
Eric Andersen0460ff21999-10-25 23:32:44 +0000248 /*
249 * Save memory by not exec-ing anything large (like a shell)
250 * before the user wants it. This is critical if swap is not
251 * enabled and the system has low memory. Generally this will
252 * be run on the second virtual console, and the first will
253 * be allowed to start a shell or whatever an init script
254 * specifies.
255 */
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000256 char c;
Eric Andersen7f1acfd1999-10-29 23:09:13 +0000257 write(1, press_enter, sizeof(press_enter) - 1);
Eric Andersen0460ff21999-10-25 23:32:44 +0000258 read(0, &c, 1);
259 }
260
261 /* Log the process name and args */
Eric Andersen219d6f51999-11-02 19:43:01 +0000262 message(log, "Starting pid(%d): '", getpid());
Eric Andersen2f6c04f1999-11-01 23:59:44 +0000263 while ( *cmd) message(log, "%s ", *cmd++);
264 message(log, "'\r\n");
265
Eric Andersenc7c41d31999-10-28 00:24:35 +0000266 /* Now run it. The new program will take over this PID,
Eric Andersena7456061999-10-27 02:31:32 +0000267 * so nothing further in init.c should be run. */
Eric Andersenc7c41d31999-10-28 00:24:35 +0000268 execvp(*command, (char**)command+1);
Eric Andersen0460ff21999-10-25 23:32:44 +0000269
Eric Andersen219d6f51999-11-02 19:43:01 +0000270 message(log, "Bummer, could not run '%s'\n", command);
Eric Andersen0460ff21999-10-25 23:32:44 +0000271 exit(-1);
272 }
273 return pid;
274}
275
Eric Andersen219d6f51999-11-02 19:43:01 +0000276/* Make sure there is enough memory to do something useful. *
277 * Calls swapon if needed so be sure /proc is mounted. */
278static void check_memory()
279{
280 struct stat statbuf;
281 const char* const swap_on_cmd[] =
282 { "/bin/swapon", "swapon", "-a", 0};
283 const char *no_memory =
284 "Sorry, your computer does not have enough memory.\r\n";
285
286 if (mem_total() > 3500)
287 return;
288
289 if (stat("/etc/fstab", &statbuf) == 0) {
290 /* Try to turn on swap */
291 waitfor(run(swap_on_cmd, log, FALSE));
292 if (mem_total() < 3500)
293 goto goodnight;
294 } else
295 goto goodnight;
296 return;
297
298goodnight:
299 message(console, "%s", no_memory);
300 while (1) sleep(1);
301}
302
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000303static void shutdown_system(void)
Eric Andersencc8ed391999-10-05 16:24:54 +0000304{
Eric Andersen2f6c04f1999-11-01 23:59:44 +0000305 const char* const swap_off_cmd[] = { "swapoff", "swapoff", "-a", 0};
306 const char* const umount_cmd[] = { "umount", "umount", "-a", "-n", 0};
Eric Andersencc8ed391999-10-05 16:24:54 +0000307
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000308 message(console, "The system is going down NOW !!\r\n");
Eric Andersen0460ff21999-10-25 23:32:44 +0000309 sync();
Eric Andersen2f6c04f1999-11-01 23:59:44 +0000310#ifndef DEBUG_INIT
Eric Andersen0460ff21999-10-25 23:32:44 +0000311 /* Allow Ctrl-Alt-Del to reboot system. */
312 reboot(RB_ENABLE_CAD);
Eric Andersen2f6c04f1999-11-01 23:59:44 +0000313#endif
Eric Andersen0460ff21999-10-25 23:32:44 +0000314 /* Send signals to every process _except_ pid 1 */
315 message(console, "Sending SIGHUP to all processes.\r\n");
Eric Andersen2f6c04f1999-11-01 23:59:44 +0000316#ifndef DEBUG_INIT
Eric Andersen0460ff21999-10-25 23:32:44 +0000317 kill(-1, SIGHUP);
Eric Andersen2f6c04f1999-11-01 23:59:44 +0000318#endif
Eric Andersen0460ff21999-10-25 23:32:44 +0000319 sleep(2);
320 sync();
321 message(console, "Sending SIGKILL to all processes.\r\n");
Eric Andersen2f6c04f1999-11-01 23:59:44 +0000322#ifndef DEBUG_INIT
Eric Andersen0460ff21999-10-25 23:32:44 +0000323 kill(-1, SIGKILL);
Eric Andersen2f6c04f1999-11-01 23:59:44 +0000324#endif
Eric Andersen0460ff21999-10-25 23:32:44 +0000325 sleep(1);
Eric Andersen2f6c04f1999-11-01 23:59:44 +0000326 waitfor(run( swap_off_cmd, log, FALSE));
327 waitfor(run( umount_cmd, log, FALSE));
Eric Andersen0460ff21999-10-25 23:32:44 +0000328 sync();
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000329 if (get_kernel_revision() <= 2 * 65536 + 2 * 256 + 11) {
Eric Andersen0460ff21999-10-25 23:32:44 +0000330 /* Removed bdflush call, kupdate in kernels >2.2.11 */
331 bdflush(1, 0);
Eric Andersencc8ed391999-10-05 16:24:54 +0000332 sync();
Eric Andersen0460ff21999-10-25 23:32:44 +0000333 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000334}
335
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000336static void halt_signal(int sig)
Eric Andersencc8ed391999-10-05 16:24:54 +0000337{
Eric Andersenabc7d591999-10-19 00:27:50 +0000338 shutdown_system();
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000339 message(console,
340 "The system is halted. Press CTRL-ALT-DEL or turn off power\r\n");
Eric Andersen2f6c04f1999-11-01 23:59:44 +0000341#ifndef DEBUG_INIT
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000342 reboot(RB_POWER_OFF);
Eric Andersen2f6c04f1999-11-01 23:59:44 +0000343#endif
Eric Andersenabc7d591999-10-19 00:27:50 +0000344 exit(0);
Eric Andersencc8ed391999-10-05 16:24:54 +0000345}
346
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000347static void reboot_signal(int sig)
Eric Andersencc8ed391999-10-05 16:24:54 +0000348{
Eric Andersenabc7d591999-10-19 00:27:50 +0000349 shutdown_system();
350 message(console, "Please stand by while rebooting the system.\r\n");
Eric Andersen2f6c04f1999-11-01 23:59:44 +0000351#ifndef DEBUG_INIT
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000352 reboot(RB_AUTOBOOT);
Eric Andersen2f6c04f1999-11-01 23:59:44 +0000353#endif
Eric Andersenabc7d591999-10-19 00:27:50 +0000354 exit(0);
Eric Andersencc8ed391999-10-05 16:24:54 +0000355}
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000356
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000357extern int init_main(int argc, char **argv)
Eric Andersen0460ff21999-10-25 23:32:44 +0000358{
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000359 int run_rc = TRUE;
Eric Andersen219d6f51999-11-02 19:43:01 +0000360 int wait_for_enter = TRUE;
Eric Andersenc7c41d31999-10-28 00:24:35 +0000361 pid_t pid1 = 0;
362 pid_t pid2 = 0;
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000363 struct stat statbuf;
Eric Andersen2f6c04f1999-11-01 23:59:44 +0000364 const char* const init_commands[] = { INITSCRIPT, INITSCRIPT, 0};
365 const char* const shell_commands[] = { SHELL, "-" SHELL, 0};
Eric Andersen219d6f51999-11-02 19:43:01 +0000366 const char* const* tty0_commands = shell_commands;
Eric Andersenc7c41d31999-10-28 00:24:35 +0000367 const char* const* tty1_commands = shell_commands;
Eric Andersen2f6c04f1999-11-01 23:59:44 +0000368#ifdef DEBUG_INIT
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000369 char *hello_msg_format =
Eric Andersen04579781999-10-29 23:12:50 +0000370 "init(%d) started: BusyBox v%s (%s) multi-call binary\r\n";
Eric Andersen2f6c04f1999-11-01 23:59:44 +0000371#else
372 char *hello_msg_format =
373 "init started: BusyBox v%s (%s) multi-call binary\r\n";
Eric Andersen7f1acfd1999-10-29 23:09:13 +0000374#endif
Eric Andersen0460ff21999-10-25 23:32:44 +0000375
Eric Andersen219d6f51999-11-02 19:43:01 +0000376
377 /* Check if we are supposed to be in single user mode */
378 if ( argc > 1 && (!strcmp(argv[1], "single") ||
379 !strcmp(argv[1], "-s") || !strcmp(argv[1], "1"))) {
380 run_rc = FALSE;
381 }
382
383
Eric Andersen2f6c04f1999-11-01 23:59:44 +0000384 /* Set up sig handlers -- be sure to
385 * clear all of these in run() */
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000386 signal(SIGUSR1, halt_signal);
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000387 signal(SIGUSR2, reboot_signal);
388 signal(SIGINT, reboot_signal);
389 signal(SIGTERM, reboot_signal);
Eric Andersen0460ff21999-10-25 23:32:44 +0000390
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000391 /* Turn off rebooting via CTL-ALT-DEL -- we get a
392 * SIGINT on CAD so we can shut things down gracefully... */
Eric Andersen2f6c04f1999-11-01 23:59:44 +0000393#ifndef DEBUG_INIT
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000394 reboot(RB_DISABLE_CAD);
Eric Andersen7f1acfd1999-10-29 23:09:13 +0000395#endif
Eric Andersen219d6f51999-11-02 19:43:01 +0000396
Eric Andersenc7c41d31999-10-28 00:24:35 +0000397 /* Figure out where the default console should be */
398 console_init();
Eric Andersen0460ff21999-10-25 23:32:44 +0000399
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000400 /* Close whatever files are open, and reset the console. */
401 close(0);
402 close(1);
403 close(2);
Eric Andersen7f1acfd1999-10-29 23:09:13 +0000404 set_term(0);
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000405 setsid();
Eric Andersencc8ed391999-10-05 16:24:54 +0000406
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000407 /* Make sure PATH is set to something sane */
Eric Andersen219d6f51999-11-02 19:43:01 +0000408 putenv(PATH_DEFAULT);
Eric Andersencc8ed391999-10-05 16:24:54 +0000409
Eric Andersen219d6f51999-11-02 19:43:01 +0000410
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000411 /* Hello world */
Eric Andersen7f1acfd1999-10-29 23:09:13 +0000412#ifndef DEBUG_INIT
Eric Andersenc1525e81999-10-29 00:07:31 +0000413 message(log, hello_msg_format, BB_VER, BB_BT);
Eric Andersen7f1acfd1999-10-29 23:09:13 +0000414 message(console, hello_msg_format, BB_VER, BB_BT);
415#else
416 message(log, hello_msg_format, getpid(), BB_VER, BB_BT);
417 message(console, hello_msg_format, getpid(), BB_VER, BB_BT);
418#endif
Eric Andersencc8ed391999-10-05 16:24:54 +0000419
Eric Andersenc7c41d31999-10-28 00:24:35 +0000420
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000421 /* Mount /proc */
Eric Andersen2f6c04f1999-11-01 23:59:44 +0000422 if (mount ("proc", "/proc", "proc", 0, 0) == 0) {
Eric Andersena7456061999-10-27 02:31:32 +0000423 message(log, "Mounting /proc: done.\n");
Eric Andersen7f1acfd1999-10-29 23:09:13 +0000424 message(console, "Mounting /proc: done.\n");
Eric Andersenc7c41d31999-10-28 00:24:35 +0000425 } else {
Eric Andersenc7c41d31999-10-28 00:24:35 +0000426 message(log, "Mounting /proc: failed!\n");
Eric Andersen7f1acfd1999-10-29 23:09:13 +0000427 message(console, "Mounting /proc: failed!\n");
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000428 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000429
Eric Andersen219d6f51999-11-02 19:43:01 +0000430 /* Make sure there is enough memory to do something useful. */
431 check_memory();
Eric Andersencc8ed391999-10-05 16:24:54 +0000432
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000433
434 /* Make sure an init script exists before trying to run it */
Eric Andersen219d6f51999-11-02 19:43:01 +0000435 if (run_rc == TRUE && stat(INITSCRIPT, &statbuf)==0) {
436 wait_for_enter = FALSE;
437 tty0_commands = init_commands;
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000438 }
439
Eric Andersen219d6f51999-11-02 19:43:01 +0000440
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000441 /* Ok, now launch the rc script and/or prepare to
442 * start up some VTs if somebody hits enter...
443 */
444 for (;;) {
Eric Andersenc7c41d31999-10-28 00:24:35 +0000445 pid_t wpid;
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000446 int status;
447
448 if (pid1 == 0 && tty0_commands) {
Eric Andersen2f6c04f1999-11-01 23:59:44 +0000449 pid1 = run(tty0_commands, console, wait_for_enter);
Eric Andersencc8ed391999-10-05 16:24:54 +0000450 }
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000451 if (pid2 == 0 && tty1_commands) {
Eric Andersenbe971d61999-11-03 16:52:50 +0000452 pid2 = run(tty1_commands, second_console, TRUE);
Eric Andersencc8ed391999-10-05 16:24:54 +0000453 }
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000454 wpid = wait(&status);
Eric Andersena7456061999-10-27 02:31:32 +0000455 if (wpid > 0 ) {
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000456 message(log, "pid %d exited, status=%x.\n", wpid, status);
457 }
Eric Andersen219d6f51999-11-02 19:43:01 +0000458 if (wpid == pid1) {
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000459 pid1 = 0;
Eric Andersen219d6f51999-11-02 19:43:01 +0000460 if (run_rc == TRUE) {
461 /* Don't respawn init script if it exits. */
462 run_rc=FALSE;
463 wait_for_enter=TRUE;
464 tty0_commands=shell_commands;
465 }
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000466 }
467 if (wpid == pid2) {
468 pid2 = 0;
469 }
470 sleep(1);
471 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000472}