/src/httpd/srclib/apr/poll/unix/epoll.c
Line | Count | Source |
1 | | /* Licensed to the Apache Software Foundation (ASF) under one or more |
2 | | * contributor license agreements. See the NOTICE file distributed with |
3 | | * this work for additional information regarding copyright ownership. |
4 | | * The ASF licenses this file to You under the Apache License, Version 2.0 |
5 | | * (the "License"); you may not use this file except in compliance with |
6 | | * the License. You may obtain a copy of the License at |
7 | | * |
8 | | * http://www.apache.org/licenses/LICENSE-2.0 |
9 | | * |
10 | | * Unless required by applicable law or agreed to in writing, software |
11 | | * distributed under the License is distributed on an "AS IS" BASIS, |
12 | | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
13 | | * See the License for the specific language governing permissions and |
14 | | * limitations under the License. |
15 | | */ |
16 | | |
17 | | #include "apr.h" |
18 | | #include "apr_poll.h" |
19 | | #include "apr_time.h" |
20 | | #include "apr_portable.h" |
21 | | #include "apr_arch_file_io.h" |
22 | | #include "apr_arch_networkio.h" |
23 | | #include "apr_arch_poll_private.h" |
24 | | #include "apr_arch_inherit.h" |
25 | | |
26 | | #if defined(HAVE_EPOLL) |
27 | | |
28 | | #if defined(HAVE_EPOLL_PWAIT2) |
29 | | #include <time.h> |
30 | | #endif |
31 | | |
32 | | /* epoll_wait() with a timeout in microseconds (negative for no timeout): |
33 | | * epoll_pwait2() is precise to the microsecond where available (configure), |
34 | | * epoll_wait() takes milliseconds (the timeout rounded up then). |
35 | | */ |
36 | | static int do_epoll_wait(int epfd, struct epoll_event *events, |
37 | | int maxevents, apr_interval_time_t timeout) |
38 | 0 | { |
39 | | #if defined(HAVE_EPOLL_PWAIT2) |
40 | | struct timespec ts, *tsp = poll_timespec(timeout, &ts); |
41 | | |
42 | | return epoll_pwait2(epfd, events, maxevents, tsp, NULL); |
43 | | #else |
44 | 0 | if (timeout > 0) { |
45 | | /* convert microseconds to milliseconds (round up) */ |
46 | 0 | timeout = (timeout + 999) / 1000; |
47 | 0 | } |
48 | 0 | return epoll_wait(epfd, events, maxevents, (int)timeout); |
49 | 0 | #endif |
50 | 0 | } |
51 | | |
52 | | static unsigned get_epoll_event(apr_int16_t event) |
53 | 0 | { |
54 | 0 | unsigned rv = 0; |
55 | |
|
56 | 0 | if (event & APR_POLLIN) |
57 | 0 | rv |= EPOLLIN; |
58 | 0 | if (event & APR_POLLPRI) |
59 | 0 | rv |= EPOLLPRI; |
60 | 0 | if (event & APR_POLLOUT) |
61 | 0 | rv |= EPOLLOUT; |
62 | 0 | #ifdef EPOLLEXCLUSIVE |
63 | 0 | if (event & APR_POLLEXCL) |
64 | 0 | rv |= EPOLLEXCLUSIVE; |
65 | 0 | #endif |
66 | 0 | if (event & APR_POLLONESHOT) |
67 | 0 | rv |= EPOLLONESHOT; |
68 | 0 | if (event & APR_POLLET) |
69 | 0 | rv |= EPOLLET; |
70 | 0 | #ifdef EPOLLRDHUP |
71 | 0 | if (event & APR_POLLRDHUP) |
72 | 0 | rv |= EPOLLRDHUP; |
73 | 0 | #endif |
74 | | /* APR_POLLNVAL is not handled by epoll. EPOLLERR and EPOLLHUP are return-only */ |
75 | |
|
76 | 0 | return rv; |
77 | 0 | } |
78 | | |
79 | | static apr_int16_t get_epoll_revent(unsigned event) |
80 | 0 | { |
81 | 0 | apr_int16_t rv = 0; |
82 | |
|
83 | 0 | if (event & EPOLLIN) |
84 | 0 | rv |= APR_POLLIN; |
85 | 0 | if (event & EPOLLPRI) |
86 | 0 | rv |= APR_POLLPRI; |
87 | 0 | if (event & EPOLLOUT) |
88 | 0 | rv |= APR_POLLOUT; |
89 | 0 | if (event & EPOLLERR) |
90 | 0 | rv |= APR_POLLERR; |
91 | 0 | if (event & EPOLLHUP) |
92 | 0 | rv |= APR_POLLHUP; |
93 | 0 | #ifdef EPOLLRDHUP |
94 | 0 | if (event & EPOLLRDHUP) |
95 | 0 | rv |= APR_POLLRDHUP; |
96 | 0 | #endif |
97 | | /* APR_POLLNVAL is not handled by epoll. */ |
98 | |
|
99 | 0 | return rv; |
100 | 0 | } |
101 | | |
102 | | struct apr_pollset_private_t |
103 | | { |
104 | | int epoll_fd; |
105 | | struct epoll_event *pollset; |
106 | | apr_pollfd_t *result_set; |
107 | | #if APR_HAS_THREADS |
108 | | /* A thread mutex to protect operations on the rings */ |
109 | | apr_thread_mutex_t *ring_lock; |
110 | | #endif |
111 | | /* A ring containing all of the pollfd_t that are active */ |
112 | | struct pfd_ring_t query_ring; |
113 | | /* A ring of pollfd_t that have been used, and then _remove()'d */ |
114 | | struct pfd_ring_t free_ring; |
115 | | /* A ring of pollfd_t where rings that have been _remove()`ed but |
116 | | might still be inside a _poll() */ |
117 | | struct pfd_ring_t dead_ring; |
118 | | }; |
119 | | |
120 | | static apr_status_t impl_pollset_cleanup(apr_pollset_t *pollset) |
121 | 0 | { |
122 | 0 | close(pollset->p->epoll_fd); |
123 | 0 | return APR_SUCCESS; |
124 | 0 | } |
125 | | |
126 | | |
127 | | static apr_status_t impl_pollset_create(apr_pollset_t *pollset, |
128 | | apr_uint32_t size, |
129 | | apr_pool_t *p, |
130 | | apr_uint32_t flags) |
131 | 0 | { |
132 | 0 | apr_status_t rv; |
133 | 0 | int fd; |
134 | |
|
135 | 0 | #ifdef HAVE_EPOLL_CREATE1 |
136 | 0 | fd = epoll_create1(EPOLL_CLOEXEC); |
137 | | #else |
138 | | fd = epoll_create(size); |
139 | | #endif |
140 | 0 | if (fd < 0) { |
141 | 0 | pollset->p = NULL; |
142 | 0 | return apr_get_netos_error(); |
143 | 0 | } |
144 | | |
145 | | #ifndef HAVE_EPOLL_CREATE1 |
146 | | { |
147 | | int fd_flags; |
148 | | |
149 | | if ((fd_flags = fcntl(fd, F_GETFD)) == -1) { |
150 | | rv = errno; |
151 | | close(fd); |
152 | | pollset->p = NULL; |
153 | | return rv; |
154 | | } |
155 | | |
156 | | fd_flags |= FD_CLOEXEC; |
157 | | if (fcntl(fd, F_SETFD, fd_flags) == -1) { |
158 | | rv = errno; |
159 | | close(fd); |
160 | | pollset->p = NULL; |
161 | | return rv; |
162 | | } |
163 | | } |
164 | | #endif |
165 | | |
166 | 0 | pollset->p = apr_pcalloc(p, sizeof(apr_pollset_private_t)); |
167 | 0 | #if APR_HAS_THREADS |
168 | 0 | if ((flags & APR_POLLSET_THREADSAFE) && |
169 | 0 | !(flags & APR_POLLSET_NOCOPY) && |
170 | 0 | ((rv = apr_thread_mutex_create(&pollset->p->ring_lock, |
171 | 0 | APR_THREAD_MUTEX_DEFAULT, |
172 | 0 | p)) != APR_SUCCESS)) { |
173 | 0 | close(fd); |
174 | 0 | pollset->p = NULL; |
175 | 0 | return rv; |
176 | 0 | } |
177 | | #else |
178 | | if (flags & APR_POLLSET_THREADSAFE) { |
179 | | close(fd); |
180 | | pollset->p = NULL; |
181 | | return APR_ENOTIMPL; |
182 | | } |
183 | | #endif |
184 | 0 | pollset->p->epoll_fd = fd; |
185 | 0 | pollset->p->pollset = apr_palloc(p, size * sizeof(struct epoll_event)); |
186 | 0 | pollset->p->result_set = apr_palloc(p, size * sizeof(apr_pollfd_t)); |
187 | |
|
188 | 0 | if (!(flags & APR_POLLSET_NOCOPY)) { |
189 | 0 | APR_RING_INIT(&pollset->p->query_ring, pfd_elem_t, link); |
190 | 0 | APR_RING_INIT(&pollset->p->free_ring, pfd_elem_t, link); |
191 | 0 | APR_RING_INIT(&pollset->p->dead_ring, pfd_elem_t, link); |
192 | 0 | } |
193 | 0 | return APR_SUCCESS; |
194 | 0 | } |
195 | | |
196 | | static apr_status_t impl_pollset_add(apr_pollset_t *pollset, |
197 | | const apr_pollfd_t *descriptor) |
198 | 0 | { |
199 | 0 | struct epoll_event ev = {0}; |
200 | 0 | int ret; |
201 | 0 | pfd_elem_t *elem = NULL; |
202 | 0 | int reused = 0; |
203 | 0 | apr_status_t rv = APR_SUCCESS; |
204 | |
|
205 | 0 | ev.events = get_epoll_event(descriptor->reqevents); |
206 | |
|
207 | 0 | if (pollset->flags & APR_POLLSET_NOCOPY) { |
208 | 0 | ev.data.ptr = (void *)descriptor; |
209 | 0 | } |
210 | 0 | else { |
211 | 0 | pollset_lock_rings(); |
212 | |
|
213 | 0 | if (descriptor->reqevents & (APR_POLLONESHOT | APR_POLLET)) { |
214 | | /* A one-shot (disarmed) or edge-triggered descriptor added again |
215 | | * is still registered, its element reused rather than leaked |
216 | | */ |
217 | 0 | elem = pfd_ring_take(&(pollset->p->query_ring), descriptor); |
218 | 0 | reused = (elem != NULL); |
219 | 0 | } |
220 | 0 | if (!elem) { |
221 | 0 | elem = pfd_ring_get(&(pollset->p->free_ring), pollset->pool); |
222 | 0 | } |
223 | 0 | elem->pfd = *descriptor; |
224 | 0 | ev.data.ptr = elem; |
225 | 0 | } |
226 | 0 | { |
227 | 0 | int fd = poll_desc_fd(descriptor); |
228 | 0 | if (descriptor->reqevents & (APR_POLLONESHOT | APR_POLLET)) { |
229 | | /* One-shot and edge-triggered descriptors stay registered once |
230 | | * added, NOCOPY pollset or not (disarmed when removed, one-shot |
231 | | * ones when returned too), so try to re-arm first (one syscall); |
232 | | * adding them again also changes their events. |
233 | | */ |
234 | 0 | ret = epoll_ctl(pollset->p->epoll_fd, EPOLL_CTL_MOD, fd, &ev); |
235 | 0 | if (ret != 0 && errno == ENOENT) { |
236 | 0 | ret = epoll_ctl(pollset->p->epoll_fd, EPOLL_CTL_ADD, fd, &ev); |
237 | 0 | } |
238 | 0 | } |
239 | 0 | else { |
240 | 0 | ret = epoll_ctl(pollset->p->epoll_fd, EPOLL_CTL_ADD, fd, &ev); |
241 | 0 | if (ret != 0 && errno == EEXIST) { |
242 | | /* Removed while one-shot or edge-triggered, it's still |
243 | | * registered (disarmed) |
244 | | */ |
245 | 0 | ret = epoll_ctl(pollset->p->epoll_fd, EPOLL_CTL_MOD, fd, &ev); |
246 | 0 | } |
247 | 0 | } |
248 | 0 | } |
249 | |
|
250 | 0 | if (0 != ret) { |
251 | 0 | rv = apr_get_netos_error(); |
252 | 0 | } |
253 | |
|
254 | 0 | if (!(pollset->flags & APR_POLLSET_NOCOPY)) { |
255 | 0 | if (rv != APR_SUCCESS && !reused) { |
256 | 0 | APR_RING_INSERT_TAIL(&(pollset->p->free_ring), elem, pfd_elem_t, link); |
257 | 0 | } |
258 | 0 | else { |
259 | 0 | APR_RING_INSERT_TAIL(&(pollset->p->query_ring), elem, pfd_elem_t, link); |
260 | 0 | } |
261 | 0 | pollset_unlock_rings(); |
262 | 0 | } |
263 | |
|
264 | 0 | return rv; |
265 | 0 | } |
266 | | |
267 | | static apr_status_t impl_pollset_remove(apr_pollset_t *pollset, |
268 | | const apr_pollfd_t *descriptor) |
269 | 0 | { |
270 | 0 | pfd_elem_t *ep; |
271 | 0 | apr_status_t rv = APR_SUCCESS; |
272 | 0 | struct epoll_event ev = {0}; /* ignored, but must be passed with |
273 | | * kernel < 2.6.9 |
274 | | */ |
275 | 0 | int ret; |
276 | 0 | int fd = poll_desc_fd(descriptor); |
277 | |
|
278 | 0 | if (descriptor->reqevents & (APR_POLLONESHOT | APR_POLLET)) { |
279 | | /* Disarm but keep it registered, re-armed by the next add (MOD): |
280 | | * no events but those always reported (ERR/HUP), at most once |
281 | | * (one-shot or edge-triggered) and ignored by impl_pollset_poll() |
282 | | * (NULL data). A copying pollset's element still goes to the dead |
283 | | * ring below. |
284 | | */ |
285 | 0 | ev.events = (descriptor->reqevents & APR_POLLONESHOT) ? EPOLLONESHOT |
286 | 0 | : EPOLLET; |
287 | 0 | ev.data.ptr = NULL; |
288 | 0 | ret = epoll_ctl(pollset->p->epoll_fd, EPOLL_CTL_MOD, fd, &ev); |
289 | 0 | } |
290 | 0 | else { |
291 | 0 | ret = epoll_ctl(pollset->p->epoll_fd, EPOLL_CTL_DEL, fd, &ev); |
292 | 0 | } |
293 | 0 | if (ret < 0) { |
294 | 0 | rv = APR_NOTFOUND; |
295 | 0 | } |
296 | |
|
297 | 0 | if (!(pollset->flags & APR_POLLSET_NOCOPY)) { |
298 | 0 | pollset_lock_rings(); |
299 | |
|
300 | 0 | ep = pfd_ring_take(&(pollset->p->query_ring), descriptor); |
301 | 0 | if (ep) { |
302 | 0 | APR_RING_INSERT_TAIL(&(pollset->p->dead_ring), ep, pfd_elem_t, link); |
303 | 0 | } |
304 | |
|
305 | 0 | pollset_unlock_rings(); |
306 | 0 | } |
307 | |
|
308 | 0 | return rv; |
309 | 0 | } |
310 | | |
311 | | static apr_status_t impl_pollset_poll(apr_pollset_t *pollset, |
312 | | apr_interval_time_t timeout, |
313 | | apr_int32_t *num, |
314 | | const apr_pollfd_t **descriptors) |
315 | 0 | { |
316 | 0 | int ret; |
317 | 0 | apr_status_t rv = APR_SUCCESS; |
318 | |
|
319 | 0 | *num = 0; |
320 | |
|
321 | 0 | ret = do_epoll_wait(pollset->p->epoll_fd, pollset->p->pollset, |
322 | 0 | pollset->nalloc, timeout); |
323 | | |
324 | | /* A copying pollset's elements are copied with the ring lock held, so |
325 | | * that none is rewritten by an add (re-armed one-shot) nor recycled by |
326 | | * another poller (dead ring) meanwhile. |
327 | | */ |
328 | 0 | if (!(pollset->flags & APR_POLLSET_NOCOPY)) { |
329 | 0 | pollset_lock_rings(); |
330 | 0 | } |
331 | 0 | if (ret < 0) { |
332 | 0 | rv = apr_get_netos_error(); |
333 | 0 | } |
334 | 0 | else if (ret == 0) { |
335 | 0 | rv = APR_TIMEUP; |
336 | 0 | } |
337 | 0 | else { |
338 | 0 | int i, j; |
339 | 0 | const apr_pollfd_t *fdptr; |
340 | |
|
341 | 0 | for (i = 0, j = 0; i < ret; i++) { |
342 | 0 | void *ptr = pollset->p->pollset[i].data.ptr; |
343 | |
|
344 | 0 | if (!ptr) { |
345 | 0 | continue; /* disarmed (ERR/HUP) */ |
346 | 0 | } |
347 | 0 | if (pollset->flags & APR_POLLSET_NOCOPY) { |
348 | 0 | fdptr = (apr_pollfd_t *)ptr; |
349 | 0 | } |
350 | 0 | else { |
351 | 0 | fdptr = &(((pfd_elem_t *)ptr)->pfd); |
352 | 0 | } |
353 | | /* Check if the polled descriptor is our |
354 | | * wakeup pipe. In that case do not put it result set. |
355 | | */ |
356 | 0 | if (pollset_is_wakeup(pollset, fdptr)) { |
357 | 0 | pollset_drain_wakeup(pollset); |
358 | 0 | rv = APR_EINTR; |
359 | 0 | } |
360 | 0 | else { |
361 | 0 | pollset->p->result_set[j] = *fdptr; |
362 | 0 | pollset->p->result_set[j].rtnevents = |
363 | 0 | get_epoll_revent(pollset->p->pollset[i].events); |
364 | 0 | j++; |
365 | 0 | } |
366 | 0 | } |
367 | 0 | if (((*num) = j)) { /* any event besides wakeup pipe? */ |
368 | 0 | rv = APR_SUCCESS; |
369 | |
|
370 | 0 | if (descriptors) { |
371 | 0 | *descriptors = pollset->p->result_set; |
372 | 0 | } |
373 | 0 | } |
374 | 0 | } |
375 | |
|
376 | 0 | if (!(pollset->flags & APR_POLLSET_NOCOPY)) { |
377 | | /* Shift all PFDs in the Dead Ring to the Free Ring */ |
378 | 0 | APR_RING_CONCAT(&(pollset->p->free_ring), &(pollset->p->dead_ring), pfd_elem_t, link); |
379 | |
|
380 | 0 | pollset_unlock_rings(); |
381 | 0 | } |
382 | |
|
383 | 0 | return rv; |
384 | 0 | } |
385 | | |
386 | | /* apr_pollset_poller_poll(): the events and results in the poller's arrays |
387 | | * (a NOCOPY pollset's data.ptr are the caller's descriptors), the wakeup pipe |
388 | | * drained. |
389 | | */ |
390 | | static apr_status_t impl_pollset_poller_create(apr_pollset_poller_t *poller, |
391 | | apr_pool_t *p) |
392 | 0 | { |
393 | 0 | poller->buf = apr_palloc(p, poller->max * sizeof(struct epoll_event)); |
394 | 0 | return APR_SUCCESS; |
395 | 0 | } |
396 | | |
397 | | static apr_status_t impl_pollset_poller_poll(apr_pollset_poller_t *poller, |
398 | | apr_interval_time_t timeout, |
399 | | apr_int32_t *num) |
400 | 0 | { |
401 | 0 | apr_pollset_t *pollset = poller->pollset; |
402 | 0 | apr_pollfd_t *results = poller->results; |
403 | 0 | struct epoll_event *events = poller->buf; |
404 | 0 | int ret, i, j, woken = 0; |
405 | |
|
406 | 0 | *num = 0; |
407 | |
|
408 | 0 | ret = do_epoll_wait(pollset->p->epoll_fd, events, poller->max, timeout); |
409 | 0 | if (ret < 0) { |
410 | 0 | return apr_get_netos_error(); |
411 | 0 | } |
412 | 0 | if (ret == 0) { |
413 | 0 | return APR_TIMEUP; |
414 | 0 | } |
415 | | |
416 | 0 | for (i = 0, j = 0; i < ret; i++) { |
417 | 0 | const apr_pollfd_t *fdptr = events[i].data.ptr; |
418 | 0 | if (!fdptr) { |
419 | 0 | continue; /* disarmed (ERR/HUP) */ |
420 | 0 | } |
421 | 0 | if (pollset_is_wakeup(pollset, fdptr)) { |
422 | 0 | pollset_drain_wakeup(pollset); |
423 | 0 | woken = 1; |
424 | 0 | continue; |
425 | 0 | } |
426 | 0 | results[j] = *fdptr; |
427 | 0 | results[j].rtnevents = get_epoll_revent(events[i].events); |
428 | 0 | j++; |
429 | 0 | } |
430 | |
|
431 | 0 | *num = j; |
432 | | /* EINTR for the wakeup alone, SUCCESS with no descriptor for the events |
433 | | * of disarmed descriptors |
434 | | */ |
435 | 0 | return (j || !woken) ? APR_SUCCESS : APR_EINTR; |
436 | 0 | } |
437 | | |
438 | | /* The reqevents honored (apr_poll_method_options()) */ |
439 | | #ifdef EPOLLEXCLUSIVE |
440 | | #define EPOLL_OPTIONS_EXCL APR_POLLEXCL |
441 | | #else |
442 | | #define EPOLL_OPTIONS_EXCL 0 |
443 | | #endif |
444 | | #ifdef EPOLLRDHUP |
445 | | #define EPOLL_OPTIONS_RDHUP APR_POLLRDHUP |
446 | | #else |
447 | | #define EPOLL_OPTIONS_RDHUP 0 |
448 | | #endif |
449 | | #define EPOLL_OPTIONS (APR_POLLIN | APR_POLLPRI | APR_POLLOUT \ |
450 | | | APR_POLLONESHOT | APR_POLLET \ |
451 | | | EPOLL_OPTIONS_EXCL | EPOLL_OPTIONS_RDHUP) |
452 | | |
453 | | static const apr_pollset_provider_t impl = { |
454 | | impl_pollset_create, |
455 | | impl_pollset_add, |
456 | | impl_pollset_remove, |
457 | | impl_pollset_poll, |
458 | | impl_pollset_poller_create, |
459 | | impl_pollset_poller_poll, |
460 | | impl_pollset_cleanup, |
461 | | "epoll", |
462 | | APR_POLLSET_EPOLL, |
463 | | EPOLL_OPTIONS |
464 | | }; |
465 | | |
466 | | const apr_pollset_provider_t *const apr_pollset_provider_epoll = &impl; |
467 | | |
468 | | static apr_status_t impl_pollcb_cleanup(apr_pollcb_t *pollcb) |
469 | 0 | { |
470 | 0 | close(pollcb->fd); |
471 | 0 | return APR_SUCCESS; |
472 | 0 | } |
473 | | |
474 | | static apr_status_t impl_pollcb_create(apr_pollcb_t *pollcb, |
475 | | apr_uint32_t size, |
476 | | apr_pool_t *p, |
477 | | apr_uint32_t flags) |
478 | 0 | { |
479 | 0 | int fd; |
480 | |
|
481 | 0 | #ifdef HAVE_EPOLL_CREATE1 |
482 | 0 | fd = epoll_create1(EPOLL_CLOEXEC); |
483 | | #else |
484 | | fd = epoll_create(size); |
485 | | #endif |
486 | |
|
487 | 0 | if (fd < 0) { |
488 | 0 | return apr_get_netos_error(); |
489 | 0 | } |
490 | | |
491 | | #ifndef HAVE_EPOLL_CREATE1 |
492 | | { |
493 | | int fd_flags; |
494 | | apr_status_t rv; |
495 | | |
496 | | if ((fd_flags = fcntl(fd, F_GETFD)) == -1) { |
497 | | rv = errno; |
498 | | close(fd); |
499 | | pollcb->fd = -1; |
500 | | return rv; |
501 | | } |
502 | | |
503 | | fd_flags |= FD_CLOEXEC; |
504 | | if (fcntl(fd, F_SETFD, fd_flags) == -1) { |
505 | | rv = errno; |
506 | | close(fd); |
507 | | pollcb->fd = -1; |
508 | | return rv; |
509 | | } |
510 | | } |
511 | | #endif |
512 | | |
513 | 0 | pollcb->fd = fd; |
514 | 0 | pollcb->pollset.epoll = apr_palloc(p, size * sizeof(struct epoll_event)); |
515 | |
|
516 | 0 | return APR_SUCCESS; |
517 | 0 | } |
518 | | |
519 | | static apr_status_t impl_pollcb_add(apr_pollcb_t *pollcb, |
520 | | apr_pollfd_t *descriptor) |
521 | 0 | { |
522 | 0 | struct epoll_event ev = { 0 }; |
523 | 0 | int ret, fd; |
524 | |
|
525 | 0 | ev.events = get_epoll_event(descriptor->reqevents); |
526 | 0 | ev.data.ptr = (void *) descriptor; |
527 | |
|
528 | 0 | fd = poll_desc_fd(descriptor); |
529 | 0 | if (descriptor->reqevents & (APR_POLLONESHOT | APR_POLLET)) { |
530 | | /* One-shot and edge-triggered descriptors stay registered once |
531 | | * added (disarmed when removed, one-shot ones when returned too), so |
532 | | * try to re-arm first (one syscall); adding them again also changes |
533 | | * their events. |
534 | | */ |
535 | 0 | ret = epoll_ctl(pollcb->fd, EPOLL_CTL_MOD, fd, &ev); |
536 | 0 | if (ret != 0 && errno == ENOENT) { |
537 | 0 | ret = epoll_ctl(pollcb->fd, EPOLL_CTL_ADD, fd, &ev); |
538 | 0 | } |
539 | 0 | } |
540 | 0 | else { |
541 | 0 | ret = epoll_ctl(pollcb->fd, EPOLL_CTL_ADD, fd, &ev); |
542 | 0 | if (ret != 0 && errno == EEXIST) { |
543 | | /* Removed while one-shot or edge-triggered, it's still |
544 | | * registered (disarmed) |
545 | | */ |
546 | 0 | ret = epoll_ctl(pollcb->fd, EPOLL_CTL_MOD, fd, &ev); |
547 | 0 | } |
548 | 0 | } |
549 | |
|
550 | 0 | if (ret == -1) { |
551 | 0 | return apr_get_netos_error(); |
552 | 0 | } |
553 | | |
554 | 0 | return APR_SUCCESS; |
555 | 0 | } |
556 | | |
557 | | static apr_status_t impl_pollcb_remove(apr_pollcb_t *pollcb, |
558 | | apr_pollfd_t *descriptor) |
559 | 0 | { |
560 | 0 | apr_status_t rv = APR_SUCCESS; |
561 | 0 | struct epoll_event ev = {0}; /* ignored, but must be passed with |
562 | | * kernel < 2.6.9 |
563 | | */ |
564 | 0 | int ret; |
565 | |
|
566 | 0 | if (descriptor->reqevents & (APR_POLLONESHOT | APR_POLLET)) { |
567 | | /* Disarm but keep it registered, re-armed by the next add (MOD): |
568 | | * no events but those always reported (ERR/HUP), at most once |
569 | | * (one-shot or edge-triggered) and ignored by impl_pollcb_poll() |
570 | | * (NULL data). |
571 | | */ |
572 | 0 | int fd = poll_desc_fd(descriptor); |
573 | 0 | ev.events = (descriptor->reqevents & APR_POLLONESHOT) ? EPOLLONESHOT |
574 | 0 | : EPOLLET; |
575 | 0 | ev.data.ptr = NULL; |
576 | 0 | ret = epoll_ctl(pollcb->fd, EPOLL_CTL_MOD, fd, &ev); |
577 | 0 | return (ret < 0) ? APR_NOTFOUND : APR_SUCCESS; |
578 | 0 | } |
579 | | |
580 | 0 | ret = epoll_ctl(pollcb->fd, EPOLL_CTL_DEL, poll_desc_fd(descriptor), &ev); |
581 | |
|
582 | 0 | if (ret < 0) { |
583 | 0 | rv = APR_NOTFOUND; |
584 | 0 | } |
585 | |
|
586 | 0 | return rv; |
587 | 0 | } |
588 | | |
589 | | |
590 | | static apr_status_t impl_pollcb_poll(apr_pollcb_t *pollcb, |
591 | | apr_interval_time_t timeout, |
592 | | apr_pollcb_cb_t func, |
593 | | void *baton) |
594 | 0 | { |
595 | 0 | int ret, i; |
596 | 0 | apr_status_t rv = APR_SUCCESS; |
597 | |
|
598 | 0 | ret = do_epoll_wait(pollcb->fd, pollcb->pollset.epoll, pollcb->nalloc, |
599 | 0 | timeout); |
600 | 0 | if (ret < 0) { |
601 | 0 | rv = apr_get_netos_error(); |
602 | 0 | } |
603 | 0 | else if (ret == 0) { |
604 | 0 | rv = APR_TIMEUP; |
605 | 0 | } |
606 | 0 | else { |
607 | 0 | for (i = 0; i < ret; i++) { |
608 | 0 | apr_pollfd_t *pollfd = (apr_pollfd_t *)(pollcb->pollset.epoll[i].data.ptr); |
609 | |
|
610 | 0 | if (!pollfd) { |
611 | 0 | continue; /* disarmed (ERR/HUP) */ |
612 | 0 | } |
613 | 0 | if (pollcb_is_wakeup(pollcb, pollfd)) { |
614 | 0 | pollcb_drain_wakeup(pollcb); |
615 | 0 | return APR_EINTR; |
616 | 0 | } |
617 | | |
618 | 0 | pollfd->rtnevents = get_epoll_revent(pollcb->pollset.epoll[i].events); |
619 | |
|
620 | 0 | rv = func(baton, pollfd); |
621 | 0 | if (rv) { |
622 | 0 | return rv; |
623 | 0 | } |
624 | 0 | } |
625 | 0 | } |
626 | | |
627 | 0 | return rv; |
628 | 0 | } |
629 | | |
630 | | static const apr_pollcb_provider_t impl_cb = { |
631 | | impl_pollcb_create, |
632 | | impl_pollcb_add, |
633 | | impl_pollcb_remove, |
634 | | impl_pollcb_poll, |
635 | | impl_pollcb_cleanup, |
636 | | "epoll", |
637 | | APR_POLLSET_EPOLL, |
638 | | EPOLL_OPTIONS |
639 | | }; |
640 | | |
641 | | const apr_pollcb_provider_t *const apr_pollcb_provider_epoll = &impl_cb; |
642 | | |
643 | | #endif /* HAVE_EPOLL */ |