xref: /minix3/lib/libc/rpc/mt_misc.c (revision f14fb602092e015ff630df58e17c2a9cd57d29b3)
1 /*	$NetBSD: mt_misc.c,v 1.9 2012/03/20 17:14:50 matt Exp $	*/
2 
3 /*-
4  * Copyright (c) 2004 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Frank van der Linden.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 /*
33  *	Define and initialize MT data for libnsl.
34  *	The _libnsl_lock_init() function below is the library's .init handler.
35  */
36 
37 /* #pragma ident	"@(#)mt_misc.c	1.24	93/04/29 SMI" */
38 
39 #include <sys/cdefs.h>
40 #if defined(LIBC_SCCS) && !defined(lint)
41 __RCSID("$NetBSD: mt_misc.c,v 1.9 2012/03/20 17:14:50 matt Exp $");
42 #endif
43 
44 #include	"namespace.h"
45 #include	"reentrant.h"
46 #include	<rpc/rpc.h>
47 #include	<sys/time.h>
48 #include	<stdlib.h>
49 #include	<string.h>
50 
51 #ifdef _REENTRANT
52 
53 /* protects the services list (svc.c) */
54 rwlock_t	svc_lock = RWLOCK_INITIALIZER;
55 /* protects svc_fdset and the xports[] array */
56 rwlock_t	svc_fd_lock = RWLOCK_INITIALIZER;
57 /* protects the RPCBIND address cache */
58 rwlock_t	rpcbaddr_cache_lock = RWLOCK_INITIALIZER;
59 
60 /* protects authdes cache (svcauth_des.c) */
61 mutex_t	authdes_lock = MUTEX_INITIALIZER;
62 /* auth_none.c serialization */
63 mutex_t	authnone_lock = MUTEX_INITIALIZER;
64 /* protects the Auths list (svc_auth.c) */
65 mutex_t	authsvc_lock = MUTEX_INITIALIZER;
66 /* protects client-side fd lock array */
67 mutex_t	clnt_fd_lock = MUTEX_INITIALIZER;
68 /* clnt_raw.c serialization */
69 mutex_t	clntraw_lock = MUTEX_INITIALIZER;
70 /* domainname and domain_fd (getdname.c) and default_domain (rpcdname.c) */
71 mutex_t	dname_lock = MUTEX_INITIALIZER;
72 /* dupreq variables (svc_dg.c) */
73 mutex_t	dupreq_lock = MUTEX_INITIALIZER;
74 /* protects first_time and hostname (key_call.c) */
75 mutex_t	keyserv_lock = MUTEX_INITIALIZER;
76 /* serializes rpc_trace() (rpc_trace.c) */
77 mutex_t	libnsl_trace_lock = MUTEX_INITIALIZER;
78 /* loopnconf (rpcb_clnt.c) */
79 mutex_t	loopnconf_lock = MUTEX_INITIALIZER;
80 /* serializes ops initializations */
81 mutex_t	ops_lock = MUTEX_INITIALIZER;
82 /* protects ``port'' static in bindresvport() */
83 mutex_t	portnum_lock = MUTEX_INITIALIZER;
84 /* protects proglst list (svc_simple.c) */
85 mutex_t	proglst_lock = MUTEX_INITIALIZER;
86 /* serializes clnt_com_create() (rpc_soc.c) */
87 mutex_t	rpcsoc_lock = MUTEX_INITIALIZER;
88 /* svc_raw.c serialization */
89 mutex_t	svcraw_lock = MUTEX_INITIALIZER;
90 /* xprtlist (svc_generic.c) */
91 mutex_t	xprtlist_lock = MUTEX_INITIALIZER;
92 /* serializes calls to public key routines */
93 mutex_t serialize_pkey = MUTEX_INITIALIZER;
94 
95 #endif /* _REENTRANT */
96 
97 
98 #undef	rpc_createerr
99 
100 struct rpc_createerr rpc_createerr;
101 
102 #ifdef _REENTRANT
103 static thread_key_t rce_key;
104 static once_t rce_once = ONCE_INITIALIZER;
105 
106 static void
__rpc_createerr_setup(void)107 __rpc_createerr_setup(void)
108 {
109 
110 	thr_keycreate(&rce_key, free);
111 }
112 #endif /* _REENTRANT */
113 
114 struct rpc_createerr*
__rpc_createerr(void)115 __rpc_createerr(void)
116 {
117 #ifdef _REENTRANT
118 	struct rpc_createerr *rce_addr = 0;
119 
120 	if (__isthreaded == 0)
121 		return (&rpc_createerr);
122 	thr_once(&rce_once, __rpc_createerr_setup);
123 	rce_addr = thr_getspecific(rce_key);
124 	if (rce_addr == NULL) {
125 		rce_addr = malloc(sizeof(*rce_addr));
126 		if (rce_addr == NULL)
127 			return &rpc_createerr;
128 		thr_setspecific(rce_key, (void *) rce_addr);
129 		memset(rce_addr, 0, sizeof (struct rpc_createerr));
130 	}
131 
132 	return (rce_addr);
133 #else
134 	return &rpc_createerr;
135 #endif
136 }
137