Explorar el Código

security: add BSecurity_GlobalFree

ambrop7 hace 15 años
padre
commit
87b07fcebd
Se han modificado 3 ficheros con 37 adiciones y 1 borrados
  1. 1 0
      client/client.c
  2. 29 0
      security/BSecurity.c
  3. 7 1
      security/BSecurity.h

+ 1 - 0
client/client.c

@@ -622,6 +622,7 @@ fail3:
         ASSERT_FORCE(PR_Cleanup() == PR_SUCCESS)
         PL_ArenaFinish();
     }
+    BSecurity_GlobalFree();
 fail2b:
     BThreadWorkDispatcher_Free(&twd);
 fail2a:

+ 29 - 0
security/BSecurity.c

@@ -20,6 +20,8 @@
  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  */
 
+#include <stddef.h>
+
 #ifdef BADVPN_THREADWORK_USE_PTHREAD
     #include <pthread.h>
 #endif
@@ -98,6 +100,7 @@ int BSecurity_GlobalInit (int use_threads)
     bsecurity_use_threads = use_threads;
     
     #ifdef BADVPN_THREADWORK_USE_PTHREAD
+    // set callbacks
     if (use_threads) {
         CRYPTO_set_id_callback(id_callback);
         CRYPTO_set_locking_callback(locking_callback);
@@ -118,6 +121,32 @@ fail0:
     #endif
 }
 
+void BSecurity_GlobalFree (void)
+{
+    ASSERT(bsecurity_initialized)
+    
+    #ifdef BADVPN_THREADWORK_USE_PTHREAD
+    
+    if (bsecurity_use_threads) {
+        // remove callbacks
+        CRYPTO_set_locking_callback(NULL);
+        CRYPTO_set_id_callback(NULL);
+        
+        // free locks
+        while (bsecurity_num_locks > 0) {
+            ASSERT_FORCE(pthread_mutex_destroy(&bsecurity_locks[bsecurity_num_locks - 1]) == 0)
+            bsecurity_num_locks--;
+        }
+        
+        // free locks array
+        BFree(bsecurity_locks);
+    }
+    
+    #endif
+    
+    bsecurity_initialized = 0;
+}
+
 void BSecurity_GlobalAssert (int need_threads)
 {
     ASSERT(need_threads == 0 || need_threads == 1)

+ 7 - 1
security/BSecurity.h

@@ -29,7 +29,7 @@
 
 /**
  * Initializes security functions.
- * May only be called once.
+ * Security must not be initialized.
  * 
  * @param use_threads whether the application may call security functions
  *                    from different threads. Must be 0 or 1.
@@ -37,6 +37,12 @@
  */
 int BSecurity_GlobalInit (int use_threads);
 
+/**
+ * Deinitializes security functions.
+ * Security must be initialized.
+ */
+void BSecurity_GlobalFree (void);
+
 /**
  * Asserts that {@link BSecurity_GlobalInit} was done, and that it was
  * done with use_threads=1 if need_threads=1.