1 | /* |
---|
2 | * Copyright (c) 1995-1999 by Internet Software Consortium. |
---|
3 | * |
---|
4 | * Permission to use, copy, modify, and distribute this software for any |
---|
5 | * purpose with or without fee is hereby granted, provided that the above |
---|
6 | * copyright notice and this permission notice appear in all copies. |
---|
7 | * |
---|
8 | * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS |
---|
9 | * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES |
---|
10 | * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE |
---|
11 | * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL |
---|
12 | * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR |
---|
13 | * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS |
---|
14 | * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS |
---|
15 | * SOFTWARE. |
---|
16 | */ |
---|
17 | |
---|
18 | #include <machine/atomic.h> |
---|
19 | #include <limits.h> |
---|
20 | #include <stdlib.h> |
---|
21 | #include <sys/types.h> |
---|
22 | #include <netinet/in.h> |
---|
23 | #include <arpa/nameser.h> |
---|
24 | #include <resolv.h> |
---|
25 | #include <bits/libc-lock.h> |
---|
26 | |
---|
27 | #include "libc-symbols.h" |
---|
28 | |
---|
29 | |
---|
30 | /* The following bit is copied from res_data.c (where it is #ifdef'ed |
---|
31 | out) since res_init() should go into libc.so but the rest of that |
---|
32 | file should not. */ |
---|
33 | |
---|
34 | extern unsigned long long int __res_initstamp attribute_hidden; |
---|
35 | /* We have atomic increment operations on 64-bit platforms. */ |
---|
36 | #if __WORDSIZE == 64 |
---|
37 | # define atomicinclock(lock) (void) 0 |
---|
38 | # define atomicincunlock(lock) (void) 0 |
---|
39 | # define atomicinc(var) atomic_increment (&(var)) |
---|
40 | #else |
---|
41 | __libc_lock_define_initialized (static, lock); |
---|
42 | # define atomicinclock(lock) __libc_lock_lock (lock) |
---|
43 | # define atomicincunlock(lock) __libc_lock_unlock (lock) |
---|
44 | # define atomicinc(var) ++var |
---|
45 | #endif |
---|
46 | |
---|
47 | int |
---|
48 | res_init(void) { |
---|
49 | extern int __res_vinit(res_state, int); |
---|
50 | |
---|
51 | /* |
---|
52 | * These three fields used to be statically initialized. This made |
---|
53 | * it hard to use this code in a shared library. It is necessary, |
---|
54 | * now that we're doing dynamic initialization here, that we preserve |
---|
55 | * the old semantics: if an application modifies one of these three |
---|
56 | * fields of _res before res_init() is called, res_init() will not |
---|
57 | * alter them. Of course, if an application is setting them to |
---|
58 | * _zero_ before calling res_init(), hoping to override what used |
---|
59 | * to be the static default, we can't detect it and unexpected results |
---|
60 | * will follow. Zero for any of these fields would make no sense, |
---|
61 | * so one can safely assume that the applications were already getting |
---|
62 | * unexpected results. |
---|
63 | * |
---|
64 | * _res.options is tricky since some apps were known to diddle the bits |
---|
65 | * before res_init() was first called. We can't replicate that semantic |
---|
66 | * with dynamic initialization (they may have turned bits off that are |
---|
67 | * set in RES_DEFAULT). Our solution is to declare such applications |
---|
68 | * "broken". They could fool us by setting RES_INIT but none do (yet). |
---|
69 | */ |
---|
70 | if (!_res.retrans) |
---|
71 | _res.retrans = RES_TIMEOUT; |
---|
72 | if (!_res.retry) |
---|
73 | _res.retry = 4; |
---|
74 | if (!(_res.options & RES_INIT)) |
---|
75 | _res.options = RES_DEFAULT; |
---|
76 | else if (_res.nscount > 0) { |
---|
77 | __res_nclose (&_res); /* Close any VC sockets. */ |
---|
78 | int ns; |
---|
79 | for (ns = 0; ns < MAXNS; ns++) { |
---|
80 | free (_res._u._ext.nsaddrs[ns]); |
---|
81 | _res._u._ext.nsaddrs[ns] = NULL; |
---|
82 | } |
---|
83 | } |
---|
84 | |
---|
85 | /* |
---|
86 | * This one used to initialize implicitly to zero, so unless the app |
---|
87 | * has set it to something in particular, we can randomize it now. |
---|
88 | */ |
---|
89 | if (!_res.id) |
---|
90 | _res.id = res_randomid(); |
---|
91 | |
---|
92 | atomicinclock (lock); |
---|
93 | /* Request all threads to re-initialize their resolver states, |
---|
94 | resolv.conf might have changed. */ |
---|
95 | atomicinc (__res_initstamp); |
---|
96 | atomicincunlock (lock); |
---|
97 | |
---|
98 | return (__res_vinit(&_res, 1)); |
---|
99 | } |
---|
100 | |
---|
101 | /* Initialize resp if RES_INIT is not yet set or if res_init in some other |
---|
102 | thread requested re-initializing. */ |
---|
103 | int |
---|
104 | __res_maybe_init (res_state resp, int preinit) |
---|
105 | { |
---|
106 | if (resp->options & RES_INIT) { |
---|
107 | if (__res_initstamp != resp->_u._ext.initstamp) { |
---|
108 | if (resp->nscount > 0) { |
---|
109 | __res_nclose (resp); |
---|
110 | int ns; |
---|
111 | for (ns = 0; ns < MAXNS; ns++) { |
---|
112 | free (resp->_u._ext.nsaddrs[ns]); |
---|
113 | resp->_u._ext.nsaddrs[ns] = NULL; |
---|
114 | } |
---|
115 | return __res_vinit (resp, 1); |
---|
116 | } |
---|
117 | } |
---|
118 | return 0; |
---|
119 | } else if (preinit) { |
---|
120 | if (!resp->retrans) |
---|
121 | resp->retrans = RES_TIMEOUT; |
---|
122 | if (!resp->retry) |
---|
123 | resp->retry = 4; |
---|
124 | resp->options = RES_DEFAULT; |
---|
125 | if (!resp->id) |
---|
126 | resp->id = res_randomid (); |
---|
127 | return __res_vinit (resp, 1); |
---|
128 | } else |
---|
129 | return __res_ninit (resp); |
---|
130 | } |
---|
131 | libc_hidden_def (__res_maybe_init) |
---|
132 | |
---|
133 | /* This needs to be after the use of _res in res_init, above. */ |
---|
134 | #undef _res |
---|
135 | |
---|
136 | /* The resolver state for use by single-threaded programs. |
---|
137 | This differs from plain `struct __res_state _res;' in that it doesn't |
---|
138 | create a common definition, but a plain symbol that resides in .bss, |
---|
139 | which can have an alias. */ |
---|
140 | struct __res_state _res __attribute__((section (".bss"))); |
---|
141 | |
---|
142 | #define USE___THREAD 1 |
---|
143 | |
---|
144 | #if USE___THREAD |
---|
145 | #undef __resp |
---|
146 | __thread struct __res_state *__resp = &_res; |
---|
147 | extern __thread struct __res_state *__libc_resp |
---|
148 | __attribute__ ((alias ("__resp"))) attribute_hidden; |
---|
149 | #endif |
---|
150 | |
---|
151 | /* We declare this with compat_symbol so that it's not |
---|
152 | visible at link time. Programs must use the accessor functions. */ |
---|
153 | #if defined HAVE_ELF && defined SHARED && defined DO_VERSIONING |
---|
154 | # include <shlib-compat.h> |
---|
155 | compat_symbol (libc, _res, _res, GLIBC_2_0); |
---|
156 | #endif |
---|
157 | |
---|
158 | #include <shlib-compat.h> |
---|
159 | |
---|
160 | #if SHLIB_COMPAT(libc, GLIBC_2_0, GLIBC_2_2) |
---|
161 | # undef res_init |
---|
162 | extern int __res_init_weak (void); |
---|
163 | weak_extern (__res_init_weak); |
---|
164 | strong_alias (__res_init, __res_init_weak); |
---|
165 | compat_symbol (libc, __res_init_weak, res_init, GLIBC_2_0); |
---|
166 | #endif |
---|