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