blob: 7258d3151910d4d5811c6086a6ec8358539f820e [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 Andersen4c781471999-11-26 08:12:56 +000043#ifdef BB_SYSLOGD
44#include <sys/syslog.h>
45#endif
Eric Andersencc8ed391999-10-05 16:24:54 +000046
Eric Andersended62591999-11-18 00:19:26 +000047#define DEV_CONSOLE "/dev/console" /* Logical system console */
Eric Andersenbe971d61999-11-03 16:52:50 +000048#define VT_PRIMARY "/dev/tty1" /* Primary virtual console */
49#define VT_SECONDARY "/dev/tty2" /* Virtual console */
50#define VT_LOG "/dev/tty3" /* Virtual console */
Eric Andersen219d6f51999-11-02 19:43:01 +000051#define SERIAL_CON0 "/dev/ttyS0" /* Primary serial console */
52#define SERIAL_CON1 "/dev/ttyS1" /* Serial console */
Eric Andersen8a8fbb81999-10-26 00:18:56 +000053#define SHELL "/bin/sh" /* Default shell */
Eric Andersen219d6f51999-11-02 19:43:01 +000054#define INITSCRIPT "/etc/init.d/rcS" /* Initscript. */
Eric Andersen0460ff21999-10-25 23:32:44 +000055#define PATH_DEFAULT "PATH=/usr/local/sbin:/sbin:/bin:/usr/sbin:/usr/bin"
56
Eric Andersen3ae0c781999-11-04 01:13:21 +000057#define LOG 0x1
58#define CONSOLE 0x2
Eric Andersended62591999-11-18 00:19:26 +000059static char *console = DEV_CONSOLE;
Eric Andersenbe971d61999-11-03 16:52:50 +000060static char *second_console = VT_SECONDARY;
61static char *log = VT_LOG;
Eric Andersene18c75a1999-11-05 04:23:05 +000062static int kernel_version = 0;
Eric Andersen0460ff21999-10-25 23:32:44 +000063
64
65/* try to open up the specified device */
Eric Andersen8a8fbb81999-10-26 00:18:56 +000066int device_open(char *device, int mode)
Eric Andersencc8ed391999-10-05 16:24:54 +000067{
Eric Andersen0460ff21999-10-25 23:32:44 +000068 int m, f, fd = -1;
Eric Andersen8a8fbb81999-10-26 00:18:56 +000069
Eric Andersen7f1acfd1999-10-29 23:09:13 +000070 m = mode | O_NONBLOCK;
Eric Andersen8a8fbb81999-10-26 00:18:56 +000071
Eric Andersen0460ff21999-10-25 23:32:44 +000072 /* Retry up to 5 times */
Eric Andersen8a8fbb81999-10-26 00:18:56 +000073 for (f = 0; f < 5; f++)
74 if ((fd = open(device, m)) >= 0)
75 break;
76 if (fd < 0)
77 return fd;
Eric Andersen3ae0c781999-11-04 01:13:21 +000078 /* Reset original flags. */
Eric Andersen0460ff21999-10-25 23:32:44 +000079 if (m != mode)
80 fcntl(fd, F_SETFL, mode);
81 return fd;
82}
Eric Andersencc8ed391999-10-05 16:24:54 +000083
Eric Andersen3ae0c781999-11-04 01:13:21 +000084/* print a message to the specified device:
85 * device may be bitwise-or'd from LOG | CONSOLE */
86void message(int device, char *fmt, ...)
Eric Andersen0460ff21999-10-25 23:32:44 +000087{
88 int fd;
89 va_list arguments;
Eric Andersen4c781471999-11-26 08:12:56 +000090#ifdef BB_SYSLOGD
91
92 /* Log the message to syslogd */
93 if (device & LOG ) {
94 char msg[1024];
95 va_start(arguments, fmt);
96 vsnprintf(msg, sizeof(msg), fmt, arguments);
97 va_end(arguments);
98 syslog(LOG_DAEMON|LOG_NOTICE, msg);
99 }
100
101#else
102 static int log_fd=-1;
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000103
Eric Andersen3ae0c781999-11-04 01:13:21 +0000104 /* Take full control of the log tty, and never close it.
105 * It's mine, all mine! Muhahahaha! */
Eric Andersen07e52971999-11-07 07:38:08 +0000106 if (log_fd < 0) {
107 if (log == NULL) {
108 /* don't even try to log, because there is no such console */
109 log_fd = -2;
110 /* log to main console instead */
111 device = CONSOLE;
112 }
113 else if ((log_fd = device_open(log, O_RDWR|O_NDELAY)) < 0) {
Eric Andersen3ae0c781999-11-04 01:13:21 +0000114 log_fd=-1;
115 fprintf(stderr, "Bummer, can't write to log on %s!\r\n", log);
116 fflush(stderr);
117 return;
118 }
119 }
Eric Andersen07e52971999-11-07 07:38:08 +0000120 if ( (device & LOG) && (log_fd >= 0) ) {
Eric Andersen0460ff21999-10-25 23:32:44 +0000121 va_start(arguments, fmt);
Eric Andersen3ae0c781999-11-04 01:13:21 +0000122 vdprintf(log_fd, fmt, arguments);
Eric Andersena7456061999-10-27 02:31:32 +0000123 va_end(arguments);
124 }
Eric Andersen4c781471999-11-26 08:12:56 +0000125#endif
126
Eric Andersen3ae0c781999-11-04 01:13:21 +0000127 if (device & CONSOLE) {
Eric Andersended62591999-11-18 00:19:26 +0000128 /* Always send console messages to /dev/console so people will see them. */
129 if ((fd = device_open(DEV_CONSOLE, O_WRONLY|O_NOCTTY|O_NDELAY)) >= 0) {
Eric Andersen3ae0c781999-11-04 01:13:21 +0000130 va_start(arguments, fmt);
131 vdprintf(fd, fmt, arguments);
132 va_end(arguments);
133 close(fd);
134 } else {
135 fprintf(stderr, "Bummer, can't print: ");
136 va_start(arguments, fmt);
137 vfprintf(stderr, fmt, arguments);
138 fflush(stderr);
139 va_end(arguments);
140 }
141 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000142}
143
Eric Andersen3ae0c781999-11-04 01:13:21 +0000144
Eric Andersen0460ff21999-10-25 23:32:44 +0000145/* Set terminal settings to reasonable defaults */
Eric Andersenc7c41d31999-10-28 00:24:35 +0000146void set_term( int fd)
Eric Andersencc8ed391999-10-05 16:24:54 +0000147{
Eric Andersen0460ff21999-10-25 23:32:44 +0000148 struct termios tty;
Eric Andersen08b10341999-11-19 02:38:58 +0000149#if 0
Eric Andersen3ae0c781999-11-04 01:13:21 +0000150 static const char control_characters[] = {
151 '\003', '\034', '\177', '\030', '\004', '\0',
152 '\1', '\0', '\021', '\023', '\032', '\0', '\022',
153 '\017', '\027', '\026', '\0'
154 };
Eric Andersen08b10341999-11-19 02:38:58 +0000155#else
156 static const char control_characters[] = {
157 '\003', '\034', '\177', '\025', '\004', '\0',
158 '\1', '\0', '\021', '\023', '\032', '\0', '\022',
159 '\017', '\027', '\026', '\0'
160 };
161#endif
Eric Andersencc8ed391999-10-05 16:24:54 +0000162
Eric Andersenc7c41d31999-10-28 00:24:35 +0000163 tcgetattr(fd, &tty);
Eric Andersencc8ed391999-10-05 16:24:54 +0000164
Eric Andersen08b10341999-11-19 02:38:58 +0000165 /* set control chars */
166 memcpy(tty.c_cc, control_characters, sizeof(control_characters));
Eric Andersencc8ed391999-10-05 16:24:54 +0000167
Eric Andersen219d6f51999-11-02 19:43:01 +0000168 /* use line dicipline 0 */
Eric Andersen7f1acfd1999-10-29 23:09:13 +0000169 tty.c_line = 0;
170
Eric Andersen08b10341999-11-19 02:38:58 +0000171 /* Make it be sane */
172 //tty.c_cflag &= CBAUD|CBAUDEX|CSIZE|CSTOPB|PARENB|PARODD;
173 //tty.c_cflag |= HUPCL|CLOCAL;
174
175 /* input modes */
176 tty.c_iflag = ICRNL|IXON|IXOFF;
177
Eric Andersen7f1acfd1999-10-29 23:09:13 +0000178 /* output modes */
Eric Andersenc7c41d31999-10-28 00:24:35 +0000179 tty.c_oflag = OPOST|ONLCR;
Eric Andersen7f1acfd1999-10-29 23:09:13 +0000180
181 /* local modes */
Eric Andersen08b10341999-11-19 02:38:58 +0000182 tty.c_lflag = ISIG|ICANON|ECHO|ECHOE|ECHOK|ECHOCTL|ECHOKE|IEXTEN;
Eric Andersencc8ed391999-10-05 16:24:54 +0000183
Eric Andersenc7c41d31999-10-28 00:24:35 +0000184 tcsetattr(fd, TCSANOW, &tty);
Eric Andersencc8ed391999-10-05 16:24:54 +0000185}
186
Eric Andersen219d6f51999-11-02 19:43:01 +0000187/* How much memory does this machine have? */
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000188static int mem_total()
Eric Andersencc8ed391999-10-05 16:24:54 +0000189{
Eric Andersenabc7d591999-10-19 00:27:50 +0000190 char s[80];
Eric Andersen219d6f51999-11-02 19:43:01 +0000191 char *p = "/proc/meminfo";
Eric Andersenabc7d591999-10-19 00:27:50 +0000192 FILE *f;
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000193 const char pattern[] = "MemTotal:";
Eric Andersencc8ed391999-10-05 16:24:54 +0000194
Eric Andersen219d6f51999-11-02 19:43:01 +0000195 if ((f = fopen(p, "r")) < 0) {
Eric Andersen3ae0c781999-11-04 01:13:21 +0000196 message(LOG, "Error opening %s: %s\n", p, strerror( errno));
Eric Andersen219d6f51999-11-02 19:43:01 +0000197 return -1;
198 }
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000199 while (NULL != fgets(s, 79, f)) {
200 p = strstr(s, pattern);
Eric Andersenabc7d591999-10-19 00:27:50 +0000201 if (NULL != p) {
202 fclose(f);
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000203 return (atoi(p + strlen(pattern)));
Eric Andersenabc7d591999-10-19 00:27:50 +0000204 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000205 }
Eric Andersenabc7d591999-10-19 00:27:50 +0000206 return -1;
Eric Andersencc8ed391999-10-05 16:24:54 +0000207}
208
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000209static void console_init()
Eric Andersen0460ff21999-10-25 23:32:44 +0000210{
211 int fd;
212 int tried_devcons = 0;
Eric Andersen219d6f51999-11-02 19:43:01 +0000213 int tried_vtprimary = 0;
Eric Andersen07e52971999-11-07 07:38:08 +0000214 struct serial_struct sr;
Eric Andersen0460ff21999-10-25 23:32:44 +0000215 char *s;
216
Eric Andersen219d6f51999-11-02 19:43:01 +0000217 if ((s = getenv("CONSOLE")) != NULL) {
Eric Andersen0460ff21999-10-25 23:32:44 +0000218 console = s;
Eric Andersen07e52971999-11-07 07:38:08 +0000219 }
Eric Andersenfbb39c81999-11-08 17:00:52 +0000220#if #cpu(sparc)
Eric Andersen07e52971999-11-07 07:38:08 +0000221 /* sparc kernel supports console=tty[ab] parameter which is also
222 * passed to init, so catch it here */
223 else if ((s = getenv("console")) != NULL) {
224 /* remap tty[ab] to /dev/ttyS[01] */
225 if (strcmp( s, "ttya" )==0)
226 console = SERIAL_CON0;
227 else if (strcmp( s, "ttyb" )==0)
228 console = SERIAL_CON1;
229 }
Eric Andersen219d6f51999-11-02 19:43:01 +0000230#endif
Eric Andersen07e52971999-11-07 07:38:08 +0000231 else {
232 struct vt_stat vt;
233 static char the_console[13];
234
235 console = the_console;
236 /* 2.2 kernels: identify the real console backend and try to use it */
Eric Andersen08b10341999-11-19 02:38:58 +0000237 if (ioctl(0, TIOCGSERIAL, &sr) == 0) {
Eric Andersen07e52971999-11-07 07:38:08 +0000238 /* this is a serial console */
239 snprintf( the_console, sizeof the_console, "/dev/ttyS%d", sr.line );
240 }
241 else if (ioctl(0, VT_GETSTATE, &vt) == 0) {
242 /* this is linux virtual tty */
243 snprintf( the_console, sizeof the_console, "/dev/tty%d", vt.v_active );
244 } else {
Eric Andersended62591999-11-18 00:19:26 +0000245 console = DEV_CONSOLE;
Eric Andersen07e52971999-11-07 07:38:08 +0000246 tried_devcons++;
247 }
Eric Andersen0460ff21999-10-25 23:32:44 +0000248 }
Eric Andersena7456061999-10-27 02:31:32 +0000249
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000250 while ((fd = open(console, O_RDONLY | O_NONBLOCK)) < 0) {
Eric Andersen219d6f51999-11-02 19:43:01 +0000251 /* Can't open selected console -- try /dev/console */
Eric Andersen0460ff21999-10-25 23:32:44 +0000252 if (!tried_devcons) {
253 tried_devcons++;
Eric Andersended62591999-11-18 00:19:26 +0000254 console = DEV_CONSOLE;
Eric Andersen0460ff21999-10-25 23:32:44 +0000255 continue;
256 }
Eric Andersenbe971d61999-11-03 16:52:50 +0000257 /* Can't open selected console -- try vt1 */
258 if (!tried_vtprimary) {
259 tried_vtprimary++;
260 console = VT_PRIMARY;
261 continue;
262 }
Eric Andersen0460ff21999-10-25 23:32:44 +0000263 break;
264 }
265 if (fd < 0)
Eric Andersen219d6f51999-11-02 19:43:01 +0000266 /* Perhaps we should panic here? */
Eric Andersen0460ff21999-10-25 23:32:44 +0000267 console = "/dev/null";
Eric Andersen07e52971999-11-07 07:38:08 +0000268 else {
269 /* check for serial console and disable logging to tty3 & running a
270 * shell to tty2 */
271 if (ioctl(0,TIOCGSERIAL,&sr) == 0) {
Eric Andersen08b10341999-11-19 02:38:58 +0000272 message(LOG|CONSOLE, "serial console detected. Disabling 2nd virtual terminal.\r\n", console );
Eric Andersen07e52971999-11-07 07:38:08 +0000273 log = NULL;
274 second_console = NULL;
275 }
Eric Andersen0460ff21999-10-25 23:32:44 +0000276 close(fd);
Eric Andersen07e52971999-11-07 07:38:08 +0000277 }
Eric Andersen3ae0c781999-11-04 01:13:21 +0000278 message(LOG, "console=%s\n", console );
Eric Andersen0460ff21999-10-25 23:32:44 +0000279}
280
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000281static int waitfor(int pid)
Eric Andersen0460ff21999-10-25 23:32:44 +0000282{
283 int status, wpid;
284
Eric Andersen3ae0c781999-11-04 01:13:21 +0000285 message(LOG, "Waiting for process %d.\n", pid);
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000286 while ((wpid = wait(&status)) != pid) {
287 if (wpid > 0)
Eric Andersen3ae0c781999-11-04 01:13:21 +0000288 message(LOG, "pid %d exited, status=0x%x.\n", wpid, status);
Eric Andersen0460ff21999-10-25 23:32:44 +0000289 }
290 return wpid;
291}
292
Eric Andersena7456061999-10-27 02:31:32 +0000293
Eric Andersenc7c41d31999-10-28 00:24:35 +0000294static pid_t run(const char * const* command,
295 char *terminal, int get_enter)
Eric Andersen0460ff21999-10-25 23:32:44 +0000296{
Eric Andersen2f6c04f1999-11-01 23:59:44 +0000297 int fd;
Eric Andersenc7c41d31999-10-28 00:24:35 +0000298 pid_t pid;
Eric Andersen219d6f51999-11-02 19:43:01 +0000299 const char * const* cmd = command+1;
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000300 static const char press_enter[] =
Eric Andersen0460ff21999-10-25 23:32:44 +0000301 "\nPlease press Enter to activate this console. ";
302
Eric Andersen0460ff21999-10-25 23:32:44 +0000303 if ((pid = fork()) == 0) {
304 /* Clean up */
305 close(0);
306 close(1);
307 close(2);
308 setsid();
309
Eric Andersen2f6c04f1999-11-01 23:59:44 +0000310 /* Reset signal handlers set for parent process */
311 signal(SIGUSR1, SIG_DFL);
312 signal(SIGUSR2, SIG_DFL);
313 signal(SIGINT, SIG_DFL);
314 signal(SIGTERM, SIG_DFL);
315
316 if ((fd = device_open(terminal, O_RDWR)) < 0) {
Eric Andersen3ae0c781999-11-04 01:13:21 +0000317 message(LOG, "Bummer, can't open %s\r\n", terminal);
Eric Andersen2f6c04f1999-11-01 23:59:44 +0000318 exit(-1);
319 }
320 dup(fd);
321 dup(fd);
Eric Andersenc7c41d31999-10-28 00:24:35 +0000322 tcsetpgrp(0, getpgrp());
323 set_term(0);
Eric Andersen0460ff21999-10-25 23:32:44 +0000324
Eric Andersen2f6c04f1999-11-01 23:59:44 +0000325 if (get_enter==TRUE) {
Eric Andersen0460ff21999-10-25 23:32:44 +0000326 /*
327 * Save memory by not exec-ing anything large (like a shell)
328 * before the user wants it. This is critical if swap is not
329 * enabled and the system has low memory. Generally this will
330 * be run on the second virtual console, and the first will
331 * be allowed to start a shell or whatever an init script
332 * specifies.
333 */
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000334 char c;
Eric Andersen3ae0c781999-11-04 01:13:21 +0000335 message(LOG, "Waiting for enter to start '%s' (pid %d, console %s)\r\n",
336 *cmd, getpid(), terminal );
Eric Andersen7f1acfd1999-10-29 23:09:13 +0000337 write(1, press_enter, sizeof(press_enter) - 1);
Eric Andersen0460ff21999-10-25 23:32:44 +0000338 read(0, &c, 1);
339 }
340
341 /* Log the process name and args */
Eric Andersen3ae0c781999-11-04 01:13:21 +0000342 message(LOG, "Starting pid %d, console %s: '", getpid(), terminal);
343 while ( *cmd) message(LOG, "%s ", *cmd++);
344 message(LOG, "'\r\n");
Eric Andersen2f6c04f1999-11-01 23:59:44 +0000345
Eric Andersenc7c41d31999-10-28 00:24:35 +0000346 /* Now run it. The new program will take over this PID,
Eric Andersena7456061999-10-27 02:31:32 +0000347 * so nothing further in init.c should be run. */
Eric Andersenc7c41d31999-10-28 00:24:35 +0000348 execvp(*command, (char**)command+1);
Eric Andersen0460ff21999-10-25 23:32:44 +0000349
Eric Andersen3ae0c781999-11-04 01:13:21 +0000350 message(LOG, "Bummer, could not run '%s'\n", command);
Eric Andersen0460ff21999-10-25 23:32:44 +0000351 exit(-1);
352 }
353 return pid;
354}
355
Eric Andersen219d6f51999-11-02 19:43:01 +0000356/* Make sure there is enough memory to do something useful. *
357 * Calls swapon if needed so be sure /proc is mounted. */
358static void check_memory()
359{
360 struct stat statbuf;
361 const char* const swap_on_cmd[] =
362 { "/bin/swapon", "swapon", "-a", 0};
Eric Andersen219d6f51999-11-02 19:43:01 +0000363
364 if (mem_total() > 3500)
365 return;
366
367 if (stat("/etc/fstab", &statbuf) == 0) {
368 /* Try to turn on swap */
369 waitfor(run(swap_on_cmd, log, FALSE));
370 if (mem_total() < 3500)
371 goto goodnight;
372 } else
373 goto goodnight;
374 return;
375
376goodnight:
Eric Andersen3ae0c781999-11-04 01:13:21 +0000377 message(CONSOLE, "Sorry, your computer does not have enough memory.\r\n");
Eric Andersen219d6f51999-11-02 19:43:01 +0000378 while (1) sleep(1);
379}
380
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000381static void shutdown_system(void)
Eric Andersencc8ed391999-10-05 16:24:54 +0000382{
Eric Andersen2f6c04f1999-11-01 23:59:44 +0000383 const char* const swap_off_cmd[] = { "swapoff", "swapoff", "-a", 0};
384 const char* const umount_cmd[] = { "umount", "umount", "-a", "-n", 0};
Eric Andersencc8ed391999-10-05 16:24:54 +0000385
Eric Andersen2f6c04f1999-11-01 23:59:44 +0000386#ifndef DEBUG_INIT
Eric Andersen0460ff21999-10-25 23:32:44 +0000387 /* Allow Ctrl-Alt-Del to reboot system. */
388 reboot(RB_ENABLE_CAD);
Eric Andersen2f6c04f1999-11-01 23:59:44 +0000389#endif
Eric Andersen08b10341999-11-19 02:38:58 +0000390 message(CONSOLE, "\r\nThe system is going down NOW !!\r\n");
Eric Andersen3ae0c781999-11-04 01:13:21 +0000391 sync();
Eric Andersen0460ff21999-10-25 23:32:44 +0000392 /* Send signals to every process _except_ pid 1 */
Eric Andersen3ae0c781999-11-04 01:13:21 +0000393 message(CONSOLE, "Sending SIGHUP to all processes.\r\n");
Eric Andersen2f6c04f1999-11-01 23:59:44 +0000394#ifndef DEBUG_INIT
Eric Andersen0460ff21999-10-25 23:32:44 +0000395 kill(-1, SIGHUP);
Eric Andersen2f6c04f1999-11-01 23:59:44 +0000396#endif
Eric Andersen0460ff21999-10-25 23:32:44 +0000397 sleep(2);
398 sync();
Eric Andersen3ae0c781999-11-04 01:13:21 +0000399 message(CONSOLE, "Sending SIGKILL to all processes.\r\n");
Eric Andersen2f6c04f1999-11-01 23:59:44 +0000400#ifndef DEBUG_INIT
Eric Andersen0460ff21999-10-25 23:32:44 +0000401 kill(-1, SIGKILL);
Eric Andersen2f6c04f1999-11-01 23:59:44 +0000402#endif
Eric Andersen0460ff21999-10-25 23:32:44 +0000403 sleep(1);
Eric Andersen08b10341999-11-19 02:38:58 +0000404 message(CONSOLE, "Disabling swap.\r\n");
Eric Andersen3ae0c781999-11-04 01:13:21 +0000405 waitfor(run( swap_off_cmd, console, FALSE));
Eric Andersen08b10341999-11-19 02:38:58 +0000406 message(CONSOLE, "Unmounting filesystems.\r\n");
Eric Andersen3ae0c781999-11-04 01:13:21 +0000407 waitfor(run( umount_cmd, console, FALSE));
Eric Andersen0460ff21999-10-25 23:32:44 +0000408 sync();
Eric Andersene18c75a1999-11-05 04:23:05 +0000409 if (kernel_version > 0 && kernel_version <= 2 * 65536 + 2 * 256 + 11) {
Eric Andersencf8c9cf1999-11-05 00:31:46 +0000410 /* bdflush, kupdate not needed for kernels >2.2.11 */
Eric Andersene18c75a1999-11-05 04:23:05 +0000411 message(CONSOLE, "Flushing buffers.\r\n");
Eric Andersencf8c9cf1999-11-05 00:31:46 +0000412 bdflush(1, 0);
413 sync();
414 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000415}
416
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000417static void halt_signal(int sig)
Eric Andersencc8ed391999-10-05 16:24:54 +0000418{
Eric Andersenabc7d591999-10-19 00:27:50 +0000419 shutdown_system();
Eric Andersen3ae0c781999-11-04 01:13:21 +0000420 message(CONSOLE,
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000421 "The system is halted. Press CTRL-ALT-DEL or turn off power\r\n");
Eric Andersen3ae0c781999-11-04 01:13:21 +0000422 sync();
Eric Andersen2f6c04f1999-11-01 23:59:44 +0000423#ifndef DEBUG_INIT
Eric Andersen3ae0c781999-11-04 01:13:21 +0000424 reboot(RB_HALT_SYSTEM);
425 //reboot(RB_POWER_OFF);
Eric Andersen2f6c04f1999-11-01 23:59:44 +0000426#endif
Eric Andersenabc7d591999-10-19 00:27:50 +0000427 exit(0);
Eric Andersencc8ed391999-10-05 16:24:54 +0000428}
429
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000430static void reboot_signal(int sig)
Eric Andersencc8ed391999-10-05 16:24:54 +0000431{
Eric Andersenabc7d591999-10-19 00:27:50 +0000432 shutdown_system();
Eric Andersen3ae0c781999-11-04 01:13:21 +0000433 message(CONSOLE, "Please stand by while rebooting the system.\r\n");
434 sync();
Eric Andersen2f6c04f1999-11-01 23:59:44 +0000435#ifndef DEBUG_INIT
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000436 reboot(RB_AUTOBOOT);
Eric Andersen2f6c04f1999-11-01 23:59:44 +0000437#endif
Eric Andersenabc7d591999-10-19 00:27:50 +0000438 exit(0);
Eric Andersencc8ed391999-10-05 16:24:54 +0000439}
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000440
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000441extern int init_main(int argc, char **argv)
Eric Andersen0460ff21999-10-25 23:32:44 +0000442{
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000443 int run_rc = TRUE;
Eric Andersen219d6f51999-11-02 19:43:01 +0000444 int wait_for_enter = TRUE;
Eric Andersenc7c41d31999-10-28 00:24:35 +0000445 pid_t pid1 = 0;
446 pid_t pid2 = 0;
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000447 struct stat statbuf;
Eric Andersenb6a44b81999-11-13 04:47:09 +0000448 const char* const rc_script_command[] = { INITSCRIPT, INITSCRIPT, 0};
449 const char* const shell_command[] = { SHELL, "-" SHELL, 0};
450 const char* const* tty0_command = shell_command;
451 const char* const* tty1_command = shell_command;
Eric Andersen4c781471999-11-26 08:12:56 +0000452#ifdef BB_INIT_CMD_IF_RC_SCRIPT_EXITS
453 const char* const rc_exit_command[] = { "BB_INIT_CMD_IF_RC_SCRIPT_EXITS",
454 "BB_INIT_CMD_IF_RC_SCRIPT_EXITS", 0 };
Eric Andersenb6a44b81999-11-13 04:47:09 +0000455#endif
456
Eric Andersen2f6c04f1999-11-01 23:59:44 +0000457#ifdef DEBUG_INIT
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000458 char *hello_msg_format =
Eric Andersen04579781999-10-29 23:12:50 +0000459 "init(%d) started: BusyBox v%s (%s) multi-call binary\r\n";
Eric Andersen2f6c04f1999-11-01 23:59:44 +0000460#else
461 char *hello_msg_format =
462 "init started: BusyBox v%s (%s) multi-call binary\r\n";
Eric Andersen7f1acfd1999-10-29 23:09:13 +0000463#endif
Eric Andersen0460ff21999-10-25 23:32:44 +0000464
Eric Andersen219d6f51999-11-02 19:43:01 +0000465
Eric Andersend73dc5b1999-11-10 23:13:02 +0000466#ifndef DEBUG_INIT
Eric Andersen07274581999-11-21 21:50:07 +0000467 /* Expect to be PID 1 iff we are run as init (not linuxrc) */
468 if (getpid() != 1 && strstr(argv[0], "init")!=NULL ) {
Eric Andersend73dc5b1999-11-10 23:13:02 +0000469 usage( "init\n\nInit is the parent of all processes.\n\n"
470 "This version of init is designed to be run only by the kernel\n");
471 }
472#endif
473
Eric Andersen219d6f51999-11-02 19:43:01 +0000474 /* Check if we are supposed to be in single user mode */
475 if ( argc > 1 && (!strcmp(argv[1], "single") ||
476 !strcmp(argv[1], "-s") || !strcmp(argv[1], "1"))) {
477 run_rc = FALSE;
478 }
479
480
Eric Andersen2f6c04f1999-11-01 23:59:44 +0000481 /* Set up sig handlers -- be sure to
482 * clear all of these in run() */
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000483 signal(SIGUSR1, halt_signal);
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000484 signal(SIGUSR2, reboot_signal);
485 signal(SIGINT, reboot_signal);
486 signal(SIGTERM, reboot_signal);
Eric Andersen0460ff21999-10-25 23:32:44 +0000487
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000488 /* Turn off rebooting via CTL-ALT-DEL -- we get a
489 * SIGINT on CAD so we can shut things down gracefully... */
Eric Andersen2f6c04f1999-11-01 23:59:44 +0000490#ifndef DEBUG_INIT
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000491 reboot(RB_DISABLE_CAD);
Eric Andersen7f1acfd1999-10-29 23:09:13 +0000492#endif
Eric Andersen219d6f51999-11-02 19:43:01 +0000493
Eric Andersenc7c41d31999-10-28 00:24:35 +0000494 /* Figure out where the default console should be */
495 console_init();
Eric Andersen0460ff21999-10-25 23:32:44 +0000496
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000497 /* Close whatever files are open, and reset the console. */
498 close(0);
499 close(1);
500 close(2);
Eric Andersen7f1acfd1999-10-29 23:09:13 +0000501 set_term(0);
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000502 setsid();
Eric Andersencc8ed391999-10-05 16:24:54 +0000503
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000504 /* Make sure PATH is set to something sane */
Eric Andersen219d6f51999-11-02 19:43:01 +0000505 putenv(PATH_DEFAULT);
Eric Andersencc8ed391999-10-05 16:24:54 +0000506
Eric Andersen219d6f51999-11-02 19:43:01 +0000507
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000508 /* Hello world */
Eric Andersen7f1acfd1999-10-29 23:09:13 +0000509#ifndef DEBUG_INIT
Eric Andersen3ae0c781999-11-04 01:13:21 +0000510 message(CONSOLE|LOG, hello_msg_format, BB_VER, BB_BT);
Eric Andersen7f1acfd1999-10-29 23:09:13 +0000511#else
Eric Andersen3ae0c781999-11-04 01:13:21 +0000512 message(CONSOLE|LOG, hello_msg_format, getpid(), BB_VER, BB_BT);
Eric Andersen7f1acfd1999-10-29 23:09:13 +0000513#endif
Eric Andersencc8ed391999-10-05 16:24:54 +0000514
Eric Andersenc7c41d31999-10-28 00:24:35 +0000515
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000516 /* Mount /proc */
Eric Andersene18c75a1999-11-05 04:23:05 +0000517 if (mount ("proc", "/proc", "proc", 0, 0) == 0) {
Eric Andersen3ae0c781999-11-04 01:13:21 +0000518 message(CONSOLE|LOG, "Mounting /proc: done.\n");
Eric Andersene18c75a1999-11-05 04:23:05 +0000519 kernel_version = get_kernel_revision();
520 } else
Eric Andersen3ae0c781999-11-04 01:13:21 +0000521 message(CONSOLE|LOG, "Mounting /proc: failed!\n");
Eric Andersencc8ed391999-10-05 16:24:54 +0000522
Eric Andersen219d6f51999-11-02 19:43:01 +0000523 /* Make sure there is enough memory to do something useful. */
524 check_memory();
Eric Andersencc8ed391999-10-05 16:24:54 +0000525
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000526
527 /* Make sure an init script exists before trying to run it */
Eric Andersen219d6f51999-11-02 19:43:01 +0000528 if (run_rc == TRUE && stat(INITSCRIPT, &statbuf)==0) {
529 wait_for_enter = FALSE;
Eric Andersenb6a44b81999-11-13 04:47:09 +0000530 tty0_command = rc_script_command;
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000531 }
532
Eric Andersen219d6f51999-11-02 19:43:01 +0000533
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000534 /* Ok, now launch the rc script and/or prepare to
535 * start up some VTs if somebody hits enter...
536 */
537 for (;;) {
Eric Andersenc7c41d31999-10-28 00:24:35 +0000538 pid_t wpid;
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000539 int status;
540
Eric Andersenb6a44b81999-11-13 04:47:09 +0000541 if (pid1 == 0 && tty0_command) {
542 pid1 = run(tty0_command, console, wait_for_enter);
Eric Andersencc8ed391999-10-05 16:24:54 +0000543 }
Eric Andersenb6a44b81999-11-13 04:47:09 +0000544 if (pid2 == 0 && tty1_command && second_console) {
545 pid2 = run(tty1_command, second_console, TRUE);
Eric Andersencc8ed391999-10-05 16:24:54 +0000546 }
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000547 wpid = wait(&status);
Eric Andersena7456061999-10-27 02:31:32 +0000548 if (wpid > 0 ) {
Eric Andersen3ae0c781999-11-04 01:13:21 +0000549 message(LOG, "pid %d exited, status=%x.\n", wpid, status);
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000550 }
Eric Andersencf8c9cf1999-11-05 00:31:46 +0000551 /* Don't respawn init script if it exits */
Eric Andersen219d6f51999-11-02 19:43:01 +0000552 if (wpid == pid1) {
Eric Andersencf8c9cf1999-11-05 00:31:46 +0000553 if (run_rc == FALSE) {
554 pid1 = 0;
Eric Andersenb6a44b81999-11-13 04:47:09 +0000555 }
Eric Andersen4c781471999-11-26 08:12:56 +0000556#ifdef BB_INIT_CMD_IF_RC_SCRIPT_EXITS
Eric Andersencf8c9cf1999-11-05 00:31:46 +0000557 else {
Eric Andersenb6a44b81999-11-13 04:47:09 +0000558 pid1 = 0;
Eric Andersen219d6f51999-11-02 19:43:01 +0000559 run_rc=FALSE;
560 wait_for_enter=TRUE;
Eric Andersenb6a44b81999-11-13 04:47:09 +0000561 tty0_command=rc_exit_command;
Eric Andersen219d6f51999-11-02 19:43:01 +0000562 }
Eric Andersencf8c9cf1999-11-05 00:31:46 +0000563#endif
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000564 }
565 if (wpid == pid2) {
566 pid2 = 0;
567 }
568 sleep(1);
569 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000570}