10Sstevel@tonic-gate /*
20Sstevel@tonic-gate * CDDL HEADER START
30Sstevel@tonic-gate *
40Sstevel@tonic-gate * The contents of this file are subject to the terms of the
5*3864Sraf * Common Development and Distribution License (the "License").
6*3864Sraf * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate *
80Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate * See the License for the specific language governing permissions
110Sstevel@tonic-gate * and limitations under the License.
120Sstevel@tonic-gate *
130Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate *
190Sstevel@tonic-gate * CDDL HEADER END
200Sstevel@tonic-gate */
21132Srobinson
220Sstevel@tonic-gate /*
23*3864Sraf * Copyright 2007 Sun Microsystems, Inc. All rights reserved.
240Sstevel@tonic-gate * Use is subject to license terms.
250Sstevel@tonic-gate */
260Sstevel@tonic-gate
270Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI"
280Sstevel@tonic-gate
290Sstevel@tonic-gate /*
300Sstevel@tonic-gate * Define and initialize MT data for libnsl.
310Sstevel@tonic-gate * The _libnsl_lock_init() function below is the library's .init handler.
320Sstevel@tonic-gate */
330Sstevel@tonic-gate
340Sstevel@tonic-gate #include "mt.h"
350Sstevel@tonic-gate #include "rpc_mt.h"
360Sstevel@tonic-gate #include <unistd.h>
370Sstevel@tonic-gate #include <rpc/rpc.h>
380Sstevel@tonic-gate #include <sys/time.h>
390Sstevel@tonic-gate #include <stdlib.h>
400Sstevel@tonic-gate #include <syslog.h>
410Sstevel@tonic-gate
420Sstevel@tonic-gate extern mutex_t _ti_userlock;
430Sstevel@tonic-gate
440Sstevel@tonic-gate sigset_t fillset; /* from sigfillset() */
450Sstevel@tonic-gate
460Sstevel@tonic-gate rwlock_t svc_lock; /* protects the services list (svc.c) */
470Sstevel@tonic-gate rwlock_t svc_fd_lock; /* protects svc_fdset and the xports[] array */
480Sstevel@tonic-gate rwlock_t rpcbaddr_cache_lock; /* protects the RPCBIND address cache */
490Sstevel@tonic-gate static rwlock_t *rwlock_table[] = {
500Sstevel@tonic-gate &svc_lock,
510Sstevel@tonic-gate &svc_fd_lock,
520Sstevel@tonic-gate &rpcbaddr_cache_lock
530Sstevel@tonic-gate };
540Sstevel@tonic-gate
550Sstevel@tonic-gate mutex_t authdes_lock; /* protects authdes cache (svcauth_des.c) */
560Sstevel@tonic-gate mutex_t authnone_lock; /* auth_none.c serialization */
570Sstevel@tonic-gate mutex_t authsvc_lock; /* protects the Auths list (svc_auth.c) */
580Sstevel@tonic-gate mutex_t clntraw_lock; /* clnt_raw.c serialization */
590Sstevel@tonic-gate mutex_t dname_lock; /* domainname and domain_fd (getdname.c) */
600Sstevel@tonic-gate /* and default_domain (rpcdname.c) */
610Sstevel@tonic-gate mutex_t dupreq_lock; /* dupreq variables (svc_dg.c) */
620Sstevel@tonic-gate mutex_t keyserv_lock; /* protects first_time and hostname */
630Sstevel@tonic-gate /* (key_call.c) */
640Sstevel@tonic-gate mutex_t libnsl_trace_lock; /* serializes rpc_trace() (rpc_trace.c) */
650Sstevel@tonic-gate mutex_t loopnconf_lock; /* loopnconf (rpcb_clnt.c) */
660Sstevel@tonic-gate mutex_t ops_lock; /* serializes ops initializations */
670Sstevel@tonic-gate mutex_t portnum_lock; /* protects ``port'' static in bindresvport() */
680Sstevel@tonic-gate mutex_t proglst_lock; /* protects proglst list (svc_simple.c) */
690Sstevel@tonic-gate mutex_t rpcsoc_lock; /* serializes clnt_com_create() (rpc_soc.c) */
700Sstevel@tonic-gate mutex_t svcraw_lock; /* svc_raw.c serialization */
710Sstevel@tonic-gate mutex_t xprtlist_lock; /* xprtlist (svc_generic.c) */
720Sstevel@tonic-gate mutex_t serialize_pkey; /* serializes calls to public key routines */
730Sstevel@tonic-gate mutex_t svc_thr_mutex; /* protects thread related variables */
740Sstevel@tonic-gate mutex_t svc_mutex; /* protects service handle free lists */
750Sstevel@tonic-gate mutex_t svc_exit_mutex; /* used for clean mt exit */
760Sstevel@tonic-gate
770Sstevel@tonic-gate static mutex_t *mutex_table[] = {
780Sstevel@tonic-gate &authdes_lock,
790Sstevel@tonic-gate &authnone_lock,
800Sstevel@tonic-gate &authsvc_lock,
810Sstevel@tonic-gate &clntraw_lock,
820Sstevel@tonic-gate &dname_lock,
830Sstevel@tonic-gate &dupreq_lock,
840Sstevel@tonic-gate &keyserv_lock,
850Sstevel@tonic-gate &libnsl_trace_lock,
860Sstevel@tonic-gate &loopnconf_lock,
870Sstevel@tonic-gate &ops_lock,
880Sstevel@tonic-gate &portnum_lock,
890Sstevel@tonic-gate &proglst_lock,
900Sstevel@tonic-gate &rpcsoc_lock,
910Sstevel@tonic-gate &svcraw_lock,
920Sstevel@tonic-gate &xprtlist_lock,
930Sstevel@tonic-gate &serialize_pkey,
940Sstevel@tonic-gate &svc_thr_mutex,
950Sstevel@tonic-gate &svc_mutex,
960Sstevel@tonic-gate &svc_exit_mutex
970Sstevel@tonic-gate };
980Sstevel@tonic-gate
990Sstevel@tonic-gate cond_t svc_thr_fdwait; /* threads wait on this for work */
1000Sstevel@tonic-gate
1010Sstevel@tonic-gate static void
_libnsl_prefork()1020Sstevel@tonic-gate _libnsl_prefork()
1030Sstevel@tonic-gate {
104132Srobinson (void) mutex_lock(&_ti_userlock);
1050Sstevel@tonic-gate }
1060Sstevel@tonic-gate
1070Sstevel@tonic-gate static void
_libnsl_child_atfork()1080Sstevel@tonic-gate _libnsl_child_atfork()
1090Sstevel@tonic-gate {
110132Srobinson (void) mutex_unlock(&_ti_userlock);
1110Sstevel@tonic-gate }
1120Sstevel@tonic-gate
1130Sstevel@tonic-gate static void
_libnsl_parent_atfork()1140Sstevel@tonic-gate _libnsl_parent_atfork()
1150Sstevel@tonic-gate {
116132Srobinson (void) mutex_unlock(&_ti_userlock);
1170Sstevel@tonic-gate }
1180Sstevel@tonic-gate
1190Sstevel@tonic-gate #pragma init(_libnsl_lock_init)
1200Sstevel@tonic-gate
1210Sstevel@tonic-gate void
_libnsl_lock_init()1220Sstevel@tonic-gate _libnsl_lock_init()
1230Sstevel@tonic-gate {
1240Sstevel@tonic-gate int i;
1250Sstevel@tonic-gate
1261219Sraf (void) sigfillset(&fillset);
1270Sstevel@tonic-gate
1280Sstevel@tonic-gate for (i = 0; i < (sizeof (mutex_table) / sizeof (mutex_table[0])); i++)
129132Srobinson (void) mutex_init(mutex_table[i], 0, (void *) 0);
1300Sstevel@tonic-gate
1310Sstevel@tonic-gate for (i = 0; i < (sizeof (rwlock_table) / sizeof (rwlock_table[0])); i++)
132132Srobinson (void) rwlock_init(rwlock_table[i], 0, (void *) 0);
1330Sstevel@tonic-gate
134132Srobinson (void) cond_init(&svc_thr_fdwait, USYNC_THREAD, 0);
1350Sstevel@tonic-gate
1360Sstevel@tonic-gate /*
1370Sstevel@tonic-gate * There is no way to unregister these atfork functions,
1380Sstevel@tonic-gate * but we don't need to. The dynamic linker and libc take
1390Sstevel@tonic-gate * care of unregistering them if/when the library is unloaded.
1400Sstevel@tonic-gate */
1410Sstevel@tonic-gate (void) pthread_atfork(_libnsl_prefork,
1420Sstevel@tonic-gate _libnsl_parent_atfork, _libnsl_child_atfork);
1430Sstevel@tonic-gate }
1440Sstevel@tonic-gate
1450Sstevel@tonic-gate #pragma fini(_libnsl_fini)
1460Sstevel@tonic-gate
1470Sstevel@tonic-gate void _key_call_fini(void);
1480Sstevel@tonic-gate
1490Sstevel@tonic-gate void
_libnsl_fini()1500Sstevel@tonic-gate _libnsl_fini()
1510Sstevel@tonic-gate {
1520Sstevel@tonic-gate _key_call_fini();
1530Sstevel@tonic-gate }
1540Sstevel@tonic-gate
1550Sstevel@tonic-gate #undef rpc_createerr
1560Sstevel@tonic-gate
1570Sstevel@tonic-gate struct rpc_createerr rpc_createerr;
1580Sstevel@tonic-gate
1590Sstevel@tonic-gate struct rpc_createerr *
__rpc_createerr()1600Sstevel@tonic-gate __rpc_createerr()
1610Sstevel@tonic-gate {
162*3864Sraf static pthread_key_t rce_key = PTHREAD_ONCE_KEY_NP;
1630Sstevel@tonic-gate struct rpc_createerr *rce_addr;
1640Sstevel@tonic-gate
1650Sstevel@tonic-gate if (thr_main())
1660Sstevel@tonic-gate return (&rpc_createerr);
1670Sstevel@tonic-gate rce_addr = thr_get_storage(&rce_key, sizeof (*rce_addr), free);
1680Sstevel@tonic-gate if (rce_addr == NULL) {
1690Sstevel@tonic-gate syslog(LOG_ERR, "__rpc_createerr : out of memory.");
1700Sstevel@tonic-gate return (&rpc_createerr);
1710Sstevel@tonic-gate }
1720Sstevel@tonic-gate return (rce_addr);
1730Sstevel@tonic-gate }
1740Sstevel@tonic-gate
1750Sstevel@tonic-gate #undef rpc_callerr
1760Sstevel@tonic-gate
1770Sstevel@tonic-gate struct rpc_err rpc_callerr;
1780Sstevel@tonic-gate
1790Sstevel@tonic-gate struct rpc_err *
__rpc_callerr(void)1800Sstevel@tonic-gate __rpc_callerr(void)
1810Sstevel@tonic-gate {
182*3864Sraf static pthread_key_t rpc_callerr_key = PTHREAD_ONCE_KEY_NP;
183*3864Sraf struct rpc_err *tsd;
1840Sstevel@tonic-gate
1850Sstevel@tonic-gate if (thr_main())
1860Sstevel@tonic-gate return (&rpc_callerr);
1870Sstevel@tonic-gate tsd = thr_get_storage(&rpc_callerr_key, sizeof (struct rpc_err), free);
1880Sstevel@tonic-gate if (tsd == NULL) {
1890Sstevel@tonic-gate syslog(LOG_ERR, "__rpc_callerr : out of memory.");
1900Sstevel@tonic-gate return (&rpc_callerr);
1910Sstevel@tonic-gate }
1920Sstevel@tonic-gate return (tsd);
1930Sstevel@tonic-gate }
194