minit.c (1215B)
1 #include <signal.h> 2 #include <unistd.h> 3 #include <stdio.h> 4 #include <sys/reboot.h> 5 6 #define LEN(x) (sizeof (x) / sizeof *(x)) 7 8 static void sigpoweroff(void); 9 static void sigreboot(void); 10 static void sighalt(void); 11 static void sigrestart(void); 12 static void spawn(char *const []); 13 14 static struct { 15 int sig; 16 void (*handler)(void);} sigmap[] = { 17 { SIGUSR1, sigpoweroff }, 18 { SIGINT, sigreboot }, 19 { SIGUSR2, sighalt }, 20 { SIGQUIT, sigrestart }, 21 }; 22 23 static sigset_t set; 24 25 #include "config.h" 26 27 int main(void) { 28 int sig; 29 size_t i; 30 if (getpid() != 1) return 1; 31 32 sigfillset(&set); 33 sigprocmask(SIG_BLOCK, &set, 0); 34 spawn(rcinitcmd); 35 while (1) { 36 sigwait(&set, &sig); 37 for (i = 0; i < LEN(sigmap); i++) { 38 if (sigmap[i].sig == sig) { 39 sigmap[i].handler(); 40 break; 41 } 42 } 43 } 44 } 45 46 static void sigpoweroff(void){spawn(rcpoweroffcmd);} 47 static void sigreboot(void){spawn(rcrebootcmd);} 48 static void sighalt(void){reboot(RB_POWER_OFF);} 49 static void sigrestart(void){reboot(RB_AUTOBOOT);} 50 static void spawn(char *const argv[]){ 51 switch (fork()) { 52 case 0: 53 sigprocmask(SIG_UNBLOCK, &set, NULL); 54 setsid(); 55 execvp(argv[0], argv); 56 perror("execvp"); 57 _exit(1); 58 case -1: 59 perror("fork"); 60 } 61 }