Skip to content

Commit

Permalink
Add WT_SESSION to WSLENV so it propagates into WSL (#4157)
Browse files Browse the repository at this point in the history
This makes sure that things running inside WSL can see `WT_SESSION`.

Closes #3948.
  • Loading branch information
DHowett authored and msftbot[bot] committed Jan 9, 2020
1 parent d97135b commit 988f0a6
Showing 1 changed file with 19 additions and 12 deletions.
31 changes: 19 additions & 12 deletions src/cascadia/TerminalConnection/ConptyConnection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,18 @@ namespace winrt::Microsoft::Terminal::TerminalConnection::implementation
std::wstring cmdline{ wil::ExpandEnvironmentStringsW<std::wstring>(_commandline.c_str()) }; // mutable copy -- required for CreateProcessW

Utils::EnvironmentVariableMapW environment;
auto zeroEnvMap = wil::scope_exit([&] {
// Can't zero the keys, but at least we can zero the values.
for (auto& [name, value] : environment)
{
::SecureZeroMemory(value.data(), value.size() * sizeof(decltype(value.begin())::value_type));
}

environment.clear();
});

// Populate the environment map with the current environment.
RETURN_IF_FAILED(Utils::UpdateEnvironmentMapW(environment));

{
// Convert connection Guid to string and ignore the enclosing '{}'.
Expand All @@ -97,7 +109,13 @@ namespace winrt::Microsoft::Terminal::TerminalConnection::implementation
const auto guidSubStr = std::wstring_view{ wsGuid }.substr(1);

// Ensure every connection has the unique identifier in the environment.
environment.emplace(L"WT_SESSION", guidSubStr.data());
environment.insert_or_assign(L"WT_SESSION", guidSubStr.data());

auto wslEnv = environment[L"WSLENV"]; // We always want to load something, even if it's blank.
// WSLENV is a colon-delimited list of environment variables (+flags) that should appear inside WSL
// https://devblogs.microsoft.com/commandline/share-environment-vars-between-wsl-and-windows/
wslEnv = L"WT_SESSION:" + wslEnv; // prepend WT_SESSION to make sure it's visible inside WSL.
environment.insert_or_assign(L"WSLENV", wslEnv);
}

std::vector<wchar_t> newEnvVars;
Expand All @@ -106,17 +124,6 @@ namespace winrt::Microsoft::Terminal::TerminalConnection::implementation
newEnvVars.size() * sizeof(decltype(newEnvVars.begin())::value_type));
});

auto zeroEnvMap = wil::scope_exit([&] {
// Can't zero the keys, but at least we can zero the values.
for (auto& [name, value] : environment)
{
::SecureZeroMemory(value.data(), value.size() * sizeof(decltype(value.begin())::value_type));
}

environment.clear();
});

RETURN_IF_FAILED(Utils::UpdateEnvironmentMapW(environment));
RETURN_IF_FAILED(Utils::EnvironmentMapToEnvironmentStringsW(environment, newEnvVars));

LPWCH lpEnvironment = newEnvVars.empty() ? nullptr : newEnvVars.data();
Expand Down

0 comments on commit 988f0a6

Please sign in to comment.