Cygwin: pty: Fix the issue regarding open and close multiple PTYs.

- If two PTYs are opened in the same process and the first one
  is closed, the helper process for the first PTY remains running.
  This patch fixes the issue.
This commit is contained in:
Takashi Yano 2020-01-14 13:10:54 +09:00 committed by Ken Brown
parent a43d4d5f42
commit 452e91a8fa
1 changed files with 11 additions and 4 deletions

View File

@ -2224,8 +2224,7 @@ fhandler_pty_master::close ()
termios_printf ("CloseHandle (output_mutex<%p>), %E", output_mutex); termios_printf ("CloseHandle (output_mutex<%p>), %E", output_mutex);
if (!NT_SUCCESS (status)) if (!NT_SUCCESS (status))
debug_printf ("NtQueryObject: %y", status); debug_printf ("NtQueryObject: %y", status);
else if (obi.HandleCount == (get_pseudo_console () ? 2 : 1)) else if (obi.HandleCount == 1)
/* Helper process has inherited one. */
{ {
termios_printf ("Closing last master of pty%d", get_minor ()); termios_printf ("Closing last master of pty%d", get_minor ());
/* Close Pseudo Console */ /* Close Pseudo Console */
@ -3167,14 +3166,14 @@ fhandler_pty_master::setup_pseudoconsole ()
get_ttyp ()->attach_pcon_in_fork = true; get_ttyp ()->attach_pcon_in_fork = true;
SIZE_T bytesRequired; SIZE_T bytesRequired;
InitializeProcThreadAttributeList (NULL, 1, 0, &bytesRequired); InitializeProcThreadAttributeList (NULL, 2, 0, &bytesRequired);
STARTUPINFOEXW si_helper; STARTUPINFOEXW si_helper;
ZeroMemory (&si_helper, sizeof (si_helper)); ZeroMemory (&si_helper, sizeof (si_helper));
si_helper.StartupInfo.cb = sizeof (STARTUPINFOEXW); si_helper.StartupInfo.cb = sizeof (STARTUPINFOEXW);
si_helper.lpAttributeList = (PPROC_THREAD_ATTRIBUTE_LIST) si_helper.lpAttributeList = (PPROC_THREAD_ATTRIBUTE_LIST)
HeapAlloc (GetProcessHeap (), 0, bytesRequired); HeapAlloc (GetProcessHeap (), 0, bytesRequired);
InitializeProcThreadAttributeList (si_helper.lpAttributeList, InitializeProcThreadAttributeList (si_helper.lpAttributeList,
1, 0, &bytesRequired); 2, 0, &bytesRequired);
UpdateProcThreadAttribute (si_helper.lpAttributeList, UpdateProcThreadAttribute (si_helper.lpAttributeList,
0, 0,
PROC_THREAD_ATTRIBUTE_PSEUDOCONSOLE, PROC_THREAD_ATTRIBUTE_PSEUDOCONSOLE,
@ -3186,6 +3185,14 @@ fhandler_pty_master::setup_pseudoconsole ()
/* Create a pipe for receiving pseudo console handles */ /* Create a pipe for receiving pseudo console handles */
HANDLE hr, hw; HANDLE hr, hw;
CreatePipe (&hr, &hw, &sec_none, 0); CreatePipe (&hr, &hw, &sec_none, 0);
/* Inherit only handles which are needed by helper. */
HANDLE handles_to_inherit[] = {hello, goodbye, hw};
UpdateProcThreadAttribute (si_helper.lpAttributeList,
0,
PROC_THREAD_ATTRIBUTE_HANDLE_LIST,
handles_to_inherit,
sizeof (handles_to_inherit),
NULL, NULL);
/* Create helper process */ /* Create helper process */
WCHAR cmd[MAX_PATH]; WCHAR cmd[MAX_PATH];
path_conv helper ("/bin/cygwin-console-helper.exe"); path_conv helper ("/bin/cygwin-console-helper.exe");