diff options
Diffstat (limited to 'compat/shm.cpp')
-rw-r--r-- | compat/shm.cpp | 133 |
1 files changed, 15 insertions, 118 deletions
diff --git a/compat/shm.cpp b/compat/shm.cpp index 4bd89d35..f59469dc 100644 --- a/compat/shm.cpp +++ b/compat/shm.cpp @@ -10,130 +10,36 @@ #if defined _WIN32 #include <cstring> -#include <stdio.h> +#include <cstdio> #include <accctrl.h> #include <aclapi.h> -#if !defined __WINE__ +#ifdef QT_CORE_LIB # include <QDebug> +# define warn(str, ...) (qDebug() << "shm:" str ": " << __VA_ARGS__) +#else +# define warn(str, ...) (void)0 #endif -struct secattr final -{ - bool success; - SECURITY_DESCRIPTOR* pSD; - SECURITY_ATTRIBUTES attrs; - PSID pEveryoneSID; - PACL pACL; - - void cleanup(); - secattr(DWORD perms); - ~secattr(); -}; - -void secattr::cleanup() -{ - if (pEveryoneSID) - FreeSid(pEveryoneSID); - if (pACL) - LocalFree(pACL); - if (pSD) - LocalFree(pSD); - success = false; - pSD = nullptr; - pEveryoneSID = nullptr; - pACL = nullptr; -} - -secattr::secattr(DWORD perms) : success(true), pSD(nullptr), pEveryoneSID(nullptr), pACL(nullptr) -{ - 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)); - - 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) - { - fprintf(stderr, "LocalAlloc: %d\n", (int) GetLastError()); - goto cleanup; - } - - 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::~secattr() -{ - cleanup(); -} - shm_wrapper::shm_wrapper(const char* shm_name, const char* mutex_name, int map_size) { - secattr sa(GENERIC_ALL|SYNCHRONIZE); - if (mutex_name == nullptr) mutex = nullptr; else { - mutex = CreateMutexA(sa.success ? &sa.attrs : nullptr, false, mutex_name); + mutex = CreateMutexA(nullptr, false, mutex_name); if (!mutex) { - #if !defined __WINE__ - qDebug() << "CreateMutexA:" << (int) GetLastError(); - #endif + warn("CreateMutexA", (int) GetLastError()); return; } } mapped_file = CreateFileMappingA( INVALID_HANDLE_VALUE, - sa.success ? &sa.attrs : nullptr, + nullptr, PAGE_READWRITE, 0, map_size, @@ -141,9 +47,7 @@ shm_wrapper::shm_wrapper(const char* shm_name, const char* mutex_name, int map_s if (!mapped_file) { -#if !defined __WINE__ - qDebug() << "CreateFileMappingA:", (int) GetLastError(); -#endif + warn("CreateFileMappingA", (int) GetLastError()); return; } @@ -155,19 +59,15 @@ shm_wrapper::shm_wrapper(const char* shm_name, const char* mutex_name, int map_s map_size); if (!mem) - { -#if !defined __WINE__ - qDebug() << "MapViewOfFile:" << (int) GetLastError(); -#endif - } + warn("MapViewOfFile:", (int) GetLastError()); } shm_wrapper::~shm_wrapper() { - if(!UnmapViewOfFile(mem)) + if (mem && !UnmapViewOfFile(mem)) goto fail; - if (!CloseHandle(mapped_file)) + if (mapped_file && !CloseHandle(mapped_file)) goto fail; if (mutex && !CloseHandle(mutex)) @@ -176,10 +76,7 @@ shm_wrapper::~shm_wrapper() return; fail: - (void)0; -#if !defined __WINE__ - qDebug() << "failed to close mapping"; -#endif + warn("failed to close mapping", (int) GetLastError()); } bool shm_wrapper::lock() @@ -209,7 +106,7 @@ shm_wrapper::shm_wrapper(const char *shm_name, const char* /*mutex_name*/, int m strcat(filename, shm_name); fd = shm_open(filename, O_RDWR | O_CREAT, 0600); (void) ftruncate(fd, map_size); - mem = mmap(NULL, map_size, PROT_READ|PROT_WRITE, MAP_SHARED, fd, (off_t)0); + mem = mmap(nullptr, map_size, PROT_READ|PROT_WRITE, MAP_SHARED, fd, (off_t)0); } shm_wrapper::~shm_wrapper() @@ -234,7 +131,7 @@ bool shm_wrapper::success() #ifndef _WIN32 return mem != (void*) -1; #else - return mem != NULL; + return mem != nullptr; #endif } |