Blame SOURCES/coolkey-thread-fix.patch

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