Skip to content

Commit 38dbca3

Browse files
author
Ben Hillis
committed
Switch WSLg to use new wslinfo --vm-id functonality
1 parent c85d2f3 commit 38dbca3

1 file changed

Lines changed: 19 additions & 4 deletions

File tree

WSLGd/main.cpp

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616

1717
constexpr auto c_serviceIdTemplate = "%08X-FACB-11E6-BD58-64006A7986D3";
1818
constexpr auto c_userName = "wslg";
19-
constexpr auto c_vmIdEnv = "WSL2_VM_ID";
2019

2120
constexpr auto c_dbusDir = "/var/run/dbus";
2221
constexpr auto c_versionFile = "/etc/versions.txt";
@@ -133,6 +132,22 @@ bool GetEnvBool(const char *EnvName, bool DefaultValue)
133132
return DefaultValue;
134133
}
135134

135+
std::string GetVmId()
136+
{
137+
std::unique_ptr<FILE, decltype(&pclose)> pipe(popen("/usr/bin/wslinfo --vm-id -n", "r"), pclose);
138+
THROW_LAST_ERROR_IF(!pipe);
139+
140+
std::array<char, 128> buffer;
141+
std::string result;
142+
while (fgets(buffer.data(), buffer.size(), pipe.get()) != nullptr) {
143+
result += buffer.data();
144+
}
145+
146+
THROW_ERRNO_IF(EINVAL, pclose(pipe.release()) != 0);
147+
148+
return result;
149+
}
150+
136151
void SetupOptionalEnv()
137152
{
138153
#if HAVE_WINPR
@@ -255,9 +270,9 @@ try {
255270
}
256271

257272
// Query the VM ID.
258-
auto vmId = getenv(c_vmIdEnv);
259-
if (!vmId) {
260-
LOG_ERROR("%s must be set.", c_vmIdEnv);
273+
auto vmId = GetVmId();
274+
if (vmId.empty()) {
275+
LOG_ERROR("could not query VM ID.");
261276
return 1;
262277
}
263278

0 commit comments

Comments
 (0)