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