select.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2022, 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.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. * SPDX-License-Identifier: curl
  22. *
  23. ***************************************************************************/
  24. #include "curl_setup.h"
  25. #include <limits.h>
  26. #ifdef HAVE_SYS_SELECT_H
  27. #include <sys/select.h>
  28. #elif defined(HAVE_UNISTD_H)
  29. #include <unistd.h>
  30. #endif
  31. #if !defined(HAVE_SELECT) && !defined(HAVE_POLL_FINE)
  32. #error "We can't compile without select() or poll() support."
  33. #endif
  34. #ifdef MSDOS
  35. #include <dos.h> /* delay() */
  36. #endif
  37. #include <curl/curl.h>
  38. #include "urldata.h"
  39. #include "connect.h"
  40. #include "select.h"
  41. #include "timediff.h"
  42. #include "warnless.h"
  43. /*
  44. * Internal function used for waiting a specific amount of ms
  45. * in Curl_socket_check() and Curl_poll() when no file descriptor
  46. * is provided to wait on, just being used to delay execution.
  47. * WinSock select() and poll() timeout mechanisms need a valid
  48. * socket descriptor in a not null file descriptor set to work.
  49. * Waiting indefinitely with this function is not allowed, a
  50. * zero or negative timeout value will return immediately.
  51. * Timeout resolution, accuracy, as well as maximum supported
  52. * value is system dependent, neither factor is a critical issue
  53. * for the intended use of this function in the library.
  54. *
  55. * Return values:
  56. * -1 = system call error, invalid timeout value, or interrupted
  57. * 0 = specified timeout has elapsed
  58. */
  59. int Curl_wait_ms(timediff_t timeout_ms)
  60. {
  61. int r = 0;
  62. if(!timeout_ms)
  63. return 0;
  64. if(timeout_ms < 0) {
  65. SET_SOCKERRNO(EINVAL);
  66. return -1;
  67. }
  68. #if defined(MSDOS)
  69. delay(timeout_ms);
  70. #elif defined(WIN32)
  71. /* prevent overflow, timeout_ms is typecast to ULONG/DWORD. */
  72. #if TIMEDIFF_T_MAX >= ULONG_MAX
  73. if(timeout_ms >= ULONG_MAX)
  74. timeout_ms = ULONG_MAX-1;
  75. /* don't use ULONG_MAX, because that is equal to INFINITE */
  76. #endif
  77. Sleep((ULONG)timeout_ms);
  78. #else
  79. #if defined(HAVE_POLL_FINE)
  80. /* prevent overflow, timeout_ms is typecast to int. */
  81. #if TIMEDIFF_T_MAX > INT_MAX
  82. if(timeout_ms > INT_MAX)
  83. timeout_ms = INT_MAX;
  84. #endif
  85. r = poll(NULL, 0, (int)timeout_ms);
  86. #else
  87. {
  88. struct timeval pending_tv;
  89. r = select(0, NULL, NULL, NULL, curlx_mstotv(&pending_tv, timeout_ms));
  90. }
  91. #endif /* HAVE_POLL_FINE */
  92. #endif /* USE_WINSOCK */
  93. if(r)
  94. r = -1;
  95. return r;
  96. }
  97. #ifndef HAVE_POLL_FINE
  98. /*
  99. * This is a wrapper around select() to aid in Windows compatibility.
  100. * A negative timeout value makes this function wait indefinitely,
  101. * unless no valid file descriptor is given, when this happens the
  102. * negative timeout is ignored and the function times out immediately.
  103. *
  104. * Return values:
  105. * -1 = system call error or fd >= FD_SETSIZE
  106. * 0 = timeout
  107. * N = number of signalled file descriptors
  108. */
  109. static int our_select(curl_socket_t maxfd, /* highest socket number */
  110. fd_set *fds_read, /* sockets ready for reading */
  111. fd_set *fds_write, /* sockets ready for writing */
  112. fd_set *fds_err, /* sockets with errors */
  113. timediff_t timeout_ms) /* milliseconds to wait */
  114. {
  115. struct timeval pending_tv;
  116. struct timeval *ptimeout;
  117. #ifdef USE_WINSOCK
  118. /* WinSock select() can't handle zero events. See the comment below. */
  119. if((!fds_read || fds_read->fd_count == 0) &&
  120. (!fds_write || fds_write->fd_count == 0) &&
  121. (!fds_err || fds_err->fd_count == 0)) {
  122. /* no sockets, just wait */
  123. return Curl_wait_ms(timeout_ms);
  124. }
  125. #endif
  126. ptimeout = curlx_mstotv(&pending_tv, timeout_ms);
  127. #ifdef USE_WINSOCK
  128. /* WinSock select() must not be called with an fd_set that contains zero
  129. fd flags, or it will return WSAEINVAL. But, it also can't be called
  130. with no fd_sets at all! From the documentation:
  131. Any two of the parameters, readfds, writefds, or exceptfds, can be
  132. given as null. At least one must be non-null, and any non-null
  133. descriptor set must contain at least one handle to a socket.
  134. It is unclear why WinSock doesn't just handle this for us instead of
  135. calling this an error. Luckily, with WinSock, we can _also_ ask how
  136. many bits are set on an fd_set. So, let's just check it beforehand.
  137. */
  138. return select((int)maxfd + 1,
  139. fds_read && fds_read->fd_count ? fds_read : NULL,
  140. fds_write && fds_write->fd_count ? fds_write : NULL,
  141. fds_err && fds_err->fd_count ? fds_err : NULL, ptimeout);
  142. #else
  143. return select((int)maxfd + 1, fds_read, fds_write, fds_err, ptimeout);
  144. #endif
  145. }
  146. #endif
  147. /*
  148. * Wait for read or write events on a set of file descriptors. It uses poll()
  149. * when a fine poll() is available, in order to avoid limits with FD_SETSIZE,
  150. * otherwise select() is used. An error is returned if select() is being used
  151. * and a file descriptor is too large for FD_SETSIZE.
  152. *
  153. * A negative timeout value makes this function wait indefinitely,
  154. * unless no valid file descriptor is given, when this happens the
  155. * negative timeout is ignored and the function times out immediately.
  156. *
  157. * Return values:
  158. * -1 = system call error or fd >= FD_SETSIZE
  159. * 0 = timeout
  160. * [bitmask] = action as described below
  161. *
  162. * CURL_CSELECT_IN - first socket is readable
  163. * CURL_CSELECT_IN2 - second socket is readable
  164. * CURL_CSELECT_OUT - write socket is writable
  165. * CURL_CSELECT_ERR - an error condition occurred
  166. */
  167. int Curl_socket_check(curl_socket_t readfd0, /* two sockets to read from */
  168. curl_socket_t readfd1,
  169. curl_socket_t writefd, /* socket to write to */
  170. timediff_t timeout_ms) /* milliseconds to wait */
  171. {
  172. struct pollfd pfd[3];
  173. int num;
  174. int r;
  175. if((readfd0 == CURL_SOCKET_BAD) && (readfd1 == CURL_SOCKET_BAD) &&
  176. (writefd == CURL_SOCKET_BAD)) {
  177. /* no sockets, just wait */
  178. return Curl_wait_ms(timeout_ms);
  179. }
  180. /* Avoid initial timestamp, avoid Curl_now() call, when elapsed
  181. time in this function does not need to be measured. This happens
  182. when function is called with a zero timeout or a negative timeout
  183. value indicating a blocking call should be performed. */
  184. num = 0;
  185. if(readfd0 != CURL_SOCKET_BAD) {
  186. pfd[num].fd = readfd0;
  187. pfd[num].events = POLLRDNORM|POLLIN|POLLRDBAND|POLLPRI;
  188. pfd[num].revents = 0;
  189. num++;
  190. }
  191. if(readfd1 != CURL_SOCKET_BAD) {
  192. pfd[num].fd = readfd1;
  193. pfd[num].events = POLLRDNORM|POLLIN|POLLRDBAND|POLLPRI;
  194. pfd[num].revents = 0;
  195. num++;
  196. }
  197. if(writefd != CURL_SOCKET_BAD) {
  198. pfd[num].fd = writefd;
  199. pfd[num].events = POLLWRNORM|POLLOUT|POLLPRI;
  200. pfd[num].revents = 0;
  201. num++;
  202. }
  203. r = Curl_poll(pfd, num, timeout_ms);
  204. if(r <= 0)
  205. return r;
  206. r = 0;
  207. num = 0;
  208. if(readfd0 != CURL_SOCKET_BAD) {
  209. if(pfd[num].revents & (POLLRDNORM|POLLIN|POLLERR|POLLHUP))
  210. r |= CURL_CSELECT_IN;
  211. if(pfd[num].revents & (POLLRDBAND|POLLPRI|POLLNVAL))
  212. r |= CURL_CSELECT_ERR;
  213. num++;
  214. }
  215. if(readfd1 != CURL_SOCKET_BAD) {
  216. if(pfd[num].revents & (POLLRDNORM|POLLIN|POLLERR|POLLHUP))
  217. r |= CURL_CSELECT_IN2;
  218. if(pfd[num].revents & (POLLRDBAND|POLLPRI|POLLNVAL))
  219. r |= CURL_CSELECT_ERR;
  220. num++;
  221. }
  222. if(writefd != CURL_SOCKET_BAD) {
  223. if(pfd[num].revents & (POLLWRNORM|POLLOUT))
  224. r |= CURL_CSELECT_OUT;
  225. if(pfd[num].revents & (POLLERR|POLLHUP|POLLPRI|POLLNVAL))
  226. r |= CURL_CSELECT_ERR;
  227. }
  228. return r;
  229. }
  230. /*
  231. * This is a wrapper around poll(). If poll() does not exist, then
  232. * select() is used instead. An error is returned if select() is
  233. * being used and a file descriptor is too large for FD_SETSIZE.
  234. * A negative timeout value makes this function wait indefinitely,
  235. * unless no valid file descriptor is given, when this happens the
  236. * negative timeout is ignored and the function times out immediately.
  237. *
  238. * Return values:
  239. * -1 = system call error or fd >= FD_SETSIZE
  240. * 0 = timeout
  241. * N = number of structures with non zero revent fields
  242. */
  243. int Curl_poll(struct pollfd ufds[], unsigned int nfds, timediff_t timeout_ms)
  244. {
  245. #ifdef HAVE_POLL_FINE
  246. int pending_ms;
  247. #else
  248. fd_set fds_read;
  249. fd_set fds_write;
  250. fd_set fds_err;
  251. curl_socket_t maxfd;
  252. #endif
  253. bool fds_none = TRUE;
  254. unsigned int i;
  255. int r;
  256. if(ufds) {
  257. for(i = 0; i < nfds; i++) {
  258. if(ufds[i].fd != CURL_SOCKET_BAD) {
  259. fds_none = FALSE;
  260. break;
  261. }
  262. }
  263. }
  264. if(fds_none) {
  265. /* no sockets, just wait */
  266. return Curl_wait_ms(timeout_ms);
  267. }
  268. /* Avoid initial timestamp, avoid Curl_now() call, when elapsed
  269. time in this function does not need to be measured. This happens
  270. when function is called with a zero timeout or a negative timeout
  271. value indicating a blocking call should be performed. */
  272. #ifdef HAVE_POLL_FINE
  273. /* prevent overflow, timeout_ms is typecast to int. */
  274. #if TIMEDIFF_T_MAX > INT_MAX
  275. if(timeout_ms > INT_MAX)
  276. timeout_ms = INT_MAX;
  277. #endif
  278. if(timeout_ms > 0)
  279. pending_ms = (int)timeout_ms;
  280. else if(timeout_ms < 0)
  281. pending_ms = -1;
  282. else
  283. pending_ms = 0;
  284. r = poll(ufds, nfds, pending_ms);
  285. if(r <= 0) {
  286. if((r == -1) && (SOCKERRNO == EINTR))
  287. /* make EINTR from select or poll not a "lethal" error */
  288. r = 0;
  289. return r;
  290. }
  291. for(i = 0; i < nfds; i++) {
  292. if(ufds[i].fd == CURL_SOCKET_BAD)
  293. continue;
  294. if(ufds[i].revents & POLLHUP)
  295. ufds[i].revents |= POLLIN;
  296. if(ufds[i].revents & POLLERR)
  297. ufds[i].revents |= POLLIN|POLLOUT;
  298. }
  299. #else /* HAVE_POLL_FINE */
  300. FD_ZERO(&fds_read);
  301. FD_ZERO(&fds_write);
  302. FD_ZERO(&fds_err);
  303. maxfd = (curl_socket_t)-1;
  304. for(i = 0; i < nfds; i++) {
  305. ufds[i].revents = 0;
  306. if(ufds[i].fd == CURL_SOCKET_BAD)
  307. continue;
  308. VERIFY_SOCK(ufds[i].fd);
  309. if(ufds[i].events & (POLLIN|POLLOUT|POLLPRI|
  310. POLLRDNORM|POLLWRNORM|POLLRDBAND)) {
  311. if(ufds[i].fd > maxfd)
  312. maxfd = ufds[i].fd;
  313. if(ufds[i].events & (POLLRDNORM|POLLIN))
  314. FD_SET(ufds[i].fd, &fds_read);
  315. if(ufds[i].events & (POLLWRNORM|POLLOUT))
  316. FD_SET(ufds[i].fd, &fds_write);
  317. if(ufds[i].events & (POLLRDBAND|POLLPRI))
  318. FD_SET(ufds[i].fd, &fds_err);
  319. }
  320. }
  321. /*
  322. Note also that WinSock ignores the first argument, so we don't worry
  323. about the fact that maxfd is computed incorrectly with WinSock (since
  324. curl_socket_t is unsigned in such cases and thus -1 is the largest
  325. value).
  326. */
  327. r = our_select(maxfd, &fds_read, &fds_write, &fds_err, timeout_ms);
  328. if(r <= 0) {
  329. if((r == -1) && (SOCKERRNO == EINTR))
  330. /* make EINTR from select or poll not a "lethal" error */
  331. r = 0;
  332. return r;
  333. }
  334. r = 0;
  335. for(i = 0; i < nfds; i++) {
  336. ufds[i].revents = 0;
  337. if(ufds[i].fd == CURL_SOCKET_BAD)
  338. continue;
  339. if(FD_ISSET(ufds[i].fd, &fds_read)) {
  340. if(ufds[i].events & POLLRDNORM)
  341. ufds[i].revents |= POLLRDNORM;
  342. if(ufds[i].events & POLLIN)
  343. ufds[i].revents |= POLLIN;
  344. }
  345. if(FD_ISSET(ufds[i].fd, &fds_write)) {
  346. if(ufds[i].events & POLLWRNORM)
  347. ufds[i].revents |= POLLWRNORM;
  348. if(ufds[i].events & POLLOUT)
  349. ufds[i].revents |= POLLOUT;
  350. }
  351. if(FD_ISSET(ufds[i].fd, &fds_err)) {
  352. if(ufds[i].events & POLLRDBAND)
  353. ufds[i].revents |= POLLRDBAND;
  354. if(ufds[i].events & POLLPRI)
  355. ufds[i].revents |= POLLPRI;
  356. }
  357. if(ufds[i].revents)
  358. r++;
  359. }
  360. #endif /* HAVE_POLL_FINE */
  361. return r;
  362. }