Skip to content

Commit d8cd707

Browse files
benhillisBen Hillis
andauthored
cleanup: refactor wslrelay and other minor cleanup (#14099)
* cleanup: refactor wslrelay and other minor cleanup * pr feedback --------- Co-authored-by: Ben Hillis <benhill@ntdev.microsoft.com>
1 parent b9b108c commit d8cd707

File tree

6 files changed

+32
-23
lines changed

6 files changed

+32
-23
lines changed

src/windows/common/CMakeLists.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
set(SOURCES
22
ConsoleProgressBar.cpp
33
ConsoleProgressIndicator.cpp
4+
ConsoleState.cpp
45
DeviceHostProxy.cpp
56
disk.cpp
67
Distribution.cpp
@@ -32,7 +33,6 @@ set(SOURCES
3233
string.cpp
3334
SubProcess.cpp
3435
svccomm.cpp
35-
ConsoleState.cpp
3636
WslClient.cpp
3737
WslCoreConfig.cpp
3838
WslCoreFilesystem.cpp
@@ -74,6 +74,7 @@ set(HEADERS
7474
../inc/wslrelay.h
7575
ConsoleProgressBar.h
7676
ConsoleProgressIndicator.h
77+
ConsoleState.h
7778
DeviceHostProxy.h
7879
disk.hpp
7980
Distribution.h
@@ -107,7 +108,6 @@ set(HEADERS
107108
Stringify.h
108109
SubProcess.h
109110
svccomm.hpp
110-
ConsoleState.h
111111
WslClient.h
112112
WslCoreConfig.h
113113
WslCoreFilesystem.h

src/windows/common/ConsoleState.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ Module Name:
99
Abstract:
1010
1111
This file contains function definitions for the ConsoleState helper class.
12+
1213
--*/
1314

1415
#include "precomp.h"

src/windows/common/helpers.cpp

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,11 @@ class ProcessLauncher
178178
launcher.AddOption(wslrelay::disable_telemetry_option);
179179
}
180180

181+
if (WI_IsFlagSet(Flags, LaunchWslRelayFlags::ConnectPipe))
182+
{
183+
launcher.AddOption(wslrelay::connect_pipe_option);
184+
}
185+
181186
return launcher.Launch(UserToken, WI_IsFlagSet(Flags, LaunchWslRelayFlags::HideWindow));
182187
}
183188
} // namespace
@@ -501,11 +506,10 @@ bool wsl::windows::common::helpers::IsWslSupportInterfacePresent()
501506
void wsl::windows::common::helpers::LaunchDebugConsole(
502507
_In_ LPCWSTR PipeName, _In_ bool ConnectExistingPipe, _In_ HANDLE UserToken, _In_opt_ HANDLE LogFile, _In_ bool DisableTelemetry)
503508
{
504-
wslrelay::RelayMode relayMode;
509+
LaunchWslRelayFlags flags{};
505510
wil::unique_hfile pipe;
506511
if (ConnectExistingPipe)
507512
{
508-
relayMode = wslrelay::RelayMode::DebugConsoleRelay;
509513
// Connect to an existing pipe. The connection should be:
510514
// Asynchronous (FILE_FLAG_OVERLAPPED)
511515
// Anonymous (SECURITY_SQOS_PRESENT | SECURITY_ANONYMOUS)
@@ -515,21 +519,20 @@ void wsl::windows::common::helpers::LaunchDebugConsole(
515519
}
516520
else
517521
{
518-
relayMode = wslrelay::RelayMode::DebugConsole;
519-
// Create a new pipe server. The pipe should be:
522+
// Create a new pipe server the child process will connect to. The pipe should be:
520523
// Bi-directional: PIPE_ACCESS_DUPLEX
521524
// Asynchronous: FILE_FLAG_OVERLAPPED
522525
// Raw: PIPE_TYPE_BYTE | PIPE_READMODE_BYTE
523526
// Blocking: PIPE_WAIT
527+
WI_SetFlag(flags, LaunchWslRelayFlags::ConnectPipe);
524528
pipe.reset(CreateNamedPipeW(
525529
PipeName, (PIPE_ACCESS_DUPLEX | FILE_FLAG_OVERLAPPED), (PIPE_TYPE_BYTE | PIPE_READMODE_BYTE | PIPE_WAIT), 1, LX_RELAY_BUFFER_SIZE, LX_RELAY_BUFFER_SIZE, 0, nullptr));
526530
}
527531

528532
THROW_LAST_ERROR_IF(!pipe);
529533

530-
LaunchWslRelayFlags flags{};
531534
WI_SetFlagIf(flags, LaunchWslRelayFlags::DisableTelemetry, DisableTelemetry);
532-
wil::unique_handle info{LaunchWslRelay(relayMode, LogFile, nullptr, pipe.get(), {}, nullptr, UserToken, flags)};
535+
wil::unique_handle info{LaunchWslRelay(wslrelay::RelayMode::DebugConsole, LogFile, nullptr, pipe.get(), {}, nullptr, UserToken, flags)};
533536
}
534537

535538
[[nodiscard]] wil::unique_handle wsl::windows::common::helpers::LaunchInteropServer(
@@ -550,7 +553,7 @@ void wsl::windows::common::helpers::LaunchKdRelay(_In_ LPCWSTR PipeName, _In_ HA
550553

551554
THROW_LAST_ERROR_IF(!pipe);
552555

553-
LaunchWslRelayFlags flags{};
556+
LaunchWslRelayFlags flags = LaunchWslRelayFlags::ConnectPipe;
554557
WI_SetFlagIf(flags, LaunchWslRelayFlags::DisableTelemetry, DisableTelemetry);
555558
wil::unique_handle info{LaunchWslRelay(wslrelay::RelayMode::KdRelay, nullptr, nullptr, pipe.get(), Port, ExitEvent, UserToken, flags)};
556559
}

src/windows/common/helpers.hpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,8 @@ enum class LaunchWslRelayFlags
6262
{
6363
None = 0,
6464
DisableTelemetry = 1,
65-
HideWindow = 2
65+
HideWindow = 2,
66+
ConnectPipe = 4
6667
};
6768

6869
DEFINE_ENUM_FLAG_OPERATORS(LaunchWslRelayFlags);

src/windows/inc/wslrelay.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ enum RelayMode
1919
{
2020
Invalid = -1,
2121
DebugConsole,
22-
DebugConsoleRelay,
2322
PortRelay,
2423
KdRelay
2524
};
@@ -32,4 +31,5 @@ LPCWSTR const pipe_option = L"--pipe";
3231
LPCWSTR const exit_event_option = L"--exit-event";
3332
LPCWSTR const port_option = L"--port";
3433
LPCWSTR const disable_telemetry_option = L"--disable-telemetry";
34+
LPCWSTR const connect_pipe_option = L"--connect-pipe";
3535
} // namespace wslrelay

src/windows/wslrelay/main.cpp

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,10 @@ try
3838
wslrelay::RelayMode mode{wslrelay::RelayMode::Invalid};
3939
wil::unique_handle pipe{};
4040
wil::unique_handle exitEvent{};
41-
int port{};
41+
uint32_t port{};
4242
GUID vmId{};
4343
bool disableTelemetry = !wsl::shared::OfficialBuild;
44+
bool connectPipe = false;
4445

4546
ArgumentParser parser(GetCommandLineW(), wslrelay::binary_name);
4647
parser.AddArgument(Integer(reinterpret_cast<int&>(mode)), wslrelay::mode_option);
@@ -50,30 +51,36 @@ try
5051
parser.AddArgument(Handle{exitEvent}, wslrelay::exit_event_option);
5152
parser.AddArgument(Integer{port}, wslrelay::port_option);
5253
parser.AddArgument(disableTelemetry, wslrelay::disable_telemetry_option);
54+
parser.AddArgument(connectPipe, wslrelay::connect_pipe_option);
5355
parser.Parse();
5456

5557
// Initialize logging.
5658
WslTraceLoggingInitialize(LxssTelemetryProvider, disableTelemetry);
5759
auto cleanup = wil::scope_exit_log(WI_DIAGNOSTICS_INFO, [] { WslTraceLoggingUninitialize(); });
5860

61+
// Ensure that the other end of the pipe has connected if required.
62+
if (connectPipe)
63+
{
64+
std::vector<HANDLE> exitEvents;
65+
if (exitEvent)
66+
{
67+
exitEvents.push_back(exitEvent.get());
68+
}
69+
70+
wsl::windows::common::helpers::ConnectPipe(pipe.get(), (15 * 1000), exitEvents);
71+
}
72+
5973
// Perform the requested operation.
6074
switch (mode)
6175
{
6276
case wslrelay::RelayMode::DebugConsole:
63-
case wslrelay::RelayMode::DebugConsoleRelay:
6477
{
6578
// If not relaying to a file, create a console window.
6679
if (!handle)
6780
{
6881
wsl::windows::common::helpers::CreateConsole(L"WSL Debug Console");
6982
}
7083

71-
if (mode == wslrelay::RelayMode::DebugConsole)
72-
{
73-
// Ensure that the other end of the pipe has connected.
74-
wsl::windows::common::helpers::ConnectPipe(pipe.get(), (15 * 1000));
75-
}
76-
7784
// Relay the contents of the pipe to the output handle.
7885
wsl::windows::common::relay::InterruptableRelay(pipe.get(), handle ? handle.get() : GetStdHandle(STD_OUTPUT_HANDLE));
7986

@@ -98,9 +105,6 @@ try
98105
{
99106
THROW_HR_IF(E_INVALIDARG, port == 0);
100107

101-
// Ensure that the other end of the pipe has connected.
102-
wsl::windows::common::helpers::ConnectPipe(pipe.get(), (15 * 1000), {exitEvent.get()});
103-
104108
// Bind, listen, and accept a connection on the specified port.
105109
const wil::unique_socket listenSocket(WSASocket(AF_INET, SOCK_STREAM, IPPROTO_TCP, nullptr, 0, WSA_FLAG_OVERLAPPED));
106110
THROW_LAST_ERROR_IF(!listenSocket);
@@ -126,7 +130,7 @@ try
126130
}
127131

128132
default:
129-
THROW_HR(E_INVALIDARG);
133+
THROW_HR_MSG(E_INVALIDARG, "Invalid relay mode %d specified.", static_cast<int>(mode));
130134
}
131135

132136
return 0;

0 commit comments

Comments
 (0)