| 6 |
| * |
| 7 | * Redistribution and use in source and binary forms, with or without |
| 8 | * modification, are permitted provided that the following conditions |
| 9 | * are met: |
| 10 | * 1. Redistributions of source code must retain the above copyright |
| 11 | * notice, this list of conditions and the following disclaimer. |
| 12 | * 2. Redistributions in binary form must reproduce the above copyright |
| 13 | * notice, this list of conditions and the following disclaimer in the |
| 14 | * documentation and/or other materials provided with the distribution. |
| 15 | * 3. The name of the author may not be used to endorse or promote products |
| 16 | * derived from this software without specific prior written permission. |
| 17 | * |
| 18 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
| 19 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
| 20 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
| 21 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 22 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
| 23 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 24 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 25 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 26 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
| 27 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 28 | */ |
| 29 |
| 30 | #include "config.h" |
| 31 |
| 32 | #ifdef WIN32 |
| 33 | #define WIN32_LEAN_AND_MEAN |
| 34 | #include <winsock2.h> |
| 35 | #include <windows.h> |
| 36 | #undef WIN32_LEAN_AND_MEAN |
| 37 | #endif |
| 38 | #include <sys/types.h> |
| 39 | #ifdef HAVE_SYS_TIME_H |
| 40 | #include <sys/time.h> |
| 41 | #endif |
| 42 | #include <sys/queue.h> |
| 43 | #ifdef HAVE_SYS_SOCKET_H |
| 44 | #include <sys/socket.h> |
| 45 | #endif |
| 46 | #include <signal.h> |
| 47 | #include <stdio.h> |
| 48 | #include <stdlib.h> |
| 49 | #include <string.h> |
| 50 | #ifdef HAVE_UNISTD_H |
| 51 | #include <unistd.h> |
| 52 | #endif |
| 53 | #include <errno.h> |
| 54 | #ifdef HAVE_FCNTL_H |
| 55 | #include <fcntl.h> |
| 56 | #endif |
| 57 | #include <assert.h> |
| 58 |
| 59 | #include "event.h" |
| 60 | #include "event-internal.h" |
| 61 | #include "evsignal.h" |
| 62 | #include "evutil.h" |
| 63 | #include "log.h" |
| 64 |
| 65 | struct event_base *evsignal_base = NULL; |
| 66 |
| 67 | static void evsignal_handler(int sig); |
| 68 |
| 69 | #ifdef WIN32 |
| 70 | #define error_is_eagain(err) \ |
| 71 | ((err) == EAGAIN || (err) == WSAEWOULDBLOCK) |
| 72 | #else |
| 73 | #define error_is_eagain(err) ((err) == EAGAIN) |
| 74 | #endif |
| 75 |
| 76 | /* Callback for when the signal handler write a byte to our signaling socket */ |
| 77 | static void |
| 78 | evsignal_cb(int fd, short what, void *arg) |
| 79 | { |
| 80 | static char signals[1]; |
| 81 | #ifdef WIN32 |
| 82 | SSIZE_T n; |
| 83 | #else |
| 84 | ssize_t n; |
| 85 | #endif |
| 86 |
| 87 | n = recv(fd, signals, sizeof(signals), 0); |
| 88 | if (n == -1) { |
| 89 | int err = EVUTIL_SOCKET_ERROR(); |
| 90 | if (! error_is_eagain(err)) |
| 91 | event_err(1, "%s: read", __func__); |
| 92 | } |
| 93 | } |
| 94 |
| 95 | #ifdef HAVE_SETFD |
| 96 | #define FD_CLOSEONEXEC(x) do { \ |
| 97 | if (fcntl(x, F_SETFD, 1) == -1) \ |
| 98 | event_warn("fcntl(%d, F_SETFD)", x); \ |
| 99 | } while (0) |
| 100 | #else |
| 101 | #define FD_CLOSEONEXEC(x) |
| 102 | #endif |
| 103 |
| 104 | int |
| 105 | evsignal_init(struct event_base *base) |
| 106 | { |
| 107 | int i; |
| 108 |
| 109 | /* |
| 110 | * Our signal handler is going to write to one end of the socket |
| 111 | * pair to wake up our event loop. The event loop then scans for |
| 112 | * signals that got delivered. |
| 113 | */ |
| 114 | if (evutil_socketpair( |
| 115 | AF_UNIX, SOCK_STREAM, 0, base->sig.ev_signal_pair) == -1) { |
| 116 | #ifdef WIN32 |
| 117 | /* Make this nonfatal on win32, where sometimes people |
| 118 | have localhost firewalled. */ |
| 119 | event_warn("%s: socketpair", __func__); |
| 120 | #else |
| 121 | event_err(1, "%s: socketpair", __func__); |
| 122 | #endif |
| 123 | return -1; |
| 124 | } |
| 125 |
| 126 | FD_CLOSEONEXEC(base->sig.ev_signal_pair[0]); |
| 127 | FD_CLOSEONEXEC(base->sig.ev_signal_pair[1]); |
| 128 | base->sig.sh_old = NULL; |
| 129 | base->sig.sh_old_max = 0; |
| 130 | base->sig.evsignal_caught = 0; |
| 131 | memset(&base->sig.evsigcaught, 0, sizeof(sig_atomic_t)*NSIG); |
| 132 | /* initialize the queues for all events */ |
| 133 | for (i = 0; i < NSIG; ++i) |
| 134 | TAILQ_INIT(&base->sig.evsigevents[i]); |
| 135 |
| 136 | evutil_make_socket_nonblocking(base->sig.ev_signal_pair[0]); |
| 137 | evutil_make_socket_nonblocking(base->sig.ev_signal_pair[1]); |
| 138 |
| 139 | event_set(&base->sig.ev_signal, base->sig.ev_signal_pair[1], |
| 140 | EV_READ | EV_PERSIST, evsignal_cb, &base->sig.ev_signal); |
| 141 | base->sig.ev_signal.ev_base = base; |
| 142 | base->sig.ev_signal.ev_flags |= EVLIST_INTERNAL; |
| 143 |
| 144 | return 0; |
| 145 | } |
| 146 |
| 147 | /* Helper: set the signal handler for evsignal to handler in base, so that |
| 148 | * we can restore the original handler when we clear the current one. */ |
| 149 | int |
| 150 | _evsignal_set_handler(struct event_base *base, |
| 151 | int evsignal, void (*handler)(int)) |
| 152 | { |
| 153 | #ifdef HAVE_SIGACTION |
| 154 | struct sigaction sa; |
| 155 | #else |
| 156 | ev_sighandler_t sh; |
| 157 | #endif |
| 158 | struct evsignal_info *sig = &base->sig; |
| 159 | void *p; |
| 160 |
| 161 | /* |
| 162 | * resize saved signal handler array up to the highest signal number. |
| 163 | * a dynamic array is used to keep footprint on the low side. |
| 164 | */ |
| 165 | if (evsignal >= sig->sh_old_max) { |
| 166 | int new_max = evsignal + 1; |
| 167 | event_debug(("%s: evsignal (%d) >= sh_old_max (%d), resizing", |
| 168 | __func__, evsignal, sig->sh_old_max)); |
| 169 | p = realloc(sig->sh_old, new_max * sizeof(*sig->sh_old)); |
| 170 | if (p == NULL) { |
| 171 | event_warn("realloc"); |
| 172 | return (-1); |
| 173 | } |
| 174 |
| 175 | memset((char *)p + sig->sh_old_max * sizeof(*sig->sh_old), |
| 176 | 0, (new_max - sig->sh_old_max) * sizeof(*sig->sh_old)); |
| 177 |
| 178 | sig->sh_old_max = new_max; |
| 179 | sig->sh_old = p; |
| 180 | } |
| 181 |
| 182 | /* allocate space for previous handler out of dynamic array */ |
| 183 | sig->sh_old[evsignal] = malloc(sizeof *sig->sh_old[evsignal]); |
| 184 | if (sig->sh_old[evsignal] == NULL) { |
| 185 | event_warn("malloc"); |
| 186 | return (-1); |
| 187 | } |
| 188 |
| 189 | /* save previous handler and setup new handler */ |
| 190 | #ifdef HAVE_SIGACTION |
| 191 | memset(&sa, 0, sizeof(sa)); |
| 192 | sa.sa_handler = handler; |
| 193 | sa.sa_flags |= SA_RESTART; |
| 194 | sigfillset(&sa.sa_mask); |
| 195 |
| 196 | if (sigaction(evsignal, &sa, sig->sh_old[evsignal]) == -1) { |
| 197 | event_warn("sigaction"); |
| 198 | free(sig->sh_old[evsignal]); |
| 199 | sig->sh_old[evsignal] = NULL; |
| 200 | return (-1); |
| 201 | } |
| 202 | #else |
| 203 | if ((sh = signal(evsignal, handler)) == SIG_ERR) { |
| 204 | event_warn("signal"); |
| 205 | free(sig->sh_old[evsignal]); |
| 206 | sig->sh_old[evsignal] = NULL; |
| 207 | return (-1); |
| 208 | } |
| 209 | *sig->sh_old[evsignal] = sh; |
| 210 | #endif |
| 211 |
| 212 | return (0); |
| 213 | } |
| 214 |
| 215 | int |
| 216 | evsignal_add(struct event *ev) |
| 217 | { |
| 218 | int evsignal; |
| 219 | struct event_base *base = ev->ev_base; |
| 220 | struct evsignal_info *sig = &ev->ev_base->sig; |
| 221 |
| 222 | if (ev->ev_events & (EV_READ|EV_WRITE)) |
| 223 | event_errx(1, "%s: EV_SIGNAL incompatible use", __func__); |
| 224 | evsignal = EVENT_SIGNAL(ev); |
| 225 | assert(evsignal >= 0 && evsignal < NSIG); |
| 226 | if (TAILQ_EMPTY(&sig->evsigevents[evsignal])) { |
| 227 | event_debug(("%s: %p: changing signal handler", __func__, ev)); |
| 228 | if (_evsignal_set_handler( |
| 229 | base, evsignal, evsignal_handler) == -1) |
| 230 | return (-1); |
| 231 |
| 232 | /* catch signals if they happen quickly */ |
| 233 | evsignal_base = base; |
| 234 |
| 235 | if (!sig->ev_signal_added) { |
| 236 | if (event_add(&sig->ev_signal, NULL)) |
| 237 | return (-1); |
| 238 | sig->ev_signal_added = 1; |
| 239 | } |
| 240 | } |
| 241 |
| 242 | /* multiple events may listen to the same signal */ |
| 243 | TAILQ_INSERT_TAIL(&sig->evsigevents[evsignal], ev, ev_signal_next); |
| 244 |
| 245 | return (0); |
| 246 | } |
| 247 |
| 248 | int |
| 249 | _evsignal_restore_handler(struct event_base *base, int evsignal) |
| 250 | { |
| 251 | int ret = 0; |
| 252 | struct evsignal_info *sig = &base->sig; |
| 253 | #ifdef HAVE_SIGACTION |
| 254 | struct sigaction *sh; |
| 255 | #else |
| 256 | ev_sighandler_t *sh; |
| 257 | #endif |
| 258 |
| 259 | /* restore previous handler */ |
| 260 | sh = sig->sh_old[evsignal]; |
| 261 | sig->sh_old[evsignal] = NULL; |
| 262 | #ifdef HAVE_SIGACTION |
| 263 | if (sigaction(evsignal, sh, NULL) == -1) { |
| 264 | event_warn("sigaction"); |
| 265 | ret = -1; |
| 266 | } |
| 267 | #else |
| 268 | if (signal(evsignal, *sh) == SIG_ERR) { |
| 269 | event_warn("signal"); |
| 270 | ret = -1; |
| 271 | } |
| 272 | #endif |
| 273 | free(sh); |
| 274 |
| 275 | return ret; |
| 276 | } |
| 277 |
| 278 | int |
| 279 | evsignal_del(struct event *ev) |
| 280 | { |
| 281 | struct event_base *base = ev->ev_base; |
| 282 | struct evsignal_info *sig = &base->sig; |
| 283 | int evsignal = EVENT_SIGNAL(ev); |
| 284 |
| 285 | assert(evsignal >= 0 && evsignal < NSIG); |
| 286 |
| 287 | /* multiple events may listen to the same signal */ |
| 288 | TAILQ_REMOVE(&sig->evsigevents[evsignal], ev, ev_signal_next); |
| 289 |
| 290 | if (!TAILQ_EMPTY(&sig->evsigevents[evsignal])) |
| 291 | return (0); |
| 292 |
| 293 | event_debug(("%s: %p: restoring signal handler", __func__, ev)); |
| 294 |
| 295 | return (_evsignal_restore_handler(ev->ev_base, EVENT_SIGNAL(ev))); |
| 296 | } |
| 297 |
| 298 | static void |
| 299 | evsignal_handler(int sig) |
| 300 | { |
| 301 | int save_errno = errno; |
| 302 |
| 303 | if (evsignal_base == NULL) { |
| 304 | event_warn( |
| 305 | "%s: received signal %d, but have no base configured", |
| 306 | __func__, sig); |
| 307 | return; |
| 308 | } |
| 309 |
| 310 | evsignal_base->sig.evsigcaught[sig]++; |
| 311 | evsignal_base->sig.evsignal_caught = 1; |
| 312 |
| 313 | #ifndef HAVE_SIGACTION |
| 314 | signal(sig, evsignal_handler); |
| 315 | #endif |
| 316 |
| 317 | /* Wake up our notification mechanism */ |
| 318 | send(evsignal_base->sig.ev_signal_pair[0], "a", 1, 0); |
| 319 | errno = save_errno; |
| 320 | } |
| 321 |
| 322 | void |
| 323 | evsignal_process(struct event_base *base) |
| 324 | { |
| 325 | struct evsignal_info *sig = &base->sig; |
| 326 | struct event *ev, *next_ev; |
| 327 | sig_atomic_t ncalls; |
| 328 | int i; |
| 329 |
| 330 | base->sig.evsignal_caught = 0; |
| 331 | for (i = 1; i < NSIG; ++i) { |
| 332 | ncalls = sig->evsigcaught[i]; |
| 333 | if (ncalls == 0) |
| 334 | continue; |
| 335 | sig->evsigcaught[i] -= ncalls; |
| 336 |
| 337 | for (ev = TAILQ_FIRST(&sig->evsigevents[i]); |
| 338 | ev != NULL; ev = next_ev) { |
| 339 | next_ev = TAILQ_NEXT(ev, ev_signal_next); |
| 340 | if (!(ev->ev_events & EV_PERSIST)) |
| 341 | event_del(ev); |
| 342 | event_active(ev, EV_SIGNAL, ncalls); |
| 343 | } |
| 344 |
| 345 | } |
| 346 | } |
| 347 |
| 348 | void |
| 349 | evsignal_dealloc(struct event_base *base) |
| 350 | { |
| 351 | int i = 0; |
| 352 | if (base->sig.ev_signal_added) { |
| 353 | event_del(&base->sig.ev_signal); |
| 354 | base->sig.ev_signal_added = 0; |
| 355 | } |
| 356 | for (i = 0; i < NSIG; ++i) { |
| 357 | if (i < base->sig.sh_old_max && base->sig.sh_old[i] != NULL) |
| 358 | _evsignal_restore_handler(base, i); |
| 359 | } |
| 360 |
| 361 | if (base->sig.ev_signal_pair[0] != -1) { |
| 362 | EVUTIL_CLOSESOCKET(base->sig.ev_signal_pair[0]); |
| 363 | base->sig.ev_signal_pair[0] = -1; |
| 364 | } |
| 365 | if (base->sig.ev_signal_pair[1] != -1) { |
| 366 | EVUTIL_CLOSESOCKET(base->sig.ev_signal_pair[1]); |
| 367 | base->sig.ev_signal_pair[1] = -1; |
| 368 | } |
| 369 | base->sig.sh_old_max = 0; |
| 370 |
| 371 | /* per index frees are handled in evsig_del() */ |
| 372 | if (base->sig.sh_old) { |
| 373 | free(base->sig.sh_old); |
| 374 | base->sig.sh_old = NULL; |
| 375 | } |
| 376 | } |
| 377 |