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 CONSOLE "/dev/console" /* Logical system console */
45 #define VT_PRIMARY "/dev/tty0" /* Virtual console master */
46 #define VT_SECONDARY "/dev/tty1" /* Virtual console master */
47 #define VT_LOG "/dev/tty2" /* Virtual console master */
48 #define SHELL "/bin/sh" /* Default shell */
49 #define INITSCRIPT "/etc/init.d/rcS" /* Initscript. */
50 #define PATH_DEFAULT "PATH=/usr/local/sbin:/sbin:/bin:/usr/sbin:/usr/bin"
52 static char *console = CONSOLE;
53 //static char *first_terminal = "/dev/tty1";
54 static char *second_terminal = "/dev/tty2";
55 static char *log = "/dev/tty3";
59 /* try to open up the specified device */
60 int device_open(char *device, int mode)
64 mode = m | O_NONBLOCK;
66 /* Retry up to 5 times */
67 for (f = 0; f < 5; f++)
68 if ((fd = open(device, m)) >= 0)
72 /* Set original flags. */
74 fcntl(fd, F_SETFL, mode);
78 /* print a message to the specified device */
79 void message(char *device, char *fmt, ...)
84 if ((fd = device_open(device, O_WRONLY|O_NOCTTY|O_NDELAY)) >= 0) {
85 va_start(arguments, fmt);
86 vdprintf(fd, fmt, arguments);
90 fprintf(stderr, "Bummer, can't print: ");
91 va_start(arguments, fmt);
92 vfprintf(stderr, fmt, arguments);
98 /* Set terminal settings to reasonable defaults */
99 void set_term( int fd)
104 tty.c_cflag &= CBAUD|CBAUDEX|CSIZE|CSTOPB|PARENB|PARODD;
105 tty.c_cflag |= HUPCL|CLOCAL;
108 tty.c_cc[VQUIT] = 28;
109 tty.c_cc[VERASE] = 127;
110 tty.c_cc[VKILL] = 24;
114 tty.c_cc[VSTART] = 17;
115 tty.c_cc[VSTOP] = 19;
116 tty.c_cc[VSUSP] = 26;
118 tty.c_iflag = IGNPAR|ICRNL|IXON|IXANY;
119 tty.c_oflag = OPOST|ONLCR;
120 tty.c_lflag = ISIG|ICANON|ECHO|ECHOCTL|ECHOPRT|ECHOKE;
122 tcsetattr(fd, TCSANOW, &tty);
125 static int mem_total()
130 const char pattern[] = "MemTotal:";
132 f = fopen("/proc/meminfo", "r");
133 while (NULL != fgets(s, 79, f)) {
134 p = strstr(s, pattern);
137 return (atoi(p + strlen(pattern)));
143 static void set_free_pages()
148 f = fopen("/proc/sys/vm/freepages", "r");
152 f = fopen("/proc/sys/vm/freepages", "w");
153 fprintf(f, "30\t40\t50\n");
154 printf("\nIncreased /proc/sys/vm/freepages values to 30/40/50\n");
160 static void console_init()
164 int tried_devcons = 0;
165 int tried_vtmaster = 0;
168 fprintf(stderr, "entering console_init()\n");
169 if ((s = getenv("CONSOLE")) != NULL)
176 if ( stat(CONSOLE, &statbuf) && S_ISLNK(statbuf.st_mode)) {
177 fprintf(stderr, "Yikes! /dev/console does not exist or is a symlink.\n");
179 while ((fd = open(console, O_RDONLY | O_NONBLOCK)) < 0) {
180 if (!tried_devcons) {
185 if (!tried_vtmaster) {
187 console = VT_PRIMARY;
193 console = "/dev/null";
196 fprintf(stderr, "console=%s\n", console);
199 static int waitfor(int pid)
203 message(log, "Waiting for process %d.\n", pid);
204 while ((wpid = wait(&status)) != pid) {
206 message(log, "pid %d exited, status=%x.\n", wpid, status);
212 static pid_t run(const char * const* command,
213 char *terminal, int get_enter)
217 static const char press_enter[] =
218 "\nPlease press Enter to activate this console. ";
220 if ((pid = fork()) == 0) {
227 open(terminal, O_RDWR);
230 tcsetpgrp(0, getpgrp());
235 * Save memory by not exec-ing anything large (like a shell)
236 * before the user wants it. This is critical if swap is not
237 * enabled and the system has low memory. Generally this will
238 * be run on the second virtual console, and the first will
239 * be allowed to start a shell or whatever an init script
243 write(0, press_enter, sizeof(press_enter) - 1);
247 /* Log the process name and args */
249 /* Now run it. The new program will take over this PID,
250 * so nothing further in init.c should be run. */
251 message(log, "Executing '%s', pid(%d)\r\n", *command, getpid());
252 execvp(*command, (char**)command+1);
254 message(log, "Hmm. Trying as a script.\r\n");
255 /* If shell scripts are not executed, force the issue */
256 if (errno == ENOEXEC) {
261 for( i=0 ; i<16 && command[i]; i++)
262 args[3+i] = (char*)command[i];
264 execvp(*args, (char**)args+1);
266 message(log, "Could not execute '%s'\n", command);
273 static void shutdown_system(void)
275 const char* const swap_off_cmd[] = { "/bin/swapoff", "-a", 0};
276 const char* const umount_cmd[] = { "/bin/umount", "-a", "-n", 0};
278 message(console, "The system is going down NOW !!\r\n");
280 /* Allow Ctrl-Alt-Del to reboot system. */
281 reboot(RB_ENABLE_CAD);
283 /* Send signals to every process _except_ pid 1 */
284 message(console, "Sending SIGHUP to all processes.\r\n");
288 message(console, "Sending SIGKILL to all processes.\r\n");
291 waitfor(run( swap_off_cmd, console, 0));
292 waitfor(run( umount_cmd, console, 0));
294 if (get_kernel_revision() <= 2 * 65536 + 2 * 256 + 11) {
295 /* Removed bdflush call, kupdate in kernels >2.2.11 */
301 static void halt_signal(int sig)
305 "The system is halted. Press CTRL-ALT-DEL or turn off power\r\n");
306 reboot(RB_POWER_OFF);
310 static void reboot_signal(int sig)
313 message(console, "Please stand by while rebooting the system.\r\n");
318 extern int init_main(int argc, char **argv)
324 const char* const swap_on_cmd[] = { "/bin/swapon", " -a ", 0};
325 const char* const init_commands[] = { SHELL, " -c", " exec ", INITSCRIPT, 0};
326 const char* const shell_commands[] = { SHELL, " -", 0};
327 const char* const* tty0_commands = init_commands;
328 const char* const* tty1_commands = shell_commands;
329 char *hello_msg_format =
330 "init started: BusyBox v%s (%s) multi-call binary\r\n";
331 const char *no_memory =
332 "Sorry, your computer does not have enough memory.\r\n";
334 /* Set up sig handlers */
335 signal(SIGUSR1, halt_signal);
336 signal(SIGSEGV, halt_signal);
337 signal(SIGPWR, halt_signal);
338 signal(SIGALRM, halt_signal);
339 signal(SIGHUP, halt_signal);
340 signal(SIGUSR2, reboot_signal);
341 signal(SIGINT, reboot_signal);
342 signal(SIGTERM, reboot_signal);
344 /* Turn off rebooting via CTL-ALT-DEL -- we get a
345 * SIGINT on CAD so we can shut things down gracefully... */
346 reboot(RB_DISABLE_CAD);
348 /* Figure out where the default console should be */
351 /* Close whatever files are open, and reset the console. */
358 /* Make sure PATH is set to something sane */
359 if (getenv("PATH") == NULL)
360 putenv(PATH_DEFAULT);
363 message(log, hello_msg_format, BB_VER, BB_BT);
364 fprintf(stderr, hello_msg_format, BB_VER, BB_BT);
368 if (mount("/proc", "/proc", "proc", 0, 0) == 0) {
369 fprintf(stderr, "Mounting /proc: done.\n");
370 message(log, "Mounting /proc: done.\n");
372 fprintf(stderr, "Mounting /proc: failed!\n");
373 message(log, "Mounting /proc: failed!\n");
376 /* Make sure there is enough memory to do something useful */
378 if (mem_total() < 3500) {
380 retval = stat("/etc/fstab", &statbuf);
382 fprintf(stderr, "%s", no_memory);
387 /* Try to turn on swap */
388 waitfor(run(swap_on_cmd, console, 0));
389 if (mem_total() < 2000) {
390 fprintf(stderr, "%s", no_memory);
398 /* Check if we are supposed to be in single user mode */
399 if ( argc > 1 && (!strcmp(argv[1], "single") ||
400 !strcmp(argv[1], "-s") || !strcmp(argv[1], "1"))) {
402 tty0_commands = shell_commands;
403 tty1_commands = shell_commands;
406 /* Make sure an init script exists before trying to run it */
407 if (run_rc == TRUE && stat(INITSCRIPT, &statbuf)) {
408 tty0_commands = shell_commands;
409 tty1_commands = shell_commands;
412 /* Ok, now launch the rc script and/or prepare to
413 * start up some VTs if somebody hits enter...
419 if (pid1 == 0 && tty0_commands) {
420 pid1 = run(tty0_commands, console, 1);
422 if (pid2 == 0 && tty1_commands) {
423 pid2 = run(tty1_commands, second_terminal, 1);
425 wpid = wait(&status);
427 message(log, "pid %d exited, status=%x.\n", wpid, status);
429 /* Don't respawn an init script if it exits */
430 if (run_rc == FALSE && wpid == pid1) {