res-close.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /* Deallocation functions for the resolver state.
  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) 1985, 1989, 1993
  17. * The Regents of the University of California. All rights reserved.
  18. *
  19. * Redistribution and use in source and binary forms, with or without
  20. * modification, are permitted provided that the following conditions
  21. * are met:
  22. * 1. Redistributions of source code must retain the above copyright
  23. * notice, this list of conditions and the following disclaimer.
  24. * 2. Redistributions in binary form must reproduce the above copyright
  25. * notice, this list of conditions and the following disclaimer in the
  26. * documentation and/or other materials provided with the distribution.
  27. * 4. Neither the name of the University nor the names of its contributors
  28. * may be used to endorse or promote products derived from this software
  29. * without specific prior written permission.
  30. *
  31. * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  32. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  33. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  34. * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  35. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  36. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  37. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  38. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  39. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  40. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  41. * SUCH DAMAGE.
  42. */
  43. /*
  44. * Portions Copyright (c) 1993 by Digital Equipment Corporation.
  45. *
  46. * Permission to use, copy, modify, and distribute this software for any
  47. * purpose with or without fee is hereby granted, provided that the above
  48. * copyright notice and this permission notice appear in all copies, and that
  49. * the name of Digital Equipment Corporation not be used in advertising or
  50. * publicity pertaining to distribution of the document or software without
  51. * specific, written prior permission.
  52. *
  53. * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL
  54. * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES
  55. * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL DIGITAL EQUIPMENT
  56. * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
  57. * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
  58. * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
  59. * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
  60. * SOFTWARE.
  61. */
  62. /*
  63. * Portions Copyright (c) 1996-1999 by Internet Software Consortium.
  64. *
  65. * Permission to use, copy, modify, and distribute this software for any
  66. * purpose with or without fee is hereby granted, provided that the above
  67. * copyright notice and this permission notice appear in all copies.
  68. *
  69. * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
  70. * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
  71. * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
  72. * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
  73. * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
  74. * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
  75. * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
  76. * SOFTWARE.
  77. */
  78. #include <resolv-internal.h>
  79. #include <resolv_context.h>
  80. #include <resolv_conf.h>
  81. #include <not-cancel.h>
  82. #include <stdlib.h>
  83. /* Close all open sockets. If FREE_ADDR is true, deallocate any
  84. separately allocated name server addresses. */
  85. void
  86. __res_iclose (res_state statp, bool free_addr)
  87. {
  88. if (statp->_vcsock >= 0)
  89. {
  90. __close_nocancel_nostatus (statp->_vcsock);
  91. statp->_vcsock = -1;
  92. statp->_flags &= ~(RES_F_VC | RES_F_CONN);
  93. }
  94. for (int ns = 0; ns < statp->nscount; ns++)
  95. if (statp->_u._ext.nsaddrs[ns] != NULL)
  96. {
  97. if (statp->_u._ext.nssocks[ns] != -1)
  98. {
  99. __close_nocancel_nostatus (statp->_u._ext.nssocks[ns]);
  100. statp->_u._ext.nssocks[ns] = -1;
  101. }
  102. if (free_addr)
  103. {
  104. free (statp->_u._ext.nsaddrs[ns]);
  105. statp->_u._ext.nsaddrs[ns] = NULL;
  106. }
  107. }
  108. if (free_addr)
  109. __resolv_conf_detach (statp);
  110. }
  111. libc_hidden_def (__res_iclose)
  112. void
  113. res_nclose (res_state statp)
  114. {
  115. __res_iclose (statp, true);
  116. }
  117. libc_hidden_def (__res_nclose)
  118. /* This is called when a thread is exiting to free resources held in _res. */
  119. void
  120. __res_thread_freeres (void)
  121. {
  122. __resolv_context_freeres ();
  123. if (_res.nscount == 0)
  124. /* Never called res_ninit. */
  125. return;
  126. __res_iclose (&_res, true); /* Close any VC sockets. */
  127. /* Make sure we do a full re-initialization the next time. */
  128. _res.options = 0;
  129. }
  130. /* Also must be called when the main thread exits. */
  131. text_set_element (__libc_subfreeres, __res_thread_freeres);