1 /*-
2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3 *
4 * Copyright (c) 2006 The FreeBSD Project. All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
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.
14 *
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
25 * SUCH DAMAGE.
26 *
27 * $FreeBSD$
28 */
29
30 #include <sys/types.h>
31 #include <sys/stat.h>
32 #include <sys/time.h>
33 #include <netinet/in.h>
34 #include <arpa/nameser.h>
35 #include <resolv.h>
36 #include <stdlib.h>
37
38 #include "namespace.h"
39 #include "reentrant.h"
40 #include "un-namespace.h"
41
42 #include "res_private.h"
43
44 #undef _res
45
46 struct __res_state _res;
47
48 static thread_key_t res_key;
49 static once_t res_init_once = ONCE_INITIALIZER;
50 static int res_thr_keycreated = 0;
51
52 static void
free_res(void * ptr)53 free_res(void *ptr)
54 {
55 res_state statp = ptr;
56
57 if (statp->_u._ext.ext != NULL)
58 res_ndestroy(statp);
59 free(statp);
60 }
61
62 static void
res_keycreate(void)63 res_keycreate(void)
64 {
65 res_thr_keycreated = thr_keycreate(&res_key, free_res) == 0;
66 }
67
68 static res_state
res_check_reload(res_state statp)69 res_check_reload(res_state statp)
70 {
71 struct timespec now;
72 struct stat sb;
73 struct __res_state_ext *ext;
74
75 if ((statp->options & RES_INIT) == 0) {
76 return (statp);
77 }
78
79 ext = statp->_u._ext.ext;
80 if (ext == NULL || ext->reload_period == 0) {
81 return (statp);
82 }
83
84 if (clock_gettime(CLOCK_MONOTONIC_FAST, &now) != 0 ||
85 (now.tv_sec - ext->conf_stat) < ext->reload_period) {
86 return (statp);
87 }
88
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;
94 }
95
96 return (statp);
97 }
98
99 res_state
__res_state(void)100 __res_state(void)
101 {
102 res_state statp;
103
104 if (thr_main() != 0)
105 return res_check_reload(&_res);
106
107 if (thr_once(&res_init_once, res_keycreate) != 0 ||
108 !res_thr_keycreated)
109 return (&_res);
110
111 statp = thr_getspecific(res_key);
112 if (statp != NULL)
113 return res_check_reload(statp);
114 statp = calloc(1, sizeof(*statp));
115 if (statp == NULL)
116 return (&_res);
117 #ifdef __BIND_RES_TEXT
118 statp->options = RES_TIMEOUT; /* Motorola, et al. */
119 #endif
120 if (thr_setspecific(res_key, statp) == 0)
121 return (statp);
122 free(statp);
123 return (&_res);
124 }
125