Mercurial > hg > toybox
annotate toys/oneit.c @ 233:d4176f3f3835
Zap toys/Config.in and instead create generated/Config.in from contents of
toys/*.c. Move relevant info into comment at the top of each toys/*.c. Also
convert more of Makefile into a thin wrapper around shell scripts that actually
do the work. (Makefile is only still there for the user interface.)
author | Rob Landley <rob@landley.net> |
---|---|
date | Sat, 19 Jan 2008 17:08:39 -0600 |
parents | 9dc48c97d6f9 |
children | 163498bf547b |
rev | line source |
---|---|
233
d4176f3f3835
Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents:
195
diff
changeset
|
1 /* vi: set sw=4 ts=4: |
d4176f3f3835
Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents:
195
diff
changeset
|
2 * |
d4176f3f3835
Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents:
195
diff
changeset
|
3 * oneit.c, tiny one-process init replacement. |
34 | 4 * |
194
30a6db5a95c2
Add comments about SUSv3 specs (or lack thereof).
Rob Landley <rob@landley.net>
parents:
186
diff
changeset
|
5 * Copyright 2005, 2007 by Rob Landley <rob@landley.net>. |
30a6db5a95c2
Add comments about SUSv3 specs (or lack thereof).
Rob Landley <rob@landley.net>
parents:
186
diff
changeset
|
6 * |
30a6db5a95c2
Add comments about SUSv3 specs (or lack thereof).
Rob Landley <rob@landley.net>
parents:
186
diff
changeset
|
7 * Not in SUSv3. |
233
d4176f3f3835
Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents:
195
diff
changeset
|
8 |
d4176f3f3835
Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents:
195
diff
changeset
|
9 config ONEIT |
d4176f3f3835
Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents:
195
diff
changeset
|
10 bool "oneit" |
d4176f3f3835
Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents:
195
diff
changeset
|
11 default y |
d4176f3f3835
Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents:
195
diff
changeset
|
12 help |
d4176f3f3835
Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents:
195
diff
changeset
|
13 usage: oneit [-p] [-c /dev/tty0] command [...] |
d4176f3f3835
Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents:
195
diff
changeset
|
14 |
d4176f3f3835
Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents:
195
diff
changeset
|
15 A simple init program that runs a single supplied command line with a |
d4176f3f3835
Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents:
195
diff
changeset
|
16 controlling tty (so CTRL-C can kill it). |
d4176f3f3835
Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents:
195
diff
changeset
|
17 |
d4176f3f3835
Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents:
195
diff
changeset
|
18 -p Power off instead of rebooting when command exits. |
d4176f3f3835
Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents:
195
diff
changeset
|
19 -c Which console device to use. |
d4176f3f3835
Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents:
195
diff
changeset
|
20 |
d4176f3f3835
Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents:
195
diff
changeset
|
21 The oneit command runs the supplied command line as a child process |
d4176f3f3835
Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents:
195
diff
changeset
|
22 (because PID 1 has signals blocked), attached to /dev/tty0, in its |
d4176f3f3835
Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents:
195
diff
changeset
|
23 own session. Then oneit reaps zombies until the child exits, at |
d4176f3f3835
Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents:
195
diff
changeset
|
24 which point it reboots (or with -p, powers off) the system. |
d4176f3f3835
Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents:
195
diff
changeset
|
25 */ |
34 | 26 |
27 #include "toys.h" | |
28 #include <sys/reboot.h> | |
29 | |
30 // The minimum amount of work necessary to get ctrl-c and such to work is: | |
31 // | |
32 // - Fork a child (PID 1 is special: can't exit, has various signals blocked). | |
33 // - Do a setsid() (so we have our own session). | |
34 // - In the child, attach stdio to /dev/tty0 (/dev/console is special) | |
35 // - Exec the rest of the command line. | |
36 // | |
37 // PID 1 then reaps zombies until the child process it spawned exits, at which | |
38 // point it calls sync() and reboot(). I could stick a kill -1 in there. | |
39 | |
195
9dc48c97d6f9
Add -c option to oneit, to specify console other than /dev/tty0.
Rob Landley <rob@landley.net>
parents:
194
diff
changeset
|
40 #define TT toy.oneit |
9dc48c97d6f9
Add -c option to oneit, to specify console other than /dev/tty0.
Rob Landley <rob@landley.net>
parents:
194
diff
changeset
|
41 |
186
25447caf1b4b
Change command main() functions to return void, and exit(toys.exitval) from
Rob Landley <rob@landley.net>
parents:
156
diff
changeset
|
42 void oneit_main(void) |
34 | 43 { |
44 int i; | |
45 pid_t pid; | |
46 | |
47 // Create a new child process. | |
48 pid = vfork(); | |
49 if (pid) { | |
50 | |
51 // pid 1 just reaps zombies until it gets its child, then halts the system. | |
52 while (pid!=wait(&i)); | |
53 sync(); | |
54 reboot(toys.optflags ? RB_POWER_OFF : RB_AUTOBOOT); | |
55 } | |
156
1e8f4b05cb65
Remove trailing whitespace (thanks to Charlie Shepherd), and a couple comment
Rob Landley <rob@landley.net>
parents:
34
diff
changeset
|
56 |
34 | 57 // Redirect stdio to /dev/tty0, with new session ID, so ctrl-c works. |
58 setsid(); | |
59 for (i=0; i<3; i++) { | |
60 close(i); | |
195
9dc48c97d6f9
Add -c option to oneit, to specify console other than /dev/tty0.
Rob Landley <rob@landley.net>
parents:
194
diff
changeset
|
61 xopen(TT.console ? TT.console : "/dev/tty0",O_RDWR); |
34 | 62 } |
63 | |
186
25447caf1b4b
Change command main() functions to return void, and exit(toys.exitval) from
Rob Landley <rob@landley.net>
parents:
156
diff
changeset
|
64 // Can't xexec() here, because we vforked so we don't want to error_exit(). |
34 | 65 toy_exec(toys.optargs); |
66 execvp(*toys.optargs, toys.optargs); | |
67 _exit(127); | |
68 } |