Cygwin: termios: Trim buffer size for GetConsoleProcessList()

Currently, the buffer of 128KB is passed to GetConsoleProcessList().
This causes page fault in the select() loop for console due to:
https://github.com/microsoft/terminal/issues/18264
because the previous code calls GetConsoleProcessList() with large
buffer and PeekConsoleInput() with small buffer alternately.
With this patch, the minimum buffer size is used that is determined
by GetConsoleProcessList() with small buffer passed.

Addresses: https://cygwin.com/pipermail/cygwin/2024-December/256841.html
Fixes: 72770148ad ("Cygwin: pty: Prevent pty from changing code page of parent console.")
Reported-by: Steven Buehler <buehlersj@outlook.com>
Signed-off-by: Takashi Yano <takashi.yano@nifty.ne.jp>
This commit is contained in:
Takashi Yano 2024-12-04 13:00:39 +09:00
parent d636b11d2e
commit 1a49c17840
2 changed files with 15 additions and 2 deletions

View file

@ -870,8 +870,18 @@ fhandler_termios::get_console_process_id (DWORD pid, bool match,
DWORD *list = (DWORD *) tp.c_get ();
const DWORD buf_size = NT_MAX_PATH / sizeof (DWORD);
DWORD num = GetConsoleProcessList (list, buf_size);
if (num == 0 || num > buf_size)
DWORD buf_size1 = 1;
DWORD num;
/* The buffer of too large size does not seem to be expected by new condrv.
https://github.com/microsoft/terminal/issues/18264#issuecomment-2515448548
Use the minimum buffer size in the loop. */
while ((num = GetConsoleProcessList (list, buf_size1)) > buf_size1)
{
if (num > buf_size)
return 0;
buf_size1 = num;
}
if (num == 0)
return 0;
DWORD res_pri = 0, res = 0;

View file

@ -48,3 +48,6 @@ Fixes:
- sched_setscheduler(2) allows to change the priority if the policy is
equal to the value returned by sched_getscheduler(2).
- Fix frequent page fault caused in Windows Terminal.
Addresses: https://cygwin.com/pipermail/cygwin/2024-December/256841.html