xref: /titanic_51/usr/src/lib/libnsl/rpc/mt_misc.c (revision 8eea8e29cc4374d1ee24c25a07f45af132db3499)
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  * Copyright 1986-2003 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	tsd_lock;		/* protects TSD key creation */
72 mutex_t	xprtlist_lock;		/* xprtlist (svc_generic.c) */
73 mutex_t serialize_pkey;		/* serializes calls to public key routines */
74 mutex_t	svc_thr_mutex;		/* protects thread related variables */
75 mutex_t	svc_mutex;		/* protects service handle free lists */
76 mutex_t	svc_exit_mutex;		/* used for clean mt exit */
77 
78 static mutex_t	*mutex_table[] = {
79 	&authdes_lock,
80 	&authnone_lock,
81 	&authsvc_lock,
82 	&clntraw_lock,
83 	&dname_lock,
84 	&dupreq_lock,
85 	&keyserv_lock,
86 	&libnsl_trace_lock,
87 	&loopnconf_lock,
88 	&ops_lock,
89 	&portnum_lock,
90 	&proglst_lock,
91 	&rpcsoc_lock,
92 	&svcraw_lock,
93 	&tsd_lock,
94 	&xprtlist_lock,
95 	&serialize_pkey,
96 	&svc_thr_mutex,
97 	&svc_mutex,
98 	&svc_exit_mutex
99 };
100 
101 cond_t	svc_thr_fdwait;		/* threads wait on this for work */
102 
103 static void
104 _libnsl_prefork()
105 {
106 	mutex_lock(&_ti_userlock);
107 }
108 
109 static void
110 _libnsl_child_atfork()
111 {
112 	mutex_unlock(&_ti_userlock);
113 }
114 
115 static void
116 _libnsl_parent_atfork()
117 {
118 	mutex_unlock(&_ti_userlock);
119 }
120 
121 #pragma init(_libnsl_lock_init)
122 
123 void
124 _libnsl_lock_init()
125 {
126 	int	i;
127 
128 	(void) _sigfillset(&fillset);
129 
130 	for (i = 0; i <  (sizeof (mutex_table) / sizeof (mutex_table[0])); i++)
131 		mutex_init(mutex_table[i], 0, (void *) 0);
132 
133 	for (i = 0; i < (sizeof (rwlock_table) / sizeof (rwlock_table[0])); i++)
134 		rwlock_init(rwlock_table[i], 0, (void *) 0);
135 
136 	cond_init(&svc_thr_fdwait, USYNC_THREAD, 0);
137 
138 	/*
139 	 * There is no way to unregister these atfork functions,
140 	 * but we don't need to.  The dynamic linker and libc take
141 	 * care of unregistering them if/when the library is unloaded.
142 	 */
143 	(void) pthread_atfork(_libnsl_prefork,
144 		_libnsl_parent_atfork, _libnsl_child_atfork);
145 }
146 
147 #pragma fini(_libnsl_fini)
148 
149 void _key_call_fini(void);
150 
151 void
152 _libnsl_fini()
153 {
154 	_key_call_fini();
155 }
156 
157 #undef	rpc_createerr
158 
159 struct rpc_createerr rpc_createerr;
160 
161 struct rpc_createerr *
162 __rpc_createerr()
163 {
164 	static pthread_key_t rce_key = 0;
165 	struct rpc_createerr *rce_addr;
166 
167 	if (thr_main())
168 		return (&rpc_createerr);
169 	rce_addr = thr_get_storage(&rce_key, sizeof (*rce_addr), free);
170 	if (rce_addr == NULL) {
171 		syslog(LOG_ERR, "__rpc_createerr : out of memory.");
172 		return (&rpc_createerr);
173 	}
174 	return (rce_addr);
175 }
176 
177 #undef rpc_callerr
178 
179 struct rpc_err rpc_callerr;
180 
181 struct rpc_err *
182 __rpc_callerr(void)
183 {
184 	static pthread_key_t rpc_callerr_key = 0;
185 	struct rpc_err *tsd = 0;
186 
187 	if (thr_main())
188 		return (&rpc_callerr);
189 	tsd = thr_get_storage(&rpc_callerr_key, sizeof (struct rpc_err), free);
190 	if (tsd == NULL) {
191 		syslog(LOG_ERR, "__rpc_callerr : out of memory.");
192 		return (&rpc_callerr);
193 	}
194 	return (tsd);
195 }
196