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" /* 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 */
52 #define INITSCRIPT "/etc/init.d/rcS" /* Initscript. */
53 #define PATH_DEFAULT "PATH=/usr/local/sbin:/sbin:/bin:/usr/sbin:/usr/bin"
55 static char *console = CONSOLE;
56 //static char *first_terminal = "/dev/tty1";
57 static char *second_terminal = "/dev/tty2";
58 static char *log = "/dev/tty3";
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 /* Set original flags. */
77 fcntl(fd, F_SETFL, mode);
81 /* print a message to the specified device */
82 void message(char *device, char *fmt, ...)
87 if ((fd = device_open(device, O_WRONLY|O_NOCTTY|O_NDELAY)) >= 0) {
88 va_start(arguments, fmt);
89 vdprintf(fd, fmt, arguments);
93 fprintf(stderr, "Bummer, can't print: ");
94 va_start(arguments, fmt);
95 vfprintf(stderr, fmt, arguments);
101 /* Set terminal settings to reasonable defaults */
102 void set_term( int fd)
105 static const char control_characters[] = {
106 '\003', '\034', '\177', '\025', '\004', '\0',
107 '\1', '\0', '\021', '\023', '\032', '\0', '\022',
108 '\017', '\027', '\026', '\0'
114 tty.c_iflag = IGNPAR|ICRNL|IXON|IXOFF|IXANY;
116 /* use lineo dicipline 0 */
120 tty.c_oflag = OPOST|ONLCR;
123 tty.c_lflag = ISIG|ICANON|ECHO|ECHOE|ECHOK|ECHOCTL|ECHOPRT|ECHOKE|IEXTEN;
126 memcpy(tty.c_cc, control_characters, sizeof(control_characters));
128 tcsetattr(fd, TCSANOW, &tty);
131 static int mem_total()
136 const char pattern[] = "MemTotal:";
138 f = fopen("/proc/meminfo", "r");
139 while (NULL != fgets(s, 79, f)) {
140 p = strstr(s, pattern);
143 return (atoi(p + strlen(pattern)));
149 static void set_free_pages()
154 f = fopen("/proc/sys/vm/freepages", "r");
158 f = fopen("/proc/sys/vm/freepages", "w");
159 fprintf(f, "30\t40\t50\n");
160 message(log, "\nIncreased /proc/sys/vm/freepages values to 30/40/50\n");
166 static void console_init()
170 int tried_devcons = 0;
171 int tried_vtmaster = 0;
174 if ((s = getenv("CONSOLE")) != NULL)
181 if ( stat(CONSOLE, &statbuf) && S_ISLNK(statbuf.st_mode)) {
182 fprintf(stderr, "Yikes! /dev/console does not exist or is a symlink.\n");
183 message(log, "Yikes! /dev/console does not exist or is a symlink.\n");
185 while ((fd = open(console, O_RDONLY | O_NONBLOCK)) < 0) {
186 if (!tried_devcons) {
191 if (!tried_vtmaster) {
193 console = VT_PRIMARY;
199 console = "/dev/null";
202 message(log, "console=%s\n", console);
205 static int waitfor(int pid)
209 message(log, "Waiting for process %d.\n", pid);
210 while ((wpid = wait(&status)) != pid) {
212 message(log, "pid %d exited, status=%x.\n", wpid, status);
218 static pid_t run(const char * const* command,
219 char *terminal, int get_enter)
223 static const char press_enter[] =
224 "\nPlease press Enter to activate this console. ";
226 if ((pid = fork()) == 0) {
233 f=open(terminal, O_RDWR);
236 tcsetpgrp(0, getpgrp());
241 * Save memory by not exec-ing anything large (like a shell)
242 * before the user wants it. This is critical if swap is not
243 * enabled and the system has low memory. Generally this will
244 * be run on the second virtual console, and the first will
245 * be allowed to start a shell or whatever an init script
249 write(1, press_enter, sizeof(press_enter) - 1);
253 /* Log the process name and args */
255 /* Now run it. The new program will take over this PID,
256 * so nothing further in init.c should be run. */
257 message(log, "Executing '%s', pid(%d)\r\n", *command, getpid());
258 execvp(*command, (char**)command+1);
260 message(log, "Hmm. Trying as a script.\r\n");
261 /* If shell scripts are not executed, force the issue */
262 if (errno == ENOEXEC) {
267 for( i=0 ; i<16 && command[i]; i++)
268 args[3+i] = (char*)command[i];
270 execvp(*args, (char**)args+1);
272 message(log, "Could not execute '%s'\n", command);
279 static void shutdown_system(void)
281 const char* const swap_off_cmd[] = { "/bin/swapoff", "-a", 0};
282 const char* const umount_cmd[] = { "/bin/umount", "-a", "-n", 0};
284 message(console, "The system is going down NOW !!\r\n");
286 /* Allow Ctrl-Alt-Del to reboot system. */
287 reboot(RB_ENABLE_CAD);
289 /* Send signals to every process _except_ pid 1 */
290 message(console, "Sending SIGHUP to all processes.\r\n");
294 message(console, "Sending SIGKILL to all processes.\r\n");
297 waitfor(run( swap_off_cmd, console, 0));
298 waitfor(run( umount_cmd, console, 0));
300 if (get_kernel_revision() <= 2 * 65536 + 2 * 256 + 11) {
301 /* Removed bdflush call, kupdate in kernels >2.2.11 */
307 static void halt_signal(int sig)
311 "The system is halted. Press CTRL-ALT-DEL or turn off power\r\n");
312 reboot(RB_POWER_OFF);
316 static void reboot_signal(int sig)
319 message(console, "Please stand by while rebooting the system.\r\n");
325 extern int init_main(int argc, char **argv)
331 const char* const swap_on_cmd[] = { "/bin/swapon", " -a ", 0};
332 const char* const init_commands[] = { SHELL, " -c", " exec ", INITSCRIPT, 0};
333 const char* const shell_commands[] = { SHELL, " -", 0};
334 const char* const* tty0_commands = init_commands;
335 const char* const* tty1_commands = shell_commands;
337 char *hello_msg_format =
338 "init(%d) started: BusyBox v%s (%s) multi-call binary\r\n";
340 char *hello_msg_format =
341 "init started: BusyBox v%s (%s) multi-call binary\r\n";
343 const char *no_memory =
344 "Sorry, your computer does not have enough memory.\r\n";
347 /* Set up sig handlers */
348 signal(SIGUSR1, halt_signal);
349 signal(SIGSEGV, halt_signal);
350 signal(SIGPWR, halt_signal);
351 signal(SIGALRM, halt_signal);
352 signal(SIGHUP, halt_signal);
353 signal(SIGUSR2, reboot_signal);
354 signal(SIGINT, reboot_signal);
355 signal(SIGTERM, reboot_signal);
357 /* Turn off rebooting via CTL-ALT-DEL -- we get a
358 * SIGINT on CAD so we can shut things down gracefully... */
359 reboot(RB_DISABLE_CAD);
361 /* Figure out where the default console should be */
364 /* Close whatever files are open, and reset the console. */
371 /* Make sure PATH is set to something sane */
372 if (getenv("PATH") == NULL)
373 putenv(PATH_DEFAULT);
377 message(log, hello_msg_format, BB_VER, BB_BT);
378 message(console, hello_msg_format, BB_VER, BB_BT);
380 message(log, hello_msg_format, getpid(), BB_VER, BB_BT);
381 message(console, hello_msg_format, getpid(), BB_VER, BB_BT);
386 if (mount("/proc", "/proc", "proc", 0, 0) == 0) {
387 message(log, "Mounting /proc: done.\n");
388 message(console, "Mounting /proc: done.\n");
390 message(log, "Mounting /proc: failed!\n");
391 message(console, "Mounting /proc: failed!\n");
394 /* Make sure there is enough memory to do something useful */
396 if (mem_total() < 3500) {
398 retval = stat("/etc/fstab", &statbuf);
400 message(console, "%s", no_memory);
405 /* Try to turn on swap */
406 waitfor(run(swap_on_cmd, console, 0));
407 if (mem_total() < 2000) {
408 message(console, "%s", no_memory);
416 /* Check if we are supposed to be in single user mode */
417 if ( argc > 1 && (!strcmp(argv[1], "single") ||
418 !strcmp(argv[1], "-s") || !strcmp(argv[1], "1"))) {
420 tty0_commands = shell_commands;
421 tty1_commands = shell_commands;
424 /* Make sure an init script exists before trying to run it */
425 if (run_rc == TRUE && stat(INITSCRIPT, &statbuf)) {
426 tty0_commands = shell_commands;
427 tty1_commands = shell_commands;
430 /* Ok, now launch the rc script and/or prepare to
431 * start up some VTs if somebody hits enter...
437 if (pid1 == 0 && tty0_commands) {
438 pid1 = run(tty0_commands, console, 1);
440 if (pid2 == 0 && tty1_commands) {
441 pid2 = run(tty1_commands, second_terminal, 1);
443 wpid = wait(&status);
445 message(log, "pid %d exited, status=%x.\n", wpid, status);
447 /* Don't respawn an init script if it exits */
448 if (run_rc == FALSE && wpid == pid1) {