1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 1998 - 2009, Daniel Stenberg, <daniel@haxx.se>, et al.
10 * This software is licensed as described in the file COPYING, which
11 * you should have received as part of this distribution. The terms
12 * are also available at http://curl.haxx.se/docs/copyright.html.
14 * You may opt to use, copy, modify, merge, publish, distribute and/or sell
15 * copies of the Software, and permit persons to whom the Software is
16 * furnished to do so, under the terms of the COPYING file.
18 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19 * KIND, either express or implied.
21 ***************************************************************************/
25 #ifdef HAVE_SYS_SELECT_H
26 #include <sys/select.h>
28 #ifdef HAVE_SYS_TIME_H
32 #if !defined(HAVE_SELECT) && !defined(HAVE_POLL_FINE)
33 #error "We can't compile without select() or poll() support."
36 #if defined(__BEOS__) && !defined(__HAIKU__)
37 /* BeOS has FD_SET defined in socket.h */
42 #include <dos.h> /* delay() */
45 #include <curl/curl.h>
51 /* Winsock and TPF sockets are not in range [0..FD_SETSIZE-1] */
53 #if defined(USE_WINSOCK) || defined(TPF)
54 #define VERIFY_SOCK(x) do { } while(0)
56 #define VALID_SOCK(s) (((s) >= 0) && ((s) < FD_SETSIZE))
57 #define VERIFY_SOCK(x) do { \
58 if(!VALID_SOCK(x)) { \
59 SET_SOCKERRNO(EINVAL); \
65 /* Convenience local macros */
67 #define elapsed_ms (int)curlx_tvdiff(curlx_tvnow(), initial_tv)
69 #ifdef CURL_ACKNOWLEDGE_EINTR
70 #define error_not_EINTR (1)
72 #define error_not_EINTR (error != EINTR)
76 * Internal function used for waiting a specific amount of ms
77 * in Curl_socket_ready() and Curl_poll() when no file descriptor
78 * is provided to wait on, just being used to delay execution.
79 * WinSock select() and poll() timeout mechanisms need a valid
80 * socket descriptor in a not null file descriptor set to work.
81 * Waiting indefinitely with this function is not allowed, a
82 * zero or negative timeout value will return immediately.
83 * Timeout resolution, accuracy, as well as maximum supported
84 * value is system dependent, neither factor is a citical issue
85 * for the intended use of this function in the library.
86 * On non-DOS and non-Winsock platforms, when compiled with
87 * CURL_ACKNOWLEDGE_EINTR defined, EINTR condition is honored
88 * and function might exit early without awaiting full timeout,
89 * otherwise EINTR will be ignored and full timeout will elapse.
92 * -1 = system call error, invalid timeout value, or interrupted
93 * 0 = specified timeout has elapsed
95 static int wait_ms(int timeout_ms)
97 #if !defined(MSDOS) && !defined(USE_WINSOCK)
98 #ifndef HAVE_POLL_FINE
99 struct timeval pending_tv;
101 struct timeval initial_tv;
110 SET_SOCKERRNO(EINVAL);
115 #elif defined(USE_WINSOCK)
118 pending_ms = timeout_ms;
119 initial_tv = curlx_tvnow();
121 #if defined(HAVE_POLL_FINE)
122 r = poll(NULL, 0, pending_ms);
124 pending_tv.tv_sec = pending_ms / 1000;
125 pending_tv.tv_usec = (pending_ms % 1000) * 1000;
126 r = select(0, NULL, NULL, NULL, &pending_tv);
127 #endif /* HAVE_POLL_FINE */
131 if(error && error_not_EINTR)
133 pending_ms = timeout_ms - elapsed_ms;
137 #endif /* USE_WINSOCK */
144 * This is an internal function used for waiting for read or write
145 * events on a pair of file descriptors. It uses poll() when a fine
146 * poll() is available, in order to avoid limits with FD_SETSIZE,
147 * otherwise select() is used. An error is returned if select() is
148 * being used and a file descriptor is too large for FD_SETSIZE.
149 * A negative timeout value makes this function wait indefinitely,
150 * unles no valid file descriptor is given, when this happens the
151 * negative timeout is ignored and the function times out immediately.
152 * When compiled with CURL_ACKNOWLEDGE_EINTR defined, EINTR condition
153 * is honored and function might exit early without awaiting timeout,
154 * otherwise EINTR will be ignored.
157 * -1 = system call error or fd >= FD_SETSIZE
159 * CURL_CSELECT_IN | CURL_CSELECT_OUT | CURL_CSELECT_ERR
161 int Curl_socket_ready(curl_socket_t readfd, curl_socket_t writefd,
164 #ifdef HAVE_POLL_FINE
165 struct pollfd pfd[2];
168 struct timeval pending_tv;
169 struct timeval *ptimeout;
175 struct timeval initial_tv = {0,0};
181 if((readfd == CURL_SOCKET_BAD) && (writefd == CURL_SOCKET_BAD)) {
182 r = wait_ms(timeout_ms);
186 /* Avoid initial timestamp, avoid curlx_tvnow() call, when elapsed
187 time in this function does not need to be measured. This happens
188 when function is called with a zero timeout or a negative timeout
189 value indicating a blocking call should be performed. */
192 pending_ms = timeout_ms;
193 initial_tv = curlx_tvnow();
196 #ifdef HAVE_POLL_FINE
199 if(readfd != CURL_SOCKET_BAD) {
200 pfd[num].fd = readfd;
201 pfd[num].events = POLLRDNORM|POLLIN|POLLRDBAND|POLLPRI;
202 pfd[num].revents = 0;
205 if(writefd != CURL_SOCKET_BAD) {
206 pfd[num].fd = writefd;
207 pfd[num].events = POLLWRNORM|POLLOUT;
208 pfd[num].revents = 0;
217 r = poll(pfd, num, pending_ms);
221 if(error && error_not_EINTR)
224 pending_ms = timeout_ms - elapsed_ms;
237 if(readfd != CURL_SOCKET_BAD) {
238 if(pfd[num].revents & (POLLRDNORM|POLLIN|POLLERR|POLLHUP))
239 ret |= CURL_CSELECT_IN;
240 if(pfd[num].revents & (POLLRDBAND|POLLPRI|POLLNVAL))
241 ret |= CURL_CSELECT_ERR;
244 if(writefd != CURL_SOCKET_BAD) {
245 if(pfd[num].revents & (POLLWRNORM|POLLOUT))
246 ret |= CURL_CSELECT_OUT;
247 if(pfd[num].revents & (POLLERR|POLLHUP|POLLNVAL))
248 ret |= CURL_CSELECT_ERR;
253 #else /* HAVE_POLL_FINE */
256 maxfd = (curl_socket_t)-1;
259 if(readfd != CURL_SOCKET_BAD) {
261 FD_SET(readfd, &fds_read);
262 FD_SET(readfd, &fds_err);
267 if(writefd != CURL_SOCKET_BAD) {
268 VERIFY_SOCK(writefd);
269 FD_SET(writefd, &fds_write);
270 FD_SET(writefd, &fds_err);
275 ptimeout = (timeout_ms < 0) ? NULL : &pending_tv;
279 pending_tv.tv_sec = pending_ms / 1000;
280 pending_tv.tv_usec = (pending_ms % 1000) * 1000;
282 else if(!timeout_ms) {
283 pending_tv.tv_sec = 0;
284 pending_tv.tv_usec = 0;
286 r = select((int)maxfd + 1, &fds_read, &fds_write, &fds_err, ptimeout);
290 if(error && error_not_EINTR)
293 pending_ms = timeout_ms - elapsed_ms;
305 if(readfd != CURL_SOCKET_BAD) {
306 if(FD_ISSET(readfd, &fds_read))
307 ret |= CURL_CSELECT_IN;
308 if(FD_ISSET(readfd, &fds_err))
309 ret |= CURL_CSELECT_ERR;
311 if(writefd != CURL_SOCKET_BAD) {
312 if(FD_ISSET(writefd, &fds_write))
313 ret |= CURL_CSELECT_OUT;
314 if(FD_ISSET(writefd, &fds_err))
315 ret |= CURL_CSELECT_ERR;
320 #endif /* HAVE_POLL_FINE */
325 * This is a wrapper around poll(). If poll() does not exist, then
326 * select() is used instead. An error is returned if select() is
327 * being used and a file descriptor is too large for FD_SETSIZE.
328 * A negative timeout value makes this function wait indefinitely,
329 * unles no valid file descriptor is given, when this happens the
330 * negative timeout is ignored and the function times out immediately.
331 * When compiled with CURL_ACKNOWLEDGE_EINTR defined, EINTR condition
332 * is honored and function might exit early without awaiting timeout,
333 * otherwise EINTR will be ignored.
336 * -1 = system call error or fd >= FD_SETSIZE
338 * N = number of structures with non zero revent fields
340 int Curl_poll(struct pollfd ufds[], unsigned int nfds, int timeout_ms)
342 #ifndef HAVE_POLL_FINE
343 struct timeval pending_tv;
344 struct timeval *ptimeout;
350 struct timeval initial_tv = {0,0};
351 bool fds_none = TRUE;
358 for (i = 0; i < nfds; i++) {
359 if(ufds[i].fd != CURL_SOCKET_BAD) {
366 r = wait_ms(timeout_ms);
370 /* Avoid initial timestamp, avoid curlx_tvnow() call, when elapsed
371 time in this function does not need to be measured. This happens
372 when function is called with a zero timeout or a negative timeout
373 value indicating a blocking call should be performed. */
376 pending_ms = timeout_ms;
377 initial_tv = curlx_tvnow();
380 #ifdef HAVE_POLL_FINE
387 r = poll(ufds, nfds, pending_ms);
391 if(error && error_not_EINTR)
394 pending_ms = timeout_ms - elapsed_ms;
405 for (i = 0; i < nfds; i++) {
406 if(ufds[i].fd == CURL_SOCKET_BAD)
408 if(ufds[i].revents & POLLHUP)
409 ufds[i].revents |= POLLIN;
410 if(ufds[i].revents & POLLERR)
411 ufds[i].revents |= (POLLIN|POLLOUT);
414 #else /* HAVE_POLL_FINE */
419 maxfd = (curl_socket_t)-1;
421 for (i = 0; i < nfds; i++) {
423 if(ufds[i].fd == CURL_SOCKET_BAD)
425 VERIFY_SOCK(ufds[i].fd);
426 if(ufds[i].events & (POLLIN|POLLOUT|POLLPRI|
427 POLLRDNORM|POLLWRNORM|POLLRDBAND)) {
428 if(ufds[i].fd > maxfd)
430 if(ufds[i].events & (POLLRDNORM|POLLIN))
431 FD_SET(ufds[i].fd, &fds_read);
432 if(ufds[i].events & (POLLWRNORM|POLLOUT))
433 FD_SET(ufds[i].fd, &fds_write);
434 if(ufds[i].events & (POLLRDBAND|POLLPRI))
435 FD_SET(ufds[i].fd, &fds_err);
439 ptimeout = (timeout_ms < 0) ? NULL : &pending_tv;
443 pending_tv.tv_sec = pending_ms / 1000;
444 pending_tv.tv_usec = (pending_ms % 1000) * 1000;
446 else if(!timeout_ms) {
447 pending_tv.tv_sec = 0;
448 pending_tv.tv_usec = 0;
450 r = select((int)maxfd + 1, &fds_read, &fds_write, &fds_err, ptimeout);
454 if(error && error_not_EINTR)
457 pending_ms = timeout_ms - elapsed_ms;
469 for (i = 0; i < nfds; i++) {
471 if(ufds[i].fd == CURL_SOCKET_BAD)
473 if(FD_ISSET(ufds[i].fd, &fds_read))
474 ufds[i].revents |= POLLIN;
475 if(FD_ISSET(ufds[i].fd, &fds_write))
476 ufds[i].revents |= POLLOUT;
477 if(FD_ISSET(ufds[i].fd, &fds_err))
478 ufds[i].revents |= POLLPRI;
479 if(ufds[i].revents != 0)
483 #endif /* HAVE_POLL_FINE */
490 * This is a replacement for select() on the TPF platform.
491 * It is used whenever libcurl calls select().
492 * The call below to tpf_process_signals() is required because
493 * TPF's select calls are not signal interruptible.
495 * Return values are the same as select's.
497 int tpf_select_libcurl(int maxfds, fd_set* reads, fd_set* writes,
498 fd_set* excepts, struct timeval* tv)
502 rc = tpf_select_bsd(maxfds, reads, writes, excepts, tv);
503 tpf_process_signals();