annotate toys/other/oneit.c @ 656:6df4ccc0acbe

Regularize command headers, update links to standards documents.
author Rob Landley <rob@landley.net>
date Sat, 25 Aug 2012 18:08:51 -0500
parents 2986aa63a021
children 7e846e281e38
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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 *
656
6df4ccc0acbe Regularize command headers, update links to standards documents.
Rob Landley <rob@landley.net>
parents: 653
diff changeset
3 * oneit.c - tiny init replacement to launch a single child process.
34
c1f4f9101af7 Add oneit.
Rob Landley <rob@landley.net>
parents:
diff changeset
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>.
233
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 195
diff changeset
6
304
93223118c813 Option parsing: stopearly is now a ^ prefix (not +), and an option string with
Rob Landley <rob@landley.net>
parents: 237
diff changeset
7 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
8
233
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
c1f4f9101af7 Add oneit.
Rob Landley <rob@landley.net>
parents:
diff changeset
26
c1f4f9101af7 Add oneit.
Rob Landley <rob@landley.net>
parents:
diff changeset
27 #include "toys.h"
c1f4f9101af7 Add oneit.
Rob Landley <rob@landley.net>
parents:
diff changeset
28 #include <sys/reboot.h>
c1f4f9101af7 Add oneit.
Rob Landley <rob@landley.net>
parents:
diff changeset
29
237
7cb15eae1664 Zap toylist.h, moving contents of global structures into DEFINE_GLOBALS()
Rob Landley <rob@landley.net>
parents: 234
diff changeset
30 DEFINE_GLOBALS(
7cb15eae1664 Zap toylist.h, moving contents of global structures into DEFINE_GLOBALS()
Rob Landley <rob@landley.net>
parents: 234
diff changeset
31 char *console;
7cb15eae1664 Zap toylist.h, moving contents of global structures into DEFINE_GLOBALS()
Rob Landley <rob@landley.net>
parents: 234
diff changeset
32 )
7cb15eae1664 Zap toylist.h, moving contents of global structures into DEFINE_GLOBALS()
Rob Landley <rob@landley.net>
parents: 234
diff changeset
33
7cb15eae1664 Zap toylist.h, moving contents of global structures into DEFINE_GLOBALS()
Rob Landley <rob@landley.net>
parents: 234
diff changeset
34 #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
35
34
c1f4f9101af7 Add oneit.
Rob Landley <rob@landley.net>
parents:
diff changeset
36 // 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
37 //
c1f4f9101af7 Add oneit.
Rob Landley <rob@landley.net>
parents:
diff changeset
38 // - 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
39 // - Do a setsid() (so we have our own session).
c1f4f9101af7 Add oneit.
Rob Landley <rob@landley.net>
parents:
diff changeset
40 // - In the child, attach stdio to /dev/tty0 (/dev/console is special)
c1f4f9101af7 Add oneit.
Rob Landley <rob@landley.net>
parents:
diff changeset
41 // - Exec the rest of the command line.
c1f4f9101af7 Add oneit.
Rob Landley <rob@landley.net>
parents:
diff changeset
42 //
c1f4f9101af7 Add oneit.
Rob Landley <rob@landley.net>
parents:
diff changeset
43 // 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
44 // 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
45
195
9dc48c97d6f9 Add -c option to oneit, to specify console other than /dev/tty0.
Rob Landley <rob@landley.net>
parents: 194
diff changeset
46
186
25447caf1b4b Change command main() functions to return void, and exit(toys.exitval) from
Rob Landley <rob@landley.net>
parents: 156
diff changeset
47 void oneit_main(void)
34
c1f4f9101af7 Add oneit.
Rob Landley <rob@landley.net>
parents:
diff changeset
48 {
c1f4f9101af7 Add oneit.
Rob Landley <rob@landley.net>
parents:
diff changeset
49 int i;
c1f4f9101af7 Add oneit.
Rob Landley <rob@landley.net>
parents:
diff changeset
50 pid_t pid;
c1f4f9101af7 Add oneit.
Rob Landley <rob@landley.net>
parents:
diff changeset
51
c1f4f9101af7 Add oneit.
Rob Landley <rob@landley.net>
parents:
diff changeset
52 // Create a new child process.
c1f4f9101af7 Add oneit.
Rob Landley <rob@landley.net>
parents:
diff changeset
53 pid = vfork();
c1f4f9101af7 Add oneit.
Rob Landley <rob@landley.net>
parents:
diff changeset
54 if (pid) {
c1f4f9101af7 Add oneit.
Rob Landley <rob@landley.net>
parents:
diff changeset
55
c1f4f9101af7 Add oneit.
Rob Landley <rob@landley.net>
parents:
diff changeset
56 // pid 1 just reaps zombies until it gets its child, then halts the system.
c1f4f9101af7 Add oneit.
Rob Landley <rob@landley.net>
parents:
diff changeset
57 while (pid!=wait(&i));
c1f4f9101af7 Add oneit.
Rob Landley <rob@landley.net>
parents:
diff changeset
58 sync();
330
2a003d86d805 Teach oneit to reboot on exit more reliably.
Rob Landley <rob@landley.net>
parents: 304
diff changeset
59
2a003d86d805 Teach oneit to reboot on exit more reliably.
Rob Landley <rob@landley.net>
parents: 304
diff changeset
60 // 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
61 // 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
62 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
63 sleep(5);
2a003d86d805 Teach oneit to reboot on exit more reliably.
Rob Landley <rob@landley.net>
parents: 304
diff changeset
64 _exit(1);
34
c1f4f9101af7 Add oneit.
Rob Landley <rob@landley.net>
parents:
diff changeset
65 }
156
1e8f4b05cb65 Remove trailing whitespace (thanks to Charlie Shepherd), and a couple comment
Rob Landley <rob@landley.net>
parents: 34
diff changeset
66
34
c1f4f9101af7 Add oneit.
Rob Landley <rob@landley.net>
parents:
diff changeset
67 // Redirect stdio to /dev/tty0, with new session ID, so ctrl-c works.
c1f4f9101af7 Add oneit.
Rob Landley <rob@landley.net>
parents:
diff changeset
68 setsid();
c1f4f9101af7 Add oneit.
Rob Landley <rob@landley.net>
parents:
diff changeset
69 for (i=0; i<3; i++) {
c1f4f9101af7 Add oneit.
Rob Landley <rob@landley.net>
parents:
diff changeset
70 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
71 xopen(TT.console ? TT.console : "/dev/tty0",O_RDWR);
34
c1f4f9101af7 Add oneit.
Rob Landley <rob@landley.net>
parents:
diff changeset
72 }
c1f4f9101af7 Add oneit.
Rob Landley <rob@landley.net>
parents:
diff changeset
73
186
25447caf1b4b Change command main() functions to return void, and exit(toys.exitval) from
Rob Landley <rob@landley.net>
parents: 156
diff changeset
74 // 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
75 toy_exec(toys.optargs);
c1f4f9101af7 Add oneit.
Rob Landley <rob@landley.net>
parents:
diff changeset
76 execvp(*toys.optargs, toys.optargs);
c1f4f9101af7 Add oneit.
Rob Landley <rob@landley.net>
parents:
diff changeset
77 _exit(127);
c1f4f9101af7 Add oneit.
Rob Landley <rob@landley.net>
parents:
diff changeset
78 }