xref: /illumos-gate/usr/src/lib/libnsl/rpc/mt_misc.c (revision 1da57d55)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*cb620785Sraf  * Common Development and Distribution License (the "License").
6*cb620785Sraf  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
2161961e0fSrobinson 
227c478bd9Sstevel@tonic-gate /*
23*cb620785Sraf  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate /*
287c478bd9Sstevel@tonic-gate  *	Define and initialize MT data for libnsl.
297c478bd9Sstevel@tonic-gate  *	The _libnsl_lock_init() function below is the library's .init handler.
307c478bd9Sstevel@tonic-gate  */
317c478bd9Sstevel@tonic-gate 
327c478bd9Sstevel@tonic-gate #include "mt.h"
337c478bd9Sstevel@tonic-gate #include "rpc_mt.h"
347c478bd9Sstevel@tonic-gate #include <unistd.h>
357c478bd9Sstevel@tonic-gate #include <rpc/rpc.h>
367c478bd9Sstevel@tonic-gate #include <sys/time.h>
377c478bd9Sstevel@tonic-gate #include <stdlib.h>
387c478bd9Sstevel@tonic-gate #include <syslog.h>
397c478bd9Sstevel@tonic-gate 
407c478bd9Sstevel@tonic-gate extern	mutex_t	_ti_userlock;
417c478bd9Sstevel@tonic-gate 
427c478bd9Sstevel@tonic-gate sigset_t fillset;		/* from sigfillset() */
437c478bd9Sstevel@tonic-gate 
447c478bd9Sstevel@tonic-gate rwlock_t	svc_lock;	/* protects the services list (svc.c) */
457c478bd9Sstevel@tonic-gate rwlock_t	svc_fd_lock;	/* protects svc_fdset and the xports[] array */
467c478bd9Sstevel@tonic-gate rwlock_t	rpcbaddr_cache_lock; /* protects the RPCBIND address cache */
477c478bd9Sstevel@tonic-gate static rwlock_t	*rwlock_table[] = {
487c478bd9Sstevel@tonic-gate 	&svc_lock,
497c478bd9Sstevel@tonic-gate 	&svc_fd_lock,
507c478bd9Sstevel@tonic-gate 	&rpcbaddr_cache_lock
517c478bd9Sstevel@tonic-gate };
527c478bd9Sstevel@tonic-gate 
537c478bd9Sstevel@tonic-gate mutex_t	authdes_lock;		/* protects authdes cache (svcauth_des.c) */
547c478bd9Sstevel@tonic-gate mutex_t	authnone_lock;		/* auth_none.c serialization */
557c478bd9Sstevel@tonic-gate mutex_t	authsvc_lock;		/* protects the Auths list (svc_auth.c) */
567c478bd9Sstevel@tonic-gate mutex_t	clntraw_lock;		/* clnt_raw.c serialization */
577c478bd9Sstevel@tonic-gate mutex_t	dname_lock;		/* domainname and domain_fd (getdname.c) */
587c478bd9Sstevel@tonic-gate 				/*	and default_domain (rpcdname.c) */
597c478bd9Sstevel@tonic-gate mutex_t	dupreq_lock;		/* dupreq variables (svc_dg.c) */
607c478bd9Sstevel@tonic-gate mutex_t	keyserv_lock;		/* protects first_time and hostname */
617c478bd9Sstevel@tonic-gate 				/*	(key_call.c) */
627c478bd9Sstevel@tonic-gate mutex_t	libnsl_trace_lock;	/* serializes rpc_trace() (rpc_trace.c) */
637c478bd9Sstevel@tonic-gate mutex_t	loopnconf_lock;		/* loopnconf (rpcb_clnt.c) */
647c478bd9Sstevel@tonic-gate mutex_t	ops_lock;		/* serializes ops initializations */
657c478bd9Sstevel@tonic-gate mutex_t	portnum_lock;		/* protects ``port'' static in bindresvport() */
667c478bd9Sstevel@tonic-gate mutex_t	proglst_lock;		/* protects proglst list (svc_simple.c) */
677c478bd9Sstevel@tonic-gate mutex_t	rpcsoc_lock;		/* serializes clnt_com_create() (rpc_soc.c) */
687c478bd9Sstevel@tonic-gate mutex_t	svcraw_lock;		/* svc_raw.c serialization */
697c478bd9Sstevel@tonic-gate mutex_t	xprtlist_lock;		/* xprtlist (svc_generic.c) */
707c478bd9Sstevel@tonic-gate mutex_t serialize_pkey;		/* serializes calls to public key routines */
717c478bd9Sstevel@tonic-gate mutex_t	svc_thr_mutex;		/* protects thread related variables */
727c478bd9Sstevel@tonic-gate mutex_t	svc_mutex;		/* protects service handle free lists */
737c478bd9Sstevel@tonic-gate mutex_t	svc_exit_mutex;		/* used for clean mt exit */
747c478bd9Sstevel@tonic-gate 
757c478bd9Sstevel@tonic-gate static mutex_t	*mutex_table[] = {
767c478bd9Sstevel@tonic-gate 	&authdes_lock,
777c478bd9Sstevel@tonic-gate 	&authnone_lock,
787c478bd9Sstevel@tonic-gate 	&authsvc_lock,
797c478bd9Sstevel@tonic-gate 	&clntraw_lock,
807c478bd9Sstevel@tonic-gate 	&dname_lock,
817c478bd9Sstevel@tonic-gate 	&dupreq_lock,
827c478bd9Sstevel@tonic-gate 	&keyserv_lock,
837c478bd9Sstevel@tonic-gate 	&libnsl_trace_lock,
847c478bd9Sstevel@tonic-gate 	&loopnconf_lock,
857c478bd9Sstevel@tonic-gate 	&ops_lock,
867c478bd9Sstevel@tonic-gate 	&portnum_lock,
877c478bd9Sstevel@tonic-gate 	&proglst_lock,
887c478bd9Sstevel@tonic-gate 	&rpcsoc_lock,
897c478bd9Sstevel@tonic-gate 	&svcraw_lock,
907c478bd9Sstevel@tonic-gate 	&xprtlist_lock,
917c478bd9Sstevel@tonic-gate 	&serialize_pkey,
927c478bd9Sstevel@tonic-gate 	&svc_thr_mutex,
937c478bd9Sstevel@tonic-gate 	&svc_mutex,
947c478bd9Sstevel@tonic-gate 	&svc_exit_mutex
957c478bd9Sstevel@tonic-gate };
967c478bd9Sstevel@tonic-gate 
977c478bd9Sstevel@tonic-gate cond_t	svc_thr_fdwait;		/* threads wait on this for work */
987c478bd9Sstevel@tonic-gate 
997c478bd9Sstevel@tonic-gate static void
_libnsl_prefork()1007c478bd9Sstevel@tonic-gate _libnsl_prefork()
1017c478bd9Sstevel@tonic-gate {
10261961e0fSrobinson 	(void) mutex_lock(&_ti_userlock);
1037c478bd9Sstevel@tonic-gate }
1047c478bd9Sstevel@tonic-gate 
1057c478bd9Sstevel@tonic-gate static void
_libnsl_child_atfork()1067c478bd9Sstevel@tonic-gate _libnsl_child_atfork()
1077c478bd9Sstevel@tonic-gate {
10861961e0fSrobinson 	(void) mutex_unlock(&_ti_userlock);
1097c478bd9Sstevel@tonic-gate }
1107c478bd9Sstevel@tonic-gate 
1117c478bd9Sstevel@tonic-gate static void
_libnsl_parent_atfork()1127c478bd9Sstevel@tonic-gate _libnsl_parent_atfork()
1137c478bd9Sstevel@tonic-gate {
11461961e0fSrobinson 	(void) mutex_unlock(&_ti_userlock);
1157c478bd9Sstevel@tonic-gate }
1167c478bd9Sstevel@tonic-gate 
1177c478bd9Sstevel@tonic-gate #pragma init(_libnsl_lock_init)
1187c478bd9Sstevel@tonic-gate 
1197c478bd9Sstevel@tonic-gate void
_libnsl_lock_init()1207c478bd9Sstevel@tonic-gate _libnsl_lock_init()
1217c478bd9Sstevel@tonic-gate {
1227c478bd9Sstevel@tonic-gate 	int	i;
1237c478bd9Sstevel@tonic-gate 
124e8031f0aSraf 	(void) sigfillset(&fillset);
1257c478bd9Sstevel@tonic-gate 
1267c478bd9Sstevel@tonic-gate 	for (i = 0; i <  (sizeof (mutex_table) / sizeof (mutex_table[0])); i++)
12761961e0fSrobinson 		(void) mutex_init(mutex_table[i], 0, (void *) 0);
1287c478bd9Sstevel@tonic-gate 
1297c478bd9Sstevel@tonic-gate 	for (i = 0; i < (sizeof (rwlock_table) / sizeof (rwlock_table[0])); i++)
13061961e0fSrobinson 		(void) rwlock_init(rwlock_table[i], 0, (void *) 0);
1317c478bd9Sstevel@tonic-gate 
13261961e0fSrobinson 	(void) cond_init(&svc_thr_fdwait, USYNC_THREAD, 0);
1337c478bd9Sstevel@tonic-gate 
1347c478bd9Sstevel@tonic-gate 	/*
1357c478bd9Sstevel@tonic-gate 	 * There is no way to unregister these atfork functions,
1367c478bd9Sstevel@tonic-gate 	 * but we don't need to.  The dynamic linker and libc take
1377c478bd9Sstevel@tonic-gate 	 * care of unregistering them if/when the library is unloaded.
1387c478bd9Sstevel@tonic-gate 	 */
1397c478bd9Sstevel@tonic-gate 	(void) pthread_atfork(_libnsl_prefork,
1407c478bd9Sstevel@tonic-gate 		_libnsl_parent_atfork, _libnsl_child_atfork);
1417c478bd9Sstevel@tonic-gate }
1427c478bd9Sstevel@tonic-gate 
1437c478bd9Sstevel@tonic-gate #pragma fini(_libnsl_fini)
1447c478bd9Sstevel@tonic-gate 
1457c478bd9Sstevel@tonic-gate void _key_call_fini(void);
1467c478bd9Sstevel@tonic-gate 
1477c478bd9Sstevel@tonic-gate void
_libnsl_fini()1487c478bd9Sstevel@tonic-gate _libnsl_fini()
1497c478bd9Sstevel@tonic-gate {
1507c478bd9Sstevel@tonic-gate 	_key_call_fini();
1517c478bd9Sstevel@tonic-gate }
1527c478bd9Sstevel@tonic-gate 
1537c478bd9Sstevel@tonic-gate #undef	rpc_createerr
1547c478bd9Sstevel@tonic-gate 
1557c478bd9Sstevel@tonic-gate struct rpc_createerr rpc_createerr;
1567c478bd9Sstevel@tonic-gate 
1577c478bd9Sstevel@tonic-gate struct rpc_createerr *
__rpc_createerr()1587c478bd9Sstevel@tonic-gate __rpc_createerr()
1597c478bd9Sstevel@tonic-gate {
160*cb620785Sraf 	static pthread_key_t rce_key = PTHREAD_ONCE_KEY_NP;
1617c478bd9Sstevel@tonic-gate 	struct rpc_createerr *rce_addr;
1627c478bd9Sstevel@tonic-gate 
1637c478bd9Sstevel@tonic-gate 	if (thr_main())
1647c478bd9Sstevel@tonic-gate 		return (&rpc_createerr);
1657c478bd9Sstevel@tonic-gate 	rce_addr = thr_get_storage(&rce_key, sizeof (*rce_addr), free);
1667c478bd9Sstevel@tonic-gate 	if (rce_addr == NULL) {
1677c478bd9Sstevel@tonic-gate 		syslog(LOG_ERR, "__rpc_createerr : out of memory.");
1687c478bd9Sstevel@tonic-gate 		return (&rpc_createerr);
1697c478bd9Sstevel@tonic-gate 	}
1707c478bd9Sstevel@tonic-gate 	return (rce_addr);
1717c478bd9Sstevel@tonic-gate }
1727c478bd9Sstevel@tonic-gate 
1737c478bd9Sstevel@tonic-gate #undef rpc_callerr
1747c478bd9Sstevel@tonic-gate 
1757c478bd9Sstevel@tonic-gate struct rpc_err rpc_callerr;
1767c478bd9Sstevel@tonic-gate 
1777c478bd9Sstevel@tonic-gate struct rpc_err *
__rpc_callerr(void)1787c478bd9Sstevel@tonic-gate __rpc_callerr(void)
1797c478bd9Sstevel@tonic-gate {
180*cb620785Sraf 	static pthread_key_t rpc_callerr_key = PTHREAD_ONCE_KEY_NP;
181*cb620785Sraf 	struct rpc_err *tsd;
1827c478bd9Sstevel@tonic-gate 
1837c478bd9Sstevel@tonic-gate 	if (thr_main())
1847c478bd9Sstevel@tonic-gate 		return (&rpc_callerr);
1857c478bd9Sstevel@tonic-gate 	tsd = thr_get_storage(&rpc_callerr_key, sizeof (struct rpc_err), free);
1867c478bd9Sstevel@tonic-gate 	if (tsd == NULL) {
1877c478bd9Sstevel@tonic-gate 		syslog(LOG_ERR, "__rpc_callerr : out of memory.");
1887c478bd9Sstevel@tonic-gate 		return (&rpc_callerr);
1897c478bd9Sstevel@tonic-gate 	}
1907c478bd9Sstevel@tonic-gate 	return (tsd);
1917c478bd9Sstevel@tonic-gate }
192