select.c 16 KB

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