select.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2017, 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 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)Curl_timediff(Curl_now(), 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 curltime initial_tv;
  71. int pending_ms;
  72. #endif
  73. int r = 0;
  74. if(!timeout_ms)
  75. return 0;
  76. if(timeout_ms < 0) {
  77. SET_SOCKERRNO(EINVAL);
  78. return -1;
  79. }
  80. #if defined(MSDOS)
  81. delay(timeout_ms);
  82. #elif defined(USE_WINSOCK)
  83. Sleep(timeout_ms);
  84. #else
  85. pending_ms = timeout_ms;
  86. initial_tv = Curl_now();
  87. do {
  88. int error;
  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. * unless 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 curltime initial_tv = {0, 0};
  149. int pending_ms = 0;
  150. int r;
  151. int ret;
  152. #if SIZEOF_TIME_T != SIZEOF_INT
  153. /* wrap-around precaution */
  154. if(timeout_ms >= INT_MAX)
  155. timeout_ms = INT_MAX;
  156. #endif
  157. if((readfd0 == CURL_SOCKET_BAD) && (readfd1 == CURL_SOCKET_BAD) &&
  158. (writefd == CURL_SOCKET_BAD)) {
  159. /* no sockets, just wait */
  160. r = Curl_wait_ms((int)timeout_ms);
  161. return r;
  162. }
  163. /* Avoid initial timestamp, avoid Curl_now() call, when elapsed
  164. time in this function does not need to be measured. This happens
  165. when function is called with a zero timeout or a negative timeout
  166. value indicating a blocking call should be performed. */
  167. if(timeout_ms > 0) {
  168. pending_ms = (int)timeout_ms;
  169. initial_tv = Curl_now();
  170. }
  171. #ifdef HAVE_POLL_FINE
  172. num = 0;
  173. if(readfd0 != CURL_SOCKET_BAD) {
  174. pfd[num].fd = readfd0;
  175. pfd[num].events = POLLRDNORM|POLLIN|POLLRDBAND|POLLPRI;
  176. pfd[num].revents = 0;
  177. num++;
  178. }
  179. if(readfd1 != CURL_SOCKET_BAD) {
  180. pfd[num].fd = readfd1;
  181. pfd[num].events = POLLRDNORM|POLLIN|POLLRDBAND|POLLPRI;
  182. pfd[num].revents = 0;
  183. num++;
  184. }
  185. if(writefd != CURL_SOCKET_BAD) {
  186. pfd[num].fd = writefd;
  187. pfd[num].events = POLLWRNORM|POLLOUT;
  188. pfd[num].revents = 0;
  189. num++;
  190. }
  191. do {
  192. int error;
  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. int error;
  266. if(timeout_ms > 0) {
  267. pending_tv.tv_sec = pending_ms / 1000;
  268. pending_tv.tv_usec = (pending_ms % 1000) * 1000;
  269. }
  270. else if(!timeout_ms) {
  271. pending_tv.tv_sec = 0;
  272. pending_tv.tv_usec = 0;
  273. }
  274. /* WinSock select() must not be called with an fd_set that contains zero
  275. fd flags, or it will return WSAEINVAL. But, it also can't be called
  276. with no fd_sets at all! From the documentation:
  277. Any two of the parameters, readfds, writefds, or exceptfds, can be
  278. given as null. At least one must be non-null, and any non-null
  279. descriptor set must contain at least one handle to a socket.
  280. We know that we have at least one bit set in at least two fd_sets in
  281. this case, but we may have no bits set in either fds_read or fd_write,
  282. so check for that and handle it. Luckily, with WinSock, we can _also_
  283. ask how many bits are set on an fd_set.
  284. It is unclear why WinSock doesn't just handle this for us instead of
  285. calling this an error.
  286. Note also that WinSock ignores the first argument, so we don't worry
  287. about the fact that maxfd is computed incorrectly with WinSock (since
  288. curl_socket_t is unsigned in such cases and thus -1 is the largest
  289. value).
  290. */
  291. #ifdef USE_WINSOCK
  292. r = select((int)maxfd + 1,
  293. fds_read.fd_count ? &fds_read : NULL,
  294. fds_write.fd_count ? &fds_write : NULL,
  295. &fds_err, ptimeout);
  296. #else
  297. r = select((int)maxfd + 1, &fds_read, &fds_write, &fds_err, ptimeout);
  298. #endif
  299. if(r != -1)
  300. break;
  301. error = SOCKERRNO;
  302. if(error && ERROR_NOT_EINTR(error))
  303. break;
  304. if(timeout_ms > 0) {
  305. pending_ms = (int)(timeout_ms - ELAPSED_MS());
  306. if(pending_ms <= 0) {
  307. r = 0; /* Simulate a "call timed out" case */
  308. break;
  309. }
  310. }
  311. } while(r == -1);
  312. if(r < 0)
  313. return -1;
  314. if(r == 0)
  315. return 0;
  316. ret = 0;
  317. if(readfd0 != CURL_SOCKET_BAD) {
  318. if(FD_ISSET(readfd0, &fds_read))
  319. ret |= CURL_CSELECT_IN;
  320. if(FD_ISSET(readfd0, &fds_err))
  321. ret |= CURL_CSELECT_ERR;
  322. }
  323. if(readfd1 != CURL_SOCKET_BAD) {
  324. if(FD_ISSET(readfd1, &fds_read))
  325. ret |= CURL_CSELECT_IN2;
  326. if(FD_ISSET(readfd1, &fds_err))
  327. ret |= CURL_CSELECT_ERR;
  328. }
  329. if(writefd != CURL_SOCKET_BAD) {
  330. if(FD_ISSET(writefd, &fds_write))
  331. ret |= CURL_CSELECT_OUT;
  332. if(FD_ISSET(writefd, &fds_err))
  333. ret |= CURL_CSELECT_ERR;
  334. }
  335. return ret;
  336. #endif /* HAVE_POLL_FINE */
  337. }
  338. /*
  339. * This is a wrapper around poll(). If poll() does not exist, then
  340. * select() is used instead. An error is returned if select() is
  341. * being used and a file descriptor is too large for FD_SETSIZE.
  342. * A negative timeout value makes this function wait indefinitely,
  343. * unless no valid file descriptor is given, when this happens the
  344. * negative timeout is ignored and the function times out immediately.
  345. *
  346. * Return values:
  347. * -1 = system call error or fd >= FD_SETSIZE
  348. * 0 = timeout
  349. * N = number of structures with non zero revent fields
  350. */
  351. int Curl_poll(struct pollfd ufds[], unsigned int nfds, int timeout_ms)
  352. {
  353. #ifndef HAVE_POLL_FINE
  354. struct timeval pending_tv;
  355. struct timeval *ptimeout;
  356. fd_set fds_read;
  357. fd_set fds_write;
  358. fd_set fds_err;
  359. curl_socket_t maxfd;
  360. #endif
  361. struct curltime initial_tv = {0, 0};
  362. bool fds_none = TRUE;
  363. unsigned int i;
  364. int pending_ms = 0;
  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 Curl_now() 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 = Curl_now();
  385. }
  386. #ifdef HAVE_POLL_FINE
  387. do {
  388. int error;
  389. if(timeout_ms < 0)
  390. pending_ms = -1;
  391. else if(!timeout_ms)
  392. pending_ms = 0;
  393. r = poll(ufds, nfds, pending_ms);
  394. if(r != -1)
  395. break;
  396. error = SOCKERRNO;
  397. if(error && ERROR_NOT_EINTR(error))
  398. break;
  399. if(timeout_ms > 0) {
  400. pending_ms = (int)(timeout_ms - ELAPSED_MS());
  401. if(pending_ms <= 0) {
  402. r = 0; /* Simulate a "call timed out" case */
  403. break;
  404. }
  405. }
  406. } while(r == -1);
  407. if(r < 0)
  408. return -1;
  409. if(r == 0)
  410. return 0;
  411. for(i = 0; i < nfds; i++) {
  412. if(ufds[i].fd == CURL_SOCKET_BAD)
  413. continue;
  414. if(ufds[i].revents & POLLHUP)
  415. ufds[i].revents |= POLLIN;
  416. if(ufds[i].revents & POLLERR)
  417. ufds[i].revents |= (POLLIN|POLLOUT);
  418. }
  419. #else /* HAVE_POLL_FINE */
  420. FD_ZERO(&fds_read);
  421. FD_ZERO(&fds_write);
  422. FD_ZERO(&fds_err);
  423. maxfd = (curl_socket_t)-1;
  424. for(i = 0; i < nfds; i++) {
  425. ufds[i].revents = 0;
  426. if(ufds[i].fd == CURL_SOCKET_BAD)
  427. continue;
  428. VERIFY_SOCK(ufds[i].fd);
  429. if(ufds[i].events & (POLLIN|POLLOUT|POLLPRI|
  430. POLLRDNORM|POLLWRNORM|POLLRDBAND)) {
  431. if(ufds[i].fd > maxfd)
  432. maxfd = ufds[i].fd;
  433. if(ufds[i].events & (POLLRDNORM|POLLIN))
  434. FD_SET(ufds[i].fd, &fds_read);
  435. if(ufds[i].events & (POLLWRNORM|POLLOUT))
  436. FD_SET(ufds[i].fd, &fds_write);
  437. if(ufds[i].events & (POLLRDBAND|POLLPRI))
  438. FD_SET(ufds[i].fd, &fds_err);
  439. }
  440. }
  441. #ifdef USE_WINSOCK
  442. /* WinSock select() can't handle zero events. See the comment about this in
  443. Curl_check_socket(). */
  444. if(fds_read.fd_count == 0 && fds_write.fd_count == 0
  445. && fds_err.fd_count == 0) {
  446. r = Curl_wait_ms(timeout_ms);
  447. return r;
  448. }
  449. #endif
  450. ptimeout = (timeout_ms < 0) ? NULL : &pending_tv;
  451. do {
  452. int error;
  453. if(timeout_ms > 0) {
  454. pending_tv.tv_sec = pending_ms / 1000;
  455. pending_tv.tv_usec = (pending_ms % 1000) * 1000;
  456. }
  457. else if(!timeout_ms) {
  458. pending_tv.tv_sec = 0;
  459. pending_tv.tv_usec = 0;
  460. }
  461. #ifdef USE_WINSOCK
  462. r = select((int)maxfd + 1,
  463. /* WinSock select() can't handle fd_sets with zero bits set, so
  464. don't give it such arguments. See the comment about this in
  465. Curl_check_socket().
  466. */
  467. fds_read.fd_count ? &fds_read : NULL,
  468. fds_write.fd_count ? &fds_write : NULL,
  469. fds_err.fd_count ? &fds_err : NULL, ptimeout);
  470. #else
  471. r = select((int)maxfd + 1, &fds_read, &fds_write, &fds_err, ptimeout);
  472. #endif
  473. if(r != -1)
  474. break;
  475. error = SOCKERRNO;
  476. if(error && ERROR_NOT_EINTR(error))
  477. break;
  478. if(timeout_ms > 0) {
  479. pending_ms = timeout_ms - ELAPSED_MS();
  480. if(pending_ms <= 0) {
  481. r = 0; /* Simulate a "call timed out" case */
  482. break;
  483. }
  484. }
  485. } while(r == -1);
  486. if(r < 0)
  487. return -1;
  488. if(r == 0)
  489. return 0;
  490. r = 0;
  491. for(i = 0; i < nfds; i++) {
  492. ufds[i].revents = 0;
  493. if(ufds[i].fd == CURL_SOCKET_BAD)
  494. continue;
  495. if(FD_ISSET(ufds[i].fd, &fds_read))
  496. ufds[i].revents |= POLLIN;
  497. if(FD_ISSET(ufds[i].fd, &fds_write))
  498. ufds[i].revents |= POLLOUT;
  499. if(FD_ISSET(ufds[i].fd, &fds_err))
  500. ufds[i].revents |= POLLPRI;
  501. if(ufds[i].revents != 0)
  502. r++;
  503. }
  504. #endif /* HAVE_POLL_FINE */
  505. return r;
  506. }
  507. #ifdef TPF
  508. /*
  509. * This is a replacement for select() on the TPF platform.
  510. * It is used whenever libcurl calls select().
  511. * The call below to tpf_process_signals() is required because
  512. * TPF's select calls are not signal interruptible.
  513. *
  514. * Return values are the same as select's.
  515. */
  516. int tpf_select_libcurl(int maxfds, fd_set *reads, fd_set *writes,
  517. fd_set *excepts, struct timeval *tv)
  518. {
  519. int rc;
  520. rc = tpf_select_bsd(maxfds, reads, writes, excepts, tv);
  521. tpf_process_signals();
  522. return rc;
  523. }
  524. #endif /* TPF */