2 * Copyright (c) 2011, JANET(UK)
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
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.
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.
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
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
40 #include "gssapiP_eap.h"
42 /* Clean up thread-local data; called on thread detach */
44 destroyThreadLocalData(struct gss_eap_thread_local_data* tld)
47 gssEapDestroyStatusInfo(tld->status_info);
49 gssEapDestroyKrbContext(tld->context);
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()
59 static DWORD tlsIndex;
61 /* Access thread-local data */
62 struct gss_eap_thread_local_data *
63 gssEapGetThreadLocalData()
65 return TlsGetValue(tlsIndex);
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 */
73 struct gss_eap_thread_local_data *tlsData;
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)
83 /* No break: Initialize the index for first thread.*/
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));
90 TlsSetValue(tlsIndex, tlsData);
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);
100 destroyThreadLocalData(tlsData);
101 TlsSetValue(tlsIndex, NULL);
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);
111 destroyThreadLocalData(tlsData);
112 /* Release the TLS index. */
121 UNREFERENCED_PARAMETER(hDLL);
122 UNREFERENCED_PARAMETER(reserved);
127 /* PTHREAD implementation */
128 static GSSEAP_THREAD_ONCE tldKeyOnce = GSSEAP_ONCE_INITIALIZER;
129 static GSSEAP_THREAD_KEY tldKey;
131 pthreadDestroyThreadLocalData(void* arg)
133 struct gss_eap_thread_local_data* tld = arg;
136 destroyThreadLocalData(tld);
141 createThreadLocalDataKey(void)
143 GSSEAP_KEY_CREATE(&tldKey, pthreadDestroyThreadLocalData);
146 struct gss_eap_thread_local_data *
147 gssEapGetThreadLocalData()
149 struct gss_eap_thread_local_data *tld;
150 GSSEAP_ONCE(&tldKeyOnce, createThreadLocalDataKey);
151 tld = GSSEAP_GETSPECIFIC(tldKey);
154 tld = GSSEAP_CALLOC(1, sizeof(*tld));
155 GSSEAP_SETSPECIFIC(tldKey, tld);