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