1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 1998 - 2007, 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.
22 ***************************************************************************/
26 #ifdef HAVE_SYS_SELECT_H
27 #include <sys/select.h>
29 #ifdef HAVE_SYS_TIME_H
33 #if !defined(HAVE_SELECT) && !defined(HAVE_POLL_FINE)
34 #error "We can't compile without select() or poll() support."
38 /* BeOS has FD_SET defined in socket.h */
43 #include <dos.h> /* delay() */
46 #include <curl/curl.h>
52 /* Winsock and TPF sockets are not in range [0..FD_SETSIZE-1] */
54 #if defined(USE_WINSOCK) || defined(TPF)
55 #define VERIFY_SOCK(x) do { } while(0)
57 #define VALID_SOCK(s) (((s) >= 0) && ((s) < FD_SETSIZE))
58 #define VERIFY_SOCK(x) do { \
59 if(!VALID_SOCK(x)) { \
60 SET_SOCKERRNO(EINVAL); \
66 /* Convenience local macros */
68 #define elapsed_ms (int)curlx_tvdiff(curlx_tvnow(), initial_tv)
70 #ifdef CURL_ACKNOWLEDGE_EINTR
71 #define error_is_EINTR (error == EINTR)
73 #define error_is_EINTR (0)
77 * Internal function used for waiting a specific amount of ms
78 * in Curl_socket_ready() and Curl_poll() when no file descriptor
79 * is provided to wait on, just being used to delay execution.
80 * WinSock select() and poll() timeout mechanisms need a valid
81 * socket descriptor in a not null file descriptor set to work.
82 * Waiting indefinitely with this function is not allowed, a
83 * zero or negative timeout value will return immediately.
84 * Timeout resolution, accuracy, as well as maximum supported
85 * value is system dependant, neither factor is a citical issue
86 * for the intended use of this function in the library.
87 * On non-DOS and non-Winsock platforms, when compiled with
88 * CURL_ACKNOWLEDGE_EINTR defined, EINTR condition is honored
89 * and function might exit early without awaiting full timeout,
90 * otherwise EINTR will be ignored and full timeout will elapse.
93 * -1 = system call error, invalid timeout value, or interrupted
94 * 0 = specified timeout has elapsed
96 static int wait_ms(int timeout_ms)
98 #if !defined(MSDOS) && !defined(USE_WINSOCK)
99 #ifndef HAVE_POLL_FINE
100 struct timeval pending_tv;
102 struct timeval initial_tv;
111 SET_SOCKERRNO(EINVAL);
116 #elif defined(USE_WINSOCK)
119 pending_ms = timeout_ms;
120 initial_tv = curlx_tvnow();
122 #if defined(HAVE_POLL_FINE)
123 r = poll(NULL, 0, pending_ms);
125 pending_tv.tv_sec = pending_ms / 1000;
126 pending_tv.tv_usec = (pending_ms % 1000) * 1000;
127 r = select(0, NULL, NULL, NULL, &pending_tv);
128 #endif /* HAVE_POLL_FINE */
132 if((error == EINVAL) || error_is_EINTR)
134 pending_ms = timeout_ms - elapsed_ms;
138 #endif /* USE_WINSOCK */
145 * This is an internal function used for waiting for read or write
146 * events on a pair of file descriptors. It uses poll() when a fine
147 * poll() is available, in order to avoid limits with FD_SETSIZE,
148 * otherwise select() is used. An error is returned if select() is
149 * being used and a file descriptor is too large for FD_SETSIZE.
150 * A negative timeout value makes this function wait indefinitely,
151 * unles no valid file descriptor is given, when this happens the
152 * negative timeout is ignored and the function times out immediately.
153 * When compiled with CURL_ACKNOWLEDGE_EINTR defined, EINTR condition
154 * is honored and function might exit early without awaiting timeout,
155 * otherwise EINTR will be ignored.
158 * -1 = system call error or fd >= FD_SETSIZE
160 * CURL_CSELECT_IN | CURL_CSELECT_OUT | CURL_CSELECT_ERR
162 int Curl_socket_ready(curl_socket_t readfd, curl_socket_t writefd,
165 #ifdef HAVE_POLL_FINE
166 struct pollfd pfd[2];
169 struct timeval pending_tv;
170 struct timeval *ptimeout;
176 struct timeval initial_tv = {0,0};
182 if((readfd == CURL_SOCKET_BAD) && (writefd == CURL_SOCKET_BAD)) {
183 r = wait_ms(timeout_ms);
187 /* Avoid initial timestamp, avoid gettimeofday() call, when elapsed
188 time in this function does not need to be measured. This happens
189 when function is called with a zero timeout or a negative timeout
190 value indicating a blocking call should be performed. */
193 pending_ms = timeout_ms;
194 initial_tv = curlx_tvnow();
197 #ifdef HAVE_POLL_FINE
200 if(readfd != CURL_SOCKET_BAD) {
201 pfd[num].fd = readfd;
202 pfd[num].events = POLLRDNORM|POLLIN|POLLRDBAND|POLLPRI;
203 pfd[num].revents = 0;
206 if(writefd != CURL_SOCKET_BAD) {
207 pfd[num].fd = writefd;
208 pfd[num].events = POLLWRNORM|POLLOUT;
209 pfd[num].revents = 0;
218 r = poll(pfd, num, pending_ms);
222 if((error == EINVAL) || error_is_EINTR)
225 pending_ms = timeout_ms - elapsed_ms;
238 if(readfd != CURL_SOCKET_BAD) {
239 if(pfd[num].revents & (POLLRDNORM|POLLIN|POLLERR|POLLHUP))
240 ret |= CURL_CSELECT_IN;
241 if(pfd[num].revents & (POLLRDBAND|POLLPRI|POLLNVAL))
242 ret |= CURL_CSELECT_ERR;
245 if(writefd != CURL_SOCKET_BAD) {
246 if(pfd[num].revents & (POLLWRNORM|POLLOUT))
247 ret |= CURL_CSELECT_OUT;
248 if(pfd[num].revents & (POLLERR|POLLHUP|POLLNVAL))
249 ret |= CURL_CSELECT_ERR;
254 #else /* HAVE_POLL_FINE */
257 maxfd = (curl_socket_t)-1;
260 if(readfd != CURL_SOCKET_BAD) {
262 FD_SET(readfd, &fds_read);
263 FD_SET(readfd, &fds_err);
268 if(writefd != CURL_SOCKET_BAD) {
269 VERIFY_SOCK(writefd);
270 FD_SET(writefd, &fds_write);
271 FD_SET(writefd, &fds_err);
276 ptimeout = (timeout_ms < 0) ? NULL : &pending_tv;
280 pending_tv.tv_sec = pending_ms / 1000;
281 pending_tv.tv_usec = (pending_ms % 1000) * 1000;
283 else if(!timeout_ms) {
284 pending_tv.tv_sec = 0;
285 pending_tv.tv_usec = 0;
287 r = select((int)maxfd + 1, &fds_read, &fds_write, &fds_err, ptimeout);
291 if((error == EINVAL) || (error == EBADF) || error_is_EINTR)
294 pending_ms = timeout_ms - elapsed_ms;
306 if(readfd != CURL_SOCKET_BAD) {
307 if(FD_ISSET(readfd, &fds_read))
308 ret |= CURL_CSELECT_IN;
309 if(FD_ISSET(readfd, &fds_err))
310 ret |= CURL_CSELECT_ERR;
312 if(writefd != CURL_SOCKET_BAD) {
313 if(FD_ISSET(writefd, &fds_write))
314 ret |= CURL_CSELECT_OUT;
315 if(FD_ISSET(writefd, &fds_err))
316 ret |= CURL_CSELECT_ERR;
321 #endif /* HAVE_POLL_FINE */
326 * This is a wrapper around poll(). If poll() does not exist, then
327 * select() is used instead. An error is returned if select() is
328 * being used and a file descriptor is too large for FD_SETSIZE.
329 * A negative timeout value makes this function wait indefinitely,
330 * unles no valid file descriptor is given, when this happens the
331 * negative timeout is ignored and the function times out immediately.
332 * When compiled with CURL_ACKNOWLEDGE_EINTR defined, EINTR condition
333 * is honored and function might exit early without awaiting timeout,
334 * otherwise EINTR will be ignored.
337 * -1 = system call error or fd >= FD_SETSIZE
339 * N = number of structures with non zero revent fields
341 int Curl_poll(struct pollfd ufds[], unsigned int nfds, int timeout_ms)
343 #ifndef HAVE_POLL_FINE
344 struct timeval pending_tv;
345 struct timeval *ptimeout;
351 struct timeval initial_tv = {0,0};
352 bool fds_none = TRUE;
359 for (i = 0; i < nfds; i++) {
360 if(ufds[i].fd != CURL_SOCKET_BAD) {
367 r = wait_ms(timeout_ms);
371 /* Avoid initial timestamp, avoid gettimeofday() call, when elapsed
372 time in this function does not need to be measured. This happens
373 when function is called with a zero timeout or a negative timeout
374 value indicating a blocking call should be performed. */
377 pending_ms = timeout_ms;
378 initial_tv = curlx_tvnow();
381 #ifdef HAVE_POLL_FINE
388 r = poll(ufds, nfds, pending_ms);
392 if((error == EINVAL) || error_is_EINTR)
395 pending_ms = timeout_ms - elapsed_ms;
401 #else /* HAVE_POLL_FINE */
406 maxfd = (curl_socket_t)-1;
408 for (i = 0; i < nfds; i++) {
410 if(ufds[i].fd == CURL_SOCKET_BAD)
412 VERIFY_SOCK(ufds[i].fd);
413 if(ufds[i].events & (POLLIN|POLLOUT|POLLPRI|
414 POLLRDNORM|POLLWRNORM|POLLRDBAND)) {
415 if(ufds[i].fd > maxfd)
417 if(ufds[i].events & (POLLRDNORM|POLLIN))
418 FD_SET(ufds[i].fd, &fds_read);
419 if(ufds[i].events & (POLLWRNORM|POLLOUT))
420 FD_SET(ufds[i].fd, &fds_write);
421 if(ufds[i].events & (POLLRDBAND|POLLPRI))
422 FD_SET(ufds[i].fd, &fds_err);
426 ptimeout = (timeout_ms < 0) ? NULL : &pending_tv;
430 pending_tv.tv_sec = pending_ms / 1000;
431 pending_tv.tv_usec = (pending_ms % 1000) * 1000;
433 else if(!timeout_ms) {
434 pending_tv.tv_sec = 0;
435 pending_tv.tv_usec = 0;
437 r = select((int)maxfd + 1, &fds_read, &fds_write, &fds_err, ptimeout);
441 if((error == EINVAL) || (error == EBADF) || error_is_EINTR)
444 pending_ms = timeout_ms - elapsed_ms;
456 for (i = 0; i < nfds; i++) {
458 if(ufds[i].fd == CURL_SOCKET_BAD)
460 if(FD_ISSET(ufds[i].fd, &fds_read))
461 ufds[i].revents |= POLLIN;
462 if(FD_ISSET(ufds[i].fd, &fds_write))
463 ufds[i].revents |= POLLOUT;
464 if(FD_ISSET(ufds[i].fd, &fds_err))
465 ufds[i].revents |= POLLPRI;
466 if(ufds[i].revents != 0)
470 #endif /* HAVE_POLL_FINE */
477 * This is a replacement for select() on the TPF platform.
478 * It is used whenever libcurl calls select().
479 * The call below to tpf_process_signals() is required because
480 * TPF's select calls are not signal interruptible.
482 * Return values are the same as select's.
484 int tpf_select_libcurl(int maxfds, fd_set* reads, fd_set* writes,
485 fd_set* excepts, struct timeval* tv)
489 rc = tpf_select_bsd(maxfds, reads, writes, excepts, tv);
490 tpf_process_signals();