asyn-thread.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2021, 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 "socketpair.h"
  24. /***********************************************************************
  25. * Only for threaded name resolves builds
  26. **********************************************************************/
  27. #ifdef CURLRES_THREADED
  28. #ifdef HAVE_NETINET_IN_H
  29. #include <netinet/in.h>
  30. #endif
  31. #ifdef HAVE_NETDB_H
  32. #include <netdb.h>
  33. #endif
  34. #ifdef HAVE_ARPA_INET_H
  35. #include <arpa/inet.h>
  36. #endif
  37. #ifdef __VMS
  38. #include <in.h>
  39. #include <inet.h>
  40. #endif
  41. #if defined(USE_THREADS_POSIX)
  42. # ifdef HAVE_PTHREAD_H
  43. # include <pthread.h>
  44. # endif
  45. #elif defined(USE_THREADS_WIN32)
  46. # ifdef HAVE_PROCESS_H
  47. # include <process.h>
  48. # endif
  49. #endif
  50. #if (defined(NETWARE) && defined(__NOVELL_LIBC__))
  51. #undef in_addr_t
  52. #define in_addr_t unsigned long
  53. #endif
  54. #ifdef HAVE_GETADDRINFO
  55. # define RESOLVER_ENOMEM EAI_MEMORY
  56. #else
  57. # define RESOLVER_ENOMEM ENOMEM
  58. #endif
  59. #include "urldata.h"
  60. #include "sendf.h"
  61. #include "hostip.h"
  62. #include "hash.h"
  63. #include "share.h"
  64. #include "url.h"
  65. #include "multiif.h"
  66. #include "inet_ntop.h"
  67. #include "curl_threads.h"
  68. #include "connect.h"
  69. #include "socketpair.h"
  70. /* The last 3 #include files should be in this order */
  71. #include "curl_printf.h"
  72. #include "curl_memory.h"
  73. #include "memdebug.h"
  74. struct resdata {
  75. struct curltime start;
  76. };
  77. /* Doubly linked list of orphaned thread handles. */
  78. struct thread_list {
  79. curl_thread_t handle;
  80. /* 'exiting' is set true right before an orphaned thread exits.
  81. it should only be set by the orphaned thread from
  82. signal_orphan_is_exiting(). */
  83. bool exiting;
  84. struct thread_list *prev, *next;
  85. };
  86. /* Orphaned threads: A global list of resolver threads that could not be
  87. * completed in time and so they were abandoned by their parent. The list is
  88. * culled periodically by soon-to-be exiting orphans to wait on and destroy
  89. * those that are in the process of or have since exited, which is fast. On
  90. * global cleanup we wait on and destroy any remaining threads, which may be
  91. * slow but at that point we cannot defer it any longer.
  92. */
  93. struct orphaned_threads {
  94. /* Mutex to lock this. To avoid deadlock the thread-specific thread_sync_data
  95. mutex cannot be used as an inner lock when orphaned_threads is locked. */
  96. curl_mutex_t mutex;
  97. /* List of orphaned threads. */
  98. struct thread_list *first, *last;
  99. /* Count of threads in the list that are in the process of or have exited.
  100. (ie .exiting member of the thread_list item is set true) */
  101. size_t exiting_count;
  102. };
  103. static struct orphaned_threads orphaned_threads;
  104. /* Flags for wait_and_destroy_orphaned_threads().
  105. They're documented above the function definition. */
  106. #define WAIT_DESTROY_ALL (1<<0)
  107. #define WAIT_DESTROY_EXITING_THREADS_ONLY (1<<1)
  108. static void wait_and_destroy_orphaned_threads(int flags);
  109. static void signal_orphan_is_exiting(struct thread_list *orphan);
  110. /*
  111. * Curl_resolver_global_init()
  112. * Called from curl_global_init() to initialize global resolver environment.
  113. */
  114. int Curl_resolver_global_init(void)
  115. {
  116. memset(&orphaned_threads, 0, sizeof(orphaned_threads));
  117. if(Curl_mutex_init(&orphaned_threads.mutex))
  118. return CURLE_FAILED_INIT;
  119. return CURLE_OK;
  120. }
  121. /*
  122. * Curl_resolver_global_cleanup()
  123. * Called from curl_global_cleanup() to destroy global resolver environment.
  124. */
  125. void Curl_resolver_global_cleanup(void)
  126. {
  127. /* Take ownership of all orphaned resolver threads and wait for them to exit.
  128. This is necessary because the user may choose to unload the shared library
  129. that is/contains libcurl. */
  130. wait_and_destroy_orphaned_threads(WAIT_DESTROY_ALL);
  131. Curl_mutex_destroy(&orphaned_threads.mutex);
  132. }
  133. /*
  134. * Curl_resolver_init()
  135. * Called from curl_easy_init() -> Curl_open() to initialize resolver
  136. * URL-state specific environment ('resolver' member of the UrlState
  137. * structure).
  138. */
  139. CURLcode Curl_resolver_init(struct Curl_easy *easy, void **resolver)
  140. {
  141. (void)easy;
  142. *resolver = calloc(1, sizeof(struct resdata));
  143. if(!*resolver)
  144. return CURLE_OUT_OF_MEMORY;
  145. return CURLE_OK;
  146. }
  147. /*
  148. * Curl_resolver_cleanup()
  149. * Called from curl_easy_cleanup() -> Curl_close() to cleanup resolver
  150. * URL-state specific environment ('resolver' member of the UrlState
  151. * structure).
  152. */
  153. void Curl_resolver_cleanup(void *resolver)
  154. {
  155. free(resolver);
  156. }
  157. /*
  158. * Curl_resolver_duphandle()
  159. * Called from curl_easy_duphandle() to duplicate resolver URL state-specific
  160. * environment ('resolver' member of the UrlState structure).
  161. */
  162. CURLcode Curl_resolver_duphandle(struct Curl_easy *easy, void **to, void *from)
  163. {
  164. (void)from;
  165. return Curl_resolver_init(easy, to);
  166. }
  167. static void destroy_async_data(struct Curl_async *);
  168. /*
  169. * Cancel all possibly still on-going resolves for this connection.
  170. */
  171. void Curl_resolver_cancel(struct Curl_easy *data)
  172. {
  173. destroy_async_data(&data->state.async);
  174. }
  175. /* This function is used to init a threaded resolve */
  176. static bool init_resolve_thread(struct Curl_easy *data,
  177. const char *hostname, int port,
  178. const struct addrinfo *hints);
  179. /* Data for synchronization between resolver thread and its parent */
  180. struct thread_sync_data {
  181. curl_mutex_t *mtx;
  182. int done;
  183. int port;
  184. char *hostname; /* hostname to resolve, Curl_async.hostname
  185. duplicate */
  186. #ifndef CURL_DISABLE_SOCKETPAIR
  187. struct Curl_easy *data;
  188. curl_socket_t sock_pair[2]; /* socket pair */
  189. #endif
  190. int sock_error;
  191. struct Curl_addrinfo *res;
  192. #ifdef HAVE_GETADDRINFO
  193. struct addrinfo hints;
  194. #endif
  195. struct thread_data *td; /* for thread-self cleanup */
  196. };
  197. struct thread_data {
  198. curl_thread_t thread_hnd;
  199. unsigned int poll_interval;
  200. timediff_t interval_end;
  201. struct thread_sync_data tsd;
  202. /* 'reserved' memory must be available in case the thread is orphaned */
  203. void *reserved;
  204. };
  205. static struct thread_sync_data *conn_thread_sync_data(struct Curl_easy *data)
  206. {
  207. return &(data->state.async.tdata->tsd);
  208. }
  209. /* Destroy resolver thread synchronization data */
  210. static
  211. void destroy_thread_sync_data(struct thread_sync_data *tsd)
  212. {
  213. if(tsd->mtx) {
  214. Curl_mutex_destroy(tsd->mtx);
  215. free(tsd->mtx);
  216. }
  217. free(tsd->hostname);
  218. if(tsd->res)
  219. Curl_freeaddrinfo(tsd->res);
  220. #ifndef CURL_DISABLE_SOCKETPAIR
  221. /*
  222. * close one end of the socket pair (may be done in resolver thread);
  223. * the other end (for reading) is always closed in the parent thread.
  224. */
  225. if(tsd->sock_pair[1] != CURL_SOCKET_BAD) {
  226. sclose(tsd->sock_pair[1]);
  227. }
  228. #endif
  229. memset(tsd, 0, sizeof(*tsd));
  230. }
  231. /* Initialize resolver thread synchronization data */
  232. static
  233. int init_thread_sync_data(struct thread_data *td,
  234. const char *hostname,
  235. int port,
  236. const struct addrinfo *hints)
  237. {
  238. struct thread_sync_data *tsd = &td->tsd;
  239. memset(tsd, 0, sizeof(*tsd));
  240. tsd->td = td;
  241. tsd->port = port;
  242. /* Treat the request as done until the thread actually starts so any early
  243. * cleanup gets done properly.
  244. */
  245. tsd->done = 1;
  246. #ifdef HAVE_GETADDRINFO
  247. DEBUGASSERT(hints);
  248. tsd->hints = *hints;
  249. #else
  250. (void) hints;
  251. #endif
  252. tsd->mtx = malloc(sizeof(curl_mutex_t));
  253. if(!tsd->mtx)
  254. goto err_exit;
  255. if(Curl_mutex_init(tsd->mtx)) {
  256. free(tsd->mtx);
  257. tsd->mtx = NULL;
  258. goto err_exit;
  259. }
  260. #ifndef CURL_DISABLE_SOCKETPAIR
  261. /* create socket pair, avoid AF_LOCAL since it doesn't build on Solaris */
  262. if(Curl_socketpair(AF_UNIX, SOCK_STREAM, 0, &tsd->sock_pair[0]) < 0) {
  263. tsd->sock_pair[0] = CURL_SOCKET_BAD;
  264. tsd->sock_pair[1] = CURL_SOCKET_BAD;
  265. goto err_exit;
  266. }
  267. #endif
  268. tsd->sock_error = CURL_ASYNC_SUCCESS;
  269. /* Copying hostname string because original can be destroyed by parent
  270. * thread during gethostbyname execution.
  271. */
  272. tsd->hostname = strdup(hostname);
  273. if(!tsd->hostname)
  274. goto err_exit;
  275. return 1;
  276. err_exit:
  277. /* Memory allocation failed */
  278. destroy_thread_sync_data(tsd);
  279. return 0;
  280. }
  281. static int getaddrinfo_complete(struct Curl_easy *data)
  282. {
  283. struct thread_sync_data *tsd = conn_thread_sync_data(data);
  284. int rc;
  285. rc = Curl_addrinfo_callback(data, tsd->sock_error, tsd->res);
  286. /* The tsd->res structure has been copied to async.dns and perhaps the DNS
  287. cache. Set our copy to NULL so destroy_thread_sync_data doesn't free it.
  288. */
  289. tsd->res = NULL;
  290. return rc;
  291. }
  292. #ifdef HAVE_GETADDRINFO
  293. /*
  294. * getaddrinfo_thread() resolves a name and then exits.
  295. *
  296. * For builds without ARES, but with ENABLE_IPV6, create a resolver thread
  297. * and wait on it.
  298. */
  299. static unsigned int CURL_STDCALL getaddrinfo_thread(void *arg)
  300. {
  301. struct thread_sync_data *tsd = (struct thread_sync_data *)arg;
  302. struct thread_data *td = tsd->td;
  303. struct thread_list *orphan = NULL;
  304. char service[12];
  305. int rc;
  306. #ifndef CURL_DISABLE_SOCKETPAIR
  307. char buf[1];
  308. #endif
  309. msnprintf(service, sizeof(service), "%d", tsd->port);
  310. rc = Curl_getaddrinfo_ex(tsd->hostname, service, &tsd->hints, &tsd->res);
  311. if(rc) {
  312. tsd->sock_error = SOCKERRNO?SOCKERRNO:rc;
  313. if(tsd->sock_error == 0)
  314. tsd->sock_error = RESOLVER_ENOMEM;
  315. }
  316. else {
  317. Curl_addrinfo_set_port(tsd->res, tsd->port);
  318. }
  319. Curl_mutex_acquire(tsd->mtx);
  320. if(tsd->done) {
  321. /* too late, gotta clean up the mess */
  322. Curl_mutex_release(tsd->mtx);
  323. destroy_thread_sync_data(tsd);
  324. orphan = (struct thread_list *)td->reserved;
  325. free(td);
  326. }
  327. else {
  328. #ifndef CURL_DISABLE_SOCKETPAIR
  329. if(tsd->sock_pair[1] != CURL_SOCKET_BAD) {
  330. /* DNS has been resolved, signal client task */
  331. buf[0] = 1;
  332. if(swrite(tsd->sock_pair[1], buf, sizeof(buf)) < 0) {
  333. /* update sock_erro to errno */
  334. tsd->sock_error = SOCKERRNO;
  335. }
  336. }
  337. #endif
  338. tsd->done = 1;
  339. Curl_mutex_release(tsd->mtx);
  340. }
  341. if(orphan)
  342. signal_orphan_is_exiting(orphan);
  343. return 0;
  344. }
  345. #else /* HAVE_GETADDRINFO */
  346. /*
  347. * gethostbyname_thread() resolves a name and then exits.
  348. */
  349. static unsigned int CURL_STDCALL gethostbyname_thread(void *arg)
  350. {
  351. struct thread_sync_data *tsd = (struct thread_sync_data *)arg;
  352. struct thread_data *td = tsd->td;
  353. struct thread_list *orphan = NULL;
  354. tsd->res = Curl_ipv4_resolve_r(tsd->hostname, tsd->port);
  355. if(!tsd->res) {
  356. tsd->sock_error = SOCKERRNO;
  357. if(tsd->sock_error == 0)
  358. tsd->sock_error = RESOLVER_ENOMEM;
  359. }
  360. Curl_mutex_acquire(tsd->mtx);
  361. if(tsd->done) {
  362. /* too late, gotta clean up the mess */
  363. Curl_mutex_release(tsd->mtx);
  364. destroy_thread_sync_data(tsd);
  365. orphan = (struct thread_list *)td->reserved;
  366. free(td);
  367. }
  368. else {
  369. tsd->done = 1;
  370. Curl_mutex_release(tsd->mtx);
  371. }
  372. if(orphan)
  373. signal_orphan_is_exiting(orphan);
  374. return 0;
  375. }
  376. #endif /* HAVE_GETADDRINFO */
  377. /*
  378. * destroy_async_data() cleans up async resolver data and thread handle.
  379. */
  380. static void destroy_async_data(struct Curl_async *async)
  381. {
  382. if(async->tdata) {
  383. struct thread_data *td = async->tdata;
  384. int done;
  385. #ifndef CURL_DISABLE_SOCKETPAIR
  386. curl_socket_t sock_rd = td->tsd.sock_pair[0];
  387. struct Curl_easy *data = td->tsd.data;
  388. #endif
  389. /* We can't wait any longer for the resolver thread so if it's not done
  390. * then it must be orphaned.
  391. *
  392. * 1) add thread to orphaned threads list
  393. * 2) set thread done (this signals to thread it has been orphaned)
  394. *
  395. * An orphaned thread does most of its own cleanup, and any remaining
  396. * cleanup is handled during global cleanup.
  397. */
  398. Curl_mutex_acquire(td->tsd.mtx);
  399. if(!td->tsd.done && td->thread_hnd != curl_thread_t_null) {
  400. struct thread_list *orphan = (struct thread_list *)td->reserved;
  401. Curl_mutex_acquire(&orphaned_threads.mutex);
  402. #ifdef DEBUGBUILD
  403. {
  404. struct thread_list empty;
  405. memset(&empty, 0, sizeof(empty));
  406. DEBUGASSERT(!memcmp(&empty, orphan, sizeof(empty)));
  407. }
  408. #endif
  409. orphan->handle = td->thread_hnd;
  410. orphan->exiting = false;
  411. if(orphaned_threads.last) {
  412. orphaned_threads.last->next = orphan;
  413. orphan->prev = orphaned_threads.last;
  414. }
  415. else {
  416. orphaned_threads.first = orphan;
  417. orphan->prev = NULL;
  418. }
  419. orphaned_threads.last = orphan;
  420. orphan->next = NULL;
  421. Curl_mutex_release(&orphaned_threads.mutex);
  422. }
  423. done = td->tsd.done;
  424. td->tsd.done = 1;
  425. Curl_mutex_release(td->tsd.mtx);
  426. if(done) {
  427. if(td->thread_hnd != curl_thread_t_null)
  428. Curl_thread_join(&td->thread_hnd);
  429. destroy_thread_sync_data(&td->tsd);
  430. free(td->reserved);
  431. free(td);
  432. }
  433. #ifndef CURL_DISABLE_SOCKETPAIR
  434. /*
  435. * ensure CURLMOPT_SOCKETFUNCTION fires CURL_POLL_REMOVE
  436. * before the FD is invalidated to avoid EBADF on EPOLL_CTL_DEL
  437. */
  438. Curl_multi_closed(data, sock_rd);
  439. sclose(sock_rd);
  440. #endif
  441. }
  442. async->tdata = NULL;
  443. free(async->hostname);
  444. async->hostname = NULL;
  445. }
  446. /*
  447. * init_resolve_thread() starts a new thread that performs the actual
  448. * resolve. This function returns before the resolve is done.
  449. *
  450. * Returns FALSE in case of failure, otherwise TRUE.
  451. */
  452. static bool init_resolve_thread(struct Curl_easy *data,
  453. const char *hostname, int port,
  454. const struct addrinfo *hints)
  455. {
  456. struct thread_data *td = calloc(1, sizeof(struct thread_data));
  457. int err = ENOMEM;
  458. struct Curl_async *asp = &data->state.async;
  459. data->state.async.tdata = td;
  460. if(!td)
  461. goto errno_exit;
  462. asp->port = port;
  463. asp->done = FALSE;
  464. asp->status = 0;
  465. asp->dns = NULL;
  466. td->thread_hnd = curl_thread_t_null;
  467. td->reserved = calloc(1, sizeof(struct thread_list));
  468. if(!td->reserved || !init_thread_sync_data(td, hostname, port, hints)) {
  469. asp->tdata = NULL;
  470. free(td->reserved);
  471. free(td);
  472. goto errno_exit;
  473. }
  474. free(asp->hostname);
  475. asp->hostname = strdup(hostname);
  476. if(!asp->hostname)
  477. goto err_exit;
  478. /* The thread will set this to 1 when complete. */
  479. td->tsd.done = 0;
  480. #ifdef HAVE_GETADDRINFO
  481. td->thread_hnd = Curl_thread_create(getaddrinfo_thread, &td->tsd);
  482. #else
  483. td->thread_hnd = Curl_thread_create(gethostbyname_thread, &td->tsd);
  484. #endif
  485. if(td->thread_hnd == curl_thread_t_null) {
  486. /* The thread never started, so mark it as done here for proper cleanup. */
  487. td->tsd.done = 1;
  488. err = errno;
  489. goto err_exit;
  490. }
  491. return TRUE;
  492. err_exit:
  493. destroy_async_data(asp);
  494. errno_exit:
  495. errno = err;
  496. return FALSE;
  497. }
  498. /*
  499. * 'entry' may be NULL and then no data is returned
  500. */
  501. static CURLcode thread_wait_resolv(struct Curl_easy *data,
  502. struct Curl_dns_entry **entry,
  503. bool report)
  504. {
  505. struct thread_data *td;
  506. CURLcode result = CURLE_OK;
  507. DEBUGASSERT(data);
  508. td = data->state.async.tdata;
  509. DEBUGASSERT(td);
  510. DEBUGASSERT(td->thread_hnd != curl_thread_t_null);
  511. /* wait for the thread to resolve the name */
  512. if(Curl_thread_join(&td->thread_hnd)) {
  513. if(entry)
  514. result = getaddrinfo_complete(data);
  515. }
  516. else
  517. DEBUGASSERT(0);
  518. data->state.async.done = TRUE;
  519. if(entry)
  520. *entry = data->state.async.dns;
  521. if(!data->state.async.dns && report)
  522. /* a name was not resolved, report error */
  523. result = Curl_resolver_error(data);
  524. destroy_async_data(&data->state.async);
  525. if(!data->state.async.dns && report)
  526. connclose(data->conn, "asynch resolve failed");
  527. return result;
  528. }
  529. /*
  530. * Until we gain a way to signal the resolver threads to stop early, we must
  531. * simply wait for them and ignore their results.
  532. */
  533. void Curl_resolver_kill(struct Curl_easy *data)
  534. {
  535. struct thread_data *td = data->state.async.tdata;
  536. /* If we're still resolving, we must wait for the threads to fully clean up,
  537. unfortunately. Otherwise, we can simply cancel to clean up any resolver
  538. data. */
  539. if(td && td->thread_hnd != curl_thread_t_null)
  540. (void)thread_wait_resolv(data, NULL, FALSE);
  541. else
  542. Curl_resolver_cancel(data);
  543. }
  544. /*
  545. * Curl_resolver_wait_resolv()
  546. *
  547. * Waits for a resolve to finish. This function should be avoided since using
  548. * this risk getting the multi interface to "hang".
  549. *
  550. * If 'entry' is non-NULL, make it point to the resolved dns entry
  551. *
  552. * Returns CURLE_COULDNT_RESOLVE_HOST if the host was not resolved,
  553. * CURLE_OPERATION_TIMEDOUT if a time-out occurred, or other errors.
  554. *
  555. * This is the version for resolves-in-a-thread.
  556. */
  557. CURLcode Curl_resolver_wait_resolv(struct Curl_easy *data,
  558. struct Curl_dns_entry **entry)
  559. {
  560. return thread_wait_resolv(data, entry, TRUE);
  561. }
  562. /*
  563. * Curl_resolver_is_resolved() is called repeatedly to check if a previous
  564. * name resolve request has completed. It should also make sure to time-out if
  565. * the operation seems to take too long.
  566. */
  567. CURLcode Curl_resolver_is_resolved(struct Curl_easy *data,
  568. struct Curl_dns_entry **entry)
  569. {
  570. struct thread_data *td = data->state.async.tdata;
  571. int done = 0;
  572. DEBUGASSERT(entry);
  573. *entry = NULL;
  574. if(!td) {
  575. DEBUGASSERT(td);
  576. return CURLE_COULDNT_RESOLVE_HOST;
  577. }
  578. Curl_mutex_acquire(td->tsd.mtx);
  579. done = td->tsd.done;
  580. Curl_mutex_release(td->tsd.mtx);
  581. if(done) {
  582. getaddrinfo_complete(data);
  583. if(!data->state.async.dns) {
  584. CURLcode result = Curl_resolver_error(data);
  585. destroy_async_data(&data->state.async);
  586. return result;
  587. }
  588. destroy_async_data(&data->state.async);
  589. *entry = data->state.async.dns;
  590. }
  591. else {
  592. /* poll for name lookup done with exponential backoff up to 250ms */
  593. /* should be fine even if this converts to 32 bit */
  594. timediff_t elapsed = Curl_timediff(Curl_now(),
  595. data->progress.t_startsingle);
  596. if(elapsed < 0)
  597. elapsed = 0;
  598. if(td->poll_interval == 0)
  599. /* Start at 1ms poll interval */
  600. td->poll_interval = 1;
  601. else if(elapsed >= td->interval_end)
  602. /* Back-off exponentially if last interval expired */
  603. td->poll_interval *= 2;
  604. if(td->poll_interval > 250)
  605. td->poll_interval = 250;
  606. td->interval_end = elapsed + td->poll_interval;
  607. Curl_expire(data, td->poll_interval, EXPIRE_ASYNC_NAME);
  608. }
  609. return CURLE_OK;
  610. }
  611. int Curl_resolver_getsock(struct Curl_easy *data, curl_socket_t *socks)
  612. {
  613. int ret_val = 0;
  614. timediff_t milli;
  615. timediff_t ms;
  616. struct resdata *reslv = (struct resdata *)data->state.async.resolver;
  617. #ifndef CURL_DISABLE_SOCKETPAIR
  618. struct thread_data *td = data->state.async.tdata;
  619. #else
  620. (void)socks;
  621. #endif
  622. #ifndef CURL_DISABLE_SOCKETPAIR
  623. if(td) {
  624. /* return read fd to client for polling the DNS resolution status */
  625. socks[0] = td->tsd.sock_pair[0];
  626. td->tsd.data = data;
  627. ret_val = GETSOCK_READSOCK(0);
  628. }
  629. else {
  630. #endif
  631. ms = Curl_timediff(Curl_now(), reslv->start);
  632. if(ms < 3)
  633. milli = 0;
  634. else if(ms <= 50)
  635. milli = ms/3;
  636. else if(ms <= 250)
  637. milli = 50;
  638. else
  639. milli = 200;
  640. Curl_expire(data, milli, EXPIRE_ASYNC_NAME);
  641. #ifndef CURL_DISABLE_SOCKETPAIR
  642. }
  643. #endif
  644. return ret_val;
  645. }
  646. #ifndef HAVE_GETADDRINFO
  647. /*
  648. * Curl_getaddrinfo() - for platforms without getaddrinfo
  649. */
  650. struct Curl_addrinfo *Curl_resolver_getaddrinfo(struct Curl_easy *data,
  651. const char *hostname,
  652. int port,
  653. int *waitp)
  654. {
  655. struct resdata *reslv = (struct resdata *)data->state.async.resolver;
  656. *waitp = 0; /* default to synchronous response */
  657. reslv->start = Curl_now();
  658. /* fire up a new resolver thread! */
  659. if(init_resolve_thread(data, hostname, port, NULL)) {
  660. *waitp = 1; /* expect asynchronous response */
  661. return NULL;
  662. }
  663. failf(data, "getaddrinfo() thread failed");
  664. return NULL;
  665. }
  666. #else /* !HAVE_GETADDRINFO */
  667. /*
  668. * Curl_resolver_getaddrinfo() - for getaddrinfo
  669. */
  670. struct Curl_addrinfo *Curl_resolver_getaddrinfo(struct Curl_easy *data,
  671. const char *hostname,
  672. int port,
  673. int *waitp)
  674. {
  675. struct addrinfo hints;
  676. int pf = PF_INET;
  677. struct resdata *reslv = (struct resdata *)data->state.async.resolver;
  678. *waitp = 0; /* default to synchronous response */
  679. #ifdef CURLRES_IPV6
  680. if(Curl_ipv6works(data))
  681. /* The stack seems to be IPv6-enabled */
  682. pf = PF_UNSPEC;
  683. #endif /* CURLRES_IPV6 */
  684. memset(&hints, 0, sizeof(hints));
  685. hints.ai_family = pf;
  686. hints.ai_socktype = (data->conn->transport == TRNSPRT_TCP)?
  687. SOCK_STREAM : SOCK_DGRAM;
  688. reslv->start = Curl_now();
  689. /* fire up a new resolver thread! */
  690. if(init_resolve_thread(data, hostname, port, &hints)) {
  691. *waitp = 1; /* expect asynchronous response */
  692. return NULL;
  693. }
  694. failf(data, "getaddrinfo() thread failed to start");
  695. return NULL;
  696. }
  697. #endif /* !HAVE_GETADDRINFO */
  698. CURLcode Curl_set_dns_servers(struct Curl_easy *data,
  699. char *servers)
  700. {
  701. (void)data;
  702. (void)servers;
  703. return CURLE_NOT_BUILT_IN;
  704. }
  705. CURLcode Curl_set_dns_interface(struct Curl_easy *data,
  706. const char *interf)
  707. {
  708. (void)data;
  709. (void)interf;
  710. return CURLE_NOT_BUILT_IN;
  711. }
  712. CURLcode Curl_set_dns_local_ip4(struct Curl_easy *data,
  713. const char *local_ip4)
  714. {
  715. (void)data;
  716. (void)local_ip4;
  717. return CURLE_NOT_BUILT_IN;
  718. }
  719. CURLcode Curl_set_dns_local_ip6(struct Curl_easy *data,
  720. const char *local_ip6)
  721. {
  722. (void)data;
  723. (void)local_ip6;
  724. return CURLE_NOT_BUILT_IN;
  725. }
  726. /* Helper function to wait and destroy some or all orphaned threads.
  727. *
  728. * WAIT_DESTROY_ALL:
  729. * Wait and destroy all orphaned threads. This operation is not safe to specify
  730. * in code that could run in any thread that may be orphaned (ie any resolver
  731. * thread). Waiting on all orphaned threads may take some time. This operation
  732. * must be specified in the call from global cleanup, and ideally nowhere else.
  733. *
  734. * WAIT_DESTROY_EXITING_THREADS_ONLY:
  735. * Wait and destroy only orphaned threads that are in the process of or have
  736. * since exited (ie those with .exiting set true). This is fast.
  737. *
  738. * When the calling thread owns orphaned_threads.mutex it must not call this
  739. * function or deadlock my occur.
  740. */
  741. static void wait_and_destroy_orphaned_threads(int flags)
  742. {
  743. struct thread_list *thread = NULL;
  744. Curl_mutex_acquire(&orphaned_threads.mutex);
  745. if((flags & WAIT_DESTROY_EXITING_THREADS_ONLY)) {
  746. struct thread_list *p, *next;
  747. struct thread_list *first = NULL, *last = NULL;
  748. if(!orphaned_threads.exiting_count) {
  749. Curl_mutex_release(&orphaned_threads.mutex);
  750. return;
  751. }
  752. for(p = orphaned_threads.first; p; p = next) {
  753. next = p->next;
  754. if(!p->exiting)
  755. continue;
  756. /* remove thread list item from orphaned_threads */
  757. if(p->prev)
  758. p->prev->next = p->next;
  759. if(p->next)
  760. p->next->prev = p->prev;
  761. if(orphaned_threads.first == p)
  762. orphaned_threads.first = p->next;
  763. if(orphaned_threads.last == p)
  764. orphaned_threads.last = p->prev;
  765. /* add thread list item to new thread list */
  766. if(last) {
  767. last->next = p;
  768. p->prev = last;
  769. }
  770. else {
  771. first = p;
  772. p->prev = NULL;
  773. }
  774. last = p;
  775. p->next = NULL;
  776. }
  777. thread = first;
  778. orphaned_threads.exiting_count = 0;
  779. }
  780. else if((flags & WAIT_DESTROY_ALL)) {
  781. thread = orphaned_threads.first;
  782. orphaned_threads.first = NULL;
  783. orphaned_threads.last = NULL;
  784. orphaned_threads.exiting_count = 0;
  785. }
  786. Curl_mutex_release(&orphaned_threads.mutex);
  787. /* Wait and free. Must be done unlocked or there could be deadlock. */
  788. while(thread) {
  789. struct thread_list *next = thread->next;
  790. Curl_thread_join(&thread->handle);
  791. free(thread);
  792. thread = next;
  793. }
  794. }
  795. /* Helper function that must be called from an orphaned thread right before it
  796. exits. */
  797. static void signal_orphan_is_exiting(struct thread_list *orphan)
  798. {
  799. DEBUGASSERT(orphan->handle && !orphan->exiting);
  800. wait_and_destroy_orphaned_threads(WAIT_DESTROY_EXITING_THREADS_ONLY);
  801. Curl_mutex_acquire(&orphaned_threads.mutex);
  802. orphan->exiting = true;
  803. orphaned_threads.exiting_count++;
  804. Curl_mutex_release(&orphaned_threads.mutex);
  805. }
  806. #endif /* CURLRES_THREADED */