Add a couple of local macros to improve code readability.
[platform/upstream/curl.git] / lib / select.c
1 /***************************************************************************
2  *                                  _   _ ____  _
3  *  Project                     ___| | | |  _ \| |
4  *                             / __| | | | |_) | |
5  *                            | (__| |_| |  _ <| |___
6  *                             \___|\___/|_| \_\_____|
7  *
8  * Copyright (C) 1998 - 2007, Daniel Stenberg, <daniel@haxx.se>, et al.
9  *
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.
13  *
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.
17  *
18  * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19  * KIND, either express or implied.
20  *
21  * $Id$
22  ***************************************************************************/
23
24 #include "setup.h"
25
26 #include <errno.h>
27
28 #ifdef HAVE_SYS_SELECT_H
29 #include <sys/select.h>
30 #endif
31 #ifdef HAVE_SYS_TIME_H
32 #include <sys/time.h>
33 #endif
34
35 #ifndef HAVE_SELECT
36 #error "We can't compile without select() support!"
37 #endif
38
39 #ifdef __BEOS__
40 /* BeOS has FD_SET defined in socket.h */
41 #include <socket.h>
42 #endif
43
44 #ifdef __MSDOS__
45 #include <dos.h>  /* delay() */
46 #endif
47
48 #include <curl/curl.h>
49
50 #include "urldata.h"
51 #include "connect.h"
52 #include "select.h"
53
54 #ifdef USE_WINSOCK
55 #  undef  EINTR
56 #  define EINTR  WSAEINTR
57 #  undef  EINVAL
58 #  define EINVAL WSAEINVAL
59 #endif
60
61 /* Winsock and TPF sockets are not in range [0..FD_SETSIZE-1] */
62
63 #if defined(USE_WINSOCK) || defined(TPF)
64 #define VERIFY_SOCK(x) do { } while (0)
65 #else
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); \
70     return -1; \
71   } \
72 } while(0)
73 #endif
74
75 /* Convenience local macros */
76
77 #define elapsed_ms  (int)curlx_tvdiff(curlx_tvnow(), initial_tv)
78
79 #ifdef CURL_ACKNOWLEDGE_EINTR
80 #define sockerrno_not_EINTR  (SOCKERRNO != EINTR)
81 #else
82 #define sockerrno_not_EINTR  (1)
83 #endif
84
85 /*
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.
100  *
101  * Return values:
102  *   -1 = system call error, invalid timeout value, or interrupted
103  *    0 = specified timeout has elapsed
104  */
105 static int wait_ms(int timeout_ms)
106 {
107 #if !defined(__MSDOS__) && !defined(USE_WINSOCK)
108 #ifndef HAVE_POLL_FINE
109   struct timeval pending_tv;
110 #endif
111   struct timeval initial_tv;
112   int pending_ms;
113 #endif
114   int r = 0;
115
116   if (!timeout_ms)
117     return 0;
118   if (timeout_ms < 0) {
119     SET_SOCKERRNO(EINVAL);
120     return -1;
121   }
122 #if defined(__MSDOS__)
123   delay(timeout_ms);
124 #elif defined(USE_WINSOCK)
125   Sleep(timeout_ms);
126 #else
127   pending_ms = timeout_ms;
128   initial_tv = curlx_tvnow();
129   do {
130 #if defined(HAVE_POLL_FINE)
131     r = poll(NULL, 0, pending_ms);
132 #else
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 */
140   if (r)
141     r = -1;
142   return r;
143 }
144
145 /*
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.
157  *
158  * Return values:
159  *   -1 = system call error or fd >= FD_SETSIZE
160  *    0 = timeout
161  *    CSELECT_IN | CSELECT_OUT | CSELECT_ERR
162  */
163 int Curl_select(curl_socket_t readfd, curl_socket_t writefd, int timeout_ms)
164 {
165 #ifdef HAVE_POLL_FINE
166   struct pollfd pfd[2];
167   int num;
168 #else
169   struct timeval pending_tv;
170   struct timeval *ptimeout;
171   fd_set fds_read;
172   fd_set fds_write;
173   fd_set fds_err;
174   curl_socket_t maxfd;
175 #endif
176   struct timeval initial_tv;
177   int pending_ms;
178   int r;
179   int ret;
180
181   if((readfd == CURL_SOCKET_BAD) && (writefd == CURL_SOCKET_BAD)) {
182     r = wait_ms(timeout_ms);
183     return r;
184   }
185
186   pending_ms = timeout_ms;
187   initial_tv = curlx_tvnow();
188
189 #ifdef HAVE_POLL_FINE
190
191   num = 0;
192   if (readfd != CURL_SOCKET_BAD) {
193     pfd[num].fd = readfd;
194     pfd[num].events = POLLIN;
195     pfd[num].revents = 0;
196     num++;
197   }
198   if (writefd != CURL_SOCKET_BAD) {
199     pfd[num].fd = writefd;
200     pfd[num].events = POLLOUT;
201     pfd[num].revents = 0;
202     num++;
203   }
204
205   do {
206     if (timeout_ms < 0)
207       pending_ms = -1;
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)));
211
212   if (r < 0)
213     return -1;
214   if (r == 0)
215     return 0;
216
217   ret = 0;
218   num = 0;
219   if (readfd != CURL_SOCKET_BAD) {
220     if (pfd[num].revents & (POLLIN|POLLHUP))
221       ret |= CSELECT_IN;
222     if (pfd[num].revents & POLLERR) {
223 #ifdef __CYGWIN__
224       /* Cygwin 1.5.21 needs this hack to pass test 160 */
225       if (ERRNO == EINPROGRESS)
226         ret |= CSELECT_IN;
227       else
228 #endif
229         ret |= CSELECT_ERR;
230     }
231     num++;
232   }
233   if (writefd != CURL_SOCKET_BAD) {
234     if (pfd[num].revents & POLLOUT)
235       ret |= CSELECT_OUT;
236     if (pfd[num].revents & (POLLERR|POLLHUP))
237       ret |= CSELECT_ERR;
238   }
239
240   return ret;
241
242 #else  /* HAVE_POLL_FINE */
243
244   FD_ZERO(&fds_err);
245   maxfd = (curl_socket_t)-1;
246
247   FD_ZERO(&fds_read);
248   if (readfd != CURL_SOCKET_BAD) {
249     VERIFY_SOCK(readfd);
250     FD_SET(readfd, &fds_read);
251     FD_SET(readfd, &fds_err);
252     maxfd = readfd;
253   }
254
255   FD_ZERO(&fds_write);
256   if (writefd != CURL_SOCKET_BAD) {
257     VERIFY_SOCK(writefd);
258     FD_SET(writefd, &fds_write);
259     FD_SET(writefd, &fds_err);
260     if (writefd > maxfd)
261       maxfd = writefd;
262   }
263
264   ptimeout = (timeout_ms < 0) ? NULL : &pending_tv;
265
266   do {
267     if (ptimeout) {
268       pending_tv.tv_sec = pending_ms / 1000;
269       pending_tv.tv_usec = (pending_ms % 1000) * 1000;
270     }
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)));
274
275   if (r < 0)
276     return -1;
277   if (r == 0)
278     return 0;
279
280   ret = 0;
281   if (readfd != CURL_SOCKET_BAD) {
282     if (FD_ISSET(readfd, &fds_read))
283       ret |= CSELECT_IN;
284     if (FD_ISSET(readfd, &fds_err))
285       ret |= CSELECT_ERR;
286   }
287   if (writefd != CURL_SOCKET_BAD) {
288     if (FD_ISSET(writefd, &fds_write))
289       ret |= CSELECT_OUT;
290     if (FD_ISSET(writefd, &fds_err))
291       ret |= CSELECT_ERR;
292   }
293
294   return ret;
295
296 #endif  /* HAVE_POLL_FINE */
297
298 }
299
300 /*
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.
310  *
311  * Return values:
312  *   -1 = system call error or fd >= FD_SETSIZE
313  *    0 = timeout
314  *    N = number of structures with non zero revent fields
315  */
316 int Curl_poll(struct pollfd ufds[], unsigned int nfds, int timeout_ms)
317 {
318 #ifndef HAVE_POLL_FINE
319   struct timeval pending_tv;
320   struct timeval *ptimeout;
321   fd_set fds_read;
322   fd_set fds_write;
323   fd_set fds_err;
324   curl_socket_t maxfd;
325 #endif
326   struct timeval initial_tv;
327   bool fds_none = TRUE;
328   unsigned int i;
329   int pending_ms;
330   int r;
331
332   if (ufds) {
333     for (i = 0; i < nfds; i++) {
334       if (ufds[i].fd != CURL_SOCKET_BAD) {
335         fds_none = FALSE;
336         break;
337       }
338     }
339   }
340   if (fds_none) {
341     r = wait_ms(timeout_ms);
342     return r;
343   }
344
345   pending_ms = timeout_ms;
346   initial_tv = curlx_tvnow();
347
348 #ifdef HAVE_POLL_FINE
349
350   do {
351     if (timeout_ms < 0)
352       pending_ms = -1;
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)));
356
357 #else  /* HAVE_POLL_FINE */
358
359   FD_ZERO(&fds_read);
360   FD_ZERO(&fds_write);
361   FD_ZERO(&fds_err);
362   maxfd = (curl_socket_t)-1;
363
364   for (i = 0; i < nfds; i++) {
365     ufds[i].revents = 0;
366     if (ufds[i].fd == CURL_SOCKET_BAD)
367       continue;
368     VERIFY_SOCK(ufds[i].fd);
369     if (ufds[i].events & (POLLIN|POLLOUT|POLLERR)) {
370       if (ufds[i].fd > maxfd)
371         maxfd = ufds[i].fd;
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);
378     }
379   }
380
381   ptimeout = (timeout_ms < 0) ? NULL : &pending_tv;
382
383   do {
384     if (ptimeout) {
385       pending_tv.tv_sec = pending_ms / 1000;
386       pending_tv.tv_usec = (pending_ms % 1000) * 1000;
387     }
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)));
391
392   if (r < 0)
393     return -1;
394   if (r == 0)
395     return 0;
396
397   r = 0;
398   for (i = 0; i < nfds; i++) {
399     ufds[i].revents = 0;
400     if (ufds[i].fd == CURL_SOCKET_BAD)
401       continue;
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)
409       r++;
410   }
411
412 #endif  /* HAVE_POLL_FINE */
413
414   return r;
415 }
416
417 #ifdef TPF
418 /*
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.
423  *
424  * Return values are the same as select's.
425  */
426 int tpf_select_libcurl(int maxfds, fd_set* reads, fd_set* writes,
427                        fd_set* excepts, struct timeval* tv)
428 {
429    int rc;
430
431    rc = tpf_select_bsd(maxfds, reads, writes, excepts, tv);
432    tpf_process_signals();
433    return(rc);
434 }
435 #endif /* TPF */