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