asyn-thread.c 25 KB

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