-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathSignals.cpp
279 lines (235 loc) · 7.09 KB
/
Signals.cpp
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
/*#ifdef DEBUG
#include <iostream>
#endif*/
#include <list>
#include <stdexcept>
#include <algorithm>
#include <cerrno>
#include <cstring>
#include <csignal>
#include <boost/function.hpp>
#include <boost/function_equal.hpp>
#include <unistd.h>
#include <fcntl.h>
#include <sys/select.h>
#include "Signals.hpp"
extern "C" void SignalQueue_signalHandler(int signum, siginfo_t*, void*);
namespace LibWheel
{
SignalException::SignalException(int signum, const char* desc, const siginfo_t* info) throw()
: description(desc), siginfo(info), number(signum)
{}
SignalException::SignalException(int signum, const char* desc) throw()
: description(desc), siginfo(NULL), number(signum)
{}
SignalException::SignalException(int signum) throw()
: description(), siginfo(NULL), number(signum)
{}
SignalException::~SignalException() throw()
{}
const siginfo_t*
SignalException::getInfo() const throw()
{
return siginfo;
}
const char*
SignalException::what() const throw()
{
return description.c_str();
}
int
SignalException::getSignalNumber() const throw()
{
return number;
}
Interrupt::Interrupt(const siginfo_t* info) throw()
: SignalException(SIGINT, "SIGINT received", info)
{}
Interrupt::Interrupt() throw()
: SignalException(SIGINT, "SIGINT received")
{}
int
Interrupt::getSignalNumber()
{
return SIGINT;
}
namespace SignalQueue
{
struct Pipe
{
int fds[2];
Pipe() throw(std::runtime_error);
~Pipe();
private:
Pipe(const Pipe&);
Pipe& operator= (const Pipe&);
};
Pipe::Pipe() throw(std::runtime_error)
{
int ret = pipe(fds);
if (ret == -1)
throw std::runtime_error(strerror(errno));
ret = fcntl(fds[0], F_SETFL, O_NONBLOCK);
if (ret == -1)
throw std::runtime_error(strerror(errno));
ret = fcntl(fds[1], F_SETFL, O_NONBLOCK);
if (ret == -1)
throw std::runtime_error(strerror(errno));
}
Pipe::~Pipe()
{
close(fds[0]);
close(fds[1]);
}
static const Pipe&
getPipe()
{
static Pipe pipe;
return pipe;
}
typedef std::list<boost::function<void()> > HandlerList;
// static // FIXME
HandlerList*
getSignalTable()
{
static HandlerList sigs[_NSIG];
return sigs;
}
static int
getWriteFD()
{
return getPipe().fds[1];
}
void
setHandler(int sig, Action act) THROW((std::domain_error, std::invalid_argument))
{
struct sigaction handler;
int ret;
switch (act)
{
case DEFAULT:
handler.sa_handler = SIG_DFL;
handler.sa_flags = 0;
break;
case IGNORE:
handler.sa_handler = SIG_IGN;
handler.sa_flags = 0;
break;
case HANDLE:
handler.sa_sigaction = SignalQueue_signalHandler;
handler.sa_flags = SA_SIGINFO;
break;
default:
throw std::domain_error("Invalid action");
}
// make sure that everything has been constructed
(void)getPipe();
// register the signal handler
sigemptyset(&handler.sa_mask);
ret = sigaction(sig, &handler, NULL);
if (ret == -1)
throw std::invalid_argument("Invalid signal number");
}
void
addHandler(int sig, boost::function<void()> act) THROW((std::invalid_argument))
{
if ((sig < 1) || (sig > _NSIG))
throw std::invalid_argument("Invalid signal number");
getSignalTable()[sig].push_back(act);
}
void deleteHandlers(int sig) THROW((std::invalid_argument))
{
if ((sig < 1) || (sig > _NSIG))
throw std::invalid_argument("Invalid signal number");
getSignalTable()[sig].clear();
}
void
handleNext()
{
int ret;
unsigned char signal;
// get the signal
ret = read(getReadFD(), &signal, 1);
if (ret != 1)
{
if (errno == EAGAIN)
return;
else
throw std::runtime_error(strerror(errno));
}
/*#ifdef DEBUG
std::cout << "signal " << (int)signal << " received" << std::endl;
#endif*/
const HandlerList& list = getSignalTable()[signal];
for (HandlerList::const_iterator i = list.begin(); i != list.end(); ++i)
(*i)();
}
void
handleAll()
{
while (pending())
handleNext();
}
bool
pending() throw(std::runtime_error)
{
struct timeval timeout;
fd_set readfds;
int ret;
// call select() with a zero timeout to check if data is ready
timeout.tv_sec = 0;
timeout.tv_usec = 0;
FD_ZERO(&readfds);
FD_SET(getReadFD(), &readfds);
ret = ::select(getReadFD()+1, &readfds, NULL, NULL, &timeout);
if (ret == -1)
throw std::runtime_error(strerror(errno));
else if (ret == 1)
return true;
else
return false;
}
int
getReadFD()
{
return getPipe().fds[0];
}
int
select(int n, fd_set* readfds, fd_set* writefds, fd_set* exceptfds, struct timeval* timeout)
{
fd_set rfd;
int ret;
int max_fd = (getReadFD() >= n) ? getReadFD()+1 : n;
while (1)
{
if (readfds != NULL)
std::memcpy(&rfd, readfds, sizeof(rfd));
else
FD_ZERO(&rfd);
FD_SET(getReadFD(), &rfd);
do
{
ret = ::select(max_fd, &rfd, writefds, exceptfds, timeout);
}
while ((ret == -1) && (errno == EINTR));
if (ret == -1)
return ret;
else if (FD_ISSET(getReadFD(), &rfd))
{
handleAll();
if (ret > 1)
return ret-1;
}
else
return ret;
}
}
} // namespace SignalQueue
} // namespace LibWheel
// C++ signal handler conventions require that this be in the global namespace
extern "C" void SignalQueue_signalHandler(int signum, siginfo_t*, void*)
{
char buf[1];
buf[0] = signum;
write(LibWheel::SignalQueue::getWriteFD(), buf, 1);
}