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