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