blob: b8cbbf64e597ea00a1165fc5569bcdd9cd0b6915 [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 Andersen8a8fbb81999-10-26 00:18:56 +000044#define CONSOLE "/dev/console" /* Logical system console */
45#define VT_PRIMARY "/dev/tty0" /* Virtual console master */
46#define VT_SECONDARY "/dev/tty1" /* Virtual console master */
47#define VT_LOG "/dev/tty2" /* Virtual console master */
48#define SHELL "/bin/sh" /* Default shell */
49#define INITSCRIPT "/etc/init.d/rcS" /* Initscript. */
Eric Andersen0460ff21999-10-25 23:32:44 +000050#define PATH_DEFAULT "PATH=/usr/local/sbin:/sbin:/bin:/usr/sbin:/usr/bin"
51
Eric Andersen8a8fbb81999-10-26 00:18:56 +000052static char *console = CONSOLE;
Eric Andersenc7c41d31999-10-28 00:24:35 +000053//static char *first_terminal = "/dev/tty1";
Eric Andersen8a8fbb81999-10-26 00:18:56 +000054static char *second_terminal = "/dev/tty2";
55static char *log = "/dev/tty3";
Eric Andersen0460ff21999-10-25 23:32:44 +000056
57
58
59/* try to open up the specified device */
Eric Andersen8a8fbb81999-10-26 00:18:56 +000060int device_open(char *device, int mode)
Eric Andersencc8ed391999-10-05 16:24:54 +000061{
Eric Andersen0460ff21999-10-25 23:32:44 +000062 int m, f, fd = -1;
Eric Andersen8a8fbb81999-10-26 00:18:56 +000063
Eric Andersen0460ff21999-10-25 23:32:44 +000064 mode = m | O_NONBLOCK;
Eric Andersen8a8fbb81999-10-26 00:18:56 +000065
Eric Andersen0460ff21999-10-25 23:32:44 +000066 /* Retry up to 5 times */
Eric Andersen8a8fbb81999-10-26 00:18:56 +000067 for (f = 0; f < 5; f++)
68 if ((fd = open(device, m)) >= 0)
69 break;
70 if (fd < 0)
71 return fd;
Eric Andersen0460ff21999-10-25 23:32:44 +000072 /* Set original flags. */
73 if (m != mode)
74 fcntl(fd, F_SETFL, mode);
75 return fd;
76}
Eric Andersencc8ed391999-10-05 16:24:54 +000077
Eric Andersen0460ff21999-10-25 23:32:44 +000078/* print a message to the specified device */
Eric Andersen8a8fbb81999-10-26 00:18:56 +000079void message(char *device, char *fmt, ...)
Eric Andersen0460ff21999-10-25 23:32:44 +000080{
81 int fd;
82 va_list arguments;
Eric Andersen8a8fbb81999-10-26 00:18:56 +000083
Eric Andersenc7c41d31999-10-28 00:24:35 +000084 if ((fd = device_open(device, O_WRONLY|O_NOCTTY|O_NDELAY)) >= 0) {
Eric Andersen0460ff21999-10-25 23:32:44 +000085 va_start(arguments, fmt);
86 vdprintf(fd, fmt, arguments);
Eric Andersencc8ed391999-10-05 16:24:54 +000087 va_end(arguments);
Eric Andersen8a8fbb81999-10-26 00:18:56 +000088 close(fd);
Eric Andersena7456061999-10-27 02:31:32 +000089 } else {
90 fprintf(stderr, "Bummer, can't print: ");
91 va_start(arguments, fmt);
92 vfprintf(stderr, fmt, arguments);
93 fflush(stderr);
94 va_end(arguments);
95 }
Eric Andersencc8ed391999-10-05 16:24:54 +000096}
97
Eric Andersen0460ff21999-10-25 23:32:44 +000098/* Set terminal settings to reasonable defaults */
Eric Andersenc7c41d31999-10-28 00:24:35 +000099void set_term( int fd)
Eric Andersencc8ed391999-10-05 16:24:54 +0000100{
Eric Andersen0460ff21999-10-25 23:32:44 +0000101 struct termios tty;
Eric Andersencc8ed391999-10-05 16:24:54 +0000102
Eric Andersenc7c41d31999-10-28 00:24:35 +0000103 tcgetattr(fd, &tty);
104 tty.c_cflag &= CBAUD|CBAUDEX|CSIZE|CSTOPB|PARENB|PARODD;
105 tty.c_cflag |= HUPCL|CLOCAL;
Eric Andersencc8ed391999-10-05 16:24:54 +0000106
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000107 tty.c_cc[VINTR] = 3;
108 tty.c_cc[VQUIT] = 28;
Eric Andersen0460ff21999-10-25 23:32:44 +0000109 tty.c_cc[VERASE] = 127;
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000110 tty.c_cc[VKILL] = 24;
111 tty.c_cc[VEOF] = 4;
112 tty.c_cc[VTIME] = 0;
113 tty.c_cc[VMIN] = 1;
Eric Andersen0460ff21999-10-25 23:32:44 +0000114 tty.c_cc[VSTART] = 17;
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000115 tty.c_cc[VSTOP] = 19;
116 tty.c_cc[VSUSP] = 26;
Eric Andersencc8ed391999-10-05 16:24:54 +0000117
Eric Andersenc7c41d31999-10-28 00:24:35 +0000118 tty.c_iflag = IGNPAR|ICRNL|IXON|IXANY;
119 tty.c_oflag = OPOST|ONLCR;
120 tty.c_lflag = ISIG|ICANON|ECHO|ECHOCTL|ECHOPRT|ECHOKE;
Eric Andersencc8ed391999-10-05 16:24:54 +0000121
Eric Andersenc7c41d31999-10-28 00:24:35 +0000122 tcsetattr(fd, TCSANOW, &tty);
Eric Andersencc8ed391999-10-05 16:24:54 +0000123}
124
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000125static int mem_total()
Eric Andersencc8ed391999-10-05 16:24:54 +0000126{
Eric Andersenabc7d591999-10-19 00:27:50 +0000127 char s[80];
128 char *p;
129 FILE *f;
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000130 const char pattern[] = "MemTotal:";
Eric Andersencc8ed391999-10-05 16:24:54 +0000131
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000132 f = fopen("/proc/meminfo", "r");
133 while (NULL != fgets(s, 79, f)) {
134 p = strstr(s, pattern);
Eric Andersenabc7d591999-10-19 00:27:50 +0000135 if (NULL != p) {
136 fclose(f);
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000137 return (atoi(p + strlen(pattern)));
Eric Andersenabc7d591999-10-19 00:27:50 +0000138 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000139 }
Eric Andersenabc7d591999-10-19 00:27:50 +0000140 return -1;
Eric Andersencc8ed391999-10-05 16:24:54 +0000141}
142
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000143static void set_free_pages()
Eric Andersencc8ed391999-10-05 16:24:54 +0000144{
Eric Andersenabc7d591999-10-19 00:27:50 +0000145 char s[80];
146 FILE *f;
Eric Andersencc8ed391999-10-05 16:24:54 +0000147
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000148 f = fopen("/proc/sys/vm/freepages", "r");
149 fgets(s, 79, f);
Eric Andersenabc7d591999-10-19 00:27:50 +0000150 if (atoi(s) < 32) {
151 fclose(f);
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000152 f = fopen("/proc/sys/vm/freepages", "w");
153 fprintf(f, "30\t40\t50\n");
Eric Andersenabc7d591999-10-19 00:27:50 +0000154 printf("\nIncreased /proc/sys/vm/freepages values to 30/40/50\n");
155 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000156 fclose(f);
Eric Andersencc8ed391999-10-05 16:24:54 +0000157}
158
Eric Andersen0460ff21999-10-25 23:32:44 +0000159
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000160static void console_init()
Eric Andersen0460ff21999-10-25 23:32:44 +0000161{
162 int fd;
Eric Andersena7456061999-10-27 02:31:32 +0000163 struct stat statbuf;
Eric Andersen0460ff21999-10-25 23:32:44 +0000164 int tried_devcons = 0;
165 int tried_vtmaster = 0;
166 char *s;
167
Eric Andersenc7c41d31999-10-28 00:24:35 +0000168 fprintf(stderr, "entering console_init()\n");
Eric Andersen0460ff21999-10-25 23:32:44 +0000169 if ((s = getenv("CONSOLE")) != NULL)
170 console = s;
171 else {
172 console = CONSOLE;
173 tried_devcons++;
174 }
Eric Andersena7456061999-10-27 02:31:32 +0000175
176 if ( stat(CONSOLE, &statbuf) && S_ISLNK(statbuf.st_mode)) {
Eric Andersenc7c41d31999-10-28 00:24:35 +0000177 fprintf(stderr, "Yikes! /dev/console does not exist or is a symlink.\n");
Eric Andersena7456061999-10-27 02:31:32 +0000178 }
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000179 while ((fd = open(console, O_RDONLY | O_NONBLOCK)) < 0) {
Eric Andersen0460ff21999-10-25 23:32:44 +0000180 if (!tried_devcons) {
181 tried_devcons++;
182 console = CONSOLE;
183 continue;
184 }
185 if (!tried_vtmaster) {
186 tried_vtmaster++;
187 console = VT_PRIMARY;
188 continue;
189 }
190 break;
191 }
192 if (fd < 0)
193 console = "/dev/null";
194 else
195 close(fd);
Eric Andersena7456061999-10-27 02:31:32 +0000196 fprintf(stderr, "console=%s\n", console);
Eric Andersen0460ff21999-10-25 23:32:44 +0000197}
198
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000199static int waitfor(int pid)
Eric Andersen0460ff21999-10-25 23:32:44 +0000200{
201 int status, wpid;
202
203 message(log, "Waiting for process %d.\n", pid);
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000204 while ((wpid = wait(&status)) != pid) {
205 if (wpid > 0)
206 message(log, "pid %d exited, status=%x.\n", wpid, status);
Eric Andersen0460ff21999-10-25 23:32:44 +0000207 }
208 return wpid;
209}
210
Eric Andersena7456061999-10-27 02:31:32 +0000211
Eric Andersenc7c41d31999-10-28 00:24:35 +0000212static pid_t run(const char * const* command,
213 char *terminal, int get_enter)
Eric Andersen0460ff21999-10-25 23:32:44 +0000214{
Eric Andersenc7c41d31999-10-28 00:24:35 +0000215 int i;
216 pid_t pid;
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000217 static const char press_enter[] =
Eric Andersen0460ff21999-10-25 23:32:44 +0000218 "\nPlease press Enter to activate this console. ";
219
Eric Andersen0460ff21999-10-25 23:32:44 +0000220 if ((pid = fork()) == 0) {
221 /* Clean up */
222 close(0);
223 close(1);
224 close(2);
225 setsid();
226
Eric Andersena7456061999-10-27 02:31:32 +0000227 open(terminal, O_RDWR);
228 dup(0);
229 dup(0);
Eric Andersenc7c41d31999-10-28 00:24:35 +0000230 tcsetpgrp(0, getpgrp());
231 set_term(0);
Eric Andersen0460ff21999-10-25 23:32:44 +0000232
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000233 if (get_enter) {
Eric Andersen0460ff21999-10-25 23:32:44 +0000234 /*
235 * Save memory by not exec-ing anything large (like a shell)
236 * before the user wants it. This is critical if swap is not
237 * enabled and the system has low memory. Generally this will
238 * be run on the second virtual console, and the first will
239 * be allowed to start a shell or whatever an init script
240 * specifies.
241 */
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000242 char c;
Eric Andersenc7c41d31999-10-28 00:24:35 +0000243 write(0, press_enter, sizeof(press_enter) - 1);
Eric Andersen0460ff21999-10-25 23:32:44 +0000244 read(0, &c, 1);
245 }
246
247 /* Log the process name and args */
Eric Andersen0460ff21999-10-25 23:32:44 +0000248
Eric Andersenc7c41d31999-10-28 00:24:35 +0000249 /* Now run it. The new program will take over this PID,
Eric Andersena7456061999-10-27 02:31:32 +0000250 * so nothing further in init.c should be run. */
Eric Andersenc7c41d31999-10-28 00:24:35 +0000251 message(log, "Executing '%s', pid(%d)\r\n", *command, getpid());
252 execvp(*command, (char**)command+1);
Eric Andersen0460ff21999-10-25 23:32:44 +0000253
Eric Andersenc7c41d31999-10-28 00:24:35 +0000254 message(log, "Hmm. Trying as a script.\r\n");
Eric Andersen0460ff21999-10-25 23:32:44 +0000255 /* If shell scripts are not executed, force the issue */
256 if (errno == ENOEXEC) {
Eric Andersenc7c41d31999-10-28 00:24:35 +0000257 char * args[16];
258 args[0] = SHELL;
259 args[1] = "-c";
260 args[2] = "exec";
261 for( i=0 ; i<16 && command[i]; i++)
262 args[3+i] = (char*)command[i];
263 args[i] = NULL;
264 execvp(*args, (char**)args+1);
Eric Andersen0460ff21999-10-25 23:32:44 +0000265 }
Eric Andersenc7c41d31999-10-28 00:24:35 +0000266 message(log, "Could not execute '%s'\n", command);
Eric Andersen0460ff21999-10-25 23:32:44 +0000267 exit(-1);
268 }
269 return pid;
270}
271
Eric Andersena7456061999-10-27 02:31:32 +0000272
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000273static void shutdown_system(void)
Eric Andersencc8ed391999-10-05 16:24:54 +0000274{
Eric Andersenc7c41d31999-10-28 00:24:35 +0000275 const char* const swap_off_cmd[] = { "/bin/swapoff", "-a", 0};
276 const char* const umount_cmd[] = { "/bin/umount", "-a", "-n", 0};
Eric Andersencc8ed391999-10-05 16:24:54 +0000277
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000278 message(console, "The system is going down NOW !!\r\n");
Eric Andersen0460ff21999-10-25 23:32:44 +0000279 sync();
280 /* Allow Ctrl-Alt-Del to reboot system. */
281 reboot(RB_ENABLE_CAD);
Eric Andersencc8ed391999-10-05 16:24:54 +0000282
Eric Andersen0460ff21999-10-25 23:32:44 +0000283 /* Send signals to every process _except_ pid 1 */
284 message(console, "Sending SIGHUP to all processes.\r\n");
285 kill(-1, SIGHUP);
286 sleep(2);
287 sync();
288 message(console, "Sending SIGKILL to all processes.\r\n");
289 kill(-1, SIGKILL);
290 sleep(1);
Eric Andersenc7c41d31999-10-28 00:24:35 +0000291 waitfor(run( swap_off_cmd, console, 0));
292 waitfor(run( umount_cmd, console, 0));
Eric Andersen0460ff21999-10-25 23:32:44 +0000293 sync();
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000294 if (get_kernel_revision() <= 2 * 65536 + 2 * 256 + 11) {
Eric Andersen0460ff21999-10-25 23:32:44 +0000295 /* Removed bdflush call, kupdate in kernels >2.2.11 */
296 bdflush(1, 0);
Eric Andersencc8ed391999-10-05 16:24:54 +0000297 sync();
Eric Andersen0460ff21999-10-25 23:32:44 +0000298 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000299}
300
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000301static void halt_signal(int sig)
Eric Andersencc8ed391999-10-05 16:24:54 +0000302{
Eric Andersenabc7d591999-10-19 00:27:50 +0000303 shutdown_system();
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000304 message(console,
305 "The system is halted. Press CTRL-ALT-DEL or turn off power\r\n");
306 reboot(RB_POWER_OFF);
Eric Andersenabc7d591999-10-19 00:27:50 +0000307 exit(0);
Eric Andersencc8ed391999-10-05 16:24:54 +0000308}
309
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000310static void reboot_signal(int sig)
Eric Andersencc8ed391999-10-05 16:24:54 +0000311{
Eric Andersenabc7d591999-10-19 00:27:50 +0000312 shutdown_system();
313 message(console, "Please stand by while rebooting the system.\r\n");
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000314 reboot(RB_AUTOBOOT);
Eric Andersenabc7d591999-10-19 00:27:50 +0000315 exit(0);
Eric Andersencc8ed391999-10-05 16:24:54 +0000316}
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000317
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000318extern int init_main(int argc, char **argv)
Eric Andersen0460ff21999-10-25 23:32:44 +0000319{
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000320 int run_rc = TRUE;
Eric Andersenc7c41d31999-10-28 00:24:35 +0000321 pid_t pid1 = 0;
322 pid_t pid2 = 0;
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000323 struct stat statbuf;
Eric Andersenc7c41d31999-10-28 00:24:35 +0000324 const char* const swap_on_cmd[] = { "/bin/swapon", " -a ", 0};
325 const char* const init_commands[] = { SHELL, " -c", " exec ", INITSCRIPT, 0};
326 const char* const shell_commands[] = { SHELL, " -", 0};
327 const char* const* tty0_commands = init_commands;
328 const char* const* tty1_commands = shell_commands;
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000329 char *hello_msg_format =
Eric Andersenc1525e81999-10-29 00:07:31 +0000330 "init started: BusyBox v%s (%s) multi-call binary\r\n";
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000331 const char *no_memory =
332 "Sorry, your computer does not have enough memory.\r\n";
Eric Andersen0460ff21999-10-25 23:32:44 +0000333
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000334 /* Set up sig handlers */
335 signal(SIGUSR1, halt_signal);
336 signal(SIGSEGV, halt_signal);
337 signal(SIGPWR, halt_signal);
338 signal(SIGALRM, halt_signal);
339 signal(SIGHUP, halt_signal);
340 signal(SIGUSR2, reboot_signal);
341 signal(SIGINT, reboot_signal);
342 signal(SIGTERM, reboot_signal);
Eric Andersen0460ff21999-10-25 23:32:44 +0000343
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000344 /* Turn off rebooting via CTL-ALT-DEL -- we get a
345 * SIGINT on CAD so we can shut things down gracefully... */
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000346 reboot(RB_DISABLE_CAD);
Eric Andersenc7c41d31999-10-28 00:24:35 +0000347
348 /* Figure out where the default console should be */
349 console_init();
Eric Andersen0460ff21999-10-25 23:32:44 +0000350
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000351 /* Close whatever files are open, and reset the console. */
352 close(0);
353 close(1);
354 close(2);
Eric Andersenc7c41d31999-10-28 00:24:35 +0000355 //set_term(0);
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000356 setsid();
Eric Andersencc8ed391999-10-05 16:24:54 +0000357
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000358 /* Make sure PATH is set to something sane */
359 if (getenv("PATH") == NULL)
360 putenv(PATH_DEFAULT);
Eric Andersencc8ed391999-10-05 16:24:54 +0000361
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000362 /* Hello world */
Eric Andersenc1525e81999-10-29 00:07:31 +0000363 message(log, hello_msg_format, BB_VER, BB_BT);
364 fprintf(stderr, hello_msg_format, BB_VER, BB_BT);
Eric Andersencc8ed391999-10-05 16:24:54 +0000365
Eric Andersenc7c41d31999-10-28 00:24:35 +0000366
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000367 /* Mount /proc */
Eric Andersenc7c41d31999-10-28 00:24:35 +0000368 if (mount("/proc", "/proc", "proc", 0, 0) == 0) {
369 fprintf(stderr, "Mounting /proc: done.\n");
Eric Andersena7456061999-10-27 02:31:32 +0000370 message(log, "Mounting /proc: done.\n");
Eric Andersenc7c41d31999-10-28 00:24:35 +0000371 } else {
372 fprintf(stderr, "Mounting /proc: failed!\n");
373 message(log, "Mounting /proc: failed!\n");
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000374 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000375
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000376 /* Make sure there is enough memory to do something useful */
377 set_free_pages();
Eric Andersenc1525e81999-10-29 00:07:31 +0000378 if (mem_total() < 3500) {
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000379 int retval;
380 retval = stat("/etc/fstab", &statbuf);
381 if (retval) {
Eric Andersenc7c41d31999-10-28 00:24:35 +0000382 fprintf(stderr, "%s", no_memory);
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000383 while (1) {
384 sleep(1);
385 }
386 } else {
387 /* Try to turn on swap */
Eric Andersenc7c41d31999-10-28 00:24:35 +0000388 waitfor(run(swap_on_cmd, console, 0));
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000389 if (mem_total() < 2000) {
Eric Andersenc7c41d31999-10-28 00:24:35 +0000390 fprintf(stderr, "%s", no_memory);
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000391 while (1) {
392 sleep(1);
Eric Andersenabc7d591999-10-19 00:27:50 +0000393 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000394 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000395 }
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000396 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000397
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000398 /* Check if we are supposed to be in single user mode */
399 if ( argc > 1 && (!strcmp(argv[1], "single") ||
400 !strcmp(argv[1], "-s") || !strcmp(argv[1], "1"))) {
401 run_rc = FALSE;
402 tty0_commands = shell_commands;
Eric Andersena7456061999-10-27 02:31:32 +0000403 tty1_commands = shell_commands;
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000404 }
405
406 /* Make sure an init script exists before trying to run it */
407 if (run_rc == TRUE && stat(INITSCRIPT, &statbuf)) {
408 tty0_commands = shell_commands;
409 tty1_commands = shell_commands;
410 }
411
412 /* Ok, now launch the rc script and/or prepare to
413 * start up some VTs if somebody hits enter...
414 */
415 for (;;) {
Eric Andersenc7c41d31999-10-28 00:24:35 +0000416 pid_t wpid;
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000417 int status;
418
419 if (pid1 == 0 && tty0_commands) {
Eric Andersenc7c41d31999-10-28 00:24:35 +0000420 pid1 = run(tty0_commands, console, 1);
Eric Andersencc8ed391999-10-05 16:24:54 +0000421 }
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000422 if (pid2 == 0 && tty1_commands) {
423 pid2 = run(tty1_commands, second_terminal, 1);
Eric Andersencc8ed391999-10-05 16:24:54 +0000424 }
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000425 wpid = wait(&status);
Eric Andersena7456061999-10-27 02:31:32 +0000426 if (wpid > 0 ) {
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000427 message(log, "pid %d exited, status=%x.\n", wpid, status);
428 }
429 /* Don't respawn an init script if it exits */
430 if (run_rc == FALSE && wpid == pid1) {
431 pid1 = 0;
432 }
433 if (wpid == pid2) {
434 pid2 = 0;
435 }
436 sleep(1);
437 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000438}