select.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2021, 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.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 critical 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. #ifndef HAVE_POLL_FINE
  122. /*
  123. * This is a wrapper around select() to aid in Windows compatibility.
  124. * A negative timeout value makes this function wait indefinitely,
  125. * unless no valid file descriptor is given, when this happens the
  126. * negative timeout is ignored and the function times out immediately.
  127. *
  128. * Return values:
  129. * -1 = system call error or fd >= FD_SETSIZE
  130. * 0 = timeout
  131. * N = number of signalled file descriptors
  132. */
  133. static int our_select(curl_socket_t maxfd, /* highest socket number */
  134. fd_set *fds_read, /* sockets ready for reading */
  135. fd_set *fds_write, /* sockets ready for writing */
  136. fd_set *fds_err, /* sockets with errors */
  137. timediff_t timeout_ms) /* milliseconds to wait */
  138. {
  139. struct timeval pending_tv;
  140. struct timeval *ptimeout;
  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. /* no sockets, just wait */
  147. return Curl_wait_ms(timeout_ms);
  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. Luckily, with WinSock, we can _also_ ask how
  196. many bits are set on an fd_set. So, let's just check it beforehand.
  197. */
  198. return select((int)maxfd + 1,
  199. fds_read && fds_read->fd_count ? fds_read : NULL,
  200. fds_write && fds_write->fd_count ? fds_write : NULL,
  201. fds_err && fds_err->fd_count ? fds_err : NULL, ptimeout);
  202. #else
  203. return select((int)maxfd + 1, fds_read, fds_write, fds_err, ptimeout);
  204. #endif
  205. }
  206. #endif
  207. /*
  208. * Wait for read or write events on a set of file descriptors. It uses poll()
  209. * when a fine poll() is available, in order to avoid limits with FD_SETSIZE,
  210. * otherwise select() is used. An error is returned if select() is being used
  211. * and a file descriptor is too large for FD_SETSIZE.
  212. *
  213. * A negative timeout value makes this function wait indefinitely,
  214. * unless no valid file descriptor is given, when this happens the
  215. * negative timeout is ignored and the function times out immediately.
  216. *
  217. * Return values:
  218. * -1 = system call error or fd >= FD_SETSIZE
  219. * 0 = timeout
  220. * [bitmask] = action as described below
  221. *
  222. * CURL_CSELECT_IN - first socket is readable
  223. * CURL_CSELECT_IN2 - second socket is readable
  224. * CURL_CSELECT_OUT - write socket is writable
  225. * CURL_CSELECT_ERR - an error condition occurred
  226. */
  227. int Curl_socket_check(curl_socket_t readfd0, /* two sockets to read from */
  228. curl_socket_t readfd1,
  229. curl_socket_t writefd, /* socket to write to */
  230. timediff_t timeout_ms) /* milliseconds to wait */
  231. {
  232. struct pollfd pfd[3];
  233. int num;
  234. int r;
  235. if((readfd0 == CURL_SOCKET_BAD) && (readfd1 == CURL_SOCKET_BAD) &&
  236. (writefd == CURL_SOCKET_BAD)) {
  237. /* no sockets, just wait */
  238. return Curl_wait_ms(timeout_ms);
  239. }
  240. /* Avoid initial timestamp, avoid Curl_now() call, when elapsed
  241. time in this function does not need to be measured. This happens
  242. when function is called with a zero timeout or a negative timeout
  243. value indicating a blocking call should be performed. */
  244. num = 0;
  245. if(readfd0 != CURL_SOCKET_BAD) {
  246. pfd[num].fd = readfd0;
  247. pfd[num].events = POLLRDNORM|POLLIN|POLLRDBAND|POLLPRI;
  248. pfd[num].revents = 0;
  249. num++;
  250. }
  251. if(readfd1 != CURL_SOCKET_BAD) {
  252. pfd[num].fd = readfd1;
  253. pfd[num].events = POLLRDNORM|POLLIN|POLLRDBAND|POLLPRI;
  254. pfd[num].revents = 0;
  255. num++;
  256. }
  257. if(writefd != CURL_SOCKET_BAD) {
  258. pfd[num].fd = writefd;
  259. pfd[num].events = POLLWRNORM|POLLOUT|POLLPRI;
  260. pfd[num].revents = 0;
  261. num++;
  262. }
  263. r = Curl_poll(pfd, num, timeout_ms);
  264. if(r <= 0)
  265. return r;
  266. r = 0;
  267. num = 0;
  268. if(readfd0 != CURL_SOCKET_BAD) {
  269. if(pfd[num].revents & (POLLRDNORM|POLLIN|POLLERR|POLLHUP))
  270. r |= CURL_CSELECT_IN;
  271. if(pfd[num].revents & (POLLRDBAND|POLLPRI|POLLNVAL))
  272. r |= CURL_CSELECT_ERR;
  273. num++;
  274. }
  275. if(readfd1 != CURL_SOCKET_BAD) {
  276. if(pfd[num].revents & (POLLRDNORM|POLLIN|POLLERR|POLLHUP))
  277. r |= CURL_CSELECT_IN2;
  278. if(pfd[num].revents & (POLLRDBAND|POLLPRI|POLLNVAL))
  279. r |= CURL_CSELECT_ERR;
  280. num++;
  281. }
  282. if(writefd != CURL_SOCKET_BAD) {
  283. if(pfd[num].revents & (POLLWRNORM|POLLOUT))
  284. r |= CURL_CSELECT_OUT;
  285. if(pfd[num].revents & (POLLERR|POLLHUP|POLLPRI|POLLNVAL))
  286. r |= CURL_CSELECT_ERR;
  287. }
  288. return r;
  289. }
  290. /*
  291. * This is a wrapper around poll(). If poll() does not exist, then
  292. * select() is used instead. An error is returned if select() is
  293. * being used and a file descriptor is too large for FD_SETSIZE.
  294. * A negative timeout value makes this function wait indefinitely,
  295. * unless no valid file descriptor is given, when this happens the
  296. * negative timeout is ignored and the function times out immediately.
  297. *
  298. * Return values:
  299. * -1 = system call error or fd >= FD_SETSIZE
  300. * 0 = timeout
  301. * N = number of structures with non zero revent fields
  302. */
  303. int Curl_poll(struct pollfd ufds[], unsigned int nfds, timediff_t timeout_ms)
  304. {
  305. #ifdef HAVE_POLL_FINE
  306. int pending_ms;
  307. #else
  308. fd_set fds_read;
  309. fd_set fds_write;
  310. fd_set fds_err;
  311. curl_socket_t maxfd;
  312. #endif
  313. bool fds_none = TRUE;
  314. unsigned int i;
  315. int r;
  316. if(ufds) {
  317. for(i = 0; i < nfds; i++) {
  318. if(ufds[i].fd != CURL_SOCKET_BAD) {
  319. fds_none = FALSE;
  320. break;
  321. }
  322. }
  323. }
  324. if(fds_none) {
  325. /* no sockets, just wait */
  326. return Curl_wait_ms(timeout_ms);
  327. }
  328. /* Avoid initial timestamp, avoid Curl_now() call, when elapsed
  329. time in this function does not need to be measured. This happens
  330. when function is called with a zero timeout or a negative timeout
  331. value indicating a blocking call should be performed. */
  332. #ifdef HAVE_POLL_FINE
  333. /* prevent overflow, timeout_ms is typecast to int. */
  334. #if TIMEDIFF_T_MAX > INT_MAX
  335. if(timeout_ms > INT_MAX)
  336. timeout_ms = INT_MAX;
  337. #endif
  338. if(timeout_ms > 0)
  339. pending_ms = (int)timeout_ms;
  340. else if(timeout_ms < 0)
  341. pending_ms = -1;
  342. else
  343. pending_ms = 0;
  344. r = poll(ufds, nfds, pending_ms);
  345. if(r <= 0)
  346. return r;
  347. for(i = 0; i < nfds; i++) {
  348. if(ufds[i].fd == CURL_SOCKET_BAD)
  349. continue;
  350. if(ufds[i].revents & POLLHUP)
  351. ufds[i].revents |= POLLIN;
  352. if(ufds[i].revents & POLLERR)
  353. ufds[i].revents |= POLLIN|POLLOUT;
  354. }
  355. #else /* HAVE_POLL_FINE */
  356. FD_ZERO(&fds_read);
  357. FD_ZERO(&fds_write);
  358. FD_ZERO(&fds_err);
  359. maxfd = (curl_socket_t)-1;
  360. for(i = 0; i < nfds; i++) {
  361. ufds[i].revents = 0;
  362. if(ufds[i].fd == CURL_SOCKET_BAD)
  363. continue;
  364. VERIFY_SOCK(ufds[i].fd);
  365. if(ufds[i].events & (POLLIN|POLLOUT|POLLPRI|
  366. POLLRDNORM|POLLWRNORM|POLLRDBAND)) {
  367. if(ufds[i].fd > maxfd)
  368. maxfd = ufds[i].fd;
  369. if(ufds[i].events & (POLLRDNORM|POLLIN))
  370. FD_SET(ufds[i].fd, &fds_read);
  371. if(ufds[i].events & (POLLWRNORM|POLLOUT))
  372. FD_SET(ufds[i].fd, &fds_write);
  373. if(ufds[i].events & (POLLRDBAND|POLLPRI))
  374. FD_SET(ufds[i].fd, &fds_err);
  375. }
  376. }
  377. /*
  378. Note also that WinSock ignores the first argument, so we don't worry
  379. about the fact that maxfd is computed incorrectly with WinSock (since
  380. curl_socket_t is unsigned in such cases and thus -1 is the largest
  381. value).
  382. */
  383. r = our_select(maxfd, &fds_read, &fds_write, &fds_err, timeout_ms);
  384. if(r <= 0)
  385. return r;
  386. r = 0;
  387. for(i = 0; i < nfds; i++) {
  388. ufds[i].revents = 0;
  389. if(ufds[i].fd == CURL_SOCKET_BAD)
  390. continue;
  391. if(FD_ISSET(ufds[i].fd, &fds_read)) {
  392. if(ufds[i].events & POLLRDNORM)
  393. ufds[i].revents |= POLLRDNORM;
  394. if(ufds[i].events & POLLIN)
  395. ufds[i].revents |= POLLIN;
  396. }
  397. if(FD_ISSET(ufds[i].fd, &fds_write)) {
  398. if(ufds[i].events & POLLWRNORM)
  399. ufds[i].revents |= POLLWRNORM;
  400. if(ufds[i].events & POLLOUT)
  401. ufds[i].revents |= POLLOUT;
  402. }
  403. if(FD_ISSET(ufds[i].fd, &fds_err)) {
  404. if(ufds[i].events & POLLRDBAND)
  405. ufds[i].revents |= POLLRDBAND;
  406. if(ufds[i].events & POLLPRI)
  407. ufds[i].revents |= POLLPRI;
  408. }
  409. if(ufds[i].revents)
  410. r++;
  411. }
  412. #endif /* HAVE_POLL_FINE */
  413. return r;
  414. }
  415. #ifdef TPF
  416. /*
  417. * This is a replacement for select() on the TPF platform.
  418. * It is used whenever libcurl calls select().
  419. * The call below to tpf_process_signals() is required because
  420. * TPF's select calls are not signal interruptible.
  421. *
  422. * Return values are the same as select's.
  423. */
  424. int tpf_select_libcurl(int maxfds, fd_set *reads, fd_set *writes,
  425. fd_set *excepts, struct timeval *tv)
  426. {
  427. int rc;
  428. rc = tpf_select_bsd(maxfds, reads, writes, excepts, tv);
  429. tpf_process_signals();
  430. return rc;
  431. }
  432. #endif /* TPF */