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 #define VT_CONSOLE "/dev/console" /* Logical system console */
45 #define VT_PRIMARY "/dev/tty1" /* Primary virtual console */
46 #define VT_SECONDARY "/dev/tty2" /* Virtual console */
47 #define VT_LOG "/dev/tty3" /* Virtual console */
48 #define SERIAL_CON0 "/dev/ttyS0" /* Primary serial console */
49 #define SERIAL_CON1 "/dev/ttyS1" /* Serial console */
50 #define SHELL "/bin/sh" /* Default shell */
51 #define INITSCRIPT "/etc/init.d/rcS" /* Initscript. */
52 #define PATH_DEFAULT "PATH=/usr/local/sbin:/sbin:/bin:/usr/sbin:/usr/bin"
56 static char *console = VT_CONSOLE;
57 static char *second_console = VT_SECONDARY;
58 static char *log = VT_LOG;
59 static int kernel_version = 0;
62 /* try to open up the specified device */
63 int device_open(char *device, int mode)
67 m = mode | O_NONBLOCK;
69 /* Retry up to 5 times */
70 for (f = 0; f < 5; f++)
71 if ((fd = open(device, m)) >= 0)
75 /* Reset original flags. */
77 fcntl(fd, F_SETFL, mode);
81 /* print a message to the specified device:
82 * device may be bitwise-or'd from LOG | CONSOLE */
83 void message(int device, char *fmt, ...)
89 /* Take full control of the log tty, and never close it.
90 * It's mine, all mine! Muhahahaha! */
92 if ((log_fd = device_open(log, O_RDWR|O_NDELAY)) < 0) {
94 fprintf(stderr, "Bummer, can't write to log on %s!\r\n", log);
100 if ( (device & LOG) && (log_fd != -1) ) {
101 va_start(arguments, fmt);
102 vdprintf(log_fd, fmt, arguments);
105 if (device & CONSOLE) {
106 if ((fd = device_open(console, O_WRONLY|O_NOCTTY|O_NDELAY)) >= 0) {
107 va_start(arguments, fmt);
108 vdprintf(fd, fmt, arguments);
112 fprintf(stderr, "Bummer, can't print: ");
113 va_start(arguments, fmt);
114 vfprintf(stderr, fmt, arguments);
122 /* Set terminal settings to reasonable defaults */
123 void set_term( int fd)
126 static const char control_characters[] = {
127 '\003', '\034', '\177', '\030', '\004', '\0',
128 '\1', '\0', '\021', '\023', '\032', '\0', '\022',
129 '\017', '\027', '\026', '\0'
134 /* Make it be sane */
135 tty.c_cflag &= CBAUD|CBAUDEX|CSIZE|CSTOPB|PARENB|PARODD;
136 tty.c_cflag |= HUPCL|CLOCAL;
139 tty.c_iflag = IGNPAR|ICRNL|IXON|IXOFF|IXANY;
141 /* use line dicipline 0 */
145 tty.c_oflag = OPOST|ONLCR;
148 tty.c_lflag = ISIG|ICANON|ECHO|ECHOE|ECHOK|ECHOCTL|ECHOPRT|ECHOKE|IEXTEN;
151 memcpy(tty.c_cc, control_characters, sizeof(control_characters));
153 tcsetattr(fd, TCSANOW, &tty);
156 /* How much memory does this machine have? */
157 static int mem_total()
160 char *p = "/proc/meminfo";
162 const char pattern[] = "MemTotal:";
164 if ((f = fopen(p, "r")) < 0) {
165 message(LOG, "Error opening %s: %s\n", p, strerror( errno));
168 while (NULL != fgets(s, 79, f)) {
169 p = strstr(s, pattern);
172 return (atoi(p + strlen(pattern)));
178 static void console_init()
181 int tried_devcons = 0;
182 int tried_vtprimary = 0;
185 if ((s = getenv("CONSOLE")) != NULL) {
187 /* Apparently the sparc does wierd things... */
188 #if defined (__sparc__)
189 if (strncmp( s, "/dev/tty", 8 )==0) {
200 console = VT_CONSOLE;
204 while ((fd = open(console, O_RDONLY | O_NONBLOCK)) < 0) {
205 /* Can't open selected console -- try /dev/console */
206 if (!tried_devcons) {
208 console = VT_CONSOLE;
211 /* Can't open selected console -- try vt1 */
212 if (!tried_vtprimary) {
214 console = VT_PRIMARY;
220 /* Perhaps we should panic here? */
221 console = "/dev/null";
224 message(LOG, "console=%s\n", console );
227 static int waitfor(int pid)
231 message(LOG, "Waiting for process %d.\n", pid);
232 while ((wpid = wait(&status)) != pid) {
234 message(LOG, "pid %d exited, status=0x%x.\n", wpid, status);
240 static pid_t run(const char * const* command,
241 char *terminal, int get_enter)
245 const char * const* cmd = command+1;
246 static const char press_enter[] =
247 "\nPlease press Enter to activate this console. ";
249 if ((pid = fork()) == 0) {
256 /* Reset signal handlers set for parent process */
257 signal(SIGUSR1, SIG_DFL);
258 signal(SIGUSR2, SIG_DFL);
259 signal(SIGINT, SIG_DFL);
260 signal(SIGTERM, SIG_DFL);
262 if ((fd = device_open(terminal, O_RDWR)) < 0) {
263 message(LOG, "Bummer, can't open %s\r\n", terminal);
268 tcsetpgrp(0, getpgrp());
271 if (get_enter==TRUE) {
273 * Save memory by not exec-ing anything large (like a shell)
274 * before the user wants it. This is critical if swap is not
275 * enabled and the system has low memory. Generally this will
276 * be run on the second virtual console, and the first will
277 * be allowed to start a shell or whatever an init script
281 message(LOG, "Waiting for enter to start '%s' (pid %d, console %s)\r\n",
282 *cmd, getpid(), terminal );
283 write(1, press_enter, sizeof(press_enter) - 1);
287 /* Log the process name and args */
288 message(LOG, "Starting pid %d, console %s: '", getpid(), terminal);
289 while ( *cmd) message(LOG, "%s ", *cmd++);
290 message(LOG, "'\r\n");
292 /* Now run it. The new program will take over this PID,
293 * so nothing further in init.c should be run. */
294 execvp(*command, (char**)command+1);
296 message(LOG, "Bummer, could not run '%s'\n", command);
302 /* Make sure there is enough memory to do something useful. *
303 * Calls swapon if needed so be sure /proc is mounted. */
304 static void check_memory()
307 const char* const swap_on_cmd[] =
308 { "/bin/swapon", "swapon", "-a", 0};
310 if (mem_total() > 3500)
313 if (stat("/etc/fstab", &statbuf) == 0) {
314 /* Try to turn on swap */
315 waitfor(run(swap_on_cmd, log, FALSE));
316 if (mem_total() < 3500)
323 message(CONSOLE, "Sorry, your computer does not have enough memory.\r\n");
327 static void shutdown_system(void)
329 const char* const swap_off_cmd[] = { "swapoff", "swapoff", "-a", 0};
330 const char* const umount_cmd[] = { "umount", "umount", "-a", "-n", 0};
333 /* Allow Ctrl-Alt-Del to reboot system. */
334 reboot(RB_ENABLE_CAD);
336 message(CONSOLE, "The system is going down NOW !!\r\n");
338 /* Send signals to every process _except_ pid 1 */
339 message(CONSOLE, "Sending SIGHUP to all processes.\r\n");
345 message(CONSOLE, "Sending SIGKILL to all processes.\r\n");
350 waitfor(run( swap_off_cmd, console, FALSE));
351 waitfor(run( umount_cmd, console, FALSE));
353 if (kernel_version > 0 && kernel_version <= 2 * 65536 + 2 * 256 + 11) {
354 /* bdflush, kupdate not needed for kernels >2.2.11 */
355 message(CONSOLE, "Flushing buffers.\r\n");
361 static void halt_signal(int sig)
365 "The system is halted. Press CTRL-ALT-DEL or turn off power\r\n");
368 reboot(RB_HALT_SYSTEM);
369 //reboot(RB_POWER_OFF);
374 static void reboot_signal(int sig)
377 message(CONSOLE, "Please stand by while rebooting the system.\r\n");
385 extern int init_main(int argc, char **argv)
388 int wait_for_enter = TRUE;
392 const char* const init_commands[] = { INITSCRIPT, INITSCRIPT, 0};
393 const char* const shell_commands[] = { SHELL, "-" SHELL, 0};
394 const char* const* tty0_commands = shell_commands;
395 const char* const* tty1_commands = shell_commands;
397 char *hello_msg_format =
398 "init(%d) started: BusyBox v%s (%s) multi-call binary\r\n";
400 char *hello_msg_format =
401 "init started: BusyBox v%s (%s) multi-call binary\r\n";
405 /* Check if we are supposed to be in single user mode */
406 if ( argc > 1 && (!strcmp(argv[1], "single") ||
407 !strcmp(argv[1], "-s") || !strcmp(argv[1], "1"))) {
412 /* Set up sig handlers -- be sure to
413 * clear all of these in run() */
414 signal(SIGUSR1, halt_signal);
415 signal(SIGUSR2, reboot_signal);
416 signal(SIGINT, reboot_signal);
417 signal(SIGTERM, reboot_signal);
419 /* Turn off rebooting via CTL-ALT-DEL -- we get a
420 * SIGINT on CAD so we can shut things down gracefully... */
422 reboot(RB_DISABLE_CAD);
425 /* Figure out where the default console should be */
428 /* Close whatever files are open, and reset the console. */
435 /* Make sure PATH is set to something sane */
436 putenv(PATH_DEFAULT);
441 message(CONSOLE|LOG, hello_msg_format, BB_VER, BB_BT);
443 message(CONSOLE|LOG, hello_msg_format, getpid(), BB_VER, BB_BT);
448 if (mount ("proc", "/proc", "proc", 0, 0) == 0) {
449 message(CONSOLE|LOG, "Mounting /proc: done.\n");
450 kernel_version = get_kernel_revision();
452 message(CONSOLE|LOG, "Mounting /proc: failed!\n");
454 /* Make sure there is enough memory to do something useful. */
458 /* Make sure an init script exists before trying to run it */
459 if (run_rc == TRUE && stat(INITSCRIPT, &statbuf)==0) {
460 wait_for_enter = FALSE;
461 tty0_commands = init_commands;
465 /* Ok, now launch the rc script and/or prepare to
466 * start up some VTs if somebody hits enter...
472 if (pid1 == 0 && tty0_commands) {
473 pid1 = run(tty0_commands, console, wait_for_enter);
475 if (pid2 == 0 && tty1_commands) {
476 pid2 = run(tty1_commands, second_console, TRUE);
478 wpid = wait(&status);
480 message(LOG, "pid %d exited, status=%x.\n", wpid, status);
482 /* Don't respawn init script if it exits */
484 if (run_rc == FALSE) {
488 /* Turn this on to start a shell on the console if the init script exits.... */
492 tty0_commands=shell_commands;