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