res_state.c revision 157594
1156956Sume/*-
2156956Sume * Copyright (c) 2006 The FreeBSD Project. All rights reserved.
3156956Sume *
4156956Sume * Redistribution and use in source and binary forms, with or without
5156956Sume * modification, are permitted provided that the following conditions
6156956Sume * are met:
7156956Sume * 1. Redistributions of source code must retain the above copyright
8156956Sume *    notice, this list of conditions and the following disclaimer.
9156956Sume * 2. Redistributions in binary form must reproduce the above copyright
10156956Sume *    notice, this list of conditions and the following disclaimer in the
11156956Sume *    documentation and/or other materials provided with the distribution.
12156956Sume *
13156956Sume * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
14156956Sume * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15156956Sume * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16156956Sume * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
17156956Sume * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18156956Sume * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19156956Sume * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20156956Sume * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21156956Sume * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22156956Sume * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
23156956Sume * SUCH DAMAGE.
24156956Sume *
25156956Sume * $FreeBSD: head/lib/libc/resolv/res_state.c 157594 2006-04-08 18:19:35Z ume $
26156956Sume */
27156956Sume
28156956Sume#include <sys/types.h>
29156956Sume#include <netinet/in.h>
30156956Sume#include <arpa/nameser.h>
31156956Sume#include <resolv.h>
32156956Sume#include <stdlib.h>
33156956Sume
34156956Sume#include "namespace.h"
35156956Sume#include "reentrant.h"
36156956Sume#include "un-namespace.h"
37156956Sume
38156956Sume#undef _res
39156956Sume
40156956Sumestruct __res_state _res;
41156956Sume
42156956Sumestatic thread_key_t res_key;
43156956Sumestatic once_t res_init_once = ONCE_INITIALIZER;
44156956Sumestatic int res_thr_keycreated = 0;
45156956Sume
46156956Sumestatic void
47156956Sumefree_res(void *ptr)
48156956Sume{
49156956Sume	res_state statp = ptr;
50156956Sume
51157594Sume	if (statp->_u._ext.ext != NULL)
52156956Sume		res_ndestroy(statp);
53156956Sume	free(statp);
54156956Sume}
55156956Sume
56156956Sumestatic void
57156956Sumeres_keycreate(void)
58156956Sume{
59156956Sume	res_thr_keycreated = thr_keycreate(&res_key, free_res) == 0;
60156956Sume}
61156956Sume
62156956Sumeres_state
63156956Sume__res_state(void)
64156956Sume{
65156956Sume	res_state statp;
66156956Sume
67156956Sume	if (thr_main() != 0)
68156956Sume		return (&_res);
69156956Sume
70156956Sume	if (thr_once(&res_init_once, res_keycreate) != 0 ||
71156956Sume	    !res_thr_keycreated)
72156956Sume		return (&_res);
73156956Sume
74156956Sume	statp = thr_getspecific(res_key);
75156956Sume	if (statp != NULL)
76156956Sume		return (statp);
77156956Sume	statp = calloc(1, sizeof(*statp));
78156956Sume	if (statp == NULL)
79156956Sume		return (&_res);
80156956Sume#ifdef __BIND_RES_TEXT
81156956Sume	statp->options = RES_TIMEOUT;			/* Motorola, et al. */
82156956Sume#endif
83156956Sume	if (thr_setspecific(res_key, statp) == 0)
84156956Sume		return (statp);
85156956Sume	free(statp);
86156956Sume	return (&_res);
87156956Sume}
88156956Sume
89156956Sume/* binary backward compatibility for FreeBSD 5.x and 6.x */
90156956Sumestruct __res_state_ext *
91156956Sume___res_ext(void)
92156956Sume{
93156956Sume	return (__res_state()->_u._ext.ext);
94156956Sume}
95156956Sume
96156956Sume__weak_reference(__res_state, ___res);
97