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