[Pcsclite-cvs-commit] r3295 - /trunk/PCSC/src/readerfactory.c

rousseau at users.alioth.debian.org rousseau at users.alioth.debian.org
Thu Jan 29 16:08:11 UTC 2009


Author: rousseau
Date: Thu Jan 29 16:08:10 2009
New Revision: 3295

URL: http://svn.debian.org/wsvn/pcsclite/?sc=1&rev=3295
Log:
RFLockSharing() & RFUnlockSharing(): add a mutex to avoid a race
condition

Closes: [#311377] Race condition in SCardBeginTransaction

Modified:
    trunk/PCSC/src/readerfactory.c

Modified: trunk/PCSC/src/readerfactory.c
URL: http://svn.debian.org/wsvn/pcsclite/trunk/PCSC/src/readerfactory.c?rev=3295&op=diff
==============================================================================
--- trunk/PCSC/src/readerfactory.c (original)
+++ trunk/PCSC/src/readerfactory.c Thu Jan 29 16:08:10 2009
@@ -47,6 +47,7 @@
 static DWORD dwNumReadersContexts = 0;
 static char *ConfigFile = NULL;
 static int ConfigFileCRC = 0;
+static PCSCLITE_MUTEX LockMutex = PTHREAD_MUTEX_INITIALIZER;
 
 #define IDENTITY_SHIFT 16
 
@@ -1021,18 +1022,20 @@
 LONG RFLockSharing(DWORD hCard)
 {
 	PREADER_CONTEXT rContext = NULL;
+	LONG rv;
 
 	(void)RFReaderInfoById(hCard, &rContext);
 
-	if (RFCheckSharing(hCard) == SCARD_S_SUCCESS)
+	(void)SYS_MutexLock(&LockMutex);
+	rv = RFCheckSharing(hCard);
+	if (SCARD_S_SUCCESS == rv)
 	{
 		rContext->LockCount += 1;
 		rContext->dwLockId = hCard;
 	}
-	else
-		return SCARD_E_SHARING_VIOLATION;
-
-	return SCARD_S_SUCCESS;
+	(void)SYS_MutexUnLock(&LockMutex);
+
+	return rv;
 }
 
 LONG RFUnlockSharing(DWORD hCard)
@@ -1044,16 +1047,18 @@
 	if (rv != SCARD_S_SUCCESS)
 		return rv;
 
+	(void)SYS_MutexLock(&LockMutex);
 	rv = RFCheckSharing(hCard);
-	if (rv != SCARD_S_SUCCESS)
-		return rv;
-
-	if (rContext->LockCount > 0)
-		rContext->LockCount -= 1;
-	if (0 == rContext->LockCount)
-		rContext->dwLockId = 0;
-
-	return SCARD_S_SUCCESS;
+	if (SCARD_S_SUCCESS == rv)
+	{
+		if (rContext->LockCount > 0)
+			rContext->LockCount -= 1;
+		if (0 == rContext->LockCount)
+			rContext->dwLockId = 0;
+	}
+	(void)SYS_MutexUnLock(&LockMutex);
+
+	return rv;
 }
 
 LONG RFUnblockContext(SCARDCONTEXT hContext)




More information about the Pcsclite-cvs-commit mailing list