1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * CDDL HEADER START
3*0Sstevel@tonic-gate  *
4*0Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*0Sstevel@tonic-gate  * with the License.
8*0Sstevel@tonic-gate  *
9*0Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate  * and limitations under the License.
13*0Sstevel@tonic-gate  *
14*0Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate  *
20*0Sstevel@tonic-gate  * CDDL HEADER END
21*0Sstevel@tonic-gate  */
22*0Sstevel@tonic-gate /*
23*0Sstevel@tonic-gate  * Copyright 1986-2003 Sun Microsystems, Inc.  All rights reserved.
24*0Sstevel@tonic-gate  * Use is subject to license terms.
25*0Sstevel@tonic-gate  */
26*0Sstevel@tonic-gate 
27*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
28*0Sstevel@tonic-gate 
29*0Sstevel@tonic-gate /*
30*0Sstevel@tonic-gate  *	Define and initialize MT data for libnsl.
31*0Sstevel@tonic-gate  *	The _libnsl_lock_init() function below is the library's .init handler.
32*0Sstevel@tonic-gate  */
33*0Sstevel@tonic-gate 
34*0Sstevel@tonic-gate #include "mt.h"
35*0Sstevel@tonic-gate #include "rpc_mt.h"
36*0Sstevel@tonic-gate #include <unistd.h>
37*0Sstevel@tonic-gate #include <rpc/rpc.h>
38*0Sstevel@tonic-gate #include <sys/time.h>
39*0Sstevel@tonic-gate #include <stdlib.h>
40*0Sstevel@tonic-gate #include <syslog.h>
41*0Sstevel@tonic-gate 
42*0Sstevel@tonic-gate extern	mutex_t	_ti_userlock;
43*0Sstevel@tonic-gate 
44*0Sstevel@tonic-gate sigset_t fillset;		/* from sigfillset() */
45*0Sstevel@tonic-gate 
46*0Sstevel@tonic-gate rwlock_t	svc_lock;	/* protects the services list (svc.c) */
47*0Sstevel@tonic-gate rwlock_t	svc_fd_lock;	/* protects svc_fdset and the xports[] array */
48*0Sstevel@tonic-gate rwlock_t	rpcbaddr_cache_lock; /* protects the RPCBIND address cache */
49*0Sstevel@tonic-gate static rwlock_t	*rwlock_table[] = {
50*0Sstevel@tonic-gate 	&svc_lock,
51*0Sstevel@tonic-gate 	&svc_fd_lock,
52*0Sstevel@tonic-gate 	&rpcbaddr_cache_lock
53*0Sstevel@tonic-gate };
54*0Sstevel@tonic-gate 
55*0Sstevel@tonic-gate mutex_t	authdes_lock;		/* protects authdes cache (svcauth_des.c) */
56*0Sstevel@tonic-gate mutex_t	authnone_lock;		/* auth_none.c serialization */
57*0Sstevel@tonic-gate mutex_t	authsvc_lock;		/* protects the Auths list (svc_auth.c) */
58*0Sstevel@tonic-gate mutex_t	clntraw_lock;		/* clnt_raw.c serialization */
59*0Sstevel@tonic-gate mutex_t	dname_lock;		/* domainname and domain_fd (getdname.c) */
60*0Sstevel@tonic-gate 				/*	and default_domain (rpcdname.c) */
61*0Sstevel@tonic-gate mutex_t	dupreq_lock;		/* dupreq variables (svc_dg.c) */
62*0Sstevel@tonic-gate mutex_t	keyserv_lock;		/* protects first_time and hostname */
63*0Sstevel@tonic-gate 				/*	(key_call.c) */
64*0Sstevel@tonic-gate mutex_t	libnsl_trace_lock;	/* serializes rpc_trace() (rpc_trace.c) */
65*0Sstevel@tonic-gate mutex_t	loopnconf_lock;		/* loopnconf (rpcb_clnt.c) */
66*0Sstevel@tonic-gate mutex_t	ops_lock;		/* serializes ops initializations */
67*0Sstevel@tonic-gate mutex_t	portnum_lock;		/* protects ``port'' static in bindresvport() */
68*0Sstevel@tonic-gate mutex_t	proglst_lock;		/* protects proglst list (svc_simple.c) */
69*0Sstevel@tonic-gate mutex_t	rpcsoc_lock;		/* serializes clnt_com_create() (rpc_soc.c) */
70*0Sstevel@tonic-gate mutex_t	svcraw_lock;		/* svc_raw.c serialization */
71*0Sstevel@tonic-gate mutex_t	tsd_lock;		/* protects TSD key creation */
72*0Sstevel@tonic-gate mutex_t	xprtlist_lock;		/* xprtlist (svc_generic.c) */
73*0Sstevel@tonic-gate mutex_t serialize_pkey;		/* serializes calls to public key routines */
74*0Sstevel@tonic-gate mutex_t	svc_thr_mutex;		/* protects thread related variables */
75*0Sstevel@tonic-gate mutex_t	svc_mutex;		/* protects service handle free lists */
76*0Sstevel@tonic-gate mutex_t	svc_exit_mutex;		/* used for clean mt exit */
77*0Sstevel@tonic-gate 
78*0Sstevel@tonic-gate static mutex_t	*mutex_table[] = {
79*0Sstevel@tonic-gate 	&authdes_lock,
80*0Sstevel@tonic-gate 	&authnone_lock,
81*0Sstevel@tonic-gate 	&authsvc_lock,
82*0Sstevel@tonic-gate 	&clntraw_lock,
83*0Sstevel@tonic-gate 	&dname_lock,
84*0Sstevel@tonic-gate 	&dupreq_lock,
85*0Sstevel@tonic-gate 	&keyserv_lock,
86*0Sstevel@tonic-gate 	&libnsl_trace_lock,
87*0Sstevel@tonic-gate 	&loopnconf_lock,
88*0Sstevel@tonic-gate 	&ops_lock,
89*0Sstevel@tonic-gate 	&portnum_lock,
90*0Sstevel@tonic-gate 	&proglst_lock,
91*0Sstevel@tonic-gate 	&rpcsoc_lock,
92*0Sstevel@tonic-gate 	&svcraw_lock,
93*0Sstevel@tonic-gate 	&tsd_lock,
94*0Sstevel@tonic-gate 	&xprtlist_lock,
95*0Sstevel@tonic-gate 	&serialize_pkey,
96*0Sstevel@tonic-gate 	&svc_thr_mutex,
97*0Sstevel@tonic-gate 	&svc_mutex,
98*0Sstevel@tonic-gate 	&svc_exit_mutex
99*0Sstevel@tonic-gate };
100*0Sstevel@tonic-gate 
101*0Sstevel@tonic-gate cond_t	svc_thr_fdwait;		/* threads wait on this for work */
102*0Sstevel@tonic-gate 
103*0Sstevel@tonic-gate static void
104*0Sstevel@tonic-gate _libnsl_prefork()
105*0Sstevel@tonic-gate {
106*0Sstevel@tonic-gate 	mutex_lock(&_ti_userlock);
107*0Sstevel@tonic-gate }
108*0Sstevel@tonic-gate 
109*0Sstevel@tonic-gate static void
110*0Sstevel@tonic-gate _libnsl_child_atfork()
111*0Sstevel@tonic-gate {
112*0Sstevel@tonic-gate 	mutex_unlock(&_ti_userlock);
113*0Sstevel@tonic-gate }
114*0Sstevel@tonic-gate 
115*0Sstevel@tonic-gate static void
116*0Sstevel@tonic-gate _libnsl_parent_atfork()
117*0Sstevel@tonic-gate {
118*0Sstevel@tonic-gate 	mutex_unlock(&_ti_userlock);
119*0Sstevel@tonic-gate }
120*0Sstevel@tonic-gate 
121*0Sstevel@tonic-gate #pragma init(_libnsl_lock_init)
122*0Sstevel@tonic-gate 
123*0Sstevel@tonic-gate void
124*0Sstevel@tonic-gate _libnsl_lock_init()
125*0Sstevel@tonic-gate {
126*0Sstevel@tonic-gate 	int	i;
127*0Sstevel@tonic-gate 
128*0Sstevel@tonic-gate 	(void) _sigfillset(&fillset);
129*0Sstevel@tonic-gate 
130*0Sstevel@tonic-gate 	for (i = 0; i <  (sizeof (mutex_table) / sizeof (mutex_table[0])); i++)
131*0Sstevel@tonic-gate 		mutex_init(mutex_table[i], 0, (void *) 0);
132*0Sstevel@tonic-gate 
133*0Sstevel@tonic-gate 	for (i = 0; i < (sizeof (rwlock_table) / sizeof (rwlock_table[0])); i++)
134*0Sstevel@tonic-gate 		rwlock_init(rwlock_table[i], 0, (void *) 0);
135*0Sstevel@tonic-gate 
136*0Sstevel@tonic-gate 	cond_init(&svc_thr_fdwait, USYNC_THREAD, 0);
137*0Sstevel@tonic-gate 
138*0Sstevel@tonic-gate 	/*
139*0Sstevel@tonic-gate 	 * There is no way to unregister these atfork functions,
140*0Sstevel@tonic-gate 	 * but we don't need to.  The dynamic linker and libc take
141*0Sstevel@tonic-gate 	 * care of unregistering them if/when the library is unloaded.
142*0Sstevel@tonic-gate 	 */
143*0Sstevel@tonic-gate 	(void) pthread_atfork(_libnsl_prefork,
144*0Sstevel@tonic-gate 		_libnsl_parent_atfork, _libnsl_child_atfork);
145*0Sstevel@tonic-gate }
146*0Sstevel@tonic-gate 
147*0Sstevel@tonic-gate #pragma fini(_libnsl_fini)
148*0Sstevel@tonic-gate 
149*0Sstevel@tonic-gate void _key_call_fini(void);
150*0Sstevel@tonic-gate 
151*0Sstevel@tonic-gate void
152*0Sstevel@tonic-gate _libnsl_fini()
153*0Sstevel@tonic-gate {
154*0Sstevel@tonic-gate 	_key_call_fini();
155*0Sstevel@tonic-gate }
156*0Sstevel@tonic-gate 
157*0Sstevel@tonic-gate #undef	rpc_createerr
158*0Sstevel@tonic-gate 
159*0Sstevel@tonic-gate struct rpc_createerr rpc_createerr;
160*0Sstevel@tonic-gate 
161*0Sstevel@tonic-gate struct rpc_createerr *
162*0Sstevel@tonic-gate __rpc_createerr()
163*0Sstevel@tonic-gate {
164*0Sstevel@tonic-gate 	static pthread_key_t rce_key = 0;
165*0Sstevel@tonic-gate 	struct rpc_createerr *rce_addr;
166*0Sstevel@tonic-gate 
167*0Sstevel@tonic-gate 	if (thr_main())
168*0Sstevel@tonic-gate 		return (&rpc_createerr);
169*0Sstevel@tonic-gate 	rce_addr = thr_get_storage(&rce_key, sizeof (*rce_addr), free);
170*0Sstevel@tonic-gate 	if (rce_addr == NULL) {
171*0Sstevel@tonic-gate 		syslog(LOG_ERR, "__rpc_createerr : out of memory.");
172*0Sstevel@tonic-gate 		return (&rpc_createerr);
173*0Sstevel@tonic-gate 	}
174*0Sstevel@tonic-gate 	return (rce_addr);
175*0Sstevel@tonic-gate }
176*0Sstevel@tonic-gate 
177*0Sstevel@tonic-gate #undef rpc_callerr
178*0Sstevel@tonic-gate 
179*0Sstevel@tonic-gate struct rpc_err rpc_callerr;
180*0Sstevel@tonic-gate 
181*0Sstevel@tonic-gate struct rpc_err *
182*0Sstevel@tonic-gate __rpc_callerr(void)
183*0Sstevel@tonic-gate {
184*0Sstevel@tonic-gate 	static pthread_key_t rpc_callerr_key = 0;
185*0Sstevel@tonic-gate 	struct rpc_err *tsd = 0;
186*0Sstevel@tonic-gate 
187*0Sstevel@tonic-gate 	if (thr_main())
188*0Sstevel@tonic-gate 		return (&rpc_callerr);
189*0Sstevel@tonic-gate 	tsd = thr_get_storage(&rpc_callerr_key, sizeof (struct rpc_err), free);
190*0Sstevel@tonic-gate 	if (tsd == NULL) {
191*0Sstevel@tonic-gate 		syslog(LOG_ERR, "__rpc_callerr : out of memory.");
192*0Sstevel@tonic-gate 		return (&rpc_callerr);
193*0Sstevel@tonic-gate 	}
194*0Sstevel@tonic-gate 	return (tsd);
195*0Sstevel@tonic-gate }
196