1 /* $NetBSD: signal.c,v 1.1.1.4 2021/04/07 02:43:13 christos Exp $ */
2 /* $OpenBSD: select.c,v 1.2 2002/06/25 15:50:15 mickey Exp $ */
3
4 /*
5 * Copyright 2000-2007 Niels Provos <provos@citi.umich.edu>
6 * Copyright 2007-2012 Niels Provos and Nick Mathewson
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. The name of the author may not be used to endorse or promote products
17 * derived from this software without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30 #include "event2/event-config.h"
31 #include <sys/cdefs.h>
32 __RCSID("$NetBSD: signal.c,v 1.1.1.4 2021/04/07 02:43:13 christos Exp $");
33 #include "evconfig-private.h"
34
35 #ifdef _WIN32
36 #define WIN32_LEAN_AND_MEAN
37 #include <winsock2.h>
38 #include <windows.h>
39 #undef WIN32_LEAN_AND_MEAN
40 #endif
41 #include <sys/types.h>
42 #ifdef EVENT__HAVE_SYS_TIME_H
43 #include <sys/time.h>
44 #endif
45 #include <sys/queue.h>
46 #ifdef EVENT__HAVE_SYS_SOCKET_H
47 #include <sys/socket.h>
48 #endif
49 #include <signal.h>
50 #include <stdio.h>
51 #include <stdlib.h>
52 #include <string.h>
53 #ifdef EVENT__HAVE_UNISTD_H
54 #include <unistd.h>
55 #endif
56 #include <errno.h>
57 #ifdef EVENT__HAVE_FCNTL_H
58 #include <fcntl.h>
59 #endif
60
61 #include "event2/event.h"
62 #include "event2/event_struct.h"
63 #include "event-internal.h"
64 #include "event2/util.h"
65 #include "evsignal-internal.h"
66 #include "log-internal.h"
67 #include "evmap-internal.h"
68 #include "evthread-internal.h"
69
70 /*
71 signal.c
72
73 This is the signal-handling implementation we use for backends that don't
74 have a better way to do signal handling. It uses sigaction() or signal()
75 to set a signal handler, and a socket pair to tell the event base when
76
77 Note that I said "the event base" : only one event base can be set up to use
78 this at a time. For historical reasons and backward compatibility, if you
79 add an event for a signal to event_base A, then add an event for a signal
80 (any signal!) to event_base B, event_base B will get informed about the
81 signal, but event_base A won't.
82
83 It would be neat to change this behavior in some future version of Libevent.
84 kqueue already does something far more sensible. We can make all backends
85 on Linux do a reasonable thing using signalfd.
86 */
87
88 #ifndef _WIN32
89 /* Windows wants us to call our signal handlers as __cdecl. Nobody else
90 * expects you to do anything crazy like this. */
91 #ifndef __cdecl
92 #define __cdecl
93 #endif
94 #endif
95
96 static int evsig_add(struct event_base *, evutil_socket_t, short, short, void *);
97 static int evsig_del(struct event_base *, evutil_socket_t, short, short, void *);
98
99 static const struct eventop evsigops = {
100 "signal",
101 NULL,
102 evsig_add,
103 evsig_del,
104 NULL,
105 NULL,
106 0, 0, 0
107 };
108
109 #ifndef EVENT__DISABLE_THREAD_SUPPORT
110 /* Lock for evsig_base and evsig_base_n_signals_added fields. */
111 static void *evsig_base_lock = NULL;
112 #endif
113 /* The event base that's currently getting informed about signals. */
114 static struct event_base *evsig_base = NULL;
115 /* A copy of evsig_base->sigev_n_signals_added. */
116 static int evsig_base_n_signals_added = 0;
117 static evutil_socket_t evsig_base_fd = -1;
118
119 static void __cdecl evsig_handler(int sig);
120
121 #define EVSIGBASE_LOCK() EVLOCK_LOCK(evsig_base_lock, 0)
122 #define EVSIGBASE_UNLOCK() EVLOCK_UNLOCK(evsig_base_lock, 0)
123
124 void
evsig_set_base_(struct event_base * base)125 evsig_set_base_(struct event_base *base)
126 {
127 EVSIGBASE_LOCK();
128 evsig_base = base;
129 evsig_base_n_signals_added = base->sig.ev_n_signals_added;
130 evsig_base_fd = base->sig.ev_signal_pair[1];
131 EVSIGBASE_UNLOCK();
132 }
133
134 /* Callback for when the signal handler write a byte to our signaling socket */
135 static void
evsig_cb(evutil_socket_t fd,short what,void * arg)136 evsig_cb(evutil_socket_t fd, short what, void *arg)
137 {
138 static char signals[1024];
139 ev_ssize_t n;
140 int i;
141 int ncaught[NSIG];
142 struct event_base *base;
143
144 base = arg;
145
146 memset(&ncaught, 0, sizeof(ncaught));
147
148 while (1) {
149 #ifdef _WIN32
150 n = recv(fd, signals, sizeof(signals), 0);
151 #else
152 n = read(fd, signals, sizeof(signals));
153 #endif
154 if (n == -1) {
155 int err = evutil_socket_geterror(fd);
156 if (! EVUTIL_ERR_RW_RETRIABLE(err))
157 event_sock_err(1, fd, "%s: recv", __func__);
158 break;
159 } else if (n == 0) {
160 /* XXX warn? */
161 break;
162 }
163 for (i = 0; i < n; ++i) {
164 ev_uint8_t sig = signals[i];
165 if (sig < NSIG)
166 ncaught[sig]++;
167 }
168 }
169
170 EVBASE_ACQUIRE_LOCK(base, th_base_lock);
171 for (i = 0; i < NSIG; ++i) {
172 if (ncaught[i])
173 evmap_signal_active_(base, i, ncaught[i]);
174 }
175 EVBASE_RELEASE_LOCK(base, th_base_lock);
176 }
177
178 int
evsig_init_(struct event_base * base)179 evsig_init_(struct event_base *base)
180 {
181 /*
182 * Our signal handler is going to write to one end of the socket
183 * pair to wake up our event loop. The event loop then scans for
184 * signals that got delivered.
185 */
186 if (evutil_make_internal_pipe_(base->sig.ev_signal_pair) == -1) {
187 #ifdef _WIN32
188 /* Make this nonfatal on win32, where sometimes people
189 have localhost firewalled. */
190 event_sock_warn(-1, "%s: socketpair", __func__);
191 #else
192 event_sock_err(1, -1, "%s: socketpair", __func__);
193 #endif
194 return -1;
195 }
196
197 if (base->sig.sh_old) {
198 mm_free(base->sig.sh_old);
199 }
200 base->sig.sh_old = NULL;
201 base->sig.sh_old_max = 0;
202
203 event_assign(&base->sig.ev_signal, base, base->sig.ev_signal_pair[0],
204 EV_READ | EV_PERSIST, evsig_cb, base);
205
206 base->sig.ev_signal.ev_flags |= EVLIST_INTERNAL;
207 event_priority_set(&base->sig.ev_signal, 0);
208
209 base->evsigsel = &evsigops;
210
211 return 0;
212 }
213
214 /* Helper: set the signal handler for evsignal to handler in base, so that
215 * we can restore the original handler when we clear the current one. */
216 int
evsig_set_handler_(struct event_base * base,int evsignal,void (__cdecl * handler)(int))217 evsig_set_handler_(struct event_base *base,
218 int evsignal, void (__cdecl *handler)(int))
219 {
220 #ifdef EVENT__HAVE_SIGACTION
221 struct sigaction sa;
222 #else
223 ev_sighandler_t sh;
224 #endif
225 struct evsig_info *sig = &base->sig;
226 void *p;
227
228 /*
229 * resize saved signal handler array up to the highest signal number.
230 * a dynamic array is used to keep footprint on the low side.
231 */
232 if (evsignal >= sig->sh_old_max) {
233 int new_max = evsignal + 1;
234 event_debug(("%s: evsignal (%d) >= sh_old_max (%d), resizing",
235 __func__, evsignal, sig->sh_old_max));
236 p = mm_realloc(sig->sh_old, new_max * sizeof(*sig->sh_old));
237 if (p == NULL) {
238 event_warn("realloc");
239 return (-1);
240 }
241
242 memset((char *)p + sig->sh_old_max * sizeof(*sig->sh_old),
243 0, (new_max - sig->sh_old_max) * sizeof(*sig->sh_old));
244
245 sig->sh_old_max = new_max;
246 sig->sh_old = p;
247 }
248
249 /* allocate space for previous handler out of dynamic array */
250 sig->sh_old[evsignal] = mm_malloc(sizeof *sig->sh_old[evsignal]);
251 if (sig->sh_old[evsignal] == NULL) {
252 event_warn("malloc");
253 return (-1);
254 }
255
256 /* save previous handler and setup new handler */
257 #ifdef EVENT__HAVE_SIGACTION
258 memset(&sa, 0, sizeof(sa));
259 sa.sa_handler = handler;
260 sa.sa_flags |= SA_RESTART;
261 sigfillset(&sa.sa_mask);
262
263 if (sigaction(evsignal, &sa, sig->sh_old[evsignal]) == -1) {
264 event_warn("sigaction");
265 mm_free(sig->sh_old[evsignal]);
266 sig->sh_old[evsignal] = NULL;
267 return (-1);
268 }
269 #else
270 if ((sh = signal(evsignal, handler)) == SIG_ERR) {
271 event_warn("signal");
272 mm_free(sig->sh_old[evsignal]);
273 sig->sh_old[evsignal] = NULL;
274 return (-1);
275 }
276 *sig->sh_old[evsignal] = sh;
277 #endif
278
279 return (0);
280 }
281
282 static int
evsig_add(struct event_base * base,evutil_socket_t evsignal,short old,short events,void * p)283 evsig_add(struct event_base *base, evutil_socket_t evsignal, short old, short events, void *p)
284 {
285 struct evsig_info *sig = &base->sig;
286 (void)p;
287
288 EVUTIL_ASSERT(evsignal >= 0 && evsignal < NSIG);
289
290 /* catch signals if they happen quickly */
291 EVSIGBASE_LOCK();
292 if (evsig_base != base && evsig_base_n_signals_added) {
293 event_warnx("Added a signal to event base %p with signals "
294 "already added to event_base %p. Only one can have "
295 "signals at a time with the %s backend. The base with "
296 "the most recently added signal or the most recent "
297 "event_base_loop() call gets preference; do "
298 "not rely on this behavior in future Libevent versions.",
299 base, evsig_base, base->evsel->name);
300 }
301 evsig_base = base;
302 evsig_base_n_signals_added = ++sig->ev_n_signals_added;
303 evsig_base_fd = base->sig.ev_signal_pair[1];
304 EVSIGBASE_UNLOCK();
305
306 event_debug(("%s: %d: changing signal handler", __func__, (int)evsignal));
307 if (evsig_set_handler_(base, (int)evsignal, evsig_handler) == -1) {
308 goto err;
309 }
310
311
312 if (!sig->ev_signal_added) {
313 if (event_add_nolock_(&sig->ev_signal, NULL, 0))
314 goto err;
315 sig->ev_signal_added = 1;
316 }
317
318 return (0);
319
320 err:
321 EVSIGBASE_LOCK();
322 --evsig_base_n_signals_added;
323 --sig->ev_n_signals_added;
324 EVSIGBASE_UNLOCK();
325 return (-1);
326 }
327
328 int
evsig_restore_handler_(struct event_base * base,int evsignal)329 evsig_restore_handler_(struct event_base *base, int evsignal)
330 {
331 int ret = 0;
332 struct evsig_info *sig = &base->sig;
333 #ifdef EVENT__HAVE_SIGACTION
334 struct sigaction *sh;
335 #else
336 ev_sighandler_t *sh;
337 #endif
338
339 if (evsignal >= sig->sh_old_max) {
340 /* Can't actually restore. */
341 /* XXXX.*/
342 return 0;
343 }
344
345 /* restore previous handler */
346 sh = sig->sh_old[evsignal];
347 sig->sh_old[evsignal] = NULL;
348 #ifdef EVENT__HAVE_SIGACTION
349 if (sigaction(evsignal, sh, NULL) == -1) {
350 event_warn("sigaction");
351 ret = -1;
352 }
353 #else
354 if (signal(evsignal, *sh) == SIG_ERR) {
355 event_warn("signal");
356 ret = -1;
357 }
358 #endif
359
360 mm_free(sh);
361
362 return ret;
363 }
364
365 static int
evsig_del(struct event_base * base,evutil_socket_t evsignal,short old,short events,void * p)366 evsig_del(struct event_base *base, evutil_socket_t evsignal, short old, short events, void *p)
367 {
368 EVUTIL_ASSERT(evsignal >= 0 && evsignal < NSIG);
369
370 event_debug(("%s: "EV_SOCK_FMT": restoring signal handler",
371 __func__, EV_SOCK_ARG(evsignal)));
372
373 EVSIGBASE_LOCK();
374 --evsig_base_n_signals_added;
375 --base->sig.ev_n_signals_added;
376 EVSIGBASE_UNLOCK();
377
378 return (evsig_restore_handler_(base, (int)evsignal));
379 }
380
381 static void __cdecl
evsig_handler(int sig)382 evsig_handler(int sig)
383 {
384 int save_errno = errno;
385 #ifdef _WIN32
386 int socket_errno = EVUTIL_SOCKET_ERROR();
387 #endif
388 ev_uint8_t msg;
389
390 if (evsig_base == NULL) {
391 event_warnx(
392 "%s: received signal %d, but have no base configured",
393 __func__, sig);
394 return;
395 }
396
397 #ifndef EVENT__HAVE_SIGACTION
398 signal(sig, evsig_handler);
399 #endif
400
401 /* Wake up our notification mechanism */
402 msg = sig;
403 #ifdef _WIN32
404 send(evsig_base_fd, (char*)&msg, 1, 0);
405 #else
406 {
407 int r = write(evsig_base_fd, (char*)&msg, 1);
408 (void)r; /* Suppress 'unused return value' and 'unused var' */
409 }
410 #endif
411 errno = save_errno;
412 #ifdef _WIN32
413 EVUTIL_SET_SOCKET_ERROR(socket_errno);
414 #endif
415 }
416
417 void
evsig_dealloc_(struct event_base * base)418 evsig_dealloc_(struct event_base *base)
419 {
420 int i = 0;
421 if (base->sig.ev_signal_added) {
422 event_del(&base->sig.ev_signal);
423 base->sig.ev_signal_added = 0;
424 }
425 /* debug event is created in evsig_init_/event_assign even when
426 * ev_signal_added == 0, so unassign is required */
427 event_debug_unassign(&base->sig.ev_signal);
428
429 for (i = 0; i < NSIG; ++i) {
430 if (i < base->sig.sh_old_max && base->sig.sh_old[i] != NULL)
431 evsig_restore_handler_(base, i);
432 }
433 EVSIGBASE_LOCK();
434 if (base == evsig_base) {
435 evsig_base = NULL;
436 evsig_base_n_signals_added = 0;
437 evsig_base_fd = -1;
438 }
439 EVSIGBASE_UNLOCK();
440
441 if (base->sig.ev_signal_pair[0] != -1) {
442 evutil_closesocket(base->sig.ev_signal_pair[0]);
443 base->sig.ev_signal_pair[0] = -1;
444 }
445 if (base->sig.ev_signal_pair[1] != -1) {
446 evutil_closesocket(base->sig.ev_signal_pair[1]);
447 base->sig.ev_signal_pair[1] = -1;
448 }
449 base->sig.sh_old_max = 0;
450
451 /* per index frees are handled in evsig_del() */
452 if (base->sig.sh_old) {
453 mm_free(base->sig.sh_old);
454 base->sig.sh_old = NULL;
455 }
456 }
457
458 static void
evsig_free_globals_locks(void)459 evsig_free_globals_locks(void)
460 {
461 #ifndef EVENT__DISABLE_THREAD_SUPPORT
462 if (evsig_base_lock != NULL) {
463 EVTHREAD_FREE_LOCK(evsig_base_lock, 0);
464 evsig_base_lock = NULL;
465 }
466 #endif
467 return;
468 }
469
470 void
evsig_free_globals_(void)471 evsig_free_globals_(void)
472 {
473 evsig_free_globals_locks();
474 }
475
476 #ifndef EVENT__DISABLE_THREAD_SUPPORT
477 int
evsig_global_setup_locks_(const int enable_locks)478 evsig_global_setup_locks_(const int enable_locks)
479 {
480 EVTHREAD_SETUP_GLOBAL_LOCK(evsig_base_lock, 0);
481 return 0;
482 }
483
484 #endif
485