xref: /titanic_44/usr/src/lib/libnsl/rpc/mt_misc.c (revision 321502cd0930b1eb6d4805e17f16234f3e3ff4b2)
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 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 /*
30  *	Define and initialize MT data for libnsl.
31  *	The _libnsl_lock_init() function below is the library's .init handler.
32  */
33 
34 #include "mt.h"
35 #include "rpc_mt.h"
36 #include <unistd.h>
37 #include <rpc/rpc.h>
38 #include <sys/time.h>
39 #include <stdlib.h>
40 #include <syslog.h>
41 
42 extern	mutex_t	_ti_userlock;
43 
44 sigset_t fillset;		/* from sigfillset() */
45 
46 rwlock_t	svc_lock;	/* protects the services list (svc.c) */
47 rwlock_t	svc_fd_lock;	/* protects svc_fdset and the xports[] array */
48 rwlock_t	rpcbaddr_cache_lock; /* protects the RPCBIND address cache */
49 static rwlock_t	*rwlock_table[] = {
50 	&svc_lock,
51 	&svc_fd_lock,
52 	&rpcbaddr_cache_lock
53 };
54 
55 mutex_t	authdes_lock;		/* protects authdes cache (svcauth_des.c) */
56 mutex_t	authnone_lock;		/* auth_none.c serialization */
57 mutex_t	authsvc_lock;		/* protects the Auths list (svc_auth.c) */
58 mutex_t	clntraw_lock;		/* clnt_raw.c serialization */
59 mutex_t	dname_lock;		/* domainname and domain_fd (getdname.c) */
60 				/*	and default_domain (rpcdname.c) */
61 mutex_t	dupreq_lock;		/* dupreq variables (svc_dg.c) */
62 mutex_t	keyserv_lock;		/* protects first_time and hostname */
63 				/*	(key_call.c) */
64 mutex_t	libnsl_trace_lock;	/* serializes rpc_trace() (rpc_trace.c) */
65 mutex_t	loopnconf_lock;		/* loopnconf (rpcb_clnt.c) */
66 mutex_t	ops_lock;		/* serializes ops initializations */
67 mutex_t	portnum_lock;		/* protects ``port'' static in bindresvport() */
68 mutex_t	proglst_lock;		/* protects proglst list (svc_simple.c) */
69 mutex_t	rpcsoc_lock;		/* serializes clnt_com_create() (rpc_soc.c) */
70 mutex_t	svcraw_lock;		/* svc_raw.c serialization */
71 mutex_t	xprtlist_lock;		/* xprtlist (svc_generic.c) */
72 mutex_t serialize_pkey;		/* serializes calls to public key routines */
73 mutex_t	svc_thr_mutex;		/* protects thread related variables */
74 mutex_t	svc_mutex;		/* protects service handle free lists */
75 mutex_t	svc_exit_mutex;		/* used for clean mt exit */
76 
77 static mutex_t	*mutex_table[] = {
78 	&authdes_lock,
79 	&authnone_lock,
80 	&authsvc_lock,
81 	&clntraw_lock,
82 	&dname_lock,
83 	&dupreq_lock,
84 	&keyserv_lock,
85 	&libnsl_trace_lock,
86 	&loopnconf_lock,
87 	&ops_lock,
88 	&portnum_lock,
89 	&proglst_lock,
90 	&rpcsoc_lock,
91 	&svcraw_lock,
92 	&xprtlist_lock,
93 	&serialize_pkey,
94 	&svc_thr_mutex,
95 	&svc_mutex,
96 	&svc_exit_mutex
97 };
98 
99 cond_t	svc_thr_fdwait;		/* threads wait on this for work */
100 
101 static void
102 _libnsl_prefork()
103 {
104 	(void) mutex_lock(&_ti_userlock);
105 }
106 
107 static void
108 _libnsl_child_atfork()
109 {
110 	(void) mutex_unlock(&_ti_userlock);
111 }
112 
113 static void
114 _libnsl_parent_atfork()
115 {
116 	(void) mutex_unlock(&_ti_userlock);
117 }
118 
119 #pragma init(_libnsl_lock_init)
120 
121 void
122 _libnsl_lock_init()
123 {
124 	int	i;
125 
126 	(void) sigfillset(&fillset);
127 
128 	for (i = 0; i <  (sizeof (mutex_table) / sizeof (mutex_table[0])); i++)
129 		(void) mutex_init(mutex_table[i], 0, (void *) 0);
130 
131 	for (i = 0; i < (sizeof (rwlock_table) / sizeof (rwlock_table[0])); i++)
132 		(void) rwlock_init(rwlock_table[i], 0, (void *) 0);
133 
134 	(void) cond_init(&svc_thr_fdwait, USYNC_THREAD, 0);
135 
136 	/*
137 	 * There is no way to unregister these atfork functions,
138 	 * but we don't need to.  The dynamic linker and libc take
139 	 * care of unregistering them if/when the library is unloaded.
140 	 */
141 	(void) pthread_atfork(_libnsl_prefork,
142 		_libnsl_parent_atfork, _libnsl_child_atfork);
143 }
144 
145 #pragma fini(_libnsl_fini)
146 
147 void _key_call_fini(void);
148 
149 void
150 _libnsl_fini()
151 {
152 	_key_call_fini();
153 }
154 
155 #undef	rpc_createerr
156 
157 struct rpc_createerr rpc_createerr;
158 
159 struct rpc_createerr *
160 __rpc_createerr()
161 {
162 	static pthread_key_t rce_key = PTHREAD_ONCE_KEY_NP;
163 	struct rpc_createerr *rce_addr;
164 
165 	if (thr_main())
166 		return (&rpc_createerr);
167 	rce_addr = thr_get_storage(&rce_key, sizeof (*rce_addr), free);
168 	if (rce_addr == NULL) {
169 		syslog(LOG_ERR, "__rpc_createerr : out of memory.");
170 		return (&rpc_createerr);
171 	}
172 	return (rce_addr);
173 }
174 
175 #undef rpc_callerr
176 
177 struct rpc_err rpc_callerr;
178 
179 struct rpc_err *
180 __rpc_callerr(void)
181 {
182 	static pthread_key_t rpc_callerr_key = PTHREAD_ONCE_KEY_NP;
183 	struct rpc_err *tsd;
184 
185 	if (thr_main())
186 		return (&rpc_callerr);
187 	tsd = thr_get_storage(&rpc_callerr_key, sizeof (struct rpc_err), free);
188 	if (tsd == NULL) {
189 		syslog(LOG_ERR, "__rpc_callerr : out of memory.");
190 		return (&rpc_callerr);
191 	}
192 	return (tsd);
193 }
194