Back to index

glibc  2.9
res_libc.c
Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 1995-1999 by Internet Software Consortium.
00003  *
00004  * Permission to use, copy, modify, and distribute this software for any
00005  * purpose with or without fee is hereby granted, provided that the above
00006  * copyright notice and this permission notice appear in all copies.
00007  *
00008  * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
00009  * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
00010  * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
00011  * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
00012  * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
00013  * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
00014  * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
00015  * SOFTWARE.
00016  */
00017 
00018 #include <atomic.h>
00019 #include <limits.h>
00020 #include <sys/types.h>
00021 #include <netinet/in.h>
00022 #include <arpa/nameser.h>
00023 #include <resolv.h>
00024 #include <bits/libc-lock.h>
00025 
00026 
00027 /* The following bit is copied from res_data.c (where it is #ifdef'ed
00028    out) since res_init() should go into libc.so but the rest of that
00029    file should not.  */
00030 
00031 extern unsigned long long int __res_initstamp attribute_hidden;
00032 /* We have atomic increment operations on 64-bit platforms.  */
00033 #if __WORDSIZE == 64
00034 # define atomicinclock(lock) (void) 0
00035 # define atomicincunlock(lock) (void) 0
00036 # define atomicinc(var) catomic_increment (&(var))
00037 #else
00038 __libc_lock_define_initialized (static, lock);
00039 # define atomicinclock(lock) __libc_lock_lock (lock)
00040 # define atomicincunlock(lock) __libc_lock_unlock (lock)
00041 # define atomicinc(var) ++var
00042 #endif
00043 
00044 int
00045 res_init(void) {
00046        extern int __res_vinit(res_state, int);
00047 
00048        /*
00049         * These three fields used to be statically initialized.  This made
00050         * it hard to use this code in a shared library.  It is necessary,
00051         * now that we're doing dynamic initialization here, that we preserve
00052         * the old semantics: if an application modifies one of these three
00053         * fields of _res before res_init() is called, res_init() will not
00054         * alter them.  Of course, if an application is setting them to
00055         * _zero_ before calling res_init(), hoping to override what used
00056         * to be the static default, we can't detect it and unexpected results
00057         * will follow.  Zero for any of these fields would make no sense,
00058         * so one can safely assume that the applications were already getting
00059         * unexpected results.
00060         *
00061         * _res.options is tricky since some apps were known to diddle the bits
00062         * before res_init() was first called. We can't replicate that semantic
00063         * with dynamic initialization (they may have turned bits off that are
00064         * set in RES_DEFAULT).  Our solution is to declare such applications
00065         * "broken".  They could fool us by setting RES_INIT but none do (yet).
00066         */
00067        if (!_res.retrans)
00068               _res.retrans = RES_TIMEOUT;
00069        if (!_res.retry)
00070               _res.retry = 4;
00071        if (!(_res.options & RES_INIT))
00072               _res.options = RES_DEFAULT;
00073        else if (_res.nscount > 0)
00074               __res_iclose (&_res, true); /* Close any VC sockets.  */
00075 
00076        /*
00077         * This one used to initialize implicitly to zero, so unless the app
00078         * has set it to something in particular, we can randomize it now.
00079         */
00080        if (!_res.id)
00081               _res.id = res_randomid();
00082 
00083        atomicinclock (lock);
00084        /* Request all threads to re-initialize their resolver states,
00085           resolv.conf might have changed.  */
00086        atomicinc (__res_initstamp);
00087        atomicincunlock (lock);
00088 
00089        return (__res_vinit(&_res, 1));
00090 }
00091 
00092 /* Initialize resp if RES_INIT is not yet set or if res_init in some other
00093    thread requested re-initializing.  */
00094 int
00095 __res_maybe_init (res_state resp, int preinit)
00096 {
00097        if (resp->options & RES_INIT) {
00098               if (__res_initstamp != resp->_u._ext.initstamp) {
00099                      if (resp->nscount > 0) {
00100                             __res_iclose (resp, true);
00101                             return __res_vinit (resp, 1);
00102                      }
00103               }
00104               return 0;
00105        } else if (preinit) {
00106               if (!resp->retrans)
00107                      resp->retrans = RES_TIMEOUT;
00108               if (!resp->retry)
00109                      resp->retry = 4;
00110               resp->options = RES_DEFAULT;
00111               if (!resp->id)
00112                      resp->id = res_randomid ();
00113               return __res_vinit (resp, 1);
00114        } else
00115               return __res_ninit (resp);
00116 }
00117 libc_hidden_def (__res_maybe_init)
00118 
00119 /* This needs to be after the use of _res in res_init, above.  */
00120 #undef _res
00121 
00122 /* The resolver state for use by single-threaded programs.
00123    This differs from plain `struct __res_state _res;' in that it doesn't
00124    create a common definition, but a plain symbol that resides in .bss,
00125    which can have an alias.  */
00126 struct __res_state _res __attribute__((section (".bss")));
00127 
00128 #include <tls.h>
00129 
00130 #if USE___THREAD
00131 #undef __resp
00132 __thread struct __res_state *__resp = &_res;
00133 extern __thread struct __res_state *__libc_resp
00134   __attribute__ ((alias ("__resp"))) attribute_hidden;
00135 #endif
00136 
00137 /* We declare this with compat_symbol so that it's not
00138    visible at link time.  Programs must use the accessor functions.  */
00139 #if defined HAVE_ELF && defined SHARED && defined DO_VERSIONING
00140 # include <shlib-compat.h>
00141 compat_symbol (libc, _res, _res, GLIBC_2_0);
00142 #endif
00143 
00144 #include <shlib-compat.h>
00145 
00146 #if SHLIB_COMPAT(libc, GLIBC_2_0, GLIBC_2_2)
00147 # undef res_init
00148 extern int __res_init_weak (void);
00149 weak_extern (__res_init_weak);
00150 strong_alias (__res_init, __res_init_weak);
00151 compat_symbol (libc, __res_init_weak, res_init, GLIBC_2_0);
00152 #endif