Description: Fix coolkey thread issues
Origin: http://pkgs.fedoraproject.org/gitweb/?p=coolkey.git;a=commit;h=4099f2251ffdf1e8be0e96ede91183557ba1aaba
Author: Robert Relyea <rrelyea@fedoraproject.org>
Date:   Fri Dec 18 23:37:45 2009 +0000

 SVN 86 https://fedorahosted.org/coolkey/changeset?reponame=&new=86%40%2F&old=85%40%2F

    Fix coolkey thread issues:
    1) Coolkey returned errors when ever thread safety was requested, even
        though coolkey was thread safe.
    2) Coolkey would hang if used with non-threaded applications which did not
        link with -lpthreads

--- a/src/coolkey/coolkey.cpp
+++ b/src/coolkey/coolkey.cpp
@@ -42,7 +42,9 @@
 
 static SlotList *slotList = NULL;
 
-static OSLock finalizeLock(false);
+static OSLock *finalizeLock = NULL;
+#define FINALIZE_GETLOCK() if (finalizeLock) finalizeLock->getLock();
+#define FINALIZE_RELEASELOCK() if (finalizeLock) finalizeLock->releaseLock();
 
 static CK_BBOOL initialized = FALSE;
 static CK_BBOOL finalizing = FALSE;
@@ -208,11 +210,13 @@
     if( initialized ) {
         return CKR_CRYPTOKI_ALREADY_INITIALIZED;
     }
-    if (!finalizeLock.isValid()) {
+    if (finalizeLock && !finalizeLock->isValid()) {
 	return CKR_CANT_LOCK;
     }
     CK_C_INITIALIZE_ARGS* initArgs = (CK_C_INITIALIZE_ARGS*) pInitArgs;
+    OSLock::setThreadSafe(0);
     if( initArgs != NULL ) {
+	bool needThreads;
 	/* work around a bug in NSS where the library parameters are only
 	 * send if locking is requested */
 	if (initArgs->LibraryParameters) {
@@ -220,7 +224,17 @@
 	} else {
 	    Params::ClearParams();
 	}
-        if( (initArgs->flags & CKF_OS_LOCKING_OK) || initArgs->LockMutex ){
+  	needThreads = ((initArgs->flags & CKF_OS_LOCKING_OK) != 0);
+	OSLock::setThreadSafe(needThreads);
+	/* don't get a finalize lock unless someone initializes us asking
+	 * us to use threads */
+	if (needThreads && !finalizeLock) {
+	    finalizeLock = new OSLock(true);
+	    if (finalizeLock == NULL) return CKR_HOST_MEMORY;
+	}
+	/* only support OS LOCKING threads */
+        if( ((initArgs->flags & CKF_OS_LOCKING_OK) == 0) 
+						&& initArgs->LockMutex ){
             throw PKCS11Exception(CKR_CANT_LOCK);
         }
     }
@@ -259,9 +273,9 @@
     // the finalizing call first, we know it will set waitEvent before
     // we can get the lock, so we only need to protect setting finalizing
     // to true.
-    finalizeLock.getLock();
+    FINALIZE_GETLOCK();
     finalizing = TRUE;
-    finalizeLock.releaseLock();
+    FINALIZE_RELEASELOCK();
     if (waitEvent) {
 	/* we're waiting on a slot event, shutdown first to allow
 	 * the wait function to complete before we pull the rug out.
@@ -273,10 +287,10 @@
     } 
     delete slotList;
     delete log;
-    finalizeLock.getLock();
+    FINALIZE_GETLOCK();
     finalizing = FALSE;
     initialized = FALSE;
-    finalizeLock.releaseLock();
+    FINALIZE_RELEASELOCK();
     return CKR_OK;
 }
 
@@ -595,17 +609,17 @@
 CK_RV
 C_WaitForSlotEvent(CK_FLAGS flags, CK_SLOT_ID_PTR pSlot, CK_VOID_PTR pReserved)
 {
-    finalizeLock.getLock();
+    FINALIZE_GETLOCK();
     if( ! initialized ) {
-        finalizeLock.releaseLock();
+	FINALIZE_RELEASELOCK();
         return CKR_CRYPTOKI_NOT_INITIALIZED;
     }
     if (finalizing) {
-        finalizeLock.releaseLock();
+	FINALIZE_RELEASELOCK();
         return CKR_CRYPTOKI_NOT_INITIALIZED;
     }
     waitEvent = TRUE;
-    finalizeLock.releaseLock();
+    FINALIZE_RELEASELOCK();
     try {
         log->log("C_WaitForSlotEvent called\n");
         slotList->waitForSlotEvent(flags, pSlot, pReserved);
--- a/src/coolkey/machdep.cpp
+++ b/src/coolkey/machdep.cpp
@@ -37,6 +37,8 @@
 #include <stdlib.h>
 #endif
 
+bool OSLock::needThread = 0;
+
 #ifdef _WIN32
 //
 // Windows functions to grab a named shared memory segment of a specific size,
@@ -123,6 +125,10 @@
 
 OSLock::OSLock(bool exceptionAllowed)
 {
+    if (!needThread) {
+	lockData = NULL;
+	return;
+    }
     lockData = new OSLockData;
     if (lockData) {
 	InitializeCriticalSection(&lockData->mutex);
@@ -439,6 +445,9 @@
     int rc;
 
     lockData = NULL;
+    if (!needThread) {
+	return;
+    }
 #ifdef MAC
     if (!OSLock_attr_init) {
 	rc = pthread_mutexattr_init(&OSLock_attr);
--- a/src/coolkey/machdep.h
+++ b/src/coolkey/machdep.h
@@ -40,12 +40,14 @@
 class OSLock {
 private:
    OSLockData *lockData;
+   static bool needThread;
 public:
    OSLock(bool exceptionAllowed = true);
    ~OSLock();
    bool isValid();
    void getLock();
    void releaseLock();
+   static void setThreadSafe(bool thread) { needThread = thread; }
 };
 
 typedef unsigned long OSTime;
