/src/httpd/srclib/apr/poll/unix/select.c
Line | Count | Source |
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 | | #ifdef WIN32 |
18 | | /* POSIX defines 1024 for the FD_SETSIZE */ |
19 | | #define FD_SETSIZE 1024 |
20 | | #endif |
21 | | |
22 | | #include "apr.h" |
23 | | #include "apr_poll.h" |
24 | | #include "apr_time.h" |
25 | | #include "apr_portable.h" |
26 | | #include "apr_arch_file_io.h" |
27 | | #include "apr_arch_networkio.h" |
28 | | #include "apr_arch_poll_private.h" |
29 | | |
30 | | #ifdef POLL_USES_SELECT |
31 | | |
32 | | APR_DECLARE(apr_status_t) apr_poll(apr_pollfd_t *aprset, int num, |
33 | | apr_int32_t *nsds, |
34 | | apr_interval_time_t timeout) |
35 | | { |
36 | | fd_set readset, writeset, exceptset; |
37 | | int rv, i; |
38 | | int maxfd = -1; |
39 | | struct timeval tv, *tvptr; |
40 | | |
41 | | #ifdef WIN32 |
42 | | /* On Win32, select() must be presented with at least one socket to |
43 | | * poll on, or select() will return WSAEINVAL. So, we'll just |
44 | | * short-circuit and bail now. |
45 | | */ |
46 | | if (num == 0) { |
47 | | (*nsds) = 0; |
48 | | if (timeout > 0) { |
49 | | apr_sleep(timeout); |
50 | | return APR_TIMEUP; |
51 | | } |
52 | | return APR_SUCCESS; |
53 | | } |
54 | | #endif |
55 | | |
56 | | if (timeout < 0) { |
57 | | tvptr = NULL; |
58 | | } |
59 | | else { |
60 | | tv.tv_sec = (long) apr_time_sec(timeout); |
61 | | tv.tv_usec = (long) apr_time_usec(timeout); |
62 | | tvptr = &tv; |
63 | | } |
64 | | |
65 | | FD_ZERO(&readset); |
66 | | FD_ZERO(&writeset); |
67 | | FD_ZERO(&exceptset); |
68 | | |
69 | | for (i = 0; i < num; i++) { |
70 | | apr_os_sock_t fd; |
71 | | |
72 | | aprset[i].rtnevents = 0; |
73 | | |
74 | | if (aprset[i].desc_type == APR_POLL_SOCKET) { |
75 | | fd = aprset[i].desc.s->socketdes; |
76 | | } |
77 | | else if (aprset[i].desc_type == APR_POLL_FILE) { |
78 | | #if !APR_FILES_AS_SOCKETS |
79 | | return APR_EBADF; |
80 | | #else |
81 | | |
82 | | fd = aprset[i].desc.f->filedes; |
83 | | |
84 | | #endif /* APR_FILES_AS_SOCKETS */ |
85 | | } |
86 | | else { |
87 | | break; |
88 | | } |
89 | | #if !defined(WIN32) /* socket sets handled with array of handles */ |
90 | | if (fd >= FD_SETSIZE) { |
91 | | /* XXX invent new error code so application has a clue */ |
92 | | return APR_EBADF; |
93 | | } |
94 | | #endif |
95 | | if (aprset[i].reqevents & APR_POLLIN) { |
96 | | FD_SET(fd, &readset); |
97 | | } |
98 | | if (aprset[i].reqevents & APR_POLLOUT) { |
99 | | FD_SET(fd, &writeset); |
100 | | } |
101 | | if (aprset[i].reqevents & |
102 | | (APR_POLLPRI | APR_POLLERR | APR_POLLHUP | APR_POLLNVAL)) { |
103 | | FD_SET(fd, &exceptset); |
104 | | } |
105 | | if ((int) fd > maxfd) { |
106 | | maxfd = (int) fd; |
107 | | } |
108 | | } |
109 | | |
110 | | rv = select(maxfd + 1, &readset, &writeset, &exceptset, tvptr); |
111 | | |
112 | | (*nsds) = rv; |
113 | | if ((*nsds) == 0) { |
114 | | return APR_TIMEUP; |
115 | | } |
116 | | if ((*nsds) < 0) { |
117 | | return apr_get_netos_error(); |
118 | | } |
119 | | |
120 | | (*nsds) = 0; |
121 | | for (i = 0; i < num; i++) { |
122 | | apr_os_sock_t fd; |
123 | | |
124 | | if (aprset[i].desc_type == APR_POLL_SOCKET) { |
125 | | fd = aprset[i].desc.s->socketdes; |
126 | | } |
127 | | else if (aprset[i].desc_type == APR_POLL_FILE) { |
128 | | #if !APR_FILES_AS_SOCKETS |
129 | | return APR_EBADF; |
130 | | #else |
131 | | fd = aprset[i].desc.f->filedes; |
132 | | #endif |
133 | | } |
134 | | else { |
135 | | break; |
136 | | } |
137 | | if (FD_ISSET(fd, &readset)) { |
138 | | aprset[i].rtnevents |= APR_POLLIN; |
139 | | } |
140 | | if (FD_ISSET(fd, &writeset)) { |
141 | | aprset[i].rtnevents |= APR_POLLOUT; |
142 | | } |
143 | | if (FD_ISSET(fd, &exceptset)) { |
144 | | aprset[i].rtnevents |= APR_POLLERR; |
145 | | } |
146 | | if (aprset[i].rtnevents) { |
147 | | (*nsds)++; |
148 | | } |
149 | | } |
150 | | |
151 | | return APR_SUCCESS; |
152 | | } |
153 | | |
154 | | #endif /* POLL_USES_SELECT */ |
155 | | |
156 | | struct apr_pollset_private_t |
157 | | { |
158 | | fd_set readset, writeset, exceptset; |
159 | | int maxfd; |
160 | | apr_pollfd_t *query_set; |
161 | | apr_pollfd_t *result_set; |
162 | | /* APR_POLLONESHOT descriptors returned already (out of the sets) */ |
163 | | char *disarmed; |
164 | | #if APR_HAS_THREADS |
165 | | /* APR_POLLSET_THREADSAFE: the sets and query_set are modified under the |
166 | | * lock, and apr_pollset_poll() selects copies of them (taken under the |
167 | | * lock) so that descriptors can be added or removed meanwhile; the |
168 | | * ready ones are then found again by their descriptor (if still there). |
169 | | */ |
170 | | apr_thread_mutex_t *lock; |
171 | | apr_pollfd_t *poll_query; |
172 | | /* number of threads in select(), to wake them up on add */ |
173 | | apr_uint32_t polling; |
174 | | #endif |
175 | | }; |
176 | | |
177 | | #if APR_HAS_THREADS |
178 | 0 | #define pollset_lock(ps) do { \ |
179 | 0 | if ((ps)->p->lock) { \ |
180 | 0 | apr_thread_mutex_lock((ps)->p->lock); \ |
181 | 0 | } \ |
182 | 0 | } while (0) |
183 | 0 | #define pollset_unlock(ps) do { \ |
184 | 0 | if ((ps)->p->lock) { \ |
185 | 0 | apr_thread_mutex_unlock((ps)->p->lock); \ |
186 | 0 | } \ |
187 | 0 | } while (0) |
188 | | #else |
189 | | #define pollset_lock(ps) do { } while (0) |
190 | | #define pollset_unlock(ps) do { } while (0) |
191 | | #endif |
192 | | |
193 | | static apr_status_t get_desc_fd(const apr_pollfd_t *desc, apr_os_sock_t *fd) |
194 | 0 | { |
195 | 0 | if (desc->desc_type == APR_POLL_SOCKET) { |
196 | 0 | *fd = desc->desc.s->socketdes; |
197 | 0 | } |
198 | 0 | else { |
199 | | #if !APR_FILES_AS_SOCKETS |
200 | | return APR_EBADF; |
201 | | #else |
202 | 0 | *fd = desc->desc.f->filedes; |
203 | 0 | #endif |
204 | 0 | } |
205 | 0 | return APR_SUCCESS; |
206 | 0 | } |
207 | | |
208 | | static apr_status_t impl_pollset_create(apr_pollset_t *pollset, |
209 | | apr_uint32_t size, |
210 | | apr_pool_t *p, |
211 | | apr_uint32_t flags) |
212 | 0 | { |
213 | | #if !APR_HAS_THREADS |
214 | | if (flags & APR_POLLSET_THREADSAFE) { |
215 | | pollset->p = NULL; |
216 | | return APR_ENOTIMPL; |
217 | | } |
218 | | #endif |
219 | 0 | #ifdef FD_SETSIZE |
220 | 0 | if (size > FD_SETSIZE) { |
221 | 0 | pollset->p = NULL; |
222 | 0 | return APR_EINVAL; |
223 | 0 | } |
224 | 0 | #endif |
225 | 0 | pollset->p = apr_pcalloc(p, sizeof(apr_pollset_private_t)); |
226 | 0 | FD_ZERO(&(pollset->p->readset)); |
227 | 0 | FD_ZERO(&(pollset->p->writeset)); |
228 | 0 | FD_ZERO(&(pollset->p->exceptset)); |
229 | 0 | pollset->p->maxfd = 0; |
230 | 0 | pollset->p->query_set = apr_palloc(p, size * sizeof(apr_pollfd_t)); |
231 | 0 | pollset->p->result_set = apr_palloc(p, size * sizeof(apr_pollfd_t)); |
232 | 0 | pollset->p->disarmed = apr_pcalloc(p, size); |
233 | 0 | #if APR_HAS_THREADS |
234 | 0 | if (flags & APR_POLLSET_THREADSAFE) { |
235 | 0 | apr_status_t rv = apr_thread_mutex_create(&pollset->p->lock, |
236 | 0 | APR_THREAD_MUTEX_DEFAULT, |
237 | 0 | p); |
238 | 0 | if (rv != APR_SUCCESS) { |
239 | 0 | pollset->p = NULL; |
240 | 0 | return rv; |
241 | 0 | } |
242 | 0 | pollset->p->poll_query = apr_palloc(p, size * sizeof(apr_pollfd_t)); |
243 | 0 | } |
244 | 0 | #endif |
245 | | |
246 | 0 | return APR_SUCCESS; |
247 | 0 | } |
248 | | |
249 | | /* The index of desc in the pollset (tried at hint first), or -1 */ |
250 | | static apr_int32_t find_desc(apr_pollset_t *pollset, |
251 | | const apr_pollfd_t *desc, apr_uint32_t hint) |
252 | 0 | { |
253 | 0 | apr_uint32_t i; |
254 | |
|
255 | 0 | if (hint < pollset->nelts |
256 | 0 | && pollset->p->query_set[hint].desc.s == desc->desc.s) { |
257 | 0 | return hint; |
258 | 0 | } |
259 | 0 | for (i = 0; i < pollset->nelts; i++) { |
260 | 0 | if (pollset->p->query_set[i].desc.s == desc->desc.s) { |
261 | 0 | return i; |
262 | 0 | } |
263 | 0 | } |
264 | 0 | return -1; |
265 | 0 | } |
266 | | |
267 | | static apr_status_t impl_pollset_add(apr_pollset_t *pollset, |
268 | | const apr_pollfd_t *descriptor) |
269 | 0 | { |
270 | 0 | apr_os_sock_t fd; |
271 | 0 | apr_status_t rv; |
272 | 0 | int wakeup = 0; |
273 | |
|
274 | 0 | if ((rv = get_desc_fd(descriptor, &fd)) != APR_SUCCESS) { |
275 | 0 | return rv; |
276 | 0 | } |
277 | 0 | #if !defined(WIN32) /* socket sets handled with array of handles */ |
278 | 0 | if (fd >= FD_SETSIZE) { |
279 | | /* XXX invent new error code so application has a clue */ |
280 | 0 | return APR_EBADF; |
281 | 0 | } |
282 | 0 | #endif |
283 | | |
284 | 0 | pollset_lock(pollset); |
285 | | |
286 | | /* An APR_POLLONESHOT descriptor stays in the pollset once returned |
287 | | * (disarmed), the next add re-arms it. |
288 | | */ |
289 | 0 | if (descriptor->reqevents & APR_POLLONESHOT) { |
290 | 0 | apr_int32_t k = find_desc(pollset, descriptor, 0); |
291 | 0 | if (k >= 0 && (pollset->p->query_set[k].reqevents & APR_POLLONESHOT)) { |
292 | 0 | pollset->p->query_set[k] = *descriptor; |
293 | 0 | pollset->p->disarmed[k] = 0; |
294 | 0 | goto set_fd; |
295 | 0 | } |
296 | 0 | } |
297 | | |
298 | 0 | if (pollset->nelts == pollset->nalloc) { |
299 | 0 | pollset_unlock(pollset); |
300 | 0 | return APR_ENOMEM; |
301 | 0 | } |
302 | | |
303 | 0 | pollset->p->query_set[pollset->nelts] = *descriptor; |
304 | 0 | pollset->p->disarmed[pollset->nelts] = 0; |
305 | 0 | pollset->nelts++; |
306 | |
|
307 | 0 | set_fd: |
308 | | /* The events of a descriptor re-armed (still armed or not) are replaced */ |
309 | 0 | FD_CLR(fd, &(pollset->p->readset)); |
310 | 0 | FD_CLR(fd, &(pollset->p->writeset)); |
311 | 0 | FD_CLR(fd, &(pollset->p->exceptset)); |
312 | 0 | if (descriptor->reqevents & APR_POLLIN) { |
313 | 0 | FD_SET(fd, &(pollset->p->readset)); |
314 | 0 | } |
315 | 0 | if (descriptor->reqevents & APR_POLLOUT) { |
316 | 0 | FD_SET(fd, &(pollset->p->writeset)); |
317 | 0 | } |
318 | 0 | if (descriptor->reqevents & |
319 | 0 | (APR_POLLPRI | APR_POLLERR | APR_POLLHUP | APR_POLLNVAL)) { |
320 | 0 | FD_SET(fd, &(pollset->p->exceptset)); |
321 | 0 | } |
322 | 0 | if ((int) fd > pollset->p->maxfd) { |
323 | 0 | pollset->p->maxfd = (int) fd; |
324 | 0 | } |
325 | |
|
326 | 0 | #if APR_HAS_THREADS |
327 | | /* A thread selecting copies of the sets wouldn't see the new descriptor: |
328 | | * interrupt it if possible (APR_EINTR, see apr_pollset_add()), else it's |
329 | | * polled from the next call. |
330 | | */ |
331 | 0 | wakeup = (pollset->p->polling && (pollset->flags & APR_POLLSET_WAKEABLE) |
332 | 0 | && !pollset_is_wakeup(pollset, descriptor)); |
333 | 0 | #endif |
334 | |
|
335 | 0 | pollset_unlock(pollset); |
336 | |
|
337 | 0 | if (wakeup) { |
338 | 0 | apr_pollset_wakeup(pollset); |
339 | 0 | } |
340 | 0 | return APR_SUCCESS; |
341 | 0 | } |
342 | | |
343 | | static apr_status_t impl_pollset_remove(apr_pollset_t * pollset, |
344 | | const apr_pollfd_t * descriptor) |
345 | 0 | { |
346 | 0 | apr_uint32_t i; |
347 | 0 | apr_os_sock_t fd; |
348 | 0 | apr_status_t rv; |
349 | |
|
350 | 0 | if ((rv = get_desc_fd(descriptor, &fd)) != APR_SUCCESS) { |
351 | 0 | return rv; |
352 | 0 | } |
353 | | |
354 | 0 | pollset_lock(pollset); |
355 | |
|
356 | 0 | for (i = 0; i < pollset->nelts; i++) { |
357 | 0 | if (descriptor->desc.s == pollset->p->query_set[i].desc.s) { |
358 | | /* Found an instance of the fd: remove this and any other copies */ |
359 | 0 | apr_uint32_t dst = i; |
360 | 0 | apr_uint32_t old_nelts = pollset->nelts; |
361 | 0 | pollset->nelts--; |
362 | 0 | for (i++; i < old_nelts; i++) { |
363 | 0 | if (descriptor->desc.s == pollset->p->query_set[i].desc.s) { |
364 | 0 | pollset->nelts--; |
365 | 0 | } |
366 | 0 | else { |
367 | 0 | pollset->p->query_set[dst] = pollset->p->query_set[i]; |
368 | 0 | pollset->p->disarmed[dst] = pollset->p->disarmed[i]; |
369 | 0 | dst++; |
370 | 0 | } |
371 | 0 | } |
372 | 0 | FD_CLR(fd, &(pollset->p->readset)); |
373 | 0 | FD_CLR(fd, &(pollset->p->writeset)); |
374 | 0 | FD_CLR(fd, &(pollset->p->exceptset)); |
375 | 0 | if (((int) fd == pollset->p->maxfd) && (pollset->p->maxfd > 0)) { |
376 | 0 | pollset->p->maxfd--; |
377 | 0 | } |
378 | 0 | pollset_unlock(pollset); |
379 | 0 | return APR_SUCCESS; |
380 | 0 | } |
381 | 0 | } |
382 | | |
383 | 0 | pollset_unlock(pollset); |
384 | 0 | return APR_NOTFOUND; |
385 | 0 | } |
386 | | |
387 | | static int do_select(int maxfd, fd_set *readset, fd_set *writeset, |
388 | | fd_set *exceptset, apr_interval_time_t timeout) |
389 | 0 | { |
390 | 0 | struct timeval tv, *tvptr; |
391 | |
|
392 | | #ifdef WIN32 |
393 | | /* On Win32, select() must be presented with at least one socket to |
394 | | * poll on, or select() will return WSAEINVAL: none when the pollset is |
395 | | * empty or its APR_POLLONESHOT descriptors are all disarmed. |
396 | | */ |
397 | | if (!readset->fd_count && !writeset->fd_count && !exceptset->fd_count) { |
398 | | if (timeout > 0) { |
399 | | apr_sleep(timeout); |
400 | | } |
401 | | return 0; |
402 | | } |
403 | | #endif |
404 | |
|
405 | 0 | if (timeout < 0) { |
406 | 0 | tvptr = NULL; |
407 | 0 | } |
408 | 0 | else { |
409 | 0 | tv.tv_sec = (long) apr_time_sec(timeout); |
410 | 0 | tv.tv_usec = (long) apr_time_usec(timeout); |
411 | 0 | tvptr = &tv; |
412 | 0 | } |
413 | |
|
414 | 0 | return select(maxfd + 1, readset, writeset, exceptset, tvptr); |
415 | 0 | } |
416 | | |
417 | | /* Fill in the results (at most max) from the selected sets for the nelts |
418 | | * descriptors of query (the pollset's own query_set or a copy of it): the |
419 | | * descriptors still in the pollset (and still armed for APR_POLLONESHOT |
420 | | * ones, disarmed then); the wakeup pipe drained and *woken set if woken is |
421 | | * not NULL (else skipped, not drained). With the pollset locked. |
422 | | */ |
423 | | static apr_int32_t get_results(apr_pollset_t *pollset, |
424 | | fd_set *readset, fd_set *writeset, |
425 | | fd_set *exceptset, apr_pollfd_t *query, |
426 | | apr_uint32_t nelts, int *woken, |
427 | | apr_pollfd_t *results, apr_int32_t max) |
428 | 0 | { |
429 | 0 | apr_uint32_t i; |
430 | 0 | apr_int32_t j, k; |
431 | |
|
432 | 0 | for (i = 0, j = 0; i < nelts && j < max; i++) { |
433 | 0 | apr_os_sock_t fd; |
434 | |
|
435 | 0 | if (get_desc_fd(&query[i], &fd) != APR_SUCCESS) { |
436 | 0 | continue; |
437 | 0 | } |
438 | 0 | if (!FD_ISSET(fd, readset) && !FD_ISSET(fd, writeset) && |
439 | 0 | !FD_ISSET(fd, exceptset)) { |
440 | 0 | continue; |
441 | 0 | } |
442 | 0 | if (pollset_is_wakeup(pollset, &query[i])) { |
443 | 0 | if (woken) { |
444 | 0 | pollset_drain_wakeup(pollset); |
445 | 0 | *woken = 1; |
446 | 0 | } |
447 | 0 | continue; |
448 | 0 | } |
449 | 0 | if (query == pollset->p->query_set) { |
450 | 0 | k = i; |
451 | 0 | } |
452 | 0 | else if ((k = find_desc(pollset, &query[i], i)) < 0) { |
453 | 0 | continue; /* removed meanwhile */ |
454 | 0 | } |
455 | 0 | if (pollset->p->query_set[k].reqevents & APR_POLLONESHOT) { |
456 | 0 | if (pollset->p->disarmed[k]) { |
457 | 0 | continue; /* returned already (by another thread) */ |
458 | 0 | } |
459 | 0 | pollset->p->disarmed[k] = 1; |
460 | 0 | FD_CLR(fd, &(pollset->p->readset)); |
461 | 0 | FD_CLR(fd, &(pollset->p->writeset)); |
462 | 0 | FD_CLR(fd, &(pollset->p->exceptset)); |
463 | 0 | } |
464 | 0 | results[j] = pollset->p->query_set[k]; |
465 | 0 | results[j].rtnevents = 0; |
466 | 0 | if (FD_ISSET(fd, readset)) { |
467 | 0 | results[j].rtnevents |= APR_POLLIN; |
468 | 0 | } |
469 | 0 | if (FD_ISSET(fd, writeset)) { |
470 | 0 | results[j].rtnevents |= APR_POLLOUT; |
471 | 0 | } |
472 | 0 | if (FD_ISSET(fd, exceptset)) { |
473 | 0 | results[j].rtnevents |= APR_POLLERR; |
474 | 0 | } |
475 | 0 | j++; |
476 | 0 | } |
477 | 0 | return j; |
478 | 0 | } |
479 | | |
480 | | static apr_status_t impl_pollset_poll(apr_pollset_t *pollset, |
481 | | apr_interval_time_t timeout, |
482 | | apr_int32_t *num, |
483 | | const apr_pollfd_t **descriptors) |
484 | 0 | { |
485 | 0 | int rs, maxfd; |
486 | 0 | fd_set readset, writeset, exceptset; |
487 | 0 | apr_pollfd_t *query = pollset->p->query_set; |
488 | 0 | apr_uint32_t nelts; |
489 | 0 | apr_status_t rv = APR_SUCCESS; |
490 | |
|
491 | 0 | *num = 0; |
492 | |
|
493 | 0 | pollset_lock(pollset); |
494 | 0 | nelts = pollset->nelts; |
495 | 0 | maxfd = pollset->p->maxfd; |
496 | 0 | memcpy(&readset, &(pollset->p->readset), sizeof(fd_set)); |
497 | 0 | memcpy(&writeset, &(pollset->p->writeset), sizeof(fd_set)); |
498 | 0 | memcpy(&exceptset, &(pollset->p->exceptset), sizeof(fd_set)); |
499 | 0 | #if APR_HAS_THREADS |
500 | 0 | if (pollset->p->lock) { |
501 | 0 | query = pollset->p->poll_query; |
502 | 0 | memcpy(query, pollset->p->query_set, nelts * sizeof(*query)); |
503 | 0 | pollset->p->polling++; |
504 | 0 | } |
505 | 0 | #endif |
506 | 0 | pollset_unlock(pollset); |
507 | |
|
508 | 0 | rs = do_select(maxfd, &readset, &writeset, &exceptset, timeout); |
509 | |
|
510 | 0 | pollset_lock(pollset); |
511 | 0 | #if APR_HAS_THREADS |
512 | 0 | if (pollset->p->lock) { |
513 | 0 | pollset->p->polling--; |
514 | 0 | } |
515 | 0 | #endif |
516 | 0 | if (rs < 0) { |
517 | 0 | rv = apr_get_netos_error(); |
518 | 0 | } |
519 | 0 | else if (rs == 0) { |
520 | 0 | rv = APR_TIMEUP; |
521 | 0 | } |
522 | 0 | else { |
523 | 0 | int woken = 0; |
524 | |
|
525 | 0 | *num = get_results(pollset, &readset, &writeset, &exceptset, |
526 | 0 | query, nelts, &woken, pollset->p->result_set, |
527 | 0 | nelts); |
528 | 0 | if (descriptors) { |
529 | 0 | *descriptors = pollset->p->result_set; |
530 | 0 | } |
531 | | /* EINTR for the wakeup alone, SUCCESS with no descriptor for the |
532 | | * events of descriptors removed meanwhile |
533 | | */ |
534 | 0 | rv = (*num || !woken) ? APR_SUCCESS : APR_EINTR; |
535 | 0 | } |
536 | 0 | pollset_unlock(pollset); |
537 | |
|
538 | 0 | return rv; |
539 | 0 | } |
540 | | |
541 | | /* apr_pollset_poller_poll(): select copies of the sets (taken under the |
542 | | * lock), the ready descriptors found in the pollset's own query set. |
543 | | */ |
544 | | static apr_status_t impl_pollset_poller_poll(apr_pollset_poller_t *poller, |
545 | | apr_interval_time_t timeout, |
546 | | apr_int32_t *num) |
547 | 0 | { |
548 | 0 | apr_pollset_t *pollset = poller->pollset; |
549 | 0 | fd_set readset, writeset, exceptset; |
550 | 0 | int rs, maxfd; |
551 | 0 | apr_status_t rv; |
552 | |
|
553 | 0 | pollset_lock(pollset); |
554 | 0 | maxfd = pollset->p->maxfd; |
555 | 0 | memcpy(&readset, &(pollset->p->readset), sizeof(fd_set)); |
556 | 0 | memcpy(&writeset, &(pollset->p->writeset), sizeof(fd_set)); |
557 | 0 | memcpy(&exceptset, &(pollset->p->exceptset), sizeof(fd_set)); |
558 | 0 | #if APR_HAS_THREADS |
559 | 0 | pollset->p->polling++; |
560 | 0 | #endif |
561 | 0 | pollset_unlock(pollset); |
562 | |
|
563 | 0 | rs = do_select(maxfd, &readset, &writeset, &exceptset, timeout); |
564 | |
|
565 | 0 | pollset_lock(pollset); |
566 | 0 | #if APR_HAS_THREADS |
567 | 0 | pollset->p->polling--; |
568 | 0 | #endif |
569 | 0 | if (rs < 0) { |
570 | 0 | rv = apr_get_netos_error(); |
571 | 0 | } |
572 | 0 | else if (rs == 0) { |
573 | 0 | rv = APR_TIMEUP; |
574 | 0 | } |
575 | 0 | else { |
576 | 0 | int woken = 0; |
577 | | |
578 | | /* The descriptors still in the pollset (a removed one isn't, one |
579 | | * added meanwhile is returned if its fd was polled ready) |
580 | | */ |
581 | 0 | *num = get_results(pollset, &readset, &writeset, &exceptset, |
582 | 0 | pollset->p->query_set, pollset->nelts, &woken, |
583 | 0 | poller->results, poller->max); |
584 | | /* EINTR for the wakeup alone, SUCCESS with no descriptor for the |
585 | | * events consumed here |
586 | | */ |
587 | 0 | rv = (*num || !woken) ? APR_SUCCESS : APR_EINTR; |
588 | 0 | } |
589 | 0 | pollset_unlock(pollset); |
590 | |
|
591 | 0 | return rv; |
592 | 0 | } |
593 | | |
594 | | /* The reqevents honored (apr_poll_method_options()) */ |
595 | | #define SELECT_OPTIONS (APR_POLLIN | APR_POLLPRI | APR_POLLOUT | APR_POLLONESHOT) |
596 | | |
597 | | static const apr_pollset_provider_t impl = { |
598 | | impl_pollset_create, |
599 | | impl_pollset_add, |
600 | | impl_pollset_remove, |
601 | | impl_pollset_poll, |
602 | | NULL, |
603 | | impl_pollset_poller_poll, |
604 | | NULL, |
605 | | "select", |
606 | | APR_POLLSET_SELECT, |
607 | | SELECT_OPTIONS |
608 | | }; |
609 | | |
610 | | const apr_pollset_provider_t *apr_pollset_provider_select = &impl; |