forked from eldipa/tiburoncin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsignal.c
74 lines (63 loc) · 1.52 KB
/
signal.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#define _POSIX_C_SOURCE 200112L
#include <signal.h>
int interrupted = 0;
/*
* Save the signal number into the interrupted global variable
* only if the program wasn't interrupted before.
**/
static void int_handler(int signum) {
if (!interrupted)
interrupted = signum;
}
static int initialize_block_all_sigset(sigset_t *set) {
if (sigfillset(set) != -1 \
&& sigdelset(set, SIGBUS) != -1 \
&& sigdelset(set, SIGFPE) != -1 \
&& sigdelset(set, SIGILL) != -1 \
&& sigdelset(set, SIGSEGV) != -1 \
&& sigdelset(set, SIGCONT) != -1 \
&& sigdelset(set, SIGTSTP) != -1 \
&& sigdelset(set, SIGTTIN) != -1 \
&& sigdelset(set, SIGTTOU) != -1) {
return 0;
}
else {
return -1;
}
}
int block_all_signals() {
sigset_t set;
if (initialize_block_all_sigset(&set) != -1 \
&& sigprocmask(SIG_SETMASK, &set, 0) != -1) {
return 0;
}
else {
return -1;
}
}
int setup_signal_handlers() {
struct sigaction sa;
sigfillset(&sa.sa_mask); /* do not interrupt a sig handler */
sa.sa_flags = 0;
sa.sa_handler = int_handler;
if (sigaction(SIGINT, &sa, 0) == -1)
return -1;
if (sigaction(SIGTERM, &sa, 0) == -1)
return -1;
sa.sa_handler = SIG_IGN;
if (sigaction(SIGPIPE, &sa, 0) == -1)
return -1;
return 0;
}
int initialize_interrupt_sigset(sigset_t *set) {
if (initialize_block_all_sigset(set) != -1 \
&& sigdelset(set, SIGINT) != -1 \
&& sigdelset(set, SIGQUIT) != -1 \
&& sigdelset(set, SIGTERM) != -1 \
&& sigdelset(set, SIGPIPE) != -1) {
return 0;
}
else {
return -1;
}
}