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