diff options
author | Stanislaw Halik <sthalik@misaki.pl> | 2018-01-23 15:22:20 +0100 |
---|---|---|
committer | Stanislaw Halik <sthalik@misaki.pl> | 2018-01-23 15:22:20 +0100 |
commit | e719ab6cf5efef6deef0848a666e7603a170f775 (patch) | |
tree | 4d3587a80feedc1e4f9fe48ef6891122e5d781e1 | |
parent | 01a1f8871fb62a2997d2bac64a1fd4b41e8957f7 (diff) |
compat/shm: add never_inline
Also, move out of header in `struct secattr'.
-rw-r--r-- | compat/shm.cpp | 172 | ||||
-rw-r--r-- | compat/shm.h | 11 |
2 files changed, 94 insertions, 89 deletions
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 <QDebug> -#endif - #include <cstring> #include <stdio.h> #include <accctrl.h> #include <aclapi.h> -struct secattr +#if !defined __WINE__ +# include <QDebug> +#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 <sys/types.h> #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; } }; |