1*adb74221Smatt /* $NetBSD: mt_misc.c,v 1.9 2012/03/20 17:14:50 matt Exp $ */
2388c103eSchristos
3388c103eSchristos /*-
4388c103eSchristos * Copyright (c) 2004 The NetBSD Foundation, Inc.
5388c103eSchristos * All rights reserved.
6388c103eSchristos *
7388c103eSchristos * This code is derived from software contributed to The NetBSD Foundation
8388c103eSchristos * by Frank van der Linden.
9388c103eSchristos *
10388c103eSchristos * Redistribution and use in source and binary forms, with or without
11388c103eSchristos * modification, are permitted provided that the following conditions
12388c103eSchristos * are met:
13388c103eSchristos * 1. Redistributions of source code must retain the above copyright
14388c103eSchristos * notice, this list of conditions and the following disclaimer.
15388c103eSchristos * 2. Redistributions in binary form must reproduce the above copyright
16388c103eSchristos * notice, this list of conditions and the following disclaimer in the
17388c103eSchristos * documentation and/or other materials provided with the distribution.
18388c103eSchristos *
19388c103eSchristos * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20388c103eSchristos * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21388c103eSchristos * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22388c103eSchristos * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23388c103eSchristos * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24388c103eSchristos * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25388c103eSchristos * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26388c103eSchristos * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27388c103eSchristos * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28388c103eSchristos * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29388c103eSchristos * POSSIBILITY OF SUCH DAMAGE.
30388c103eSchristos */
317df0ccbaSfvdl
327df0ccbaSfvdl /*
337df0ccbaSfvdl * Define and initialize MT data for libnsl.
347df0ccbaSfvdl * The _libnsl_lock_init() function below is the library's .init handler.
357df0ccbaSfvdl */
367df0ccbaSfvdl
377df0ccbaSfvdl /* #pragma ident "@(#)mt_misc.c 1.24 93/04/29 SMI" */
387df0ccbaSfvdl
395c945215Sitojun #include <sys/cdefs.h>
405c945215Sitojun #if defined(LIBC_SCCS) && !defined(lint)
41*adb74221Smatt __RCSID("$NetBSD: mt_misc.c,v 1.9 2012/03/20 17:14:50 matt Exp $");
425c945215Sitojun #endif
435c945215Sitojun
444e50f4b6Skleink #include "namespace.h"
457df0ccbaSfvdl #include "reentrant.h"
467df0ccbaSfvdl #include <rpc/rpc.h>
477df0ccbaSfvdl #include <sys/time.h>
487df0ccbaSfvdl #include <stdlib.h>
493fdac2b8Sthorpej #include <string.h>
507df0ccbaSfvdl
513fdac2b8Sthorpej #ifdef _REENTRANT
527df0ccbaSfvdl
533fdac2b8Sthorpej /* protects the services list (svc.c) */
543fdac2b8Sthorpej rwlock_t svc_lock = RWLOCK_INITIALIZER;
553fdac2b8Sthorpej /* protects svc_fdset and the xports[] array */
563fdac2b8Sthorpej rwlock_t svc_fd_lock = RWLOCK_INITIALIZER;
573fdac2b8Sthorpej /* protects the RPCBIND address cache */
583fdac2b8Sthorpej rwlock_t rpcbaddr_cache_lock = RWLOCK_INITIALIZER;
597df0ccbaSfvdl
603fdac2b8Sthorpej /* protects authdes cache (svcauth_des.c) */
613fdac2b8Sthorpej mutex_t authdes_lock = MUTEX_INITIALIZER;
623fdac2b8Sthorpej /* auth_none.c serialization */
633fdac2b8Sthorpej mutex_t authnone_lock = MUTEX_INITIALIZER;
643fdac2b8Sthorpej /* protects the Auths list (svc_auth.c) */
653fdac2b8Sthorpej mutex_t authsvc_lock = MUTEX_INITIALIZER;
663fdac2b8Sthorpej /* protects client-side fd lock array */
673fdac2b8Sthorpej mutex_t clnt_fd_lock = MUTEX_INITIALIZER;
683fdac2b8Sthorpej /* clnt_raw.c serialization */
693fdac2b8Sthorpej mutex_t clntraw_lock = MUTEX_INITIALIZER;
703fdac2b8Sthorpej /* domainname and domain_fd (getdname.c) and default_domain (rpcdname.c) */
713fdac2b8Sthorpej mutex_t dname_lock = MUTEX_INITIALIZER;
723fdac2b8Sthorpej /* dupreq variables (svc_dg.c) */
733fdac2b8Sthorpej mutex_t dupreq_lock = MUTEX_INITIALIZER;
743fdac2b8Sthorpej /* protects first_time and hostname (key_call.c) */
753fdac2b8Sthorpej mutex_t keyserv_lock = MUTEX_INITIALIZER;
763fdac2b8Sthorpej /* serializes rpc_trace() (rpc_trace.c) */
773fdac2b8Sthorpej mutex_t libnsl_trace_lock = MUTEX_INITIALIZER;
783fdac2b8Sthorpej /* loopnconf (rpcb_clnt.c) */
793fdac2b8Sthorpej mutex_t loopnconf_lock = MUTEX_INITIALIZER;
803fdac2b8Sthorpej /* serializes ops initializations */
813fdac2b8Sthorpej mutex_t ops_lock = MUTEX_INITIALIZER;
823fdac2b8Sthorpej /* protects ``port'' static in bindresvport() */
833fdac2b8Sthorpej mutex_t portnum_lock = MUTEX_INITIALIZER;
843fdac2b8Sthorpej /* protects proglst list (svc_simple.c) */
853fdac2b8Sthorpej mutex_t proglst_lock = MUTEX_INITIALIZER;
863fdac2b8Sthorpej /* serializes clnt_com_create() (rpc_soc.c) */
873fdac2b8Sthorpej mutex_t rpcsoc_lock = MUTEX_INITIALIZER;
883fdac2b8Sthorpej /* svc_raw.c serialization */
893fdac2b8Sthorpej mutex_t svcraw_lock = MUTEX_INITIALIZER;
903fdac2b8Sthorpej /* xprtlist (svc_generic.c) */
913fdac2b8Sthorpej mutex_t xprtlist_lock = MUTEX_INITIALIZER;
923fdac2b8Sthorpej /* serializes calls to public key routines */
933fdac2b8Sthorpej mutex_t serialize_pkey = MUTEX_INITIALIZER;
947df0ccbaSfvdl
953fdac2b8Sthorpej #endif /* _REENTRANT */
967df0ccbaSfvdl
977df0ccbaSfvdl
987df0ccbaSfvdl #undef rpc_createerr
997df0ccbaSfvdl
1007df0ccbaSfvdl struct rpc_createerr rpc_createerr;
1017df0ccbaSfvdl
1023fdac2b8Sthorpej #ifdef _REENTRANT
1033fdac2b8Sthorpej static thread_key_t rce_key;
1043fdac2b8Sthorpej static once_t rce_once = ONCE_INITIALIZER;
1053fdac2b8Sthorpej
1063fdac2b8Sthorpej static void
__rpc_createerr_setup(void)1073fdac2b8Sthorpej __rpc_createerr_setup(void)
1083fdac2b8Sthorpej {
1093fdac2b8Sthorpej
1103fdac2b8Sthorpej thr_keycreate(&rce_key, free);
1113fdac2b8Sthorpej }
1123fdac2b8Sthorpej #endif /* _REENTRANT */
1133fdac2b8Sthorpej
1147df0ccbaSfvdl struct rpc_createerr*
__rpc_createerr(void)115*adb74221Smatt __rpc_createerr(void)
1167df0ccbaSfvdl {
1173fdac2b8Sthorpej #ifdef _REENTRANT
1187df0ccbaSfvdl struct rpc_createerr *rce_addr = 0;
1197df0ccbaSfvdl
1203fdac2b8Sthorpej if (__isthreaded == 0)
1217df0ccbaSfvdl return (&rpc_createerr);
1223fdac2b8Sthorpej thr_once(&rce_once, __rpc_createerr_setup);
1233fdac2b8Sthorpej rce_addr = thr_getspecific(rce_key);
1243fdac2b8Sthorpej if (rce_addr == NULL) {
125c9cdc302Schristos rce_addr = malloc(sizeof(*rce_addr));
126c9cdc302Schristos if (rce_addr == NULL)
127c9cdc302Schristos return &rpc_createerr;
1283fdac2b8Sthorpej thr_setspecific(rce_key, (void *) rce_addr);
1297df0ccbaSfvdl memset(rce_addr, 0, sizeof (struct rpc_createerr));
1307df0ccbaSfvdl }
1313fdac2b8Sthorpej
1327df0ccbaSfvdl return (rce_addr);
1337df0ccbaSfvdl #else
1347df0ccbaSfvdl return &rpc_createerr;
1357df0ccbaSfvdl #endif
1367df0ccbaSfvdl }
137