2 * Copyright (c) 1995-1999 by Internet Software Consortium.
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.
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
20 #include <sys/types.h>
21 #include <netinet/in.h>
22 #include <arpa/nameser.h>
24 #include <bits/libc-lock.h>
27 /* The following bit is copied from res_data.c (where it is #ifdef'ed
28 out) since res_init() should go into libc.so but the rest of that
31 extern unsigned long long int __res_initstamp attribute_hidden
;
32 /* We have atomic increment operations on 64-bit platforms. */
34 # define atomicinclock(lock) (void) 0
35 # define atomicincunlock(lock) (void) 0
36 # define atomicinc(var) catomic_increment (&(var))
38 __libc_lock_define_initialized (static, lock
);
39 # define atomicinclock(lock) __libc_lock_lock (lock)
40 # define atomicincunlock(lock) __libc_lock_unlock (lock)
41 # define atomicinc(var) ++var
46 extern int __res_vinit(res_state
, int);
49 * These three fields used to be statically initialized. This made
50 * it hard to use this code in a shared library. It is necessary,
51 * now that we're doing dynamic initialization here, that we preserve
52 * the old semantics: if an application modifies one of these three
53 * fields of _res before res_init() is called, res_init() will not
54 * alter them. Of course, if an application is setting them to
55 * _zero_ before calling res_init(), hoping to override what used
56 * to be the static default, we can't detect it and unexpected results
57 * will follow. Zero for any of these fields would make no sense,
58 * so one can safely assume that the applications were already getting
61 * _res.options is tricky since some apps were known to diddle the bits
62 * before res_init() was first called. We can't replicate that semantic
63 * with dynamic initialization (they may have turned bits off that are
64 * set in RES_DEFAULT). Our solution is to declare such applications
65 * "broken". They could fool us by setting RES_INIT but none do (yet).
68 _res
.retrans
= RES_TIMEOUT
;
71 if (!(_res
.options
& RES_INIT
))
72 _res
.options
= RES_DEFAULT
;
73 else if (_res
.nscount
> 0)
74 __res_iclose (&_res
, true); /* Close any VC sockets. */
77 * This one used to initialize implicitly to zero, so unless the app
78 * has set it to something in particular, we can randomize it now.
81 _res
.id
= res_randomid();
84 /* Request all threads to re-initialize their resolver states,
85 resolv.conf might have changed. */
86 atomicinc (__res_initstamp
);
87 atomicincunlock (lock
);
89 return (__res_vinit(&_res
, 1));
92 /* Initialize resp if RES_INIT is not yet set or if res_init in some other
93 thread requested re-initializing. */
95 __res_maybe_init (res_state resp
, int preinit
)
97 if (resp
->options
& RES_INIT
) {
98 if (__res_initstamp
!= resp
->_u
._ext
.initstamp
) {
99 if (resp
->nscount
> 0)
100 __res_iclose (resp
, true);
101 return __res_vinit (resp
, 1);
104 } else if (preinit
) {
106 resp
->retrans
= RES_TIMEOUT
;
109 resp
->options
= RES_DEFAULT
;
111 resp
->id
= res_randomid ();
112 return __res_vinit (resp
, 1);
114 return __res_ninit (resp
);
116 libc_hidden_def (__res_maybe_init
)
118 /* This needs to be after the use of _res in res_init, above. */
121 /* The resolver state for use by single-threaded programs.
122 This differs from plain `struct __res_state _res;' in that it doesn't
123 create a common definition, but a plain symbol that resides in .bss,
124 which can have an alias. */
125 struct __res_state _res
__attribute__ ((nocommon
));
128 __thread
struct __res_state
*__resp
= &_res
;
129 extern __thread
struct __res_state
*__libc_resp
130 __attribute__ ((alias ("__resp"))) attribute_hidden
;
132 /* We declare this with compat_symbol so that it's not
133 visible at link time. Programs must use the accessor functions. */
135 # include <shlib-compat.h>
136 compat_symbol (libc
, _res
, _res
, GLIBC_2_0
);
139 #include <shlib-compat.h>
141 #if SHLIB_COMPAT(libc, GLIBC_2_0, GLIBC_2_2)
143 extern int __res_init_weak (void);
144 weak_extern (__res_init_weak
);
145 strong_alias (__res_init
, __res_init_weak
);
146 compat_symbol (libc
, __res_init_weak
, res_init
, GLIBC_2_0
);