]> granicus.if.org Git - p11-kit/commitdiff
Only do shared object and DLL initialization in libraries
authorStef Walter <stefw@gnome.org>
Wed, 6 Feb 2013 21:16:42 +0000 (22:16 +0100)
committerStef Walter <stefw@gnome.org>
Wed, 20 Feb 2013 09:17:54 +0000 (10:17 +0100)
Don't do library initialization on shared object load when not running
in a library. We'll want to plug into this and do different things
per library in the future.

common/library.c
common/library.h
p11-kit/util.c
trust/module.c

index de340bbe094db5c25efb01b4f276761e87226635..0bc7e0c80f29b4a56977ba0a287cb0c38316677e 100644 (file)
@@ -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 */
index 338dc0f08c1cd811bd4ededff880119a229fe676..b310cb9acc1c398e4c45185e959ed00de337c9a7 100644 (file)
@@ -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_ */
index 11a9a2270b1bd8232f5eb3cf4d4d1f7e1024b740..95190c5af1a0d404e6ee62efb57c81fb95b00fb1 100644 (file)
@@ -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 */
index ab28095bbc5df8a8a1bda8d4000bf7cda18bb74d..2583b2b9989c88fe08692265d55592f936674568 100644 (file)
@@ -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 */