mirror of
https://github.com/mutouyun/cpp-ipc.git
synced 2025-12-07 01:06:45 +08:00
[Windows] CreateFileMapping/CreateSemaphore with LPSECURITY_ATTRIBUTES.
This commit is contained in:
parent
5a92ea3031
commit
19f471b07c
35
src/platform/get_sa.h
Normal file
35
src/platform/get_sa.h
Normal 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
|
||||
@ -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<DWORD>(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.
|
||||
|
||||
@ -13,7 +13,8 @@
|
||||
#include "platform/detail.h"
|
||||
#include "memory/resource.h"
|
||||
|
||||
namespace ipc::detail {
|
||||
namespace ipc {
|
||||
namespace detail {
|
||||
|
||||
struct has_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));
|
||||
}
|
||||
|
||||
} // namespace ipc::detail
|
||||
} // namespace detail
|
||||
} // namespace ipc
|
||||
|
||||
@ -11,6 +11,7 @@
|
||||
#include "shm.h"
|
||||
|
||||
#include "platform/to_tchar.h"
|
||||
#include "platform/get_sa.h"
|
||||
#include "platform/detail.h"
|
||||
#include "memory/resource.h"
|
||||
|
||||
@ -24,7 +25,7 @@ public:
|
||||
static void remove(char const * /*name*/) {}
|
||||
|
||||
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) {
|
||||
ipc::error("fail CreateSemaphore[%lu]: %s\n", ::GetLastError(), name.c_str());
|
||||
return false;
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user