diff options
| author | Eric Andersen <andersen@codepoet.org> | 1999-10-25 23:32:44 +0000 |
|---|---|---|
| committer | Eric Andersen <andersen@codepoet.org> | 1999-10-25 23:32:44 +0000 |
| commit | 0460ff2e5d443b485c2f6447c8c2ece1b3bec9c3 (patch) | |
| tree | d761a790ee864a0a566d29c9d56173763fa04888 /init | |
| parent | d143e8f2bd03470e891a5e3ccca5734edd917e73 (diff) | |
| download | busybox-w32-0460ff2e5d443b485c2f6447c8c2ece1b3bec9c3.tar.gz busybox-w32-0460ff2e5d443b485c2f6447c8c2ece1b3bec9c3.tar.bz2 busybox-w32-0460ff2e5d443b485c2f6447c8c2ece1b3bec9c3.zip | |
Stuf
Diffstat (limited to 'init')
| -rw-r--r-- | init/init.c | 628 |
1 files changed, 321 insertions, 307 deletions
diff --git a/init/init.c b/init/init.c index c32124dd0..0676bf76d 100644 --- a/init/init.c +++ b/init/init.c | |||
| @@ -41,144 +41,88 @@ | |||
| 41 | #include <sys/vt.h> /* for vt_stat */ | 41 | #include <sys/vt.h> /* for vt_stat */ |
| 42 | #include <sys/ioctl.h> | 42 | #include <sys/ioctl.h> |
| 43 | 43 | ||
| 44 | static const char init_usage[] = "Used internally by the system."; | 44 | #define DEBUG_INIT |
| 45 | static char console[16] = ""; | ||
| 46 | static const char* default_console = "/dev/tty2"; | ||
| 47 | static char* first_terminal = NULL; | ||
| 48 | static const char* second_terminal = "/dev/tty2"; | ||
| 49 | static const char* log = "/dev/tty3"; | ||
| 50 | static char* term_ptr = NULL; | ||
| 51 | 45 | ||
| 52 | static void | 46 | #define CONSOLE "/dev/console" /* Logical system console */ |
| 53 | message(const char* terminal, const char * pattern, ...) | 47 | #define VT_PRIMARY "/dev/tty0" /* Virtual console master */ |
| 54 | { | 48 | #define VT_SECONDARY "/dev/tty1" /* Virtual console master */ |
| 55 | int fd; | 49 | #define VT_LOG "/dev/tty2" /* Virtual console master */ |
| 56 | FILE * con = 0; | 50 | #define SHELL "/bin/sh" /* Default shell */ |
| 57 | va_list arguments; | 51 | #define INITSCRIPT "/etc/init.d/rcS" /* Initscript. */ |
| 58 | 52 | #define PATH_DEFAULT "PATH=/usr/local/sbin:/sbin:/bin:/usr/sbin:/usr/bin" | |
| 59 | /* | ||
| 60 | * Open the console device each time a message is printed. If the user | ||
| 61 | * has switched consoles, the open will get the new console. If we kept | ||
| 62 | * the console open, we'd always print to the same one. | ||
| 63 | */ | ||
| 64 | if ( !terminal | ||
| 65 | || ((fd = open(terminal, O_WRONLY|O_NOCTTY)) < 0) | ||
| 66 | || ((con = fdopen(fd, "w")) == NULL) ) | ||
| 67 | return; | ||
| 68 | 53 | ||
| 69 | va_start(arguments, pattern); | 54 | static int maxproclen=0; |
| 70 | vfprintf(con, pattern, arguments); | 55 | static char* argv0; |
| 71 | va_end(arguments); | 56 | |
| 72 | fclose(con); | 57 | static char* console = CONSOLE; |
| 58 | static char* second_terminal = "/dev/tty2"; | ||
| 59 | static char* log = "/dev/tty3"; | ||
| 60 | |||
| 61 | |||
| 62 | |||
| 63 | /* try to open up the specified device */ | ||
| 64 | int device_open(char* device, int mode) | ||
| 65 | { | ||
| 66 | int m, f, fd = -1; | ||
| 67 | |||
| 68 | mode = m | O_NONBLOCK; | ||
| 69 | |||
| 70 | /* Retry up to 5 times */ | ||
| 71 | for(f = 0; f < 5; f++) | ||
| 72 | if ((fd = open(device, m)) >= 0) break; | ||
| 73 | if (fd < 0) return fd; | ||
| 74 | /* Set original flags. */ | ||
| 75 | if (m != mode) | ||
| 76 | fcntl(fd, F_SETFL, mode); | ||
| 77 | return fd; | ||
| 73 | } | 78 | } |
| 74 | 79 | ||
| 75 | static int | 80 | /* print a message to the specified device */ |
| 76 | waitfor(int pid) | 81 | void message(char* device, char *fmt, ...) |
| 77 | { | 82 | { |
| 78 | int status; | 83 | int fd; |
| 79 | int wpid; | 84 | va_list arguments; |
| 80 | 85 | if ((fd = device_open(device, O_WRONLY|O_NOCTTY|O_NDELAY)) >= 0) { | |
| 81 | message(log, "Waiting for process %d.\n", pid); | 86 | va_start(arguments, fmt); |
| 82 | while ( (wpid = wait(&status)) != pid ) { | 87 | vdprintf(fd, fmt, arguments); |
| 83 | if ( wpid > 0 ) { | 88 | va_end(arguments); |
| 84 | message( | 89 | } |
| 85 | log | 90 | close( fd); |
| 86 | ,"pid %d exited, status=%x.\n" | ||
| 87 | ,wpid | ||
| 88 | ,status); | ||
| 89 | } | ||
| 90 | } | ||
| 91 | return wpid; | ||
| 92 | } | 91 | } |
| 93 | 92 | ||
| 94 | static int | 93 | /* Set terminal settings to reasonable defaults */ |
| 95 | run(const char* program, const char* const* arguments, | 94 | void set_term() |
| 96 | const char* terminal, int get_enter) | ||
| 97 | { | 95 | { |
| 98 | static const char control_characters[] = { | 96 | int fd; |
| 99 | '\003', | 97 | struct termios tty; |
| 100 | '\034', | 98 | |
| 101 | '\177', | 99 | if ((fd = device_open(console, O_RDWR|O_NOCTTY)) < 0) { |
| 102 | '\025', | 100 | message(log, "can't open %s", console); |
| 103 | '\004', | 101 | return; |
| 104 | '\0', | 102 | } |
| 105 | '\1', | 103 | ioctl(fd, TCGETS, &tty); |
| 106 | '\0', | 104 | tty.c_cflag &= CBAUD|CBAUDEX|CSIZE|CSTOPB|PARENB|PARODD; |
| 107 | '\021', | 105 | tty.c_cflag |= HUPCL|CLOCAL; |
| 108 | '\023', | 106 | |
| 109 | '\032', | 107 | tty.c_cc[VINTR] = 3; |
| 110 | '\0', | 108 | tty.c_cc[VQUIT] = 28; |
| 111 | '\022', | 109 | tty.c_cc[VERASE] = 127; |
| 112 | '\017', | 110 | tty.c_cc[VKILL] = 24; |
| 113 | '\027', | 111 | tty.c_cc[VEOF] = 4; |
| 114 | '\026', | 112 | tty.c_cc[VTIME] = 0; |
| 115 | '\0' | 113 | tty.c_cc[VMIN] = 1; |
| 116 | }; | 114 | tty.c_cc[VSTART] = 17; |
| 117 | 115 | tty.c_cc[VSTOP] = 19; | |
| 118 | static char * environment[] = { | 116 | tty.c_cc[VSUSP] = 26; |
| 119 | "HOME=/", | 117 | |
| 120 | "PATH=/bin:/sbin:/usr/bin:/usr/sbin", | 118 | /* Set pre and post processing */ |
| 121 | "SHELL=/bin/sh", | 119 | tty.c_iflag = IGNPAR|ICRNL|IXON|IXANY; |
| 122 | 0, | 120 | tty.c_oflag = OPOST|ONLCR; |
| 123 | "USER=root", | 121 | tty.c_lflag = ISIG|ICANON|ECHO|ECHOCTL|ECHOPRT|ECHOKE; |
| 124 | 0 | 122 | |
| 125 | }; | 123 | /* Now set the terminal line. */ |
| 126 | 124 | ioctl(fd, TCSETS, &tty); | |
| 127 | static const char press_enter[] = | 125 | close( fd); |
| 128 | "\nPlease press Enter to activate this console. "; | ||
| 129 | |||
| 130 | int pid; | ||
| 131 | |||
| 132 | environment[3]=term_ptr; | ||
| 133 | |||
| 134 | pid = fork(); | ||
| 135 | if ( pid == 0 ) { | ||
| 136 | struct termios t; | ||
| 137 | const char * const * arg; | ||
| 138 | |||
| 139 | close(0); | ||
| 140 | close(1); | ||
| 141 | close(2); | ||
| 142 | setsid(); | ||
| 143 | |||
| 144 | open(terminal, O_RDWR); | ||
| 145 | dup(0); | ||
| 146 | dup(0); | ||
| 147 | tcsetpgrp(0, getpgrp()); | ||
| 148 | |||
| 149 | tcgetattr(0, &t); | ||
| 150 | memcpy(t.c_cc, control_characters, sizeof(control_characters)); | ||
| 151 | t.c_line = 0; | ||
| 152 | t.c_iflag = ICRNL|IXON|IXOFF; | ||
| 153 | t.c_oflag = OPOST|ONLCR; | ||
| 154 | t.c_lflag = ISIG|ICANON|ECHO|ECHOE|ECHOK|ECHOCTL|ECHOKE|IEXTEN; | ||
| 155 | tcsetattr(0, TCSANOW, &t); | ||
| 156 | |||
| 157 | if ( get_enter ) { | ||
| 158 | /* | ||
| 159 | * Save memory by not exec-ing anything large (like a shell) | ||
| 160 | * before the user wants it. This is critical if swap is not | ||
| 161 | * enabled and the system has low memory. Generally this will | ||
| 162 | * be run on the second virtual console, and the first will | ||
| 163 | * be allowed to start a shell or whatever an init script | ||
| 164 | * specifies. | ||
| 165 | */ | ||
| 166 | char c; | ||
| 167 | write(1, press_enter, sizeof(press_enter) - 1); | ||
| 168 | read(0, &c, 1); | ||
| 169 | } | ||
| 170 | |||
| 171 | message(log, "Executing "); | ||
| 172 | arg = arguments; | ||
| 173 | while ( *arg != 0 ) | ||
| 174 | message(log, "%s ", *arg++); | ||
| 175 | message(log, "\n"); | ||
| 176 | |||
| 177 | execve(program, (char * *)arguments, (char * *)environment); | ||
| 178 | message(log, "%s: could not execute: %s.\r\n", program, strerror(errno)); | ||
| 179 | exit(-1); | ||
| 180 | } | ||
| 181 | return pid; | ||
| 182 | } | 126 | } |
| 183 | 127 | ||
| 184 | static int | 128 | static int |
| @@ -217,33 +161,168 @@ set_free_pages() | |||
| 217 | fclose(f); | 161 | fclose(f); |
| 218 | } | 162 | } |
| 219 | 163 | ||
| 164 | |||
| 220 | static void | 165 | static void |
| 221 | shutdown_system(void) | 166 | console_init() |
| 222 | { | 167 | { |
| 223 | static const char * const umount_args[] = {"/bin/umount", "-a", "-n", 0}; | 168 | int fd; |
| 224 | static const char * const swapoff_args[] = {"/bin/swapoff", "-a", 0}; | 169 | int tried_devcons = 0; |
| 170 | int tried_vtmaster = 0; | ||
| 171 | char *s; | ||
| 172 | |||
| 173 | if ((s = getenv("CONSOLE")) != NULL) | ||
| 174 | console = s; | ||
| 175 | else { | ||
| 176 | console = CONSOLE; | ||
| 177 | tried_devcons++; | ||
| 178 | } | ||
| 179 | while ((fd = open(console, O_RDONLY|O_NONBLOCK)) < 0) { | ||
| 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); | ||
| 196 | } | ||
| 225 | 197 | ||
| 226 | message(console, "The system is going down NOW !!"); | 198 | static int |
| 227 | sync(); | 199 | waitfor(int pid) |
| 228 | /* Allow Ctrl-Alt-Del to reboot system. */ | 200 | { |
| 229 | reboot(RB_ENABLE_CAD); | 201 | int status, wpid; |
| 230 | 202 | ||
| 231 | /* Send signals to every process _except_ pid 1 */ | 203 | message(log, "Waiting for process %d.\n", pid); |
| 232 | message(console, "Sending SIGHUP to all processes.\r\n"); | 204 | while ( (wpid = wait(&status)) != pid ) { |
| 233 | kill(-1, SIGHUP); | 205 | if ( wpid > 0 ) |
| 234 | sleep(2); | 206 | message(log,"pid %d exited, status=%x.\n", wpid, status); |
| 235 | sync(); | 207 | } |
| 236 | message(console, "Sending SIGKILL to all processes.\r\n"); | 208 | return wpid; |
| 237 | kill(-1, SIGKILL); | 209 | } |
| 238 | sleep(1); | 210 | |
| 239 | waitfor(run("/bin/swapoff", swapoff_args, console, 0)); | 211 | static int |
| 240 | waitfor(run("/bin/umount", umount_args, console, 0)); | 212 | run(const char* command, char* terminal, int get_enter) |
| 241 | sync(); | 213 | { |
| 242 | if (get_kernel_revision() <= 2*65536+2*256+11) { | 214 | int f, pid; |
| 243 | /* Removed bdflush call, kupdate in kernels >2.2.11 */ | 215 | char *args[16]; |
| 244 | bdflush(1, 0); | 216 | char buf[256]; |
| 245 | sync(); | 217 | char* ptr; |
| 218 | static const char press_enter[] = | ||
| 219 | "\nPlease press Enter to activate this console. "; | ||
| 220 | |||
| 221 | |||
| 222 | /* Make a proper command from the command string */ | ||
| 223 | strcpy(buf, command); | ||
| 224 | ptr = buf; | ||
| 225 | for(f = 1; f < 15; f++) { | ||
| 226 | /* Skip white space */ | ||
| 227 | while(*ptr == ' ' || *ptr == '\t') ptr++; | ||
| 228 | args[f] = ptr; | ||
| 229 | /* May be trailing space.. */ | ||
| 230 | if (*ptr == 0) break; | ||
| 231 | /* Skip this `word' */ | ||
| 232 | while(*ptr && *ptr != ' ' && *ptr != '\t' && *ptr != '#') | ||
| 233 | ptr++; | ||
| 234 | /* If end-of-line, break */ | ||
| 235 | if (*ptr == '#' || *ptr == 0) { | ||
| 236 | f++; | ||
| 237 | *ptr = 0; | ||
| 238 | break; | ||
| 239 | } | ||
| 240 | /* End word with \0 and continue */ | ||
| 241 | args[f] = NULL; | ||
| 242 | } | ||
| 243 | args[0] = args[1]; | ||
| 244 | |||
| 245 | |||
| 246 | if ((pid = fork()) == 0) { | ||
| 247 | /* Clean up */ | ||
| 248 | close(0); | ||
| 249 | close(1); | ||
| 250 | close(2); | ||
| 251 | setsid(); | ||
| 252 | |||
| 253 | if ((f = device_open(terminal, O_RDWR|O_NOCTTY)) < 0) { | ||
| 254 | message( log, "open(%s) failed: %s", terminal, strerror(errno)); | ||
| 255 | return -1; | ||
| 256 | } | ||
| 257 | dup(f); | ||
| 258 | dup(f); | ||
| 259 | tcsetpgrp(0, getpgrp()); | ||
| 260 | set_term(); | ||
| 261 | |||
| 262 | if ( get_enter ) { | ||
| 263 | /* | ||
| 264 | * Save memory by not exec-ing anything large (like a shell) | ||
| 265 | * before the user wants it. This is critical if swap is not | ||
| 266 | * enabled and the system has low memory. Generally this will | ||
| 267 | * be run on the second virtual console, and the first will | ||
| 268 | * be allowed to start a shell or whatever an init script | ||
| 269 | * specifies. | ||
| 270 | */ | ||
| 271 | char c; | ||
| 272 | write(1, press_enter, sizeof(press_enter) - 1); | ||
| 273 | read(0, &c, 1); | ||
| 246 | } | 274 | } |
| 275 | |||
| 276 | /* Log the process name and args */ | ||
| 277 | message(log, "Executing '%s'\n", command); | ||
| 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 | |||
| 283 | /* If shell scripts are not executed, force the issue */ | ||
| 284 | if (errno == ENOEXEC) { | ||
| 285 | char buf[256]; | ||
| 286 | args[1] = SHELL; | ||
| 287 | args[2] = "-c"; | ||
| 288 | strcpy(buf, "exec "); | ||
| 289 | strcat(buf, command); | ||
| 290 | args[3] = buf; | ||
| 291 | args[4] = NULL; | ||
| 292 | execvp(args[1], args + 1); | ||
| 293 | } | ||
| 294 | message(log, "Could not execute '%s'\n", command, strerror(errno)); | ||
| 295 | exit(-1); | ||
| 296 | } | ||
| 297 | return pid; | ||
| 298 | } | ||
| 299 | |||
| 300 | #ifndef DEBUG_INIT | ||
| 301 | static void | ||
| 302 | shutdown_system(void) | ||
| 303 | { | ||
| 304 | |||
| 305 | message(console, "The system is going down NOW !!"); | ||
| 306 | sync(); | ||
| 307 | /* Allow Ctrl-Alt-Del to reboot system. */ | ||
| 308 | reboot(RB_ENABLE_CAD); | ||
| 309 | |||
| 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(); | ||
| 321 | if (get_kernel_revision() <= 2*65536+2*256+11) { | ||
| 322 | /* Removed bdflush call, kupdate in kernels >2.2.11 */ | ||
| 323 | bdflush(1, 0); | ||
| 324 | sync(); | ||
| 325 | } | ||
| 247 | } | 326 | } |
| 248 | 327 | ||
| 249 | static void | 328 | static void |
| @@ -251,7 +330,7 @@ halt_signal(int sig) | |||
| 251 | { | 330 | { |
| 252 | shutdown_system(); | 331 | shutdown_system(); |
| 253 | message(console, "The system is halted. Press CTRL-ALT-DEL or turn off power\r\n"); | 332 | message(console, "The system is halted. Press CTRL-ALT-DEL or turn off power\r\n"); |
| 254 | reboot( RB_HALT_SYSTEM); | 333 | reboot( RB_POWER_OFF); |
| 255 | exit(0); | 334 | exit(0); |
| 256 | } | 335 | } |
| 257 | 336 | ||
| @@ -263,202 +342,136 @@ reboot_signal(int sig) | |||
| 263 | reboot( RB_AUTOBOOT); | 342 | reboot( RB_AUTOBOOT); |
| 264 | exit(0); | 343 | exit(0); |
| 265 | } | 344 | } |
| 345 | #endif | ||
| 266 | 346 | ||
| 267 | static void | 347 | int setproctitle(char *fmt, ...) |
| 268 | configure_terminals( int serial_cons, int single_user_mode ) | ||
| 269 | { | 348 | { |
| 270 | char *tty; | 349 | va_list ap; |
| 271 | struct serial_struct sr; | 350 | int len; |
| 272 | struct vt_stat vt; | 351 | char buf[256]; |
| 273 | 352 | ||
| 274 | 353 | buf[0] = 0; | |
| 275 | switch (serial_cons) { | 354 | va_start(ap, fmt); |
| 276 | case -1: | 355 | len = vsprintf(buf, fmt, ap); |
| 277 | /* 2.2 kernels: | 356 | va_end(ap); |
| 278 | * identify the real console backend and try to make use of it */ | 357 | memset(argv0, 0, maxproclen + 1); |
| 279 | if (ioctl(0,TIOCGSERIAL,&sr) == 0) { | 358 | strncpy(argv0, buf, maxproclen); |
| 280 | sprintf( console, "/dev/ttyS%d", sr.line ); | 359 | return len; |
| 281 | serial_cons = sr.line+1; | ||
| 282 | } | ||
| 283 | else if (ioctl(0, VT_GETSTATE, &vt) == 0) { | ||
| 284 | sprintf( console, "/dev/tty%d", vt.v_active ); | ||
| 285 | serial_cons = 0; | ||
| 286 | } | ||
| 287 | else { | ||
| 288 | /* unknown backend: fallback to /dev/console */ | ||
| 289 | strcpy( console, "/dev/console" ); | ||
| 290 | serial_cons = 0; | ||
| 291 | } | ||
| 292 | break; | ||
| 293 | |||
| 294 | case 1: | ||
| 295 | strcpy( console, "/dev/cua0" ); | ||
| 296 | break; | ||
| 297 | case 2: | ||
| 298 | strcpy( console, "/dev/cua1" ); | ||
| 299 | break; | ||
| 300 | default: | ||
| 301 | tty = ttyname(0); | ||
| 302 | if (tty) { | ||
| 303 | strcpy( console, tty ); | ||
| 304 | if (!strncmp( tty, "/dev/cua", 8 )) | ||
| 305 | serial_cons=1; | ||
| 306 | } | ||
| 307 | else | ||
| 308 | /* falls back to /dev/tty1 if an error occurs */ | ||
| 309 | strcpy( console, default_console ); | ||
| 310 | } | ||
| 311 | if (!first_terminal) | ||
| 312 | first_terminal = console; | ||
| 313 | #if defined (__sparc__) | ||
| 314 | if (serial_cons > 0 && !strncmp(term_ptr,"TERM=linux",10)) | ||
| 315 | term_ptr = "TERM=vt100"; | ||
| 316 | #endif | ||
| 317 | if (serial_cons) { | ||
| 318 | /* disable other but the first terminal: | ||
| 319 | * VT is not initialized anymore on 2.2 kernel when booting from | ||
| 320 | * serial console, therefore modprobe is flooding the display with | ||
| 321 | * "can't locate module char-major-4" messages. */ | ||
| 322 | log = 0; | ||
| 323 | second_terminal = 0; | ||
| 324 | } | ||
| 325 | } | 360 | } |
| 326 | 361 | ||
| 327 | extern int | 362 | extern int |
| 328 | init_main(int argc, char * * argv) | 363 | init_main(int argc, char * * argv) |
| 329 | { | 364 | { |
| 330 | const char * rc_arguments[100]; | 365 | int run_rc = TRUE; |
| 331 | const char * arguments[100]; | 366 | int pid1 = 0; |
| 332 | int run_rc = TRUE; | 367 | int pid2 = 0; |
| 333 | int j; | 368 | struct stat statbuf; |
| 334 | int pid1 = 0; | 369 | const char* init_commands = SHELL "-c exec " INITSCRIPT; |
| 335 | int pid2 = 0; | 370 | const char* shell_commands = SHELL; |
| 336 | struct stat statbuf; | 371 | const char* tty0_commands = init_commands; |
| 337 | const char * tty_commands[3] = { "etc/init.d/rcS", "bin/sh"}; | 372 | const char* tty1_commands = shell_commands; |
| 338 | int serial_console = 0; | 373 | const char* no_memory = |
| 339 | int retval; | 374 | "Sorry, your computer does not have enough memory.\n"; |
| 340 | 375 | ||
| 341 | /* | 376 | /* For later use */ |
| 342 | * If I am started as /linuxrc instead of /sbin/init, I don't have the | 377 | argv0 = argv[0]; |
| 343 | * environment that init expects. I can't fix the signal behavior. Try | 378 | maxproclen = strlen(argv[0]); |
| 344 | * to divorce from the controlling terminal with setsid(). This won't work | 379 | setproctitle("init [boot]"); |
| 345 | * if I am the process group leader. | 380 | |
| 346 | */ | 381 | |
| 347 | setsid(); | 382 | #ifndef DEBUG_INIT |
| 348 | 383 | /* Set up sig handlers */ | |
| 349 | signal(SIGUSR1, halt_signal); | 384 | signal(SIGUSR1, halt_signal); |
| 385 | signal(SIGSEGV, halt_signal); | ||
| 386 | signal(SIGPWR, halt_signal); | ||
| 387 | signal(SIGALRM, halt_signal); | ||
| 388 | signal(SIGHUP, halt_signal); | ||
| 350 | signal(SIGUSR2, reboot_signal); | 389 | signal(SIGUSR2, reboot_signal); |
| 351 | signal(SIGINT, reboot_signal); | 390 | signal(SIGINT, reboot_signal); |
| 352 | signal(SIGTERM, reboot_signal); | 391 | signal(SIGTERM, reboot_signal); |
| 392 | #endif | ||
| 393 | /* Figure out where the default console should be */ | ||
| 394 | console_init(); | ||
| 353 | 395 | ||
| 396 | /* Turn off rebooting via CTL-ALT-DEL -- we get a | ||
| 397 | * SIGINT on CAD so we can shut things down gracefully... */ | ||
| 398 | #ifndef DEBUG_INIT | ||
| 354 | reboot(RB_DISABLE_CAD); | 399 | reboot(RB_DISABLE_CAD); |
| 400 | #endif | ||
| 355 | 401 | ||
| 356 | message(log, "%s: started. ", argv[0]); | 402 | /* Close whatever files are open, and reset the console. */ |
| 403 | close(0); | ||
| 404 | close(1); | ||
| 405 | close(2); | ||
| 406 | set_term(); | ||
| 407 | setsid(); | ||
| 357 | 408 | ||
| 358 | for ( j = 1; j < argc; j++ ) { | 409 | /* Make sure PATH is set to something sane */ |
| 359 | if ( strcmp(argv[j], "single") == 0 ) { | 410 | if (getenv("PATH") == NULL) |
| 360 | run_rc = FALSE; | 411 | putenv(PATH_DEFAULT); |
| 361 | tty_commands[0] = "bin/sh"; | ||
| 362 | tty_commands[1] = 0; | ||
| 363 | } | ||
| 364 | } | ||
| 365 | for ( j = 0; __environ[j] != 0; j++ ) { | ||
| 366 | if ( strncmp(__environ[j], "tty", 3) == 0 | ||
| 367 | && __environ[j][3] >= '1' | ||
| 368 | && __environ[j][3] <= '2' | ||
| 369 | && __environ[j][4] == '=' ) { | ||
| 370 | const char * s = &__environ[j][5]; | ||
| 371 | 412 | ||
| 372 | if ( *s == 0 || strcmp(s, "off") == 0 ) | 413 | /* Hello world */ |
| 373 | s = 0; | 414 | message(console, "%s started: BusyBox v%s (%s) multi-call binary", |
| 415 | argv[0], BB_VER, BB_BT); | ||
| 416 | message(log, "%s started: BusyBox v%s (%s) multi-call binary", | ||
| 417 | argv[0], BB_VER, BB_BT); | ||
| 374 | 418 | ||
| 375 | tty_commands[__environ[j][3] - '1'] = s; | ||
| 376 | } | ||
| 377 | /* Should catch the syntax of Sparc kernel console setting. */ | ||
| 378 | /* The kernel does not recognize a serial console when getting*/ | ||
| 379 | /* console=/dev/ttySX !! */ | ||
| 380 | else if ( strcmp(__environ[j], "console=ttya") == 0 ) { | ||
| 381 | serial_console=1; | ||
| 382 | } | ||
| 383 | else if ( strcmp(__environ[j], "console=ttyb") == 0 ) { | ||
| 384 | serial_console=2; | ||
| 385 | } | ||
| 386 | /* standard console settings */ | ||
| 387 | else if ( strncmp(__environ[j], "console=", 8) == 0 ) { | ||
| 388 | first_terminal=&(__environ[j][8]); | ||
| 389 | } | ||
| 390 | else if ( strncmp(__environ[j], "TERM=", 5) == 0) { | ||
| 391 | term_ptr=__environ[j]; | ||
| 392 | } | ||
| 393 | } | ||
| 394 | 419 | ||
| 395 | printf("mounting /proc ...\n"); | 420 | /* Mount /proc */ |
| 421 | message(console, "Mounting /proc: \n"); | ||
| 396 | if (mount("/proc","/proc","proc",0,0)) { | 422 | if (mount("/proc","/proc","proc",0,0)) { |
| 397 | perror("mounting /proc failed\n"); | 423 | message(log, "%s: could not mount /proc!\n", argv[0]); |
| 398 | } | 424 | message(console, "failed!\n"); |
| 399 | printf("\tdone.\n"); | ||
| 400 | |||
| 401 | if (get_kernel_revision() >= 2*65536+1*256+71) { | ||
| 402 | /* if >= 2.1.71 kernel, /dev/console is not a symlink anymore: | ||
| 403 | * use it as primary console */ | ||
| 404 | serial_console=-1; | ||
| 405 | } | 425 | } |
| 426 | message(console, "done.\n"); | ||
| 406 | 427 | ||
| 407 | /* Make sure /etc/init.d/rc exists */ | ||
| 408 | retval= stat(tty_commands[0],&statbuf); | ||
| 409 | if (retval) | ||
| 410 | run_rc = FALSE; | ||
| 411 | |||
| 412 | configure_terminals( serial_console, run_rc); | ||
| 413 | 428 | ||
| 429 | /* Make sure there is enough memory to do something useful*/ | ||
| 414 | set_free_pages(); | 430 | set_free_pages(); |
| 415 | |||
| 416 | /* not enough memory to do anything useful*/ | ||
| 417 | if (mem_total() < 2000) { | 431 | if (mem_total() < 2000) { |
| 432 | int retval; | ||
| 418 | retval= stat("/etc/fstab",&statbuf); | 433 | retval= stat("/etc/fstab",&statbuf); |
| 419 | if (retval) { | 434 | if (retval) { |
| 420 | printf("You do not have enough RAM, sorry.\n"); | 435 | message(console, "%s", no_memory); |
| 421 | while (1) { sleep(1);} | 436 | while (1) { sleep(1);} |
| 422 | } else { | 437 | } else { |
| 423 | /* Try to turn on swap */ | 438 | /* Try to turn on swap */ |
| 424 | static const char * const swapon_args[] = {"/bin/swapon", "-a", 0}; | 439 | waitfor(run("/bin/swapon -a", console, 0)); |
| 425 | waitfor(run("/bin/swapon", swapon_args, console, 0)); | ||
| 426 | if (mem_total() < 2000) { | 440 | if (mem_total() < 2000) { |
| 427 | printf("You do not have enough RAM, sorry.\n"); | 441 | message(console, "%s", no_memory); |
| 428 | while (1) { sleep(1);} | 442 | while (1) { sleep(1);} |
| 429 | } | 443 | } |
| 430 | } | 444 | } |
| 431 | } | 445 | } |
| 432 | 446 | ||
| 433 | /* | 447 | /* Check if we are supposed to be in single user mode */ |
| 434 | * Don't modify **argv directly, it would show up in the "ps" display. | 448 | if (!strcmp(argv[1], "single") || !strcmp(argv[1], "-s") || !strcmp(argv[1], "1")) { |
| 435 | * I don't want "init" to look like "rc". | 449 | run_rc = FALSE; |
| 436 | */ | 450 | tty0_commands = shell_commands; |
| 437 | rc_arguments[0] = tty_commands[0]; | 451 | tty1_commands = 0; |
| 438 | for ( j = 1; j < argc; j++ ) { | 452 | setproctitle("init [S]"); |
| 439 | rc_arguments[j] = argv[j]; | 453 | } else { |
| 454 | setproctitle("init [1]"); | ||
| 440 | } | 455 | } |
| 441 | rc_arguments[j] = 0; | ||
| 442 | 456 | ||
| 443 | arguments[0] = "-sh"; | 457 | /* Make sure an init script exists before trying to run it */ |
| 444 | arguments[1] = 0; | 458 | if ( run_rc == TRUE && stat( INITSCRIPT, &statbuf)) { |
| 445 | 459 | tty0_commands = shell_commands; | |
| 446 | /* Ok, now launch the rc script /etc/init.d/rcS and prepare to start up | 460 | tty1_commands = shell_commands; |
| 447 | * some VTs on tty1 and tty2 if somebody hits enter | 461 | } |
| 462 | |||
| 463 | /* Ok, now launch the rc script and/or prepare to | ||
| 464 | * start up some VTs if somebody hits enter... | ||
| 448 | */ | 465 | */ |
| 449 | for ( ; ; ) { | 466 | for ( ; ; ) { |
| 450 | int wpid; | 467 | int wpid; |
| 451 | int status; | 468 | int status; |
| 452 | 469 | ||
| 453 | if ( pid1 == 0 && tty_commands[0] ) { | 470 | if ( pid1 == 0 && *tty0_commands ) { |
| 454 | if ( run_rc == TRUE ) { | 471 | pid1 = run(tty0_commands, console, 1); |
| 455 | pid1 = run(tty_commands[0], rc_arguments, first_terminal, 0); | ||
| 456 | } else { | ||
| 457 | pid2 = run(tty_commands[1], arguments, first_terminal, 1); | ||
| 458 | } | ||
| 459 | } | 472 | } |
| 460 | if ( pid2 == 0 && second_terminal && tty_commands[1] ) { | 473 | if ( pid2 == 0 && *tty1_commands ) { |
| 461 | pid2 = run(tty_commands[1], arguments, second_terminal, 1); | 474 | pid2 = run(tty1_commands, second_terminal, 1); |
| 462 | } | 475 | } |
| 463 | wpid = wait(&status); | 476 | wpid = wait(&status); |
| 464 | if ( wpid > 0 && wpid != pid1) { | 477 | if ( wpid > 0 && wpid != pid1) { |
| @@ -467,6 +480,7 @@ init_main(int argc, char * * argv) | |||
| 467 | if ( wpid == pid2 ) { | 480 | if ( wpid == pid2 ) { |
| 468 | pid2 = 0; | 481 | pid2 = 0; |
| 469 | } | 482 | } |
| 483 | sleep(1); | ||
| 470 | } | 484 | } |
| 471 | } | 485 | } |
| 472 | 486 | ||
