• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 [-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 
GLOBALS(char * c;)35 GLOBALS(
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.)
50 static 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 
oneit_main(void)65 void oneit_main(void)
66 {
67   int i, pid, pipes[] = {SIGUSR1, SIGUSR2, SIGTERM, SIGINT};
68   char *ss = toybuf+5;
69 
70   // Setup signal handlers for signals of interest
71   for (i = 0; i<ARRAY_LEN(pipes); i++) xsignal(pipes[i], oneit_signaled);
72 
73   // Autodetect console from sysfs if no -c
74   memcpy(toybuf, "/dev/", 5);
75   i = sizeof(toybuf)-6;
76   if (!TT.c && (TT.c = readfile("/sys/class/tty/console/active", ss, i))) {
77     // Take last entry, remove newline terminator
78     while (TT.c[i = strcspn(TT.c, " \n")]) {
79       TT.c[i++] = 0;
80       if (TT.c[i]) TT.c += i;
81       else break;
82     }
83     // Ensure exactly one /dev prefix
84     strstart(&TT.c, "/dev/");
85     memmove(toybuf+5, TT.c, strlen(TT.c));
86     TT.c = toybuf;
87   }
88 
89   // Redirect stdin/out/err. Remember, O_CLOEXEC is backwards for xopen()
90   close(0);
91   xopen_stdio(TT.c ? : "/dev/tty0", O_RDWR|O_CLOEXEC);
92   close(1);
93   dup(0);
94   close(2);
95   dup(0);
96 
97   if (FLAG(3)) {
98     // Ensure next available filehandles are #3 and #4
99     close(3);
100     close(4);
101     xpipe(pipes);
102     fcntl(4, F_SETFD, FD_CLOEXEC);
103   }
104 
105   while (!toys.signal) {
106 
107     // Create a new child process.
108     pid = XVFORK();
109     if (pid) {
110 
111       // pid 1 reaps zombies until it gets its child, then halts system.
112       // We ignore the return value of write (what would we do with it?)
113       // but save it in a variable we never read to make fortify shut up.
114       // (Real problem is if pid2 never reads, write() fills pipe and blocks.)
115       while (pid != wait(&i)) if (FLAG(3)) i = write(4, &pid, 4);
116       if (FLAG(n)) continue;
117 
118       oneit_signaled(FLAG(p) ? SIGUSR2 : SIGTERM);
119     } else {
120       // new session ID in child, so ctrl-c works.
121       setsid();
122 
123       // Can't xexec() here, we vforked so we don't want to error_exit().
124       toy_exec(toys.optargs);
125       execvp(*toys.optargs, toys.optargs);
126       perror_msg("%s not in PATH=%s", *toys.optargs, getenv("PATH"));
127 
128       break;
129     }
130   }
131 
132   // Give reboot() time to kick in, or avoid rapid spinning if exec failed
133   sleep(5);
134   _exit(127);
135 }
136