Apache HTTPD
epoll.c
Go to the documentation of this file.
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"
24#include "apr_arch_inherit.h"
25
26#if defined(HAVE_EPOLL)
27
29{
30 apr_int16_t rv = 0;
31
32 if (event & APR_POLLIN)
33 rv |= EPOLLIN;
34 if (event & APR_POLLPRI)
35 rv |= EPOLLPRI;
36 if (event & APR_POLLOUT)
37 rv |= EPOLLOUT;
38 /* APR_POLLNVAL is not handled by epoll. EPOLLERR and EPOLLHUP are return-only */
39
40 return rv;
41}
42
44{
45 apr_int16_t rv = 0;
46
47 if (event & EPOLLIN)
48 rv |= APR_POLLIN;
49 if (event & EPOLLPRI)
50 rv |= APR_POLLPRI;
51 if (event & EPOLLOUT)
52 rv |= APR_POLLOUT;
53 if (event & EPOLLERR)
54 rv |= APR_POLLERR;
55 if (event & EPOLLHUP)
56 rv |= APR_POLLHUP;
57 /* APR_POLLNVAL is not handled by epoll. */
58
59 return rv;
60}
61
63{
64 int epoll_fd;
65 struct epoll_event *pollset;
67#if APR_HAS_THREADS
68 /* A thread mutex to protect operations on the rings */
70#endif
71 /* A ring containing all of the pollfd_t that are active */
73 /* A ring of pollfd_t that have been used, and then _remove()'d */
75 /* A ring of pollfd_t where rings that have been _remove()`ed but
76 might still be inside a _poll() */
78};
79
81{
82 close(pollset->p->epoll_fd);
83 return APR_SUCCESS;
84}
85
86
91{
92 apr_status_t rv;
93 int fd;
94
95#ifdef HAVE_EPOLL_CREATE1
97#else
99#endif
100 if (fd < 0) {
101 pollset->p = NULL;
102 return apr_get_netos_error();
103 }
104
105#ifndef HAVE_EPOLL_CREATE1
106 {
107 int fd_flags;
108
109 if ((fd_flags = fcntl(fd, F_GETFD)) == -1) {
110 rv = errno;
111 close(fd);
112 pollset->p = NULL;
113 return rv;
114 }
115
117 if (fcntl(fd, F_SETFD, fd_flags) == -1) {
118 rv = errno;
119 close(fd);
120 pollset->p = NULL;
121 return rv;
122 }
123 }
124#endif
125
127#if APR_HAS_THREADS
130 ((rv = apr_thread_mutex_create(&pollset->p->ring_lock,
132 p)) != APR_SUCCESS)) {
133 close(fd);
134 pollset->p = NULL;
135 return rv;
136 }
137#else
139 close(fd);
140 pollset->p = NULL;
141 return APR_ENOTIMPL;
142 }
143#endif
144 pollset->p->epoll_fd = fd;
145 pollset->p->pollset = apr_palloc(p, size * sizeof(struct epoll_event));
147
148 if (!(flags & APR_POLLSET_NOCOPY)) {
149 APR_RING_INIT(&pollset->p->query_ring, pfd_elem_t, link);
150 APR_RING_INIT(&pollset->p->free_ring, pfd_elem_t, link);
151 APR_RING_INIT(&pollset->p->dead_ring, pfd_elem_t, link);
152 }
153 return APR_SUCCESS;
154}
155
158{
159 struct epoll_event ev = {0};
160 int ret;
163
165
167 ev.data.ptr = (void *)descriptor;
168 }
169 else {
171
172 if (!APR_RING_EMPTY(&(pollset->p->free_ring), pfd_elem_t, link)) {
173 elem = APR_RING_FIRST(&(pollset->p->free_ring));
174 APR_RING_REMOVE(elem, link);
175 }
176 else {
179 }
180 elem->pfd = *descriptor;
181 ev.data.ptr = elem;
182 }
184 ret = epoll_ctl(pollset->p->epoll_fd, EPOLL_CTL_ADD,
186 }
187 else {
188 ret = epoll_ctl(pollset->p->epoll_fd, EPOLL_CTL_ADD,
190 }
191
192 if (0 != ret) {
193 rv = apr_get_netos_error();
194 }
195
196 if (!(pollset->flags & APR_POLLSET_NOCOPY)) {
197 if (rv != APR_SUCCESS) {
198 APR_RING_INSERT_TAIL(&(pollset->p->free_ring), elem, pfd_elem_t, link);
199 }
200 else {
201 APR_RING_INSERT_TAIL(&(pollset->p->query_ring), elem, pfd_elem_t, link);
202 }
204 }
205
206 return rv;
207}
208
211{
212 pfd_elem_t *ep;
214 struct epoll_event ev = {0}; /* ignored, but must be passed with
215 * kernel < 2.6.9
216 */
217 int ret;
218
220 ret = epoll_ctl(pollset->p->epoll_fd, EPOLL_CTL_DEL,
222 }
223 else {
224 ret = epoll_ctl(pollset->p->epoll_fd, EPOLL_CTL_DEL,
226 }
227 if (ret < 0) {
228 rv = APR_NOTFOUND;
229 }
230
231 if (!(pollset->flags & APR_POLLSET_NOCOPY)) {
233
234 for (ep = APR_RING_FIRST(&(pollset->p->query_ring));
235 ep != APR_RING_SENTINEL(&(pollset->p->query_ring),
236 pfd_elem_t, link);
237 ep = APR_RING_NEXT(ep, link)) {
238
239 if (descriptor->desc.s == ep->pfd.desc.s) {
240 APR_RING_REMOVE(ep, link);
241 APR_RING_INSERT_TAIL(&(pollset->p->dead_ring),
242 ep, pfd_elem_t, link);
243 break;
244 }
245 }
246
248 }
249
250 return rv;
251}
252
257{
258 int ret;
260
261 *num = 0;
262
263 if (timeout > 0) {
264 timeout = (timeout + 999) / 1000;
265 }
266
267 ret = epoll_wait(pollset->p->epoll_fd, pollset->p->pollset, pollset->nalloc,
268 timeout);
269 if (ret < 0) {
270 rv = apr_get_netos_error();
271 }
272 else if (ret == 0) {
273 rv = APR_TIMEUP;
274 }
275 else {
276 int i, j;
277 const apr_pollfd_t *fdptr;
278
279 for (i = 0, j = 0; i < ret; i++) {
281 fdptr = (apr_pollfd_t *)(pollset->p->pollset[i].data.ptr);
282 }
283 else {
284 fdptr = &(((pfd_elem_t *) (pollset->p->pollset[i].data.ptr))->pfd);
285 }
286 /* Check if the polled descriptor is our
287 * wakeup pipe. In that case do not put it result set.
288 */
290 fdptr->desc_type == APR_POLL_FILE &&
291 fdptr->desc.f == pollset->wakeup_pipe[0]) {
293 rv = APR_EINTR;
294 }
295 else {
296 pollset->p->result_set[j] = *fdptr;
298 get_epoll_revent(pollset->p->pollset[i].events);
299 j++;
300 }
301 }
302 if (((*num) = j)) { /* any event besides wakeup pipe? */
303 rv = APR_SUCCESS;
304
305 if (descriptors) {
307 }
308 }
309 }
310
311 if (!(pollset->flags & APR_POLLSET_NOCOPY)) {
313
314 /* Shift all PFDs in the Dead Ring to the Free Ring */
315 APR_RING_CONCAT(&(pollset->p->free_ring), &(pollset->p->dead_ring), pfd_elem_t, link);
316
318 }
319
320 return rv;
321}
322
323static const apr_pollset_provider_t impl = {
329 "epoll"
330};
331
333
335{
336 close(pollcb->fd);
337 return APR_SUCCESS;
338}
339
342 apr_pool_t *p,
344{
345 int fd;
346
347#ifdef HAVE_EPOLL_CREATE1
349#else
351#endif
352
353 if (fd < 0) {
354 return apr_get_netos_error();
355 }
356
357#ifndef HAVE_EPOLL_CREATE1
358 {
359 int fd_flags;
360 apr_status_t rv;
361
362 if ((fd_flags = fcntl(fd, F_GETFD)) == -1) {
363 rv = errno;
364 close(fd);
365 pollcb->fd = -1;
366 return rv;
367 }
368
370 if (fcntl(fd, F_SETFD, fd_flags) == -1) {
371 rv = errno;
372 close(fd);
373 pollcb->fd = -1;
374 return rv;
375 }
376 }
377#endif
378
379 pollcb->fd = fd;
380 pollcb->pollset.epoll = apr_palloc(p, size * sizeof(struct epoll_event));
381
382 return APR_SUCCESS;
383}
384
387{
388 struct epoll_event ev = { 0 };
389 int ret;
390
392 ev.data.ptr = (void *) descriptor;
393
397 }
398 else {
401 }
402
403 if (ret == -1) {
404 return apr_get_netos_error();
405 }
406
407 return APR_SUCCESS;
408}
409
412{
414 struct epoll_event ev = {0}; /* ignored, but must be passed with
415 * kernel < 2.6.9
416 */
417 int ret;
418
422 }
423 else {
426 }
427
428 if (ret < 0) {
429 rv = APR_NOTFOUND;
430 }
431
432 return rv;
433}
434
435
439 void *baton)
440{
441 int ret, i;
443
444 if (timeout > 0) {
445 timeout = (timeout + 999) / 1000;
446 }
447
449 timeout);
450 if (ret < 0) {
451 rv = apr_get_netos_error();
452 }
453 else if (ret == 0) {
454 rv = APR_TIMEUP;
455 }
456 else {
457 for (i = 0; i < ret; i++) {
458 apr_pollfd_t *pollfd = (apr_pollfd_t *)(pollcb->pollset.epoll[i].data.ptr);
459
461 pollfd->desc_type == APR_POLL_FILE &&
462 pollfd->desc.f == pollcb->wakeup_pipe[0]) {
464 return APR_EINTR;
465 }
466
467 pollfd->rtnevents = get_epoll_revent(pollcb->pollset.epoll[i].events);
468
469 rv = func(baton, pollfd);
470 if (rv) {
471 return rv;
472 }
473 }
474 }
475
476 return rv;
477}
478
479static const apr_pollcb_provider_t impl_cb = {
485 "epoll"
486};
487
489
490#endif /* HAVE_EPOLL */
void apr_poll_drain_wakeup_pipe(apr_file_t **wakeup_pipe)
Definition wakeup.c:138
APR Poll interface.
APR Portability Routines.
APR Time Library.
ap_vhost_iterate_conn_cb void * baton
Definition http_vhost.h:87
#define APR_ENOTIMPL
Definition apr_errno.h:476
#define APR_TIMEUP
Definition apr_errno.h:450
#define APR_NOTFOUND
Definition apr_errno.h:463
#define APR_EINTR
Definition apr_errno.h:737
apr_file_t * fd
const char apr_ssize_t int flags
Definition apr_encode.h:168
apr_size_t size
#define apr_get_netos_error()
Definition apr_errno.h:1222
#define APR_SUCCESS
Definition apr_errno.h:225
int apr_status_t
Definition apr_errno.h:44
apr_status_t(* apr_pollcb_cb_t)(void *baton, apr_pollfd_t *descriptor)
Definition apr_poll.h:401
apr_interval_time_t apr_int32_t const apr_pollfd_t ** descriptors
Definition apr_poll.h:274
apr_interval_time_t apr_int32_t * num
Definition apr_poll.h:273
const apr_pollfd_t * descriptor
Definition apr_poll.h:230
apr_interval_time_t apr_pollcb_cb_t func
Definition apr_poll.h:422
@ APR_POLL_SOCKET
Definition apr_poll.h:93
@ APR_POLL_FILE
Definition apr_poll.h:94
#define APR_RING_INSERT_TAIL(hp, nep, elem, link)
Definition apr_ring.h:328
#define APR_RING_INIT(hp, elem, link)
Definition apr_ring.h:192
#define APR_RING_HEAD(head, elem)
Definition apr_ring.h:91
#define APR_RING_CONCAT(h1, h2, elem, link)
Definition apr_ring.h:338
#define APR_RING_SENTINEL(hp, elem, link)
Definition apr_ring.h:159
#define APR_RING_EMPTY(hp, elem, link)
Definition apr_ring.h:204
#define APR_RING_REMOVE(ep, link)
Definition apr_ring.h:381
#define APR_RING_FIRST(hp)
Definition apr_ring.h:166
#define APR_RING_NEXT(ep, link)
Definition apr_ring.h:177
#define APR_RING_ELEM_INIT(ep, link)
Definition apr_ring.h:212
apr_int64_t apr_interval_time_t
Definition apr_time.h:55
#define APR_POLLSET_NOCOPY
Definition apr_poll.h:65
#define APR_POLLSET_WAKEABLE
Definition apr_poll.h:68
#define APR_POLLSET_THREADSAFE
Definition apr_poll.h:62
#define APR_POLLPRI
Definition apr_poll.h:50
#define APR_POLLERR
Definition apr_poll.h:52
#define APR_POLLOUT
Definition apr_poll.h:51
#define APR_POLLIN
Definition apr_poll.h:49
#define APR_POLLHUP
Definition apr_poll.h:53
static md_http_impl_t impl
Definition md_curl.c:639
apr_pool_t * p
Definition md_event.c:32
return NULL
Definition mod_so.c:359
int i
Definition mod_so.c:347
static apr_status_t impl_pollset_poll(apr_pollset_t *pollset, apr_interval_time_t timeout, apr_int32_t *num, const apr_pollfd_t **descriptors)
Definition select.c:339
static apr_status_t impl_pollset_add(apr_pollset_t *pollset, const apr_pollfd_t *descriptor)
Definition select.c:226
static apr_status_t impl_pollset_create(apr_pollset_t *pollset, apr_uint32_t size, apr_pool_t *p, apr_uint32_t flags)
Definition select.c:197
static apr_status_t impl_pollset_remove(apr_pollset_t *pollset, const apr_pollfd_t *descriptor)
Definition select.c:294
apr_file_t * wakeup_pipe[2]
apr_pollcb_pset pollset
apr_int16_t reqevents
Definition apr_poll.h:111
apr_datatype_e desc_type
Definition apr_poll.h:110
apr_descriptor desc
Definition apr_poll.h:113
apr_int16_t rtnevents
Definition apr_poll.h:112
apr_pollfd_t * result_set
Definition select.c:190
apr_file_t * wakeup_pipe[2]
apr_pollset_private_t * p
static apr_pollcb_t * pollcb
Definition testpoll.c:42
static apr_pollset_t * pollset
Definition testpoll.c:41
apr_socket_t * s
Definition apr_poll.h:101
apr_file_t * f
Definition apr_poll.h:100
IN ULONG IN INT timeout