blob: 0850cf810e7fbdfcb852b16a0ab404049849c553 [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 */
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 */
Eric Andersen2f6c04f1999-11-01 23:59:44 +000052//#define INITSCRIPT "/etc/init.d/rcS" /* Initscript. */
53#define INITSCRIPT "/tmp/foo.sh"
Eric Andersen0460ff21999-10-25 23:32:44 +000054#define PATH_DEFAULT "PATH=/usr/local/sbin:/sbin:/bin:/usr/sbin:/usr/bin"
55
Eric Andersen8a8fbb81999-10-26 00:18:56 +000056static char *console = CONSOLE;
Eric Andersenc7c41d31999-10-28 00:24:35 +000057//static char *first_terminal = "/dev/tty1";
Eric Andersen8a8fbb81999-10-26 00:18:56 +000058static char *second_terminal = "/dev/tty2";
59static char *log = "/dev/tty3";
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 Andersen7f1acfd1999-10-29 23:09:13 +0000117 /* use lineo dicipline 0 */
118 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 Andersen8a8fbb81999-10-26 00:18:56 +0000132static int mem_total()
Eric Andersencc8ed391999-10-05 16:24:54 +0000133{
Eric Andersenabc7d591999-10-19 00:27:50 +0000134 char s[80];
135 char *p;
136 FILE *f;
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000137 const char pattern[] = "MemTotal:";
Eric Andersencc8ed391999-10-05 16:24:54 +0000138
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000139 f = fopen("/proc/meminfo", "r");
140 while (NULL != fgets(s, 79, f)) {
141 p = strstr(s, pattern);
Eric Andersenabc7d591999-10-19 00:27:50 +0000142 if (NULL != p) {
143 fclose(f);
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000144 return (atoi(p + strlen(pattern)));
Eric Andersenabc7d591999-10-19 00:27:50 +0000145 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000146 }
Eric Andersenabc7d591999-10-19 00:27:50 +0000147 return -1;
Eric Andersencc8ed391999-10-05 16:24:54 +0000148}
149
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000150static void set_free_pages()
Eric Andersencc8ed391999-10-05 16:24:54 +0000151{
Eric Andersenabc7d591999-10-19 00:27:50 +0000152 char s[80];
153 FILE *f;
Eric Andersencc8ed391999-10-05 16:24:54 +0000154
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000155 f = fopen("/proc/sys/vm/freepages", "r");
156 fgets(s, 79, f);
Eric Andersenabc7d591999-10-19 00:27:50 +0000157 if (atoi(s) < 32) {
158 fclose(f);
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000159 f = fopen("/proc/sys/vm/freepages", "w");
160 fprintf(f, "30\t40\t50\n");
Eric Andersen7f1acfd1999-10-29 23:09:13 +0000161 message(log, "\nIncreased /proc/sys/vm/freepages values to 30/40/50\n");
Eric Andersenabc7d591999-10-19 00:27:50 +0000162 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000163 fclose(f);
Eric Andersencc8ed391999-10-05 16:24:54 +0000164}
165
Eric Andersen0460ff21999-10-25 23:32:44 +0000166
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000167static void console_init()
Eric Andersen0460ff21999-10-25 23:32:44 +0000168{
169 int fd;
Eric Andersena7456061999-10-27 02:31:32 +0000170 struct stat statbuf;
Eric Andersen0460ff21999-10-25 23:32:44 +0000171 int tried_devcons = 0;
172 int tried_vtmaster = 0;
173 char *s;
174
175 if ((s = getenv("CONSOLE")) != NULL)
176 console = s;
177 else {
178 console = CONSOLE;
179 tried_devcons++;
180 }
Eric Andersena7456061999-10-27 02:31:32 +0000181
182 if ( stat(CONSOLE, &statbuf) && S_ISLNK(statbuf.st_mode)) {
Eric Andersenc7c41d31999-10-28 00:24:35 +0000183 fprintf(stderr, "Yikes! /dev/console does not exist or is a symlink.\n");
Eric Andersen7f1acfd1999-10-29 23:09:13 +0000184 message(log, "Yikes! /dev/console does not exist or is a symlink.\n");
Eric Andersena7456061999-10-27 02:31:32 +0000185 }
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000186 while ((fd = open(console, O_RDONLY | O_NONBLOCK)) < 0) {
Eric Andersen0460ff21999-10-25 23:32:44 +0000187 if (!tried_devcons) {
188 tried_devcons++;
189 console = CONSOLE;
190 continue;
191 }
192 if (!tried_vtmaster) {
193 tried_vtmaster++;
194 console = VT_PRIMARY;
195 continue;
196 }
197 break;
198 }
199 if (fd < 0)
200 console = "/dev/null";
201 else
202 close(fd);
Eric Andersen7f1acfd1999-10-29 23:09:13 +0000203 message(log, "console=%s\n", console);
Eric Andersen0460ff21999-10-25 23:32:44 +0000204}
205
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000206static int waitfor(int pid)
Eric Andersen0460ff21999-10-25 23:32:44 +0000207{
208 int status, wpid;
209
210 message(log, "Waiting for process %d.\n", pid);
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000211 while ((wpid = wait(&status)) != pid) {
212 if (wpid > 0)
213 message(log, "pid %d exited, status=%x.\n", wpid, status);
Eric Andersen0460ff21999-10-25 23:32:44 +0000214 }
215 return wpid;
216}
217
Eric Andersena7456061999-10-27 02:31:32 +0000218
Eric Andersenc7c41d31999-10-28 00:24:35 +0000219static pid_t run(const char * const* command,
220 char *terminal, int get_enter)
Eric Andersen0460ff21999-10-25 23:32:44 +0000221{
Eric Andersen2f6c04f1999-11-01 23:59:44 +0000222 int fd;
Eric Andersenc7c41d31999-10-28 00:24:35 +0000223 pid_t pid;
Eric Andersen2f6c04f1999-11-01 23:59:44 +0000224 const char * const* cmd = command;
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000225 static const char press_enter[] =
Eric Andersen0460ff21999-10-25 23:32:44 +0000226 "\nPlease press Enter to activate this console. ";
227
Eric Andersen0460ff21999-10-25 23:32:44 +0000228 if ((pid = fork()) == 0) {
229 /* Clean up */
230 close(0);
231 close(1);
232 close(2);
233 setsid();
234
Eric Andersen2f6c04f1999-11-01 23:59:44 +0000235 /* Reset signal handlers set for parent process */
236 signal(SIGUSR1, SIG_DFL);
237 signal(SIGUSR2, SIG_DFL);
238 signal(SIGINT, SIG_DFL);
239 signal(SIGTERM, SIG_DFL);
240
241 if ((fd = device_open(terminal, O_RDWR)) < 0) {
242 message(log, "Bummer, can't open %s\r\n", terminal);
243 exit(-1);
244 }
245 dup(fd);
246 dup(fd);
Eric Andersenc7c41d31999-10-28 00:24:35 +0000247 tcsetpgrp(0, getpgrp());
248 set_term(0);
Eric Andersen0460ff21999-10-25 23:32:44 +0000249
Eric Andersen2f6c04f1999-11-01 23:59:44 +0000250 if (get_enter==TRUE) {
Eric Andersen0460ff21999-10-25 23:32:44 +0000251 /*
252 * Save memory by not exec-ing anything large (like a shell)
253 * before the user wants it. This is critical if swap is not
254 * enabled and the system has low memory. Generally this will
255 * be run on the second virtual console, and the first will
256 * be allowed to start a shell or whatever an init script
257 * specifies.
258 */
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000259 char c;
Eric Andersen7f1acfd1999-10-29 23:09:13 +0000260 write(1, press_enter, sizeof(press_enter) - 1);
Eric Andersen0460ff21999-10-25 23:32:44 +0000261 read(0, &c, 1);
262 }
263
264 /* Log the process name and args */
Eric Andersen2f6c04f1999-11-01 23:59:44 +0000265 message(log, "Executing pid(%d): '", getpid());
266 while ( *cmd) message(log, "%s ", *cmd++);
267 message(log, "'\r\n");
268
Eric Andersenc7c41d31999-10-28 00:24:35 +0000269 /* Now run it. The new program will take over this PID,
Eric Andersena7456061999-10-27 02:31:32 +0000270 * so nothing further in init.c should be run. */
Eric Andersenc7c41d31999-10-28 00:24:35 +0000271 execvp(*command, (char**)command+1);
Eric Andersen0460ff21999-10-25 23:32:44 +0000272
Eric Andersen2f6c04f1999-11-01 23:59:44 +0000273 message(log, "Bummer, could not execute '%s'\n", command);
Eric Andersen0460ff21999-10-25 23:32:44 +0000274 exit(-1);
275 }
276 return pid;
277}
278
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000279static void shutdown_system(void)
Eric Andersencc8ed391999-10-05 16:24:54 +0000280{
Eric Andersen2f6c04f1999-11-01 23:59:44 +0000281 const char* const swap_off_cmd[] = { "swapoff", "swapoff", "-a", 0};
282 const char* const umount_cmd[] = { "umount", "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();
Eric Andersen2f6c04f1999-11-01 23:59:44 +0000286#ifndef DEBUG_INIT
Eric Andersen0460ff21999-10-25 23:32:44 +0000287 /* Allow Ctrl-Alt-Del to reboot system. */
288 reboot(RB_ENABLE_CAD);
Eric Andersen2f6c04f1999-11-01 23:59:44 +0000289#endif
Eric Andersen0460ff21999-10-25 23:32:44 +0000290 /* Send signals to every process _except_ pid 1 */
291 message(console, "Sending SIGHUP to all processes.\r\n");
Eric Andersen2f6c04f1999-11-01 23:59:44 +0000292#ifndef DEBUG_INIT
Eric Andersen0460ff21999-10-25 23:32:44 +0000293 kill(-1, SIGHUP);
Eric Andersen2f6c04f1999-11-01 23:59:44 +0000294#endif
Eric Andersen0460ff21999-10-25 23:32:44 +0000295 sleep(2);
296 sync();
297 message(console, "Sending SIGKILL to all processes.\r\n");
Eric Andersen2f6c04f1999-11-01 23:59:44 +0000298#ifndef DEBUG_INIT
Eric Andersen0460ff21999-10-25 23:32:44 +0000299 kill(-1, SIGKILL);
Eric Andersen2f6c04f1999-11-01 23:59:44 +0000300#endif
Eric Andersen0460ff21999-10-25 23:32:44 +0000301 sleep(1);
Eric Andersen2f6c04f1999-11-01 23:59:44 +0000302 waitfor(run( swap_off_cmd, log, FALSE));
303 waitfor(run( umount_cmd, log, FALSE));
Eric Andersen0460ff21999-10-25 23:32:44 +0000304 sync();
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000305 if (get_kernel_revision() <= 2 * 65536 + 2 * 256 + 11) {
Eric Andersen0460ff21999-10-25 23:32:44 +0000306 /* Removed bdflush call, kupdate in kernels >2.2.11 */
307 bdflush(1, 0);
Eric Andersencc8ed391999-10-05 16:24:54 +0000308 sync();
Eric Andersen0460ff21999-10-25 23:32:44 +0000309 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000310}
311
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000312static void halt_signal(int sig)
Eric Andersencc8ed391999-10-05 16:24:54 +0000313{
Eric Andersenabc7d591999-10-19 00:27:50 +0000314 shutdown_system();
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000315 message(console,
316 "The system is halted. Press CTRL-ALT-DEL or turn off power\r\n");
Eric Andersen2f6c04f1999-11-01 23:59:44 +0000317#ifndef DEBUG_INIT
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000318 reboot(RB_POWER_OFF);
Eric Andersen2f6c04f1999-11-01 23:59:44 +0000319#endif
Eric Andersenabc7d591999-10-19 00:27:50 +0000320 exit(0);
Eric Andersencc8ed391999-10-05 16:24:54 +0000321}
322
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000323static void reboot_signal(int sig)
Eric Andersencc8ed391999-10-05 16:24:54 +0000324{
Eric Andersenabc7d591999-10-19 00:27:50 +0000325 shutdown_system();
326 message(console, "Please stand by while rebooting the system.\r\n");
Eric Andersen2f6c04f1999-11-01 23:59:44 +0000327#ifndef DEBUG_INIT
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000328 reboot(RB_AUTOBOOT);
Eric Andersen2f6c04f1999-11-01 23:59:44 +0000329#endif
Eric Andersenabc7d591999-10-19 00:27:50 +0000330 exit(0);
Eric Andersencc8ed391999-10-05 16:24:54 +0000331}
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000332
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000333extern int init_main(int argc, char **argv)
Eric Andersen0460ff21999-10-25 23:32:44 +0000334{
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000335 int run_rc = TRUE;
Eric Andersen2f6c04f1999-11-01 23:59:44 +0000336 int wait_for_enter = FALSE;
Eric Andersenc7c41d31999-10-28 00:24:35 +0000337 pid_t pid1 = 0;
338 pid_t pid2 = 0;
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000339 struct stat statbuf;
Eric Andersen2f6c04f1999-11-01 23:59:44 +0000340 const char* const swap_on_cmd[] = { "/bin/swapon", "swapon", "-a", 0};
341 const char* const init_commands[] = { INITSCRIPT, INITSCRIPT, 0};
342 const char* const shell_commands[] = { SHELL, "-" SHELL, 0};
Eric Andersenc7c41d31999-10-28 00:24:35 +0000343 const char* const* tty0_commands = init_commands;
344 const char* const* tty1_commands = shell_commands;
Eric Andersen2f6c04f1999-11-01 23:59:44 +0000345#ifdef DEBUG_INIT
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000346 char *hello_msg_format =
Eric Andersen04579781999-10-29 23:12:50 +0000347 "init(%d) started: BusyBox v%s (%s) multi-call binary\r\n";
Eric Andersen2f6c04f1999-11-01 23:59:44 +0000348#else
349 char *hello_msg_format =
350 "init started: BusyBox v%s (%s) multi-call binary\r\n";
Eric Andersen7f1acfd1999-10-29 23:09:13 +0000351#endif
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000352 const char *no_memory =
353 "Sorry, your computer does not have enough memory.\r\n";
Eric Andersen0460ff21999-10-25 23:32:44 +0000354
Eric Andersen2f6c04f1999-11-01 23:59:44 +0000355 /* Set up sig handlers -- be sure to
356 * clear all of these in run() */
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000357 signal(SIGUSR1, halt_signal);
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000358 signal(SIGUSR2, reboot_signal);
359 signal(SIGINT, reboot_signal);
360 signal(SIGTERM, reboot_signal);
Eric Andersen0460ff21999-10-25 23:32:44 +0000361
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000362 /* Turn off rebooting via CTL-ALT-DEL -- we get a
363 * SIGINT on CAD so we can shut things down gracefully... */
Eric Andersen2f6c04f1999-11-01 23:59:44 +0000364#ifndef DEBUG_INIT
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000365 reboot(RB_DISABLE_CAD);
Eric Andersen7f1acfd1999-10-29 23:09:13 +0000366#endif
Eric Andersenc7c41d31999-10-28 00:24:35 +0000367 /* Figure out where the default console should be */
368 console_init();
Eric Andersen0460ff21999-10-25 23:32:44 +0000369
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000370 /* Close whatever files are open, and reset the console. */
371 close(0);
372 close(1);
373 close(2);
Eric Andersen7f1acfd1999-10-29 23:09:13 +0000374 set_term(0);
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000375 setsid();
Eric Andersencc8ed391999-10-05 16:24:54 +0000376
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000377 /* Make sure PATH is set to something sane */
378 if (getenv("PATH") == NULL)
379 putenv(PATH_DEFAULT);
Eric Andersencc8ed391999-10-05 16:24:54 +0000380
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000381 /* Hello world */
Eric Andersen7f1acfd1999-10-29 23:09:13 +0000382#ifndef DEBUG_INIT
Eric Andersenc1525e81999-10-29 00:07:31 +0000383 message(log, hello_msg_format, BB_VER, BB_BT);
Eric Andersen7f1acfd1999-10-29 23:09:13 +0000384 message(console, hello_msg_format, BB_VER, BB_BT);
385#else
386 message(log, hello_msg_format, getpid(), BB_VER, BB_BT);
387 message(console, hello_msg_format, getpid(), BB_VER, BB_BT);
388#endif
Eric Andersencc8ed391999-10-05 16:24:54 +0000389
Eric Andersenc7c41d31999-10-28 00:24:35 +0000390
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000391 /* Mount /proc */
Eric Andersen2f6c04f1999-11-01 23:59:44 +0000392 if (mount ("proc", "/proc", "proc", 0, 0) == 0) {
Eric Andersena7456061999-10-27 02:31:32 +0000393 message(log, "Mounting /proc: done.\n");
Eric Andersen7f1acfd1999-10-29 23:09:13 +0000394 message(console, "Mounting /proc: done.\n");
Eric Andersenc7c41d31999-10-28 00:24:35 +0000395 } else {
Eric Andersenc7c41d31999-10-28 00:24:35 +0000396 message(log, "Mounting /proc: failed!\n");
Eric Andersen7f1acfd1999-10-29 23:09:13 +0000397 message(console, "Mounting /proc: failed!\n");
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000398 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000399
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000400 /* Make sure there is enough memory to do something useful */
401 set_free_pages();
Eric Andersenc1525e81999-10-29 00:07:31 +0000402 if (mem_total() < 3500) {
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000403 int retval;
404 retval = stat("/etc/fstab", &statbuf);
405 if (retval) {
Eric Andersen7f1acfd1999-10-29 23:09:13 +0000406 message(console, "%s", no_memory);
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000407 while (1) {
408 sleep(1);
409 }
410 } else {
411 /* Try to turn on swap */
Eric Andersen2f6c04f1999-11-01 23:59:44 +0000412 waitfor(run(swap_on_cmd, log, FALSE));
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000413 if (mem_total() < 2000) {
Eric Andersen7f1acfd1999-10-29 23:09:13 +0000414 message(console, "%s", no_memory);
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000415 while (1) {
416 sleep(1);
Eric Andersenabc7d591999-10-19 00:27:50 +0000417 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000418 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000419 }
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000420 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000421
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000422 /* Check if we are supposed to be in single user mode */
423 if ( argc > 1 && (!strcmp(argv[1], "single") ||
424 !strcmp(argv[1], "-s") || !strcmp(argv[1], "1"))) {
425 run_rc = FALSE;
Eric Andersen2f6c04f1999-11-01 23:59:44 +0000426 wait_for_enter = TRUE;
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000427 tty0_commands = shell_commands;
Eric Andersena7456061999-10-27 02:31:32 +0000428 tty1_commands = shell_commands;
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000429 }
430
431 /* Make sure an init script exists before trying to run it */
432 if (run_rc == TRUE && stat(INITSCRIPT, &statbuf)) {
Eric Andersen2f6c04f1999-11-01 23:59:44 +0000433 wait_for_enter = TRUE;
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000434 tty0_commands = shell_commands;
435 tty1_commands = shell_commands;
436 }
437
438 /* Ok, now launch the rc script and/or prepare to
439 * start up some VTs if somebody hits enter...
440 */
441 for (;;) {
Eric Andersenc7c41d31999-10-28 00:24:35 +0000442 pid_t wpid;
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000443 int status;
444
445 if (pid1 == 0 && tty0_commands) {
Eric Andersen2f6c04f1999-11-01 23:59:44 +0000446 pid1 = run(tty0_commands, console, wait_for_enter);
Eric Andersencc8ed391999-10-05 16:24:54 +0000447 }
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000448 if (pid2 == 0 && tty1_commands) {
Eric Andersen2f6c04f1999-11-01 23:59:44 +0000449 pid2 = run(tty1_commands, second_terminal, TRUE);
Eric Andersencc8ed391999-10-05 16:24:54 +0000450 }
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000451 wpid = wait(&status);
Eric Andersena7456061999-10-27 02:31:32 +0000452 if (wpid > 0 ) {
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000453 message(log, "pid %d exited, status=%x.\n", wpid, status);
454 }
455 /* Don't respawn an init script if it exits */
456 if (run_rc == FALSE && wpid == pid1) {
457 pid1 = 0;
458 }
459 if (wpid == pid2) {
460 pid2 = 0;
461 }
462 sleep(1);
463 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000464}