Prevent an implicit int promotion in malloc/tst-alloc_buffer.c
[glibc.git] / resolv / res_libc.c
blob3d7b4f72d0dda60393510749a5a4c3ab17aa2f2a
1 /* Definitions related to res_init linked into libc instead of libresolv.
2 Copyright (C) 1995-2017 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <http://www.gnu.org/licenses/>. */
20 * Copyright (c) 1995-1999 by Internet Software Consortium.
22 * Permission to use, copy, modify, and distribute this software for any
23 * purpose with or without fee is hereby granted, provided that the above
24 * copyright notice and this permission notice appear in all copies.
26 * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
27 * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
28 * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
29 * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
30 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
31 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
32 * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
33 * SOFTWARE.
36 #include <atomic.h>
37 #include <limits.h>
38 #include <sys/types.h>
39 #include <netinet/in.h>
40 #include <arpa/nameser.h>
41 #include <resolv.h>
42 #include <libc-lock.h>
43 #include <resolv-internal.h>
45 /* We have atomic increment operations on 64-bit platforms. */
46 #if __WORDSIZE == 64
47 # define atomicinclock(lock) (void) 0
48 # define atomicincunlock(lock) (void) 0
49 # define atomicinc(var) catomic_increment (&(var))
50 #else
51 __libc_lock_define_initialized (static, lock);
52 # define atomicinclock(lock) __libc_lock_lock (lock)
53 # define atomicincunlock(lock) __libc_lock_unlock (lock)
54 # define atomicinc(var) ++var
55 #endif
57 int
58 res_init (void)
60 /* These three fields used to be statically initialized. This made
61 it hard to use this code in a shared library. It is necessary,
62 now that we're doing dynamic initialization here, that we
63 preserve the old semantics: if an application modifies one of
64 these three fields of _res before res_init is called,
65 res_init will not alter them. Of course, if an application is
66 setting them to _zero_ before calling res_init, hoping to
67 override what used to be the static default, we can't detect it
68 and unexpected results will follow. Zero for any of these fields
69 would make no sense, so one can safely assume that the
70 applications were already getting unexpected results.
72 _res.options is tricky since some apps were known to diddle the
73 bits before res_init was first called. We can't replicate that
74 semantic with dynamic initialization (they may have turned bits
75 off that are set in RES_DEFAULT). Our solution is to declare
76 such applications "broken". They could fool us by setting
77 RES_INIT but none do (yet). */
78 if (!_res.retrans)
79 _res.retrans = RES_TIMEOUT;
80 if (!_res.retry)
81 _res.retry = RES_DFLRETRY;
82 if (!(_res.options & RES_INIT))
83 _res.options = RES_DEFAULT;
84 else if (_res.nscount > 0)
85 __res_iclose (&_res, true); /* Close any VC sockets. */
87 /* This one used to initialize implicitly to zero, so unless the app
88 has set it to something in particular, we can randomize it *
89 now. */
90 if (!_res.id)
91 _res.id = res_randomid ();
93 atomicinclock (lock);
94 /* Request all threads to re-initialize their resolver states,
95 resolv.conf might have changed. */
96 atomicinc (__res_initstamp);
97 atomicincunlock (lock);
99 return __res_vinit (&_res, 1);
102 /* Initialize *RESP if RES_INIT is not yet set in RESP->options, or if
103 res_init in some other thread requested re-initializing. */
105 __res_maybe_init (res_state resp, int preinit)
107 if (resp->options & RES_INIT)
109 if (__res_initstamp != resp->_u._ext.initstamp)
111 if (resp->nscount > 0)
112 __res_iclose (resp, true);
113 return __res_vinit (resp, 1);
115 return 0;
117 else if (preinit)
119 if (!resp->retrans)
120 resp->retrans = RES_TIMEOUT;
121 if (!resp->retry)
122 resp->retry = RES_DFLRETRY;
123 resp->options = RES_DEFAULT;
124 if (!resp->id)
125 resp->id = res_randomid ();
126 return __res_vinit (resp, 1);
128 else
129 return __res_ninit (resp);
131 libc_hidden_def (__res_maybe_init)
133 /* This needs to be after the use of _res in res_init, above. */
134 #undef _res
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__ ((nocommon));
142 #undef __resp
143 __thread struct __res_state *__resp = &_res;
144 extern __thread struct __res_state *__libc_resp
145 __attribute__ ((alias ("__resp"))) attribute_hidden;
147 #include <shlib-compat.h>
149 /* We declare this with compat_symbol so that it's not
150 visible at link time. Programs must use the accessor functions. */
151 #ifdef SHARED
152 compat_symbol (libc, _res, _res, GLIBC_2_0);
153 #endif
155 #if SHLIB_COMPAT (libc, GLIBC_2_0, GLIBC_2_2)
156 # undef res_init
157 extern int __res_init_weak (void);
158 weak_extern (__res_init_weak);
159 strong_alias (__res_init, __res_init_weak);
160 compat_symbol (libc, __res_init_weak, res_init, GLIBC_2_0);
161 #endif