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 | 0460ff2 | 1999-10-25 23:32:44 +0000 | [diff] [blame] | 44 | #define DEBUG_INIT |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 45 | |
Eric Andersen | 8a8fbb8 | 1999-10-26 00:18:56 +0000 | [diff] [blame^] | 46 | #define CONSOLE "/dev/console" /* Logical system console */ |
| 47 | #define VT_PRIMARY "/dev/tty0" /* Virtual console master */ |
| 48 | #define VT_SECONDARY "/dev/tty1" /* Virtual console master */ |
| 49 | #define VT_LOG "/dev/tty2" /* Virtual console master */ |
| 50 | #define SHELL "/bin/sh" /* Default shell */ |
| 51 | #define INITSCRIPT "/etc/init.d/rcS" /* Initscript. */ |
Eric Andersen | 0460ff2 | 1999-10-25 23:32:44 +0000 | [diff] [blame] | 52 | #define PATH_DEFAULT "PATH=/usr/local/sbin:/sbin:/bin:/usr/sbin:/usr/bin" |
| 53 | |
Eric Andersen | 8a8fbb8 | 1999-10-26 00:18:56 +0000 | [diff] [blame^] | 54 | static char *console = CONSOLE; |
| 55 | static char *second_terminal = "/dev/tty2"; |
| 56 | static char *log = "/dev/tty3"; |
Eric Andersen | 0460ff2 | 1999-10-25 23:32:44 +0000 | [diff] [blame] | 57 | |
| 58 | |
| 59 | |
| 60 | /* try to open up the specified device */ |
Eric Andersen | 8a8fbb8 | 1999-10-26 00:18:56 +0000 | [diff] [blame^] | 61 | int device_open(char *device, int mode) |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 62 | { |
Eric Andersen | 0460ff2 | 1999-10-25 23:32:44 +0000 | [diff] [blame] | 63 | int m, f, fd = -1; |
Eric Andersen | 8a8fbb8 | 1999-10-26 00:18:56 +0000 | [diff] [blame^] | 64 | |
Eric Andersen | 0460ff2 | 1999-10-25 23:32:44 +0000 | [diff] [blame] | 65 | mode = m | O_NONBLOCK; |
Eric Andersen | 8a8fbb8 | 1999-10-26 00:18:56 +0000 | [diff] [blame^] | 66 | |
Eric Andersen | 0460ff2 | 1999-10-25 23:32:44 +0000 | [diff] [blame] | 67 | /* Retry up to 5 times */ |
Eric Andersen | 8a8fbb8 | 1999-10-26 00:18:56 +0000 | [diff] [blame^] | 68 | for (f = 0; f < 5; f++) |
| 69 | if ((fd = open(device, m)) >= 0) |
| 70 | break; |
| 71 | if (fd < 0) |
| 72 | return fd; |
Eric Andersen | 0460ff2 | 1999-10-25 23:32:44 +0000 | [diff] [blame] | 73 | /* Set original flags. */ |
| 74 | if (m != mode) |
| 75 | fcntl(fd, F_SETFL, mode); |
| 76 | return fd; |
| 77 | } |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 78 | |
Eric Andersen | 0460ff2 | 1999-10-25 23:32:44 +0000 | [diff] [blame] | 79 | /* print a message to the specified device */ |
Eric Andersen | 8a8fbb8 | 1999-10-26 00:18:56 +0000 | [diff] [blame^] | 80 | void message(char *device, char *fmt, ...) |
Eric Andersen | 0460ff2 | 1999-10-25 23:32:44 +0000 | [diff] [blame] | 81 | { |
| 82 | int fd; |
| 83 | va_list arguments; |
Eric Andersen | 8a8fbb8 | 1999-10-26 00:18:56 +0000 | [diff] [blame^] | 84 | |
| 85 | if ((fd = device_open(device, O_WRONLY | O_NOCTTY | O_NDELAY)) >= 0) { |
Eric Andersen | 0460ff2 | 1999-10-25 23:32:44 +0000 | [diff] [blame] | 86 | va_start(arguments, fmt); |
| 87 | vdprintf(fd, fmt, arguments); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 88 | va_end(arguments); |
Eric Andersen | 8a8fbb8 | 1999-10-26 00:18:56 +0000 | [diff] [blame^] | 89 | close(fd); |
| 90 | } else |
| 91 | vprintf(fmt, arguments); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 92 | } |
| 93 | |
Eric Andersen | 0460ff2 | 1999-10-25 23:32:44 +0000 | [diff] [blame] | 94 | /* Set terminal settings to reasonable defaults */ |
| 95 | void set_term() |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 96 | { |
Eric Andersen | 0460ff2 | 1999-10-25 23:32:44 +0000 | [diff] [blame] | 97 | int fd; |
| 98 | struct termios tty; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 99 | |
Eric Andersen | 8a8fbb8 | 1999-10-26 00:18:56 +0000 | [diff] [blame^] | 100 | if ((fd = device_open(console, O_RDWR | O_NOCTTY)) < 0) { |
| 101 | message(log, "can't open %s\n", console); |
Eric Andersen | 0460ff2 | 1999-10-25 23:32:44 +0000 | [diff] [blame] | 102 | return; |
| 103 | } |
| 104 | ioctl(fd, TCGETS, &tty); |
Eric Andersen | 8a8fbb8 | 1999-10-26 00:18:56 +0000 | [diff] [blame^] | 105 | tty.c_cflag &= CBAUD | CBAUDEX | CSIZE | CSTOPB | PARENB | PARODD; |
| 106 | tty.c_cflag |= HUPCL | CLOCAL; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 107 | |
Eric Andersen | 8a8fbb8 | 1999-10-26 00:18:56 +0000 | [diff] [blame^] | 108 | tty.c_cc[VINTR] = 3; |
| 109 | tty.c_cc[VQUIT] = 28; |
Eric Andersen | 0460ff2 | 1999-10-25 23:32:44 +0000 | [diff] [blame] | 110 | tty.c_cc[VERASE] = 127; |
Eric Andersen | 8a8fbb8 | 1999-10-26 00:18:56 +0000 | [diff] [blame^] | 111 | tty.c_cc[VKILL] = 24; |
| 112 | tty.c_cc[VEOF] = 4; |
| 113 | tty.c_cc[VTIME] = 0; |
| 114 | tty.c_cc[VMIN] = 1; |
Eric Andersen | 0460ff2 | 1999-10-25 23:32:44 +0000 | [diff] [blame] | 115 | tty.c_cc[VSTART] = 17; |
Eric Andersen | 8a8fbb8 | 1999-10-26 00:18:56 +0000 | [diff] [blame^] | 116 | tty.c_cc[VSTOP] = 19; |
| 117 | tty.c_cc[VSUSP] = 26; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 118 | |
Eric Andersen | 0460ff2 | 1999-10-25 23:32:44 +0000 | [diff] [blame] | 119 | /* Set pre and post processing */ |
Eric Andersen | 8a8fbb8 | 1999-10-26 00:18:56 +0000 | [diff] [blame^] | 120 | tty.c_iflag = IGNPAR | ICRNL | IXON | IXANY; |
| 121 | tty.c_oflag = OPOST | ONLCR; |
| 122 | tty.c_lflag = ISIG | ICANON | ECHO | ECHOCTL | ECHOPRT | ECHOKE; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 123 | |
Eric Andersen | 0460ff2 | 1999-10-25 23:32:44 +0000 | [diff] [blame] | 124 | /* Now set the terminal line. */ |
| 125 | ioctl(fd, TCSETS, &tty); |
Eric Andersen | 8a8fbb8 | 1999-10-26 00:18:56 +0000 | [diff] [blame^] | 126 | close(fd); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 127 | } |
| 128 | |
Eric Andersen | 8a8fbb8 | 1999-10-26 00:18:56 +0000 | [diff] [blame^] | 129 | static int mem_total() |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 130 | { |
Eric Andersen | abc7d59 | 1999-10-19 00:27:50 +0000 | [diff] [blame] | 131 | char s[80]; |
| 132 | char *p; |
| 133 | FILE *f; |
Eric Andersen | 8a8fbb8 | 1999-10-26 00:18:56 +0000 | [diff] [blame^] | 134 | const char pattern[] = "MemTotal:"; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 135 | |
Eric Andersen | 8a8fbb8 | 1999-10-26 00:18:56 +0000 | [diff] [blame^] | 136 | f = fopen("/proc/meminfo", "r"); |
| 137 | while (NULL != fgets(s, 79, f)) { |
| 138 | p = strstr(s, pattern); |
Eric Andersen | abc7d59 | 1999-10-19 00:27:50 +0000 | [diff] [blame] | 139 | if (NULL != p) { |
| 140 | fclose(f); |
Eric Andersen | 8a8fbb8 | 1999-10-26 00:18:56 +0000 | [diff] [blame^] | 141 | return (atoi(p + strlen(pattern))); |
Eric Andersen | abc7d59 | 1999-10-19 00:27:50 +0000 | [diff] [blame] | 142 | } |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 143 | } |
Eric Andersen | abc7d59 | 1999-10-19 00:27:50 +0000 | [diff] [blame] | 144 | return -1; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 145 | } |
| 146 | |
Eric Andersen | 8a8fbb8 | 1999-10-26 00:18:56 +0000 | [diff] [blame^] | 147 | static void set_free_pages() |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 148 | { |
Eric Andersen | abc7d59 | 1999-10-19 00:27:50 +0000 | [diff] [blame] | 149 | char s[80]; |
| 150 | FILE *f; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 151 | |
Eric Andersen | 8a8fbb8 | 1999-10-26 00:18:56 +0000 | [diff] [blame^] | 152 | f = fopen("/proc/sys/vm/freepages", "r"); |
| 153 | fgets(s, 79, f); |
Eric Andersen | abc7d59 | 1999-10-19 00:27:50 +0000 | [diff] [blame] | 154 | if (atoi(s) < 32) { |
| 155 | fclose(f); |
Eric Andersen | 8a8fbb8 | 1999-10-26 00:18:56 +0000 | [diff] [blame^] | 156 | f = fopen("/proc/sys/vm/freepages", "w"); |
| 157 | fprintf(f, "30\t40\t50\n"); |
Eric Andersen | abc7d59 | 1999-10-19 00:27:50 +0000 | [diff] [blame] | 158 | printf("\nIncreased /proc/sys/vm/freepages values to 30/40/50\n"); |
| 159 | } |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 160 | fclose(f); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 161 | } |
| 162 | |
Eric Andersen | 0460ff2 | 1999-10-25 23:32:44 +0000 | [diff] [blame] | 163 | |
Eric Andersen | 8a8fbb8 | 1999-10-26 00:18:56 +0000 | [diff] [blame^] | 164 | static void console_init() |
Eric Andersen | 0460ff2 | 1999-10-25 23:32:44 +0000 | [diff] [blame] | 165 | { |
| 166 | int fd; |
| 167 | int tried_devcons = 0; |
| 168 | int tried_vtmaster = 0; |
| 169 | char *s; |
| 170 | |
| 171 | if ((s = getenv("CONSOLE")) != NULL) |
| 172 | console = s; |
| 173 | else { |
| 174 | console = CONSOLE; |
| 175 | tried_devcons++; |
| 176 | } |
Eric Andersen | 8a8fbb8 | 1999-10-26 00:18:56 +0000 | [diff] [blame^] | 177 | while ((fd = open(console, O_RDONLY | O_NONBLOCK)) < 0) { |
Eric Andersen | 0460ff2 | 1999-10-25 23:32:44 +0000 | [diff] [blame] | 178 | if (!tried_devcons) { |
| 179 | tried_devcons++; |
| 180 | console = CONSOLE; |
| 181 | continue; |
| 182 | } |
| 183 | if (!tried_vtmaster) { |
| 184 | tried_vtmaster++; |
| 185 | console = VT_PRIMARY; |
| 186 | continue; |
| 187 | } |
| 188 | break; |
| 189 | } |
| 190 | if (fd < 0) |
| 191 | console = "/dev/null"; |
| 192 | else |
| 193 | close(fd); |
| 194 | } |
| 195 | |
Eric Andersen | 8a8fbb8 | 1999-10-26 00:18:56 +0000 | [diff] [blame^] | 196 | static int waitfor(int pid) |
Eric Andersen | 0460ff2 | 1999-10-25 23:32:44 +0000 | [diff] [blame] | 197 | { |
| 198 | int status, wpid; |
| 199 | |
| 200 | message(log, "Waiting for process %d.\n", pid); |
Eric Andersen | 8a8fbb8 | 1999-10-26 00:18:56 +0000 | [diff] [blame^] | 201 | while ((wpid = wait(&status)) != pid) { |
| 202 | if (wpid > 0) |
| 203 | message(log, "pid %d exited, status=%x.\n", wpid, status); |
Eric Andersen | 0460ff2 | 1999-10-25 23:32:44 +0000 | [diff] [blame] | 204 | } |
| 205 | return wpid; |
| 206 | } |
| 207 | |
Eric Andersen | 8a8fbb8 | 1999-10-26 00:18:56 +0000 | [diff] [blame^] | 208 | static int run(const char *command, char *terminal, int get_enter) |
Eric Andersen | 0460ff2 | 1999-10-25 23:32:44 +0000 | [diff] [blame] | 209 | { |
Eric Andersen | 8a8fbb8 | 1999-10-26 00:18:56 +0000 | [diff] [blame^] | 210 | int f, pid; |
Eric Andersen | 0460ff2 | 1999-10-25 23:32:44 +0000 | [diff] [blame] | 211 | char *args[16]; |
| 212 | char buf[256]; |
Eric Andersen | 8a8fbb8 | 1999-10-26 00:18:56 +0000 | [diff] [blame^] | 213 | char *ptr; |
| 214 | static const char press_enter[] = |
Eric Andersen | 0460ff2 | 1999-10-25 23:32:44 +0000 | [diff] [blame] | 215 | "\nPlease press Enter to activate this console. "; |
| 216 | |
Eric Andersen | 8a8fbb8 | 1999-10-26 00:18:56 +0000 | [diff] [blame^] | 217 | |
| 218 | /* Make a proper command from the command string */ |
Eric Andersen | 0460ff2 | 1999-10-25 23:32:44 +0000 | [diff] [blame] | 219 | strcpy(buf, command); |
| 220 | ptr = buf; |
Eric Andersen | 8a8fbb8 | 1999-10-26 00:18:56 +0000 | [diff] [blame^] | 221 | for (f = 1; f < 15; f++) { |
Eric Andersen | 0460ff2 | 1999-10-25 23:32:44 +0000 | [diff] [blame] | 222 | /* Skip white space */ |
Eric Andersen | 8a8fbb8 | 1999-10-26 00:18:56 +0000 | [diff] [blame^] | 223 | while (*ptr == ' ' || *ptr == '\t') |
| 224 | ptr++; |
Eric Andersen | 0460ff2 | 1999-10-25 23:32:44 +0000 | [diff] [blame] | 225 | args[f] = ptr; |
| 226 | /* May be trailing space.. */ |
Eric Andersen | 8a8fbb8 | 1999-10-26 00:18:56 +0000 | [diff] [blame^] | 227 | if (*ptr == 0) |
| 228 | break; |
Eric Andersen | 0460ff2 | 1999-10-25 23:32:44 +0000 | [diff] [blame] | 229 | /* Skip this `word' */ |
Eric Andersen | 8a8fbb8 | 1999-10-26 00:18:56 +0000 | [diff] [blame^] | 230 | while (*ptr && *ptr != ' ' && *ptr != '\t' && *ptr != '#') |
Eric Andersen | 0460ff2 | 1999-10-25 23:32:44 +0000 | [diff] [blame] | 231 | ptr++; |
| 232 | /* If end-of-line, break */ |
| 233 | if (*ptr == '#' || *ptr == 0) { |
| 234 | f++; |
| 235 | *ptr = 0; |
| 236 | break; |
| 237 | } |
| 238 | /* End word with \0 and continue */ |
| 239 | args[f] = NULL; |
| 240 | } |
| 241 | args[0] = args[1]; |
Eric Andersen | 8a8fbb8 | 1999-10-26 00:18:56 +0000 | [diff] [blame^] | 242 | |
Eric Andersen | 0460ff2 | 1999-10-25 23:32:44 +0000 | [diff] [blame] | 243 | |
| 244 | if ((pid = fork()) == 0) { |
| 245 | /* Clean up */ |
| 246 | close(0); |
| 247 | close(1); |
| 248 | close(2); |
| 249 | setsid(); |
| 250 | |
Eric Andersen | 8a8fbb8 | 1999-10-26 00:18:56 +0000 | [diff] [blame^] | 251 | if ((f = device_open(terminal, O_RDWR | O_NOCTTY)) < 0) { |
| 252 | message(log, "open(%s) failed: %s\n", terminal, strerror(errno)); |
Eric Andersen | 0460ff2 | 1999-10-25 23:32:44 +0000 | [diff] [blame] | 253 | return -1; |
| 254 | } |
| 255 | dup(f); |
| 256 | dup(f); |
| 257 | tcsetpgrp(0, getpgrp()); |
| 258 | set_term(); |
| 259 | |
Eric Andersen | 8a8fbb8 | 1999-10-26 00:18:56 +0000 | [diff] [blame^] | 260 | if (get_enter) { |
Eric Andersen | 0460ff2 | 1999-10-25 23:32:44 +0000 | [diff] [blame] | 261 | /* |
| 262 | * Save memory by not exec-ing anything large (like a shell) |
| 263 | * before the user wants it. This is critical if swap is not |
| 264 | * enabled and the system has low memory. Generally this will |
| 265 | * be run on the second virtual console, and the first will |
| 266 | * be allowed to start a shell or whatever an init script |
| 267 | * specifies. |
| 268 | */ |
Eric Andersen | 8a8fbb8 | 1999-10-26 00:18:56 +0000 | [diff] [blame^] | 269 | char c; |
Eric Andersen | 0460ff2 | 1999-10-25 23:32:44 +0000 | [diff] [blame] | 270 | write(1, press_enter, sizeof(press_enter) - 1); |
| 271 | read(0, &c, 1); |
Eric Andersen | 8a8fbb8 | 1999-10-26 00:18:56 +0000 | [diff] [blame^] | 272 | message(console, "Got an enter\r\n"); |
Eric Andersen | 0460ff2 | 1999-10-25 23:32:44 +0000 | [diff] [blame] | 273 | } |
| 274 | |
| 275 | /* Log the process name and args */ |
Eric Andersen | 8a8fbb8 | 1999-10-26 00:18:56 +0000 | [diff] [blame^] | 276 | message(console, "Executing "); |
| 277 | message(console, "'%s'\r\n", command); |
Eric Andersen | 0460ff2 | 1999-10-25 23:32:44 +0000 | [diff] [blame] | 278 | |
| 279 | /* Now run it. This should take over the PID, so nothing |
| 280 | * further in init.c should be run by this PID. */ |
| 281 | execvp(args[1], args + 1); |
| 282 | |
Eric Andersen | 8a8fbb8 | 1999-10-26 00:18:56 +0000 | [diff] [blame^] | 283 | message(console, "Hmm. Trying as a script.\r\n"); |
Eric Andersen | 0460ff2 | 1999-10-25 23:32:44 +0000 | [diff] [blame] | 284 | /* If shell scripts are not executed, force the issue */ |
| 285 | if (errno == ENOEXEC) { |
| 286 | char buf[256]; |
| 287 | args[1] = SHELL; |
| 288 | args[2] = "-c"; |
| 289 | strcpy(buf, "exec "); |
| 290 | strcat(buf, command); |
| 291 | args[3] = buf; |
| 292 | args[4] = NULL; |
| 293 | execvp(args[1], args + 1); |
| 294 | } |
Eric Andersen | 8a8fbb8 | 1999-10-26 00:18:56 +0000 | [diff] [blame^] | 295 | message(console, "Could not execute '%s'\n", command); |
Eric Andersen | 0460ff2 | 1999-10-25 23:32:44 +0000 | [diff] [blame] | 296 | exit(-1); |
| 297 | } |
| 298 | return pid; |
| 299 | } |
| 300 | |
| 301 | #ifndef DEBUG_INIT |
Eric Andersen | 8a8fbb8 | 1999-10-26 00:18:56 +0000 | [diff] [blame^] | 302 | static void shutdown_system(void) |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 303 | { |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 304 | |
Eric Andersen | 8a8fbb8 | 1999-10-26 00:18:56 +0000 | [diff] [blame^] | 305 | message(console, "The system is going down NOW !!\r\n"); |
Eric Andersen | 0460ff2 | 1999-10-25 23:32:44 +0000 | [diff] [blame] | 306 | sync(); |
| 307 | /* Allow Ctrl-Alt-Del to reboot system. */ |
| 308 | reboot(RB_ENABLE_CAD); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 309 | |
Eric Andersen | 0460ff2 | 1999-10-25 23:32:44 +0000 | [diff] [blame] | 310 | /* Send signals to every process _except_ pid 1 */ |
| 311 | message(console, "Sending SIGHUP to all processes.\r\n"); |
| 312 | kill(-1, SIGHUP); |
| 313 | sleep(2); |
| 314 | sync(); |
| 315 | message(console, "Sending SIGKILL to all processes.\r\n"); |
| 316 | kill(-1, SIGKILL); |
| 317 | sleep(1); |
| 318 | waitfor(run("/bin/swapoff -a", console, 0)); |
| 319 | waitfor(run("/bin/umount -a -n", console, 0)); |
| 320 | sync(); |
Eric Andersen | 8a8fbb8 | 1999-10-26 00:18:56 +0000 | [diff] [blame^] | 321 | if (get_kernel_revision() <= 2 * 65536 + 2 * 256 + 11) { |
Eric Andersen | 0460ff2 | 1999-10-25 23:32:44 +0000 | [diff] [blame] | 322 | /* Removed bdflush call, kupdate in kernels >2.2.11 */ |
| 323 | bdflush(1, 0); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 324 | sync(); |
Eric Andersen | 0460ff2 | 1999-10-25 23:32:44 +0000 | [diff] [blame] | 325 | } |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 326 | } |
| 327 | |
Eric Andersen | 8a8fbb8 | 1999-10-26 00:18:56 +0000 | [diff] [blame^] | 328 | static void halt_signal(int sig) |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 329 | { |
Eric Andersen | abc7d59 | 1999-10-19 00:27:50 +0000 | [diff] [blame] | 330 | shutdown_system(); |
Eric Andersen | 8a8fbb8 | 1999-10-26 00:18:56 +0000 | [diff] [blame^] | 331 | message(console, |
| 332 | "The system is halted. Press CTRL-ALT-DEL or turn off power\r\n"); |
| 333 | reboot(RB_POWER_OFF); |
Eric Andersen | abc7d59 | 1999-10-19 00:27:50 +0000 | [diff] [blame] | 334 | exit(0); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 335 | } |
| 336 | |
Eric Andersen | 8a8fbb8 | 1999-10-26 00:18:56 +0000 | [diff] [blame^] | 337 | static void reboot_signal(int sig) |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 338 | { |
Eric Andersen | abc7d59 | 1999-10-19 00:27:50 +0000 | [diff] [blame] | 339 | shutdown_system(); |
| 340 | message(console, "Please stand by while rebooting the system.\r\n"); |
Eric Andersen | 8a8fbb8 | 1999-10-26 00:18:56 +0000 | [diff] [blame^] | 341 | reboot(RB_AUTOBOOT); |
Eric Andersen | abc7d59 | 1999-10-19 00:27:50 +0000 | [diff] [blame] | 342 | exit(0); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 343 | } |
Eric Andersen | 8a8fbb8 | 1999-10-26 00:18:56 +0000 | [diff] [blame^] | 344 | |
Eric Andersen | abc7d59 | 1999-10-19 00:27:50 +0000 | [diff] [blame] | 345 | #endif |
Eric Andersen | 0460ff2 | 1999-10-25 23:32:44 +0000 | [diff] [blame] | 346 | |
Eric Andersen | 8a8fbb8 | 1999-10-26 00:18:56 +0000 | [diff] [blame^] | 347 | extern int init_main(int argc, char **argv) |
Eric Andersen | 0460ff2 | 1999-10-25 23:32:44 +0000 | [diff] [blame] | 348 | { |
Eric Andersen | 8a8fbb8 | 1999-10-26 00:18:56 +0000 | [diff] [blame^] | 349 | int run_rc = TRUE; |
| 350 | int pid1 = 0; |
| 351 | int pid2 = 0; |
| 352 | struct stat statbuf; |
| 353 | const char *init_commands = SHELL "-c exec " INITSCRIPT; |
| 354 | const char *shell_commands = SHELL; |
| 355 | const char *tty0_commands = init_commands; |
| 356 | const char *tty1_commands = shell_commands; |
| 357 | char *hello_msg_format = |
| 358 | "init started: BusyBox v%s (%s) multi-call binary\r\n"; |
| 359 | const char *no_memory = |
| 360 | "Sorry, your computer does not have enough memory.\r\n"; |
Eric Andersen | 0460ff2 | 1999-10-25 23:32:44 +0000 | [diff] [blame] | 361 | |
| 362 | |
| 363 | #ifndef DEBUG_INIT |
Eric Andersen | 8a8fbb8 | 1999-10-26 00:18:56 +0000 | [diff] [blame^] | 364 | /* Set up sig handlers */ |
| 365 | signal(SIGUSR1, halt_signal); |
| 366 | signal(SIGSEGV, halt_signal); |
| 367 | signal(SIGPWR, halt_signal); |
| 368 | signal(SIGALRM, halt_signal); |
| 369 | signal(SIGHUP, halt_signal); |
| 370 | signal(SIGUSR2, reboot_signal); |
| 371 | signal(SIGINT, reboot_signal); |
| 372 | signal(SIGTERM, reboot_signal); |
Eric Andersen | 0460ff2 | 1999-10-25 23:32:44 +0000 | [diff] [blame] | 373 | #endif |
Eric Andersen | 8a8fbb8 | 1999-10-26 00:18:56 +0000 | [diff] [blame^] | 374 | /* Figure out where the default console should be */ |
| 375 | console_init(); |
Eric Andersen | 0460ff2 | 1999-10-25 23:32:44 +0000 | [diff] [blame] | 376 | |
Eric Andersen | 8a8fbb8 | 1999-10-26 00:18:56 +0000 | [diff] [blame^] | 377 | /* Turn off rebooting via CTL-ALT-DEL -- we get a |
| 378 | * SIGINT on CAD so we can shut things down gracefully... */ |
Eric Andersen | 0460ff2 | 1999-10-25 23:32:44 +0000 | [diff] [blame] | 379 | #ifndef DEBUG_INIT |
Eric Andersen | 8a8fbb8 | 1999-10-26 00:18:56 +0000 | [diff] [blame^] | 380 | reboot(RB_DISABLE_CAD); |
Eric Andersen | 0460ff2 | 1999-10-25 23:32:44 +0000 | [diff] [blame] | 381 | #endif |
| 382 | |
Eric Andersen | 8a8fbb8 | 1999-10-26 00:18:56 +0000 | [diff] [blame^] | 383 | /* Close whatever files are open, and reset the console. */ |
| 384 | close(0); |
| 385 | close(1); |
| 386 | close(2); |
| 387 | set_term(); |
| 388 | setsid(); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 389 | |
Eric Andersen | 8a8fbb8 | 1999-10-26 00:18:56 +0000 | [diff] [blame^] | 390 | /* Make sure PATH is set to something sane */ |
| 391 | if (getenv("PATH") == NULL) |
| 392 | putenv(PATH_DEFAULT); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 393 | |
Eric Andersen | 8a8fbb8 | 1999-10-26 00:18:56 +0000 | [diff] [blame^] | 394 | /* Hello world */ |
| 395 | message(log, hello_msg_format, BB_VER, BB_BT); |
| 396 | message(console, hello_msg_format, BB_VER, BB_BT); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 397 | |
Eric Andersen | 8a8fbb8 | 1999-10-26 00:18:56 +0000 | [diff] [blame^] | 398 | /* Mount /proc */ |
| 399 | if (mount("/proc", "/proc", "proc", 0, 0)) { |
| 400 | message(log, "Mounting /proc: failed!\n"); |
| 401 | message(console, "Mounting /proc: failed!\r\n"); |
| 402 | } else { |
| 403 | message(console, "Mounting /proc: done.\r\n"); |
| 404 | } |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 405 | |
Eric Andersen | 8a8fbb8 | 1999-10-26 00:18:56 +0000 | [diff] [blame^] | 406 | /* Make sure there is enough memory to do something useful */ |
| 407 | set_free_pages(); |
| 408 | if (mem_total() < 2000) { |
| 409 | int retval; |
| 410 | retval = stat("/etc/fstab", &statbuf); |
| 411 | if (retval) { |
| 412 | message(console, "%s", no_memory); |
| 413 | while (1) { |
| 414 | sleep(1); |
| 415 | } |
| 416 | } else { |
| 417 | /* Try to turn on swap */ |
| 418 | waitfor(run("/bin/swapon -a", console, 0)); |
| 419 | if (mem_total() < 2000) { |
Eric Andersen | 0460ff2 | 1999-10-25 23:32:44 +0000 | [diff] [blame] | 420 | message(console, "%s", no_memory); |
Eric Andersen | 8a8fbb8 | 1999-10-26 00:18:56 +0000 | [diff] [blame^] | 421 | while (1) { |
| 422 | sleep(1); |
Eric Andersen | abc7d59 | 1999-10-19 00:27:50 +0000 | [diff] [blame] | 423 | } |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 424 | } |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 425 | } |
Eric Andersen | 8a8fbb8 | 1999-10-26 00:18:56 +0000 | [diff] [blame^] | 426 | } |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 427 | |
Eric Andersen | 8a8fbb8 | 1999-10-26 00:18:56 +0000 | [diff] [blame^] | 428 | /* Check if we are supposed to be in single user mode */ |
| 429 | if ( argc > 1 && (!strcmp(argv[1], "single") || |
| 430 | !strcmp(argv[1], "-s") || !strcmp(argv[1], "1"))) { |
| 431 | run_rc = FALSE; |
| 432 | tty0_commands = shell_commands; |
| 433 | tty1_commands = 0; |
| 434 | } |
| 435 | |
| 436 | /* Make sure an init script exists before trying to run it */ |
| 437 | if (run_rc == TRUE && stat(INITSCRIPT, &statbuf)) { |
| 438 | tty0_commands = shell_commands; |
| 439 | tty1_commands = shell_commands; |
| 440 | } |
| 441 | |
| 442 | /* Ok, now launch the rc script and/or prepare to |
| 443 | * start up some VTs if somebody hits enter... |
| 444 | */ |
| 445 | for (;;) { |
| 446 | int wpid; |
| 447 | int status; |
| 448 | |
| 449 | if (pid1 == 0 && tty0_commands) { |
| 450 | pid1 = run(tty0_commands, console, 1); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 451 | } |
Eric Andersen | 8a8fbb8 | 1999-10-26 00:18:56 +0000 | [diff] [blame^] | 452 | if (pid2 == 0 && tty1_commands) { |
| 453 | pid2 = run(tty1_commands, second_terminal, 1); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 454 | } |
Eric Andersen | 8a8fbb8 | 1999-10-26 00:18:56 +0000 | [diff] [blame^] | 455 | wpid = wait(&status); |
| 456 | if (wpid > 0 && wpid != pid1) { |
| 457 | message(log, "pid %d exited, status=%x.\n", wpid, status); |
| 458 | } |
| 459 | /* Don't respawn an init script if it exits */ |
| 460 | if (run_rc == FALSE && wpid == pid1) { |
| 461 | pid1 = 0; |
| 462 | } |
| 463 | if (wpid == pid2) { |
| 464 | pid2 = 0; |
| 465 | } |
| 466 | sleep(1); |
| 467 | } |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 468 | } |