asyn-thread.c 25 KB

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