-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
51 lines (39 loc) · 1.29 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
44
45
46
47
48
49
50
51
#include <assert.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/signalfd.h>
#include <unistd.h>
int
main (int argc, char **argv)
{
/* refer to signalfd_example for explanations on signalfd calls */
int sfd, sockfd;
sigset_t mask;
size_t s;
struct signalfd_siginfo fdsi;
struct sockaddr_in addr;
sigemptyset(&mask);
sigaddset(&mask, SIGINT);
sigaddset(&mask, SIGQUIT);
assert(sigprocmask(SIG_BLOCK, &mask, NULL) != -1);
assert((sfd = signalfd(-1, &mask, 0)) != -1);
memset(&addr, 0, sizeof(addr));
addr.sin_family = AF_INET;
addr.sin_addr.s_addr = INADDR_ANY;
addr.sin_port = htons(43443);
assert(bind(sockfd, (struct sockaddr *)&addr, sizeof(addr) == 0));
assert(listen(sockfd, 1024) == 0);
while(1) {
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);
}