Optimized version of powf()
[glibc.git] / resolv / res_libc.c
blob3bf3887b0c5232b1fea7dcc44c3a6973507db248
1 /*
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
15 * SOFTWARE.
18 /* This file contains the definitions related to res_init which are
19 linked into libc instead of libresolv. */
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>
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
42 int
43 res_init(void) {
45 * These three fields used to be statically initialized. This made
46 * it hard to use this code in a shared library. It is necessary,
47 * now that we're doing dynamic initialization here, that we preserve
48 * the old semantics: if an application modifies one of these three
49 * fields of _res before res_init() is called, res_init() will not
50 * alter them. Of course, if an application is setting them to
51 * _zero_ before calling res_init(), hoping to override what used
52 * to be the static default, we can't detect it and unexpected results
53 * will follow. Zero for any of these fields would make no sense,
54 * so one can safely assume that the applications were already getting
55 * unexpected results.
57 * _res.options is tricky since some apps were known to diddle the bits
58 * before res_init() was first called. We can't replicate that semantic
59 * with dynamic initialization (they may have turned bits off that are
60 * set in RES_DEFAULT). Our solution is to declare such applications
61 * "broken". They could fool us by setting RES_INIT but none do (yet).
63 if (!_res.retrans)
64 _res.retrans = RES_TIMEOUT;
65 if (!_res.retry)
66 _res.retry = RES_DFLRETRY;
67 if (!(_res.options & RES_INIT))
68 _res.options = RES_DEFAULT;
69 else if (_res.nscount > 0)
70 __res_iclose (&_res, true); /* Close any VC sockets. */
73 * This one used to initialize implicitly to zero, so unless the app
74 * has set it to something in particular, we can randomize it now.
76 if (!_res.id)
77 _res.id = res_randomid();
79 atomicinclock (lock);
80 /* Request all threads to re-initialize their resolver states,
81 resolv.conf might have changed. */
82 atomicinc (__res_initstamp);
83 atomicincunlock (lock);
85 return (__res_vinit(&_res, 1));
88 /* Initialize resp if RES_INIT is not yet set or if res_init in some other
89 thread requested re-initializing. */
90 int
91 __res_maybe_init (res_state resp, int preinit)
93 if (resp->options & RES_INIT) {
94 if (__res_initstamp != resp->_u._ext.initstamp) {
95 if (resp->nscount > 0)
96 __res_iclose (resp, true);
97 return __res_vinit (resp, 1);
99 return 0;
100 } else if (preinit) {
101 if (!resp->retrans)
102 resp->retrans = RES_TIMEOUT;
103 if (!resp->retry)
104 resp->retry = RES_DFLRETRY;
105 resp->options = RES_DEFAULT;
106 if (!resp->id)
107 resp->id = res_randomid ();
108 return __res_vinit (resp, 1);
109 } else
110 return __res_ninit (resp);
112 libc_hidden_def (__res_maybe_init)
114 /* This needs to be after the use of _res in res_init, above. */
115 #undef _res
117 /* The resolver state for use by single-threaded programs.
118 This differs from plain `struct __res_state _res;' in that it doesn't
119 create a common definition, but a plain symbol that resides in .bss,
120 which can have an alias. */
121 struct __res_state _res __attribute__ ((nocommon));
123 #undef __resp
124 __thread struct __res_state *__resp = &_res;
125 extern __thread struct __res_state *__libc_resp
126 __attribute__ ((alias ("__resp"))) attribute_hidden;
128 #include <shlib-compat.h>
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 compat_symbol (libc, _res, _res, GLIBC_2_0);
134 #endif
136 #if SHLIB_COMPAT(libc, GLIBC_2_0, GLIBC_2_2)
137 # undef res_init
138 extern int __res_init_weak (void);
139 weak_extern (__res_init_weak);
140 strong_alias (__res_init, __res_init_weak);
141 compat_symbol (libc, __res_init_weak, res_init, GLIBC_2_0);
142 #endif