res_state.c revision 1.6.8.2 1 1.6.8.2 martin /* $NetBSD: res_state.c,v 1.6.8.2 2008/04/28 20:23:03 martin Exp $ */
2 1.6.8.2 martin
3 1.6.8.2 martin /*-
4 1.6.8.2 martin * Copyright (c) 2004 The NetBSD Foundation, Inc.
5 1.6.8.2 martin * All rights reserved.
6 1.6.8.2 martin *
7 1.6.8.2 martin * This code is derived from software contributed to The NetBSD Foundation
8 1.6.8.2 martin * by Christos Zoulas.
9 1.6.8.2 martin *
10 1.6.8.2 martin * Redistribution and use in source and binary forms, with or without
11 1.6.8.2 martin * modification, are permitted provided that the following conditions
12 1.6.8.2 martin * are met:
13 1.6.8.2 martin * 1. Redistributions of source code must retain the above copyright
14 1.6.8.2 martin * notice, this list of conditions and the following disclaimer.
15 1.6.8.2 martin * 2. Redistributions in binary form must reproduce the above copyright
16 1.6.8.2 martin * notice, this list of conditions and the following disclaimer in the
17 1.6.8.2 martin * documentation and/or other materials provided with the distribution.
18 1.6.8.2 martin *
19 1.6.8.2 martin * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 1.6.8.2 martin * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 1.6.8.2 martin * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 1.6.8.2 martin * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 1.6.8.2 martin * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 1.6.8.2 martin * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 1.6.8.2 martin * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 1.6.8.2 martin * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 1.6.8.2 martin * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 1.6.8.2 martin * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 1.6.8.2 martin * POSSIBILITY OF SUCH DAMAGE.
30 1.6.8.2 martin */
31 1.6.8.2 martin
32 1.6.8.2 martin #include <sys/cdefs.h>
33 1.6.8.2 martin #if defined(LIBC_SCCS) && !defined(lint)
34 1.6.8.2 martin __RCSID("$NetBSD: res_state.c,v 1.6.8.2 2008/04/28 20:23:03 martin Exp $");
35 1.6.8.2 martin #endif
36 1.6.8.2 martin
37 1.6.8.2 martin #include <sys/types.h>
38 1.6.8.2 martin #include <sys/queue.h>
39 1.6.8.2 martin #include <arpa/inet.h>
40 1.6.8.2 martin #include <arpa/nameser.h>
41 1.6.8.2 martin #include <stdlib.h>
42 1.6.8.2 martin #include <unistd.h>
43 1.6.8.2 martin #include <resolv.h>
44 1.6.8.2 martin #include <netdb.h>
45 1.6.8.2 martin
46 1.6.8.2 martin #include "pthread.h"
47 1.6.8.2 martin #include "pthread_int.h"
48 1.6.8.2 martin
49 1.6.8.2 martin static SLIST_HEAD(, _res_st) res_list = LIST_HEAD_INITIALIZER(&res_list);
50 1.6.8.2 martin
51 1.6.8.2 martin struct _res_st {
52 1.6.8.2 martin /* __res_put_state() assumes st_res is the first member. */
53 1.6.8.2 martin struct __res_state st_res;
54 1.6.8.2 martin
55 1.6.8.2 martin SLIST_ENTRY(_res_st) st_list;
56 1.6.8.2 martin };
57 1.6.8.2 martin
58 1.6.8.2 martin static pthread_mutex_t res_mtx = PTHREAD_MUTEX_INITIALIZER;
59 1.6.8.2 martin
60 1.6.8.2 martin res_state __res_state(void);
61 1.6.8.2 martin res_state __res_get_state(void);
62 1.6.8.2 martin void __res_put_state(res_state);
63 1.6.8.2 martin
64 1.6.8.2 martin #ifdef RES_STATE_DEBUG
65 1.6.8.2 martin static void
66 1.6.8.2 martin res_state_debug(const char *msg, void *p)
67 1.6.8.2 martin {
68 1.6.8.2 martin char buf[512];
69 1.6.8.2 martin pthread_t self = pthread__self();
70 1.6.8.2 martin int len = snprintf(buf, sizeof(buf), "%p: %s %p\n", self, msg, p);
71 1.6.8.2 martin
72 1.6.8.2 martin (void)write(STDOUT_FILENO, buf, (size_t)len);
73 1.6.8.2 martin }
74 1.6.8.2 martin #else
75 1.6.8.2 martin #define res_state_debug(a, b)
76 1.6.8.2 martin #endif
77 1.6.8.2 martin
78 1.6.8.2 martin
79 1.6.8.2 martin res_state
80 1.6.8.2 martin __res_get_state(void)
81 1.6.8.2 martin {
82 1.6.8.2 martin res_state res;
83 1.6.8.2 martin struct _res_st *st;
84 1.6.8.2 martin pthread_mutex_lock(&res_mtx);
85 1.6.8.2 martin st = SLIST_FIRST(&res_list);
86 1.6.8.2 martin if (st != NULL) {
87 1.6.8.2 martin SLIST_REMOVE_HEAD(&res_list, st_list);
88 1.6.8.2 martin pthread_mutex_unlock(&res_mtx);
89 1.6.8.2 martin res = &st->st_res;
90 1.6.8.2 martin res_state_debug("checkout from list", st);
91 1.6.8.2 martin } else {
92 1.6.8.2 martin pthread_mutex_unlock(&res_mtx);
93 1.6.8.2 martin st = malloc(sizeof(*st));
94 1.6.8.2 martin if (st == NULL) {
95 1.6.8.2 martin h_errno = NETDB_INTERNAL;
96 1.6.8.2 martin return NULL;
97 1.6.8.2 martin }
98 1.6.8.2 martin res = &st->st_res;
99 1.6.8.2 martin res->options = 0;
100 1.6.8.2 martin res_state_debug("alloc new", res);
101 1.6.8.2 martin }
102 1.6.8.2 martin if ((res->options & RES_INIT) == 0) {
103 1.6.8.2 martin if (res_ninit(res) == -1) {
104 1.6.8.2 martin h_errno = NETDB_INTERNAL;
105 1.6.8.2 martin free(st);
106 1.6.8.2 martin return NULL;
107 1.6.8.2 martin }
108 1.6.8.2 martin }
109 1.6.8.2 martin return res;
110 1.6.8.2 martin }
111 1.6.8.2 martin
112 1.6.8.2 martin void
113 1.6.8.2 martin /*ARGSUSED*/
114 1.6.8.2 martin __res_put_state(res_state res)
115 1.6.8.2 martin {
116 1.6.8.2 martin struct _res_st *st = (struct _res_st *)(void *)res;
117 1.6.8.2 martin
118 1.6.8.2 martin res_state_debug("free", res);
119 1.6.8.2 martin pthread_mutex_lock(&res_mtx);
120 1.6.8.2 martin SLIST_INSERT_HEAD(&res_list, st, st_list);
121 1.6.8.2 martin pthread_mutex_unlock(&res_mtx);
122 1.6.8.2 martin }
123 1.6.8.2 martin
124 1.6.8.2 martin /*
125 1.6.8.2 martin * This is aliased via a macro to _res; don't allow multi-threaded programs
126 1.6.8.2 martin * to use it.
127 1.6.8.2 martin */
128 1.6.8.2 martin res_state
129 1.6.8.2 martin __res_state(void)
130 1.6.8.2 martin {
131 1.6.8.2 martin static const char res[] = "_res is not supported for multi-threaded"
132 1.6.8.2 martin " programs.\n";
133 1.6.8.2 martin (void)write(STDERR_FILENO, res, sizeof(res) - 1);
134 1.6.8.2 martin abort();
135 1.6.8.2 martin return NULL;
136 1.6.8.2 martin }
137