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 #pragma ident "%Z%%M% %I% %E% SMI" 287c478bd9Sstevel@tonic-gate 297c478bd9Sstevel@tonic-gate /* 307c478bd9Sstevel@tonic-gate * Define and initialize MT data for libnsl. 317c478bd9Sstevel@tonic-gate * The _libnsl_lock_init() function below is the library's .init handler. 327c478bd9Sstevel@tonic-gate */ 337c478bd9Sstevel@tonic-gate 347c478bd9Sstevel@tonic-gate #include "mt.h" 357c478bd9Sstevel@tonic-gate #include "rpc_mt.h" 367c478bd9Sstevel@tonic-gate #include <unistd.h> 377c478bd9Sstevel@tonic-gate #include <rpc/rpc.h> 387c478bd9Sstevel@tonic-gate #include <sys/time.h> 397c478bd9Sstevel@tonic-gate #include <stdlib.h> 407c478bd9Sstevel@tonic-gate #include <syslog.h> 417c478bd9Sstevel@tonic-gate 427c478bd9Sstevel@tonic-gate extern mutex_t _ti_userlock; 437c478bd9Sstevel@tonic-gate 447c478bd9Sstevel@tonic-gate sigset_t fillset; /* from sigfillset() */ 457c478bd9Sstevel@tonic-gate 467c478bd9Sstevel@tonic-gate rwlock_t svc_lock; /* protects the services list (svc.c) */ 477c478bd9Sstevel@tonic-gate rwlock_t svc_fd_lock; /* protects svc_fdset and the xports[] array */ 487c478bd9Sstevel@tonic-gate rwlock_t rpcbaddr_cache_lock; /* protects the RPCBIND address cache */ 497c478bd9Sstevel@tonic-gate static rwlock_t *rwlock_table[] = { 507c478bd9Sstevel@tonic-gate &svc_lock, 517c478bd9Sstevel@tonic-gate &svc_fd_lock, 527c478bd9Sstevel@tonic-gate &rpcbaddr_cache_lock 537c478bd9Sstevel@tonic-gate }; 547c478bd9Sstevel@tonic-gate 557c478bd9Sstevel@tonic-gate mutex_t authdes_lock; /* protects authdes cache (svcauth_des.c) */ 567c478bd9Sstevel@tonic-gate mutex_t authnone_lock; /* auth_none.c serialization */ 577c478bd9Sstevel@tonic-gate mutex_t authsvc_lock; /* protects the Auths list (svc_auth.c) */ 587c478bd9Sstevel@tonic-gate mutex_t clntraw_lock; /* clnt_raw.c serialization */ 597c478bd9Sstevel@tonic-gate mutex_t dname_lock; /* domainname and domain_fd (getdname.c) */ 607c478bd9Sstevel@tonic-gate /* and default_domain (rpcdname.c) */ 617c478bd9Sstevel@tonic-gate mutex_t dupreq_lock; /* dupreq variables (svc_dg.c) */ 627c478bd9Sstevel@tonic-gate mutex_t keyserv_lock; /* protects first_time and hostname */ 637c478bd9Sstevel@tonic-gate /* (key_call.c) */ 647c478bd9Sstevel@tonic-gate mutex_t libnsl_trace_lock; /* serializes rpc_trace() (rpc_trace.c) */ 657c478bd9Sstevel@tonic-gate mutex_t loopnconf_lock; /* loopnconf (rpcb_clnt.c) */ 667c478bd9Sstevel@tonic-gate mutex_t ops_lock; /* serializes ops initializations */ 677c478bd9Sstevel@tonic-gate mutex_t portnum_lock; /* protects ``port'' static in bindresvport() */ 687c478bd9Sstevel@tonic-gate mutex_t proglst_lock; /* protects proglst list (svc_simple.c) */ 697c478bd9Sstevel@tonic-gate mutex_t rpcsoc_lock; /* serializes clnt_com_create() (rpc_soc.c) */ 707c478bd9Sstevel@tonic-gate mutex_t svcraw_lock; /* svc_raw.c serialization */ 717c478bd9Sstevel@tonic-gate mutex_t xprtlist_lock; /* xprtlist (svc_generic.c) */ 727c478bd9Sstevel@tonic-gate mutex_t serialize_pkey; /* serializes calls to public key routines */ 737c478bd9Sstevel@tonic-gate mutex_t svc_thr_mutex; /* protects thread related variables */ 747c478bd9Sstevel@tonic-gate mutex_t svc_mutex; /* protects service handle free lists */ 757c478bd9Sstevel@tonic-gate mutex_t svc_exit_mutex; /* used for clean mt exit */ 767c478bd9Sstevel@tonic-gate 777c478bd9Sstevel@tonic-gate static mutex_t *mutex_table[] = { 787c478bd9Sstevel@tonic-gate &authdes_lock, 797c478bd9Sstevel@tonic-gate &authnone_lock, 807c478bd9Sstevel@tonic-gate &authsvc_lock, 817c478bd9Sstevel@tonic-gate &clntraw_lock, 827c478bd9Sstevel@tonic-gate &dname_lock, 837c478bd9Sstevel@tonic-gate &dupreq_lock, 847c478bd9Sstevel@tonic-gate &keyserv_lock, 857c478bd9Sstevel@tonic-gate &libnsl_trace_lock, 867c478bd9Sstevel@tonic-gate &loopnconf_lock, 877c478bd9Sstevel@tonic-gate &ops_lock, 887c478bd9Sstevel@tonic-gate &portnum_lock, 897c478bd9Sstevel@tonic-gate &proglst_lock, 907c478bd9Sstevel@tonic-gate &rpcsoc_lock, 917c478bd9Sstevel@tonic-gate &svcraw_lock, 927c478bd9Sstevel@tonic-gate &xprtlist_lock, 937c478bd9Sstevel@tonic-gate &serialize_pkey, 947c478bd9Sstevel@tonic-gate &svc_thr_mutex, 957c478bd9Sstevel@tonic-gate &svc_mutex, 967c478bd9Sstevel@tonic-gate &svc_exit_mutex 977c478bd9Sstevel@tonic-gate }; 987c478bd9Sstevel@tonic-gate 997c478bd9Sstevel@tonic-gate cond_t svc_thr_fdwait; /* threads wait on this for work */ 1007c478bd9Sstevel@tonic-gate 1017c478bd9Sstevel@tonic-gate static void 1027c478bd9Sstevel@tonic-gate _libnsl_prefork() 1037c478bd9Sstevel@tonic-gate { 10461961e0fSrobinson (void) mutex_lock(&_ti_userlock); 1057c478bd9Sstevel@tonic-gate } 1067c478bd9Sstevel@tonic-gate 1077c478bd9Sstevel@tonic-gate static void 1087c478bd9Sstevel@tonic-gate _libnsl_child_atfork() 1097c478bd9Sstevel@tonic-gate { 11061961e0fSrobinson (void) mutex_unlock(&_ti_userlock); 1117c478bd9Sstevel@tonic-gate } 1127c478bd9Sstevel@tonic-gate 1137c478bd9Sstevel@tonic-gate static void 1147c478bd9Sstevel@tonic-gate _libnsl_parent_atfork() 1157c478bd9Sstevel@tonic-gate { 11661961e0fSrobinson (void) mutex_unlock(&_ti_userlock); 1177c478bd9Sstevel@tonic-gate } 1187c478bd9Sstevel@tonic-gate 1197c478bd9Sstevel@tonic-gate #pragma init(_libnsl_lock_init) 1207c478bd9Sstevel@tonic-gate 1217c478bd9Sstevel@tonic-gate void 1227c478bd9Sstevel@tonic-gate _libnsl_lock_init() 1237c478bd9Sstevel@tonic-gate { 1247c478bd9Sstevel@tonic-gate int i; 1257c478bd9Sstevel@tonic-gate 126e8031f0aSraf (void) sigfillset(&fillset); 1277c478bd9Sstevel@tonic-gate 1287c478bd9Sstevel@tonic-gate for (i = 0; i < (sizeof (mutex_table) / sizeof (mutex_table[0])); i++) 12961961e0fSrobinson (void) mutex_init(mutex_table[i], 0, (void *) 0); 1307c478bd9Sstevel@tonic-gate 1317c478bd9Sstevel@tonic-gate for (i = 0; i < (sizeof (rwlock_table) / sizeof (rwlock_table[0])); i++) 13261961e0fSrobinson (void) rwlock_init(rwlock_table[i], 0, (void *) 0); 1337c478bd9Sstevel@tonic-gate 13461961e0fSrobinson (void) cond_init(&svc_thr_fdwait, USYNC_THREAD, 0); 1357c478bd9Sstevel@tonic-gate 1367c478bd9Sstevel@tonic-gate /* 1377c478bd9Sstevel@tonic-gate * There is no way to unregister these atfork functions, 1387c478bd9Sstevel@tonic-gate * but we don't need to. The dynamic linker and libc take 1397c478bd9Sstevel@tonic-gate * care of unregistering them if/when the library is unloaded. 1407c478bd9Sstevel@tonic-gate */ 1417c478bd9Sstevel@tonic-gate (void) pthread_atfork(_libnsl_prefork, 1427c478bd9Sstevel@tonic-gate _libnsl_parent_atfork, _libnsl_child_atfork); 1437c478bd9Sstevel@tonic-gate } 1447c478bd9Sstevel@tonic-gate 1457c478bd9Sstevel@tonic-gate #pragma fini(_libnsl_fini) 1467c478bd9Sstevel@tonic-gate 1477c478bd9Sstevel@tonic-gate void _key_call_fini(void); 1487c478bd9Sstevel@tonic-gate 1497c478bd9Sstevel@tonic-gate void 1507c478bd9Sstevel@tonic-gate _libnsl_fini() 1517c478bd9Sstevel@tonic-gate { 1527c478bd9Sstevel@tonic-gate _key_call_fini(); 1537c478bd9Sstevel@tonic-gate } 1547c478bd9Sstevel@tonic-gate 1557c478bd9Sstevel@tonic-gate #undef rpc_createerr 1567c478bd9Sstevel@tonic-gate 1577c478bd9Sstevel@tonic-gate struct rpc_createerr rpc_createerr; 1587c478bd9Sstevel@tonic-gate 1597c478bd9Sstevel@tonic-gate struct rpc_createerr * 1607c478bd9Sstevel@tonic-gate __rpc_createerr() 1617c478bd9Sstevel@tonic-gate { 162*cb620785Sraf static pthread_key_t rce_key = PTHREAD_ONCE_KEY_NP; 1637c478bd9Sstevel@tonic-gate struct rpc_createerr *rce_addr; 1647c478bd9Sstevel@tonic-gate 1657c478bd9Sstevel@tonic-gate if (thr_main()) 1667c478bd9Sstevel@tonic-gate return (&rpc_createerr); 1677c478bd9Sstevel@tonic-gate rce_addr = thr_get_storage(&rce_key, sizeof (*rce_addr), free); 1687c478bd9Sstevel@tonic-gate if (rce_addr == NULL) { 1697c478bd9Sstevel@tonic-gate syslog(LOG_ERR, "__rpc_createerr : out of memory."); 1707c478bd9Sstevel@tonic-gate return (&rpc_createerr); 1717c478bd9Sstevel@tonic-gate } 1727c478bd9Sstevel@tonic-gate return (rce_addr); 1737c478bd9Sstevel@tonic-gate } 1747c478bd9Sstevel@tonic-gate 1757c478bd9Sstevel@tonic-gate #undef rpc_callerr 1767c478bd9Sstevel@tonic-gate 1777c478bd9Sstevel@tonic-gate struct rpc_err rpc_callerr; 1787c478bd9Sstevel@tonic-gate 1797c478bd9Sstevel@tonic-gate struct rpc_err * 1807c478bd9Sstevel@tonic-gate __rpc_callerr(void) 1817c478bd9Sstevel@tonic-gate { 182*cb620785Sraf static pthread_key_t rpc_callerr_key = PTHREAD_ONCE_KEY_NP; 183*cb620785Sraf struct rpc_err *tsd; 1847c478bd9Sstevel@tonic-gate 1857c478bd9Sstevel@tonic-gate if (thr_main()) 1867c478bd9Sstevel@tonic-gate return (&rpc_callerr); 1877c478bd9Sstevel@tonic-gate tsd = thr_get_storage(&rpc_callerr_key, sizeof (struct rpc_err), free); 1887c478bd9Sstevel@tonic-gate if (tsd == NULL) { 1897c478bd9Sstevel@tonic-gate syslog(LOG_ERR, "__rpc_callerr : out of memory."); 1907c478bd9Sstevel@tonic-gate return (&rpc_callerr); 1917c478bd9Sstevel@tonic-gate } 1927c478bd9Sstevel@tonic-gate return (tsd); 1937c478bd9Sstevel@tonic-gate } 194