comparison toys/other/oneit.c @ 653:2986aa63a021

Move commands into "posix", "lsb", and "other" menus/directories.
author Rob Landley <rob@landley.net>
date Sat, 25 Aug 2012 14:25:22 -0500
parents toys/oneit.c@2a003d86d805
children 6df4ccc0acbe
comparison
equal deleted inserted replaced
652:2d7c56913fda 653:2986aa63a021
1 /* vi: set sw=4 ts=4:
2 *
3 * oneit.c, tiny one-process init replacement.
4 *
5 * Copyright 2005, 2007 by Rob Landley <rob@landley.net>.
6 *
7 * Not in SUSv3.
8
9 USE_ONEIT(NEWTOY(oneit, "^<1c:p", TOYFLAG_SBIN))
10
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 */
28
29 #include "toys.h"
30 #include <sys/reboot.h>
31
32 DEFINE_GLOBALS(
33 char *console;
34 )
35
36 #define TT this.oneit
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
49 void oneit_main(void)
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();
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);
67 }
68
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);
73 xopen(TT.console ? TT.console : "/dev/tty0",O_RDWR);
74 }
75
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);
79 _exit(127);
80 }