/src/p11-kit/common/library.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Copyright (c) 2011 Collabora Ltd |
3 | | * Copyright (c) 2012 Stef Walter |
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 | | * * Redistributions of source code must retain the above |
10 | | * copyright notice, this list of conditions and the |
11 | | * following disclaimer. |
12 | | * * Redistributions in binary form must reproduce the |
13 | | * above copyright notice, this list of conditions and |
14 | | * the following disclaimer in the documentation and/or |
15 | | * other materials provided with the distribution. |
16 | | * * The names of contributors to this software may not be |
17 | | * used to endorse or promote products derived from this |
18 | | * software without specific prior written permission. |
19 | | * |
20 | | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
21 | | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
22 | | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS |
23 | | * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE |
24 | | * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, |
25 | | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, |
26 | | * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS |
27 | | * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED |
28 | | * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
29 | | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF |
30 | | * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH |
31 | | * DAMAGE. |
32 | | * |
33 | | * |
34 | | * CONTRIBUTORS |
35 | | * Stef Walter <stef@thewalter.net> |
36 | | */ |
37 | | |
38 | | #include "config.h" |
39 | | |
40 | | #include "compat.h" |
41 | 46 | #define P11_DEBUG_FLAG P11_DEBUG_LIB |
42 | | #include "debug.h" |
43 | | #include "library.h" |
44 | | #include "message.h" |
45 | | |
46 | | #include <assert.h> |
47 | | #ifdef HAVE_LOCALE_H |
48 | | #include <locale.h> |
49 | | #endif |
50 | | #include <stdarg.h> |
51 | | #include <stdlib.h> |
52 | | #include <stdio.h> |
53 | | #include <string.h> |
54 | | |
55 | | #define P11_MESSAGE_MAX 512 |
56 | | |
57 | | typedef struct { |
58 | | char message[P11_MESSAGE_MAX]; |
59 | | } p11_local; |
60 | | |
61 | | static p11_local * _p11_library_get_thread_local (void); |
62 | | |
63 | | #ifdef PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP |
64 | | p11_mutex_t p11_library_mutex = PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP; |
65 | | |
66 | | p11_mutex_t p11_virtual_mutex = PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP; |
67 | | #else |
68 | | p11_mutex_t p11_library_mutex; |
69 | | |
70 | | p11_mutex_t p11_virtual_mutex; |
71 | | #endif |
72 | | |
73 | | #ifdef OS_UNIX |
74 | | #ifndef __GNUC__ |
75 | | pthread_once_t p11_library_once = PTHREAD_ONCE_INIT; |
76 | | #endif |
77 | | #endif |
78 | | |
79 | | unsigned int p11_forkid = 1; |
80 | | |
81 | | #ifdef HAVE_STRERROR_L |
82 | | extern locale_t p11_message_locale; |
83 | | #endif |
84 | | |
85 | | #ifdef __linux__ |
86 | | /* used only under __linux__ in the getprogname() emulation in compat.c. */ |
87 | | char *p11_program_realpath; |
88 | | #endif |
89 | | |
90 | | static char * |
91 | | thread_local_message (void) |
92 | 42 | { |
93 | 42 | p11_local *local; |
94 | 42 | local = _p11_library_get_thread_local (); |
95 | 42 | return local ? local->message : NULL; |
96 | 42 | } |
97 | | |
98 | | static char * |
99 | | dont_store_message (void) |
100 | 0 | { |
101 | 0 | return NULL; |
102 | 0 | } |
103 | | |
104 | | static void |
105 | | uninit_common (void) |
106 | 23 | { |
107 | 23 | p11_debug ("uninitializing library"); |
108 | 23 | } |
109 | | |
110 | | #ifdef OS_UNIX |
111 | | |
112 | | #ifdef P11_TLS_KEYWORD |
113 | | static p11_local * |
114 | | _p11_library_get_thread_local (void) |
115 | 42 | { |
116 | 42 | static P11_TLS_KEYWORD p11_local local; |
117 | 42 | static P11_TLS_KEYWORD bool local_initialized = false; |
118 | | |
119 | 42 | if (!local_initialized) { |
120 | 1 | memset (&local, 0, sizeof (p11_local)); |
121 | 1 | local_initialized = true; |
122 | 1 | } |
123 | | |
124 | 42 | return &local; |
125 | 42 | } |
126 | | #else |
127 | | static pthread_key_t threadlocal = 0; |
128 | | |
129 | | static p11_local * |
130 | | _p11_library_get_thread_local (void) |
131 | | { |
132 | | p11_local *local; |
133 | | |
134 | | p11_library_init_once (); |
135 | | |
136 | | local = pthread_getspecific (threadlocal); |
137 | | if (local == NULL) { |
138 | | local = calloc (1, sizeof (p11_local)); |
139 | | pthread_setspecific (threadlocal, local); |
140 | | } |
141 | | |
142 | | return local; |
143 | | } |
144 | | #endif |
145 | | |
146 | | static void |
147 | | count_forks (void) |
148 | 0 | { |
149 | | /* Thread safe, executed in child, one thread exists */ |
150 | 0 | p11_forkid++; |
151 | 0 | } |
152 | | |
153 | | void |
154 | | p11_library_init_impl (void) |
155 | 23 | { |
156 | 23 | p11_debug_init (); |
157 | 23 | p11_debug ("initializing library"); |
158 | 23 | P11_RECURSIVE_MUTEX_INIT (p11_library_mutex); |
159 | 23 | P11_RECURSIVE_MUTEX_INIT (p11_virtual_mutex); |
160 | | #ifndef P11_TLS_KEYWORD |
161 | | pthread_key_create (&threadlocal, free); |
162 | | #endif |
163 | 23 | p11_message_storage = thread_local_message; |
164 | 23 | #ifdef HAVE_STRERROR_L |
165 | 23 | if (p11_message_locale == (locale_t) 0) |
166 | 23 | p11_message_locale = newlocale (LC_ALL_MASK, "POSIX", (locale_t) 0); |
167 | 23 | #endif |
168 | | |
169 | 23 | pthread_atfork (NULL, NULL, count_forks); |
170 | 23 | } |
171 | | |
172 | | void |
173 | | p11_library_init (void) |
174 | 23 | { |
175 | 23 | p11_library_init_impl (); |
176 | 23 | } |
177 | | |
178 | | void |
179 | | p11_library_uninit (void) |
180 | 23 | { |
181 | 23 | uninit_common (); |
182 | | |
183 | | #ifndef P11_TLS_KEYWORD |
184 | | /* Some cleanup to pacify valgrind */ |
185 | | free (pthread_getspecific (threadlocal)); |
186 | | pthread_setspecific (threadlocal, NULL); |
187 | | #endif |
188 | | |
189 | 23 | #ifdef HAVE_STRERROR_L |
190 | 23 | if (p11_message_locale != (locale_t) 0) |
191 | 23 | freelocale (p11_message_locale); |
192 | 23 | p11_message_locale = 0; |
193 | 23 | #endif |
194 | 23 | p11_message_storage = dont_store_message; |
195 | | #ifndef P11_TLS_KEYWORD |
196 | | pthread_key_delete (threadlocal); |
197 | | #endif |
198 | 23 | p11_mutex_uninit (&p11_virtual_mutex); |
199 | 23 | p11_mutex_uninit (&p11_library_mutex); |
200 | | |
201 | 23 | #ifdef __linux__ |
202 | 23 | free (p11_program_realpath); |
203 | 23 | #endif |
204 | 23 | } |
205 | | |
206 | | #endif /* OS_UNIX */ |
207 | | |
208 | | #ifdef OS_WIN32 |
209 | | |
210 | | static DWORD threadlocal = TLS_OUT_OF_INDEXES; |
211 | | |
212 | | BOOL WINAPI DllMain (HINSTANCE, DWORD, LPVOID); |
213 | | |
214 | | static p11_local * |
215 | | _p11_library_get_thread_local (void) |
216 | | { |
217 | | LPVOID data; |
218 | | |
219 | | if (threadlocal == TLS_OUT_OF_INDEXES) |
220 | | return NULL; |
221 | | |
222 | | data = TlsGetValue (threadlocal); |
223 | | if (data == NULL) { |
224 | | data = LocalAlloc (LPTR, sizeof (p11_local)); |
225 | | TlsSetValue (threadlocal, data); |
226 | | } |
227 | | |
228 | | return (p11_local *)data; |
229 | | } |
230 | | |
231 | | void |
232 | | p11_library_init (void) |
233 | | { |
234 | | p11_debug_init (); |
235 | | p11_debug ("initializing library"); |
236 | | P11_RECURSIVE_MUTEX_INIT (p11_library_mutex); |
237 | | P11_RECURSIVE_MUTEX_INIT (p11_virtual_mutex); |
238 | | threadlocal = TlsAlloc (); |
239 | | if (threadlocal == TLS_OUT_OF_INDEXES) |
240 | | p11_debug ("couldn't setup tls"); |
241 | | else |
242 | | p11_message_storage = thread_local_message; |
243 | | } |
244 | | |
245 | | void |
246 | | p11_library_thread_cleanup (void) |
247 | | { |
248 | | p11_local *local; |
249 | | if (threadlocal != TLS_OUT_OF_INDEXES) { |
250 | | p11_debug ("thread stopped, freeing tls"); |
251 | | local = TlsGetValue (threadlocal); |
252 | | LocalFree (local); |
253 | | } |
254 | | } |
255 | | |
256 | | void |
257 | | p11_library_uninit (void) |
258 | | { |
259 | | LPVOID data; |
260 | | |
261 | | uninit_common (); |
262 | | |
263 | | if (threadlocal != TLS_OUT_OF_INDEXES) { |
264 | | p11_message_storage = dont_store_message; |
265 | | data = TlsGetValue (threadlocal); |
266 | | LocalFree (data); |
267 | | TlsFree (threadlocal); |
268 | | } |
269 | | p11_mutex_uninit (&p11_virtual_mutex); |
270 | | p11_mutex_uninit (&p11_library_mutex); |
271 | | } |
272 | | |
273 | | #endif /* OS_WIN32 */ |