blob: dbd9f281211342f0d16faa398915e2eb945fd504 [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>
Eric Andersen4c781471999-11-26 08:12:56 +000044#ifdef BB_SYSLOGD
45#include <sys/syslog.h>
46#endif
Eric Andersencc8ed391999-10-05 16:24:54 +000047
Eric Andersen0ecb54a1999-12-05 23:24:55 +000048#if ! defined BB_FEATURE_USE_PROCFS
49#error Sorry, I depend on the /proc filesystem right now.
50#endif
51
52
Eric Andersenbe971d61999-11-03 16:52:50 +000053#define VT_PRIMARY "/dev/tty1" /* Primary virtual console */
54#define VT_SECONDARY "/dev/tty2" /* Virtual console */
55#define VT_LOG "/dev/tty3" /* Virtual console */
Eric Andersen219d6f51999-11-02 19:43:01 +000056#define SERIAL_CON0 "/dev/ttyS0" /* Primary serial console */
57#define SERIAL_CON1 "/dev/ttyS1" /* Serial console */
Eric Andersend00c2621999-12-07 08:37:31 +000058#define GETTY "/sbin/getty" /* Default location of getty */
Eric Andersen8a8fbb81999-10-26 00:18:56 +000059#define SHELL "/bin/sh" /* Default shell */
Eric Andersen219d6f51999-11-02 19:43:01 +000060#define INITSCRIPT "/etc/init.d/rcS" /* Initscript. */
Eric Andersen0460ff21999-10-25 23:32:44 +000061
Eric Andersen3ae0c781999-11-04 01:13:21 +000062#define LOG 0x1
63#define CONSOLE 0x2
Eric Andersenb186d981999-12-03 09:19:54 +000064static char *console = _PATH_CONSOLE;
Eric Andersenbe971d61999-11-03 16:52:50 +000065static char *second_console = VT_SECONDARY;
66static char *log = VT_LOG;
Eric Andersene18c75a1999-11-05 04:23:05 +000067static int kernel_version = 0;
Eric Andersen0460ff21999-10-25 23:32:44 +000068
69
70/* try to open up the specified device */
Eric Andersen8a8fbb81999-10-26 00:18:56 +000071int device_open(char *device, int mode)
Eric Andersencc8ed391999-10-05 16:24:54 +000072{
Eric Andersen0460ff21999-10-25 23:32:44 +000073 int m, f, fd = -1;
Eric Andersen8a8fbb81999-10-26 00:18:56 +000074
Eric Andersen7f1acfd1999-10-29 23:09:13 +000075 m = mode | O_NONBLOCK;
Eric Andersen8a8fbb81999-10-26 00:18:56 +000076
Eric Andersen0460ff21999-10-25 23:32:44 +000077 /* Retry up to 5 times */
Eric Andersen8a8fbb81999-10-26 00:18:56 +000078 for (f = 0; f < 5; f++)
79 if ((fd = open(device, m)) >= 0)
80 break;
81 if (fd < 0)
82 return fd;
Eric Andersen3ae0c781999-11-04 01:13:21 +000083 /* Reset original flags. */
Eric Andersen0460ff21999-10-25 23:32:44 +000084 if (m != mode)
85 fcntl(fd, F_SETFL, mode);
86 return fd;
87}
Eric Andersencc8ed391999-10-05 16:24:54 +000088
Eric Andersen3ae0c781999-11-04 01:13:21 +000089/* print a message to the specified device:
90 * device may be bitwise-or'd from LOG | CONSOLE */
91void message(int device, char *fmt, ...)
Eric Andersen0460ff21999-10-25 23:32:44 +000092{
93 int fd;
94 va_list arguments;
Eric Andersen4c781471999-11-26 08:12:56 +000095#ifdef BB_SYSLOGD
96
97 /* Log the message to syslogd */
98 if (device & LOG ) {
99 char msg[1024];
100 va_start(arguments, fmt);
101 vsnprintf(msg, sizeof(msg), fmt, arguments);
102 va_end(arguments);
103 syslog(LOG_DAEMON|LOG_NOTICE, msg);
104 }
105
106#else
107 static int log_fd=-1;
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000108
Eric Andersen3ae0c781999-11-04 01:13:21 +0000109 /* Take full control of the log tty, and never close it.
110 * It's mine, all mine! Muhahahaha! */
Eric Andersen07e52971999-11-07 07:38:08 +0000111 if (log_fd < 0) {
112 if (log == NULL) {
113 /* don't even try to log, because there is no such console */
114 log_fd = -2;
115 /* log to main console instead */
116 device = CONSOLE;
117 }
118 else if ((log_fd = device_open(log, O_RDWR|O_NDELAY)) < 0) {
Eric Andersen3ae0c781999-11-04 01:13:21 +0000119 log_fd=-1;
120 fprintf(stderr, "Bummer, can't write to log on %s!\r\n", log);
121 fflush(stderr);
122 return;
123 }
124 }
Eric Andersen07e52971999-11-07 07:38:08 +0000125 if ( (device & LOG) && (log_fd >= 0) ) {
Eric Andersen0460ff21999-10-25 23:32:44 +0000126 va_start(arguments, fmt);
Eric Andersen3ae0c781999-11-04 01:13:21 +0000127 vdprintf(log_fd, fmt, arguments);
Eric Andersena7456061999-10-27 02:31:32 +0000128 va_end(arguments);
129 }
Eric Andersen4c781471999-11-26 08:12:56 +0000130#endif
131
Eric Andersen3ae0c781999-11-04 01:13:21 +0000132 if (device & CONSOLE) {
Eric Andersended62591999-11-18 00:19:26 +0000133 /* Always send console messages to /dev/console so people will see them. */
Eric Andersenb186d981999-12-03 09:19:54 +0000134 if ((fd = device_open(_PATH_CONSOLE, O_WRONLY|O_NOCTTY|O_NDELAY)) >= 0) {
Eric Andersen3ae0c781999-11-04 01:13:21 +0000135 va_start(arguments, fmt);
136 vdprintf(fd, fmt, arguments);
137 va_end(arguments);
138 close(fd);
139 } else {
140 fprintf(stderr, "Bummer, can't print: ");
141 va_start(arguments, fmt);
142 vfprintf(stderr, fmt, arguments);
143 fflush(stderr);
144 va_end(arguments);
145 }
146 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000147}
148
Eric Andersen3ae0c781999-11-04 01:13:21 +0000149
Eric Andersen0460ff21999-10-25 23:32:44 +0000150/* Set terminal settings to reasonable defaults */
Eric Andersenc7c41d31999-10-28 00:24:35 +0000151void set_term( int fd)
Eric Andersencc8ed391999-10-05 16:24:54 +0000152{
Eric Andersen0460ff21999-10-25 23:32:44 +0000153 struct termios tty;
Eric Andersen08b10341999-11-19 02:38:58 +0000154#if 0
Eric Andersen3ae0c781999-11-04 01:13:21 +0000155 static const char control_characters[] = {
156 '\003', '\034', '\177', '\030', '\004', '\0',
157 '\1', '\0', '\021', '\023', '\032', '\0', '\022',
158 '\017', '\027', '\026', '\0'
159 };
Eric Andersen08b10341999-11-19 02:38:58 +0000160#else
161 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 };
166#endif
Eric Andersencc8ed391999-10-05 16:24:54 +0000167
Eric Andersenc7c41d31999-10-28 00:24:35 +0000168 tcgetattr(fd, &tty);
Eric Andersencc8ed391999-10-05 16:24:54 +0000169
Eric Andersen08b10341999-11-19 02:38:58 +0000170 /* set control chars */
171 memcpy(tty.c_cc, control_characters, sizeof(control_characters));
Eric Andersencc8ed391999-10-05 16:24:54 +0000172
Eric Andersen219d6f51999-11-02 19:43:01 +0000173 /* use line dicipline 0 */
Eric Andersen7f1acfd1999-10-29 23:09:13 +0000174 tty.c_line = 0;
175
Eric Andersen08b10341999-11-19 02:38:58 +0000176 /* Make it be sane */
177 //tty.c_cflag &= CBAUD|CBAUDEX|CSIZE|CSTOPB|PARENB|PARODD;
178 //tty.c_cflag |= HUPCL|CLOCAL;
179
180 /* input modes */
181 tty.c_iflag = ICRNL|IXON|IXOFF;
182
Eric Andersen7f1acfd1999-10-29 23:09:13 +0000183 /* output modes */
Eric Andersenc7c41d31999-10-28 00:24:35 +0000184 tty.c_oflag = OPOST|ONLCR;
Eric Andersen7f1acfd1999-10-29 23:09:13 +0000185
186 /* local modes */
Eric Andersen08b10341999-11-19 02:38:58 +0000187 tty.c_lflag = ISIG|ICANON|ECHO|ECHOE|ECHOK|ECHOCTL|ECHOKE|IEXTEN;
Eric Andersencc8ed391999-10-05 16:24:54 +0000188
Eric Andersenc7c41d31999-10-28 00:24:35 +0000189 tcsetattr(fd, TCSANOW, &tty);
Eric Andersencc8ed391999-10-05 16:24:54 +0000190}
191
Eric Andersen219d6f51999-11-02 19:43:01 +0000192/* How much memory does this machine have? */
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000193static int mem_total()
Eric Andersencc8ed391999-10-05 16:24:54 +0000194{
Eric Andersenabc7d591999-10-19 00:27:50 +0000195 char s[80];
Eric Andersen219d6f51999-11-02 19:43:01 +0000196 char *p = "/proc/meminfo";
Eric Andersenabc7d591999-10-19 00:27:50 +0000197 FILE *f;
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000198 const char pattern[] = "MemTotal:";
Eric Andersencc8ed391999-10-05 16:24:54 +0000199
Eric Andersen219d6f51999-11-02 19:43:01 +0000200 if ((f = fopen(p, "r")) < 0) {
Eric Andersen3ae0c781999-11-04 01:13:21 +0000201 message(LOG, "Error opening %s: %s\n", p, strerror( errno));
Eric Andersen219d6f51999-11-02 19:43:01 +0000202 return -1;
203 }
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000204 while (NULL != fgets(s, 79, f)) {
205 p = strstr(s, pattern);
Eric Andersenabc7d591999-10-19 00:27:50 +0000206 if (NULL != p) {
207 fclose(f);
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000208 return (atoi(p + strlen(pattern)));
Eric Andersenabc7d591999-10-19 00:27:50 +0000209 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000210 }
Eric Andersenabc7d591999-10-19 00:27:50 +0000211 return -1;
Eric Andersencc8ed391999-10-05 16:24:54 +0000212}
213
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000214static void console_init()
Eric Andersen0460ff21999-10-25 23:32:44 +0000215{
216 int fd;
217 int tried_devcons = 0;
Eric Andersen219d6f51999-11-02 19:43:01 +0000218 int tried_vtprimary = 0;
Eric Andersen07e52971999-11-07 07:38:08 +0000219 struct serial_struct sr;
Eric Andersen0460ff21999-10-25 23:32:44 +0000220 char *s;
221
Eric Andersen219d6f51999-11-02 19:43:01 +0000222 if ((s = getenv("CONSOLE")) != NULL) {
Eric Andersen0460ff21999-10-25 23:32:44 +0000223 console = s;
Eric Andersen07e52971999-11-07 07:38:08 +0000224 }
Eric Andersenfbb39c81999-11-08 17:00:52 +0000225#if #cpu(sparc)
Eric Andersen07e52971999-11-07 07:38:08 +0000226 /* sparc kernel supports console=tty[ab] parameter which is also
227 * passed to init, so catch it here */
228 else if ((s = getenv("console")) != NULL) {
229 /* remap tty[ab] to /dev/ttyS[01] */
230 if (strcmp( s, "ttya" )==0)
231 console = SERIAL_CON0;
232 else if (strcmp( s, "ttyb" )==0)
233 console = SERIAL_CON1;
234 }
Eric Andersen219d6f51999-11-02 19:43:01 +0000235#endif
Eric Andersen07e52971999-11-07 07:38:08 +0000236 else {
237 struct vt_stat vt;
238 static char the_console[13];
239
240 console = the_console;
241 /* 2.2 kernels: identify the real console backend and try to use it */
Eric Andersen08b10341999-11-19 02:38:58 +0000242 if (ioctl(0, TIOCGSERIAL, &sr) == 0) {
Eric Andersen07e52971999-11-07 07:38:08 +0000243 /* this is a serial console */
244 snprintf( the_console, sizeof the_console, "/dev/ttyS%d", sr.line );
245 }
246 else if (ioctl(0, VT_GETSTATE, &vt) == 0) {
247 /* this is linux virtual tty */
248 snprintf( the_console, sizeof the_console, "/dev/tty%d", vt.v_active );
249 } else {
Eric Andersenb186d981999-12-03 09:19:54 +0000250 console = _PATH_CONSOLE;
Eric Andersen07e52971999-11-07 07:38:08 +0000251 tried_devcons++;
252 }
Eric Andersen0460ff21999-10-25 23:32:44 +0000253 }
Eric Andersena7456061999-10-27 02:31:32 +0000254
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000255 while ((fd = open(console, O_RDONLY | O_NONBLOCK)) < 0) {
Eric Andersen219d6f51999-11-02 19:43:01 +0000256 /* Can't open selected console -- try /dev/console */
Eric Andersen0460ff21999-10-25 23:32:44 +0000257 if (!tried_devcons) {
258 tried_devcons++;
Eric Andersenb186d981999-12-03 09:19:54 +0000259 console = _PATH_CONSOLE;
Eric Andersen0460ff21999-10-25 23:32:44 +0000260 continue;
261 }
Eric Andersenbe971d61999-11-03 16:52:50 +0000262 /* Can't open selected console -- try vt1 */
263 if (!tried_vtprimary) {
264 tried_vtprimary++;
265 console = VT_PRIMARY;
266 continue;
267 }
Eric Andersen0460ff21999-10-25 23:32:44 +0000268 break;
269 }
270 if (fd < 0)
Eric Andersen219d6f51999-11-02 19:43:01 +0000271 /* Perhaps we should panic here? */
Eric Andersen0460ff21999-10-25 23:32:44 +0000272 console = "/dev/null";
Eric Andersen07e52971999-11-07 07:38:08 +0000273 else {
274 /* check for serial console and disable logging to tty3 & running a
275 * shell to tty2 */
276 if (ioctl(0,TIOCGSERIAL,&sr) == 0) {
Eric Andersen08b10341999-11-19 02:38:58 +0000277 message(LOG|CONSOLE, "serial console detected. Disabling 2nd virtual terminal.\r\n", console );
Eric Andersen07e52971999-11-07 07:38:08 +0000278 log = NULL;
279 second_console = NULL;
280 }
Eric Andersen0460ff21999-10-25 23:32:44 +0000281 close(fd);
Eric Andersen07e52971999-11-07 07:38:08 +0000282 }
Eric Andersen3ae0c781999-11-04 01:13:21 +0000283 message(LOG, "console=%s\n", console );
Eric Andersen0460ff21999-10-25 23:32:44 +0000284}
285
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000286static int waitfor(int pid)
Eric Andersen0460ff21999-10-25 23:32:44 +0000287{
288 int status, wpid;
289
Eric Andersen3ae0c781999-11-04 01:13:21 +0000290 message(LOG, "Waiting for process %d.\n", pid);
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000291 while ((wpid = wait(&status)) != pid) {
292 if (wpid > 0)
Eric Andersen3ae0c781999-11-04 01:13:21 +0000293 message(LOG, "pid %d exited, status=0x%x.\n", wpid, status);
Eric Andersen0460ff21999-10-25 23:32:44 +0000294 }
295 return wpid;
296}
297
Eric Andersena7456061999-10-27 02:31:32 +0000298
Eric Andersenc7c41d31999-10-28 00:24:35 +0000299static pid_t run(const char * const* command,
300 char *terminal, int get_enter)
Eric Andersen0460ff21999-10-25 23:32:44 +0000301{
Eric Andersen2f6c04f1999-11-01 23:59:44 +0000302 int fd;
Eric Andersenc7c41d31999-10-28 00:24:35 +0000303 pid_t pid;
Eric Andersen219d6f51999-11-02 19:43:01 +0000304 const char * const* cmd = command+1;
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000305 static const char press_enter[] =
Eric Andersen0460ff21999-10-25 23:32:44 +0000306 "\nPlease press Enter to activate this console. ";
307
Eric Andersen0460ff21999-10-25 23:32:44 +0000308 if ((pid = fork()) == 0) {
309 /* Clean up */
310 close(0);
311 close(1);
312 close(2);
313 setsid();
314
Eric Andersen2f6c04f1999-11-01 23:59:44 +0000315 /* Reset signal handlers set for parent process */
316 signal(SIGUSR1, SIG_DFL);
317 signal(SIGUSR2, SIG_DFL);
318 signal(SIGINT, SIG_DFL);
319 signal(SIGTERM, SIG_DFL);
320
321 if ((fd = device_open(terminal, O_RDWR)) < 0) {
Eric Andersen3ae0c781999-11-04 01:13:21 +0000322 message(LOG, "Bummer, can't open %s\r\n", terminal);
Eric Andersen2f6c04f1999-11-01 23:59:44 +0000323 exit(-1);
324 }
325 dup(fd);
326 dup(fd);
Eric Andersenc7c41d31999-10-28 00:24:35 +0000327 tcsetpgrp(0, getpgrp());
328 set_term(0);
Eric Andersen0460ff21999-10-25 23:32:44 +0000329
Eric Andersen2f6c04f1999-11-01 23:59:44 +0000330 if (get_enter==TRUE) {
Eric Andersen0460ff21999-10-25 23:32:44 +0000331 /*
332 * Save memory by not exec-ing anything large (like a shell)
333 * before the user wants it. This is critical if swap is not
334 * enabled and the system has low memory. Generally this will
335 * be run on the second virtual console, and the first will
336 * be allowed to start a shell or whatever an init script
337 * specifies.
338 */
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000339 char c;
Eric Andersen3ae0c781999-11-04 01:13:21 +0000340 message(LOG, "Waiting for enter to start '%s' (pid %d, console %s)\r\n",
341 *cmd, getpid(), terminal );
Eric Andersen7f1acfd1999-10-29 23:09:13 +0000342 write(1, press_enter, sizeof(press_enter) - 1);
Eric Andersen0460ff21999-10-25 23:32:44 +0000343 read(0, &c, 1);
344 }
345
346 /* Log the process name and args */
Eric Andersen3ae0c781999-11-04 01:13:21 +0000347 message(LOG, "Starting pid %d, console %s: '", getpid(), terminal);
348 while ( *cmd) message(LOG, "%s ", *cmd++);
349 message(LOG, "'\r\n");
Eric Andersen2f6c04f1999-11-01 23:59:44 +0000350
Eric Andersenc7c41d31999-10-28 00:24:35 +0000351 /* Now run it. The new program will take over this PID,
Eric Andersena7456061999-10-27 02:31:32 +0000352 * so nothing further in init.c should be run. */
Eric Andersenc7c41d31999-10-28 00:24:35 +0000353 execvp(*command, (char**)command+1);
Eric Andersen0460ff21999-10-25 23:32:44 +0000354
Eric Andersen3ae0c781999-11-04 01:13:21 +0000355 message(LOG, "Bummer, could not run '%s'\n", command);
Eric Andersen0460ff21999-10-25 23:32:44 +0000356 exit(-1);
357 }
358 return pid;
359}
360
Eric Andersen219d6f51999-11-02 19:43:01 +0000361/* Make sure there is enough memory to do something useful. *
362 * Calls swapon if needed so be sure /proc is mounted. */
363static void check_memory()
364{
365 struct stat statbuf;
366 const char* const swap_on_cmd[] =
367 { "/bin/swapon", "swapon", "-a", 0};
Eric Andersen219d6f51999-11-02 19:43:01 +0000368
369 if (mem_total() > 3500)
370 return;
371
372 if (stat("/etc/fstab", &statbuf) == 0) {
373 /* Try to turn on swap */
374 waitfor(run(swap_on_cmd, log, FALSE));
375 if (mem_total() < 3500)
376 goto goodnight;
377 } else
378 goto goodnight;
379 return;
380
381goodnight:
Eric Andersen3ae0c781999-11-04 01:13:21 +0000382 message(CONSOLE, "Sorry, your computer does not have enough memory.\r\n");
Eric Andersen219d6f51999-11-02 19:43:01 +0000383 while (1) sleep(1);
384}
385
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000386static void shutdown_system(void)
Eric Andersencc8ed391999-10-05 16:24:54 +0000387{
Eric Andersen2f6c04f1999-11-01 23:59:44 +0000388 const char* const swap_off_cmd[] = { "swapoff", "swapoff", "-a", 0};
389 const char* const umount_cmd[] = { "umount", "umount", "-a", "-n", 0};
Eric Andersencc8ed391999-10-05 16:24:54 +0000390
Eric Andersen2f6c04f1999-11-01 23:59:44 +0000391#ifndef DEBUG_INIT
Eric Andersen0460ff21999-10-25 23:32:44 +0000392 /* Allow Ctrl-Alt-Del to reboot system. */
393 reboot(RB_ENABLE_CAD);
Eric Andersen2f6c04f1999-11-01 23:59:44 +0000394#endif
Eric Andersen08b10341999-11-19 02:38:58 +0000395 message(CONSOLE, "\r\nThe system is going down NOW !!\r\n");
Eric Andersen3ae0c781999-11-04 01:13:21 +0000396 sync();
Eric Andersen0460ff21999-10-25 23:32:44 +0000397 /* Send signals to every process _except_ pid 1 */
Eric Andersen3ae0c781999-11-04 01:13:21 +0000398 message(CONSOLE, "Sending SIGHUP to all processes.\r\n");
Eric Andersen2f6c04f1999-11-01 23:59:44 +0000399#ifndef DEBUG_INIT
Eric Andersen0460ff21999-10-25 23:32:44 +0000400 kill(-1, SIGHUP);
Eric Andersen2f6c04f1999-11-01 23:59:44 +0000401#endif
Eric Andersen0460ff21999-10-25 23:32:44 +0000402 sleep(2);
403 sync();
Eric Andersen3ae0c781999-11-04 01:13:21 +0000404 message(CONSOLE, "Sending SIGKILL to all processes.\r\n");
Eric Andersen2f6c04f1999-11-01 23:59:44 +0000405#ifndef DEBUG_INIT
Eric Andersen0460ff21999-10-25 23:32:44 +0000406 kill(-1, SIGKILL);
Eric Andersen2f6c04f1999-11-01 23:59:44 +0000407#endif
Eric Andersen0460ff21999-10-25 23:32:44 +0000408 sleep(1);
Eric Andersen08b10341999-11-19 02:38:58 +0000409 message(CONSOLE, "Disabling swap.\r\n");
Eric Andersen3ae0c781999-11-04 01:13:21 +0000410 waitfor(run( swap_off_cmd, console, FALSE));
Eric Andersen08b10341999-11-19 02:38:58 +0000411 message(CONSOLE, "Unmounting filesystems.\r\n");
Eric Andersen3ae0c781999-11-04 01:13:21 +0000412 waitfor(run( umount_cmd, console, FALSE));
Eric Andersen0460ff21999-10-25 23:32:44 +0000413 sync();
Eric Andersene18c75a1999-11-05 04:23:05 +0000414 if (kernel_version > 0 && kernel_version <= 2 * 65536 + 2 * 256 + 11) {
Eric Andersencf8c9cf1999-11-05 00:31:46 +0000415 /* bdflush, kupdate not needed for kernels >2.2.11 */
Eric Andersene18c75a1999-11-05 04:23:05 +0000416 message(CONSOLE, "Flushing buffers.\r\n");
Eric Andersencf8c9cf1999-11-05 00:31:46 +0000417 bdflush(1, 0);
418 sync();
419 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000420}
421
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000422static void halt_signal(int sig)
Eric Andersencc8ed391999-10-05 16:24:54 +0000423{
Eric Andersenabc7d591999-10-19 00:27:50 +0000424 shutdown_system();
Eric Andersen3ae0c781999-11-04 01:13:21 +0000425 message(CONSOLE,
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000426 "The system is halted. Press CTRL-ALT-DEL or turn off power\r\n");
Eric Andersen3ae0c781999-11-04 01:13:21 +0000427 sync();
Eric Andersen2f6c04f1999-11-01 23:59:44 +0000428#ifndef DEBUG_INIT
Eric Andersenb186d981999-12-03 09:19:54 +0000429 while (1) sleep(1);
Eric Andersen3ae0c781999-11-04 01:13:21 +0000430 reboot(RB_HALT_SYSTEM);
431 //reboot(RB_POWER_OFF);
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 Andersenb186d981999-12-03 09:19:54 +0000441 while (1) sleep(1);
Eric Andersen2f6c04f1999-11-01 23:59:44 +0000442#ifndef DEBUG_INIT
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000443 reboot(RB_AUTOBOOT);
Eric Andersen2f6c04f1999-11-01 23:59:44 +0000444#endif
Eric Andersenabc7d591999-10-19 00:27:50 +0000445 exit(0);
Eric Andersencc8ed391999-10-05 16:24:54 +0000446}
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000447
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000448extern int init_main(int argc, char **argv)
Eric Andersen0460ff21999-10-25 23:32:44 +0000449{
Eric Andersend00c2621999-12-07 08:37:31 +0000450 int run_rc = FALSE;
451 int single = FALSE;
452 int wait_for_enter_tty1 = TRUE;
453 int wait_for_enter_tty2 = TRUE;
Eric Andersenc7c41d31999-10-28 00:24:35 +0000454 pid_t pid1 = 0;
455 pid_t pid2 = 0;
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000456 struct stat statbuf;
Eric Andersenb6a44b81999-11-13 04:47:09 +0000457 const char* const rc_script_command[] = { INITSCRIPT, INITSCRIPT, 0};
Eric Andersend00c2621999-12-07 08:37:31 +0000458 const char* const getty1_command[] = { GETTY, GETTY, VT_PRIMARY, 0};
459 const char* const getty2_command[] = { GETTY, GETTY, VT_SECONDARY, 0};
Eric Andersenb6a44b81999-11-13 04:47:09 +0000460 const char* const shell_command[] = { SHELL, "-" SHELL, 0};
Eric Andersenb6a44b81999-11-13 04:47:09 +0000461 const char* const* tty1_command = shell_command;
Eric Andersend00c2621999-12-07 08:37:31 +0000462 const char* const* tty2_command = shell_command;
Eric Andersen4c781471999-11-26 08:12:56 +0000463#ifdef BB_INIT_CMD_IF_RC_SCRIPT_EXITS
464 const char* const rc_exit_command[] = { "BB_INIT_CMD_IF_RC_SCRIPT_EXITS",
465 "BB_INIT_CMD_IF_RC_SCRIPT_EXITS", 0 };
Eric Andersenb6a44b81999-11-13 04:47:09 +0000466#endif
467
Eric Andersen2f6c04f1999-11-01 23:59:44 +0000468#ifdef DEBUG_INIT
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000469 char *hello_msg_format =
Eric Andersen04579781999-10-29 23:12:50 +0000470 "init(%d) started: BusyBox v%s (%s) multi-call binary\r\n";
Eric Andersen2f6c04f1999-11-01 23:59:44 +0000471#else
472 char *hello_msg_format =
473 "init started: BusyBox v%s (%s) multi-call binary\r\n";
Eric Andersen7f1acfd1999-10-29 23:09:13 +0000474#endif
Eric Andersen0460ff21999-10-25 23:32:44 +0000475
Eric Andersen219d6f51999-11-02 19:43:01 +0000476
Eric Andersend73dc5b1999-11-10 23:13:02 +0000477#ifndef DEBUG_INIT
Eric Andersen07274581999-11-21 21:50:07 +0000478 /* Expect to be PID 1 iff we are run as init (not linuxrc) */
479 if (getpid() != 1 && strstr(argv[0], "init")!=NULL ) {
Eric Andersend73dc5b1999-11-10 23:13:02 +0000480 usage( "init\n\nInit is the parent of all processes.\n\n"
481 "This version of init is designed to be run only by the kernel\n");
482 }
483#endif
484
Eric Andersen2f6c04f1999-11-01 23:59:44 +0000485 /* Set up sig handlers -- be sure to
486 * clear all of these in run() */
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000487 signal(SIGUSR1, halt_signal);
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000488 signal(SIGUSR2, reboot_signal);
489 signal(SIGINT, reboot_signal);
490 signal(SIGTERM, reboot_signal);
Eric Andersen0460ff21999-10-25 23:32:44 +0000491
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000492 /* Turn off rebooting via CTL-ALT-DEL -- we get a
493 * SIGINT on CAD so we can shut things down gracefully... */
Eric Andersen2f6c04f1999-11-01 23:59:44 +0000494#ifndef DEBUG_INIT
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000495 reboot(RB_DISABLE_CAD);
Eric Andersen7f1acfd1999-10-29 23:09:13 +0000496#endif
Eric Andersen219d6f51999-11-02 19:43:01 +0000497
Eric Andersenc7c41d31999-10-28 00:24:35 +0000498 /* Figure out where the default console should be */
499 console_init();
Eric Andersen0460ff21999-10-25 23:32:44 +0000500
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000501 /* Close whatever files are open, and reset the console. */
502 close(0);
503 close(1);
504 close(2);
Eric Andersen7f1acfd1999-10-29 23:09:13 +0000505 set_term(0);
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000506 setsid();
Eric Andersencc8ed391999-10-05 16:24:54 +0000507
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000508 /* Make sure PATH is set to something sane */
Eric Andersenb186d981999-12-03 09:19:54 +0000509 putenv(_PATH_STDPATH);
Eric Andersencc8ed391999-10-05 16:24:54 +0000510
Eric Andersen219d6f51999-11-02 19:43:01 +0000511
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000512 /* Hello world */
Eric Andersen7f1acfd1999-10-29 23:09:13 +0000513#ifndef DEBUG_INIT
Eric Andersen3ae0c781999-11-04 01:13:21 +0000514 message(CONSOLE|LOG, hello_msg_format, BB_VER, BB_BT);
Eric Andersen7f1acfd1999-10-29 23:09:13 +0000515#else
Eric Andersen3ae0c781999-11-04 01:13:21 +0000516 message(CONSOLE|LOG, hello_msg_format, getpid(), BB_VER, BB_BT);
Eric Andersen7f1acfd1999-10-29 23:09:13 +0000517#endif
Eric Andersencc8ed391999-10-05 16:24:54 +0000518
Eric Andersenc7c41d31999-10-28 00:24:35 +0000519
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000520 /* Mount /proc */
Eric Andersene18c75a1999-11-05 04:23:05 +0000521 if (mount ("proc", "/proc", "proc", 0, 0) == 0) {
Eric Andersen3ae0c781999-11-04 01:13:21 +0000522 message(CONSOLE|LOG, "Mounting /proc: done.\n");
Eric Andersene18c75a1999-11-05 04:23:05 +0000523 kernel_version = get_kernel_revision();
524 } else
Eric Andersen3ae0c781999-11-04 01:13:21 +0000525 message(CONSOLE|LOG, "Mounting /proc: failed!\n");
Eric Andersencc8ed391999-10-05 16:24:54 +0000526
Eric Andersen219d6f51999-11-02 19:43:01 +0000527 /* Make sure there is enough memory to do something useful. */
528 check_memory();
Eric Andersencc8ed391999-10-05 16:24:54 +0000529
Eric Andersend00c2621999-12-07 08:37:31 +0000530 /* Check if we are supposed to be in single user mode */
531 if ( argc > 1 && (!strcmp(argv[1], "single") ||
532 !strcmp(argv[1], "-s") || !strcmp(argv[1], "1"))) {
533 single = TRUE;
534 tty1_command = shell_command;
535 tty2_command = shell_command;
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000536 }
537
Eric Andersend00c2621999-12-07 08:37:31 +0000538 /* Make sure an init script exists before trying to run it */
539 if (single==FALSE && stat(INITSCRIPT, &statbuf)==0) {
540 run_rc = TRUE;
541 wait_for_enter_tty1 = FALSE;
542 tty1_command = rc_script_command;
543 }
544
545 /* Make sure /sbin/getty exists before trying to run it */
546 if (stat(GETTY, &statbuf)==0) {
547 char* where;
548 wait_for_enter_tty2 = FALSE;
549 where = strrchr( console, '/');
550 if ( where != NULL) {
551 strcpy( (char*)getty2_command[2], where);
552 }
553 tty2_command = getty2_command;
554 /* Check on hooking a getty onto tty1 */
555 if (run_rc == FALSE && single==FALSE) {
556 wait_for_enter_tty1 = FALSE;
557 where = strrchr( second_console, '/');
558 if ( where != NULL) {
559 strcpy( (char*)getty1_command[2], where);
560 }
561 tty1_command = getty1_command;
562 }
563 }
564
Eric Andersen219d6f51999-11-02 19:43:01 +0000565
Eric Andersend00c2621999-12-07 08:37:31 +0000566 /* Ok, now launch the tty1_command and tty2_command */
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000567 for (;;) {
Eric Andersenc7c41d31999-10-28 00:24:35 +0000568 pid_t wpid;
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000569 int status;
570
Eric Andersend00c2621999-12-07 08:37:31 +0000571 if (pid1 == 0 && tty1_command) {
572 pid1 = run(tty1_command, console, wait_for_enter_tty1);
Eric Andersencc8ed391999-10-05 16:24:54 +0000573 }
Eric Andersend00c2621999-12-07 08:37:31 +0000574#ifdef BB_FEATURE_INIT_SECOND_CONSOLE
575 if (pid2 == 0 && tty2_command && second_console) {
576 pid2 = run(tty2_command, second_console, wait_for_enter_tty2);
Eric Andersencc8ed391999-10-05 16:24:54 +0000577 }
Eric Andersend00c2621999-12-07 08:37:31 +0000578#endif
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000579 wpid = wait(&status);
Eric Andersena7456061999-10-27 02:31:32 +0000580 if (wpid > 0 ) {
Eric Andersen3ae0c781999-11-04 01:13:21 +0000581 message(LOG, "pid %d exited, status=%x.\n", wpid, status);
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000582 }
Eric Andersencf8c9cf1999-11-05 00:31:46 +0000583 /* Don't respawn init script if it exits */
Eric Andersen219d6f51999-11-02 19:43:01 +0000584 if (wpid == pid1) {
Eric Andersencf8c9cf1999-11-05 00:31:46 +0000585 if (run_rc == FALSE) {
586 pid1 = 0;
Eric Andersenb6a44b81999-11-13 04:47:09 +0000587 }
Eric Andersen4c781471999-11-26 08:12:56 +0000588#ifdef BB_INIT_CMD_IF_RC_SCRIPT_EXITS
Eric Andersencf8c9cf1999-11-05 00:31:46 +0000589 else {
Eric Andersenb6a44b81999-11-13 04:47:09 +0000590 pid1 = 0;
Eric Andersen219d6f51999-11-02 19:43:01 +0000591 run_rc=FALSE;
Eric Andersend00c2621999-12-07 08:37:31 +0000592 wait_for_enter_tty1=TRUE;
593 tty1_command=rc_exit_command;
Eric Andersen219d6f51999-11-02 19:43:01 +0000594 }
Eric Andersencf8c9cf1999-11-05 00:31:46 +0000595#endif
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000596 }
Eric Andersend00c2621999-12-07 08:37:31 +0000597#ifdef BB_FEATURE_INIT_SECOND_CONSOLE
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000598 if (wpid == pid2) {
599 pid2 = 0;
600 }
Eric Andersend00c2621999-12-07 08:37:31 +0000601#endif
Eric Andersen8a8fbb81999-10-26 00:18:56 +0000602 sleep(1);
603 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000604}