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