/src/httpd/srclib/apr/poll/unix/poll.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 | | #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" |
24 | | #include "apr_arch_poll_private.h" |
25 | | |
26 | | #define APR_WANT_MEMFUNC |
27 | | #include "apr_want.h" |
28 | | |
29 | | #if defined(HAVE_POLL) |
30 | | |
31 | | #ifdef HAVE_ALLOCA_H |
32 | | #include <alloca.h> |
33 | | #endif |
34 | | |
35 | | #if defined(HAVE_PPOLL) |
36 | | #include <time.h> |
37 | | #endif |
38 | | |
39 | | #ifdef WIN32 |
40 | | typedef ULONG nfds_t; /* WSAPoll()'s */ |
41 | | #endif |
42 | | |
43 | | /* A disarmed APR_POLLONESHOT descriptor's fd, ignored by poll() and |
44 | | * WSAPoll() (negative) |
45 | | */ |
46 | 0 | #define DISARMED_FD ((apr_os_sock_t)-1) |
47 | | |
48 | | /* poll() with a timeout in microseconds (negative for no timeout): ppoll() is |
49 | | * precise to the microsecond where available, poll() and WSAPoll() take |
50 | | * milliseconds (the timeout rounded up then). WSAPoll() requires at least one |
51 | | * valid socket: a set empty or with all its descriptors disarmed sleeps for |
52 | | * the timeout. |
53 | | */ |
54 | | static int do_poll(struct pollfd *fds, nfds_t nfds, |
55 | | apr_interval_time_t timeout) |
56 | 0 | { |
57 | 0 | #if defined(HAVE_PPOLL) |
58 | 0 | struct timespec ts, *tsp = poll_timespec(timeout, &ts); |
59 | |
|
60 | 0 | return ppoll(fds, nfds, tsp, NULL); |
61 | | #else |
62 | | #ifdef WIN32 |
63 | | nfds_t i; |
64 | | |
65 | | for (i = 0; i < nfds && fds[i].fd == DISARMED_FD; i++) { |
66 | | continue; |
67 | | } |
68 | | if (i == nfds) { |
69 | | if (timeout > 0) { |
70 | | apr_sleep(timeout); |
71 | | } |
72 | | return 0; |
73 | | } |
74 | | #endif |
75 | | if (timeout > 0) { |
76 | | /* convert microseconds to milliseconds (round up) */ |
77 | | timeout = (timeout + 999) / 1000; |
78 | | } |
79 | | #ifdef WIN32 |
80 | | return WSAPoll(fds, nfds, (int)timeout); |
81 | | #else |
82 | | return poll(fds, nfds, (int)timeout); |
83 | | #endif |
84 | | #endif |
85 | 0 | } |
86 | | |
87 | | static apr_int16_t get_event(apr_int16_t event) |
88 | 0 | { |
89 | 0 | apr_int16_t rv = 0; |
90 | |
|
91 | 0 | if (event & APR_POLLIN) |
92 | 0 | rv |= POLLIN; |
93 | 0 | if (event & APR_POLLPRI) |
94 | 0 | rv |= POLLPRI; |
95 | 0 | if (event & APR_POLLOUT) |
96 | 0 | rv |= POLLOUT; |
97 | 0 | #ifdef POLLRDHUP |
98 | 0 | if (event & APR_POLLRDHUP) |
99 | 0 | rv |= POLLRDHUP; |
100 | 0 | #endif |
101 | | /* POLLERR, POLLHUP, and POLLNVAL aren't valid as requested events */ |
102 | |
|
103 | 0 | return rv; |
104 | 0 | } |
105 | | |
106 | | static apr_int16_t get_revent(apr_int16_t event) |
107 | 0 | { |
108 | 0 | apr_int16_t rv = 0; |
109 | |
|
110 | 0 | if (event & POLLIN) |
111 | 0 | rv |= APR_POLLIN; |
112 | 0 | if (event & POLLPRI) |
113 | 0 | rv |= APR_POLLPRI; |
114 | 0 | if (event & POLLOUT) |
115 | 0 | rv |= APR_POLLOUT; |
116 | 0 | if (event & POLLERR) |
117 | 0 | rv |= APR_POLLERR; |
118 | 0 | if (event & POLLHUP) |
119 | 0 | rv |= APR_POLLHUP; |
120 | 0 | if (event & POLLNVAL) |
121 | 0 | rv |= APR_POLLNVAL; |
122 | 0 | #ifdef POLLRDHUP |
123 | 0 | if (event & POLLRDHUP) |
124 | 0 | rv |= APR_POLLRDHUP; |
125 | 0 | #endif |
126 | |
|
127 | 0 | return rv; |
128 | 0 | } |
129 | | |
130 | | #ifdef POLL_USES_POLL |
131 | | |
132 | | #define SMALL_POLLSET_LIMIT 8 |
133 | | |
134 | | APR_DECLARE(apr_status_t) apr_poll(apr_pollfd_t *aprset, apr_int32_t num, |
135 | | apr_int32_t *nsds, |
136 | | apr_interval_time_t timeout) |
137 | 0 | { |
138 | 0 | int i, num_to_poll; |
139 | 0 | #ifdef HAVE_C_VARARRAYS |
140 | | /* XXX: I trust that this is a segv when insufficient stack exists? */ |
141 | 0 | struct pollfd pollset[num + 1]; /* +1 since allocating 0 is undefined behaviour */ |
142 | | #elif defined(HAVE_ALLOCA) |
143 | | struct pollfd *pollset = alloca(sizeof(struct pollfd) * num); |
144 | | if (!pollset) |
145 | | return APR_ENOMEM; |
146 | | #else |
147 | | struct pollfd tmp_pollset[SMALL_POLLSET_LIMIT]; |
148 | | struct pollfd *pollset; |
149 | | |
150 | | if (num <= SMALL_POLLSET_LIMIT) { |
151 | | pollset = tmp_pollset; |
152 | | } |
153 | | else { |
154 | | /* This does require O(n) to copy the descriptors to the internal |
155 | | * mapping. |
156 | | */ |
157 | | pollset = malloc(sizeof(struct pollfd) * num); |
158 | | /* The other option is adding an apr_pool_abort() fn to invoke |
159 | | * the pool's out of memory handler |
160 | | */ |
161 | | if (!pollset) |
162 | | return APR_ENOMEM; |
163 | | } |
164 | | #endif |
165 | 0 | for (i = 0; i < num; i++) { |
166 | 0 | if (aprset[i].desc_type == APR_POLL_SOCKET) { |
167 | 0 | pollset[i].fd = aprset[i].desc.s->socketdes; |
168 | 0 | } |
169 | 0 | else if (aprset[i].desc_type == APR_POLL_FILE) { |
170 | 0 | pollset[i].fd = aprset[i].desc.f->filedes; |
171 | 0 | } |
172 | 0 | else { |
173 | 0 | break; |
174 | 0 | } |
175 | 0 | pollset[i].events = get_event(aprset[i].reqevents); |
176 | 0 | } |
177 | 0 | num_to_poll = i; |
178 | |
|
179 | 0 | i = do_poll(pollset, num_to_poll, timeout); |
180 | 0 | (*nsds) = i; |
181 | |
|
182 | 0 | if (i > 0) { /* poll() sets revents only if an event was signalled; |
183 | | * we don't promise to set rtnevents unless an event |
184 | | * was signalled |
185 | | */ |
186 | 0 | for (i = 0; i < num; i++) { |
187 | 0 | aprset[i].rtnevents = get_revent(pollset[i].revents); |
188 | 0 | } |
189 | 0 | } |
190 | |
|
191 | | #if !defined(HAVE_C_VARARRAYS) && !defined(HAVE_ALLOCA) |
192 | | if (num > SMALL_POLLSET_LIMIT) { |
193 | | free(pollset); |
194 | | } |
195 | | #endif |
196 | |
|
197 | 0 | if ((*nsds) < 0) { |
198 | 0 | return apr_get_netos_error(); |
199 | 0 | } |
200 | 0 | if ((*nsds) == 0) { |
201 | 0 | return APR_TIMEUP; |
202 | 0 | } |
203 | 0 | return APR_SUCCESS; |
204 | 0 | } |
205 | | |
206 | | |
207 | | #endif /* POLL_USES_POLL */ |
208 | | |
209 | | struct apr_pollset_private_t |
210 | | { |
211 | | struct pollfd *pollset; |
212 | | apr_pollfd_t *query_set; |
213 | | apr_pollfd_t *result_set; |
214 | | #if APR_HAS_THREADS |
215 | | /* APR_POLLSET_THREADSAFE: the arrays above are modified under the lock, |
216 | | * and apr_pollset_poll() polls a copy of them (taken under the lock) |
217 | | * so that descriptors can be added or removed meanwhile; the ready ones |
218 | | * are then found again by their descriptor (if still there). |
219 | | */ |
220 | | apr_thread_mutex_t *lock; |
221 | | struct pollfd *poll_set; |
222 | | apr_pollfd_t *poll_query; |
223 | | /* number of threads in poll(), to wake them up on add */ |
224 | | apr_uint32_t polling; |
225 | | #endif |
226 | | }; |
227 | | |
228 | | #if APR_HAS_THREADS |
229 | 0 | #define pollset_lock(ps) do { \ |
230 | 0 | if ((ps)->p->lock) { \ |
231 | 0 | apr_thread_mutex_lock((ps)->p->lock); \ |
232 | 0 | } \ |
233 | 0 | } while (0) |
234 | 0 | #define pollset_unlock(ps) do { \ |
235 | 0 | if ((ps)->p->lock) { \ |
236 | 0 | apr_thread_mutex_unlock((ps)->p->lock); \ |
237 | 0 | } \ |
238 | 0 | } while (0) |
239 | | #else |
240 | | #define pollset_lock(ps) do { } while (0) |
241 | | #define pollset_unlock(ps) do { } while (0) |
242 | | #endif |
243 | | |
244 | | static apr_status_t impl_pollset_create(apr_pollset_t *pollset, |
245 | | apr_uint32_t size, |
246 | | apr_pool_t *p, |
247 | | apr_uint32_t flags) |
248 | 0 | { |
249 | | #if !APR_HAS_THREADS |
250 | | if (flags & APR_POLLSET_THREADSAFE) { |
251 | | return APR_ENOTIMPL; |
252 | | } |
253 | | #endif |
254 | |
|
255 | 0 | pollset->p = apr_pcalloc(p, sizeof(apr_pollset_private_t)); |
256 | 0 | pollset->p->pollset = apr_palloc(p, size * sizeof(struct pollfd)); |
257 | 0 | pollset->p->query_set = apr_palloc(p, size * sizeof(apr_pollfd_t)); |
258 | 0 | pollset->p->result_set = apr_palloc(p, size * sizeof(apr_pollfd_t)); |
259 | 0 | #if APR_HAS_THREADS |
260 | 0 | if (flags & APR_POLLSET_THREADSAFE) { |
261 | 0 | apr_status_t rv = apr_thread_mutex_create(&pollset->p->lock, |
262 | 0 | APR_THREAD_MUTEX_DEFAULT, |
263 | 0 | p); |
264 | 0 | if (rv != APR_SUCCESS) { |
265 | 0 | pollset->p = NULL; |
266 | 0 | return rv; |
267 | 0 | } |
268 | 0 | pollset->p->poll_set = apr_palloc(p, size * sizeof(struct pollfd)); |
269 | 0 | pollset->p->poll_query = apr_palloc(p, size * sizeof(apr_pollfd_t)); |
270 | 0 | } |
271 | 0 | #endif |
272 | | |
273 | 0 | return APR_SUCCESS; |
274 | 0 | } |
275 | | |
276 | | /* The index of desc in the pollset (tried at hint first), or -1 */ |
277 | | static apr_int32_t find_desc(apr_pollset_t *pollset, |
278 | | const apr_pollfd_t *desc, apr_uint32_t hint) |
279 | 0 | { |
280 | 0 | apr_uint32_t i; |
281 | |
|
282 | 0 | if (hint < pollset->nelts |
283 | 0 | && pollset->p->query_set[hint].desc.s == desc->desc.s) { |
284 | 0 | return hint; |
285 | 0 | } |
286 | 0 | for (i = 0; i < pollset->nelts; i++) { |
287 | 0 | if (pollset->p->query_set[i].desc.s == desc->desc.s) { |
288 | 0 | return i; |
289 | 0 | } |
290 | 0 | } |
291 | 0 | return -1; |
292 | 0 | } |
293 | | |
294 | | static apr_status_t impl_pollset_add(apr_pollset_t *pollset, |
295 | | const apr_pollfd_t *descriptor) |
296 | 0 | { |
297 | 0 | apr_os_sock_t fd; |
298 | 0 | apr_int32_t k = -1; |
299 | 0 | int wakeup = 0; |
300 | |
|
301 | 0 | if (descriptor->desc_type == APR_POLL_SOCKET) { |
302 | 0 | fd = descriptor->desc.s->socketdes; |
303 | 0 | } |
304 | 0 | else { |
305 | 0 | #if APR_FILES_AS_SOCKETS |
306 | 0 | fd = descriptor->desc.f->filedes; |
307 | | #else |
308 | | return APR_EBADF; |
309 | | #endif |
310 | 0 | } |
311 | |
|
312 | 0 | pollset_lock(pollset); |
313 | | |
314 | | /* An APR_POLLONESHOT descriptor stays in the pollset once returned |
315 | | * (disarmed), the next add re-arms it in its slot. |
316 | | */ |
317 | 0 | if (descriptor->reqevents & APR_POLLONESHOT) { |
318 | 0 | k = find_desc(pollset, descriptor, 0); |
319 | 0 | if (k >= 0 |
320 | 0 | && !(pollset->p->query_set[k].reqevents & APR_POLLONESHOT)) { |
321 | 0 | k = -1; |
322 | 0 | } |
323 | 0 | } |
324 | 0 | if (k < 0) { |
325 | 0 | if (pollset->nelts == pollset->nalloc) { |
326 | 0 | pollset_unlock(pollset); |
327 | 0 | return APR_ENOMEM; |
328 | 0 | } |
329 | 0 | k = pollset->nelts++; |
330 | 0 | } |
331 | | |
332 | 0 | pollset->p->query_set[k] = *descriptor; |
333 | 0 | pollset->p->pollset[k].fd = fd; |
334 | 0 | pollset->p->pollset[k].events = get_event(descriptor->reqevents); |
335 | |
|
336 | 0 | #if APR_HAS_THREADS |
337 | | /* A thread polling a copy of the pollset wouldn't see the new descriptor: |
338 | | * interrupt it if possible (APR_EINTR, see apr_pollset_add()), else it's |
339 | | * polled from the next call. |
340 | | */ |
341 | 0 | wakeup = (pollset->p->polling && (pollset->flags & APR_POLLSET_WAKEABLE) |
342 | 0 | && !pollset_is_wakeup(pollset, descriptor)); |
343 | 0 | #endif |
344 | |
|
345 | 0 | pollset_unlock(pollset); |
346 | |
|
347 | 0 | if (wakeup) { |
348 | 0 | apr_pollset_wakeup(pollset); |
349 | 0 | } |
350 | 0 | return APR_SUCCESS; |
351 | 0 | } |
352 | | |
353 | | static apr_status_t impl_pollset_remove(apr_pollset_t *pollset, |
354 | | const apr_pollfd_t *descriptor) |
355 | 0 | { |
356 | 0 | apr_uint32_t i; |
357 | |
|
358 | 0 | pollset_lock(pollset); |
359 | |
|
360 | 0 | for (i = 0; i < pollset->nelts; i++) { |
361 | 0 | if (descriptor->desc.s == pollset->p->query_set[i].desc.s) { |
362 | | /* Found an instance of the fd: remove this and any other copies */ |
363 | 0 | apr_uint32_t dst = i; |
364 | 0 | apr_uint32_t old_nelts = pollset->nelts; |
365 | 0 | pollset->nelts--; |
366 | 0 | for (i++; i < old_nelts; i++) { |
367 | 0 | if (descriptor->desc.s == pollset->p->query_set[i].desc.s) { |
368 | 0 | pollset->nelts--; |
369 | 0 | } |
370 | 0 | else { |
371 | 0 | pollset->p->pollset[dst] = pollset->p->pollset[i]; |
372 | 0 | pollset->p->query_set[dst] = pollset->p->query_set[i]; |
373 | 0 | dst++; |
374 | 0 | } |
375 | 0 | } |
376 | 0 | pollset_unlock(pollset); |
377 | 0 | return APR_SUCCESS; |
378 | 0 | } |
379 | 0 | } |
380 | | |
381 | 0 | pollset_unlock(pollset); |
382 | 0 | return APR_NOTFOUND; |
383 | 0 | } |
384 | | |
385 | | /* Fill in the results (at most max) from the nfds polled fds of query (the |
386 | | * pollset's own arrays or a copy of them): the descriptors still in the |
387 | | * pollset (and still armed for APR_POLLONESHOT ones, disarmed then); the |
388 | | * wakeup pipe drained and *woken set if woken is not NULL (else skipped, not |
389 | | * drained). With the pollset locked. |
390 | | */ |
391 | | static apr_int32_t get_results(apr_pollset_t *pollset, |
392 | | struct pollfd *fds, apr_pollfd_t *query, |
393 | | apr_uint32_t nfds, int *woken, |
394 | | apr_pollfd_t *results, apr_int32_t max) |
395 | 0 | { |
396 | 0 | apr_uint32_t i; |
397 | 0 | apr_int32_t j, k; |
398 | |
|
399 | 0 | for (i = 0, j = 0; i < nfds && j < max; i++) { |
400 | 0 | if (fds[i].revents == 0) { |
401 | 0 | continue; |
402 | 0 | } |
403 | 0 | if (pollset_is_wakeup(pollset, &query[i])) { |
404 | 0 | if (woken) { |
405 | 0 | pollset_drain_wakeup(pollset); |
406 | 0 | *woken = 1; |
407 | 0 | } |
408 | 0 | continue; |
409 | 0 | } |
410 | 0 | if (query == pollset->p->query_set) { |
411 | 0 | k = i; |
412 | 0 | } |
413 | 0 | else if ((k = find_desc(pollset, &query[i], i)) < 0) { |
414 | 0 | continue; /* removed meanwhile */ |
415 | 0 | } |
416 | 0 | if (pollset->p->query_set[k].reqevents & APR_POLLONESHOT) { |
417 | 0 | if (pollset->p->pollset[k].fd == DISARMED_FD) { |
418 | 0 | continue; /* returned already (by another thread) */ |
419 | 0 | } |
420 | 0 | pollset->p->pollset[k].fd = DISARMED_FD; |
421 | 0 | } |
422 | 0 | results[j] = pollset->p->query_set[k]; |
423 | 0 | results[j].rtnevents = get_revent(fds[i].revents); |
424 | 0 | j++; |
425 | 0 | } |
426 | 0 | return j; |
427 | 0 | } |
428 | | |
429 | | static apr_status_t impl_pollset_poll(apr_pollset_t *pollset, |
430 | | apr_interval_time_t timeout, |
431 | | apr_int32_t *num, |
432 | | const apr_pollfd_t **descriptors) |
433 | 0 | { |
434 | 0 | int ret; |
435 | 0 | apr_status_t rv = APR_SUCCESS; |
436 | 0 | struct pollfd *fds = pollset->p->pollset; |
437 | 0 | apr_pollfd_t *query = pollset->p->query_set; |
438 | 0 | apr_uint32_t nfds; |
439 | |
|
440 | 0 | *num = 0; |
441 | |
|
442 | 0 | pollset_lock(pollset); |
443 | 0 | nfds = pollset->nelts; |
444 | 0 | #if APR_HAS_THREADS |
445 | 0 | if (pollset->p->lock) { |
446 | 0 | fds = pollset->p->poll_set; |
447 | 0 | query = pollset->p->poll_query; |
448 | 0 | memcpy(fds, pollset->p->pollset, nfds * sizeof(*fds)); |
449 | 0 | memcpy(query, pollset->p->query_set, nfds * sizeof(*query)); |
450 | 0 | pollset->p->polling++; |
451 | 0 | } |
452 | 0 | #endif |
453 | 0 | pollset_unlock(pollset); |
454 | |
|
455 | 0 | ret = do_poll(fds, nfds, timeout); |
456 | |
|
457 | 0 | pollset_lock(pollset); |
458 | 0 | #if APR_HAS_THREADS |
459 | 0 | if (pollset->p->lock) { |
460 | 0 | pollset->p->polling--; |
461 | 0 | } |
462 | 0 | #endif |
463 | 0 | if (ret < 0) { |
464 | 0 | rv = apr_get_netos_error(); |
465 | 0 | } |
466 | 0 | else if (ret == 0) { |
467 | 0 | rv = APR_TIMEUP; |
468 | 0 | } |
469 | 0 | else { |
470 | 0 | int woken = 0; |
471 | |
|
472 | 0 | *num = get_results(pollset, fds, query, nfds, &woken, |
473 | 0 | pollset->p->result_set, nfds); |
474 | 0 | if (*num && descriptors) { |
475 | 0 | *descriptors = pollset->p->result_set; |
476 | 0 | } |
477 | | /* EINTR for the wakeup alone, SUCCESS with no descriptor for the |
478 | | * events of descriptors removed meanwhile |
479 | | */ |
480 | 0 | rv = (*num || !woken) ? APR_SUCCESS : APR_EINTR; |
481 | 0 | } |
482 | 0 | pollset_unlock(pollset); |
483 | |
|
484 | 0 | return rv; |
485 | 0 | } |
486 | | |
487 | | /* The index in the pollset of the polled (and armed) fd, -1 if removed or |
488 | | * disarmed meanwhile; hint its index when polled, the likely one. With the |
489 | | * pollset locked. |
490 | | */ |
491 | | static apr_int32_t find_fd(apr_pollset_t *pollset, apr_os_sock_t fd, |
492 | | apr_uint32_t hint) |
493 | 0 | { |
494 | 0 | apr_uint32_t i; |
495 | |
|
496 | 0 | if (hint < pollset->nelts && pollset->p->pollset[hint].fd == fd) { |
497 | 0 | return hint; |
498 | 0 | } |
499 | 0 | for (i = 0; i < pollset->nelts; i++) { |
500 | 0 | if (pollset->p->pollset[i].fd == fd) { |
501 | 0 | return i; |
502 | 0 | } |
503 | 0 | } |
504 | 0 | return -1; |
505 | 0 | } |
506 | | |
507 | | /* apr_pollset_poller_poll(): poll the poller's copy of the pollfds (taken |
508 | | * under the lock), the ready ones found again in the pollset by their fd. |
509 | | */ |
510 | | static apr_status_t impl_pollset_poller_create(apr_pollset_poller_t *poller, |
511 | | apr_pool_t *p) |
512 | 0 | { |
513 | | /* nalloc is fixed */ |
514 | 0 | poller->buf = apr_palloc(p, poller->pollset->nalloc * sizeof(struct pollfd)); |
515 | 0 | return APR_SUCCESS; |
516 | 0 | } |
517 | | |
518 | | static apr_status_t impl_pollset_poller_poll(apr_pollset_poller_t *poller, |
519 | | apr_interval_time_t timeout, |
520 | | apr_int32_t *num) |
521 | 0 | { |
522 | 0 | apr_pollset_t *pollset = poller->pollset; |
523 | 0 | struct pollfd *fds = poller->buf; |
524 | 0 | apr_uint32_t nfds, i; |
525 | 0 | apr_int32_t j, k; |
526 | 0 | apr_status_t rv; |
527 | 0 | int ret, woken = 0; |
528 | |
|
529 | 0 | pollset_lock(pollset); |
530 | 0 | nfds = pollset->nelts; |
531 | 0 | memcpy(fds, pollset->p->pollset, nfds * sizeof(*fds)); |
532 | 0 | #if APR_HAS_THREADS |
533 | 0 | pollset->p->polling++; |
534 | 0 | #endif |
535 | 0 | pollset_unlock(pollset); |
536 | |
|
537 | 0 | ret = do_poll(fds, nfds, timeout); |
538 | |
|
539 | 0 | pollset_lock(pollset); |
540 | 0 | #if APR_HAS_THREADS |
541 | 0 | pollset->p->polling--; |
542 | 0 | #endif |
543 | 0 | if (ret < 0) { |
544 | 0 | rv = apr_get_netos_error(); |
545 | 0 | pollset_unlock(pollset); |
546 | 0 | return rv; |
547 | 0 | } |
548 | 0 | if (ret == 0) { |
549 | 0 | pollset_unlock(pollset); |
550 | 0 | return APR_TIMEUP; |
551 | 0 | } |
552 | 0 | for (i = 0, j = 0; i < nfds && j < poller->max; i++) { |
553 | 0 | if (fds[i].revents == 0) { |
554 | 0 | continue; |
555 | 0 | } |
556 | 0 | if ((k = find_fd(pollset, fds[i].fd, i)) < 0) { |
557 | 0 | continue; /* removed or disarmed meanwhile */ |
558 | 0 | } |
559 | 0 | if (pollset_is_wakeup(pollset, &pollset->p->query_set[k])) { |
560 | 0 | pollset_drain_wakeup(pollset); |
561 | 0 | woken = 1; |
562 | 0 | continue; |
563 | 0 | } |
564 | 0 | if (pollset->p->query_set[k].reqevents & APR_POLLONESHOT) { |
565 | 0 | pollset->p->pollset[k].fd = DISARMED_FD; |
566 | 0 | } |
567 | 0 | poller->results[j] = pollset->p->query_set[k]; |
568 | 0 | poller->results[j].rtnevents = get_revent(fds[i].revents); |
569 | 0 | j++; |
570 | 0 | } |
571 | 0 | pollset_unlock(pollset); |
572 | |
|
573 | 0 | *num = j; |
574 | | /* EINTR for the wakeup alone, SUCCESS with no descriptor for the events |
575 | | * consumed here |
576 | | */ |
577 | 0 | return (j || !woken) ? APR_SUCCESS : APR_EINTR; |
578 | 0 | } |
579 | | |
580 | | /* The reqevents honored (apr_poll_method_options()) */ |
581 | | #ifdef POLLRDHUP |
582 | | #define POLL_OPTIONS_RDHUP APR_POLLRDHUP |
583 | | #else |
584 | | #define POLL_OPTIONS_RDHUP 0 |
585 | | #endif |
586 | | #define POLL_OPTIONS (APR_POLLIN | APR_POLLPRI | APR_POLLOUT \ |
587 | | | APR_POLLONESHOT | POLL_OPTIONS_RDHUP) |
588 | | |
589 | | static const apr_pollset_provider_t impl = { |
590 | | impl_pollset_create, |
591 | | impl_pollset_add, |
592 | | impl_pollset_remove, |
593 | | impl_pollset_poll, |
594 | | impl_pollset_poller_create, |
595 | | impl_pollset_poller_poll, |
596 | | NULL, |
597 | | "poll", |
598 | | APR_POLLSET_POLL, |
599 | | POLL_OPTIONS |
600 | | }; |
601 | | |
602 | | const apr_pollset_provider_t *apr_pollset_provider_poll = &impl; |
603 | | |
604 | | /* Poll method pollcb. |
605 | | * This is probably usable only for WIN32 having WSAPoll |
606 | | */ |
607 | | static apr_status_t impl_pollcb_create(apr_pollcb_t *pollcb, |
608 | | apr_uint32_t size, |
609 | | apr_pool_t *p, |
610 | | apr_uint32_t flags) |
611 | 0 | { |
612 | 0 | #if APR_HAS_THREADS |
613 | 0 | return APR_ENOTIMPL; |
614 | | #else |
615 | | pollcb->fd = -1; |
616 | | |
617 | | pollcb->pollset.ps = apr_palloc(p, size * sizeof(struct pollfd)); |
618 | | pollcb->copyset = apr_palloc(p, size * sizeof(apr_pollfd_t *)); |
619 | | |
620 | | return APR_SUCCESS; |
621 | | #endif |
622 | 0 | } |
623 | | |
624 | | static apr_status_t impl_pollcb_add(apr_pollcb_t *pollcb, |
625 | | apr_pollfd_t *descriptor) |
626 | 0 | { |
627 | 0 | apr_os_sock_t fd; |
628 | 0 | apr_uint32_t i = pollcb->nelts; |
629 | |
|
630 | 0 | if (descriptor->desc_type == APR_POLL_SOCKET) { |
631 | 0 | fd = descriptor->desc.s->socketdes; |
632 | 0 | } |
633 | 0 | else { |
634 | 0 | #if APR_FILES_AS_SOCKETS |
635 | 0 | fd = descriptor->desc.f->filedes; |
636 | | #else |
637 | | return APR_EBADF; |
638 | | #endif |
639 | 0 | } |
640 | | |
641 | | /* An APR_POLLONESHOT descriptor stays in the pollcb once returned |
642 | | * (disarmed), the next add re-arms it. |
643 | | */ |
644 | 0 | if (descriptor->reqevents & APR_POLLONESHOT) { |
645 | 0 | for (i = 0; i < pollcb->nelts; i++) { |
646 | 0 | if (pollcb->copyset[i]->desc.s == descriptor->desc.s |
647 | 0 | && (pollcb->copyset[i]->reqevents & APR_POLLONESHOT)) { |
648 | 0 | break; |
649 | 0 | } |
650 | 0 | } |
651 | 0 | } |
652 | 0 | if (i == pollcb->nelts) { |
653 | 0 | if (pollcb->nelts == pollcb->nalloc) { |
654 | 0 | return APR_ENOMEM; |
655 | 0 | } |
656 | 0 | pollcb->nelts++; |
657 | 0 | } |
658 | | |
659 | 0 | pollcb->pollset.ps[i].fd = fd; |
660 | 0 | pollcb->pollset.ps[i].events = get_event(descriptor->reqevents); |
661 | 0 | pollcb->copyset[i] = descriptor; |
662 | |
|
663 | 0 | return APR_SUCCESS; |
664 | 0 | } |
665 | | |
666 | | static apr_status_t impl_pollcb_remove(apr_pollcb_t *pollcb, |
667 | | apr_pollfd_t *descriptor) |
668 | 0 | { |
669 | 0 | apr_uint32_t i; |
670 | |
|
671 | 0 | for (i = 0; i < pollcb->nelts; i++) { |
672 | 0 | if (descriptor->desc.s == pollcb->copyset[i]->desc.s) { |
673 | | /* Found an instance of the fd: remove this and any other copies */ |
674 | 0 | apr_uint32_t dst = i; |
675 | 0 | apr_uint32_t old_nelts = pollcb->nelts; |
676 | 0 | pollcb->nelts--; |
677 | 0 | for (i++; i < old_nelts; i++) { |
678 | 0 | if (descriptor->desc.s == pollcb->copyset[i]->desc.s) { |
679 | 0 | pollcb->nelts--; |
680 | 0 | } |
681 | 0 | else { |
682 | 0 | pollcb->pollset.ps[dst] = pollcb->pollset.ps[i]; |
683 | 0 | pollcb->copyset[dst] = pollcb->copyset[i]; |
684 | 0 | dst++; |
685 | 0 | } |
686 | 0 | } |
687 | 0 | return APR_SUCCESS; |
688 | 0 | } |
689 | 0 | } |
690 | | |
691 | 0 | return APR_NOTFOUND; |
692 | 0 | } |
693 | | |
694 | | static apr_status_t impl_pollcb_poll(apr_pollcb_t *pollcb, |
695 | | apr_interval_time_t timeout, |
696 | | apr_pollcb_cb_t func, |
697 | | void *baton) |
698 | 0 | { |
699 | 0 | int ret; |
700 | 0 | apr_status_t rv = APR_SUCCESS; |
701 | 0 | apr_uint32_t i; |
702 | |
|
703 | 0 | ret = do_poll(pollcb->pollset.ps, pollcb->nelts, timeout); |
704 | 0 | if (ret < 0) { |
705 | 0 | return apr_get_netos_error(); |
706 | 0 | } |
707 | 0 | else if (ret == 0) { |
708 | 0 | return APR_TIMEUP; |
709 | 0 | } |
710 | 0 | else { |
711 | 0 | for (i = 0; i < pollcb->nelts; i++) { |
712 | 0 | if (pollcb->pollset.ps[i].revents != 0) { |
713 | 0 | apr_pollfd_t *pollfd = pollcb->copyset[i]; |
714 | |
|
715 | 0 | if (pollcb_is_wakeup(pollcb, pollfd)) { |
716 | 0 | pollcb_drain_wakeup(pollcb); |
717 | 0 | return APR_EINTR; |
718 | 0 | } |
719 | 0 | if (pollfd->reqevents & APR_POLLONESHOT) { |
720 | | /* Disarmed until re-added */ |
721 | 0 | pollcb->pollset.ps[i].fd = DISARMED_FD; |
722 | 0 | } |
723 | 0 | pollfd->rtnevents = get_revent(pollcb->pollset.ps[i].revents); |
724 | 0 | rv = func(baton, pollfd); |
725 | 0 | if (rv) { |
726 | 0 | return rv; |
727 | 0 | } |
728 | 0 | } |
729 | 0 | } |
730 | 0 | } |
731 | 0 | return rv; |
732 | 0 | } |
733 | | |
734 | | static const apr_pollcb_provider_t impl_cb = { |
735 | | impl_pollcb_create, |
736 | | impl_pollcb_add, |
737 | | impl_pollcb_remove, |
738 | | impl_pollcb_poll, |
739 | | NULL, |
740 | | "poll", |
741 | | APR_POLLSET_POLL, |
742 | | POLL_OPTIONS |
743 | | }; |
744 | | |
745 | | const apr_pollcb_provider_t *apr_pollcb_provider_poll = &impl_cb; |
746 | | |
747 | | #endif /* HAVE_POLL */ |
748 | | |
749 | | /* The apr_poll_method_*() getters, for every platform */ |
750 | | |
751 | | #ifdef WIN32 |
752 | | /* What apr_pollset_create_ex() and apr_pollcb_create_ex() resolve |
753 | | * APR_POLLSET_DEFAULT to (WSAPoll() favored) |
754 | | */ |
755 | | #define POLL_DEFAULT_METHOD APR_POLLSET_POLL |
756 | | #else |
757 | 0 | #define POLL_DEFAULT_METHOD POLLSET_DEFAULT_METHOD |
758 | | #endif |
759 | | |
760 | | APR_DECLARE(apr_poll_method_e) apr_poll_method_get(void) |
761 | 0 | { |
762 | 0 | #ifdef POLL_USES_POLL |
763 | 0 | return APR_POLLSET_POLL; |
764 | | #else |
765 | | return APR_POLLSET_SELECT; |
766 | | #endif |
767 | 0 | } |
768 | | |
769 | | APR_DECLARE(apr_poll_method_e) apr_poll_method_default(void) |
770 | 0 | { |
771 | 0 | return POLL_DEFAULT_METHOD; |
772 | 0 | } |
773 | | |
774 | | APR_DECLARE(const char *) apr_poll_method_name(apr_poll_method_e method) |
775 | 0 | { |
776 | 0 | if (method == APR_POLLSET_DEFAULT) { |
777 | 0 | method = POLL_DEFAULT_METHOD; |
778 | 0 | } |
779 | 0 | switch (method) { |
780 | 0 | case APR_POLLSET_SELECT: |
781 | 0 | return "select"; |
782 | 0 | case APR_POLLSET_KQUEUE: |
783 | 0 | return "kqueue"; |
784 | 0 | case APR_POLLSET_PORT: |
785 | 0 | return "port"; |
786 | 0 | case APR_POLLSET_EPOLL: |
787 | 0 | return "epoll"; |
788 | 0 | case APR_POLLSET_POLL: |
789 | 0 | return "poll"; |
790 | 0 | case APR_POLLSET_AIO_MSGQ: |
791 | 0 | return "asio"; |
792 | 0 | default: |
793 | 0 | return "unknown"; |
794 | 0 | } |
795 | 0 | } |
796 | | |
797 | | APR_DECLARE(apr_int16_t) apr_poll_method_options(apr_poll_method_e method) |
798 | 0 | { |
799 | 0 | const apr_pollset_provider_t *provider; |
800 | |
|
801 | 0 | if (method == APR_POLLSET_DEFAULT) { |
802 | 0 | method = POLL_DEFAULT_METHOD; |
803 | 0 | } |
804 | 0 | provider = apr_pollset_provider(method); |
805 | 0 | return provider ? provider->options : 0; |
806 | 0 | } |