1ee65b806SJan Lentfer #include <port_before.h> 2ee65b806SJan Lentfer #ifdef DO_PTHREADS 3ee65b806SJan Lentfer #include <pthread.h> 4ee65b806SJan Lentfer #ifdef _LIBC 5ee65b806SJan Lentfer #include <pthread_np.h> 6ee65b806SJan Lentfer #endif 7ee65b806SJan Lentfer #endif 8ee65b806SJan Lentfer #include <errno.h> 9ee65b806SJan Lentfer #include <netdb.h> 10ee65b806SJan Lentfer #include <stdlib.h> 11ee65b806SJan Lentfer #include <string.h> 12ee65b806SJan Lentfer #include "resolv_mt.h" 13ee65b806SJan Lentfer #ifndef _LIBC 14ee65b806SJan Lentfer #include <irs.h> 15ee65b806SJan Lentfer #endif 16ee65b806SJan Lentfer #include <port_after.h> 17ee65b806SJan Lentfer 18ee65b806SJan Lentfer #ifdef DO_PTHREADS 19ee65b806SJan Lentfer static pthread_key_t key; 20ee65b806SJan Lentfer static int mt_key_initialized = 0; 21ee65b806SJan Lentfer 22ee65b806SJan Lentfer static int __res_init_ctx(void); 23ee65b806SJan Lentfer static void __res_destroy_ctx(void *); 24ee65b806SJan Lentfer 25ee65b806SJan Lentfer #if defined(sun) && !defined(__GNUC__) 26ee65b806SJan Lentfer #pragma init (_mtctxres_init) 27ee65b806SJan Lentfer #endif 28ee65b806SJan Lentfer #endif 29ee65b806SJan Lentfer 30ee65b806SJan Lentfer static mtctxres_t sharedctx; 31ee65b806SJan Lentfer 32ee65b806SJan Lentfer #ifdef DO_PTHREADS 33ee65b806SJan Lentfer /* 34ee65b806SJan Lentfer * Initialize the TSD key. By doing this at library load time, we're 35ee65b806SJan Lentfer * implicitly running without interference from other threads, so there's 36ee65b806SJan Lentfer * no need for locking. 37ee65b806SJan Lentfer */ 38ee65b806SJan Lentfer static void 39ee65b806SJan Lentfer _mtctxres_init(void) { 40ee65b806SJan Lentfer int pthread_keycreate_ret; 41ee65b806SJan Lentfer 42ee65b806SJan Lentfer pthread_keycreate_ret = pthread_key_create(&key, __res_destroy_ctx); 43ee65b806SJan Lentfer if (pthread_keycreate_ret == 0) 44ee65b806SJan Lentfer mt_key_initialized = 1; 45ee65b806SJan Lentfer } 46ee65b806SJan Lentfer #endif 47ee65b806SJan Lentfer 48ee65b806SJan Lentfer #ifndef _LIBC 49ee65b806SJan Lentfer /* 50ee65b806SJan Lentfer * To support binaries that used the private MT-safe interface in 51ee65b806SJan Lentfer * Solaris 8, we still need to provide the __res_enable_mt() 52ee65b806SJan Lentfer * and __res_disable_mt() entry points. They're do-nothing routines. 53ee65b806SJan Lentfer */ 54ee65b806SJan Lentfer int 55ee65b806SJan Lentfer __res_enable_mt(void) { 56ee65b806SJan Lentfer return (-1); 57ee65b806SJan Lentfer } 58ee65b806SJan Lentfer #endif 59ee65b806SJan Lentfer 60ee65b806SJan Lentfer int 61ee65b806SJan Lentfer __res_disable_mt(void) { 62ee65b806SJan Lentfer return (0); 63ee65b806SJan Lentfer } 64ee65b806SJan Lentfer 65ee65b806SJan Lentfer #ifdef DO_PTHREADS 66ee65b806SJan Lentfer static int 67ee65b806SJan Lentfer __res_init_ctx(void) { 68ee65b806SJan Lentfer 69ee65b806SJan Lentfer mtctxres_t *mt; 70ee65b806SJan Lentfer int ret; 71ee65b806SJan Lentfer 72ee65b806SJan Lentfer 73ee65b806SJan Lentfer if (pthread_getspecific(key) != 0) { 74ee65b806SJan Lentfer /* Already exists */ 75ee65b806SJan Lentfer return (0); 76ee65b806SJan Lentfer } 77ee65b806SJan Lentfer 78*678e8cc6SSascha Wildner if ((mt = malloc(sizeof (mtctxres_t))) == NULL) { 79ee65b806SJan Lentfer errno = ENOMEM; 80ee65b806SJan Lentfer return (-1); 81ee65b806SJan Lentfer } 82ee65b806SJan Lentfer 83ee65b806SJan Lentfer memset(mt, 0, sizeof (mtctxres_t)); 84ee65b806SJan Lentfer 85ee65b806SJan Lentfer if ((ret = pthread_setspecific(key, mt)) != 0) { 86ee65b806SJan Lentfer free(mt); 87ee65b806SJan Lentfer errno = ret; 88ee65b806SJan Lentfer return (-1); 89ee65b806SJan Lentfer } 90ee65b806SJan Lentfer 91ee65b806SJan Lentfer return (0); 92ee65b806SJan Lentfer } 93ee65b806SJan Lentfer 94ee65b806SJan Lentfer static void 95ee65b806SJan Lentfer __res_destroy_ctx(void *value) { 96ee65b806SJan Lentfer 97ee65b806SJan Lentfer mtctxres_t *mt = (mtctxres_t *)value; 98ee65b806SJan Lentfer 99*678e8cc6SSascha Wildner if (mt != NULL) 100ee65b806SJan Lentfer free(mt); 101ee65b806SJan Lentfer } 102ee65b806SJan Lentfer #endif 103ee65b806SJan Lentfer 104ee65b806SJan Lentfer mtctxres_t * 105ee65b806SJan Lentfer ___mtctxres(void) { 106ee65b806SJan Lentfer #ifdef DO_PTHREADS 107ee65b806SJan Lentfer mtctxres_t *mt; 108ee65b806SJan Lentfer 109ee65b806SJan Lentfer #ifdef _LIBC 110ee65b806SJan Lentfer if (pthread_main_np() != 0) 111ee65b806SJan Lentfer return (&sharedctx); 112ee65b806SJan Lentfer #endif 113ee65b806SJan Lentfer 114ee65b806SJan Lentfer 115ee65b806SJan Lentfer /* 116ee65b806SJan Lentfer * This if clause should only be executed if we are linking 117ee65b806SJan Lentfer * statically. When linked dynamically _mtctxres_init() should 118ee65b806SJan Lentfer * be called at binding time due the #pragma above. 119ee65b806SJan Lentfer */ 120ee65b806SJan Lentfer if (!mt_key_initialized) { 121ee65b806SJan Lentfer static pthread_mutex_t keylock = PTHREAD_MUTEX_INITIALIZER; 122ee65b806SJan Lentfer if (pthread_mutex_lock(&keylock) == 0) { 123ee65b806SJan Lentfer _mtctxres_init(); 124ee65b806SJan Lentfer (void) pthread_mutex_unlock(&keylock); 125ee65b806SJan Lentfer } 126ee65b806SJan Lentfer } 127ee65b806SJan Lentfer 128ee65b806SJan Lentfer /* 129ee65b806SJan Lentfer * If we have already been called in this thread return the existing 130ee65b806SJan Lentfer * context. Otherwise recreat a new context and return it. If 131ee65b806SJan Lentfer * that fails return a global context. 132ee65b806SJan Lentfer */ 133ee65b806SJan Lentfer if (mt_key_initialized) { 134*678e8cc6SSascha Wildner if (((mt = pthread_getspecific(key)) != NULL) || 135ee65b806SJan Lentfer (__res_init_ctx() == 0 && 136*678e8cc6SSascha Wildner (mt = pthread_getspecific(key)) != NULL)) { 137ee65b806SJan Lentfer return (mt); 138ee65b806SJan Lentfer } 139ee65b806SJan Lentfer } 140ee65b806SJan Lentfer #endif 141ee65b806SJan Lentfer return (&sharedctx); 142ee65b806SJan Lentfer } 143