res_libc.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. /* Definitions related to res_init linked into libc instead of libresolv.
  2. Copyright (C) 1995-2019 Free Software Foundation, Inc.
  3. This file is part of the GNU C Library.
  4. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Lesser General Public
  6. License as published by the Free Software Foundation; either
  7. version 2.1 of the License, or (at your option) any later version.
  8. The GNU C Library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with the GNU C Library; if not, see
  14. <http://www.gnu.org/licenses/>. */
  15. /*
  16. * Copyright (c) 1995-1999 by Internet Software Consortium.
  17. *
  18. * Permission to use, copy, modify, and distribute this software for any
  19. * purpose with or without fee is hereby granted, provided that the above
  20. * copyright notice and this permission notice appear in all copies.
  21. *
  22. * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
  23. * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
  24. * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
  25. * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
  26. * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
  27. * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
  28. * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
  29. * SOFTWARE.
  30. */
  31. #include <atomic.h>
  32. #include <limits.h>
  33. #include <sys/types.h>
  34. #include <netinet/in.h>
  35. #include <arpa/nameser.h>
  36. #include <resolv.h>
  37. #include <libc-lock.h>
  38. #include <resolv-internal.h>
  39. int
  40. res_init (void)
  41. {
  42. /* These three fields used to be statically initialized. This made
  43. it hard to use this code in a shared library. It is necessary,
  44. now that we're doing dynamic initialization here, that we
  45. preserve the old semantics: if an application modifies one of
  46. these three fields of _res before res_init is called,
  47. res_init will not alter them. Of course, if an application is
  48. setting them to _zero_ before calling res_init, hoping to
  49. override what used to be the static default, we can't detect it
  50. and unexpected results will follow. Zero for any of these fields
  51. would make no sense, so one can safely assume that the
  52. applications were already getting unexpected results.
  53. _res.options is tricky since some apps were known to diddle the
  54. bits before res_init was first called. We can't replicate that
  55. semantic with dynamic initialization (they may have turned bits
  56. off that are set in RES_DEFAULT). Our solution is to declare
  57. such applications "broken". They could fool us by setting
  58. RES_INIT but none do (yet). */
  59. if (!_res.retrans)
  60. _res.retrans = RES_TIMEOUT;
  61. if (!_res.retry)
  62. _res.retry = RES_DFLRETRY;
  63. if (!(_res.options & RES_INIT))
  64. _res.options = RES_DEFAULT;
  65. else if (_res.nscount > 0)
  66. __res_iclose (&_res, true); /* Close any VC sockets. */
  67. /* This one used to initialize implicitly to zero, so unless the app
  68. has set it to something in particular, we can randomize it *
  69. now. */
  70. if (!_res.id)
  71. _res.id = res_randomid ();
  72. return __res_vinit (&_res, 1);
  73. }
  74. /* This needs to be after the use of _res in res_init, above. */
  75. #undef _res
  76. /* The resolver state for use by single-threaded programs.
  77. This differs from plain `struct __res_state _res;' in that it doesn't
  78. create a common definition, but a plain symbol that resides in .bss,
  79. which can have an alias. */
  80. struct __res_state _res __attribute__ ((nocommon));
  81. #undef __resp
  82. __thread struct __res_state *__resp = &_res;
  83. extern __thread struct __res_state *__libc_resp
  84. __attribute__ ((alias ("__resp"))) attribute_hidden;
  85. #include <shlib-compat.h>
  86. /* We declare this with compat_symbol so that it's not
  87. visible at link time. Programs must use the accessor functions. */
  88. #ifdef SHARED
  89. compat_symbol (libc, _res, _res, GLIBC_2_0);
  90. #endif
  91. #if SHLIB_COMPAT (libc, GLIBC_2_0, GLIBC_2_2)
  92. # undef res_init
  93. extern int __res_init_weak (void);
  94. weak_extern (__res_init_weak);
  95. strong_alias (__res_init, __res_init_weak);
  96. compat_symbol (libc, __res_init_weak, res_init, GLIBC_2_0);
  97. #endif