summaryrefslogtreecommitdiffhomepage
path: root/compat/shm.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'compat/shm.cpp')
-rw-r--r--compat/shm.cpp195
1 files changed, 68 insertions, 127 deletions
diff --git a/compat/shm.cpp b/compat/shm.cpp
index 6aeb8ca9..f59469dc 100644
--- a/compat/shm.cpp
+++ b/compat/shm.cpp
@@ -7,190 +7,131 @@
#include "shm.h"
-#if defined(_WIN32)
+#if defined _WIN32
#include <cstring>
-#include <stdio.h>
+#include <cstdio>
#include <accctrl.h>
#include <aclapi.h>
-struct secattr
-{
- bool success;
- SECURITY_DESCRIPTOR* pSD;
- SECURITY_ATTRIBUTES attrs;
- PSID pEveryoneSID;
- PACL pACL;
-
- void cleanup()
- {
- if (pEveryoneSID)
- FreeSid(pEveryoneSID);
- if (pACL)
- LocalFree(pACL);
- if (pSD)
- LocalFree(pSD);
- success = false;
- pSD = nullptr;
- pEveryoneSID = nullptr;
- pACL = nullptr;
- }
+#ifdef QT_CORE_LIB
+# include <QDebug>
+# define warn(str, ...) (qDebug() << "shm:" str ": " << __VA_ARGS__)
+#else
+# define warn(str, ...) (void)0
+#endif
- secattr(DWORD perms) : success(true), pSD(nullptr), pEveryoneSID(nullptr), pACL(nullptr)
+shm_wrapper::shm_wrapper(const char* shm_name, const char* mutex_name, int map_size)
+{
+ if (mutex_name == nullptr)
+ mutex = nullptr;
+ else
{
- SID_IDENTIFIER_AUTHORITY SIDAuthWorld = SECURITY_WORLD_SID_AUTHORITY;
- EXPLICIT_ACCESS ea;
-
- if(!AllocateAndInitializeSid(&SIDAuthWorld, 1,
- SECURITY_WORLD_RID,
- 0, 0, 0, 0, 0, 0, 0,
- &pEveryoneSID))
- {
- fprintf(stderr, "AllocateAndInitializeSid: %d\n", (int) GetLastError());
- goto cleanup;
- }
-
- memset(&ea, 0, sizeof(ea));
+ mutex = CreateMutexA(nullptr, false, mutex_name);
- ea.grfAccessPermissions = perms;
- ea.grfAccessMode = SET_ACCESS;
- ea.grfInheritance = NO_INHERITANCE;
- ea.Trustee.TrusteeForm = TRUSTEE_IS_SID;
- ea.Trustee.TrusteeType = TRUSTEE_IS_WELL_KNOWN_GROUP;
- ea.Trustee.ptstrName = (LPTSTR) pEveryoneSID;
-
- if (SetEntriesInAcl(1, &ea, NULL, &pACL) != ERROR_SUCCESS)
- {
- fprintf(stderr, "SetEntriesInAcl: %d\n", (int) GetLastError());
- goto cleanup;
- }
-
- pSD = (SECURITY_DESCRIPTOR*) LocalAlloc(LPTR, SECURITY_DESCRIPTOR_MIN_LENGTH);
- if (pSD == nullptr)
+ if (!mutex)
{
- fprintf(stderr, "LocalAlloc: %d\n", (int) GetLastError());
- goto cleanup;
+ warn("CreateMutexA", (int) GetLastError());
+ return;
}
-
- if (!InitializeSecurityDescriptor(pSD,
- SECURITY_DESCRIPTOR_REVISION))
- {
- fprintf(stderr, "InitializeSecurityDescriptor: %d\n", (int) GetLastError());
- goto cleanup;
- }
-
- if (!SetSecurityDescriptorDacl(pSD,
- TRUE,
- pACL,
- FALSE))
- {
- fprintf(stderr, "SetSecurityDescriptorDacl: %d\n", (int) GetLastError());
- goto cleanup;
- }
-
- attrs.bInheritHandle = false;
- attrs.lpSecurityDescriptor = pSD;
- attrs.nLength = sizeof(SECURITY_ATTRIBUTES);
-
- return;
-cleanup:
- cleanup();
- }
-
- ~secattr()
- {
- cleanup();
}
-};
-PortableLockedShm::PortableLockedShm(const char* shmName, const char* mutexName, int mapSize)
-{
- secattr sa(GENERIC_ALL|SYNCHRONIZE);
-
- hMutex = CreateMutexA(sa.success ? &sa.attrs : nullptr, false, mutexName);
- if (!hMutex)
- {
- fprintf(stderr, "CreateMutexA: %d\n", (int) GetLastError());
- fflush(stderr);
- }
- hMapFile = CreateFileMappingA(
+ mapped_file = CreateFileMappingA(
INVALID_HANDLE_VALUE,
- sa.success ? &sa.attrs : nullptr,
+ nullptr,
PAGE_READWRITE,
0,
- mapSize,
- shmName);
- if (!hMapFile)
+ map_size,
+ shm_name);
+
+ if (!mapped_file)
{
- fprintf(stderr, "CreateFileMappingA: %d\n", (int) GetLastError());
- fflush(stderr);
+ warn("CreateFileMappingA", (int) GetLastError());
+
+ return;
}
- mem = MapViewOfFile(hMapFile,
+
+ mem = MapViewOfFile(mapped_file,
FILE_MAP_WRITE,
0,
0,
- mapSize);
+ map_size);
+
if (!mem)
- {
- fprintf(stderr, "MapViewOfFile: %d\n", (int) GetLastError());
- fflush(stderr);
- }
+ warn("MapViewOfFile:", (int) GetLastError());
}
-PortableLockedShm::~PortableLockedShm()
+shm_wrapper::~shm_wrapper()
{
- UnmapViewOfFile(mem);
- CloseHandle(hMapFile);
- CloseHandle(hMutex);
+ if (mem && !UnmapViewOfFile(mem))
+ goto fail;
+
+ if (mapped_file && !CloseHandle(mapped_file))
+ goto fail;
+
+ if (mutex && !CloseHandle(mutex))
+ goto fail;
+
+ return;
+
+fail:
+ warn("failed to close mapping", (int) GetLastError());
}
-void PortableLockedShm::lock()
+bool shm_wrapper::lock()
{
- (void) WaitForSingleObject(hMutex, INFINITE);
+ if (mutex)
+ return WaitForSingleObject(mutex, INFINITE) == WAIT_OBJECT_0;
+ else
+ return false;
}
-void PortableLockedShm::unlock()
+bool shm_wrapper::unlock()
{
- (void) ReleaseMutex(hMutex);
+ if (mutex)
+ return ReleaseMutex(mutex);
+ else
+ return false;
}
#else
#include <limits.h>
#pragma GCC diagnostic ignored "-Wunused-result"
-PortableLockedShm::PortableLockedShm(const char *shmName, const char* /*mutexName*/, int mapSize) : size(mapSize)
+shm_wrapper::shm_wrapper(const char *shm_name, const char* /*mutex_name*/, int map_size) : size(map_size)
{
- char filename[PATH_MAX+2] = {0};
+ char filename[PATH_MAX+2] {};
strcpy(filename, "/");
- strcat(filename, shmName);
+ strcat(filename, shm_name);
fd = shm_open(filename, O_RDWR | O_CREAT, 0600);
- (void) ftruncate(fd, mapSize);
- mem = mmap(NULL, mapSize, PROT_READ|PROT_WRITE, MAP_SHARED, fd, (off_t)0);
+ (void) ftruncate(fd, map_size);
+ mem = mmap(nullptr, map_size, PROT_READ|PROT_WRITE, MAP_SHARED, fd, (off_t)0);
}
-PortableLockedShm::~PortableLockedShm()
+shm_wrapper::~shm_wrapper()
{
(void) munmap(mem, size);
(void) close(fd);
}
-void PortableLockedShm::lock()
+bool shm_wrapper::lock()
{
- flock(fd, LOCK_EX);
+ return flock(fd, LOCK_EX) == 0;
}
-void PortableLockedShm::unlock()
+bool shm_wrapper::unlock()
{
- flock(fd, LOCK_UN);
+ return flock(fd, LOCK_UN) == 0;
}
#endif
-bool PortableLockedShm::success()
+bool shm_wrapper::success()
{
#ifndef _WIN32
return mem != (void*) -1;
#else
- return mem != NULL;
+ return mem != nullptr;
#endif
}
+