]> git.ipfire.org Git - thirdparty/glibc.git/blob - resolv/res_libc.c
resolv: Use RES_DFLRETRY consistently [BZ #21474]
[thirdparty/glibc.git] / resolv / res_libc.c
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 /* This file contains the definitions related to res_init which are
19 linked into libc instead of libresolv. */
20
21 #include <atomic.h>
22 #include <limits.h>
23 #include <sys/types.h>
24 #include <netinet/in.h>
25 #include <arpa/nameser.h>
26 #include <resolv.h>
27 #include <libc-lock.h>
28
29 extern unsigned long long int __res_initstamp attribute_hidden;
30 /* We have atomic increment operations on 64-bit platforms. */
31 #if __WORDSIZE == 64
32 # define atomicinclock(lock) (void) 0
33 # define atomicincunlock(lock) (void) 0
34 # define atomicinc(var) catomic_increment (&(var))
35 #else
36 __libc_lock_define_initialized (static, lock);
37 # define atomicinclock(lock) __libc_lock_lock (lock)
38 # define atomicincunlock(lock) __libc_lock_unlock (lock)
39 # define atomicinc(var) ++var
40 #endif
41
42 int
43 res_init(void) {
44 extern int __res_vinit(res_state, int);
45
46 /*
47 * These three fields used to be statically initialized. This made
48 * it hard to use this code in a shared library. It is necessary,
49 * now that we're doing dynamic initialization here, that we preserve
50 * the old semantics: if an application modifies one of these three
51 * fields of _res before res_init() is called, res_init() will not
52 * alter them. Of course, if an application is setting them to
53 * _zero_ before calling res_init(), hoping to override what used
54 * to be the static default, we can't detect it and unexpected results
55 * will follow. Zero for any of these fields would make no sense,
56 * so one can safely assume that the applications were already getting
57 * unexpected results.
58 *
59 * _res.options is tricky since some apps were known to diddle the bits
60 * before res_init() was first called. We can't replicate that semantic
61 * with dynamic initialization (they may have turned bits off that are
62 * set in RES_DEFAULT). Our solution is to declare such applications
63 * "broken". They could fool us by setting RES_INIT but none do (yet).
64 */
65 if (!_res.retrans)
66 _res.retrans = RES_TIMEOUT;
67 if (!_res.retry)
68 _res.retry = RES_DFLRETRY;
69 if (!(_res.options & RES_INIT))
70 _res.options = RES_DEFAULT;
71 else if (_res.nscount > 0)
72 __res_iclose (&_res, true); /* Close any VC sockets. */
73
74 /*
75 * This one used to initialize implicitly to zero, so unless the app
76 * has set it to something in particular, we can randomize it now.
77 */
78 if (!_res.id)
79 _res.id = res_randomid();
80
81 atomicinclock (lock);
82 /* Request all threads to re-initialize their resolver states,
83 resolv.conf might have changed. */
84 atomicinc (__res_initstamp);
85 atomicincunlock (lock);
86
87 return (__res_vinit(&_res, 1));
88 }
89
90 /* Initialize resp if RES_INIT is not yet set or if res_init in some other
91 thread requested re-initializing. */
92 int
93 __res_maybe_init (res_state resp, int preinit)
94 {
95 if (resp->options & RES_INIT) {
96 if (__res_initstamp != resp->_u._ext.initstamp) {
97 if (resp->nscount > 0)
98 __res_iclose (resp, true);
99 return __res_vinit (resp, 1);
100 }
101 return 0;
102 } else if (preinit) {
103 if (!resp->retrans)
104 resp->retrans = RES_TIMEOUT;
105 if (!resp->retry)
106 resp->retry = RES_DFLRETRY;
107 resp->options = RES_DEFAULT;
108 if (!resp->id)
109 resp->id = res_randomid ();
110 return __res_vinit (resp, 1);
111 } else
112 return __res_ninit (resp);
113 }
114 libc_hidden_def (__res_maybe_init)
115 \f
116 /* This needs to be after the use of _res in res_init, above. */
117 #undef _res
118
119 /* The resolver state for use by single-threaded programs.
120 This differs from plain `struct __res_state _res;' in that it doesn't
121 create a common definition, but a plain symbol that resides in .bss,
122 which can have an alias. */
123 struct __res_state _res __attribute__ ((nocommon));
124
125 #undef __resp
126 __thread struct __res_state *__resp = &_res;
127 extern __thread struct __res_state *__libc_resp
128 __attribute__ ((alias ("__resp"))) attribute_hidden;
129
130 /* We declare this with compat_symbol so that it's not
131 visible at link time. Programs must use the accessor functions. */
132 #ifdef SHARED
133 # include <shlib-compat.h>
134 compat_symbol (libc, _res, _res, GLIBC_2_0);
135 #endif
136
137 #include <shlib-compat.h>
138
139 #if SHLIB_COMPAT(libc, GLIBC_2_0, GLIBC_2_2)
140 # undef res_init
141 extern int __res_init_weak (void);
142 weak_extern (__res_init_weak);
143 strong_alias (__res_init, __res_init_weak);
144 compat_symbol (libc, __res_init_weak, res_init, GLIBC_2_0);
145 #endif