diff --git a/src/platform/get_sa.h b/src/platform/get_sa.h new file mode 100644 index 0000000..74b68d4 --- /dev/null +++ b/src/platform/get_sa.h @@ -0,0 +1,35 @@ +#pragma once + +#include + +namespace ipc { +namespace detail { + +inline LPSECURITY_ATTRIBUTES get_sa() { + static struct initiator { + + SECURITY_DESCRIPTOR sd_; + SECURITY_ATTRIBUTES sa_; + + bool succ_ = false; + + initiator() { + if (!::InitializeSecurityDescriptor(&sd_, SECURITY_DESCRIPTOR_REVISION)) { + ipc::error("fail InitializeSecurityDescriptor[%d]\n", static_cast(::GetLastError())); + return; + } + if (!::SetSecurityDescriptorDacl(&sd_, TRUE, NULL, FALSE)) { + ipc::error("fail SetSecurityDescriptorDacl[%d]\n", static_cast(::GetLastError())); + return; + } + sa_.nLength = sizeof(SECURITY_ATTRIBUTES); + sa_.bInheritHandle = FALSE; + sa_.lpSecurityDescriptor = &sd_; + succ_ = true; + } + } handle; + return handle.succ_ ? &handle.sa_ : nullptr; +} + +} // namespace detail +} // namespace ipc diff --git a/src/platform/shm_win.cpp b/src/platform/shm_win.cpp index ad51e2a..abe2249 100644 --- a/src/platform/shm_win.cpp +++ b/src/platform/shm_win.cpp @@ -10,6 +10,7 @@ #include "pool_alloc.h" #include "platform/to_tchar.h" +#include "platform/get_sa.h" #include "memory/resource.h" namespace { @@ -38,7 +39,7 @@ id_t acquire(char const * name, std::size_t size, unsigned mode) { } // Creates or opens a named file mapping object for a specified file. else { - h = ::CreateFileMapping(INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE | SEC_COMMIT, + h = ::CreateFileMapping(INVALID_HANDLE_VALUE, detail::get_sa(), PAGE_READWRITE | SEC_COMMIT, 0, static_cast(size), fmt_name.c_str()); // If the object exists before the function call, the function returns a handle to the existing object // (with its current size, not the specified size), and GetLastError returns ERROR_ALREADY_EXISTS. diff --git a/src/platform/to_tchar.h b/src/platform/to_tchar.h index 65c0a83..09d1f80 100644 --- a/src/platform/to_tchar.h +++ b/src/platform/to_tchar.h @@ -13,7 +13,8 @@ #include "platform/detail.h" #include "memory/resource.h" -namespace ipc::detail { +namespace ipc { +namespace detail { struct has_value_type_ { template static std::true_type check(typename T::value_type *); @@ -64,4 +65,5 @@ inline auto to_tchar(T* dst, char const * src, std::size_t size) -> IsSameChar