2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2006 The FreeBSD Project. All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 #include <sys/types.h>
33 #include <netinet/in.h>
34 #include <arpa/nameser.h>
38 #include "namespace.h"
39 #include "reentrant.h"
40 #include "un-namespace.h"
42 #include "res_private.h"
46 struct __res_state _res;
48 static thread_key_t res_key;
49 static once_t res_init_once = ONCE_INITIALIZER;
50 static int res_thr_keycreated = 0;
55 res_state statp = ptr;
57 if (statp->_u._ext.ext != NULL)
65 res_thr_keycreated = thr_keycreate(&res_key, free_res) == 0;
69 res_check_reload(res_state statp)
73 struct __res_state_ext *ext;
75 if ((statp->options & RES_INIT) == 0) {
79 ext = statp->_u._ext.ext;
80 if (ext == NULL || ext->reload_period == 0) {
84 if (clock_gettime(CLOCK_MONOTONIC_FAST, &now) != 0 ||
85 (now.tv_sec - ext->conf_stat) < ext->reload_period) {
89 ext->conf_stat = now.tv_sec;
90 if (stat(_PATH_RESCONF, &sb) == 0 &&
91 (sb.st_mtim.tv_sec != ext->conf_mtim.tv_sec ||
92 sb.st_mtim.tv_nsec != ext->conf_mtim.tv_nsec)) {
93 statp->options &= ~RES_INIT;
105 return res_check_reload(&_res);
107 if (thr_once(&res_init_once, res_keycreate) != 0 ||
111 statp = thr_getspecific(res_key);
113 return res_check_reload(statp);
114 statp = calloc(1, sizeof(*statp));
117 #ifdef __BIND_RES_TEXT
118 statp->options = RES_TIMEOUT; /* Motorola, et al. */
120 if (thr_setspecific(res_key, statp) == 0)