select.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 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, or invalid timeout value
  57. * 0 = specified timeout has elapsed, or interrupted
  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. if((r == -1) && (SOCKERRNO == EINTR))
  95. /* make EINTR from select or poll not a "lethal" error */
  96. r = 0;
  97. else
  98. r = -1;
  99. }
  100. return r;
  101. }
  102. #ifndef HAVE_POLL_FINE
  103. /*
  104. * This is a wrapper around select() to aid in Windows compatibility.
  105. * A negative timeout value makes this function wait indefinitely,
  106. * unless no valid file descriptor is given, when this happens the
  107. * negative timeout is ignored and the function times out immediately.
  108. *
  109. * Return values:
  110. * -1 = system call error or fd >= FD_SETSIZE
  111. * 0 = timeout
  112. * N = number of signalled file descriptors
  113. */
  114. static int our_select(curl_socket_t maxfd, /* highest socket number */
  115. curl_fd_set *fds_read, /* sockets ready for reading */
  116. curl_fd_set *fds_write, /* sockets ready for writing */
  117. curl_fd_set *fds_err, /* sockets with errors */
  118. timediff_t timeout_ms) /* milliseconds to wait */
  119. {
  120. struct timeval pending_tv;
  121. struct timeval *ptimeout;
  122. #ifdef USE_WINSOCK
  123. /* WinSock select() can't handle zero events. See the comment below. */
  124. if((!fds_read || fds_read->fd_count == 0) &&
  125. (!fds_write || fds_write->fd_count == 0) &&
  126. (!fds_err || fds_err->fd_count == 0)) {
  127. /* no sockets, just wait */
  128. return Curl_wait_ms(timeout_ms);
  129. }
  130. #endif
  131. ptimeout = curlx_mstotv(&pending_tv, timeout_ms);
  132. #ifdef USE_WINSOCK
  133. /* WinSock select() must not be called with an fd_set that contains zero
  134. fd flags, or it will return WSAEINVAL. But, it also can't be called
  135. with no fd_sets at all! From the documentation:
  136. Any two of the parameters, readfds, writefds, or exceptfds, can be
  137. given as null. At least one must be non-null, and any non-null
  138. descriptor set must contain at least one handle to a socket.
  139. It is unclear why WinSock doesn't just handle this for us instead of
  140. calling this an error. Luckily, with WinSock, we can _also_ ask how
  141. many bits are set on an fd_set. So, let's just check it beforehand.
  142. */
  143. return select((int)maxfd + 1,
  144. fds_read && fds_read->fd_count ? fds_read : NULL,
  145. fds_write && fds_write->fd_count ? fds_write : NULL,
  146. fds_err && fds_err->fd_count ? fds_err : NULL, ptimeout);
  147. #elif defined(FreeRTOS)
  148. {
  149. /* convert the timeout to FreeRTOS ticks */
  150. TickType_t ticks = ptimeout ? ptimeout->tv_sec * configTICK_RATE_HZ : +
  151. (ptimeout->tv_usec * configTICK_RATE_HZ) / 1000000;
  152. return FreeRTOS_select(fds_read, ticks);
  153. }
  154. #else
  155. return select((int)maxfd + 1, fds_read, fds_write, fds_err, ptimeout);
  156. #endif
  157. }
  158. #endif
  159. /*
  160. * Wait for read or write events on a set of file descriptors. It uses poll()
  161. * when a fine poll() is available, in order to avoid limits with FD_SETSIZE,
  162. * otherwise select() is used. An error is returned if select() is being used
  163. * and a file descriptor is too large for FD_SETSIZE.
  164. *
  165. * A negative timeout value makes this function wait indefinitely,
  166. * unless no valid file descriptor is given, when this happens the
  167. * negative timeout is ignored and the function times out immediately.
  168. *
  169. * Return values:
  170. * -1 = system call error or fd >= FD_SETSIZE
  171. * 0 = timeout
  172. * [bitmask] = action as described below
  173. *
  174. * CURL_CSELECT_IN - first socket is readable
  175. * CURL_CSELECT_IN2 - second socket is readable
  176. * CURL_CSELECT_OUT - write socket is writable
  177. * CURL_CSELECT_ERR - an error condition occurred
  178. */
  179. int Curl_socket_check(curl_socket_t readfd0, /* two sockets to read from */
  180. curl_socket_t readfd1,
  181. curl_socket_t writefd, /* socket to write to */
  182. timediff_t timeout_ms) /* milliseconds to wait */
  183. {
  184. struct pollfd pfd[3];
  185. int num;
  186. int r;
  187. if((readfd0 == CURL_SOCKET_BAD) && (readfd1 == CURL_SOCKET_BAD) &&
  188. (writefd == CURL_SOCKET_BAD)) {
  189. /* no sockets, just wait */
  190. return Curl_wait_ms(timeout_ms);
  191. }
  192. /* Avoid initial timestamp, avoid Curl_now() call, when elapsed
  193. time in this function does not need to be measured. This happens
  194. when function is called with a zero timeout or a negative timeout
  195. value indicating a blocking call should be performed. */
  196. num = 0;
  197. if(readfd0 != CURL_SOCKET_BAD) {
  198. pfd[num].fd = readfd0;
  199. pfd[num].events = POLLRDNORM|POLLIN|POLLRDBAND|POLLPRI;
  200. pfd[num].revents = 0;
  201. num++;
  202. }
  203. if(readfd1 != CURL_SOCKET_BAD) {
  204. pfd[num].fd = readfd1;
  205. pfd[num].events = POLLRDNORM|POLLIN|POLLRDBAND|POLLPRI;
  206. pfd[num].revents = 0;
  207. num++;
  208. }
  209. if(writefd != CURL_SOCKET_BAD) {
  210. pfd[num].fd = writefd;
  211. pfd[num].events = POLLWRNORM|POLLOUT|POLLPRI;
  212. pfd[num].revents = 0;
  213. num++;
  214. }
  215. r = Curl_poll(pfd, num, timeout_ms);
  216. if(r <= 0)
  217. return r;
  218. r = 0;
  219. num = 0;
  220. if(readfd0 != CURL_SOCKET_BAD) {
  221. if(pfd[num].revents & (POLLRDNORM|POLLIN|POLLERR|POLLHUP))
  222. r |= CURL_CSELECT_IN;
  223. if(pfd[num].revents & (POLLPRI|POLLNVAL))
  224. r |= CURL_CSELECT_ERR;
  225. num++;
  226. }
  227. if(readfd1 != CURL_SOCKET_BAD) {
  228. if(pfd[num].revents & (POLLRDNORM|POLLIN|POLLERR|POLLHUP))
  229. r |= CURL_CSELECT_IN2;
  230. if(pfd[num].revents & (POLLPRI|POLLNVAL))
  231. r |= CURL_CSELECT_ERR;
  232. num++;
  233. }
  234. if(writefd != CURL_SOCKET_BAD) {
  235. if(pfd[num].revents & (POLLWRNORM|POLLOUT))
  236. r |= CURL_CSELECT_OUT;
  237. if(pfd[num].revents & (POLLERR|POLLHUP|POLLPRI|POLLNVAL))
  238. r |= CURL_CSELECT_ERR;
  239. }
  240. return r;
  241. }
  242. /*
  243. * This is a wrapper around poll(). If poll() does not exist, then
  244. * select() is used instead. An error is returned if select() is
  245. * being used and a file descriptor is too large for FD_SETSIZE.
  246. * A negative timeout value makes this function wait indefinitely,
  247. * unless no valid file descriptor is given, when this happens the
  248. * negative timeout is ignored and the function times out immediately.
  249. *
  250. * Return values:
  251. * -1 = system call error or fd >= FD_SETSIZE
  252. * 0 = timeout
  253. * N = number of structures with non zero revent fields
  254. */
  255. int Curl_poll(struct pollfd ufds[], unsigned int nfds, timediff_t timeout_ms)
  256. {
  257. #ifdef HAVE_POLL_FINE
  258. int pending_ms;
  259. #else
  260. curl_fd_set fds_read;
  261. curl_fd_set fds_write;
  262. curl_fd_set fds_err;
  263. curl_socket_t maxfd;
  264. #endif
  265. bool fds_none = TRUE;
  266. unsigned int i;
  267. int r;
  268. if(ufds) {
  269. for(i = 0; i < nfds; i++) {
  270. if(ufds[i].fd != CURL_SOCKET_BAD) {
  271. fds_none = FALSE;
  272. break;
  273. }
  274. }
  275. }
  276. if(fds_none) {
  277. /* no sockets, just wait */
  278. return Curl_wait_ms(timeout_ms);
  279. }
  280. /* Avoid initial timestamp, avoid Curl_now() call, when elapsed
  281. time in this function does not need to be measured. This happens
  282. when function is called with a zero timeout or a negative timeout
  283. value indicating a blocking call should be performed. */
  284. #ifdef HAVE_POLL_FINE
  285. /* prevent overflow, timeout_ms is typecast to int. */
  286. #if TIMEDIFF_T_MAX > INT_MAX
  287. if(timeout_ms > INT_MAX)
  288. timeout_ms = INT_MAX;
  289. #endif
  290. if(timeout_ms > 0)
  291. pending_ms = (int)timeout_ms;
  292. else if(timeout_ms < 0)
  293. pending_ms = -1;
  294. else
  295. pending_ms = 0;
  296. r = poll(ufds, nfds, pending_ms);
  297. if(r <= 0) {
  298. if((r == -1) && (SOCKERRNO == EINTR))
  299. /* make EINTR from select or poll not a "lethal" error */
  300. r = 0;
  301. return r;
  302. }
  303. for(i = 0; i < nfds; i++) {
  304. if(ufds[i].fd == CURL_SOCKET_BAD)
  305. continue;
  306. if(ufds[i].revents & POLLHUP)
  307. ufds[i].revents |= POLLIN;
  308. if(ufds[i].revents & POLLERR)
  309. ufds[i].revents |= POLLIN|POLLOUT;
  310. }
  311. #else /* HAVE_POLL_FINE */
  312. FD_ZERO(&fds_read);
  313. FD_ZERO(&fds_write);
  314. FD_ZERO(&fds_err);
  315. maxfd = (curl_socket_t)-1;
  316. for(i = 0; i < nfds; i++) {
  317. ufds[i].revents = 0;
  318. if(ufds[i].fd == CURL_SOCKET_BAD)
  319. continue;
  320. VERIFY_SOCK(ufds[i].fd);
  321. if(ufds[i].events & (POLLIN|POLLOUT|POLLPRI|
  322. POLLRDNORM|POLLWRNORM|POLLRDBAND)) {
  323. if(ufds[i].fd > maxfd)
  324. maxfd = ufds[i].fd;
  325. if(ufds[i].events & (POLLRDNORM|POLLIN))
  326. FD_SET(ufds[i].fd, &fds_read);
  327. if(ufds[i].events & (POLLWRNORM|POLLOUT))
  328. FD_SET(ufds[i].fd, &fds_write);
  329. if(ufds[i].events & (POLLRDBAND|POLLPRI))
  330. FD_SET(ufds[i].fd, &fds_err);
  331. }
  332. }
  333. /*
  334. Note also that WinSock ignores the first argument, so we don't worry
  335. about the fact that maxfd is computed incorrectly with WinSock (since
  336. curl_socket_t is unsigned in such cases and thus -1 is the largest
  337. value).
  338. */
  339. r = our_select(maxfd, &fds_read, &fds_write, &fds_err, timeout_ms);
  340. if(r <= 0) {
  341. if((r == -1) && (SOCKERRNO == EINTR))
  342. /* make EINTR from select or poll not a "lethal" error */
  343. r = 0;
  344. return r;
  345. }
  346. r = 0;
  347. for(i = 0; i < nfds; i++) {
  348. ufds[i].revents = 0;
  349. if(ufds[i].fd == CURL_SOCKET_BAD)
  350. continue;
  351. if(FD_ISSET(ufds[i].fd, &fds_read)) {
  352. if(ufds[i].events & POLLRDNORM)
  353. ufds[i].revents |= POLLRDNORM;
  354. if(ufds[i].events & POLLIN)
  355. ufds[i].revents |= POLLIN;
  356. }
  357. if(FD_ISSET(ufds[i].fd, &fds_write)) {
  358. if(ufds[i].events & POLLWRNORM)
  359. ufds[i].revents |= POLLWRNORM;
  360. if(ufds[i].events & POLLOUT)
  361. ufds[i].revents |= POLLOUT;
  362. }
  363. if(FD_ISSET(ufds[i].fd, &fds_err)) {
  364. if(ufds[i].events & POLLRDBAND)
  365. ufds[i].revents |= POLLRDBAND;
  366. if(ufds[i].events & POLLPRI)
  367. ufds[i].revents |= POLLPRI;
  368. }
  369. if(ufds[i].revents)
  370. r++;
  371. }
  372. #endif /* HAVE_POLL_FINE */
  373. return r;
  374. }