Skip to content

Commit

Permalink
try getting VM ID from lxss before command line
Browse files Browse the repository at this point in the history
  • Loading branch information
MrVinkel authored and Biswa96 committed Oct 12, 2024
1 parent 837825b commit 274530b
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 8 deletions.
11 changes: 10 additions & 1 deletion src/GetVmId.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include "GetVmId.hpp"
#include "LxssUserSession.hpp"
#include "Helpers.hpp"
#include "GetVmIdWsl2.hpp"

#ifndef WSL_DISTRIBUTION_FLAGS_VALID

Expand Down Expand Up @@ -309,7 +310,15 @@ HRESULT GetVmId(GUID *DistroId, GUID *LxInstanceID, const int LiftedWSLVersion)

if (hRes)
{
LOG_HRESULT_ERROR("CreateLxProcess", hRes);
// Try get VM ID from command line of wslHost.exe
if (GetVmIdWsl2(LxInstanceID))
{
hRes = 0;
}
else
{
LOG_HRESULT_ERROR("CreateLxProcess", hRes);
}
}
}
else if (WindowsBuild < 20211) // Before Build 20211 Fe
Expand Down
6 changes: 3 additions & 3 deletions src/GetVmIdWsl2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -107,18 +107,18 @@ std::vector<DWORD> GetProcessIDsByName(const std::wstring& processName) {
// Extract GUID from wslHost.exe command line
// Example commandline:
// wslhost.exe --vm-id {f6446e02-236e-4b24-9916-2d4ad9a1096f} --handle 1664
bool GetVmIdWsl2(GUID& vmId) {
bool GetVmIdWsl2(GUID* vmId) {
std::vector<DWORD> pids = GetProcessIDsByName(L"wslhost.exe");
for (DWORD pid : pids) {
std::wstring cmdLine;
if (!GetCommandLineForPID(pid, cmdLine))
continue;

std::wstring cmdVmId;
if(!ExtractGUID(L"--vm-id", cmdLine, cmdVmId))
if(!ExtractGUID(L"--vm-id", cmdLine, cmdVmId))
continue;

if (IIDFromString(cmdVmId.c_str(), &vmId) == S_OK)
if (IIDFromString(cmdVmId.c_str(), vmId) == S_OK)
return true;
}
return false;
Expand Down
2 changes: 1 addition & 1 deletion src/GetVmIdWsl2.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@
#ifndef VMIDWSL2_HPP
#define VMIDWSL2_HPP

bool GetVmIdWsl2(GUID& vmId);
bool GetVmIdWsl2(GUID* vmId);

#endif /* VMIDWSL2_HPP */
6 changes: 3 additions & 3 deletions src/wslbridge2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
#include "Environment.hpp"
#include "TerminalState.hpp"
#include "windows-sock.h"
#include "GetVmIdWsl2.hpp"

union IoSockets
{
Expand Down Expand Up @@ -389,8 +388,9 @@ int main(int argc, char *argv[])
if (LiftedWSLVersion)
start_dummy(wslPath, wslCmdLine, distroName, debugMode);

if (!GetVmIdWsl2(VmId))
fatal("Failed to get VM ID");
const HRESULT hRes = GetVmId(&DistroId, &VmId, LiftedWSLVersion);
if (hRes != 0)
fatal("GetVmId: %s\n", GetErrorMessage(hRes).c_str());

inputSock = win_vsock_create();
outputSock = win_vsock_create();
Expand Down

0 comments on commit 274530b

Please sign in to comment.