select.c 14 KB

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