1 /* $OpenBSD: kqueue.c,v 1.5 2002/07/10 14:41:31 art Exp $ */ 2 3 /* 4 * Copyright 2000-2007 Niels Provos <[email protected]> 5 * Copyright 2007-2010 Niels Provos and Nick Mathewson 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 #include "event2/event-config.h" 30 #include "evconfig-private.h" 31 32 #include <sys/types.h> 33 #ifdef _EVENT_HAVE_SYS_TIME_H 34 #include <sys/time.h> 35 #endif 36 #include <sys/queue.h> 37 #include <sys/event.h> 38 #include <signal.h> 39 #include <stdio.h> 40 #include <stdlib.h> 41 #include <string.h> 42 #include <unistd.h> 43 #include <errno.h> 44 #ifdef _EVENT_HAVE_INTTYPES_H 45 #include <inttypes.h> 46 #endif 47 48 /* Some platforms apparently define the udata field of struct kevent as 49 * intptr_t, whereas others define it as void*. There doesn't seem to be an 50 * easy way to tell them apart via autoconf, so we need to use OS macros. */ 51 #if defined(_EVENT_HAVE_INTTYPES_H) && !defined(__OpenBSD__) && !defined(__FreeBSD__) && !defined(__darwin__) && !defined(__APPLE__) 52 #define PTR_TO_UDATA(x) ((intptr_t)(x)) 53 #else 54 #define PTR_TO_UDATA(x) (x) 55 #endif 56 57 #include "event-internal.h" 58 #include "log-internal.h" 59 #include "evmap-internal.h" 60 #include "event2/thread.h" 61 #include "evthread-internal.h" 62 #include "changelist-internal.h" 63 64 #define NEVENT 64 65 66 struct kqop { 67 struct kevent *changes; 68 int changes_size; 69 70 struct kevent *events; 71 int events_size; 72 int kq; 73 pid_t pid; 74 }; 75 76 static void kqop_free(struct kqop *kqop); 77 78 static void *kq_init(struct event_base *); 79 static int kq_sig_add(struct event_base *, int, short, short, void *); 80 static int kq_sig_del(struct event_base *, int, short, short, void *); 81 static int kq_dispatch(struct event_base *, struct timeval *); 82 static void kq_dealloc(struct event_base *); 83 84 const struct eventop kqops = { 85 "kqueue", 86 kq_init, 87 event_changelist_add, 88 event_changelist_del, 89 kq_dispatch, 90 kq_dealloc, 91 1 /* need reinit */, 92 EV_FEATURE_ET|EV_FEATURE_O1|EV_FEATURE_FDS, 93 EVENT_CHANGELIST_FDINFO_SIZE 94 }; 95 96 static const struct eventop kqsigops = { 97 "kqueue_signal", 98 NULL, 99 kq_sig_add, 100 kq_sig_del, 101 NULL, 102 NULL, 103 1 /* need reinit */, 104 0, 105 0 106 }; 107 108 static void * 109 kq_init(struct event_base *base) 110 { 111 int kq = -1; 112 struct kqop *kqueueop = NULL; 113 114 if (!(kqueueop = mm_calloc(1, sizeof(struct kqop)))) 115 return (NULL); 116 117 /* Initialize the kernel queue */ 118 119 if ((kq = kqueue()) == -1) { 120 event_warn("kqueue"); 121 goto err; 122 } 123 124 kqueueop->kq = kq; 125 126 kqueueop->pid = getpid(); 127 128 /* Initialize fields */ 129 kqueueop->changes = mm_calloc(NEVENT, sizeof(struct kevent)); 130 if (kqueueop->changes == NULL) 131 goto err; 132 kqueueop->events = mm_calloc(NEVENT, sizeof(struct kevent)); 133 if (kqueueop->events == NULL) 134 goto err; 135 kqueueop->events_size = kqueueop->changes_size = NEVENT; 136 137 /* Check for Mac OS X kqueue bug. */ 138 memset(&kqueueop->changes[0], 0, sizeof kqueueop->changes[0]); 139 kqueueop->changes[0].ident = -1; 140 kqueueop->changes[0].filter = EVFILT_READ; 141 kqueueop->changes[0].flags = EV_ADD; 142 /* 143 * If kqueue works, then kevent will succeed, and it will 144 * stick an error in events[0]. If kqueue is broken, then 145 * kevent will fail. 146 */ 147 if (kevent(kq, 148 kqueueop->changes, 1, kqueueop->events, NEVENT, NULL) != 1 || 149 (int)kqueueop->events[0].ident != -1 || 150 kqueueop->events[0].flags != EV_ERROR) { 151 event_warn("%s: detected broken kqueue; not using.", __func__); 152 goto err; 153 } 154 155 base->evsigsel = &kqsigops; 156 157 return (kqueueop); 158 err: 159 if (kqueueop) 160 kqop_free(kqueueop); 161 162 return (NULL); 163 } 164 165 static void 166 kq_sighandler(int sig) 167 { 168 /* Do nothing here */ 169 } 170 171 static void 172 kq_setup_kevent(struct kevent *out, evutil_socket_t fd, int filter, short change) 173 { 174 memset(out, 0, sizeof(out)); 175 out->ident = fd; 176 out->filter = filter; 177 178 if (change & EV_CHANGE_ADD) { 179 out->flags = EV_ADD; 180 if (change & EV_ET) 181 out->flags |= EV_CLEAR; 182 #ifdef NOTE_EOF 183 /* Make it behave like select() and poll() */ 184 if (filter == EVFILT_READ) 185 out->fflags = NOTE_EOF; 186 #endif 187 } else { 188 EVUTIL_ASSERT(change & EV_CHANGE_DEL); 189 out->flags = EV_DELETE; 190 } 191 } 192 193 static int 194 kq_build_changes_list(const struct event_changelist *changelist, 195 struct kqop *kqop) 196 { 197 int i; 198 int n_changes = 0; 199 200 for (i = 0; i < changelist->n_changes; ++i) { 201 struct event_change *in_ch = &changelist->changes[i]; 202 struct kevent *out_ch; 203 if (n_changes >= kqop->changes_size - 1) { 204 int newsize = kqop->changes_size * 2; 205 struct kevent *newchanges; 206 207 newchanges = mm_realloc(kqop->changes, 208 newsize * sizeof(struct kevent)); 209 if (newchanges == NULL) { 210 event_warn("%s: realloc", __func__); 211 return (-1); 212 } 213 kqop->changes = newchanges; 214 kqop->changes_size = newsize; 215 } 216 if (in_ch->read_change) { 217 out_ch = &kqop->changes[n_changes++]; 218 kq_setup_kevent(out_ch, in_ch->fd, EVFILT_READ, 219 in_ch->read_change); 220 } 221 if (in_ch->write_change) { 222 out_ch = &kqop->changes[n_changes++]; 223 kq_setup_kevent(out_ch, in_ch->fd, EVFILT_WRITE, 224 in_ch->write_change); 225 } 226 } 227 return n_changes; 228 } 229 230 static int 231 kq_grow_events(struct kqop *kqop, size_t new_size) 232 { 233 struct kevent *newresult; 234 235 newresult = mm_realloc(kqop->events, 236 new_size * sizeof(struct kevent)); 237 238 if (newresult) { 239 kqop->events = newresult; 240 kqop->events_size = new_size; 241 return 0; 242 } else { 243 return -1; 244 } 245 } 246 247 static int 248 kq_dispatch(struct event_base *base, struct timeval *tv) 249 { 250 struct kqop *kqop = base->evbase; 251 struct kevent *events = kqop->events; 252 struct kevent *changes; 253 struct timespec ts, *ts_p = NULL; 254 int i, n_changes, res; 255 256 if (tv != NULL) { 257 TIMEVAL_TO_TIMESPEC(tv, &ts); 258 ts_p = &ts; 259 } 260 261 /* Build "changes" from "base->changes" */ 262 EVUTIL_ASSERT(kqop->changes); 263 n_changes = kq_build_changes_list(&base->changelist, kqop); 264 if (n_changes < 0) 265 return -1; 266 267 event_changelist_remove_all(&base->changelist, base); 268 269 /* steal the changes array in case some broken code tries to call 270 * dispatch twice at once. */ 271 changes = kqop->changes; 272 kqop->changes = NULL; 273 274 /* Make sure that 'events' is at least as long as the list of changes: 275 * otherwise errors in the changes can get reported as a -1 return 276 * value from kevent() rather than as EV_ERROR events in the events 277 * array. 278 * 279 * (We could instead handle -1 return values from kevent() by 280 * retrying with a smaller changes array or a larger events array, 281 * but this approach seems less risky for now.) 282 */ 283 if (kqop->events_size < n_changes) { 284 int new_size = kqop->events_size; 285 do { 286 new_size *= 2; 287 } while (new_size < n_changes); 288 289 kq_grow_events(kqop, new_size); 290 events = kqop->events; 291 } 292 293 EVBASE_RELEASE_LOCK(base, th_base_lock); 294 295 res = kevent(kqop->kq, changes, n_changes, 296 events, kqop->events_size, ts_p); 297 298 EVBASE_ACQUIRE_LOCK(base, th_base_lock); 299 300 EVUTIL_ASSERT(kqop->changes == NULL); 301 kqop->changes = changes; 302 303 if (res == -1) { 304 if (errno != EINTR) { 305 event_warn("kevent"); 306 return (-1); 307 } 308 309 return (0); 310 } 311 312 event_debug(("%s: kevent reports %d", __func__, res)); 313 314 for (i = 0; i < res; i++) { 315 int which = 0; 316 317 if (events[i].flags & EV_ERROR) { 318 /* 319 * Error messages that can happen, when a delete fails. 320 * EBADF happens when the file descriptor has been 321 * closed, 322 * ENOENT when the file descriptor was closed and 323 * then reopened. 324 * EINVAL for some reasons not understood; EINVAL 325 * should not be returned ever; but FreeBSD does :-\ 326 * An error is also indicated when a callback deletes 327 * an event we are still processing. In that case 328 * the data field is set to ENOENT. 329 */ 330 if (events[i].data == EBADF || 331 events[i].data == EINVAL || 332 events[i].data == ENOENT) 333 continue; 334 errno = events[i].data; 335 return (-1); 336 } 337 338 if (events[i].filter == EVFILT_READ) { 339 which |= EV_READ; 340 } else if (events[i].filter == EVFILT_WRITE) { 341 which |= EV_WRITE; 342 } else if (events[i].filter == EVFILT_SIGNAL) { 343 which |= EV_SIGNAL; 344 } 345 346 if (!which) 347 continue; 348 349 if (events[i].filter == EVFILT_SIGNAL) { 350 evmap_signal_active(base, events[i].ident, 1); 351 } else { 352 evmap_io_active(base, events[i].ident, which | EV_ET); 353 } 354 } 355 356 if (res == kqop->events_size) { 357 /* We used all the events space that we have. Maybe we should 358 make it bigger. */ 359 kq_grow_events(kqop, kqop->events_size * 2); 360 } 361 362 return (0); 363 } 364 365 static void 366 kqop_free(struct kqop *kqop) 367 { 368 if (kqop->changes) 369 mm_free(kqop->changes); 370 if (kqop->events) 371 mm_free(kqop->events); 372 if (kqop->kq >= 0 && kqop->pid == getpid()) 373 close(kqop->kq); 374 memset(kqop, 0, sizeof(struct kqop)); 375 mm_free(kqop); 376 } 377 378 static void 379 kq_dealloc(struct event_base *base) 380 { 381 struct kqop *kqop = base->evbase; 382 evsig_dealloc(base); 383 kqop_free(kqop); 384 } 385 386 /* signal handling */ 387 static int 388 kq_sig_add(struct event_base *base, int nsignal, short old, short events, void *p) 389 { 390 struct kqop *kqop = base->evbase; 391 struct kevent kev; 392 struct timespec timeout = { 0, 0 }; 393 (void)p; 394 395 EVUTIL_ASSERT(nsignal >= 0 && nsignal < NSIG); 396 397 memset(&kev, 0, sizeof(kev)); 398 kev.ident = nsignal; 399 kev.filter = EVFILT_SIGNAL; 400 kev.flags = EV_ADD; 401 402 /* Be ready for the signal if it is sent any 403 * time between now and the next call to 404 * kq_dispatch. */ 405 if (kevent(kqop->kq, &kev, 1, NULL, 0, &timeout) == -1) 406 return (-1); 407 408 /* XXXX The manpage suggest we could use SIG_IGN instead of a 409 * do-nothing handler */ 410 if (_evsig_set_handler(base, nsignal, kq_sighandler) == -1) 411 return (-1); 412 413 return (0); 414 } 415 416 static int 417 kq_sig_del(struct event_base *base, int nsignal, short old, short events, void *p) 418 { 419 struct kqop *kqop = base->evbase; 420 struct kevent kev; 421 422 struct timespec timeout = { 0, 0 }; 423 (void)p; 424 425 EVUTIL_ASSERT(nsignal >= 0 && nsignal < NSIG); 426 427 memset(&kev, 0, sizeof(kev)); 428 kev.ident = nsignal; 429 kev.filter = EVFILT_SIGNAL; 430 kev.flags = EV_DELETE; 431 432 /* Because we insert signal events 433 * immediately, we need to delete them 434 * immediately, too */ 435 if (kevent(kqop->kq, &kev, 1, NULL, 0, &timeout) == -1) 436 return (-1); 437 438 if (_evsig_restore_handler(base, nsignal) == -1) 439 return (-1); 440 441 return (0); 442 } 443