| 1 |
root |
1.1 |
/* |
| 2 |
|
|
* Copyright 2007 Marc Alexander Lehmann <libev@schmorp.de> |
| 3 |
|
|
* Copyright 2000-2002 Niels Provos <provos@citi.umich.edu> |
| 4 |
|
|
* All rights reserved. |
| 5 |
|
|
* |
| 6 |
|
|
* Redistribution and use in source and binary forms, with or without |
| 7 |
|
|
* modification, are permitted provided that the following conditions |
| 8 |
|
|
* are met: |
| 9 |
|
|
* |
| 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 <sys/types.h> |
| 31 |
|
|
#include <sys/time.h> |
| 32 |
|
|
#include <sys/queue.h> |
| 33 |
|
|
#include <sys/event.h> |
| 34 |
|
|
#include <string.h> |
| 35 |
|
|
#include <errno.h> |
| 36 |
|
|
|
| 37 |
|
|
static int kq_fd; |
| 38 |
|
|
static struct kevent *kq_changes; |
| 39 |
|
|
static int kq_changemax, kq_changecnt; |
| 40 |
|
|
static struct kevent *kq_events; |
| 41 |
|
|
static int kq_eventmax; |
| 42 |
|
|
|
| 43 |
|
|
static void |
| 44 |
|
|
kqueue_change (int fd, int filter, int flags, int fflags) |
| 45 |
|
|
{ |
| 46 |
|
|
struct kevent *ke; |
| 47 |
|
|
|
| 48 |
|
|
array_needsize (kq_changes, kq_changemax, ++kq_changecnt, ); |
| 49 |
|
|
|
| 50 |
|
|
ke = &kq_changes [kq_changecnt - 1]; |
| 51 |
|
|
memset (ke, 0, sizeof (struct kevent)); |
| 52 |
|
|
ke->ident = fd; |
| 53 |
|
|
ke->filter = filter; |
| 54 |
|
|
ke->flags = flags; |
| 55 |
|
|
ke->fflags = fflags; |
| 56 |
|
|
} |
| 57 |
|
|
|
| 58 |
root |
1.4 |
#ifndef NOTE_EOF |
| 59 |
|
|
# define NOTE_EOF 0 |
| 60 |
|
|
#endif |
| 61 |
|
|
|
| 62 |
root |
1.1 |
static void |
| 63 |
|
|
kqueue_modify (int fd, int oev, int nev) |
| 64 |
|
|
{ |
| 65 |
root |
1.4 |
if ((oev ^ nev) & EV_READ) |
| 66 |
root |
1.1 |
{ |
| 67 |
|
|
if (nev & EV_READ) |
| 68 |
|
|
kqueue_change (fd, EVFILT_READ, EV_ADD, NOTE_EOF); |
| 69 |
|
|
else |
| 70 |
|
|
kqueue_change (fd, EVFILT_READ, EV_DELETE, 0); |
| 71 |
|
|
} |
| 72 |
|
|
|
| 73 |
root |
1.4 |
if ((oev ^ nev) & EV_WRITE) |
| 74 |
root |
1.1 |
{ |
| 75 |
|
|
if (nev & EV_WRITE) |
| 76 |
|
|
kqueue_change (fd, EVFILT_WRITE, EV_ADD, NOTE_EOF); |
| 77 |
|
|
else |
| 78 |
|
|
kqueue_change (fd, EVFILT_WRITE, EV_DELETE, 0); |
| 79 |
|
|
} |
| 80 |
|
|
} |
| 81 |
|
|
|
| 82 |
|
|
static void |
| 83 |
|
|
kqueue_poll (ev_tstamp timeout) |
| 84 |
|
|
{ |
| 85 |
|
|
int res, i; |
| 86 |
|
|
struct timespec ts; |
| 87 |
|
|
|
| 88 |
|
|
ts.tv_sec = (time_t)timeout; |
| 89 |
|
|
ts.tv_nsec = (long)(timeout - (ev_tstamp)ts.tv_sec) * 1e9; |
| 90 |
|
|
res = kevent (kq_fd, kq_changes, kq_changecnt, kq_events, kq_eventmax, &ts); |
| 91 |
|
|
kq_changecnt = 0; |
| 92 |
|
|
|
| 93 |
|
|
if (res < 0) |
| 94 |
|
|
return; |
| 95 |
|
|
|
| 96 |
|
|
for (i = 0; i < res; ++i) |
| 97 |
|
|
{ |
| 98 |
|
|
if (kq_events [i].flags & EV_ERROR) |
| 99 |
|
|
{ |
| 100 |
|
|
/* |
| 101 |
|
|
* Error messages that can happen, when a delete fails. |
| 102 |
|
|
* EBADF happens when the file discriptor has been |
| 103 |
|
|
* closed, |
| 104 |
|
|
* ENOENT when the file discriptor was closed and |
| 105 |
|
|
* then reopened. |
| 106 |
|
|
* EINVAL for some reasons not understood; EINVAL |
| 107 |
|
|
* should not be returned ever; but FreeBSD does :-\ |
| 108 |
|
|
* An error is also indicated when a callback deletes |
| 109 |
|
|
* an event we are still processing. In that case |
| 110 |
|
|
* the data field is set to ENOENT. |
| 111 |
|
|
*/ |
| 112 |
root |
1.4 |
if (kq_events [i].data == EBADF) |
| 113 |
|
|
fd_kill (kq_events [i].ident); |
| 114 |
root |
1.1 |
} |
| 115 |
|
|
else |
| 116 |
root |
1.3 |
fd_event ( |
| 117 |
root |
1.4 |
kq_events [i].ident, |
| 118 |
|
|
kq_events [i].filter == EVFILT_READ ? EV_READ |
| 119 |
|
|
: kq_events [i].filter == EVFILT_WRITE ? EV_WRITE |
| 120 |
root |
1.1 |
: 0 |
| 121 |
|
|
); |
| 122 |
|
|
} |
| 123 |
|
|
|
| 124 |
|
|
if (expect_false (res == kq_eventmax)) |
| 125 |
|
|
{ |
| 126 |
|
|
free (kq_events); |
| 127 |
|
|
kq_eventmax = array_roundsize (kq_events, kq_eventmax << 1); |
| 128 |
|
|
kq_events = malloc (sizeof (struct kevent) * kq_eventmax); |
| 129 |
|
|
} |
| 130 |
|
|
} |
| 131 |
|
|
|
| 132 |
|
|
static void |
| 133 |
root |
1.4 |
kqueue_init (int flags) |
| 134 |
root |
1.1 |
{ |
| 135 |
root |
1.2 |
struct kevent ch, ev; |
| 136 |
|
|
|
| 137 |
root |
1.1 |
/* Initalize the kernel queue */ |
| 138 |
root |
1.2 |
if ((kq_fd = kqueue ()) < 0) |
| 139 |
root |
1.1 |
return; |
| 140 |
|
|
|
| 141 |
root |
1.2 |
/* Check for Mac OS X kqueue bug. */ |
| 142 |
|
|
ch.ident = -1; |
| 143 |
|
|
ch.filter = EVFILT_READ; |
| 144 |
|
|
ch.flags = EV_ADD; |
| 145 |
root |
1.1 |
|
| 146 |
|
|
/* |
| 147 |
|
|
* If kqueue works, then kevent will succeed, and it will |
| 148 |
root |
1.4 |
* stick an error in ev. If kqueue is broken, then |
| 149 |
root |
1.1 |
* kevent will fail. |
| 150 |
|
|
*/ |
| 151 |
root |
1.2 |
if (kevent (kq_fd, &ch, 1, &ev, 1, 0) != 1 |
| 152 |
|
|
|| ev.ident != -1 |
| 153 |
|
|
|| ev.flags != EV_ERROR) |
| 154 |
root |
1.1 |
{ |
| 155 |
|
|
/* detected broken kqueue */ |
| 156 |
|
|
close (kq_fd); |
| 157 |
|
|
return; |
| 158 |
|
|
} |
| 159 |
|
|
|
| 160 |
|
|
ev_method = EVMETHOD_KQUEUE; |
| 161 |
|
|
method_fudge = 1e-3; /* needed to compensate for kevent returning early */ |
| 162 |
root |
1.5 |
method_modify = kqueue_modify; |
| 163 |
|
|
method_poll = kqueue_poll; |
| 164 |
root |
1.1 |
|
| 165 |
|
|
kq_eventmax = 64; /* intiial number of events receivable per poll */ |
| 166 |
|
|
kq_events = malloc (sizeof (struct kevent) * kq_eventmax); |
| 167 |
|
|
} |
| 168 |
|
|
|