blob: a56e23e5d9183d47fe3da6a2fe09e9387783e586 [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 */
45#define DEBUG_INIT
46
Eric Andersen8a8fbb81999-10-26 00:18:56 +000047#define CONSOLE "/dev/console" /* Logical system console */
48#define VT_PRIMARY "/dev/tty0" /* Virtual console master */
49#define VT_SECONDARY "/dev/tty1" /* Virtual console master */
50#define VT_LOG "/dev/tty2" /* Virtual console master */
51#define SHELL "/bin/sh" /* Default shell */
52#define INITSCRIPT "/etc/init.d/rcS" /* Initscript. */
Eric Andersen0460ff21999-10-25 23:32:44 +000053#define PATH_DEFAULT "PATH=/usr/local/sbin:/sbin:/bin:/usr/sbin:/usr/bin"
54
Eric Andersen8a8fbb81999-10-26 00:18:56 +000055static char *console = CONSOLE;
Eric Andersenc7c41d31999-10-28 00:24:35 +000056//static char *first_terminal = "/dev/tty1";
Eric Andersen8a8fbb81999-10-26 00:18:56 +000057static char *second_terminal = "/dev/tty2";
58static char *log = "/dev/tty3";
Eric Andersen0460ff21999-10-25 23:32:44 +000059
60
61
62/* try to open up the specified device */
Eric Andersen8a8fbb81999-10-26 00:18:56 +000063int device_open(char *device, int mode)
Eric Andersencc8ed391999-10-05 16:24:54 +000064{
Eric Andersen0460ff21999-10-25 23:32:44 +000065 int m, f, fd = -1;
Eric Andersen8a8fbb81999-10-26 00:18:56 +000066
Eric Andersen7f1acfd1999-10-29 23:09:13 +000067 m = mode | O_NONBLOCK;
Eric Andersen8a8fbb81999-10-26 00:18:56 +000068
Eric Andersen0460ff21999-10-25 23:32:44 +000069 /* Retry up to 5 times */
Eric Andersen8a8fbb81999-10-26 00:18:56 +000070 for (f = 0; f < 5; f++)
71 if ((fd = open(device, m)) >= 0)
72 break;
73 if (fd < 0)
74 return fd;
Eric Andersen0460ff21999-10-25 23:32:44 +000075 /* Set original flags. */
76 if (m != mode)
77 fcntl(fd, F_SETFL, mode);
78 return fd;
79}
Eric Andersencc8ed391999-10-05 16:24:54 +000080
Eric Andersen0460ff21999-10-25 23:32:44 +000081/* print a message to the specified device */
Eric Andersen8a8fbb81999-10-26 00:18:56 +000082void message(char *device, char *fmt, ...)
Eric Andersen0460ff21999-10-25 23:32:44 +000083{
84 int fd;
85 va_list arguments;
Eric Andersen8a8fbb81999-10-26 00:18:56 +000086
Eric Andersenc7c41d31999-10-28 00:24:35 +000087 if ((fd = device_open(device, O_WRONLY|O_NOCTTY|O_NDELAY)) >= 0) {
Eric Andersen0460ff21999-10-25 23:32:44 +000088 va_start(arguments, fmt);
89 vdprintf(fd, fmt, arguments);
Eric Andersencc8ed391999-10-05 16:24:54 +000090 va_end(arguments);
Eric Andersen8a8fbb81999-10-26 00:18:56 +000091 close(fd);
Eric Andersena7456061999-10-27 02:31:32 +000092 } else {
93 fprintf(stderr, "Bummer, can't print: ");
94 va_start(arguments, fmt);
95 vfprintf(stderr, fmt, arguments);
96 fflush(stderr);
97 va_end(arguments);
98 }
Eric Andersencc8ed391999-10-05 16:24:54 +000099}
100
Eric Andersen0460ff21999-10-25 23:32:44 +0000101/* Set terminal settings to reasonable defaults */
Eric Andersenc7c41d31999-10-28 00:24:35 +0000102void set_term( int fd)
Eric Andersencc8ed391999-10-05 16:24:54 +0000103{
Eric Andersen0460ff21999-10-25 23:32:44 +0000104 struct termios tty;
Eric Andersen7f1acfd1999-10-29 23:09:13 +0000105 static const char control_characters[] = {
106 '\003', '\034', '\177', '\025', '\004', '\0',
107 '\1', '\0', '\021', '\023', '\032', '\0', '\022',
108 '\017', '\027', '\026', '\0'
109 };
Eric Andersencc8ed391999-10-05 16:24:54 +0000110
Eric Andersenc7c41d31999-10-28 00:24:35 +0000111 tcgetattr(fd, &tty);
Eric Andersencc8ed391999-10-05 16:24:54 +0000112
Eric Andersen7f1acfd1999-10-29 23:09:13 +0000113 /* input modes */
114 tty.c_iflag = IGNPAR|ICRNL|IXON|IXOFF|IXANY;
Eric Andersencc8ed391999-10-05 16:24:54 +0000115
Eric Andersen7f1acfd1999-10-29 23:09:13 +0000116 /* use lineo dicipline 0 */
117 tty.c_line = 0;
118
119 /* output modes */
Eric Andersenc7c41d31999-10-28 00:24:35 +0000120 tty.c_oflag = OPOST|ONLCR;
Eric Andersen7f1acfd1999-10-29 23:09:13 +0000121
122 /* local modes */
123 tty.c_lflag = ISIG|ICANON|ECHO|ECHOE|ECHOK|ECHOCTL|ECHOPRT|ECHOKE|IEXTEN;
124
125 /* control chars */
126 memcpy(tty.c_cc, control_characters, sizeof(control_characters));
Eric Andersencc8ed391999-10-05 16:24:54 +0000127
Eric Andersenc7c41d31999-10-28 00:24:35 +0000128 tcsetattr(fd, TCSANOW, &tty);
Eric Andersencc8ed391999-10-05 16:24:54 +0000129}
130
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000131static int mem_total()
Eric Andersencc8ed391999-10-05 16:24:54 +0000132{
Eric Andersenabc7d591999-10-19 00:27:50 +0000133 char s[80];
134 char *p;
135 FILE *f;
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000136 const char pattern[] = "MemTotal:";
Eric Andersencc8ed391999-10-05 16:24:54 +0000137
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000138 f = fopen("/proc/meminfo", "r");
139 while (NULL != fgets(s, 79, f)) {
140 p = strstr(s, pattern);
Eric Andersenabc7d591999-10-19 00:27:50 +0000141 if (NULL != p) {
142 fclose(f);
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000143 return (atoi(p + strlen(pattern)));
Eric Andersenabc7d591999-10-19 00:27:50 +0000144 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000145 }
Eric Andersenabc7d591999-10-19 00:27:50 +0000146 return -1;
Eric Andersencc8ed391999-10-05 16:24:54 +0000147}
148
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000149static void set_free_pages()
Eric Andersencc8ed391999-10-05 16:24:54 +0000150{
Eric Andersenabc7d591999-10-19 00:27:50 +0000151 char s[80];
152 FILE *f;
Eric Andersencc8ed391999-10-05 16:24:54 +0000153
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000154 f = fopen("/proc/sys/vm/freepages", "r");
155 fgets(s, 79, f);
Eric Andersenabc7d591999-10-19 00:27:50 +0000156 if (atoi(s) < 32) {
157 fclose(f);
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000158 f = fopen("/proc/sys/vm/freepages", "w");
159 fprintf(f, "30\t40\t50\n");
Eric Andersen7f1acfd1999-10-29 23:09:13 +0000160 message(log, "\nIncreased /proc/sys/vm/freepages values to 30/40/50\n");
Eric Andersenabc7d591999-10-19 00:27:50 +0000161 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000162 fclose(f);
Eric Andersencc8ed391999-10-05 16:24:54 +0000163}
164
Eric Andersen0460ff21999-10-25 23:32:44 +0000165
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000166static void console_init()
Eric Andersen0460ff21999-10-25 23:32:44 +0000167{
168 int fd;
Eric Andersena7456061999-10-27 02:31:32 +0000169 struct stat statbuf;
Eric Andersen0460ff21999-10-25 23:32:44 +0000170 int tried_devcons = 0;
171 int tried_vtmaster = 0;
172 char *s;
173
174 if ((s = getenv("CONSOLE")) != NULL)
175 console = s;
176 else {
177 console = CONSOLE;
178 tried_devcons++;
179 }
Eric Andersena7456061999-10-27 02:31:32 +0000180
181 if ( stat(CONSOLE, &statbuf) && S_ISLNK(statbuf.st_mode)) {
Eric Andersenc7c41d31999-10-28 00:24:35 +0000182 fprintf(stderr, "Yikes! /dev/console does not exist or is a symlink.\n");
Eric Andersen7f1acfd1999-10-29 23:09:13 +0000183 message(log, "Yikes! /dev/console does not exist or is a symlink.\n");
Eric Andersena7456061999-10-27 02:31:32 +0000184 }
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000185 while ((fd = open(console, O_RDONLY | O_NONBLOCK)) < 0) {
Eric Andersen0460ff21999-10-25 23:32:44 +0000186 if (!tried_devcons) {
187 tried_devcons++;
188 console = CONSOLE;
189 continue;
190 }
191 if (!tried_vtmaster) {
192 tried_vtmaster++;
193 console = VT_PRIMARY;
194 continue;
195 }
196 break;
197 }
198 if (fd < 0)
199 console = "/dev/null";
200 else
201 close(fd);
Eric Andersen7f1acfd1999-10-29 23:09:13 +0000202 message(log, "console=%s\n", console);
Eric Andersen0460ff21999-10-25 23:32:44 +0000203}
204
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000205static int waitfor(int pid)
Eric Andersen0460ff21999-10-25 23:32:44 +0000206{
207 int status, wpid;
208
209 message(log, "Waiting for process %d.\n", pid);
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000210 while ((wpid = wait(&status)) != pid) {
211 if (wpid > 0)
212 message(log, "pid %d exited, status=%x.\n", wpid, status);
Eric Andersen0460ff21999-10-25 23:32:44 +0000213 }
214 return wpid;
215}
216
Eric Andersena7456061999-10-27 02:31:32 +0000217
Eric Andersenc7c41d31999-10-28 00:24:35 +0000218static pid_t run(const char * const* command,
219 char *terminal, int get_enter)
Eric Andersen0460ff21999-10-25 23:32:44 +0000220{
Eric Andersen7f1acfd1999-10-29 23:09:13 +0000221 int i, f;
Eric Andersenc7c41d31999-10-28 00:24:35 +0000222 pid_t pid;
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000223 static const char press_enter[] =
Eric Andersen0460ff21999-10-25 23:32:44 +0000224 "\nPlease press Enter to activate this console. ";
225
Eric Andersen0460ff21999-10-25 23:32:44 +0000226 if ((pid = fork()) == 0) {
227 /* Clean up */
228 close(0);
229 close(1);
230 close(2);
231 setsid();
232
Eric Andersen7f1acfd1999-10-29 23:09:13 +0000233 f=open(terminal, O_RDWR);
234 dup(f);
235 dup(f);
Eric Andersenc7c41d31999-10-28 00:24:35 +0000236 tcsetpgrp(0, getpgrp());
237 set_term(0);
Eric Andersen0460ff21999-10-25 23:32:44 +0000238
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000239 if (get_enter) {
Eric Andersen0460ff21999-10-25 23:32:44 +0000240 /*
241 * Save memory by not exec-ing anything large (like a shell)
242 * before the user wants it. This is critical if swap is not
243 * enabled and the system has low memory. Generally this will
244 * be run on the second virtual console, and the first will
245 * be allowed to start a shell or whatever an init script
246 * specifies.
247 */
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000248 char c;
Eric Andersen7f1acfd1999-10-29 23:09:13 +0000249 write(1, press_enter, sizeof(press_enter) - 1);
Eric Andersen0460ff21999-10-25 23:32:44 +0000250 read(0, &c, 1);
251 }
252
253 /* Log the process name and args */
Eric Andersen0460ff21999-10-25 23:32:44 +0000254
Eric Andersenc7c41d31999-10-28 00:24:35 +0000255 /* Now run it. The new program will take over this PID,
Eric Andersena7456061999-10-27 02:31:32 +0000256 * so nothing further in init.c should be run. */
Eric Andersenc7c41d31999-10-28 00:24:35 +0000257 message(log, "Executing '%s', pid(%d)\r\n", *command, getpid());
258 execvp(*command, (char**)command+1);
Eric Andersen0460ff21999-10-25 23:32:44 +0000259
Eric Andersenc7c41d31999-10-28 00:24:35 +0000260 message(log, "Hmm. Trying as a script.\r\n");
Eric Andersen0460ff21999-10-25 23:32:44 +0000261 /* If shell scripts are not executed, force the issue */
262 if (errno == ENOEXEC) {
Eric Andersenc7c41d31999-10-28 00:24:35 +0000263 char * args[16];
264 args[0] = SHELL;
265 args[1] = "-c";
266 args[2] = "exec";
267 for( i=0 ; i<16 && command[i]; i++)
268 args[3+i] = (char*)command[i];
269 args[i] = NULL;
270 execvp(*args, (char**)args+1);
Eric Andersen0460ff21999-10-25 23:32:44 +0000271 }
Eric Andersenc7c41d31999-10-28 00:24:35 +0000272 message(log, "Could not execute '%s'\n", command);
Eric Andersen0460ff21999-10-25 23:32:44 +0000273 exit(-1);
274 }
275 return pid;
276}
277
Eric Andersen7f1acfd1999-10-29 23:09:13 +0000278#ifndef DEBUG_INIT
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000279static void shutdown_system(void)
Eric Andersencc8ed391999-10-05 16:24:54 +0000280{
Eric Andersenc7c41d31999-10-28 00:24:35 +0000281 const char* const swap_off_cmd[] = { "/bin/swapoff", "-a", 0};
282 const char* const umount_cmd[] = { "/bin/umount", "-a", "-n", 0};
Eric Andersencc8ed391999-10-05 16:24:54 +0000283
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000284 message(console, "The system is going down NOW !!\r\n");
Eric Andersen0460ff21999-10-25 23:32:44 +0000285 sync();
286 /* Allow Ctrl-Alt-Del to reboot system. */
287 reboot(RB_ENABLE_CAD);
Eric Andersencc8ed391999-10-05 16:24:54 +0000288
Eric Andersen0460ff21999-10-25 23:32:44 +0000289 /* Send signals to every process _except_ pid 1 */
290 message(console, "Sending SIGHUP to all processes.\r\n");
291 kill(-1, SIGHUP);
292 sleep(2);
293 sync();
294 message(console, "Sending SIGKILL to all processes.\r\n");
295 kill(-1, SIGKILL);
296 sleep(1);
Eric Andersenc7c41d31999-10-28 00:24:35 +0000297 waitfor(run( swap_off_cmd, console, 0));
298 waitfor(run( umount_cmd, console, 0));
Eric Andersen0460ff21999-10-25 23:32:44 +0000299 sync();
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000300 if (get_kernel_revision() <= 2 * 65536 + 2 * 256 + 11) {
Eric Andersen0460ff21999-10-25 23:32:44 +0000301 /* Removed bdflush call, kupdate in kernels >2.2.11 */
302 bdflush(1, 0);
Eric Andersencc8ed391999-10-05 16:24:54 +0000303 sync();
Eric Andersen0460ff21999-10-25 23:32:44 +0000304 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000305}
306
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000307static void halt_signal(int sig)
Eric Andersencc8ed391999-10-05 16:24:54 +0000308{
Eric Andersenabc7d591999-10-19 00:27:50 +0000309 shutdown_system();
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000310 message(console,
311 "The system is halted. Press CTRL-ALT-DEL or turn off power\r\n");
312 reboot(RB_POWER_OFF);
Eric Andersenabc7d591999-10-19 00:27:50 +0000313 exit(0);
Eric Andersencc8ed391999-10-05 16:24:54 +0000314}
315
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000316static void reboot_signal(int sig)
Eric Andersencc8ed391999-10-05 16:24:54 +0000317{
Eric Andersenabc7d591999-10-19 00:27:50 +0000318 shutdown_system();
319 message(console, "Please stand by while rebooting the system.\r\n");
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000320 reboot(RB_AUTOBOOT);
Eric Andersenabc7d591999-10-19 00:27:50 +0000321 exit(0);
Eric Andersencc8ed391999-10-05 16:24:54 +0000322}
Eric Andersen7f1acfd1999-10-29 23:09:13 +0000323#endif
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000324
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000325extern int init_main(int argc, char **argv)
Eric Andersen0460ff21999-10-25 23:32:44 +0000326{
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000327 int run_rc = TRUE;
Eric Andersenc7c41d31999-10-28 00:24:35 +0000328 pid_t pid1 = 0;
329 pid_t pid2 = 0;
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000330 struct stat statbuf;
Eric Andersenc7c41d31999-10-28 00:24:35 +0000331 const char* const swap_on_cmd[] = { "/bin/swapon", " -a ", 0};
332 const char* const init_commands[] = { SHELL, " -c", " exec ", INITSCRIPT, 0};
333 const char* const shell_commands[] = { SHELL, " -", 0};
334 const char* const* tty0_commands = init_commands;
335 const char* const* tty1_commands = shell_commands;
Eric Andersen7f1acfd1999-10-29 23:09:13 +0000336#ifndef DEBUG_INIT
337 char *hello_msg_format =
338 "init(%d) started: BusyBox v%s (%s) multi-call binary\r\n";
339#else
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000340 char *hello_msg_format =
Eric Andersenc1525e81999-10-29 00:07:31 +0000341 "init started: BusyBox v%s (%s) multi-call binary\r\n";
Eric Andersen7f1acfd1999-10-29 23:09:13 +0000342#endif
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000343 const char *no_memory =
344 "Sorry, your computer does not have enough memory.\r\n";
Eric Andersen0460ff21999-10-25 23:32:44 +0000345
Eric Andersen7f1acfd1999-10-29 23:09:13 +0000346#ifndef DEBUG_INIT
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000347 /* Set up sig handlers */
348 signal(SIGUSR1, halt_signal);
349 signal(SIGSEGV, halt_signal);
350 signal(SIGPWR, halt_signal);
351 signal(SIGALRM, halt_signal);
352 signal(SIGHUP, halt_signal);
353 signal(SIGUSR2, reboot_signal);
354 signal(SIGINT, reboot_signal);
355 signal(SIGTERM, reboot_signal);
Eric Andersen0460ff21999-10-25 23:32:44 +0000356
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000357 /* Turn off rebooting via CTL-ALT-DEL -- we get a
358 * SIGINT on CAD so we can shut things down gracefully... */
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000359 reboot(RB_DISABLE_CAD);
Eric Andersen7f1acfd1999-10-29 23:09:13 +0000360#endif
Eric Andersenc7c41d31999-10-28 00:24:35 +0000361 /* Figure out where the default console should be */
362 console_init();
Eric Andersen0460ff21999-10-25 23:32:44 +0000363
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000364 /* Close whatever files are open, and reset the console. */
365 close(0);
366 close(1);
367 close(2);
Eric Andersen7f1acfd1999-10-29 23:09:13 +0000368 set_term(0);
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000369 setsid();
Eric Andersencc8ed391999-10-05 16:24:54 +0000370
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000371 /* Make sure PATH is set to something sane */
372 if (getenv("PATH") == NULL)
373 putenv(PATH_DEFAULT);
Eric Andersencc8ed391999-10-05 16:24:54 +0000374
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000375 /* Hello world */
Eric Andersen7f1acfd1999-10-29 23:09:13 +0000376#ifndef DEBUG_INIT
Eric Andersenc1525e81999-10-29 00:07:31 +0000377 message(log, hello_msg_format, BB_VER, BB_BT);
Eric Andersen7f1acfd1999-10-29 23:09:13 +0000378 message(console, hello_msg_format, BB_VER, BB_BT);
379#else
380 message(log, hello_msg_format, getpid(), BB_VER, BB_BT);
381 message(console, hello_msg_format, getpid(), BB_VER, BB_BT);
382#endif
Eric Andersencc8ed391999-10-05 16:24:54 +0000383
Eric Andersenc7c41d31999-10-28 00:24:35 +0000384
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000385 /* Mount /proc */
Eric Andersenc7c41d31999-10-28 00:24:35 +0000386 if (mount("/proc", "/proc", "proc", 0, 0) == 0) {
Eric Andersena7456061999-10-27 02:31:32 +0000387 message(log, "Mounting /proc: done.\n");
Eric Andersen7f1acfd1999-10-29 23:09:13 +0000388 message(console, "Mounting /proc: done.\n");
Eric Andersenc7c41d31999-10-28 00:24:35 +0000389 } else {
Eric Andersenc7c41d31999-10-28 00:24:35 +0000390 message(log, "Mounting /proc: failed!\n");
Eric Andersen7f1acfd1999-10-29 23:09:13 +0000391 message(console, "Mounting /proc: failed!\n");
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000392 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000393
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000394 /* Make sure there is enough memory to do something useful */
395 set_free_pages();
Eric Andersenc1525e81999-10-29 00:07:31 +0000396 if (mem_total() < 3500) {
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000397 int retval;
398 retval = stat("/etc/fstab", &statbuf);
399 if (retval) {
Eric Andersen7f1acfd1999-10-29 23:09:13 +0000400 message(console, "%s", no_memory);
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000401 while (1) {
402 sleep(1);
403 }
404 } else {
405 /* Try to turn on swap */
Eric Andersenc7c41d31999-10-28 00:24:35 +0000406 waitfor(run(swap_on_cmd, console, 0));
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000407 if (mem_total() < 2000) {
Eric Andersen7f1acfd1999-10-29 23:09:13 +0000408 message(console, "%s", no_memory);
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000409 while (1) {
410 sleep(1);
Eric Andersenabc7d591999-10-19 00:27:50 +0000411 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000412 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000413 }
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000414 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000415
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000416 /* Check if we are supposed to be in single user mode */
417 if ( argc > 1 && (!strcmp(argv[1], "single") ||
418 !strcmp(argv[1], "-s") || !strcmp(argv[1], "1"))) {
419 run_rc = FALSE;
420 tty0_commands = shell_commands;
Eric Andersena7456061999-10-27 02:31:32 +0000421 tty1_commands = shell_commands;
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000422 }
423
424 /* Make sure an init script exists before trying to run it */
425 if (run_rc == TRUE && stat(INITSCRIPT, &statbuf)) {
426 tty0_commands = shell_commands;
427 tty1_commands = shell_commands;
428 }
429
430 /* Ok, now launch the rc script and/or prepare to
431 * start up some VTs if somebody hits enter...
432 */
433 for (;;) {
Eric Andersenc7c41d31999-10-28 00:24:35 +0000434 pid_t wpid;
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000435 int status;
436
437 if (pid1 == 0 && tty0_commands) {
Eric Andersenc7c41d31999-10-28 00:24:35 +0000438 pid1 = run(tty0_commands, console, 1);
Eric Andersencc8ed391999-10-05 16:24:54 +0000439 }
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000440 if (pid2 == 0 && tty1_commands) {
441 pid2 = run(tty1_commands, second_terminal, 1);
Eric Andersencc8ed391999-10-05 16:24:54 +0000442 }
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000443 wpid = wait(&status);
Eric Andersena7456061999-10-27 02:31:32 +0000444 if (wpid > 0 ) {
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000445 message(log, "pid %d exited, status=%x.\n", wpid, status);
446 }
447 /* Don't respawn an init script if it exits */
448 if (run_rc == FALSE && wpid == pid1) {
449 pid1 = 0;
450 }
451 if (wpid == pid2) {
452 pid2 = 0;
453 }
454 sleep(1);
455 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000456}