Mercurial > hg > toybox
annotate toys/oneit.c @ 650:dd82e0b28eda
Fix bug spotted by Avery Pennarun: getusername() and getgroupname() can reuse the utoa buffer when neither is recognized, meaning uid would be shown again instead of gid.
author | Rob Landley <rob@landley.net> |
---|---|
date | Sat, 18 Aug 2012 21:12:02 -0500 |
parents | 2a003d86d805 |
children |
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 |
304
93223118c813
Option parsing: stopearly is now a ^ prefix (not +), and an option string with
Rob Landley <rob@landley.net>
parents:
237
diff
changeset
|
9 USE_ONEIT(NEWTOY(oneit, "^<1c:p", TOYFLAG_SBIN)) |
234
163498bf547b
Move NEWTOY() list from end of toylist.h to generated/newtoys.h.
Rob Landley <rob@landley.net>
parents:
233
diff
changeset
|
10 |
233
d4176f3f3835
Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents:
195
diff
changeset
|
11 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
|
12 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
|
13 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
|
14 help |
d4176f3f3835
Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents:
195
diff
changeset
|
15 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
|
16 |
d4176f3f3835
Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents:
195
diff
changeset
|
17 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
|
18 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
|
19 |
d4176f3f3835
Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents:
195
diff
changeset
|
20 -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
|
21 -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
|
22 |
d4176f3f3835
Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents:
195
diff
changeset
|
23 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
|
24 (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
|
25 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
|
26 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
|
27 */ |
34 | 28 |
29 #include "toys.h" | |
30 #include <sys/reboot.h> | |
31 | |
237
7cb15eae1664
Zap toylist.h, moving contents of global structures into DEFINE_GLOBALS()
Rob Landley <rob@landley.net>
parents:
234
diff
changeset
|
32 DEFINE_GLOBALS( |
7cb15eae1664
Zap toylist.h, moving contents of global structures into DEFINE_GLOBALS()
Rob Landley <rob@landley.net>
parents:
234
diff
changeset
|
33 char *console; |
7cb15eae1664
Zap toylist.h, moving contents of global structures into DEFINE_GLOBALS()
Rob Landley <rob@landley.net>
parents:
234
diff
changeset
|
34 ) |
7cb15eae1664
Zap toylist.h, moving contents of global structures into DEFINE_GLOBALS()
Rob Landley <rob@landley.net>
parents:
234
diff
changeset
|
35 |
7cb15eae1664
Zap toylist.h, moving contents of global structures into DEFINE_GLOBALS()
Rob Landley <rob@landley.net>
parents:
234
diff
changeset
|
36 #define TT this.oneit |
7cb15eae1664
Zap toylist.h, moving contents of global structures into DEFINE_GLOBALS()
Rob Landley <rob@landley.net>
parents:
234
diff
changeset
|
37 |
34 | 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 | |
195
9dc48c97d6f9
Add -c option to oneit, to specify console other than /dev/tty0.
Rob Landley <rob@landley.net>
parents:
194
diff
changeset
|
48 |
186
25447caf1b4b
Change command main() functions to return void, and exit(toys.exitval) from
Rob Landley <rob@landley.net>
parents:
156
diff
changeset
|
49 void oneit_main(void) |
34 | 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(); | |
330
2a003d86d805
Teach oneit to reboot on exit more reliably.
Rob Landley <rob@landley.net>
parents:
304
diff
changeset
|
61 |
2a003d86d805
Teach oneit to reboot on exit more reliably.
Rob Landley <rob@landley.net>
parents:
304
diff
changeset
|
62 // PID 1 can't call reboot() because it kills the task that calls it, |
2a003d86d805
Teach oneit to reboot on exit more reliably.
Rob Landley <rob@landley.net>
parents:
304
diff
changeset
|
63 // which causes the kernel to panic before the actual reboot happens. |
2a003d86d805
Teach oneit to reboot on exit more reliably.
Rob Landley <rob@landley.net>
parents:
304
diff
changeset
|
64 if (!vfork()) reboot((toys.optflags&1) ? RB_POWER_OFF : RB_AUTOBOOT); |
2a003d86d805
Teach oneit to reboot on exit more reliably.
Rob Landley <rob@landley.net>
parents:
304
diff
changeset
|
65 sleep(5); |
2a003d86d805
Teach oneit to reboot on exit more reliably.
Rob Landley <rob@landley.net>
parents:
304
diff
changeset
|
66 _exit(1); |
34 | 67 } |
156
1e8f4b05cb65
Remove trailing whitespace (thanks to Charlie Shepherd), and a couple comment
Rob Landley <rob@landley.net>
parents:
34
diff
changeset
|
68 |
34 | 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); | |
195
9dc48c97d6f9
Add -c option to oneit, to specify console other than /dev/tty0.
Rob Landley <rob@landley.net>
parents:
194
diff
changeset
|
73 xopen(TT.console ? TT.console : "/dev/tty0",O_RDWR); |
34 | 74 } |
75 | |
186
25447caf1b4b
Change command main() functions to return void, and exit(toys.exitval) from
Rob Landley <rob@landley.net>
parents:
156
diff
changeset
|
76 // Can't xexec() here, because we vforked so we don't want to error_exit(). |
34 | 77 toy_exec(toys.optargs); |
78 execvp(*toys.optargs, toys.optargs); | |
79 _exit(127); | |
80 } |