MemoryTracking: fix logging of free() calls done where Curl_safefree is called
[platform/upstream/curl.git] / lib / select.c
1 /***************************************************************************
2  *                                  _   _ ____  _
3  *  Project                     ___| | | |  _ \| |
4  *                             / __| | | | |_) | |
5  *                            | (__| |_| |  _ <| |___
6  *                             \___|\___/|_| \_\_____|
7  *
8  * Copyright (C) 1998 - 2011, 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  ***************************************************************************/
22
23 #include "setup.h"
24
25 #ifdef HAVE_SYS_SELECT_H
26 #include <sys/select.h>
27 #endif
28
29 #if !defined(HAVE_SELECT) && !defined(HAVE_POLL_FINE)
30 #error "We can't compile without select() or poll() support."
31 #endif
32
33 #if defined(__BEOS__) && !defined(__HAIKU__)
34 /* BeOS has FD_SET defined in socket.h */
35 #include <socket.h>
36 #endif
37
38 #ifdef MSDOS
39 #include <dos.h>  /* delay() */
40 #endif
41
42 #include <curl/curl.h>
43
44 #include "urldata.h"
45 #include "connect.h"
46 #include "select.h"
47 #include "warnless.h"
48
49 /* Winsock and TPF sockets are not in range [0..FD_SETSIZE-1] */
50
51 #if defined(USE_WINSOCK) || defined(TPF)
52 #define VERIFY_SOCK(x) do { } WHILE_FALSE
53 #else
54 #define VALID_SOCK(s) (((s) >= 0) && ((s) < FD_SETSIZE))
55 #define VERIFY_SOCK(x) do { \
56   if(!VALID_SOCK(x)) { \
57     SET_SOCKERRNO(EINVAL); \
58     return -1; \
59   } \
60 } WHILE_FALSE
61 #endif
62
63 /* Convenience local macros */
64
65 #define elapsed_ms  (int)curlx_tvdiff(curlx_tvnow(), initial_tv)
66
67 #ifdef CURL_ACKNOWLEDGE_EINTR
68 #define error_not_EINTR (1)
69 #else
70 #define error_not_EINTR (error != EINTR)
71 #endif
72
73 /*
74  * Internal function used for waiting a specific amount of ms
75  * in Curl_socket_ready() and Curl_poll() when no file descriptor
76  * is provided to wait on, just being used to delay execution.
77  * WinSock select() and poll() timeout mechanisms need a valid
78  * socket descriptor in a not null file descriptor set to work.
79  * Waiting indefinitely with this function is not allowed, a
80  * zero or negative timeout value will return immediately.
81  * Timeout resolution, accuracy, as well as maximum supported
82  * value is system dependent, neither factor is a citical issue
83  * for the intended use of this function in the library.
84  * On non-DOS and non-Winsock platforms, when compiled with
85  * CURL_ACKNOWLEDGE_EINTR defined, EINTR condition is honored
86  * and function might exit early without awaiting full timeout,
87  * otherwise EINTR will be ignored and full timeout will elapse.
88  *
89  * Return values:
90  *   -1 = system call error, invalid timeout value, or interrupted
91  *    0 = specified timeout has elapsed
92  */
93 int Curl_wait_ms(int timeout_ms)
94 {
95 #if !defined(MSDOS) && !defined(USE_WINSOCK)
96 #ifndef HAVE_POLL_FINE
97   struct timeval pending_tv;
98 #endif
99   struct timeval initial_tv;
100   int pending_ms;
101   int error;
102 #endif
103   int r = 0;
104
105   if(!timeout_ms)
106     return 0;
107   if(timeout_ms < 0) {
108     SET_SOCKERRNO(EINVAL);
109     return -1;
110   }
111 #if defined(MSDOS)
112   delay(timeout_ms);
113 #elif defined(USE_WINSOCK)
114   Sleep(timeout_ms);
115 #else
116   pending_ms = timeout_ms;
117   initial_tv = curlx_tvnow();
118   do {
119 #if defined(HAVE_POLL_FINE)
120     r = poll(NULL, 0, pending_ms);
121 #else
122     pending_tv.tv_sec = pending_ms / 1000;
123     pending_tv.tv_usec = (pending_ms % 1000) * 1000;
124     r = select(0, NULL, NULL, NULL, &pending_tv);
125 #endif /* HAVE_POLL_FINE */
126     if(r != -1)
127       break;
128     error = SOCKERRNO;
129     if(error && error_not_EINTR)
130       break;
131     pending_ms = timeout_ms - elapsed_ms;
132     if(pending_ms <= 0)
133       break;
134   } while(r == -1);
135 #endif /* USE_WINSOCK */
136   if(r)
137     r = -1;
138   return r;
139 }
140
141 /*
142  * This is an internal function used for waiting for read or write
143  * events on a pair of file descriptors.  It uses poll() when a fine
144  * poll() is available, in order to avoid limits with FD_SETSIZE,
145  * otherwise select() is used.  An error is returned if select() is
146  * being used and a file descriptor is too large for FD_SETSIZE.
147  * A negative timeout value makes this function wait indefinitely,
148  * unles no valid file descriptor is given, when this happens the
149  * negative timeout is ignored and the function times out immediately.
150  * When compiled with CURL_ACKNOWLEDGE_EINTR defined, EINTR condition
151  * is honored and function might exit early without awaiting timeout,
152  * otherwise EINTR will be ignored.
153  *
154  * Return values:
155  *   -1 = system call error or fd >= FD_SETSIZE
156  *    0 = timeout
157  *    CURL_CSELECT_IN | CURL_CSELECT_OUT | CURL_CSELECT_ERR
158  */
159 int Curl_socket_ready(curl_socket_t readfd, curl_socket_t writefd,
160                       long timeout_ms)
161 {
162 #ifdef HAVE_POLL_FINE
163   struct pollfd pfd[2];
164   int num;
165 #else
166   struct timeval pending_tv;
167   struct timeval *ptimeout;
168   fd_set fds_read;
169   fd_set fds_write;
170   fd_set fds_err;
171   curl_socket_t maxfd;
172 #endif
173   struct timeval initial_tv = {0,0};
174   int pending_ms = 0;
175   int error;
176   int r;
177   int ret;
178
179   if((readfd == CURL_SOCKET_BAD) && (writefd == CURL_SOCKET_BAD)) {
180     r = Curl_wait_ms((int)timeout_ms);
181     return r;
182   }
183
184   /* Avoid initial timestamp, avoid curlx_tvnow() call, when elapsed
185      time in this function does not need to be measured. This happens
186      when function is called with a zero timeout or a negative timeout
187      value indicating a blocking call should be performed. */
188
189   if(timeout_ms > 0) {
190     pending_ms = (int)timeout_ms;
191     initial_tv = curlx_tvnow();
192   }
193
194 #ifdef HAVE_POLL_FINE
195
196   num = 0;
197   if(readfd != CURL_SOCKET_BAD) {
198     pfd[num].fd = readfd;
199     pfd[num].events = POLLRDNORM|POLLIN|POLLRDBAND|POLLPRI;
200     pfd[num].revents = 0;
201     num++;
202   }
203   if(writefd != CURL_SOCKET_BAD) {
204     pfd[num].fd = writefd;
205     pfd[num].events = POLLWRNORM|POLLOUT;
206     pfd[num].revents = 0;
207     num++;
208   }
209
210   do {
211     if(timeout_ms < 0)
212       pending_ms = -1;
213     else if(!timeout_ms)
214       pending_ms = 0;
215     r = poll(pfd, num, pending_ms);
216     if(r != -1)
217       break;
218     error = SOCKERRNO;
219     if(error && error_not_EINTR)
220       break;
221     if(timeout_ms > 0) {
222       pending_ms = (int)(timeout_ms - elapsed_ms);
223       if(pending_ms <= 0)
224         break;
225     }
226   } while(r == -1);
227
228   if(r < 0)
229     return -1;
230   if(r == 0)
231     return 0;
232
233   ret = 0;
234   num = 0;
235   if(readfd != CURL_SOCKET_BAD) {
236     if(pfd[num].revents & (POLLRDNORM|POLLIN|POLLERR|POLLHUP))
237       ret |= CURL_CSELECT_IN;
238     if(pfd[num].revents & (POLLRDBAND|POLLPRI|POLLNVAL))
239       ret |= CURL_CSELECT_ERR;
240     num++;
241   }
242   if(writefd != CURL_SOCKET_BAD) {
243     if(pfd[num].revents & (POLLWRNORM|POLLOUT))
244       ret |= CURL_CSELECT_OUT;
245     if(pfd[num].revents & (POLLERR|POLLHUP|POLLNVAL))
246       ret |= CURL_CSELECT_ERR;
247   }
248
249   return ret;
250
251 #else  /* HAVE_POLL_FINE */
252
253   FD_ZERO(&fds_err);
254   maxfd = (curl_socket_t)-1;
255
256   FD_ZERO(&fds_read);
257   if(readfd != CURL_SOCKET_BAD) {
258     VERIFY_SOCK(readfd);
259     FD_SET(readfd, &fds_read);
260     FD_SET(readfd, &fds_err);
261     maxfd = readfd;
262   }
263
264   FD_ZERO(&fds_write);
265   if(writefd != CURL_SOCKET_BAD) {
266     VERIFY_SOCK(writefd);
267     FD_SET(writefd, &fds_write);
268     FD_SET(writefd, &fds_err);
269     if(writefd > maxfd)
270       maxfd = writefd;
271   }
272
273   ptimeout = (timeout_ms < 0) ? NULL : &pending_tv;
274
275   do {
276     if(timeout_ms > 0) {
277       pending_tv.tv_sec = pending_ms / 1000;
278       pending_tv.tv_usec = (pending_ms % 1000) * 1000;
279     }
280     else if(!timeout_ms) {
281       pending_tv.tv_sec = 0;
282       pending_tv.tv_usec = 0;
283     }
284     r = select((int)maxfd + 1, &fds_read, &fds_write, &fds_err, ptimeout);
285     if(r != -1)
286       break;
287     error = SOCKERRNO;
288     if(error && error_not_EINTR)
289       break;
290     if(timeout_ms > 0) {
291       pending_ms = timeout_ms - elapsed_ms;
292       if(pending_ms <= 0)
293         break;
294     }
295   } while(r == -1);
296
297   if(r < 0)
298     return -1;
299   if(r == 0)
300     return 0;
301
302   ret = 0;
303   if(readfd != CURL_SOCKET_BAD) {
304     if(FD_ISSET(readfd, &fds_read))
305       ret |= CURL_CSELECT_IN;
306     if(FD_ISSET(readfd, &fds_err))
307       ret |= CURL_CSELECT_ERR;
308   }
309   if(writefd != CURL_SOCKET_BAD) {
310     if(FD_ISSET(writefd, &fds_write))
311       ret |= CURL_CSELECT_OUT;
312     if(FD_ISSET(writefd, &fds_err))
313       ret |= CURL_CSELECT_ERR;
314   }
315
316   return ret;
317
318 #endif  /* HAVE_POLL_FINE */
319
320 }
321
322 /*
323  * This is a wrapper around poll().  If poll() does not exist, then
324  * select() is used instead.  An error is returned if select() is
325  * being used and a file descriptor is too large for FD_SETSIZE.
326  * A negative timeout value makes this function wait indefinitely,
327  * unles no valid file descriptor is given, when this happens the
328  * negative timeout is ignored and the function times out immediately.
329  * When compiled with CURL_ACKNOWLEDGE_EINTR defined, EINTR condition
330  * is honored and function might exit early without awaiting timeout,
331  * otherwise EINTR will be ignored.
332  *
333  * Return values:
334  *   -1 = system call error or fd >= FD_SETSIZE
335  *    0 = timeout
336  *    N = number of structures with non zero revent fields
337  */
338 int Curl_poll(struct pollfd ufds[], unsigned int nfds, int timeout_ms)
339 {
340 #ifndef HAVE_POLL_FINE
341   struct timeval pending_tv;
342   struct timeval *ptimeout;
343   fd_set fds_read;
344   fd_set fds_write;
345   fd_set fds_err;
346   curl_socket_t maxfd;
347 #endif
348   struct timeval initial_tv = {0,0};
349   bool fds_none = TRUE;
350   unsigned int i;
351   int pending_ms = 0;
352   int error;
353   int r;
354
355   if(ufds) {
356     for(i = 0; i < nfds; i++) {
357       if(ufds[i].fd != CURL_SOCKET_BAD) {
358         fds_none = FALSE;
359         break;
360       }
361     }
362   }
363   if(fds_none) {
364     r = Curl_wait_ms(timeout_ms);
365     return r;
366   }
367
368   /* Avoid initial timestamp, avoid curlx_tvnow() call, when elapsed
369      time in this function does not need to be measured. This happens
370      when function is called with a zero timeout or a negative timeout
371      value indicating a blocking call should be performed. */
372
373   if(timeout_ms > 0) {
374     pending_ms = timeout_ms;
375     initial_tv = curlx_tvnow();
376   }
377
378 #ifdef HAVE_POLL_FINE
379
380   do {
381     if(timeout_ms < 0)
382       pending_ms = -1;
383     else if(!timeout_ms)
384       pending_ms = 0;
385     r = poll(ufds, nfds, pending_ms);
386     if(r != -1)
387       break;
388     error = SOCKERRNO;
389     if(error && error_not_EINTR)
390       break;
391     if(timeout_ms > 0) {
392       pending_ms = timeout_ms - elapsed_ms;
393       if(pending_ms <= 0)
394         break;
395     }
396   } while(r == -1);
397
398   if(r < 0)
399     return -1;
400   if(r == 0)
401     return 0;
402
403   for(i = 0; i < nfds; i++) {
404     if(ufds[i].fd == CURL_SOCKET_BAD)
405       continue;
406     if(ufds[i].revents & POLLHUP)
407       ufds[i].revents |= POLLIN;
408     if(ufds[i].revents & POLLERR)
409       ufds[i].revents |= (POLLIN|POLLOUT);
410   }
411
412 #else  /* HAVE_POLL_FINE */
413
414   FD_ZERO(&fds_read);
415   FD_ZERO(&fds_write);
416   FD_ZERO(&fds_err);
417   maxfd = (curl_socket_t)-1;
418
419   for(i = 0; i < nfds; i++) {
420     ufds[i].revents = 0;
421     if(ufds[i].fd == CURL_SOCKET_BAD)
422       continue;
423     VERIFY_SOCK(ufds[i].fd);
424     if(ufds[i].events & (POLLIN|POLLOUT|POLLPRI|
425                           POLLRDNORM|POLLWRNORM|POLLRDBAND)) {
426       if(ufds[i].fd > maxfd)
427         maxfd = ufds[i].fd;
428       if(ufds[i].events & (POLLRDNORM|POLLIN))
429         FD_SET(ufds[i].fd, &fds_read);
430       if(ufds[i].events & (POLLWRNORM|POLLOUT))
431         FD_SET(ufds[i].fd, &fds_write);
432       if(ufds[i].events & (POLLRDBAND|POLLPRI))
433         FD_SET(ufds[i].fd, &fds_err);
434     }
435   }
436
437   ptimeout = (timeout_ms < 0) ? NULL : &pending_tv;
438
439   do {
440     if(timeout_ms > 0) {
441       pending_tv.tv_sec = pending_ms / 1000;
442       pending_tv.tv_usec = (pending_ms % 1000) * 1000;
443     }
444     else if(!timeout_ms) {
445       pending_tv.tv_sec = 0;
446       pending_tv.tv_usec = 0;
447     }
448     r = select((int)maxfd + 1, &fds_read, &fds_write, &fds_err, ptimeout);
449     if(r != -1)
450       break;
451     error = SOCKERRNO;
452     if(error && error_not_EINTR)
453       break;
454     if(timeout_ms > 0) {
455       pending_ms = timeout_ms - elapsed_ms;
456       if(pending_ms <= 0)
457         break;
458     }
459   } while(r == -1);
460
461   if(r < 0)
462     return -1;
463   if(r == 0)
464     return 0;
465
466   r = 0;
467   for(i = 0; i < nfds; i++) {
468     ufds[i].revents = 0;
469     if(ufds[i].fd == CURL_SOCKET_BAD)
470       continue;
471     if(FD_ISSET(ufds[i].fd, &fds_read))
472       ufds[i].revents |= POLLIN;
473     if(FD_ISSET(ufds[i].fd, &fds_write))
474       ufds[i].revents |= POLLOUT;
475     if(FD_ISSET(ufds[i].fd, &fds_err))
476       ufds[i].revents |= POLLPRI;
477     if(ufds[i].revents != 0)
478       r++;
479   }
480
481 #endif  /* HAVE_POLL_FINE */
482
483   return r;
484 }
485
486 #ifdef TPF
487 /*
488  * This is a replacement for select() on the TPF platform.
489  * It is used whenever libcurl calls select().
490  * The call below to tpf_process_signals() is required because
491  * TPF's select calls are not signal interruptible.
492  *
493  * Return values are the same as select's.
494  */
495 int tpf_select_libcurl(int maxfds, fd_set* reads, fd_set* writes,
496                        fd_set* excepts, struct timeval* tv)
497 {
498    int rc;
499
500    rc = tpf_select_bsd(maxfds, reads, writes, excepts, tv);
501    tpf_process_signals();
502    return(rc);
503 }
504 #endif /* TPF */