libtnc can be initialized more than once
authorMartin Willi <martin@revosec.ch>
Thu, 25 Oct 2012 13:23:49 +0000 (15:23 +0200)
committerMartin Willi <martin@revosec.ch>
Wed, 14 Nov 2012 09:14:40 +0000 (10:14 +0100)
src/libtnccs/tnc/tnc.c

index feded3b..728d2b2 100644 (file)
@@ -40,6 +40,11 @@ struct private_tnc_t {
         * Public members of tnc_t.
         */
        tnc_t public;
+
+       /**
+        * Number of times we have been initialized
+        */
+       refcount_t ref;
 };
 
 /**
@@ -54,9 +59,17 @@ void libtnccs_init(void)
 {
        private_tnc_t *this;
 
+       if (tnc)
+       {       /* already initialized, increase refcount */
+               this = (private_tnc_t*)tnc;
+               ref_get(&this->ref);
+               return;
+       }
+
        INIT(this,
                .public = {
                },
+               .ref = 1,
        );
 
        tnc = &this->public;
@@ -69,6 +82,11 @@ void libtnccs_deinit(void)
 {
        private_tnc_t *this = (private_tnc_t*)tnc;
 
+       if (!this || !ref_put(&this->ref))
+       {       /* have more users */
+               return;
+       }
+
        free(this);
        tnc = NULL;
 }