From 330019551cb7a399ea32eb0a74d711a5aeba0f4a Mon Sep 17 00:00:00 2001 From: Feng Wang Date: Tue, 14 Apr 2026 11:23:27 +0800 Subject: [PATCH] rework changes --- src/windows/WslcSDK/wslcsdk.cpp | 20 +++++++++++++++++++ src/windows/WslcSDK/wslcsdk.def | 1 + src/windows/WslcSDK/wslcsdk.h | 2 ++ test/windows/WslcSdkTests.cpp | 34 +++++++++++++++++++++++++++++++++ 4 files changed, 57 insertions(+) diff --git a/src/windows/WslcSDK/wslcsdk.cpp b/src/windows/WslcSDK/wslcsdk.cpp index 702d78251..024bc52bb 100644 --- a/src/windows/WslcSDK/wslcsdk.cpp +++ b/src/windows/WslcSDK/wslcsdk.cpp @@ -442,6 +442,26 @@ try } CATCH_RETURN(); +STDAPI WslcGetCliSession(_Out_ WslcSession* session, _Outptr_opt_result_z_ PWSTR* errorMessage) +try +{ + RETURN_HR_IF_NULL(E_POINTER, session); + *session = nullptr; + ErrorInfoWrapper errorInfoWrapper{errorMessage}; + + wil::com_ptr sessionManager = CreateSessionManager(); + + auto result = std::make_unique(); + if (SUCCEEDED(errorInfoWrapper.CaptureResult(sessionManager->OpenSessionByName(nullptr, &result->session)))) + { + wsl::windows::common::security::ConfigureForCOMImpersonation(result->session.get()); + *session = reinterpret_cast(result.release()); + } + + return errorInfoWrapper; +} +CATCH_RETURN(); + STDAPI WslcTerminateSession(_In_ WslcSession session) try { diff --git a/src/windows/WslcSDK/wslcsdk.def b/src/windows/WslcSDK/wslcsdk.def index 800c57d5c..efc06b882 100644 --- a/src/windows/WslcSDK/wslcsdk.def +++ b/src/windows/WslcSDK/wslcsdk.def @@ -9,6 +9,7 @@ WslcInitContainerSettings WslcInitProcessSettings WslcCreateSession +WslcGetCliSession WslcCreateContainer WslcReleaseSession diff --git a/src/windows/WslcSDK/wslcsdk.h b/src/windows/WslcSDK/wslcsdk.h index 5aa51dc84..0c70b791b 100644 --- a/src/windows/WslcSDK/wslcsdk.h +++ b/src/windows/WslcSDK/wslcsdk.h @@ -98,6 +98,8 @@ STDAPI WslcInitSessionSettings(_In_ PCWSTR name, _In_ PCWSTR storagePath, _Out_ STDAPI WslcCreateSession(_In_ WslcSessionSettings* sessionSettings, _Out_ WslcSession* session, _Outptr_opt_result_z_ PWSTR* errorMessage); +STDAPI WslcGetCliSession(_Out_ WslcSession* session, _Outptr_opt_result_z_ PWSTR* errorMessage); + // OPTIONAL SESSION SETTINGS STDAPI WslcSetSessionSettingsCpuCount(_In_ WslcSessionSettings* sessionSettings, _In_ uint32_t cpuCount); STDAPI WslcSetSessionSettingsMemory(_In_ WslcSessionSettings* sessionSettings, _In_ uint32_t memoryMb); diff --git a/test/windows/WslcSdkTests.cpp b/test/windows/WslcSdkTests.cpp index 55c29ebcc..b675a9523 100644 --- a/test/windows/WslcSdkTests.cpp +++ b/test/windows/WslcSdkTests.cpp @@ -18,6 +18,7 @@ Module Name: #include "WslcsdkPrivate.h" #include "WSLCContainerLauncher.h" #include "wslc_schema.h" +#include "e2e/WSLCExecutor.h" #include extern std::wstring g_testDataPath; @@ -42,6 +43,16 @@ void CloseSession(WslcSession session) using UniqueSession = wil::unique_any; +void ReleaseSession(WslcSession session) +{ + if (session) + { + WslcReleaseSession(session); + } +} + +using UniqueSessionRef = wil::unique_any; + void CloseContainer(WslcContainer container) { if (container) @@ -265,6 +276,29 @@ class WslcSdkTests VERIFY_ARE_EQUAL(WslcCreateSession(nullptr, &session2, nullptr), E_POINTER); } + WSLC_TEST_METHOD(GetCliSession) + { + // Null output pointer must fail. + VERIFY_ARE_EQUAL(WslcGetCliSession(nullptr, nullptr), E_POINTER); + + // Ensure no CLI session is running. + WSLCE2ETests::RunWslc(L"session terminate"); + + // WslcGetCliSession must return ERROR_NOT_FOUND when no CLI session exists. + UniqueSessionRef notFoundSession; + VERIFY_ARE_EQUAL(WslcGetCliSession(¬FoundSession, nullptr), HRESULT_FROM_WIN32(ERROR_NOT_FOUND)); + VERIFY_IS_NULL(notFoundSession.get()); + + // Start the CLI session by running a wslc command. + auto result = WSLCE2ETests::RunWslc(L"container list"); + VERIFY_ARE_EQUAL(result.ExitCode.value(), (DWORD)0); + + // Now WslcGetCliSession should find the running CLI session. + UniqueSessionRef foundSession; + VERIFY_SUCCEEDED(WslcGetCliSession(&foundSession, nullptr)); + VERIFY_IS_NOT_NULL(foundSession.get()); + } + WSLC_TEST_METHOD(TerminationCallbackViaTerminate) { std::promise promise;