blob: ba65f51fd6f2c53e6e56df14519075ca4acb9ddf [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 */
Eric Andersen219d6f51999-11-02 19:43:01 +000065#define INITSCRIPT "/etc/init.d/rcS" /* Initscript. */
Eric Andersen0460ff21999-10-25 23:32:44 +000066
Eric Andersen3ae0c781999-11-04 01:13:21 +000067#define LOG 0x1
68#define CONSOLE 0x2
Eric Andersenb186d981999-12-03 09:19:54 +000069static char *console = _PATH_CONSOLE;
Eric Andersenbe971d61999-11-03 16:52:50 +000070static char *second_console = VT_SECONDARY;
71static char *log = VT_LOG;
Eric Andersene18c75a1999-11-05 04:23:05 +000072static int kernel_version = 0;
Eric Andersen0460ff21999-10-25 23:32:44 +000073
74
75/* try to open up the specified device */
Eric Andersen8a8fbb81999-10-26 00:18:56 +000076int device_open(char *device, int mode)
Eric Andersencc8ed391999-10-05 16:24:54 +000077{
Eric Andersen0460ff21999-10-25 23:32:44 +000078 int m, f, fd = -1;
Eric Andersen8a8fbb81999-10-26 00:18:56 +000079
Eric Andersen7f1acfd1999-10-29 23:09:13 +000080 m = mode | O_NONBLOCK;
Eric Andersen8a8fbb81999-10-26 00:18:56 +000081
Eric Andersen0460ff21999-10-25 23:32:44 +000082 /* Retry up to 5 times */
Eric Andersen8a8fbb81999-10-26 00:18:56 +000083 for (f = 0; f < 5; f++)
84 if ((fd = open(device, m)) >= 0)
85 break;
86 if (fd < 0)
87 return fd;
Eric Andersen3ae0c781999-11-04 01:13:21 +000088 /* Reset original flags. */
Eric Andersen0460ff21999-10-25 23:32:44 +000089 if (m != mode)
90 fcntl(fd, F_SETFL, mode);
91 return fd;
92}
Eric Andersencc8ed391999-10-05 16:24:54 +000093
Eric Andersen3ae0c781999-11-04 01:13:21 +000094/* print a message to the specified device:
95 * device may be bitwise-or'd from LOG | CONSOLE */
96void message(int device, char *fmt, ...)
Eric Andersen0460ff21999-10-25 23:32:44 +000097{
98 int fd;
99 va_list arguments;
Eric Andersen4c781471999-11-26 08:12:56 +0000100#ifdef BB_SYSLOGD
101
102 /* Log the message to syslogd */
103 if (device & LOG ) {
104 char msg[1024];
105 va_start(arguments, fmt);
106 vsnprintf(msg, sizeof(msg), fmt, arguments);
107 va_end(arguments);
108 syslog(LOG_DAEMON|LOG_NOTICE, msg);
109 }
110
111#else
112 static int log_fd=-1;
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000113
Eric Andersen3ae0c781999-11-04 01:13:21 +0000114 /* Take full control of the log tty, and never close it.
115 * It's mine, all mine! Muhahahaha! */
Eric Andersen07e52971999-11-07 07:38:08 +0000116 if (log_fd < 0) {
117 if (log == NULL) {
118 /* don't even try to log, because there is no such console */
119 log_fd = -2;
120 /* log to main console instead */
121 device = CONSOLE;
122 }
123 else if ((log_fd = device_open(log, O_RDWR|O_NDELAY)) < 0) {
Eric Andersen3ae0c781999-11-04 01:13:21 +0000124 log_fd=-1;
125 fprintf(stderr, "Bummer, can't write to log on %s!\r\n", log);
126 fflush(stderr);
127 return;
128 }
129 }
Eric Andersen07e52971999-11-07 07:38:08 +0000130 if ( (device & LOG) && (log_fd >= 0) ) {
Eric Andersen0460ff21999-10-25 23:32:44 +0000131 va_start(arguments, fmt);
Eric Andersen3ae0c781999-11-04 01:13:21 +0000132 vdprintf(log_fd, fmt, arguments);
Eric Andersena7456061999-10-27 02:31:32 +0000133 va_end(arguments);
134 }
Eric Andersen4c781471999-11-26 08:12:56 +0000135#endif
136
Eric Andersen3ae0c781999-11-04 01:13:21 +0000137 if (device & CONSOLE) {
Eric Andersended62591999-11-18 00:19:26 +0000138 /* Always send console messages to /dev/console so people will see them. */
Eric Andersenb186d981999-12-03 09:19:54 +0000139 if ((fd = device_open(_PATH_CONSOLE, O_WRONLY|O_NOCTTY|O_NDELAY)) >= 0) {
Eric Andersen3ae0c781999-11-04 01:13:21 +0000140 va_start(arguments, fmt);
141 vdprintf(fd, fmt, arguments);
142 va_end(arguments);
143 close(fd);
144 } else {
145 fprintf(stderr, "Bummer, can't print: ");
146 va_start(arguments, fmt);
147 vfprintf(stderr, fmt, arguments);
148 fflush(stderr);
149 va_end(arguments);
150 }
151 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000152}
153
Eric Andersen3ae0c781999-11-04 01:13:21 +0000154
Eric Andersen0460ff21999-10-25 23:32:44 +0000155/* Set terminal settings to reasonable defaults */
Eric Andersenc7c41d31999-10-28 00:24:35 +0000156void set_term( int fd)
Eric Andersencc8ed391999-10-05 16:24:54 +0000157{
Eric Andersen0460ff21999-10-25 23:32:44 +0000158 struct termios tty;
Eric Andersen08b10341999-11-19 02:38:58 +0000159 static const char control_characters[] = {
160 '\003', '\034', '\177', '\025', '\004', '\0',
161 '\1', '\0', '\021', '\023', '\032', '\0', '\022',
162 '\017', '\027', '\026', '\0'
163 };
Eric Andersencc8ed391999-10-05 16:24:54 +0000164
Eric Andersenc7c41d31999-10-28 00:24:35 +0000165 tcgetattr(fd, &tty);
Eric Andersencc8ed391999-10-05 16:24:54 +0000166
Eric Andersen08b10341999-11-19 02:38:58 +0000167 /* set control chars */
168 memcpy(tty.c_cc, control_characters, sizeof(control_characters));
Eric Andersencc8ed391999-10-05 16:24:54 +0000169
Eric Andersen219d6f51999-11-02 19:43:01 +0000170 /* use line dicipline 0 */
Eric Andersen7f1acfd1999-10-29 23:09:13 +0000171 tty.c_line = 0;
172
Eric Andersen08b10341999-11-19 02:38:58 +0000173 /* Make it be sane */
174 //tty.c_cflag &= CBAUD|CBAUDEX|CSIZE|CSTOPB|PARENB|PARODD;
175 //tty.c_cflag |= HUPCL|CLOCAL;
176
177 /* input modes */
178 tty.c_iflag = ICRNL|IXON|IXOFF;
179
Eric Andersen7f1acfd1999-10-29 23:09:13 +0000180 /* output modes */
Eric Andersenc7c41d31999-10-28 00:24:35 +0000181 tty.c_oflag = OPOST|ONLCR;
Eric Andersen7f1acfd1999-10-29 23:09:13 +0000182
183 /* local modes */
Eric Andersen08b10341999-11-19 02:38:58 +0000184 tty.c_lflag = ISIG|ICANON|ECHO|ECHOE|ECHOK|ECHOCTL|ECHOKE|IEXTEN;
Eric Andersencc8ed391999-10-05 16:24:54 +0000185
Eric Andersenc7c41d31999-10-28 00:24:35 +0000186 tcsetattr(fd, TCSANOW, &tty);
Eric Andersencc8ed391999-10-05 16:24:54 +0000187}
188
Eric Andersen219d6f51999-11-02 19:43:01 +0000189/* How much memory does this machine have? */
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000190static int mem_total()
Eric Andersencc8ed391999-10-05 16:24:54 +0000191{
Eric Andersenabc7d591999-10-19 00:27:50 +0000192 char s[80];
Eric Andersen219d6f51999-11-02 19:43:01 +0000193 char *p = "/proc/meminfo";
Eric Andersenabc7d591999-10-19 00:27:50 +0000194 FILE *f;
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000195 const char pattern[] = "MemTotal:";
Eric Andersencc8ed391999-10-05 16:24:54 +0000196
Eric Andersen219d6f51999-11-02 19:43:01 +0000197 if ((f = fopen(p, "r")) < 0) {
Eric Andersen3ae0c781999-11-04 01:13:21 +0000198 message(LOG, "Error opening %s: %s\n", p, strerror( errno));
Eric Andersen219d6f51999-11-02 19:43:01 +0000199 return -1;
200 }
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000201 while (NULL != fgets(s, 79, f)) {
202 p = strstr(s, pattern);
Eric Andersenabc7d591999-10-19 00:27:50 +0000203 if (NULL != p) {
204 fclose(f);
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000205 return (atoi(p + strlen(pattern)));
Eric Andersenabc7d591999-10-19 00:27:50 +0000206 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000207 }
Eric Andersenabc7d591999-10-19 00:27:50 +0000208 return -1;
Eric Andersencc8ed391999-10-05 16:24:54 +0000209}
210
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000211static void console_init()
Eric Andersen0460ff21999-10-25 23:32:44 +0000212{
213 int fd;
214 int tried_devcons = 0;
Eric Andersen219d6f51999-11-02 19:43:01 +0000215 int tried_vtprimary = 0;
Eric Andersen07e52971999-11-07 07:38:08 +0000216 struct serial_struct sr;
Eric Andersen0460ff21999-10-25 23:32:44 +0000217 char *s;
218
Eric Andersen219d6f51999-11-02 19:43:01 +0000219 if ((s = getenv("CONSOLE")) != NULL) {
Eric Andersen0460ff21999-10-25 23:32:44 +0000220 console = s;
Eric Andersen07e52971999-11-07 07:38:08 +0000221 }
Eric Andersenfbb39c81999-11-08 17:00:52 +0000222#if #cpu(sparc)
Eric Andersen07e52971999-11-07 07:38:08 +0000223 /* sparc kernel supports console=tty[ab] parameter which is also
224 * passed to init, so catch it here */
225 else if ((s = getenv("console")) != NULL) {
226 /* remap tty[ab] to /dev/ttyS[01] */
227 if (strcmp( s, "ttya" )==0)
228 console = SERIAL_CON0;
229 else if (strcmp( s, "ttyb" )==0)
230 console = SERIAL_CON1;
231 }
Eric Andersen219d6f51999-11-02 19:43:01 +0000232#endif
Eric Andersen07e52971999-11-07 07:38:08 +0000233 else {
234 struct vt_stat vt;
235 static char the_console[13];
236
237 console = the_console;
238 /* 2.2 kernels: identify the real console backend and try to use it */
Eric Andersen08b10341999-11-19 02:38:58 +0000239 if (ioctl(0, TIOCGSERIAL, &sr) == 0) {
Eric Andersen07e52971999-11-07 07:38:08 +0000240 /* this is a serial console */
241 snprintf( the_console, sizeof the_console, "/dev/ttyS%d", sr.line );
242 }
243 else if (ioctl(0, VT_GETSTATE, &vt) == 0) {
244 /* this is linux virtual tty */
245 snprintf( the_console, sizeof the_console, "/dev/tty%d", vt.v_active );
246 } else {
Eric Andersenb186d981999-12-03 09:19:54 +0000247 console = _PATH_CONSOLE;
Eric Andersen07e52971999-11-07 07:38:08 +0000248 tried_devcons++;
249 }
Eric Andersen0460ff21999-10-25 23:32:44 +0000250 }
Eric Andersena7456061999-10-27 02:31:32 +0000251
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000252 while ((fd = open(console, O_RDONLY | O_NONBLOCK)) < 0) {
Eric Andersen219d6f51999-11-02 19:43:01 +0000253 /* Can't open selected console -- try /dev/console */
Eric Andersen0460ff21999-10-25 23:32:44 +0000254 if (!tried_devcons) {
255 tried_devcons++;
Eric Andersenb186d981999-12-03 09:19:54 +0000256 console = _PATH_CONSOLE;
Eric Andersen0460ff21999-10-25 23:32:44 +0000257 continue;
258 }
Eric Andersenbe971d61999-11-03 16:52:50 +0000259 /* Can't open selected console -- try vt1 */
260 if (!tried_vtprimary) {
261 tried_vtprimary++;
262 console = VT_PRIMARY;
263 continue;
264 }
Eric Andersen0460ff21999-10-25 23:32:44 +0000265 break;
266 }
267 if (fd < 0)
Eric Andersen219d6f51999-11-02 19:43:01 +0000268 /* Perhaps we should panic here? */
Eric Andersen0460ff21999-10-25 23:32:44 +0000269 console = "/dev/null";
Eric Andersen07e52971999-11-07 07:38:08 +0000270 else {
271 /* check for serial console and disable logging to tty3 & running a
272 * shell to tty2 */
273 if (ioctl(0,TIOCGSERIAL,&sr) == 0) {
Eric Andersen08b10341999-11-19 02:38:58 +0000274 message(LOG|CONSOLE, "serial console detected. Disabling 2nd virtual terminal.\r\n", console );
Eric Andersen07e52971999-11-07 07:38:08 +0000275 log = NULL;
276 second_console = NULL;
277 }
Eric Andersen0460ff21999-10-25 23:32:44 +0000278 close(fd);
Eric Andersen07e52971999-11-07 07:38:08 +0000279 }
Eric Andersen3ae0c781999-11-04 01:13:21 +0000280 message(LOG, "console=%s\n", console );
Eric Andersen0460ff21999-10-25 23:32:44 +0000281}
282
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000283static int waitfor(int pid)
Eric Andersen0460ff21999-10-25 23:32:44 +0000284{
285 int status, wpid;
286
Eric Andersen3ae0c781999-11-04 01:13:21 +0000287 message(LOG, "Waiting for process %d.\n", pid);
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000288 while ((wpid = wait(&status)) != pid) {
289 if (wpid > 0)
Eric Andersen3ae0c781999-11-04 01:13:21 +0000290 message(LOG, "pid %d exited, status=0x%x.\n", wpid, status);
Eric Andersen0460ff21999-10-25 23:32:44 +0000291 }
292 return wpid;
293}
294
Eric Andersena7456061999-10-27 02:31:32 +0000295
Eric Andersenc7c41d31999-10-28 00:24:35 +0000296static pid_t run(const char * const* command,
297 char *terminal, int get_enter)
Eric Andersen0460ff21999-10-25 23:32:44 +0000298{
Eric Andersen2f6c04f1999-11-01 23:59:44 +0000299 int fd;
Eric Andersenc7c41d31999-10-28 00:24:35 +0000300 pid_t pid;
Eric Andersen219d6f51999-11-02 19:43:01 +0000301 const char * const* cmd = command+1;
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000302 static const char press_enter[] =
Eric Andersen0460ff21999-10-25 23:32:44 +0000303 "\nPlease press Enter to activate this console. ";
304
Eric Andersen0460ff21999-10-25 23:32:44 +0000305 if ((pid = fork()) == 0) {
306 /* Clean up */
307 close(0);
308 close(1);
309 close(2);
310 setsid();
311
Eric Andersen2f6c04f1999-11-01 23:59:44 +0000312 /* Reset signal handlers set for parent process */
313 signal(SIGUSR1, SIG_DFL);
314 signal(SIGUSR2, SIG_DFL);
315 signal(SIGINT, SIG_DFL);
316 signal(SIGTERM, SIG_DFL);
317
318 if ((fd = device_open(terminal, O_RDWR)) < 0) {
Eric Andersen3ae0c781999-11-04 01:13:21 +0000319 message(LOG, "Bummer, can't open %s\r\n", terminal);
Eric Andersen2f6c04f1999-11-01 23:59:44 +0000320 exit(-1);
321 }
322 dup(fd);
323 dup(fd);
Eric Andersenc7c41d31999-10-28 00:24:35 +0000324 tcsetpgrp(0, getpgrp());
325 set_term(0);
Eric Andersen0460ff21999-10-25 23:32:44 +0000326
Eric Andersen2f6c04f1999-11-01 23:59:44 +0000327 if (get_enter==TRUE) {
Eric Andersen0460ff21999-10-25 23:32:44 +0000328 /*
329 * Save memory by not exec-ing anything large (like a shell)
330 * before the user wants it. This is critical if swap is not
331 * enabled and the system has low memory. Generally this will
332 * be run on the second virtual console, and the first will
333 * be allowed to start a shell or whatever an init script
334 * specifies.
335 */
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000336 char c;
Eric Andersen3ae0c781999-11-04 01:13:21 +0000337 message(LOG, "Waiting for enter to start '%s' (pid %d, console %s)\r\n",
338 *cmd, getpid(), terminal );
Eric Andersen7f1acfd1999-10-29 23:09:13 +0000339 write(1, press_enter, sizeof(press_enter) - 1);
Eric Andersen0460ff21999-10-25 23:32:44 +0000340 read(0, &c, 1);
341 }
342
343 /* Log the process name and args */
Eric Andersen2cb55071999-12-10 08:25:07 +0000344 message(LOG|CONSOLE, "Starting pid %d, console %s: '", getpid(), terminal);
345 while ( *cmd) message(LOG|CONSOLE, "%s ", *cmd++);
346 message(LOG|CONSOLE, "'\r\n");
Eric Andersen2f6c04f1999-11-01 23:59:44 +0000347
Eric Andersenc7c41d31999-10-28 00:24:35 +0000348 /* Now run it. The new program will take over this PID,
Eric Andersena7456061999-10-27 02:31:32 +0000349 * so nothing further in init.c should be run. */
Eric Andersenc7c41d31999-10-28 00:24:35 +0000350 execvp(*command, (char**)command+1);
Eric Andersen0460ff21999-10-25 23:32:44 +0000351
Eric Andersen3ae0c781999-11-04 01:13:21 +0000352 message(LOG, "Bummer, could not run '%s'\n", command);
Eric Andersen0460ff21999-10-25 23:32:44 +0000353 exit(-1);
354 }
355 return pid;
356}
357
Eric Andersen219d6f51999-11-02 19:43:01 +0000358/* Make sure there is enough memory to do something useful. *
359 * Calls swapon if needed so be sure /proc is mounted. */
360static void check_memory()
361{
362 struct stat statbuf;
363 const char* const swap_on_cmd[] =
364 { "/bin/swapon", "swapon", "-a", 0};
Eric Andersen219d6f51999-11-02 19:43:01 +0000365
366 if (mem_total() > 3500)
367 return;
368
369 if (stat("/etc/fstab", &statbuf) == 0) {
370 /* Try to turn on swap */
371 waitfor(run(swap_on_cmd, log, FALSE));
372 if (mem_total() < 3500)
373 goto goodnight;
374 } else
375 goto goodnight;
376 return;
377
378goodnight:
Eric Andersen3ae0c781999-11-04 01:13:21 +0000379 message(CONSOLE, "Sorry, your computer does not have enough memory.\r\n");
Eric Andersen219d6f51999-11-02 19:43:01 +0000380 while (1) sleep(1);
381}
382
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000383static void shutdown_system(void)
Eric Andersencc8ed391999-10-05 16:24:54 +0000384{
Eric Andersen2f6c04f1999-11-01 23:59:44 +0000385 const char* const swap_off_cmd[] = { "swapoff", "swapoff", "-a", 0};
Eric Andersen485b9551999-12-07 23:14:59 +0000386 const char* const umount_cmd[] = { "umount", "umount", "-a", 0};
Eric Andersencc8ed391999-10-05 16:24:54 +0000387
Eric Andersen2f6c04f1999-11-01 23:59:44 +0000388#ifndef DEBUG_INIT
Eric Andersen0460ff21999-10-25 23:32:44 +0000389 /* Allow Ctrl-Alt-Del to reboot system. */
390 reboot(RB_ENABLE_CAD);
Eric Andersen2f6c04f1999-11-01 23:59:44 +0000391#endif
Eric Andersen08b10341999-11-19 02:38:58 +0000392 message(CONSOLE, "\r\nThe system is going down NOW !!\r\n");
Eric Andersen3ae0c781999-11-04 01:13:21 +0000393 sync();
Eric Andersen0460ff21999-10-25 23:32:44 +0000394 /* Send signals to every process _except_ pid 1 */
Eric Andersen3ae0c781999-11-04 01:13:21 +0000395 message(CONSOLE, "Sending SIGHUP to all processes.\r\n");
Eric Andersen2f6c04f1999-11-01 23:59:44 +0000396#ifndef DEBUG_INIT
Eric Andersen0460ff21999-10-25 23:32:44 +0000397 kill(-1, SIGHUP);
Eric Andersen2f6c04f1999-11-01 23:59:44 +0000398#endif
Eric Andersen0460ff21999-10-25 23:32:44 +0000399 sleep(2);
400 sync();
Eric Andersen3ae0c781999-11-04 01:13:21 +0000401 message(CONSOLE, "Sending SIGKILL to all processes.\r\n");
Eric Andersen2f6c04f1999-11-01 23:59:44 +0000402#ifndef DEBUG_INIT
Eric Andersen0460ff21999-10-25 23:32:44 +0000403 kill(-1, SIGKILL);
Eric Andersen2f6c04f1999-11-01 23:59:44 +0000404#endif
Eric Andersen0460ff21999-10-25 23:32:44 +0000405 sleep(1);
Eric Andersen08b10341999-11-19 02:38:58 +0000406 message(CONSOLE, "Disabling swap.\r\n");
Eric Andersen3ae0c781999-11-04 01:13:21 +0000407 waitfor(run( swap_off_cmd, console, FALSE));
Eric Andersen08b10341999-11-19 02:38:58 +0000408 message(CONSOLE, "Unmounting filesystems.\r\n");
Eric Andersen3ae0c781999-11-04 01:13:21 +0000409 waitfor(run( umount_cmd, console, FALSE));
Eric Andersen0460ff21999-10-25 23:32:44 +0000410 sync();
Eric Andersene18c75a1999-11-05 04:23:05 +0000411 if (kernel_version > 0 && kernel_version <= 2 * 65536 + 2 * 256 + 11) {
Eric Andersencf8c9cf1999-11-05 00:31:46 +0000412 /* bdflush, kupdate not needed for kernels >2.2.11 */
Eric Andersene18c75a1999-11-05 04:23:05 +0000413 message(CONSOLE, "Flushing buffers.\r\n");
Eric Andersencf8c9cf1999-11-05 00:31:46 +0000414 bdflush(1, 0);
415 sync();
416 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000417}
418
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000419static void halt_signal(int sig)
Eric Andersencc8ed391999-10-05 16:24:54 +0000420{
Eric Andersenabc7d591999-10-19 00:27:50 +0000421 shutdown_system();
Eric Andersen3ae0c781999-11-04 01:13:21 +0000422 message(CONSOLE,
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000423 "The system is halted. Press CTRL-ALT-DEL or turn off power\r\n");
Eric Andersen3ae0c781999-11-04 01:13:21 +0000424 sync();
Eric Andersen2f6c04f1999-11-01 23:59:44 +0000425#ifndef DEBUG_INIT
Erik Andersen4d1d0111999-12-17 18:44:15 +0000426#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,2,0)
Eric Andersen2cb55071999-12-10 08:25:07 +0000427 if (sig == SIGUSR2)
428 reboot(RB_POWER_OFF);
429 else
Erik Andersen4d1d0111999-12-17 18:44:15 +0000430#endif
Eric Andersen2cb55071999-12-10 08:25:07 +0000431 reboot(RB_HALT_SYSTEM);
Eric Andersen2f6c04f1999-11-01 23:59:44 +0000432#endif
Eric Andersenabc7d591999-10-19 00:27:50 +0000433 exit(0);
Eric Andersencc8ed391999-10-05 16:24:54 +0000434}
435
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000436static void reboot_signal(int sig)
Eric Andersencc8ed391999-10-05 16:24:54 +0000437{
Eric Andersenabc7d591999-10-19 00:27:50 +0000438 shutdown_system();
Eric Andersen3ae0c781999-11-04 01:13:21 +0000439 message(CONSOLE, "Please stand by while rebooting the system.\r\n");
440 sync();
Eric Andersen2f6c04f1999-11-01 23:59:44 +0000441#ifndef DEBUG_INIT
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000442 reboot(RB_AUTOBOOT);
Eric Andersen2f6c04f1999-11-01 23:59:44 +0000443#endif
Eric Andersenabc7d591999-10-19 00:27:50 +0000444 exit(0);
Eric Andersencc8ed391999-10-05 16:24:54 +0000445}
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000446
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000447extern int init_main(int argc, char **argv)
Eric Andersen0460ff21999-10-25 23:32:44 +0000448{
Eric Andersend00c2621999-12-07 08:37:31 +0000449 int run_rc = FALSE;
450 int single = FALSE;
451 int wait_for_enter_tty1 = TRUE;
452 int wait_for_enter_tty2 = TRUE;
Eric Andersenc7c41d31999-10-28 00:24:35 +0000453 pid_t pid1 = 0;
454 pid_t pid2 = 0;
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000455 struct stat statbuf;
Eric Andersen84b00921999-12-11 04:16:51 +0000456 char which_vt1[30];
457 char which_vt2[30];
Eric Andersenb6a44b81999-11-13 04:47:09 +0000458 const char* const rc_script_command[] = { INITSCRIPT, INITSCRIPT, 0};
Eric Andersen84b00921999-12-11 04:16:51 +0000459 const char* const getty1_command[] = { GETTY, GETTY, "38400", which_vt1, 0};
460 const char* const getty2_command[] = { GETTY, GETTY, "38400", which_vt2, 0};
Eric Andersenb6a44b81999-11-13 04:47:09 +0000461 const char* const shell_command[] = { SHELL, "-" SHELL, 0};
Eric Andersenb6a44b81999-11-13 04:47:09 +0000462 const char* const* tty1_command = shell_command;
Eric Andersend00c2621999-12-07 08:37:31 +0000463 const char* const* tty2_command = shell_command;
Eric Andersen4c781471999-11-26 08:12:56 +0000464#ifdef BB_INIT_CMD_IF_RC_SCRIPT_EXITS
465 const char* const rc_exit_command[] = { "BB_INIT_CMD_IF_RC_SCRIPT_EXITS",
466 "BB_INIT_CMD_IF_RC_SCRIPT_EXITS", 0 };
Eric Andersenb6a44b81999-11-13 04:47:09 +0000467#endif
468
Eric Andersen2f6c04f1999-11-01 23:59:44 +0000469#ifdef DEBUG_INIT
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000470 char *hello_msg_format =
Eric Andersen04579781999-10-29 23:12:50 +0000471 "init(%d) started: BusyBox v%s (%s) multi-call binary\r\n";
Eric Andersen2f6c04f1999-11-01 23:59:44 +0000472#else
473 char *hello_msg_format =
474 "init started: BusyBox v%s (%s) multi-call binary\r\n";
Eric Andersen7f1acfd1999-10-29 23:09:13 +0000475#endif
Eric Andersen0460ff21999-10-25 23:32:44 +0000476
Eric Andersen219d6f51999-11-02 19:43:01 +0000477
Eric Andersend73dc5b1999-11-10 23:13:02 +0000478#ifndef DEBUG_INIT
Eric Andersen07274581999-11-21 21:50:07 +0000479 /* Expect to be PID 1 iff we are run as init (not linuxrc) */
480 if (getpid() != 1 && strstr(argv[0], "init")!=NULL ) {
Eric Andersend73dc5b1999-11-10 23:13:02 +0000481 usage( "init\n\nInit is the parent of all processes.\n\n"
482 "This version of init is designed to be run only by the kernel\n");
483 }
484#endif
485
Eric Andersen2f6c04f1999-11-01 23:59:44 +0000486 /* Set up sig handlers -- be sure to
487 * clear all of these in run() */
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000488 signal(SIGUSR1, halt_signal);
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000489 signal(SIGUSR2, reboot_signal);
490 signal(SIGINT, reboot_signal);
491 signal(SIGTERM, reboot_signal);
Eric Andersen0460ff21999-10-25 23:32:44 +0000492
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000493 /* Turn off rebooting via CTL-ALT-DEL -- we get a
494 * SIGINT on CAD so we can shut things down gracefully... */
Eric Andersen2f6c04f1999-11-01 23:59:44 +0000495#ifndef DEBUG_INIT
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000496 reboot(RB_DISABLE_CAD);
Eric Andersen7f1acfd1999-10-29 23:09:13 +0000497#endif
Eric Andersen219d6f51999-11-02 19:43:01 +0000498
Eric Andersenc7c41d31999-10-28 00:24:35 +0000499 /* Figure out where the default console should be */
500 console_init();
Eric Andersen0460ff21999-10-25 23:32:44 +0000501
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000502 /* Close whatever files are open, and reset the console. */
503 close(0);
504 close(1);
505 close(2);
Eric Andersen7f1acfd1999-10-29 23:09:13 +0000506 set_term(0);
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000507 setsid();
Eric Andersencc8ed391999-10-05 16:24:54 +0000508
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000509 /* Make sure PATH is set to something sane */
Eric Andersenb186d981999-12-03 09:19:54 +0000510 putenv(_PATH_STDPATH);
Eric Andersencc8ed391999-10-05 16:24:54 +0000511
Eric Andersen219d6f51999-11-02 19:43:01 +0000512
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000513 /* Hello world */
Eric Andersen7f1acfd1999-10-29 23:09:13 +0000514#ifndef DEBUG_INIT
Eric Andersen3ae0c781999-11-04 01:13:21 +0000515 message(CONSOLE|LOG, hello_msg_format, BB_VER, BB_BT);
Eric Andersen7f1acfd1999-10-29 23:09:13 +0000516#else
Eric Andersen3ae0c781999-11-04 01:13:21 +0000517 message(CONSOLE|LOG, hello_msg_format, getpid(), BB_VER, BB_BT);
Eric Andersen7f1acfd1999-10-29 23:09:13 +0000518#endif
Eric Andersencc8ed391999-10-05 16:24:54 +0000519
Eric Andersenc7c41d31999-10-28 00:24:35 +0000520
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000521 /* Mount /proc */
Eric Andersene18c75a1999-11-05 04:23:05 +0000522 if (mount ("proc", "/proc", "proc", 0, 0) == 0) {
Eric Andersen3ae0c781999-11-04 01:13:21 +0000523 message(CONSOLE|LOG, "Mounting /proc: done.\n");
Eric Andersene18c75a1999-11-05 04:23:05 +0000524 kernel_version = get_kernel_revision();
525 } else
Eric Andersen3ae0c781999-11-04 01:13:21 +0000526 message(CONSOLE|LOG, "Mounting /proc: failed!\n");
Eric Andersencc8ed391999-10-05 16:24:54 +0000527
Eric Andersen219d6f51999-11-02 19:43:01 +0000528 /* Make sure there is enough memory to do something useful. */
529 check_memory();
Eric Andersencc8ed391999-10-05 16:24:54 +0000530
Eric Andersend00c2621999-12-07 08:37:31 +0000531 /* Check if we are supposed to be in single user mode */
532 if ( argc > 1 && (!strcmp(argv[1], "single") ||
533 !strcmp(argv[1], "-s") || !strcmp(argv[1], "1"))) {
534 single = TRUE;
535 tty1_command = shell_command;
536 tty2_command = shell_command;
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000537 }
538
Eric Andersend00c2621999-12-07 08:37:31 +0000539 /* Make sure an init script exists before trying to run it */
540 if (single==FALSE && stat(INITSCRIPT, &statbuf)==0) {
541 run_rc = TRUE;
542 wait_for_enter_tty1 = FALSE;
543 tty1_command = rc_script_command;
544 }
545
546 /* Make sure /sbin/getty exists before trying to run it */
547 if (stat(GETTY, &statbuf)==0) {
548 char* where;
Eric Andersen84b00921999-12-11 04:16:51 +0000549 /* First do tty2 */
Eric Andersend00c2621999-12-07 08:37:31 +0000550 wait_for_enter_tty2 = FALSE;
Eric Andersen84b00921999-12-11 04:16:51 +0000551 where = strrchr( second_console, '/');
Eric Andersend00c2621999-12-07 08:37:31 +0000552 if ( where != NULL) {
Eric Andersen84b00921999-12-11 04:16:51 +0000553 where++;
554 strncpy( which_vt2, where, sizeof(which_vt2));
Eric Andersend00c2621999-12-07 08:37:31 +0000555 }
556 tty2_command = getty2_command;
Eric Andersen84b00921999-12-11 04:16:51 +0000557
Eric Andersend00c2621999-12-07 08:37:31 +0000558 /* Check on hooking a getty onto tty1 */
559 if (run_rc == FALSE && single==FALSE) {
560 wait_for_enter_tty1 = FALSE;
Eric Andersen84b00921999-12-11 04:16:51 +0000561 where = strrchr( console, '/');
Eric Andersend00c2621999-12-07 08:37:31 +0000562 if ( where != NULL) {
Eric Andersen84b00921999-12-11 04:16:51 +0000563 where++;
564 strncpy( which_vt1, where, sizeof(which_vt1));
Eric Andersend00c2621999-12-07 08:37:31 +0000565 }
566 tty1_command = getty1_command;
567 }
568 }
569
Eric Andersen219d6f51999-11-02 19:43:01 +0000570
Eric Andersend00c2621999-12-07 08:37:31 +0000571 /* Ok, now launch the tty1_command and tty2_command */
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000572 for (;;) {
Eric Andersenc7c41d31999-10-28 00:24:35 +0000573 pid_t wpid;
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000574 int status;
575
Eric Andersend00c2621999-12-07 08:37:31 +0000576 if (pid1 == 0 && tty1_command) {
577 pid1 = run(tty1_command, console, wait_for_enter_tty1);
Eric Andersencc8ed391999-10-05 16:24:54 +0000578 }
Eric Andersend00c2621999-12-07 08:37:31 +0000579#ifdef BB_FEATURE_INIT_SECOND_CONSOLE
580 if (pid2 == 0 && tty2_command && second_console) {
581 pid2 = run(tty2_command, second_console, wait_for_enter_tty2);
Eric Andersencc8ed391999-10-05 16:24:54 +0000582 }
Eric Andersend00c2621999-12-07 08:37:31 +0000583#endif
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000584 wpid = wait(&status);
Eric Andersena7456061999-10-27 02:31:32 +0000585 if (wpid > 0 ) {
Eric Andersen3ae0c781999-11-04 01:13:21 +0000586 message(LOG, "pid %d exited, status=%x.\n", wpid, status);
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000587 }
Eric Andersencf8c9cf1999-11-05 00:31:46 +0000588 /* Don't respawn init script if it exits */
Eric Andersen219d6f51999-11-02 19:43:01 +0000589 if (wpid == pid1) {
Eric Andersencf8c9cf1999-11-05 00:31:46 +0000590 if (run_rc == FALSE) {
591 pid1 = 0;
Eric Andersenb6a44b81999-11-13 04:47:09 +0000592 }
Eric Andersen4c781471999-11-26 08:12:56 +0000593#ifdef BB_INIT_CMD_IF_RC_SCRIPT_EXITS
Eric Andersencf8c9cf1999-11-05 00:31:46 +0000594 else {
Eric Andersenb6a44b81999-11-13 04:47:09 +0000595 pid1 = 0;
Eric Andersen219d6f51999-11-02 19:43:01 +0000596 run_rc=FALSE;
Eric Andersend00c2621999-12-07 08:37:31 +0000597 wait_for_enter_tty1=TRUE;
598 tty1_command=rc_exit_command;
Eric Andersen219d6f51999-11-02 19:43:01 +0000599 }
Eric Andersencf8c9cf1999-11-05 00:31:46 +0000600#endif
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000601 }
Eric Andersend00c2621999-12-07 08:37:31 +0000602#ifdef BB_FEATURE_INIT_SECOND_CONSOLE
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000603 if (wpid == pid2) {
604 pid2 = 0;
605 }
Eric Andersend00c2621999-12-07 08:37:31 +0000606#endif
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000607 sleep(1);
608 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000609}