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>
46 #define CONSOLE "/dev/console" /* Logical system console */
47 #define VT_PRIMARY "/dev/tty0" /* Virtual console master */
48 #define VT_SECONDARY "/dev/tty1" /* Virtual console master */
49 #define VT_LOG "/dev/tty2" /* Virtual console master */
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"
54 static int maxproclen=0;
57 static char* console = CONSOLE;
58 static char* second_terminal = "/dev/tty2";
59 static char* log = "/dev/tty3";
63 /* try to open up the specified device */
64 int device_open(char* device, int mode)
68 mode = m | O_NONBLOCK;
70 /* Retry up to 5 times */
71 for(f = 0; f < 5; f++)
72 if ((fd = open(device, m)) >= 0) break;
73 if (fd < 0) return fd;
74 /* Set original flags. */
76 fcntl(fd, F_SETFL, mode);
80 /* print a message to the specified device */
81 void message(char* device, char *fmt, ...)
85 if ((fd = device_open(device, O_WRONLY|O_NOCTTY|O_NDELAY)) >= 0) {
86 va_start(arguments, fmt);
87 vdprintf(fd, fmt, arguments);
93 /* Set terminal settings to reasonable defaults */
99 if ((fd = device_open(console, O_RDWR|O_NOCTTY)) < 0) {
100 message(log, "can't open %s", console);
103 ioctl(fd, TCGETS, &tty);
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 /* Set pre and post processing */
119 tty.c_iflag = IGNPAR|ICRNL|IXON|IXANY;
120 tty.c_oflag = OPOST|ONLCR;
121 tty.c_lflag = ISIG|ICANON|ECHO|ECHOCTL|ECHOPRT|ECHOKE;
123 /* Now set the terminal line. */
124 ioctl(fd, TCSETS, &tty);
134 const char pattern[]="MemTotal:";
136 f=fopen("/proc/meminfo","r");
137 while (NULL != fgets(s,79,f)) {
138 p=strstr(s, pattern);
141 return(atoi(p+strlen(pattern)));
153 f=fopen("/proc/sys/vm/freepages","r");
157 f=fopen("/proc/sys/vm/freepages","w");
158 fprintf(f,"30\t40\t50\n");
159 printf("\nIncreased /proc/sys/vm/freepages values to 30/40/50\n");
169 int tried_devcons = 0;
170 int tried_vtmaster = 0;
173 if ((s = getenv("CONSOLE")) != NULL)
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";
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 run(const char* command, char* terminal, int get_enter)
218 static const char press_enter[] =
219 "\nPlease press Enter to activate this console. ";
222 /* Make a proper command from the command string */
223 strcpy(buf, command);
225 for(f = 1; f < 15; f++) {
226 /* Skip white space */
227 while(*ptr == ' ' || *ptr == '\t') ptr++;
229 /* May be trailing space.. */
230 if (*ptr == 0) break;
231 /* Skip this `word' */
232 while(*ptr && *ptr != ' ' && *ptr != '\t' && *ptr != '#')
234 /* If end-of-line, break */
235 if (*ptr == '#' || *ptr == 0) {
240 /* End word with \0 and continue */
246 if ((pid = fork()) == 0) {
253 if ((f = device_open(terminal, O_RDWR|O_NOCTTY)) < 0) {
254 message( log, "open(%s) failed: %s", terminal, strerror(errno));
259 tcsetpgrp(0, getpgrp());
264 * Save memory by not exec-ing anything large (like a shell)
265 * before the user wants it. This is critical if swap is not
266 * enabled and the system has low memory. Generally this will
267 * be run on the second virtual console, and the first will
268 * be allowed to start a shell or whatever an init script
272 write(1, press_enter, sizeof(press_enter) - 1);
276 /* Log the process name and args */
277 message(log, "Executing '%s'\n", command);
279 /* Now run it. This should take over the PID, so nothing
280 * further in init.c should be run by this PID. */
281 execvp(args[1], args + 1);
283 /* If shell scripts are not executed, force the issue */
284 if (errno == ENOEXEC) {
288 strcpy(buf, "exec ");
289 strcat(buf, command);
292 execvp(args[1], args + 1);
294 message(log, "Could not execute '%s'\n", command, strerror(errno));
302 shutdown_system(void)
305 message(console, "The system is going down NOW !!");
307 /* Allow Ctrl-Alt-Del to reboot system. */
308 reboot(RB_ENABLE_CAD);
310 /* Send signals to every process _except_ pid 1 */
311 message(console, "Sending SIGHUP to all processes.\r\n");
315 message(console, "Sending SIGKILL to all processes.\r\n");
318 waitfor(run("/bin/swapoff -a", console, 0));
319 waitfor(run("/bin/umount -a -n", console, 0));
321 if (get_kernel_revision() <= 2*65536+2*256+11) {
322 /* Removed bdflush call, kupdate in kernels >2.2.11 */
332 message(console, "The system is halted. Press CTRL-ALT-DEL or turn off power\r\n");
333 reboot( RB_POWER_OFF);
338 reboot_signal(int sig)
341 message(console, "Please stand by while rebooting the system.\r\n");
342 reboot( RB_AUTOBOOT);
347 int setproctitle(char *fmt, ...)
355 len = vsprintf(buf, fmt, ap);
357 memset(argv0, 0, maxproclen + 1);
358 strncpy(argv0, buf, maxproclen);
363 init_main(int argc, char * * argv)
369 const char* init_commands = SHELL "-c exec " INITSCRIPT;
370 const char* shell_commands = SHELL;
371 const char* tty0_commands = init_commands;
372 const char* tty1_commands = shell_commands;
373 const char* no_memory =
374 "Sorry, your computer does not have enough memory.\n";
378 maxproclen = strlen(argv[0]);
379 setproctitle("init [boot]");
383 /* Set up sig handlers */
384 signal(SIGUSR1, halt_signal);
385 signal(SIGSEGV, halt_signal);
386 signal(SIGPWR, halt_signal);
387 signal(SIGALRM, halt_signal);
388 signal(SIGHUP, halt_signal);
389 signal(SIGUSR2, reboot_signal);
390 signal(SIGINT, reboot_signal);
391 signal(SIGTERM, reboot_signal);
393 /* Figure out where the default console should be */
396 /* Turn off rebooting via CTL-ALT-DEL -- we get a
397 * SIGINT on CAD so we can shut things down gracefully... */
399 reboot(RB_DISABLE_CAD);
402 /* Close whatever files are open, and reset the console. */
409 /* Make sure PATH is set to something sane */
410 if (getenv("PATH") == NULL)
411 putenv(PATH_DEFAULT);
414 message(console, "%s started: BusyBox v%s (%s) multi-call binary",
415 argv[0], BB_VER, BB_BT);
416 message(log, "%s started: BusyBox v%s (%s) multi-call binary",
417 argv[0], BB_VER, BB_BT);
421 message(console, "Mounting /proc: \n");
422 if (mount("/proc","/proc","proc",0,0)) {
423 message(log, "%s: could not mount /proc!\n", argv[0]);
424 message(console, "failed!\n");
426 message(console, "done.\n");
429 /* Make sure there is enough memory to do something useful*/
431 if (mem_total() < 2000) {
433 retval= stat("/etc/fstab",&statbuf);
435 message(console, "%s", no_memory);
436 while (1) { sleep(1);}
438 /* Try to turn on swap */
439 waitfor(run("/bin/swapon -a", console, 0));
440 if (mem_total() < 2000) {
441 message(console, "%s", no_memory);
442 while (1) { sleep(1);}
447 /* Check if we are supposed to be in single user mode */
448 if (!strcmp(argv[1], "single") || !strcmp(argv[1], "-s") || !strcmp(argv[1], "1")) {
450 tty0_commands = shell_commands;
452 setproctitle("init [S]");
454 setproctitle("init [1]");
457 /* Make sure an init script exists before trying to run it */
458 if ( run_rc == TRUE && stat( INITSCRIPT, &statbuf)) {
459 tty0_commands = shell_commands;
460 tty1_commands = shell_commands;
463 /* Ok, now launch the rc script and/or prepare to
464 * start up some VTs if somebody hits enter...
470 if ( pid1 == 0 && *tty0_commands ) {
471 pid1 = run(tty0_commands, console, 1);
473 if ( pid2 == 0 && *tty1_commands ) {
474 pid2 = run(tty1_commands, second_terminal, 1);
476 wpid = wait(&status);
477 if ( wpid > 0 && wpid != pid1) {
478 message(log, "pid %d exited, status=%x.\n", wpid, status);
480 if ( wpid == pid2 ) {