Eric Andersen | c499601 | 1999-10-20 22:08:37 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Mini init implementation for busybox |
| 3 | * |
| 4 | * |
| 5 | * Copyright (C) 1995, 1996 by Bruce Perens <bruce@pixar.com>. |
| 6 | * Adjusted by so many folks, it's impossible to keep track. |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or modify |
| 9 | * it under the terms of the GNU General Public License as published by |
| 10 | * the Free Software Foundation; either version 2 of the License, or |
| 11 | * (at your option) any later version. |
| 12 | * |
| 13 | * This program is distributed in the hope that it will be useful, |
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 16 | * General Public License for more details. |
| 17 | * |
| 18 | * You should have received a copy of the GNU General Public License |
| 19 | * along with this program; if not, write to the Free Software |
| 20 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 21 | * |
| 22 | */ |
| 23 | |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 24 | #include "internal.h" |
| 25 | #include <stdio.h> |
| 26 | #include <stdlib.h> |
| 27 | #include <stdarg.h> |
| 28 | #include <unistd.h> |
| 29 | #include <errno.h> |
| 30 | #include <signal.h> |
| 31 | #include <termios.h> |
| 32 | #include <sys/types.h> |
| 33 | #include <sys/fcntl.h> |
| 34 | #include <sys/wait.h> |
| 35 | #include <string.h> |
| 36 | #include <sys/mount.h> |
| 37 | #include <sys/reboot.h> |
| 38 | #include <sys/kdaemon.h> |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 39 | #include <sys/sysmacros.h> |
Eric Andersen | 8a8fbb8 | 1999-10-26 00:18:56 +0000 | [diff] [blame] | 40 | #include <linux/serial.h> /* for serial_struct */ |
| 41 | #include <sys/vt.h> /* for vt_stat */ |
Eric Andersen | abc7d59 | 1999-10-19 00:27:50 +0000 | [diff] [blame] | 42 | #include <sys/ioctl.h> |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 43 | |
Eric Andersen | 7f1acfd | 1999-10-29 23:09:13 +0000 | [diff] [blame] | 44 | /* Turn this on to debug init so it won't reboot when killed */ |
Eric Andersen | 2f6c04f | 1999-11-01 23:59:44 +0000 | [diff] [blame] | 45 | //#define DEBUG_INIT |
Eric Andersen | 7f1acfd | 1999-10-29 23:09:13 +0000 | [diff] [blame] | 46 | |
Eric Andersen | 8a8fbb8 | 1999-10-26 00:18:56 +0000 | [diff] [blame] | 47 | #define CONSOLE "/dev/console" /* Logical system console */ |
Eric Andersen | be971d6 | 1999-11-03 16:52:50 +0000 | [diff] [blame^] | 48 | #define VT_PRIMARY "/dev/tty1" /* Primary virtual console */ |
| 49 | #define VT_SECONDARY "/dev/tty2" /* Virtual console */ |
| 50 | #define VT_LOG "/dev/tty3" /* Virtual console */ |
Eric Andersen | 219d6f5 | 1999-11-02 19:43:01 +0000 | [diff] [blame] | 51 | #define SERIAL_CON0 "/dev/ttyS0" /* Primary serial console */ |
| 52 | #define SERIAL_CON1 "/dev/ttyS1" /* Serial console */ |
Eric Andersen | 8a8fbb8 | 1999-10-26 00:18:56 +0000 | [diff] [blame] | 53 | #define SHELL "/bin/sh" /* Default shell */ |
Eric Andersen | 219d6f5 | 1999-11-02 19:43:01 +0000 | [diff] [blame] | 54 | #define INITSCRIPT "/etc/init.d/rcS" /* Initscript. */ |
Eric Andersen | 0460ff2 | 1999-10-25 23:32:44 +0000 | [diff] [blame] | 55 | #define PATH_DEFAULT "PATH=/usr/local/sbin:/sbin:/bin:/usr/sbin:/usr/bin" |
| 56 | |
Eric Andersen | be971d6 | 1999-11-03 16:52:50 +0000 | [diff] [blame^] | 57 | static char *console = CONSOLE; |
| 58 | static char *second_console = VT_SECONDARY; |
| 59 | static char *log = VT_LOG; |
Eric Andersen | 0460ff2 | 1999-10-25 23:32:44 +0000 | [diff] [blame] | 60 | |
| 61 | |
| 62 | |
| 63 | /* try to open up the specified device */ |
Eric Andersen | 8a8fbb8 | 1999-10-26 00:18:56 +0000 | [diff] [blame] | 64 | int device_open(char *device, int mode) |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 65 | { |
Eric Andersen | 0460ff2 | 1999-10-25 23:32:44 +0000 | [diff] [blame] | 66 | int m, f, fd = -1; |
Eric Andersen | 8a8fbb8 | 1999-10-26 00:18:56 +0000 | [diff] [blame] | 67 | |
Eric Andersen | 7f1acfd | 1999-10-29 23:09:13 +0000 | [diff] [blame] | 68 | m = mode | O_NONBLOCK; |
Eric Andersen | 8a8fbb8 | 1999-10-26 00:18:56 +0000 | [diff] [blame] | 69 | |
Eric Andersen | 0460ff2 | 1999-10-25 23:32:44 +0000 | [diff] [blame] | 70 | /* Retry up to 5 times */ |
Eric Andersen | 8a8fbb8 | 1999-10-26 00:18:56 +0000 | [diff] [blame] | 71 | for (f = 0; f < 5; f++) |
| 72 | if ((fd = open(device, m)) >= 0) |
| 73 | break; |
| 74 | if (fd < 0) |
| 75 | return fd; |
Eric Andersen | 0460ff2 | 1999-10-25 23:32:44 +0000 | [diff] [blame] | 76 | /* Set original flags. */ |
| 77 | if (m != mode) |
| 78 | fcntl(fd, F_SETFL, mode); |
| 79 | return fd; |
| 80 | } |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 81 | |
Eric Andersen | 0460ff2 | 1999-10-25 23:32:44 +0000 | [diff] [blame] | 82 | /* print a message to the specified device */ |
Eric Andersen | 8a8fbb8 | 1999-10-26 00:18:56 +0000 | [diff] [blame] | 83 | void message(char *device, char *fmt, ...) |
Eric Andersen | 0460ff2 | 1999-10-25 23:32:44 +0000 | [diff] [blame] | 84 | { |
| 85 | int fd; |
| 86 | va_list arguments; |
Eric Andersen | 8a8fbb8 | 1999-10-26 00:18:56 +0000 | [diff] [blame] | 87 | |
Eric Andersen | c7c41d3 | 1999-10-28 00:24:35 +0000 | [diff] [blame] | 88 | if ((fd = device_open(device, O_WRONLY|O_NOCTTY|O_NDELAY)) >= 0) { |
Eric Andersen | 0460ff2 | 1999-10-25 23:32:44 +0000 | [diff] [blame] | 89 | va_start(arguments, fmt); |
| 90 | vdprintf(fd, fmt, arguments); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 91 | va_end(arguments); |
Eric Andersen | 8a8fbb8 | 1999-10-26 00:18:56 +0000 | [diff] [blame] | 92 | close(fd); |
Eric Andersen | a745606 | 1999-10-27 02:31:32 +0000 | [diff] [blame] | 93 | } else { |
| 94 | fprintf(stderr, "Bummer, can't print: "); |
| 95 | va_start(arguments, fmt); |
| 96 | vfprintf(stderr, fmt, arguments); |
| 97 | fflush(stderr); |
| 98 | va_end(arguments); |
| 99 | } |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 100 | } |
| 101 | |
Eric Andersen | 0460ff2 | 1999-10-25 23:32:44 +0000 | [diff] [blame] | 102 | /* Set terminal settings to reasonable defaults */ |
Eric Andersen | c7c41d3 | 1999-10-28 00:24:35 +0000 | [diff] [blame] | 103 | void set_term( int fd) |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 104 | { |
Eric Andersen | 0460ff2 | 1999-10-25 23:32:44 +0000 | [diff] [blame] | 105 | struct termios tty; |
Eric Andersen | 7f1acfd | 1999-10-29 23:09:13 +0000 | [diff] [blame] | 106 | static const char control_characters[] = { |
| 107 | '\003', '\034', '\177', '\025', '\004', '\0', |
| 108 | '\1', '\0', '\021', '\023', '\032', '\0', '\022', |
| 109 | '\017', '\027', '\026', '\0' |
| 110 | }; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 111 | |
Eric Andersen | c7c41d3 | 1999-10-28 00:24:35 +0000 | [diff] [blame] | 112 | tcgetattr(fd, &tty); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 113 | |
Eric Andersen | 7f1acfd | 1999-10-29 23:09:13 +0000 | [diff] [blame] | 114 | /* input modes */ |
| 115 | tty.c_iflag = IGNPAR|ICRNL|IXON|IXOFF|IXANY; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 116 | |
Eric Andersen | 219d6f5 | 1999-11-02 19:43:01 +0000 | [diff] [blame] | 117 | /* use line dicipline 0 */ |
Eric Andersen | 7f1acfd | 1999-10-29 23:09:13 +0000 | [diff] [blame] | 118 | tty.c_line = 0; |
| 119 | |
| 120 | /* output modes */ |
Eric Andersen | c7c41d3 | 1999-10-28 00:24:35 +0000 | [diff] [blame] | 121 | tty.c_oflag = OPOST|ONLCR; |
Eric Andersen | 7f1acfd | 1999-10-29 23:09:13 +0000 | [diff] [blame] | 122 | |
| 123 | /* local modes */ |
| 124 | tty.c_lflag = ISIG|ICANON|ECHO|ECHOE|ECHOK|ECHOCTL|ECHOPRT|ECHOKE|IEXTEN; |
| 125 | |
| 126 | /* control chars */ |
| 127 | memcpy(tty.c_cc, control_characters, sizeof(control_characters)); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 128 | |
Eric Andersen | c7c41d3 | 1999-10-28 00:24:35 +0000 | [diff] [blame] | 129 | tcsetattr(fd, TCSANOW, &tty); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 130 | } |
| 131 | |
Eric Andersen | 219d6f5 | 1999-11-02 19:43:01 +0000 | [diff] [blame] | 132 | /* How much memory does this machine have? */ |
Eric Andersen | 8a8fbb8 | 1999-10-26 00:18:56 +0000 | [diff] [blame] | 133 | static int mem_total() |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 134 | { |
Eric Andersen | abc7d59 | 1999-10-19 00:27:50 +0000 | [diff] [blame] | 135 | char s[80]; |
Eric Andersen | 219d6f5 | 1999-11-02 19:43:01 +0000 | [diff] [blame] | 136 | char *p = "/proc/meminfo"; |
Eric Andersen | abc7d59 | 1999-10-19 00:27:50 +0000 | [diff] [blame] | 137 | FILE *f; |
Eric Andersen | 8a8fbb8 | 1999-10-26 00:18:56 +0000 | [diff] [blame] | 138 | const char pattern[] = "MemTotal:"; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 139 | |
Eric Andersen | 219d6f5 | 1999-11-02 19:43:01 +0000 | [diff] [blame] | 140 | if ((f = fopen(p, "r")) < 0) { |
| 141 | message(log, "Error opening %s: %s\n", p, strerror( errno)); |
| 142 | return -1; |
| 143 | } |
Eric Andersen | 8a8fbb8 | 1999-10-26 00:18:56 +0000 | [diff] [blame] | 144 | while (NULL != fgets(s, 79, f)) { |
| 145 | p = strstr(s, pattern); |
Eric Andersen | abc7d59 | 1999-10-19 00:27:50 +0000 | [diff] [blame] | 146 | if (NULL != p) { |
| 147 | fclose(f); |
Eric Andersen | 8a8fbb8 | 1999-10-26 00:18:56 +0000 | [diff] [blame] | 148 | return (atoi(p + strlen(pattern))); |
Eric Andersen | abc7d59 | 1999-10-19 00:27:50 +0000 | [diff] [blame] | 149 | } |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 150 | } |
Eric Andersen | abc7d59 | 1999-10-19 00:27:50 +0000 | [diff] [blame] | 151 | return -1; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 152 | } |
| 153 | |
Eric Andersen | 8a8fbb8 | 1999-10-26 00:18:56 +0000 | [diff] [blame] | 154 | static void console_init() |
Eric Andersen | 0460ff2 | 1999-10-25 23:32:44 +0000 | [diff] [blame] | 155 | { |
| 156 | int fd; |
| 157 | int tried_devcons = 0; |
Eric Andersen | 219d6f5 | 1999-11-02 19:43:01 +0000 | [diff] [blame] | 158 | int tried_vtprimary = 0; |
Eric Andersen | 0460ff2 | 1999-10-25 23:32:44 +0000 | [diff] [blame] | 159 | char *s; |
| 160 | |
Eric Andersen | 219d6f5 | 1999-11-02 19:43:01 +0000 | [diff] [blame] | 161 | if ((s = getenv("CONSOLE")) != NULL) { |
Eric Andersen | 0460ff2 | 1999-10-25 23:32:44 +0000 | [diff] [blame] | 162 | console = s; |
Eric Andersen | 219d6f5 | 1999-11-02 19:43:01 +0000 | [diff] [blame] | 163 | /* Apparently the sparc does wierd things... */ |
| 164 | #if defined (__sparc__) |
| 165 | if (strncmp( s, "/dev/tty", 8 )==0) { |
| 166 | switch( s[8]) { |
| 167 | case 'a': |
| 168 | s=SERIAL_CON0; |
| 169 | break; |
| 170 | case 'b': |
| 171 | s=SERIAL_CON1; |
| 172 | } |
| 173 | } |
| 174 | #endif |
| 175 | } else { |
Eric Andersen | be971d6 | 1999-11-03 16:52:50 +0000 | [diff] [blame^] | 176 | console = CONSOLE; |
| 177 | tried_devcons++; |
Eric Andersen | 0460ff2 | 1999-10-25 23:32:44 +0000 | [diff] [blame] | 178 | } |
Eric Andersen | a745606 | 1999-10-27 02:31:32 +0000 | [diff] [blame] | 179 | |
Eric Andersen | 8a8fbb8 | 1999-10-26 00:18:56 +0000 | [diff] [blame] | 180 | while ((fd = open(console, O_RDONLY | O_NONBLOCK)) < 0) { |
Eric Andersen | 219d6f5 | 1999-11-02 19:43:01 +0000 | [diff] [blame] | 181 | /* Can't open selected console -- try /dev/console */ |
Eric Andersen | 0460ff2 | 1999-10-25 23:32:44 +0000 | [diff] [blame] | 182 | if (!tried_devcons) { |
| 183 | tried_devcons++; |
| 184 | console = CONSOLE; |
| 185 | continue; |
| 186 | } |
Eric Andersen | be971d6 | 1999-11-03 16:52:50 +0000 | [diff] [blame^] | 187 | /* Can't open selected console -- try vt1 */ |
| 188 | if (!tried_vtprimary) { |
| 189 | tried_vtprimary++; |
| 190 | console = VT_PRIMARY; |
| 191 | continue; |
| 192 | } |
Eric Andersen | 0460ff2 | 1999-10-25 23:32:44 +0000 | [diff] [blame] | 193 | break; |
| 194 | } |
| 195 | if (fd < 0) |
Eric Andersen | 219d6f5 | 1999-11-02 19:43:01 +0000 | [diff] [blame] | 196 | /* Perhaps we should panic here? */ |
Eric Andersen | 0460ff2 | 1999-10-25 23:32:44 +0000 | [diff] [blame] | 197 | console = "/dev/null"; |
| 198 | else |
| 199 | close(fd); |
Eric Andersen | 219d6f5 | 1999-11-02 19:43:01 +0000 | [diff] [blame] | 200 | message(log, "console=%s\n", console ); |
Eric Andersen | 0460ff2 | 1999-10-25 23:32:44 +0000 | [diff] [blame] | 201 | } |
| 202 | |
Eric Andersen | 8a8fbb8 | 1999-10-26 00:18:56 +0000 | [diff] [blame] | 203 | static int waitfor(int pid) |
Eric Andersen | 0460ff2 | 1999-10-25 23:32:44 +0000 | [diff] [blame] | 204 | { |
| 205 | int status, wpid; |
| 206 | |
| 207 | message(log, "Waiting for process %d.\n", pid); |
Eric Andersen | 8a8fbb8 | 1999-10-26 00:18:56 +0000 | [diff] [blame] | 208 | while ((wpid = wait(&status)) != pid) { |
| 209 | if (wpid > 0) |
| 210 | message(log, "pid %d exited, status=%x.\n", wpid, status); |
Eric Andersen | 0460ff2 | 1999-10-25 23:32:44 +0000 | [diff] [blame] | 211 | } |
| 212 | return wpid; |
| 213 | } |
| 214 | |
Eric Andersen | a745606 | 1999-10-27 02:31:32 +0000 | [diff] [blame] | 215 | |
Eric Andersen | c7c41d3 | 1999-10-28 00:24:35 +0000 | [diff] [blame] | 216 | static pid_t run(const char * const* command, |
| 217 | char *terminal, int get_enter) |
Eric Andersen | 0460ff2 | 1999-10-25 23:32:44 +0000 | [diff] [blame] | 218 | { |
Eric Andersen | 2f6c04f | 1999-11-01 23:59:44 +0000 | [diff] [blame] | 219 | int fd; |
Eric Andersen | c7c41d3 | 1999-10-28 00:24:35 +0000 | [diff] [blame] | 220 | pid_t pid; |
Eric Andersen | 219d6f5 | 1999-11-02 19:43:01 +0000 | [diff] [blame] | 221 | const char * const* cmd = command+1; |
Eric Andersen | 8a8fbb8 | 1999-10-26 00:18:56 +0000 | [diff] [blame] | 222 | static const char press_enter[] = |
Eric Andersen | 0460ff2 | 1999-10-25 23:32:44 +0000 | [diff] [blame] | 223 | "\nPlease press Enter to activate this console. "; |
| 224 | |
Eric Andersen | 0460ff2 | 1999-10-25 23:32:44 +0000 | [diff] [blame] | 225 | if ((pid = fork()) == 0) { |
| 226 | /* Clean up */ |
| 227 | close(0); |
| 228 | close(1); |
| 229 | close(2); |
| 230 | setsid(); |
| 231 | |
Eric Andersen | 2f6c04f | 1999-11-01 23:59:44 +0000 | [diff] [blame] | 232 | /* Reset signal handlers set for parent process */ |
| 233 | signal(SIGUSR1, SIG_DFL); |
| 234 | signal(SIGUSR2, SIG_DFL); |
| 235 | signal(SIGINT, SIG_DFL); |
| 236 | signal(SIGTERM, SIG_DFL); |
| 237 | |
| 238 | if ((fd = device_open(terminal, O_RDWR)) < 0) { |
| 239 | message(log, "Bummer, can't open %s\r\n", terminal); |
| 240 | exit(-1); |
| 241 | } |
| 242 | dup(fd); |
| 243 | dup(fd); |
Eric Andersen | c7c41d3 | 1999-10-28 00:24:35 +0000 | [diff] [blame] | 244 | tcsetpgrp(0, getpgrp()); |
| 245 | set_term(0); |
Eric Andersen | 0460ff2 | 1999-10-25 23:32:44 +0000 | [diff] [blame] | 246 | |
Eric Andersen | 2f6c04f | 1999-11-01 23:59:44 +0000 | [diff] [blame] | 247 | if (get_enter==TRUE) { |
Eric Andersen | 0460ff2 | 1999-10-25 23:32:44 +0000 | [diff] [blame] | 248 | /* |
| 249 | * Save memory by not exec-ing anything large (like a shell) |
| 250 | * before the user wants it. This is critical if swap is not |
| 251 | * enabled and the system has low memory. Generally this will |
| 252 | * be run on the second virtual console, and the first will |
| 253 | * be allowed to start a shell or whatever an init script |
| 254 | * specifies. |
| 255 | */ |
Eric Andersen | 8a8fbb8 | 1999-10-26 00:18:56 +0000 | [diff] [blame] | 256 | char c; |
Eric Andersen | 7f1acfd | 1999-10-29 23:09:13 +0000 | [diff] [blame] | 257 | write(1, press_enter, sizeof(press_enter) - 1); |
Eric Andersen | 0460ff2 | 1999-10-25 23:32:44 +0000 | [diff] [blame] | 258 | read(0, &c, 1); |
| 259 | } |
| 260 | |
| 261 | /* Log the process name and args */ |
Eric Andersen | 219d6f5 | 1999-11-02 19:43:01 +0000 | [diff] [blame] | 262 | message(log, "Starting pid(%d): '", getpid()); |
Eric Andersen | 2f6c04f | 1999-11-01 23:59:44 +0000 | [diff] [blame] | 263 | while ( *cmd) message(log, "%s ", *cmd++); |
| 264 | message(log, "'\r\n"); |
| 265 | |
Eric Andersen | c7c41d3 | 1999-10-28 00:24:35 +0000 | [diff] [blame] | 266 | /* Now run it. The new program will take over this PID, |
Eric Andersen | a745606 | 1999-10-27 02:31:32 +0000 | [diff] [blame] | 267 | * so nothing further in init.c should be run. */ |
Eric Andersen | c7c41d3 | 1999-10-28 00:24:35 +0000 | [diff] [blame] | 268 | execvp(*command, (char**)command+1); |
Eric Andersen | 0460ff2 | 1999-10-25 23:32:44 +0000 | [diff] [blame] | 269 | |
Eric Andersen | 219d6f5 | 1999-11-02 19:43:01 +0000 | [diff] [blame] | 270 | message(log, "Bummer, could not run '%s'\n", command); |
Eric Andersen | 0460ff2 | 1999-10-25 23:32:44 +0000 | [diff] [blame] | 271 | exit(-1); |
| 272 | } |
| 273 | return pid; |
| 274 | } |
| 275 | |
Eric Andersen | 219d6f5 | 1999-11-02 19:43:01 +0000 | [diff] [blame] | 276 | /* Make sure there is enough memory to do something useful. * |
| 277 | * Calls swapon if needed so be sure /proc is mounted. */ |
| 278 | static void check_memory() |
| 279 | { |
| 280 | struct stat statbuf; |
| 281 | const char* const swap_on_cmd[] = |
| 282 | { "/bin/swapon", "swapon", "-a", 0}; |
| 283 | const char *no_memory = |
| 284 | "Sorry, your computer does not have enough memory.\r\n"; |
| 285 | |
| 286 | if (mem_total() > 3500) |
| 287 | return; |
| 288 | |
| 289 | if (stat("/etc/fstab", &statbuf) == 0) { |
| 290 | /* Try to turn on swap */ |
| 291 | waitfor(run(swap_on_cmd, log, FALSE)); |
| 292 | if (mem_total() < 3500) |
| 293 | goto goodnight; |
| 294 | } else |
| 295 | goto goodnight; |
| 296 | return; |
| 297 | |
| 298 | goodnight: |
| 299 | message(console, "%s", no_memory); |
| 300 | while (1) sleep(1); |
| 301 | } |
| 302 | |
Eric Andersen | 8a8fbb8 | 1999-10-26 00:18:56 +0000 | [diff] [blame] | 303 | static void shutdown_system(void) |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 304 | { |
Eric Andersen | 2f6c04f | 1999-11-01 23:59:44 +0000 | [diff] [blame] | 305 | const char* const swap_off_cmd[] = { "swapoff", "swapoff", "-a", 0}; |
| 306 | const char* const umount_cmd[] = { "umount", "umount", "-a", "-n", 0}; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 307 | |
Eric Andersen | 8a8fbb8 | 1999-10-26 00:18:56 +0000 | [diff] [blame] | 308 | message(console, "The system is going down NOW !!\r\n"); |
Eric Andersen | 0460ff2 | 1999-10-25 23:32:44 +0000 | [diff] [blame] | 309 | sync(); |
Eric Andersen | 2f6c04f | 1999-11-01 23:59:44 +0000 | [diff] [blame] | 310 | #ifndef DEBUG_INIT |
Eric Andersen | 0460ff2 | 1999-10-25 23:32:44 +0000 | [diff] [blame] | 311 | /* Allow Ctrl-Alt-Del to reboot system. */ |
| 312 | reboot(RB_ENABLE_CAD); |
Eric Andersen | 2f6c04f | 1999-11-01 23:59:44 +0000 | [diff] [blame] | 313 | #endif |
Eric Andersen | 0460ff2 | 1999-10-25 23:32:44 +0000 | [diff] [blame] | 314 | /* Send signals to every process _except_ pid 1 */ |
| 315 | message(console, "Sending SIGHUP to all processes.\r\n"); |
Eric Andersen | 2f6c04f | 1999-11-01 23:59:44 +0000 | [diff] [blame] | 316 | #ifndef DEBUG_INIT |
Eric Andersen | 0460ff2 | 1999-10-25 23:32:44 +0000 | [diff] [blame] | 317 | kill(-1, SIGHUP); |
Eric Andersen | 2f6c04f | 1999-11-01 23:59:44 +0000 | [diff] [blame] | 318 | #endif |
Eric Andersen | 0460ff2 | 1999-10-25 23:32:44 +0000 | [diff] [blame] | 319 | sleep(2); |
| 320 | sync(); |
| 321 | message(console, "Sending SIGKILL to all processes.\r\n"); |
Eric Andersen | 2f6c04f | 1999-11-01 23:59:44 +0000 | [diff] [blame] | 322 | #ifndef DEBUG_INIT |
Eric Andersen | 0460ff2 | 1999-10-25 23:32:44 +0000 | [diff] [blame] | 323 | kill(-1, SIGKILL); |
Eric Andersen | 2f6c04f | 1999-11-01 23:59:44 +0000 | [diff] [blame] | 324 | #endif |
Eric Andersen | 0460ff2 | 1999-10-25 23:32:44 +0000 | [diff] [blame] | 325 | sleep(1); |
Eric Andersen | 2f6c04f | 1999-11-01 23:59:44 +0000 | [diff] [blame] | 326 | waitfor(run( swap_off_cmd, log, FALSE)); |
| 327 | waitfor(run( umount_cmd, log, FALSE)); |
Eric Andersen | 0460ff2 | 1999-10-25 23:32:44 +0000 | [diff] [blame] | 328 | sync(); |
Eric Andersen | 8a8fbb8 | 1999-10-26 00:18:56 +0000 | [diff] [blame] | 329 | if (get_kernel_revision() <= 2 * 65536 + 2 * 256 + 11) { |
Eric Andersen | 0460ff2 | 1999-10-25 23:32:44 +0000 | [diff] [blame] | 330 | /* Removed bdflush call, kupdate in kernels >2.2.11 */ |
| 331 | bdflush(1, 0); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 332 | sync(); |
Eric Andersen | 0460ff2 | 1999-10-25 23:32:44 +0000 | [diff] [blame] | 333 | } |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 334 | } |
| 335 | |
Eric Andersen | 8a8fbb8 | 1999-10-26 00:18:56 +0000 | [diff] [blame] | 336 | static void halt_signal(int sig) |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 337 | { |
Eric Andersen | abc7d59 | 1999-10-19 00:27:50 +0000 | [diff] [blame] | 338 | shutdown_system(); |
Eric Andersen | 8a8fbb8 | 1999-10-26 00:18:56 +0000 | [diff] [blame] | 339 | message(console, |
| 340 | "The system is halted. Press CTRL-ALT-DEL or turn off power\r\n"); |
Eric Andersen | 2f6c04f | 1999-11-01 23:59:44 +0000 | [diff] [blame] | 341 | #ifndef DEBUG_INIT |
Eric Andersen | 8a8fbb8 | 1999-10-26 00:18:56 +0000 | [diff] [blame] | 342 | reboot(RB_POWER_OFF); |
Eric Andersen | 2f6c04f | 1999-11-01 23:59:44 +0000 | [diff] [blame] | 343 | #endif |
Eric Andersen | abc7d59 | 1999-10-19 00:27:50 +0000 | [diff] [blame] | 344 | exit(0); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 345 | } |
| 346 | |
Eric Andersen | 8a8fbb8 | 1999-10-26 00:18:56 +0000 | [diff] [blame] | 347 | static void reboot_signal(int sig) |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 348 | { |
Eric Andersen | abc7d59 | 1999-10-19 00:27:50 +0000 | [diff] [blame] | 349 | shutdown_system(); |
| 350 | message(console, "Please stand by while rebooting the system.\r\n"); |
Eric Andersen | 2f6c04f | 1999-11-01 23:59:44 +0000 | [diff] [blame] | 351 | #ifndef DEBUG_INIT |
Eric Andersen | 8a8fbb8 | 1999-10-26 00:18:56 +0000 | [diff] [blame] | 352 | reboot(RB_AUTOBOOT); |
Eric Andersen | 2f6c04f | 1999-11-01 23:59:44 +0000 | [diff] [blame] | 353 | #endif |
Eric Andersen | abc7d59 | 1999-10-19 00:27:50 +0000 | [diff] [blame] | 354 | exit(0); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 355 | } |
Eric Andersen | 8a8fbb8 | 1999-10-26 00:18:56 +0000 | [diff] [blame] | 356 | |
Eric Andersen | 8a8fbb8 | 1999-10-26 00:18:56 +0000 | [diff] [blame] | 357 | extern int init_main(int argc, char **argv) |
Eric Andersen | 0460ff2 | 1999-10-25 23:32:44 +0000 | [diff] [blame] | 358 | { |
Eric Andersen | 8a8fbb8 | 1999-10-26 00:18:56 +0000 | [diff] [blame] | 359 | int run_rc = TRUE; |
Eric Andersen | 219d6f5 | 1999-11-02 19:43:01 +0000 | [diff] [blame] | 360 | int wait_for_enter = TRUE; |
Eric Andersen | c7c41d3 | 1999-10-28 00:24:35 +0000 | [diff] [blame] | 361 | pid_t pid1 = 0; |
| 362 | pid_t pid2 = 0; |
Eric Andersen | 8a8fbb8 | 1999-10-26 00:18:56 +0000 | [diff] [blame] | 363 | struct stat statbuf; |
Eric Andersen | 2f6c04f | 1999-11-01 23:59:44 +0000 | [diff] [blame] | 364 | const char* const init_commands[] = { INITSCRIPT, INITSCRIPT, 0}; |
| 365 | const char* const shell_commands[] = { SHELL, "-" SHELL, 0}; |
Eric Andersen | 219d6f5 | 1999-11-02 19:43:01 +0000 | [diff] [blame] | 366 | const char* const* tty0_commands = shell_commands; |
Eric Andersen | c7c41d3 | 1999-10-28 00:24:35 +0000 | [diff] [blame] | 367 | const char* const* tty1_commands = shell_commands; |
Eric Andersen | 2f6c04f | 1999-11-01 23:59:44 +0000 | [diff] [blame] | 368 | #ifdef DEBUG_INIT |
Eric Andersen | 8a8fbb8 | 1999-10-26 00:18:56 +0000 | [diff] [blame] | 369 | char *hello_msg_format = |
Eric Andersen | 0457978 | 1999-10-29 23:12:50 +0000 | [diff] [blame] | 370 | "init(%d) started: BusyBox v%s (%s) multi-call binary\r\n"; |
Eric Andersen | 2f6c04f | 1999-11-01 23:59:44 +0000 | [diff] [blame] | 371 | #else |
| 372 | char *hello_msg_format = |
| 373 | "init started: BusyBox v%s (%s) multi-call binary\r\n"; |
Eric Andersen | 7f1acfd | 1999-10-29 23:09:13 +0000 | [diff] [blame] | 374 | #endif |
Eric Andersen | 0460ff2 | 1999-10-25 23:32:44 +0000 | [diff] [blame] | 375 | |
Eric Andersen | 219d6f5 | 1999-11-02 19:43:01 +0000 | [diff] [blame] | 376 | |
| 377 | /* Check if we are supposed to be in single user mode */ |
| 378 | if ( argc > 1 && (!strcmp(argv[1], "single") || |
| 379 | !strcmp(argv[1], "-s") || !strcmp(argv[1], "1"))) { |
| 380 | run_rc = FALSE; |
| 381 | } |
| 382 | |
| 383 | |
Eric Andersen | 2f6c04f | 1999-11-01 23:59:44 +0000 | [diff] [blame] | 384 | /* Set up sig handlers -- be sure to |
| 385 | * clear all of these in run() */ |
Eric Andersen | 8a8fbb8 | 1999-10-26 00:18:56 +0000 | [diff] [blame] | 386 | signal(SIGUSR1, halt_signal); |
Eric Andersen | 8a8fbb8 | 1999-10-26 00:18:56 +0000 | [diff] [blame] | 387 | signal(SIGUSR2, reboot_signal); |
| 388 | signal(SIGINT, reboot_signal); |
| 389 | signal(SIGTERM, reboot_signal); |
Eric Andersen | 0460ff2 | 1999-10-25 23:32:44 +0000 | [diff] [blame] | 390 | |
Eric Andersen | 8a8fbb8 | 1999-10-26 00:18:56 +0000 | [diff] [blame] | 391 | /* Turn off rebooting via CTL-ALT-DEL -- we get a |
| 392 | * SIGINT on CAD so we can shut things down gracefully... */ |
Eric Andersen | 2f6c04f | 1999-11-01 23:59:44 +0000 | [diff] [blame] | 393 | #ifndef DEBUG_INIT |
Eric Andersen | 8a8fbb8 | 1999-10-26 00:18:56 +0000 | [diff] [blame] | 394 | reboot(RB_DISABLE_CAD); |
Eric Andersen | 7f1acfd | 1999-10-29 23:09:13 +0000 | [diff] [blame] | 395 | #endif |
Eric Andersen | 219d6f5 | 1999-11-02 19:43:01 +0000 | [diff] [blame] | 396 | |
Eric Andersen | c7c41d3 | 1999-10-28 00:24:35 +0000 | [diff] [blame] | 397 | /* Figure out where the default console should be */ |
| 398 | console_init(); |
Eric Andersen | 0460ff2 | 1999-10-25 23:32:44 +0000 | [diff] [blame] | 399 | |
Eric Andersen | 8a8fbb8 | 1999-10-26 00:18:56 +0000 | [diff] [blame] | 400 | /* Close whatever files are open, and reset the console. */ |
| 401 | close(0); |
| 402 | close(1); |
| 403 | close(2); |
Eric Andersen | 7f1acfd | 1999-10-29 23:09:13 +0000 | [diff] [blame] | 404 | set_term(0); |
Eric Andersen | 8a8fbb8 | 1999-10-26 00:18:56 +0000 | [diff] [blame] | 405 | setsid(); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 406 | |
Eric Andersen | 8a8fbb8 | 1999-10-26 00:18:56 +0000 | [diff] [blame] | 407 | /* Make sure PATH is set to something sane */ |
Eric Andersen | 219d6f5 | 1999-11-02 19:43:01 +0000 | [diff] [blame] | 408 | putenv(PATH_DEFAULT); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 409 | |
Eric Andersen | 219d6f5 | 1999-11-02 19:43:01 +0000 | [diff] [blame] | 410 | |
Eric Andersen | 8a8fbb8 | 1999-10-26 00:18:56 +0000 | [diff] [blame] | 411 | /* Hello world */ |
Eric Andersen | 7f1acfd | 1999-10-29 23:09:13 +0000 | [diff] [blame] | 412 | #ifndef DEBUG_INIT |
Eric Andersen | c1525e8 | 1999-10-29 00:07:31 +0000 | [diff] [blame] | 413 | message(log, hello_msg_format, BB_VER, BB_BT); |
Eric Andersen | 7f1acfd | 1999-10-29 23:09:13 +0000 | [diff] [blame] | 414 | message(console, hello_msg_format, BB_VER, BB_BT); |
| 415 | #else |
| 416 | message(log, hello_msg_format, getpid(), BB_VER, BB_BT); |
| 417 | message(console, hello_msg_format, getpid(), BB_VER, BB_BT); |
| 418 | #endif |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 419 | |
Eric Andersen | c7c41d3 | 1999-10-28 00:24:35 +0000 | [diff] [blame] | 420 | |
Eric Andersen | 8a8fbb8 | 1999-10-26 00:18:56 +0000 | [diff] [blame] | 421 | /* Mount /proc */ |
Eric Andersen | 2f6c04f | 1999-11-01 23:59:44 +0000 | [diff] [blame] | 422 | if (mount ("proc", "/proc", "proc", 0, 0) == 0) { |
Eric Andersen | a745606 | 1999-10-27 02:31:32 +0000 | [diff] [blame] | 423 | message(log, "Mounting /proc: done.\n"); |
Eric Andersen | 7f1acfd | 1999-10-29 23:09:13 +0000 | [diff] [blame] | 424 | message(console, "Mounting /proc: done.\n"); |
Eric Andersen | c7c41d3 | 1999-10-28 00:24:35 +0000 | [diff] [blame] | 425 | } else { |
Eric Andersen | c7c41d3 | 1999-10-28 00:24:35 +0000 | [diff] [blame] | 426 | message(log, "Mounting /proc: failed!\n"); |
Eric Andersen | 7f1acfd | 1999-10-29 23:09:13 +0000 | [diff] [blame] | 427 | message(console, "Mounting /proc: failed!\n"); |
Eric Andersen | 8a8fbb8 | 1999-10-26 00:18:56 +0000 | [diff] [blame] | 428 | } |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 429 | |
Eric Andersen | 219d6f5 | 1999-11-02 19:43:01 +0000 | [diff] [blame] | 430 | /* Make sure there is enough memory to do something useful. */ |
| 431 | check_memory(); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 432 | |
Eric Andersen | 8a8fbb8 | 1999-10-26 00:18:56 +0000 | [diff] [blame] | 433 | |
| 434 | /* Make sure an init script exists before trying to run it */ |
Eric Andersen | 219d6f5 | 1999-11-02 19:43:01 +0000 | [diff] [blame] | 435 | if (run_rc == TRUE && stat(INITSCRIPT, &statbuf)==0) { |
| 436 | wait_for_enter = FALSE; |
| 437 | tty0_commands = init_commands; |
Eric Andersen | 8a8fbb8 | 1999-10-26 00:18:56 +0000 | [diff] [blame] | 438 | } |
| 439 | |
Eric Andersen | 219d6f5 | 1999-11-02 19:43:01 +0000 | [diff] [blame] | 440 | |
Eric Andersen | 8a8fbb8 | 1999-10-26 00:18:56 +0000 | [diff] [blame] | 441 | /* Ok, now launch the rc script and/or prepare to |
| 442 | * start up some VTs if somebody hits enter... |
| 443 | */ |
| 444 | for (;;) { |
Eric Andersen | c7c41d3 | 1999-10-28 00:24:35 +0000 | [diff] [blame] | 445 | pid_t wpid; |
Eric Andersen | 8a8fbb8 | 1999-10-26 00:18:56 +0000 | [diff] [blame] | 446 | int status; |
| 447 | |
| 448 | if (pid1 == 0 && tty0_commands) { |
Eric Andersen | 2f6c04f | 1999-11-01 23:59:44 +0000 | [diff] [blame] | 449 | pid1 = run(tty0_commands, console, wait_for_enter); |
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 | if (pid2 == 0 && tty1_commands) { |
Eric Andersen | be971d6 | 1999-11-03 16:52:50 +0000 | [diff] [blame^] | 452 | pid2 = run(tty1_commands, second_console, TRUE); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 453 | } |
Eric Andersen | 8a8fbb8 | 1999-10-26 00:18:56 +0000 | [diff] [blame] | 454 | wpid = wait(&status); |
Eric Andersen | a745606 | 1999-10-27 02:31:32 +0000 | [diff] [blame] | 455 | if (wpid > 0 ) { |
Eric Andersen | 8a8fbb8 | 1999-10-26 00:18:56 +0000 | [diff] [blame] | 456 | message(log, "pid %d exited, status=%x.\n", wpid, status); |
| 457 | } |
Eric Andersen | 219d6f5 | 1999-11-02 19:43:01 +0000 | [diff] [blame] | 458 | if (wpid == pid1) { |
Eric Andersen | 8a8fbb8 | 1999-10-26 00:18:56 +0000 | [diff] [blame] | 459 | pid1 = 0; |
Eric Andersen | 219d6f5 | 1999-11-02 19:43:01 +0000 | [diff] [blame] | 460 | if (run_rc == TRUE) { |
| 461 | /* Don't respawn init script if it exits. */ |
| 462 | run_rc=FALSE; |
| 463 | wait_for_enter=TRUE; |
| 464 | tty0_commands=shell_commands; |
| 465 | } |
Eric Andersen | 8a8fbb8 | 1999-10-26 00:18:56 +0000 | [diff] [blame] | 466 | } |
| 467 | if (wpid == pid2) { |
| 468 | pid2 = 0; |
| 469 | } |
| 470 | sleep(1); |
| 471 | } |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 472 | } |