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_networkio.h"
22 #include "apr_arch_file_io.h"
34 #define HAS_SOCKETS(dt) (dt == APR_POLL_SOCKET) ? 1 : 0
35 #define HAS_PIPES(dt) (dt == APR_POLL_FILE) ? 1 : 0
38 #ifdef HAVE_POLL /* We can just use poll to do our socket polling. */
40 static apr_int16_t get_event(apr_int16_t event)
44 if (event & APR_POLLIN)
46 if (event & APR_POLLPRI)
48 if (event & APR_POLLOUT)
50 if (event & APR_POLLERR)
52 if (event & APR_POLLHUP)
54 if (event & APR_POLLNVAL)
60 static apr_int16_t get_revent(apr_int16_t event)
80 #define SMALL_POLLSET_LIMIT 8
82 APR_DECLARE(apr_status_t) apr_poll(apr_pollfd_t *aprset, apr_int32_t num,
83 apr_int32_t *nsds, apr_interval_time_t timeout)
87 /* XXX: I trust that this is a segv when insufficient stack exists? */
88 struct pollfd pollset[num];
89 #elif defined(HAVE_ALLOCA)
90 struct pollfd *pollset = alloca(sizeof(struct pollfd) * num);
94 struct pollfd tmp_pollset[SMALL_POLLSET_LIMIT];
95 struct pollfd *pollset;
97 if (num <= SMALL_POLLSET_LIMIT) {
98 pollset = tmp_pollset;
101 /* This does require O(n) to copy the descriptors to the internal
104 pollset = malloc(sizeof(struct pollfd) * num);
105 /* The other option is adding an apr_pool_abort() fn to invoke
106 * the pool's out of memory handler
112 for (i = 0; i < num; i++) {
113 if (aprset[i].desc_type == APR_POLL_SOCKET) {
114 pollset[i].fd = aprset[i].desc.s->socketdes;
116 else if (aprset[i].desc_type == APR_POLL_FILE) {
117 pollset[i].fd = aprset[i].desc.f->filedes;
122 pollset[i].events = get_event(aprset[i].reqevents);
127 timeout /= 1000; /* convert microseconds to milliseconds */
130 i = poll(pollset, num_to_poll, timeout);
133 for (i = 0; i < num; i++) {
134 aprset[i].rtnevents = get_revent(pollset[i].revents);
137 #if !defined(HAVE_VLA) && !defined(HAVE_ALLOCA)
138 if (num > SMALL_POLLSET_LIMIT) {
144 return apr_get_netos_error();
153 #else /* Use select to mimic poll */
155 APR_DECLARE(apr_status_t) apr_poll(apr_pollfd_t *aprset, int num, apr_int32_t *nsds,
156 apr_interval_time_t timeout)
158 fd_set readset, writeset, exceptset;
161 struct timeval tv, *tvptr;
163 apr_datatype_e set_type = APR_NO_DESC;
170 tv.tv_sec = (long)apr_time_sec(timeout);
171 tv.tv_usec = (long)apr_time_usec(timeout);
179 for (i = 0; i < num; i++) {
182 aprset[i].rtnevents = 0;
184 if (aprset[i].desc_type == APR_POLL_SOCKET) {
186 if (HAS_PIPES(set_type)) {
190 set_type = APR_POLL_SOCKET;
193 fd = aprset[i].desc.s->socketdes;
195 else if (aprset[i].desc_type == APR_POLL_FILE) {
196 #if !APR_FILES_AS_SOCKETS
200 if (aprset[i].desc.f->is_pipe && !HAS_SOCKETS(set_type)) {
201 set_type = APR_POLL_FILE;
207 fd = aprset[i].desc.f->filedes;
209 #endif /* APR_FILES_AS_SOCKETS */
214 #if !defined(WIN32) && !defined(NETWARE) /* socket sets handled with array of handles */
215 if (fd >= FD_SETSIZE) {
216 /* XXX invent new error code so application has a clue */
220 if (aprset[i].reqevents & APR_POLLIN) {
221 FD_SET(fd, &readset);
223 if (aprset[i].reqevents & APR_POLLOUT) {
224 FD_SET(fd, &writeset);
226 if (aprset[i].reqevents &
227 (APR_POLLPRI | APR_POLLERR | APR_POLLHUP | APR_POLLNVAL)) {
228 FD_SET(fd, &exceptset);
230 if ((int)fd > maxfd) {
236 if (HAS_PIPES(set_type)) {
237 rv = pipe_select(maxfd + 1, &readset, &writeset, &exceptset, tvptr);
242 rv = select(maxfd + 1, &readset, &writeset, &exceptset, tvptr);
253 return apr_get_netos_error();
256 for (i = 0; i < num; i++) {
259 if (aprset[i].desc_type == APR_POLL_SOCKET) {
260 fd = aprset[i].desc.s->socketdes;
262 else if (aprset[i].desc_type == APR_POLL_FILE) {
263 #if !APR_FILES_AS_SOCKETS
266 fd = aprset[i].desc.f->filedes;
272 if (FD_ISSET(fd, &readset)) {
273 aprset[i].rtnevents |= APR_POLLIN;
275 if (FD_ISSET(fd, &writeset)) {
276 aprset[i].rtnevents |= APR_POLLOUT;
278 if (FD_ISSET(fd, &exceptset)) {
279 aprset[i].rtnevents |= APR_POLLERR;
289 struct apr_pollset_t {
293 struct pollfd *pollset;
295 fd_set readset, writeset, exceptset;
298 apr_pollfd_t *query_set;
299 apr_pollfd_t *result_set;
306 APR_DECLARE(apr_status_t) apr_pollset_create(apr_pollset_t **pollset,
311 #if !defined(HAVE_POLL) && defined(FD_SETSIZE)
312 if (size > FD_SETSIZE) {
317 *pollset = apr_palloc(p, sizeof(**pollset));
318 (*pollset)->nelts = 0;
319 (*pollset)->nalloc = size;
321 (*pollset)->pollset = apr_palloc(p, size * sizeof(struct pollfd));
323 FD_ZERO(&((*pollset)->readset));
324 FD_ZERO(&((*pollset)->writeset));
325 FD_ZERO(&((*pollset)->exceptset));
326 (*pollset)->maxfd = 0;
328 (*pollset)->set_type = APR_NO_DESC;
331 (*pollset)->query_set = apr_palloc(p, size * sizeof(apr_pollfd_t));
332 (*pollset)->result_set = apr_palloc(p, size * sizeof(apr_pollfd_t));
333 (*pollset)->pool = p;
337 APR_DECLARE(apr_status_t) apr_pollset_destroy(apr_pollset_t *pollset)
339 /* A no-op function for now. If we later implement /dev/poll
340 * support, we'll need to close the /dev/poll fd here
345 APR_DECLARE(apr_status_t) apr_pollset_add(apr_pollset_t *pollset,
346 const apr_pollfd_t *descriptor)
352 if (pollset->nelts == pollset->nalloc) {
356 pollset->query_set[pollset->nelts] = *descriptor;
359 if (descriptor->desc_type == APR_POLL_SOCKET) {
360 pollset->pollset[pollset->nelts].fd = descriptor->desc.s->socketdes;
363 pollset->pollset[pollset->nelts].fd = descriptor->desc.f->filedes;
366 pollset->pollset[pollset->nelts].events = get_event(descriptor->reqevents);
368 if (descriptor->desc_type == APR_POLL_SOCKET) {
370 /* NetWare can't handle mixed descriptor types in select() */
371 if (HAS_PIPES(pollset->set_type)) {
375 pollset->set_type = APR_POLL_SOCKET;
378 fd = descriptor->desc.s->socketdes;
381 #if !APR_FILES_AS_SOCKETS
385 /* NetWare can't handle mixed descriptor types in select() */
386 if (descriptor->desc.f->is_pipe && !HAS_SOCKETS(pollset->set_type)) {
387 pollset->set_type = APR_POLL_FILE;
388 fd = descriptor->desc.f->filedes;
394 fd = descriptor->desc.f->filedes;
398 #if !defined(WIN32) && !defined(NETWARE) /* socket sets handled with array of handles */
399 if (fd >= FD_SETSIZE) {
400 /* XXX invent new error code so application has a clue */
404 if (descriptor->reqevents & APR_POLLIN) {
405 FD_SET(fd, &(pollset->readset));
407 if (descriptor->reqevents & APR_POLLOUT) {
408 FD_SET(fd, &(pollset->writeset));
410 if (descriptor->reqevents &
411 (APR_POLLPRI | APR_POLLERR | APR_POLLHUP | APR_POLLNVAL)) {
412 FD_SET(fd, &(pollset->exceptset));
414 if ((int)fd > pollset->maxfd) {
415 pollset->maxfd = (int)fd;
422 APR_DECLARE(apr_status_t) apr_pollset_remove(apr_pollset_t *pollset,
423 const apr_pollfd_t *descriptor)
431 for (i = 0; i < pollset->nelts; i++) {
432 if (descriptor->desc.s == pollset->query_set[i].desc.s) {
433 /* Found an instance of the fd: remove this and any other copies */
434 apr_uint32_t dst = i;
435 apr_uint32_t old_nelts = pollset->nelts;
437 for (i++; i < old_nelts; i++) {
438 if (descriptor->desc.s == pollset->query_set[i].desc.s) {
442 pollset->pollset[dst] = pollset->pollset[i];
443 pollset->query_set[dst] = pollset->query_set[i];
452 if (descriptor->desc_type == APR_POLL_SOCKET) {
453 fd = descriptor->desc.s->socketdes;
456 #if !APR_FILES_AS_SOCKETS
459 fd = descriptor->desc.f->filedes;
463 for (i = 0; i < pollset->nelts; i++) {
464 if (descriptor->desc.s == pollset->query_set[i].desc.s) {
465 /* Found an instance of the fd: remove this and any other copies */
466 apr_uint32_t dst = i;
467 apr_uint32_t old_nelts = pollset->nelts;
469 for (i++; i < old_nelts; i++) {
470 if (descriptor->desc.s == pollset->query_set[i].desc.s) {
474 pollset->query_set[dst] = pollset->query_set[i];
478 FD_CLR(fd, &(pollset->readset));
479 FD_CLR(fd, &(pollset->writeset));
480 FD_CLR(fd, &(pollset->exceptset));
481 if (((int)fd == pollset->maxfd) && (pollset->maxfd > 0)) {
493 APR_DECLARE(apr_status_t) apr_pollset_poll(apr_pollset_t *pollset,
494 apr_interval_time_t timeout,
496 const apr_pollfd_t **descriptors)
504 rv = poll(pollset->pollset, pollset->nelts, timeout);
507 return apr_get_netos_error();
513 for (i = 0; i < pollset->nelts; i++) {
514 if (pollset->pollset[i].revents != 0) {
515 pollset->result_set[j] = pollset->query_set[i];
516 pollset->result_set[j].rtnevents =
517 get_revent(pollset->pollset[i].revents);
521 *descriptors = pollset->result_set;
527 APR_DECLARE(apr_status_t) apr_pollset_poll(apr_pollset_t *pollset,
528 apr_interval_time_t timeout,
530 const apr_pollfd_t **descriptors)
534 struct timeval tv, *tvptr;
535 fd_set readset, writeset, exceptset;
541 tv.tv_sec = (long)apr_time_sec(timeout);
542 tv.tv_usec = (long)apr_time_usec(timeout);
546 memcpy(&readset, &(pollset->readset), sizeof(fd_set));
547 memcpy(&writeset, &(pollset->writeset), sizeof(fd_set));
548 memcpy(&exceptset, &(pollset->exceptset), sizeof(fd_set));
551 if (HAS_PIPES(pollset->set_type)) {
552 rv = pipe_select(pollset->maxfd + 1, &readset, &writeset, &exceptset, tvptr);
556 rv = select(pollset->maxfd + 1, &readset, &writeset, &exceptset, tvptr);
558 /* Set initial *num now for expected -1 / 0 failures, or errors below */
561 return apr_get_netos_error();
567 for (i = 0; i < pollset->nelts; i++) {
569 if (pollset->query_set[i].desc_type == APR_POLL_SOCKET) {
570 fd = pollset->query_set[i].desc.s->socketdes;
573 #if !APR_FILES_AS_SOCKETS
576 fd = pollset->query_set[i].desc.f->filedes;
579 if (FD_ISSET(fd, &readset) || FD_ISSET(fd, &writeset) ||
580 FD_ISSET(fd, &exceptset)) {
581 pollset->result_set[j] = pollset->query_set[i];
582 pollset->result_set[j].rtnevents = 0;
583 if (FD_ISSET(fd, &readset)) {
584 pollset->result_set[j].rtnevents |= APR_POLLIN;
586 if (FD_ISSET(fd, &writeset)) {
587 pollset->result_set[j].rtnevents |= APR_POLLOUT;
589 if (FD_ISSET(fd, &exceptset)) {
590 pollset->result_set[j].rtnevents |= APR_POLLERR;
596 /* Reset computed *num to account for multiply-polled fd's which
597 * select() - on some platforms, treats as a single fd result.
598 * The *num returned must match the size of result_set[]
601 *descriptors = pollset->result_set;