diff options
author | Eric Andersen <andersen@codepoet.org> | 1999-11-01 23:59:44 +0000 |
---|---|---|
committer | Eric Andersen <andersen@codepoet.org> | 1999-11-01 23:59:44 +0000 |
commit | 2f6c04f63c55a03366dfd5848f00bda664c9a90d (patch) | |
tree | 3e9c595c46a1032cc1bd4d20694c8a0edfd643f9 | |
parent | ff719d663e49ab3abb8060ad93e62b160029f63b (diff) | |
download | busybox-w32-2f6c04f63c55a03366dfd5848f00bda664c9a90d.tar.gz busybox-w32-2f6c04f63c55a03366dfd5848f00bda664c9a90d.tar.bz2 busybox-w32-2f6c04f63c55a03366dfd5848f00bda664c9a90d.zip |
Ha! Got init working.
-Erik
-rw-r--r-- | Makefile | 6 | ||||
-rw-r--r-- | busybox.def.h | 8 | ||||
-rw-r--r-- | busybox.spec | 4 | ||||
-rw-r--r-- | examples/busybox.spec | 4 | ||||
-rw-r--r-- | init.c | 98 | ||||
-rw-r--r-- | init/init.c | 98 | ||||
-rw-r--r-- | utility.c | 2 |
7 files changed, 118 insertions, 102 deletions
@@ -17,12 +17,12 @@ | |||
17 | 17 | ||
18 | 18 | ||
19 | PROG=busybox | 19 | PROG=busybox |
20 | VERSION=0.31 | 20 | VERSION=0.32 |
21 | BUILDTIME=$(shell date "+%Y%m%d-%H%M") | 21 | BUILDTIME=$(shell date "+%Y%m%d-%H%M") |
22 | 22 | ||
23 | # Comment out the following to make a debuggable build | 23 | # Comment out the following to make a debuggable build |
24 | # Leave this off for production use. | 24 | # Leave this off for production use. |
25 | DODEBUG=true | 25 | DODEBUG=false |
26 | # If you want a static binary, turn this on. I can't think | 26 | # If you want a static binary, turn this on. I can't think |
27 | # of many situations where anybody would ever want it static, | 27 | # of many situations where anybody would ever want it static, |
28 | # but... | 28 | # but... |
@@ -34,7 +34,7 @@ ARCH=`uname -m | sed -e 's/i.86/i386/' | sed -e 's/sparc.*/sparc/'` | |||
34 | 34 | ||
35 | # -D_GNU_SOURCE is needed because environ is used in init.c | 35 | # -D_GNU_SOURCE is needed because environ is used in init.c |
36 | ifeq ($(DODEBUG),true) | 36 | ifeq ($(DODEBUG),true) |
37 | CFLAGS=-Wall -g -D_GNU_SOURCE | 37 | CFLAGS=-Wall -g -D_GNU_SOURCE -DDEBUG_INIT |
38 | STRIP= | 38 | STRIP= |
39 | LDFLAGS= | 39 | LDFLAGS= |
40 | else | 40 | else |
diff --git a/busybox.def.h b/busybox.def.h index 57f835e24..0440b9f2b 100644 --- a/busybox.def.h +++ b/busybox.def.h | |||
@@ -15,12 +15,12 @@ | |||
15 | #define BB_DMESG | 15 | #define BB_DMESG |
16 | //#define BB_DUTMP | 16 | //#define BB_DUTMP |
17 | //#define BB_FDFLUSH | 17 | //#define BB_FDFLUSH |
18 | //#define BB_FIND | 18 | #define BB_FIND |
19 | //#define BB_FSCK_MINIX | 19 | #define BB_FSCK_MINIX |
20 | //#define BB_MKFS_MINIX | 20 | #define BB_MKFS_MINIX |
21 | #define BB_CHVT | 21 | #define BB_CHVT |
22 | #define BB_DEALLOCVT | 22 | #define BB_DEALLOCVT |
23 | //#define BB_GREP | 23 | #define BB_GREP |
24 | //#define BB_HALT | 24 | //#define BB_HALT |
25 | #define BB_INIT | 25 | #define BB_INIT |
26 | #define BB_KILL | 26 | #define BB_KILL |
diff --git a/busybox.spec b/busybox.spec index 281a381a7..422725671 100644 --- a/busybox.spec +++ b/busybox.spec | |||
@@ -1,5 +1,5 @@ | |||
1 | Name: busybox | 1 | Name: busybox |
2 | Version: 0.29alpha | 2 | Version: 0.32 |
3 | Release: 1 | 3 | Release: 1 |
4 | Group: System/Utilities | 4 | Group: System/Utilities |
5 | Summary: BusyBox is a tiny suite of Unix utilities in a multi-call binary. | 5 | Summary: BusyBox is a tiny suite of Unix utilities in a multi-call binary. |
@@ -7,7 +7,7 @@ Copyright: GPL | |||
7 | Packager : Erik Andersen <andersen@lineo.com> | 7 | Packager : Erik Andersen <andersen@lineo.com> |
8 | Conflicts: fileutils grep shellutils | 8 | Conflicts: fileutils grep shellutils |
9 | Buildroot: /tmp/%{Name}-%{Version} | 9 | Buildroot: /tmp/%{Name}-%{Version} |
10 | Source: busybox-0.29a1.tar.gz | 10 | Source: busybox-0.32.tar.gz |
11 | 11 | ||
12 | %Description | 12 | %Description |
13 | BusyBox is a suite of "tiny" Unix utilities in a multi-call binary. It | 13 | BusyBox is a suite of "tiny" Unix utilities in a multi-call binary. It |
diff --git a/examples/busybox.spec b/examples/busybox.spec index 281a381a7..422725671 100644 --- a/examples/busybox.spec +++ b/examples/busybox.spec | |||
@@ -1,5 +1,5 @@ | |||
1 | Name: busybox | 1 | Name: busybox |
2 | Version: 0.29alpha | 2 | Version: 0.32 |
3 | Release: 1 | 3 | Release: 1 |
4 | Group: System/Utilities | 4 | Group: System/Utilities |
5 | Summary: BusyBox is a tiny suite of Unix utilities in a multi-call binary. | 5 | Summary: BusyBox is a tiny suite of Unix utilities in a multi-call binary. |
@@ -7,7 +7,7 @@ Copyright: GPL | |||
7 | Packager : Erik Andersen <andersen@lineo.com> | 7 | Packager : Erik Andersen <andersen@lineo.com> |
8 | Conflicts: fileutils grep shellutils | 8 | Conflicts: fileutils grep shellutils |
9 | Buildroot: /tmp/%{Name}-%{Version} | 9 | Buildroot: /tmp/%{Name}-%{Version} |
10 | Source: busybox-0.29a1.tar.gz | 10 | Source: busybox-0.32.tar.gz |
11 | 11 | ||
12 | %Description | 12 | %Description |
13 | BusyBox is a suite of "tiny" Unix utilities in a multi-call binary. It | 13 | BusyBox is a suite of "tiny" Unix utilities in a multi-call binary. It |
@@ -42,14 +42,15 @@ | |||
42 | #include <sys/ioctl.h> | 42 | #include <sys/ioctl.h> |
43 | 43 | ||
44 | /* Turn this on to debug init so it won't reboot when killed */ | 44 | /* Turn this on to debug init so it won't reboot when killed */ |
45 | #define DEBUG_INIT | 45 | //#define DEBUG_INIT |
46 | 46 | ||
47 | #define CONSOLE "/dev/console" /* Logical system console */ | 47 | #define CONSOLE "/dev/console" /* Logical system console */ |
48 | #define VT_PRIMARY "/dev/tty0" /* Virtual console master */ | 48 | #define VT_PRIMARY "/dev/tty0" /* Virtual console master */ |
49 | #define VT_SECONDARY "/dev/tty1" /* Virtual console master */ | 49 | #define VT_SECONDARY "/dev/tty1" /* Virtual console master */ |
50 | #define VT_LOG "/dev/tty2" /* Virtual console master */ | 50 | #define VT_LOG "/dev/tty2" /* Virtual console master */ |
51 | #define SHELL "/bin/sh" /* Default shell */ | 51 | #define SHELL "/bin/sh" /* Default shell */ |
52 | #define INITSCRIPT "/etc/init.d/rcS" /* Initscript. */ | 52 | //#define INITSCRIPT "/etc/init.d/rcS" /* Initscript. */ |
53 | #define INITSCRIPT "/tmp/foo.sh" | ||
53 | #define PATH_DEFAULT "PATH=/usr/local/sbin:/sbin:/bin:/usr/sbin:/usr/bin" | 54 | #define PATH_DEFAULT "PATH=/usr/local/sbin:/sbin:/bin:/usr/sbin:/usr/bin" |
54 | 55 | ||
55 | static char *console = CONSOLE; | 56 | static char *console = CONSOLE; |
@@ -218,8 +219,9 @@ static int waitfor(int pid) | |||
218 | static pid_t run(const char * const* command, | 219 | static pid_t run(const char * const* command, |
219 | char *terminal, int get_enter) | 220 | char *terminal, int get_enter) |
220 | { | 221 | { |
221 | int i, f; | 222 | int fd; |
222 | pid_t pid; | 223 | pid_t pid; |
224 | const char * const* cmd = command; | ||
223 | static const char press_enter[] = | 225 | static const char press_enter[] = |
224 | "\nPlease press Enter to activate this console. "; | 226 | "\nPlease press Enter to activate this console. "; |
225 | 227 | ||
@@ -230,13 +232,22 @@ static pid_t run(const char * const* command, | |||
230 | close(2); | 232 | close(2); |
231 | setsid(); | 233 | setsid(); |
232 | 234 | ||
233 | f=open(terminal, O_RDWR); | 235 | /* Reset signal handlers set for parent process */ |
234 | dup(f); | 236 | signal(SIGUSR1, SIG_DFL); |
235 | dup(f); | 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); | ||
236 | tcsetpgrp(0, getpgrp()); | 247 | tcsetpgrp(0, getpgrp()); |
237 | set_term(0); | 248 | set_term(0); |
238 | 249 | ||
239 | if (get_enter) { | 250 | if (get_enter==TRUE) { |
240 | /* | 251 | /* |
241 | * Save memory by not exec-ing anything large (like a shell) | 252 | * Save memory by not exec-ing anything large (like a shell) |
242 | * before the user wants it. This is critical if swap is not | 253 | * before the user wants it. This is critical if swap is not |
@@ -251,51 +262,45 @@ static pid_t run(const char * const* command, | |||
251 | } | 262 | } |
252 | 263 | ||
253 | /* Log the process name and args */ | 264 | /* Log the process name and args */ |
254 | 265 | message(log, "Executing pid(%d): '", getpid()); | |
266 | while ( *cmd) message(log, "%s ", *cmd++); | ||
267 | message(log, "'\r\n"); | ||
268 | |||
255 | /* Now run it. The new program will take over this PID, | 269 | /* Now run it. The new program will take over this PID, |
256 | * so nothing further in init.c should be run. */ | 270 | * so nothing further in init.c should be run. */ |
257 | message(log, "Executing '%s', pid(%d)\r\n", *command, getpid()); | ||
258 | execvp(*command, (char**)command+1); | 271 | execvp(*command, (char**)command+1); |
259 | 272 | ||
260 | message(log, "Hmm. Trying as a script.\r\n"); | 273 | message(log, "Bummer, could not execute '%s'\n", command); |
261 | /* If shell scripts are not executed, force the issue */ | ||
262 | if (errno == ENOEXEC) { | ||
263 | char * args[16]; | ||
264 | args[0] = SHELL; | ||
265 | args[1] = "-c"; | ||
266 | args[2] = "exec"; | ||
267 | for( i=0 ; i<16 && command[i]; i++) | ||
268 | args[3+i] = (char*)command[i]; | ||
269 | args[i] = NULL; | ||
270 | execvp(*args, (char**)args+1); | ||
271 | } | ||
272 | message(log, "Could not execute '%s'\n", command); | ||
273 | exit(-1); | 274 | exit(-1); |
274 | } | 275 | } |
275 | return pid; | 276 | return pid; |
276 | } | 277 | } |
277 | 278 | ||
278 | #ifndef DEBUG_INIT | ||
279 | static void shutdown_system(void) | 279 | static void shutdown_system(void) |
280 | { | 280 | { |
281 | const char* const swap_off_cmd[] = { "/bin/swapoff", "-a", 0}; | 281 | const char* const swap_off_cmd[] = { "swapoff", "swapoff", "-a", 0}; |
282 | const char* const umount_cmd[] = { "/bin/umount", "-a", "-n", 0}; | 282 | const char* const umount_cmd[] = { "umount", "umount", "-a", "-n", 0}; |
283 | 283 | ||
284 | message(console, "The system is going down NOW !!\r\n"); | 284 | message(console, "The system is going down NOW !!\r\n"); |
285 | sync(); | 285 | sync(); |
286 | #ifndef DEBUG_INIT | ||
286 | /* Allow Ctrl-Alt-Del to reboot system. */ | 287 | /* Allow Ctrl-Alt-Del to reboot system. */ |
287 | reboot(RB_ENABLE_CAD); | 288 | reboot(RB_ENABLE_CAD); |
288 | 289 | #endif | |
289 | /* Send signals to every process _except_ pid 1 */ | 290 | /* Send signals to every process _except_ pid 1 */ |
290 | message(console, "Sending SIGHUP to all processes.\r\n"); | 291 | message(console, "Sending SIGHUP to all processes.\r\n"); |
292 | #ifndef DEBUG_INIT | ||
291 | kill(-1, SIGHUP); | 293 | kill(-1, SIGHUP); |
294 | #endif | ||
292 | sleep(2); | 295 | sleep(2); |
293 | sync(); | 296 | sync(); |
294 | message(console, "Sending SIGKILL to all processes.\r\n"); | 297 | message(console, "Sending SIGKILL to all processes.\r\n"); |
298 | #ifndef DEBUG_INIT | ||
295 | kill(-1, SIGKILL); | 299 | kill(-1, SIGKILL); |
300 | #endif | ||
296 | sleep(1); | 301 | sleep(1); |
297 | waitfor(run( swap_off_cmd, console, 0)); | 302 | waitfor(run( swap_off_cmd, log, FALSE)); |
298 | waitfor(run( umount_cmd, console, 0)); | 303 | waitfor(run( umount_cmd, log, FALSE)); |
299 | sync(); | 304 | sync(); |
300 | if (get_kernel_revision() <= 2 * 65536 + 2 * 256 + 11) { | 305 | if (get_kernel_revision() <= 2 * 65536 + 2 * 256 + 11) { |
301 | /* Removed bdflush call, kupdate in kernels >2.2.11 */ | 306 | /* Removed bdflush call, kupdate in kernels >2.2.11 */ |
@@ -309,7 +314,9 @@ static void halt_signal(int sig) | |||
309 | shutdown_system(); | 314 | shutdown_system(); |
310 | message(console, | 315 | message(console, |
311 | "The system is halted. Press CTRL-ALT-DEL or turn off power\r\n"); | 316 | "The system is halted. Press CTRL-ALT-DEL or turn off power\r\n"); |
317 | #ifndef DEBUG_INIT | ||
312 | reboot(RB_POWER_OFF); | 318 | reboot(RB_POWER_OFF); |
319 | #endif | ||
313 | exit(0); | 320 | exit(0); |
314 | } | 321 | } |
315 | 322 | ||
@@ -317,45 +324,44 @@ static void reboot_signal(int sig) | |||
317 | { | 324 | { |
318 | shutdown_system(); | 325 | shutdown_system(); |
319 | message(console, "Please stand by while rebooting the system.\r\n"); | 326 | message(console, "Please stand by while rebooting the system.\r\n"); |
327 | #ifndef DEBUG_INIT | ||
320 | reboot(RB_AUTOBOOT); | 328 | reboot(RB_AUTOBOOT); |
329 | #endif | ||
321 | exit(0); | 330 | exit(0); |
322 | } | 331 | } |
323 | #endif | ||
324 | 332 | ||
325 | extern int init_main(int argc, char **argv) | 333 | extern int init_main(int argc, char **argv) |
326 | { | 334 | { |
327 | int run_rc = TRUE; | 335 | int run_rc = TRUE; |
336 | int wait_for_enter = FALSE; | ||
328 | pid_t pid1 = 0; | 337 | pid_t pid1 = 0; |
329 | pid_t pid2 = 0; | 338 | pid_t pid2 = 0; |
330 | struct stat statbuf; | 339 | struct stat statbuf; |
331 | const char* const swap_on_cmd[] = { "/bin/swapon", " -a ", 0}; | 340 | const char* const swap_on_cmd[] = { "/bin/swapon", "swapon", "-a", 0}; |
332 | const char* const init_commands[] = { SHELL, " -c", " exec ", INITSCRIPT, 0}; | 341 | const char* const init_commands[] = { INITSCRIPT, INITSCRIPT, 0}; |
333 | const char* const shell_commands[] = { SHELL, " -", 0}; | 342 | const char* const shell_commands[] = { SHELL, "-" SHELL, 0}; |
334 | const char* const* tty0_commands = init_commands; | 343 | const char* const* tty0_commands = init_commands; |
335 | const char* const* tty1_commands = shell_commands; | 344 | const char* const* tty1_commands = shell_commands; |
336 | #ifndef DEBUG_INIT | 345 | #ifdef DEBUG_INIT |
337 | char *hello_msg_format = | 346 | char *hello_msg_format = |
338 | "init started: BusyBox v%s (%s) multi-call binary\r\n"; | 347 | "init(%d) started: BusyBox v%s (%s) multi-call binary\r\n"; |
339 | #else | 348 | #else |
340 | char *hello_msg_format = | 349 | char *hello_msg_format = |
341 | "init(%d) started: BusyBox v%s (%s) multi-call binary\r\n"; | 350 | "init started: BusyBox v%s (%s) multi-call binary\r\n"; |
342 | #endif | 351 | #endif |
343 | const char *no_memory = | 352 | const char *no_memory = |
344 | "Sorry, your computer does not have enough memory.\r\n"; | 353 | "Sorry, your computer does not have enough memory.\r\n"; |
345 | 354 | ||
346 | #ifndef DEBUG_INIT | 355 | /* Set up sig handlers -- be sure to |
347 | /* Set up sig handlers */ | 356 | * clear all of these in run() */ |
348 | signal(SIGUSR1, halt_signal); | 357 | signal(SIGUSR1, halt_signal); |
349 | signal(SIGSEGV, halt_signal); | ||
350 | signal(SIGPWR, halt_signal); | ||
351 | signal(SIGALRM, halt_signal); | ||
352 | signal(SIGHUP, halt_signal); | ||
353 | signal(SIGUSR2, reboot_signal); | 358 | signal(SIGUSR2, reboot_signal); |
354 | signal(SIGINT, reboot_signal); | 359 | signal(SIGINT, reboot_signal); |
355 | signal(SIGTERM, reboot_signal); | 360 | signal(SIGTERM, reboot_signal); |
356 | 361 | ||
357 | /* Turn off rebooting via CTL-ALT-DEL -- we get a | 362 | /* Turn off rebooting via CTL-ALT-DEL -- we get a |
358 | * SIGINT on CAD so we can shut things down gracefully... */ | 363 | * SIGINT on CAD so we can shut things down gracefully... */ |
364 | #ifndef DEBUG_INIT | ||
359 | reboot(RB_DISABLE_CAD); | 365 | reboot(RB_DISABLE_CAD); |
360 | #endif | 366 | #endif |
361 | /* Figure out where the default console should be */ | 367 | /* Figure out where the default console should be */ |
@@ -383,7 +389,7 @@ extern int init_main(int argc, char **argv) | |||
383 | 389 | ||
384 | 390 | ||
385 | /* Mount /proc */ | 391 | /* Mount /proc */ |
386 | if (mount("/proc", "/proc", "proc", 0, 0) == 0) { | 392 | if (mount ("proc", "/proc", "proc", 0, 0) == 0) { |
387 | message(log, "Mounting /proc: done.\n"); | 393 | message(log, "Mounting /proc: done.\n"); |
388 | message(console, "Mounting /proc: done.\n"); | 394 | message(console, "Mounting /proc: done.\n"); |
389 | } else { | 395 | } else { |
@@ -403,7 +409,7 @@ extern int init_main(int argc, char **argv) | |||
403 | } | 409 | } |
404 | } else { | 410 | } else { |
405 | /* Try to turn on swap */ | 411 | /* Try to turn on swap */ |
406 | waitfor(run(swap_on_cmd, console, 0)); | 412 | waitfor(run(swap_on_cmd, log, FALSE)); |
407 | if (mem_total() < 2000) { | 413 | if (mem_total() < 2000) { |
408 | message(console, "%s", no_memory); | 414 | message(console, "%s", no_memory); |
409 | while (1) { | 415 | while (1) { |
@@ -417,12 +423,14 @@ extern int init_main(int argc, char **argv) | |||
417 | if ( argc > 1 && (!strcmp(argv[1], "single") || | 423 | if ( argc > 1 && (!strcmp(argv[1], "single") || |
418 | !strcmp(argv[1], "-s") || !strcmp(argv[1], "1"))) { | 424 | !strcmp(argv[1], "-s") || !strcmp(argv[1], "1"))) { |
419 | run_rc = FALSE; | 425 | run_rc = FALSE; |
426 | wait_for_enter = TRUE; | ||
420 | tty0_commands = shell_commands; | 427 | tty0_commands = shell_commands; |
421 | tty1_commands = shell_commands; | 428 | tty1_commands = shell_commands; |
422 | } | 429 | } |
423 | 430 | ||
424 | /* Make sure an init script exists before trying to run it */ | 431 | /* Make sure an init script exists before trying to run it */ |
425 | if (run_rc == TRUE && stat(INITSCRIPT, &statbuf)) { | 432 | if (run_rc == TRUE && stat(INITSCRIPT, &statbuf)) { |
433 | wait_for_enter = TRUE; | ||
426 | tty0_commands = shell_commands; | 434 | tty0_commands = shell_commands; |
427 | tty1_commands = shell_commands; | 435 | tty1_commands = shell_commands; |
428 | } | 436 | } |
@@ -435,10 +443,10 @@ extern int init_main(int argc, char **argv) | |||
435 | int status; | 443 | int status; |
436 | 444 | ||
437 | if (pid1 == 0 && tty0_commands) { | 445 | if (pid1 == 0 && tty0_commands) { |
438 | pid1 = run(tty0_commands, console, 1); | 446 | pid1 = run(tty0_commands, console, wait_for_enter); |
439 | } | 447 | } |
440 | if (pid2 == 0 && tty1_commands) { | 448 | if (pid2 == 0 && tty1_commands) { |
441 | pid2 = run(tty1_commands, second_terminal, 1); | 449 | pid2 = run(tty1_commands, second_terminal, TRUE); |
442 | } | 450 | } |
443 | wpid = wait(&status); | 451 | wpid = wait(&status); |
444 | if (wpid > 0 ) { | 452 | if (wpid > 0 ) { |
diff --git a/init/init.c b/init/init.c index bcc8483da..0850cf810 100644 --- a/init/init.c +++ b/init/init.c | |||
@@ -42,14 +42,15 @@ | |||
42 | #include <sys/ioctl.h> | 42 | #include <sys/ioctl.h> |
43 | 43 | ||
44 | /* Turn this on to debug init so it won't reboot when killed */ | 44 | /* Turn this on to debug init so it won't reboot when killed */ |
45 | #define DEBUG_INIT | 45 | //#define DEBUG_INIT |
46 | 46 | ||
47 | #define CONSOLE "/dev/console" /* Logical system console */ | 47 | #define CONSOLE "/dev/console" /* Logical system console */ |
48 | #define VT_PRIMARY "/dev/tty0" /* Virtual console master */ | 48 | #define VT_PRIMARY "/dev/tty0" /* Virtual console master */ |
49 | #define VT_SECONDARY "/dev/tty1" /* Virtual console master */ | 49 | #define VT_SECONDARY "/dev/tty1" /* Virtual console master */ |
50 | #define VT_LOG "/dev/tty2" /* Virtual console master */ | 50 | #define VT_LOG "/dev/tty2" /* Virtual console master */ |
51 | #define SHELL "/bin/sh" /* Default shell */ | 51 | #define SHELL "/bin/sh" /* Default shell */ |
52 | #define INITSCRIPT "/etc/init.d/rcS" /* Initscript. */ | 52 | //#define INITSCRIPT "/etc/init.d/rcS" /* Initscript. */ |
53 | #define INITSCRIPT "/tmp/foo.sh" | ||
53 | #define PATH_DEFAULT "PATH=/usr/local/sbin:/sbin:/bin:/usr/sbin:/usr/bin" | 54 | #define PATH_DEFAULT "PATH=/usr/local/sbin:/sbin:/bin:/usr/sbin:/usr/bin" |
54 | 55 | ||
55 | static char *console = CONSOLE; | 56 | static char *console = CONSOLE; |
@@ -218,8 +219,9 @@ static int waitfor(int pid) | |||
218 | static pid_t run(const char * const* command, | 219 | static pid_t run(const char * const* command, |
219 | char *terminal, int get_enter) | 220 | char *terminal, int get_enter) |
220 | { | 221 | { |
221 | int i, f; | 222 | int fd; |
222 | pid_t pid; | 223 | pid_t pid; |
224 | const char * const* cmd = command; | ||
223 | static const char press_enter[] = | 225 | static const char press_enter[] = |
224 | "\nPlease press Enter to activate this console. "; | 226 | "\nPlease press Enter to activate this console. "; |
225 | 227 | ||
@@ -230,13 +232,22 @@ static pid_t run(const char * const* command, | |||
230 | close(2); | 232 | close(2); |
231 | setsid(); | 233 | setsid(); |
232 | 234 | ||
233 | f=open(terminal, O_RDWR); | 235 | /* Reset signal handlers set for parent process */ |
234 | dup(f); | 236 | signal(SIGUSR1, SIG_DFL); |
235 | dup(f); | 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); | ||
236 | tcsetpgrp(0, getpgrp()); | 247 | tcsetpgrp(0, getpgrp()); |
237 | set_term(0); | 248 | set_term(0); |
238 | 249 | ||
239 | if (get_enter) { | 250 | if (get_enter==TRUE) { |
240 | /* | 251 | /* |
241 | * Save memory by not exec-ing anything large (like a shell) | 252 | * Save memory by not exec-ing anything large (like a shell) |
242 | * before the user wants it. This is critical if swap is not | 253 | * before the user wants it. This is critical if swap is not |
@@ -251,51 +262,45 @@ static pid_t run(const char * const* command, | |||
251 | } | 262 | } |
252 | 263 | ||
253 | /* Log the process name and args */ | 264 | /* Log the process name and args */ |
254 | 265 | message(log, "Executing pid(%d): '", getpid()); | |
266 | while ( *cmd) message(log, "%s ", *cmd++); | ||
267 | message(log, "'\r\n"); | ||
268 | |||
255 | /* Now run it. The new program will take over this PID, | 269 | /* Now run it. The new program will take over this PID, |
256 | * so nothing further in init.c should be run. */ | 270 | * so nothing further in init.c should be run. */ |
257 | message(log, "Executing '%s', pid(%d)\r\n", *command, getpid()); | ||
258 | execvp(*command, (char**)command+1); | 271 | execvp(*command, (char**)command+1); |
259 | 272 | ||
260 | message(log, "Hmm. Trying as a script.\r\n"); | 273 | message(log, "Bummer, could not execute '%s'\n", command); |
261 | /* If shell scripts are not executed, force the issue */ | ||
262 | if (errno == ENOEXEC) { | ||
263 | char * args[16]; | ||
264 | args[0] = SHELL; | ||
265 | args[1] = "-c"; | ||
266 | args[2] = "exec"; | ||
267 | for( i=0 ; i<16 && command[i]; i++) | ||
268 | args[3+i] = (char*)command[i]; | ||
269 | args[i] = NULL; | ||
270 | execvp(*args, (char**)args+1); | ||
271 | } | ||
272 | message(log, "Could not execute '%s'\n", command); | ||
273 | exit(-1); | 274 | exit(-1); |
274 | } | 275 | } |
275 | return pid; | 276 | return pid; |
276 | } | 277 | } |
277 | 278 | ||
278 | #ifndef DEBUG_INIT | ||
279 | static void shutdown_system(void) | 279 | static void shutdown_system(void) |
280 | { | 280 | { |
281 | const char* const swap_off_cmd[] = { "/bin/swapoff", "-a", 0}; | 281 | const char* const swap_off_cmd[] = { "swapoff", "swapoff", "-a", 0}; |
282 | const char* const umount_cmd[] = { "/bin/umount", "-a", "-n", 0}; | 282 | const char* const umount_cmd[] = { "umount", "umount", "-a", "-n", 0}; |
283 | 283 | ||
284 | message(console, "The system is going down NOW !!\r\n"); | 284 | message(console, "The system is going down NOW !!\r\n"); |
285 | sync(); | 285 | sync(); |
286 | #ifndef DEBUG_INIT | ||
286 | /* Allow Ctrl-Alt-Del to reboot system. */ | 287 | /* Allow Ctrl-Alt-Del to reboot system. */ |
287 | reboot(RB_ENABLE_CAD); | 288 | reboot(RB_ENABLE_CAD); |
288 | 289 | #endif | |
289 | /* Send signals to every process _except_ pid 1 */ | 290 | /* Send signals to every process _except_ pid 1 */ |
290 | message(console, "Sending SIGHUP to all processes.\r\n"); | 291 | message(console, "Sending SIGHUP to all processes.\r\n"); |
292 | #ifndef DEBUG_INIT | ||
291 | kill(-1, SIGHUP); | 293 | kill(-1, SIGHUP); |
294 | #endif | ||
292 | sleep(2); | 295 | sleep(2); |
293 | sync(); | 296 | sync(); |
294 | message(console, "Sending SIGKILL to all processes.\r\n"); | 297 | message(console, "Sending SIGKILL to all processes.\r\n"); |
298 | #ifndef DEBUG_INIT | ||
295 | kill(-1, SIGKILL); | 299 | kill(-1, SIGKILL); |
300 | #endif | ||
296 | sleep(1); | 301 | sleep(1); |
297 | waitfor(run( swap_off_cmd, console, 0)); | 302 | waitfor(run( swap_off_cmd, log, FALSE)); |
298 | waitfor(run( umount_cmd, console, 0)); | 303 | waitfor(run( umount_cmd, log, FALSE)); |
299 | sync(); | 304 | sync(); |
300 | if (get_kernel_revision() <= 2 * 65536 + 2 * 256 + 11) { | 305 | if (get_kernel_revision() <= 2 * 65536 + 2 * 256 + 11) { |
301 | /* Removed bdflush call, kupdate in kernels >2.2.11 */ | 306 | /* Removed bdflush call, kupdate in kernels >2.2.11 */ |
@@ -309,7 +314,9 @@ static void halt_signal(int sig) | |||
309 | shutdown_system(); | 314 | shutdown_system(); |
310 | message(console, | 315 | message(console, |
311 | "The system is halted. Press CTRL-ALT-DEL or turn off power\r\n"); | 316 | "The system is halted. Press CTRL-ALT-DEL or turn off power\r\n"); |
317 | #ifndef DEBUG_INIT | ||
312 | reboot(RB_POWER_OFF); | 318 | reboot(RB_POWER_OFF); |
319 | #endif | ||
313 | exit(0); | 320 | exit(0); |
314 | } | 321 | } |
315 | 322 | ||
@@ -317,45 +324,44 @@ static void reboot_signal(int sig) | |||
317 | { | 324 | { |
318 | shutdown_system(); | 325 | shutdown_system(); |
319 | message(console, "Please stand by while rebooting the system.\r\n"); | 326 | message(console, "Please stand by while rebooting the system.\r\n"); |
327 | #ifndef DEBUG_INIT | ||
320 | reboot(RB_AUTOBOOT); | 328 | reboot(RB_AUTOBOOT); |
329 | #endif | ||
321 | exit(0); | 330 | exit(0); |
322 | } | 331 | } |
323 | #endif | ||
324 | 332 | ||
325 | extern int init_main(int argc, char **argv) | 333 | extern int init_main(int argc, char **argv) |
326 | { | 334 | { |
327 | int run_rc = TRUE; | 335 | int run_rc = TRUE; |
336 | int wait_for_enter = FALSE; | ||
328 | pid_t pid1 = 0; | 337 | pid_t pid1 = 0; |
329 | pid_t pid2 = 0; | 338 | pid_t pid2 = 0; |
330 | struct stat statbuf; | 339 | struct stat statbuf; |
331 | const char* const swap_on_cmd[] = { "/bin/swapon", " -a ", 0}; | 340 | const char* const swap_on_cmd[] = { "/bin/swapon", "swapon", "-a", 0}; |
332 | const char* const init_commands[] = { SHELL, " -c", " exec ", INITSCRIPT, 0}; | 341 | const char* const init_commands[] = { INITSCRIPT, INITSCRIPT, 0}; |
333 | const char* const shell_commands[] = { SHELL, " -", 0}; | 342 | const char* const shell_commands[] = { SHELL, "-" SHELL, 0}; |
334 | const char* const* tty0_commands = init_commands; | 343 | const char* const* tty0_commands = init_commands; |
335 | const char* const* tty1_commands = shell_commands; | 344 | const char* const* tty1_commands = shell_commands; |
336 | #ifndef DEBUG_INIT | 345 | #ifdef DEBUG_INIT |
337 | char *hello_msg_format = | 346 | char *hello_msg_format = |
338 | "init started: BusyBox v%s (%s) multi-call binary\r\n"; | 347 | "init(%d) started: BusyBox v%s (%s) multi-call binary\r\n"; |
339 | #else | 348 | #else |
340 | char *hello_msg_format = | 349 | char *hello_msg_format = |
341 | "init(%d) started: BusyBox v%s (%s) multi-call binary\r\n"; | 350 | "init started: BusyBox v%s (%s) multi-call binary\r\n"; |
342 | #endif | 351 | #endif |
343 | const char *no_memory = | 352 | const char *no_memory = |
344 | "Sorry, your computer does not have enough memory.\r\n"; | 353 | "Sorry, your computer does not have enough memory.\r\n"; |
345 | 354 | ||
346 | #ifndef DEBUG_INIT | 355 | /* Set up sig handlers -- be sure to |
347 | /* Set up sig handlers */ | 356 | * clear all of these in run() */ |
348 | signal(SIGUSR1, halt_signal); | 357 | signal(SIGUSR1, halt_signal); |
349 | signal(SIGSEGV, halt_signal); | ||
350 | signal(SIGPWR, halt_signal); | ||
351 | signal(SIGALRM, halt_signal); | ||
352 | signal(SIGHUP, halt_signal); | ||
353 | signal(SIGUSR2, reboot_signal); | 358 | signal(SIGUSR2, reboot_signal); |
354 | signal(SIGINT, reboot_signal); | 359 | signal(SIGINT, reboot_signal); |
355 | signal(SIGTERM, reboot_signal); | 360 | signal(SIGTERM, reboot_signal); |
356 | 361 | ||
357 | /* Turn off rebooting via CTL-ALT-DEL -- we get a | 362 | /* Turn off rebooting via CTL-ALT-DEL -- we get a |
358 | * SIGINT on CAD so we can shut things down gracefully... */ | 363 | * SIGINT on CAD so we can shut things down gracefully... */ |
364 | #ifndef DEBUG_INIT | ||
359 | reboot(RB_DISABLE_CAD); | 365 | reboot(RB_DISABLE_CAD); |
360 | #endif | 366 | #endif |
361 | /* Figure out where the default console should be */ | 367 | /* Figure out where the default console should be */ |
@@ -383,7 +389,7 @@ extern int init_main(int argc, char **argv) | |||
383 | 389 | ||
384 | 390 | ||
385 | /* Mount /proc */ | 391 | /* Mount /proc */ |
386 | if (mount("/proc", "/proc", "proc", 0, 0) == 0) { | 392 | if (mount ("proc", "/proc", "proc", 0, 0) == 0) { |
387 | message(log, "Mounting /proc: done.\n"); | 393 | message(log, "Mounting /proc: done.\n"); |
388 | message(console, "Mounting /proc: done.\n"); | 394 | message(console, "Mounting /proc: done.\n"); |
389 | } else { | 395 | } else { |
@@ -403,7 +409,7 @@ extern int init_main(int argc, char **argv) | |||
403 | } | 409 | } |
404 | } else { | 410 | } else { |
405 | /* Try to turn on swap */ | 411 | /* Try to turn on swap */ |
406 | waitfor(run(swap_on_cmd, console, 0)); | 412 | waitfor(run(swap_on_cmd, log, FALSE)); |
407 | if (mem_total() < 2000) { | 413 | if (mem_total() < 2000) { |
408 | message(console, "%s", no_memory); | 414 | message(console, "%s", no_memory); |
409 | while (1) { | 415 | while (1) { |
@@ -417,12 +423,14 @@ extern int init_main(int argc, char **argv) | |||
417 | if ( argc > 1 && (!strcmp(argv[1], "single") || | 423 | if ( argc > 1 && (!strcmp(argv[1], "single") || |
418 | !strcmp(argv[1], "-s") || !strcmp(argv[1], "1"))) { | 424 | !strcmp(argv[1], "-s") || !strcmp(argv[1], "1"))) { |
419 | run_rc = FALSE; | 425 | run_rc = FALSE; |
426 | wait_for_enter = TRUE; | ||
420 | tty0_commands = shell_commands; | 427 | tty0_commands = shell_commands; |
421 | tty1_commands = shell_commands; | 428 | tty1_commands = shell_commands; |
422 | } | 429 | } |
423 | 430 | ||
424 | /* Make sure an init script exists before trying to run it */ | 431 | /* Make sure an init script exists before trying to run it */ |
425 | if (run_rc == TRUE && stat(INITSCRIPT, &statbuf)) { | 432 | if (run_rc == TRUE && stat(INITSCRIPT, &statbuf)) { |
433 | wait_for_enter = TRUE; | ||
426 | tty0_commands = shell_commands; | 434 | tty0_commands = shell_commands; |
427 | tty1_commands = shell_commands; | 435 | tty1_commands = shell_commands; |
428 | } | 436 | } |
@@ -435,10 +443,10 @@ extern int init_main(int argc, char **argv) | |||
435 | int status; | 443 | int status; |
436 | 444 | ||
437 | if (pid1 == 0 && tty0_commands) { | 445 | if (pid1 == 0 && tty0_commands) { |
438 | pid1 = run(tty0_commands, console, 1); | 446 | pid1 = run(tty0_commands, console, wait_for_enter); |
439 | } | 447 | } |
440 | if (pid2 == 0 && tty1_commands) { | 448 | if (pid2 == 0 && tty1_commands) { |
441 | pid2 = run(tty1_commands, second_terminal, 1); | 449 | pid2 = run(tty1_commands, second_terminal, TRUE); |
442 | } | 450 | } |
443 | wpid = wait(&status); | 451 | wpid = wait(&status); |
444 | if (wpid > 0 ) { | 452 | if (wpid > 0 ) { |
@@ -60,7 +60,7 @@ get_kernel_revision() | |||
60 | 60 | ||
61 | file = fopen(filename,"r"); | 61 | file = fopen(filename,"r"); |
62 | if (file == NULL) { | 62 | if (file == NULL) { |
63 | perror(filename); | 63 | /* bummer, /proc must not be mounted... */ |
64 | return( 0); | 64 | return( 0); |
65 | } | 65 | } |
66 | fscanf(file,"%d.%d.%d",&major,&minor,&patch); | 66 | fscanf(file,"%d.%d.%d",&major,&minor,&patch); |