call eap_mech constructors from DllMain
[moonshot.git] / moonshot / 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 /*
34  * Thread local data abstraction.
35  */
36
37 #include "gssapiP_eap.h"
38
39 /* Clean up thread-local data; called on thread detach */
40 static void
41 destroyThreadLocalData(struct gss_eap_thread_local_data *tld)
42 {
43     if (tld->statusInfo != NULL)
44         gssEapDestroyStatusInfo(tld->statusInfo);
45     if (tld->krbContext != NULL)
46         gssEapDestroyKrbContext(tld->krbContext);
47     GSSEAP_FREE(tld);
48 }
49
50 #ifdef WIN32
51
52 /*
53  * This is the tls index returned by TlsAlloc() on process init.
54  * Each thread, on thread attach in DllMain(), allocates its thread-local
55  * data and uses this index with TlsSetValue() to store it.
56  * It can then subsequently be retrieved with TlsGetValue().
57  */
58 static DWORD tlsIndex;
59
60 /* Access thread-local data */
61 struct gss_eap_thread_local_data *
62 gssEapGetThreadLocalData(void)
63 {
64     return TlsGetValue(tlsIndex);
65 }
66
67 BOOL WINAPI
68 DllMain(HINSTANCE hDLL,     /* DLL module handle */
69         DWORD reason,       /* reason called */
70         LPVOID reserved)    /* reserved */
71 {
72     struct gss_eap_thread_local_data *tlsData;
73
74     switch (reason) {
75         case DLL_PROCESS_ATTACH:
76             /* Allocate a TLS index. */
77             gssEapInitiatorInit();
78             tlsIndex = TlsAlloc();
79             if (tlsIndex == TLS_OUT_OF_INDEXES)
80                 return FALSE;
81             /* No break: Initialize the index for first thread.*/
82         case DLL_THREAD_ATTACH:
83             /* Initialize the TLS index for this thread. */
84             tlsData = GSSEAP_CALLOC(1, sizeof(*tlsData));
85             if (tlsData != NULL)
86                 TlsSetValue(tlsIndex, tlsData);
87             break;
88         case DLL_THREAD_DETACH:
89             /* Release the allocated memory for this thread. */
90             tlsData = TlsGetValue(tlsIndex);
91             if (tlsData != NULL) {
92                 destroyThreadLocalData(tlsData);
93                 TlsSetValue(tlsIndex, NULL);
94             }
95             break;
96         case DLL_PROCESS_DETACH:
97             /* Release the allocated memory for this thread. */
98             tlsData = TlsGetValue(tlsIndex);
99             if (tlsData != NULL)
100                 destroyThreadLocalData(tlsData);
101             /* Release the TLS index. */
102             TlsFree(tlsIndex);
103             gssEapFinalize();
104             break;
105         default:
106             break;
107     }
108
109     return TRUE;
110     UNREFERENCED_PARAMETER(hDLL);
111     UNREFERENCED_PARAMETER(reserved);
112 }
113
114 #else /* WIN32 */
115
116 /* pthreads implementation */
117
118 static GSSEAP_THREAD_ONCE tldKeyOnce = GSSEAP_ONCE_INITIALIZER;
119 static GSSEAP_THREAD_KEY tldKey;
120
121 static void
122 pthreadDestroyThreadLocalData(void *arg)
123 {
124     struct gss_eap_thread_local_data* tld = arg;
125
126     if (tld != NULL)
127         destroyThreadLocalData(tld);
128 }
129
130 static void
131 createThreadLocalDataKey(void)
132 {
133     GSSEAP_KEY_CREATE(&tldKey, pthreadDestroyThreadLocalData);
134 }
135
136 struct gss_eap_thread_local_data *
137 gssEapGetThreadLocalData()
138 {
139     struct gss_eap_thread_local_data *tld;
140
141     GSSEAP_ONCE(&tldKeyOnce, createThreadLocalDataKey);
142
143     tld = GSSEAP_GETSPECIFIC(tldKey);
144     if (tld == NULL) {
145         tld = GSSEAP_CALLOC(1, sizeof(*tld));
146         if (tld == NULL)
147             return NULL;
148
149         GSSEAP_SETSPECIFIC(tldKey, tld);
150     }
151
152     return tld;
153 }
154 #endif /* WIN32 */