blob: 459f07394655e49c1fd6a1720d4e1b3a517825d1 [file] [log] [blame]
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in
12 * the documentation and/or other materials provided with the
13 * distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
18 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
19 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
22 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
25 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 */
28#include <sys/cdefs.h>
29#include <sys/types.h>
30#include <arpa/inet.h>
Calin Juravle569fb982014-03-04 15:01:29 +000031#include <arpa/nameser.h>
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080032#include <netdb.h>
33#include "resolv_private.h"
34#include "resolv_cache.h"
35#include <pthread.h>
36#include <stdlib.h>
Elliott Hughes76f89162015-01-26 13:34:58 -080037#include <string.h>
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080038
39#define _REALLY_INCLUDE_SYS__SYSTEM_PROPERTIES_H_
40#include <sys/_system_properties.h>
41
Yabin Cui4a2891d2015-03-04 16:53:23 -080042#include "private/ThreadLocalBuffer.h"
43
David 'Digit' Turnerb6cd6812011-03-17 21:31:33 +010044/* Set to 1 to enable debug traces */
45#define DEBUG 0
46
47#if DEBUG
Elliott Hugheseb847bc2013-10-09 15:50:50 -070048# include "private/libc_logging.h"
David 'Digit' Turnerb6cd6812011-03-17 21:31:33 +010049# include <unistd.h> /* for gettid() */
Elliott Hughes8f2a5a02013-03-15 15:30:25 -070050# define D(...) __libc_format_log(ANDROID_LOG_DEBUG,"libc", __VA_ARGS__)
David 'Digit' Turnerb6cd6812011-03-17 21:31:33 +010051#else
52# define D(...) do{}while(0)
53#endif
54
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080055typedef struct {
David 'Digit' Turnerb6cd6812011-03-17 21:31:33 +010056 int _h_errno;
Szymon Jakubczakea9bf672014-02-14 17:07:23 -050057 // TODO: Have one __res_state per network so we don't have to repopulate frequently.
David 'Digit' Turnerb6cd6812011-03-17 21:31:33 +010058 struct __res_state _nres[1];
59 unsigned _serial;
60 struct prop_info* _pi;
61 struct res_static _rstatic[1];
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080062} _res_thread;
63
64static _res_thread*
65_res_thread_alloc(void)
66{
David 'Digit' Turnerb6cd6812011-03-17 21:31:33 +010067 _res_thread* rt = calloc(1, sizeof(*rt));
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080068
69 if (rt) {
70 rt->_h_errno = 0;
71 /* Special system property which tracks any changes to 'net.*'. */
72 rt->_serial = 0;
73 rt->_pi = (struct prop_info*) __system_property_find("net.change");
74 if (rt->_pi) {
Colin Cross5cf32de2013-01-23 23:07:06 -080075 rt->_serial = __system_property_serial(rt->_pi);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080076 }
David 'Digit' Turnerb6cd6812011-03-17 21:31:33 +010077 memset(rt->_rstatic, 0, sizeof rt->_rstatic);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080078 }
79 return rt;
80}
81
82static void
83_res_static_done( res_static rs )
84{
85 /* fortunately, there is nothing to do here, since the
86 * points in h_addr_ptrs and host_aliases should all
87 * point to 'hostbuf'
88 */
89 if (rs->hostf) { /* should not happen in theory, but just be safe */
90 fclose(rs->hostf);
91 rs->hostf = NULL;
92 }
93 free(rs->servent.s_aliases);
94}
95
96static void
97_res_thread_free( void* _rt )
98{
99 _res_thread* rt = _rt;
100
David 'Digit' Turnerb6cd6812011-03-17 21:31:33 +0100101 D("%s: rt=%p for thread=%d", __FUNCTION__, rt, gettid());
102
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800103 _res_static_done(rt->_rstatic);
104 res_ndestroy(rt->_nres);
105 free(rt);
106}
107
Yabin Cui4a2891d2015-03-04 16:53:23 -0800108BIONIC_PTHREAD_KEY_WITH_CONSTRUCTOR(_res_key, _res_thread_free);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800109
110static _res_thread*
111_res_thread_get(void)
112{
113 _res_thread* rt;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800114 rt = pthread_getspecific( _res_key );
David 'Digit' Turnerb6cd6812011-03-17 21:31:33 +0100115
116 if (rt != NULL) {
117 /* We already have one thread-specific DNS state object.
118 * Check the serial value for any changes to net.* properties */
119 D("%s: Called for tid=%d rt=%p rt->pi=%p rt->serial=%d",
120 __FUNCTION__, gettid(), rt, rt->_pi, rt->_serial);
121 if (rt->_pi == NULL) {
122 /* The property wasn't created when _res_thread_get() was
123 * called the last time. This should only happen very
124 * early during the boot sequence. First, let's try to see if it
125 * is here now. */
126 rt->_pi = (struct prop_info*) __system_property_find("net.change");
127 if (rt->_pi == NULL) {
128 /* Still nothing, return current state */
129 D("%s: exiting for tid=%d rt=%d since system property not found",
130 __FUNCTION__, gettid(), rt);
131 return rt;
132 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800133 }
Colin Cross5cf32de2013-01-23 23:07:06 -0800134 if (rt->_serial == __system_property_serial(rt->_pi)) {
David 'Digit' Turnerb6cd6812011-03-17 21:31:33 +0100135 /* Nothing changed, so return the current state */
136 D("%s: tid=%d rt=%p nothing changed, returning",
137 __FUNCTION__, gettid(), rt);
138 return rt;
139 }
140 /* Update the recorded serial number, and go reset the state */
Colin Cross5cf32de2013-01-23 23:07:06 -0800141 rt->_serial = __system_property_serial(rt->_pi);
David 'Digit' Turnerb6cd6812011-03-17 21:31:33 +0100142 goto RESET_STATE;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800143 }
David 'Digit' Turnerb6cd6812011-03-17 21:31:33 +0100144
145 /* It is the first time this function is called in this thread,
146 * we need to create a new thread-specific DNS resolver state. */
147 rt = _res_thread_alloc();
148 if (rt == NULL) {
149 return NULL;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800150 }
David 'Digit' Turnerb6cd6812011-03-17 21:31:33 +0100151 pthread_setspecific( _res_key, rt );
152 D("%s: tid=%d Created new DNS state rt=%p",
153 __FUNCTION__, gettid(), rt);
154
155RESET_STATE:
156 /* Reset the state, note that res_ninit() can now properly reset
157 * an existing state without leaking memory.
158 */
159 D("%s: tid=%d, rt=%p, resetting DNS state (options RES_INIT=%d)",
160 __FUNCTION__, gettid(), rt, (rt->_nres->options & RES_INIT) != 0);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800161 if ( res_ninit( rt->_nres ) < 0 ) {
David 'Digit' Turnerb6cd6812011-03-17 21:31:33 +0100162 /* This should not happen */
163 D("%s: tid=%d rt=%p, woot, res_ninit() returned < 0",
164 __FUNCTION__, gettid(), rt);
165 _res_thread_free(rt);
166 pthread_setspecific( _res_key, NULL );
167 return NULL;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800168 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800169 return rt;
170}
171
Jim Huang7cc56662010-10-15 02:02:57 +0800172__LIBC_HIDDEN__
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800173struct __res_state _nres;
174
175#if 0
176struct resolv_cache*
177__get_res_cache(void)
178{
179 _res_thread* rt = _res_thread_get();
180
181 if (!rt)
182 return NULL;
183
184 if (!rt->_cache) {
185 rt->_cache = _resolv_cache_create();
186 }
187 return rt->_cache;
188}
189#endif
190
191int*
192__get_h_errno(void)
193{
194 _res_thread* rt = _res_thread_get();
195 static int panic = NETDB_INTERNAL;
196
197 return rt ? &rt->_h_errno : &panic;
198}
199
200res_state
201__res_get_state(void)
202{
203 _res_thread* rt = _res_thread_get();
204
205 return rt ? rt->_nres : NULL;
206}
207
208void
Elliott Hughes68c27552014-07-07 09:44:17 -0700209__res_put_state(res_state res __unused)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800210{
211 /* nothing to do */
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800212}
213
214res_static
215__res_get_static(void)
216{
217 _res_thread* rt = _res_thread_get();
218
219 return rt ? rt->_rstatic : NULL;
220}