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