xref: /freebsd/lib/libc/resolv/res_state.c (revision a4dc509f723944821bcfcc52005ff87c9a5dee5b)
1 /*-
2  * Copyright (c) 2006 The FreeBSD Project. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
14  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
17  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
23  * SUCH DAMAGE.
24  *
25  * $FreeBSD$
26  */
27 
28 #include <sys/types.h>
29 #include <sys/stat.h>
30 #include <sys/time.h>
31 #include <netinet/in.h>
32 #include <arpa/nameser.h>
33 #include <resolv.h>
34 #include <stdlib.h>
35 
36 #include "namespace.h"
37 #include "reentrant.h"
38 #include "un-namespace.h"
39 
40 #undef _res
41 
42 struct __res_state _res;
43 
44 static thread_key_t res_key;
45 static once_t res_init_once = ONCE_INITIALIZER;
46 static int res_thr_keycreated = 0;
47 
48 static void
49 free_res(void *ptr)
50 {
51 	res_state statp = ptr;
52 
53 	if (statp->_u._ext.ext != NULL)
54 		res_ndestroy(statp);
55 	free(statp);
56 }
57 
58 static void
59 res_keycreate(void)
60 {
61 	res_thr_keycreated = thr_keycreate(&res_key, free_res) == 0;
62 }
63 
64 static res_state
65 res_check_reload(res_state statp)
66 {
67 	struct timespec now;
68 	struct stat sb;
69 
70 	if ((statp->options & RES_INIT) == 0 || statp->reload_period == 0) {
71 		return (statp);
72 	}
73 
74 	if (clock_gettime(CLOCK_MONOTONIC_FAST, &now) != 0 ||
75 	    (now.tv_sec - statp->conf_stat) < statp->reload_period) {
76 		return (statp);
77 	}
78 
79 	statp->conf_stat = now.tv_sec;
80 	if (stat(_PATH_RESCONF, &sb) == 0 &&
81 	    (sb.st_mtim.tv_sec  != statp->conf_mtim.tv_sec ||
82 	     sb.st_mtim.tv_nsec != statp->conf_mtim.tv_nsec)) {
83 		statp->options &= ~RES_INIT;
84 	}
85 
86 	return (statp);
87 }
88 
89 res_state
90 __res_state(void)
91 {
92 	res_state statp;
93 
94 	if (thr_main() != 0)
95 		return res_check_reload(&_res);
96 
97 	if (thr_once(&res_init_once, res_keycreate) != 0 ||
98 	    !res_thr_keycreated)
99 		return (&_res);
100 
101 	statp = thr_getspecific(res_key);
102 	if (statp != NULL)
103 		return res_check_reload(statp);
104 	statp = calloc(1, sizeof(*statp));
105 	if (statp == NULL)
106 		return (&_res);
107 #ifdef __BIND_RES_TEXT
108 	statp->options = RES_TIMEOUT;			/* Motorola, et al. */
109 #endif
110 	if (thr_setspecific(res_key, statp) == 0)
111 		return (statp);
112 	free(statp);
113 	return (&_res);
114 }
115