-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
45 lines (35 loc) · 1.14 KB
/
main.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
#include <signal.h>
#include <sys/signalfd.h>
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int
main (int argc, char **argv)
{
/* man signalfd */
sigset_t mask;
/* create our signal mask for signals we want to catch in polling loop */
sigemptyset(&mask);
sigaddset(&mask, SIGINT);
sigaddset(&mask, SIGQUIT);
/* block the default action for signals we want to catch */
assert(sigprocmask(SIG_BLOCK, &mask, NULL) != -1);
int sfd;
assert((sfd = signalfd(-1, &mask, 0)) != -1);
struct signalfd_siginfo fdsi;
size_t s;
while(1) {
/* read the signalfd and process the results */
s = read(sfd, &fdsi, sizeof(struct signalfd_siginfo));
assert(s == sizeof(struct signalfd_siginfo));
if (fdsi.ssi_signo == SIGINT) {
printf("INT!\n");
break;
} else if (fdsi.ssi_signo == SIGQUIT) {
printf("QUIT!\n");
break;
}
}
exit(0);
}