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