Apache HTTPD
kqueue.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#ifdef HAVE_KQUEUE
27
29{
30 apr_int16_t rv = 0;
31
32 if (event == EVFILT_READ)
33 rv |= APR_POLLIN;
34 else if (event == EVFILT_WRITE)
35 rv |= APR_POLLOUT;
36 if (flags & EV_EOF)
37 rv |= APR_POLLHUP;
38 /* APR_POLLPRI, APR_POLLERR, and APR_POLLNVAL are not handled by this
39 * implementation.
40 * TODO: See if EV_ERROR + certain system errors in the returned data field
41 * should map to APR_POLLNVAL.
42 */
43 return rv;
44}
45
47{
48 int kqueue_fd;
49 struct kevent kevent;
51 struct kevent *ke_set;
53#if APR_HAS_THREADS
54 /* A thread mutex to protect operations on the rings */
56#endif
57 /* A ring containing all of the pollfd_t that are active */
59 /* A ring of pollfd_t that have been used, and then _remove'd */
61 /* A ring of pollfd_t where rings that have been _remove'd but
62 might still be inside a _poll */
64};
65
67{
68 close(pollset->p->kqueue_fd);
69 return APR_SUCCESS;
70}
71
76{
77 apr_status_t rv;
79#if APR_HAS_THREADS
81 ((rv = apr_thread_mutex_create(&pollset->p->ring_lock,
83 p)) != APR_SUCCESS)) {
84 pollset->p = NULL;
85 return rv;
86 }
87#else
89 pollset->p = NULL;
90 return APR_ENOTIMPL;
91 }
92#endif
93
94 /* POLLIN and POLLOUT are represented in different returned
95 * events, so we need 2 entries per descriptor in the result set,
96 * both for what is returned by kevent() and what is returned to
97 * the caller of apr_pollset_poll() (since it doesn't spend the
98 * CPU to coalesce separate APR_POLLIN and APR_POLLOUT events
99 * for the same descriptor)
100 */
101 pollset->p->setsize = 2 * size;
102
103 pollset->p->ke_set =
104 (struct kevent *) apr_palloc(p, pollset->p->setsize * sizeof(struct kevent));
105
106 memset(pollset->p->ke_set, 0, pollset->p->setsize * sizeof(struct kevent));
107
108 pollset->p->kqueue_fd = kqueue();
109
110 if (pollset->p->kqueue_fd == -1) {
111 pollset->p = NULL;
112 return apr_get_netos_error();
113 }
114
115 {
116 int flags;
117
118 if ((flags = fcntl(pollset->p->kqueue_fd, F_GETFD)) == -1) {
119 rv = errno;
120 close(pollset->p->kqueue_fd);
121 pollset->p = NULL;
122 return rv;
123 }
124
125 flags |= FD_CLOEXEC;
126 if (fcntl(pollset->p->kqueue_fd, F_SETFD, flags) == -1) {
127 rv = errno;
128 close(pollset->p->kqueue_fd);
129 pollset->p = NULL;
130 return rv;
131 }
132 }
133
134 pollset->p->result_set = apr_palloc(p, pollset->p->setsize * sizeof(apr_pollfd_t));
135
136 APR_RING_INIT(&pollset->p->query_ring, pfd_elem_t, link);
137 APR_RING_INIT(&pollset->p->free_ring, pfd_elem_t, link);
138 APR_RING_INIT(&pollset->p->dead_ring, pfd_elem_t, link);
139
140 return APR_SUCCESS;
141}
142
145{
149
151
152 if (!APR_RING_EMPTY(&(pollset->p->free_ring), pfd_elem_t, link)) {
153 elem = APR_RING_FIRST(&(pollset->p->free_ring));
154 APR_RING_REMOVE(elem, link);
155 }
156 else {
159 }
160 elem->pfd = *descriptor;
161
164 }
165 else {
167 }
168
170 EV_SET(&pollset->p->kevent, fd, EVFILT_READ, EV_ADD, 0, 0, elem);
171
172 if (kevent(pollset->p->kqueue_fd, &pollset->p->kevent, 1, NULL, 0,
173 NULL) == -1) {
174 rv = apr_get_netos_error();
175 }
176 }
177
179 EV_SET(&pollset->p->kevent, fd, EVFILT_WRITE, EV_ADD, 0, 0, elem);
180
181 if (kevent(pollset->p->kqueue_fd, &pollset->p->kevent, 1, NULL, 0,
182 NULL) == -1) {
183 rv = apr_get_netos_error();
184 }
185 }
186
187 if (rv == APR_SUCCESS) {
188 APR_RING_INSERT_TAIL(&(pollset->p->query_ring), elem, pfd_elem_t, link);
189 }
190 else {
191 APR_RING_INSERT_TAIL(&(pollset->p->free_ring), elem, pfd_elem_t, link);
192 }
193
195
196 return rv;
197}
198
201{
202 pfd_elem_t *ep;
203 apr_status_t rv;
205
207
210 }
211 else {
213 }
214
215 rv = APR_NOTFOUND; /* unless at least one of the specified conditions is */
217 EV_SET(&pollset->p->kevent, fd, EVFILT_READ, EV_DELETE, 0, 0, NULL);
218
219 if (kevent(pollset->p->kqueue_fd, &pollset->p->kevent, 1, NULL, 0,
220 NULL) != -1) {
221 rv = APR_SUCCESS;
222 }
223 }
224
226 EV_SET(&pollset->p->kevent, fd, EVFILT_WRITE, EV_DELETE, 0, 0, NULL);
227
228 if (kevent(pollset->p->kqueue_fd, &pollset->p->kevent, 1, NULL, 0,
229 NULL) != -1) {
230 rv = APR_SUCCESS;
231 }
232 }
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 return rv;
250}
251
256{
257 int ret;
258 struct timespec tv, *tvptr;
260
261 *num = 0;
262
263 if (timeout < 0) {
264 tvptr = NULL;
265 }
266 else {
267 tv.tv_sec = (long) apr_time_sec(timeout);
268 tv.tv_nsec = (long) apr_time_usec(timeout) * 1000;
269 tvptr = &tv;
270 }
271
272 ret = kevent(pollset->p->kqueue_fd, NULL, 0, pollset->p->ke_set,
273 pollset->p->setsize, tvptr);
274 if (ret < 0) {
275 rv = apr_get_netos_error();
276 }
277 else if (ret == 0) {
278 rv = APR_TIMEUP;
279 }
280 else {
281 int i, j;
282 const apr_pollfd_t *fd;
283
284 for (i = 0, j = 0; i < ret; i++) {
285 fd = &((pfd_elem_t *)pollset->p->ke_set[i].udata)->pfd;
287 fd->desc_type == APR_POLL_FILE &&
288 fd->desc.f == pollset->wakeup_pipe[0]) {
290 rv = APR_EINTR;
291 }
292 else {
293 pollset->p->result_set[j] = *fd;
295 get_kqueue_revent(pollset->p->ke_set[i].filter,
296 pollset->p->ke_set[i].flags);
297 j++;
298 }
299 }
300 if ((*num = j)) { /* any event besides wakeup pipe? */
301 rv = APR_SUCCESS;
302 if (descriptors) {
304 }
305 }
306 }
307
309
310 /* Shift all PFDs in the Dead Ring to the Free Ring */
311 APR_RING_CONCAT(&(pollset->p->free_ring), &(pollset->p->dead_ring),
312 pfd_elem_t, link);
313
315
316 return rv;
317}
318
319static const apr_pollset_provider_t impl = {
325 "kqueue"
326};
327
329
331{
332 close(pollcb->fd);
333 return APR_SUCCESS;
334}
335
338 apr_pool_t *p,
340{
341 int fd;
342
343 fd = kqueue();
344 if (fd < 0) {
345 return apr_get_netos_error();
346 }
347
348 {
349 int flags;
350 apr_status_t rv;
351
352 if ((flags = fcntl(fd, F_GETFD)) == -1) {
353 rv = errno;
354 close(fd);
355 pollcb->fd = -1;
356 return rv;
357 }
358
359 flags |= FD_CLOEXEC;
360 if (fcntl(fd, F_SETFD, flags) == -1) {
361 rv = errno;
362 close(fd);
363 pollcb->fd = -1;
364 return rv;
365 }
366 }
367
368 pollcb->fd = fd;
369 pollcb->pollset.ke = (struct kevent *) apr_pcalloc(p, 2 * size * sizeof(struct kevent));
370
371 return APR_SUCCESS;
372}
373
376{
378 struct kevent ev;
380
383 }
384 else {
386 }
387
390
391 if (kevent(pollcb->fd, &ev, 1, NULL, 0, NULL) == -1) {
392 rv = apr_get_netos_error();
393 }
394 }
395
398
399 if (kevent(pollcb->fd, &ev, 1, NULL, 0, NULL) == -1) {
400 rv = apr_get_netos_error();
401 }
402 }
403
404 return rv;
405}
406
409{
410 apr_status_t rv;
411 struct kevent ev;
413
416 }
417 else {
419 }
420
421 rv = APR_NOTFOUND; /* unless at least one of the specified conditions is */
423 EV_SET(&ev, fd, EVFILT_READ, EV_DELETE, 0, 0, NULL);
424
425 if (kevent(pollcb->fd, &ev, 1, NULL, 0, NULL) != -1) {
426 rv = APR_SUCCESS;
427 }
428 }
429
432
433 if (kevent(pollcb->fd, &ev, 1, NULL, 0, NULL) != -1) {
434 rv = APR_SUCCESS;
435 }
436 }
437
438 return rv;
439}
440
441
445 void *baton)
446{
447 int ret, i;
448 struct timespec tv, *tvptr;
450
451 if (timeout < 0) {
452 tvptr = NULL;
453 }
454 else {
455 tv.tv_sec = (long) apr_time_sec(timeout);
456 tv.tv_nsec = (long) apr_time_usec(timeout) * 1000;
457 tvptr = &tv;
458 }
459
460 ret = kevent(pollcb->fd, NULL, 0, pollcb->pollset.ke, 2 * pollcb->nalloc,
461 tvptr);
462
463 if (ret < 0) {
464 rv = apr_get_netos_error();
465 }
466 else if (ret == 0) {
467 rv = APR_TIMEUP;
468 }
469 else {
470 for (i = 0; i < ret; i++) {
471 apr_pollfd_t *pollfd = (apr_pollfd_t *)(pollcb->pollset.ke[i].udata);
472
474 pollfd->desc_type == APR_POLL_FILE &&
475 pollfd->desc.f == pollcb->wakeup_pipe[0]) {
477 return APR_EINTR;
478 }
479
480 pollfd->rtnevents = get_kqueue_revent(pollcb->pollset.ke[i].filter,
481 pollcb->pollset.ke[i].flags);
482
483 rv = func(baton, pollfd);
484
485 if (rv) {
486 return rv;
487 }
488 }
489 }
490
491 return rv;
492}
493
494static const apr_pollcb_provider_t impl_cb = {
500 "kqueue"
501};
502
504
505#endif /* HAVE_KQUEUE */
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_pcalloc(p, size)
Definition apr_pools.h:465
int apr_os_sock_t
#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_time_sec(time)
Definition apr_time.h:63
#define apr_time_usec(time)
Definition apr_time.h:66
#define APR_POLLSET_WAKEABLE
Definition apr_poll.h:68
#define APR_POLLSET_THREADSAFE
Definition apr_poll.h:62
#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_uint32_t flags
Definition select.c:191
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