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