resolv_context.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. /* Temporary, thread-local resolver state.
  2. Copyright (C) 2017-2019 Free Software Foundation, Inc.
  3. This file is part of the GNU C Library.
  4. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Lesser General Public
  6. License as published by the Free Software Foundation; either
  7. version 2.1 of the License, or (at your option) any later version.
  8. The GNU C Library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with the GNU C Library; if not, see
  14. <http://www.gnu.org/licenses/>. */
  15. #include <resolv_context.h>
  16. #include <resolv_conf.h>
  17. #include <resolv-internal.h>
  18. #include <assert.h>
  19. #include <errno.h>
  20. #include <stdlib.h>
  21. #include <stdio.h>
  22. /* Currently active struct resolv_context object. This pointer forms
  23. the start of a single-linked list, using the __next member of
  24. struct resolv_context. This list serves two purposes:
  25. (a) A subsequent call to __resolv_context_get will only increment
  26. the reference counter and will not allocate a new object. The
  27. _res state freshness check is skipped in this case, too.
  28. (b) The per-thread cleanup function defined by the resolver calls
  29. __resolv_context_freeres, which will deallocate all the context
  30. objects. This avoids the need for cancellation handlers and
  31. the complexity they bring, but it requires heap allocation of
  32. the context object because the per-thread cleanup functions run
  33. only after the stack has been fully unwound (and all on-stack
  34. objects have been deallocated at this point).
  35. The TLS variable current is updated even in
  36. __resolv_context_get_override, to support case (b) above. This does
  37. not override the per-thread resolver state (as obtained by the
  38. non-res_state function such as __resolv_context_get) in an
  39. observable way because the wrapped context is only used to
  40. implement the res_n* functions in the resolver, and those do not
  41. call back into user code which could indirectly use the per-thread
  42. resolver state. */
  43. static __thread struct resolv_context *current attribute_tls_model_ie;
  44. /* The resolv_conf handling will gives us a ctx->conf pointer even if
  45. these fields do not match because a mis-match does not cause a loss
  46. of state (_res objects can store the full information). This
  47. function checks to ensure that there is a full patch, to prevent
  48. overwriting a patched configuration. */
  49. static bool
  50. replicated_configuration_matches (const struct resolv_context *ctx)
  51. {
  52. return ctx->resp->options == ctx->conf->options
  53. && ctx->resp->retrans == ctx->conf->retrans
  54. && ctx->resp->retry == ctx->conf->retry
  55. && ctx->resp->ndots == ctx->conf->ndots;
  56. }
  57. /* Initialize *RESP if RES_INIT is not yet set in RESP->options, or if
  58. res_init in some other thread requested re-initializing. */
  59. static __attribute__ ((warn_unused_result)) bool
  60. maybe_init (struct resolv_context *ctx, bool preinit)
  61. {
  62. struct __res_state *resp = ctx->resp;
  63. if (resp->options & RES_INIT)
  64. {
  65. if (resp->options & RES_NORELOAD)
  66. /* Configuration reloading was explicitly disabled. */
  67. return true;
  68. /* If there is no associated resolv_conf object despite the
  69. initialization, something modified *ctx->resp. Do not
  70. override those changes. */
  71. if (ctx->conf != NULL && replicated_configuration_matches (ctx))
  72. {
  73. struct resolv_conf *current = __resolv_conf_get_current ();
  74. if (current == NULL)
  75. return false;
  76. /* Check if the configuration changed. */
  77. if (current != ctx->conf)
  78. {
  79. /* This call will detach the extended resolver state. */
  80. if (resp->nscount > 0)
  81. __res_iclose (resp, true);
  82. /* Reattach the current configuration. */
  83. if (__resolv_conf_attach (ctx->resp, current))
  84. {
  85. __resolv_conf_put (ctx->conf);
  86. /* ctx takes ownership, so we do not release current. */
  87. ctx->conf = current;
  88. }
  89. }
  90. else
  91. /* No change. Drop the reference count for current. */
  92. __resolv_conf_put (current);
  93. }
  94. return true;
  95. }
  96. assert (ctx->conf == NULL);
  97. if (preinit)
  98. {
  99. if (!resp->retrans)
  100. resp->retrans = RES_TIMEOUT;
  101. if (!resp->retry)
  102. resp->retry = RES_DFLRETRY;
  103. resp->options = RES_DEFAULT;
  104. if (!resp->id)
  105. resp->id = res_randomid ();
  106. }
  107. if (__res_vinit (resp, preinit) < 0)
  108. return false;
  109. ctx->conf = __resolv_conf_get (ctx->resp);
  110. return true;
  111. }
  112. /* Allocate a new context object and initialize it. The object is put
  113. on the current list. */
  114. static struct resolv_context *
  115. context_alloc (struct __res_state *resp)
  116. {
  117. struct resolv_context *ctx = malloc (sizeof (*ctx));
  118. if (ctx == NULL)
  119. return NULL;
  120. ctx->resp = resp;
  121. ctx->conf = __resolv_conf_get (resp);
  122. ctx->__refcount = 1;
  123. ctx->__from_res = true;
  124. ctx->__next = current;
  125. current = ctx;
  126. return ctx;
  127. }
  128. /* Deallocate the context object and all the state within. */
  129. static void
  130. context_free (struct resolv_context *ctx)
  131. {
  132. int error_code = errno;
  133. current = ctx->__next;
  134. __resolv_conf_put (ctx->conf);
  135. free (ctx);
  136. __set_errno (error_code);
  137. }
  138. /* Reuse the current context object. */
  139. static struct resolv_context *
  140. context_reuse (void)
  141. {
  142. /* A context object created by __resolv_context_get_override cannot
  143. be reused. */
  144. assert (current->__from_res);
  145. ++current->__refcount;
  146. /* Check for reference counter wraparound. This can only happen if
  147. the get/put functions are not properly paired. */
  148. assert (current->__refcount > 0);
  149. return current;
  150. }
  151. /* Backing function for the __resolv_context_get family of
  152. functions. */
  153. static struct resolv_context *
  154. context_get (bool preinit)
  155. {
  156. if (current != NULL)
  157. return context_reuse ();
  158. struct resolv_context *ctx = context_alloc (&_res);
  159. if (ctx == NULL)
  160. return NULL;
  161. if (!maybe_init (ctx, preinit))
  162. {
  163. context_free (ctx);
  164. return NULL;
  165. }
  166. return ctx;
  167. }
  168. struct resolv_context *
  169. __resolv_context_get (void)
  170. {
  171. return context_get (false);
  172. }
  173. libc_hidden_def (__resolv_context_get)
  174. struct resolv_context *
  175. __resolv_context_get_preinit (void)
  176. {
  177. return context_get (true);
  178. }
  179. libc_hidden_def (__resolv_context_get_preinit)
  180. struct resolv_context *
  181. __resolv_context_get_override (struct __res_state *resp)
  182. {
  183. /* NB: As explained asbove, context_alloc will put the context on
  184. the current list. */
  185. struct resolv_context *ctx = context_alloc (resp);
  186. if (ctx == NULL)
  187. return NULL;
  188. ctx->__from_res = false;
  189. return ctx;
  190. }
  191. libc_hidden_def (__resolv_context_get_override)
  192. void
  193. __resolv_context_put (struct resolv_context *ctx)
  194. {
  195. if (ctx == NULL)
  196. return;
  197. /* NB: Callers assume that this function preserves errno and
  198. h_errno. */
  199. assert (current == ctx);
  200. assert (ctx->__refcount > 0);
  201. if (ctx->__from_res && --ctx->__refcount > 0)
  202. /* Do not pop this context yet. */
  203. return;
  204. context_free (ctx);
  205. }
  206. libc_hidden_def (__resolv_context_put)
  207. void
  208. __resolv_context_freeres (void)
  209. {
  210. /* Deallocate the entire chain of context objects. */
  211. struct resolv_context *ctx = current;
  212. current = NULL;
  213. while (ctx != NULL)
  214. {
  215. struct resolv_context *next = ctx->__next;
  216. context_free (ctx);
  217. ctx = next;
  218. }
  219. }