oneit.c 3.6 KB

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