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

Takashi Yano takashi.yano@nifty.ne.jp
Wed Jan 1 06:48:00 GMT 2020


- 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.
---
 winsup/cygwin/fhandler_tty.cc         |  8 ++++----
 winsup/utils/cygwin-console-helper.cc | 15 ++++++++++++++-
 2 files changed, 18 insertions(+), 5 deletions(-)

diff --git a/winsup/cygwin/fhandler_tty.cc b/winsup/cygwin/fhandler_tty.cc
index f10f0fc61..65b12fd6c 100644
--- a/winsup/cygwin/fhandler_tty.cc
+++ b/winsup/cygwin/fhandler_tty.cc
@@ -2233,8 +2233,7 @@ fhandler_pty_master::close ()
     termios_printf ("CloseHandle (output_mutex<%p>), %E", output_mutex);
   if (!NT_SUCCESS (status))
     debug_printf ("NtQueryObject: %y", status);
-  else if (obi.HandleCount == (get_pseudo_console () ? 2 : 1))
-			      /* Helper process has inherited one. */
+  else if (obi.HandleCount == 1)
     {
       termios_printf ("Closing last master of pty%d", get_minor ());
       /* Close Pseudo Console */
@@ -3202,14 +3201,15 @@ fhandler_pty_master::setup_pseudoconsole ()
   path_conv helper ("/bin/cygwin-console-helper.exe");
   size_t len = helper.get_wide_win32_path_len ();
   helper.get_wide_win32_path (cmd);
-  __small_swprintf (cmd + len, L" %p %p %p", hello, goodbye, hw);
+  __small_swprintf (cmd + len, L" %p %p %p %p",
+		    hello, goodbye, hw, GetCurrentProcessId ());
   si_helper.StartupInfo.dwFlags = STARTF_USESTDHANDLES;
   si_helper.StartupInfo.hStdInput = NULL;
   si_helper.StartupInfo.hStdOutput = NULL;
   si_helper.StartupInfo.hStdError = NULL;
   PROCESS_INFORMATION pi_helper;
   CreateProcessW (NULL, cmd, &sec_none, &sec_none,
-		  TRUE, EXTENDED_STARTUPINFO_PRESENT,
+		  FALSE, EXTENDED_STARTUPINFO_PRESENT,
 		  NULL, NULL, &si_helper.StartupInfo, &pi_helper);
   WaitForSingleObject (hello, INFINITE);
   /* Retrieve pseudo console handles */
diff --git a/winsup/utils/cygwin-console-helper.cc b/winsup/utils/cygwin-console-helper.cc
index 66004bd15..6255fb93d 100644
--- a/winsup/utils/cygwin-console-helper.cc
+++ b/winsup/utils/cygwin-console-helper.cc
@@ -4,14 +4,24 @@ int
 main (int argc, char **argv)
 {
   char *end;
+  HANDLE parent = NULL;
   if (argc < 3)
     exit (1);
+  if (argc == 5)
+    parent = OpenProcess (PROCESS_DUP_HANDLE, FALSE,
+			  strtoull (argv[4], &end, 0));
   HANDLE h = (HANDLE) strtoull (argv[1], &end, 0);
+  if (parent)
+    DuplicateHandle (parent, h, GetCurrentProcess (), &h,
+		     0, FALSE, DUPLICATE_SAME_ACCESS);
   SetEvent (h);
-  if (argc == 4) /* Pseudo console helper mode for PTY */
+  if (argc == 4 || argc == 5) /* Pseudo console helper mode for PTY */
     {
       SetConsoleCtrlHandler (NULL, TRUE);
       HANDLE hPipe = (HANDLE) strtoull (argv[3], &end, 0);
+      if (parent)
+	DuplicateHandle (parent, hPipe, GetCurrentProcess (), &hPipe,
+			 0, FALSE, DUPLICATE_SAME_ACCESS);
       char buf[64];
       sprintf (buf, "StdHandles=%p,%p\n",
 	       GetStdHandle (STD_INPUT_HANDLE),
@@ -21,6 +31,9 @@ main (int argc, char **argv)
       CloseHandle (hPipe);
     }
   h = (HANDLE) strtoull (argv[2], &end, 0);
+  if (parent)
+    DuplicateHandle (parent, h, GetCurrentProcess (), &h,
+		     0, FALSE, DUPLICATE_SAME_ACCESS);
   WaitForSingleObject (h, INFINITE);
   exit (0);
 }
-- 
2.21.0



More information about the Cygwin-patches mailing list