Merge up from branch.
[shibboleth/cpp-sp.git] / oncrpc / rpc_comm.c
1 /*********************************************************************
2  * RPC for the Windows NT Operating System
3  * 1993 by Martin F. Gergeleit
4  * Users may use, copy or modify Sun RPC for the Windows NT Operating 
5  * System according to the Sun copyright below.
6  *
7  * RPC for the Windows NT Operating System COMES WITH ABSOLUTELY NO 
8  * WARRANTY, NOR WILL I BE LIABLE FOR ANY DAMAGES INCURRED FROM THE 
9  * USE OF. USE ENTIRELY AT YOUR OWN RISK!!!
10  *********************************************************************/
11
12 /* @(#)rpc_commondata.c 2.1 88/07/29 4.0 RPCSRC */
13 /*
14  * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
15  * unrestricted use provided that this legend is included on all tape
16  * media and as a part of the software program in whole or part.  Users
17  * may copy or modify Sun RPC without charge, but are not authorized
18  * to license or distribute it to anyone else except as part of a product or
19  * program developed by the user.
20  * 
21  * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
22  * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
23  * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
24  * 
25  * Sun RPC is provided with no support and without any obligation on the
26  * part of Sun Microsystems, Inc. to assist in its use, correction,
27  * modification or enhancement.
28  * 
29  * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
30  * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
31  * OR ANY PART THEREOF.
32  * 
33  * In no event will Sun Microsystems, Inc. be liable for any lost revenue
34  * or profits or other special, indirect and consequential damages, even if
35  * Sun has been advised of the possibility of such damages.
36  * 
37  * Sun Microsystems, Inc.
38  * 2550 Garcia Avenue
39  * Mountain View, California  94043
40  */
41 #include <rpc/rpc.h>
42 #include <stdio.h>
43 /*
44  * This file should only contain common data (global data) that is exported
45  * by public interfaces 
46  */
47
48 /* modified by Scott Cantor to make global data per-thread */
49
50 #ifndef WIN32
51 #include <pthread.h>
52 pthread_once_t __thr_onc_control = PTHREAD_ONCE_INIT;   /* insures single execution */
53 pthread_key_t __thr_key;                                /* pthread key */
54 void _thr_onc_init();                                   /* creates pthread key */
55 void _thr_onc_term(void*);                              /* key destructor function */
56 #endif
57
58 /* these are only used in an out of memory situation... */
59 static fd_set __g_svc_fdset;
60 static struct opaque_auth __g_null_auth;
61 static struct rpc_createerr_t __g_rpc_createerr_t;
62
63 /* per-thread global variables encapsulated in one block, makes TLS mgmt easier */
64 struct __thr_rpc_vars {
65     fd_set _svc_fdset;
66     struct opaque_auth __null_auth;
67     struct rpc_createerr_t _rpc_createerr_t;
68 };
69
70 #ifdef WIN32
71
72 DWORD __thr_key;
73
74 struct __thr_rpc_vars* _get_thr_rpc_vars()
75 {
76     struct __thr_rpc_vars* ptr = TlsGetValue(__thr_key);
77
78     if (!ptr && (ptr=malloc(sizeof(struct __thr_rpc_vars)))) {
79         memset(ptr,0,sizeof(struct __thr_rpc_vars));
80         TlsSetValue(__thr_key, ptr);
81     }
82     else if (!ptr) {
83         nt_rpc_report("out of memory");
84     }
85     return ptr;
86 }
87
88 #else
89
90 struct __thr_rpc_vars* _get_thr_rpc_vars()
91 {
92     struct __thr_rpc_vars* ptr = NULL;
93
94     pthread_once(&__thr_onc_control, _thr_onc_init);
95     ptr = pthread_getspecific(__thr_key);
96     if (!ptr && (ptr=malloc(sizeof(struct __thr_rpc_vars)))) {
97         memset(ptr,0,sizeof(struct __thr_rpc_vars));
98         pthread_setspecific(__thr_key, ptr);
99     }
100     else if (!ptr) {
101         fprintf(stderr,"_get_thr_rpc_vars: out of memory");
102     }
103     return ptr;
104 }
105
106 void _thr_onc_init()
107 {
108     pthread_key_create(&__thr_key, _thr_onc_term);
109 }
110
111 void _thr_onc_term(void* ptr)
112 {
113     if (ptr)
114         free(ptr);
115 }
116
117 #endif
118
119 #if defined(WIN32) && defined(__BORLANDC__)
120 #define ONC_EXPORT __declspec(dllexport)
121 #else
122 #define ONC_EXPORT
123 #endif
124
125 ONC_EXPORT struct opaque_auth* _thr_null_auth(void)
126 {
127     struct __thr_rpc_vars* ptr = _get_thr_rpc_vars();
128     return ptr ? &(ptr->__null_auth) : &__g_null_auth;
129 }
130
131 ONC_EXPORT struct rpc_createerr_t* _thr_rpc_createerr(void)
132 {
133     struct __thr_rpc_vars* ptr = _get_thr_rpc_vars();
134     return ptr ? &(ptr->_rpc_createerr_t) : &__g_rpc_createerr_t;
135 }
136
137 #ifdef FD_SETSIZE
138
139 ONC_EXPORT fd_set* _thr_svc_fdset(void)
140 {
141     struct __thr_rpc_vars* ptr = _get_thr_rpc_vars();
142     return ptr ? &(ptr->_svc_fdset) : &__g_svc_fdset;
143 }
144
145 #else
146
147 int svc_fds;
148
149 #endif /* def FD_SETSIZE */
150