-
Notifications
You must be signed in to change notification settings - Fork 9
/
signal.c
129 lines (115 loc) · 2.47 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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
/* signal.c
*
* Copyright (C) 2000,2001,2002 by Salvatore Sanfilippo
* <antirez@invece.org>
*
* This code is under the GPL license version 2
* See the COPYING file for more information
*/
/* ens.h must be included before all other includes */
#include "ens.h"
#include <signal.h>
#include <stdlib.h>
static volatile int signal_up = 0;
static volatile int signal_usr1 = 0;
static volatile int signal_usr2 = 0;
/* The signals handler:
* for the signals that can't be handled here
* just set the flag */
void signal_handler(int signum)
{
switch(signum) {
case SIGHUP:
signal_up++;
break;
case SIGUSR1:
signal_usr1++;
break;
case SIGUSR2:
signal_usr2++;
break;
case SIGSEGV:
ylog(VERB_FORCE, "SIGSEGV trapped -- INTERNAL ERROR\n");
dump_state();
abort();
break;
default:
ylog(VERB_FORCE, "Signal %d trapped: exit\n", signum);
exit(1);
}
install_signal_handler();
}
/* Handle the signals that can't be handled asyncronously */
int handle_signals(void)
{
int count = 0;
if (signal_up) {
signal_up--;
ylog(VERB_LOW, "SIGHUP trapped: read the config file\n");
config_reset();
config_read(configfile);
count++;
}
if (signal_usr1) {
signal_usr1--;
dump_state();
count++;
}
if (signal_usr2) {
signal_usr2--;
opt_forward = !opt_forward;
ylog(VERB_LOW, "SIGUSR2 trapped, forwarding is %s\n",
opt_forward ? "ON" : "OFF");
fflush(logfp);
count++;
}
return count;
}
/* Install the handlers */
void install_signal_handler(void)
{
Signal(SIGHUP, signal_handler);
Signal(SIGUSR1, signal_handler);
Signal(SIGUSR2, signal_handler);
Signal(SIGSEGV, signal_handler);
}
/* Portable signal() from R.Stevens,
* modified to reset the handler */
void (*Signal(int signo, void (*func)(int)))(int)
{
struct sigaction act, oact;
act.sa_handler = func;
sigemptyset(&act.sa_mask);
act.sa_flags = 0; /* So if set SA_RESETHAND is cleared */
if (signo == SIGALRM)
{
#ifdef SA_INTERRUPT
act.sa_flags |= SA_INTERRUPT; /* SunOS 4.x */
#endif
}
else
{
#ifdef SA_RESTART
act.sa_flags |= SA_RESTART; /* SVR4, 4.4BSD, Linux */
#endif
}
if (sigaction(signo, &act, &oact) == -1)
return SIG_ERR;
return (oact.sa_handler);
}
/* Block the given signal */
int signal_block(int sig)
{
sigset_t set;
sigemptyset(&set);
sigaddset(&set, sig);
return sigprocmask(SIG_BLOCK, &set, NULL);
}
/* Unblock the given signal */
int signal_unblock(int sig)
{
sigset_t set;
sigemptyset(&set);
sigaddset(&set, sig);
return sigprocmask(SIG_UNBLOCK, &set, NULL);
}