1 /* $NetBSD: kqueue.c,v 1.1.1.3 2017/01/31 21:14:52 christos Exp $ */ 2 /* $OpenBSD: kqueue.c,v 1.5 2002/07/10 14:41:31 art 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: kqueue.c,v 1.1.1.3 2017/01/31 21:14:52 christos Exp $"); 33 #include "evconfig-private.h" 34 35 #ifdef EVENT__HAVE_KQUEUE 36 37 #include <sys/types.h> 38 #ifdef EVENT__HAVE_SYS_TIME_H 39 #include <sys/time.h> 40 #endif 41 #include <sys/queue.h> 42 #include <sys/event.h> 43 #include <signal.h> 44 #include <stdio.h> 45 #include <stdlib.h> 46 #include <string.h> 47 #include <unistd.h> 48 #include <errno.h> 49 #ifdef EVENT__HAVE_INTTYPES_H 50 #include <inttypes.h> 51 #endif 52 53 /* Some platforms apparently define the udata field of struct kevent as 54 * intptr_t, whereas others define it as void*. There doesn't seem to be an 55 * easy way to tell them apart via autoconf, so we need to use OS macros. */ 56 #if defined(EVENT__HAVE_INTTYPES_H) && !defined(__OpenBSD__) && !defined(__FreeBSD__) && !defined(__darwin__) && !defined(__APPLE__) && !defined(__CloudABI__) 57 #define PTR_TO_UDATA(x) ((intptr_t)(x)) 58 #define INT_TO_UDATA(x) ((intptr_t)(x)) 59 #else 60 #define PTR_TO_UDATA(x) (x) 61 #define INT_TO_UDATA(x) ((void*)(x)) 62 #endif 63 64 #include "event-internal.h" 65 #include "log-internal.h" 66 #include "evmap-internal.h" 67 #include "event2/thread.h" 68 #include "evthread-internal.h" 69 #include "changelist-internal.h" 70 71 #include "kqueue-internal.h" 72 73 #define NEVENT 64 74 75 struct kqop { 76 struct kevent *changes; 77 int changes_size; 78 79 struct kevent *events; 80 int events_size; 81 int kq; 82 int notify_event_added; 83 pid_t pid; 84 }; 85 86 static void kqop_free(struct kqop *kqop); 87 88 static void *kq_init(struct event_base *); 89 static int kq_sig_add(struct event_base *, int, short, short, void *); 90 static int kq_sig_del(struct event_base *, int, short, short, void *); 91 static int kq_dispatch(struct event_base *, struct timeval *); 92 static void kq_dealloc(struct event_base *); 93 94 const struct eventop kqops = { 95 "kqueue", 96 kq_init, 97 event_changelist_add_, 98 event_changelist_del_, 99 kq_dispatch, 100 kq_dealloc, 101 1 /* need reinit */, 102 EV_FEATURE_ET|EV_FEATURE_O1|EV_FEATURE_FDS, 103 EVENT_CHANGELIST_FDINFO_SIZE 104 }; 105 106 static const struct eventop kqsigops = { 107 "kqueue_signal", 108 NULL, 109 kq_sig_add, 110 kq_sig_del, 111 NULL, 112 NULL, 113 1 /* need reinit */, 114 0, 115 0 116 }; 117 118 static void * 119 kq_init(struct event_base *base) 120 { 121 int kq = -1; 122 struct kqop *kqueueop = NULL; 123 124 if (!(kqueueop = mm_calloc(1, sizeof(struct kqop)))) 125 return (NULL); 126 127 /* Initialize the kernel queue */ 128 129 if ((kq = kqueue()) == -1) { 130 event_warn("kqueue"); 131 goto err; 132 } 133 134 kqueueop->kq = kq; 135 136 kqueueop->pid = getpid(); 137 138 /* Initialize fields */ 139 kqueueop->changes = mm_calloc(NEVENT, sizeof(struct kevent)); 140 if (kqueueop->changes == NULL) 141 goto err; 142 kqueueop->events = mm_calloc(NEVENT, sizeof(struct kevent)); 143 if (kqueueop->events == NULL) 144 goto err; 145 kqueueop->events_size = kqueueop->changes_size = NEVENT; 146 147 /* Check for Mac OS X kqueue bug. */ 148 memset(&kqueueop->changes[0], 0, sizeof kqueueop->changes[0]); 149 kqueueop->changes[0].ident = -1; 150 kqueueop->changes[0].filter = EVFILT_READ; 151 kqueueop->changes[0].flags = EV_ADD; 152 /* 153 * If kqueue works, then kevent will succeed, and it will 154 * stick an error in events[0]. If kqueue is broken, then 155 * kevent will fail. 156 */ 157 if (kevent(kq, 158 kqueueop->changes, 1, kqueueop->events, NEVENT, NULL) != 1 || 159 (int)kqueueop->events[0].ident != -1 || 160 !(kqueueop->events[0].flags & EV_ERROR)) { 161 event_warn("%s: detected broken kqueue; not using.", __func__); 162 goto err; 163 } 164 165 base->evsigsel = &kqsigops; 166 167 return (kqueueop); 168 err: 169 if (kqueueop) 170 kqop_free(kqueueop); 171 172 return (NULL); 173 } 174 175 #define ADD_UDATA 0x30303 176 177 static void 178 kq_setup_kevent(struct kevent *out, evutil_socket_t fd, int filter, short change) 179 { 180 memset(out, 0, sizeof(struct kevent)); 181 out->ident = fd; 182 out->filter = filter; 183 184 if (change & EV_CHANGE_ADD) { 185 out->flags = EV_ADD; 186 /* We set a magic number here so that we can tell 'add' 187 * errors from 'del' errors. */ 188 out->udata = INT_TO_UDATA(ADD_UDATA); 189 if (change & EV_ET) 190 out->flags |= EV_CLEAR; 191 #ifdef NOTE_EOF 192 /* Make it behave like select() and poll() */ 193 if (filter == EVFILT_READ) 194 out->fflags = NOTE_EOF; 195 #endif 196 } else { 197 EVUTIL_ASSERT(change & EV_CHANGE_DEL); 198 out->flags = EV_DELETE; 199 } 200 } 201 202 static int 203 kq_build_changes_list(const struct event_changelist *changelist, 204 struct kqop *kqop) 205 { 206 int i; 207 int n_changes = 0; 208 209 for (i = 0; i < changelist->n_changes; ++i) { 210 struct event_change *in_ch = &changelist->changes[i]; 211 struct kevent *out_ch; 212 if (n_changes >= kqop->changes_size - 1) { 213 int newsize = kqop->changes_size * 2; 214 struct kevent *newchanges; 215 216 newchanges = mm_realloc(kqop->changes, 217 newsize * sizeof(struct kevent)); 218 if (newchanges == NULL) { 219 event_warn("%s: realloc", __func__); 220 return (-1); 221 } 222 kqop->changes = newchanges; 223 kqop->changes_size = newsize; 224 } 225 if (in_ch->read_change) { 226 out_ch = &kqop->changes[n_changes++]; 227 kq_setup_kevent(out_ch, in_ch->fd, EVFILT_READ, 228 in_ch->read_change); 229 } 230 if (in_ch->write_change) { 231 out_ch = &kqop->changes[n_changes++]; 232 kq_setup_kevent(out_ch, in_ch->fd, EVFILT_WRITE, 233 in_ch->write_change); 234 } 235 } 236 return n_changes; 237 } 238 239 static int 240 kq_grow_events(struct kqop *kqop, size_t new_size) 241 { 242 struct kevent *newresult; 243 244 newresult = mm_realloc(kqop->events, 245 new_size * sizeof(struct kevent)); 246 247 if (newresult) { 248 kqop->events = newresult; 249 kqop->events_size = new_size; 250 return 0; 251 } else { 252 return -1; 253 } 254 } 255 256 static int 257 kq_dispatch(struct event_base *base, struct timeval *tv) 258 { 259 struct kqop *kqop = base->evbase; 260 struct kevent *events = kqop->events; 261 struct kevent *changes; 262 struct timespec ts, *ts_p = NULL; 263 int i, n_changes, res; 264 265 if (tv != NULL) { 266 ts.tv_sec = tv->tv_sec; 267 ts.tv_nsec = tv->tv_usec * 1000; 268 ts_p = &ts; 269 } 270 271 /* Build "changes" from "base->changes" */ 272 EVUTIL_ASSERT(kqop->changes); 273 n_changes = kq_build_changes_list(&base->changelist, kqop); 274 if (n_changes < 0) 275 return -1; 276 277 event_changelist_remove_all_(&base->changelist, base); 278 279 /* steal the changes array in case some broken code tries to call 280 * dispatch twice at once. */ 281 changes = kqop->changes; 282 kqop->changes = NULL; 283 284 /* Make sure that 'events' is at least as long as the list of changes: 285 * otherwise errors in the changes can get reported as a -1 return 286 * value from kevent() rather than as EV_ERROR events in the events 287 * array. 288 * 289 * (We could instead handle -1 return values from kevent() by 290 * retrying with a smaller changes array or a larger events array, 291 * but this approach seems less risky for now.) 292 */ 293 if (kqop->events_size < n_changes) { 294 int new_size = kqop->events_size; 295 do { 296 new_size *= 2; 297 } while (new_size < n_changes); 298 299 kq_grow_events(kqop, new_size); 300 events = kqop->events; 301 } 302 303 EVBASE_RELEASE_LOCK(base, th_base_lock); 304 305 res = kevent(kqop->kq, changes, n_changes, 306 events, kqop->events_size, ts_p); 307 308 EVBASE_ACQUIRE_LOCK(base, th_base_lock); 309 310 EVUTIL_ASSERT(kqop->changes == NULL); 311 kqop->changes = changes; 312 313 if (res == -1) { 314 if (errno != EINTR) { 315 event_warn("kevent"); 316 return (-1); 317 } 318 319 return (0); 320 } 321 322 event_debug(("%s: kevent reports %d", __func__, res)); 323 324 for (i = 0; i < res; i++) { 325 int which = 0; 326 327 if (events[i].flags & EV_ERROR) { 328 switch (events[i].data) { 329 330 /* Can occur on delete if we are not currently 331 * watching any events on this fd. That can 332 * happen when the fd was closed and another 333 * file was opened with that fd. */ 334 case ENOENT: 335 /* Can occur for reasons not fully understood 336 * on FreeBSD. */ 337 case EINVAL: 338 continue; 339 #if defined(__FreeBSD__) 340 /* 341 * This currently occurs if an FD is closed 342 * before the EV_DELETE makes it out via kevent(). 343 * The FreeBSD capabilities code sees the blank 344 * capability set and rejects the request to 345 * modify an event. 346 * 347 * To be strictly correct - when an FD is closed, 348 * all the registered events are also removed. 349 * Queuing EV_DELETE to a closed FD is wrong. 350 * The event(s) should just be deleted from 351 * the pending changelist. 352 */ 353 case ENOTCAPABLE: 354 continue; 355 #endif 356 357 /* Can occur on a delete if the fd is closed. */ 358 case EBADF: 359 /* XXXX On NetBSD, we can also get EBADF if we 360 * try to add the write side of a pipe, but 361 * the read side has already been closed. 362 * Other BSDs call this situation 'EPIPE'. It 363 * would be good if we had a way to report 364 * this situation. */ 365 continue; 366 /* These two can occur on an add if the fd was one side 367 * of a pipe, and the other side was closed. */ 368 case EPERM: 369 case EPIPE: 370 /* Report read events, if we're listening for 371 * them, so that the user can learn about any 372 * add errors. (If the operation was a 373 * delete, then udata should be cleared.) */ 374 if (events[i].udata) { 375 /* The operation was an add: 376 * report the error as a read. */ 377 which |= EV_READ; 378 break; 379 } else { 380 /* The operation was a del: 381 * report nothing. */ 382 continue; 383 } 384 385 /* Other errors shouldn't occur. */ 386 default: 387 errno = events[i].data; 388 return (-1); 389 } 390 } else if (events[i].filter == EVFILT_READ) { 391 which |= EV_READ; 392 } else if (events[i].filter == EVFILT_WRITE) { 393 which |= EV_WRITE; 394 } else if (events[i].filter == EVFILT_SIGNAL) { 395 which |= EV_SIGNAL; 396 #ifdef EVFILT_USER 397 } else if (events[i].filter == EVFILT_USER) { 398 base->is_notify_pending = 0; 399 #endif 400 } 401 402 if (!which) 403 continue; 404 405 if (events[i].filter == EVFILT_SIGNAL) { 406 evmap_signal_active_(base, events[i].ident, 1); 407 } else { 408 evmap_io_active_(base, events[i].ident, which | EV_ET); 409 } 410 } 411 412 if (res == kqop->events_size) { 413 /* We used all the events space that we have. Maybe we should 414 make it bigger. */ 415 kq_grow_events(kqop, kqop->events_size * 2); 416 } 417 418 return (0); 419 } 420 421 static void 422 kqop_free(struct kqop *kqop) 423 { 424 if (kqop->changes) 425 mm_free(kqop->changes); 426 if (kqop->events) 427 mm_free(kqop->events); 428 if (kqop->kq >= 0 && kqop->pid == getpid()) 429 close(kqop->kq); 430 memset(kqop, 0, sizeof(struct kqop)); 431 mm_free(kqop); 432 } 433 434 static void 435 kq_dealloc(struct event_base *base) 436 { 437 struct kqop *kqop = base->evbase; 438 evsig_dealloc_(base); 439 kqop_free(kqop); 440 } 441 442 /* signal handling */ 443 static int 444 kq_sig_add(struct event_base *base, int nsignal, short old, short events, void *p) 445 { 446 struct kqop *kqop = base->evbase; 447 struct kevent kev; 448 struct timespec timeout = { 0, 0 }; 449 (void)p; 450 451 EVUTIL_ASSERT(nsignal >= 0 && nsignal < NSIG); 452 453 memset(&kev, 0, sizeof(kev)); 454 kev.ident = nsignal; 455 kev.filter = EVFILT_SIGNAL; 456 kev.flags = EV_ADD; 457 458 /* Be ready for the signal if it is sent any 459 * time between now and the next call to 460 * kq_dispatch. */ 461 if (kevent(kqop->kq, &kev, 1, NULL, 0, &timeout) == -1) 462 return (-1); 463 464 /* We can set the handler for most signals to SIG_IGN and 465 * still have them reported to us in the queue. However, 466 * if the handler for SIGCHLD is SIG_IGN, the system reaps 467 * zombie processes for us, and we don't get any notification. 468 * This appears to be the only signal with this quirk. */ 469 if (evsig_set_handler_(base, nsignal, 470 nsignal == SIGCHLD ? SIG_DFL : SIG_IGN) == -1) 471 return (-1); 472 473 return (0); 474 } 475 476 static int 477 kq_sig_del(struct event_base *base, int nsignal, short old, short events, void *p) 478 { 479 struct kqop *kqop = base->evbase; 480 struct kevent kev; 481 482 struct timespec timeout = { 0, 0 }; 483 (void)p; 484 485 EVUTIL_ASSERT(nsignal >= 0 && nsignal < NSIG); 486 487 memset(&kev, 0, sizeof(kev)); 488 kev.ident = nsignal; 489 kev.filter = EVFILT_SIGNAL; 490 kev.flags = EV_DELETE; 491 492 /* Because we insert signal events 493 * immediately, we need to delete them 494 * immediately, too */ 495 if (kevent(kqop->kq, &kev, 1, NULL, 0, &timeout) == -1) 496 return (-1); 497 498 if (evsig_restore_handler_(base, nsignal) == -1) 499 return (-1); 500 501 return (0); 502 } 503 504 505 /* OSX 10.6 and FreeBSD 8.1 add support for EVFILT_USER, which we can use 506 * to wake up the event loop from another thread. */ 507 508 /* Magic number we use for our filter ID. */ 509 #define NOTIFY_IDENT 42 510 511 int 512 event_kq_add_notify_event_(struct event_base *base) 513 { 514 struct kqop *kqop = base->evbase; 515 #if defined(EVFILT_USER) && defined(NOTE_TRIGGER) 516 struct kevent kev; 517 struct timespec timeout = { 0, 0 }; 518 #endif 519 520 if (kqop->notify_event_added) 521 return 0; 522 523 #if defined(EVFILT_USER) && defined(NOTE_TRIGGER) 524 memset(&kev, 0, sizeof(kev)); 525 kev.ident = NOTIFY_IDENT; 526 kev.filter = EVFILT_USER; 527 kev.flags = EV_ADD | EV_CLEAR; 528 529 if (kevent(kqop->kq, &kev, 1, NULL, 0, &timeout) == -1) { 530 event_warn("kevent: adding EVFILT_USER event"); 531 return -1; 532 } 533 534 kqop->notify_event_added = 1; 535 536 return 0; 537 #else 538 return -1; 539 #endif 540 } 541 542 int 543 event_kq_notify_base_(struct event_base *base) 544 { 545 struct kqop *kqop = base->evbase; 546 #if defined(EVFILT_USER) && defined(NOTE_TRIGGER) 547 struct kevent kev; 548 struct timespec timeout = { 0, 0 }; 549 #endif 550 if (! kqop->notify_event_added) 551 return -1; 552 553 #if defined(EVFILT_USER) && defined(NOTE_TRIGGER) 554 memset(&kev, 0, sizeof(kev)); 555 kev.ident = NOTIFY_IDENT; 556 kev.filter = EVFILT_USER; 557 kev.fflags = NOTE_TRIGGER; 558 559 if (kevent(kqop->kq, &kev, 1, NULL, 0, &timeout) == -1) { 560 event_warn("kevent: triggering EVFILT_USER event"); 561 return -1; 562 } 563 564 return 0; 565 #else 566 return -1; 567 #endif 568 } 569 570 #endif /* EVENT__HAVE_KQUEUE */ 571