mirror of
https://github.com/owasp-modsecurity/ModSecurity.git
synced 2025-08-15 23:55:03 +03:00
Merge pull request #3210 from eduar-hte/shared-files-deadlock
Fixed shared files deadlock in a multi-threaded Windows application
This commit is contained in:
commit
68d551c5f9
@ -17,8 +17,7 @@
|
|||||||
|
|
||||||
#include <fcntl.h>
|
#include <fcntl.h>
|
||||||
#ifdef WIN32
|
#ifdef WIN32
|
||||||
#include <windows.h>
|
#include <algorithm>
|
||||||
#include <io.h>
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
||||||
@ -34,7 +33,32 @@ SharedFiles::handlers_map::iterator SharedFiles::add_new_handler(
|
|||||||
return m_handlers.end();
|
return m_handlers.end();
|
||||||
}
|
}
|
||||||
|
|
||||||
return m_handlers.insert({ fileName, {fp, 0} }).first;
|
#ifdef WIN32
|
||||||
|
// replace invalid characters for a Win32 named object
|
||||||
|
auto tmp = fileName;
|
||||||
|
std::replace(tmp.begin(), tmp.end(), '\\', '_');
|
||||||
|
std::replace(tmp.begin(), tmp.end(), '/', '_');
|
||||||
|
|
||||||
|
// use named mutex for multi-process locking support
|
||||||
|
const auto mutexName = "Global\\ModSecurity_" + tmp;
|
||||||
|
|
||||||
|
HANDLE hMutex = CreateMutex(NULL, FALSE, mutexName.c_str());
|
||||||
|
if (hMutex == NULL) {
|
||||||
|
error->assign("Failed to create mutex for shared file: " + fileName);
|
||||||
|
fclose(fp);
|
||||||
|
return m_handlers.end();
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
auto handler = handler_info {
|
||||||
|
fp,
|
||||||
|
#ifdef WIN32
|
||||||
|
hMutex,
|
||||||
|
#endif
|
||||||
|
0
|
||||||
|
};
|
||||||
|
// cppcheck-suppress resourceLeak ; false positive, fp is closed in SharedFiles::close
|
||||||
|
return m_handlers.insert({ fileName, handler }).first;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -69,6 +93,9 @@ void SharedFiles::close(const std::string& fileName) {
|
|||||||
if (it->second.cnt == 0)
|
if (it->second.cnt == 0)
|
||||||
{
|
{
|
||||||
fclose(it->second.fp);
|
fclose(it->second.fp);
|
||||||
|
#ifdef WIN32
|
||||||
|
CloseHandle(it->second.hMutex);
|
||||||
|
#endif
|
||||||
|
|
||||||
m_handlers.erase(it);
|
m_handlers.erase(it);
|
||||||
}
|
}
|
||||||
@ -92,9 +119,11 @@ bool SharedFiles::write(const std::string& fileName,
|
|||||||
lock.l_type = F_WRLCK;
|
lock.l_type = F_WRLCK;
|
||||||
fcntl(fileno(it->second.fp), F_SETLKW, &lock);
|
fcntl(fileno(it->second.fp), F_SETLKW, &lock);
|
||||||
#else
|
#else
|
||||||
auto handle = reinterpret_cast<HANDLE>(_get_osfhandle(fileno(it->second.fp)));
|
DWORD dwWaitResult = WaitForSingleObject(it->second.hMutex, INFINITE);
|
||||||
OVERLAPPED overlapped = { 0 };
|
if (dwWaitResult != WAIT_OBJECT_0) {
|
||||||
::LockFileEx(handle, LOCKFILE_EXCLUSIVE_LOCK, 0, MAXDWORD, MAXDWORD, &overlapped);
|
error->assign("couldn't lock shared file: " + fileName);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
auto wrote = fwrite(msg.c_str(), 1, msg.size(), it->second.fp);
|
auto wrote = fwrite(msg.c_str(), 1, msg.size(), it->second.fp);
|
||||||
@ -109,8 +138,7 @@ bool SharedFiles::write(const std::string& fileName,
|
|||||||
lock.l_type = F_UNLCK;
|
lock.l_type = F_UNLCK;
|
||||||
fcntl(fileno(it->second.fp), F_SETLKW, &lock);
|
fcntl(fileno(it->second.fp), F_SETLKW, &lock);
|
||||||
#else
|
#else
|
||||||
overlapped = { 0 };
|
::ReleaseMutex(it->second.hMutex);
|
||||||
::UnlockFileEx(handle, 0, MAXDWORD, MAXDWORD, &overlapped);
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
return ret;
|
return ret;
|
||||||
|
@ -18,6 +18,9 @@
|
|||||||
|
|
||||||
|
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
#ifdef WIN32
|
||||||
|
#include <Windows.h>
|
||||||
|
#endif
|
||||||
|
|
||||||
#include <unordered_map>
|
#include <unordered_map>
|
||||||
#include <string>
|
#include <string>
|
||||||
@ -53,6 +56,9 @@ private:
|
|||||||
|
|
||||||
struct handler_info {
|
struct handler_info {
|
||||||
FILE* fp;
|
FILE* fp;
|
||||||
|
#ifdef WIN32
|
||||||
|
HANDLE hMutex;
|
||||||
|
#endif
|
||||||
unsigned int cnt;
|
unsigned int cnt;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user