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