summaryrefslogtreecommitdiff
path: root/usr/src/lib/nsswitch/dns/common/dns_mt.c
blob: 4af3f671c06747400e8b4e86bc47c475d6c22f6c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License (the "License").
 * You may not use this file except in compliance with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */

/*
 * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 */
/*
 * Copyright (c) 2013, Joyent, Inc.  All rights reserved.
 */

/*
 * dns_mt.c
 *
 * This file contains all the MT related routines for the DNS backend.
 */

#include "dns_common.h"
#include <dlfcn.h>

/*
 * If the DNS name service switch routines are used in a binary that depends
 * on an older libresolv (libresolv.so.1, say), then having nss_dns.so.1 or
 * libnss_dns.a depend on a newer libresolv (libresolv.so.2) will cause
 * relocation problems. In particular, copy relocation of the _res structure
 * (which changes in size from libresolv.so.1 to libresolv.so.2) could
 * cause corruption, and result in a number of strange problems, including
 * core dumps. Hence, we check if a libresolv is already loaded.
 */

#pragma init(_nss_dns_init)
static void	_nss_dns_init(void);

extern struct hostent *res_gethostbyname(const char *);

#define	RES_SET_NO_HOSTS_FALLBACK	"__joy_res_set_no_hosts_fallback"
extern void	__joy_res_set_no_hosts_fallback(void);

#define	RES_UNSET_NO_HOSTS_FALLBACK	"__joy_res_unset_no_hosts_fallback"
extern void	__joy_res_unset_no_hosts_fallback(void);

#define		RES_GET_RES	"__res_get_res"
extern struct __res_state	*__res_get_res(void);

#define		RES_ENABLE_MT			"__res_enable_mt"
extern int	__res_enable_mt(void);

#define		RES_DISABLE_MT			"__res_disable_mt"
extern int	__res_disable_mt(void);

#define		RES_GET_H_ERRNO			"__res_get_h_errno"
extern int	*__res_get_h_errno();

#define		__H_ERRNO			"__joy_h_errno"
extern int	*__joy_h_errno(void);

#define		RES_OVERRIDE_RETRY		"__joy_res_override_retry"
extern int	__joy_res_override_retry(int);

static void	__fallback_set_no_hosts(void);
static int	__is_mt_safe(void);

void	(*set_no_hosts_fallback)(void) = __fallback_set_no_hosts;
void	(*unset_no_hosts_fallback)(void) = __fallback_set_no_hosts;
struct __res_state	*(*set_res_retry)() = 0;
int	(*enable_mt)() = __is_mt_safe;
int	(*disable_mt)() = __is_mt_safe;
int	*(*get_h_errno)(void) = __joy_h_errno;
int	(*override_retry)(int) = __joy_res_override_retry;

/* Usually set from the Makefile */
#ifndef	NSS_DNS_LIBRESOLV
#define	NSS_DNS_LIBRESOLV	"libresolv.so.2"
#endif

/* From libresolv */
extern	int	h_errno;

mutex_t	one_lane = DEFAULTMUTEX;

/* Because we link against libresolv_joy.so.2, this is relatively easy. */
void
_nss_dns_init(void)
{
	enable_mt = __is_mt_safe;
	disable_mt = __is_mt_safe;
}


/*
 *
 * Integration of BIND 8.1.2 introduced two new Sun private functions,
 * __res_enable_mt() and __res_disable_mt(), that enabled and disabled
 * MT mode per-thread. These functions are in the private libresolv.so.2
 * interface, and intended for use by nss_dns.so.1.
 *
 * BIND 8.2.2 removed the need for those two functions.  As similar
 * functionality was provided in BIND further up the stack. However the
 * functions remain to satisfy any application that directly called upon
 * them.  Only, __res_enable_mt() was modified to return failure.
 * Indicated by a non-zero return value.  So that those unconventional
 * applications would not then presume that res_send() and friends are
 * MT-safe, when in fact they are not.
 *
 * To prevent nss_dns from locking inappropriately __is_mt_safe() is
 * called in place of __res_enable_mt() and __res_disable_mt() if BIND
 * 8.2.2 libresolv.so.2 being used.  __is_mt_safe() returns success
 * indicated by a return code of zero. Signifying that no locking is
 * necessary.
 *
 * MT applications making calls to gethostby*_r() or getipnodeby*()
 * linked to libresolv.so.1 or linked statically with pre-BIND 8.2.2
 * libresolv.a, doubtful as we don't ship a static version, would require
 * locking within the nsswitch back-end.  Hence the mechanism can not
 * simply be removed.
 *
 */
static int
__is_mt_safe(void) {
	return (0);
}


static void
__fallback_set_no_hosts(void) {
}


/*
 * Common code to enable/disable MT mode, set/unset no-/etc/hosts fallback,
 * and to set the number of retries.
 */
void
switch_resolver_setup(int *mt_disabled, sigset_t *oldmask, int *old_retry) {

	/*
	 * Try to enable MT mode. If that isn't possible, mask signals,
	 * and mutex_lock.
	 */
	*mt_disabled = 1;
	if (enable_mt == 0 || (*mt_disabled = (*enable_mt)()) != 0) {
		sigset_t	newmask;
		(void) sigfillset(&newmask);
		(void) thr_sigsetmask(SIG_SETMASK, &newmask, oldmask);
		(void) mutex_lock(&one_lane);
	}

	/*
	 * Disable any fallback to /etc/hosts (or /etc/inet/ipnodes, when
	 * libresolv knows about that file).
	 */
	(*set_no_hosts_fallback)();

	/*
	 * The NS switch wants to handle retries on its own.
	 */
	*old_retry = (*override_retry)(1);
}


void
switch_resolver_reset(int mt_disabled, sigset_t oldmask, int old_retry) {

	if (mt_disabled) {
		(void) mutex_unlock(&one_lane);
		(void) thr_sigsetmask(SIG_SETMASK, &oldmask, NULL);
	} else {
		(void) (*disable_mt)();
	}

	(*unset_no_hosts_fallback)();

	(void) (*override_retry)(old_retry);
}