test use = not == for string compare
[mech_eap.git] / mech_eap / util_tld.c
1 /*
2  * Copyright (c) 2011, JANET(UK)
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  *
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * 3. Neither the name of JANET(UK) nor the names of its contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  */
32
33 /* Access all thread-local data through these methods which 
34  * use pthreads to manage thread-local memory on Unix and TlsFoo() on Windows.
35  * This would be more flexible, scalable, and extensible 
36  * if implemented through a callback interface, but given that 
37  * there are currently only two 'clients', hard-coding seems more 
38  * straightforward
39  */
40 #include "gssapiP_eap.h"
41
42 /* Clean up thread-local data; called on thread detach */
43 static void
44 destroyThreadLocalData(struct gss_eap_thread_local_data* tld)
45 {
46     if (tld->status_info)
47         gssEapDestroyStatusInfo(tld->status_info);
48     if (tld->context)
49         gssEapDestroyKrbContext(tld->context);
50     GSSEAP_FREE(tld);
51 }
52
53 #ifdef WIN32
54
55 /* This is the tls index returned by TlsAlloc() on process init.  
56  * Each thread, on thread attach in DllMain(), allocates its thread-local data and uses this index with TlsSetValue() to store it.
57  * It can then subsequently be retrieved with TlsGetValue()
58  */
59 static DWORD tlsIndex;
60
61 /* Access thread-local data */
62 struct gss_eap_thread_local_data *
63 gssEapGetThreadLocalData()
64 {
65     return TlsGetValue(tlsIndex);
66 }
67  
68 /* DllMain() is the entry-point function for this DLL. */
69 BOOL WINAPI DllMain(HINSTANCE hDLL, /* DLL module handle */
70     DWORD reason,                    /* reason called */
71     LPVOID reserved)                 /* reserved */
72
73     struct gss_eap_thread_local_data *tlsData;
74
75     switch (reason) 
76     { 
77         // The DLL is loading due to process 
78         // initialization or a call to LoadLibrary. 
79         case DLL_PROCESS_ATTACH: 
80             /* Allocate a TLS index. */
81             if ((tlsIndex = TlsAlloc()) == TLS_OUT_OF_INDEXES) 
82                 return FALSE; 
83             /* No break: Initialize the index for first thread.*/
84  
85         /* The attached process creates a new thread. */
86         case DLL_THREAD_ATTACH: 
87             /* Initialize the TLS index for this thread. */
88             tlsData = GSSEAP_CALLOC(1, sizeof(*tlsData));
89             if (tlsData != NULL) 
90                 TlsSetValue(tlsIndex, tlsData); 
91  
92             break; 
93  
94         /* The thread of the attached process terminates. */
95         case DLL_THREAD_DETACH: 
96             /* Release the allocated memory for this thread. */
97             tlsData = TlsGetValue(tlsIndex); 
98             if (tlsData != NULL) 
99             {
100                 destroyThreadLocalData(tlsData); 
101                 TlsSetValue(tlsIndex, NULL);
102             }
103  
104             break; 
105  
106         /* DLL unload due to process termination or FreeLibrary. */
107         case DLL_PROCESS_DETACH: 
108             /* Release the allocated memory for this thread. */
109             tlsData = TlsGetValue(tlsIndex); 
110             if (tlsData != NULL) 
111                 destroyThreadLocalData(tlsData); 
112             /* Release the TLS index. */
113             TlsFree(tlsIndex); 
114             break; 
115  
116         default: 
117             break; 
118     } 
119  
120     return TRUE; 
121     UNREFERENCED_PARAMETER(hDLL); 
122     UNREFERENCED_PARAMETER(reserved); 
123 }
124
125 #else /* WIN32 */
126
127 /* PTHREAD implementation */
128 static GSSEAP_THREAD_ONCE tldKeyOnce = GSSEAP_ONCE_INITIALIZER;
129 static GSSEAP_THREAD_KEY tldKey;
130 static void 
131 pthreadDestroyThreadLocalData(void* arg)
132 {
133     struct gss_eap_thread_local_data* tld = arg;
134     if (tld)
135     {
136         destroyThreadLocalData(tld);
137     }
138 }
139
140 static void 
141 createThreadLocalDataKey(void)
142 {
143     GSSEAP_KEY_CREATE(&tldKey, pthreadDestroyThreadLocalData);
144 }
145
146 struct gss_eap_thread_local_data * 
147 gssEapGetThreadLocalData()
148 {
149     struct gss_eap_thread_local_data *tld;
150     GSSEAP_ONCE(&tldKeyOnce, createThreadLocalDataKey);
151     tld = GSSEAP_GETSPECIFIC(tldKey);
152     if (!tld)
153     {
154         tld = GSSEAP_CALLOC(1, sizeof(*tld));
155         GSSEAP_SETSPECIFIC(tldKey, tld);
156     }
157     return tld;
158 }
159
160 #endif /* WIN32 */