OSDN Git Service

Merge tag 'android-7.1.2_r2' into cm-14.1
[android-x86/external-toybox.git] / toys / other / oneit.c
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
34 GLOBALS(
35   char *console;
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 (!vfork()) reboot(action);
61 }
62
63 void oneit_main(void)
64 {
65   int i, pid, pipes[] = {SIGUSR1, SIGUSR2, SIGTERM, SIGINT};
66
67   // Setup signal handlers for signals of interest
68   for (i = 0; i<ARRAY_LEN(pipes); i++) xsignal(pipes[i], oneit_signaled);
69
70   if (toys.optflags & FLAG_3) {
71     // Ensure next available filehandles are #3 and #4
72     while (xopen_stdio("/", 0) < 3);
73     close(3);
74     close(4);
75     xpipe(pipes);
76     fcntl(4, F_SETFD, FD_CLOEXEC);
77   }
78
79   while (!toys.signal) {
80
81     // Create a new child process.
82     pid = vfork();
83     if (pid) {
84
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;
91
92       oneit_signaled((toys.optflags & FLAG_p) ? SIGUSR2 : SIGTERM);
93     } else {
94       // Redirect stdio to /dev/tty0, with new session ID, so ctrl-c works.
95       setsid();
96       for (i=0; i<3; i++) {
97         close(i);
98         // Remember, O_CLOEXEC is backwards for xopen()
99         xopen(TT.console ? TT.console : "/dev/tty0", O_RDWR|O_CLOEXEC);
100       }
101
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"));
106
107       break;
108     }
109   }
110
111   // Give reboot() time to kick in, or avoid rapid spinning if exec failed
112   sleep(5);
113   _exit(127);
114 }