res_state.c revision 7cc5666d94615d9249163dc7ac0f58c2614497ce
1/*
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>
31#include "arpa_nameser.h"
32#include <netdb.h>
33#include "resolv_private.h"
34#include "resolv_cache.h"
35#include <pthread.h>
36#include <stdlib.h>
37
38#define _REALLY_INCLUDE_SYS__SYSTEM_PROPERTIES_H_
39#include <sys/_system_properties.h>
40
41static pthread_key_t   _res_key;
42static pthread_once_t  _res_once;
43
44typedef struct {
45    int                    _h_errno;
46    struct __res_state     _nres[1];
47    unsigned               _serial;
48    struct prop_info*      _pi;
49    struct res_static      _rstatic[1];
50} _res_thread;
51
52static _res_thread*
53_res_thread_alloc(void)
54{
55    _res_thread*  rt = malloc(sizeof(*rt));
56
57    if (rt) {
58        rt->_h_errno = 0;
59        /* Special system property which tracks any changes to 'net.*'. */
60        rt->_serial = 0;
61        rt->_pi = (struct prop_info*) __system_property_find("net.change");
62        if (rt->_pi) {
63            rt->_serial = rt->_pi->serial;
64        }
65        if ( res_ninit( rt->_nres ) < 0 ) {
66            free(rt);
67            rt = NULL;
68        } else {
69            memset(rt->_rstatic, 0, sizeof rt->_rstatic);
70        }
71    }
72    return rt;
73}
74
75static void
76_res_static_done( res_static  rs )
77{
78    /* fortunately, there is nothing to do here, since the
79     * points in h_addr_ptrs and host_aliases should all
80     * point to 'hostbuf'
81     */
82    if (rs->hostf) {  /* should not happen in theory, but just be safe */
83        fclose(rs->hostf);
84        rs->hostf = NULL;
85    }
86    free(rs->servent.s_aliases);
87}
88
89static void
90_res_thread_free( void*  _rt )
91{
92    _res_thread*  rt = _rt;
93
94    _res_static_done(rt->_rstatic);
95    res_ndestroy(rt->_nres);
96    free(rt);
97}
98
99static void
100_res_init_key( void )
101{
102    pthread_key_create( &_res_key, _res_thread_free );
103}
104
105static _res_thread*
106_res_thread_get(void)
107{
108    _res_thread*  rt;
109    pthread_once( &_res_once, _res_init_key );
110    rt = pthread_getspecific( _res_key );
111    if (rt == NULL) {
112        if ((rt = _res_thread_alloc()) == NULL) {
113            return NULL;
114        }
115        rt->_h_errno = 0;
116        rt->_serial = 0;
117        pthread_setspecific( _res_key, rt );
118    }
119    /* Check the serial value for any chanes to net.* properties. */
120    if (rt->_pi == NULL) {
121        rt->_pi = (struct prop_info*) __system_property_find("net.change");
122    }
123    if (rt->_pi == NULL || rt->_serial == rt->_pi->serial) {
124        return rt;
125    }
126    rt->_serial = rt->_pi->serial;
127    /* Reload from system properties. */
128    if ( res_ninit( rt->_nres ) < 0 ) {
129        free(rt);
130        rt = NULL;
131        pthread_setspecific( _res_key, rt );
132    }
133    _resolv_cache_reset(rt->_serial);
134    return rt;
135}
136
137__LIBC_HIDDEN__
138struct __res_state _nres;
139
140#if 0
141struct resolv_cache*
142__get_res_cache(void)
143{
144    _res_thread*  rt = _res_thread_get();
145
146    if (!rt)
147        return NULL;
148
149    if (!rt->_cache) {
150        rt->_cache = _resolv_cache_create();
151    }
152    return rt->_cache;
153}
154#endif
155
156int*
157__get_h_errno(void)
158{
159    _res_thread*  rt    = _res_thread_get();
160    static int    panic = NETDB_INTERNAL;
161
162    return rt ? &rt->_h_errno : &panic;
163}
164
165res_state
166__res_get_state(void)
167{
168    _res_thread*  rt = _res_thread_get();
169
170    return rt ? rt->_nres : NULL;
171}
172
173void
174__res_put_state(res_state res)
175{
176    /* nothing to do */
177    res=res;
178}
179
180res_static
181__res_get_static(void)
182{
183    _res_thread*  rt = _res_thread_get();
184
185    return rt ? rt->_rstatic : NULL;
186}
187