blob: 83fc9519ab04a2a9a5700076f9775e3270b01bd0 [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 Andersen0460ff21999-10-25 23:32:44 +000044#define DEBUG_INIT
Eric Andersencc8ed391999-10-05 16:24:54 +000045
Eric Andersen8a8fbb81999-10-26 00:18:56 +000046#define CONSOLE "/dev/console" /* Logical system console */
47#define VT_PRIMARY "/dev/tty0" /* Virtual console master */
48#define VT_SECONDARY "/dev/tty1" /* Virtual console master */
49#define VT_LOG "/dev/tty2" /* Virtual console master */
50#define SHELL "/bin/sh" /* Default shell */
51#define INITSCRIPT "/etc/init.d/rcS" /* Initscript. */
Eric Andersen0460ff21999-10-25 23:32:44 +000052#define PATH_DEFAULT "PATH=/usr/local/sbin:/sbin:/bin:/usr/sbin:/usr/bin"
53
Eric Andersen8a8fbb81999-10-26 00:18:56 +000054static char *console = CONSOLE;
Eric Andersena7456061999-10-27 02:31:32 +000055static char *first_terminal = "/dev/tty1";
Eric Andersen8a8fbb81999-10-26 00:18:56 +000056static char *second_terminal = "/dev/tty2";
57static char *log = "/dev/tty3";
Eric Andersen0460ff21999-10-25 23:32:44 +000058
59
60
61/* try to open up the specified device */
Eric Andersen8a8fbb81999-10-26 00:18:56 +000062int device_open(char *device, int mode)
Eric Andersencc8ed391999-10-05 16:24:54 +000063{
Eric Andersen0460ff21999-10-25 23:32:44 +000064 int m, f, fd = -1;
Eric Andersen8a8fbb81999-10-26 00:18:56 +000065
Eric Andersen0460ff21999-10-25 23:32:44 +000066 mode = m | O_NONBLOCK;
Eric Andersen8a8fbb81999-10-26 00:18:56 +000067
Eric Andersen0460ff21999-10-25 23:32:44 +000068 /* Retry up to 5 times */
Eric Andersen8a8fbb81999-10-26 00:18:56 +000069 for (f = 0; f < 5; f++)
70 if ((fd = open(device, m)) >= 0)
71 break;
72 if (fd < 0)
73 return fd;
Eric Andersen0460ff21999-10-25 23:32:44 +000074 /* Set original flags. */
75 if (m != mode)
76 fcntl(fd, F_SETFL, mode);
77 return fd;
78}
Eric Andersencc8ed391999-10-05 16:24:54 +000079
Eric Andersen0460ff21999-10-25 23:32:44 +000080/* print a message to the specified device */
Eric Andersen8a8fbb81999-10-26 00:18:56 +000081void message(char *device, char *fmt, ...)
Eric Andersen0460ff21999-10-25 23:32:44 +000082{
83 int fd;
84 va_list arguments;
Eric Andersen8a8fbb81999-10-26 00:18:56 +000085
86 if ((fd = device_open(device, O_WRONLY | O_NOCTTY | O_NDELAY)) >= 0) {
Eric Andersen0460ff21999-10-25 23:32:44 +000087 va_start(arguments, fmt);
88 vdprintf(fd, fmt, arguments);
Eric Andersencc8ed391999-10-05 16:24:54 +000089 va_end(arguments);
Eric Andersen8a8fbb81999-10-26 00:18:56 +000090 close(fd);
Eric Andersena7456061999-10-27 02:31:32 +000091 } else {
92 fprintf(stderr, "Bummer, can't print: ");
93 va_start(arguments, fmt);
94 vfprintf(stderr, fmt, arguments);
95 fflush(stderr);
96 va_end(arguments);
97 }
Eric Andersencc8ed391999-10-05 16:24:54 +000098}
99
Eric Andersen0460ff21999-10-25 23:32:44 +0000100/* Set terminal settings to reasonable defaults */
101void set_term()
Eric Andersencc8ed391999-10-05 16:24:54 +0000102{
Eric Andersen0460ff21999-10-25 23:32:44 +0000103 int fd;
104 struct termios tty;
Eric Andersencc8ed391999-10-05 16:24:54 +0000105
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000106 if ((fd = device_open(console, O_RDWR | O_NOCTTY)) < 0) {
107 message(log, "can't open %s\n", console);
Eric Andersen0460ff21999-10-25 23:32:44 +0000108 return;
109 }
110 ioctl(fd, TCGETS, &tty);
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000111 tty.c_cflag &= CBAUD | CBAUDEX | CSIZE | CSTOPB | PARENB | PARODD;
112 tty.c_cflag |= HUPCL | CLOCAL;
Eric Andersencc8ed391999-10-05 16:24:54 +0000113
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000114 tty.c_cc[VINTR] = 3;
115 tty.c_cc[VQUIT] = 28;
Eric Andersen0460ff21999-10-25 23:32:44 +0000116 tty.c_cc[VERASE] = 127;
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000117 tty.c_cc[VKILL] = 24;
118 tty.c_cc[VEOF] = 4;
119 tty.c_cc[VTIME] = 0;
120 tty.c_cc[VMIN] = 1;
Eric Andersen0460ff21999-10-25 23:32:44 +0000121 tty.c_cc[VSTART] = 17;
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000122 tty.c_cc[VSTOP] = 19;
123 tty.c_cc[VSUSP] = 26;
Eric Andersencc8ed391999-10-05 16:24:54 +0000124
Eric Andersen0460ff21999-10-25 23:32:44 +0000125 /* Set pre and post processing */
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000126 tty.c_iflag = IGNPAR | ICRNL | IXON | IXANY;
127 tty.c_oflag = OPOST | ONLCR;
128 tty.c_lflag = ISIG | ICANON | ECHO | ECHOCTL | ECHOPRT | ECHOKE;
Eric Andersencc8ed391999-10-05 16:24:54 +0000129
Eric Andersen0460ff21999-10-25 23:32:44 +0000130 /* Now set the terminal line. */
131 ioctl(fd, TCSETS, &tty);
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000132 close(fd);
Eric Andersencc8ed391999-10-05 16:24:54 +0000133}
134
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000135static int mem_total()
Eric Andersencc8ed391999-10-05 16:24:54 +0000136{
Eric Andersenabc7d591999-10-19 00:27:50 +0000137 char s[80];
138 char *p;
139 FILE *f;
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000140 const char pattern[] = "MemTotal:";
Eric Andersencc8ed391999-10-05 16:24:54 +0000141
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000142 f = fopen("/proc/meminfo", "r");
143 while (NULL != fgets(s, 79, f)) {
144 p = strstr(s, pattern);
Eric Andersenabc7d591999-10-19 00:27:50 +0000145 if (NULL != p) {
146 fclose(f);
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000147 return (atoi(p + strlen(pattern)));
Eric Andersenabc7d591999-10-19 00:27:50 +0000148 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000149 }
Eric Andersenabc7d591999-10-19 00:27:50 +0000150 return -1;
Eric Andersencc8ed391999-10-05 16:24:54 +0000151}
152
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000153static void set_free_pages()
Eric Andersencc8ed391999-10-05 16:24:54 +0000154{
Eric Andersenabc7d591999-10-19 00:27:50 +0000155 char s[80];
156 FILE *f;
Eric Andersencc8ed391999-10-05 16:24:54 +0000157
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000158 f = fopen("/proc/sys/vm/freepages", "r");
159 fgets(s, 79, f);
Eric Andersenabc7d591999-10-19 00:27:50 +0000160 if (atoi(s) < 32) {
161 fclose(f);
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000162 f = fopen("/proc/sys/vm/freepages", "w");
163 fprintf(f, "30\t40\t50\n");
Eric Andersenabc7d591999-10-19 00:27:50 +0000164 printf("\nIncreased /proc/sys/vm/freepages values to 30/40/50\n");
165 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000166 fclose(f);
Eric Andersencc8ed391999-10-05 16:24:54 +0000167}
168
Eric Andersen0460ff21999-10-25 23:32:44 +0000169
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000170static void console_init()
Eric Andersen0460ff21999-10-25 23:32:44 +0000171{
172 int fd;
Eric Andersena7456061999-10-27 02:31:32 +0000173 struct stat statbuf;
Eric Andersen0460ff21999-10-25 23:32:44 +0000174 int tried_devcons = 0;
175 int tried_vtmaster = 0;
176 char *s;
177
178 if ((s = getenv("CONSOLE")) != NULL)
179 console = s;
180 else {
181 console = CONSOLE;
182 tried_devcons++;
183 }
Eric Andersena7456061999-10-27 02:31:32 +0000184
185 if ( stat(CONSOLE, &statbuf) && S_ISLNK(statbuf.st_mode)) {
186 fprintf(stderr, "/dev/console does not exist, or is a symlink.\n");
187 }
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000188 while ((fd = open(console, O_RDONLY | O_NONBLOCK)) < 0) {
Eric Andersen0460ff21999-10-25 23:32:44 +0000189 if (!tried_devcons) {
190 tried_devcons++;
191 console = CONSOLE;
192 continue;
193 }
194 if (!tried_vtmaster) {
195 tried_vtmaster++;
196 console = VT_PRIMARY;
197 continue;
198 }
199 break;
200 }
201 if (fd < 0)
202 console = "/dev/null";
203 else
204 close(fd);
Eric Andersena7456061999-10-27 02:31:32 +0000205 fprintf(stderr, "console=%s\n", console);
Eric Andersen0460ff21999-10-25 23:32:44 +0000206}
207
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000208static int waitfor(int pid)
Eric Andersen0460ff21999-10-25 23:32:44 +0000209{
210 int status, wpid;
211
212 message(log, "Waiting for process %d.\n", pid);
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000213 while ((wpid = wait(&status)) != pid) {
214 if (wpid > 0)
215 message(log, "pid %d exited, status=%x.\n", wpid, status);
Eric Andersen0460ff21999-10-25 23:32:44 +0000216 }
217 return wpid;
218}
219
Eric Andersena7456061999-10-27 02:31:32 +0000220
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000221static int run(const char *command, char *terminal, int get_enter)
Eric Andersen0460ff21999-10-25 23:32:44 +0000222{
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000223 int f, pid;
Eric Andersen0460ff21999-10-25 23:32:44 +0000224 char *args[16];
225 char buf[256];
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000226 char *ptr;
227 static const char press_enter[] =
Eric Andersen0460ff21999-10-25 23:32:44 +0000228 "\nPlease press Enter to activate this console. ";
229
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000230
231 /* Make a proper command from the command string */
Eric Andersen0460ff21999-10-25 23:32:44 +0000232 strcpy(buf, command);
233 ptr = buf;
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000234 for (f = 1; f < 15; f++) {
Eric Andersen0460ff21999-10-25 23:32:44 +0000235 /* Skip white space */
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000236 while (*ptr == ' ' || *ptr == '\t')
237 ptr++;
Eric Andersen0460ff21999-10-25 23:32:44 +0000238 args[f] = ptr;
239 /* May be trailing space.. */
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000240 if (*ptr == 0)
241 break;
Eric Andersen0460ff21999-10-25 23:32:44 +0000242 /* Skip this `word' */
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000243 while (*ptr && *ptr != ' ' && *ptr != '\t' && *ptr != '#')
Eric Andersen0460ff21999-10-25 23:32:44 +0000244 ptr++;
245 /* If end-of-line, break */
246 if (*ptr == '#' || *ptr == 0) {
247 f++;
248 *ptr = 0;
249 break;
250 }
251 /* End word with \0 and continue */
252 args[f] = NULL;
253 }
254 args[0] = args[1];
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000255
Eric Andersen0460ff21999-10-25 23:32:44 +0000256
257 if ((pid = fork()) == 0) {
258 /* Clean up */
259 close(0);
260 close(1);
261 close(2);
262 setsid();
263
Eric Andersena7456061999-10-27 02:31:32 +0000264#if 1
265 //if ((f = device_open(terminal, O_RDWR | O_NOCTTY)) < 0) {
266 if ((f = device_open(terminal, O_RDWR )) < 0) {
267 message(log, "open(%s) failed: %s\n",
268 terminal, strerror(errno));
Eric Andersen0460ff21999-10-25 23:32:44 +0000269 return -1;
270 }
271 dup(f);
272 dup(f);
Eric Andersena7456061999-10-27 02:31:32 +0000273#else
274 open(terminal, O_RDWR);
275 dup(0);
276 dup(0);
277 //tcsetpgrp(0, getpgrp());
278#endif
Eric Andersen0460ff21999-10-25 23:32:44 +0000279 set_term();
280
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000281 if (get_enter) {
Eric Andersen0460ff21999-10-25 23:32:44 +0000282 /*
283 * Save memory by not exec-ing anything large (like a shell)
284 * before the user wants it. This is critical if swap is not
285 * enabled and the system has low memory. Generally this will
286 * be run on the second virtual console, and the first will
287 * be allowed to start a shell or whatever an init script
288 * specifies.
289 */
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000290 char c;
Eric Andersen0460ff21999-10-25 23:32:44 +0000291 write(1, press_enter, sizeof(press_enter) - 1);
292 read(0, &c, 1);
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000293 message(console, "Got an enter\r\n");
Eric Andersen0460ff21999-10-25 23:32:44 +0000294 }
295
296 /* Log the process name and args */
Eric Andersena7456061999-10-27 02:31:32 +0000297 message(console, "Executing '%s'\r\n", command);
298 message(log, "Executing '%s'\r\n", command);
Eric Andersen0460ff21999-10-25 23:32:44 +0000299
Eric Andersena7456061999-10-27 02:31:32 +0000300 /* Now run it. This program should take over this PID,
301 * so nothing further in init.c should be run. */
Eric Andersen0460ff21999-10-25 23:32:44 +0000302 execvp(args[1], args + 1);
303
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000304 message(console, "Hmm. Trying as a script.\r\n");
Eric Andersen0460ff21999-10-25 23:32:44 +0000305 /* If shell scripts are not executed, force the issue */
306 if (errno == ENOEXEC) {
307 char buf[256];
308 args[1] = SHELL;
309 args[2] = "-c";
310 strcpy(buf, "exec ");
311 strcat(buf, command);
312 args[3] = buf;
313 args[4] = NULL;
314 execvp(args[1], args + 1);
315 }
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000316 message(console, "Could not execute '%s'\n", command);
Eric Andersen0460ff21999-10-25 23:32:44 +0000317 exit(-1);
318 }
319 return pid;
320}
321
Eric Andersena7456061999-10-27 02:31:32 +0000322
Eric Andersen0460ff21999-10-25 23:32:44 +0000323#ifndef DEBUG_INIT
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000324static void shutdown_system(void)
Eric Andersencc8ed391999-10-05 16:24:54 +0000325{
Eric Andersencc8ed391999-10-05 16:24:54 +0000326
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000327 message(console, "The system is going down NOW !!\r\n");
Eric Andersen0460ff21999-10-25 23:32:44 +0000328 sync();
329 /* Allow Ctrl-Alt-Del to reboot system. */
330 reboot(RB_ENABLE_CAD);
Eric Andersencc8ed391999-10-05 16:24:54 +0000331
Eric Andersen0460ff21999-10-25 23:32:44 +0000332 /* Send signals to every process _except_ pid 1 */
333 message(console, "Sending SIGHUP to all processes.\r\n");
334 kill(-1, SIGHUP);
335 sleep(2);
336 sync();
337 message(console, "Sending SIGKILL to all processes.\r\n");
338 kill(-1, SIGKILL);
339 sleep(1);
340 waitfor(run("/bin/swapoff -a", console, 0));
341 waitfor(run("/bin/umount -a -n", console, 0));
342 sync();
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000343 if (get_kernel_revision() <= 2 * 65536 + 2 * 256 + 11) {
Eric Andersen0460ff21999-10-25 23:32:44 +0000344 /* Removed bdflush call, kupdate in kernels >2.2.11 */
345 bdflush(1, 0);
Eric Andersencc8ed391999-10-05 16:24:54 +0000346 sync();
Eric Andersen0460ff21999-10-25 23:32:44 +0000347 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000348}
349
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000350static void halt_signal(int sig)
Eric Andersencc8ed391999-10-05 16:24:54 +0000351{
Eric Andersenabc7d591999-10-19 00:27:50 +0000352 shutdown_system();
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000353 message(console,
354 "The system is halted. Press CTRL-ALT-DEL or turn off power\r\n");
355 reboot(RB_POWER_OFF);
Eric Andersenabc7d591999-10-19 00:27:50 +0000356 exit(0);
Eric Andersencc8ed391999-10-05 16:24:54 +0000357}
358
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000359static void reboot_signal(int sig)
Eric Andersencc8ed391999-10-05 16:24:54 +0000360{
Eric Andersenabc7d591999-10-19 00:27:50 +0000361 shutdown_system();
362 message(console, "Please stand by while rebooting the system.\r\n");
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000363 reboot(RB_AUTOBOOT);
Eric Andersenabc7d591999-10-19 00:27:50 +0000364 exit(0);
Eric Andersencc8ed391999-10-05 16:24:54 +0000365}
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000366
Eric Andersenabc7d591999-10-19 00:27:50 +0000367#endif
Eric Andersen0460ff21999-10-25 23:32:44 +0000368
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000369extern int init_main(int argc, char **argv)
Eric Andersen0460ff21999-10-25 23:32:44 +0000370{
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000371 int run_rc = TRUE;
372 int pid1 = 0;
373 int pid2 = 0;
374 struct stat statbuf;
Eric Andersena7456061999-10-27 02:31:32 +0000375 const char *init_commands = SHELL " -c exec " INITSCRIPT;
376 const char *shell_commands = SHELL " -";
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000377 const char *tty0_commands = init_commands;
378 const char *tty1_commands = shell_commands;
379 char *hello_msg_format =
380 "init started: BusyBox v%s (%s) multi-call binary\r\n";
381 const char *no_memory =
382 "Sorry, your computer does not have enough memory.\r\n";
Eric Andersen0460ff21999-10-25 23:32:44 +0000383
384
385#ifndef DEBUG_INIT
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000386 /* Set up sig handlers */
387 signal(SIGUSR1, halt_signal);
388 signal(SIGSEGV, halt_signal);
389 signal(SIGPWR, halt_signal);
390 signal(SIGALRM, halt_signal);
391 signal(SIGHUP, halt_signal);
392 signal(SIGUSR2, reboot_signal);
393 signal(SIGINT, reboot_signal);
394 signal(SIGTERM, reboot_signal);
Eric Andersen0460ff21999-10-25 23:32:44 +0000395#endif
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000396 /* Figure out where the default console should be */
397 console_init();
Eric Andersen0460ff21999-10-25 23:32:44 +0000398
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000399 /* Turn off rebooting via CTL-ALT-DEL -- we get a
400 * SIGINT on CAD so we can shut things down gracefully... */
Eric Andersen0460ff21999-10-25 23:32:44 +0000401#ifndef DEBUG_INIT
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000402 reboot(RB_DISABLE_CAD);
Eric Andersen0460ff21999-10-25 23:32:44 +0000403#endif
404
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000405 /* Close whatever files are open, and reset the console. */
406 close(0);
407 close(1);
408 close(2);
409 set_term();
410 setsid();
Eric Andersencc8ed391999-10-05 16:24:54 +0000411
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000412 /* Make sure PATH is set to something sane */
413 if (getenv("PATH") == NULL)
414 putenv(PATH_DEFAULT);
Eric Andersencc8ed391999-10-05 16:24:54 +0000415
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000416 /* Hello world */
417 message(log, hello_msg_format, BB_VER, BB_BT);
418 message(console, hello_msg_format, BB_VER, BB_BT);
Eric Andersencc8ed391999-10-05 16:24:54 +0000419
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000420 /* Mount /proc */
421 if (mount("/proc", "/proc", "proc", 0, 0)) {
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000422 message(console, "Mounting /proc: failed!\r\n");
Eric Andersena7456061999-10-27 02:31:32 +0000423 message(log, "Mounting /proc: failed!\n");
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000424 } else {
425 message(console, "Mounting /proc: done.\r\n");
Eric Andersena7456061999-10-27 02:31:32 +0000426 message(log, "Mounting /proc: done.\n");
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000427 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000428
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000429 /* Make sure there is enough memory to do something useful */
430 set_free_pages();
431 if (mem_total() < 2000) {
432 int retval;
433 retval = stat("/etc/fstab", &statbuf);
434 if (retval) {
435 message(console, "%s", no_memory);
436 while (1) {
437 sleep(1);
438 }
439 } else {
440 /* Try to turn on swap */
441 waitfor(run("/bin/swapon -a", console, 0));
442 if (mem_total() < 2000) {
Eric Andersen0460ff21999-10-25 23:32:44 +0000443 message(console, "%s", no_memory);
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000444 while (1) {
445 sleep(1);
Eric Andersenabc7d591999-10-19 00:27:50 +0000446 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000447 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000448 }
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000449 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000450
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000451 /* Check if we are supposed to be in single user mode */
452 if ( argc > 1 && (!strcmp(argv[1], "single") ||
453 !strcmp(argv[1], "-s") || !strcmp(argv[1], "1"))) {
454 run_rc = FALSE;
455 tty0_commands = shell_commands;
Eric Andersena7456061999-10-27 02:31:32 +0000456 tty1_commands = shell_commands;
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000457 }
458
459 /* Make sure an init script exists before trying to run it */
460 if (run_rc == TRUE && stat(INITSCRIPT, &statbuf)) {
461 tty0_commands = shell_commands;
462 tty1_commands = shell_commands;
463 }
464
465 /* Ok, now launch the rc script and/or prepare to
466 * start up some VTs if somebody hits enter...
467 */
468 for (;;) {
469 int wpid;
470 int status;
471
472 if (pid1 == 0 && tty0_commands) {
Eric Andersena7456061999-10-27 02:31:32 +0000473 pid1 = run(tty0_commands, first_terminal, 1);
Eric Andersencc8ed391999-10-05 16:24:54 +0000474 }
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000475 if (pid2 == 0 && tty1_commands) {
476 pid2 = run(tty1_commands, second_terminal, 1);
Eric Andersencc8ed391999-10-05 16:24:54 +0000477 }
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000478 wpid = wait(&status);
Eric Andersena7456061999-10-27 02:31:32 +0000479 if (wpid > 0 ) {
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000480 message(log, "pid %d exited, status=%x.\n", wpid, status);
481 }
482 /* Don't respawn an init script if it exits */
483 if (run_rc == FALSE && wpid == pid1) {
484 pid1 = 0;
485 }
486 if (wpid == pid2) {
487 pid2 = 0;
488 }
489 sleep(1);
490 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000491}