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