-
Notifications
You must be signed in to change notification settings - Fork 82
/
signal.c
206 lines (176 loc) · 5.16 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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
#include "../common.h"
#include <netinet/ip.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <sys/epoll.h>
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
#include <signal.h>
#include <assert.h>
#include <errno.h>
#include <stdio.h>
#include <sched.h>
#include <poll.h>
#include "override.h"
#include "trace.h"
static bool signal_override[NSIG];
static struct sigaction sa_user[NSIG];
bool __thread signal_received;
bool __thread signal_interrupted;
static void
signal_override_handler(int signum)
{
int tmp;
TRACE_SIGNAL_ENTRY(tmp);
signal_received = true;
if (!(sa_user[signum].sa_flags & SA_RESTART))
signal_interrupted = true;
if (signum >= 0 && signum < NSIG && signal_override[signum])
sa_user[signum].sa_handler(signum);
TRACE_SIGNAL_EXIT(tmp);
}
static void
sigaction_override_handler(int signum, siginfo_t *siginfo, void *context)
{
int tmp;
TRACE_SIGNAL_ENTRY(tmp);
signal_received = true;
if (!(sa_user[signum].sa_flags & SA_RESTART))
signal_interrupted = true;
if (signum >= 0 && signum < NSIG && signal_override[signum])
sa_user[signum].sa_sigaction(signum, siginfo, context);
TRACE_SIGNAL_EXIT(tmp);
}
__attribute__((visibility("default")))
sighandler_t
signal(int signum, sighandler_t handler)
{
sighandler_t old;
TRACE_CALL("signal");
TRACE_ARG(INT, signum);
TRACE_LAST_ARG(SIGHANDLER, handler);
TRACE_FLUSH();
if (signum < 0 || signum >= NSIG)
old = LIBC(signal, signum, handler);
else if (signal_override[signum])
{
if (sa_user[signum].sa_flags & SA_SIGINFO)
old = (sighandler_t)sa_user[signum].sa_sigaction;
else
old = sa_user[signum].sa_handler;
if (handler == SIG_DFL || handler == SIG_IGN)
{
/* Don't override SIG_DFL or SIG_IGN */
signal_override[signum] = false;
LIBC(signal, signum, handler);
}
else
{
/* Install override */
sa_user[signum].sa_handler = handler;
sigemptyset(&sa_user[signum].sa_mask);
sa_user[signum].sa_flags = 0;
LIBC(signal, signum, signal_override_handler);
}
}
else
{
if (handler == SIG_DFL || handler == SIG_IGN)
old = LIBC(signal, signum, handler);
else
{
/* Install override */
signal_override[signum] = true;
sa_user[signum].sa_handler = handler;
sigemptyset(&sa_user[signum].sa_mask);
sa_user[signum].sa_flags = 0;
old = LIBC(signal, signum, signal_override_handler);
}
}
TRACE_RETURN(SIGHANDLER, old);
return old;
}
__attribute__((visibility("default")))
int
sigaction(int signum, const struct sigaction *act, struct sigaction *oldact)
{
int ret;
TRACE_CALL("sigaction");
TRACE_ARG(INT, signum);
TRACE_ARG(SIGACTION_PTR, act);
TRACE_FLUSH();
if (signum < 0 || signum >= NSIG)
ret = LIBC(sigaction, signum, act, oldact);
else if (signal_override[signum])
{
struct sigaction sa_override;
if (oldact)
*oldact = sa_user[signum];
if (act)
{
/* Don't override SIG_DFL or SIG_IGN */
if (act->sa_handler == SIG_DFL || act->sa_handler == SIG_IGN)
{
signal_override[signum] = false;
ret = LIBC(sigaction, signum, act, NULL);
}
else
{
/* Install override */
sa_override = sa_user[signum] = *act;
if (sa_override.sa_flags & SA_SIGINFO)
sa_override.sa_sigaction = sigaction_override_handler;
else
sa_override.sa_handler = signal_override_handler;
ret = LIBC(sigaction, signum, &sa_override, NULL);
}
}
else
ret = 0;
}
else
{
struct sigaction sa_override;
if (act == NULL || act->sa_handler == SIG_DFL ||
act->sa_handler == SIG_IGN)
{
/* No need to install override */
ret = LIBC(sigaction, signum, act, oldact);
}
else
{
/* Install override */
sa_override = sa_user[signum] = *act;
if (sa_override.sa_flags & SA_SIGINFO)
sa_override.sa_sigaction = sigaction_override_handler;
else
sa_override.sa_handler = signal_override_handler;
signal_override[signum] = true;
ret = LIBC(sigaction, signum, &sa_override, oldact);
}
}
TRACE_LAST_ARG(SIGACTION_PTR, oldact);
TRACE_RETURN(INT, ret);
return ret;
}
__attribute__((visibility("default")))
int
siginterrupt(int signum, int flag)
{
int ret;
TRACE_CALL("siginterrupt");
TRACE_ARG(INT, signum);
TRACE_LAST_ARG(INT, flag);
TRACE_FLUSH();
if (signum >= 0 && signum < NSIG)
{
if (flag)
sa_user[signum].sa_flags &= ~SA_RESTART;
else
sa_user[signum].sa_flags |= SA_RESTART;
}
ret = LIBC(siginterrupt, signum, flag);
TRACE_RETURN(INT, ret);
return ret;
}