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