Apache HTTPD
poll.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"
23#include "apr_arch_misc.h"
25
26#if defined(HAVE_POLL)
27
28#ifdef HAVE_ALLOCA_H
29#include <alloca.h>
30#endif
31
33{
34 apr_int16_t rv = 0;
35
36 if (event & APR_POLLIN)
37 rv |= POLLIN;
38 if (event & APR_POLLPRI)
39 rv |= POLLPRI;
40 if (event & APR_POLLOUT)
41 rv |= POLLOUT;
42 /* POLLERR, POLLHUP, and POLLNVAL aren't valid as requested events */
43
44 return rv;
45}
46
48{
49 apr_int16_t rv = 0;
50
51 if (event & POLLIN)
52 rv |= APR_POLLIN;
53 if (event & POLLPRI)
54 rv |= APR_POLLPRI;
55 if (event & POLLOUT)
56 rv |= APR_POLLOUT;
57 if (event & POLLERR)
58 rv |= APR_POLLERR;
59 if (event & POLLHUP)
60 rv |= APR_POLLHUP;
61 if (event & POLLNVAL)
62 rv |= APR_POLLNVAL;
63
64 return rv;
65}
66
67#ifdef POLL_USES_POLL
68
69#define SMALL_POLLSET_LIMIT 8
70
74{
75 int i, num_to_poll;
76#ifdef HAVE_VLA
77 /* XXX: I trust that this is a segv when insufficient stack exists? */
78 struct pollfd pollset[num + 1]; /* +1 since allocating 0 is undefined behaviour */
79#elif defined(HAVE_ALLOCA)
80 struct pollfd *pollset = alloca(sizeof(struct pollfd) * num);
81 if (!pollset)
82 return APR_ENOMEM;
83#else
85 struct pollfd *pollset;
86
87 if (num <= SMALL_POLLSET_LIMIT) {
89 }
90 else {
91 /* This does require O(n) to copy the descriptors to the internal
92 * mapping.
93 */
94 pollset = malloc(sizeof(struct pollfd) * num);
95 /* The other option is adding an apr_pool_abort() fn to invoke
96 * the pool's out of memory handler
97 */
98 if (!pollset)
99 return APR_ENOMEM;
100 }
101#endif
102 for (i = 0; i < num; i++) {
103 if (aprset[i].desc_type == APR_POLL_SOCKET) {
104 pollset[i].fd = aprset[i].desc.s->socketdes;
105 }
106 else if (aprset[i].desc_type == APR_POLL_FILE) {
107 pollset[i].fd = aprset[i].desc.f->filedes;
108 }
109 else {
110 break;
111 }
112 pollset[i].events = get_event(aprset[i].reqevents);
113 }
114 num_to_poll = i;
115
116 if (timeout > 0) {
117 /* convert microseconds to milliseconds (round up) */
118 timeout = (timeout + 999) / 1000;
119 }
120
121 i = poll(pollset, num_to_poll, timeout);
122 (*nsds) = i;
123
124 if (i > 0) { /* poll() sets revents only if an event was signalled;
125 * we don't promise to set rtnevents unless an event
126 * was signalled
127 */
128 for (i = 0; i < num; i++) {
129 aprset[i].rtnevents = get_revent(pollset[i].revents);
130 }
131 }
132
133#if !defined(HAVE_VLA) && !defined(HAVE_ALLOCA)
134 if (num > SMALL_POLLSET_LIMIT) {
135 free(pollset);
136 }
137#endif
138
139 if ((*nsds) < 0) {
140 return apr_get_netos_error();
141 }
142 if ((*nsds) == 0) {
143 return APR_TIMEUP;
144 }
145 return APR_SUCCESS;
146}
147
148
149#endif /* POLL_USES_POLL */
150
152{
153 struct pollfd *pollset;
156};
157
160 apr_pool_t *p,
162{
164 return APR_ENOTIMPL;
165 }
166#ifdef WIN32
168 return APR_ENOTIMPL;
169 }
170#endif
172 pollset->p->pollset = apr_palloc(p, size * sizeof(struct pollfd));
175
176 return APR_SUCCESS;
177}
178
181{
182 if (pollset->nelts == pollset->nalloc) {
183 return APR_ENOMEM;
184 }
185
187
189 pollset->p->pollset[pollset->nelts].fd = descriptor->desc.s->socketdes;
190 }
191 else {
192#if APR_FILES_AS_SOCKETS
193 pollset->p->pollset[pollset->nelts].fd = descriptor->desc.f->filedes;
194#else
197 pollset->p->pollset[pollset->nelts].fd = (SOCKET)descriptor->desc.f->filedes;
198 else
199 return APR_EBADF;
200#endif
201 }
202 pollset->p->pollset[pollset->nelts].events =
204 pollset->nelts++;
205
206 return APR_SUCCESS;
207}
208
211{
213
214 for (i = 0; i < pollset->nelts; i++) {
215 if (descriptor->desc.s == pollset->p->query_set[i].desc.s) {
216 /* Found an instance of the fd: remove this and any other copies */
219 pollset->nelts--;
220 for (i++; i < old_nelts; i++) {
221 if (descriptor->desc.s == pollset->p->query_set[i].desc.s) {
222 pollset->nelts--;
223 }
224 else {
225 pollset->p->pollset[dst] = pollset->p->pollset[i];
227 dst++;
228 }
229 }
230 return APR_SUCCESS;
231 }
232 }
233
234 return APR_NOTFOUND;
235}
236
241{
242 int ret;
244
245 *num = 0;
246
247#ifdef WIN32
248 /* WSAPoll() requires at least one socket. */
249 if (pollset->nelts == 0) {
250 if (timeout > 0) {
252 return APR_TIMEUP;
253 }
254 return APR_SUCCESS;
255 }
256#endif
257
258 if (timeout > 0) {
259 timeout = (timeout + 999) / 1000;
260 }
261
262#ifdef WIN32
263 ret = WSAPoll(pollset->p->pollset, pollset->nelts, (int)timeout);
264#else
265 ret = poll(pollset->p->pollset, pollset->nelts, timeout);
266#endif
267 if (ret < 0) {
268 return apr_get_netos_error();
269 }
270 else if (ret == 0) {
271 return APR_TIMEUP;
272 }
273 else {
274 apr_uint32_t i, j;
275
276 for (i = 0, j = 0; i < pollset->nelts; i++) {
277 if (pollset->p->pollset[i].revents != 0) {
278 /* Check if the polled descriptor is our
279 * wakeup pipe. In that case do not put it result set.
280 */
285 rv = APR_EINTR;
286 }
287 else {
290 get_revent(pollset->p->pollset[i].revents);
291 j++;
292 }
293 }
294 }
295 if ((*num = j)) { /* any event besides wakeup pipe? */
296 rv = APR_SUCCESS;
297 }
298 }
299 if (descriptors && (*num))
301 return rv;
302}
303
304static const apr_pollset_provider_t impl = {
309 NULL,
310 "poll"
311};
312
314
315/* Poll method pollcb.
316 * This is probably usable only for WIN32 having WSAPoll
317 */
320 apr_pool_t *p,
322{
323#if APR_HAS_THREADS
324 return APR_ENOTIMPL;
325#else
326 pollcb->fd = -1;
327#ifdef WIN32
329 return APR_ENOTIMPL;
330 }
331#endif
332
333 pollcb->pollset.ps = apr_palloc(p, size * sizeof(struct pollfd));
334 pollcb->copyset = apr_palloc(p, size * sizeof(apr_pollfd_t *));
335
336 return APR_SUCCESS;
337#endif
338}
339
342{
343 if (pollcb->nelts == pollcb->nalloc) {
344 return APR_ENOMEM;
345 }
346
349 }
350 else {
351#if APR_FILES_AS_SOCKETS
353#else
354 return APR_EBADF;
355#endif
356 }
357
358 pollcb->pollset.ps[pollcb->nelts].events =
361 pollcb->nelts++;
362
363 return APR_SUCCESS;
364}
365
368{
370
371 for (i = 0; i < pollcb->nelts; i++) {
372 if (descriptor->desc.s == pollcb->copyset[i]->desc.s) {
373 /* Found an instance of the fd: remove this and any other copies */
376 pollcb->nelts--;
377 for (i++; i < old_nelts; i++) {
378 if (descriptor->desc.s == pollcb->copyset[i]->desc.s) {
379 pollcb->nelts--;
380 }
381 else {
382 pollcb->pollset.ps[dst] = pollcb->pollset.ps[i];
384 dst++;
385 }
386 }
387 return APR_SUCCESS;
388 }
389 }
390
391 return APR_NOTFOUND;
392}
393
397 void *baton)
398{
399 int ret;
402
403#ifdef WIN32
404 /* WSAPoll() requires at least one socket. */
405 if (pollcb->nelts == 0) {
406 if (timeout > 0) {
408 return APR_TIMEUP;
409 }
410 return APR_SUCCESS;
411 }
412#endif
413
414 if (timeout > 0) {
415 timeout = (timeout + 999) / 1000;
416 }
417
418#ifdef WIN32
420#else
421 ret = poll(pollcb->pollset.ps, pollcb->nelts, timeout);
422#endif
423 if (ret < 0) {
424 return apr_get_netos_error();
425 }
426 else if (ret == 0) {
427 return APR_TIMEUP;
428 }
429 else {
430 for (i = 0; i < pollcb->nelts; i++) {
431 if (pollcb->pollset.ps[i].revents != 0) {
433
435 pollfd->desc_type == APR_POLL_FILE &&
436 pollfd->desc.f == pollcb->wakeup_pipe[0]) {
438 return APR_EINTR;
439 }
440
441 pollfd->rtnevents = get_revent(pollcb->pollset.ps[i].revents);
442 rv = func(baton, pollfd);
443 if (rv) {
444 return rv;
445 }
446 }
447 }
448 }
449 return rv;
450}
451
452static const apr_pollcb_provider_t impl_cb = {
457 NULL,
458 "poll"
459};
460
462
463#endif /* HAVE_POLL */
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_EBADF
Definition apr_errno.h:704
#define APR_ENOMEM
Definition apr_errno.h:683
#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
const char apr_ssize_t int flags
Definition apr_encode.h:168
const void apr_status_t(*) apr_status_t(* APR_DECLARE)(void) apr_pool_pre_cleanup_register(apr_pool_t *p
Definition apr_pools.h:646
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_int32_t apr_int32_t * nsds
Definition apr_poll.h:301
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
apr_int64_t apr_interval_time_t
Definition apr_time.h:55
#define APR_POLLSET_WAKEABLE
Definition apr_poll.h:68
#define APR_POLLSET_THREADSAFE
Definition apr_poll.h:62
#define APR_POLLNVAL
Definition apr_poll.h:54
#define APR_POLLPRI
Definition apr_poll.h:50
#define APR_POLLERR
Definition apr_poll.h:52
#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_pollfd_t ** copyset
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_pollfd_t * query_set
Definition select.c:189
apr_file_t * wakeup_pipe[2]
apr_pollset_private_t * p
SHORT revents
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
#define POLLHUP
#define POLLIN
#define POLLOUT
#define POLLERR
#define POLLPRI
#define POLLNVAL
IN ULONG IN INT timeout
#define WSAPoll
#define APR_HAVE_LATE_DLL_FUNC(fn)