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, "^<1c:p", TOYFLAG_SBIN))
11 usage: oneit [-p] [-c /dev/tty0] command [...]
13 A simple init program that runs a single supplied command line with a
14 controlling tty (so CTRL-C can kill it).
16 -p Power off instead of rebooting when command exits.
17 -c Which console device to use.
19 The oneit command runs the supplied command line as a child process
20 (because PID 1 has signals blocked), attached to /dev/tty0, in its
21 own session. Then oneit reaps zombies until the child exits, at
22 which point it reboots (or with -p, powers off) the system.
27 #include <sys/reboot.h>
33 // The minimum amount of work necessary to get ctrl-c and such to work is:
35 // - Fork a child (PID 1 is special: can't exit, has various signals blocked).
36 // - Do a setsid() (so we have our own session).
37 // - In the child, attach stdio to /dev/tty0 (/dev/console is special)
38 // - Exec the rest of the command line.
40 // PID 1 then reaps zombies until the child process it spawned exits, at which
41 // point it calls sync() and reboot(). I could stick a kill -1 in there.
49 // Create a new child process.
53 // pid 1 just reaps zombies until it gets its child, then halts the system.
54 while (pid != wait(&i));
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 if (!vfork()) reboot((toys.optflags & FLAG_p) ? RB_POWER_OFF : RB_AUTOBOOT);
64 // Redirect stdio to /dev/tty0, with new session ID, so ctrl-c works.
68 // Remember, O_CLOEXEC is backwards for xopen()
69 xopen(TT.console ? TT.console : "/dev/tty0", O_RDWR|O_CLOEXEC);
72 // Can't xexec() here, because we vforked so we don't want to error_exit().
73 toy_exec(toys.optargs);
74 execvp(*toys.optargs, toys.optargs);