Lines 42-48
Link Here
|
42 |
|
42 |
|
43 |
static SlotList *slotList = NULL; |
43 |
static SlotList *slotList = NULL; |
44 |
|
44 |
|
45 |
static OSLock finalizeLock(false); |
45 |
static OSLock *finalizeLock = NULL; |
|
|
46 |
#define FINALIZE_GETLOCK() if (finalizeLock) finalizeLock->getLock(); |
47 |
#define FINALIZE_RELEASELOCK() if (finalizeLock) finalizeLock->releaseLock(); |
46 |
|
48 |
|
47 |
static CK_BBOOL initialized = FALSE; |
49 |
static CK_BBOOL initialized = FALSE; |
48 |
static CK_BBOOL finalizing = FALSE; |
50 |
static CK_BBOOL finalizing = FALSE; |
Lines 208-218
Link Here
|
208 |
if( initialized ) { |
210 |
if( initialized ) { |
209 |
return CKR_CRYPTOKI_ALREADY_INITIALIZED; |
211 |
return CKR_CRYPTOKI_ALREADY_INITIALIZED; |
210 |
} |
212 |
} |
211 |
if (!finalizeLock.isValid()) { |
213 |
if (finalizeLock && !finalizeLock->isValid()) { |
212 |
return CKR_CANT_LOCK; |
214 |
return CKR_CANT_LOCK; |
213 |
} |
215 |
} |
214 |
CK_C_INITIALIZE_ARGS* initArgs = (CK_C_INITIALIZE_ARGS*) pInitArgs; |
216 |
CK_C_INITIALIZE_ARGS* initArgs = (CK_C_INITIALIZE_ARGS*) pInitArgs; |
|
|
217 |
OSLock::setThreadSafe(0); |
215 |
if( initArgs != NULL ) { |
218 |
if( initArgs != NULL ) { |
|
|
219 |
bool needThreads; |
216 |
/* work around a bug in NSS where the library parameters are only |
220 |
/* work around a bug in NSS where the library parameters are only |
217 |
* send if locking is requested */ |
221 |
* send if locking is requested */ |
218 |
if (initArgs->LibraryParameters) { |
222 |
if (initArgs->LibraryParameters) { |
Lines 220-226
Link Here
|
220 |
} else { |
224 |
} else { |
221 |
Params::ClearParams(); |
225 |
Params::ClearParams(); |
222 |
} |
226 |
} |
223 |
if( (initArgs->flags & CKF_OS_LOCKING_OK) || initArgs->LockMutex ){ |
227 |
needThreads = ((initArgs->flags & CKF_OS_LOCKING_OK) != 0); |
|
|
228 |
OSLock::setThreadSafe(needThreads); |
229 |
/* don't get a finalize lock unless someone initializes us asking |
230 |
* us to use threads */ |
231 |
if (needThreads && !finalizeLock) { |
232 |
finalizeLock = new OSLock(true); |
233 |
if (finalizeLock == NULL) return CKR_HOST_MEMORY; |
234 |
} |
235 |
/* only support OS LOCKING threads */ |
236 |
if( ((initArgs->flags & CKF_OS_LOCKING_OK) == 0) |
237 |
&& initArgs->LockMutex ){ |
224 |
throw PKCS11Exception(CKR_CANT_LOCK); |
238 |
throw PKCS11Exception(CKR_CANT_LOCK); |
225 |
} |
239 |
} |
226 |
} |
240 |
} |
Lines 259-267
Link Here
|
259 |
// the finalizing call first, we know it will set waitEvent before |
273 |
// the finalizing call first, we know it will set waitEvent before |
260 |
// we can get the lock, so we only need to protect setting finalizing |
274 |
// we can get the lock, so we only need to protect setting finalizing |
261 |
// to true. |
275 |
// to true. |
262 |
finalizeLock.getLock(); |
276 |
FINALIZE_GETLOCK(); |
263 |
finalizing = TRUE; |
277 |
finalizing = TRUE; |
264 |
finalizeLock.releaseLock(); |
278 |
FINALIZE_RELEASELOCK(); |
265 |
if (waitEvent) { |
279 |
if (waitEvent) { |
266 |
/* we're waiting on a slot event, shutdown first to allow |
280 |
/* we're waiting on a slot event, shutdown first to allow |
267 |
* the wait function to complete before we pull the rug out. |
281 |
* the wait function to complete before we pull the rug out. |
Lines 273-282
Link Here
|
273 |
} |
287 |
} |
274 |
delete slotList; |
288 |
delete slotList; |
275 |
delete log; |
289 |
delete log; |
276 |
finalizeLock.getLock(); |
290 |
FINALIZE_GETLOCK(); |
277 |
finalizing = FALSE; |
291 |
finalizing = FALSE; |
278 |
initialized = FALSE; |
292 |
initialized = FALSE; |
279 |
finalizeLock.releaseLock(); |
293 |
FINALIZE_RELEASELOCK(); |
280 |
return CKR_OK; |
294 |
return CKR_OK; |
281 |
} |
295 |
} |
282 |
|
296 |
|
Lines 595-611
Link Here
|
595 |
CK_RV |
609 |
CK_RV |
596 |
C_WaitForSlotEvent(CK_FLAGS flags, CK_SLOT_ID_PTR pSlot, CK_VOID_PTR pReserved) |
610 |
C_WaitForSlotEvent(CK_FLAGS flags, CK_SLOT_ID_PTR pSlot, CK_VOID_PTR pReserved) |
597 |
{ |
611 |
{ |
598 |
finalizeLock.getLock(); |
612 |
FINALIZE_GETLOCK(); |
599 |
if( ! initialized ) { |
613 |
if( ! initialized ) { |
600 |
finalizeLock.releaseLock(); |
614 |
FINALIZE_RELEASELOCK(); |
601 |
return CKR_CRYPTOKI_NOT_INITIALIZED; |
615 |
return CKR_CRYPTOKI_NOT_INITIALIZED; |
602 |
} |
616 |
} |
603 |
if (finalizing) { |
617 |
if (finalizing) { |
604 |
finalizeLock.releaseLock(); |
618 |
FINALIZE_RELEASELOCK(); |
605 |
return CKR_CRYPTOKI_NOT_INITIALIZED; |
619 |
return CKR_CRYPTOKI_NOT_INITIALIZED; |
606 |
} |
620 |
} |
607 |
waitEvent = TRUE; |
621 |
waitEvent = TRUE; |
608 |
finalizeLock.releaseLock(); |
622 |
FINALIZE_RELEASELOCK(); |
609 |
try { |
623 |
try { |
610 |
log->log("C_WaitForSlotEvent called\n"); |
624 |
log->log("C_WaitForSlotEvent called\n"); |
611 |
slotList->waitForSlotEvent(flags, pSlot, pReserved); |
625 |
slotList->waitForSlotEvent(flags, pSlot, pReserved); |