other-licenses/android/res_state.c

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/other-licenses/android/res_state.c	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,211 @@
     1.4 +/*
     1.5 + * Copyright (C) 2008 The Android Open Source Project
     1.6 + * All rights reserved.
     1.7 + *
     1.8 + * Redistribution and use in source and binary forms, with or without
     1.9 + * modification, are permitted provided that the following conditions
    1.10 + * are met:
    1.11 + *  * Redistributions of source code must retain the above copyright
    1.12 + *    notice, this list of conditions and the following disclaimer.
    1.13 + *  * Redistributions in binary form must reproduce the above copyright
    1.14 + *    notice, this list of conditions and the following disclaimer in
    1.15 + *    the documentation and/or other materials provided with the
    1.16 + *    distribution.
    1.17 + *
    1.18 + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
    1.19 + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
    1.20 + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
    1.21 + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
    1.22 + * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
    1.23 + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
    1.24 + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
    1.25 + * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
    1.26 + * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
    1.27 + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
    1.28 + * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
    1.29 + * SUCH DAMAGE.
    1.30 + */
    1.31 +
    1.32 +/*
    1.33 + * This version of this file is derived from Android 2.3 "Gingerbread",
    1.34 + * which contains uncredited changes by Android/Google developers.  It has
    1.35 + * been modified in 2011 for use in the Android build of Mozilla Firefox by
    1.36 + * Mozilla contributors (including Michael Edwards <m.k.edwards@gmail.com>,
    1.37 + * and Steve Workman <sjhworkman@gmail.com>).
    1.38 + * These changes are offered under the same license as the original NetBSD
    1.39 + * file, whose copyright and license are unchanged above.
    1.40 + */
    1.41 +
    1.42 +/*
    1.43 + * This version of this file is derived from Android 2.3 "Gingerbread",
    1.44 + * which contains uncredited changes by Android/Google developers.  It has
    1.45 + * been modified in 2011 for use in the Android build of Mozilla Firefox by
    1.46 + * Mozilla contributors (including Michael Edwards <m.k.edwards@gmail.com>,
    1.47 + * and Steve Workman <sjhworkman@gmail.com>).
    1.48 + * These changes are offered under the same license as the original NetBSD
    1.49 + * file, whose copyright and license are unchanged above.
    1.50 + */
    1.51 +
    1.52 +#define ANDROID_CHANGES 1
    1.53 +#define MOZILLA_NECKO_EXCLUDE_CODE 1
    1.54 +
    1.55 +#include <sys/cdefs.h>
    1.56 +#include <sys/types.h>
    1.57 +#include <arpa/inet.h>
    1.58 +#include "arpa_nameser.h"
    1.59 +#include <netdb.h>
    1.60 +#include "resolv_private.h"
    1.61 +#include "resolv_cache.h"
    1.62 +#include <pthread.h>
    1.63 +#include <stdlib.h>
    1.64 +
    1.65 +#define _REALLY_INCLUDE_SYS__SYSTEM_PROPERTIES_H_
    1.66 +#include <sys/_system_properties.h>
    1.67 +
    1.68 +static pthread_key_t   _res_key;
    1.69 +static pthread_once_t  _res_once;
    1.70 +
    1.71 +typedef struct {
    1.72 +    int                    _h_errno;
    1.73 +    struct __res_state     _nres[1];
    1.74 +    unsigned               _serial;
    1.75 +    struct prop_info*      _pi;
    1.76 +    struct res_static      _rstatic[1];
    1.77 +} _res_thread;
    1.78 +
    1.79 +static _res_thread*
    1.80 +_res_thread_alloc(void)
    1.81 +{
    1.82 +    _res_thread*  rt = malloc(sizeof(*rt));
    1.83 +
    1.84 +    if (rt) {
    1.85 +        rt->_h_errno = 0;
    1.86 +        /* Special system property which tracks any changes to 'net.*'. */
    1.87 +        rt->_serial = 0;
    1.88 +        rt->_pi = (struct prop_info*) __system_property_find("net.change");
    1.89 +        if (rt->_pi) {
    1.90 +            rt->_serial = rt->_pi->serial;
    1.91 +        }
    1.92 +        if ( res_ninit( rt->_nres ) < 0 ) {
    1.93 +            free(rt);
    1.94 +            rt = NULL;
    1.95 +        } else {
    1.96 +            memset(rt->_rstatic, 0, sizeof rt->_rstatic);
    1.97 +        }
    1.98 +    }
    1.99 +    return rt;
   1.100 +}
   1.101 +
   1.102 +static void
   1.103 +_res_static_done( res_static  rs )
   1.104 +{
   1.105 +    /* fortunately, there is nothing to do here, since the
   1.106 +     * points in h_addr_ptrs and host_aliases should all
   1.107 +     * point to 'hostbuf'
   1.108 +     */
   1.109 +    if (rs->hostf) {  /* should not happen in theory, but just be safe */
   1.110 +        fclose(rs->hostf);
   1.111 +        rs->hostf = NULL;
   1.112 +    }
   1.113 +    free(rs->servent.s_aliases);
   1.114 +}
   1.115 +
   1.116 +static void
   1.117 +_res_thread_free( void*  _rt )
   1.118 +{
   1.119 +    _res_thread*  rt = _rt;
   1.120 +
   1.121 +    _res_static_done(rt->_rstatic);
   1.122 +    res_ndestroy(rt->_nres);
   1.123 +    free(rt);
   1.124 +}
   1.125 +
   1.126 +static void
   1.127 +_res_init_key( void )
   1.128 +{
   1.129 +    pthread_key_create( &_res_key, _res_thread_free );
   1.130 +}
   1.131 +
   1.132 +static _res_thread*
   1.133 +_res_thread_get(void)
   1.134 +{
   1.135 +    _res_thread*  rt;
   1.136 +    pthread_once( &_res_once, _res_init_key );
   1.137 +    rt = pthread_getspecific( _res_key );
   1.138 +    if (rt == NULL) {
   1.139 +        if ((rt = _res_thread_alloc()) == NULL) {
   1.140 +            return NULL;
   1.141 +        }
   1.142 +        rt->_h_errno = 0;
   1.143 +        rt->_serial = 0;
   1.144 +        pthread_setspecific( _res_key, rt );
   1.145 +    }
   1.146 +    /* Check the serial value for any chanes to net.* properties. */
   1.147 +    if (rt->_pi == NULL) {
   1.148 +        rt->_pi = (struct prop_info*) __system_property_find("net.change");
   1.149 +    }
   1.150 +    if (rt->_pi == NULL || rt->_serial == rt->_pi->serial) {
   1.151 +        return rt;
   1.152 +    }
   1.153 +    rt->_serial = rt->_pi->serial;
   1.154 +    /* Reload from system properties. */
   1.155 +    if ( res_ninit( rt->_nres ) < 0 ) {
   1.156 +        free(rt);
   1.157 +        rt = NULL;
   1.158 +        pthread_setspecific( _res_key, rt );
   1.159 +    }
   1.160 +#ifdef USE_RESOLV_CACHE
   1.161 +    _resolv_cache_reset(rt->_serial);
   1.162 +#endif
   1.163 +    return rt;
   1.164 +}
   1.165 +
   1.166 +struct __res_state _nres;
   1.167 +
   1.168 +#if 0
   1.169 +struct resolv_cache*
   1.170 +__get_res_cache(void)
   1.171 +{
   1.172 +    _res_thread*  rt = _res_thread_get();
   1.173 +
   1.174 +    if (!rt)
   1.175 +        return NULL;
   1.176 +
   1.177 +    if (!rt->_cache) {
   1.178 +        rt->_cache = _resolv_cache_create();
   1.179 +    }
   1.180 +    return rt->_cache;
   1.181 +}
   1.182 +#endif
   1.183 +
   1.184 +int*
   1.185 +__get_h_errno(void)
   1.186 +{
   1.187 +    _res_thread*  rt    = _res_thread_get();
   1.188 +    static int    panic = NETDB_INTERNAL;
   1.189 +
   1.190 +    return rt ? &rt->_h_errno : &panic;
   1.191 +}
   1.192 +
   1.193 +res_state
   1.194 +__res_get_state(void)
   1.195 +{
   1.196 +    _res_thread*  rt = _res_thread_get();
   1.197 +
   1.198 +    return rt ? rt->_nres : NULL;
   1.199 +}
   1.200 +
   1.201 +void
   1.202 +__res_put_state(res_state res)
   1.203 +{
   1.204 +    /* nothing to do */
   1.205 +    res=res;
   1.206 +}
   1.207 +
   1.208 +res_static
   1.209 +__res_get_static(void)
   1.210 +{
   1.211 +    _res_thread*  rt = _res_thread_get();
   1.212 +
   1.213 +    return rt ? rt->_rstatic : NULL;
   1.214 +}

mercurial