[Windows] CreateFileMapping/CreateSemaphore with LPSECURITY_ATTRIBUTES.

This commit is contained in:
mutouyun 2020-07-11 15:29:20 +08:00
parent 5a92ea3031
commit 19f471b07c
4 changed files with 43 additions and 4 deletions

35
src/platform/get_sa.h Normal file
View File

@ -0,0 +1,35 @@
#pragma once
#include <securitybaseapi.h>
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<int>(::GetLastError()));
return;
}
if (!::SetSecurityDescriptorDacl(&sd_, TRUE, NULL, FALSE)) {
ipc::error("fail SetSecurityDescriptorDacl[%d]\n", static_cast<int>(::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

View File

@ -10,6 +10,7 @@
#include "pool_alloc.h" #include "pool_alloc.h"
#include "platform/to_tchar.h" #include "platform/to_tchar.h"
#include "platform/get_sa.h"
#include "memory/resource.h" #include "memory/resource.h"
namespace { 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. // Creates or opens a named file mapping object for a specified file.
else { 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<DWORD>(size), fmt_name.c_str()); 0, static_cast<DWORD>(size), fmt_name.c_str());
// If the object exists before the function call, the function returns a handle to the existing object // 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. // (with its current size, not the specified size), and GetLastError returns ERROR_ALREADY_EXISTS.

View File

@ -13,7 +13,8 @@
#include "platform/detail.h" #include "platform/detail.h"
#include "memory/resource.h" #include "memory/resource.h"
namespace ipc::detail { namespace ipc {
namespace detail {
struct has_value_type_ { struct has_value_type_ {
template <typename T> static std::true_type check(typename T::value_type *); template <typename T> 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<T
std::memcpy(dst, wstr.data(), (ipc::detail::min)(wstr.size(), size)); std::memcpy(dst, wstr.data(), (ipc::detail::min)(wstr.size(), size));
} }
} // namespace ipc::detail } // namespace detail
} // namespace ipc

View File

@ -11,6 +11,7 @@
#include "shm.h" #include "shm.h"
#include "platform/to_tchar.h" #include "platform/to_tchar.h"
#include "platform/get_sa.h"
#include "platform/detail.h" #include "platform/detail.h"
#include "memory/resource.h" #include "memory/resource.h"
@ -24,7 +25,7 @@ public:
static void remove(char const * /*name*/) {} static void remove(char const * /*name*/) {}
bool open(ipc::string && name, long count = 0, long limit = LONG_MAX) { bool open(ipc::string && name, long count = 0, long limit = LONG_MAX) {
h_ = ::CreateSemaphore(NULL, count, limit, ipc::detail::to_tchar(std::move(name)).c_str()); h_ = ::CreateSemaphore(detail::get_sa(), count, limit, ipc::detail::to_tchar(std::move(name)).c_str());
if (h_ == NULL) { if (h_ == NULL) {
ipc::error("fail CreateSemaphore[%lu]: %s\n", ::GetLastError(), name.c_str()); ipc::error("fail CreateSemaphore[%lu]: %s\n", ::GetLastError(), name.c_str());
return false; return false;