diff options
author | rrelyea <rrelyea@fba4d07e-fe0f-4d7f-8147-e0026e666dc0> | 2010-08-04 16:21:13 +0000 |
---|---|---|
committer | rrelyea <rrelyea@fba4d07e-fe0f-4d7f-8147-e0026e666dc0> | 2010-08-04 16:21:13 +0000 |
commit | b855b24f093deb6fb52a004c4daebb9523f04658 (patch) | |
tree | d288e0063f44cf549870c906306791927055f14c /src | |
parent | 891d15dcd5e131908763545d48dc4ab4c1d6a7dc (diff) |
Only get locks if the application requests it.
git-svn-id: http://svn.fedorahosted.org/svn/coolkey/trunk@86 fba4d07e-fe0f-4d7f-8147-e0026e666dc0
Diffstat (limited to 'src')
-rw-r--r-- | src/coolkey/coolkey.cpp | 36 | ||||
-rw-r--r-- | src/coolkey/machdep.cpp | 9 | ||||
-rw-r--r-- | src/coolkey/machdep.h | 2 |
3 files changed, 36 insertions, 11 deletions
diff --git a/src/coolkey/coolkey.cpp b/src/coolkey/coolkey.cpp index 458c264..ab8f263 100644 --- a/src/coolkey/coolkey.cpp +++ b/src/coolkey/coolkey.cpp @@ -42,7 +42,9 @@ static Log *log = NULL; 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 @@ C_Initialize(CK_VOID_PTR pInitArgs) 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 @@ C_Initialize(CK_VOID_PTR pInitArgs) } 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 @@ C_Finalize(CK_VOID_PTR pReserved) // 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 @@ C_Finalize(CK_VOID_PTR pReserved) } delete slotList; delete log; - finalizeLock.getLock(); + FINALIZE_GETLOCK(); finalizing = FALSE; initialized = FALSE; - finalizeLock.releaseLock(); + FINALIZE_RELEASELOCK(); return CKR_OK; } @@ -595,17 +609,17 @@ C_GetAttributeValue(CK_SESSION_HANDLE hSession, CK_OBJECT_HANDLE hObject, 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); diff --git a/src/coolkey/machdep.cpp b/src/coolkey/machdep.cpp index 49d0c57..31fa954 100644 --- 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 @@ struct OSLockData { OSLock::OSLock(bool exceptionAllowed) { + if (!needThread) { + lockData = NULL; + return; + } lockData = new OSLockData; if (lockData) { InitializeCriticalSection(&lockData->mutex); @@ -360,6 +366,9 @@ OSLock::OSLock(bool exceptionAllowed) int rc; lockData = NULL; + if (!needThread) { + return; + } #ifdef MAC if (!OSLock_attr_init) { rc = pthread_mutexattr_init(&OSLock_attr); diff --git a/src/coolkey/machdep.h b/src/coolkey/machdep.h index 0f702ad..3d68191 100644 --- a/src/coolkey/machdep.h +++ b/src/coolkey/machdep.h @@ -40,12 +40,14 @@ struct OSLockData; 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; |