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 ((fd_flags = fcntl(fd, F_GETFD)) == -1) {
116 fd_flags |= FD_CLOEXEC;
117 if (fcntl(fd, F_SETFD, fd_flags) == -1) {
126 pollset->p = apr_palloc(p, sizeof(apr_pollset_private_t));
128 if ((flags & APR_POLLSET_THREADSAFE) &&
129 !(flags & APR_POLLSET_NOCOPY) &&
130 ((rv = apr_thread_mutex_create(&pollset->p->ring_lock,
131 APR_THREAD_MUTEX_DEFAULT,
132 p)) != APR_SUCCESS)) {
138 if (flags & APR_POLLSET_THREADSAFE) {
144 pollset->p->epoll_fd = fd;
145 pollset->p->pollset = apr_palloc(p, size * sizeof(struct epoll_event));
146 pollset->p->result_set = apr_palloc(p, size * sizeof(apr_pollfd_t));
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);
156 static apr_status_t impl_pollset_add(apr_pollset_t *pollset,
157 const apr_pollfd_t *descriptor)
159 struct epoll_event ev = {0};
161 pfd_elem_t *elem = NULL;
162 apr_status_t rv = APR_SUCCESS;
164 ev.events = get_epoll_event(descriptor->reqevents);
166 if (pollset->flags & APR_POLLSET_NOCOPY) {
167 ev.data.ptr = (void *)descriptor;
170 pollset_lock_rings();
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);
177 elem = (pfd_elem_t *) apr_palloc(pollset->pool, sizeof(pfd_elem_t));
178 APR_RING_ELEM_INIT(elem, link);
180 elem->pfd = *descriptor;
183 if (descriptor->desc_type == APR_POLL_SOCKET) {
184 ret = epoll_ctl(pollset->p->epoll_fd, EPOLL_CTL_ADD,
185 descriptor->desc.s->socketdes, &ev);
188 ret = epoll_ctl(pollset->p->epoll_fd, EPOLL_CTL_ADD,
189 descriptor->desc.f->filedes, &ev);
193 rv = apr_get_netos_error();
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);
201 APR_RING_INSERT_TAIL(&(pollset->p->query_ring), elem, pfd_elem_t, link);
203 pollset_unlock_rings();
209 static apr_status_t impl_pollset_remove(apr_pollset_t *pollset,
210 const apr_pollfd_t *descriptor)
213 apr_status_t rv = APR_SUCCESS;
214 struct epoll_event ev = {0}; /* ignored, but must be passed with
219 if (descriptor->desc_type == APR_POLL_SOCKET) {
220 ret = epoll_ctl(pollset->p->epoll_fd, EPOLL_CTL_DEL,
221 descriptor->desc.s->socketdes, &ev);
224 ret = epoll_ctl(pollset->p->epoll_fd, EPOLL_CTL_DEL,
225 descriptor->desc.f->filedes, &ev);
231 if (!(pollset->flags & APR_POLLSET_NOCOPY)) {
232 pollset_lock_rings();
234 for (ep = APR_RING_FIRST(&(pollset->p->query_ring));
235 ep != APR_RING_SENTINEL(&(pollset->p->query_ring),
237 ep = APR_RING_NEXT(ep, link)) {
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);
247 pollset_unlock_rings();
253 static apr_status_t impl_pollset_poll(apr_pollset_t *pollset,
254 apr_interval_time_t timeout,
256 const apr_pollfd_t **descriptors)
259 apr_status_t rv = APR_SUCCESS;
266 ret = epoll_wait(pollset->p->epoll_fd, pollset->p->pollset, pollset->nalloc,
271 rv = apr_get_netos_error();
277 for (i = 0, j = 0; i < ret; i++) {
278 if (pollset->flags & APR_POLLSET_NOCOPY) {
279 fdptr = (apr_pollfd_t *)(pollset->p->pollset[i].data.ptr);
282 fdptr = &(((pfd_elem_t *) (pollset->p->pollset[i].data.ptr))->pfd);
284 /* Check if the polled descriptor is our
285 * wakeup pipe. In that case do not put it result set.
287 if ((pollset->flags & APR_POLLSET_WAKEABLE) &&
288 fdptr->desc_type == APR_POLL_FILE &&
289 fdptr->desc.f == pollset->wakeup_pipe[0]) {
290 apr_pollset_drain_wakeup_pipe(pollset);
294 pollset->p->result_set[j] = *fdptr;
295 pollset->p->result_set[j].rtnevents =
296 get_epoll_revent(pollset->p->pollset[i].events);
300 if (((*num) = j)) { /* any event besides wakeup pipe? */
304 *descriptors = pollset->p->result_set;
309 if (!(pollset->flags & APR_POLLSET_NOCOPY)) {
310 pollset_lock_rings();
312 /* Shift all PFDs in the Dead Ring to the Free Ring */
313 APR_RING_CONCAT(&(pollset->p->free_ring), &(pollset->p->dead_ring), pfd_elem_t, link);
315 pollset_unlock_rings();
321 static apr_pollset_provider_t impl = {
326 impl_pollset_cleanup,
330 apr_pollset_provider_t *apr_pollset_provider_epoll = &impl;
332 static apr_status_t cb_cleanup(void *p_)
334 apr_pollcb_t *pollcb = (apr_pollcb_t *) p_;
339 static apr_status_t impl_pollcb_create(apr_pollcb_t *pollcb,
346 #ifdef HAVE_EPOLL_CREATE1
347 fd = epoll_create1(EPOLL_CLOEXEC);
349 fd = epoll_create(size);
353 return apr_get_netos_error();
356 #ifndef HAVE_EPOLL_CREATE1
361 if ((fd_flags = fcntl(fd, F_GETFD)) == -1) {
368 fd_flags |= FD_CLOEXEC;
369 if (fcntl(fd, F_SETFD, fd_flags) == -1) {
379 pollcb->pollset.epoll = apr_palloc(p, size * sizeof(struct epoll_event));
380 apr_pool_cleanup_register(p, pollcb, cb_cleanup, apr_pool_cleanup_null);
385 static apr_status_t impl_pollcb_add(apr_pollcb_t *pollcb,
386 apr_pollfd_t *descriptor)
388 struct epoll_event ev;
391 ev.events = get_epoll_event(descriptor->reqevents);
392 ev.data.ptr = (void *)descriptor;
394 if (descriptor->desc_type == APR_POLL_SOCKET) {
395 ret = epoll_ctl(pollcb->fd, EPOLL_CTL_ADD,
396 descriptor->desc.s->socketdes, &ev);
399 ret = epoll_ctl(pollcb->fd, EPOLL_CTL_ADD,
400 descriptor->desc.f->filedes, &ev);
404 return apr_get_netos_error();
410 static apr_status_t impl_pollcb_remove(apr_pollcb_t *pollcb,
411 apr_pollfd_t *descriptor)
413 apr_status_t rv = APR_SUCCESS;
414 struct epoll_event ev = {0}; /* ignored, but must be passed with
419 if (descriptor->desc_type == APR_POLL_SOCKET) {
420 ret = epoll_ctl(pollcb->fd, EPOLL_CTL_DEL,
421 descriptor->desc.s->socketdes, &ev);
424 ret = epoll_ctl(pollcb->fd, EPOLL_CTL_DEL,
425 descriptor->desc.f->filedes, &ev);
436 static apr_status_t impl_pollcb_poll(apr_pollcb_t *pollcb,
437 apr_interval_time_t timeout,
438 apr_pollcb_cb_t func,
442 apr_status_t rv = APR_SUCCESS;
448 ret = epoll_wait(pollcb->fd, pollcb->pollset.epoll, pollcb->nalloc,
451 rv = apr_get_netos_error();
457 for (i = 0; i < ret; i++) {
458 apr_pollfd_t *pollfd = (apr_pollfd_t *)(pollcb->pollset.epoll[i].data.ptr);
459 pollfd->rtnevents = get_epoll_revent(pollcb->pollset.epoll[i].events);
461 rv = func(baton, pollfd);
471 static apr_pollcb_provider_t impl_cb = {
479 apr_pollcb_provider_t *apr_pollcb_provider_epoll = &impl_cb;
481 #endif /* HAVE_EPOLL */