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 */ |
| 48 | #define VT_PRIMARY "/dev/tty0" /* Virtual console master */ |
| 49 | #define VT_SECONDARY "/dev/tty1" /* Virtual console master */ |
| 50 | #define VT_LOG "/dev/tty2" /* Virtual console master */ |
| 51 | #define SHELL "/bin/sh" /* Default shell */ |
Eric Andersen | 2f6c04f | 1999-11-01 23:59:44 +0000 | [diff] [blame^] | 52 | //#define INITSCRIPT "/etc/init.d/rcS" /* Initscript. */ |
| 53 | #define INITSCRIPT "/tmp/foo.sh" |
Eric Andersen | 0460ff2 | 1999-10-25 23:32:44 +0000 | [diff] [blame] | 54 | #define PATH_DEFAULT "PATH=/usr/local/sbin:/sbin:/bin:/usr/sbin:/usr/bin" |
| 55 | |
Eric Andersen | 8a8fbb8 | 1999-10-26 00:18:56 +0000 | [diff] [blame] | 56 | static char *console = CONSOLE; |
Eric Andersen | c7c41d3 | 1999-10-28 00:24:35 +0000 | [diff] [blame] | 57 | //static char *first_terminal = "/dev/tty1"; |
Eric Andersen | 8a8fbb8 | 1999-10-26 00:18:56 +0000 | [diff] [blame] | 58 | static char *second_terminal = "/dev/tty2"; |
| 59 | static char *log = "/dev/tty3"; |
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 | 7f1acfd | 1999-10-29 23:09:13 +0000 | [diff] [blame] | 117 | /* use lineo dicipline 0 */ |
| 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 | 8a8fbb8 | 1999-10-26 00:18:56 +0000 | [diff] [blame] | 132 | static int mem_total() |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 133 | { |
Eric Andersen | abc7d59 | 1999-10-19 00:27:50 +0000 | [diff] [blame] | 134 | char s[80]; |
| 135 | char *p; |
| 136 | FILE *f; |
Eric Andersen | 8a8fbb8 | 1999-10-26 00:18:56 +0000 | [diff] [blame] | 137 | const char pattern[] = "MemTotal:"; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 138 | |
Eric Andersen | 8a8fbb8 | 1999-10-26 00:18:56 +0000 | [diff] [blame] | 139 | f = fopen("/proc/meminfo", "r"); |
| 140 | while (NULL != fgets(s, 79, f)) { |
| 141 | p = strstr(s, pattern); |
Eric Andersen | abc7d59 | 1999-10-19 00:27:50 +0000 | [diff] [blame] | 142 | if (NULL != p) { |
| 143 | fclose(f); |
Eric Andersen | 8a8fbb8 | 1999-10-26 00:18:56 +0000 | [diff] [blame] | 144 | return (atoi(p + strlen(pattern))); |
Eric Andersen | abc7d59 | 1999-10-19 00:27:50 +0000 | [diff] [blame] | 145 | } |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 146 | } |
Eric Andersen | abc7d59 | 1999-10-19 00:27:50 +0000 | [diff] [blame] | 147 | return -1; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 148 | } |
| 149 | |
Eric Andersen | 8a8fbb8 | 1999-10-26 00:18:56 +0000 | [diff] [blame] | 150 | static void set_free_pages() |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 151 | { |
Eric Andersen | abc7d59 | 1999-10-19 00:27:50 +0000 | [diff] [blame] | 152 | char s[80]; |
| 153 | FILE *f; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 154 | |
Eric Andersen | 8a8fbb8 | 1999-10-26 00:18:56 +0000 | [diff] [blame] | 155 | f = fopen("/proc/sys/vm/freepages", "r"); |
| 156 | fgets(s, 79, f); |
Eric Andersen | abc7d59 | 1999-10-19 00:27:50 +0000 | [diff] [blame] | 157 | if (atoi(s) < 32) { |
| 158 | fclose(f); |
Eric Andersen | 8a8fbb8 | 1999-10-26 00:18:56 +0000 | [diff] [blame] | 159 | f = fopen("/proc/sys/vm/freepages", "w"); |
| 160 | fprintf(f, "30\t40\t50\n"); |
Eric Andersen | 7f1acfd | 1999-10-29 23:09:13 +0000 | [diff] [blame] | 161 | message(log, "\nIncreased /proc/sys/vm/freepages values to 30/40/50\n"); |
Eric Andersen | abc7d59 | 1999-10-19 00:27:50 +0000 | [diff] [blame] | 162 | } |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 163 | fclose(f); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 164 | } |
| 165 | |
Eric Andersen | 0460ff2 | 1999-10-25 23:32:44 +0000 | [diff] [blame] | 166 | |
Eric Andersen | 8a8fbb8 | 1999-10-26 00:18:56 +0000 | [diff] [blame] | 167 | static void console_init() |
Eric Andersen | 0460ff2 | 1999-10-25 23:32:44 +0000 | [diff] [blame] | 168 | { |
| 169 | int fd; |
Eric Andersen | a745606 | 1999-10-27 02:31:32 +0000 | [diff] [blame] | 170 | struct stat statbuf; |
Eric Andersen | 0460ff2 | 1999-10-25 23:32:44 +0000 | [diff] [blame] | 171 | int tried_devcons = 0; |
| 172 | int tried_vtmaster = 0; |
| 173 | char *s; |
| 174 | |
| 175 | if ((s = getenv("CONSOLE")) != NULL) |
| 176 | console = s; |
| 177 | else { |
| 178 | console = CONSOLE; |
| 179 | tried_devcons++; |
| 180 | } |
Eric Andersen | a745606 | 1999-10-27 02:31:32 +0000 | [diff] [blame] | 181 | |
| 182 | if ( stat(CONSOLE, &statbuf) && S_ISLNK(statbuf.st_mode)) { |
Eric Andersen | c7c41d3 | 1999-10-28 00:24:35 +0000 | [diff] [blame] | 183 | fprintf(stderr, "Yikes! /dev/console does not exist or is a symlink.\n"); |
Eric Andersen | 7f1acfd | 1999-10-29 23:09:13 +0000 | [diff] [blame] | 184 | message(log, "Yikes! /dev/console does not exist or is a symlink.\n"); |
Eric Andersen | a745606 | 1999-10-27 02:31:32 +0000 | [diff] [blame] | 185 | } |
Eric Andersen | 8a8fbb8 | 1999-10-26 00:18:56 +0000 | [diff] [blame] | 186 | while ((fd = open(console, O_RDONLY | O_NONBLOCK)) < 0) { |
Eric Andersen | 0460ff2 | 1999-10-25 23:32:44 +0000 | [diff] [blame] | 187 | if (!tried_devcons) { |
| 188 | tried_devcons++; |
| 189 | console = CONSOLE; |
| 190 | continue; |
| 191 | } |
| 192 | if (!tried_vtmaster) { |
| 193 | tried_vtmaster++; |
| 194 | console = VT_PRIMARY; |
| 195 | continue; |
| 196 | } |
| 197 | break; |
| 198 | } |
| 199 | if (fd < 0) |
| 200 | console = "/dev/null"; |
| 201 | else |
| 202 | close(fd); |
Eric Andersen | 7f1acfd | 1999-10-29 23:09:13 +0000 | [diff] [blame] | 203 | message(log, "console=%s\n", console); |
Eric Andersen | 0460ff2 | 1999-10-25 23:32:44 +0000 | [diff] [blame] | 204 | } |
| 205 | |
Eric Andersen | 8a8fbb8 | 1999-10-26 00:18:56 +0000 | [diff] [blame] | 206 | static int waitfor(int pid) |
Eric Andersen | 0460ff2 | 1999-10-25 23:32:44 +0000 | [diff] [blame] | 207 | { |
| 208 | int status, wpid; |
| 209 | |
| 210 | message(log, "Waiting for process %d.\n", pid); |
Eric Andersen | 8a8fbb8 | 1999-10-26 00:18:56 +0000 | [diff] [blame] | 211 | while ((wpid = wait(&status)) != pid) { |
| 212 | if (wpid > 0) |
| 213 | message(log, "pid %d exited, status=%x.\n", wpid, status); |
Eric Andersen | 0460ff2 | 1999-10-25 23:32:44 +0000 | [diff] [blame] | 214 | } |
| 215 | return wpid; |
| 216 | } |
| 217 | |
Eric Andersen | a745606 | 1999-10-27 02:31:32 +0000 | [diff] [blame] | 218 | |
Eric Andersen | c7c41d3 | 1999-10-28 00:24:35 +0000 | [diff] [blame] | 219 | static pid_t run(const char * const* command, |
| 220 | char *terminal, int get_enter) |
Eric Andersen | 0460ff2 | 1999-10-25 23:32:44 +0000 | [diff] [blame] | 221 | { |
Eric Andersen | 2f6c04f | 1999-11-01 23:59:44 +0000 | [diff] [blame^] | 222 | int fd; |
Eric Andersen | c7c41d3 | 1999-10-28 00:24:35 +0000 | [diff] [blame] | 223 | pid_t pid; |
Eric Andersen | 2f6c04f | 1999-11-01 23:59:44 +0000 | [diff] [blame^] | 224 | const char * const* cmd = command; |
Eric Andersen | 8a8fbb8 | 1999-10-26 00:18:56 +0000 | [diff] [blame] | 225 | static const char press_enter[] = |
Eric Andersen | 0460ff2 | 1999-10-25 23:32:44 +0000 | [diff] [blame] | 226 | "\nPlease press Enter to activate this console. "; |
| 227 | |
Eric Andersen | 0460ff2 | 1999-10-25 23:32:44 +0000 | [diff] [blame] | 228 | if ((pid = fork()) == 0) { |
| 229 | /* Clean up */ |
| 230 | close(0); |
| 231 | close(1); |
| 232 | close(2); |
| 233 | setsid(); |
| 234 | |
Eric Andersen | 2f6c04f | 1999-11-01 23:59:44 +0000 | [diff] [blame^] | 235 | /* Reset signal handlers set for parent process */ |
| 236 | signal(SIGUSR1, SIG_DFL); |
| 237 | signal(SIGUSR2, SIG_DFL); |
| 238 | signal(SIGINT, SIG_DFL); |
| 239 | signal(SIGTERM, SIG_DFL); |
| 240 | |
| 241 | if ((fd = device_open(terminal, O_RDWR)) < 0) { |
| 242 | message(log, "Bummer, can't open %s\r\n", terminal); |
| 243 | exit(-1); |
| 244 | } |
| 245 | dup(fd); |
| 246 | dup(fd); |
Eric Andersen | c7c41d3 | 1999-10-28 00:24:35 +0000 | [diff] [blame] | 247 | tcsetpgrp(0, getpgrp()); |
| 248 | set_term(0); |
Eric Andersen | 0460ff2 | 1999-10-25 23:32:44 +0000 | [diff] [blame] | 249 | |
Eric Andersen | 2f6c04f | 1999-11-01 23:59:44 +0000 | [diff] [blame^] | 250 | if (get_enter==TRUE) { |
Eric Andersen | 0460ff2 | 1999-10-25 23:32:44 +0000 | [diff] [blame] | 251 | /* |
| 252 | * Save memory by not exec-ing anything large (like a shell) |
| 253 | * before the user wants it. This is critical if swap is not |
| 254 | * enabled and the system has low memory. Generally this will |
| 255 | * be run on the second virtual console, and the first will |
| 256 | * be allowed to start a shell or whatever an init script |
| 257 | * specifies. |
| 258 | */ |
Eric Andersen | 8a8fbb8 | 1999-10-26 00:18:56 +0000 | [diff] [blame] | 259 | char c; |
Eric Andersen | 7f1acfd | 1999-10-29 23:09:13 +0000 | [diff] [blame] | 260 | write(1, press_enter, sizeof(press_enter) - 1); |
Eric Andersen | 0460ff2 | 1999-10-25 23:32:44 +0000 | [diff] [blame] | 261 | read(0, &c, 1); |
| 262 | } |
| 263 | |
| 264 | /* Log the process name and args */ |
Eric Andersen | 2f6c04f | 1999-11-01 23:59:44 +0000 | [diff] [blame^] | 265 | message(log, "Executing pid(%d): '", getpid()); |
| 266 | while ( *cmd) message(log, "%s ", *cmd++); |
| 267 | message(log, "'\r\n"); |
| 268 | |
Eric Andersen | c7c41d3 | 1999-10-28 00:24:35 +0000 | [diff] [blame] | 269 | /* Now run it. The new program will take over this PID, |
Eric Andersen | a745606 | 1999-10-27 02:31:32 +0000 | [diff] [blame] | 270 | * so nothing further in init.c should be run. */ |
Eric Andersen | c7c41d3 | 1999-10-28 00:24:35 +0000 | [diff] [blame] | 271 | execvp(*command, (char**)command+1); |
Eric Andersen | 0460ff2 | 1999-10-25 23:32:44 +0000 | [diff] [blame] | 272 | |
Eric Andersen | 2f6c04f | 1999-11-01 23:59:44 +0000 | [diff] [blame^] | 273 | message(log, "Bummer, could not execute '%s'\n", command); |
Eric Andersen | 0460ff2 | 1999-10-25 23:32:44 +0000 | [diff] [blame] | 274 | exit(-1); |
| 275 | } |
| 276 | return pid; |
| 277 | } |
| 278 | |
Eric Andersen | 8a8fbb8 | 1999-10-26 00:18:56 +0000 | [diff] [blame] | 279 | static void shutdown_system(void) |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 280 | { |
Eric Andersen | 2f6c04f | 1999-11-01 23:59:44 +0000 | [diff] [blame^] | 281 | const char* const swap_off_cmd[] = { "swapoff", "swapoff", "-a", 0}; |
| 282 | const char* const umount_cmd[] = { "umount", "umount", "-a", "-n", 0}; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 283 | |
Eric Andersen | 8a8fbb8 | 1999-10-26 00:18:56 +0000 | [diff] [blame] | 284 | message(console, "The system is going down NOW !!\r\n"); |
Eric Andersen | 0460ff2 | 1999-10-25 23:32:44 +0000 | [diff] [blame] | 285 | sync(); |
Eric Andersen | 2f6c04f | 1999-11-01 23:59:44 +0000 | [diff] [blame^] | 286 | #ifndef DEBUG_INIT |
Eric Andersen | 0460ff2 | 1999-10-25 23:32:44 +0000 | [diff] [blame] | 287 | /* Allow Ctrl-Alt-Del to reboot system. */ |
| 288 | reboot(RB_ENABLE_CAD); |
Eric Andersen | 2f6c04f | 1999-11-01 23:59:44 +0000 | [diff] [blame^] | 289 | #endif |
Eric Andersen | 0460ff2 | 1999-10-25 23:32:44 +0000 | [diff] [blame] | 290 | /* Send signals to every process _except_ pid 1 */ |
| 291 | message(console, "Sending SIGHUP to all processes.\r\n"); |
Eric Andersen | 2f6c04f | 1999-11-01 23:59:44 +0000 | [diff] [blame^] | 292 | #ifndef DEBUG_INIT |
Eric Andersen | 0460ff2 | 1999-10-25 23:32:44 +0000 | [diff] [blame] | 293 | kill(-1, SIGHUP); |
Eric Andersen | 2f6c04f | 1999-11-01 23:59:44 +0000 | [diff] [blame^] | 294 | #endif |
Eric Andersen | 0460ff2 | 1999-10-25 23:32:44 +0000 | [diff] [blame] | 295 | sleep(2); |
| 296 | sync(); |
| 297 | message(console, "Sending SIGKILL to all processes.\r\n"); |
Eric Andersen | 2f6c04f | 1999-11-01 23:59:44 +0000 | [diff] [blame^] | 298 | #ifndef DEBUG_INIT |
Eric Andersen | 0460ff2 | 1999-10-25 23:32:44 +0000 | [diff] [blame] | 299 | kill(-1, SIGKILL); |
Eric Andersen | 2f6c04f | 1999-11-01 23:59:44 +0000 | [diff] [blame^] | 300 | #endif |
Eric Andersen | 0460ff2 | 1999-10-25 23:32:44 +0000 | [diff] [blame] | 301 | sleep(1); |
Eric Andersen | 2f6c04f | 1999-11-01 23:59:44 +0000 | [diff] [blame^] | 302 | waitfor(run( swap_off_cmd, log, FALSE)); |
| 303 | waitfor(run( umount_cmd, log, FALSE)); |
Eric Andersen | 0460ff2 | 1999-10-25 23:32:44 +0000 | [diff] [blame] | 304 | sync(); |
Eric Andersen | 8a8fbb8 | 1999-10-26 00:18:56 +0000 | [diff] [blame] | 305 | if (get_kernel_revision() <= 2 * 65536 + 2 * 256 + 11) { |
Eric Andersen | 0460ff2 | 1999-10-25 23:32:44 +0000 | [diff] [blame] | 306 | /* Removed bdflush call, kupdate in kernels >2.2.11 */ |
| 307 | bdflush(1, 0); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 308 | sync(); |
Eric Andersen | 0460ff2 | 1999-10-25 23:32:44 +0000 | [diff] [blame] | 309 | } |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 310 | } |
| 311 | |
Eric Andersen | 8a8fbb8 | 1999-10-26 00:18:56 +0000 | [diff] [blame] | 312 | static void halt_signal(int sig) |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 313 | { |
Eric Andersen | abc7d59 | 1999-10-19 00:27:50 +0000 | [diff] [blame] | 314 | shutdown_system(); |
Eric Andersen | 8a8fbb8 | 1999-10-26 00:18:56 +0000 | [diff] [blame] | 315 | message(console, |
| 316 | "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^] | 317 | #ifndef DEBUG_INIT |
Eric Andersen | 8a8fbb8 | 1999-10-26 00:18:56 +0000 | [diff] [blame] | 318 | reboot(RB_POWER_OFF); |
Eric Andersen | 2f6c04f | 1999-11-01 23:59:44 +0000 | [diff] [blame^] | 319 | #endif |
Eric Andersen | abc7d59 | 1999-10-19 00:27:50 +0000 | [diff] [blame] | 320 | exit(0); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 321 | } |
| 322 | |
Eric Andersen | 8a8fbb8 | 1999-10-26 00:18:56 +0000 | [diff] [blame] | 323 | static void reboot_signal(int sig) |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 324 | { |
Eric Andersen | abc7d59 | 1999-10-19 00:27:50 +0000 | [diff] [blame] | 325 | shutdown_system(); |
| 326 | message(console, "Please stand by while rebooting the system.\r\n"); |
Eric Andersen | 2f6c04f | 1999-11-01 23:59:44 +0000 | [diff] [blame^] | 327 | #ifndef DEBUG_INIT |
Eric Andersen | 8a8fbb8 | 1999-10-26 00:18:56 +0000 | [diff] [blame] | 328 | reboot(RB_AUTOBOOT); |
Eric Andersen | 2f6c04f | 1999-11-01 23:59:44 +0000 | [diff] [blame^] | 329 | #endif |
Eric Andersen | abc7d59 | 1999-10-19 00:27:50 +0000 | [diff] [blame] | 330 | exit(0); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 331 | } |
Eric Andersen | 8a8fbb8 | 1999-10-26 00:18:56 +0000 | [diff] [blame] | 332 | |
Eric Andersen | 8a8fbb8 | 1999-10-26 00:18:56 +0000 | [diff] [blame] | 333 | extern int init_main(int argc, char **argv) |
Eric Andersen | 0460ff2 | 1999-10-25 23:32:44 +0000 | [diff] [blame] | 334 | { |
Eric Andersen | 8a8fbb8 | 1999-10-26 00:18:56 +0000 | [diff] [blame] | 335 | int run_rc = TRUE; |
Eric Andersen | 2f6c04f | 1999-11-01 23:59:44 +0000 | [diff] [blame^] | 336 | int wait_for_enter = FALSE; |
Eric Andersen | c7c41d3 | 1999-10-28 00:24:35 +0000 | [diff] [blame] | 337 | pid_t pid1 = 0; |
| 338 | pid_t pid2 = 0; |
Eric Andersen | 8a8fbb8 | 1999-10-26 00:18:56 +0000 | [diff] [blame] | 339 | struct stat statbuf; |
Eric Andersen | 2f6c04f | 1999-11-01 23:59:44 +0000 | [diff] [blame^] | 340 | const char* const swap_on_cmd[] = { "/bin/swapon", "swapon", "-a", 0}; |
| 341 | const char* const init_commands[] = { INITSCRIPT, INITSCRIPT, 0}; |
| 342 | const char* const shell_commands[] = { SHELL, "-" SHELL, 0}; |
Eric Andersen | c7c41d3 | 1999-10-28 00:24:35 +0000 | [diff] [blame] | 343 | const char* const* tty0_commands = init_commands; |
| 344 | const char* const* tty1_commands = shell_commands; |
Eric Andersen | 2f6c04f | 1999-11-01 23:59:44 +0000 | [diff] [blame^] | 345 | #ifdef DEBUG_INIT |
Eric Andersen | 8a8fbb8 | 1999-10-26 00:18:56 +0000 | [diff] [blame] | 346 | char *hello_msg_format = |
Eric Andersen | 0457978 | 1999-10-29 23:12:50 +0000 | [diff] [blame] | 347 | "init(%d) started: BusyBox v%s (%s) multi-call binary\r\n"; |
Eric Andersen | 2f6c04f | 1999-11-01 23:59:44 +0000 | [diff] [blame^] | 348 | #else |
| 349 | char *hello_msg_format = |
| 350 | "init started: BusyBox v%s (%s) multi-call binary\r\n"; |
Eric Andersen | 7f1acfd | 1999-10-29 23:09:13 +0000 | [diff] [blame] | 351 | #endif |
Eric Andersen | 8a8fbb8 | 1999-10-26 00:18:56 +0000 | [diff] [blame] | 352 | const char *no_memory = |
| 353 | "Sorry, your computer does not have enough memory.\r\n"; |
Eric Andersen | 0460ff2 | 1999-10-25 23:32:44 +0000 | [diff] [blame] | 354 | |
Eric Andersen | 2f6c04f | 1999-11-01 23:59:44 +0000 | [diff] [blame^] | 355 | /* Set up sig handlers -- be sure to |
| 356 | * clear all of these in run() */ |
Eric Andersen | 8a8fbb8 | 1999-10-26 00:18:56 +0000 | [diff] [blame] | 357 | signal(SIGUSR1, halt_signal); |
Eric Andersen | 8a8fbb8 | 1999-10-26 00:18:56 +0000 | [diff] [blame] | 358 | signal(SIGUSR2, reboot_signal); |
| 359 | signal(SIGINT, reboot_signal); |
| 360 | signal(SIGTERM, reboot_signal); |
Eric Andersen | 0460ff2 | 1999-10-25 23:32:44 +0000 | [diff] [blame] | 361 | |
Eric Andersen | 8a8fbb8 | 1999-10-26 00:18:56 +0000 | [diff] [blame] | 362 | /* Turn off rebooting via CTL-ALT-DEL -- we get a |
| 363 | * SIGINT on CAD so we can shut things down gracefully... */ |
Eric Andersen | 2f6c04f | 1999-11-01 23:59:44 +0000 | [diff] [blame^] | 364 | #ifndef DEBUG_INIT |
Eric Andersen | 8a8fbb8 | 1999-10-26 00:18:56 +0000 | [diff] [blame] | 365 | reboot(RB_DISABLE_CAD); |
Eric Andersen | 7f1acfd | 1999-10-29 23:09:13 +0000 | [diff] [blame] | 366 | #endif |
Eric Andersen | c7c41d3 | 1999-10-28 00:24:35 +0000 | [diff] [blame] | 367 | /* Figure out where the default console should be */ |
| 368 | console_init(); |
Eric Andersen | 0460ff2 | 1999-10-25 23:32:44 +0000 | [diff] [blame] | 369 | |
Eric Andersen | 8a8fbb8 | 1999-10-26 00:18:56 +0000 | [diff] [blame] | 370 | /* Close whatever files are open, and reset the console. */ |
| 371 | close(0); |
| 372 | close(1); |
| 373 | close(2); |
Eric Andersen | 7f1acfd | 1999-10-29 23:09:13 +0000 | [diff] [blame] | 374 | set_term(0); |
Eric Andersen | 8a8fbb8 | 1999-10-26 00:18:56 +0000 | [diff] [blame] | 375 | setsid(); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 376 | |
Eric Andersen | 8a8fbb8 | 1999-10-26 00:18:56 +0000 | [diff] [blame] | 377 | /* Make sure PATH is set to something sane */ |
| 378 | if (getenv("PATH") == NULL) |
| 379 | putenv(PATH_DEFAULT); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 380 | |
Eric Andersen | 8a8fbb8 | 1999-10-26 00:18:56 +0000 | [diff] [blame] | 381 | /* Hello world */ |
Eric Andersen | 7f1acfd | 1999-10-29 23:09:13 +0000 | [diff] [blame] | 382 | #ifndef DEBUG_INIT |
Eric Andersen | c1525e8 | 1999-10-29 00:07:31 +0000 | [diff] [blame] | 383 | message(log, hello_msg_format, BB_VER, BB_BT); |
Eric Andersen | 7f1acfd | 1999-10-29 23:09:13 +0000 | [diff] [blame] | 384 | message(console, hello_msg_format, BB_VER, BB_BT); |
| 385 | #else |
| 386 | message(log, hello_msg_format, getpid(), BB_VER, BB_BT); |
| 387 | message(console, hello_msg_format, getpid(), BB_VER, BB_BT); |
| 388 | #endif |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 389 | |
Eric Andersen | c7c41d3 | 1999-10-28 00:24:35 +0000 | [diff] [blame] | 390 | |
Eric Andersen | 8a8fbb8 | 1999-10-26 00:18:56 +0000 | [diff] [blame] | 391 | /* Mount /proc */ |
Eric Andersen | 2f6c04f | 1999-11-01 23:59:44 +0000 | [diff] [blame^] | 392 | if (mount ("proc", "/proc", "proc", 0, 0) == 0) { |
Eric Andersen | a745606 | 1999-10-27 02:31:32 +0000 | [diff] [blame] | 393 | message(log, "Mounting /proc: done.\n"); |
Eric Andersen | 7f1acfd | 1999-10-29 23:09:13 +0000 | [diff] [blame] | 394 | message(console, "Mounting /proc: done.\n"); |
Eric Andersen | c7c41d3 | 1999-10-28 00:24:35 +0000 | [diff] [blame] | 395 | } else { |
Eric Andersen | c7c41d3 | 1999-10-28 00:24:35 +0000 | [diff] [blame] | 396 | message(log, "Mounting /proc: failed!\n"); |
Eric Andersen | 7f1acfd | 1999-10-29 23:09:13 +0000 | [diff] [blame] | 397 | message(console, "Mounting /proc: failed!\n"); |
Eric Andersen | 8a8fbb8 | 1999-10-26 00:18:56 +0000 | [diff] [blame] | 398 | } |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 399 | |
Eric Andersen | 8a8fbb8 | 1999-10-26 00:18:56 +0000 | [diff] [blame] | 400 | /* Make sure there is enough memory to do something useful */ |
| 401 | set_free_pages(); |
Eric Andersen | c1525e8 | 1999-10-29 00:07:31 +0000 | [diff] [blame] | 402 | if (mem_total() < 3500) { |
Eric Andersen | 8a8fbb8 | 1999-10-26 00:18:56 +0000 | [diff] [blame] | 403 | int retval; |
| 404 | retval = stat("/etc/fstab", &statbuf); |
| 405 | if (retval) { |
Eric Andersen | 7f1acfd | 1999-10-29 23:09:13 +0000 | [diff] [blame] | 406 | message(console, "%s", no_memory); |
Eric Andersen | 8a8fbb8 | 1999-10-26 00:18:56 +0000 | [diff] [blame] | 407 | while (1) { |
| 408 | sleep(1); |
| 409 | } |
| 410 | } else { |
| 411 | /* Try to turn on swap */ |
Eric Andersen | 2f6c04f | 1999-11-01 23:59:44 +0000 | [diff] [blame^] | 412 | waitfor(run(swap_on_cmd, log, FALSE)); |
Eric Andersen | 8a8fbb8 | 1999-10-26 00:18:56 +0000 | [diff] [blame] | 413 | if (mem_total() < 2000) { |
Eric Andersen | 7f1acfd | 1999-10-29 23:09:13 +0000 | [diff] [blame] | 414 | message(console, "%s", no_memory); |
Eric Andersen | 8a8fbb8 | 1999-10-26 00:18:56 +0000 | [diff] [blame] | 415 | while (1) { |
| 416 | sleep(1); |
Eric Andersen | abc7d59 | 1999-10-19 00:27:50 +0000 | [diff] [blame] | 417 | } |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 418 | } |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 419 | } |
Eric Andersen | 8a8fbb8 | 1999-10-26 00:18:56 +0000 | [diff] [blame] | 420 | } |
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 | /* Check if we are supposed to be in single user mode */ |
| 423 | if ( argc > 1 && (!strcmp(argv[1], "single") || |
| 424 | !strcmp(argv[1], "-s") || !strcmp(argv[1], "1"))) { |
| 425 | run_rc = FALSE; |
Eric Andersen | 2f6c04f | 1999-11-01 23:59:44 +0000 | [diff] [blame^] | 426 | wait_for_enter = TRUE; |
Eric Andersen | 8a8fbb8 | 1999-10-26 00:18:56 +0000 | [diff] [blame] | 427 | tty0_commands = shell_commands; |
Eric Andersen | a745606 | 1999-10-27 02:31:32 +0000 | [diff] [blame] | 428 | tty1_commands = shell_commands; |
Eric Andersen | 8a8fbb8 | 1999-10-26 00:18:56 +0000 | [diff] [blame] | 429 | } |
| 430 | |
| 431 | /* Make sure an init script exists before trying to run it */ |
| 432 | if (run_rc == TRUE && stat(INITSCRIPT, &statbuf)) { |
Eric Andersen | 2f6c04f | 1999-11-01 23:59:44 +0000 | [diff] [blame^] | 433 | wait_for_enter = TRUE; |
Eric Andersen | 8a8fbb8 | 1999-10-26 00:18:56 +0000 | [diff] [blame] | 434 | tty0_commands = shell_commands; |
| 435 | tty1_commands = shell_commands; |
| 436 | } |
| 437 | |
| 438 | /* Ok, now launch the rc script and/or prepare to |
| 439 | * start up some VTs if somebody hits enter... |
| 440 | */ |
| 441 | for (;;) { |
Eric Andersen | c7c41d3 | 1999-10-28 00:24:35 +0000 | [diff] [blame] | 442 | pid_t wpid; |
Eric Andersen | 8a8fbb8 | 1999-10-26 00:18:56 +0000 | [diff] [blame] | 443 | int status; |
| 444 | |
| 445 | if (pid1 == 0 && tty0_commands) { |
Eric Andersen | 2f6c04f | 1999-11-01 23:59:44 +0000 | [diff] [blame^] | 446 | pid1 = run(tty0_commands, console, wait_for_enter); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 447 | } |
Eric Andersen | 8a8fbb8 | 1999-10-26 00:18:56 +0000 | [diff] [blame] | 448 | if (pid2 == 0 && tty1_commands) { |
Eric Andersen | 2f6c04f | 1999-11-01 23:59:44 +0000 | [diff] [blame^] | 449 | pid2 = run(tty1_commands, second_terminal, TRUE); |
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 | wpid = wait(&status); |
Eric Andersen | a745606 | 1999-10-27 02:31:32 +0000 | [diff] [blame] | 452 | if (wpid > 0 ) { |
Eric Andersen | 8a8fbb8 | 1999-10-26 00:18:56 +0000 | [diff] [blame] | 453 | message(log, "pid %d exited, status=%x.\n", wpid, status); |
| 454 | } |
| 455 | /* Don't respawn an init script if it exits */ |
| 456 | if (run_rc == FALSE && wpid == pid1) { |
| 457 | pid1 = 0; |
| 458 | } |
| 459 | if (wpid == pid2) { |
| 460 | pid2 = 0; |
| 461 | } |
| 462 | sleep(1); |
| 463 | } |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 464 | } |