select.c 14 KB

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