From e719ab6cf5efef6deef0848a666e7603a170f775 Mon Sep 17 00:00:00 2001 From: Stanislaw Halik Date: Tue, 23 Jan 2018 15:22:20 +0100 Subject: compat/shm: add never_inline Also, move out of header in `struct secattr'. --- compat/shm.cpp | 172 +++++++++++++++++++++++++++++---------------------------- compat/shm.h | 11 ++-- 2 files changed, 94 insertions(+), 89 deletions(-) (limited to 'compat') diff --git a/compat/shm.cpp b/compat/shm.cpp index 7b04e774..c11e48e7 100644 --- a/compat/shm.cpp +++ b/compat/shm.cpp @@ -9,17 +9,17 @@ #if defined(_WIN32) -#if !defined __WINE__ -# include -#endif - #include #include #include #include -struct secattr +#if !defined __WINE__ +# include +#endif + +struct secattr final { bool success; SECURITY_DESCRIPTOR* pSD; @@ -27,85 +27,9 @@ struct secattr 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; - } - - 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() - { - cleanup(); - } + void cleanup(); + secattr(DWORD perms); + ~secattr(); }; shm_wrapper::shm_wrapper(const char* shm_name, const char* mutex_name, int map_size) @@ -233,3 +157,83 @@ bool shm_wrapper::success() return mem != NULL; #endif } + +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(); +} diff --git a/compat/shm.h b/compat/shm.h index 61efaf68..5ea6c80a 100644 --- a/compat/shm.h +++ b/compat/shm.h @@ -19,6 +19,7 @@ #include #endif +#include "macros.hpp" #include "export.hpp" class OTR_COMPAT_EXPORT shm_wrapper final @@ -31,10 +32,10 @@ class OTR_COMPAT_EXPORT shm_wrapper final #endif public: - shm_wrapper(const char *shm_name, const char *mutex_name, int map_size); - ~shm_wrapper(); - bool lock(); - bool unlock(); - bool success(); + never_inline shm_wrapper(const char *shm_name, const char *mutex_name, int map_size); + never_inline ~shm_wrapper(); + never_inline bool lock(); + never_inline bool unlock(); + never_inline bool success(); inline void* ptr() { return mem; } }; -- cgit v1.2.3