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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
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"
26 #if defined(HAVE_EPOLL)
28 static apr_int16_t get_epoll_event(apr_int16_t event)
32 if (event & APR_POLLIN)
34 if (event & APR_POLLPRI)
36 if (event & APR_POLLOUT)
38 /* APR_POLLNVAL is not handled by epoll. EPOLLERR and EPOLLHUP are return-only */
43 static apr_int16_t get_epoll_revent(apr_int16_t event)
57 /* APR_POLLNVAL is not handled by epoll. */
62 struct apr_pollset_private_t
65 struct epoll_event *pollset;
66 apr_pollfd_t *result_set;
68 /* A thread mutex to protect operations on the rings */
69 apr_thread_mutex_t *ring_lock;
71 /* A ring containing all of the pollfd_t that are active */
72 APR_RING_HEAD(pfd_query_ring_t, pfd_elem_t) query_ring;
73 /* A ring of pollfd_t that have been used, and then _remove()'d */
74 APR_RING_HEAD(pfd_free_ring_t, pfd_elem_t) free_ring;
75 /* A ring of pollfd_t where rings that have been _remove()`ed but
76 might still be inside a _poll() */
77 APR_RING_HEAD(pfd_dead_ring_t, pfd_elem_t) dead_ring;
80 static apr_status_t impl_pollset_cleanup(apr_pollset_t *pollset)
82 close(pollset->p->epoll_fd);
87 static apr_status_t impl_pollset_create(apr_pollset_t *pollset,
95 #ifdef HAVE_EPOLL_CREATE1
96 fd = epoll_create1(EPOLL_CLOEXEC);
98 fd = epoll_create(size);
102 return apr_get_netos_error();
105 #ifndef HAVE_EPOLL_CREATE1
109 if ((flags = fcntl(fd, F_GETFD)) == -1)
113 if (fcntl(fd, F_SETFD, flags) == -1)
118 pollset->p = apr_palloc(p, sizeof(apr_pollset_private_t));
120 if ((flags & APR_POLLSET_THREADSAFE) &&
121 !(flags & APR_POLLSET_NOCOPY) &&
122 ((rv = apr_thread_mutex_create(&pollset->p->ring_lock,
123 APR_THREAD_MUTEX_DEFAULT,
124 p)) != APR_SUCCESS)) {
129 if (flags & APR_POLLSET_THREADSAFE) {
134 pollset->p->epoll_fd = fd;
135 pollset->p->pollset = apr_palloc(p, size * sizeof(struct epoll_event));
136 pollset->p->result_set = apr_palloc(p, size * sizeof(apr_pollfd_t));
138 if (!(flags & APR_POLLSET_NOCOPY)) {
139 APR_RING_INIT(&pollset->p->query_ring, pfd_elem_t, link);
140 APR_RING_INIT(&pollset->p->free_ring, pfd_elem_t, link);
141 APR_RING_INIT(&pollset->p->dead_ring, pfd_elem_t, link);
146 static apr_status_t impl_pollset_add(apr_pollset_t *pollset,
147 const apr_pollfd_t *descriptor)
149 struct epoll_event ev = {0};
151 pfd_elem_t *elem = NULL;
152 apr_status_t rv = APR_SUCCESS;
154 ev.events = get_epoll_event(descriptor->reqevents);
156 if (pollset->flags & APR_POLLSET_NOCOPY) {
157 ev.data.ptr = (void *)descriptor;
160 pollset_lock_rings();
162 if (!APR_RING_EMPTY(&(pollset->p->free_ring), pfd_elem_t, link)) {
163 elem = APR_RING_FIRST(&(pollset->p->free_ring));
164 APR_RING_REMOVE(elem, link);
167 elem = (pfd_elem_t *) apr_palloc(pollset->pool, sizeof(pfd_elem_t));
168 APR_RING_ELEM_INIT(elem, link);
170 elem->pfd = *descriptor;
173 if (descriptor->desc_type == APR_POLL_SOCKET) {
174 ret = epoll_ctl(pollset->p->epoll_fd, EPOLL_CTL_ADD,
175 descriptor->desc.s->socketdes, &ev);
178 ret = epoll_ctl(pollset->p->epoll_fd, EPOLL_CTL_ADD,
179 descriptor->desc.f->filedes, &ev);
183 rv = apr_get_netos_error();
186 if (!(pollset->flags & APR_POLLSET_NOCOPY)) {
187 if (rv != APR_SUCCESS) {
188 APR_RING_INSERT_TAIL(&(pollset->p->free_ring), elem, pfd_elem_t, link);
191 APR_RING_INSERT_TAIL(&(pollset->p->query_ring), elem, pfd_elem_t, link);
193 pollset_unlock_rings();
199 static apr_status_t impl_pollset_remove(apr_pollset_t *pollset,
200 const apr_pollfd_t *descriptor)
203 apr_status_t rv = APR_SUCCESS;
204 struct epoll_event ev = {0}; /* ignored, but must be passed with
209 if (descriptor->desc_type == APR_POLL_SOCKET) {
210 ret = epoll_ctl(pollset->p->epoll_fd, EPOLL_CTL_DEL,
211 descriptor->desc.s->socketdes, &ev);
214 ret = epoll_ctl(pollset->p->epoll_fd, EPOLL_CTL_DEL,
215 descriptor->desc.f->filedes, &ev);
221 if (!(pollset->flags & APR_POLLSET_NOCOPY)) {
222 pollset_lock_rings();
224 for (ep = APR_RING_FIRST(&(pollset->p->query_ring));
225 ep != APR_RING_SENTINEL(&(pollset->p->query_ring),
227 ep = APR_RING_NEXT(ep, link)) {
229 if (descriptor->desc.s == ep->pfd.desc.s) {
230 APR_RING_REMOVE(ep, link);
231 APR_RING_INSERT_TAIL(&(pollset->p->dead_ring),
232 ep, pfd_elem_t, link);
237 pollset_unlock_rings();
243 static apr_status_t impl_pollset_poll(apr_pollset_t *pollset,
244 apr_interval_time_t timeout,
246 const apr_pollfd_t **descriptors)
249 apr_status_t rv = APR_SUCCESS;
256 ret = epoll_wait(pollset->p->epoll_fd, pollset->p->pollset, pollset->nalloc,
261 rv = apr_get_netos_error();
267 for (i = 0, j = 0; i < ret; i++) {
268 if (pollset->flags & APR_POLLSET_NOCOPY) {
269 fdptr = (apr_pollfd_t *)(pollset->p->pollset[i].data.ptr);
272 fdptr = &(((pfd_elem_t *) (pollset->p->pollset[i].data.ptr))->pfd);
274 /* Check if the polled descriptor is our
275 * wakeup pipe. In that case do not put it result set.
277 if ((pollset->flags & APR_POLLSET_WAKEABLE) &&
278 fdptr->desc_type == APR_POLL_FILE &&
279 fdptr->desc.f == pollset->wakeup_pipe[0]) {
280 apr_pollset_drain_wakeup_pipe(pollset);
284 pollset->p->result_set[j] = *fdptr;
285 pollset->p->result_set[j].rtnevents =
286 get_epoll_revent(pollset->p->pollset[i].events);
290 if (((*num) = j)) { /* any event besides wakeup pipe? */
294 *descriptors = pollset->p->result_set;
299 if (!(pollset->flags & APR_POLLSET_NOCOPY)) {
300 pollset_lock_rings();
302 /* Shift all PFDs in the Dead Ring to the Free Ring */
303 APR_RING_CONCAT(&(pollset->p->free_ring), &(pollset->p->dead_ring), pfd_elem_t, link);
305 pollset_unlock_rings();
311 static apr_pollset_provider_t impl = {
316 impl_pollset_cleanup,
320 apr_pollset_provider_t *apr_pollset_provider_epoll = &impl;
322 static apr_status_t cb_cleanup(void *p_)
324 apr_pollcb_t *pollcb = (apr_pollcb_t *) p_;
329 static apr_status_t impl_pollcb_create(apr_pollcb_t *pollcb,
336 #ifdef HAVE_EPOLL_CREATE1
337 fd = epoll_create1(EPOLL_CLOEXEC);
339 fd = epoll_create(size);
343 return apr_get_netos_error();
346 #ifndef HAVE_EPOLL_CREATE1
350 if ((flags = fcntl(fd, F_GETFD)) == -1)
354 if (fcntl(fd, F_SETFD, flags) == -1)
360 pollcb->pollset.epoll = apr_palloc(p, size * sizeof(struct epoll_event));
361 apr_pool_cleanup_register(p, pollcb, cb_cleanup, apr_pool_cleanup_null);
366 static apr_status_t impl_pollcb_add(apr_pollcb_t *pollcb,
367 apr_pollfd_t *descriptor)
369 struct epoll_event ev;
372 ev.events = get_epoll_event(descriptor->reqevents);
373 ev.data.ptr = (void *)descriptor;
375 if (descriptor->desc_type == APR_POLL_SOCKET) {
376 ret = epoll_ctl(pollcb->fd, EPOLL_CTL_ADD,
377 descriptor->desc.s->socketdes, &ev);
380 ret = epoll_ctl(pollcb->fd, EPOLL_CTL_ADD,
381 descriptor->desc.f->filedes, &ev);
385 return apr_get_netos_error();
391 static apr_status_t impl_pollcb_remove(apr_pollcb_t *pollcb,
392 apr_pollfd_t *descriptor)
394 apr_status_t rv = APR_SUCCESS;
395 struct epoll_event ev = {0}; /* ignored, but must be passed with
400 if (descriptor->desc_type == APR_POLL_SOCKET) {
401 ret = epoll_ctl(pollcb->fd, EPOLL_CTL_DEL,
402 descriptor->desc.s->socketdes, &ev);
405 ret = epoll_ctl(pollcb->fd, EPOLL_CTL_DEL,
406 descriptor->desc.f->filedes, &ev);
417 static apr_status_t impl_pollcb_poll(apr_pollcb_t *pollcb,
418 apr_interval_time_t timeout,
419 apr_pollcb_cb_t func,
423 apr_status_t rv = APR_SUCCESS;
429 ret = epoll_wait(pollcb->fd, pollcb->pollset.epoll, pollcb->nalloc,
432 rv = apr_get_netos_error();
438 for (i = 0; i < ret; i++) {
439 apr_pollfd_t *pollfd = (apr_pollfd_t *)(pollcb->pollset.epoll[i].data.ptr);
440 pollfd->rtnevents = get_epoll_revent(pollcb->pollset.epoll[i].events);
442 rv = func(baton, pollfd);
452 static apr_pollcb_provider_t impl_cb = {
460 apr_pollcb_provider_t *apr_pollcb_provider_epoll = &impl_cb;
462 #endif /* HAVE_EPOLL */