comparison toys/other/oneit.c @ 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. The actual code should be the same afterward, this is just cosmetic refactoring.
author Rob Landley <rob@landley.net>
date Tue, 13 Nov 2012 17:14:08 -0600
parents 7e846e281e38
children 8c78a7e5486d
comparison
equal deleted inserted replaced
693:4a5a250e0633 694:786841fdb1e0
1 /* vi: set sw=4 ts=4: 1 /* oneit.c - tiny init replacement to launch a single child process.
2 *
3 * oneit.c - tiny init replacement to launch a single child process.
4 * 2 *
5 * Copyright 2005, 2007 by Rob Landley <rob@landley.net>. 3 * Copyright 2005, 2007 by Rob Landley <rob@landley.net>.
6 4
7 USE_ONEIT(NEWTOY(oneit, "^<1c:p", TOYFLAG_SBIN)) 5 USE_ONEIT(NEWTOY(oneit, "^<1c:p", TOYFLAG_SBIN))
8 6
9 config ONEIT 7 config ONEIT
10 bool "oneit" 8 bool "oneit"
11 default y 9 default y
12 help 10 help
13 usage: oneit [-p] [-c /dev/tty0] command [...] 11 usage: oneit [-p] [-c /dev/tty0] command [...]
14 12
15 A simple init program that runs a single supplied command line with a 13 A simple init program that runs a single supplied command line with a
16 controlling tty (so CTRL-C can kill it). 14 controlling tty (so CTRL-C can kill it).
17 15
18 -p Power off instead of rebooting when command exits. 16 -p Power off instead of rebooting when command exits.
19 -c Which console device to use. 17 -c Which console device to use.
20 18
21 The oneit command runs the supplied command line as a child process 19 The oneit command runs the supplied command line as a child process
22 (because PID 1 has signals blocked), attached to /dev/tty0, in its 20 (because PID 1 has signals blocked), attached to /dev/tty0, in its
23 own session. Then oneit reaps zombies until the child exits, at 21 own session. Then oneit reaps zombies until the child exits, at
24 which point it reboots (or with -p, powers off) the system. 22 which point it reboots (or with -p, powers off) the system.
25 */ 23 */
26 24
27 #define FOR_oneit 25 #define FOR_oneit
28 #include "toys.h" 26 #include "toys.h"
29 #include <sys/reboot.h> 27 #include <sys/reboot.h>
30 28
31 GLOBALS( 29 GLOBALS(
32 char *console; 30 char *console;
33 ) 31 )
34 32
35 // The minimum amount of work necessary to get ctrl-c and such to work is: 33 // The minimum amount of work necessary to get ctrl-c and such to work is:
36 // 34 //
37 // - Fork a child (PID 1 is special: can't exit, has various signals blocked). 35 // - Fork a child (PID 1 is special: can't exit, has various signals blocked).
51 // Create a new child process. 49 // Create a new child process.
52 pid = vfork(); 50 pid = vfork();
53 if (pid) { 51 if (pid) {
54 52
55 // pid 1 just reaps zombies until it gets its child, then halts the system. 53 // pid 1 just reaps zombies until it gets its child, then halts the system.
56 while (pid!=wait(&i)); 54 while (pid != wait(&i));
57 sync(); 55 sync();
58 56
59 // PID 1 can't call reboot() because it kills the task that calls it, 57 // PID 1 can't call reboot() because it kills the task that calls it,
60 // which causes the kernel to panic before the actual reboot happens. 58 // which causes the kernel to panic before the actual reboot happens.
61 if (!vfork()) 59 if (!vfork()) reboot((toys.optflags & FLAG_p) ? RB_POWER_OFF : RB_AUTOBOOT);
62 reboot((toys.optflags & FLAG_p) ? RB_POWER_OFF : RB_AUTOBOOT); 60 sleep(5);
63 sleep(5); 61 _exit(1);
64 _exit(1);
65 } 62 }
66 63
67 // Redirect stdio to /dev/tty0, with new session ID, so ctrl-c works. 64 // Redirect stdio to /dev/tty0, with new session ID, so ctrl-c works.
68 setsid(); 65 setsid();
69 for (i=0; i<3; i++) { 66 for (i=0; i<3; i++) {
70 close(i); 67 close(i);
71 xopen(TT.console ? TT.console : "/dev/tty0",O_RDWR); 68 xopen(TT.console ? TT.console : "/dev/tty0", O_RDWR);
72 } 69 }
73 70
74 // Can't xexec() here, because we vforked so we don't want to error_exit(). 71 // Can't xexec() here, because we vforked so we don't want to error_exit().
75 toy_exec(toys.optargs); 72 toy_exec(toys.optargs);
76 execvp(*toys.optargs, toys.optargs); 73 execvp(*toys.optargs, toys.optargs);