]> git.ipfire.org Git - thirdparty/strongswan.git/commitdiff
libstrongswan can be initialized more than once
authorMartin Willi <martin@revosec.ch>
Thu, 25 Oct 2012 12:33:09 +0000 (14:33 +0200)
committerMartin Willi <martin@revosec.ch>
Wed, 14 Nov 2012 09:14:31 +0000 (10:14 +0100)
src/libstrongswan/library.c
src/libstrongswan/library.h

index 2a8dda7e1b0ce91f0770154060bc8a060d48c402..30a7774dfacf985cd0b281f1df85f26e99a6b9ef 100644 (file)
@@ -44,12 +44,22 @@ struct private_library_t {
         * Hashtable with registered objects (name => object)
         */
        hashtable_t *objects;
+
+       /**
+        * Integrity check failed?
+        */
+       bool integrity_failed;
+
+       /**
+        * Number of times we have been initialized
+        */
+       refcount_t ref;
 };
 
 /**
  * library instance
  */
-library_t *lib;
+library_t *lib = NULL;
 
 /**
  * Deinitialize library
@@ -59,6 +69,11 @@ void library_deinit()
        private_library_t *this = (private_library_t*)lib;
        bool detailed;
 
+       if (!this || !ref_put(&this->ref))
+       {       /* have more users */
+               return;
+       }
+
        detailed = lib->settings->get_bool(lib->settings,
                                                                "libstrongswan.leak_detective.detailed", TRUE);
 
@@ -142,11 +157,19 @@ bool library_init(char *settings)
        private_library_t *this;
        printf_hook_t *pfh;
 
+       if (lib)
+       {       /* already initialized, increase refcount */
+               this = (private_library_t*)lib;
+               ref_get(&this->ref);
+               return !this->integrity_failed;
+       }
+
        INIT(this,
                .public = {
                        .get = _get,
                        .set = _set,
                },
+               .ref = 1,
        );
        lib = &this->public;
 
@@ -204,14 +227,14 @@ bool library_init(char *settings)
                if (!lib->integrity->check(lib->integrity, "libstrongswan", library_init))
                {
                        DBG1(DBG_LIB, "integrity check of libstrongswan failed");
-                       return FALSE;
+                       this->integrity_failed = TRUE;
                }
 #else /* !INTEGRITY_TEST */
                DBG1(DBG_LIB, "integrity test enabled, but not supported");
-               return FALSE;
+               this->integrity_failed = TRUE;
 #endif /* INTEGRITY_TEST */
        }
 
-       return TRUE;
+       return !this->integrity_failed;
 }
 
index 43c6b33d3338f30293e12351e17270ddb7901d92..6f455a617dcec839eb8373e48566171f2101e990 100644 (file)
@@ -202,6 +202,9 @@ struct library_t {
 /**
  * Initialize library, creates "lib" instance.
  *
+ * library_init() may be called multiple times in a single process, but each
+ * caller should call library_deinit() for each call to library_init().
+ *
  * @param settings             file to read settings from, may be NULL for default
  * @return                             FALSE if integrity check failed
  */