| 1 | // Copyright (C) Microsoft Corporation. All rights reserved. |
| 2 | |
| 3 | #include "precomp.h" |
| 4 | #include "GnsRpcServer.h" |
| 5 | #include "wsldeps.h" |
| 6 | |
| 7 | static constexpr PCWSTR c_SystemOnlySDDL = |
| 8 | // Owner: System |
| 9 | L"O:SY" |
| 10 | // Group: System |
| 11 | L"G:SY" |
| 12 | // Begin DACL |
| 13 | L"D:" |
| 14 | // Allow System full access |
| 15 | L"(A;;GA;;;SY)"; |
| 16 | |
| 17 | static std::weak_ptr<GnsRpcServer> instance; |
| 18 | wil::unique_hlocal_security_descriptor systemOnlySD; |
| 19 | |
| 20 | std::shared_ptr<GnsRpcServer> GnsRpcServer::GetOrCreate() |
| 21 | { |
| 22 | static wil::srwlock instanceLock; |
| 23 | auto lock = instanceLock.lock_exclusive(); |
| 24 | |
| 25 | if (nullptr == systemOnlySD) |
| 26 | { |
| 27 | THROW_IF_WIN32_BOOL_FALSE(ConvertStringSecurityDescriptorToSecurityDescriptor(c_SystemOnlySDDL, SDDL_REVISION_1, &systemOnlySD, nullptr)); |
| 28 | } |
| 29 | |
| 30 | auto ptr = instance.lock(); |
| 31 | if (!ptr) |
| 32 | { |
| 33 | ptr = std::make_shared<GnsRpcServer>(); |
| 34 | instance = ptr; |
| 35 | } |
| 36 | |
| 37 | return ptr; |
| 38 | } |
| 39 | |
| 40 | GnsRpcServer::GnsRpcServer() |
| 41 | { |
| 42 | THROW_IF_FAILED(WslDepsRegisterGnsRpcServer(systemOnlySD.get(), &m_serverUuid)); |
| 43 | } |
| 44 | |
| 45 | GnsRpcServer::~GnsRpcServer() noexcept |
| 46 | { |
| 47 | if (!m_moved) |
| 48 | { |
| 49 | LOG_IF_FAILED(WslDepsUnregisterGnsRpcServer(&m_serverUuid)); |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | GnsRpcServer::GnsRpcServer(GnsRpcServer&& Other) noexcept |
| 54 | { |
| 55 | *this = std::move(Other); |
| 56 | } |
| 57 | |
| 58 | GnsRpcServer& GnsRpcServer::operator=(GnsRpcServer&& Other) noexcept |
| 59 | { |
| 60 | m_serverUuid = Other.m_serverUuid; |
| 61 | Other.m_serverUuid = GUID{}; |
| 62 | Other.m_moved = true; |
| 63 | |
| 64 | return *this; |
| 65 | } |
| 66 | |
| 67 | const UUID& GnsRpcServer::GetServerUuid() const noexcept |
| 68 | { |
| 69 | return m_serverUuid; |
| 70 | } |