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