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