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