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 ***************************************************************************/
28 #ifdef HAVE_SYS_SELECT_H
29 #include <sys/select.h>
31 #ifdef HAVE_SYS_TIME_H
36 #error "We can't compile without select() support!"
40 /* BeOS has FD_SET defined in socket.h */
45 #include <dos.h> /* delay() */
48 #include <curl/curl.h>
56 # define EINTR WSAEINTR
58 # define EINVAL WSAEINVAL
61 /* Winsock and TPF sockets are not in range [0..FD_SETSIZE-1] */
63 #if defined(USE_WINSOCK) || defined(TPF)
64 #define VERIFY_SOCK(x) do { } while (0)
66 #define VALID_SOCK(s) (((s) >= 0) && ((s) < FD_SETSIZE))
67 #define VERIFY_SOCK(x) do { \
68 if(!VALID_SOCK(x)) { \
69 SET_SOCKERRNO(EINVAL); \
75 /* Convenience local macros */
77 #define elapsed_ms (int)curlx_tvdiff(curlx_tvnow(), initial_tv)
79 #ifdef CURL_ACKNOWLEDGE_EINTR
80 #define sockerrno_not_EINTR (SOCKERRNO != EINTR)
82 #define sockerrno_not_EINTR (1)
86 * Internal function used for waiting a specific amount of ms
87 * in Curl_select() and Curl_poll() when no file descriptor is
88 * provided to wait on, just being used to delay execution.
89 * WinSock select() and poll() timeout mechanisms need a valid
90 * socket descriptor in a not null file descriptor set to work.
91 * Waiting indefinitely with this function is not allowed, a
92 * zero or negative timeout value will return immediately.
93 * Timeout resolution, accuracy, as well as maximum supported
94 * value is system dependant, neither factor is a citical issue
95 * for the intended use of this function in the library.
96 * On non-DOS and non-Winsock platforms, when compiled with
97 * CURL_ACKNOWLEDGE_EINTR defined, EINTR condition is honored
98 * and function might exit early without awaiting full timeout,
99 * otherwise EINTR will be ignored and full timeout will elapse.
102 * -1 = system call error, invalid timeout value, or interrupted
103 * 0 = specified timeout has elapsed
105 static int wait_ms(int timeout_ms)
107 #if !defined(__MSDOS__) && !defined(USE_WINSOCK)
108 #ifndef HAVE_POLL_FINE
109 struct timeval pending_tv;
111 struct timeval initial_tv;
118 if (timeout_ms < 0) {
119 SET_SOCKERRNO(EINVAL);
122 #if defined(__MSDOS__)
124 #elif defined(USE_WINSOCK)
127 pending_ms = timeout_ms;
128 initial_tv = curlx_tvnow();
130 #if defined(HAVE_POLL_FINE)
131 r = poll(NULL, 0, pending_ms);
133 pending_tv.tv_sec = pending_ms / 1000;
134 pending_tv.tv_usec = (pending_ms % 1000) * 1000;
135 r = select(0, NULL, NULL, NULL, &pending_tv);
136 #endif /* HAVE_POLL_FINE */
137 } while ((r == -1) && (SOCKERRNO != EINVAL) && sockerrno_not_EINTR &&
138 ((pending_ms = timeout_ms - elapsed_ms) > 0));
139 #endif /* USE_WINSOCK */
146 * This is an internal function used for waiting for read or write
147 * events on a pair of file descriptors. It uses poll() when a fine
148 * poll() is available, in order to avoid limits with FD_SETSIZE,
149 * otherwise select() is used. An error is returned if select() is
150 * being used and a file descriptor is too large for FD_SETSIZE.
151 * A negative timeout value makes this function wait indefinitely,
152 * unles no valid file descriptor is given, when this happens the
153 * negative timeout is ignored and the function times out immediately.
154 * When compiled with CURL_ACKNOWLEDGE_EINTR defined, EINTR condition
155 * is honored and function might exit early without awaiting timeout,
156 * otherwise EINTR will be ignored.
159 * -1 = system call error or fd >= FD_SETSIZE
161 * CSELECT_IN | CSELECT_OUT | CSELECT_ERR
163 int Curl_select(curl_socket_t readfd, curl_socket_t writefd, int timeout_ms)
165 #ifdef HAVE_POLL_FINE
166 struct pollfd pfd[2];
169 struct timeval pending_tv;
170 struct timeval *ptimeout;
176 struct timeval initial_tv;
181 if((readfd == CURL_SOCKET_BAD) && (writefd == CURL_SOCKET_BAD)) {
182 r = wait_ms(timeout_ms);
186 pending_ms = timeout_ms;
187 initial_tv = curlx_tvnow();
189 #ifdef HAVE_POLL_FINE
192 if (readfd != CURL_SOCKET_BAD) {
193 pfd[num].fd = readfd;
194 pfd[num].events = POLLIN;
195 pfd[num].revents = 0;
198 if (writefd != CURL_SOCKET_BAD) {
199 pfd[num].fd = writefd;
200 pfd[num].events = POLLOUT;
201 pfd[num].revents = 0;
208 r = poll(pfd, num, pending_ms);
209 } while ((r == -1) && (SOCKERRNO != EINVAL) && sockerrno_not_EINTR &&
210 ((timeout_ms < 0) || ((pending_ms = timeout_ms - elapsed_ms) > 0)));
219 if (readfd != CURL_SOCKET_BAD) {
220 if (pfd[num].revents & (POLLIN|POLLHUP))
222 if (pfd[num].revents & POLLERR) {
224 /* Cygwin 1.5.21 needs this hack to pass test 160 */
225 if (ERRNO == EINPROGRESS)
233 if (writefd != CURL_SOCKET_BAD) {
234 if (pfd[num].revents & POLLOUT)
236 if (pfd[num].revents & (POLLERR|POLLHUP))
242 #else /* HAVE_POLL_FINE */
245 maxfd = (curl_socket_t)-1;
248 if (readfd != CURL_SOCKET_BAD) {
250 FD_SET(readfd, &fds_read);
251 FD_SET(readfd, &fds_err);
256 if (writefd != CURL_SOCKET_BAD) {
257 VERIFY_SOCK(writefd);
258 FD_SET(writefd, &fds_write);
259 FD_SET(writefd, &fds_err);
264 ptimeout = (timeout_ms < 0) ? NULL : &pending_tv;
268 pending_tv.tv_sec = pending_ms / 1000;
269 pending_tv.tv_usec = (pending_ms % 1000) * 1000;
271 r = select((int)maxfd + 1, &fds_read, &fds_write, &fds_err, ptimeout);
272 } while ((r == -1) && (SOCKERRNO != EINVAL) && sockerrno_not_EINTR &&
273 ((timeout_ms < 0) || ((pending_ms = timeout_ms - elapsed_ms) > 0)));
281 if (readfd != CURL_SOCKET_BAD) {
282 if (FD_ISSET(readfd, &fds_read))
284 if (FD_ISSET(readfd, &fds_err))
287 if (writefd != CURL_SOCKET_BAD) {
288 if (FD_ISSET(writefd, &fds_write))
290 if (FD_ISSET(writefd, &fds_err))
296 #endif /* HAVE_POLL_FINE */
301 * This is a wrapper around poll(). If poll() does not exist, then
302 * select() is used instead. An error is returned if select() is
303 * being used and a file descriptor is too large for FD_SETSIZE.
304 * A negative timeout value makes this function wait indefinitely,
305 * unles no valid file descriptor is given, when this happens the
306 * negative timeout is ignored and the function times out immediately.
307 * When compiled with CURL_ACKNOWLEDGE_EINTR defined, EINTR condition
308 * is honored and function might exit early without awaiting timeout,
309 * otherwise EINTR will be ignored.
312 * -1 = system call error or fd >= FD_SETSIZE
314 * N = number of structures with non zero revent fields
316 int Curl_poll(struct pollfd ufds[], unsigned int nfds, int timeout_ms)
318 #ifndef HAVE_POLL_FINE
319 struct timeval pending_tv;
320 struct timeval *ptimeout;
326 struct timeval initial_tv;
327 bool fds_none = TRUE;
333 for (i = 0; i < nfds; i++) {
334 if (ufds[i].fd != CURL_SOCKET_BAD) {
341 r = wait_ms(timeout_ms);
345 pending_ms = timeout_ms;
346 initial_tv = curlx_tvnow();
348 #ifdef HAVE_POLL_FINE
353 r = poll(ufds, nfds, pending_ms);
354 } while ((r == -1) && (SOCKERRNO != EINVAL) && sockerrno_not_EINTR &&
355 ((timeout_ms < 0) || ((pending_ms = timeout_ms - elapsed_ms) > 0)));
357 #else /* HAVE_POLL_FINE */
362 maxfd = (curl_socket_t)-1;
364 for (i = 0; i < nfds; i++) {
366 if (ufds[i].fd == CURL_SOCKET_BAD)
368 VERIFY_SOCK(ufds[i].fd);
369 if (ufds[i].events & (POLLIN|POLLOUT|POLLERR)) {
370 if (ufds[i].fd > maxfd)
372 if (ufds[i].events & POLLIN)
373 FD_SET(ufds[i].fd, &fds_read);
374 if (ufds[i].events & POLLOUT)
375 FD_SET(ufds[i].fd, &fds_write);
376 if (ufds[i].events & POLLERR)
377 FD_SET(ufds[i].fd, &fds_err);
381 ptimeout = (timeout_ms < 0) ? NULL : &pending_tv;
385 pending_tv.tv_sec = pending_ms / 1000;
386 pending_tv.tv_usec = (pending_ms % 1000) * 1000;
388 r = select((int)maxfd + 1, &fds_read, &fds_write, &fds_err, ptimeout);
389 } while ((r == -1) && (SOCKERRNO != EINVAL) && sockerrno_not_EINTR &&
390 ((timeout_ms < 0) || ((pending_ms = timeout_ms - elapsed_ms) > 0)));
398 for (i = 0; i < nfds; i++) {
400 if (ufds[i].fd == CURL_SOCKET_BAD)
402 if (FD_ISSET(ufds[i].fd, &fds_read))
403 ufds[i].revents |= POLLIN;
404 if (FD_ISSET(ufds[i].fd, &fds_write))
405 ufds[i].revents |= POLLOUT;
406 if (FD_ISSET(ufds[i].fd, &fds_err))
407 ufds[i].revents |= POLLERR;
408 if (ufds[i].revents != 0)
412 #endif /* HAVE_POLL_FINE */
419 * This is a replacement for select() on the TPF platform.
420 * It is used whenever libcurl calls select().
421 * The call below to tpf_process_signals() is required because
422 * TPF's select calls are not signal interruptible.
424 * Return values are the same as select's.
426 int tpf_select_libcurl(int maxfds, fd_set* reads, fd_set* writes,
427 fd_set* excepts, struct timeval* tv)
431 rc = tpf_select_bsd(maxfds, reads, writes, excepts, tv);
432 tpf_process_signals();