| Rob Landley | 2896480 | 2008-01-19 17:08:39 -0600 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: |
| 2 | * |
| 3 | * oneit.c, tiny one-process init replacement. |
| Rob Landley | b23d230 | 2006-11-25 13:48:02 -0500 | [diff] [blame] | 4 | * |
| Rob Landley | fece5cb | 2007-12-03 20:05:57 -0600 | [diff] [blame] | 5 | * Copyright 2005, 2007 by Rob Landley <rob@landley.net>. |
| 6 | * |
| 7 | * Not in SUSv3. |
| Rob Landley | 2896480 | 2008-01-19 17:08:39 -0600 | [diff] [blame] | 8 | |
| Rob Landley | b1487dc | 2008-06-26 22:48:43 -0500 | [diff] [blame] | 9 | USE_ONEIT(NEWTOY(oneit, "^<1c:p", TOYFLAG_SBIN)) |
| Rob Landley | 55928b1 | 2008-01-19 17:43:27 -0600 | [diff] [blame] | 10 | |
| Rob Landley | 2896480 | 2008-01-19 17:08:39 -0600 | [diff] [blame] | 11 | config ONEIT |
| 12 | bool "oneit" |
| 13 | default y |
| 14 | help |
| 15 | usage: oneit [-p] [-c /dev/tty0] command [...] |
| 16 | |
| 17 | A simple init program that runs a single supplied command line with a |
| 18 | controlling tty (so CTRL-C can kill it). |
| 19 | |
| 20 | -p Power off instead of rebooting when command exits. |
| 21 | -c Which console device to use. |
| 22 | |
| 23 | The oneit command runs the supplied command line as a child process |
| 24 | (because PID 1 has signals blocked), attached to /dev/tty0, in its |
| 25 | own session. Then oneit reaps zombies until the child exits, at |
| 26 | which point it reboots (or with -p, powers off) the system. |
| 27 | */ |
| Rob Landley | b23d230 | 2006-11-25 13:48:02 -0500 | [diff] [blame] | 28 | |
| 29 | #include "toys.h" |
| 30 | #include <sys/reboot.h> |
| 31 | |
| Rob Landley | b1aaba1 | 2008-01-20 17:25:44 -0600 | [diff] [blame] | 32 | DEFINE_GLOBALS( |
| 33 | char *console; |
| 34 | ) |
| 35 | |
| 36 | #define TT this.oneit |
| 37 | |
| Rob Landley | b23d230 | 2006-11-25 13:48:02 -0500 | [diff] [blame] | 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 | |
| Rob Landley | a8f2580 | 2007-12-09 15:30:36 -0600 | [diff] [blame] | 48 | |
| Rob Landley | efda21c | 2007-11-29 18:14:37 -0600 | [diff] [blame] | 49 | void oneit_main(void) |
| Rob Landley | b23d230 | 2006-11-25 13:48:02 -0500 | [diff] [blame] | 50 | { |
| 51 | int i; |
| 52 | pid_t pid; |
| 53 | |
| 54 | // Create a new child process. |
| 55 | pid = vfork(); |
| 56 | if (pid) { |
| 57 | |
| 58 | // pid 1 just reaps zombies until it gets its child, then halts the system. |
| 59 | while (pid!=wait(&i)); |
| 60 | sync(); |
| Rob Landley | cca4450 | 2008-12-14 02:12:07 -0600 | [diff] [blame] | 61 | |
| 62 | // PID 1 can't call reboot() because it kills the task that calls it, |
| 63 | // which causes the kernel to panic before the actual reboot happens. |
| 64 | if (!vfork()) reboot((toys.optflags&1) ? RB_POWER_OFF : RB_AUTOBOOT); |
| 65 | sleep(5); |
| 66 | _exit(1); |
| Rob Landley | b23d230 | 2006-11-25 13:48:02 -0500 | [diff] [blame] | 67 | } |
| Rob Landley | 2c22685 | 2007-11-15 18:30:30 -0600 | [diff] [blame] | 68 | |
| Rob Landley | b23d230 | 2006-11-25 13:48:02 -0500 | [diff] [blame] | 69 | // Redirect stdio to /dev/tty0, with new session ID, so ctrl-c works. |
| 70 | setsid(); |
| 71 | for (i=0; i<3; i++) { |
| 72 | close(i); |
| Rob Landley | a8f2580 | 2007-12-09 15:30:36 -0600 | [diff] [blame] | 73 | xopen(TT.console ? TT.console : "/dev/tty0",O_RDWR); |
| Rob Landley | b23d230 | 2006-11-25 13:48:02 -0500 | [diff] [blame] | 74 | } |
| 75 | |
| Rob Landley | efda21c | 2007-11-29 18:14:37 -0600 | [diff] [blame] | 76 | // Can't xexec() here, because we vforked so we don't want to error_exit(). |
| Rob Landley | b23d230 | 2006-11-25 13:48:02 -0500 | [diff] [blame] | 77 | toy_exec(toys.optargs); |
| 78 | execvp(*toys.optargs, toys.optargs); |
| 79 | _exit(127); |
| 80 | } |