select.c 13 KB

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