Merge commit '281819e5f8b19cd8627541a22d261906fd190276' into merges
[unleashed.git] / usr / src / lib / libresolv2 / common / irs / irs_data.c
blob5d920ee2af43d43852b21242a134a544b4ba8de1
1 /*
2 * Copyright (c) 1997, 2010, Oracle and/or its affiliates. All rights reserved.
3 */
5 /*
6 * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
7 * Copyright (c) 1996,1999 by Internet Software Consortium.
9 * Permission to use, copy, modify, and distribute this software for any
10 * purpose with or without fee is hereby granted, provided that the above
11 * copyright notice and this permission notice appear in all copies.
13 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
14 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
15 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR
16 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
17 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
18 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
19 * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
22 #include "port_before.h"
24 #ifndef __BIND_NOSTATIC
26 #include <sys/types.h>
28 #include <netinet/in.h>
29 #include <arpa/nameser.h>
31 #include <resolv.h>
32 #include <stdio.h>
33 #include <string.h>
34 #include <isc/memcluster.h>
36 #ifdef DO_PTHREADS
37 #include <pthread.h>
38 #endif
40 #include <irs.h>
41 #include <stdlib.h>
43 #include "port_after.h"
45 #include "irs_data.h"
46 #undef _res
47 #if !(__GLIBC__ > 2 || __GLIBC__ == 2 && __GLIBC_MINOR__ >= 3)
48 #undef h_errno
49 extern int h_errno;
50 #endif
52 extern struct __res_state _res;
54 #ifdef DO_PTHREADS
55 static pthread_key_t key;
56 static int once = 0;
57 #else
58 static struct net_data *net_data;
59 #endif
61 void
62 irs_destroy(void) {
63 #ifndef DO_PTHREADS
64 if (net_data != NULL)
65 net_data_destroy(net_data);
66 net_data = NULL;
67 #endif
70 void
71 net_data_destroy(void *p) {
72 struct net_data *net_data = p;
74 res_ndestroy(net_data->res);
75 if (net_data->gr != NULL) {
76 (*net_data->gr->close)(net_data->gr);
77 net_data->gr = NULL;
79 if (net_data->pw != NULL) {
80 (*net_data->pw->close)(net_data->pw);
81 net_data->pw = NULL;
83 if (net_data->sv != NULL) {
84 (*net_data->sv->close)(net_data->sv);
85 net_data->sv = NULL;
87 if (net_data->pr != NULL) {
88 (*net_data->pr->close)(net_data->pr);
89 net_data->pr = NULL;
91 if (net_data->ho != NULL) {
92 (*net_data->ho->close)(net_data->ho);
93 net_data->ho = NULL;
95 if (net_data->nw != NULL) {
96 (*net_data->nw->close)(net_data->nw);
97 net_data->nw = NULL;
99 if (net_data->ng != NULL) {
100 (*net_data->ng->close)(net_data->ng);
101 net_data->ng = NULL;
103 if (net_data->ho_data != NULL) {
104 free(net_data->ho_data);
105 net_data->ho_data = NULL;
107 if (net_data->nw_data != NULL) {
108 free(net_data->nw_data);
109 net_data->nw_data = NULL;
112 (*net_data->irs->close)(net_data->irs);
113 memput(net_data, sizeof *net_data);
117 * applications that need a specific config file other than
118 * _PATH_IRS_CONF should call net_data_init directly rather than letting
119 * the various wrapper functions make the first call. - brister
122 struct net_data *
123 net_data_init(const char *conf_file) {
124 #ifdef DO_PTHREADS
125 #ifndef LIBBIND_MUTEX_INITIALIZER
126 #define LIBBIND_MUTEX_INITIALIZER PTHREAD_MUTEX_INITIALIZER
127 #endif
128 static pthread_mutex_t keylock = LIBBIND_MUTEX_INITIALIZER;
129 struct net_data *net_data;
131 if (!once) {
132 if (pthread_mutex_lock(&keylock) != 0)
133 return (NULL);
134 if (!once) {
135 if (pthread_key_create(&key, net_data_destroy) != 0) {
136 (void)pthread_mutex_unlock(&keylock);
137 return (NULL);
139 once = 1;
141 if (pthread_mutex_unlock(&keylock) != 0)
142 return (NULL);
144 net_data = pthread_getspecific(key);
145 #endif
147 if (net_data == NULL) {
148 net_data = net_data_create(conf_file);
149 if (net_data == NULL)
150 return (NULL);
151 #ifdef DO_PTHREADS
152 if (pthread_setspecific(key, net_data) != 0) {
153 net_data_destroy(net_data);
154 return (NULL);
156 #endif
159 return (net_data);
162 struct net_data *
163 net_data_create(const char *conf_file) {
164 struct net_data *net_data;
166 net_data = memget(sizeof (struct net_data));
167 if (net_data == NULL)
168 return (NULL);
169 memset(net_data, 0, sizeof (struct net_data));
171 if ((net_data->irs = irs_gen_acc("", conf_file)) == NULL) {
172 memput(net_data, sizeof (struct net_data));
173 return (NULL);
175 #ifndef DO_PTHREADS
176 (*net_data->irs->res_set)(net_data->irs, &_res, NULL);
177 #endif
179 net_data->res = (*net_data->irs->res_get)(net_data->irs);
180 if (net_data->res == NULL) {
181 (*net_data->irs->close)(net_data->irs);
182 memput(net_data, sizeof (struct net_data));
183 return (NULL);
186 if ((net_data->res->options & RES_INIT) == 0U &&
187 res_ninit(net_data->res) == -1) {
188 (*net_data->irs->close)(net_data->irs);
189 memput(net_data, sizeof (struct net_data));
190 return (NULL);
193 return (net_data);
196 void
197 net_data_minimize(struct net_data *net_data) {
198 res_nclose(net_data->res);
201 struct __res_state *
202 __res_state(void) {
203 /* NULL param here means use the default config file. */
204 struct net_data *net_data = net_data_init(NULL);
205 if (net_data && net_data->res)
206 return (net_data->res);
208 return (&_res);
211 int *
212 __h_errno(void) {
213 /* NULL param here means use the default config file. */
214 struct net_data *net_data = net_data_init(NULL);
215 if (net_data && net_data->res)
216 return (&net_data->res->res_h_errno);
217 #ifdef ORIGINAL_ISC_CODE
218 #if !(__GLIBC__ > 2 || __GLIBC__ == 2 && __GLIBC_MINOR__ >= 3)
219 return(&_res.res_h_errno);
220 #else
221 return (&h_errno);
222 #endif
223 #else
224 return (&h_errno);
225 #endif /* ORIGINAL_ISC_CODE */
228 void
229 __h_errno_set(struct __res_state *res, int err) {
232 #if (__GLIBC__ > 2 || __GLIBC__ == 2 && __GLIBC_MINOR__ >= 3)
233 res->res_h_errno = err;
234 #else
235 h_errno = res->res_h_errno = err;
236 #endif
239 #endif /*__BIND_NOSTATIC*/
241 /*! \file */