34 #define WIN32_LEAN_AND_MEAN
37 #undef WIN32_LEAN_AND_MEAN
39 #include <sys/types.h>
40 #ifdef HAVE_SYS_TIME_H
43 #include <sys/queue.h>
44 #ifdef HAVE_SYS_SOCKET_H
45 #include <sys/socket.h>
61 #include "event-internal.h"
68 static void evsignal_handler(
int sig);
72 evsignal_cb(
int fd,
short what,
void *arg)
74 static char signals[1];
81 n = recv(fd, signals,
sizeof(signals), 0);
83 event_err(1,
"%s: read", __func__);
87 #define FD_CLOSEONEXEC(x) do { \
88 if (fcntl(x, F_SETFD, 1) == -1) \
89 event_warn("fcntl(%d, F_SETFD)", x); \
92 #define FD_CLOSEONEXEC(x)
105 if (evutil_socketpair(
106 AF_UNIX, SOCK_STREAM, 0, base->sig.ev_signal_pair) == -1) {
110 event_warn(
"%s: socketpair", __func__);
112 event_err(1,
"%s: socketpair", __func__);
117 FD_CLOSEONEXEC(base->sig.ev_signal_pair[0]);
118 FD_CLOSEONEXEC(base->sig.ev_signal_pair[1]);
119 base->sig.sh_old = NULL;
120 base->sig.sh_old_max = 0;
121 base->sig.evsignal_caught = 0;
122 memset(&base->sig.evsigcaught, 0,
sizeof(sig_atomic_t)*NSIG);
124 for (i = 0; i < NSIG; ++
i)
125 TAILQ_INIT(&base->sig.evsigevents[i]);
127 evutil_make_socket_nonblocking(base->sig.ev_signal_pair[0]);
129 event_set(&base->sig.ev_signal, base->sig.ev_signal_pair[1],
130 EV_READ | EV_PERSIST, evsignal_cb, &base->sig.ev_signal);
131 base->sig.ev_signal.ev_base = base;
132 base->sig.ev_signal.ev_flags |= EVLIST_INTERNAL;
140 _evsignal_set_handler(
struct event_base *base,
141 int evsignal,
void (*
handler)(
int))
143 #ifdef HAVE_SIGACTION
155 if (evsignal >= sig->sh_old_max) {
156 int new_max = evsignal + 1;
157 event_debug((
"%s: evsignal (%d) >= sh_old_max (%d), resizing",
158 __func__, evsignal, sig->sh_old_max));
159 p = realloc(sig->sh_old, new_max *
sizeof(*sig->sh_old));
161 event_warn(
"realloc");
165 memset((
char *)p + sig->sh_old_max *
sizeof(*sig->sh_old),
166 0, (new_max - sig->sh_old_max) *
sizeof(*sig->sh_old));
168 sig->sh_old_max = new_max;
173 sig->sh_old[evsignal] = malloc(
sizeof *sig->sh_old[evsignal]);
174 if (sig->sh_old[evsignal] == NULL) {
175 event_warn(
"malloc");
180 #ifdef HAVE_SIGACTION
181 memset(&sa, 0,
sizeof(sa));
183 sa.sa_flags |= SA_RESTART;
184 sigfillset(&sa.sa_mask);
186 if (
sigaction(evsignal, &sa, sig->sh_old[evsignal]) == -1) {
187 event_warn(
"sigaction");
188 free(sig->sh_old[evsignal]);
192 if ((sh = signal(evsignal,
handler)) == SIG_ERR) {
193 event_warn(
"signal");
194 free(sig->sh_old[evsignal]);
197 *sig->sh_old[evsignal] = sh;
204 evsignal_add(
struct event *ev)
210 if (ev->ev_events & (EV_READ|EV_WRITE))
211 event_errx(1,
"%s: EV_SIGNAL incompatible use", __func__);
212 evsignal = EVENT_SIGNAL(ev);
213 assert(evsignal >= 0 && evsignal < NSIG);
214 if (TAILQ_EMPTY(&sig->evsigevents[evsignal])) {
215 event_debug((
"%s: %p: changing signal handler", __func__, ev));
216 if (_evsignal_set_handler(
217 base, evsignal, evsignal_handler) == -1)
221 evsignal_base = base;
223 if (!sig->ev_signal_added) {
224 if (event_add(&sig->ev_signal, NULL))
226 sig->ev_signal_added = 1;
231 TAILQ_INSERT_TAIL(&sig->evsigevents[evsignal], ev, ev_signal_next);
237 _evsignal_restore_handler(
struct event_base *base,
int evsignal)
241 #ifdef HAVE_SIGACTION
248 sh = sig->sh_old[evsignal];
249 sig->sh_old[evsignal] = NULL;
250 #ifdef HAVE_SIGACTION
251 if (
sigaction(evsignal, sh, NULL) == -1) {
252 event_warn(
"sigaction");
256 if (signal(evsignal, *sh) == SIG_ERR) {
257 event_warn(
"signal");
267 evsignal_del(
struct event *ev)
271 int evsignal = EVENT_SIGNAL(ev);
273 assert(evsignal >= 0 && evsignal < NSIG);
276 TAILQ_REMOVE(&sig->evsigevents[evsignal], ev, ev_signal_next);
278 if (!TAILQ_EMPTY(&sig->evsigevents[evsignal]))
281 event_debug((
"%s: %p: restoring signal handler", __func__, ev));
283 return (_evsignal_restore_handler(ev->ev_base, EVENT_SIGNAL(ev)));
287 evsignal_handler(
int sig)
289 int save_errno = errno;
291 if (evsignal_base == NULL) {
293 "%s: received signal %d, but have no base configured",
298 evsignal_base->sig.evsigcaught[sig]++;
299 evsignal_base->sig.evsignal_caught = 1;
301 #ifndef HAVE_SIGACTION
302 signal(sig, evsignal_handler);
306 send(evsignal_base->sig.ev_signal_pair[0],
"a", 1, 0);
314 struct event *ev, *next_ev;
318 base->sig.evsignal_caught = 0;
319 for (i = 1; i < NSIG; ++
i) {
320 ncalls = sig->evsigcaught[
i];
323 sig->evsigcaught[
i] -= ncalls;
325 for (ev = TAILQ_FIRST(&sig->evsigevents[i]);
326 ev != NULL; ev = next_ev) {
327 next_ev = TAILQ_NEXT(ev, ev_signal_next);
328 if (!(ev->ev_events & EV_PERSIST))
330 event_active(ev, EV_SIGNAL, ncalls);
340 if (base->sig.ev_signal_added) {
341 event_del(&base->sig.ev_signal);
342 base->sig.ev_signal_added = 0;
344 for (i = 0; i < NSIG; ++
i) {
345 if (i < base->sig.sh_old_max && base->sig.sh_old[i] != NULL)
346 _evsignal_restore_handler(base, i);
349 EVUTIL_CLOSESOCKET(base->sig.ev_signal_pair[0]);
350 base->sig.ev_signal_pair[0] = -1;
351 EVUTIL_CLOSESOCKET(base->sig.ev_signal_pair[1]);
352 base->sig.ev_signal_pair[1] = -1;
353 base->sig.sh_old_max = 0;
356 free(base->sig.sh_old);