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