comparison toys/other/oneit.c @ 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.
author Rob Landley <rob@landley.net>
date Mon, 08 Oct 2012 00:02:30 -0500
parents 6df4ccc0acbe
children 786841fdb1e0
comparison
equal deleted inserted replaced
673:c102f31a753e 674:7e846e281e38
22 (because PID 1 has signals blocked), attached to /dev/tty0, in its 22 (because PID 1 has signals blocked), attached to /dev/tty0, in its
23 own session. Then oneit reaps zombies until the child exits, at 23 own session. Then oneit reaps zombies until the child exits, at
24 which point it reboots (or with -p, powers off) the system. 24 which point it reboots (or with -p, powers off) the system.
25 */ 25 */
26 26
27 #define FOR_oneit
27 #include "toys.h" 28 #include "toys.h"
28 #include <sys/reboot.h> 29 #include <sys/reboot.h>
29 30
30 DEFINE_GLOBALS( 31 GLOBALS(
31 char *console; 32 char *console;
32 ) 33 )
33
34 #define TT this.oneit
35 34
36 // The minimum amount of work necessary to get ctrl-c and such to work is: 35 // The minimum amount of work necessary to get ctrl-c and such to work is:
37 // 36 //
38 // - Fork a child (PID 1 is special: can't exit, has various signals blocked). 37 // - Fork a child (PID 1 is special: can't exit, has various signals blocked).
39 // - Do a setsid() (so we have our own session). 38 // - Do a setsid() (so we have our own session).
57 while (pid!=wait(&i)); 56 while (pid!=wait(&i));
58 sync(); 57 sync();
59 58
60 // PID 1 can't call reboot() because it kills the task that calls it, 59 // PID 1 can't call reboot() because it kills the task that calls it,
61 // which causes the kernel to panic before the actual reboot happens. 60 // which causes the kernel to panic before the actual reboot happens.
62 if (!vfork()) reboot((toys.optflags&1) ? RB_POWER_OFF : RB_AUTOBOOT); 61 if (!vfork())
62 reboot((toys.optflags & FLAG_p) ? RB_POWER_OFF : RB_AUTOBOOT);
63 sleep(5); 63 sleep(5);
64 _exit(1); 64 _exit(1);
65 } 65 }
66 66
67 // Redirect stdio to /dev/tty0, with new session ID, so ctrl-c works. 67 // Redirect stdio to /dev/tty0, with new session ID, so ctrl-c works.