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> |
| 26 | #include <stdlib.h> |
| 27 | #include <stdarg.h> |
| 28 | #include <unistd.h> |
| 29 | #include <errno.h> |
| 30 | #include <signal.h> |
| 31 | #include <termios.h> |
| 32 | #include <sys/types.h> |
| 33 | #include <sys/fcntl.h> |
| 34 | #include <sys/wait.h> |
| 35 | #include <string.h> |
| 36 | #include <sys/mount.h> |
| 37 | #include <sys/reboot.h> |
| 38 | #include <sys/kdaemon.h> |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 39 | #include <sys/sysmacros.h> |
Eric Andersen | 8a8fbb8 | 1999-10-26 00:18:56 +0000 | [diff] [blame] | 40 | #include <linux/serial.h> /* for serial_struct */ |
| 41 | #include <sys/vt.h> /* for vt_stat */ |
Eric Andersen | abc7d59 | 1999-10-19 00:27:50 +0000 | [diff] [blame] | 42 | #include <sys/ioctl.h> |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 43 | |
Eric Andersen | 3ae0c78 | 1999-11-04 01:13:21 +0000 | [diff] [blame] | 44 | #define VT_CONSOLE "/dev/console" /* Logical system console */ |
Eric Andersen | be971d6 | 1999-11-03 16:52:50 +0000 | [diff] [blame] | 45 | #define VT_PRIMARY "/dev/tty1" /* Primary virtual console */ |
| 46 | #define VT_SECONDARY "/dev/tty2" /* Virtual console */ |
| 47 | #define VT_LOG "/dev/tty3" /* Virtual console */ |
Eric Andersen | 219d6f5 | 1999-11-02 19:43:01 +0000 | [diff] [blame] | 48 | #define SERIAL_CON0 "/dev/ttyS0" /* Primary serial console */ |
| 49 | #define SERIAL_CON1 "/dev/ttyS1" /* Serial console */ |
Eric Andersen | 8a8fbb8 | 1999-10-26 00:18:56 +0000 | [diff] [blame] | 50 | #define SHELL "/bin/sh" /* Default shell */ |
Eric Andersen | 219d6f5 | 1999-11-02 19:43:01 +0000 | [diff] [blame] | 51 | #define INITSCRIPT "/etc/init.d/rcS" /* Initscript. */ |
Eric Andersen | 0460ff2 | 1999-10-25 23:32:44 +0000 | [diff] [blame] | 52 | #define PATH_DEFAULT "PATH=/usr/local/sbin:/sbin:/bin:/usr/sbin:/usr/bin" |
| 53 | |
Eric Andersen | 3ae0c78 | 1999-11-04 01:13:21 +0000 | [diff] [blame] | 54 | #define LOG 0x1 |
| 55 | #define CONSOLE 0x2 |
| 56 | static char *console = VT_CONSOLE; |
Eric Andersen | be971d6 | 1999-11-03 16:52:50 +0000 | [diff] [blame] | 57 | static char *second_console = VT_SECONDARY; |
| 58 | static char *log = VT_LOG; |
Eric Andersen | e18c75a | 1999-11-05 04:23:05 +0000 | [diff] [blame] | 59 | static int kernel_version = 0; |
Eric Andersen | 0460ff2 | 1999-10-25 23:32:44 +0000 | [diff] [blame] | 60 | |
| 61 | |
| 62 | /* try to open up the specified device */ |
Eric Andersen | 8a8fbb8 | 1999-10-26 00:18:56 +0000 | [diff] [blame] | 63 | int device_open(char *device, int mode) |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 64 | { |
Eric Andersen | 0460ff2 | 1999-10-25 23:32:44 +0000 | [diff] [blame] | 65 | int m, f, fd = -1; |
Eric Andersen | 8a8fbb8 | 1999-10-26 00:18:56 +0000 | [diff] [blame] | 66 | |
Eric Andersen | 7f1acfd | 1999-10-29 23:09:13 +0000 | [diff] [blame] | 67 | m = mode | O_NONBLOCK; |
Eric Andersen | 8a8fbb8 | 1999-10-26 00:18:56 +0000 | [diff] [blame] | 68 | |
Eric Andersen | 0460ff2 | 1999-10-25 23:32:44 +0000 | [diff] [blame] | 69 | /* Retry up to 5 times */ |
Eric Andersen | 8a8fbb8 | 1999-10-26 00:18:56 +0000 | [diff] [blame] | 70 | for (f = 0; f < 5; f++) |
| 71 | if ((fd = open(device, m)) >= 0) |
| 72 | break; |
| 73 | if (fd < 0) |
| 74 | return fd; |
Eric Andersen | 3ae0c78 | 1999-11-04 01:13:21 +0000 | [diff] [blame] | 75 | /* Reset original flags. */ |
Eric Andersen | 0460ff2 | 1999-10-25 23:32:44 +0000 | [diff] [blame] | 76 | if (m != mode) |
| 77 | fcntl(fd, F_SETFL, mode); |
| 78 | return fd; |
| 79 | } |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 80 | |
Eric Andersen | 3ae0c78 | 1999-11-04 01:13:21 +0000 | [diff] [blame] | 81 | /* print a message to the specified device: |
| 82 | * device may be bitwise-or'd from LOG | CONSOLE */ |
| 83 | void message(int device, char *fmt, ...) |
Eric Andersen | 0460ff2 | 1999-10-25 23:32:44 +0000 | [diff] [blame] | 84 | { |
| 85 | int fd; |
Eric Andersen | 3ae0c78 | 1999-11-04 01:13:21 +0000 | [diff] [blame] | 86 | static int log_fd=-1; |
Eric Andersen | 0460ff2 | 1999-10-25 23:32:44 +0000 | [diff] [blame] | 87 | va_list arguments; |
Eric Andersen | 8a8fbb8 | 1999-10-26 00:18:56 +0000 | [diff] [blame] | 88 | |
Eric Andersen | 3ae0c78 | 1999-11-04 01:13:21 +0000 | [diff] [blame] | 89 | /* Take full control of the log tty, and never close it. |
| 90 | * It's mine, all mine! Muhahahaha! */ |
Eric Andersen | 07e5297 | 1999-11-07 07:38:08 +0000 | [diff] [blame] | 91 | if (log_fd < 0) { |
| 92 | if (log == NULL) { |
| 93 | /* don't even try to log, because there is no such console */ |
| 94 | log_fd = -2; |
| 95 | /* log to main console instead */ |
| 96 | device = CONSOLE; |
| 97 | } |
| 98 | else if ((log_fd = device_open(log, O_RDWR|O_NDELAY)) < 0) { |
Eric Andersen | 3ae0c78 | 1999-11-04 01:13:21 +0000 | [diff] [blame] | 99 | log_fd=-1; |
| 100 | fprintf(stderr, "Bummer, can't write to log on %s!\r\n", log); |
| 101 | fflush(stderr); |
| 102 | return; |
| 103 | } |
| 104 | } |
| 105 | |
Eric Andersen | 07e5297 | 1999-11-07 07:38:08 +0000 | [diff] [blame] | 106 | if ( (device & LOG) && (log_fd >= 0) ) { |
Eric Andersen | 0460ff2 | 1999-10-25 23:32:44 +0000 | [diff] [blame] | 107 | va_start(arguments, fmt); |
Eric Andersen | 3ae0c78 | 1999-11-04 01:13:21 +0000 | [diff] [blame] | 108 | vdprintf(log_fd, fmt, arguments); |
Eric Andersen | a745606 | 1999-10-27 02:31:32 +0000 | [diff] [blame] | 109 | va_end(arguments); |
| 110 | } |
Eric Andersen | 3ae0c78 | 1999-11-04 01:13:21 +0000 | [diff] [blame] | 111 | if (device & CONSOLE) { |
| 112 | if ((fd = device_open(console, O_WRONLY|O_NOCTTY|O_NDELAY)) >= 0) { |
| 113 | va_start(arguments, fmt); |
| 114 | vdprintf(fd, fmt, arguments); |
| 115 | va_end(arguments); |
| 116 | close(fd); |
| 117 | } else { |
| 118 | fprintf(stderr, "Bummer, can't print: "); |
| 119 | va_start(arguments, fmt); |
| 120 | vfprintf(stderr, fmt, arguments); |
| 121 | fflush(stderr); |
| 122 | va_end(arguments); |
| 123 | } |
| 124 | } |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 125 | } |
| 126 | |
Eric Andersen | 3ae0c78 | 1999-11-04 01:13:21 +0000 | [diff] [blame] | 127 | |
Eric Andersen | 0460ff2 | 1999-10-25 23:32:44 +0000 | [diff] [blame] | 128 | /* Set terminal settings to reasonable defaults */ |
Eric Andersen | c7c41d3 | 1999-10-28 00:24:35 +0000 | [diff] [blame] | 129 | void set_term( int fd) |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 130 | { |
Eric Andersen | 0460ff2 | 1999-10-25 23:32:44 +0000 | [diff] [blame] | 131 | struct termios tty; |
Eric Andersen | 3ae0c78 | 1999-11-04 01:13:21 +0000 | [diff] [blame] | 132 | static const char control_characters[] = { |
| 133 | '\003', '\034', '\177', '\030', '\004', '\0', |
| 134 | '\1', '\0', '\021', '\023', '\032', '\0', '\022', |
| 135 | '\017', '\027', '\026', '\0' |
| 136 | }; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 137 | |
Eric Andersen | c7c41d3 | 1999-10-28 00:24:35 +0000 | [diff] [blame] | 138 | tcgetattr(fd, &tty); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 139 | |
Eric Andersen | 3ae0c78 | 1999-11-04 01:13:21 +0000 | [diff] [blame] | 140 | /* Make it be sane */ |
| 141 | tty.c_cflag &= CBAUD|CBAUDEX|CSIZE|CSTOPB|PARENB|PARODD; |
| 142 | tty.c_cflag |= HUPCL|CLOCAL; |
| 143 | |
Eric Andersen | 7f1acfd | 1999-10-29 23:09:13 +0000 | [diff] [blame] | 144 | /* input modes */ |
| 145 | tty.c_iflag = IGNPAR|ICRNL|IXON|IXOFF|IXANY; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 146 | |
Eric Andersen | 219d6f5 | 1999-11-02 19:43:01 +0000 | [diff] [blame] | 147 | /* use line dicipline 0 */ |
Eric Andersen | 7f1acfd | 1999-10-29 23:09:13 +0000 | [diff] [blame] | 148 | tty.c_line = 0; |
| 149 | |
| 150 | /* output modes */ |
Eric Andersen | c7c41d3 | 1999-10-28 00:24:35 +0000 | [diff] [blame] | 151 | tty.c_oflag = OPOST|ONLCR; |
Eric Andersen | 7f1acfd | 1999-10-29 23:09:13 +0000 | [diff] [blame] | 152 | |
| 153 | /* local modes */ |
| 154 | tty.c_lflag = ISIG|ICANON|ECHO|ECHOE|ECHOK|ECHOCTL|ECHOPRT|ECHOKE|IEXTEN; |
| 155 | |
| 156 | /* control chars */ |
| 157 | memcpy(tty.c_cc, control_characters, sizeof(control_characters)); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 158 | |
Eric Andersen | c7c41d3 | 1999-10-28 00:24:35 +0000 | [diff] [blame] | 159 | tcsetattr(fd, TCSANOW, &tty); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 160 | } |
| 161 | |
Eric Andersen | 219d6f5 | 1999-11-02 19:43:01 +0000 | [diff] [blame] | 162 | /* How much memory does this machine have? */ |
Eric Andersen | 8a8fbb8 | 1999-10-26 00:18:56 +0000 | [diff] [blame] | 163 | static int mem_total() |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 164 | { |
Eric Andersen | abc7d59 | 1999-10-19 00:27:50 +0000 | [diff] [blame] | 165 | char s[80]; |
Eric Andersen | 219d6f5 | 1999-11-02 19:43:01 +0000 | [diff] [blame] | 166 | char *p = "/proc/meminfo"; |
Eric Andersen | abc7d59 | 1999-10-19 00:27:50 +0000 | [diff] [blame] | 167 | FILE *f; |
Eric Andersen | 8a8fbb8 | 1999-10-26 00:18:56 +0000 | [diff] [blame] | 168 | const char pattern[] = "MemTotal:"; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 169 | |
Eric Andersen | 219d6f5 | 1999-11-02 19:43:01 +0000 | [diff] [blame] | 170 | if ((f = fopen(p, "r")) < 0) { |
Eric Andersen | 3ae0c78 | 1999-11-04 01:13:21 +0000 | [diff] [blame] | 171 | message(LOG, "Error opening %s: %s\n", p, strerror( errno)); |
Eric Andersen | 219d6f5 | 1999-11-02 19:43:01 +0000 | [diff] [blame] | 172 | return -1; |
| 173 | } |
Eric Andersen | 8a8fbb8 | 1999-10-26 00:18:56 +0000 | [diff] [blame] | 174 | while (NULL != fgets(s, 79, f)) { |
| 175 | p = strstr(s, pattern); |
Eric Andersen | abc7d59 | 1999-10-19 00:27:50 +0000 | [diff] [blame] | 176 | if (NULL != p) { |
| 177 | fclose(f); |
Eric Andersen | 8a8fbb8 | 1999-10-26 00:18:56 +0000 | [diff] [blame] | 178 | return (atoi(p + strlen(pattern))); |
Eric Andersen | abc7d59 | 1999-10-19 00:27:50 +0000 | [diff] [blame] | 179 | } |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 180 | } |
Eric Andersen | abc7d59 | 1999-10-19 00:27:50 +0000 | [diff] [blame] | 181 | return -1; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 182 | } |
| 183 | |
Eric Andersen | 8a8fbb8 | 1999-10-26 00:18:56 +0000 | [diff] [blame] | 184 | static void console_init() |
Eric Andersen | 0460ff2 | 1999-10-25 23:32:44 +0000 | [diff] [blame] | 185 | { |
| 186 | int fd; |
| 187 | int tried_devcons = 0; |
Eric Andersen | 219d6f5 | 1999-11-02 19:43:01 +0000 | [diff] [blame] | 188 | int tried_vtprimary = 0; |
Eric Andersen | 07e5297 | 1999-11-07 07:38:08 +0000 | [diff] [blame] | 189 | struct serial_struct sr; |
Eric Andersen | 0460ff2 | 1999-10-25 23:32:44 +0000 | [diff] [blame] | 190 | char *s; |
| 191 | |
Eric Andersen | 219d6f5 | 1999-11-02 19:43:01 +0000 | [diff] [blame] | 192 | if ((s = getenv("CONSOLE")) != NULL) { |
Eric Andersen | 0460ff2 | 1999-10-25 23:32:44 +0000 | [diff] [blame] | 193 | console = s; |
Eric Andersen | 07e5297 | 1999-11-07 07:38:08 +0000 | [diff] [blame] | 194 | } |
Eric Andersen | fbb39c8 | 1999-11-08 17:00:52 +0000 | [diff] [blame] | 195 | #if #cpu(sparc) |
Eric Andersen | 07e5297 | 1999-11-07 07:38:08 +0000 | [diff] [blame] | 196 | /* sparc kernel supports console=tty[ab] parameter which is also |
| 197 | * passed to init, so catch it here */ |
| 198 | else if ((s = getenv("console")) != NULL) { |
| 199 | /* remap tty[ab] to /dev/ttyS[01] */ |
| 200 | if (strcmp( s, "ttya" )==0) |
| 201 | console = SERIAL_CON0; |
| 202 | else if (strcmp( s, "ttyb" )==0) |
| 203 | console = SERIAL_CON1; |
| 204 | } |
Eric Andersen | 219d6f5 | 1999-11-02 19:43:01 +0000 | [diff] [blame] | 205 | #endif |
Eric Andersen | 07e5297 | 1999-11-07 07:38:08 +0000 | [diff] [blame] | 206 | else { |
| 207 | struct vt_stat vt; |
| 208 | static char the_console[13]; |
| 209 | |
| 210 | console = the_console; |
| 211 | /* 2.2 kernels: identify the real console backend and try to use it */ |
| 212 | if (ioctl(0,TIOCGSERIAL,&sr) == 0) { |
| 213 | /* this is a serial console */ |
| 214 | snprintf( the_console, sizeof the_console, "/dev/ttyS%d", sr.line ); |
| 215 | } |
| 216 | else if (ioctl(0, VT_GETSTATE, &vt) == 0) { |
| 217 | /* this is linux virtual tty */ |
| 218 | snprintf( the_console, sizeof the_console, "/dev/tty%d", vt.v_active ); |
| 219 | } else { |
| 220 | console = VT_CONSOLE; |
| 221 | tried_devcons++; |
| 222 | } |
Eric Andersen | 0460ff2 | 1999-10-25 23:32:44 +0000 | [diff] [blame] | 223 | } |
Eric Andersen | a745606 | 1999-10-27 02:31:32 +0000 | [diff] [blame] | 224 | |
Eric Andersen | 8a8fbb8 | 1999-10-26 00:18:56 +0000 | [diff] [blame] | 225 | while ((fd = open(console, O_RDONLY | O_NONBLOCK)) < 0) { |
Eric Andersen | 219d6f5 | 1999-11-02 19:43:01 +0000 | [diff] [blame] | 226 | /* Can't open selected console -- try /dev/console */ |
Eric Andersen | 0460ff2 | 1999-10-25 23:32:44 +0000 | [diff] [blame] | 227 | if (!tried_devcons) { |
| 228 | tried_devcons++; |
Eric Andersen | 3ae0c78 | 1999-11-04 01:13:21 +0000 | [diff] [blame] | 229 | console = VT_CONSOLE; |
Eric Andersen | 0460ff2 | 1999-10-25 23:32:44 +0000 | [diff] [blame] | 230 | continue; |
| 231 | } |
Eric Andersen | be971d6 | 1999-11-03 16:52:50 +0000 | [diff] [blame] | 232 | /* Can't open selected console -- try vt1 */ |
| 233 | if (!tried_vtprimary) { |
| 234 | tried_vtprimary++; |
| 235 | console = VT_PRIMARY; |
| 236 | continue; |
| 237 | } |
Eric Andersen | 0460ff2 | 1999-10-25 23:32:44 +0000 | [diff] [blame] | 238 | break; |
| 239 | } |
| 240 | if (fd < 0) |
Eric Andersen | 219d6f5 | 1999-11-02 19:43:01 +0000 | [diff] [blame] | 241 | /* Perhaps we should panic here? */ |
Eric Andersen | 0460ff2 | 1999-10-25 23:32:44 +0000 | [diff] [blame] | 242 | console = "/dev/null"; |
Eric Andersen | 07e5297 | 1999-11-07 07:38:08 +0000 | [diff] [blame] | 243 | else { |
| 244 | /* check for serial console and disable logging to tty3 & running a |
| 245 | * shell to tty2 */ |
| 246 | if (ioctl(0,TIOCGSERIAL,&sr) == 0) { |
| 247 | log = NULL; |
| 248 | second_console = NULL; |
| 249 | } |
Eric Andersen | 0460ff2 | 1999-10-25 23:32:44 +0000 | [diff] [blame] | 250 | close(fd); |
Eric Andersen | 07e5297 | 1999-11-07 07:38:08 +0000 | [diff] [blame] | 251 | } |
Eric Andersen | 3ae0c78 | 1999-11-04 01:13:21 +0000 | [diff] [blame] | 252 | message(LOG, "console=%s\n", console ); |
Eric Andersen | 0460ff2 | 1999-10-25 23:32:44 +0000 | [diff] [blame] | 253 | } |
| 254 | |
Eric Andersen | 8a8fbb8 | 1999-10-26 00:18:56 +0000 | [diff] [blame] | 255 | static int waitfor(int pid) |
Eric Andersen | 0460ff2 | 1999-10-25 23:32:44 +0000 | [diff] [blame] | 256 | { |
| 257 | int status, wpid; |
| 258 | |
Eric Andersen | 3ae0c78 | 1999-11-04 01:13:21 +0000 | [diff] [blame] | 259 | message(LOG, "Waiting for process %d.\n", pid); |
Eric Andersen | 8a8fbb8 | 1999-10-26 00:18:56 +0000 | [diff] [blame] | 260 | while ((wpid = wait(&status)) != pid) { |
| 261 | if (wpid > 0) |
Eric Andersen | 3ae0c78 | 1999-11-04 01:13:21 +0000 | [diff] [blame] | 262 | message(LOG, "pid %d exited, status=0x%x.\n", wpid, status); |
Eric Andersen | 0460ff2 | 1999-10-25 23:32:44 +0000 | [diff] [blame] | 263 | } |
| 264 | return wpid; |
| 265 | } |
| 266 | |
Eric Andersen | a745606 | 1999-10-27 02:31:32 +0000 | [diff] [blame] | 267 | |
Eric Andersen | c7c41d3 | 1999-10-28 00:24:35 +0000 | [diff] [blame] | 268 | static pid_t run(const char * const* command, |
| 269 | char *terminal, int get_enter) |
Eric Andersen | 0460ff2 | 1999-10-25 23:32:44 +0000 | [diff] [blame] | 270 | { |
Eric Andersen | 2f6c04f | 1999-11-01 23:59:44 +0000 | [diff] [blame] | 271 | int fd; |
Eric Andersen | c7c41d3 | 1999-10-28 00:24:35 +0000 | [diff] [blame] | 272 | pid_t pid; |
Eric Andersen | 219d6f5 | 1999-11-02 19:43:01 +0000 | [diff] [blame] | 273 | const char * const* cmd = command+1; |
Eric Andersen | 8a8fbb8 | 1999-10-26 00:18:56 +0000 | [diff] [blame] | 274 | static const char press_enter[] = |
Eric Andersen | 0460ff2 | 1999-10-25 23:32:44 +0000 | [diff] [blame] | 275 | "\nPlease press Enter to activate this console. "; |
| 276 | |
Eric Andersen | 0460ff2 | 1999-10-25 23:32:44 +0000 | [diff] [blame] | 277 | if ((pid = fork()) == 0) { |
| 278 | /* Clean up */ |
| 279 | close(0); |
| 280 | close(1); |
| 281 | close(2); |
| 282 | setsid(); |
| 283 | |
Eric Andersen | 2f6c04f | 1999-11-01 23:59:44 +0000 | [diff] [blame] | 284 | /* Reset signal handlers set for parent process */ |
| 285 | signal(SIGUSR1, SIG_DFL); |
| 286 | signal(SIGUSR2, SIG_DFL); |
| 287 | signal(SIGINT, SIG_DFL); |
| 288 | signal(SIGTERM, SIG_DFL); |
| 289 | |
| 290 | if ((fd = device_open(terminal, O_RDWR)) < 0) { |
Eric Andersen | 3ae0c78 | 1999-11-04 01:13:21 +0000 | [diff] [blame] | 291 | message(LOG, "Bummer, can't open %s\r\n", terminal); |
Eric Andersen | 2f6c04f | 1999-11-01 23:59:44 +0000 | [diff] [blame] | 292 | exit(-1); |
| 293 | } |
| 294 | dup(fd); |
| 295 | dup(fd); |
Eric Andersen | c7c41d3 | 1999-10-28 00:24:35 +0000 | [diff] [blame] | 296 | tcsetpgrp(0, getpgrp()); |
| 297 | set_term(0); |
Eric Andersen | 0460ff2 | 1999-10-25 23:32:44 +0000 | [diff] [blame] | 298 | |
Eric Andersen | 2f6c04f | 1999-11-01 23:59:44 +0000 | [diff] [blame] | 299 | if (get_enter==TRUE) { |
Eric Andersen | 0460ff2 | 1999-10-25 23:32:44 +0000 | [diff] [blame] | 300 | /* |
| 301 | * Save memory by not exec-ing anything large (like a shell) |
| 302 | * before the user wants it. This is critical if swap is not |
| 303 | * enabled and the system has low memory. Generally this will |
| 304 | * be run on the second virtual console, and the first will |
| 305 | * be allowed to start a shell or whatever an init script |
| 306 | * specifies. |
| 307 | */ |
Eric Andersen | 8a8fbb8 | 1999-10-26 00:18:56 +0000 | [diff] [blame] | 308 | char c; |
Eric Andersen | 3ae0c78 | 1999-11-04 01:13:21 +0000 | [diff] [blame] | 309 | message(LOG, "Waiting for enter to start '%s' (pid %d, console %s)\r\n", |
| 310 | *cmd, getpid(), terminal ); |
Eric Andersen | 7f1acfd | 1999-10-29 23:09:13 +0000 | [diff] [blame] | 311 | write(1, press_enter, sizeof(press_enter) - 1); |
Eric Andersen | 0460ff2 | 1999-10-25 23:32:44 +0000 | [diff] [blame] | 312 | read(0, &c, 1); |
| 313 | } |
| 314 | |
| 315 | /* Log the process name and args */ |
Eric Andersen | 3ae0c78 | 1999-11-04 01:13:21 +0000 | [diff] [blame] | 316 | message(LOG, "Starting pid %d, console %s: '", getpid(), terminal); |
| 317 | while ( *cmd) message(LOG, "%s ", *cmd++); |
| 318 | message(LOG, "'\r\n"); |
Eric Andersen | 2f6c04f | 1999-11-01 23:59:44 +0000 | [diff] [blame] | 319 | |
Eric Andersen | c7c41d3 | 1999-10-28 00:24:35 +0000 | [diff] [blame] | 320 | /* Now run it. The new program will take over this PID, |
Eric Andersen | a745606 | 1999-10-27 02:31:32 +0000 | [diff] [blame] | 321 | * so nothing further in init.c should be run. */ |
Eric Andersen | c7c41d3 | 1999-10-28 00:24:35 +0000 | [diff] [blame] | 322 | execvp(*command, (char**)command+1); |
Eric Andersen | 0460ff2 | 1999-10-25 23:32:44 +0000 | [diff] [blame] | 323 | |
Eric Andersen | 3ae0c78 | 1999-11-04 01:13:21 +0000 | [diff] [blame] | 324 | message(LOG, "Bummer, could not run '%s'\n", command); |
Eric Andersen | 0460ff2 | 1999-10-25 23:32:44 +0000 | [diff] [blame] | 325 | exit(-1); |
| 326 | } |
| 327 | return pid; |
| 328 | } |
| 329 | |
Eric Andersen | 219d6f5 | 1999-11-02 19:43:01 +0000 | [diff] [blame] | 330 | /* Make sure there is enough memory to do something useful. * |
| 331 | * Calls swapon if needed so be sure /proc is mounted. */ |
| 332 | static void check_memory() |
| 333 | { |
| 334 | struct stat statbuf; |
| 335 | const char* const swap_on_cmd[] = |
| 336 | { "/bin/swapon", "swapon", "-a", 0}; |
Eric Andersen | 219d6f5 | 1999-11-02 19:43:01 +0000 | [diff] [blame] | 337 | |
| 338 | if (mem_total() > 3500) |
| 339 | return; |
| 340 | |
| 341 | if (stat("/etc/fstab", &statbuf) == 0) { |
| 342 | /* Try to turn on swap */ |
| 343 | waitfor(run(swap_on_cmd, log, FALSE)); |
| 344 | if (mem_total() < 3500) |
| 345 | goto goodnight; |
| 346 | } else |
| 347 | goto goodnight; |
| 348 | return; |
| 349 | |
| 350 | goodnight: |
Eric Andersen | 3ae0c78 | 1999-11-04 01:13:21 +0000 | [diff] [blame] | 351 | message(CONSOLE, "Sorry, your computer does not have enough memory.\r\n"); |
Eric Andersen | 219d6f5 | 1999-11-02 19:43:01 +0000 | [diff] [blame] | 352 | while (1) sleep(1); |
| 353 | } |
| 354 | |
Eric Andersen | 8a8fbb8 | 1999-10-26 00:18:56 +0000 | [diff] [blame] | 355 | static void shutdown_system(void) |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 356 | { |
Eric Andersen | 2f6c04f | 1999-11-01 23:59:44 +0000 | [diff] [blame] | 357 | const char* const swap_off_cmd[] = { "swapoff", "swapoff", "-a", 0}; |
| 358 | const char* const umount_cmd[] = { "umount", "umount", "-a", "-n", 0}; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 359 | |
Eric Andersen | 2f6c04f | 1999-11-01 23:59:44 +0000 | [diff] [blame] | 360 | #ifndef DEBUG_INIT |
Eric Andersen | 0460ff2 | 1999-10-25 23:32:44 +0000 | [diff] [blame] | 361 | /* Allow Ctrl-Alt-Del to reboot system. */ |
| 362 | reboot(RB_ENABLE_CAD); |
Eric Andersen | 2f6c04f | 1999-11-01 23:59:44 +0000 | [diff] [blame] | 363 | #endif |
Eric Andersen | 3ae0c78 | 1999-11-04 01:13:21 +0000 | [diff] [blame] | 364 | message(CONSOLE, "The system is going down NOW !!\r\n"); |
| 365 | sync(); |
Eric Andersen | 0460ff2 | 1999-10-25 23:32:44 +0000 | [diff] [blame] | 366 | /* Send signals to every process _except_ pid 1 */ |
Eric Andersen | 3ae0c78 | 1999-11-04 01:13:21 +0000 | [diff] [blame] | 367 | message(CONSOLE, "Sending SIGHUP to all processes.\r\n"); |
Eric Andersen | 2f6c04f | 1999-11-01 23:59:44 +0000 | [diff] [blame] | 368 | #ifndef DEBUG_INIT |
Eric Andersen | 0460ff2 | 1999-10-25 23:32:44 +0000 | [diff] [blame] | 369 | kill(-1, SIGHUP); |
Eric Andersen | 2f6c04f | 1999-11-01 23:59:44 +0000 | [diff] [blame] | 370 | #endif |
Eric Andersen | 0460ff2 | 1999-10-25 23:32:44 +0000 | [diff] [blame] | 371 | sleep(2); |
| 372 | sync(); |
Eric Andersen | 3ae0c78 | 1999-11-04 01:13:21 +0000 | [diff] [blame] | 373 | message(CONSOLE, "Sending SIGKILL to all processes.\r\n"); |
Eric Andersen | 2f6c04f | 1999-11-01 23:59:44 +0000 | [diff] [blame] | 374 | #ifndef DEBUG_INIT |
Eric Andersen | 0460ff2 | 1999-10-25 23:32:44 +0000 | [diff] [blame] | 375 | kill(-1, SIGKILL); |
Eric Andersen | 2f6c04f | 1999-11-01 23:59:44 +0000 | [diff] [blame] | 376 | #endif |
Eric Andersen | 0460ff2 | 1999-10-25 23:32:44 +0000 | [diff] [blame] | 377 | sleep(1); |
Eric Andersen | 3ae0c78 | 1999-11-04 01:13:21 +0000 | [diff] [blame] | 378 | waitfor(run( swap_off_cmd, console, FALSE)); |
| 379 | waitfor(run( umount_cmd, console, FALSE)); |
Eric Andersen | 0460ff2 | 1999-10-25 23:32:44 +0000 | [diff] [blame] | 380 | sync(); |
Eric Andersen | e18c75a | 1999-11-05 04:23:05 +0000 | [diff] [blame] | 381 | if (kernel_version > 0 && kernel_version <= 2 * 65536 + 2 * 256 + 11) { |
Eric Andersen | cf8c9cf | 1999-11-05 00:31:46 +0000 | [diff] [blame] | 382 | /* bdflush, kupdate not needed for kernels >2.2.11 */ |
Eric Andersen | e18c75a | 1999-11-05 04:23:05 +0000 | [diff] [blame] | 383 | message(CONSOLE, "Flushing buffers.\r\n"); |
Eric Andersen | cf8c9cf | 1999-11-05 00:31:46 +0000 | [diff] [blame] | 384 | bdflush(1, 0); |
| 385 | sync(); |
| 386 | } |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 387 | } |
| 388 | |
Eric Andersen | 8a8fbb8 | 1999-10-26 00:18:56 +0000 | [diff] [blame] | 389 | static void halt_signal(int sig) |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 390 | { |
Eric Andersen | abc7d59 | 1999-10-19 00:27:50 +0000 | [diff] [blame] | 391 | shutdown_system(); |
Eric Andersen | 3ae0c78 | 1999-11-04 01:13:21 +0000 | [diff] [blame] | 392 | message(CONSOLE, |
Eric Andersen | 8a8fbb8 | 1999-10-26 00:18:56 +0000 | [diff] [blame] | 393 | "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] | 394 | sync(); |
Eric Andersen | 2f6c04f | 1999-11-01 23:59:44 +0000 | [diff] [blame] | 395 | #ifndef DEBUG_INIT |
Eric Andersen | 3ae0c78 | 1999-11-04 01:13:21 +0000 | [diff] [blame] | 396 | reboot(RB_HALT_SYSTEM); |
| 397 | //reboot(RB_POWER_OFF); |
Eric Andersen | 2f6c04f | 1999-11-01 23:59:44 +0000 | [diff] [blame] | 398 | #endif |
Eric Andersen | abc7d59 | 1999-10-19 00:27:50 +0000 | [diff] [blame] | 399 | exit(0); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 400 | } |
| 401 | |
Eric Andersen | 8a8fbb8 | 1999-10-26 00:18:56 +0000 | [diff] [blame] | 402 | static void reboot_signal(int sig) |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 403 | { |
Eric Andersen | abc7d59 | 1999-10-19 00:27:50 +0000 | [diff] [blame] | 404 | shutdown_system(); |
Eric Andersen | 3ae0c78 | 1999-11-04 01:13:21 +0000 | [diff] [blame] | 405 | message(CONSOLE, "Please stand by while rebooting the system.\r\n"); |
| 406 | sync(); |
Eric Andersen | 2f6c04f | 1999-11-01 23:59:44 +0000 | [diff] [blame] | 407 | #ifndef DEBUG_INIT |
Eric Andersen | 8a8fbb8 | 1999-10-26 00:18:56 +0000 | [diff] [blame] | 408 | reboot(RB_AUTOBOOT); |
Eric Andersen | 2f6c04f | 1999-11-01 23:59:44 +0000 | [diff] [blame] | 409 | #endif |
Eric Andersen | abc7d59 | 1999-10-19 00:27:50 +0000 | [diff] [blame] | 410 | exit(0); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 411 | } |
Eric Andersen | 8a8fbb8 | 1999-10-26 00:18:56 +0000 | [diff] [blame] | 412 | |
Eric Andersen | 8a8fbb8 | 1999-10-26 00:18:56 +0000 | [diff] [blame] | 413 | extern int init_main(int argc, char **argv) |
Eric Andersen | 0460ff2 | 1999-10-25 23:32:44 +0000 | [diff] [blame] | 414 | { |
Eric Andersen | 8a8fbb8 | 1999-10-26 00:18:56 +0000 | [diff] [blame] | 415 | int run_rc = TRUE; |
Eric Andersen | 219d6f5 | 1999-11-02 19:43:01 +0000 | [diff] [blame] | 416 | int wait_for_enter = TRUE; |
Eric Andersen | c7c41d3 | 1999-10-28 00:24:35 +0000 | [diff] [blame] | 417 | pid_t pid1 = 0; |
| 418 | pid_t pid2 = 0; |
Eric Andersen | 8a8fbb8 | 1999-10-26 00:18:56 +0000 | [diff] [blame] | 419 | struct stat statbuf; |
Eric Andersen | b6a44b8 | 1999-11-13 04:47:09 +0000 | [diff] [blame] | 420 | const char* const rc_script_command[] = { INITSCRIPT, INITSCRIPT, 0}; |
| 421 | const char* const shell_command[] = { SHELL, "-" SHELL, 0}; |
| 422 | const char* const* tty0_command = shell_command; |
| 423 | const char* const* tty1_command = shell_command; |
| 424 | #ifdef BB_CONSOLE_CMD_IF_RC_SCRIPT_EXITS |
| 425 | const char* const rc_exit_command[] = BB_CONSOLE_CMD_IF_RC_SCRIPT_EXITS; |
| 426 | #endif |
| 427 | |
Eric Andersen | 2f6c04f | 1999-11-01 23:59:44 +0000 | [diff] [blame] | 428 | #ifdef DEBUG_INIT |
Eric Andersen | 8a8fbb8 | 1999-10-26 00:18:56 +0000 | [diff] [blame] | 429 | char *hello_msg_format = |
Eric Andersen | 0457978 | 1999-10-29 23:12:50 +0000 | [diff] [blame] | 430 | "init(%d) started: BusyBox v%s (%s) multi-call binary\r\n"; |
Eric Andersen | 2f6c04f | 1999-11-01 23:59:44 +0000 | [diff] [blame] | 431 | #else |
| 432 | char *hello_msg_format = |
| 433 | "init started: BusyBox v%s (%s) multi-call binary\r\n"; |
Eric Andersen | 7f1acfd | 1999-10-29 23:09:13 +0000 | [diff] [blame] | 434 | #endif |
Eric Andersen | 0460ff2 | 1999-10-25 23:32:44 +0000 | [diff] [blame] | 435 | |
Eric Andersen | 219d6f5 | 1999-11-02 19:43:01 +0000 | [diff] [blame] | 436 | |
Eric Andersen | d73dc5b | 1999-11-10 23:13:02 +0000 | [diff] [blame] | 437 | #ifndef DEBUG_INIT |
| 438 | if (getpid() != 1) { |
| 439 | usage( "init\n\nInit is the parent of all processes.\n\n" |
| 440 | "This version of init is designed to be run only by the kernel\n"); |
| 441 | } |
| 442 | #endif |
| 443 | |
Eric Andersen | 219d6f5 | 1999-11-02 19:43:01 +0000 | [diff] [blame] | 444 | /* Check if we are supposed to be in single user mode */ |
| 445 | if ( argc > 1 && (!strcmp(argv[1], "single") || |
| 446 | !strcmp(argv[1], "-s") || !strcmp(argv[1], "1"))) { |
| 447 | run_rc = FALSE; |
| 448 | } |
| 449 | |
| 450 | |
Eric Andersen | 2f6c04f | 1999-11-01 23:59:44 +0000 | [diff] [blame] | 451 | /* Set up sig handlers -- be sure to |
| 452 | * clear all of these in run() */ |
Eric Andersen | 8a8fbb8 | 1999-10-26 00:18:56 +0000 | [diff] [blame] | 453 | signal(SIGUSR1, halt_signal); |
Eric Andersen | 8a8fbb8 | 1999-10-26 00:18:56 +0000 | [diff] [blame] | 454 | signal(SIGUSR2, reboot_signal); |
| 455 | signal(SIGINT, reboot_signal); |
| 456 | signal(SIGTERM, reboot_signal); |
Eric Andersen | 0460ff2 | 1999-10-25 23:32:44 +0000 | [diff] [blame] | 457 | |
Eric Andersen | 8a8fbb8 | 1999-10-26 00:18:56 +0000 | [diff] [blame] | 458 | /* Turn off rebooting via CTL-ALT-DEL -- we get a |
| 459 | * SIGINT on CAD so we can shut things down gracefully... */ |
Eric Andersen | 2f6c04f | 1999-11-01 23:59:44 +0000 | [diff] [blame] | 460 | #ifndef DEBUG_INIT |
Eric Andersen | 8a8fbb8 | 1999-10-26 00:18:56 +0000 | [diff] [blame] | 461 | reboot(RB_DISABLE_CAD); |
Eric Andersen | 7f1acfd | 1999-10-29 23:09:13 +0000 | [diff] [blame] | 462 | #endif |
Eric Andersen | 219d6f5 | 1999-11-02 19:43:01 +0000 | [diff] [blame] | 463 | |
Eric Andersen | c7c41d3 | 1999-10-28 00:24:35 +0000 | [diff] [blame] | 464 | /* Figure out where the default console should be */ |
| 465 | console_init(); |
Eric Andersen | 0460ff2 | 1999-10-25 23:32:44 +0000 | [diff] [blame] | 466 | |
Eric Andersen | 8a8fbb8 | 1999-10-26 00:18:56 +0000 | [diff] [blame] | 467 | /* Close whatever files are open, and reset the console. */ |
| 468 | close(0); |
| 469 | close(1); |
| 470 | close(2); |
Eric Andersen | 7f1acfd | 1999-10-29 23:09:13 +0000 | [diff] [blame] | 471 | set_term(0); |
Eric Andersen | 8a8fbb8 | 1999-10-26 00:18:56 +0000 | [diff] [blame] | 472 | setsid(); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 473 | |
Eric Andersen | 8a8fbb8 | 1999-10-26 00:18:56 +0000 | [diff] [blame] | 474 | /* Make sure PATH is set to something sane */ |
Eric Andersen | 219d6f5 | 1999-11-02 19:43:01 +0000 | [diff] [blame] | 475 | putenv(PATH_DEFAULT); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 476 | |
Eric Andersen | 219d6f5 | 1999-11-02 19:43:01 +0000 | [diff] [blame] | 477 | |
Eric Andersen | 8a8fbb8 | 1999-10-26 00:18:56 +0000 | [diff] [blame] | 478 | /* Hello world */ |
Eric Andersen | 7f1acfd | 1999-10-29 23:09:13 +0000 | [diff] [blame] | 479 | #ifndef DEBUG_INIT |
Eric Andersen | 3ae0c78 | 1999-11-04 01:13:21 +0000 | [diff] [blame] | 480 | message(CONSOLE|LOG, hello_msg_format, BB_VER, BB_BT); |
Eric Andersen | 7f1acfd | 1999-10-29 23:09:13 +0000 | [diff] [blame] | 481 | #else |
Eric Andersen | 3ae0c78 | 1999-11-04 01:13:21 +0000 | [diff] [blame] | 482 | message(CONSOLE|LOG, hello_msg_format, getpid(), BB_VER, BB_BT); |
Eric Andersen | 7f1acfd | 1999-10-29 23:09:13 +0000 | [diff] [blame] | 483 | #endif |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 484 | |
Eric Andersen | c7c41d3 | 1999-10-28 00:24:35 +0000 | [diff] [blame] | 485 | |
Eric Andersen | 8a8fbb8 | 1999-10-26 00:18:56 +0000 | [diff] [blame] | 486 | /* Mount /proc */ |
Eric Andersen | e18c75a | 1999-11-05 04:23:05 +0000 | [diff] [blame] | 487 | if (mount ("proc", "/proc", "proc", 0, 0) == 0) { |
Eric Andersen | 3ae0c78 | 1999-11-04 01:13:21 +0000 | [diff] [blame] | 488 | message(CONSOLE|LOG, "Mounting /proc: done.\n"); |
Eric Andersen | e18c75a | 1999-11-05 04:23:05 +0000 | [diff] [blame] | 489 | kernel_version = get_kernel_revision(); |
| 490 | } else |
Eric Andersen | 3ae0c78 | 1999-11-04 01:13:21 +0000 | [diff] [blame] | 491 | message(CONSOLE|LOG, "Mounting /proc: failed!\n"); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 492 | |
Eric Andersen | 219d6f5 | 1999-11-02 19:43:01 +0000 | [diff] [blame] | 493 | /* Make sure there is enough memory to do something useful. */ |
| 494 | check_memory(); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 495 | |
Eric Andersen | 8a8fbb8 | 1999-10-26 00:18:56 +0000 | [diff] [blame] | 496 | |
| 497 | /* Make sure an init script exists before trying to run it */ |
Eric Andersen | 219d6f5 | 1999-11-02 19:43:01 +0000 | [diff] [blame] | 498 | if (run_rc == TRUE && stat(INITSCRIPT, &statbuf)==0) { |
| 499 | wait_for_enter = FALSE; |
Eric Andersen | b6a44b8 | 1999-11-13 04:47:09 +0000 | [diff] [blame] | 500 | tty0_command = rc_script_command; |
Eric Andersen | 8a8fbb8 | 1999-10-26 00:18:56 +0000 | [diff] [blame] | 501 | } |
| 502 | |
Eric Andersen | 219d6f5 | 1999-11-02 19:43:01 +0000 | [diff] [blame] | 503 | |
Eric Andersen | 8a8fbb8 | 1999-10-26 00:18:56 +0000 | [diff] [blame] | 504 | /* Ok, now launch the rc script and/or prepare to |
| 505 | * start up some VTs if somebody hits enter... |
| 506 | */ |
| 507 | for (;;) { |
Eric Andersen | c7c41d3 | 1999-10-28 00:24:35 +0000 | [diff] [blame] | 508 | pid_t wpid; |
Eric Andersen | 8a8fbb8 | 1999-10-26 00:18:56 +0000 | [diff] [blame] | 509 | int status; |
| 510 | |
Eric Andersen | b6a44b8 | 1999-11-13 04:47:09 +0000 | [diff] [blame] | 511 | if (pid1 == 0 && tty0_command) { |
| 512 | pid1 = run(tty0_command, console, wait_for_enter); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 513 | } |
Eric Andersen | b6a44b8 | 1999-11-13 04:47:09 +0000 | [diff] [blame] | 514 | if (pid2 == 0 && tty1_command && second_console) { |
| 515 | pid2 = run(tty1_command, second_console, TRUE); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 516 | } |
Eric Andersen | 8a8fbb8 | 1999-10-26 00:18:56 +0000 | [diff] [blame] | 517 | wpid = wait(&status); |
Eric Andersen | a745606 | 1999-10-27 02:31:32 +0000 | [diff] [blame] | 518 | if (wpid > 0 ) { |
Eric Andersen | 3ae0c78 | 1999-11-04 01:13:21 +0000 | [diff] [blame] | 519 | message(LOG, "pid %d exited, status=%x.\n", wpid, status); |
Eric Andersen | 8a8fbb8 | 1999-10-26 00:18:56 +0000 | [diff] [blame] | 520 | } |
Eric Andersen | cf8c9cf | 1999-11-05 00:31:46 +0000 | [diff] [blame] | 521 | /* Don't respawn init script if it exits */ |
Eric Andersen | 219d6f5 | 1999-11-02 19:43:01 +0000 | [diff] [blame] | 522 | if (wpid == pid1) { |
Eric Andersen | cf8c9cf | 1999-11-05 00:31:46 +0000 | [diff] [blame] | 523 | if (run_rc == FALSE) { |
| 524 | pid1 = 0; |
Eric Andersen | b6a44b8 | 1999-11-13 04:47:09 +0000 | [diff] [blame] | 525 | } |
| 526 | #ifdef BB_CONSOLE_CMD_IF_RC_SCRIPT_EXITS |
Eric Andersen | cf8c9cf | 1999-11-05 00:31:46 +0000 | [diff] [blame] | 527 | else { |
Eric Andersen | b6a44b8 | 1999-11-13 04:47:09 +0000 | [diff] [blame] | 528 | pid1 = 0; |
Eric Andersen | 219d6f5 | 1999-11-02 19:43:01 +0000 | [diff] [blame] | 529 | run_rc=FALSE; |
| 530 | wait_for_enter=TRUE; |
Eric Andersen | b6a44b8 | 1999-11-13 04:47:09 +0000 | [diff] [blame] | 531 | tty0_command=rc_exit_command; |
Eric Andersen | 219d6f5 | 1999-11-02 19:43:01 +0000 | [diff] [blame] | 532 | } |
Eric Andersen | cf8c9cf | 1999-11-05 00:31:46 +0000 | [diff] [blame] | 533 | #endif |
Eric Andersen | 8a8fbb8 | 1999-10-26 00:18:56 +0000 | [diff] [blame] | 534 | } |
| 535 | if (wpid == pid2) { |
| 536 | pid2 = 0; |
| 537 | } |
| 538 | sleep(1); |
| 539 | } |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 540 | } |
Eric Andersen | a9c95ea | 1999-11-15 17:33:30 +0000 | [diff] [blame^] | 541 | |
| 542 | #if defined FOO |
| 543 | #error Ack! |
| 544 | #endif |