Mercurial > hg > toybox
annotate toys/other/oneit.c @ 1539:3e85af1f7e22 draft
First batch of sed tests.
Only good for TEST_HOST=1 at the moment because the test infrastructure itself
depends on sed, so if an unfinished sed is in the $PATH it goes boing. But
hey, corner cases!
I have... more.
author | Rob Landley <rob@landley.net> |
---|---|
date | Wed, 29 Oct 2014 18:44:33 -0500 |
parents | 8c78a7e5486d |
children | b2b2d214727a |
rev | line source |
---|---|
694
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
674
diff
changeset
|
1 /* oneit.c - tiny init replacement to launch a single child process. |
34 | 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>. |
233
d4176f3f3835
Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents:
195
diff
changeset
|
4 |
304
93223118c813
Option parsing: stopearly is now a ^ prefix (not +), and an option string with
Rob Landley <rob@landley.net>
parents:
237
diff
changeset
|
5 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
|
6 |
233
d4176f3f3835
Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents:
195
diff
changeset
|
7 config ONEIT |
694
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
674
diff
changeset
|
8 bool "oneit" |
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
674
diff
changeset
|
9 default y |
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
674
diff
changeset
|
10 help |
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
674
diff
changeset
|
11 usage: oneit [-p] [-c /dev/tty0] command [...] |
233
d4176f3f3835
Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents:
195
diff
changeset
|
12 |
694
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
674
diff
changeset
|
13 A simple init program that runs a single supplied command line with a |
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
674
diff
changeset
|
14 controlling tty (so CTRL-C can kill it). |
233
d4176f3f3835
Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents:
195
diff
changeset
|
15 |
694
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
674
diff
changeset
|
16 -p Power off instead of rebooting when command exits. |
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
674
diff
changeset
|
17 -c Which console device to use. |
233
d4176f3f3835
Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents:
195
diff
changeset
|
18 |
694
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
674
diff
changeset
|
19 The oneit command runs the supplied command line as a child process |
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
674
diff
changeset
|
20 (because PID 1 has signals blocked), attached to /dev/tty0, in its |
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
674
diff
changeset
|
21 own session. Then oneit reaps zombies until the child exits, at |
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
674
diff
changeset
|
22 which point it reboots (or with -p, powers off) the system. |
233
d4176f3f3835
Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents:
195
diff
changeset
|
23 */ |
34 | 24 |
674
7e846e281e38
New build infrastructure to generate FLAG_ macros and TT alias, #define FOR_commandname before #including toys.h to trigger it. Rename DEFINE_GLOBALS() to just GLOBALS() (because I could never remember if it was DECLARE_GLOBALS). Convert existing commands to use new infrastructure, and replace optflag constants with FLAG_ macros where appropriate.
Rob Landley <rob@landley.net>
parents:
656
diff
changeset
|
25 #define FOR_oneit |
34 | 26 #include "toys.h" |
27 #include <sys/reboot.h> | |
28 | |
674
7e846e281e38
New build infrastructure to generate FLAG_ macros and TT alias, #define FOR_commandname before #including toys.h to trigger it. Rename DEFINE_GLOBALS() to just GLOBALS() (because I could never remember if it was DECLARE_GLOBALS). Convert existing commands to use new infrastructure, and replace optflag constants with FLAG_ macros where appropriate.
Rob Landley <rob@landley.net>
parents:
656
diff
changeset
|
29 GLOBALS( |
694
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
674
diff
changeset
|
30 char *console; |
237
7cb15eae1664
Zap toylist.h, moving contents of global structures into DEFINE_GLOBALS()
Rob Landley <rob@landley.net>
parents:
234
diff
changeset
|
31 ) |
7cb15eae1664
Zap toylist.h, moving contents of global structures into DEFINE_GLOBALS()
Rob Landley <rob@landley.net>
parents:
234
diff
changeset
|
32 |
34 | 33 // The minimum amount of work necessary to get ctrl-c and such to work is: |
34 // | |
35 // - Fork a child (PID 1 is special: can't exit, has various signals blocked). | |
36 // - Do a setsid() (so we have our own session). | |
37 // - In the child, attach stdio to /dev/tty0 (/dev/console is special) | |
38 // - Exec the rest of the command line. | |
39 // | |
40 // PID 1 then reaps zombies until the child process it spawned exits, at which | |
41 // point it calls sync() and reboot(). I could stick a kill -1 in there. | |
42 | |
195
9dc48c97d6f9
Add -c option to oneit, to specify console other than /dev/tty0.
Rob Landley <rob@landley.net>
parents:
194
diff
changeset
|
43 |
186
25447caf1b4b
Change command main() functions to return void, and exit(toys.exitval) from
Rob Landley <rob@landley.net>
parents:
156
diff
changeset
|
44 void oneit_main(void) |
34 | 45 { |
46 int i; | |
47 pid_t pid; | |
48 | |
49 // Create a new child process. | |
50 pid = vfork(); | |
51 if (pid) { | |
52 | |
53 // pid 1 just reaps zombies until it gets its child, then halts the system. | |
694
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
674
diff
changeset
|
54 while (pid != wait(&i)); |
34 | 55 sync(); |
330
2a003d86d805
Teach oneit to reboot on exit more reliably.
Rob Landley <rob@landley.net>
parents:
304
diff
changeset
|
56 |
694
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
674
diff
changeset
|
57 // PID 1 can't call reboot() because it kills the task that calls it, |
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
674
diff
changeset
|
58 // which causes the kernel to panic before the actual reboot happens. |
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
674
diff
changeset
|
59 if (!vfork()) reboot((toys.optflags & FLAG_p) ? RB_POWER_OFF : RB_AUTOBOOT); |
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
674
diff
changeset
|
60 sleep(5); |
786841fdb1e0
Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents:
674
diff
changeset
|
61 _exit(1); |
34 | 62 } |
156
1e8f4b05cb65
Remove trailing whitespace (thanks to Charlie Shepherd), and a couple comment
Rob Landley <rob@landley.net>
parents:
34
diff
changeset
|
63 |
34 | 64 // Redirect stdio to /dev/tty0, with new session ID, so ctrl-c works. |
65 setsid(); | |
66 for (i=0; i<3; i++) { | |
67 close(i); | |
1429
8c78a7e5486d
Disable xopen's default O_CLOEXEC for oneit.
Rob Landley <rob@landley.net>
parents:
694
diff
changeset
|
68 // Remember, O_CLOEXEC is backwards for xopen() |
8c78a7e5486d
Disable xopen's default O_CLOEXEC for oneit.
Rob Landley <rob@landley.net>
parents:
694
diff
changeset
|
69 xopen(TT.console ? TT.console : "/dev/tty0", O_RDWR|O_CLOEXEC); |
34 | 70 } |
71 | |
186
25447caf1b4b
Change command main() functions to return void, and exit(toys.exitval) from
Rob Landley <rob@landley.net>
parents:
156
diff
changeset
|
72 // Can't xexec() here, because we vforked so we don't want to error_exit(). |
34 | 73 toy_exec(toys.optargs); |
74 execvp(*toys.optargs, toys.optargs); | |
75 _exit(127); | |
76 } |