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! */
93 /* don't even try to log, because there is no such console */
95 /* log to main console instead */
98 else if ((log_fd = device_open(log, O_RDWR|O_NDELAY)) < 0) {
100 fprintf(stderr, "Bummer, can't write to log on %s!\r\n", log);
106 if ( (device & LOG) && (log_fd >= 0) ) {
107 va_start(arguments, fmt);
108 vdprintf(log_fd, fmt, arguments);
111 if (device & CONSOLE) {
112 if ((fd = device_open(console, O_WRONLY|O_NOCTTY|O_NDELAY)) >= 0) {
113 va_start(arguments, fmt);
114 vdprintf(fd, fmt, arguments);
118 fprintf(stderr, "Bummer, can't print: ");
119 va_start(arguments, fmt);
120 vfprintf(stderr, fmt, arguments);
128 /* Set terminal settings to reasonable defaults */
129 void set_term( int fd)
132 static const char control_characters[] = {
133 '\003', '\034', '\177', '\030', '\004', '\0',
134 '\1', '\0', '\021', '\023', '\032', '\0', '\022',
135 '\017', '\027', '\026', '\0'
140 /* Make it be sane */
141 tty.c_cflag &= CBAUD|CBAUDEX|CSIZE|CSTOPB|PARENB|PARODD;
142 tty.c_cflag |= HUPCL|CLOCAL;
145 tty.c_iflag = IGNPAR|ICRNL|IXON|IXOFF|IXANY;
147 /* use line dicipline 0 */
151 tty.c_oflag = OPOST|ONLCR;
154 tty.c_lflag = ISIG|ICANON|ECHO|ECHOE|ECHOK|ECHOCTL|ECHOPRT|ECHOKE|IEXTEN;
157 memcpy(tty.c_cc, control_characters, sizeof(control_characters));
159 tcsetattr(fd, TCSANOW, &tty);
162 /* How much memory does this machine have? */
163 static int mem_total()
166 char *p = "/proc/meminfo";
168 const char pattern[] = "MemTotal:";
170 if ((f = fopen(p, "r")) < 0) {
171 message(LOG, "Error opening %s: %s\n", p, strerror( errno));
174 while (NULL != fgets(s, 79, f)) {
175 p = strstr(s, pattern);
178 return (atoi(p + strlen(pattern)));
184 static void console_init()
187 int tried_devcons = 0;
188 int tried_vtprimary = 0;
189 struct serial_struct sr;
192 if ((s = getenv("CONSOLE")) != NULL) {
196 /* sparc kernel supports console=tty[ab] parameter which is also
197 * passed to init, so catch it here */
198 else if ((s = getenv("console")) != NULL) {
199 /* remap tty[ab] to /dev/ttyS[01] */
200 if (strcmp( s, "ttya" )==0)
201 console = SERIAL_CON0;
202 else if (strcmp( s, "ttyb" )==0)
203 console = SERIAL_CON1;
208 static char the_console[13];
210 console = the_console;
211 /* 2.2 kernels: identify the real console backend and try to use it */
212 if (ioctl(0,TIOCGSERIAL,&sr) == 0) {
213 /* this is a serial console */
214 snprintf( the_console, sizeof the_console, "/dev/ttyS%d", sr.line );
216 else if (ioctl(0, VT_GETSTATE, &vt) == 0) {
217 /* this is linux virtual tty */
218 snprintf( the_console, sizeof the_console, "/dev/tty%d", vt.v_active );
220 console = VT_CONSOLE;
225 while ((fd = open(console, O_RDONLY | O_NONBLOCK)) < 0) {
226 /* Can't open selected console -- try /dev/console */
227 if (!tried_devcons) {
229 console = VT_CONSOLE;
232 /* Can't open selected console -- try vt1 */
233 if (!tried_vtprimary) {
235 console = VT_PRIMARY;
241 /* Perhaps we should panic here? */
242 console = "/dev/null";
244 /* check for serial console and disable logging to tty3 & running a
246 if (ioctl(0,TIOCGSERIAL,&sr) == 0) {
248 second_console = NULL;
252 message(LOG, "console=%s\n", console );
255 static int waitfor(int pid)
259 message(LOG, "Waiting for process %d.\n", pid);
260 while ((wpid = wait(&status)) != pid) {
262 message(LOG, "pid %d exited, status=0x%x.\n", wpid, status);
268 static pid_t run(const char * const* command,
269 char *terminal, int get_enter)
273 const char * const* cmd = command+1;
274 static const char press_enter[] =
275 "\nPlease press Enter to activate this console. ";
277 if ((pid = fork()) == 0) {
284 /* Reset signal handlers set for parent process */
285 signal(SIGUSR1, SIG_DFL);
286 signal(SIGUSR2, SIG_DFL);
287 signal(SIGINT, SIG_DFL);
288 signal(SIGTERM, SIG_DFL);
290 if ((fd = device_open(terminal, O_RDWR)) < 0) {
291 message(LOG, "Bummer, can't open %s\r\n", terminal);
296 tcsetpgrp(0, getpgrp());
299 if (get_enter==TRUE) {
301 * Save memory by not exec-ing anything large (like a shell)
302 * before the user wants it. This is critical if swap is not
303 * enabled and the system has low memory. Generally this will
304 * be run on the second virtual console, and the first will
305 * be allowed to start a shell or whatever an init script
309 message(LOG, "Waiting for enter to start '%s' (pid %d, console %s)\r\n",
310 *cmd, getpid(), terminal );
311 write(1, press_enter, sizeof(press_enter) - 1);
315 /* Log the process name and args */
316 message(LOG, "Starting pid %d, console %s: '", getpid(), terminal);
317 while ( *cmd) message(LOG, "%s ", *cmd++);
318 message(LOG, "'\r\n");
320 /* Now run it. The new program will take over this PID,
321 * so nothing further in init.c should be run. */
322 execvp(*command, (char**)command+1);
324 message(LOG, "Bummer, could not run '%s'\n", command);
330 /* Make sure there is enough memory to do something useful. *
331 * Calls swapon if needed so be sure /proc is mounted. */
332 static void check_memory()
335 const char* const swap_on_cmd[] =
336 { "/bin/swapon", "swapon", "-a", 0};
338 if (mem_total() > 3500)
341 if (stat("/etc/fstab", &statbuf) == 0) {
342 /* Try to turn on swap */
343 waitfor(run(swap_on_cmd, log, FALSE));
344 if (mem_total() < 3500)
351 message(CONSOLE, "Sorry, your computer does not have enough memory.\r\n");
355 static void shutdown_system(void)
357 const char* const swap_off_cmd[] = { "swapoff", "swapoff", "-a", 0};
358 const char* const umount_cmd[] = { "umount", "umount", "-a", "-n", 0};
361 /* Allow Ctrl-Alt-Del to reboot system. */
362 reboot(RB_ENABLE_CAD);
364 message(CONSOLE, "The system is going down NOW !!\r\n");
366 /* Send signals to every process _except_ pid 1 */
367 message(CONSOLE, "Sending SIGHUP to all processes.\r\n");
373 message(CONSOLE, "Sending SIGKILL to all processes.\r\n");
378 waitfor(run( swap_off_cmd, console, FALSE));
379 waitfor(run( umount_cmd, console, FALSE));
381 if (kernel_version > 0 && kernel_version <= 2 * 65536 + 2 * 256 + 11) {
382 /* bdflush, kupdate not needed for kernels >2.2.11 */
383 message(CONSOLE, "Flushing buffers.\r\n");
389 static void halt_signal(int sig)
393 "The system is halted. Press CTRL-ALT-DEL or turn off power\r\n");
396 reboot(RB_HALT_SYSTEM);
397 //reboot(RB_POWER_OFF);
402 static void reboot_signal(int sig)
405 message(CONSOLE, "Please stand by while rebooting the system.\r\n");
413 extern int init_main(int argc, char **argv)
416 int wait_for_enter = TRUE;
420 const char* const rc_script_command[] = { INITSCRIPT, INITSCRIPT, 0};
421 const char* const shell_command[] = { SHELL, "-" SHELL, 0};
422 const char* const* tty0_command = shell_command;
423 const char* const* tty1_command = shell_command;
424 #ifdef BB_CONSOLE_CMD_IF_RC_SCRIPT_EXITS
425 const char* const rc_exit_command[] = BB_CONSOLE_CMD_IF_RC_SCRIPT_EXITS;
429 char *hello_msg_format =
430 "init(%d) started: BusyBox v%s (%s) multi-call binary\r\n";
432 char *hello_msg_format =
433 "init started: BusyBox v%s (%s) multi-call binary\r\n";
439 usage( "init\n\nInit is the parent of all processes.\n\n"
440 "This version of init is designed to be run only by the kernel\n");
444 /* Check if we are supposed to be in single user mode */
445 if ( argc > 1 && (!strcmp(argv[1], "single") ||
446 !strcmp(argv[1], "-s") || !strcmp(argv[1], "1"))) {
451 /* Set up sig handlers -- be sure to
452 * clear all of these in run() */
453 signal(SIGUSR1, halt_signal);
454 signal(SIGUSR2, reboot_signal);
455 signal(SIGINT, reboot_signal);
456 signal(SIGTERM, reboot_signal);
458 /* Turn off rebooting via CTL-ALT-DEL -- we get a
459 * SIGINT on CAD so we can shut things down gracefully... */
461 reboot(RB_DISABLE_CAD);
464 /* Figure out where the default console should be */
467 /* Close whatever files are open, and reset the console. */
474 /* Make sure PATH is set to something sane */
475 putenv(PATH_DEFAULT);
480 message(CONSOLE|LOG, hello_msg_format, BB_VER, BB_BT);
482 message(CONSOLE|LOG, hello_msg_format, getpid(), BB_VER, BB_BT);
487 if (mount ("proc", "/proc", "proc", 0, 0) == 0) {
488 message(CONSOLE|LOG, "Mounting /proc: done.\n");
489 kernel_version = get_kernel_revision();
491 message(CONSOLE|LOG, "Mounting /proc: failed!\n");
493 /* Make sure there is enough memory to do something useful. */
497 /* Make sure an init script exists before trying to run it */
498 if (run_rc == TRUE && stat(INITSCRIPT, &statbuf)==0) {
499 wait_for_enter = FALSE;
500 tty0_command = rc_script_command;
504 /* Ok, now launch the rc script and/or prepare to
505 * start up some VTs if somebody hits enter...
511 if (pid1 == 0 && tty0_command) {
512 pid1 = run(tty0_command, console, wait_for_enter);
514 if (pid2 == 0 && tty1_command && second_console) {
515 pid2 = run(tty1_command, second_console, TRUE);
517 wpid = wait(&status);
519 message(LOG, "pid %d exited, status=%x.\n", wpid, status);
521 /* Don't respawn init script if it exits */
523 if (run_rc == FALSE) {
526 #ifdef BB_CONSOLE_CMD_IF_RC_SCRIPT_EXITS
531 tty0_command=rc_exit_command;