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