summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--common/library.c60
-rw-r--r--common/library.h2
-rw-r--r--p11-kit/util.c54
-rw-r--r--trust/module.c55
4 files changed, 122 insertions, 49 deletions
diff --git a/common/library.c b/common/library.c
index de340bb..0bc7e0c 100644
--- a/common/library.c
+++ b/common/library.c
@@ -176,18 +176,12 @@ p11_library_init_impl (void)
pthread_key_create (&thread_local, free);
}
-#ifdef __GNUC__
-__attribute__((constructor))
-#endif
void
p11_library_init (void)
{
p11_library_init_once ();
}
-#ifdef __GNUC__
-__attribute__((destructor))
-#endif
void
p11_library_uninit (void)
{
@@ -233,17 +227,21 @@ p11_library_init (void)
p11_debug ("initializing library");
p11_mutex_init (&p11_library_mutex);
thread_local = TlsAlloc ();
+ if (thread_local == TLS_OUT_OF_INDEXES)
+ p11_debug ("couldn't setup tls");
}
-static void
-free_tls_value (LPVOID data)
+void
+p11_library_thread_cleanup (void)
{
p11_local *local = data;
- if (local == NULL)
- return;
- if (local->last_error)
- LocalFree (local->last_error);
- LocalFree (data);
+ if (thread_local != TLS_OUT_OF_INDEXES) {
+ p11_debug ("thread stopped, freeing tls");
+ local = TlsGetValue (thread_local);
+ if (local->last_error)
+ LocalFree (local->last_error);
+ LocalFree (local);
+ }
}
void
@@ -261,40 +259,4 @@ p11_library_uninit (void)
_p11_mutex_uninit (&p11_library_mutex);
}
-
-BOOL WINAPI
-DllMain (HINSTANCE instance,
- DWORD reason,
- LPVOID reserved)
-{
- LPVOID data;
-
- switch (reason) {
- case DLL_PROCESS_ATTACH:
- p11_library_init ();
- if (thread_local == TLS_OUT_OF_INDEXES) {
- p11_debug ("couldn't setup tls");
- return FALSE;
- }
- break;
-
- case DLL_THREAD_DETACH:
- if (thread_local != TLS_OUT_OF_INDEXES) {
- p11_debug ("thread stopped, freeing tls");
- data = TlsGetValue (thread_local);
- free_tls_value (data);
- }
- break;
-
- case DLL_PROCESS_DETACH:
- p11_library_uninit ();
- break;
-
- default:
- break;
- }
-
- return TRUE;
-}
-
#endif /* OS_WIN32 */
diff --git a/common/library.h b/common/library.h
index 338dc0f..b310cb9 100644
--- a/common/library.h
+++ b/common/library.h
@@ -79,6 +79,8 @@ void p11_library_init_impl (void);
void p11_library_init (void);
+void p11_library_thread_cleanup (void);
+
void p11_library_uninit (void);
#endif /* P11_LIBRARY_H_ */
diff --git a/p11-kit/util.c b/p11-kit/util.c
index 11a9a22..95190c5 100644
--- a/p11-kit/util.c
+++ b/p11-kit/util.c
@@ -231,3 +231,57 @@ _p11_get_progname_unlocked (void)
return NULL;
return p11_my_progname;
}
+
+#ifdef OS_UNIX
+
+void p11_kit_init (void);
+
+void p11_kit_fini (void);
+
+#ifdef __GNUC__
+__attribute__((constructor))
+#endif
+void
+p11_kit_init (void)
+{
+ p11_library_init_once ();
+}
+
+#ifdef __GNUC__
+__attribute__((destructor))
+#endif
+void
+p11_kit_fini (void)
+{
+ p11_library_uninit ();
+}
+
+#endif /* OS_UNIX */
+
+#ifdef OS_WIN32
+
+BOOL WINAPI
+DllMain (HINSTANCE instance,
+ DWORD reason,
+ LPVOID reserved)
+{
+ LPVOID data;
+
+ switch (reason) {
+ case DLL_PROCESS_ATTACH:
+ p11_library_init ();
+ break;
+ case DLL_THREAD_DETACH:
+ p11_library_thread_cleanup ();
+ break;
+ case DLL_PROCESS_DETACH:
+ p11_library_uninit ();
+ break;
+ default:
+ break;
+ }
+
+ return TRUE;
+}
+
+#endif /* OS_WIN32 */
diff --git a/trust/module.c b/trust/module.c
index ab28095..2583b2b 100644
--- a/trust/module.c
+++ b/trust/module.c
@@ -1506,6 +1506,7 @@ __declspec(dllexport)
CK_RV
C_GetFunctionList (CK_FUNCTION_LIST_PTR_PTR list)
{
+ p11_library_init_once ();
return sys_C_GetFunctionList (list);
}
@@ -1515,3 +1516,57 @@ p11_module_next_id (void)
static CK_ULONG unique = 0x10;
return (unique)++;
}
+
+#ifdef OS_UNIX
+
+void p11_trust_module_init (void);
+
+void p11_trust_module_fini (void);
+
+#ifdef __GNUC__
+__attribute__((constructor))
+#endif
+void
+p11_trust_module_init (void)
+{
+ p11_library_init_once ();
+}
+
+#ifdef __GNUC__
+__attribute__((destructor))
+#endif
+void
+p11_trust_module_fini (void)
+{
+ p11_library_uninit ();
+}
+
+#endif /* OS_UNIX */
+
+#ifdef OS_WIN32
+
+BOOL WINAPI
+DllMain (HINSTANCE instance,
+ DWORD reason,
+ LPVOID reserved)
+{
+ LPVOID data;
+
+ switch (reason) {
+ case DLL_PROCESS_ATTACH:
+ p11_library_init ();
+ break;
+ case DLL_THREAD_DETACH:
+ p11_library_thread_cleanup ();
+ break;
+ case DLL_PROCESS_DETACH:
+ p11_library_uninit ();
+ break;
+ default:
+ break;
+ }
+
+ return TRUE;
+}
+
+#endif /* OS_WIN32 */