| 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 | #define _GNU_SOURCE 1 |
| 33 |
| 34 | #include <sys/types.h> |
| 35 | #ifdef HAVE_SYS_TIME_H |
| 36 | #include <sys/time.h> |
| 37 | #else |
| 38 | #include <sys/_libevent_time.h> |
| 39 | #endif |
| 40 | #include <sys/queue.h> |
| 41 | #include <sys/event.h> |
| 42 | #include <signal.h> |
| 43 | #include <stdio.h> |
| 44 | #include <stdlib.h> |
| 45 | #include <string.h> |
| 46 | #include <unistd.h> |
| 47 | #include <errno.h> |
| 48 | #include <assert.h> |
| 49 | #ifdef 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(HAVE_INTTYPES_H) && !defined(__OpenBSD__) && !defined(__FreeBSD__) && !defined(__darwin__) && !defined(__APPLE__) |
| 57 | #define PTR_TO_UDATA(x) ((intptr_t)(x)) |
| 58 | #else |
| 59 | #define PTR_TO_UDATA(x) (x) |
| 60 | #endif |
| 61 |
| 62 | #include "event.h" |
| 63 | #include "event-internal.h" |
| 64 | #include "log.h" |
| 65 | #include "evsignal.h" |
| 66 |
| 67 | #define EVLIST_X_KQINKERNEL 0x1000 |
| 68 |
| 69 | #define NEVENT 64 |
| 70 |
| 71 | struct kqop { |
| 72 | struct kevent *changes; |
| 73 | int nchanges; |
| 74 | struct kevent *events; |
| 75 | struct event_list evsigevents[NSIG]; |
| 76 | int nevents; |
| 77 | int kq; |
| 78 | pid_t pid; |
| 79 | }; |
| 80 |
| 81 | static void *kq_init (struct event_base *); |
| 82 | static int kq_add (void *, struct event *); |
| 83 | static int kq_del (void *, struct event *); |
| 84 | static int kq_dispatch (struct event_base *, void *, struct timeval *); |
| 85 | static int kq_insert (struct kqop *, struct kevent *); |
| 86 | static void kq_dealloc (struct event_base *, void *); |
| 87 |
| 88 | const struct eventop kqops = { |
| 89 | "kqueue", |
| 90 | kq_init, |
| 91 | kq_add, |
| 92 | kq_del, |
| 93 | kq_dispatch, |
| 94 | kq_dealloc, |
| 95 | 1 /* need reinit */ |
| 96 | }; |
| 97 |
| 98 | static void * |
| 99 | kq_init(struct event_base *base) |
| 100 | { |
| 101 | int i, kq; |
| 102 | struct kqop *kqueueop; |
| 103 |
| 104 | /* Disable kqueue when this environment variable is set */ |
| 105 | if (evutil_getenv("EVENT_NOKQUEUE")) |
| 106 | return (NULL); |
| 107 |
| 108 | if (!(kqueueop = calloc(1, sizeof(struct kqop)))) |
| 109 | return (NULL); |
| 110 |
| 111 | /* Initalize the kernel queue */ |
| 112 |
| 113 | if ((kq = kqueue()) == -1) { |
| 114 | event_warn("kqueue"); |
| 115 | free (kqueueop); |
| 116 | return (NULL); |
| 117 | } |
| 118 |
| 119 | kqueueop->kq = kq; |
| 120 |
| 121 | kqueueop->pid = getpid(); |
| 122 |
| 123 | /* Initalize fields */ |
| 124 | kqueueop->changes = malloc(NEVENT * sizeof(struct kevent)); |
| 125 | if (kqueueop->changes == NULL) { |
| 126 | free (kqueueop); |
| 127 | return (NULL); |
| 128 | } |
| 129 | kqueueop->events = malloc(NEVENT * sizeof(struct kevent)); |
| 130 | if (kqueueop->events == NULL) { |
| 131 | free (kqueueop->changes); |
| 132 | free (kqueueop); |
| 133 | return (NULL); |
| 134 | } |
| 135 | kqueueop->nevents = NEVENT; |
| 136 |
| 137 | /* we need to keep track of multiple events per signal */ |
| 138 | for (i = 0; i < NSIG; ++i) { |
| 139 | TAILQ_INIT(&kqueueop->evsigevents[i]); |
| 140 | } |
| 141 |
| 142 | return (kqueueop); |
| 143 | } |
| 144 |
| 145 | static int |
| 146 | kq_insert(struct kqop *kqop, struct kevent *kev) |
| 147 | { |
| 148 | int nevents = kqop->nevents; |
| 149 |
| 150 | if (kqop->nchanges == nevents) { |
| 151 | struct kevent *newchange; |
| 152 | struct kevent *newresult; |
| 153 |
| 154 | nevents *= 2; |
| 155 |
| 156 | newchange = realloc(kqop->changes, |
| 157 | nevents * sizeof(struct kevent)); |
| 158 | if (newchange == NULL) { |
| 159 | event_warn("%s: malloc", __func__); |
| 160 | return (-1); |
| 161 | } |
| 162 | kqop->changes = newchange; |
| 163 |
| 164 | newresult = realloc(kqop->events, |
| 165 | nevents * sizeof(struct kevent)); |
| 166 |
| 167 | /* |
| 168 | * If we fail, we don't have to worry about freeing, |
| 169 | * the next realloc will pick it up. |
| 170 | */ |
| 171 | if (newresult == NULL) { |
| 172 | event_warn("%s: malloc", __func__); |
| 173 | return (-1); |
| 174 | } |
| 175 | kqop->events = newresult; |
| 176 |
| 177 | kqop->nevents = nevents; |
| 178 | } |
| 179 |
| 180 | memcpy(&kqop->changes[kqop->nchanges++], kev, sizeof(struct kevent)); |
| 181 |
| 182 | event_debug(("%s: fd %d %s%s", |
| 183 | __func__, (int)kev->ident, |
| 184 | kev->filter == EVFILT_READ ? "EVFILT_READ" : "EVFILT_WRITE", |
| 185 | kev->flags == EV_DELETE ? " (del)" : "")); |
| 186 |
| 187 | return (0); |
| 188 | } |
| 189 |
| 190 | static void |
| 191 | kq_sighandler(int sig) |
| 192 | { |
| 193 | /* Do nothing here */ |
| 194 | } |
| 195 |
| 196 | static int |
| 197 | kq_dispatch(struct event_base *base, void *arg, struct timeval *tv) |
| 198 | { |
| 199 | struct kqop *kqop = arg; |
| 200 | struct kevent *changes = kqop->changes; |
| 201 | struct kevent *events = kqop->events; |
| 202 | struct event *ev; |
| 203 | struct timespec ts, *ts_p = NULL; |
| 204 | int i, res; |
| 205 |
| 206 | if (tv != NULL) { |
| 207 | TIMEVAL_TO_TIMESPEC(tv, &ts); |
| 208 | ts_p = &ts; |
| 209 | } |
| 210 |
| 211 | res = kevent(kqop->kq, changes, kqop->nchanges, |
| 212 | events, kqop->nevents, ts_p); |
| 213 | kqop->nchanges = 0; |
| 214 | if (res == -1) { |
| 215 | if (errno != EINTR) { |
| 216 | event_warn("kevent"); |
| 217 | return (-1); |
| 218 | } |
| 219 |
| 220 | return (0); |
| 221 | } |
| 222 |
| 223 | event_debug(("%s: kevent reports %d", __func__, res)); |
| 224 |
| 225 | for (i = 0; i < res; i++) { |
| 226 | int which = 0; |
| 227 |
| 228 | if (events[i].flags & EV_ERROR) { |
| 229 | /* |
| 230 | * Error messages that can happen, when a delete fails. |
| 231 | * EBADF happens when the file discriptor has been |
| 232 | * closed, |
| 233 | * ENOENT when the file discriptor was closed and |
| 234 | * then reopened. |
| 235 | * EINVAL for some reasons not understood; EINVAL |
| 236 | * should not be returned ever; but FreeBSD does :-\ |
| 237 | * An error is also indicated when a callback deletes |
| 238 | * an event we are still processing. In that case |
| 239 | * the data field is set to ENOENT. |
| 240 | */ |
| 241 | if (events[i].data == EBADF || |
| 242 | events[i].data == EINVAL || |
| 243 | events[i].data == ENOENT) |
| 244 | continue; |
| 245 | errno = events[i].data; |
| 246 | return (-1); |
| 247 | } |
| 248 |
| 249 | if (events[i].filter == EVFILT_READ) { |
| 250 | which |= EV_READ; |
| 251 | } else if (events[i].filter == EVFILT_WRITE) { |
| 252 | which |= EV_WRITE; |
| 253 | } else if (events[i].filter == EVFILT_SIGNAL) { |
| 254 | which |= EV_SIGNAL; |
| 255 | } |
| 256 |
| 257 | if (!which) |
| 258 | continue; |
| 259 |
| 260 | if (events[i].filter == EVFILT_SIGNAL) { |
| 261 | struct event_list *head = |
| 262 | (struct event_list *)events[i].udata; |
| 263 | TAILQ_FOREACH(ev, head, ev_signal_next) { |
| 264 | event_active(ev, which, events[i].data); |
| 265 | } |
| 266 | } else { |
| 267 | ev = (struct event *)events[i].udata; |
| 268 |
| 269 | if (!(ev->ev_events & EV_PERSIST)) |
| 270 | ev->ev_flags &= ~EVLIST_X_KQINKERNEL; |
| 271 |
| 272 | event_active(ev, which, 1); |
| 273 | } |
| 274 | } |
| 275 |
| 276 | return (0); |
| 277 | } |
| 278 |
| 279 |
| 280 | static int |
| 281 | kq_add(void *arg, struct event *ev) |
| 282 | { |
| 283 | struct kqop *kqop = arg; |
| 284 | struct kevent kev; |
| 285 |
| 286 | if (ev->ev_events & EV_SIGNAL) { |
| 287 | int nsignal = EVENT_SIGNAL(ev); |
| 288 |
| 289 | assert(nsignal >= 0 && nsignal < NSIG); |
| 290 | if (TAILQ_EMPTY(&kqop->evsigevents[nsignal])) { |
| 291 | struct timespec timeout = { 0, 0 }; |
| 292 |
| 293 | memset(&kev, 0, sizeof(kev)); |
| 294 | kev.ident = nsignal; |
| 295 | kev.filter = EVFILT_SIGNAL; |
| 296 | kev.flags = EV_ADD; |
| 297 | kev.udata = PTR_TO_UDATA(&kqop->evsigevents[nsignal]); |
| 298 |
| 299 | /* Be ready for the signal if it is sent any |
| 300 | * time between now and the next call to |
| 301 | * kq_dispatch. */ |
| 302 | if (kevent(kqop->kq, &kev, 1, NULL, 0, &timeout) == -1) |
| 303 | return (-1); |
| 304 |
| 305 | if (_evsignal_set_handler(ev->ev_base, nsignal, |
| 306 | kq_sighandler) == -1) |
| 307 | return (-1); |
| 308 | } |
| 309 |
| 310 | TAILQ_INSERT_TAIL(&kqop->evsigevents[nsignal], ev, |
| 311 | ev_signal_next); |
| 312 | ev->ev_flags |= EVLIST_X_KQINKERNEL; |
| 313 | return (0); |
| 314 | } |
| 315 |
| 316 | if (ev->ev_events & EV_READ) { |
| 317 | memset(&kev, 0, sizeof(kev)); |
| 318 | kev.ident = ev->ev_fd; |
| 319 | kev.filter = EVFILT_READ; |
| 320 | #ifdef NOTE_EOF |
| 321 | /* Make it behave like select() and poll() */ |
| 322 | kev.fflags = NOTE_EOF; |
| 323 | #endif |
| 324 | kev.flags = EV_ADD; |
| 325 | if (!(ev->ev_events & EV_PERSIST)) |
| 326 | kev.flags |= EV_ONESHOT; |
| 327 | kev.udata = PTR_TO_UDATA(ev); |
| 328 |
| 329 | if (kq_insert(kqop, &kev) == -1) |
| 330 | return (-1); |
| 331 |
| 332 | ev->ev_flags |= EVLIST_X_KQINKERNEL; |
| 333 | } |
| 334 |
| 335 | if (ev->ev_events & EV_WRITE) { |
| 336 | memset(&kev, 0, sizeof(kev)); |
| 337 | kev.ident = ev->ev_fd; |
| 338 | kev.filter = EVFILT_WRITE; |
| 339 | kev.flags = EV_ADD; |
| 340 | if (!(ev->ev_events & EV_PERSIST)) |
| 341 | kev.flags |= EV_ONESHOT; |
| 342 | kev.udata = PTR_TO_UDATA(ev); |
| 343 |
| 344 | if (kq_insert(kqop, &kev) == -1) |
| 345 | return (-1); |
| 346 |
| 347 | ev->ev_flags |= EVLIST_X_KQINKERNEL; |
| 348 | } |
| 349 |
| 350 | return (0); |
| 351 | } |
| 352 |
| 353 | static int |
| 354 | kq_del(void *arg, struct event *ev) |
| 355 | { |
| 356 | struct kqop *kqop = arg; |
| 357 | struct kevent kev; |
| 358 |
| 359 | if (!(ev->ev_flags & EVLIST_X_KQINKERNEL)) |
| 360 | return (0); |
| 361 |
| 362 | if (ev->ev_events & EV_SIGNAL) { |
| 363 | int nsignal = EVENT_SIGNAL(ev); |
| 364 | struct timespec timeout = { 0, 0 }; |
| 365 |
| 366 | assert(nsignal >= 0 && nsignal < NSIG); |
| 367 | TAILQ_REMOVE(&kqop->evsigevents[nsignal], ev, ev_signal_next); |
| 368 | if (TAILQ_EMPTY(&kqop->evsigevents[nsignal])) { |
| 369 | memset(&kev, 0, sizeof(kev)); |
| 370 | kev.ident = nsignal; |
| 371 | kev.filter = EVFILT_SIGNAL; |
| 372 | kev.flags = EV_DELETE; |
| 373 |
| 374 | /* Because we insert signal events |
| 375 | * immediately, we need to delete them |
| 376 | * immediately, too */ |
| 377 | if (kevent(kqop->kq, &kev, 1, NULL, 0, &timeout) == -1) |
| 378 | return (-1); |
| 379 |
| 380 | if (_evsignal_restore_handler(ev->ev_base, |
| 381 | nsignal) == -1) |
| 382 | return (-1); |
| 383 | } |
| 384 |
| 385 | ev->ev_flags &= ~EVLIST_X_KQINKERNEL; |
| 386 | return (0); |
| 387 | } |
| 388 |
| 389 | if (ev->ev_events & EV_READ) { |
| 390 | memset(&kev, 0, sizeof(kev)); |
| 391 | kev.ident = ev->ev_fd; |
| 392 | kev.filter = EVFILT_READ; |
| 393 | kev.flags = EV_DELETE; |
| 394 |
| 395 | if (kq_insert(kqop, &kev) == -1) |
| 396 | return (-1); |
| 397 |
| 398 | ev->ev_flags &= ~EVLIST_X_KQINKERNEL; |
| 399 | } |
| 400 |
| 401 | if (ev->ev_events & EV_WRITE) { |
| 402 | memset(&kev, 0, sizeof(kev)); |
| 403 | kev.ident = ev->ev_fd; |
| 404 | kev.filter = EVFILT_WRITE; |
| 405 | kev.flags = EV_DELETE; |
| 406 |
| 407 | if (kq_insert(kqop, &kev) == -1) |
| 408 | return (-1); |
| 409 |
| 410 | ev->ev_flags &= ~EVLIST_X_KQINKERNEL; |
| 411 | } |
| 412 |
| 413 | return (0); |
| 414 | } |
| 415 |
| 416 | static void |
| 417 | kq_dealloc(struct event_base *base, void *arg) |
| 418 | { |
| 419 | struct kqop *kqop = arg; |
| 420 |
| 421 | evsignal_dealloc(base); |
| 422 |
| 423 | if (kqop->changes) |
| 424 | free(kqop->changes); |
| 425 | if (kqop->events) |
| 426 | free(kqop->events); |
| 427 | if (kqop->kq >= 0 && kqop->pid == getpid()) |
| 428 | close(kqop->kq); |
| 429 |
| 430 | memset(kqop, 0, sizeof(struct kqop)); |
| 431 | free(kqop); |
| 432 | } |
| 433 |