1 #include <sys/types.h>
2 #include <sys/stat.h>
3 #include <signal.h>
4 #include <unistd.h>
5 #include "runit.h"
6 #include "strerr.h"
7 #include "sig.h"
8 #include "open.h"
9 #include "error.h"
10
11 #define USAGE " 0|6"
12 #define FATAL "init: fatal: "
13 /* #define WARNING "init: warning: " */
14
15 const char *progname;
16
usage(void)17 void usage(void) { strerr_die4x(0, "usage: ", progname, USAGE, "\n"); }
18
runit_halt()19 void runit_halt () {
20 if (open_trunc(STOPIT) == -1)
21 strerr_die4sys(111, FATAL, "unable to create ", STOPIT, ": ");
22 if (chmod(STOPIT, 0100) == -1)
23 strerr_die4sys(111, FATAL, "unable to chmod ", STOPIT, ": ");
24 if (chmod(REBOOT, 0) == -1)
25 if (errno != error_noent)
26 strerr_die4sys(111, FATAL, "unable to chmod ", REBOOT, ": ");
27 kill(1, sig_cont);
28 _exit(0);
29 }
30
runit_reboot()31 void runit_reboot () {
32 if (open_trunc(STOPIT) == -1)
33 strerr_die4sys(111, FATAL, "unable to create ", STOPIT, ": ");
34 if (chmod(STOPIT, 0100) == -1)
35 strerr_die4sys(111, FATAL, "unable to chmod ", STOPIT, ": ");
36 if (open_trunc(REBOOT) == -1)
37 strerr_die4sys(111, FATAL, "unable to create ", REBOOT, ": ");
38 if (chmod(REBOOT, 0100) == -1)
39 strerr_die4sys(111, FATAL, "unable to chmod ", REBOOT, ": ");
40 kill(1, sig_cont);
41 _exit(0);
42 }
43
main(int argc,const char * const * argv,char * const * envp)44 int main (int argc, const char * const *argv, char * const *envp) {
45 const char *prog[2];
46
47 progname =*argv++;
48
49 if (getpid() == 1) {
50 prog[1] =0;
51 prog[0] ="runit";
52
53 /* kernel is starting init, runit does the job. */
54 execve(RUNIT, (char *const *)prog, envp);
55
56 /* serious error */
57 strerr_die4sys(111, FATAL, "unable to start ", prog[0], ": ");
58 }
59
60 if (! *argv || ! **argv) usage();
61 switch (**argv) {
62 case '0':
63 runit_halt();
64 break;
65 case '6':
66 runit_reboot();
67 break;
68 default:
69 usage();
70 }
71 /* not reached */
72 _exit(0);
73 }
74