[opensc-commits] svn pam_pkcs11 changed [269] C_Initalize should be called after each fork

webmaster at opensc-project.org webmaster at opensc-project.org
Tue May 15 07:18:25 UTC 2007


Revision: 269
Author:   ludovic.rousseau
Date:     2007-05-15 07:18:24 +0000 (Tue, 15 May 2007)

Log Message:
-----------
C_Initalize should be called after each fork

The pkcs11_eventmgr first C_Initialize then becomes a daemon... The
PKCS#11 standard states that after fork the user must call C_Initialize
again, in order to allow proper library initialization at new process.

Also, don't quit on major errors... try to reinitialize. 

Closes ticket #14. Thanks to Alon Bar-Lev for the patch

Modified Paths:
--------------
    trunk/src/common/pkcs11_lib.c
    trunk/src/tools/pkcs11_eventmgr.c
Modified: trunk/src/common/pkcs11_lib.c
===================================================================
--- trunk/src/common/pkcs11_lib.c	2007-05-09 08:48:00 UTC (rev 268)
+++ trunk/src/common/pkcs11_lib.c	2007-05-15 07:18:24 UTC (rev 269)
@@ -617,6 +617,7 @@
 struct pkcs11_handle_str {
   void *module_handle;
   CK_FUNCTION_LIST_PTR fl;
+  int should_finalize;
   slot_t *slots;
   CK_ULONG slot_count;
   CK_SESSION_HANDLE session;
@@ -714,7 +715,9 @@
   /* initialise the module */
   if (flag) rv = h->fl->C_Initialize((CK_VOID_PTR) &initArgs);
   else      rv = h->fl->C_Initialize(NULL);
-  if (rv != CKR_OK) {
+  if (rv == CKR_OK)
+    h->should_finalize = 1;
+  else if (rv != CKR_CRYPTOKI_ALREADY_INITIALIZED) {
     set_error("C_Initialize() failed: %x", rv);
     return -1;
   }
@@ -803,7 +806,8 @@
 {
   /* finalise pkcs #11 module */
   if (h->fl != NULL)
-    h->fl->C_Finalize(NULL);
+    if (h->should_finalize)
+      h->fl->C_Finalize(NULL);
   /* unload the module */
   if (h->module_handle != NULL)
     dlclose(h->module_handle);

Modified: trunk/src/tools/pkcs11_eventmgr.c
===================================================================
--- trunk/src/tools/pkcs11_eventmgr.c	2007-05-09 08:48:00 UTC (rev 268)
+++ trunk/src/tools/pkcs11_eventmgr.c	2007-05-15 07:18:24 UTC (rev 269)
@@ -73,6 +73,7 @@
 struct pkcs11_handle_str {
   void *module_handle;
   CK_FUNCTION_LIST_PTR fl;
+  int should_finalize;
   slot_t *slots;
   CK_ULONG slot_count;
   CK_SESSION_HANDLE session;
@@ -509,15 +510,6 @@
         return 1;
     }
 
-    /* open pkcs11 sesion */
-    DBG("initialising pkcs #11 module...");
-    rv = ph->fl->C_Initialize(NULL);
-    if (rv != 0) {
-        release_pkcs11_module(ph);
-        DBG1("C_Initialize() failed: %d", rv);
-        return 1;
-    }
-
     /* put my self into background if flag is set */
     if (daemonize) {
 	DBG("Going to be daemon...");
@@ -529,6 +521,18 @@
 	}
     }
 
+    /* open pkcs11 sesion */
+    DBG("initialising pkcs #11 module...");
+    rv = ph->fl->C_Initialize(NULL);
+    if (rv != 0) {
+        release_pkcs11_module(ph);
+	if (ctx)
+	  scconf_free(ctx);
+        DBG1("C_Initialize() failed: %d", rv);
+        return 1;
+    }
+    ph->should_finalize = 1;
+
     /* 
      * Wait endlessly for all events in the list of readers
      * We only stop in case of an error
@@ -550,6 +554,8 @@
 	   new_state = get_a_token();
 	   if (new_state == CARD_ERROR) {
     		DBG("Error trying to get a token");
+		rv = ph->fl->C_Finalize(NULL);
+		rv = ph->fl->C_Initialize(NULL);
     		break;
 	   }
 	   if (old_state == new_state ) { /* state unchanged */





More information about the opensc-commits mailing list