select.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  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. fd_set *fds_read, /* sockets ready for reading */
  116. fd_set *fds_write, /* sockets ready for writing */
  117. 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. #else
  148. return select((int)maxfd + 1, fds_read, fds_write, fds_err, ptimeout);
  149. #endif
  150. }
  151. #endif
  152. /*
  153. * Wait for read or write events on a set of file descriptors. It uses poll()
  154. * when a fine poll() is available, in order to avoid limits with FD_SETSIZE,
  155. * otherwise select() is used. An error is returned if select() is being used
  156. * and a file descriptor is too large for FD_SETSIZE.
  157. *
  158. * A negative timeout value makes this function wait indefinitely,
  159. * unless no valid file descriptor is given, when this happens the
  160. * negative timeout is ignored and the function times out immediately.
  161. *
  162. * Return values:
  163. * -1 = system call error or fd >= FD_SETSIZE
  164. * 0 = timeout
  165. * [bitmask] = action as described below
  166. *
  167. * CURL_CSELECT_IN - first socket is readable
  168. * CURL_CSELECT_IN2 - second socket is readable
  169. * CURL_CSELECT_OUT - write socket is writable
  170. * CURL_CSELECT_ERR - an error condition occurred
  171. */
  172. int Curl_socket_check(curl_socket_t readfd0, /* two sockets to read from */
  173. curl_socket_t readfd1,
  174. curl_socket_t writefd, /* socket to write to */
  175. timediff_t timeout_ms) /* milliseconds to wait */
  176. {
  177. struct pollfd pfd[3];
  178. int num;
  179. int r;
  180. if((readfd0 == CURL_SOCKET_BAD) && (readfd1 == CURL_SOCKET_BAD) &&
  181. (writefd == CURL_SOCKET_BAD)) {
  182. /* no sockets, just wait */
  183. return Curl_wait_ms(timeout_ms);
  184. }
  185. /* Avoid initial timestamp, avoid Curl_now() call, when elapsed
  186. time in this function does not need to be measured. This happens
  187. when function is called with a zero timeout or a negative timeout
  188. value indicating a blocking call should be performed. */
  189. num = 0;
  190. if(readfd0 != CURL_SOCKET_BAD) {
  191. pfd[num].fd = readfd0;
  192. pfd[num].events = POLLRDNORM|POLLIN|POLLRDBAND|POLLPRI;
  193. pfd[num].revents = 0;
  194. num++;
  195. }
  196. if(readfd1 != CURL_SOCKET_BAD) {
  197. pfd[num].fd = readfd1;
  198. pfd[num].events = POLLRDNORM|POLLIN|POLLRDBAND|POLLPRI;
  199. pfd[num].revents = 0;
  200. num++;
  201. }
  202. if(writefd != CURL_SOCKET_BAD) {
  203. pfd[num].fd = writefd;
  204. pfd[num].events = POLLWRNORM|POLLOUT|POLLPRI;
  205. pfd[num].revents = 0;
  206. num++;
  207. }
  208. r = Curl_poll(pfd, num, timeout_ms);
  209. if(r <= 0)
  210. return r;
  211. r = 0;
  212. num = 0;
  213. if(readfd0 != CURL_SOCKET_BAD) {
  214. if(pfd[num].revents & (POLLRDNORM|POLLIN|POLLERR|POLLHUP))
  215. r |= CURL_CSELECT_IN;
  216. if(pfd[num].revents & (POLLPRI|POLLNVAL))
  217. r |= CURL_CSELECT_ERR;
  218. num++;
  219. }
  220. if(readfd1 != CURL_SOCKET_BAD) {
  221. if(pfd[num].revents & (POLLRDNORM|POLLIN|POLLERR|POLLHUP))
  222. r |= CURL_CSELECT_IN2;
  223. if(pfd[num].revents & (POLLPRI|POLLNVAL))
  224. r |= CURL_CSELECT_ERR;
  225. num++;
  226. }
  227. if(writefd != CURL_SOCKET_BAD) {
  228. if(pfd[num].revents & (POLLWRNORM|POLLOUT))
  229. r |= CURL_CSELECT_OUT;
  230. if(pfd[num].revents & (POLLERR|POLLHUP|POLLPRI|POLLNVAL))
  231. r |= CURL_CSELECT_ERR;
  232. }
  233. return r;
  234. }
  235. /*
  236. * This is a wrapper around poll(). If poll() does not exist, then
  237. * select() is used instead. An error is returned if select() is
  238. * being used and a file descriptor is too large for FD_SETSIZE.
  239. * A negative timeout value makes this function wait indefinitely,
  240. * unless no valid file descriptor is given, when this happens the
  241. * negative timeout is ignored and the function times out immediately.
  242. *
  243. * Return values:
  244. * -1 = system call error or fd >= FD_SETSIZE
  245. * 0 = timeout
  246. * N = number of structures with non zero revent fields
  247. */
  248. int Curl_poll(struct pollfd ufds[], unsigned int nfds, timediff_t timeout_ms)
  249. {
  250. #ifdef HAVE_POLL_FINE
  251. int pending_ms;
  252. #else
  253. fd_set fds_read;
  254. fd_set fds_write;
  255. fd_set fds_err;
  256. curl_socket_t maxfd;
  257. #endif
  258. bool fds_none = TRUE;
  259. unsigned int i;
  260. int r;
  261. if(ufds) {
  262. for(i = 0; i < nfds; i++) {
  263. if(ufds[i].fd != CURL_SOCKET_BAD) {
  264. fds_none = FALSE;
  265. break;
  266. }
  267. }
  268. }
  269. if(fds_none) {
  270. /* no sockets, just wait */
  271. return Curl_wait_ms(timeout_ms);
  272. }
  273. /* Avoid initial timestamp, avoid Curl_now() call, when elapsed
  274. time in this function does not need to be measured. This happens
  275. when function is called with a zero timeout or a negative timeout
  276. value indicating a blocking call should be performed. */
  277. #ifdef HAVE_POLL_FINE
  278. /* prevent overflow, timeout_ms is typecast to int. */
  279. #if TIMEDIFF_T_MAX > INT_MAX
  280. if(timeout_ms > INT_MAX)
  281. timeout_ms = INT_MAX;
  282. #endif
  283. if(timeout_ms > 0)
  284. pending_ms = (int)timeout_ms;
  285. else if(timeout_ms < 0)
  286. pending_ms = -1;
  287. else
  288. pending_ms = 0;
  289. r = poll(ufds, nfds, pending_ms);
  290. if(r <= 0) {
  291. if((r == -1) && (SOCKERRNO == EINTR))
  292. /* make EINTR from select or poll not a "lethal" error */
  293. r = 0;
  294. return r;
  295. }
  296. for(i = 0; i < nfds; i++) {
  297. if(ufds[i].fd == CURL_SOCKET_BAD)
  298. continue;
  299. if(ufds[i].revents & POLLHUP)
  300. ufds[i].revents |= POLLIN;
  301. if(ufds[i].revents & POLLERR)
  302. ufds[i].revents |= POLLIN|POLLOUT;
  303. }
  304. #else /* HAVE_POLL_FINE */
  305. FD_ZERO(&fds_read);
  306. FD_ZERO(&fds_write);
  307. FD_ZERO(&fds_err);
  308. maxfd = (curl_socket_t)-1;
  309. for(i = 0; i < nfds; i++) {
  310. ufds[i].revents = 0;
  311. if(ufds[i].fd == CURL_SOCKET_BAD)
  312. continue;
  313. VERIFY_SOCK(ufds[i].fd);
  314. if(ufds[i].events & (POLLIN|POLLOUT|POLLPRI|
  315. POLLRDNORM|POLLWRNORM|POLLRDBAND)) {
  316. if(ufds[i].fd > maxfd)
  317. maxfd = ufds[i].fd;
  318. if(ufds[i].events & (POLLRDNORM|POLLIN))
  319. FD_SET(ufds[i].fd, &fds_read);
  320. if(ufds[i].events & (POLLWRNORM|POLLOUT))
  321. FD_SET(ufds[i].fd, &fds_write);
  322. if(ufds[i].events & (POLLRDBAND|POLLPRI))
  323. FD_SET(ufds[i].fd, &fds_err);
  324. }
  325. }
  326. /*
  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 = our_select(maxfd, &fds_read, &fds_write, &fds_err, timeout_ms);
  333. if(r <= 0) {
  334. if((r == -1) && (SOCKERRNO == EINTR))
  335. /* make EINTR from select or poll not a "lethal" error */
  336. r = 0;
  337. return r;
  338. }
  339. r = 0;
  340. for(i = 0; i < nfds; i++) {
  341. ufds[i].revents = 0;
  342. if(ufds[i].fd == CURL_SOCKET_BAD)
  343. continue;
  344. if(FD_ISSET(ufds[i].fd, &fds_read)) {
  345. if(ufds[i].events & POLLRDNORM)
  346. ufds[i].revents |= POLLRDNORM;
  347. if(ufds[i].events & POLLIN)
  348. ufds[i].revents |= POLLIN;
  349. }
  350. if(FD_ISSET(ufds[i].fd, &fds_write)) {
  351. if(ufds[i].events & POLLWRNORM)
  352. ufds[i].revents |= POLLWRNORM;
  353. if(ufds[i].events & POLLOUT)
  354. ufds[i].revents |= POLLOUT;
  355. }
  356. if(FD_ISSET(ufds[i].fd, &fds_err)) {
  357. if(ufds[i].events & POLLRDBAND)
  358. ufds[i].revents |= POLLRDBAND;
  359. if(ufds[i].events & POLLPRI)
  360. ufds[i].revents |= POLLPRI;
  361. }
  362. if(ufds[i].revents)
  363. r++;
  364. }
  365. #endif /* HAVE_POLL_FINE */
  366. return r;
  367. }