1 /* oneit.c - tiny init replacement to launch a single child process.
3 * Copyright 2005, 2007 by Rob Landley <rob@landley.net>.
5 USE_ONEIT(NEWTOY(oneit, "^<1nc:p3[!pn]", TOYFLAG_SBIN))
11 usage: oneit [-p] [-c /dev/tty0] command [...]
13 Simple init program that runs a single supplied command line with a
14 controlling tty (so CTRL-C can kill it).
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.)
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).
26 Responds to SIGUSR1 by halting the system, SIGUSR2 by powering off,
27 and SIGTERM or SIGINT reboot.
32 #include <sys/reboot.h>
38 // The minimum amount of work necessary to get ctrl-c and such to work is:
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.
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.
48 // Perform actions in response to signals. (Only root can send us signals.)
49 static void oneit_signaled(int signal)
51 int action = RB_AUTOBOOT;
54 if (signal == SIGUSR1) action = RB_HALT_SYSTEM;
55 if (signal == SIGUSR2) action = RB_POWER_OFF;
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.
60 if (!vfork()) reboot(action);
65 int i, pid, pipes[] = {SIGUSR1, SIGUSR2, SIGTERM, SIGINT};
67 // Setup signal handlers for signals of interest
68 for (i = 0; i<ARRAY_LEN(pipes); i++) xsignal(pipes[i], oneit_signaled);
70 if (toys.optflags & FLAG_3) {
71 // Ensure next available filehandles are #3 and #4
72 while (xopen_stdio("/", 0) < 3);
76 fcntl(4, F_SETFD, FD_CLOEXEC);
79 while (!toys.signal) {
81 // Create a new child process.
85 // pid 1 reaps zombies until it gets its child, then halts system.
86 // We ignore the return value of write (what would we do with it?)
87 // but save it in a variable we never read to make fortify shut up.
88 // (Real problem is if pid2 never reads, write() fills pipe and blocks.)
89 while (pid != wait(&i)) if (toys.optflags & FLAG_3) i = write(4, &pid, 4);
90 if (toys.optflags & FLAG_n) continue;
92 oneit_signaled((toys.optflags & FLAG_p) ? SIGUSR2 : SIGTERM);
94 // Redirect stdio to /dev/tty0, with new session ID, so ctrl-c works.
98 // Remember, O_CLOEXEC is backwards for xopen()
99 xopen(TT.console ? TT.console : "/dev/tty0", O_RDWR|O_CLOEXEC);
102 // Can't xexec() here, we vforked so we don't want to error_exit().
103 toy_exec(toys.optargs);
104 execvp(*toys.optargs, toys.optargs);
105 perror_msg("%s not in PATH=%s", *toys.optargs, getenv("PATH"));
111 // Give reboot() time to kick in, or avoid rapid spinning if exec failed