-
Notifications
You must be signed in to change notification settings - Fork 0
/
myinit.c
60 lines (53 loc) · 1.35 KB
/
myinit.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
/* See LICENSE file for copyright and license details. */
#include <sys/types.h>
#include <sys/wait.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#define LEN(x) (sizeof (x) / sizeof *(x))
static void spawn(char* const []);
static struct {
int sig;
char* const cmd[3];
} sigmap[] = {
{ SIGINT, { "/bin/rc.shutdown", "poweroff", NULL }},
{ SIGUSR1, {"/bin/rc.shutdown", "reboot", NULL }},
{ SIGUSR2, {"/bin/rc.shutdown", "soft-reboot", NULL }},
};
static char* const rcInitCmd[] = { "/bin/rc.init", NULL };
static sigset_t set;
static void
spawn(char* const argv[]) {
switch(fork()) {
case 0:
sigprocmask(SIG_UNBLOCK, &set, NULL);
setsid();
signal(SIGCHLD, SIG_DFL);
execvp(argv[0], argv);
perror("Could not execvp");
_exit(1);
case -1:
perror("fork");
}
}
int
main(void) {
int sig;
size_t i;
sigfillset(&set);
sigprocmask(SIG_BLOCK, &set, NULL);
signal(SIGCHLD, SIG_IGN);
spawn(rcInitCmd);
while(1) {
sigwait(&set, &sig);
for(i = 0; i < LEN(sigmap); i++) {
if(sigmap[i].sig == sig) {
execvp(sigmap[i].cmd[0], sigmap[i].cmd);
perror("Could not execvp");
}
}
}
/* not reachable */
return 0;
}