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