@samitouri / QOSAMI-WSL / commits / d28ae62c

cleanup: minor DuplicateHandle helper hygene (#14276)

* cleanup: minor DuplicateHandle helper hygene * pr feedback * Permissions -> DesiredAccess --------- Co-authored-by: Ben Hillis <benhill@ntdev.microsoft.com>

Ben Hillis committed Feb 25, 2026 at 13:22 UTC d28ae62c0e00da88e1ed722ef1cc3874afa5ef76
8 files changed +35 -22
src/windows/common/Dmesg.cpp
+1 -1
@@ -18,7 +18,7 @@ Abstract:
18 DmesgCollector::DmesgCollector(GUID VmId, const wil::unique_event& ExitEvent, bool EnableTelemetry, bool EnableDebugConsole, const std::wstring& Com1PipeName) :
19 m_com1PipeName(Com1PipeName), m_runtimeId(VmId), m_debugConsole(EnableDebugConsole), m_telemetry(EnableTelemetry)
20 {
21 - m_exitEvent.reset(wsl::windows::common::helpers::DuplicateHandle(ExitEvent.get()));
21 + m_exitEvent.reset(wsl::windows::common::wslutil::DuplicateHandle(ExitEvent.get()));
22 m_overlappedEvent.create(wil::EventOptions::ManualReset);
23 m_overlapped.hEvent = m_overlappedEvent.get();
24 m_threadExit.create(wil::EventOptions::ManualReset);
src/windows/common/helpers.cpp
-10
@@ -266,16 +266,6 @@ wsl::windows::common::helpers::unique_proc_attribute_list wsl::windows::common::
266 return List;
267 }
268
269 -[[nodiscard]] HANDLE wsl::windows::common::helpers::DuplicateHandle(_In_ HANDLE Handle, _In_ DWORD DesiredAccess, _In_ BOOL InheritHandle, _In_ DWORD Options)
270 -{
271 - // N.B. This function does not return a wil::unique_handle so that the caller
272 - // can pick its own desired type (e.g. wil::unique_event).
273 - HANDLE Result;
274 - THROW_IF_WIN32_BOOL_FALSE(::DuplicateHandle(GetCurrentProcess(), Handle, GetCurrentProcess(), &Result, DesiredAccess, InheritHandle, Options));
275 -
276 - return Result;
277 -}
278 -
269 std::vector<gsl::byte> wsl::windows::common::helpers::GenerateConfigurationMessage(
270 _In_ const std::wstring& DistributionName,
271 _In_ ULONG FixedDrivesBitmap,
src/windows/common/helpers.hpp
-2
@@ -118,8 +118,6 @@ void CreateConsole(_In_ LPCWSTR ConsoleTitle = nullptr);
118
119 unique_proc_attribute_list CreateProcThreadAttributeList(_In_ DWORD AttributeCount);
120
121 -[[nodiscard]] HANDLE DuplicateHandle(_In_ HANDLE Handle, _In_ DWORD DesiredAccess = 0, _In_ BOOL InheritHandle = FALSE, _In_ DWORD Options = DUPLICATE_SAME_ACCESS);
122 -
121 std::vector<gsl::byte> GenerateConfigurationMessage(
122 _In_ const std::wstring& DistributionName,
123 _In_ ULONG FixedDrivesBitmap = 0,
src/windows/common/svccomm.cpp
+2 -2
@@ -176,7 +176,7 @@ void InitializeInterop(_In_ HANDLE ServerPort, _In_ const GUID& DistroId)
176 // Create a thread to handle interop requests.
177 //
178
179 - wil::unique_handle WorkerThreadSeverPort{wsl::windows::common::helpers::DuplicateHandle(ServerPort)};
179 + wil::unique_handle WorkerThreadSeverPort{wsl::windows::common::wslutil::DuplicateHandle(ServerPort)};
180 std::thread([WorkerThreadSeverPort = std::move(WorkerThreadSeverPort)]() mutable {
181 wsl::windows::common::wslutil::SetThreadDescription(L"Interop");
182 wsl::windows::common::interop::WorkerThread(std::move(WorkerThreadSeverPort));
@@ -194,7 +194,7 @@ void SpawnWslHost(_In_ HANDLE ServerPort, _In_ const GUID& DistroId, _In_opt_ LP
194 {
195 wsl::windows::common::helpers::SetHandleInheritable(ServerPort);
196 const auto RegistrationComplete = wil::unique_event(wil::EventOptions::None);
197 - const wil::unique_handle ParentProcess{wsl::windows::common::helpers::DuplicateHandle(GetCurrentProcess(), 0, TRUE)};
197 + const wil::unique_handle ParentProcess{wsl::windows::common::wslutil::DuplicateHandle(GetCurrentProcess(), std::nullopt, TRUE)};
198 THROW_LAST_ERROR_IF(!ParentProcess);
199
200 const wil::unique_handle Process{wsl::windows::common::helpers::LaunchInteropServer(
src/windows/common/wslutil.cpp
+25 -4
@@ -574,15 +574,36 @@ std::wstring wsl::windows::common::wslutil::DownloadFile(std::wstring_view Url,
574 return file.Path().c_str();
575 }
576
577 -[[nodiscard]] HANDLE wsl::windows::common::wslutil::DuplicateHandleFromCallingProcess(_In_ HANDLE handleInTarget)
577 +[[nodiscard]] HANDLE wsl::windows::common::wslutil::DuplicateHandle(_In_ HANDLE Handle, _In_ std::optional<DWORD> DesiredAccess, _In_ BOOL InheritHandle)
578 +{
579 + HANDLE newHandle;
580 + THROW_IF_WIN32_BOOL_FALSE(::DuplicateHandle(
581 + GetCurrentProcess(), Handle, GetCurrentProcess(), &newHandle, DesiredAccess.value_or(0), InheritHandle, DesiredAccess.has_value() ? 0 : DUPLICATE_SAME_ACCESS));
582 +
583 + return newHandle;
584 +}
585 +
586 +[[nodiscard]] HANDLE wsl::windows::common::wslutil::DuplicateHandleFromCallingProcess(_In_ HANDLE Handle)
587 +{
588 + const wil::unique_handle caller = OpenCallingProcess(PROCESS_DUP_HANDLE);
589 + THROW_LAST_ERROR_IF(!caller);
590 +
591 + HANDLE newHandle;
592 + THROW_IF_WIN32_BOOL_FALSE(::DuplicateHandle(caller.get(), Handle, GetCurrentProcess(), &newHandle, 0, FALSE, DUPLICATE_SAME_ACCESS));
593 +
594 + return newHandle;
595 +}
596 +
597 +[[nodiscard]] HANDLE wsl::windows::common::wslutil::DuplicateHandleToCallingProcess(_In_ HANDLE Handle, _In_ std::optional<DWORD> DesiredAccess)
598 {
599 const wil::unique_handle caller = OpenCallingProcess(PROCESS_DUP_HANDLE);
600 THROW_LAST_ERROR_IF(!caller);
601
582 - HANDLE handle;
583 - THROW_IF_WIN32_BOOL_FALSE(DuplicateHandle(caller.get(), handleInTarget, GetCurrentProcess(), &handle, 0, FALSE, DUPLICATE_SAME_ACCESS));
602 + HANDLE newHandle;
603 + THROW_IF_WIN32_BOOL_FALSE(::DuplicateHandle(
604 + GetCurrentProcess(), Handle, caller.get(), &newHandle, DesiredAccess.value_or(0), FALSE, DesiredAccess.has_value() ? 0 : DUPLICATE_SAME_ACCESS));
605
585 - return handle;
606 + return newHandle;
607 }
608
609 void wsl::windows::common::wslutil::EnforceFileLimit(LPCWSTR Path, size_t Limit, const std::function<bool(const std::filesystem::directory_entry&)>& pred)
src/windows/common/wslutil.h
+5 -1
@@ -101,7 +101,11 @@ GUID CreateV5Uuid(const GUID& namespaceGuid, const std::span<const std::byte> na
101
102 std::wstring DownloadFile(std::wstring_view Url, std::wstring Filename);
103
104 -[[nodiscard]] HANDLE DuplicateHandleFromCallingProcess(_In_ HANDLE handleInTarget);
104 +[[nodiscard]] HANDLE DuplicateHandle(_In_ HANDLE Handle, _In_ std::optional<DWORD> DesiredAccess = std::nullopt, _In_ BOOL InheritHandle = FALSE);
105 +
106 +[[nodiscard]] HANDLE DuplicateHandleFromCallingProcess(_In_ HANDLE Handle);
107 +
108 +[[nodiscard]] HANDLE DuplicateHandleToCallingProcess(_In_ HANDLE Handle, _In_ std::optional<DWORD> Permissions = {});
109
110 void EnforceFileLimit(LPCWSTR Folder, size_t limit, const std::function<bool(const std::filesystem::directory_entry&)>& pred);
111
src/windows/service/exe/GuestTelemetryLogger.cpp
+1 -1
@@ -51,7 +51,7 @@ void GuestTelemetryLogger::Start(const wil::unique_event& ExitEvent)
51
52 THROW_LAST_ERROR_IF(!pipe);
53
54 - wil::unique_handle exitEvent(wsl::windows::common::helpers::DuplicateHandle(ExitEvent.get()));
54 + wil::unique_handle exitEvent(wsl::windows::common::wslutil::DuplicateHandle(ExitEvent.get()));
55 m_thread = std::thread([Self = shared_from_this(), Pipe = std::move(pipe), ExitEvent = std::move(exitEvent)]() {
56 try
57 {
src/windows/service/exe/Lifetime.cpp
+1 -1
@@ -132,7 +132,7 @@ void LifetimeManager::RegisterCallback(_In_ ULONG64 ClientKey, _In_ const std::f
132 if (proc == client->clientProcesses.end())
133 {
134 OwnedProcess newProcess{};
135 - newProcess.process.reset(wsl::windows::common::helpers::DuplicateHandle(ClientProcess));
135 + newProcess.process.reset(wsl::windows::common::wslutil::DuplicateHandle(ClientProcess));
136 newProcess.InitializeListenForTermination(s_OnClientProcessTerminated, this);
137 client->clientProcesses.emplace_back(std::move(newProcess));
138 client->clientProcesses.back().ListenForTermination();