blob: 561b5fd527186feddd823c743badb27c3b56cd4a [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>
Eric Andersenb186d981999-12-03 09:19:54 +000032#include <paths.h>
Eric Andersencc8ed391999-10-05 16:24:54 +000033#include <sys/types.h>
34#include <sys/fcntl.h>
35#include <sys/wait.h>
36#include <string.h>
37#include <sys/mount.h>
38#include <sys/reboot.h>
39#include <sys/kdaemon.h>
Eric Andersencc8ed391999-10-05 16:24:54 +000040#include <sys/sysmacros.h>
Eric Andersen8a8fbb81999-10-26 00:18:56 +000041#include <linux/serial.h> /* for serial_struct */
42#include <sys/vt.h> /* for vt_stat */
Eric Andersenabc7d591999-10-19 00:27:50 +000043#include <sys/ioctl.h>
Erik Andersen4d1d0111999-12-17 18:44:15 +000044#include <linux/version.h>
Eric Andersen4c781471999-11-26 08:12:56 +000045#ifdef BB_SYSLOGD
46#include <sys/syslog.h>
47#endif
Eric Andersencc8ed391999-10-05 16:24:54 +000048
Eric Andersen0ecb54a1999-12-05 23:24:55 +000049#if ! defined BB_FEATURE_USE_PROCFS
50#error Sorry, I depend on the /proc filesystem right now.
51#endif
52
Erik Andersen4d1d0111999-12-17 18:44:15 +000053#ifndef KERNEL_VERSION
54#define KERNEL_VERSION(a,b,c) (((a) << 16) + ((b) << 8) + (c))
55#endif
56
Eric Andersen0ecb54a1999-12-05 23:24:55 +000057
Eric Andersenbe971d61999-11-03 16:52:50 +000058#define VT_PRIMARY "/dev/tty1" /* Primary virtual console */
59#define VT_SECONDARY "/dev/tty2" /* Virtual console */
60#define VT_LOG "/dev/tty3" /* Virtual console */
Eric Andersen219d6f51999-11-02 19:43:01 +000061#define SERIAL_CON0 "/dev/ttyS0" /* Primary serial console */
62#define SERIAL_CON1 "/dev/ttyS1" /* Serial console */
Eric Andersend00c2621999-12-07 08:37:31 +000063#define GETTY "/sbin/getty" /* Default location of getty */
Eric Andersen8a8fbb81999-10-26 00:18:56 +000064#define SHELL "/bin/sh" /* Default shell */
Erik Andersenf294a021999-12-21 02:54:37 +000065#ifndef BB_INIT_SCRIPT
66#define BB_INIT_SCRIPT "/etc/init.d/rcS" /* Initscript. */
67#endif
Eric Andersen0460ff21999-10-25 23:32:44 +000068
Eric Andersen3ae0c781999-11-04 01:13:21 +000069#define LOG 0x1
70#define CONSOLE 0x2
Eric Andersenb186d981999-12-03 09:19:54 +000071static char *console = _PATH_CONSOLE;
Eric Andersenbe971d61999-11-03 16:52:50 +000072static char *second_console = VT_SECONDARY;
73static char *log = VT_LOG;
Eric Andersene18c75a1999-11-05 04:23:05 +000074static int kernel_version = 0;
Eric Andersen0460ff21999-10-25 23:32:44 +000075
76
77/* try to open up the specified device */
Eric Andersen8a8fbb81999-10-26 00:18:56 +000078int device_open(char *device, int mode)
Eric Andersencc8ed391999-10-05 16:24:54 +000079{
Eric Andersen0460ff21999-10-25 23:32:44 +000080 int m, f, fd = -1;
Eric Andersen8a8fbb81999-10-26 00:18:56 +000081
Eric Andersen7f1acfd1999-10-29 23:09:13 +000082 m = mode | O_NONBLOCK;
Eric Andersen8a8fbb81999-10-26 00:18:56 +000083
Eric Andersen0460ff21999-10-25 23:32:44 +000084 /* Retry up to 5 times */
Eric Andersen8a8fbb81999-10-26 00:18:56 +000085 for (f = 0; f < 5; f++)
86 if ((fd = open(device, m)) >= 0)
87 break;
88 if (fd < 0)
89 return fd;
Eric Andersen3ae0c781999-11-04 01:13:21 +000090 /* Reset original flags. */
Eric Andersen0460ff21999-10-25 23:32:44 +000091 if (m != mode)
92 fcntl(fd, F_SETFL, mode);
93 return fd;
94}
Eric Andersencc8ed391999-10-05 16:24:54 +000095
Eric Andersen3ae0c781999-11-04 01:13:21 +000096/* print a message to the specified device:
97 * device may be bitwise-or'd from LOG | CONSOLE */
98void message(int device, char *fmt, ...)
Eric Andersen0460ff21999-10-25 23:32:44 +000099{
100 int fd;
101 va_list arguments;
Eric Andersen4c781471999-11-26 08:12:56 +0000102#ifdef BB_SYSLOGD
103
104 /* Log the message to syslogd */
105 if (device & LOG ) {
106 char msg[1024];
107 va_start(arguments, fmt);
108 vsnprintf(msg, sizeof(msg), fmt, arguments);
109 va_end(arguments);
110 syslog(LOG_DAEMON|LOG_NOTICE, msg);
111 }
112
113#else
114 static int log_fd=-1;
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000115
Eric Andersen3ae0c781999-11-04 01:13:21 +0000116 /* Take full control of the log tty, and never close it.
117 * It's mine, all mine! Muhahahaha! */
Eric Andersen07e52971999-11-07 07:38:08 +0000118 if (log_fd < 0) {
119 if (log == NULL) {
120 /* don't even try to log, because there is no such console */
121 log_fd = -2;
122 /* log to main console instead */
123 device = CONSOLE;
124 }
125 else if ((log_fd = device_open(log, O_RDWR|O_NDELAY)) < 0) {
Eric Andersen3ae0c781999-11-04 01:13:21 +0000126 log_fd=-1;
127 fprintf(stderr, "Bummer, can't write to log on %s!\r\n", log);
128 fflush(stderr);
129 return;
130 }
131 }
Eric Andersen07e52971999-11-07 07:38:08 +0000132 if ( (device & LOG) && (log_fd >= 0) ) {
Eric Andersen0460ff21999-10-25 23:32:44 +0000133 va_start(arguments, fmt);
Eric Andersen3ae0c781999-11-04 01:13:21 +0000134 vdprintf(log_fd, fmt, arguments);
Eric Andersena7456061999-10-27 02:31:32 +0000135 va_end(arguments);
136 }
Eric Andersen4c781471999-11-26 08:12:56 +0000137#endif
138
Eric Andersen3ae0c781999-11-04 01:13:21 +0000139 if (device & CONSOLE) {
Eric Andersended62591999-11-18 00:19:26 +0000140 /* Always send console messages to /dev/console so people will see them. */
Eric Andersenb186d981999-12-03 09:19:54 +0000141 if ((fd = device_open(_PATH_CONSOLE, O_WRONLY|O_NOCTTY|O_NDELAY)) >= 0) {
Eric Andersen3ae0c781999-11-04 01:13:21 +0000142 va_start(arguments, fmt);
143 vdprintf(fd, fmt, arguments);
144 va_end(arguments);
145 close(fd);
146 } else {
147 fprintf(stderr, "Bummer, can't print: ");
148 va_start(arguments, fmt);
149 vfprintf(stderr, fmt, arguments);
150 fflush(stderr);
151 va_end(arguments);
152 }
153 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000154}
155
Eric Andersen3ae0c781999-11-04 01:13:21 +0000156
Eric Andersen0460ff21999-10-25 23:32:44 +0000157/* Set terminal settings to reasonable defaults */
Eric Andersenc7c41d31999-10-28 00:24:35 +0000158void set_term( int fd)
Eric Andersencc8ed391999-10-05 16:24:54 +0000159{
Eric Andersen0460ff21999-10-25 23:32:44 +0000160 struct termios tty;
Eric Andersen08b10341999-11-19 02:38:58 +0000161 static const char control_characters[] = {
162 '\003', '\034', '\177', '\025', '\004', '\0',
163 '\1', '\0', '\021', '\023', '\032', '\0', '\022',
164 '\017', '\027', '\026', '\0'
165 };
Eric Andersencc8ed391999-10-05 16:24:54 +0000166
Eric Andersenc7c41d31999-10-28 00:24:35 +0000167 tcgetattr(fd, &tty);
Eric Andersencc8ed391999-10-05 16:24:54 +0000168
Eric Andersen08b10341999-11-19 02:38:58 +0000169 /* set control chars */
170 memcpy(tty.c_cc, control_characters, sizeof(control_characters));
Eric Andersencc8ed391999-10-05 16:24:54 +0000171
Eric Andersen219d6f51999-11-02 19:43:01 +0000172 /* use line dicipline 0 */
Eric Andersen7f1acfd1999-10-29 23:09:13 +0000173 tty.c_line = 0;
174
Eric Andersen08b10341999-11-19 02:38:58 +0000175 /* Make it be sane */
176 //tty.c_cflag &= CBAUD|CBAUDEX|CSIZE|CSTOPB|PARENB|PARODD;
177 //tty.c_cflag |= HUPCL|CLOCAL;
178
179 /* input modes */
180 tty.c_iflag = ICRNL|IXON|IXOFF;
181
Eric Andersen7f1acfd1999-10-29 23:09:13 +0000182 /* output modes */
Eric Andersenc7c41d31999-10-28 00:24:35 +0000183 tty.c_oflag = OPOST|ONLCR;
Eric Andersen7f1acfd1999-10-29 23:09:13 +0000184
185 /* local modes */
Eric Andersen08b10341999-11-19 02:38:58 +0000186 tty.c_lflag = ISIG|ICANON|ECHO|ECHOE|ECHOK|ECHOCTL|ECHOKE|IEXTEN;
Eric Andersencc8ed391999-10-05 16:24:54 +0000187
Eric Andersenc7c41d31999-10-28 00:24:35 +0000188 tcsetattr(fd, TCSANOW, &tty);
Eric Andersencc8ed391999-10-05 16:24:54 +0000189}
190
Eric Andersen219d6f51999-11-02 19:43:01 +0000191/* How much memory does this machine have? */
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000192static int mem_total()
Eric Andersencc8ed391999-10-05 16:24:54 +0000193{
Eric Andersenabc7d591999-10-19 00:27:50 +0000194 char s[80];
Eric Andersen219d6f51999-11-02 19:43:01 +0000195 char *p = "/proc/meminfo";
Eric Andersenabc7d591999-10-19 00:27:50 +0000196 FILE *f;
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000197 const char pattern[] = "MemTotal:";
Eric Andersencc8ed391999-10-05 16:24:54 +0000198
Eric Andersen219d6f51999-11-02 19:43:01 +0000199 if ((f = fopen(p, "r")) < 0) {
Eric Andersen3ae0c781999-11-04 01:13:21 +0000200 message(LOG, "Error opening %s: %s\n", p, strerror( errno));
Eric Andersen219d6f51999-11-02 19:43:01 +0000201 return -1;
202 }
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000203 while (NULL != fgets(s, 79, f)) {
204 p = strstr(s, pattern);
Eric Andersenabc7d591999-10-19 00:27:50 +0000205 if (NULL != p) {
206 fclose(f);
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000207 return (atoi(p + strlen(pattern)));
Eric Andersenabc7d591999-10-19 00:27:50 +0000208 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000209 }
Eric Andersenabc7d591999-10-19 00:27:50 +0000210 return -1;
Eric Andersencc8ed391999-10-05 16:24:54 +0000211}
212
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000213static void console_init()
Eric Andersen0460ff21999-10-25 23:32:44 +0000214{
215 int fd;
216 int tried_devcons = 0;
Eric Andersen219d6f51999-11-02 19:43:01 +0000217 int tried_vtprimary = 0;
Eric Andersen07e52971999-11-07 07:38:08 +0000218 struct serial_struct sr;
Eric Andersen0460ff21999-10-25 23:32:44 +0000219 char *s;
220
Eric Andersen219d6f51999-11-02 19:43:01 +0000221 if ((s = getenv("CONSOLE")) != NULL) {
Eric Andersen0460ff21999-10-25 23:32:44 +0000222 console = s;
Eric Andersen07e52971999-11-07 07:38:08 +0000223 }
Eric Andersenfbb39c81999-11-08 17:00:52 +0000224#if #cpu(sparc)
Eric Andersen07e52971999-11-07 07:38:08 +0000225 /* sparc kernel supports console=tty[ab] parameter which is also
226 * passed to init, so catch it here */
227 else if ((s = getenv("console")) != NULL) {
228 /* remap tty[ab] to /dev/ttyS[01] */
229 if (strcmp( s, "ttya" )==0)
230 console = SERIAL_CON0;
231 else if (strcmp( s, "ttyb" )==0)
232 console = SERIAL_CON1;
233 }
Eric Andersen219d6f51999-11-02 19:43:01 +0000234#endif
Eric Andersen07e52971999-11-07 07:38:08 +0000235 else {
236 struct vt_stat vt;
237 static char the_console[13];
238
239 console = the_console;
240 /* 2.2 kernels: identify the real console backend and try to use it */
Eric Andersen08b10341999-11-19 02:38:58 +0000241 if (ioctl(0, TIOCGSERIAL, &sr) == 0) {
Eric Andersen07e52971999-11-07 07:38:08 +0000242 /* this is a serial console */
243 snprintf( the_console, sizeof the_console, "/dev/ttyS%d", sr.line );
244 }
245 else if (ioctl(0, VT_GETSTATE, &vt) == 0) {
246 /* this is linux virtual tty */
247 snprintf( the_console, sizeof the_console, "/dev/tty%d", vt.v_active );
248 } else {
Eric Andersenb186d981999-12-03 09:19:54 +0000249 console = _PATH_CONSOLE;
Eric Andersen07e52971999-11-07 07:38:08 +0000250 tried_devcons++;
251 }
Eric Andersen0460ff21999-10-25 23:32:44 +0000252 }
Eric Andersena7456061999-10-27 02:31:32 +0000253
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000254 while ((fd = open(console, O_RDONLY | O_NONBLOCK)) < 0) {
Eric Andersen219d6f51999-11-02 19:43:01 +0000255 /* Can't open selected console -- try /dev/console */
Eric Andersen0460ff21999-10-25 23:32:44 +0000256 if (!tried_devcons) {
257 tried_devcons++;
Eric Andersenb186d981999-12-03 09:19:54 +0000258 console = _PATH_CONSOLE;
Eric Andersen0460ff21999-10-25 23:32:44 +0000259 continue;
260 }
Eric Andersenbe971d61999-11-03 16:52:50 +0000261 /* Can't open selected console -- try vt1 */
262 if (!tried_vtprimary) {
263 tried_vtprimary++;
264 console = VT_PRIMARY;
265 continue;
266 }
Eric Andersen0460ff21999-10-25 23:32:44 +0000267 break;
268 }
269 if (fd < 0)
Eric Andersen219d6f51999-11-02 19:43:01 +0000270 /* Perhaps we should panic here? */
Eric Andersen0460ff21999-10-25 23:32:44 +0000271 console = "/dev/null";
Eric Andersen07e52971999-11-07 07:38:08 +0000272 else {
273 /* check for serial console and disable logging to tty3 & running a
274 * shell to tty2 */
275 if (ioctl(0,TIOCGSERIAL,&sr) == 0) {
Eric Andersen08b10341999-11-19 02:38:58 +0000276 message(LOG|CONSOLE, "serial console detected. Disabling 2nd virtual terminal.\r\n", console );
Eric Andersen07e52971999-11-07 07:38:08 +0000277 log = NULL;
278 second_console = NULL;
279 }
Eric Andersen0460ff21999-10-25 23:32:44 +0000280 close(fd);
Eric Andersen07e52971999-11-07 07:38:08 +0000281 }
Eric Andersen3ae0c781999-11-04 01:13:21 +0000282 message(LOG, "console=%s\n", console );
Eric Andersen0460ff21999-10-25 23:32:44 +0000283}
284
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000285static int waitfor(int pid)
Eric Andersen0460ff21999-10-25 23:32:44 +0000286{
287 int status, wpid;
288
Eric Andersen3ae0c781999-11-04 01:13:21 +0000289 message(LOG, "Waiting for process %d.\n", pid);
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000290 while ((wpid = wait(&status)) != pid) {
291 if (wpid > 0)
Eric Andersen3ae0c781999-11-04 01:13:21 +0000292 message(LOG, "pid %d exited, status=0x%x.\n", wpid, status);
Eric Andersen0460ff21999-10-25 23:32:44 +0000293 }
294 return wpid;
295}
296
Eric Andersena7456061999-10-27 02:31:32 +0000297
Eric Andersenc7c41d31999-10-28 00:24:35 +0000298static pid_t run(const char * const* command,
299 char *terminal, int get_enter)
Eric Andersen0460ff21999-10-25 23:32:44 +0000300{
Eric Andersen2f6c04f1999-11-01 23:59:44 +0000301 int fd;
Eric Andersenc7c41d31999-10-28 00:24:35 +0000302 pid_t pid;
Eric Andersen219d6f51999-11-02 19:43:01 +0000303 const char * const* cmd = command+1;
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000304 static const char press_enter[] =
Eric Andersen0460ff21999-10-25 23:32:44 +0000305 "\nPlease press Enter to activate this console. ";
306
Eric Andersen0460ff21999-10-25 23:32:44 +0000307 if ((pid = fork()) == 0) {
308 /* Clean up */
309 close(0);
310 close(1);
311 close(2);
312 setsid();
313
Eric Andersen2f6c04f1999-11-01 23:59:44 +0000314 /* Reset signal handlers set for parent process */
315 signal(SIGUSR1, SIG_DFL);
316 signal(SIGUSR2, SIG_DFL);
317 signal(SIGINT, SIG_DFL);
318 signal(SIGTERM, SIG_DFL);
319
320 if ((fd = device_open(terminal, O_RDWR)) < 0) {
Eric Andersen3ae0c781999-11-04 01:13:21 +0000321 message(LOG, "Bummer, can't open %s\r\n", terminal);
Eric Andersen2f6c04f1999-11-01 23:59:44 +0000322 exit(-1);
323 }
324 dup(fd);
325 dup(fd);
Eric Andersenc7c41d31999-10-28 00:24:35 +0000326 tcsetpgrp(0, getpgrp());
327 set_term(0);
Eric Andersen0460ff21999-10-25 23:32:44 +0000328
Eric Andersen2f6c04f1999-11-01 23:59:44 +0000329 if (get_enter==TRUE) {
Eric Andersen0460ff21999-10-25 23:32:44 +0000330 /*
331 * Save memory by not exec-ing anything large (like a shell)
332 * before the user wants it. This is critical if swap is not
333 * enabled and the system has low memory. Generally this will
334 * be run on the second virtual console, and the first will
335 * be allowed to start a shell or whatever an init script
336 * specifies.
337 */
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000338 char c;
Eric Andersen3ae0c781999-11-04 01:13:21 +0000339 message(LOG, "Waiting for enter to start '%s' (pid %d, console %s)\r\n",
340 *cmd, getpid(), terminal );
Eric Andersen7f1acfd1999-10-29 23:09:13 +0000341 write(1, press_enter, sizeof(press_enter) - 1);
Eric Andersen0460ff21999-10-25 23:32:44 +0000342 read(0, &c, 1);
343 }
344
345 /* Log the process name and args */
Eric Andersen2cb55071999-12-10 08:25:07 +0000346 message(LOG|CONSOLE, "Starting pid %d, console %s: '", getpid(), terminal);
347 while ( *cmd) message(LOG|CONSOLE, "%s ", *cmd++);
348 message(LOG|CONSOLE, "'\r\n");
Eric Andersen2f6c04f1999-11-01 23:59:44 +0000349
Eric Andersenc7c41d31999-10-28 00:24:35 +0000350 /* Now run it. The new program will take over this PID,
Eric Andersena7456061999-10-27 02:31:32 +0000351 * so nothing further in init.c should be run. */
Eric Andersenc7c41d31999-10-28 00:24:35 +0000352 execvp(*command, (char**)command+1);
Eric Andersen0460ff21999-10-25 23:32:44 +0000353
Eric Andersen3ae0c781999-11-04 01:13:21 +0000354 message(LOG, "Bummer, could not run '%s'\n", command);
Eric Andersen0460ff21999-10-25 23:32:44 +0000355 exit(-1);
356 }
357 return pid;
358}
359
Eric Andersen219d6f51999-11-02 19:43:01 +0000360/* Make sure there is enough memory to do something useful. *
361 * Calls swapon if needed so be sure /proc is mounted. */
362static void check_memory()
363{
364 struct stat statbuf;
365 const char* const swap_on_cmd[] =
366 { "/bin/swapon", "swapon", "-a", 0};
Eric Andersen219d6f51999-11-02 19:43:01 +0000367
368 if (mem_total() > 3500)
369 return;
370
371 if (stat("/etc/fstab", &statbuf) == 0) {
372 /* Try to turn on swap */
373 waitfor(run(swap_on_cmd, log, FALSE));
374 if (mem_total() < 3500)
375 goto goodnight;
376 } else
377 goto goodnight;
378 return;
379
380goodnight:
Eric Andersen3ae0c781999-11-04 01:13:21 +0000381 message(CONSOLE, "Sorry, your computer does not have enough memory.\r\n");
Eric Andersen219d6f51999-11-02 19:43:01 +0000382 while (1) sleep(1);
383}
384
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000385static void shutdown_system(void)
Eric Andersencc8ed391999-10-05 16:24:54 +0000386{
Eric Andersen2f6c04f1999-11-01 23:59:44 +0000387 const char* const swap_off_cmd[] = { "swapoff", "swapoff", "-a", 0};
Eric Andersen485b9551999-12-07 23:14:59 +0000388 const char* const umount_cmd[] = { "umount", "umount", "-a", 0};
Eric Andersencc8ed391999-10-05 16:24:54 +0000389
Eric Andersen2f6c04f1999-11-01 23:59:44 +0000390#ifndef DEBUG_INIT
Eric Andersen0460ff21999-10-25 23:32:44 +0000391 /* Allow Ctrl-Alt-Del to reboot system. */
392 reboot(RB_ENABLE_CAD);
Eric Andersen2f6c04f1999-11-01 23:59:44 +0000393#endif
Eric Andersen08b10341999-11-19 02:38:58 +0000394 message(CONSOLE, "\r\nThe system is going down NOW !!\r\n");
Eric Andersen3ae0c781999-11-04 01:13:21 +0000395 sync();
Eric Andersen0460ff21999-10-25 23:32:44 +0000396 /* Send signals to every process _except_ pid 1 */
Eric Andersen3ae0c781999-11-04 01:13:21 +0000397 message(CONSOLE, "Sending SIGHUP to all processes.\r\n");
Eric Andersen2f6c04f1999-11-01 23:59:44 +0000398#ifndef DEBUG_INIT
Eric Andersen0460ff21999-10-25 23:32:44 +0000399 kill(-1, SIGHUP);
Eric Andersen2f6c04f1999-11-01 23:59:44 +0000400#endif
Eric Andersen0460ff21999-10-25 23:32:44 +0000401 sleep(2);
402 sync();
Eric Andersen3ae0c781999-11-04 01:13:21 +0000403 message(CONSOLE, "Sending SIGKILL to all processes.\r\n");
Eric Andersen2f6c04f1999-11-01 23:59:44 +0000404#ifndef DEBUG_INIT
Eric Andersen0460ff21999-10-25 23:32:44 +0000405 kill(-1, SIGKILL);
Eric Andersen2f6c04f1999-11-01 23:59:44 +0000406#endif
Eric Andersen0460ff21999-10-25 23:32:44 +0000407 sleep(1);
Eric Andersen08b10341999-11-19 02:38:58 +0000408 message(CONSOLE, "Disabling swap.\r\n");
Eric Andersen3ae0c781999-11-04 01:13:21 +0000409 waitfor(run( swap_off_cmd, console, FALSE));
Eric Andersen08b10341999-11-19 02:38:58 +0000410 message(CONSOLE, "Unmounting filesystems.\r\n");
Eric Andersen3ae0c781999-11-04 01:13:21 +0000411 waitfor(run( umount_cmd, console, FALSE));
Eric Andersen0460ff21999-10-25 23:32:44 +0000412 sync();
Eric Andersene18c75a1999-11-05 04:23:05 +0000413 if (kernel_version > 0 && kernel_version <= 2 * 65536 + 2 * 256 + 11) {
Eric Andersencf8c9cf1999-11-05 00:31:46 +0000414 /* bdflush, kupdate not needed for kernels >2.2.11 */
Eric Andersene18c75a1999-11-05 04:23:05 +0000415 message(CONSOLE, "Flushing buffers.\r\n");
Eric Andersencf8c9cf1999-11-05 00:31:46 +0000416 bdflush(1, 0);
417 sync();
418 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000419}
420
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000421static void halt_signal(int sig)
Eric Andersencc8ed391999-10-05 16:24:54 +0000422{
Eric Andersenabc7d591999-10-19 00:27:50 +0000423 shutdown_system();
Eric Andersen3ae0c781999-11-04 01:13:21 +0000424 message(CONSOLE,
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000425 "The system is halted. Press CTRL-ALT-DEL or turn off power\r\n");
Eric Andersen3ae0c781999-11-04 01:13:21 +0000426 sync();
Eric Andersen2f6c04f1999-11-01 23:59:44 +0000427#ifndef DEBUG_INIT
Erik Andersen4d1d0111999-12-17 18:44:15 +0000428#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,2,0)
Eric Andersen2cb55071999-12-10 08:25:07 +0000429 if (sig == SIGUSR2)
430 reboot(RB_POWER_OFF);
431 else
Erik Andersen4d1d0111999-12-17 18:44:15 +0000432#endif
Eric Andersen2cb55071999-12-10 08:25:07 +0000433 reboot(RB_HALT_SYSTEM);
Eric Andersen2f6c04f1999-11-01 23:59:44 +0000434#endif
Eric Andersenabc7d591999-10-19 00:27:50 +0000435 exit(0);
Eric Andersencc8ed391999-10-05 16:24:54 +0000436}
437
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000438static void reboot_signal(int sig)
Eric Andersencc8ed391999-10-05 16:24:54 +0000439{
Eric Andersenabc7d591999-10-19 00:27:50 +0000440 shutdown_system();
Eric Andersen3ae0c781999-11-04 01:13:21 +0000441 message(CONSOLE, "Please stand by while rebooting the system.\r\n");
442 sync();
Eric Andersen2f6c04f1999-11-01 23:59:44 +0000443#ifndef DEBUG_INIT
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000444 reboot(RB_AUTOBOOT);
Eric Andersen2f6c04f1999-11-01 23:59:44 +0000445#endif
Eric Andersenabc7d591999-10-19 00:27:50 +0000446 exit(0);
Eric Andersencc8ed391999-10-05 16:24:54 +0000447}
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000448
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000449extern int init_main(int argc, char **argv)
Eric Andersen0460ff21999-10-25 23:32:44 +0000450{
Eric Andersend00c2621999-12-07 08:37:31 +0000451 int run_rc = FALSE;
452 int single = FALSE;
453 int wait_for_enter_tty1 = TRUE;
454 int wait_for_enter_tty2 = TRUE;
Eric Andersenc7c41d31999-10-28 00:24:35 +0000455 pid_t pid1 = 0;
456 pid_t pid2 = 0;
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000457 struct stat statbuf;
Eric Andersen84b00921999-12-11 04:16:51 +0000458 char which_vt1[30];
459 char which_vt2[30];
Erik Andersenf294a021999-12-21 02:54:37 +0000460 const char* const rc_script_command[] = { BB_INIT_SCRIPT, BB_INIT_SCRIPT, 0};
Eric Andersen84b00921999-12-11 04:16:51 +0000461 const char* const getty1_command[] = { GETTY, GETTY, "38400", which_vt1, 0};
462 const char* const getty2_command[] = { GETTY, GETTY, "38400", which_vt2, 0};
Eric Andersenb6a44b81999-11-13 04:47:09 +0000463 const char* const shell_command[] = { SHELL, "-" SHELL, 0};
Eric Andersenb6a44b81999-11-13 04:47:09 +0000464 const char* const* tty1_command = shell_command;
Eric Andersend00c2621999-12-07 08:37:31 +0000465 const char* const* tty2_command = shell_command;
Eric Andersen4c781471999-11-26 08:12:56 +0000466#ifdef BB_INIT_CMD_IF_RC_SCRIPT_EXITS
467 const char* const rc_exit_command[] = { "BB_INIT_CMD_IF_RC_SCRIPT_EXITS",
468 "BB_INIT_CMD_IF_RC_SCRIPT_EXITS", 0 };
Eric Andersenb6a44b81999-11-13 04:47:09 +0000469#endif
470
Eric Andersen2f6c04f1999-11-01 23:59:44 +0000471#ifdef DEBUG_INIT
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000472 char *hello_msg_format =
Eric Andersen04579781999-10-29 23:12:50 +0000473 "init(%d) started: BusyBox v%s (%s) multi-call binary\r\n";
Eric Andersen2f6c04f1999-11-01 23:59:44 +0000474#else
475 char *hello_msg_format =
476 "init started: BusyBox v%s (%s) multi-call binary\r\n";
Eric Andersen7f1acfd1999-10-29 23:09:13 +0000477#endif
Eric Andersen0460ff21999-10-25 23:32:44 +0000478
Eric Andersen219d6f51999-11-02 19:43:01 +0000479
Eric Andersend73dc5b1999-11-10 23:13:02 +0000480#ifndef DEBUG_INIT
Eric Andersen07274581999-11-21 21:50:07 +0000481 /* Expect to be PID 1 iff we are run as init (not linuxrc) */
482 if (getpid() != 1 && strstr(argv[0], "init")!=NULL ) {
Eric Andersend73dc5b1999-11-10 23:13:02 +0000483 usage( "init\n\nInit is the parent of all processes.\n\n"
484 "This version of init is designed to be run only by the kernel\n");
485 }
486#endif
487
Eric Andersen2f6c04f1999-11-01 23:59:44 +0000488 /* Set up sig handlers -- be sure to
489 * clear all of these in run() */
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000490 signal(SIGUSR1, halt_signal);
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000491 signal(SIGUSR2, reboot_signal);
492 signal(SIGINT, reboot_signal);
493 signal(SIGTERM, reboot_signal);
Eric Andersen0460ff21999-10-25 23:32:44 +0000494
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000495 /* Turn off rebooting via CTL-ALT-DEL -- we get a
496 * SIGINT on CAD so we can shut things down gracefully... */
Eric Andersen2f6c04f1999-11-01 23:59:44 +0000497#ifndef DEBUG_INIT
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000498 reboot(RB_DISABLE_CAD);
Eric Andersen7f1acfd1999-10-29 23:09:13 +0000499#endif
Eric Andersen219d6f51999-11-02 19:43:01 +0000500
Eric Andersenc7c41d31999-10-28 00:24:35 +0000501 /* Figure out where the default console should be */
502 console_init();
Eric Andersen0460ff21999-10-25 23:32:44 +0000503
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000504 /* Close whatever files are open, and reset the console. */
505 close(0);
506 close(1);
507 close(2);
Eric Andersen7f1acfd1999-10-29 23:09:13 +0000508 set_term(0);
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000509 setsid();
Eric Andersencc8ed391999-10-05 16:24:54 +0000510
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000511 /* Make sure PATH is set to something sane */
Eric Andersenb186d981999-12-03 09:19:54 +0000512 putenv(_PATH_STDPATH);
Eric Andersencc8ed391999-10-05 16:24:54 +0000513
Eric Andersen219d6f51999-11-02 19:43:01 +0000514
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000515 /* Hello world */
Eric Andersen7f1acfd1999-10-29 23:09:13 +0000516#ifndef DEBUG_INIT
Eric Andersen3ae0c781999-11-04 01:13:21 +0000517 message(CONSOLE|LOG, hello_msg_format, BB_VER, BB_BT);
Eric Andersen7f1acfd1999-10-29 23:09:13 +0000518#else
Eric Andersen3ae0c781999-11-04 01:13:21 +0000519 message(CONSOLE|LOG, hello_msg_format, getpid(), BB_VER, BB_BT);
Eric Andersen7f1acfd1999-10-29 23:09:13 +0000520#endif
Eric Andersencc8ed391999-10-05 16:24:54 +0000521
Eric Andersenc7c41d31999-10-28 00:24:35 +0000522
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000523 /* Mount /proc */
Eric Andersene18c75a1999-11-05 04:23:05 +0000524 if (mount ("proc", "/proc", "proc", 0, 0) == 0) {
Eric Andersen3ae0c781999-11-04 01:13:21 +0000525 message(CONSOLE|LOG, "Mounting /proc: done.\n");
Eric Andersene18c75a1999-11-05 04:23:05 +0000526 kernel_version = get_kernel_revision();
527 } else
Eric Andersen3ae0c781999-11-04 01:13:21 +0000528 message(CONSOLE|LOG, "Mounting /proc: failed!\n");
Eric Andersencc8ed391999-10-05 16:24:54 +0000529
Eric Andersen219d6f51999-11-02 19:43:01 +0000530 /* Make sure there is enough memory to do something useful. */
531 check_memory();
Eric Andersencc8ed391999-10-05 16:24:54 +0000532
Eric Andersend00c2621999-12-07 08:37:31 +0000533 /* Check if we are supposed to be in single user mode */
534 if ( argc > 1 && (!strcmp(argv[1], "single") ||
535 !strcmp(argv[1], "-s") || !strcmp(argv[1], "1"))) {
536 single = TRUE;
537 tty1_command = shell_command;
538 tty2_command = shell_command;
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000539 }
540
Eric Andersend00c2621999-12-07 08:37:31 +0000541 /* Make sure an init script exists before trying to run it */
Erik Andersenf294a021999-12-21 02:54:37 +0000542 if (single==FALSE && stat(BB_INIT_SCRIPT, &statbuf)==0) {
Eric Andersend00c2621999-12-07 08:37:31 +0000543 run_rc = TRUE;
544 wait_for_enter_tty1 = FALSE;
545 tty1_command = rc_script_command;
546 }
547
548 /* Make sure /sbin/getty exists before trying to run it */
549 if (stat(GETTY, &statbuf)==0) {
550 char* where;
Eric Andersen84b00921999-12-11 04:16:51 +0000551 /* First do tty2 */
Eric Andersend00c2621999-12-07 08:37:31 +0000552 wait_for_enter_tty2 = FALSE;
Eric Andersen84b00921999-12-11 04:16:51 +0000553 where = strrchr( second_console, '/');
Eric Andersend00c2621999-12-07 08:37:31 +0000554 if ( where != NULL) {
Eric Andersen84b00921999-12-11 04:16:51 +0000555 where++;
556 strncpy( which_vt2, where, sizeof(which_vt2));
Eric Andersend00c2621999-12-07 08:37:31 +0000557 }
558 tty2_command = getty2_command;
Eric Andersen84b00921999-12-11 04:16:51 +0000559
Eric Andersend00c2621999-12-07 08:37:31 +0000560 /* Check on hooking a getty onto tty1 */
561 if (run_rc == FALSE && single==FALSE) {
562 wait_for_enter_tty1 = FALSE;
Eric Andersen84b00921999-12-11 04:16:51 +0000563 where = strrchr( console, '/');
Eric Andersend00c2621999-12-07 08:37:31 +0000564 if ( where != NULL) {
Eric Andersen84b00921999-12-11 04:16:51 +0000565 where++;
566 strncpy( which_vt1, where, sizeof(which_vt1));
Eric Andersend00c2621999-12-07 08:37:31 +0000567 }
568 tty1_command = getty1_command;
569 }
570 }
571
Eric Andersen219d6f51999-11-02 19:43:01 +0000572
Eric Andersend00c2621999-12-07 08:37:31 +0000573 /* Ok, now launch the tty1_command and tty2_command */
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000574 for (;;) {
Eric Andersenc7c41d31999-10-28 00:24:35 +0000575 pid_t wpid;
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000576 int status;
577
Eric Andersend00c2621999-12-07 08:37:31 +0000578 if (pid1 == 0 && tty1_command) {
579 pid1 = run(tty1_command, console, wait_for_enter_tty1);
Eric Andersencc8ed391999-10-05 16:24:54 +0000580 }
Eric Andersend00c2621999-12-07 08:37:31 +0000581#ifdef BB_FEATURE_INIT_SECOND_CONSOLE
582 if (pid2 == 0 && tty2_command && second_console) {
583 pid2 = run(tty2_command, second_console, wait_for_enter_tty2);
Eric Andersencc8ed391999-10-05 16:24:54 +0000584 }
Eric Andersend00c2621999-12-07 08:37:31 +0000585#endif
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000586 wpid = wait(&status);
Eric Andersena7456061999-10-27 02:31:32 +0000587 if (wpid > 0 ) {
Eric Andersen3ae0c781999-11-04 01:13:21 +0000588 message(LOG, "pid %d exited, status=%x.\n", wpid, status);
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000589 }
Eric Andersencf8c9cf1999-11-05 00:31:46 +0000590 /* Don't respawn init script if it exits */
Eric Andersen219d6f51999-11-02 19:43:01 +0000591 if (wpid == pid1) {
Eric Andersencf8c9cf1999-11-05 00:31:46 +0000592 if (run_rc == FALSE) {
593 pid1 = 0;
Eric Andersenb6a44b81999-11-13 04:47:09 +0000594 }
Eric Andersen4c781471999-11-26 08:12:56 +0000595#ifdef BB_INIT_CMD_IF_RC_SCRIPT_EXITS
Eric Andersencf8c9cf1999-11-05 00:31:46 +0000596 else {
Eric Andersenb6a44b81999-11-13 04:47:09 +0000597 pid1 = 0;
Eric Andersen219d6f51999-11-02 19:43:01 +0000598 run_rc=FALSE;
Eric Andersend00c2621999-12-07 08:37:31 +0000599 wait_for_enter_tty1=TRUE;
600 tty1_command=rc_exit_command;
Eric Andersen219d6f51999-11-02 19:43:01 +0000601 }
Eric Andersencf8c9cf1999-11-05 00:31:46 +0000602#endif
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000603 }
Eric Andersend00c2621999-12-07 08:37:31 +0000604#ifdef BB_FEATURE_INIT_SECOND_CONSOLE
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000605 if (wpid == pid2) {
606 pid2 = 0;
607 }
Eric Andersend00c2621999-12-07 08:37:31 +0000608#endif
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000609 sleep(1);
610 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000611}