2 * Mini init implementation for busybox
5 * Copyright (C) 1995, 1996 by Bruce Perens <bruce@pixar.com>.
6 * Adjusted by so many folks, it's impossible to keep track.
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.
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.
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
32 #include <sys/types.h>
33 #include <sys/fcntl.h>
36 #include <sys/mount.h>
37 #include <sys/reboot.h>
38 #include <sys/kdaemon.h>
39 #include <sys/sysmacros.h>
40 #include <linux/serial.h> /* for serial_struct */
41 #include <sys/vt.h> /* for vt_stat */
42 #include <sys/ioctl.h>
44 /* Turn this on to debug init so it won't reboot when killed */
47 #define CONSOLE "/dev/console" /* Logical system console */
48 #define VT_PRIMARY "/dev/tty0" /* Primary virtual console */
49 #define VT_SECONDARY "/dev/tty1" /* Virtual console */
50 #define VT_LOG "/dev/tty2" /* Virtual console */
51 #define SERIAL_CON0 "/dev/ttyS0" /* Primary serial console */
52 #define SERIAL_CON1 "/dev/ttyS1" /* Serial console */
53 #define SHELL "/bin/sh" /* Default shell */
54 #define INITSCRIPT "/etc/init.d/rcS" /* Initscript. */
55 #define PATH_DEFAULT "PATH=/usr/local/sbin:/sbin:/bin:/usr/sbin:/usr/bin"
57 static char *console = VT_PRIMARY;
58 static char *second_terminal = VT_SECONDARY;
59 static char *log = "/dev/tty3";
63 /* try to open up the specified device */
64 int device_open(char *device, int mode)
68 m = mode | O_NONBLOCK;
70 /* Retry up to 5 times */
71 for (f = 0; f < 5; f++)
72 if ((fd = open(device, m)) >= 0)
76 /* Set original flags. */
78 fcntl(fd, F_SETFL, mode);
82 /* print a message to the specified device */
83 void message(char *device, char *fmt, ...)
88 if ((fd = device_open(device, O_WRONLY|O_NOCTTY|O_NDELAY)) >= 0) {
89 va_start(arguments, fmt);
90 vdprintf(fd, fmt, arguments);
94 fprintf(stderr, "Bummer, can't print: ");
95 va_start(arguments, fmt);
96 vfprintf(stderr, fmt, arguments);
102 /* Set terminal settings to reasonable defaults */
103 void set_term( int fd)
106 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'
115 tty.c_iflag = IGNPAR|ICRNL|IXON|IXOFF|IXANY;
117 /* use line dicipline 0 */
121 tty.c_oflag = OPOST|ONLCR;
124 tty.c_lflag = ISIG|ICANON|ECHO|ECHOE|ECHOK|ECHOCTL|ECHOPRT|ECHOKE|IEXTEN;
127 memcpy(tty.c_cc, control_characters, sizeof(control_characters));
129 tcsetattr(fd, TCSANOW, &tty);
132 /* How much memory does this machine have? */
133 static int mem_total()
136 char *p = "/proc/meminfo";
138 const char pattern[] = "MemTotal:";
140 if ((f = fopen(p, "r")) < 0) {
141 message(log, "Error opening %s: %s\n", p, strerror( errno));
144 while (NULL != fgets(s, 79, f)) {
145 p = strstr(s, pattern);
148 return (atoi(p + strlen(pattern)));
154 static void console_init()
157 int tried_devcons = 0;
158 int tried_vtprimary = 0;
161 if ((s = getenv("CONSOLE")) != NULL) {
163 /* Apparently the sparc does wierd things... */
164 #if defined (__sparc__)
165 if (strncmp( s, "/dev/tty", 8 )==0) {
176 console = VT_PRIMARY;
180 while ((fd = open(console, O_RDONLY | O_NONBLOCK)) < 0) {
181 /* Can't open selected console -- try vt1 */
182 if (!tried_vtprimary) {
184 console = VT_PRIMARY;
187 /* Can't open selected console -- try /dev/console */
188 if (!tried_devcons) {
196 /* Perhaps we should panic here? */
197 console = "/dev/null";
200 message(log, "console=%s\n", console );
203 static int waitfor(int pid)
207 message(log, "Waiting for process %d.\n", pid);
208 while ((wpid = wait(&status)) != pid) {
210 message(log, "pid %d exited, status=%x.\n", wpid, status);
216 static pid_t run(const char * const* command,
217 char *terminal, int get_enter)
221 const char * const* cmd = command+1;
222 static const char press_enter[] =
223 "\nPlease press Enter to activate this console. ";
225 if ((pid = fork()) == 0) {
232 /* 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);
238 if ((fd = device_open(terminal, O_RDWR)) < 0) {
239 message(log, "Bummer, can't open %s\r\n", terminal);
244 tcsetpgrp(0, getpgrp());
247 if (get_enter==TRUE) {
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
257 write(1, press_enter, sizeof(press_enter) - 1);
261 /* Log the process name and args */
262 message(log, "Starting pid(%d): '", getpid());
263 while ( *cmd) message(log, "%s ", *cmd++);
264 message(log, "'\r\n");
266 /* Now run it. The new program will take over this PID,
267 * so nothing further in init.c should be run. */
268 execvp(*command, (char**)command+1);
270 message(log, "Bummer, could not run '%s'\n", command);
276 /* Make sure there is enough memory to do something useful. *
277 * Calls swapon if needed so be sure /proc is mounted. */
278 static void check_memory()
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";
286 if (mem_total() > 3500)
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)
299 message(console, "%s", no_memory);
303 static void shutdown_system(void)
305 const char* const swap_off_cmd[] = { "swapoff", "swapoff", "-a", 0};
306 const char* const umount_cmd[] = { "umount", "umount", "-a", "-n", 0};
308 message(console, "The system is going down NOW !!\r\n");
311 /* Allow Ctrl-Alt-Del to reboot system. */
312 reboot(RB_ENABLE_CAD);
314 /* Send signals to every process _except_ pid 1 */
315 message(console, "Sending SIGHUP to all processes.\r\n");
321 message(console, "Sending SIGKILL to all processes.\r\n");
326 waitfor(run( swap_off_cmd, log, FALSE));
327 waitfor(run( umount_cmd, log, FALSE));
329 if (get_kernel_revision() <= 2 * 65536 + 2 * 256 + 11) {
330 /* Removed bdflush call, kupdate in kernels >2.2.11 */
336 static void halt_signal(int sig)
340 "The system is halted. Press CTRL-ALT-DEL or turn off power\r\n");
342 reboot(RB_POWER_OFF);
347 static void reboot_signal(int sig)
350 message(console, "Please stand by while rebooting the system.\r\n");
357 extern int init_main(int argc, char **argv)
360 int wait_for_enter = TRUE;
364 const char* const init_commands[] = { INITSCRIPT, INITSCRIPT, 0};
365 const char* const shell_commands[] = { SHELL, "-" SHELL, 0};
366 const char* const* tty0_commands = shell_commands;
367 const char* const* tty1_commands = shell_commands;
369 char *hello_msg_format =
370 "init(%d) started: BusyBox v%s (%s) multi-call binary\r\n";
372 char *hello_msg_format =
373 "init started: BusyBox v%s (%s) multi-call binary\r\n";
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"))) {
384 /* Set up sig handlers -- be sure to
385 * clear all of these in run() */
386 signal(SIGUSR1, halt_signal);
387 signal(SIGUSR2, reboot_signal);
388 signal(SIGINT, reboot_signal);
389 signal(SIGTERM, reboot_signal);
391 /* Turn off rebooting via CTL-ALT-DEL -- we get a
392 * SIGINT on CAD so we can shut things down gracefully... */
394 reboot(RB_DISABLE_CAD);
397 /* Figure out where the default console should be */
400 /* Close whatever files are open, and reset the console. */
407 /* Make sure PATH is set to something sane */
408 putenv(PATH_DEFAULT);
413 message(log, hello_msg_format, BB_VER, BB_BT);
414 message(console, hello_msg_format, BB_VER, BB_BT);
416 message(log, hello_msg_format, getpid(), BB_VER, BB_BT);
417 message(console, hello_msg_format, getpid(), BB_VER, BB_BT);
422 if (mount ("proc", "/proc", "proc", 0, 0) == 0) {
423 message(log, "Mounting /proc: done.\n");
424 message(console, "Mounting /proc: done.\n");
426 message(log, "Mounting /proc: failed!\n");
427 message(console, "Mounting /proc: failed!\n");
430 /* Make sure there is enough memory to do something useful. */
434 /* Make sure an init script exists before trying to run it */
435 if (run_rc == TRUE && stat(INITSCRIPT, &statbuf)==0) {
436 wait_for_enter = FALSE;
437 tty0_commands = init_commands;
441 /* Ok, now launch the rc script and/or prepare to
442 * start up some VTs if somebody hits enter...
448 if (pid1 == 0 && tty0_commands) {
449 pid1 = run(tty0_commands, console, wait_for_enter);
451 if (pid2 == 0 && tty1_commands) {
452 pid2 = run(tty1_commands, second_terminal, TRUE);
454 wpid = wait(&status);
456 message(log, "pid %d exited, status=%x.\n", wpid, status);
460 if (run_rc == TRUE) {
461 /* Don't respawn init script if it exits. */
464 tty0_commands=shell_commands;