Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

159 lines
4.5 KiB

  1. // LockWrap.cpp: implementation of the CLockWrap class.
  2. // LockWrap.cpp: implementation of the CIOPCriticalSection class.
  3. // LockWrap.cpp: implementation of the CIOPMutex class.
  4. // LockWrap.cpp: implementation of the CSCardLock class.
  5. //
  6. //////////////////////////////////////////////////////////////////////
  7. #include "NoWarning.h"
  8. #include "LockWrap.h"
  9. #include "SmartCard.h"
  10. using namespace iop;
  11. //////////////////////////////////////////////////////////////////////
  12. // Construction/Destruction
  13. //////////////////////////////////////////////////////////////////////
  14. CLockWrap::CLockWrap(CIOPLock *pIOPLock)
  15. : m_IOPCritSect(pIOPLock),
  16. m_IOPMutex(pIOPLock),
  17. m_SCardLock(pIOPLock),
  18. m_pIOPLock(pIOPLock)
  19. {
  20. if (0 == m_pIOPLock->RefCount())
  21. m_pIOPLock->SmartCard()->ResetSelect();
  22. m_pIOPLock->IncrementRefCount();
  23. }
  24. CLockWrap::~CLockWrap()
  25. {
  26. try {
  27. m_pIOPLock->DecrementRefCount();
  28. }
  29. catch(...) {};
  30. }
  31. CIOPCriticalSection::CIOPCriticalSection(CIOPLock *pIOPLock)
  32. : m_pIOPLock(pIOPLock)
  33. {
  34. #if defined(SLBIOP_RM_HANG_AT_PROCESS_DEATH)
  35. EnterCriticalSection(m_pIOPLock->CriticalSection());
  36. #endif
  37. }
  38. CIOPCriticalSection::~CIOPCriticalSection()
  39. {
  40. #if defined(SLBIOP_RM_HANG_AT_PROCESS_DEATH)
  41. try {
  42. LeaveCriticalSection(m_pIOPLock->CriticalSection());
  43. }
  44. catch(...) {};
  45. #endif
  46. }
  47. CIOPMutex::CIOPMutex(CIOPLock *pIOPLock) : m_pIOPLock(pIOPLock)
  48. {
  49. #if defined(SLBIOP_RM_HANG_AT_PROCESS_DEATH)
  50. if (0 == m_pIOPLock->RefCount())
  51. {
  52. if (WaitForSingleObject(m_pIOPLock->MutexHandle(),
  53. INFINITE) == WAIT_FAILED)
  54. throw scu::OsException(GetLastError());
  55. }
  56. #endif
  57. }
  58. CIOPMutex::~CIOPMutex()
  59. {
  60. #if defined(SLBIOP_RM_HANG_AT_PROCESS_DEATH)
  61. try
  62. {
  63. if (0 == m_pIOPLock->RefCount())
  64. ReleaseMutex(m_pIOPLock->MutexHandle());
  65. }
  66. catch (...)
  67. {}
  68. #endif
  69. }
  70. CSCardLock::CSCardLock(CIOPLock *pIOPLock) : m_pIOPLock(pIOPLock)
  71. {
  72. if (0 == m_pIOPLock->RefCount())
  73. {
  74. HRESULT hResult;
  75. SCARDHANDLE hCard = m_pIOPLock->SmartCard()->getCardHandle();
  76. hResult = SCardBeginTransaction(hCard);
  77. if (hResult != SCARD_S_SUCCESS)
  78. {
  79. DWORD dwState;
  80. DWORD dwProtocol;
  81. BYTE bATR[CSmartCard::cMaxAtrLength];
  82. DWORD dwATRLen = sizeof bATR / sizeof *bATR;
  83. DWORD dwReaderNameLen = 0;
  84. HRESULT hr = SCardStatus(hCard, NULL, &dwReaderNameLen,
  85. &dwState, &dwProtocol, bATR,
  86. &dwATRLen);
  87. if (hr == SCARD_W_RESET_CARD)
  88. {
  89. m_pIOPLock->SmartCard()->ResetSelect();
  90. m_pIOPLock->SmartCard()->ReConnect();
  91. }
  92. else
  93. throw scu::OsException(hResult);
  94. hr = SCardBeginTransaction(hCard);
  95. if (hr != SCARD_S_SUCCESS)
  96. throw scu::OsException(hResult);
  97. }
  98. // set the dirty flag on the card to indicate that no data
  99. // on the card has changed since the beginning if the
  100. // transaction.
  101. m_pIOPLock->SmartCard()->Dirty(false);
  102. }
  103. }
  104. CSCardLock::~CSCardLock()
  105. {
  106. try
  107. {
  108. if (0 == m_pIOPLock->RefCount())
  109. {
  110. HRESULT hResult;
  111. SCARDHANDLE hCard = m_pIOPLock->SmartCard()->getCardHandle();
  112. hResult = SCardEndTransaction(hCard, SCARD_LEAVE_CARD);
  113. if (hResult != SCARD_S_SUCCESS)
  114. {
  115. DWORD dwState;
  116. DWORD dwProtocol;
  117. BYTE bATR[CSmartCard::cMaxAtrLength];
  118. DWORD dwATRLen = sizeof bATR / sizeof *bATR;
  119. DWORD dwReaderNameLen = 0;
  120. HRESULT hr = SCardStatus(hCard, NULL,
  121. &dwReaderNameLen, &dwState,
  122. &dwProtocol, bATR,
  123. &dwATRLen);
  124. // ignore failures since in destructor.
  125. if (hr == SCARD_W_RESET_CARD)
  126. {
  127. m_pIOPLock->SmartCard()->ResetSelect();
  128. m_pIOPLock->SmartCard()->ReConnect();
  129. hr = SCardEndTransaction(hCard, SCARD_LEAVE_CARD);
  130. }
  131. }
  132. }
  133. }
  134. catch (...)
  135. {}
  136. }