Account for dev/null being reported as a terminal handle

https://github.com/PowerShell/Win32-OpenSSH/issues/1330

Issue: open(dev/nul) returns a handle is passes isatty() test (i.e its being treated as a terminal handle by OS). This handle seems to work fine for as a console handle for console APIs, except when NUL is explicitly redirected. 
This works
ssh -n target hostname

but this hangs due to a deadlock from writing a log entry on a console API failure
ssh -n target hostname < NUL

Fix: Ignore console API failures when the handle is being reported as invalid.
This commit is contained in:
Manoj Ampalam 2019-05-21 13:25:49 -07:00 committed by GitHub
parent 22cc95e986
commit bfd4ddf194
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 7 additions and 6 deletions

View File

@ -90,13 +90,14 @@ ReadThread(_In_ LPVOID lpParameter)
isFirstTime = false;
DWORD dwAttributes;
if (!GetConsoleMode(GetStdHandle(STD_INPUT_HANDLE), &dwAttributes))
error("GetConsoleMode on STD_INPUT_HANDLE failed with %d", GetLastError());
/* open(dev/null) is showing up as FILE_TYPE_CHAR but is not a valid console handle */
if (GetConsoleMode(GetStdHandle(STD_INPUT_HANDLE), &dwAttributes)) {
dwAttributes |= (ENABLE_ECHO_INPUT | ENABLE_LINE_INPUT | ENABLE_PROCESSED_INPUT);
if (!SetConsoleMode(GetStdHandle(STD_INPUT_HANDLE), dwAttributes))
error("SetConsoleMode on STD_INPUT_HANDLE failed with %d", GetLastError());
debug2("SetConsoleMode on STD_INPUT_HANDLE failed with %d", GetLastError());
} else if (GetLastError() != ERROR_INVALID_HANDLE)
debug2("GetConsoleMode on STD_INPUT_HANDLE failed with %d", GetLastError());
}
if (!ReadFile(WINHANDLE(pio), pio->read_details.buf,