select.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542
  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. /*
  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(timediff_t timeout_ms)
  65. {
  66. int r = 0;
  67. if(!timeout_ms)
  68. return 0;
  69. if(timeout_ms < 0) {
  70. SET_SOCKERRNO(EINVAL);
  71. return -1;
  72. }
  73. #if defined(MSDOS)
  74. delay(timeout_ms);
  75. #elif defined(WIN32)
  76. /* prevent overflow, timeout_ms is typecast to ULONG/DWORD. */
  77. #if TIMEDIFF_T_MAX >= ULONG_MAX
  78. if(timeout_ms >= ULONG_MAX)
  79. timeout_ms = ULONG_MAX-1;
  80. /* don't use ULONG_MAX, because that is equal to INFINITE */
  81. #endif
  82. Sleep((ULONG)timeout_ms);
  83. #else
  84. #if defined(HAVE_POLL_FINE)
  85. /* prevent overflow, timeout_ms is typecast to int. */
  86. #if TIMEDIFF_T_MAX > INT_MAX
  87. if(timeout_ms > INT_MAX)
  88. timeout_ms = INT_MAX;
  89. #endif
  90. r = poll(NULL, 0, (int)timeout_ms);
  91. #else
  92. {
  93. struct timeval pending_tv;
  94. timediff_t tv_sec = timeout_ms / 1000;
  95. timediff_t tv_usec = (timeout_ms % 1000) * 1000; /* max=999999 */
  96. #ifdef HAVE_SUSECONDS_T
  97. #if TIMEDIFF_T_MAX > TIME_T_MAX
  98. /* tv_sec overflow check in case time_t is signed */
  99. if(tv_sec > TIME_T_MAX)
  100. tv_sec = TIME_T_MAX;
  101. #endif
  102. pending_tv.tv_sec = (time_t)tv_sec;
  103. pending_tv.tv_usec = (suseconds_t)tv_usec;
  104. #else
  105. #if TIMEDIFF_T_MAX > INT_MAX
  106. /* tv_sec overflow check in case time_t is signed */
  107. if(tv_sec > INT_MAX)
  108. tv_sec = INT_MAX;
  109. #endif
  110. pending_tv.tv_sec = (int)tv_sec;
  111. pending_tv.tv_usec = (int)tv_usec;
  112. #endif
  113. r = select(0, NULL, NULL, NULL, &pending_tv);
  114. }
  115. #endif /* HAVE_POLL_FINE */
  116. #endif /* USE_WINSOCK */
  117. if(r)
  118. r = -1;
  119. return r;
  120. }
  121. /*
  122. * This is a wrapper around select() to aid in Windows compatibility.
  123. * A negative timeout value makes this function wait indefinitely,
  124. * unless no valid file descriptor is given, when this happens the
  125. * negative timeout is ignored and the function times out immediately.
  126. *
  127. * Return values:
  128. * -1 = system call error or fd >= FD_SETSIZE
  129. * 0 = timeout
  130. * N = number of signalled file descriptors
  131. */
  132. int Curl_select(curl_socket_t maxfd, /* highest socket number */
  133. fd_set *fds_read, /* sockets ready for reading */
  134. fd_set *fds_write, /* sockets ready for writing */
  135. fd_set *fds_err, /* sockets with errors */
  136. timediff_t timeout_ms) /* milliseconds to wait */
  137. {
  138. struct timeval pending_tv;
  139. struct timeval *ptimeout;
  140. int r;
  141. #ifdef USE_WINSOCK
  142. /* WinSock select() can't handle zero events. See the comment below. */
  143. if((!fds_read || fds_read->fd_count == 0) &&
  144. (!fds_write || fds_write->fd_count == 0) &&
  145. (!fds_err || fds_err->fd_count == 0)) {
  146. r = Curl_wait_ms(timeout_ms);
  147. return r;
  148. }
  149. #endif
  150. ptimeout = &pending_tv;
  151. if(timeout_ms < 0) {
  152. ptimeout = NULL;
  153. }
  154. else if(timeout_ms > 0) {
  155. timediff_t tv_sec = timeout_ms / 1000;
  156. timediff_t tv_usec = (timeout_ms % 1000) * 1000; /* max=999999 */
  157. #ifdef HAVE_SUSECONDS_T
  158. #if TIMEDIFF_T_MAX > TIME_T_MAX
  159. /* tv_sec overflow check in case time_t is signed */
  160. if(tv_sec > TIME_T_MAX)
  161. tv_sec = TIME_T_MAX;
  162. #endif
  163. pending_tv.tv_sec = (time_t)tv_sec;
  164. pending_tv.tv_usec = (suseconds_t)tv_usec;
  165. #elif defined(WIN32) /* maybe also others in the future */
  166. #if TIMEDIFF_T_MAX > LONG_MAX
  167. /* tv_sec overflow check on Windows there we know it is long */
  168. if(tv_sec > LONG_MAX)
  169. tv_sec = LONG_MAX;
  170. #endif
  171. pending_tv.tv_sec = (long)tv_sec;
  172. pending_tv.tv_usec = (long)tv_usec;
  173. #else
  174. #if TIMEDIFF_T_MAX > INT_MAX
  175. /* tv_sec overflow check in case time_t is signed */
  176. if(tv_sec > INT_MAX)
  177. tv_sec = INT_MAX;
  178. #endif
  179. pending_tv.tv_sec = (int)tv_sec;
  180. pending_tv.tv_usec = (int)tv_usec;
  181. #endif
  182. }
  183. else {
  184. pending_tv.tv_sec = 0;
  185. pending_tv.tv_usec = 0;
  186. }
  187. #ifdef USE_WINSOCK
  188. /* WinSock select() must not be called with an fd_set that contains zero
  189. fd flags, or it will return WSAEINVAL. But, it also can't be called
  190. with no fd_sets at all! From the documentation:
  191. Any two of the parameters, readfds, writefds, or exceptfds, can be
  192. given as null. At least one must be non-null, and any non-null
  193. descriptor set must contain at least one handle to a socket.
  194. It is unclear why WinSock doesn't just handle this for us instead of
  195. calling this an error.
  196. */
  197. r = select((int)maxfd + 1,
  198. fds_read && fds_read->fd_count ? fds_read : NULL,
  199. fds_write && fds_write->fd_count ? fds_write : NULL,
  200. fds_err && fds_err->fd_count ? fds_err : NULL, ptimeout);
  201. #else
  202. r = select((int)maxfd + 1, fds_read, fds_write, fds_err, ptimeout);
  203. #endif
  204. return r;
  205. }
  206. /*
  207. * Wait for read or write events on a set of file descriptors. It uses poll()
  208. * when a fine poll() is available, in order to avoid limits with FD_SETSIZE,
  209. * otherwise select() is used. An error is returned if select() is being used
  210. * and a file descriptor is too large for FD_SETSIZE.
  211. *
  212. * A negative timeout value makes this function wait indefinitely,
  213. * unless no valid file descriptor is given, when this happens the
  214. * negative timeout is ignored and the function times out immediately.
  215. *
  216. * Return values:
  217. * -1 = system call error or fd >= FD_SETSIZE
  218. * 0 = timeout
  219. * [bitmask] = action as described below
  220. *
  221. * CURL_CSELECT_IN - first socket is readable
  222. * CURL_CSELECT_IN2 - second socket is readable
  223. * CURL_CSELECT_OUT - write socket is writable
  224. * CURL_CSELECT_ERR - an error condition occurred
  225. */
  226. int Curl_socket_check(curl_socket_t readfd0, /* two sockets to read from */
  227. curl_socket_t readfd1,
  228. curl_socket_t writefd, /* socket to write to */
  229. timediff_t timeout_ms) /* milliseconds to wait */
  230. {
  231. #ifdef HAVE_POLL_FINE
  232. struct pollfd pfd[3];
  233. int num;
  234. #else
  235. fd_set fds_read;
  236. fd_set fds_write;
  237. fd_set fds_err;
  238. curl_socket_t maxfd;
  239. #endif
  240. int r;
  241. int ret;
  242. if((readfd0 == CURL_SOCKET_BAD) && (readfd1 == CURL_SOCKET_BAD) &&
  243. (writefd == CURL_SOCKET_BAD)) {
  244. /* no sockets, just wait */
  245. r = Curl_wait_ms(timeout_ms);
  246. return r;
  247. }
  248. /* Avoid initial timestamp, avoid Curl_now() call, when elapsed
  249. time in this function does not need to be measured. This happens
  250. when function is called with a zero timeout or a negative timeout
  251. value indicating a blocking call should be performed. */
  252. #ifdef HAVE_POLL_FINE
  253. num = 0;
  254. if(readfd0 != CURL_SOCKET_BAD) {
  255. pfd[num].fd = readfd0;
  256. pfd[num].events = POLLRDNORM|POLLIN|POLLRDBAND|POLLPRI;
  257. pfd[num].revents = 0;
  258. num++;
  259. }
  260. if(readfd1 != CURL_SOCKET_BAD) {
  261. pfd[num].fd = readfd1;
  262. pfd[num].events = POLLRDNORM|POLLIN|POLLRDBAND|POLLPRI;
  263. pfd[num].revents = 0;
  264. num++;
  265. }
  266. if(writefd != CURL_SOCKET_BAD) {
  267. pfd[num].fd = writefd;
  268. pfd[num].events = POLLWRNORM|POLLOUT;
  269. pfd[num].revents = 0;
  270. num++;
  271. }
  272. r = Curl_poll(pfd, num, timeout_ms);
  273. if(r <= 0)
  274. return r;
  275. ret = 0;
  276. num = 0;
  277. if(readfd0 != CURL_SOCKET_BAD) {
  278. if(pfd[num].revents & (POLLRDNORM|POLLIN|POLLERR|POLLHUP))
  279. ret |= CURL_CSELECT_IN;
  280. if(pfd[num].revents & (POLLRDBAND|POLLPRI|POLLNVAL))
  281. ret |= CURL_CSELECT_ERR;
  282. num++;
  283. }
  284. if(readfd1 != CURL_SOCKET_BAD) {
  285. if(pfd[num].revents & (POLLRDNORM|POLLIN|POLLERR|POLLHUP))
  286. ret |= CURL_CSELECT_IN2;
  287. if(pfd[num].revents & (POLLRDBAND|POLLPRI|POLLNVAL))
  288. ret |= CURL_CSELECT_ERR;
  289. num++;
  290. }
  291. if(writefd != CURL_SOCKET_BAD) {
  292. if(pfd[num].revents & (POLLWRNORM|POLLOUT))
  293. ret |= CURL_CSELECT_OUT;
  294. if(pfd[num].revents & (POLLERR|POLLHUP|POLLNVAL))
  295. ret |= CURL_CSELECT_ERR;
  296. }
  297. return ret;
  298. #else /* HAVE_POLL_FINE */
  299. FD_ZERO(&fds_err);
  300. maxfd = (curl_socket_t)-1;
  301. FD_ZERO(&fds_read);
  302. if(readfd0 != CURL_SOCKET_BAD) {
  303. VERIFY_SOCK(readfd0);
  304. FD_SET(readfd0, &fds_read);
  305. FD_SET(readfd0, &fds_err);
  306. maxfd = readfd0;
  307. }
  308. if(readfd1 != CURL_SOCKET_BAD) {
  309. VERIFY_SOCK(readfd1);
  310. FD_SET(readfd1, &fds_read);
  311. FD_SET(readfd1, &fds_err);
  312. if(readfd1 > maxfd)
  313. maxfd = readfd1;
  314. }
  315. FD_ZERO(&fds_write);
  316. if(writefd != CURL_SOCKET_BAD) {
  317. VERIFY_SOCK(writefd);
  318. FD_SET(writefd, &fds_write);
  319. FD_SET(writefd, &fds_err);
  320. if(writefd > maxfd)
  321. maxfd = writefd;
  322. }
  323. /* We know that we have at least one bit set in at least two fd_sets in
  324. this case, but we may have no bits set in either fds_read or fd_write,
  325. so check for that and handle it. Luckily, with WinSock, we can _also_
  326. ask how many bits are set on an fd_set.
  327. Note also that WinSock ignores the first argument, so we don't worry
  328. about the fact that maxfd is computed incorrectly with WinSock (since
  329. curl_socket_t is unsigned in such cases and thus -1 is the largest
  330. value).
  331. */
  332. r = Curl_select(maxfd, &fds_read, &fds_write, &fds_err, timeout_ms);
  333. if(r < 0)
  334. return -1;
  335. if(r == 0)
  336. return 0;
  337. ret = 0;
  338. if(readfd0 != CURL_SOCKET_BAD) {
  339. if(FD_ISSET(readfd0, &fds_read))
  340. ret |= CURL_CSELECT_IN;
  341. if(FD_ISSET(readfd0, &fds_err))
  342. ret |= CURL_CSELECT_ERR;
  343. }
  344. if(readfd1 != CURL_SOCKET_BAD) {
  345. if(FD_ISSET(readfd1, &fds_read))
  346. ret |= CURL_CSELECT_IN2;
  347. if(FD_ISSET(readfd1, &fds_err))
  348. ret |= CURL_CSELECT_ERR;
  349. }
  350. if(writefd != CURL_SOCKET_BAD) {
  351. if(FD_ISSET(writefd, &fds_write))
  352. ret |= CURL_CSELECT_OUT;
  353. if(FD_ISSET(writefd, &fds_err))
  354. ret |= CURL_CSELECT_ERR;
  355. }
  356. return ret;
  357. #endif /* HAVE_POLL_FINE */
  358. }
  359. /*
  360. * This is a wrapper around poll(). If poll() does not exist, then
  361. * select() is used instead. An error is returned if select() is
  362. * being used and a file descriptor is too large for FD_SETSIZE.
  363. * A negative timeout value makes this function wait indefinitely,
  364. * unless no valid file descriptor is given, when this happens the
  365. * negative timeout is ignored and the function times out immediately.
  366. *
  367. * Return values:
  368. * -1 = system call error or fd >= FD_SETSIZE
  369. * 0 = timeout
  370. * N = number of structures with non zero revent fields
  371. */
  372. int Curl_poll(struct pollfd ufds[], unsigned int nfds, timediff_t timeout_ms)
  373. {
  374. #ifdef HAVE_POLL_FINE
  375. int pending_ms;
  376. #else
  377. fd_set fds_read;
  378. fd_set fds_write;
  379. fd_set fds_err;
  380. curl_socket_t maxfd;
  381. #endif
  382. bool fds_none = TRUE;
  383. unsigned int i;
  384. int r;
  385. if(ufds) {
  386. for(i = 0; i < nfds; i++) {
  387. if(ufds[i].fd != CURL_SOCKET_BAD) {
  388. fds_none = FALSE;
  389. break;
  390. }
  391. }
  392. }
  393. if(fds_none) {
  394. /* no sockets, just wait */
  395. r = Curl_wait_ms(timeout_ms);
  396. return r;
  397. }
  398. /* Avoid initial timestamp, avoid Curl_now() call, when elapsed
  399. time in this function does not need to be measured. This happens
  400. when function is called with a zero timeout or a negative timeout
  401. value indicating a blocking call should be performed. */
  402. #ifdef HAVE_POLL_FINE
  403. /* prevent overflow, timeout_ms is typecast to int. */
  404. #if TIMEDIFF_T_MAX > INT_MAX
  405. if(timeout_ms > INT_MAX)
  406. timeout_ms = INT_MAX;
  407. #endif
  408. if(timeout_ms > 0)
  409. pending_ms = (int)timeout_ms;
  410. else if(timeout_ms < 0)
  411. pending_ms = -1;
  412. else
  413. pending_ms = 0;
  414. r = poll(ufds, nfds, pending_ms);
  415. if(r < 0)
  416. return -1;
  417. if(r == 0)
  418. return 0;
  419. for(i = 0; i < nfds; i++) {
  420. if(ufds[i].fd == CURL_SOCKET_BAD)
  421. continue;
  422. if(ufds[i].revents & POLLHUP)
  423. ufds[i].revents |= POLLIN;
  424. if(ufds[i].revents & POLLERR)
  425. ufds[i].revents |= (POLLIN|POLLOUT);
  426. }
  427. #else /* HAVE_POLL_FINE */
  428. FD_ZERO(&fds_read);
  429. FD_ZERO(&fds_write);
  430. FD_ZERO(&fds_err);
  431. maxfd = (curl_socket_t)-1;
  432. for(i = 0; i < nfds; i++) {
  433. ufds[i].revents = 0;
  434. if(ufds[i].fd == CURL_SOCKET_BAD)
  435. continue;
  436. VERIFY_SOCK(ufds[i].fd);
  437. if(ufds[i].events & (POLLIN|POLLOUT|POLLPRI|
  438. POLLRDNORM|POLLWRNORM|POLLRDBAND)) {
  439. if(ufds[i].fd > maxfd)
  440. maxfd = ufds[i].fd;
  441. if(ufds[i].events & (POLLRDNORM|POLLIN))
  442. FD_SET(ufds[i].fd, &fds_read);
  443. if(ufds[i].events & (POLLWRNORM|POLLOUT))
  444. FD_SET(ufds[i].fd, &fds_write);
  445. if(ufds[i].events & (POLLRDBAND|POLLPRI))
  446. FD_SET(ufds[i].fd, &fds_err);
  447. }
  448. }
  449. r = Curl_select(maxfd, &fds_read, &fds_write, &fds_err, timeout_ms);
  450. if(r < 0)
  451. return -1;
  452. if(r == 0)
  453. return 0;
  454. r = 0;
  455. for(i = 0; i < nfds; i++) {
  456. ufds[i].revents = 0;
  457. if(ufds[i].fd == CURL_SOCKET_BAD)
  458. continue;
  459. if(FD_ISSET(ufds[i].fd, &fds_read))
  460. ufds[i].revents |= POLLIN;
  461. if(FD_ISSET(ufds[i].fd, &fds_write))
  462. ufds[i].revents |= POLLOUT;
  463. if(FD_ISSET(ufds[i].fd, &fds_err))
  464. ufds[i].revents |= POLLPRI;
  465. if(ufds[i].revents != 0)
  466. r++;
  467. }
  468. #endif /* HAVE_POLL_FINE */
  469. return r;
  470. }
  471. #ifdef TPF
  472. /*
  473. * This is a replacement for select() on the TPF platform.
  474. * It is used whenever libcurl calls select().
  475. * The call below to tpf_process_signals() is required because
  476. * TPF's select calls are not signal interruptible.
  477. *
  478. * Return values are the same as select's.
  479. */
  480. int tpf_select_libcurl(int maxfds, fd_set *reads, fd_set *writes,
  481. fd_set *excepts, struct timeval *tv)
  482. {
  483. int rc;
  484. rc = tpf_select_bsd(maxfds, reads, writes, excepts, tv);
  485. tpf_process_signals();
  486. return rc;
  487. }
  488. #endif /* TPF */