other-licenses/android/res_state.c

Tue, 06 Jan 2015 21:39:09 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Tue, 06 Jan 2015 21:39:09 +0100
branch
TOR_BUG_9701
changeset 8
97036ab72558
permissions
-rw-r--r--

Conditionally force memory storage according to privacy.thirdparty.isolate;
This solves Tor bug #9701, complying with disk avoidance documented in
https://www.torproject.org/projects/torbrowser/design/#disk-avoidance.

     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  */
    29 /*
    30  * This version of this file is derived from Android 2.3 "Gingerbread",
    31  * which contains uncredited changes by Android/Google developers.  It has
    32  * been modified in 2011 for use in the Android build of Mozilla Firefox by
    33  * Mozilla contributors (including Michael Edwards <m.k.edwards@gmail.com>,
    34  * and Steve Workman <sjhworkman@gmail.com>).
    35  * These changes are offered under the same license as the original NetBSD
    36  * file, whose copyright and license are unchanged above.
    37  */
    39 /*
    40  * This version of this file is derived from Android 2.3 "Gingerbread",
    41  * which contains uncredited changes by Android/Google developers.  It has
    42  * been modified in 2011 for use in the Android build of Mozilla Firefox by
    43  * Mozilla contributors (including Michael Edwards <m.k.edwards@gmail.com>,
    44  * and Steve Workman <sjhworkman@gmail.com>).
    45  * These changes are offered under the same license as the original NetBSD
    46  * file, whose copyright and license are unchanged above.
    47  */
    49 #define ANDROID_CHANGES 1
    50 #define MOZILLA_NECKO_EXCLUDE_CODE 1
    52 #include <sys/cdefs.h>
    53 #include <sys/types.h>
    54 #include <arpa/inet.h>
    55 #include "arpa_nameser.h"
    56 #include <netdb.h>
    57 #include "resolv_private.h"
    58 #include "resolv_cache.h"
    59 #include <pthread.h>
    60 #include <stdlib.h>
    62 #define _REALLY_INCLUDE_SYS__SYSTEM_PROPERTIES_H_
    63 #include <sys/_system_properties.h>
    65 static pthread_key_t   _res_key;
    66 static pthread_once_t  _res_once;
    68 typedef struct {
    69     int                    _h_errno;
    70     struct __res_state     _nres[1];
    71     unsigned               _serial;
    72     struct prop_info*      _pi;
    73     struct res_static      _rstatic[1];
    74 } _res_thread;
    76 static _res_thread*
    77 _res_thread_alloc(void)
    78 {
    79     _res_thread*  rt = malloc(sizeof(*rt));
    81     if (rt) {
    82         rt->_h_errno = 0;
    83         /* Special system property which tracks any changes to 'net.*'. */
    84         rt->_serial = 0;
    85         rt->_pi = (struct prop_info*) __system_property_find("net.change");
    86         if (rt->_pi) {
    87             rt->_serial = rt->_pi->serial;
    88         }
    89         if ( res_ninit( rt->_nres ) < 0 ) {
    90             free(rt);
    91             rt = NULL;
    92         } else {
    93             memset(rt->_rstatic, 0, sizeof rt->_rstatic);
    94         }
    95     }
    96     return rt;
    97 }
    99 static void
   100 _res_static_done( res_static  rs )
   101 {
   102     /* fortunately, there is nothing to do here, since the
   103      * points in h_addr_ptrs and host_aliases should all
   104      * point to 'hostbuf'
   105      */
   106     if (rs->hostf) {  /* should not happen in theory, but just be safe */
   107         fclose(rs->hostf);
   108         rs->hostf = NULL;
   109     }
   110     free(rs->servent.s_aliases);
   111 }
   113 static void
   114 _res_thread_free( void*  _rt )
   115 {
   116     _res_thread*  rt = _rt;
   118     _res_static_done(rt->_rstatic);
   119     res_ndestroy(rt->_nres);
   120     free(rt);
   121 }
   123 static void
   124 _res_init_key( void )
   125 {
   126     pthread_key_create( &_res_key, _res_thread_free );
   127 }
   129 static _res_thread*
   130 _res_thread_get(void)
   131 {
   132     _res_thread*  rt;
   133     pthread_once( &_res_once, _res_init_key );
   134     rt = pthread_getspecific( _res_key );
   135     if (rt == NULL) {
   136         if ((rt = _res_thread_alloc()) == NULL) {
   137             return NULL;
   138         }
   139         rt->_h_errno = 0;
   140         rt->_serial = 0;
   141         pthread_setspecific( _res_key, rt );
   142     }
   143     /* Check the serial value for any chanes to net.* properties. */
   144     if (rt->_pi == NULL) {
   145         rt->_pi = (struct prop_info*) __system_property_find("net.change");
   146     }
   147     if (rt->_pi == NULL || rt->_serial == rt->_pi->serial) {
   148         return rt;
   149     }
   150     rt->_serial = rt->_pi->serial;
   151     /* Reload from system properties. */
   152     if ( res_ninit( rt->_nres ) < 0 ) {
   153         free(rt);
   154         rt = NULL;
   155         pthread_setspecific( _res_key, rt );
   156     }
   157 #ifdef USE_RESOLV_CACHE
   158     _resolv_cache_reset(rt->_serial);
   159 #endif
   160     return rt;
   161 }
   163 struct __res_state _nres;
   165 #if 0
   166 struct resolv_cache*
   167 __get_res_cache(void)
   168 {
   169     _res_thread*  rt = _res_thread_get();
   171     if (!rt)
   172         return NULL;
   174     if (!rt->_cache) {
   175         rt->_cache = _resolv_cache_create();
   176     }
   177     return rt->_cache;
   178 }
   179 #endif
   181 int*
   182 __get_h_errno(void)
   183 {
   184     _res_thread*  rt    = _res_thread_get();
   185     static int    panic = NETDB_INTERNAL;
   187     return rt ? &rt->_h_errno : &panic;
   188 }
   190 res_state
   191 __res_get_state(void)
   192 {
   193     _res_thread*  rt = _res_thread_get();
   195     return rt ? rt->_nres : NULL;
   196 }
   198 void
   199 __res_put_state(res_state res)
   200 {
   201     /* nothing to do */
   202     res=res;
   203 }
   205 res_static
   206 __res_get_static(void)
   207 {
   208     _res_thread*  rt = _res_thread_get();
   210     return rt ? rt->_rstatic : NULL;
   211 }

mercurial