resolv_conf.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700
  1. /* Extended resolver state separate from struct __res_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_conf.h>
  16. #include <alloc_buffer.h>
  17. #include <assert.h>
  18. #include <libc-lock.h>
  19. #include <resolv-internal.h>
  20. #include <sys/stat.h>
  21. #include <libc-symbols.h>
  22. /* _res._u._ext.__glibc_extension_index is used as an index into a
  23. struct resolv_conf_array object. The intent of this construction
  24. is to make reasonably sure that even if struct __res_state objects
  25. are copied around and patched by applications, we can still detect
  26. accesses to stale extended resolver state. The array elements are
  27. either struct resolv_conf * pointers (if the LSB is cleared) or
  28. free list entries (if the LSB is set). The free list is used to
  29. speed up finding available entries in the array. */
  30. #define DYNARRAY_STRUCT resolv_conf_array
  31. #define DYNARRAY_ELEMENT uintptr_t
  32. #define DYNARRAY_PREFIX resolv_conf_array_
  33. #define DYNARRAY_INITIAL_SIZE 0
  34. #include <malloc/dynarray-skeleton.c>
  35. /* A magic constant for XORing the extension index
  36. (_res._u._ext.__glibc_extension_index). This makes it less likely
  37. that a valid index is created by accident. In particular, a zero
  38. value leads to an invalid index. */
  39. #define INDEX_MAGIC 0x26a8fa5e48af8061ULL
  40. /* Global resolv.conf-related state. */
  41. struct resolv_conf_global
  42. {
  43. /* struct __res_state objects contain the extension index
  44. (_res._u._ext.__glibc_extension_index ^ INDEX_MAGIC), which
  45. refers to an element of this array. When a struct resolv_conf
  46. object (extended resolver state) is associated with a struct
  47. __res_state object (legacy resolver state), its reference count
  48. is increased and added to this array. Conversely, if the
  49. extended state is detached from the basic state (during
  50. reinitialization or deallocation), the index is decremented, and
  51. the array element is overwritten with NULL. */
  52. struct resolv_conf_array array;
  53. /* Start of the free list in the array. Zero if the free list is
  54. empty. Otherwise, free_list_start >> 1 is the first element of
  55. the free list (and the free list entries all have their LSB set
  56. and are shifted one to the left). */
  57. uintptr_t free_list_start;
  58. /* Cached current configuration object for /etc/resolv.conf. */
  59. struct resolv_conf *conf_current;
  60. /* These properties of /etc/resolv.conf are used to check if the
  61. configuration needs reloading. */
  62. struct timespec conf_mtime;
  63. struct timespec conf_ctime;
  64. off64_t conf_size;
  65. ino64_t conf_ino;
  66. };
  67. /* Lazily allocated storage for struct resolv_conf_global. */
  68. static struct resolv_conf_global *global;
  69. /* The lock synchronizes access to global and *global. It also
  70. protects the __refcount member of struct resolv_conf. */
  71. __libc_lock_define_initialized (static, lock);
  72. /* Ensure that GLOBAL is allocated and lock it. Return NULL if
  73. memory allocation failes. */
  74. static struct resolv_conf_global *
  75. get_locked_global (void)
  76. {
  77. __libc_lock_lock (lock);
  78. /* Use relaxed MO through because of load outside the lock in
  79. __resolv_conf_detach. */
  80. struct resolv_conf_global *global_copy = atomic_load_relaxed (&global);
  81. if (global_copy == NULL)
  82. {
  83. global_copy = calloc (1, sizeof (*global));
  84. if (global_copy == NULL)
  85. return NULL;
  86. atomic_store_relaxed (&global, global_copy);
  87. resolv_conf_array_init (&global_copy->array);
  88. }
  89. return global_copy;
  90. }
  91. /* Relinquish the lock acquired by get_locked_global. */
  92. static void
  93. put_locked_global (struct resolv_conf_global *global_copy)
  94. {
  95. __libc_lock_unlock (lock);
  96. }
  97. /* Decrement the reference counter. The caller must acquire the lock
  98. around the function call. */
  99. static void
  100. conf_decrement (struct resolv_conf *conf)
  101. {
  102. assert (conf->__refcount > 0);
  103. if (--conf->__refcount == 0)
  104. free (conf);
  105. }
  106. struct resolv_conf *
  107. __resolv_conf_get_current (void)
  108. {
  109. struct stat64 st;
  110. if (stat64 (_PATH_RESCONF, &st) != 0)
  111. {
  112. switch (errno)
  113. {
  114. case EACCES:
  115. case EISDIR:
  116. case ELOOP:
  117. case ENOENT:
  118. case ENOTDIR:
  119. case EPERM:
  120. /* Ignore errors due to file system contents. */
  121. memset (&st, 0, sizeof (st));
  122. break;
  123. default:
  124. /* Other errors are fatal. */
  125. return NULL;
  126. }
  127. }
  128. struct resolv_conf_global *global_copy = get_locked_global ();
  129. if (global_copy == NULL)
  130. return NULL;
  131. struct resolv_conf *conf;
  132. if (global_copy->conf_current != NULL
  133. && (global_copy->conf_mtime.tv_sec == st.st_mtim.tv_sec
  134. && global_copy->conf_mtime.tv_nsec == st.st_mtim.tv_nsec
  135. && global_copy->conf_ctime.tv_sec == st.st_ctim.tv_sec
  136. && global_copy->conf_ctime.tv_nsec == st.st_ctim.tv_nsec
  137. && global_copy->conf_ino == st.st_ino
  138. && global_copy->conf_size == st.st_size))
  139. /* We can reuse the cached configuration object. */
  140. conf = global_copy->conf_current;
  141. else
  142. {
  143. /* Parse configuration while holding the lock. This avoids
  144. duplicate work. */
  145. conf = __resolv_conf_load (NULL);
  146. if (conf != NULL)
  147. {
  148. if (global_copy->conf_current != NULL)
  149. conf_decrement (global_copy->conf_current);
  150. global_copy->conf_current = conf; /* Takes ownership. */
  151. /* Update file modification stamps. The configuration we
  152. read could be a newer version of the file, but this does
  153. not matter because this will lead to an extraneous reload
  154. later. */
  155. global_copy->conf_mtime = st.st_mtim;
  156. global_copy->conf_ctime = st.st_ctim;
  157. global_copy->conf_ino = st.st_ino;
  158. global_copy->conf_size = st.st_size;
  159. }
  160. }
  161. if (conf != NULL)
  162. {
  163. /* Return an additional reference. */
  164. assert (conf->__refcount > 0);
  165. ++conf->__refcount;
  166. assert (conf->__refcount > 0);
  167. }
  168. put_locked_global (global_copy);
  169. return conf;
  170. }
  171. /* Internal implementation of __resolv_conf_get, without validation
  172. against *RESP. */
  173. static struct resolv_conf *
  174. resolv_conf_get_1 (const struct __res_state *resp)
  175. {
  176. /* Not initialized, and therefore no assoicated context. */
  177. if (!(resp->options & RES_INIT))
  178. return NULL;
  179. struct resolv_conf_global *global_copy = get_locked_global ();
  180. if (global_copy == NULL)
  181. /* A memory allocation failure here means that no associated
  182. contexts exists, so returning NULL is correct. */
  183. return NULL;
  184. size_t index = resp->_u._ext.__glibc_extension_index ^ INDEX_MAGIC;
  185. struct resolv_conf *conf = NULL;
  186. if (index < resolv_conf_array_size (&global_copy->array))
  187. {
  188. uintptr_t *slot = resolv_conf_array_at (&global_copy->array, index);
  189. if (!(*slot & 1))
  190. {
  191. conf = (struct resolv_conf *) *slot;
  192. assert (conf->__refcount > 0);
  193. ++conf->__refcount;
  194. }
  195. }
  196. put_locked_global (global_copy);
  197. return conf;
  198. }
  199. /* Return true if both IPv4 addresses are equal. */
  200. static bool
  201. same_address_v4 (const struct sockaddr_in *left,
  202. const struct sockaddr_in *right)
  203. {
  204. return left->sin_addr.s_addr == right->sin_addr.s_addr
  205. && left->sin_port == right->sin_port;
  206. }
  207. /* Return true if both IPv6 addresses are equal. This ignores the
  208. flow label. */
  209. static bool
  210. same_address_v6 (const struct sockaddr_in6 *left,
  211. const struct sockaddr_in6 *right)
  212. {
  213. return memcmp (&left->sin6_addr, &right->sin6_addr,
  214. sizeof (left->sin6_addr)) == 0
  215. && left->sin6_port == right->sin6_port
  216. && left->sin6_scope_id == right->sin6_scope_id;
  217. }
  218. static bool
  219. same_address (const struct sockaddr *left, const struct sockaddr *right)
  220. {
  221. if (left->sa_family != right->sa_family)
  222. return false;
  223. switch (left->sa_family)
  224. {
  225. case AF_INET:
  226. return same_address_v4 ((const struct sockaddr_in *) left,
  227. (const struct sockaddr_in *) right);
  228. case AF_INET6:
  229. return same_address_v6 ((const struct sockaddr_in6 *) left,
  230. (const struct sockaddr_in6 *) right);
  231. }
  232. return false;
  233. }
  234. /* Check that *RESP and CONF match. Used by __resolv_conf_get. */
  235. static bool
  236. resolv_conf_matches (const struct __res_state *resp,
  237. const struct resolv_conf *conf)
  238. {
  239. /* NB: Do not compare the options, retrans, retry, ndots. These can
  240. be changed by applicaiton. */
  241. /* Check that the name servers in *RESP have not been modified by
  242. the application. */
  243. {
  244. size_t nserv = conf->nameserver_list_size;
  245. if (nserv > MAXNS)
  246. nserv = MAXNS;
  247. /* _ext.nscount is 0 until initialized by res_send.c. */
  248. if (resp->nscount != nserv
  249. || (resp->_u._ext.nscount != 0 && resp->_u._ext.nscount != nserv))
  250. return false;
  251. for (size_t i = 0; i < nserv; ++i)
  252. {
  253. if (resp->nsaddr_list[i].sin_family == 0)
  254. {
  255. if (resp->_u._ext.nsaddrs[i]->sin6_family != AF_INET6)
  256. return false;
  257. if (!same_address ((struct sockaddr *) resp->_u._ext.nsaddrs[i],
  258. conf->nameserver_list[i]))
  259. return false;
  260. }
  261. else if (resp->nsaddr_list[i].sin_family != AF_INET)
  262. return false;
  263. else if (!same_address ((struct sockaddr *) &resp->nsaddr_list[i],
  264. conf->nameserver_list[i]))
  265. return false;
  266. }
  267. }
  268. /* Check that the search list in *RESP has not been modified by the
  269. application. */
  270. {
  271. if (resp->dnsrch[0] == NULL)
  272. {
  273. /* Empty search list. No default domain name. */
  274. return conf->search_list_size == 0 && resp->defdname[0] == '\0';
  275. }
  276. if (resp->dnsrch[0] != resp->defdname)
  277. /* If the search list is not empty, it must start with the
  278. default domain name. */
  279. return false;
  280. size_t nsearch;
  281. for (nsearch = 0; nsearch < MAXDNSRCH; ++nsearch)
  282. if (resp->dnsrch[nsearch] == NULL)
  283. break;
  284. if (nsearch > MAXDNSRCH)
  285. /* Search list is not null-terminated. */
  286. return false;
  287. size_t search_list_size = 0;
  288. for (size_t i = 0; i < conf->search_list_size; ++i)
  289. {
  290. if (resp->dnsrch[i] != NULL)
  291. {
  292. search_list_size += strlen (resp->dnsrch[i]) + 1;
  293. if (strcmp (resp->dnsrch[i], conf->search_list[i]) != 0)
  294. return false;
  295. }
  296. else
  297. {
  298. /* resp->dnsrch is truncated if the number of elements
  299. exceeds MAXDNSRCH, or if the combined storage space for
  300. the search list exceeds what can be stored in
  301. resp->defdname. */
  302. if (i == MAXDNSRCH || search_list_size > sizeof (resp->dnsrch))
  303. break;
  304. /* Otherwise, a mismatch indicates a match failure. */
  305. return false;
  306. }
  307. }
  308. }
  309. /* Check that the sort list has not been modified. */
  310. {
  311. size_t nsort = conf->sort_list_size;
  312. if (nsort > MAXRESOLVSORT)
  313. nsort = MAXRESOLVSORT;
  314. if (resp->nsort != nsort)
  315. return false;
  316. for (size_t i = 0; i < nsort; ++i)
  317. if (resp->sort_list[i].addr.s_addr != conf->sort_list[i].addr.s_addr
  318. || resp->sort_list[i].mask != conf->sort_list[i].mask)
  319. return false;
  320. }
  321. return true;
  322. }
  323. struct resolv_conf *
  324. __resolv_conf_get (struct __res_state *resp)
  325. {
  326. struct resolv_conf *conf = resolv_conf_get_1 (resp);
  327. if (conf == NULL)
  328. return NULL;
  329. if (resolv_conf_matches (resp, conf))
  330. return conf;
  331. __resolv_conf_put (conf);
  332. return NULL;
  333. }
  334. void
  335. __resolv_conf_put (struct resolv_conf *conf)
  336. {
  337. if (conf == NULL)
  338. return;
  339. __libc_lock_lock (lock);
  340. conf_decrement (conf);
  341. __libc_lock_unlock (lock);
  342. }
  343. struct resolv_conf *
  344. __resolv_conf_allocate (const struct resolv_conf *init)
  345. {
  346. /* Allocate in decreasing order of alignment. */
  347. _Static_assert (__alignof__ (const char *const *)
  348. <= __alignof__ (struct resolv_conf), "alignment");
  349. _Static_assert (__alignof__ (struct sockaddr_in6)
  350. <= __alignof__ (const char *const *), "alignment");
  351. _Static_assert (__alignof__ (struct sockaddr_in)
  352. == __alignof__ (struct sockaddr_in6), "alignment");
  353. _Static_assert (__alignof__ (struct resolv_sortlist_entry)
  354. <= __alignof__ (struct sockaddr_in), "alignment");
  355. /* Space needed by the nameserver addresses. */
  356. size_t address_space = 0;
  357. for (size_t i = 0; i < init->nameserver_list_size; ++i)
  358. if (init->nameserver_list[i]->sa_family == AF_INET)
  359. address_space += sizeof (struct sockaddr_in);
  360. else
  361. {
  362. assert (init->nameserver_list[i]->sa_family == AF_INET6);
  363. address_space += sizeof (struct sockaddr_in6);
  364. }
  365. /* Space needed by the search list strings. */
  366. size_t string_space = 0;
  367. for (size_t i = 0; i < init->search_list_size; ++i)
  368. string_space += strlen (init->search_list[i]) + 1;
  369. /* Allocate the buffer. */
  370. void *ptr;
  371. struct alloc_buffer buffer = alloc_buffer_allocate
  372. (sizeof (struct resolv_conf)
  373. + init->nameserver_list_size * sizeof (init->nameserver_list[0])
  374. + address_space
  375. + init->search_list_size * sizeof (init->search_list[0])
  376. + init->sort_list_size * sizeof (init->sort_list[0])
  377. + string_space,
  378. &ptr);
  379. struct resolv_conf *conf
  380. = alloc_buffer_alloc (&buffer, struct resolv_conf);
  381. if (conf == NULL)
  382. /* Memory allocation failure. */
  383. return NULL;
  384. assert (conf == ptr);
  385. /* Initialize the contents. */
  386. conf->__refcount = 1;
  387. conf->retrans = init->retrans;
  388. conf->retry = init->retry;
  389. conf->options = init->options;
  390. conf->ndots = init->ndots;
  391. /* Allocate the arrays with pointers. These must come first because
  392. they have the highets alignment. */
  393. conf->nameserver_list_size = init->nameserver_list_size;
  394. const struct sockaddr **nameserver_array = alloc_buffer_alloc_array
  395. (&buffer, const struct sockaddr *, init->nameserver_list_size);
  396. conf->nameserver_list = nameserver_array;
  397. conf->search_list_size = init->search_list_size;
  398. const char **search_array = alloc_buffer_alloc_array
  399. (&buffer, const char *, init->search_list_size);
  400. conf->search_list = search_array;
  401. /* Fill the name server list array. */
  402. for (size_t i = 0; i < init->nameserver_list_size; ++i)
  403. if (init->nameserver_list[i]->sa_family == AF_INET)
  404. {
  405. struct sockaddr_in *sa = alloc_buffer_alloc
  406. (&buffer, struct sockaddr_in);
  407. *sa = *(struct sockaddr_in *) init->nameserver_list[i];
  408. nameserver_array[i] = (struct sockaddr *) sa;
  409. }
  410. else
  411. {
  412. struct sockaddr_in6 *sa = alloc_buffer_alloc
  413. (&buffer, struct sockaddr_in6);
  414. *sa = *(struct sockaddr_in6 *) init->nameserver_list[i];
  415. nameserver_array[i] = (struct sockaddr *) sa;
  416. }
  417. /* Allocate and fill the sort list array. */
  418. {
  419. conf->sort_list_size = init->sort_list_size;
  420. struct resolv_sortlist_entry *array = alloc_buffer_alloc_array
  421. (&buffer, struct resolv_sortlist_entry, init->sort_list_size);
  422. conf->sort_list = array;
  423. for (size_t i = 0; i < init->sort_list_size; ++i)
  424. array[i] = init->sort_list[i];
  425. }
  426. /* Fill the search list array. This must come last because the
  427. strings are the least aligned part of the allocation. */
  428. {
  429. for (size_t i = 0; i < init->search_list_size; ++i)
  430. search_array[i] = alloc_buffer_copy_string
  431. (&buffer, init->search_list[i]);
  432. }
  433. assert (!alloc_buffer_has_failed (&buffer));
  434. return conf;
  435. }
  436. /* Update *RESP from the extended state. */
  437. static __attribute__ ((nonnull (1, 2), warn_unused_result)) bool
  438. update_from_conf (struct __res_state *resp, const struct resolv_conf *conf)
  439. {
  440. resp->defdname[0] = '\0';
  441. resp->pfcode = 0;
  442. resp->_vcsock = -1;
  443. resp->_flags = 0;
  444. resp->ipv6_unavail = false;
  445. resp->__glibc_unused_qhook = NULL;
  446. resp->__glibc_unused_rhook = NULL;
  447. resp->retrans = conf->retrans;
  448. resp->retry = conf->retry;
  449. resp->options = conf->options;
  450. resp->ndots = conf->ndots;
  451. /* Copy the name server addresses. */
  452. {
  453. resp->nscount = 0;
  454. resp->_u._ext.nscount = 0;
  455. size_t nserv = conf->nameserver_list_size;
  456. if (nserv > MAXNS)
  457. nserv = MAXNS;
  458. for (size_t i = 0; i < nserv; i++)
  459. {
  460. if (conf->nameserver_list[i]->sa_family == AF_INET)
  461. {
  462. resp->nsaddr_list[i]
  463. = *(struct sockaddr_in *)conf->nameserver_list[i];
  464. resp->_u._ext.nsaddrs[i] = NULL;
  465. }
  466. else
  467. {
  468. assert (conf->nameserver_list[i]->sa_family == AF_INET6);
  469. resp->nsaddr_list[i].sin_family = 0;
  470. /* Make a defensive copy of the name server address, in
  471. case the application overwrites it. */
  472. struct sockaddr_in6 *sa = malloc (sizeof (*sa));
  473. if (sa == NULL)
  474. {
  475. for (size_t j = 0; j < i; ++j)
  476. free (resp->_u._ext.nsaddrs[j]);
  477. return false;
  478. }
  479. *sa = *(struct sockaddr_in6 *)conf->nameserver_list[i];
  480. resp->_u._ext.nsaddrs[i] = sa;
  481. }
  482. resp->_u._ext.nssocks[i] = -1;
  483. }
  484. resp->nscount = nserv;
  485. /* Leave resp->_u._ext.nscount at 0. res_send.c handles this. */
  486. }
  487. /* Fill in the prefix of the search list. It is truncated either at
  488. MAXDNSRCH, or if reps->defdname has insufficient space. */
  489. {
  490. struct alloc_buffer buffer
  491. = alloc_buffer_create (resp->defdname, sizeof (resp->defdname));
  492. size_t size = conf->search_list_size;
  493. size_t i;
  494. for (i = 0; i < size && i < MAXDNSRCH; ++i)
  495. {
  496. resp->dnsrch[i] = alloc_buffer_copy_string
  497. (&buffer, conf->search_list[i]);
  498. if (resp->dnsrch[i] == NULL)
  499. /* No more space in resp->defdname. Truncate. */
  500. break;
  501. }
  502. resp->dnsrch[i] = NULL;
  503. }
  504. /* Copy the sort list. */
  505. {
  506. size_t nsort = conf->sort_list_size;
  507. if (nsort > MAXRESOLVSORT)
  508. nsort = MAXRESOLVSORT;
  509. for (size_t i = 0; i < nsort; ++i)
  510. {
  511. resp->sort_list[i].addr = conf->sort_list[i].addr;
  512. resp->sort_list[i].mask = conf->sort_list[i].mask;
  513. }
  514. resp->nsort = nsort;
  515. }
  516. /* The overlapping parts of both configurations should agree after
  517. initialization. */
  518. assert (resolv_conf_matches (resp, conf));
  519. return true;
  520. }
  521. /* Decrement the configuration object at INDEX and free it if the
  522. reference counter reaches 0. *GLOBAL_COPY must be locked and
  523. remains so. */
  524. static void
  525. decrement_at_index (struct resolv_conf_global *global_copy, size_t index)
  526. {
  527. if (index < resolv_conf_array_size (&global_copy->array))
  528. {
  529. /* Index found. */
  530. uintptr_t *slot = resolv_conf_array_at (&global_copy->array, index);
  531. /* Check that the slot is not already part of the free list. */
  532. if (!(*slot & 1))
  533. {
  534. struct resolv_conf *conf = (struct resolv_conf *) *slot;
  535. conf_decrement (conf);
  536. /* Put the slot onto the free list. */
  537. *slot = global_copy->free_list_start;
  538. global_copy->free_list_start = (index << 1) | 1;
  539. }
  540. }
  541. }
  542. bool
  543. __resolv_conf_attach (struct __res_state *resp, struct resolv_conf *conf)
  544. {
  545. assert (conf->__refcount > 0);
  546. struct resolv_conf_global *global_copy = get_locked_global ();
  547. if (global_copy == NULL)
  548. return false;
  549. /* Try to find an unused index in the array. */
  550. size_t index;
  551. {
  552. if (global_copy->free_list_start & 1)
  553. {
  554. /* Unlink from the free list. */
  555. index = global_copy->free_list_start >> 1;
  556. uintptr_t *slot = resolv_conf_array_at (&global_copy->array, index);
  557. global_copy->free_list_start = *slot;
  558. assert (global_copy->free_list_start == 0
  559. || global_copy->free_list_start & 1);
  560. /* Install the configuration pointer. */
  561. *slot = (uintptr_t) conf;
  562. }
  563. else
  564. {
  565. size_t size = resolv_conf_array_size (&global_copy->array);
  566. /* No usable index found. Increase the array size. */
  567. resolv_conf_array_add (&global_copy->array, (uintptr_t) conf);
  568. if (resolv_conf_array_has_failed (&global_copy->array))
  569. {
  570. put_locked_global (global_copy);
  571. __set_errno (ENOMEM);
  572. return false;
  573. }
  574. /* The new array element was added at the end. */
  575. index = size;
  576. }
  577. }
  578. /* We have added a new reference to the object. */
  579. ++conf->__refcount;
  580. assert (conf->__refcount > 0);
  581. put_locked_global (global_copy);
  582. if (!update_from_conf (resp, conf))
  583. {
  584. /* Drop the reference we acquired. Reacquire the lock. The
  585. object has already been allocated, so it cannot be NULL this
  586. time. */
  587. global_copy = get_locked_global ();
  588. decrement_at_index (global_copy, index);
  589. put_locked_global (global_copy);
  590. return false;
  591. }
  592. resp->_u._ext.__glibc_extension_index = index ^ INDEX_MAGIC;
  593. return true;
  594. }
  595. void
  596. __resolv_conf_detach (struct __res_state *resp)
  597. {
  598. if (atomic_load_relaxed (&global) == NULL)
  599. /* Detach operation after a shutdown, or without any prior
  600. attachment. We cannot free the data (and there might not be
  601. anything to free anyway). */
  602. return;
  603. struct resolv_conf_global *global_copy = get_locked_global ();
  604. size_t index = resp->_u._ext.__glibc_extension_index ^ INDEX_MAGIC;
  605. decrement_at_index (global_copy, index);
  606. /* Clear the index field, so that accidental reuse is less
  607. likely. */
  608. resp->_u._ext.__glibc_extension_index = 0;
  609. put_locked_global (global_copy);
  610. }
  611. /* Deallocate the global data. */
  612. libc_freeres_fn (freeres)
  613. {
  614. /* No locking because this function is supposed to be called when
  615. the process has turned single-threaded. */
  616. if (global == NULL)
  617. return;
  618. if (global->conf_current != NULL)
  619. {
  620. conf_decrement (global->conf_current);
  621. global->conf_current = NULL;
  622. }
  623. /* Note that this frees only the array itself. The pointed-to
  624. configuration objects should have been deallocated by res_nclose
  625. and per-thread cleanup functions. */
  626. resolv_conf_array_free (&global->array);
  627. free (global);
  628. /* Stop potential future __resolv_conf_detach calls from accessing
  629. deallocated memory. */
  630. global = NULL;
  631. }