toybox/toys/other/oneit.c
<<
>>
Prefs
   1/* oneit.c - tiny init replacement to launch a single child process.
   2 *
   3 * Copyright 2005, 2007 by Rob Landley <rob@landley.net>.
   4
   5USE_ONEIT(NEWTOY(oneit, "^<1nc:p3[!pn]", TOYFLAG_SBIN))
   6
   7config ONEIT
   8  bool "oneit"
   9  default y
  10  help
  11    usage: oneit [-prn3] [-c CONSOLE] [COMMAND...]
  12
  13    Simple init program that runs a single supplied command line with a
  14    controlling tty (so CTRL-C can kill it).
  15
  16    -c  Which console device to use (/dev/console doesn't do CTRL-C, etc)
  17    -p  Power off instead of rebooting when command exits
  18    -r  Restart child when it exits
  19    -n  No reboot, just relaunch command line
  20    -3  Write 32 bit PID of each exiting reparented process to fd 3 of child
  21        (Blocking writes, child must read to avoid eventual deadlock.)
  22
  23    Spawns a single child process (because PID 1 has signals blocked)
  24    in its own session, reaps zombies until the child exits, then
  25    reboots the system (or powers off with -p, or restarts the child with -r).
  26
  27    Responds to SIGUSR1 by halting the system, SIGUSR2 by powering off,
  28    and SIGTERM or SIGINT reboot.
  29*/
  30
  31#define FOR_oneit
  32#include "toys.h"
  33#include <sys/reboot.h>
  34
  35GLOBALS(
  36  char *c;
  37)
  38
  39// The minimum amount of work necessary to get ctrl-c and such to work is:
  40//
  41// - Fork a child (PID 1 is special: can't exit, has various signals blocked).
  42// - Do a setsid() (so we have our own session).
  43// - In the child, attach stdio to TT.c (/dev/console is special)
  44// - Exec the rest of the command line.
  45//
  46// PID 1 then reaps zombies until the child process it spawned exits, at which
  47// point it calls sync() and reboot().  I could stick a kill -1 in there.
  48
  49// Perform actions in response to signals. (Only root can send us signals.)
  50static void oneit_signaled(int signal)
  51{
  52  int action = RB_AUTOBOOT;
  53
  54  toys.signal = signal;
  55  if (signal == SIGUSR1) action = RB_HALT_SYSTEM;
  56  if (signal == SIGUSR2) action = RB_POWER_OFF;
  57
  58  // PID 1 can't call reboot() because it kills the task that calls it,
  59  // which causes the kernel to panic before the actual reboot happens.
  60  sync();
  61  if (getpid()!=1) _exit(127+signal);
  62  if (!vfork()) reboot(action);
  63}
  64
  65void oneit_main(void)
  66{
  67  int i, pid, pipes[] = {SIGUSR1, SIGUSR2, SIGTERM, SIGINT};
  68
  69  // Setup signal handlers for signals of interest
  70  for (i = 0; i<ARRAY_LEN(pipes); i++) xsignal(pipes[i], oneit_signaled);
  71
  72  if (FLAG(3)) {
  73    // Ensure next available filehandles are #3 and #4
  74    while (xopen_stdio("/", 0) < 3);
  75    close(3);
  76    close(4);
  77    xpipe(pipes);
  78    fcntl(4, F_SETFD, FD_CLOEXEC);
  79  }
  80
  81  while (!toys.signal) {
  82
  83    // Create a new child process.
  84    pid = XVFORK();
  85    if (pid) {
  86
  87      // pid 1 reaps zombies until it gets its child, then halts system.
  88      // We ignore the return value of write (what would we do with it?)
  89      // but save it in a variable we never read to make fortify shut up.
  90      // (Real problem is if pid2 never reads, write() fills pipe and blocks.)
  91      while (pid != wait(&i)) if (FLAG(3)) i = write(4, &pid, 4);
  92      if (FLAG(n)) continue;
  93
  94      oneit_signaled(FLAG(p) ? SIGUSR2 : SIGTERM);
  95    } else {
  96      // Redirect stdio to TT.c, with new session ID, so ctrl-c works.
  97      setsid();
  98      for (i=0; i<3; i++) {
  99        close(i);
 100        // Remember, O_CLOEXEC is backwards for xopen()
 101        xopen_stdio(TT.c ? : "/dev/tty0", O_RDWR|O_CLOEXEC);
 102      }
 103
 104      // Can't xexec() here, we vforked so we don't want to error_exit().
 105      toy_exec(toys.optargs);
 106      execvp(*toys.optargs, toys.optargs);
 107      perror_msg("%s not in PATH=%s", *toys.optargs, getenv("PATH"));
 108
 109      break;
 110    }
 111  }
 112
 113  // Give reboot() time to kick in, or avoid rapid spinning if exec failed
 114  sleep(5);
 115  _exit(127);
 116}
 117