annotate toys/other/oneit.c @ 1429:8c78a7e5486d draft

Disable xopen's default O_CLOEXEC for oneit.
author Rob Landley <rob@landley.net>
date Sat, 09 Aug 2014 23:49:48 -0500
parents 786841fdb1e0
children b2b2d214727a
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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
c1f4f9101af7 Add oneit.
Rob Landley <rob@landley.net>
parents:
diff changeset
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
c1f4f9101af7 Add oneit.
Rob Landley <rob@landley.net>
parents:
diff changeset
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
c1f4f9101af7 Add oneit.
Rob Landley <rob@landley.net>
parents:
diff changeset
26 #include "toys.h"
c1f4f9101af7 Add oneit.
Rob Landley <rob@landley.net>
parents:
diff changeset
27 #include <sys/reboot.h>
c1f4f9101af7 Add oneit.
Rob Landley <rob@landley.net>
parents:
diff changeset
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
c1f4f9101af7 Add oneit.
Rob Landley <rob@landley.net>
parents:
diff changeset
33 // The minimum amount of work necessary to get ctrl-c and such to work is:
c1f4f9101af7 Add oneit.
Rob Landley <rob@landley.net>
parents:
diff changeset
34 //
c1f4f9101af7 Add oneit.
Rob Landley <rob@landley.net>
parents:
diff changeset
35 // - Fork a child (PID 1 is special: can't exit, has various signals blocked).
c1f4f9101af7 Add oneit.
Rob Landley <rob@landley.net>
parents:
diff changeset
36 // - Do a setsid() (so we have our own session).
c1f4f9101af7 Add oneit.
Rob Landley <rob@landley.net>
parents:
diff changeset
37 // - In the child, attach stdio to /dev/tty0 (/dev/console is special)
c1f4f9101af7 Add oneit.
Rob Landley <rob@landley.net>
parents:
diff changeset
38 // - Exec the rest of the command line.
c1f4f9101af7 Add oneit.
Rob Landley <rob@landley.net>
parents:
diff changeset
39 //
c1f4f9101af7 Add oneit.
Rob Landley <rob@landley.net>
parents:
diff changeset
40 // PID 1 then reaps zombies until the child process it spawned exits, at which
c1f4f9101af7 Add oneit.
Rob Landley <rob@landley.net>
parents:
diff changeset
41 // point it calls sync() and reboot(). I could stick a kill -1 in there.
c1f4f9101af7 Add oneit.
Rob Landley <rob@landley.net>
parents:
diff changeset
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
c1f4f9101af7 Add oneit.
Rob Landley <rob@landley.net>
parents:
diff changeset
45 {
c1f4f9101af7 Add oneit.
Rob Landley <rob@landley.net>
parents:
diff changeset
46 int i;
c1f4f9101af7 Add oneit.
Rob Landley <rob@landley.net>
parents:
diff changeset
47 pid_t pid;
c1f4f9101af7 Add oneit.
Rob Landley <rob@landley.net>
parents:
diff changeset
48
c1f4f9101af7 Add oneit.
Rob Landley <rob@landley.net>
parents:
diff changeset
49 // Create a new child process.
c1f4f9101af7 Add oneit.
Rob Landley <rob@landley.net>
parents:
diff changeset
50 pid = vfork();
c1f4f9101af7 Add oneit.
Rob Landley <rob@landley.net>
parents:
diff changeset
51 if (pid) {
c1f4f9101af7 Add oneit.
Rob Landley <rob@landley.net>
parents:
diff changeset
52
c1f4f9101af7 Add oneit.
Rob Landley <rob@landley.net>
parents:
diff changeset
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
c1f4f9101af7 Add oneit.
Rob Landley <rob@landley.net>
parents:
diff changeset
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
c1f4f9101af7 Add oneit.
Rob Landley <rob@landley.net>
parents:
diff changeset
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
c1f4f9101af7 Add oneit.
Rob Landley <rob@landley.net>
parents:
diff changeset
64 // Redirect stdio to /dev/tty0, with new session ID, so ctrl-c works.
c1f4f9101af7 Add oneit.
Rob Landley <rob@landley.net>
parents:
diff changeset
65 setsid();
c1f4f9101af7 Add oneit.
Rob Landley <rob@landley.net>
parents:
diff changeset
66 for (i=0; i<3; i++) {
c1f4f9101af7 Add oneit.
Rob Landley <rob@landley.net>
parents:
diff changeset
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
c1f4f9101af7 Add oneit.
Rob Landley <rob@landley.net>
parents:
diff changeset
70 }
c1f4f9101af7 Add oneit.
Rob Landley <rob@landley.net>
parents:
diff changeset
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
c1f4f9101af7 Add oneit.
Rob Landley <rob@landley.net>
parents:
diff changeset
73 toy_exec(toys.optargs);
c1f4f9101af7 Add oneit.
Rob Landley <rob@landley.net>
parents:
diff changeset
74 execvp(*toys.optargs, toys.optargs);
c1f4f9101af7 Add oneit.
Rob Landley <rob@landley.net>
parents:
diff changeset
75 _exit(127);
c1f4f9101af7 Add oneit.
Rob Landley <rob@landley.net>
parents:
diff changeset
76 }