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