Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions src/windows/WslcSDK/wslcsdk.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<IWSLCSessionManager> sessionManager = CreateSessionManager();

auto result = std::make_unique<WslcSessionImpl>();
if (SUCCEEDED(errorInfoWrapper.CaptureResult(sessionManager->OpenSessionByName(nullptr, &result->session))))
{
wsl::windows::common::security::ConfigureForCOMImpersonation(result->session.get());
*session = reinterpret_cast<WslcSession>(result.release());
}

return errorInfoWrapper;
}
CATCH_RETURN();

STDAPI WslcTerminateSession(_In_ WslcSession session)
try
{
Expand Down
1 change: 1 addition & 0 deletions src/windows/WslcSDK/wslcsdk.def
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ WslcInitContainerSettings
WslcInitProcessSettings

WslcCreateSession
WslcGetCliSession
WslcCreateContainer

WslcReleaseSession
Expand Down
2 changes: 2 additions & 0 deletions src/windows/WslcSDK/wslcsdk.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
34 changes: 34 additions & 0 deletions test/windows/WslcSdkTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ Module Name:
#include "WslcsdkPrivate.h"
#include "WSLCContainerLauncher.h"
#include "wslc_schema.h"
#include "e2e/WSLCExecutor.h"
#include <optional>

extern std::wstring g_testDataPath;
Expand All @@ -42,6 +43,16 @@ void CloseSession(WslcSession session)

using UniqueSession = wil::unique_any<WslcSession, decltype(CloseSession), CloseSession>;

void ReleaseSession(WslcSession session)
{
if (session)
{
WslcReleaseSession(session);
}
}

using UniqueSessionRef = wil::unique_any<WslcSession, decltype(ReleaseSession), ReleaseSession>;

void CloseContainer(WslcContainer container)
{
if (container)
Expand Down Expand Up @@ -265,6 +276,29 @@ class WslcSdkTests
VERIFY_ARE_EQUAL(WslcCreateSession(nullptr, &session2, nullptr), E_POINTER);
}

WSLC_TEST_METHOD(GetCliSession)
Comment thread
chemwolf6922 marked this conversation as resolved.
{
// 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(&notFoundSession, 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");
Comment thread
chemwolf6922 marked this conversation as resolved.
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<WslcSessionTerminationReason> promise;
Expand Down