Skip to content

Commit

Permalink
use mutex to handle driver file access
Browse files Browse the repository at this point in the history
  • Loading branch information
goldendeng committed Apr 23, 2018
1 parent b0f80b1 commit 89ccde7
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 6 deletions.
2 changes: 1 addition & 1 deletion UsbDkHelper/DriverAccess.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ class UsbDkDriverAccess : public UsbDkDriverFile
{
public:
UsbDkDriverAccess()
: UsbDkDriverFile(USBDK_USERMODE_NAME)
: UsbDkDriverFile(USBDK_USERMODE_NAME, false, true)
{}

void GetDevicesList(PUSB_DK_DEVICE_INFO &DevicesArray, ULONG &NumberDevice);
Expand Down
15 changes: 14 additions & 1 deletion UsbDkHelper/DriverFile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,15 @@
#include "DriverFile.h"


UsbDkDriverFile::UsbDkDriverFile(LPCTSTR lpFileName, bool bOverlapped)
static MutexT g_mutex;
UsbDkDriverFile::UsbDkDriverFile(LPCTSTR lpFileName, bool bOverlapped, bool bLocked)
{
m_bOverlapped = bOverlapped;
m_locked = bLocked;

if (bLocked)
g_mutex.lock();


m_hDriver = CreateFile(lpFileName,
GENERIC_READ | GENERIC_WRITE,
Expand All @@ -43,6 +49,13 @@ UsbDkDriverFile::UsbDkDriverFile(LPCTSTR lpFileName, bool bOverlapped)
}
}

UsbDkDriverFile::~UsbDkDriverFile()
{
CloseHandle(m_hDriver);
if (m_locked)
g_mutex.unlock();
}

TransferResult UsbDkDriverFile::Ioctl(DWORD Code,
bool ShortBufferOk,
LPVOID InBuffer,
Expand Down
30 changes: 27 additions & 3 deletions UsbDkHelper/DriverFile.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,29 @@

#define DRIVER_FILE_EXCEPTION_STRING TEXT("Driver file operation error. ")

class MutexT {
public:
MutexT() {
_usbdk_mutex = OpenMutex(MUTEX_ALL_ACCESS, FALSE, TEXT("usbdk_mutex"));
if (_usbdk_mutex == NULL)
_usbdk_mutex = CreateMutex(NULL, FALSE, TEXT("usbdk_mutex"));
}
~MutexT() {
CloseHandle(_usbdk_mutex);
}
void lock() {
WaitForSingleObject(_usbdk_mutex, INFINITE);
}
void unlock() {
ReleaseMutex(_usbdk_mutex);
}
private:
void* _usbdk_mutex;
// no copy
MutexT(const MutexT&);
void operator=(const MutexT&);
};

class UsbDkDriverFileException : public UsbDkW32ErrorException
{
public:
Expand All @@ -39,12 +62,11 @@ class UsbDkDriverFileException : public UsbDkW32ErrorException
class UsbDkDriverFile
{
public:
UsbDkDriverFile(LPCTSTR lpFileName, bool bOverlapped = false);
UsbDkDriverFile(LPCTSTR lpFileName, bool bOverlapped = false, bool bLocked = false);
UsbDkDriverFile(HANDLE ObjectHandle, bool bOverlapped = false)
: m_bOverlapped(bOverlapped), m_hDriver(ObjectHandle)
{}
virtual ~UsbDkDriverFile()
{ CloseHandle(m_hDriver); }
virtual ~UsbDkDriverFile();

TransferResult Ioctl(DWORD Code,
bool ShortBufferOk = false,
Expand All @@ -70,4 +92,6 @@ class UsbDkDriverFile

private:
bool m_bOverlapped;
bool m_locked;
//mutex_t _mutex;
};
5 changes: 4 additions & 1 deletion UsbDkHelper/UsbDkHelper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ BOOL UsbDk_StopRedirect(HANDLE DeviceHandle)
{
try
{
UsbDkDriverAccess driverAccess;
//UsbDkDriverAccess driverAccess;
unique_ptr<REDIRECTED_DEVICE_HANDLE> deviceHandle(unpackHandle<REDIRECTED_DEVICE_HANDLE>(DeviceHandle));
deviceHandle->RedirectorAccess.reset();
return TRUE;
Expand Down Expand Up @@ -289,6 +289,7 @@ HANDLE UsbDk_GetRedirectorSystemHandle(HANDLE DeviceHandle)

HANDLE UsbDk_CreateHiderHandle()
{
OutputDebugStringA("UsbDk_CreateHiderHandle");
try
{
unique_ptr<UsbDkHiderAccess> hiderAccess(new UsbDkHiderAccess);
Expand Down Expand Up @@ -372,10 +373,12 @@ InstallResult ModifyPersistentHideRules(const USB_DK_HIDE_RULE &Rule,

DLL InstallResult UsbDk_AddPersistentHideRule(PUSB_DK_HIDE_RULE Rule)
{
OutputDebugStringA("UsbDk_AddPersistentHideRule");
return ModifyPersistentHideRules(*Rule, &CRulesManager::AddRule);
}

DLL InstallResult UsbDk_DeletePersistentHideRule(PUSB_DK_HIDE_RULE Rule)
{
OutputDebugStringA("UsbDk_DeletePersistentHideRule");
return ModifyPersistentHideRules(*Rule, &CRulesManager::DeleteRule);
}

0 comments on commit 89ccde7

Please sign in to comment.