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_arch_networkio.h"
20
apr_poll(apr_pollfd_t * aprset,apr_int32_t num,apr_int32_t * nsds,apr_interval_time_t timeout)21 APR_DECLARE(apr_status_t) apr_poll(apr_pollfd_t *aprset, apr_int32_t num,
22 apr_int32_t *nsds, apr_interval_time_t timeout)
23 {
24 int *pollset;
25 int i;
26 int num_read = 0, num_write = 0, num_except = 0, num_total;
27 int pos_read, pos_write, pos_except;
28
29 for (i = 0; i < num; i++) {
30 if (aprset[i].desc_type == APR_POLL_SOCKET) {
31 num_read += (aprset[i].reqevents & APR_POLLIN) != 0;
32 num_write += (aprset[i].reqevents & APR_POLLOUT) != 0;
33 num_except += (aprset[i].reqevents & APR_POLLPRI) != 0;
34 }
35 }
36
37 num_total = num_read + num_write + num_except;
38 pollset = alloca(sizeof(int) * num_total);
39 memset(pollset, 0, sizeof(int) * num_total);
40
41 pos_read = 0;
42 pos_write = num_read;
43 pos_except = pos_write + num_write;
44
45 for (i = 0; i < num; i++) {
46 if (aprset[i].desc_type == APR_POLL_SOCKET) {
47 if (aprset[i].reqevents & APR_POLLIN) {
48 pollset[pos_read++] = aprset[i].desc.s->socketdes;
49 }
50
51 if (aprset[i].reqevents & APR_POLLOUT) {
52 pollset[pos_write++] = aprset[i].desc.s->socketdes;
53 }
54
55 if (aprset[i].reqevents & APR_POLLPRI) {
56 pollset[pos_except++] = aprset[i].desc.s->socketdes;
57 }
58
59 aprset[i].rtnevents = 0;
60 }
61 }
62
63 if (timeout > 0) {
64 timeout /= 1000; /* convert microseconds to milliseconds */
65 }
66
67 i = select(pollset, num_read, num_write, num_except, timeout);
68 (*nsds) = i;
69
70 if ((*nsds) < 0) {
71 return APR_FROM_OS_ERROR(sock_errno());
72 }
73
74 if ((*nsds) == 0) {
75 return APR_TIMEUP;
76 }
77
78 pos_read = 0;
79 pos_write = num_read;
80 pos_except = pos_write + num_write;
81
82 for (i = 0; i < num; i++) {
83 if (aprset[i].desc_type == APR_POLL_SOCKET) {
84 if (aprset[i].reqevents & APR_POLLIN) {
85 if (pollset[pos_read++] > 0) {
86 aprset[i].rtnevents |= APR_POLLIN;
87 }
88 }
89
90 if (aprset[i].reqevents & APR_POLLOUT) {
91 if (pollset[pos_write++] > 0) {
92 aprset[i].rtnevents |= APR_POLLOUT;
93 }
94 }
95
96 if (aprset[i].reqevents & APR_POLLPRI) {
97 if (pollset[pos_except++] > 0) {
98 aprset[i].rtnevents |= APR_POLLPRI;
99 }
100 }
101 }
102 }
103
104 return APR_SUCCESS;
105 }
106