blob: 75a659911c72131831baa07bf56fc41eaa31d459 [file] [log] [blame]
Eric Andersenc4996011999-10-20 22:08:37 +00001/*
2 * Mini init implementation for busybox
3 *
4 *
5 * Copyright (C) 1995, 1996 by Bruce Perens <bruce@pixar.com>.
6 * Adjusted by so many folks, it's impossible to keep track.
7 *
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.
12 *
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.
17 *
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
21 *
22 */
23
Eric Andersencc8ed391999-10-05 16:24:54 +000024#include "internal.h"
25#include <stdio.h>
26#include <stdlib.h>
27#include <stdarg.h>
28#include <unistd.h>
29#include <errno.h>
30#include <signal.h>
31#include <termios.h>
32#include <sys/types.h>
33#include <sys/fcntl.h>
34#include <sys/wait.h>
35#include <string.h>
36#include <sys/mount.h>
37#include <sys/reboot.h>
38#include <sys/kdaemon.h>
Eric Andersencc8ed391999-10-05 16:24:54 +000039#include <sys/sysmacros.h>
Eric Andersen8a8fbb81999-10-26 00:18:56 +000040#include <linux/serial.h> /* for serial_struct */
41#include <sys/vt.h> /* for vt_stat */
Eric Andersenabc7d591999-10-19 00:27:50 +000042#include <sys/ioctl.h>
Eric Andersencc8ed391999-10-05 16:24:54 +000043
Eric Andersended62591999-11-18 00:19:26 +000044#define DEV_CONSOLE "/dev/console" /* Logical system console */
Eric Andersenbe971d61999-11-03 16:52:50 +000045#define VT_PRIMARY "/dev/tty1" /* Primary virtual console */
46#define VT_SECONDARY "/dev/tty2" /* Virtual console */
47#define VT_LOG "/dev/tty3" /* Virtual console */
Eric Andersen219d6f51999-11-02 19:43:01 +000048#define SERIAL_CON0 "/dev/ttyS0" /* Primary serial console */
49#define SERIAL_CON1 "/dev/ttyS1" /* Serial console */
Eric Andersen8a8fbb81999-10-26 00:18:56 +000050#define SHELL "/bin/sh" /* Default shell */
Eric Andersen219d6f51999-11-02 19:43:01 +000051#define INITSCRIPT "/etc/init.d/rcS" /* Initscript. */
Eric Andersen0460ff21999-10-25 23:32:44 +000052#define PATH_DEFAULT "PATH=/usr/local/sbin:/sbin:/bin:/usr/sbin:/usr/bin"
53
Eric Andersen3ae0c781999-11-04 01:13:21 +000054#define LOG 0x1
55#define CONSOLE 0x2
Eric Andersended62591999-11-18 00:19:26 +000056static char *console = DEV_CONSOLE;
Eric Andersenbe971d61999-11-03 16:52:50 +000057static char *second_console = VT_SECONDARY;
58static char *log = VT_LOG;
Eric Andersene18c75a1999-11-05 04:23:05 +000059static int kernel_version = 0;
Eric Andersen0460ff21999-10-25 23:32:44 +000060
61
62/* try to open up the specified device */
Eric Andersen8a8fbb81999-10-26 00:18:56 +000063int device_open(char *device, int mode)
Eric Andersencc8ed391999-10-05 16:24:54 +000064{
Eric Andersen0460ff21999-10-25 23:32:44 +000065 int m, f, fd = -1;
Eric Andersen8a8fbb81999-10-26 00:18:56 +000066
Eric Andersen7f1acfd1999-10-29 23:09:13 +000067 m = mode | O_NONBLOCK;
Eric Andersen8a8fbb81999-10-26 00:18:56 +000068
Eric Andersen0460ff21999-10-25 23:32:44 +000069 /* Retry up to 5 times */
Eric Andersen8a8fbb81999-10-26 00:18:56 +000070 for (f = 0; f < 5; f++)
71 if ((fd = open(device, m)) >= 0)
72 break;
73 if (fd < 0)
74 return fd;
Eric Andersen3ae0c781999-11-04 01:13:21 +000075 /* Reset original flags. */
Eric Andersen0460ff21999-10-25 23:32:44 +000076 if (m != mode)
77 fcntl(fd, F_SETFL, mode);
78 return fd;
79}
Eric Andersencc8ed391999-10-05 16:24:54 +000080
Eric Andersen3ae0c781999-11-04 01:13:21 +000081/* print a message to the specified device:
82 * device may be bitwise-or'd from LOG | CONSOLE */
83void message(int device, char *fmt, ...)
Eric Andersen0460ff21999-10-25 23:32:44 +000084{
85 int fd;
Eric Andersen3ae0c781999-11-04 01:13:21 +000086 static int log_fd=-1;
Eric Andersen0460ff21999-10-25 23:32:44 +000087 va_list arguments;
Eric Andersen8a8fbb81999-10-26 00:18:56 +000088
Eric Andersen3ae0c781999-11-04 01:13:21 +000089 /* Take full control of the log tty, and never close it.
90 * It's mine, all mine! Muhahahaha! */
Eric Andersen07e52971999-11-07 07:38:08 +000091 if (log_fd < 0) {
92 if (log == NULL) {
93 /* don't even try to log, because there is no such console */
94 log_fd = -2;
95 /* log to main console instead */
96 device = CONSOLE;
97 }
98 else if ((log_fd = device_open(log, O_RDWR|O_NDELAY)) < 0) {
Eric Andersen3ae0c781999-11-04 01:13:21 +000099 log_fd=-1;
100 fprintf(stderr, "Bummer, can't write to log on %s!\r\n", log);
101 fflush(stderr);
102 return;
103 }
104 }
105
Eric Andersen07e52971999-11-07 07:38:08 +0000106 if ( (device & LOG) && (log_fd >= 0) ) {
Eric Andersen0460ff21999-10-25 23:32:44 +0000107 va_start(arguments, fmt);
Eric Andersen3ae0c781999-11-04 01:13:21 +0000108 vdprintf(log_fd, fmt, arguments);
Eric Andersena7456061999-10-27 02:31:32 +0000109 va_end(arguments);
110 }
Eric Andersen3ae0c781999-11-04 01:13:21 +0000111 if (device & CONSOLE) {
Eric Andersended62591999-11-18 00:19:26 +0000112 /* Always send console messages to /dev/console so people will see them. */
113 if ((fd = device_open(DEV_CONSOLE, O_WRONLY|O_NOCTTY|O_NDELAY)) >= 0) {
Eric Andersen3ae0c781999-11-04 01:13:21 +0000114 va_start(arguments, fmt);
115 vdprintf(fd, fmt, arguments);
116 va_end(arguments);
117 close(fd);
118 } else {
119 fprintf(stderr, "Bummer, can't print: ");
120 va_start(arguments, fmt);
121 vfprintf(stderr, fmt, arguments);
122 fflush(stderr);
123 va_end(arguments);
124 }
125 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000126}
127
Eric Andersen3ae0c781999-11-04 01:13:21 +0000128
Eric Andersen0460ff21999-10-25 23:32:44 +0000129/* Set terminal settings to reasonable defaults */
Eric Andersenc7c41d31999-10-28 00:24:35 +0000130void set_term( int fd)
Eric Andersencc8ed391999-10-05 16:24:54 +0000131{
Eric Andersen0460ff21999-10-25 23:32:44 +0000132 struct termios tty;
Eric Andersen08b10341999-11-19 02:38:58 +0000133#if 0
Eric Andersen3ae0c781999-11-04 01:13:21 +0000134 static const char control_characters[] = {
135 '\003', '\034', '\177', '\030', '\004', '\0',
136 '\1', '\0', '\021', '\023', '\032', '\0', '\022',
137 '\017', '\027', '\026', '\0'
138 };
Eric Andersen08b10341999-11-19 02:38:58 +0000139#else
140 static const char control_characters[] = {
141 '\003', '\034', '\177', '\025', '\004', '\0',
142 '\1', '\0', '\021', '\023', '\032', '\0', '\022',
143 '\017', '\027', '\026', '\0'
144 };
145#endif
Eric Andersencc8ed391999-10-05 16:24:54 +0000146
Eric Andersenc7c41d31999-10-28 00:24:35 +0000147 tcgetattr(fd, &tty);
Eric Andersencc8ed391999-10-05 16:24:54 +0000148
Eric Andersen08b10341999-11-19 02:38:58 +0000149 /* set control chars */
150 memcpy(tty.c_cc, control_characters, sizeof(control_characters));
Eric Andersencc8ed391999-10-05 16:24:54 +0000151
Eric Andersen219d6f51999-11-02 19:43:01 +0000152 /* use line dicipline 0 */
Eric Andersen7f1acfd1999-10-29 23:09:13 +0000153 tty.c_line = 0;
154
Eric Andersen08b10341999-11-19 02:38:58 +0000155 /* Make it be sane */
156 //tty.c_cflag &= CBAUD|CBAUDEX|CSIZE|CSTOPB|PARENB|PARODD;
157 //tty.c_cflag |= HUPCL|CLOCAL;
158
159 /* input modes */
160 tty.c_iflag = ICRNL|IXON|IXOFF;
161
Eric Andersen7f1acfd1999-10-29 23:09:13 +0000162 /* output modes */
Eric Andersenc7c41d31999-10-28 00:24:35 +0000163 tty.c_oflag = OPOST|ONLCR;
Eric Andersen7f1acfd1999-10-29 23:09:13 +0000164
165 /* local modes */
Eric Andersen08b10341999-11-19 02:38:58 +0000166 tty.c_lflag = ISIG|ICANON|ECHO|ECHOE|ECHOK|ECHOCTL|ECHOKE|IEXTEN;
Eric Andersencc8ed391999-10-05 16:24:54 +0000167
Eric Andersenc7c41d31999-10-28 00:24:35 +0000168 tcsetattr(fd, TCSANOW, &tty);
Eric Andersencc8ed391999-10-05 16:24:54 +0000169}
170
Eric Andersen219d6f51999-11-02 19:43:01 +0000171/* How much memory does this machine have? */
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000172static int mem_total()
Eric Andersencc8ed391999-10-05 16:24:54 +0000173{
Eric Andersenabc7d591999-10-19 00:27:50 +0000174 char s[80];
Eric Andersen219d6f51999-11-02 19:43:01 +0000175 char *p = "/proc/meminfo";
Eric Andersenabc7d591999-10-19 00:27:50 +0000176 FILE *f;
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000177 const char pattern[] = "MemTotal:";
Eric Andersencc8ed391999-10-05 16:24:54 +0000178
Eric Andersen219d6f51999-11-02 19:43:01 +0000179 if ((f = fopen(p, "r")) < 0) {
Eric Andersen3ae0c781999-11-04 01:13:21 +0000180 message(LOG, "Error opening %s: %s\n", p, strerror( errno));
Eric Andersen219d6f51999-11-02 19:43:01 +0000181 return -1;
182 }
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000183 while (NULL != fgets(s, 79, f)) {
184 p = strstr(s, pattern);
Eric Andersenabc7d591999-10-19 00:27:50 +0000185 if (NULL != p) {
186 fclose(f);
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000187 return (atoi(p + strlen(pattern)));
Eric Andersenabc7d591999-10-19 00:27:50 +0000188 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000189 }
Eric Andersenabc7d591999-10-19 00:27:50 +0000190 return -1;
Eric Andersencc8ed391999-10-05 16:24:54 +0000191}
192
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000193static void console_init()
Eric Andersen0460ff21999-10-25 23:32:44 +0000194{
195 int fd;
196 int tried_devcons = 0;
Eric Andersen219d6f51999-11-02 19:43:01 +0000197 int tried_vtprimary = 0;
Eric Andersen07e52971999-11-07 07:38:08 +0000198 struct serial_struct sr;
Eric Andersen0460ff21999-10-25 23:32:44 +0000199 char *s;
200
Eric Andersen219d6f51999-11-02 19:43:01 +0000201 if ((s = getenv("CONSOLE")) != NULL) {
Eric Andersen0460ff21999-10-25 23:32:44 +0000202 console = s;
Eric Andersen07e52971999-11-07 07:38:08 +0000203 }
Eric Andersenfbb39c81999-11-08 17:00:52 +0000204#if #cpu(sparc)
Eric Andersen07e52971999-11-07 07:38:08 +0000205 /* sparc kernel supports console=tty[ab] parameter which is also
206 * passed to init, so catch it here */
207 else if ((s = getenv("console")) != NULL) {
208 /* remap tty[ab] to /dev/ttyS[01] */
209 if (strcmp( s, "ttya" )==0)
210 console = SERIAL_CON0;
211 else if (strcmp( s, "ttyb" )==0)
212 console = SERIAL_CON1;
213 }
Eric Andersen219d6f51999-11-02 19:43:01 +0000214#endif
Eric Andersen07e52971999-11-07 07:38:08 +0000215 else {
216 struct vt_stat vt;
217 static char the_console[13];
218
219 console = the_console;
220 /* 2.2 kernels: identify the real console backend and try to use it */
Eric Andersen08b10341999-11-19 02:38:58 +0000221 if (ioctl(0, TIOCGSERIAL, &sr) == 0) {
Eric Andersen07e52971999-11-07 07:38:08 +0000222 /* this is a serial console */
223 snprintf( the_console, sizeof the_console, "/dev/ttyS%d", sr.line );
224 }
225 else if (ioctl(0, VT_GETSTATE, &vt) == 0) {
226 /* this is linux virtual tty */
227 snprintf( the_console, sizeof the_console, "/dev/tty%d", vt.v_active );
228 } else {
Eric Andersended62591999-11-18 00:19:26 +0000229 console = DEV_CONSOLE;
Eric Andersen07e52971999-11-07 07:38:08 +0000230 tried_devcons++;
231 }
Eric Andersen0460ff21999-10-25 23:32:44 +0000232 }
Eric Andersena7456061999-10-27 02:31:32 +0000233
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000234 while ((fd = open(console, O_RDONLY | O_NONBLOCK)) < 0) {
Eric Andersen219d6f51999-11-02 19:43:01 +0000235 /* Can't open selected console -- try /dev/console */
Eric Andersen0460ff21999-10-25 23:32:44 +0000236 if (!tried_devcons) {
237 tried_devcons++;
Eric Andersended62591999-11-18 00:19:26 +0000238 console = DEV_CONSOLE;
Eric Andersen0460ff21999-10-25 23:32:44 +0000239 continue;
240 }
Eric Andersenbe971d61999-11-03 16:52:50 +0000241 /* Can't open selected console -- try vt1 */
242 if (!tried_vtprimary) {
243 tried_vtprimary++;
244 console = VT_PRIMARY;
245 continue;
246 }
Eric Andersen0460ff21999-10-25 23:32:44 +0000247 break;
248 }
249 if (fd < 0)
Eric Andersen219d6f51999-11-02 19:43:01 +0000250 /* Perhaps we should panic here? */
Eric Andersen0460ff21999-10-25 23:32:44 +0000251 console = "/dev/null";
Eric Andersen07e52971999-11-07 07:38:08 +0000252 else {
253 /* check for serial console and disable logging to tty3 & running a
254 * shell to tty2 */
255 if (ioctl(0,TIOCGSERIAL,&sr) == 0) {
Eric Andersen08b10341999-11-19 02:38:58 +0000256 message(LOG|CONSOLE, "serial console detected. Disabling 2nd virtual terminal.\r\n", console );
Eric Andersen07e52971999-11-07 07:38:08 +0000257 log = NULL;
258 second_console = NULL;
259 }
Eric Andersen0460ff21999-10-25 23:32:44 +0000260 close(fd);
Eric Andersen07e52971999-11-07 07:38:08 +0000261 }
Eric Andersen3ae0c781999-11-04 01:13:21 +0000262 message(LOG, "console=%s\n", console );
Eric Andersen0460ff21999-10-25 23:32:44 +0000263}
264
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000265static int waitfor(int pid)
Eric Andersen0460ff21999-10-25 23:32:44 +0000266{
267 int status, wpid;
268
Eric Andersen3ae0c781999-11-04 01:13:21 +0000269 message(LOG, "Waiting for process %d.\n", pid);
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000270 while ((wpid = wait(&status)) != pid) {
271 if (wpid > 0)
Eric Andersen3ae0c781999-11-04 01:13:21 +0000272 message(LOG, "pid %d exited, status=0x%x.\n", wpid, status);
Eric Andersen0460ff21999-10-25 23:32:44 +0000273 }
274 return wpid;
275}
276
Eric Andersena7456061999-10-27 02:31:32 +0000277
Eric Andersenc7c41d31999-10-28 00:24:35 +0000278static pid_t run(const char * const* command,
279 char *terminal, int get_enter)
Eric Andersen0460ff21999-10-25 23:32:44 +0000280{
Eric Andersen2f6c04f1999-11-01 23:59:44 +0000281 int fd;
Eric Andersenc7c41d31999-10-28 00:24:35 +0000282 pid_t pid;
Eric Andersen219d6f51999-11-02 19:43:01 +0000283 const char * const* cmd = command+1;
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000284 static const char press_enter[] =
Eric Andersen0460ff21999-10-25 23:32:44 +0000285 "\nPlease press Enter to activate this console. ";
286
Eric Andersen0460ff21999-10-25 23:32:44 +0000287 if ((pid = fork()) == 0) {
288 /* Clean up */
289 close(0);
290 close(1);
291 close(2);
292 setsid();
293
Eric Andersen2f6c04f1999-11-01 23:59:44 +0000294 /* Reset signal handlers set for parent process */
295 signal(SIGUSR1, SIG_DFL);
296 signal(SIGUSR2, SIG_DFL);
297 signal(SIGINT, SIG_DFL);
298 signal(SIGTERM, SIG_DFL);
299
300 if ((fd = device_open(terminal, O_RDWR)) < 0) {
Eric Andersen3ae0c781999-11-04 01:13:21 +0000301 message(LOG, "Bummer, can't open %s\r\n", terminal);
Eric Andersen2f6c04f1999-11-01 23:59:44 +0000302 exit(-1);
303 }
304 dup(fd);
305 dup(fd);
Eric Andersenc7c41d31999-10-28 00:24:35 +0000306 tcsetpgrp(0, getpgrp());
307 set_term(0);
Eric Andersen0460ff21999-10-25 23:32:44 +0000308
Eric Andersen2f6c04f1999-11-01 23:59:44 +0000309 if (get_enter==TRUE) {
Eric Andersen0460ff21999-10-25 23:32:44 +0000310 /*
311 * Save memory by not exec-ing anything large (like a shell)
312 * before the user wants it. This is critical if swap is not
313 * enabled and the system has low memory. Generally this will
314 * be run on the second virtual console, and the first will
315 * be allowed to start a shell or whatever an init script
316 * specifies.
317 */
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000318 char c;
Eric Andersen3ae0c781999-11-04 01:13:21 +0000319 message(LOG, "Waiting for enter to start '%s' (pid %d, console %s)\r\n",
320 *cmd, getpid(), terminal );
Eric Andersen7f1acfd1999-10-29 23:09:13 +0000321 write(1, press_enter, sizeof(press_enter) - 1);
Eric Andersen0460ff21999-10-25 23:32:44 +0000322 read(0, &c, 1);
323 }
324
325 /* Log the process name and args */
Eric Andersen3ae0c781999-11-04 01:13:21 +0000326 message(LOG, "Starting pid %d, console %s: '", getpid(), terminal);
327 while ( *cmd) message(LOG, "%s ", *cmd++);
328 message(LOG, "'\r\n");
Eric Andersen2f6c04f1999-11-01 23:59:44 +0000329
Eric Andersenc7c41d31999-10-28 00:24:35 +0000330 /* Now run it. The new program will take over this PID,
Eric Andersena7456061999-10-27 02:31:32 +0000331 * so nothing further in init.c should be run. */
Eric Andersenc7c41d31999-10-28 00:24:35 +0000332 execvp(*command, (char**)command+1);
Eric Andersen0460ff21999-10-25 23:32:44 +0000333
Eric Andersen3ae0c781999-11-04 01:13:21 +0000334 message(LOG, "Bummer, could not run '%s'\n", command);
Eric Andersen0460ff21999-10-25 23:32:44 +0000335 exit(-1);
336 }
337 return pid;
338}
339
Eric Andersen219d6f51999-11-02 19:43:01 +0000340/* Make sure there is enough memory to do something useful. *
341 * Calls swapon if needed so be sure /proc is mounted. */
342static void check_memory()
343{
344 struct stat statbuf;
345 const char* const swap_on_cmd[] =
346 { "/bin/swapon", "swapon", "-a", 0};
Eric Andersen219d6f51999-11-02 19:43:01 +0000347
348 if (mem_total() > 3500)
349 return;
350
351 if (stat("/etc/fstab", &statbuf) == 0) {
352 /* Try to turn on swap */
353 waitfor(run(swap_on_cmd, log, FALSE));
354 if (mem_total() < 3500)
355 goto goodnight;
356 } else
357 goto goodnight;
358 return;
359
360goodnight:
Eric Andersen3ae0c781999-11-04 01:13:21 +0000361 message(CONSOLE, "Sorry, your computer does not have enough memory.\r\n");
Eric Andersen219d6f51999-11-02 19:43:01 +0000362 while (1) sleep(1);
363}
364
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000365static void shutdown_system(void)
Eric Andersencc8ed391999-10-05 16:24:54 +0000366{
Eric Andersen2f6c04f1999-11-01 23:59:44 +0000367 const char* const swap_off_cmd[] = { "swapoff", "swapoff", "-a", 0};
368 const char* const umount_cmd[] = { "umount", "umount", "-a", "-n", 0};
Eric Andersencc8ed391999-10-05 16:24:54 +0000369
Eric Andersen2f6c04f1999-11-01 23:59:44 +0000370#ifndef DEBUG_INIT
Eric Andersen0460ff21999-10-25 23:32:44 +0000371 /* Allow Ctrl-Alt-Del to reboot system. */
372 reboot(RB_ENABLE_CAD);
Eric Andersen2f6c04f1999-11-01 23:59:44 +0000373#endif
Eric Andersen08b10341999-11-19 02:38:58 +0000374 message(CONSOLE, "\r\nThe system is going down NOW !!\r\n");
Eric Andersen3ae0c781999-11-04 01:13:21 +0000375 sync();
Eric Andersen0460ff21999-10-25 23:32:44 +0000376 /* Send signals to every process _except_ pid 1 */
Eric Andersen3ae0c781999-11-04 01:13:21 +0000377 message(CONSOLE, "Sending SIGHUP to all processes.\r\n");
Eric Andersen2f6c04f1999-11-01 23:59:44 +0000378#ifndef DEBUG_INIT
Eric Andersen0460ff21999-10-25 23:32:44 +0000379 kill(-1, SIGHUP);
Eric Andersen2f6c04f1999-11-01 23:59:44 +0000380#endif
Eric Andersen0460ff21999-10-25 23:32:44 +0000381 sleep(2);
382 sync();
Eric Andersen3ae0c781999-11-04 01:13:21 +0000383 message(CONSOLE, "Sending SIGKILL to all processes.\r\n");
Eric Andersen2f6c04f1999-11-01 23:59:44 +0000384#ifndef DEBUG_INIT
Eric Andersen0460ff21999-10-25 23:32:44 +0000385 kill(-1, SIGKILL);
Eric Andersen2f6c04f1999-11-01 23:59:44 +0000386#endif
Eric Andersen0460ff21999-10-25 23:32:44 +0000387 sleep(1);
Eric Andersen08b10341999-11-19 02:38:58 +0000388 message(CONSOLE, "Disabling swap.\r\n");
Eric Andersen3ae0c781999-11-04 01:13:21 +0000389 waitfor(run( swap_off_cmd, console, FALSE));
Eric Andersen08b10341999-11-19 02:38:58 +0000390 message(CONSOLE, "Unmounting filesystems.\r\n");
Eric Andersen3ae0c781999-11-04 01:13:21 +0000391 waitfor(run( umount_cmd, console, FALSE));
Eric Andersen0460ff21999-10-25 23:32:44 +0000392 sync();
Eric Andersene18c75a1999-11-05 04:23:05 +0000393 if (kernel_version > 0 && kernel_version <= 2 * 65536 + 2 * 256 + 11) {
Eric Andersencf8c9cf1999-11-05 00:31:46 +0000394 /* bdflush, kupdate not needed for kernels >2.2.11 */
Eric Andersene18c75a1999-11-05 04:23:05 +0000395 message(CONSOLE, "Flushing buffers.\r\n");
Eric Andersencf8c9cf1999-11-05 00:31:46 +0000396 bdflush(1, 0);
397 sync();
398 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000399}
400
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000401static void halt_signal(int sig)
Eric Andersencc8ed391999-10-05 16:24:54 +0000402{
Eric Andersenabc7d591999-10-19 00:27:50 +0000403 shutdown_system();
Eric Andersen3ae0c781999-11-04 01:13:21 +0000404 message(CONSOLE,
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000405 "The system is halted. Press CTRL-ALT-DEL or turn off power\r\n");
Eric Andersen3ae0c781999-11-04 01:13:21 +0000406 sync();
Eric Andersen2f6c04f1999-11-01 23:59:44 +0000407#ifndef DEBUG_INIT
Eric Andersen3ae0c781999-11-04 01:13:21 +0000408 reboot(RB_HALT_SYSTEM);
409 //reboot(RB_POWER_OFF);
Eric Andersen2f6c04f1999-11-01 23:59:44 +0000410#endif
Eric Andersenabc7d591999-10-19 00:27:50 +0000411 exit(0);
Eric Andersencc8ed391999-10-05 16:24:54 +0000412}
413
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000414static void reboot_signal(int sig)
Eric Andersencc8ed391999-10-05 16:24:54 +0000415{
Eric Andersenabc7d591999-10-19 00:27:50 +0000416 shutdown_system();
Eric Andersen3ae0c781999-11-04 01:13:21 +0000417 message(CONSOLE, "Please stand by while rebooting the system.\r\n");
418 sync();
Eric Andersen2f6c04f1999-11-01 23:59:44 +0000419#ifndef DEBUG_INIT
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000420 reboot(RB_AUTOBOOT);
Eric Andersen2f6c04f1999-11-01 23:59:44 +0000421#endif
Eric Andersenabc7d591999-10-19 00:27:50 +0000422 exit(0);
Eric Andersencc8ed391999-10-05 16:24:54 +0000423}
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000424
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000425extern int init_main(int argc, char **argv)
Eric Andersen0460ff21999-10-25 23:32:44 +0000426{
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000427 int run_rc = TRUE;
Eric Andersen219d6f51999-11-02 19:43:01 +0000428 int wait_for_enter = TRUE;
Eric Andersenc7c41d31999-10-28 00:24:35 +0000429 pid_t pid1 = 0;
430 pid_t pid2 = 0;
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000431 struct stat statbuf;
Eric Andersenb6a44b81999-11-13 04:47:09 +0000432 const char* const rc_script_command[] = { INITSCRIPT, INITSCRIPT, 0};
433 const char* const shell_command[] = { SHELL, "-" SHELL, 0};
434 const char* const* tty0_command = shell_command;
435 const char* const* tty1_command = shell_command;
436#ifdef BB_CONSOLE_CMD_IF_RC_SCRIPT_EXITS
437 const char* const rc_exit_command[] = BB_CONSOLE_CMD_IF_RC_SCRIPT_EXITS;
438#endif
439
Eric Andersen2f6c04f1999-11-01 23:59:44 +0000440#ifdef DEBUG_INIT
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000441 char *hello_msg_format =
Eric Andersen04579781999-10-29 23:12:50 +0000442 "init(%d) started: BusyBox v%s (%s) multi-call binary\r\n";
Eric Andersen2f6c04f1999-11-01 23:59:44 +0000443#else
444 char *hello_msg_format =
445 "init started: BusyBox v%s (%s) multi-call binary\r\n";
Eric Andersen7f1acfd1999-10-29 23:09:13 +0000446#endif
Eric Andersen0460ff21999-10-25 23:32:44 +0000447
Eric Andersen219d6f51999-11-02 19:43:01 +0000448
Eric Andersend73dc5b1999-11-10 23:13:02 +0000449#ifndef DEBUG_INIT
Eric Andersen07274581999-11-21 21:50:07 +0000450 /* Expect to be PID 1 iff we are run as init (not linuxrc) */
451 if (getpid() != 1 && strstr(argv[0], "init")!=NULL ) {
Eric Andersend73dc5b1999-11-10 23:13:02 +0000452 usage( "init\n\nInit is the parent of all processes.\n\n"
453 "This version of init is designed to be run only by the kernel\n");
454 }
455#endif
456
Eric Andersen219d6f51999-11-02 19:43:01 +0000457 /* Check if we are supposed to be in single user mode */
458 if ( argc > 1 && (!strcmp(argv[1], "single") ||
459 !strcmp(argv[1], "-s") || !strcmp(argv[1], "1"))) {
460 run_rc = FALSE;
461 }
462
463
Eric Andersen2f6c04f1999-11-01 23:59:44 +0000464 /* Set up sig handlers -- be sure to
465 * clear all of these in run() */
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000466 signal(SIGUSR1, halt_signal);
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000467 signal(SIGUSR2, reboot_signal);
468 signal(SIGINT, reboot_signal);
469 signal(SIGTERM, reboot_signal);
Eric Andersen0460ff21999-10-25 23:32:44 +0000470
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000471 /* Turn off rebooting via CTL-ALT-DEL -- we get a
472 * SIGINT on CAD so we can shut things down gracefully... */
Eric Andersen2f6c04f1999-11-01 23:59:44 +0000473#ifndef DEBUG_INIT
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000474 reboot(RB_DISABLE_CAD);
Eric Andersen7f1acfd1999-10-29 23:09:13 +0000475#endif
Eric Andersen219d6f51999-11-02 19:43:01 +0000476
Eric Andersenc7c41d31999-10-28 00:24:35 +0000477 /* Figure out where the default console should be */
478 console_init();
Eric Andersen0460ff21999-10-25 23:32:44 +0000479
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000480 /* Close whatever files are open, and reset the console. */
481 close(0);
482 close(1);
483 close(2);
Eric Andersen7f1acfd1999-10-29 23:09:13 +0000484 set_term(0);
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000485 setsid();
Eric Andersencc8ed391999-10-05 16:24:54 +0000486
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000487 /* Make sure PATH is set to something sane */
Eric Andersen219d6f51999-11-02 19:43:01 +0000488 putenv(PATH_DEFAULT);
Eric Andersencc8ed391999-10-05 16:24:54 +0000489
Eric Andersen219d6f51999-11-02 19:43:01 +0000490
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000491 /* Hello world */
Eric Andersen7f1acfd1999-10-29 23:09:13 +0000492#ifndef DEBUG_INIT
Eric Andersen3ae0c781999-11-04 01:13:21 +0000493 message(CONSOLE|LOG, hello_msg_format, BB_VER, BB_BT);
Eric Andersen7f1acfd1999-10-29 23:09:13 +0000494#else
Eric Andersen3ae0c781999-11-04 01:13:21 +0000495 message(CONSOLE|LOG, hello_msg_format, getpid(), BB_VER, BB_BT);
Eric Andersen7f1acfd1999-10-29 23:09:13 +0000496#endif
Eric Andersencc8ed391999-10-05 16:24:54 +0000497
Eric Andersenc7c41d31999-10-28 00:24:35 +0000498
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000499 /* Mount /proc */
Eric Andersene18c75a1999-11-05 04:23:05 +0000500 if (mount ("proc", "/proc", "proc", 0, 0) == 0) {
Eric Andersen3ae0c781999-11-04 01:13:21 +0000501 message(CONSOLE|LOG, "Mounting /proc: done.\n");
Eric Andersene18c75a1999-11-05 04:23:05 +0000502 kernel_version = get_kernel_revision();
503 } else
Eric Andersen3ae0c781999-11-04 01:13:21 +0000504 message(CONSOLE|LOG, "Mounting /proc: failed!\n");
Eric Andersencc8ed391999-10-05 16:24:54 +0000505
Eric Andersen219d6f51999-11-02 19:43:01 +0000506 /* Make sure there is enough memory to do something useful. */
507 check_memory();
Eric Andersencc8ed391999-10-05 16:24:54 +0000508
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000509
510 /* Make sure an init script exists before trying to run it */
Eric Andersen219d6f51999-11-02 19:43:01 +0000511 if (run_rc == TRUE && stat(INITSCRIPT, &statbuf)==0) {
512 wait_for_enter = FALSE;
Eric Andersenb6a44b81999-11-13 04:47:09 +0000513 tty0_command = rc_script_command;
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000514 }
515
Eric Andersen219d6f51999-11-02 19:43:01 +0000516
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000517 /* Ok, now launch the rc script and/or prepare to
518 * start up some VTs if somebody hits enter...
519 */
520 for (;;) {
Eric Andersenc7c41d31999-10-28 00:24:35 +0000521 pid_t wpid;
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000522 int status;
523
Eric Andersenb6a44b81999-11-13 04:47:09 +0000524 if (pid1 == 0 && tty0_command) {
525 pid1 = run(tty0_command, console, wait_for_enter);
Eric Andersencc8ed391999-10-05 16:24:54 +0000526 }
Eric Andersenb6a44b81999-11-13 04:47:09 +0000527 if (pid2 == 0 && tty1_command && second_console) {
528 pid2 = run(tty1_command, second_console, TRUE);
Eric Andersencc8ed391999-10-05 16:24:54 +0000529 }
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000530 wpid = wait(&status);
Eric Andersena7456061999-10-27 02:31:32 +0000531 if (wpid > 0 ) {
Eric Andersen3ae0c781999-11-04 01:13:21 +0000532 message(LOG, "pid %d exited, status=%x.\n", wpid, status);
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000533 }
Eric Andersencf8c9cf1999-11-05 00:31:46 +0000534 /* Don't respawn init script if it exits */
Eric Andersen219d6f51999-11-02 19:43:01 +0000535 if (wpid == pid1) {
Eric Andersencf8c9cf1999-11-05 00:31:46 +0000536 if (run_rc == FALSE) {
537 pid1 = 0;
Eric Andersenb6a44b81999-11-13 04:47:09 +0000538 }
539#ifdef BB_CONSOLE_CMD_IF_RC_SCRIPT_EXITS
Eric Andersencf8c9cf1999-11-05 00:31:46 +0000540 else {
Eric Andersenb6a44b81999-11-13 04:47:09 +0000541 pid1 = 0;
Eric Andersen219d6f51999-11-02 19:43:01 +0000542 run_rc=FALSE;
543 wait_for_enter=TRUE;
Eric Andersenb6a44b81999-11-13 04:47:09 +0000544 tty0_command=rc_exit_command;
Eric Andersen219d6f51999-11-02 19:43:01 +0000545 }
Eric Andersencf8c9cf1999-11-05 00:31:46 +0000546#endif
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000547 }
548 if (wpid == pid2) {
549 pid2 = 0;
550 }
551 sleep(1);
552 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000553}