… | |
… | |
32 | #include <sys/queue.h> |
32 | #include <sys/queue.h> |
33 | #include <sys/event.h> |
33 | #include <sys/event.h> |
34 | #include <string.h> |
34 | #include <string.h> |
35 | #include <errno.h> |
35 | #include <errno.h> |
36 | |
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 |
37 | static void |
44 | kqueue_change (int fd, int filter, int flags, int fflags) |
38 | kqueue_change (EV_P_ int fd, int filter, int flags, int fflags) |
45 | { |
39 | { |
46 | struct kevent *ke; |
40 | struct kevent *ke; |
47 | |
41 | |
|
|
42 | ++kqueue_changecnt; |
48 | array_needsize (kq_changes, kq_changemax, ++kq_changecnt, ); |
43 | array_needsize (struct kevent, kqueue_changes, kqueue_changemax, kqueue_changecnt, EMPTY2); |
49 | |
44 | |
50 | ke = &kq_changes [kq_changecnt - 1]; |
45 | EV_SET (&kqueue_changes [kqueue_changecnt - 1], fd, filter, flags, fflags, 0, 0); |
51 | memset (ke, 0, sizeof (struct kevent)); |
|
|
52 | ke->ident = fd; |
|
|
53 | ke->filter = filter; |
|
|
54 | ke->flags = flags; |
|
|
55 | ke->fflags = fflags; |
|
|
56 | } |
46 | } |
57 | |
47 | |
58 | #ifndef NOTE_EOF |
48 | #ifndef NOTE_EOF |
59 | # define NOTE_EOF 0 |
49 | # define NOTE_EOF 0 |
60 | #endif |
50 | #endif |
61 | |
51 | |
62 | static void |
52 | static void |
63 | kqueue_modify (int fd, int oev, int nev) |
53 | kqueue_modify (EV_P_ int fd, int oev, int nev) |
64 | { |
54 | { |
65 | if ((oev ^ nev) & EV_READ) |
55 | if (oev != nev) |
66 | { |
56 | { |
67 | if (nev & EV_READ) |
57 | if (oev & EV_READ) |
68 | kqueue_change (fd, EVFILT_READ, EV_ADD, NOTE_EOF); |
|
|
69 | else |
|
|
70 | kqueue_change (fd, EVFILT_READ, EV_DELETE, 0); |
58 | kqueue_change (EV_A_ fd, EVFILT_READ , EV_DELETE, 0); |
71 | } |
|
|
72 | |
59 | |
73 | if ((oev ^ nev) & EV_WRITE) |
|
|
74 | { |
|
|
75 | if (nev & EV_WRITE) |
60 | if (oev & EV_WRITE) |
76 | kqueue_change (fd, EVFILT_WRITE, EV_ADD, NOTE_EOF); |
|
|
77 | else |
|
|
78 | kqueue_change (fd, EVFILT_WRITE, EV_DELETE, 0); |
61 | kqueue_change (EV_A_ fd, EVFILT_WRITE, EV_DELETE, 0); |
79 | } |
62 | } |
80 | } |
|
|
81 | |
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 | |
82 | static void |
74 | static void |
83 | kqueue_poll (ev_tstamp timeout) |
75 | kqueue_poll (EV_P_ ev_tstamp timeout) |
84 | { |
76 | { |
85 | int res, i; |
77 | int res, i; |
86 | struct timespec ts; |
78 | struct timespec ts; |
87 | |
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_roundsize (struct kevent, kqueue_changecnt); |
|
|
85 | kqueue_events = (struct kevent *)ev_malloc (sizeof (struct kevent) * kqueue_eventmax); |
|
|
86 | } |
|
|
87 | |
88 | ts.tv_sec = (time_t)timeout; |
88 | ts.tv_sec = (time_t)timeout; |
89 | ts.tv_nsec = (long)(timeout - (ev_tstamp)ts.tv_sec) * 1e9; |
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); |
90 | res = kevent (kqueue_fd, kqueue_changes, kqueue_changecnt, kqueue_events, kqueue_eventmax, &ts); |
91 | kq_changecnt = 0; |
91 | kqueue_changecnt = 0; |
92 | |
92 | |
93 | if (res < 0) |
93 | if (res < 0) |
|
|
94 | { |
|
|
95 | if (errno != EINTR) |
|
|
96 | syserr ("(libev) kevent"); |
|
|
97 | |
94 | return; |
98 | return; |
|
|
99 | } |
95 | |
100 | |
96 | for (i = 0; i < res; ++i) |
101 | for (i = 0; i < res; ++i) |
97 | { |
102 | { |
|
|
103 | int fd = kqueue_events [i].ident; |
|
|
104 | |
98 | if (kq_events [i].flags & EV_ERROR) |
105 | if (kqueue_events [i].flags & EV_ERROR) |
99 | { |
106 | { |
|
|
107 | int err = kqueue_events [i].data; |
|
|
108 | |
100 | /* |
109 | /* |
101 | * Error messages that can happen, when a delete fails. |
110 | * errors that may happen |
102 | * EBADF happens when the file discriptor has been |
111 | * EBADF happens when the file discriptor has been |
103 | * closed, |
112 | * closed, |
104 | * ENOENT when the file discriptor was closed and |
113 | * ENOENT when the file descriptor was closed and |
105 | * then reopened. |
114 | * then reopened. |
106 | * EINVAL for some reasons not understood; EINVAL |
115 | * EINVAL for some reasons not understood; EINVAL |
107 | * should not be returned ever; but FreeBSD does :-\ |
116 | * 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 | */ |
117 | */ |
112 | if (kq_events [i].data == EBADF) |
118 | |
113 | fd_kill (kq_events [i].ident); |
119 | /* we are only interested in errors for fds that we are interested in :) */ |
|
|
120 | if (anfds [fd].events) |
|
|
121 | { |
|
|
122 | if (err == ENOENT) /* resubmit changes on ENOENT */ |
|
|
123 | kqueue_modify (EV_A_ fd, 0, anfds [fd].events); |
|
|
124 | else if (err == EBADF) /* on EBADF, we re-check the fd */ |
|
|
125 | { |
|
|
126 | if (fd_valid (fd)) |
|
|
127 | kqueue_modify (EV_A_ fd, 0, anfds [fd].events); |
|
|
128 | else |
|
|
129 | fd_kill (EV_A_ fd); |
|
|
130 | } |
|
|
131 | else /* on all other errors, we error out on the fd */ |
|
|
132 | fd_kill (EV_A_ fd); |
|
|
133 | } |
114 | } |
134 | } |
115 | else |
135 | else |
116 | fd_event ( |
136 | fd_event ( |
117 | kq_events [i].ident, |
137 | EV_A_ |
|
|
138 | fd, |
118 | kq_events [i].filter == EVFILT_READ ? EV_READ |
139 | kqueue_events [i].filter == EVFILT_READ ? EV_READ |
119 | : kq_events [i].filter == EVFILT_WRITE ? EV_WRITE |
140 | : kqueue_events [i].filter == EVFILT_WRITE ? EV_WRITE |
120 | : 0 |
141 | : 0 |
121 | ); |
142 | ); |
122 | } |
143 | } |
123 | |
144 | |
124 | if (expect_false (res == kq_eventmax)) |
145 | if (expect_false (res == kqueue_eventmax)) |
125 | { |
146 | { |
126 | free (kq_events); |
147 | ev_free (kqueue_events); |
127 | kq_eventmax = array_roundsize (kq_events, kq_eventmax << 1); |
148 | kqueue_eventmax = array_roundsize (struct kevent, kqueue_eventmax << 1); |
128 | kq_events = malloc (sizeof (struct kevent) * kq_eventmax); |
149 | kqueue_events = (struct kevent *)ev_malloc (sizeof (struct kevent) * kqueue_eventmax); |
129 | } |
150 | } |
130 | } |
151 | } |
131 | |
152 | |
132 | static void |
153 | static int |
133 | kqueue_init (int flags) |
154 | kqueue_init (EV_P_ int flags) |
134 | { |
155 | { |
135 | struct kevent ch, ev; |
156 | struct kevent ch, ev; |
136 | |
157 | |
137 | /* Initalize the kernel queue */ |
158 | /* Initalize the kernel queue */ |
138 | if ((kq_fd = kqueue ()) < 0) |
159 | if ((kqueue_fd = kqueue ()) < 0) |
139 | return; |
160 | return 0; |
|
|
161 | |
|
|
162 | fcntl (kqueue_fd, F_SETFD, FD_CLOEXEC); /* not sure if necessary, hopefully doesn't hurt */ |
140 | |
163 | |
141 | /* Check for Mac OS X kqueue bug. */ |
164 | /* Check for Mac OS X kqueue bug. */ |
142 | ch.ident = -1; |
165 | ch.ident = -1; |
143 | ch.filter = EVFILT_READ; |
166 | ch.filter = EVFILT_READ; |
144 | ch.flags = EV_ADD; |
167 | ch.flags = EV_ADD; |
… | |
… | |
146 | /* |
169 | /* |
147 | * If kqueue works, then kevent will succeed, and it will |
170 | * If kqueue works, then kevent will succeed, and it will |
148 | * stick an error in ev. If kqueue is broken, then |
171 | * stick an error in ev. If kqueue is broken, then |
149 | * kevent will fail. |
172 | * kevent will fail. |
150 | */ |
173 | */ |
151 | if (kevent (kq_fd, &ch, 1, &ev, 1, 0) != 1 |
174 | if (kevent (kqueue_fd, &ch, 1, &ev, 1, 0) != 1 |
152 | || ev.ident != -1 |
175 | || ev.ident != -1 |
153 | || ev.flags != EV_ERROR) |
176 | || ev.flags != EV_ERROR) |
154 | { |
177 | { |
155 | /* detected broken kqueue */ |
178 | /* detected broken kqueue */ |
156 | close (kq_fd); |
179 | close (kqueue_fd); |
157 | return; |
180 | return 0; |
158 | } |
181 | } |
159 | |
182 | |
160 | ev_method = EVMETHOD_KQUEUE; |
|
|
161 | method_fudge = 1e-3; /* needed to compensate for kevent returning early */ |
183 | backend_fudge = 1e-3; /* needed to compensate for kevent returning early */ |
162 | method_modify = kqueue_modify; |
184 | backend_modify = kqueue_modify; |
163 | method_poll = kqueue_poll; |
185 | backend_poll = kqueue_poll; |
164 | |
186 | |
165 | kq_eventmax = 64; /* intiial number of events receivable per poll */ |
187 | kqueue_eventmax = 64; /* intiial number of events receivable per poll */ |
166 | kq_events = malloc (sizeof (struct kevent) * kq_eventmax); |
188 | kqueue_events = (struct kevent *)ev_malloc (sizeof (struct kevent) * kqueue_eventmax); |
167 | } |
|
|
168 | |
189 | |
|
|
190 | kqueue_changes = 0; |
|
|
191 | kqueue_changemax = 0; |
|
|
192 | kqueue_changecnt = 0; |
|
|
193 | |
|
|
194 | return EVBACKEND_KQUEUE; |
|
|
195 | } |
|
|
196 | |
|
|
197 | static void |
|
|
198 | kqueue_destroy (EV_P) |
|
|
199 | { |
|
|
200 | close (kqueue_fd); |
|
|
201 | |
|
|
202 | ev_free (kqueue_events); |
|
|
203 | ev_free (kqueue_changes); |
|
|
204 | } |
|
|
205 | |
|
|
206 | static void |
|
|
207 | kqueue_fork (EV_P) |
|
|
208 | { |
|
|
209 | close (kqueue_fd); |
|
|
210 | |
|
|
211 | while ((kqueue_fd = kqueue ()) < 0) |
|
|
212 | syserr ("(libev) kqueue"); |
|
|
213 | |
|
|
214 | fcntl (kqueue_fd, F_SETFD, FD_CLOEXEC); |
|
|
215 | |
|
|
216 | /* re-register interest in fds */ |
|
|
217 | fd_rearm_all (EV_A); |
|
|
218 | } |
|
|
219 | |