3 * oneit.c, tiny one-process init replacement.
5 * Copyright 2005, 2007 by Rob Landley <rob@landley.net>.
9 USE_ONEIT(NEWTOY(oneit, "^<1c:p", TOYFLAG_SBIN))
15 usage: oneit [-p] [-c /dev/tty0] command [...]
17 A simple init program that runs a single supplied command line with a
18 controlling tty (so CTRL-C can kill it).
20 -p Power off instead of rebooting when command exits.
21 -c Which console device to use.
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.
30 #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.
54 // Create a new child process.
58 // pid 1 just reaps zombies until it gets its child, then halts the system.
59 while (pid!=wait(&i));
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);
69 // Redirect stdio to /dev/tty0, with new session ID, so ctrl-c works.
73 xopen(TT.console ? TT.console : "/dev/tty0",O_RDWR);
76 // Can't xexec() here, because we vforked so we don't want to error_exit().
77 toy_exec(toys.optargs);
78 execvp(*toys.optargs, toys.optargs);