Coverage Report

Created: 2023-03-26 06:28

/src/httpd/srclib/apr/poll/unix/epoll.c
Line
Count
Source (jump to first uncovered line)
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
static unsigned get_epoll_event(apr_int16_t event)
29
0
{
30
0
    unsigned rv = 0;
31
32
0
    if (event & APR_POLLIN)
33
0
        rv |= EPOLLIN;
34
0
    if (event & APR_POLLPRI)
35
0
        rv |= EPOLLPRI;
36
0
    if (event & APR_POLLOUT)
37
0
        rv |= EPOLLOUT;
38
0
#ifdef EPOLLEXCLUSIVE
39
0
    if (event & APR_POLLEXCL)
40
0
        rv |= EPOLLEXCLUSIVE;
41
0
#endif
42
    /* APR_POLLNVAL is not handled by epoll.  EPOLLERR and EPOLLHUP are return-only */
43
44
0
    return rv;
45
0
}
46
47
static apr_int16_t get_epoll_revent(unsigned event)
48
0
{
49
0
    apr_int16_t rv = 0;
50
51
0
    if (event & EPOLLIN)
52
0
        rv |= APR_POLLIN;
53
0
    if (event & EPOLLPRI)
54
0
        rv |= APR_POLLPRI;
55
0
    if (event & EPOLLOUT)
56
0
        rv |= APR_POLLOUT;
57
0
    if (event & EPOLLERR)
58
0
        rv |= APR_POLLERR;
59
0
    if (event & EPOLLHUP)
60
0
        rv |= APR_POLLHUP;
61
    /* APR_POLLNVAL is not handled by epoll. */
62
63
0
    return rv;
64
0
}
65
66
struct apr_pollset_private_t
67
{
68
    int epoll_fd;
69
    struct epoll_event *pollset;
70
    apr_pollfd_t *result_set;
71
#if APR_HAS_THREADS
72
    /* A thread mutex to protect operations on the rings */
73
    apr_thread_mutex_t *ring_lock;
74
#endif
75
    /* A ring containing all of the pollfd_t that are active */
76
    APR_RING_HEAD(pfd_query_ring_t, pfd_elem_t) query_ring;
77
    /* A ring of pollfd_t that have been used, and then _remove()'d */
78
    APR_RING_HEAD(pfd_free_ring_t, pfd_elem_t) free_ring;
79
    /* A ring of pollfd_t where rings that have been _remove()`ed but
80
        might still be inside a _poll() */
81
    APR_RING_HEAD(pfd_dead_ring_t, pfd_elem_t) dead_ring;
82
};
83
84
static apr_status_t impl_pollset_cleanup(apr_pollset_t *pollset)
85
0
{
86
0
    close(pollset->p->epoll_fd);
87
0
    return APR_SUCCESS;
88
0
}
89
90
91
static apr_status_t impl_pollset_create(apr_pollset_t *pollset,
92
                                        apr_uint32_t size,
93
                                        apr_pool_t *p,
94
                                        apr_uint32_t flags)
95
0
{
96
0
    apr_status_t rv;
97
0
    int fd;
98
99
0
#ifdef HAVE_EPOLL_CREATE1
100
0
    fd = epoll_create1(EPOLL_CLOEXEC);
101
#else
102
    fd = epoll_create(size);
103
#endif
104
0
    if (fd < 0) {
105
0
        pollset->p = NULL;
106
0
        return apr_get_netos_error();
107
0
    }
108
109
#ifndef HAVE_EPOLL_CREATE1
110
    {
111
        int fd_flags;
112
113
        if ((fd_flags = fcntl(fd, F_GETFD)) == -1) {
114
            rv = errno;
115
            close(fd);
116
            pollset->p = NULL;
117
            return rv;
118
        }
119
120
        fd_flags |= FD_CLOEXEC;
121
        if (fcntl(fd, F_SETFD, fd_flags) == -1) {
122
            rv = errno;
123
            close(fd);
124
            pollset->p = NULL;
125
            return rv;
126
        }
127
    }
128
#endif
129
130
0
    pollset->p = apr_pcalloc(p, sizeof(apr_pollset_private_t));
131
0
#if APR_HAS_THREADS
132
0
    if ((flags & APR_POLLSET_THREADSAFE) &&
133
0
        !(flags & APR_POLLSET_NOCOPY) &&
134
0
        ((rv = apr_thread_mutex_create(&pollset->p->ring_lock,
135
0
                                       APR_THREAD_MUTEX_DEFAULT,
136
0
                                       p)) != APR_SUCCESS)) {
137
0
        close(fd);
138
0
        pollset->p = NULL;
139
0
        return rv;
140
0
    }
141
#else
142
    if (flags & APR_POLLSET_THREADSAFE) {
143
        close(fd);
144
        pollset->p = NULL;
145
        return APR_ENOTIMPL;
146
    }
147
#endif
148
0
    pollset->p->epoll_fd = fd;
149
0
    pollset->p->pollset = apr_palloc(p, size * sizeof(struct epoll_event));
150
0
    pollset->p->result_set = apr_palloc(p, size * sizeof(apr_pollfd_t));
151
152
0
    if (!(flags & APR_POLLSET_NOCOPY)) {
153
0
        APR_RING_INIT(&pollset->p->query_ring, pfd_elem_t, link);
154
0
        APR_RING_INIT(&pollset->p->free_ring, pfd_elem_t, link);
155
0
        APR_RING_INIT(&pollset->p->dead_ring, pfd_elem_t, link);
156
0
    }
157
0
    return APR_SUCCESS;
158
0
}
159
160
static apr_status_t impl_pollset_add(apr_pollset_t *pollset,
161
                                     const apr_pollfd_t *descriptor)
162
0
{
163
0
    struct epoll_event ev = {0};
164
0
    int ret;
165
0
    pfd_elem_t *elem = NULL;
166
0
    apr_status_t rv = APR_SUCCESS;
167
168
0
    ev.events = get_epoll_event(descriptor->reqevents);
169
170
0
    if (pollset->flags & APR_POLLSET_NOCOPY) {
171
0
        ev.data.ptr = (void *)descriptor;
172
0
    }
173
0
    else {
174
0
        pollset_lock_rings();
175
176
0
        if (!APR_RING_EMPTY(&(pollset->p->free_ring), pfd_elem_t, link)) {
177
0
            elem = APR_RING_FIRST(&(pollset->p->free_ring));
178
0
            APR_RING_REMOVE(elem, link);
179
0
        }
180
0
        else {
181
0
            elem = (pfd_elem_t *) apr_palloc(pollset->pool, sizeof(pfd_elem_t));
182
0
            APR_RING_ELEM_INIT(elem, link);
183
0
        }
184
0
        elem->pfd = *descriptor;
185
0
        ev.data.ptr = elem;
186
0
    }
187
0
    if (descriptor->desc_type == APR_POLL_SOCKET) {
188
0
        ret = epoll_ctl(pollset->p->epoll_fd, EPOLL_CTL_ADD,
189
0
                        descriptor->desc.s->socketdes, &ev);
190
0
    }
191
0
    else {
192
0
        ret = epoll_ctl(pollset->p->epoll_fd, EPOLL_CTL_ADD,
193
0
                        descriptor->desc.f->filedes, &ev);
194
0
    }
195
196
0
    if (0 != ret) {
197
0
        rv = apr_get_netos_error();
198
0
    }
199
200
0
    if (!(pollset->flags & APR_POLLSET_NOCOPY)) {
201
0
        if (rv != APR_SUCCESS) {
202
0
            APR_RING_INSERT_TAIL(&(pollset->p->free_ring), elem, pfd_elem_t, link);
203
0
        }
204
0
        else {
205
0
            APR_RING_INSERT_TAIL(&(pollset->p->query_ring), elem, pfd_elem_t, link);
206
0
        }
207
0
        pollset_unlock_rings();
208
0
    }
209
210
0
    return rv;
211
0
}
212
213
static apr_status_t impl_pollset_remove(apr_pollset_t *pollset,
214
                                        const apr_pollfd_t *descriptor)
215
0
{
216
0
    pfd_elem_t *ep;
217
0
    apr_status_t rv = APR_SUCCESS;
218
0
    struct epoll_event ev = {0}; /* ignored, but must be passed with
219
                                  * kernel < 2.6.9
220
                                  */
221
0
    int ret;
222
223
0
    if (descriptor->desc_type == APR_POLL_SOCKET) {
224
0
        ret = epoll_ctl(pollset->p->epoll_fd, EPOLL_CTL_DEL,
225
0
                        descriptor->desc.s->socketdes, &ev);
226
0
    }
227
0
    else {
228
0
        ret = epoll_ctl(pollset->p->epoll_fd, EPOLL_CTL_DEL,
229
0
                        descriptor->desc.f->filedes, &ev);
230
0
    }
231
0
    if (ret < 0) {
232
0
        rv = APR_NOTFOUND;
233
0
    }
234
235
0
    if (!(pollset->flags & APR_POLLSET_NOCOPY)) {
236
0
        pollset_lock_rings();
237
238
0
        for (ep = APR_RING_FIRST(&(pollset->p->query_ring));
239
0
             ep != APR_RING_SENTINEL(&(pollset->p->query_ring),
240
0
                                     pfd_elem_t, link);
241
0
             ep = APR_RING_NEXT(ep, link)) {
242
243
0
            if (descriptor->desc.s == ep->pfd.desc.s) {
244
0
                APR_RING_REMOVE(ep, link);
245
0
                APR_RING_INSERT_TAIL(&(pollset->p->dead_ring),
246
0
                                     ep, pfd_elem_t, link);
247
0
                break;
248
0
            }
249
0
        }
250
251
0
        pollset_unlock_rings();
252
0
    }
253
254
0
    return rv;
255
0
}
256
257
static apr_status_t impl_pollset_poll(apr_pollset_t *pollset,
258
                                           apr_interval_time_t timeout,
259
                                           apr_int32_t *num,
260
                                           const apr_pollfd_t **descriptors)
261
0
{
262
0
    int ret;
263
0
    apr_status_t rv = APR_SUCCESS;
264
265
0
    *num = 0;
266
267
0
    if (timeout > 0) {
268
0
        timeout = (timeout + 999) / 1000;
269
0
    }
270
271
0
    ret = epoll_wait(pollset->p->epoll_fd, pollset->p->pollset, pollset->nalloc,
272
0
                     timeout);
273
0
    if (ret < 0) {
274
0
        rv = apr_get_netos_error();
275
0
    }
276
0
    else if (ret == 0) {
277
0
        rv = APR_TIMEUP;
278
0
    }
279
0
    else {
280
0
        int i, j;
281
0
        const apr_pollfd_t *fdptr;
282
283
0
        for (i = 0, j = 0; i < ret; i++) {
284
0
            if (pollset->flags & APR_POLLSET_NOCOPY) {
285
0
                fdptr = (apr_pollfd_t *)(pollset->p->pollset[i].data.ptr);
286
0
            }
287
0
            else {
288
0
                fdptr = &(((pfd_elem_t *) (pollset->p->pollset[i].data.ptr))->pfd);
289
0
            }
290
            /* Check if the polled descriptor is our
291
             * wakeup pipe. In that case do not put it result set.
292
             */
293
0
            if ((pollset->flags & APR_POLLSET_WAKEABLE) &&
294
0
                fdptr->desc_type == APR_POLL_FILE &&
295
0
                fdptr->desc.f == pollset->wakeup_pipe[0]) {
296
0
                apr_poll_drain_wakeup_pipe(&pollset->wakeup_set, pollset->wakeup_pipe);
297
0
                rv = APR_EINTR;
298
0
            }
299
0
            else {
300
0
                pollset->p->result_set[j] = *fdptr;
301
0
                pollset->p->result_set[j].rtnevents =
302
0
                    get_epoll_revent(pollset->p->pollset[i].events);
303
0
                j++;
304
0
            }
305
0
        }
306
0
        if (((*num) = j)) { /* any event besides wakeup pipe? */
307
0
            rv = APR_SUCCESS;
308
309
0
            if (descriptors) {
310
0
                *descriptors = pollset->p->result_set;
311
0
            }
312
0
        }
313
0
    }
314
315
0
    if (!(pollset->flags & APR_POLLSET_NOCOPY)) {
316
0
        pollset_lock_rings();
317
318
        /* Shift all PFDs in the Dead Ring to the Free Ring */
319
0
        APR_RING_CONCAT(&(pollset->p->free_ring), &(pollset->p->dead_ring), pfd_elem_t, link);
320
321
0
        pollset_unlock_rings();
322
0
    }
323
324
0
    return rv;
325
0
}
326
327
static const apr_pollset_provider_t impl = {
328
    impl_pollset_create,
329
    impl_pollset_add,
330
    impl_pollset_remove,
331
    impl_pollset_poll,
332
    impl_pollset_cleanup,
333
    "epoll"
334
};
335
336
const apr_pollset_provider_t *const apr_pollset_provider_epoll = &impl;
337
338
static apr_status_t impl_pollcb_cleanup(apr_pollcb_t *pollcb)
339
0
{
340
0
    close(pollcb->fd);
341
0
    return APR_SUCCESS;
342
0
}
343
344
static apr_status_t impl_pollcb_create(apr_pollcb_t *pollcb,
345
                                       apr_uint32_t size,
346
                                       apr_pool_t *p,
347
                                       apr_uint32_t flags)
348
0
{
349
0
    int fd;
350
351
0
#ifdef HAVE_EPOLL_CREATE1
352
0
    fd = epoll_create1(EPOLL_CLOEXEC);
353
#else
354
    fd = epoll_create(size);
355
#endif
356
357
0
    if (fd < 0) {
358
0
        return apr_get_netos_error();
359
0
    }
360
361
#ifndef HAVE_EPOLL_CREATE1
362
    {
363
        int fd_flags;
364
        apr_status_t rv;
365
366
        if ((fd_flags = fcntl(fd, F_GETFD)) == -1) {
367
            rv = errno;
368
            close(fd);
369
            pollcb->fd = -1;
370
            return rv;
371
        }
372
373
        fd_flags |= FD_CLOEXEC;
374
        if (fcntl(fd, F_SETFD, fd_flags) == -1) {
375
            rv = errno;
376
            close(fd);
377
            pollcb->fd = -1;
378
            return rv;
379
        }
380
    }
381
#endif
382
383
0
    pollcb->fd = fd;
384
0
    pollcb->pollset.epoll = apr_palloc(p, size * sizeof(struct epoll_event));
385
386
0
    return APR_SUCCESS;
387
0
}
388
389
static apr_status_t impl_pollcb_add(apr_pollcb_t *pollcb,
390
                                    apr_pollfd_t *descriptor)
391
0
{
392
0
    struct epoll_event ev = { 0 };
393
0
    int ret;
394
395
0
    ev.events = get_epoll_event(descriptor->reqevents);
396
0
    ev.data.ptr = (void *) descriptor;
397
398
0
    if (descriptor->desc_type == APR_POLL_SOCKET) {
399
0
        ret = epoll_ctl(pollcb->fd, EPOLL_CTL_ADD,
400
0
                        descriptor->desc.s->socketdes, &ev);
401
0
    }
402
0
    else {
403
0
        ret = epoll_ctl(pollcb->fd, EPOLL_CTL_ADD,
404
0
                        descriptor->desc.f->filedes, &ev);
405
0
    }
406
407
0
    if (ret == -1) {
408
0
        return apr_get_netos_error();
409
0
    }
410
411
0
    return APR_SUCCESS;
412
0
}
413
414
static apr_status_t impl_pollcb_remove(apr_pollcb_t *pollcb,
415
                                       apr_pollfd_t *descriptor)
416
0
{
417
0
    apr_status_t rv = APR_SUCCESS;
418
0
    struct epoll_event ev = {0}; /* ignored, but must be passed with
419
                                  * kernel < 2.6.9
420
                                  */
421
0
    int ret;
422
423
0
    if (descriptor->desc_type == APR_POLL_SOCKET) {
424
0
        ret = epoll_ctl(pollcb->fd, EPOLL_CTL_DEL,
425
0
                        descriptor->desc.s->socketdes, &ev);
426
0
    }
427
0
    else {
428
0
        ret = epoll_ctl(pollcb->fd, EPOLL_CTL_DEL,
429
0
                        descriptor->desc.f->filedes, &ev);
430
0
    }
431
432
0
    if (ret < 0) {
433
0
        rv = APR_NOTFOUND;
434
0
    }
435
436
0
    return rv;
437
0
}
438
439
440
static apr_status_t impl_pollcb_poll(apr_pollcb_t *pollcb,
441
                                     apr_interval_time_t timeout,
442
                                     apr_pollcb_cb_t func,
443
                                     void *baton)
444
0
{
445
0
    int ret, i;
446
0
    apr_status_t rv = APR_SUCCESS;
447
448
0
    if (timeout > 0) {
449
0
        timeout = (timeout + 999) / 1000;
450
0
    }
451
452
0
    ret = epoll_wait(pollcb->fd, pollcb->pollset.epoll, pollcb->nalloc,
453
0
                     timeout);
454
0
    if (ret < 0) {
455
0
        rv = apr_get_netos_error();
456
0
    }
457
0
    else if (ret == 0) {
458
0
        rv = APR_TIMEUP;
459
0
    }
460
0
    else {
461
0
        for (i = 0; i < ret; i++) {
462
0
            apr_pollfd_t *pollfd = (apr_pollfd_t *)(pollcb->pollset.epoll[i].data.ptr);
463
464
0
            if ((pollcb->flags & APR_POLLSET_WAKEABLE) &&
465
0
                pollfd->desc_type == APR_POLL_FILE &&
466
0
                pollfd->desc.f == pollcb->wakeup_pipe[0]) {
467
0
                apr_poll_drain_wakeup_pipe(&pollcb->wakeup_set, pollcb->wakeup_pipe);
468
0
                return APR_EINTR;
469
0
            }
470
471
0
            pollfd->rtnevents = get_epoll_revent(pollcb->pollset.epoll[i].events);
472
473
0
            rv = func(baton, pollfd);
474
0
            if (rv) {
475
0
                return rv;
476
0
            }
477
0
        }
478
0
    }
479
480
0
    return rv;
481
0
}
482
483
static const apr_pollcb_provider_t impl_cb = {
484
    impl_pollcb_create,
485
    impl_pollcb_add,
486
    impl_pollcb_remove,
487
    impl_pollcb_poll,
488
    impl_pollcb_cleanup,
489
    "epoll"
490
};
491
492
const apr_pollcb_provider_t *const apr_pollcb_provider_epoll = &impl_cb;
493
494
#endif /* HAVE_EPOLL */