diff options
| author | Denis Vlasenko <vda.linux@googlemail.com> | 2007-02-27 19:20:33 +0000 |
|---|---|---|
| committer | Denis Vlasenko <vda.linux@googlemail.com> | 2007-02-27 19:20:33 +0000 |
| commit | 966bb4376665e0cf22e64f7901fb956edbd2f1a9 (patch) | |
| tree | f56f180f61d91368f9af8c49930558d5201551c1 /init | |
| parent | b71675419929a77031559ae5ba6399a0fdc2847b (diff) | |
| download | busybox-w32-966bb4376665e0cf22e64f7901fb956edbd2f1a9.tar.gz busybox-w32-966bb4376665e0cf22e64f7901fb956edbd2f1a9.tar.bz2 busybox-w32-966bb4376665e0cf22e64f7901fb956edbd2f1a9.zip | |
init: de-indent a block of code
Diffstat (limited to 'init')
| -rw-r--r-- | init/init.c | 282 |
1 files changed, 139 insertions, 143 deletions
diff --git a/init/init.c b/init/init.c index bc8979859..e1ad1e615 100644 --- a/init/init.c +++ b/init/init.c | |||
| @@ -316,7 +316,7 @@ static void open_stdio_to_tty(const char* tty_name, int fail) | |||
| 316 | close(0); | 316 | close(0); |
| 317 | if ((device_open(tty_name, O_RDWR)) < 0) { | 317 | if ((device_open(tty_name, O_RDWR)) < 0) { |
| 318 | dup2(1, 0); /* restore fd #0 - avoid nasty surprises */ | 318 | dup2(1, 0); /* restore fd #0 - avoid nasty surprises */ |
| 319 | message(L_LOG | L_CONSOLE, "can't open %s: %s", | 319 | message(L_LOG | L_CONSOLE, "Can't open %s: %s", |
| 320 | tty_name, strerror(errno)); | 320 | tty_name, strerror(errno)); |
| 321 | if (fail) | 321 | if (fail) |
| 322 | _exit(1); | 322 | _exit(1); |
| @@ -342,178 +342,174 @@ static pid_t run(const struct init_action *a) | |||
| 342 | char *cmd[INIT_BUFFS_SIZE]; | 342 | char *cmd[INIT_BUFFS_SIZE]; |
| 343 | char buf[INIT_BUFFS_SIZE + 6]; /* INIT_BUFFS_SIZE+strlen("exec ")+1 */ | 343 | char buf[INIT_BUFFS_SIZE + 6]; /* INIT_BUFFS_SIZE+strlen("exec ")+1 */ |
| 344 | sigset_t nmask, omask; | 344 | sigset_t nmask, omask; |
| 345 | static const char press_enter[] = | ||
| 346 | #ifdef CUSTOMIZED_BANNER | ||
| 347 | #include CUSTOMIZED_BANNER | ||
| 348 | #endif | ||
| 349 | "\nPlease press Enter to activate this console. "; | ||
| 350 | 345 | ||
| 351 | /* Block sigchild while forking. */ | 346 | /* Block sigchild while forking. */ |
| 352 | sigemptyset(&nmask); | 347 | sigemptyset(&nmask); |
| 353 | sigaddset(&nmask, SIGCHLD); | 348 | sigaddset(&nmask, SIGCHLD); |
| 354 | sigprocmask(SIG_BLOCK, &nmask, &omask); | 349 | sigprocmask(SIG_BLOCK, &nmask, &omask); |
| 350 | pid = fork(); | ||
| 351 | sigprocmask(SIG_SETMASK, &omask, NULL); | ||
| 355 | 352 | ||
| 356 | if ((pid = fork()) == 0) { | 353 | if (pid) |
| 357 | /* Clean up */ | 354 | return pid; |
| 358 | sigprocmask(SIG_SETMASK, &omask, NULL); | 355 | |
| 359 | 356 | /* Reset signal handlers that were set by the parent process */ | |
| 360 | /* Reset signal handlers that were set by the parent process */ | 357 | signal(SIGUSR1, SIG_DFL); |
| 361 | signal(SIGUSR1, SIG_DFL); | 358 | signal(SIGUSR2, SIG_DFL); |
| 362 | signal(SIGUSR2, SIG_DFL); | 359 | signal(SIGINT, SIG_DFL); |
| 363 | signal(SIGINT, SIG_DFL); | 360 | signal(SIGTERM, SIG_DFL); |
| 364 | signal(SIGTERM, SIG_DFL); | 361 | signal(SIGHUP, SIG_DFL); |
| 365 | signal(SIGHUP, SIG_DFL); | 362 | signal(SIGQUIT, SIG_DFL); |
| 366 | signal(SIGQUIT, SIG_DFL); | 363 | signal(SIGCONT, SIG_DFL); |
| 367 | signal(SIGCONT, SIG_DFL); | 364 | signal(SIGSTOP, SIG_DFL); |
| 368 | signal(SIGSTOP, SIG_DFL); | 365 | signal(SIGTSTP, SIG_DFL); |
| 369 | signal(SIGTSTP, SIG_DFL); | 366 | |
| 370 | 367 | /* Create a new session and make ourself the process | |
| 371 | /* Create a new session and make ourself the process | 368 | * group leader */ |
| 372 | * group leader */ | 369 | setsid(); |
| 373 | setsid(); | ||
| 374 | 370 | ||
| 375 | /* Open the new terminal device */ | 371 | /* Open the new terminal device */ |
| 376 | open_stdio_to_tty(a->terminal, 1); | 372 | open_stdio_to_tty(a->terminal, 1); |
| 377 | 373 | ||
| 378 | /* If the init Action requires us to wait, then force the | 374 | /* If the init Action requires us to wait, then force the |
| 379 | * supplied terminal to be the controlling tty. */ | 375 | * supplied terminal to be the controlling tty. */ |
| 380 | if (a->action & (SYSINIT | WAIT | CTRLALTDEL | SHUTDOWN | RESTART)) { | 376 | if (a->action & (SYSINIT | WAIT | CTRLALTDEL | SHUTDOWN | RESTART)) { |
| 381 | 377 | ||
| 382 | /* Now fork off another process to just hang around */ | 378 | /* Now fork off another process to just hang around */ |
| 383 | if ((pid = fork()) < 0) { | 379 | if ((pid = fork()) < 0) { |
| 384 | message(L_LOG | L_CONSOLE, "can't fork"); | 380 | message(L_LOG | L_CONSOLE, "Can't fork"); |
| 385 | _exit(1); | 381 | _exit(1); |
| 386 | } | 382 | } |
| 387 | 383 | ||
| 388 | if (pid > 0) { | 384 | if (pid > 0) { |
| 389 | 385 | ||
| 390 | /* We are the parent -- wait till the child is done */ | 386 | /* We are the parent -- wait till the child is done */ |
| 391 | signal(SIGINT, SIG_IGN); | 387 | signal(SIGINT, SIG_IGN); |
| 392 | signal(SIGTSTP, SIG_IGN); | 388 | signal(SIGTSTP, SIG_IGN); |
| 393 | signal(SIGQUIT, SIG_IGN); | 389 | signal(SIGQUIT, SIG_IGN); |
| 394 | signal(SIGCHLD, SIG_DFL); | 390 | signal(SIGCHLD, SIG_DFL); |
| 395 | 391 | ||
| 396 | waitfor(NULL, pid); | 392 | waitfor(NULL, pid); |
| 397 | /* See if stealing the controlling tty back is necessary */ | 393 | /* See if stealing the controlling tty back is necessary */ |
| 398 | if (tcgetpgrp(0) != getpid()) | 394 | if (tcgetpgrp(0) != getpid()) |
| 399 | _exit(0); | 395 | _exit(0); |
| 400 | 396 | ||
| 401 | /* Use a temporary process to steal the controlling tty. */ | 397 | /* Use a temporary process to steal the controlling tty. */ |
| 402 | if ((pid = fork()) < 0) { | 398 | if ((pid = fork()) < 0) { |
| 403 | message(L_LOG | L_CONSOLE, "can't fork"); | 399 | message(L_LOG | L_CONSOLE, "Can't fork"); |
| 404 | _exit(1); | 400 | _exit(1); |
| 405 | } | 401 | } |
| 406 | if (pid == 0) { | 402 | if (pid == 0) { |
| 407 | setsid(); | 403 | setsid(); |
| 408 | ioctl(0, TIOCSCTTY, 1); | 404 | ioctl(0, TIOCSCTTY, 1); |
| 409 | _exit(0); | ||
| 410 | } | ||
| 411 | waitfor(NULL, pid); | ||
| 412 | _exit(0); | 405 | _exit(0); |
| 413 | } | 406 | } |
| 414 | 407 | waitfor(NULL, pid); | |
| 415 | /* Now fall though to actually execute things */ | 408 | _exit(0); |
| 416 | } | 409 | } |
| 417 | 410 | ||
| 418 | /* See if any special /bin/sh requiring characters are present */ | 411 | /* Now fall though to actually execute things */ |
| 419 | if (strpbrk(a->command, "~`!$^&*()=|\\{}[];\"'<>?") != NULL) { | 412 | } |
| 420 | cmd[0] = (char *)DEFAULT_SHELL; | 413 | |
| 421 | cmd[1] = (char*)"-c"; | 414 | /* See if any special /bin/sh requiring characters are present */ |
| 422 | cmd[2] = strcat(strcpy(buf, "exec "), a->command); | 415 | if (strpbrk(a->command, "~`!$^&*()=|\\{}[];\"'<>?") != NULL) { |
| 423 | cmd[3] = NULL; | 416 | cmd[0] = (char*)DEFAULT_SHELL; |
| 424 | } else { | 417 | cmd[1] = (char*)"-c"; |
| 425 | /* Convert command (char*) into cmd (char**, one word per string) */ | 418 | cmd[2] = strcat(strcpy(buf, "exec "), a->command); |
| 426 | strcpy(buf, a->command); | 419 | cmd[3] = NULL; |
| 427 | s = buf; | 420 | } else { |
| 428 | for (tmpCmd = buf, i = 0; (tmpCmd = strsep(&s, " \t")) != NULL;) { | 421 | /* Convert command (char*) into cmd (char**, one word per string) */ |
| 429 | if (*tmpCmd != '\0') { | 422 | strcpy(buf, a->command); |
| 430 | cmd[i] = tmpCmd; | 423 | s = buf; |
| 431 | i++; | 424 | for (tmpCmd = buf, i = 0; (tmpCmd = strsep(&s, " \t")) != NULL;) { |
| 432 | } | 425 | if (*tmpCmd != '\0') { |
| 426 | cmd[i] = tmpCmd; | ||
| 427 | i++; | ||
| 433 | } | 428 | } |
| 434 | cmd[i] = NULL; | ||
| 435 | } | 429 | } |
| 430 | cmd[i] = NULL; | ||
| 431 | } | ||
| 436 | 432 | ||
| 437 | cmdpath = cmd[0]; | 433 | cmdpath = cmd[0]; |
| 438 | 434 | ||
| 439 | /* | 435 | /* |
| 440 | * Interactive shells want to see a dash in argv[0]. This | 436 | * Interactive shells want to see a dash in argv[0]. This |
| 441 | * typically is handled by login, argv will be setup this | 437 | * typically is handled by login, argv will be setup this |
| 442 | * way if a dash appears at the front of the command path | 438 | * way if a dash appears at the front of the command path |
| 443 | * (like "-/bin/sh"). | 439 | * (like "-/bin/sh"). |
| 444 | */ | 440 | */ |
| 445 | if (*cmdpath == '-') { | 441 | if (*cmdpath == '-') { |
| 446 | /* skip over the dash */ | 442 | /* skip over the dash */ |
| 447 | ++cmdpath; | 443 | ++cmdpath; |
| 448 | 444 | ||
| 449 | /* find the last component in the command pathname */ | 445 | /* find the last component in the command pathname */ |
| 450 | s = bb_get_last_path_component(cmdpath); | 446 | s = bb_get_last_path_component(cmdpath); |
| 451 | 447 | ||
| 452 | /* make a new argv[0] */ | 448 | /* make a new argv[0] */ |
| 453 | if ((cmd[0] = malloc(strlen(s) + 2)) == NULL) { | 449 | if ((cmd[0] = malloc(strlen(s) + 2)) == NULL) { |
| 454 | message(L_LOG | L_CONSOLE, bb_msg_memory_exhausted); | 450 | message(L_LOG | L_CONSOLE, bb_msg_memory_exhausted); |
| 455 | cmd[0] = cmdpath; | 451 | cmd[0] = cmdpath; |
| 456 | } else { | 452 | } else { |
| 457 | cmd[0][0] = '-'; | 453 | cmd[0][0] = '-'; |
| 458 | strcpy(cmd[0] + 1, s); | 454 | strcpy(cmd[0] + 1, s); |
| 459 | } | 455 | } |
| 460 | #if ENABLE_FEATURE_INIT_SCTTY | 456 | #if ENABLE_FEATURE_INIT_SCTTY |
| 461 | /* Establish this process as session leader and | 457 | /* Establish this process as session leader and |
| 462 | * (attempt) to make the tty (if any) a controlling tty. | 458 | * (attempt) to make the tty (if any) a controlling tty. |
| 463 | */ | 459 | */ |
| 464 | setsid(); | 460 | setsid(); |
| 465 | ioctl(0, TIOCSCTTY, 0 /*don't steal it*/); | 461 | ioctl(0, TIOCSCTTY, 0 /*don't steal it*/); |
| 466 | #endif | 462 | #endif |
| 467 | } | 463 | } |
| 468 | 464 | ||
| 469 | #if !defined(__UCLIBC__) || defined(__ARCH_HAS_MMU__) | 465 | #if !defined(__UCLIBC__) || defined(__ARCH_HAS_MMU__) |
| 470 | if (a->action & ASKFIRST) { | 466 | if (a->action & ASKFIRST) { |
| 471 | char c; | 467 | static const char press_enter[] = |
| 472 | /* | 468 | #ifdef CUSTOMIZED_BANNER |
| 473 | * Save memory by not exec-ing anything large (like a shell) | 469 | #include CUSTOMIZED_BANNER |
| 474 | * before the user wants it. This is critical if swap is not | ||
| 475 | * enabled and the system has low memory. Generally this will | ||
| 476 | * be run on the second virtual console, and the first will | ||
| 477 | * be allowed to start a shell or whatever an init script | ||
| 478 | * specifies. | ||
| 479 | */ | ||
| 480 | messageD(L_LOG, "Waiting for enter to start '%s'" | ||
| 481 | "(pid %d, tty '%s')\n", | ||
| 482 | cmdpath, getpid(), a->terminal); | ||
| 483 | full_write(1, press_enter, sizeof(press_enter) - 1); | ||
| 484 | while (read(0, &c, 1) == 1 && c != '\n') | ||
| 485 | ; | ||
| 486 | } | ||
| 487 | #endif | 470 | #endif |
| 488 | 471 | "\nPlease press Enter to activate this console. "; | |
| 489 | /* Log the process name and args */ | 472 | char c; |
| 490 | message(L_LOG, "starting pid %d, tty '%s': '%s'", | 473 | /* |
| 491 | getpid(), a->terminal, cmdpath); | 474 | * Save memory by not exec-ing anything large (like a shell) |
| 475 | * before the user wants it. This is critical if swap is not | ||
| 476 | * enabled and the system has low memory. Generally this will | ||
| 477 | * be run on the second virtual console, and the first will | ||
| 478 | * be allowed to start a shell or whatever an init script | ||
| 479 | * specifies. | ||
| 480 | */ | ||
| 481 | messageD(L_LOG, "waiting for enter to start '%s'" | ||
| 482 | "(pid %d, tty '%s')\n", | ||
| 483 | cmdpath, getpid(), a->terminal); | ||
| 484 | full_write(1, press_enter, sizeof(press_enter) - 1); | ||
| 485 | while (read(0, &c, 1) == 1 && c != '\n') | ||
| 486 | ; | ||
| 487 | } | ||
| 488 | #endif | ||
| 489 | /* Log the process name and args */ | ||
| 490 | message(L_LOG, "starting pid %d, tty '%s': '%s'", | ||
| 491 | getpid(), a->terminal, cmdpath); | ||
| 492 | 492 | ||
| 493 | #if ENABLE_FEATURE_INIT_COREDUMPS | 493 | #if ENABLE_FEATURE_INIT_COREDUMPS |
| 494 | { | 494 | { |
| 495 | struct stat sb; | 495 | struct stat sb; |
| 496 | if (stat(CORE_ENABLE_FLAG_FILE, &sb) == 0) { | 496 | if (stat(CORE_ENABLE_FLAG_FILE, &sb) == 0) { |
| 497 | struct rlimit limit; | 497 | struct rlimit limit; |
| 498 | 498 | ||
| 499 | limit.rlim_cur = RLIM_INFINITY; | 499 | limit.rlim_cur = RLIM_INFINITY; |
| 500 | limit.rlim_max = RLIM_INFINITY; | 500 | limit.rlim_max = RLIM_INFINITY; |
| 501 | setrlimit(RLIMIT_CORE, &limit); | 501 | setrlimit(RLIMIT_CORE, &limit); |
| 502 | } | ||
| 503 | } | 502 | } |
| 504 | #endif | ||
| 505 | |||
| 506 | /* Now run it. The new program will take over this PID, | ||
| 507 | * so nothing further in init.c should be run. */ | ||
| 508 | BB_EXECVP(cmdpath, cmd); | ||
| 509 | |||
| 510 | /* We're still here? Some error happened. */ | ||
| 511 | message(L_LOG | L_CONSOLE, "Cannot run '%s': %s", | ||
| 512 | cmdpath, strerror(errno)); | ||
| 513 | _exit(-1); | ||
| 514 | } | 503 | } |
| 515 | sigprocmask(SIG_SETMASK, &omask, NULL); | 504 | #endif |
| 516 | return pid; | 505 | /* Now run it. The new program will take over this PID, |
| 506 | * so nothing further in init.c should be run. */ | ||
| 507 | BB_EXECVP(cmdpath, cmd); | ||
| 508 | |||
| 509 | /* We're still here? Some error happened. */ | ||
| 510 | message(L_LOG | L_CONSOLE, "Cannot run '%s': %s", | ||
| 511 | cmdpath, strerror(errno)); | ||
| 512 | _exit(-1); | ||
| 517 | } | 513 | } |
| 518 | 514 | ||
| 519 | static int waitfor(const struct init_action *a, pid_t pid) | 515 | static int waitfor(const struct init_action *a, pid_t pid) |
