select.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2012, 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 http://curl.haxx.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 "setup.h"
  23. #ifdef HAVE_SYS_SELECT_H
  24. #include <sys/select.h>
  25. #endif
  26. #if !defined(HAVE_SELECT) && !defined(HAVE_POLL_FINE)
  27. #error "We can't compile without select() or poll() support."
  28. #endif
  29. #if defined(__BEOS__) && !defined(__HAIKU__)
  30. /* BeOS has FD_SET defined in socket.h */
  31. #include <socket.h>
  32. #endif
  33. #ifdef MSDOS
  34. #include <dos.h> /* delay() */
  35. #endif
  36. #include <curl/curl.h>
  37. #include "urldata.h"
  38. #include "connect.h"
  39. #include "select.h"
  40. #include "warnless.h"
  41. /* Convenience local macros */
  42. #define elapsed_ms (int)curlx_tvdiff(curlx_tvnow(), initial_tv)
  43. #ifdef CURL_ACKNOWLEDGE_EINTR
  44. #define error_not_EINTR (1)
  45. #else
  46. #define error_not_EINTR (error != EINTR)
  47. #endif
  48. /*
  49. * Internal function used for waiting a specific amount of ms
  50. * in Curl_socket_ready() 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. * On non-DOS and non-Winsock platforms, when compiled with
  60. * CURL_ACKNOWLEDGE_EINTR defined, EINTR condition is honored
  61. * and function might exit early without awaiting full timeout,
  62. * otherwise EINTR will be ignored and full timeout will elapse.
  63. *
  64. * Return values:
  65. * -1 = system call error, invalid timeout value, or interrupted
  66. * 0 = specified timeout has elapsed
  67. */
  68. int Curl_wait_ms(int timeout_ms)
  69. {
  70. #if !defined(MSDOS) && !defined(USE_WINSOCK)
  71. #ifndef HAVE_POLL_FINE
  72. struct timeval pending_tv;
  73. #endif
  74. struct timeval initial_tv;
  75. int pending_ms;
  76. int error;
  77. #endif
  78. int r = 0;
  79. if(!timeout_ms)
  80. return 0;
  81. if(timeout_ms < 0) {
  82. SET_SOCKERRNO(EINVAL);
  83. return -1;
  84. }
  85. #if defined(MSDOS)
  86. delay(timeout_ms);
  87. #elif defined(USE_WINSOCK)
  88. Sleep(timeout_ms);
  89. #else
  90. pending_ms = timeout_ms;
  91. initial_tv = curlx_tvnow();
  92. do {
  93. #if defined(HAVE_POLL_FINE)
  94. r = poll(NULL, 0, pending_ms);
  95. #else
  96. pending_tv.tv_sec = pending_ms / 1000;
  97. pending_tv.tv_usec = (pending_ms % 1000) * 1000;
  98. r = select(0, NULL, NULL, NULL, &pending_tv);
  99. #endif /* HAVE_POLL_FINE */
  100. if(r != -1)
  101. break;
  102. error = SOCKERRNO;
  103. if(error && error_not_EINTR)
  104. break;
  105. pending_ms = timeout_ms - elapsed_ms;
  106. if(pending_ms <= 0)
  107. break;
  108. } while(r == -1);
  109. #endif /* USE_WINSOCK */
  110. if(r)
  111. r = -1;
  112. return r;
  113. }
  114. /*
  115. * Wait for read or write events on a set of file descriptors. It uses poll()
  116. * when a fine poll() is available, in order to avoid limits with FD_SETSIZE,
  117. * otherwise select() is used. An error is returned if select() is being used
  118. * and a file descriptor is too large for FD_SETSIZE.
  119. *
  120. * A negative timeout value makes this function wait indefinitely,
  121. * unles no valid file descriptor is given, when this happens the
  122. * negative timeout is ignored and the function times out immediately.
  123. * When compiled with CURL_ACKNOWLEDGE_EINTR defined, EINTR condition
  124. * is honored and function might exit early without awaiting timeout,
  125. * otherwise EINTR will be ignored.
  126. *
  127. * Return values:
  128. * -1 = system call error or fd >= FD_SETSIZE
  129. * 0 = timeout
  130. * [bitmask] = action as described below
  131. *
  132. * CURL_CSELECT_IN - first socket is readable
  133. * CURL_CSELECT_IN2 - second socket is readable
  134. * CURL_CSELECT_OUT - write socket is writable
  135. * CURL_CSELECT_ERR - an error condition occurred
  136. */
  137. int Curl_socket_check(curl_socket_t readfd0, /* two sockets to read from */
  138. curl_socket_t readfd1,
  139. curl_socket_t writefd, /* socket to write to */
  140. long timeout_ms) /* milliseconds to wait */
  141. {
  142. #ifdef HAVE_POLL_FINE
  143. struct pollfd pfd[3];
  144. int num;
  145. #else
  146. struct timeval pending_tv;
  147. struct timeval *ptimeout;
  148. fd_set fds_read;
  149. fd_set fds_write;
  150. fd_set fds_err;
  151. curl_socket_t maxfd;
  152. #endif
  153. struct timeval initial_tv = {0,0};
  154. int pending_ms = 0;
  155. int error;
  156. int r;
  157. int ret;
  158. if((readfd0 == CURL_SOCKET_BAD) && (readfd1 == CURL_SOCKET_BAD) &&
  159. (writefd == CURL_SOCKET_BAD)) {
  160. /* no sockets, just wait */
  161. r = Curl_wait_ms((int)timeout_ms);
  162. return r;
  163. }
  164. /* Avoid initial timestamp, avoid curlx_tvnow() call, when elapsed
  165. time in this function does not need to be measured. This happens
  166. when function is called with a zero timeout or a negative timeout
  167. value indicating a blocking call should be performed. */
  168. if(timeout_ms > 0) {
  169. pending_ms = (int)timeout_ms;
  170. initial_tv = curlx_tvnow();
  171. }
  172. #ifdef HAVE_POLL_FINE
  173. num = 0;
  174. if(readfd0 != CURL_SOCKET_BAD) {
  175. pfd[num].fd = readfd0;
  176. pfd[num].events = POLLRDNORM|POLLIN|POLLRDBAND|POLLPRI;
  177. pfd[num].revents = 0;
  178. num++;
  179. }
  180. if(readfd1 != CURL_SOCKET_BAD) {
  181. pfd[num].fd = readfd1;
  182. pfd[num].events = POLLRDNORM|POLLIN|POLLRDBAND|POLLPRI;
  183. pfd[num].revents = 0;
  184. num++;
  185. }
  186. if(writefd != CURL_SOCKET_BAD) {
  187. pfd[num].fd = writefd;
  188. pfd[num].events = POLLWRNORM|POLLOUT;
  189. pfd[num].revents = 0;
  190. num++;
  191. }
  192. do {
  193. if(timeout_ms < 0)
  194. pending_ms = -1;
  195. else if(!timeout_ms)
  196. pending_ms = 0;
  197. r = poll(pfd, num, pending_ms);
  198. if(r != -1)
  199. break;
  200. error = SOCKERRNO;
  201. if(error && error_not_EINTR)
  202. break;
  203. if(timeout_ms > 0) {
  204. pending_ms = (int)(timeout_ms - elapsed_ms);
  205. if(pending_ms <= 0) {
  206. r = 0; /* Simulate a "call timed out" case */
  207. break;
  208. }
  209. }
  210. } while(r == -1);
  211. if(r < 0)
  212. return -1;
  213. if(r == 0)
  214. return 0;
  215. ret = 0;
  216. num = 0;
  217. if(readfd0 != CURL_SOCKET_BAD) {
  218. if(pfd[num].revents & (POLLRDNORM|POLLIN|POLLERR|POLLHUP))
  219. ret |= CURL_CSELECT_IN;
  220. if(pfd[num].revents & (POLLRDBAND|POLLPRI|POLLNVAL))
  221. ret |= CURL_CSELECT_ERR;
  222. num++;
  223. }
  224. if(readfd1 != CURL_SOCKET_BAD) {
  225. if(pfd[num].revents & (POLLRDNORM|POLLIN|POLLERR|POLLHUP))
  226. ret |= CURL_CSELECT_IN2;
  227. if(pfd[num].revents & (POLLRDBAND|POLLPRI|POLLNVAL))
  228. ret |= CURL_CSELECT_ERR;
  229. num++;
  230. }
  231. if(writefd != CURL_SOCKET_BAD) {
  232. if(pfd[num].revents & (POLLWRNORM|POLLOUT))
  233. ret |= CURL_CSELECT_OUT;
  234. if(pfd[num].revents & (POLLERR|POLLHUP|POLLNVAL))
  235. ret |= CURL_CSELECT_ERR;
  236. }
  237. return ret;
  238. #else /* HAVE_POLL_FINE */
  239. FD_ZERO(&fds_err);
  240. maxfd = (curl_socket_t)-1;
  241. FD_ZERO(&fds_read);
  242. if(readfd0 != CURL_SOCKET_BAD) {
  243. VERIFY_SOCK(readfd0);
  244. FD_SET(readfd0, &fds_read);
  245. FD_SET(readfd0, &fds_err);
  246. maxfd = readfd0;
  247. }
  248. if(readfd1 != CURL_SOCKET_BAD) {
  249. VERIFY_SOCK(readfd1);
  250. FD_SET(readfd1, &fds_read);
  251. FD_SET(readfd1, &fds_err);
  252. if(readfd1 > maxfd)
  253. maxfd = readfd1;
  254. }
  255. FD_ZERO(&fds_write);
  256. if(writefd != CURL_SOCKET_BAD) {
  257. VERIFY_SOCK(writefd);
  258. FD_SET(writefd, &fds_write);
  259. FD_SET(writefd, &fds_err);
  260. if(writefd > maxfd)
  261. maxfd = writefd;
  262. }
  263. ptimeout = (timeout_ms < 0) ? NULL : &pending_tv;
  264. do {
  265. if(timeout_ms > 0) {
  266. pending_tv.tv_sec = pending_ms / 1000;
  267. pending_tv.tv_usec = (pending_ms % 1000) * 1000;
  268. }
  269. else if(!timeout_ms) {
  270. pending_tv.tv_sec = 0;
  271. pending_tv.tv_usec = 0;
  272. }
  273. r = select((int)maxfd + 1, &fds_read, &fds_write, &fds_err, ptimeout);
  274. if(r != -1)
  275. break;
  276. error = SOCKERRNO;
  277. if(error && error_not_EINTR)
  278. break;
  279. if(timeout_ms > 0) {
  280. pending_ms = timeout_ms - elapsed_ms;
  281. if(pending_ms <= 0) {
  282. r = 0; /* Simulate a "call timed out" case */
  283. break;
  284. }
  285. }
  286. } while(r == -1);
  287. if(r < 0)
  288. return -1;
  289. if(r == 0)
  290. return 0;
  291. ret = 0;
  292. if(readfd0 != CURL_SOCKET_BAD) {
  293. if(FD_ISSET(readfd0, &fds_read))
  294. ret |= CURL_CSELECT_IN;
  295. if(FD_ISSET(readfd0, &fds_err))
  296. ret |= CURL_CSELECT_ERR;
  297. }
  298. if(readfd1 != CURL_SOCKET_BAD) {
  299. if(FD_ISSET(readfd1, &fds_read))
  300. ret |= CURL_CSELECT_IN2;
  301. if(FD_ISSET(readfd1, &fds_err))
  302. ret |= CURL_CSELECT_ERR;
  303. }
  304. if(writefd != CURL_SOCKET_BAD) {
  305. if(FD_ISSET(writefd, &fds_write))
  306. ret |= CURL_CSELECT_OUT;
  307. if(FD_ISSET(writefd, &fds_err))
  308. ret |= CURL_CSELECT_ERR;
  309. }
  310. return ret;
  311. #endif /* HAVE_POLL_FINE */
  312. }
  313. /*
  314. * This is a wrapper around poll(). If poll() does not exist, then
  315. * select() is used instead. An error is returned if select() is
  316. * being used and a file descriptor is too large for FD_SETSIZE.
  317. * A negative timeout value makes this function wait indefinitely,
  318. * unles no valid file descriptor is given, when this happens the
  319. * negative timeout is ignored and the function times out immediately.
  320. * When compiled with CURL_ACKNOWLEDGE_EINTR defined, EINTR condition
  321. * is honored and function might exit early without awaiting timeout,
  322. * otherwise EINTR will be ignored.
  323. *
  324. * Return values:
  325. * -1 = system call error or fd >= FD_SETSIZE
  326. * 0 = timeout
  327. * N = number of structures with non zero revent fields
  328. */
  329. int Curl_poll(struct pollfd ufds[], unsigned int nfds, int timeout_ms)
  330. {
  331. #ifndef HAVE_POLL_FINE
  332. struct timeval pending_tv;
  333. struct timeval *ptimeout;
  334. fd_set fds_read;
  335. fd_set fds_write;
  336. fd_set fds_err;
  337. curl_socket_t maxfd;
  338. #endif
  339. struct timeval initial_tv = {0,0};
  340. bool fds_none = TRUE;
  341. unsigned int i;
  342. int pending_ms = 0;
  343. int error;
  344. int r;
  345. if(ufds) {
  346. for(i = 0; i < nfds; i++) {
  347. if(ufds[i].fd != CURL_SOCKET_BAD) {
  348. fds_none = FALSE;
  349. break;
  350. }
  351. }
  352. }
  353. if(fds_none) {
  354. r = Curl_wait_ms(timeout_ms);
  355. return r;
  356. }
  357. /* Avoid initial timestamp, avoid curlx_tvnow() call, when elapsed
  358. time in this function does not need to be measured. This happens
  359. when function is called with a zero timeout or a negative timeout
  360. value indicating a blocking call should be performed. */
  361. if(timeout_ms > 0) {
  362. pending_ms = timeout_ms;
  363. initial_tv = curlx_tvnow();
  364. }
  365. #ifdef HAVE_POLL_FINE
  366. do {
  367. if(timeout_ms < 0)
  368. pending_ms = -1;
  369. else if(!timeout_ms)
  370. pending_ms = 0;
  371. r = poll(ufds, nfds, pending_ms);
  372. if(r != -1)
  373. break;
  374. error = SOCKERRNO;
  375. if(error && error_not_EINTR)
  376. break;
  377. if(timeout_ms > 0) {
  378. pending_ms = timeout_ms - elapsed_ms;
  379. if(pending_ms <= 0)
  380. break;
  381. }
  382. } while(r == -1);
  383. if(r < 0)
  384. return -1;
  385. if(r == 0)
  386. return 0;
  387. for(i = 0; i < nfds; i++) {
  388. if(ufds[i].fd == CURL_SOCKET_BAD)
  389. continue;
  390. if(ufds[i].revents & POLLHUP)
  391. ufds[i].revents |= POLLIN;
  392. if(ufds[i].revents & POLLERR)
  393. ufds[i].revents |= (POLLIN|POLLOUT);
  394. }
  395. #else /* HAVE_POLL_FINE */
  396. FD_ZERO(&fds_read);
  397. FD_ZERO(&fds_write);
  398. FD_ZERO(&fds_err);
  399. maxfd = (curl_socket_t)-1;
  400. for(i = 0; i < nfds; i++) {
  401. ufds[i].revents = 0;
  402. if(ufds[i].fd == CURL_SOCKET_BAD)
  403. continue;
  404. VERIFY_SOCK(ufds[i].fd);
  405. if(ufds[i].events & (POLLIN|POLLOUT|POLLPRI|
  406. POLLRDNORM|POLLWRNORM|POLLRDBAND)) {
  407. if(ufds[i].fd > maxfd)
  408. maxfd = ufds[i].fd;
  409. if(ufds[i].events & (POLLRDNORM|POLLIN))
  410. FD_SET(ufds[i].fd, &fds_read);
  411. if(ufds[i].events & (POLLWRNORM|POLLOUT))
  412. FD_SET(ufds[i].fd, &fds_write);
  413. if(ufds[i].events & (POLLRDBAND|POLLPRI))
  414. FD_SET(ufds[i].fd, &fds_err);
  415. }
  416. }
  417. ptimeout = (timeout_ms < 0) ? NULL : &pending_tv;
  418. do {
  419. if(timeout_ms > 0) {
  420. pending_tv.tv_sec = pending_ms / 1000;
  421. pending_tv.tv_usec = (pending_ms % 1000) * 1000;
  422. }
  423. else if(!timeout_ms) {
  424. pending_tv.tv_sec = 0;
  425. pending_tv.tv_usec = 0;
  426. }
  427. r = select((int)maxfd + 1, &fds_read, &fds_write, &fds_err, ptimeout);
  428. if(r != -1)
  429. break;
  430. error = SOCKERRNO;
  431. if(error && error_not_EINTR)
  432. break;
  433. if(timeout_ms > 0) {
  434. pending_ms = timeout_ms - elapsed_ms;
  435. if(pending_ms <= 0)
  436. break;
  437. }
  438. } while(r == -1);
  439. if(r < 0)
  440. return -1;
  441. if(r == 0)
  442. return 0;
  443. r = 0;
  444. for(i = 0; i < nfds; i++) {
  445. ufds[i].revents = 0;
  446. if(ufds[i].fd == CURL_SOCKET_BAD)
  447. continue;
  448. if(FD_ISSET(ufds[i].fd, &fds_read))
  449. ufds[i].revents |= POLLIN;
  450. if(FD_ISSET(ufds[i].fd, &fds_write))
  451. ufds[i].revents |= POLLOUT;
  452. if(FD_ISSET(ufds[i].fd, &fds_err))
  453. ufds[i].revents |= POLLPRI;
  454. if(ufds[i].revents != 0)
  455. r++;
  456. }
  457. #endif /* HAVE_POLL_FINE */
  458. return r;
  459. }
  460. #ifdef TPF
  461. /*
  462. * This is a replacement for select() on the TPF platform.
  463. * It is used whenever libcurl calls select().
  464. * The call below to tpf_process_signals() is required because
  465. * TPF's select calls are not signal interruptible.
  466. *
  467. * Return values are the same as select's.
  468. */
  469. int tpf_select_libcurl(int maxfds, fd_set* reads, fd_set* writes,
  470. fd_set* excepts, struct timeval* tv)
  471. {
  472. int rc;
  473. rc = tpf_select_bsd(maxfds, reads, writes, excepts, tv);
  474. tpf_process_signals();
  475. return(rc);
  476. }
  477. #endif /* TPF */