select.c 15 KB

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