Mercurial > hg > toybox
annotate toys/oneit.c @ 226:6aac63925eff
Update web pages.
author | Rob Landley <rob@landley.net> |
---|---|
date | Sat, 05 Jan 2008 18:09:49 -0600 |
parents | 9dc48c97d6f9 |
children | d4176f3f3835 |
rev | line source |
---|---|
34 | 1 /* oneit.c, tiny one-process init replacement. |
2 * | |
194
30a6db5a95c2
Add comments about SUSv3 specs (or lack thereof).
Rob Landley <rob@landley.net>
parents:
186
diff
changeset
|
3 * 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
|
4 * |
30a6db5a95c2
Add comments about SUSv3 specs (or lack thereof).
Rob Landley <rob@landley.net>
parents:
186
diff
changeset
|
5 * Not in SUSv3. |
34 | 6 */ |
7 | |
8 #include "toys.h" | |
9 #include <sys/reboot.h> | |
10 | |
11 // The minimum amount of work necessary to get ctrl-c and such to work is: | |
12 // | |
13 // - Fork a child (PID 1 is special: can't exit, has various signals blocked). | |
14 // - Do a setsid() (so we have our own session). | |
15 // - In the child, attach stdio to /dev/tty0 (/dev/console is special) | |
16 // - Exec the rest of the command line. | |
17 // | |
18 // PID 1 then reaps zombies until the child process it spawned exits, at which | |
19 // point it calls sync() and reboot(). I could stick a kill -1 in there. | |
20 | |
195
9dc48c97d6f9
Add -c option to oneit, to specify console other than /dev/tty0.
Rob Landley <rob@landley.net>
parents:
194
diff
changeset
|
21 #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
|
22 |
186
25447caf1b4b
Change command main() functions to return void, and exit(toys.exitval) from
Rob Landley <rob@landley.net>
parents:
156
diff
changeset
|
23 void oneit_main(void) |
34 | 24 { |
25 int i; | |
26 pid_t pid; | |
27 | |
28 // Create a new child process. | |
29 pid = vfork(); | |
30 if (pid) { | |
31 | |
32 // pid 1 just reaps zombies until it gets its child, then halts the system. | |
33 while (pid!=wait(&i)); | |
34 sync(); | |
35 reboot(toys.optflags ? RB_POWER_OFF : RB_AUTOBOOT); | |
36 } | |
156
1e8f4b05cb65
Remove trailing whitespace (thanks to Charlie Shepherd), and a couple comment
Rob Landley <rob@landley.net>
parents:
34
diff
changeset
|
37 |
34 | 38 // Redirect stdio to /dev/tty0, with new session ID, so ctrl-c works. |
39 setsid(); | |
40 for (i=0; i<3; i++) { | |
41 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
|
42 xopen(TT.console ? TT.console : "/dev/tty0",O_RDWR); |
34 | 43 } |
44 | |
186
25447caf1b4b
Change command main() functions to return void, and exit(toys.exitval) from
Rob Landley <rob@landley.net>
parents:
156
diff
changeset
|
45 // Can't xexec() here, because we vforked so we don't want to error_exit(). |
34 | 46 toy_exec(toys.optargs); |
47 execvp(*toys.optargs, toys.optargs); | |
48 _exit(127); | |
49 } |