Blob Blame History Raw
From e31080c46bfba67d6ed3ac1fa8ded1cb69e4aa97 Mon Sep 17 00:00:00 2001
From: Jan Cholasta <jcholast@redhat.com>
Date: Mon, 10 Nov 2014 17:33:23 +0000
Subject: [PATCH] Unload P11_Helper object's library when it is finalized in
 ipap11helper

https://fedorahosted.org/freeipa/ticket/4713

Reviewed-By: Alexander Bokovoy <abokovoy@redhat.com>
---
 ipapython/ipap11helper/library.c   | 5 +++++
 ipapython/ipap11helper/p11helper.c | 9 +++++++--
 2 files changed, 12 insertions(+), 2 deletions(-)

diff --git a/ipapython/ipap11helper/library.c b/ipapython/ipap11helper/library.c
index 51e24ebc2be5953a6820b768e75a3e8ccb026a64..619604dc6358e41c18c786adc64fe0326dde5f4b 100644
--- a/ipapython/ipap11helper/library.c
+++ b/ipapython/ipap11helper/library.c
@@ -70,6 +70,11 @@ CK_C_GetFunctionList loadLibrary(char* module, void** moduleHandle)
 
 	// Retrieve the entry point for C_GetFunctionList
 	pGetFunctionList = (CK_C_GetFunctionList) dlsym(pDynLib, "C_GetFunctionList");
+	if (pGetFunctionList == NULL)
+	{
+		dlclose(pDynLib);
+		return NULL;
+	}
 
 	// Store the handle so we can dlclose it later
 	*moduleHandle = pDynLib;
diff --git a/ipapython/ipap11helper/p11helper.c b/ipapython/ipap11helper/p11helper.c
index 038c26c4520cc8f71edbee15b0ccd9bf292d7588..558185e8236b79d6c5ee0ca46afb257f5fec94ab 100644
--- a/ipapython/ipap11helper/p11helper.c
+++ b/ipapython/ipap11helper/p11helper.c
@@ -66,6 +66,7 @@ PyObject_HEAD
 CK_SLOT_ID slot;
 CK_FUNCTION_LIST_PTR p11;
 CK_SESSION_HANDLE session;
+void *module_handle;
 } P11_Helper;
 
 typedef enum {
@@ -478,6 +479,7 @@ P11_Helper_new(PyTypeObject *type, PyObject *args, PyObject *kwds) {
         self->slot = 0;
         self->session = 0;
         self->p11 = NULL;
+        self->module_handle = NULL;
     }
 
     return (PyObject *) self;
@@ -496,12 +498,12 @@ static int P11_Helper_init(P11_Helper *self, PyObject *args, PyObject *kwds) {
     CK_C_GetFunctionList pGetFunctionList = loadLibrary(library_path,
             &module_handle);
     if (!pGetFunctionList) {
-        if (module_handle != NULL)
-            unloadLibrary(module_handle);
         PyErr_SetString(ipap11helperError, "Could not load the library.");
         return -1;
     }
 
+    self->module_handle = module_handle;
+
     /*
      * Load the function list
      */
@@ -567,9 +569,12 @@ P11_Helper_finalize(P11_Helper* self) {
      */
     self->p11->C_Finalize(NULL);
 
+    unloadLibrary(self->module_handle);
+
     self->p11 = NULL;
     self->session = 0;
     self->slot = 0;
+    self->module_handle = NULL;
 
     return Py_None;
 }
-- 
2.1.0