Erik Andersen | ccc7488 | 2000-01-27 02:40:21 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
Eric Andersen | c499601 | 1999-10-20 22:08:37 +0000 | [diff] [blame] | 2 | /* |
| 3 | * Mini init implementation for busybox |
| 4 | * |
| 5 | * |
| 6 | * Copyright (C) 1995, 1996 by Bruce Perens <bruce@pixar.com>. |
| 7 | * Adjusted by so many folks, it's impossible to keep track. |
| 8 | * |
| 9 | * This program is free software; you can redistribute it and/or modify |
| 10 | * it under the terms of the GNU General Public License as published by |
| 11 | * the Free Software Foundation; either version 2 of the License, or |
| 12 | * (at your option) any later version. |
| 13 | * |
| 14 | * This program is distributed in the hope that it will be useful, |
| 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 17 | * General Public License for more details. |
| 18 | * |
| 19 | * You should have received a copy of the GNU General Public License |
| 20 | * along with this program; if not, write to the Free Software |
| 21 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 22 | * |
| 23 | */ |
| 24 | |
Erik Andersen | e132f4b | 2000-02-09 04:16:43 +0000 | [diff] [blame] | 25 | /* Turn this on to disable all the dangerous |
| 26 | rebooting stuff when debugging. |
| 27 | #define DEBUG_INIT |
| 28 | */ |
| 29 | |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 30 | #include "internal.h" |
Erik Andersen | 42094cd | 2000-03-20 21:34:52 +0000 | [diff] [blame] | 31 | #include <asm/types.h> |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 32 | #include <errno.h> |
Erik Andersen | 42094cd | 2000-03-20 21:34:52 +0000 | [diff] [blame] | 33 | #include <linux/serial.h> /* for serial_struct */ |
| 34 | #include <linux/version.h> |
Eric Andersen | b186d98 | 1999-12-03 09:19:54 +0000 | [diff] [blame] | 35 | #include <paths.h> |
Erik Andersen | 42094cd | 2000-03-20 21:34:52 +0000 | [diff] [blame] | 36 | #include <signal.h> |
| 37 | #include <stdarg.h> |
| 38 | #include <stdio.h> |
| 39 | #include <stdlib.h> |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 40 | #include <string.h> |
Erik Andersen | 42094cd | 2000-03-20 21:34:52 +0000 | [diff] [blame] | 41 | #include <sys/fcntl.h> |
| 42 | #include <sys/ioctl.h> |
| 43 | #include <sys/kdaemon.h> |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 44 | #include <sys/mount.h> |
| 45 | #include <sys/reboot.h> |
Erik Andersen | d07ee46 | 2000-02-21 21:26:32 +0000 | [diff] [blame] | 46 | #include <sys/sysinfo.h> /* For check_free_memory() */ |
Eric Andersen | 4c78147 | 1999-11-26 08:12:56 +0000 | [diff] [blame] | 47 | #ifdef BB_SYSLOGD |
Erik Andersen | 983b51b | 2000-04-04 18:14:25 +0000 | [diff] [blame] | 48 | # include <sys/syslog.h> |
Eric Andersen | 4c78147 | 1999-11-26 08:12:56 +0000 | [diff] [blame] | 49 | #endif |
Erik Andersen | 42094cd | 2000-03-20 21:34:52 +0000 | [diff] [blame] | 50 | #include <sys/sysmacros.h> |
| 51 | #include <sys/types.h> |
| 52 | #include <sys/vt.h> /* for vt_stat */ |
| 53 | #include <sys/wait.h> |
| 54 | #include <termios.h> |
| 55 | #include <unistd.h> |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 56 | |
Erik Andersen | 983b51b | 2000-04-04 18:14:25 +0000 | [diff] [blame] | 57 | |
Erik Andersen | 183da4a | 2000-04-04 18:36:37 +0000 | [diff] [blame] | 58 | #if defined BB_FEATURE_INIT_COREDUMPS |
Erik Andersen | 983b51b | 2000-04-04 18:14:25 +0000 | [diff] [blame] | 59 | /* |
Erik Andersen | 183da4a | 2000-04-04 18:36:37 +0000 | [diff] [blame] | 60 | * When a file named CORE_ENABLE_FLAG_FILE exists, setrlimit is called |
| 61 | * before processes are spawned to set core file size as unlimited. |
| 62 | * This is for debugging only. Don't use this is production, unless |
| 63 | * you want core dumps lying about.... |
Erik Andersen | 983b51b | 2000-04-04 18:14:25 +0000 | [diff] [blame] | 64 | */ |
| 65 | #define CORE_ENABLE_FLAG_FILE "/.init_enable_core" |
| 66 | #include <sys/resource.h> |
| 67 | #include <sys/time.h> |
Erik Andersen | 183da4a | 2000-04-04 18:36:37 +0000 | [diff] [blame] | 68 | #endif |
Erik Andersen | 983b51b | 2000-04-04 18:14:25 +0000 | [diff] [blame] | 69 | |
Erik Andersen | 4d1d011 | 1999-12-17 18:44:15 +0000 | [diff] [blame] | 70 | #ifndef KERNEL_VERSION |
| 71 | #define KERNEL_VERSION(a,b,c) (((a) << 16) + ((b) << 8) + (c)) |
| 72 | #endif |
| 73 | |
Eric Andersen | 0ecb54a | 1999-12-05 23:24:55 +0000 | [diff] [blame] | 74 | |
Erik Andersen | 42094cd | 2000-03-20 21:34:52 +0000 | [diff] [blame] | 75 | #define VT_PRIMARY "/dev/tty1" /* Primary virtual console */ |
| 76 | #define VT_SECONDARY "/dev/tty2" /* Virtual console */ |
| 77 | #define VT_LOG "/dev/tty3" /* Virtual console */ |
| 78 | #define SERIAL_CON0 "/dev/ttyS0" /* Primary serial console */ |
| 79 | #define SERIAL_CON1 "/dev/ttyS1" /* Serial console */ |
| 80 | #define SHELL "/bin/sh" /* Default shell */ |
| 81 | #define INITTAB "/etc/inittab" /* inittab file location */ |
Erik Andersen | 96e2abd | 2000-01-07 11:40:44 +0000 | [diff] [blame] | 82 | #ifndef INIT_SCRIPT |
Erik Andersen | 42094cd | 2000-03-20 21:34:52 +0000 | [diff] [blame] | 83 | #define INIT_SCRIPT "/etc/init.d/rcS" /* Default sysinit script. */ |
Erik Andersen | 96e2abd | 2000-01-07 11:40:44 +0000 | [diff] [blame] | 84 | #endif |
Erik Andersen | 9c88cac | 1999-12-30 09:25:17 +0000 | [diff] [blame] | 85 | |
Erik Andersen | 42094cd | 2000-03-20 21:34:52 +0000 | [diff] [blame] | 86 | #define LOG 0x1 |
| 87 | #define CONSOLE 0x2 |
Erik Andersen | 7dc1607 | 2000-01-04 01:10:25 +0000 | [diff] [blame] | 88 | |
| 89 | /* Allowed init action types */ |
| 90 | typedef enum { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 91 | SYSINIT = 1, |
| 92 | RESPAWN, |
| 93 | ASKFIRST, |
| 94 | WAIT, |
Erik Andersen | e132f4b | 2000-02-09 04:16:43 +0000 | [diff] [blame] | 95 | ONCE, |
| 96 | CTRLALTDEL |
Erik Andersen | 7dc1607 | 2000-01-04 01:10:25 +0000 | [diff] [blame] | 97 | } initActionEnum; |
| 98 | |
Erik Andersen | 42094cd | 2000-03-20 21:34:52 +0000 | [diff] [blame] | 99 | /* A mapping between "inittab" action name strings and action type codes. */ |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 100 | typedef struct initActionType { |
| 101 | const char *name; |
| 102 | initActionEnum action; |
Erik Andersen | 7dc1607 | 2000-01-04 01:10:25 +0000 | [diff] [blame] | 103 | } initActionType; |
| 104 | |
| 105 | static const struct initActionType actions[] = { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 106 | {"sysinit", SYSINIT}, |
| 107 | {"respawn", RESPAWN}, |
| 108 | {"askfirst", ASKFIRST}, |
| 109 | {"wait", WAIT}, |
| 110 | {"once", ONCE}, |
Erik Andersen | e132f4b | 2000-02-09 04:16:43 +0000 | [diff] [blame] | 111 | {"ctrlaltdel", CTRLALTDEL}, |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 112 | {0} |
Erik Andersen | 7dc1607 | 2000-01-04 01:10:25 +0000 | [diff] [blame] | 113 | }; |
| 114 | |
Erik Andersen | 42094cd | 2000-03-20 21:34:52 +0000 | [diff] [blame] | 115 | /* Set up a linked list of initActions, to be read from inittab */ |
Erik Andersen | 7dc1607 | 2000-01-04 01:10:25 +0000 | [diff] [blame] | 116 | typedef struct initActionTag initAction; |
| 117 | struct initActionTag { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 118 | pid_t pid; |
| 119 | char process[256]; |
| 120 | char console[256]; |
| 121 | initAction *nextPtr; |
| 122 | initActionEnum action; |
Erik Andersen | 7dc1607 | 2000-01-04 01:10:25 +0000 | [diff] [blame] | 123 | }; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 124 | initAction *initActionList = NULL; |
Erik Andersen | 7dc1607 | 2000-01-04 01:10:25 +0000 | [diff] [blame] | 125 | |
| 126 | |
Erik Andersen | ac6e71f | 2000-01-08 22:04:33 +0000 | [diff] [blame] | 127 | static char *secondConsole = VT_SECONDARY; |
Erik Andersen | 42094cd | 2000-03-20 21:34:52 +0000 | [diff] [blame] | 128 | static char *log = VT_LOG; |
| 129 | static int kernelVersion = 0; |
| 130 | static char termType[32] = "TERM=linux"; |
| 131 | static char console[32] = _PATH_CONSOLE; |
| 132 | |
Erik Andersen | e132f4b | 2000-02-09 04:16:43 +0000 | [diff] [blame] | 133 | static void delete_initAction(initAction * action); |
Eric Andersen | 0460ff2 | 1999-10-25 23:32:44 +0000 | [diff] [blame] | 134 | |
| 135 | |
Erik Andersen | 42094cd | 2000-03-20 21:34:52 +0000 | [diff] [blame] | 136 | /* Print a message to the specified device. |
| 137 | * Device may be bitwise-or'd from LOG | CONSOLE */ |
| 138 | static void message(int device, char *fmt, ...) |
Erik Andersen | 93d6513 | 2000-04-06 08:06:36 +0000 | [diff] [blame] | 139 | __attribute__ ((format (printf, 2, 3))); |
| 140 | static void message(int device, char *fmt, ...) |
Eric Andersen | 0460ff2 | 1999-10-25 23:32:44 +0000 | [diff] [blame] | 141 | { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 142 | va_list arguments; |
| 143 | int fd; |
Erik Andersen | 7dc1607 | 2000-01-04 01:10:25 +0000 | [diff] [blame] | 144 | |
Eric Andersen | 4c78147 | 1999-11-26 08:12:56 +0000 | [diff] [blame] | 145 | #ifdef BB_SYSLOGD |
| 146 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 147 | /* Log the message to syslogd */ |
| 148 | if (device & LOG) { |
| 149 | char msg[1024]; |
Eric Andersen | 4c78147 | 1999-11-26 08:12:56 +0000 | [diff] [blame] | 150 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 151 | va_start(arguments, fmt); |
| 152 | vsnprintf(msg, sizeof(msg), fmt, arguments); |
| 153 | va_end(arguments); |
Erik Andersen | e132f4b | 2000-02-09 04:16:43 +0000 | [diff] [blame] | 154 | openlog("init", 0, LOG_USER); |
| 155 | syslog(LOG_USER|LOG_INFO, msg); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 156 | closelog(); |
Eric Andersen | 3ae0c78 | 1999-11-04 01:13:21 +0000 | [diff] [blame] | 157 | } |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 158 | #else |
| 159 | static int log_fd = -1; |
| 160 | |
| 161 | /* Take full control of the log tty, and never close it. |
| 162 | * It's mine, all mine! Muhahahaha! */ |
| 163 | if (log_fd < 0) { |
| 164 | if (log == NULL) { |
| 165 | /* don't even try to log, because there is no such console */ |
| 166 | log_fd = -2; |
| 167 | /* log to main console instead */ |
| 168 | device = CONSOLE; |
Erik Andersen | e132f4b | 2000-02-09 04:16:43 +0000 | [diff] [blame] | 169 | } else if ((log_fd = device_open(log, O_RDWR|O_NDELAY)) < 0) { |
| 170 | log_fd = -2; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 171 | fprintf(stderr, "Bummer, can't write to log on %s!\r\n", log); |
| 172 | fflush(stderr); |
Erik Andersen | e132f4b | 2000-02-09 04:16:43 +0000 | [diff] [blame] | 173 | log = NULL; |
| 174 | device = CONSOLE; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 175 | } |
| 176 | } |
| 177 | if ((device & LOG) && (log_fd >= 0)) { |
| 178 | va_start(arguments, fmt); |
| 179 | vdprintf(log_fd, fmt, arguments); |
| 180 | va_end(arguments); |
| 181 | } |
Eric Andersen | 4c78147 | 1999-11-26 08:12:56 +0000 | [diff] [blame] | 182 | #endif |
| 183 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 184 | if (device & CONSOLE) { |
| 185 | /* Always send console messages to /dev/console so people will see them. */ |
| 186 | if ( |
| 187 | (fd = |
| 188 | device_open(_PATH_CONSOLE, |
| 189 | O_WRONLY | O_NOCTTY | O_NDELAY)) >= 0) { |
| 190 | va_start(arguments, fmt); |
| 191 | vdprintf(fd, fmt, arguments); |
| 192 | va_end(arguments); |
| 193 | close(fd); |
| 194 | } else { |
| 195 | fprintf(stderr, "Bummer, can't print: "); |
| 196 | va_start(arguments, fmt); |
| 197 | vfprintf(stderr, fmt, arguments); |
| 198 | fflush(stderr); |
| 199 | va_end(arguments); |
| 200 | } |
Eric Andersen | 3ae0c78 | 1999-11-04 01:13:21 +0000 | [diff] [blame] | 201 | } |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 202 | } |
| 203 | |
Erik Andersen | 1d1d950 | 2000-04-21 01:26:49 +0000 | [diff] [blame^] | 204 | #define CTRLCHAR(ch) ((ch)&0x1f) |
Eric Andersen | 3ae0c78 | 1999-11-04 01:13:21 +0000 | [diff] [blame] | 205 | |
Eric Andersen | 0460ff2 | 1999-10-25 23:32:44 +0000 | [diff] [blame] | 206 | /* Set terminal settings to reasonable defaults */ |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 207 | void set_term(int fd) |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 208 | { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 209 | struct termios tty; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 210 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 211 | tcgetattr(fd, &tty); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 212 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 213 | /* set control chars */ |
Erik Andersen | 1d1d950 | 2000-04-21 01:26:49 +0000 | [diff] [blame^] | 214 | tty.c_cc[VINTR] = CTRLCHAR('C'); /* Ctrl-C */ |
| 215 | tty.c_cc[VQUIT] = CTRLCHAR('\\'); /* Ctrl-\ */ |
| 216 | tty.c_cc[VERASE] = CTRLCHAR('?'); /* Ctrl-? */ |
| 217 | tty.c_cc[VKILL] = CTRLCHAR('U'); /* Ctrl-U */ |
| 218 | tty.c_cc[VEOF] = CTRLCHAR('D'); /* Ctrl-D */ |
| 219 | tty.c_cc[VSTOP] = CTRLCHAR('S'); /* Ctrl-S */ |
| 220 | tty.c_cc[VSTART] = CTRLCHAR('Q'); /* Ctrl-Q */ |
| 221 | tty.c_cc[VSUSP] = CTRLCHAR('Z'); /* Ctrl-Z */ |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 222 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 223 | /* use line dicipline 0 */ |
| 224 | tty.c_line = 0; |
Eric Andersen | 7f1acfd | 1999-10-29 23:09:13 +0000 | [diff] [blame] | 225 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 226 | /* Make it be sane */ |
Erik Andersen | 6c41c44 | 2000-03-19 05:13:49 +0000 | [diff] [blame] | 227 | tty.c_cflag &= CBAUD|CBAUDEX|CSIZE|CSTOPB|PARENB|PARODD; |
| 228 | tty.c_cflag |= HUPCL|CLOCAL; |
Eric Andersen | 08b1034 | 1999-11-19 02:38:58 +0000 | [diff] [blame] | 229 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 230 | /* input modes */ |
| 231 | tty.c_iflag = ICRNL | IXON | IXOFF; |
Eric Andersen | 08b1034 | 1999-11-19 02:38:58 +0000 | [diff] [blame] | 232 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 233 | /* output modes */ |
| 234 | tty.c_oflag = OPOST | ONLCR; |
Eric Andersen | 7f1acfd | 1999-10-29 23:09:13 +0000 | [diff] [blame] | 235 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 236 | /* local modes */ |
| 237 | tty.c_lflag = |
| 238 | ISIG | ICANON | ECHO | ECHOE | ECHOK | ECHOCTL | ECHOKE | IEXTEN; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 239 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 240 | tcsetattr(fd, TCSANOW, &tty); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 241 | } |
| 242 | |
Eric Andersen | 219d6f5 | 1999-11-02 19:43:01 +0000 | [diff] [blame] | 243 | /* How much memory does this machine have? */ |
Erik Andersen | d07ee46 | 2000-02-21 21:26:32 +0000 | [diff] [blame] | 244 | static int check_free_memory() |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 245 | { |
Erik Andersen | d07ee46 | 2000-02-21 21:26:32 +0000 | [diff] [blame] | 246 | struct sysinfo info; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 247 | |
Erik Andersen | d07ee46 | 2000-02-21 21:26:32 +0000 | [diff] [blame] | 248 | sysinfo(&info); |
| 249 | if (sysinfo(&info) != 0) { |
| 250 | message(LOG, "Error checking free memory: %s\n", strerror(errno)); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 251 | return -1; |
Eric Andersen | abc7d59 | 1999-10-19 00:27:50 +0000 | [diff] [blame] | 252 | } |
Erik Andersen | d07ee46 | 2000-02-21 21:26:32 +0000 | [diff] [blame] | 253 | |
Erik Andersen | 42094cd | 2000-03-20 21:34:52 +0000 | [diff] [blame] | 254 | return((info.totalram+info.totalswap)/1024); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 255 | } |
| 256 | |
Eric Andersen | 8a8fbb8 | 1999-10-26 00:18:56 +0000 | [diff] [blame] | 257 | static void console_init() |
Eric Andersen | 0460ff2 | 1999-10-25 23:32:44 +0000 | [diff] [blame] | 258 | { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 259 | int fd; |
| 260 | int tried_devcons = 0; |
| 261 | int tried_vtprimary = 0; |
Erik Andersen | 029011b | 2000-03-04 21:19:32 +0000 | [diff] [blame] | 262 | struct vt_stat vt; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 263 | struct serial_struct sr; |
| 264 | char *s; |
Eric Andersen | 0460ff2 | 1999-10-25 23:32:44 +0000 | [diff] [blame] | 265 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 266 | if ((s = getenv("TERM")) != NULL) { |
| 267 | snprintf(termType, sizeof(termType) - 1, "TERM=%s", s); |
| 268 | } |
Erik Andersen | ac6e71f | 2000-01-08 22:04:33 +0000 | [diff] [blame] | 269 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 270 | if ((s = getenv("CONSOLE")) != NULL) { |
| 271 | snprintf(console, sizeof(console) - 1, "%s", s); |
| 272 | } |
Eric Andersen | fbb39c8 | 1999-11-08 17:00:52 +0000 | [diff] [blame] | 273 | #if #cpu(sparc) |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 274 | /* sparc kernel supports console=tty[ab] parameter which is also |
| 275 | * passed to init, so catch it here */ |
| 276 | else if ((s = getenv("console")) != NULL) { |
| 277 | /* remap tty[ab] to /dev/ttyS[01] */ |
| 278 | if (strcmp(s, "ttya") == 0) |
| 279 | snprintf(console, sizeof(console) - 1, "%s", SERIAL_CON0); |
| 280 | else if (strcmp(s, "ttyb") == 0) |
| 281 | snprintf(console, sizeof(console) - 1, "%s", SERIAL_CON1); |
| 282 | } |
Eric Andersen | 219d6f5 | 1999-11-02 19:43:01 +0000 | [diff] [blame] | 283 | #endif |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 284 | else { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 285 | /* 2.2 kernels: identify the real console backend and try to use it */ |
| 286 | if (ioctl(0, TIOCGSERIAL, &sr) == 0) { |
| 287 | /* this is a serial console */ |
| 288 | snprintf(console, sizeof(console) - 1, "/dev/ttyS%d", sr.line); |
| 289 | } else if (ioctl(0, VT_GETSTATE, &vt) == 0) { |
| 290 | /* this is linux virtual tty */ |
| 291 | snprintf(console, sizeof(console) - 1, "/dev/tty%d", |
| 292 | vt.v_active); |
| 293 | } else { |
| 294 | snprintf(console, sizeof(console) - 1, "%s", _PATH_CONSOLE); |
| 295 | tried_devcons++; |
| 296 | } |
Eric Andersen | 07e5297 | 1999-11-07 07:38:08 +0000 | [diff] [blame] | 297 | } |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 298 | |
| 299 | while ((fd = open(console, O_RDONLY | O_NONBLOCK)) < 0) { |
| 300 | /* Can't open selected console -- try /dev/console */ |
| 301 | if (!tried_devcons) { |
| 302 | tried_devcons++; |
| 303 | snprintf(console, sizeof(console) - 1, "%s", _PATH_CONSOLE); |
| 304 | continue; |
| 305 | } |
| 306 | /* Can't open selected console -- try vt1 */ |
| 307 | if (!tried_vtprimary) { |
| 308 | tried_vtprimary++; |
| 309 | snprintf(console, sizeof(console) - 1, "%s", VT_PRIMARY); |
| 310 | continue; |
| 311 | } |
| 312 | break; |
| 313 | } |
| 314 | if (fd < 0) { |
| 315 | /* Perhaps we should panic here? */ |
| 316 | snprintf(console, sizeof(console) - 1, "/dev/null"); |
Eric Andersen | 07e5297 | 1999-11-07 07:38:08 +0000 | [diff] [blame] | 317 | } else { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 318 | /* check for serial console and disable logging to tty3 & running a |
| 319 | * shell to tty2 */ |
| 320 | if (ioctl(0, TIOCGSERIAL, &sr) == 0) { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 321 | log = NULL; |
| 322 | secondConsole = NULL; |
Erik Andersen | fa4718e | 2000-02-21 19:25:12 +0000 | [diff] [blame] | 323 | /* Force the TERM setting to vt102 for serial console -- |
| 324 | * iff TERM is set to linux (the default) */ |
| 325 | if (strcmp( termType, "TERM=linux" ) == 0) |
| 326 | snprintf(termType, sizeof(termType) - 1, "TERM=vt102"); |
Erik Andersen | e132f4b | 2000-02-09 04:16:43 +0000 | [diff] [blame] | 327 | message(LOG | CONSOLE, |
| 328 | "serial console detected. Disabling virtual terminals.\r\n"); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 329 | } |
| 330 | close(fd); |
Eric Andersen | 07e5297 | 1999-11-07 07:38:08 +0000 | [diff] [blame] | 331 | } |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 332 | message(LOG, "console=%s\n", console); |
Eric Andersen | 0460ff2 | 1999-10-25 23:32:44 +0000 | [diff] [blame] | 333 | } |
| 334 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 335 | static pid_t run(char *command, char *terminal, int get_enter) |
Eric Andersen | 0460ff2 | 1999-10-25 23:32:44 +0000 | [diff] [blame] | 336 | { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 337 | int i, fd; |
| 338 | pid_t pid; |
| 339 | char *tmpCmd; |
| 340 | char *cmd[255]; |
Erik Andersen | e132f4b | 2000-02-09 04:16:43 +0000 | [diff] [blame] | 341 | char buf[255]; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 342 | static const char press_enter[] = |
| 343 | |
| 344 | "\nPlease press Enter to activate this console. "; |
| 345 | char *environment[] = { |
| 346 | "HOME=/", |
| 347 | "PATH=/usr/bin:/bin:/usr/sbin:/sbin", |
| 348 | "SHELL=/bin/sh", |
| 349 | termType, |
| 350 | "USER=root", |
| 351 | 0 |
| 352 | }; |
Erik Andersen | ac6e71f | 2000-01-08 22:04:33 +0000 | [diff] [blame] | 353 | |
Eric Andersen | 0460ff2 | 1999-10-25 23:32:44 +0000 | [diff] [blame] | 354 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 355 | if ((pid = fork()) == 0) { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 356 | /* Clean up */ |
| 357 | close(0); |
| 358 | close(1); |
| 359 | close(2); |
| 360 | setsid(); |
Eric Andersen | 0460ff2 | 1999-10-25 23:32:44 +0000 | [diff] [blame] | 361 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 362 | /* Reset signal handlers set for parent process */ |
| 363 | signal(SIGUSR1, SIG_DFL); |
| 364 | signal(SIGUSR2, SIG_DFL); |
| 365 | signal(SIGINT, SIG_DFL); |
| 366 | signal(SIGTERM, SIG_DFL); |
| 367 | signal(SIGHUP, SIG_DFL); |
Eric Andersen | 2f6c04f | 1999-11-01 23:59:44 +0000 | [diff] [blame] | 368 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 369 | if ((fd = device_open(terminal, O_RDWR)) < 0) { |
| 370 | message(LOG | CONSOLE, "Bummer, can't open %s\r\n", terminal); |
| 371 | exit(1); |
| 372 | } |
| 373 | dup2(fd, 0); |
| 374 | dup2(fd, 1); |
| 375 | dup2(fd, 2); |
| 376 | tcsetpgrp(0, getpgrp()); |
| 377 | set_term(0); |
| 378 | |
| 379 | if (get_enter == TRUE) { |
| 380 | /* |
| 381 | * Save memory by not exec-ing anything large (like a shell) |
| 382 | * before the user wants it. This is critical if swap is not |
| 383 | * enabled and the system has low memory. Generally this will |
| 384 | * be run on the second virtual console, and the first will |
| 385 | * be allowed to start a shell or whatever an init script |
| 386 | * specifies. |
| 387 | */ |
| 388 | char c; |
Erik Andersen | e132f4b | 2000-02-09 04:16:43 +0000 | [diff] [blame] | 389 | #ifdef DEBUG_INIT |
Erik Andersen | 2ac2fae | 2000-03-07 23:32:17 +0000 | [diff] [blame] | 390 | pid_t shell_pgid = getpid(); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 391 | message(LOG, "Waiting for enter to start '%s' (pid %d, console %s)\r\n", |
| 392 | command, shell_pgid, terminal); |
Erik Andersen | e132f4b | 2000-02-09 04:16:43 +0000 | [diff] [blame] | 393 | #endif |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 394 | write(fileno(stdout), press_enter, sizeof(press_enter) - 1); |
| 395 | read(fileno(stdin), &c, 1); |
| 396 | } |
| 397 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 398 | #ifdef DEBUG_INIT |
Erik Andersen | e132f4b | 2000-02-09 04:16:43 +0000 | [diff] [blame] | 399 | /* Log the process name and args */ |
| 400 | message(LOG, "Starting pid %d, console %s: '%s'\r\n", |
| 401 | shell_pgid, terminal, command); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 402 | #endif |
Erik Andersen | e132f4b | 2000-02-09 04:16:43 +0000 | [diff] [blame] | 403 | |
| 404 | /* See if any special /bin/sh requiring characters are present */ |
| 405 | if (strpbrk(command, "~`!$^&*()=|\\{}[];\"'<>?") != NULL) { |
| 406 | cmd[0] = SHELL; |
| 407 | cmd[1] = "-c"; |
| 408 | strcpy(buf, "exec "); |
| 409 | strncat(buf, command, sizeof(buf) - strlen(buf) - 1); |
| 410 | cmd[2] = buf; |
| 411 | cmd[3] = NULL; |
| 412 | } else { |
| 413 | /* Convert command (char*) into cmd (char**, one word per string) */ |
| 414 | for (tmpCmd = command, i = 0; |
| 415 | (tmpCmd = strsep(&command, " \t")) != NULL;) { |
| 416 | if (*tmpCmd != '\0') { |
| 417 | cmd[i] = tmpCmd; |
| 418 | tmpCmd++; |
| 419 | i++; |
| 420 | } |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 421 | } |
Erik Andersen | e132f4b | 2000-02-09 04:16:43 +0000 | [diff] [blame] | 422 | cmd[i] = NULL; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 423 | } |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 424 | |
Erik Andersen | 183da4a | 2000-04-04 18:36:37 +0000 | [diff] [blame] | 425 | #if defined BB_FEATURE_INIT_COREDUMPS |
Erik Andersen | 983b51b | 2000-04-04 18:14:25 +0000 | [diff] [blame] | 426 | { |
| 427 | struct stat sb; |
| 428 | if (stat (CORE_ENABLE_FLAG_FILE, &sb) == 0) { |
| 429 | struct rlimit limit; |
| 430 | limit.rlim_cur = RLIM_INFINITY; |
| 431 | limit.rlim_max = RLIM_INFINITY; |
| 432 | setrlimit(RLIMIT_CORE, &limit); |
| 433 | } |
| 434 | } |
Erik Andersen | 183da4a | 2000-04-04 18:36:37 +0000 | [diff] [blame] | 435 | #endif |
Erik Andersen | 983b51b | 2000-04-04 18:14:25 +0000 | [diff] [blame] | 436 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 437 | /* Now run it. The new program will take over this PID, |
| 438 | * so nothing further in init.c should be run. */ |
| 439 | execve(cmd[0], cmd, environment); |
| 440 | |
| 441 | /* We're still here? Some error happened. */ |
| 442 | message(LOG | CONSOLE, "Bummer, could not run '%s': %s\n", cmd[0], |
| 443 | strerror(errno)); |
| 444 | exit(-1); |
Erik Andersen | 05df239 | 2000-01-13 04:43:48 +0000 | [diff] [blame] | 445 | } |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 446 | return pid; |
Eric Andersen | 0460ff2 | 1999-10-25 23:32:44 +0000 | [diff] [blame] | 447 | } |
| 448 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 449 | static int waitfor(char *command, char *terminal, int get_enter) |
Erik Andersen | 0e3782f | 2000-01-07 02:54:55 +0000 | [diff] [blame] | 450 | { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 451 | int status, wpid; |
| 452 | int pid = run(command, terminal, get_enter); |
Erik Andersen | 0e3782f | 2000-01-07 02:54:55 +0000 | [diff] [blame] | 453 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 454 | while (1) { |
| 455 | wpid = wait(&status); |
Erik Andersen | e132f4b | 2000-02-09 04:16:43 +0000 | [diff] [blame] | 456 | if (wpid > 0 && wpid != pid) { |
| 457 | continue; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 458 | } |
| 459 | if (wpid == pid) |
| 460 | break; |
Erik Andersen | 0e3782f | 2000-01-07 02:54:55 +0000 | [diff] [blame] | 461 | } |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 462 | return wpid; |
Erik Andersen | 0e3782f | 2000-01-07 02:54:55 +0000 | [diff] [blame] | 463 | } |
| 464 | |
Eric Andersen | 219d6f5 | 1999-11-02 19:43:01 +0000 | [diff] [blame] | 465 | /* Make sure there is enough memory to do something useful. * |
Erik Andersen | e132f4b | 2000-02-09 04:16:43 +0000 | [diff] [blame] | 466 | * Calls "swapon -a" if needed so be sure /etc/fstab is present... */ |
Eric Andersen | 219d6f5 | 1999-11-02 19:43:01 +0000 | [diff] [blame] | 467 | static void check_memory() |
| 468 | { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 469 | struct stat statBuf; |
Eric Andersen | 219d6f5 | 1999-11-02 19:43:01 +0000 | [diff] [blame] | 470 | |
Erik Andersen | d07ee46 | 2000-02-21 21:26:32 +0000 | [diff] [blame] | 471 | if (check_free_memory() > 1000) |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 472 | return; |
| 473 | |
| 474 | if (stat("/etc/fstab", &statBuf) == 0) { |
Erik Andersen | 61677fe | 2000-04-13 01:18:56 +0000 | [diff] [blame] | 475 | /* swapon -a requires /proc typically */ |
| 476 | waitfor("mount proc /proc -t proc", console, FALSE); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 477 | /* Try to turn on swap */ |
Erik Andersen | 61677fe | 2000-04-13 01:18:56 +0000 | [diff] [blame] | 478 | waitfor("swapon -a", console, FALSE); |
Erik Andersen | d07ee46 | 2000-02-21 21:26:32 +0000 | [diff] [blame] | 479 | if (check_free_memory() < 1000) |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 480 | goto goodnight; |
| 481 | } else |
| 482 | goto goodnight; |
Eric Andersen | 219d6f5 | 1999-11-02 19:43:01 +0000 | [diff] [blame] | 483 | return; |
| 484 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 485 | goodnight: |
| 486 | message(CONSOLE, |
| 487 | "Sorry, your computer does not have enough memory.\r\n"); |
| 488 | while (1) |
| 489 | sleep(1); |
Eric Andersen | 219d6f5 | 1999-11-02 19:43:01 +0000 | [diff] [blame] | 490 | } |
| 491 | |
Erik Andersen | e132f4b | 2000-02-09 04:16:43 +0000 | [diff] [blame] | 492 | /* Run all commands to be run right before halt/reboot */ |
| 493 | static void run_lastAction(void) |
| 494 | { |
| 495 | initAction *a; |
| 496 | for (a = initActionList; a; a = a->nextPtr) { |
| 497 | if (a->action == CTRLALTDEL) { |
| 498 | waitfor(a->process, a->console, FALSE); |
| 499 | delete_initAction(a); |
| 500 | } |
| 501 | } |
| 502 | } |
| 503 | |
| 504 | |
Erik Andersen | 7dc1607 | 2000-01-04 01:10:25 +0000 | [diff] [blame] | 505 | #ifndef DEBUG_INIT |
Eric Andersen | 8a8fbb8 | 1999-10-26 00:18:56 +0000 | [diff] [blame] | 506 | static void shutdown_system(void) |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 507 | { |
Erik Andersen | e132f4b | 2000-02-09 04:16:43 +0000 | [diff] [blame] | 508 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 509 | /* first disable our SIGHUP signal */ |
| 510 | signal(SIGHUP, SIG_DFL); |
Erik Andersen | 7dc1607 | 2000-01-04 01:10:25 +0000 | [diff] [blame] | 511 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 512 | /* Allow Ctrl-Alt-Del to reboot system. */ |
| 513 | reboot(RB_ENABLE_CAD); |
Erik Andersen | e132f4b | 2000-02-09 04:16:43 +0000 | [diff] [blame] | 514 | |
| 515 | message(CONSOLE|LOG, "\r\nThe system is going down NOW !!\r\n"); |
Eric Andersen | cf8c9cf | 1999-11-05 00:31:46 +0000 | [diff] [blame] | 516 | sync(); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 517 | |
| 518 | /* Send signals to every process _except_ pid 1 */ |
Erik Andersen | e132f4b | 2000-02-09 04:16:43 +0000 | [diff] [blame] | 519 | message(CONSOLE|LOG, "Sending SIGTERM to all processes.\r\n"); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 520 | kill(-1, SIGTERM); |
Erik Andersen | e132f4b | 2000-02-09 04:16:43 +0000 | [diff] [blame] | 521 | sleep(1); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 522 | sync(); |
| 523 | |
Erik Andersen | e132f4b | 2000-02-09 04:16:43 +0000 | [diff] [blame] | 524 | message(CONSOLE|LOG, "Sending SIGKILL to all processes.\r\n"); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 525 | kill(-1, SIGKILL); |
Erik Andersen | e132f4b | 2000-02-09 04:16:43 +0000 | [diff] [blame] | 526 | sleep(1); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 527 | |
Erik Andersen | e132f4b | 2000-02-09 04:16:43 +0000 | [diff] [blame] | 528 | /* run everything to be run at "ctrlaltdel" */ |
| 529 | run_lastAction(); |
| 530 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 531 | sync(); |
| 532 | if (kernelVersion > 0 && kernelVersion <= 2 * 65536 + 2 * 256 + 11) { |
| 533 | /* bdflush, kupdate not needed for kernels >2.2.11 */ |
| 534 | bdflush(1, 0); |
| 535 | sync(); |
| 536 | } |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 537 | } |
| 538 | |
Eric Andersen | 8a8fbb8 | 1999-10-26 00:18:56 +0000 | [diff] [blame] | 539 | static void halt_signal(int sig) |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 540 | { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 541 | shutdown_system(); |
Erik Andersen | e132f4b | 2000-02-09 04:16:43 +0000 | [diff] [blame] | 542 | message(CONSOLE|LOG, |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 543 | "The system is halted. Press %s or turn off power\r\n", |
| 544 | (secondConsole == NULL) /* serial console */ |
| 545 | ? "Reset" : "CTRL-ALT-DEL"); |
| 546 | sync(); |
Erik Andersen | 3fe39dc | 2000-01-25 18:13:53 +0000 | [diff] [blame] | 547 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 548 | /* allow time for last message to reach serial console */ |
Erik Andersen | e132f4b | 2000-02-09 04:16:43 +0000 | [diff] [blame] | 549 | sleep(2); |
Erik Andersen | 3fe39dc | 2000-01-25 18:13:53 +0000 | [diff] [blame] | 550 | |
Erik Andersen | 4d1d011 | 1999-12-17 18:44:15 +0000 | [diff] [blame] | 551 | #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,2,0) |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 552 | if (sig == SIGUSR2) |
| 553 | reboot(RB_POWER_OFF); |
| 554 | else |
Erik Andersen | 4d1d011 | 1999-12-17 18:44:15 +0000 | [diff] [blame] | 555 | #endif |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 556 | reboot(RB_HALT_SYSTEM); |
| 557 | exit(0); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 558 | } |
| 559 | |
Eric Andersen | 8a8fbb8 | 1999-10-26 00:18:56 +0000 | [diff] [blame] | 560 | static void reboot_signal(int sig) |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 561 | { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 562 | shutdown_system(); |
Erik Andersen | e132f4b | 2000-02-09 04:16:43 +0000 | [diff] [blame] | 563 | message(CONSOLE|LOG, "Please stand by while rebooting the system.\r\n"); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 564 | sync(); |
Erik Andersen | 3fe39dc | 2000-01-25 18:13:53 +0000 | [diff] [blame] | 565 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 566 | /* allow time for last message to reach serial console */ |
| 567 | sleep(2); |
Erik Andersen | 3fe39dc | 2000-01-25 18:13:53 +0000 | [diff] [blame] | 568 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 569 | reboot(RB_AUTOBOOT); |
| 570 | exit(0); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 571 | } |
Eric Andersen | 8a8fbb8 | 1999-10-26 00:18:56 +0000 | [diff] [blame] | 572 | |
Erik Andersen | de55287 | 2000-01-23 01:34:05 +0000 | [diff] [blame] | 573 | #if defined BB_FEATURE_INIT_CHROOT |
Erik Andersen | d07ee46 | 2000-02-21 21:26:32 +0000 | [diff] [blame] | 574 | |
| 575 | #if ! defined BB_FEATURE_USE_PROCFS |
| 576 | #error Sorry, I depend on the /proc filesystem right now. |
| 577 | #endif |
| 578 | |
Erik Andersen | de55287 | 2000-01-23 01:34:05 +0000 | [diff] [blame] | 579 | static void check_chroot(int sig) |
| 580 | { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 581 | char *argv_init[2] = { "init", NULL, }; |
| 582 | char *envp_init[3] = { "HOME=/", "TERM=linux", NULL, }; |
| 583 | char rootpath[256], *tc; |
| 584 | int fd; |
Erik Andersen | de55287 | 2000-01-23 01:34:05 +0000 | [diff] [blame] | 585 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 586 | if ((fd = open("/proc/sys/kernel/init-chroot", O_RDONLY)) == -1) { |
| 587 | message(CONSOLE, |
| 588 | "SIGHUP recived, but could not open proc file\r\n"); |
| 589 | sleep(2); |
| 590 | return; |
| 591 | } |
| 592 | if (read(fd, rootpath, sizeof(rootpath)) == -1) { |
| 593 | message(CONSOLE, |
| 594 | "SIGHUP recived, but could not read proc file\r\n"); |
| 595 | sleep(2); |
| 596 | return; |
| 597 | } |
| 598 | close(fd); |
| 599 | |
| 600 | if (rootpath[0] == '\0') { |
| 601 | message(CONSOLE, |
| 602 | "SIGHUP recived, but new root is not valid: %s\r\n", |
| 603 | rootpath); |
| 604 | sleep(2); |
| 605 | return; |
| 606 | } |
| 607 | |
| 608 | tc = strrchr(rootpath, '\n'); |
| 609 | *tc = '\0'; |
| 610 | |
| 611 | /* Ok, making it this far means we commit */ |
| 612 | message(CONSOLE, "Please stand by, changing root to `%s'.\r\n", |
| 613 | rootpath); |
| 614 | |
| 615 | /* kill all other programs first */ |
| 616 | message(CONSOLE, "Sending SIGTERM to all processes.\r\n"); |
| 617 | kill(-1, SIGTERM); |
Erik Andersen | de55287 | 2000-01-23 01:34:05 +0000 | [diff] [blame] | 618 | sleep(2); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 619 | sync(); |
| 620 | |
| 621 | message(CONSOLE, "Sending SIGKILL to all processes.\r\n"); |
| 622 | kill(-1, SIGKILL); |
Erik Andersen | de55287 | 2000-01-23 01:34:05 +0000 | [diff] [blame] | 623 | sleep(2); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 624 | sync(); |
| 625 | |
| 626 | /* ok, we don't need /proc anymore. we also assume that the signaling |
| 627 | * process left the rest of the filesystems alone for us */ |
| 628 | umount("/proc"); |
| 629 | |
| 630 | /* Ok, now we chroot. Hopefully we only have two things mounted, the |
| 631 | * new chroot'd mount point, and the old "/" mount. s, |
| 632 | * we go ahead and unmount the old "/". This should trigger the kernel |
| 633 | * to set things up the Right Way(tm). */ |
| 634 | |
| 635 | if (!chroot(rootpath)) |
| 636 | umount("/dev/root"); |
| 637 | |
| 638 | /* If the chroot fails, we are already too far to turn back, so we |
| 639 | * continue and hope that executing init below will revive the system */ |
| 640 | |
| 641 | /* close all of our descriptors and open new ones */ |
| 642 | close(0); |
| 643 | close(1); |
| 644 | close(2); |
| 645 | open("/dev/console", O_RDWR, 0); |
| 646 | dup(0); |
| 647 | dup(0); |
| 648 | |
| 649 | message(CONSOLE, "Executing real init...\r\n"); |
| 650 | /* execute init in the (hopefully) new root */ |
| 651 | execve("/sbin/init", argv_init, envp_init); |
| 652 | |
| 653 | message(CONSOLE, |
| 654 | "ERROR: Could not exec new init. Press %s to reboot.\r\n", |
| 655 | (secondConsole == NULL) /* serial console */ |
| 656 | ? "Reset" : "CTRL-ALT-DEL"); |
Erik Andersen | de55287 | 2000-01-23 01:34:05 +0000 | [diff] [blame] | 657 | return; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 658 | } |
| 659 | #endif /* BB_FEATURE_INIT_CHROOT */ |
Erik Andersen | de55287 | 2000-01-23 01:34:05 +0000 | [diff] [blame] | 660 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 661 | #endif /* ! DEBUG_INIT */ |
Erik Andersen | de55287 | 2000-01-23 01:34:05 +0000 | [diff] [blame] | 662 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 663 | void new_initAction(initActionEnum action, char *process, char *cons) |
Erik Andersen | 7dc1607 | 2000-01-04 01:10:25 +0000 | [diff] [blame] | 664 | { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 665 | initAction *newAction; |
Erik Andersen | 9e73725 | 2000-01-06 01:16:13 +0000 | [diff] [blame] | 666 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 667 | if (*cons == '\0') |
| 668 | cons = console; |
Erik Andersen | 9e73725 | 2000-01-06 01:16:13 +0000 | [diff] [blame] | 669 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 670 | /* If BusyBox detects that a serial console is in use, |
| 671 | * then entries not refering to the console or null devices will _not_ be run. |
| 672 | * The exception to this rule is the null device. |
| 673 | */ |
| 674 | if (secondConsole == NULL && strcmp(cons, console) |
| 675 | && strcmp(cons, "/dev/null")) |
| 676 | return; |
| 677 | |
| 678 | newAction = calloc((size_t) (1), sizeof(initAction)); |
| 679 | if (!newAction) { |
| 680 | message(LOG | CONSOLE, "Memory allocation failure\n"); |
| 681 | while (1) |
| 682 | sleep(1); |
| 683 | } |
| 684 | newAction->nextPtr = initActionList; |
| 685 | initActionList = newAction; |
| 686 | strncpy(newAction->process, process, 255); |
| 687 | newAction->action = action; |
| 688 | strncpy(newAction->console, cons, 255); |
| 689 | newAction->pid = 0; |
Erik Andersen | 3163821 | 2000-01-15 22:28:50 +0000 | [diff] [blame] | 690 | // message(LOG|CONSOLE, "process='%s' action='%d' console='%s'\n", |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 691 | // newAction->process, newAction->action, newAction->console); |
Erik Andersen | 7dc1607 | 2000-01-04 01:10:25 +0000 | [diff] [blame] | 692 | } |
| 693 | |
Erik Andersen | e132f4b | 2000-02-09 04:16:43 +0000 | [diff] [blame] | 694 | static void delete_initAction(initAction * action) |
Erik Andersen | 7dc1607 | 2000-01-04 01:10:25 +0000 | [diff] [blame] | 695 | { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 696 | initAction *a, *b = NULL; |
| 697 | |
| 698 | for (a = initActionList; a; b = a, a = a->nextPtr) { |
| 699 | if (a == action) { |
| 700 | if (b == NULL) { |
| 701 | initActionList = a->nextPtr; |
| 702 | } else { |
| 703 | b->nextPtr = a->nextPtr; |
| 704 | } |
| 705 | free(a); |
| 706 | break; |
| 707 | } |
Erik Andersen | 7dc1607 | 2000-01-04 01:10:25 +0000 | [diff] [blame] | 708 | } |
Erik Andersen | 7dc1607 | 2000-01-04 01:10:25 +0000 | [diff] [blame] | 709 | } |
| 710 | |
Erik Andersen | 9e73725 | 2000-01-06 01:16:13 +0000 | [diff] [blame] | 711 | /* NOTE that if BB_FEATURE_USE_INITTAB is NOT defined, |
| 712 | * then parse_inittab() simply adds in some default |
| 713 | * actions(i.e runs INIT_SCRIPT and then starts a pair |
Erik Andersen | 812d466 | 2000-01-07 18:30:40 +0000 | [diff] [blame] | 714 | * of "askfirst" shells). If BB_FEATURE_USE_INITTAB |
| 715 | * _is_ defined, but /etc/inittab is missing, this |
| 716 | * results in the same set of default behaviors. |
Erik Andersen | 9e73725 | 2000-01-06 01:16:13 +0000 | [diff] [blame] | 717 | * */ |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 718 | void parse_inittab(void) |
Erik Andersen | 7dc1607 | 2000-01-04 01:10:25 +0000 | [diff] [blame] | 719 | { |
Erik Andersen | 9e73725 | 2000-01-06 01:16:13 +0000 | [diff] [blame] | 720 | #ifdef BB_FEATURE_USE_INITTAB |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 721 | FILE *file; |
| 722 | char buf[256], lineAsRead[256], tmpConsole[256]; |
| 723 | char *p, *q, *r, *s; |
| 724 | const struct initActionType *a = actions; |
| 725 | int foundIt; |
Erik Andersen | 7dc1607 | 2000-01-04 01:10:25 +0000 | [diff] [blame] | 726 | |
| 727 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 728 | file = fopen(INITTAB, "r"); |
| 729 | if (file == NULL) { |
| 730 | /* No inittab file -- set up some default behavior */ |
Erik Andersen | 9e73725 | 2000-01-06 01:16:13 +0000 | [diff] [blame] | 731 | #endif |
Erik Andersen | e132f4b | 2000-02-09 04:16:43 +0000 | [diff] [blame] | 732 | /* Swapoff on halt/reboot */ |
Erik Andersen | fb1793f | 2000-02-09 16:37:08 +0000 | [diff] [blame] | 733 | new_initAction(CTRLALTDEL, "/sbin/swapoff -a > /dev/null 2>&1", console); |
Erik Andersen | e132f4b | 2000-02-09 04:16:43 +0000 | [diff] [blame] | 734 | /* Umount all filesystems on halt/reboot */ |
| 735 | new_initAction(CTRLALTDEL, "/bin/umount -a -r > /dev/null 2>&1", console); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 736 | /* Askfirst shell on tty1 */ |
| 737 | new_initAction(ASKFIRST, SHELL, console); |
| 738 | /* Askfirst shell on tty2 */ |
| 739 | if (secondConsole != NULL) |
| 740 | new_initAction(ASKFIRST, SHELL, secondConsole); |
| 741 | /* sysinit */ |
| 742 | new_initAction(SYSINIT, INIT_SCRIPT, console); |
Erik Andersen | 7dc1607 | 2000-01-04 01:10:25 +0000 | [diff] [blame] | 743 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 744 | return; |
Erik Andersen | 9e73725 | 2000-01-06 01:16:13 +0000 | [diff] [blame] | 745 | #ifdef BB_FEATURE_USE_INITTAB |
Erik Andersen | 9e73725 | 2000-01-06 01:16:13 +0000 | [diff] [blame] | 746 | } |
Erik Andersen | 7dc1607 | 2000-01-04 01:10:25 +0000 | [diff] [blame] | 747 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 748 | while (fgets(buf, 255, file) != NULL) { |
| 749 | foundIt = FALSE; |
| 750 | for (p = buf; *p == ' ' || *p == '\t'; p++); |
| 751 | if (*p == '#' || *p == '\n') |
| 752 | continue; |
Erik Andersen | 7dc1607 | 2000-01-04 01:10:25 +0000 | [diff] [blame] | 753 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 754 | /* Trim the trailing \n */ |
| 755 | q = strrchr(p, '\n'); |
| 756 | if (q != NULL) |
| 757 | *q = '\0'; |
Erik Andersen | 7dc1607 | 2000-01-04 01:10:25 +0000 | [diff] [blame] | 758 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 759 | /* Keep a copy around for posterity's sake (and error msgs) */ |
| 760 | strcpy(lineAsRead, buf); |
| 761 | |
| 762 | /* Grab the ID field */ |
| 763 | s = p; |
| 764 | p = strchr(p, ':'); |
| 765 | if (p != NULL || *(p + 1) != '\0') { |
| 766 | *p = '\0'; |
| 767 | ++p; |
Erik Andersen | 0e3782f | 2000-01-07 02:54:55 +0000 | [diff] [blame] | 768 | } |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 769 | |
Erik Andersen | 42094cd | 2000-03-20 21:34:52 +0000 | [diff] [blame] | 770 | /* Now peel off the process field from the end |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 771 | * of the string */ |
| 772 | q = strrchr(p, ':'); |
| 773 | if (q == NULL || *(q + 1) == '\0') { |
| 774 | message(LOG | CONSOLE, "Bad inittab entry: %s\n", lineAsRead); |
| 775 | continue; |
| 776 | } else { |
| 777 | *q = '\0'; |
| 778 | ++q; |
| 779 | } |
| 780 | |
Erik Andersen | 42094cd | 2000-03-20 21:34:52 +0000 | [diff] [blame] | 781 | /* Now peel off the action field */ |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 782 | r = strrchr(p, ':'); |
| 783 | if (r == NULL || *(r + 1) == '\0') { |
| 784 | message(LOG | CONSOLE, "Bad inittab entry: %s\n", lineAsRead); |
| 785 | continue; |
| 786 | } else { |
| 787 | ++r; |
| 788 | } |
| 789 | |
| 790 | /* Ok, now process it */ |
| 791 | a = actions; |
| 792 | while (a->name != 0) { |
| 793 | if (strcmp(a->name, r) == 0) { |
| 794 | if (*s != '\0') { |
| 795 | struct stat statBuf; |
| 796 | |
| 797 | strcpy(tmpConsole, "/dev/"); |
| 798 | strncat(tmpConsole, s, 200); |
| 799 | if (stat(tmpConsole, &statBuf) != 0) { |
| 800 | message(LOG | CONSOLE, |
| 801 | "device '%s' does not exist. Did you read the directions?\n", |
| 802 | tmpConsole); |
| 803 | break; |
| 804 | } |
| 805 | s = tmpConsole; |
| 806 | } |
| 807 | new_initAction(a->action, q, s); |
| 808 | foundIt = TRUE; |
| 809 | } |
| 810 | a++; |
| 811 | } |
| 812 | if (foundIt == TRUE) |
| 813 | continue; |
| 814 | else { |
| 815 | /* Choke on an unknown action */ |
| 816 | message(LOG | CONSOLE, "Bad inittab entry: %s\n", lineAsRead); |
| 817 | } |
Erik Andersen | 7dc1607 | 2000-01-04 01:10:25 +0000 | [diff] [blame] | 818 | } |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 819 | return; |
Erik Andersen | 42094cd | 2000-03-20 21:34:52 +0000 | [diff] [blame] | 820 | #endif /* BB_FEATURE_USE_INITTAB */ |
Erik Andersen | 7dc1607 | 2000-01-04 01:10:25 +0000 | [diff] [blame] | 821 | } |
| 822 | |
Erik Andersen | e132f4b | 2000-02-09 04:16:43 +0000 | [diff] [blame] | 823 | |
| 824 | |
Eric Andersen | 8a8fbb8 | 1999-10-26 00:18:56 +0000 | [diff] [blame] | 825 | extern int init_main(int argc, char **argv) |
Eric Andersen | 0460ff2 | 1999-10-25 23:32:44 +0000 | [diff] [blame] | 826 | { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 827 | initAction *a; |
| 828 | pid_t wpid; |
| 829 | int status; |
Eric Andersen | 0460ff2 | 1999-10-25 23:32:44 +0000 | [diff] [blame] | 830 | |
Eric Andersen | d73dc5b | 1999-11-10 23:13:02 +0000 | [diff] [blame] | 831 | #ifndef DEBUG_INIT |
Erik Andersen | a51ecdd | 2000-02-24 18:09:58 +0000 | [diff] [blame] | 832 | /* Expect to be invoked as init with PID=1 or be invoked as linuxrc */ |
| 833 | if (getpid() != 1 |
| 834 | #ifdef BB_FEATURE_LINUXRC |
| 835 | && strstr(argv[0], "linuxrc") == NULL |
| 836 | #endif |
| 837 | ) |
| 838 | { |
| 839 | usage("init\n\nInit is the parent of all processes.\n\n" |
| 840 | "This version of init is designed to be run only " |
| 841 | "by the kernel.\n"); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 842 | } |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 843 | /* Set up sig handlers -- be sure to |
| 844 | * clear all of these in run() */ |
| 845 | signal(SIGUSR1, halt_signal); |
| 846 | signal(SIGUSR2, reboot_signal); |
| 847 | signal(SIGINT, reboot_signal); |
| 848 | signal(SIGTERM, reboot_signal); |
Erik Andersen | de55287 | 2000-01-23 01:34:05 +0000 | [diff] [blame] | 849 | #if defined BB_FEATURE_INIT_CHROOT |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 850 | signal(SIGHUP, check_chroot); |
Erik Andersen | de55287 | 2000-01-23 01:34:05 +0000 | [diff] [blame] | 851 | #endif |
Eric Andersen | 0460ff2 | 1999-10-25 23:32:44 +0000 | [diff] [blame] | 852 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 853 | /* Turn off rebooting via CTL-ALT-DEL -- we get a |
| 854 | * SIGINT on CAD so we can shut things down gracefully... */ |
| 855 | reboot(RB_DISABLE_CAD); |
| 856 | #endif |
Erik Andersen | 05df239 | 2000-01-13 04:43:48 +0000 | [diff] [blame] | 857 | |
Erik Andersen | 298854f | 2000-03-23 01:09:18 +0000 | [diff] [blame] | 858 | /* Figure out what kernel this is running */ |
| 859 | kernelVersion = get_kernel_revision(); |
| 860 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 861 | /* Figure out where the default console should be */ |
| 862 | console_init(); |
Eric Andersen | 0460ff2 | 1999-10-25 23:32:44 +0000 | [diff] [blame] | 863 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 864 | /* Close whatever files are open, and reset the console. */ |
| 865 | close(0); |
| 866 | close(1); |
| 867 | close(2); |
| 868 | set_term(0); |
Erik Andersen | 983b51b | 2000-04-04 18:14:25 +0000 | [diff] [blame] | 869 | chdir("/"); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 870 | setsid(); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 871 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 872 | /* Make sure PATH is set to something sane */ |
| 873 | putenv(_PATH_STDPATH); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 874 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 875 | /* Hello world */ |
Eric Andersen | 7f1acfd | 1999-10-29 23:09:13 +0000 | [diff] [blame] | 876 | #ifndef DEBUG_INIT |
Erik Andersen | ea6b67d | 2000-03-07 07:47:10 +0000 | [diff] [blame] | 877 | message( |
| 878 | #if ! defined BB_FEATURE_EXTRA_QUIET |
| 879 | CONSOLE| |
| 880 | #endif |
| 881 | LOG, |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 882 | "init started: BusyBox v%s (%s) multi-call binary\r\n", |
| 883 | BB_VER, BB_BT); |
Eric Andersen | 7f1acfd | 1999-10-29 23:09:13 +0000 | [diff] [blame] | 884 | #else |
Erik Andersen | ea6b67d | 2000-03-07 07:47:10 +0000 | [diff] [blame] | 885 | message( |
| 886 | #if ! defined BB_FEATURE_EXTRA_QUIET |
| 887 | CONSOLE| |
| 888 | #endif |
| 889 | LOG, |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 890 | "init(%d) started: BusyBox v%s (%s) multi-call binary\r\n", |
| 891 | getpid(), BB_VER, BB_BT); |
Eric Andersen | 7f1acfd | 1999-10-29 23:09:13 +0000 | [diff] [blame] | 892 | #endif |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 893 | |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 894 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 895 | /* Make sure there is enough memory to do something useful. */ |
| 896 | check_memory(); |
Erik Andersen | 9e73725 | 2000-01-06 01:16:13 +0000 | [diff] [blame] | 897 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 898 | /* Check if we are supposed to be in single user mode */ |
| 899 | if (argc > 1 && (!strcmp(argv[1], "single") || |
| 900 | !strcmp(argv[1], "-s") || !strcmp(argv[1], "1"))) { |
| 901 | /* Ask first then start a shell on tty2 */ |
| 902 | if (secondConsole != NULL) |
| 903 | new_initAction(ASKFIRST, SHELL, secondConsole); |
| 904 | /* Start a shell on tty1 */ |
| 905 | new_initAction(RESPAWN, SHELL, console); |
| 906 | } else { |
| 907 | /* Not in single user mode -- see what inittab says */ |
Eric Andersen | 8a8fbb8 | 1999-10-26 00:18:56 +0000 | [diff] [blame] | 908 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 909 | /* NOTE that if BB_FEATURE_USE_INITTAB is NOT defined, |
| 910 | * then parse_inittab() simply adds in some default |
| 911 | * actions(i.e runs INIT_SCRIPT and then starts a pair |
| 912 | * of "askfirst" shells */ |
| 913 | parse_inittab(); |
Eric Andersen | d00c262 | 1999-12-07 08:37:31 +0000 | [diff] [blame] | 914 | } |
Erik Andersen | 983b51b | 2000-04-04 18:14:25 +0000 | [diff] [blame] | 915 | |
Erik Andersen | e132f4b | 2000-02-09 04:16:43 +0000 | [diff] [blame] | 916 | /* Fix up argv[0] to be certain we claim to be init */ |
| 917 | strncpy(argv[0], "init", strlen(argv[0])+1); |
Erik Andersen | 07f5604 | 2000-02-09 06:05:01 +0000 | [diff] [blame] | 918 | if (argc > 1) |
| 919 | strncpy(argv[1], "\0", strlen(argv[1])+1); |
Eric Andersen | 219d6f5 | 1999-11-02 19:43:01 +0000 | [diff] [blame] | 920 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 921 | /* Now run everything that needs to be run */ |
| 922 | |
| 923 | /* First run the sysinit command */ |
| 924 | for (a = initActionList; a; a = a->nextPtr) { |
| 925 | if (a->action == SYSINIT) { |
| 926 | waitfor(a->process, a->console, FALSE); |
| 927 | /* Now remove the "sysinit" entry from the list */ |
| 928 | delete_initAction(a); |
Erik Andersen | 7dc1607 | 2000-01-04 01:10:25 +0000 | [diff] [blame] | 929 | } |
Erik Andersen | 7dc1607 | 2000-01-04 01:10:25 +0000 | [diff] [blame] | 930 | } |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 931 | /* Next run anything that wants to block */ |
| 932 | for (a = initActionList; a; a = a->nextPtr) { |
| 933 | if (a->action == WAIT) { |
| 934 | waitfor(a->process, a->console, FALSE); |
| 935 | /* Now remove the "wait" entry from the list */ |
| 936 | delete_initAction(a); |
Erik Andersen | 7dc1607 | 2000-01-04 01:10:25 +0000 | [diff] [blame] | 937 | } |
Eric Andersen | 8a8fbb8 | 1999-10-26 00:18:56 +0000 | [diff] [blame] | 938 | } |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 939 | /* Next run anything to be run only once */ |
| 940 | for (a = initActionList; a; a = a->nextPtr) { |
| 941 | if (a->action == ONCE) { |
| 942 | run(a->process, a->console, FALSE); |
| 943 | /* Now remove the "once" entry from the list */ |
| 944 | delete_initAction(a); |
| 945 | } |
| 946 | } |
| 947 | /* If there is nothing else to do, stop */ |
| 948 | if (initActionList == NULL) { |
| 949 | message(LOG | CONSOLE, |
| 950 | "No more tasks for init -- sleeping forever.\n"); |
| 951 | while (1) |
| 952 | sleep(1); |
| 953 | } |
| 954 | |
| 955 | /* Now run the looping stuff for the rest of forever */ |
| 956 | while (1) { |
| 957 | for (a = initActionList; a; a = a->nextPtr) { |
| 958 | /* Only run stuff with pid==0. If they have |
| 959 | * a pid, that means they are still running */ |
| 960 | if (a->pid == 0) { |
| 961 | switch (a->action) { |
| 962 | case RESPAWN: |
| 963 | /* run the respawn stuff */ |
| 964 | a->pid = run(a->process, a->console, FALSE); |
| 965 | break; |
| 966 | case ASKFIRST: |
| 967 | /* run the askfirst stuff */ |
| 968 | a->pid = run(a->process, a->console, TRUE); |
| 969 | break; |
| 970 | /* silence the compiler's incessant whining */ |
| 971 | default: |
| 972 | break; |
| 973 | } |
| 974 | } |
| 975 | } |
| 976 | /* Wait for a child process to exit */ |
| 977 | wpid = wait(&status); |
| 978 | if (wpid > 0) { |
| 979 | /* Find out who died and clean up their corpse */ |
| 980 | for (a = initActionList; a; a = a->nextPtr) { |
| 981 | if (a->pid == wpid) { |
| 982 | a->pid = 0; |
| 983 | message(LOG, |
| 984 | "Process '%s' (pid %d) exited. Scheduling it for restart.\n", |
| 985 | a->process, wpid); |
| 986 | } |
| 987 | } |
| 988 | } |
| 989 | sleep(1); |
| 990 | } |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 991 | } |
Erik Andersen | 029011b | 2000-03-04 21:19:32 +0000 | [diff] [blame] | 992 | |
| 993 | /* |
| 994 | Local Variables: |
| 995 | c-file-style: "linux" |
| 996 | c-basic-offset: 4 |
| 997 | tab-width: 4 |
| 998 | End: |
| 999 | */ |