| 1 |
/* |
| 2 |
* libev kqueue backend |
| 3 |
* |
| 4 |
* Copyright (c) 2007 Marc Alexander Lehmann <libev@schmorp.de> |
| 5 |
* All rights reserved. |
| 6 |
* |
| 7 |
* Redistribution and use in source and binary forms, with or without |
| 8 |
* modification, are permitted provided that the following conditions are |
| 9 |
* met: |
| 10 |
* |
| 11 |
* * Redistributions of source code must retain the above copyright |
| 12 |
* notice, this list of conditions and the following disclaimer. |
| 13 |
* |
| 14 |
* * Redistributions in binary form must reproduce the above |
| 15 |
* copyright notice, this list of conditions and the following |
| 16 |
* disclaimer in the documentation and/or other materials provided |
| 17 |
* with the distribution. |
| 18 |
* |
| 19 |
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 20 |
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 21 |
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 22 |
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 23 |
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 24 |
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 25 |
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 26 |
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 27 |
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 28 |
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 29 |
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 30 |
*/ |
| 31 |
|
| 32 |
#include <sys/types.h> |
| 33 |
#include <sys/time.h> |
| 34 |
#include <sys/queue.h> |
| 35 |
#include <sys/event.h> |
| 36 |
#include <string.h> |
| 37 |
#include <errno.h> |
| 38 |
|
| 39 |
void inline_speed |
| 40 |
kqueue_change (EV_P_ int fd, int filter, int flags, int fflags) |
| 41 |
{ |
| 42 |
++kqueue_changecnt; |
| 43 |
array_needsize (struct kevent, kqueue_changes, kqueue_changemax, kqueue_changecnt, EMPTY2); |
| 44 |
|
| 45 |
EV_SET (&kqueue_changes [kqueue_changecnt - 1], fd, filter, flags, fflags, 0, 0); |
| 46 |
} |
| 47 |
|
| 48 |
#ifndef NOTE_EOF |
| 49 |
# define NOTE_EOF 0 |
| 50 |
#endif |
| 51 |
|
| 52 |
static void |
| 53 |
kqueue_modify (EV_P_ int fd, int oev, int nev) |
| 54 |
{ |
| 55 |
if (oev != nev) |
| 56 |
{ |
| 57 |
if (oev & EV_READ) |
| 58 |
kqueue_change (EV_A_ fd, EVFILT_READ , EV_DELETE, 0); |
| 59 |
|
| 60 |
if (oev & EV_WRITE) |
| 61 |
kqueue_change (EV_A_ fd, EVFILT_WRITE, EV_DELETE, 0); |
| 62 |
} |
| 63 |
|
| 64 |
/* to detect close/reopen reliably, we have to re-add */ |
| 65 |
/* event requests even when oev == nev */ |
| 66 |
|
| 67 |
if (nev & EV_READ) |
| 68 |
kqueue_change (EV_A_ fd, EVFILT_READ , EV_ADD, NOTE_EOF); |
| 69 |
|
| 70 |
if (nev & EV_WRITE) |
| 71 |
kqueue_change (EV_A_ fd, EVFILT_WRITE, EV_ADD, NOTE_EOF); |
| 72 |
} |
| 73 |
|
| 74 |
static void |
| 75 |
kqueue_poll (EV_P_ ev_tstamp timeout) |
| 76 |
{ |
| 77 |
int res, i; |
| 78 |
struct timespec ts; |
| 79 |
|
| 80 |
/* need to resize so there is enough space for errors */ |
| 81 |
if (kqueue_changecnt > kqueue_eventmax) |
| 82 |
{ |
| 83 |
ev_free (kqueue_events); |
| 84 |
kqueue_eventmax = array_nextsize (sizeof (struct kevent), kqueue_eventmax, kqueue_changecnt); |
| 85 |
kqueue_events = (struct kevent *)ev_malloc (sizeof (struct kevent) * kqueue_eventmax); |
| 86 |
} |
| 87 |
|
| 88 |
ts.tv_sec = (time_t)timeout; |
| 89 |
ts.tv_nsec = (long)((timeout - (ev_tstamp)ts.tv_sec) * 1e9); |
| 90 |
res = kevent (backend_fd, kqueue_changes, kqueue_changecnt, kqueue_events, kqueue_eventmax, &ts); |
| 91 |
kqueue_changecnt = 0; |
| 92 |
|
| 93 |
if (expect_false (res < 0)) |
| 94 |
{ |
| 95 |
if (errno != EINTR) |
| 96 |
syserr ("(libev) kevent"); |
| 97 |
|
| 98 |
return; |
| 99 |
} |
| 100 |
|
| 101 |
for (i = 0; i < res; ++i) |
| 102 |
{ |
| 103 |
int fd = kqueue_events [i].ident; |
| 104 |
|
| 105 |
if (expect_false (kqueue_events [i].flags & EV_ERROR)) |
| 106 |
{ |
| 107 |
int err = kqueue_events [i].data; |
| 108 |
|
| 109 |
/* we are only interested in errors for fds that we are interested in :) */ |
| 110 |
if (anfds [fd].events) |
| 111 |
{ |
| 112 |
if (err == ENOENT) /* resubmit changes on ENOENT */ |
| 113 |
kqueue_modify (EV_A_ fd, 0, anfds [fd].events); |
| 114 |
else if (err == EBADF) /* on EBADF, we re-check the fd */ |
| 115 |
{ |
| 116 |
if (fd_valid (fd)) |
| 117 |
kqueue_modify (EV_A_ fd, 0, anfds [fd].events); |
| 118 |
else |
| 119 |
fd_kill (EV_A_ fd); |
| 120 |
} |
| 121 |
else /* on all other errors, we error out on the fd */ |
| 122 |
fd_kill (EV_A_ fd); |
| 123 |
} |
| 124 |
} |
| 125 |
else |
| 126 |
fd_event ( |
| 127 |
EV_A_ |
| 128 |
fd, |
| 129 |
kqueue_events [i].filter == EVFILT_READ ? EV_READ |
| 130 |
: kqueue_events [i].filter == EVFILT_WRITE ? EV_WRITE |
| 131 |
: 0 |
| 132 |
); |
| 133 |
} |
| 134 |
|
| 135 |
if (expect_false (res == kqueue_eventmax)) |
| 136 |
{ |
| 137 |
ev_free (kqueue_events); |
| 138 |
kqueue_eventmax = array_nextsize (sizeof (struct kevent), kqueue_eventmax, kqueue_eventmax + 1); |
| 139 |
kqueue_events = (struct kevent *)ev_malloc (sizeof (struct kevent) * kqueue_eventmax); |
| 140 |
} |
| 141 |
} |
| 142 |
|
| 143 |
int inline_size |
| 144 |
kqueue_init (EV_P_ int flags) |
| 145 |
{ |
| 146 |
/* Initalize the kernel queue */ |
| 147 |
if ((backend_fd = kqueue ()) < 0) |
| 148 |
return 0; |
| 149 |
|
| 150 |
fcntl (backend_fd, F_SETFD, FD_CLOEXEC); /* not sure if necessary, hopefully doesn't hurt */ |
| 151 |
|
| 152 |
backend_fudge = 0.; |
| 153 |
backend_modify = kqueue_modify; |
| 154 |
backend_poll = kqueue_poll; |
| 155 |
|
| 156 |
kqueue_eventmax = 64; /* initial number of events receivable per poll */ |
| 157 |
kqueue_events = (struct kevent *)ev_malloc (sizeof (struct kevent) * kqueue_eventmax); |
| 158 |
|
| 159 |
kqueue_changes = 0; |
| 160 |
kqueue_changemax = 0; |
| 161 |
kqueue_changecnt = 0; |
| 162 |
|
| 163 |
return EVBACKEND_KQUEUE; |
| 164 |
} |
| 165 |
|
| 166 |
void inline_size |
| 167 |
kqueue_destroy (EV_P) |
| 168 |
{ |
| 169 |
ev_free (kqueue_events); |
| 170 |
ev_free (kqueue_changes); |
| 171 |
} |
| 172 |
|
| 173 |
void inline_size |
| 174 |
kqueue_fork (EV_P) |
| 175 |
{ |
| 176 |
close (backend_fd); |
| 177 |
|
| 178 |
while ((backend_fd = kqueue ()) < 0) |
| 179 |
syserr ("(libev) kqueue"); |
| 180 |
|
| 181 |
fcntl (backend_fd, F_SETFD, FD_CLOEXEC); |
| 182 |
|
| 183 |
/* re-register interest in fds */ |
| 184 |
fd_rearm_all (EV_A); |
| 185 |
} |
| 186 |
|