File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1616
1717constexpr auto c_serviceIdTemplate = " %08X-FACB-11E6-BD58-64006A7986D3" ;
1818constexpr auto c_userName = " wslg" ;
19- constexpr auto c_vmIdEnv = " WSL2_VM_ID" ;
2019
2120constexpr auto c_dbusDir = " /var/run/dbus" ;
2221constexpr 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+
136151void 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
You can’t perform that action at this time.
0 commit comments