public inbox for cygwin-cvs@sourceware.org
help / color / mirror / Atom feed
From: Corinna Vinschen <corinna@sourceware.org>
To: cygwin-cvs@sourceware.org
Subject: [newlib-cygwin] Cygwin: pty: Call FreeConsole() only if attached to current pty.
Date: Tue, 19 May 2020 13:27:55 +0000 (GMT)	[thread overview]
Message-ID: <20200519132755.638053851C0A@sourceware.org> (raw)

https://sourceware.org/git/gitweb.cgi?p=newlib-cygwin.git;h=7659ff0f5afd751f42485f2684c799c5f37b0fb9

commit 7659ff0f5afd751f42485f2684c799c5f37b0fb9
Author: Takashi Yano via Cygwin-patches <cygwin-patches@cygwin.com>
Date:   Tue May 19 19:55:23 2020 +0900

    Cygwin: pty: Call FreeConsole() only if attached to current pty.
    
    - After commit 071b8e0cbd4be33449c12bb0d58f514ed8ef893c, the problem
      reported in https://cygwin.com/pipermail/cygwin/2020-May/244873.html
      occurs. This is due to freeing console device accidentally rather
      than pseudo console. This patch makes sure to call FreeConsole()
      only if the process is attached to the pseudo console of the current
      pty.

Diff:
---
 winsup/cygwin/fhandler.h      |  1 +
 winsup/cygwin/fhandler_tty.cc | 28 +++++++++++++---------------
 2 files changed, 14 insertions(+), 15 deletions(-)

diff --git a/winsup/cygwin/fhandler.h b/winsup/cygwin/fhandler.h
index ae64086df..857f0a4e0 100644
--- a/winsup/cygwin/fhandler.h
+++ b/winsup/cygwin/fhandler.h
@@ -2284,6 +2284,7 @@ class fhandler_pty_slave: public fhandler_pty_common
 
   bool try_reattach_pcon ();
   void restore_reattach_pcon ();
+  inline void free_attached_console ();
 
  public:
   /* Constructor */
diff --git a/winsup/cygwin/fhandler_tty.cc b/winsup/cygwin/fhandler_tty.cc
index 8547ec7c4..5a1bcd3ce 100644
--- a/winsup/cygwin/fhandler_tty.cc
+++ b/winsup/cygwin/fhandler_tty.cc
@@ -180,6 +180,16 @@ set_ishybrid_and_switch_to_pcon (HANDLE h)
     }
 }
 
+inline void
+fhandler_pty_slave::free_attached_console ()
+{
+  if (freeconsole_on_close && get_minor () == pcon_attached_to)
+    {
+      FreeConsole ();
+      pcon_attached_to = -1;
+    }
+}
+
 #define DEF_HOOK(name) static __typeof__ (name) *name##_Orig
 DEF_HOOK (WriteFile);
 DEF_HOOK (WriteConsoleA);
@@ -708,11 +718,7 @@ fhandler_pty_slave::~fhandler_pty_slave ()
   if (!get_ttyp ())
     {
       /* Why comes here? Who clears _tc? */
-      if (freeconsole_on_close)
-	{
-	  FreeConsole ();
-	  pcon_attached_to = -1;
-	}
+      free_attached_console ();
       return;
     }
   if (get_pseudo_console ())
@@ -739,11 +745,7 @@ fhandler_pty_slave::~fhandler_pty_slave ()
       if (used == 0)
 	{
 	  init_console_handler (false);
-	  if (freeconsole_on_close)
-	    {
-	      FreeConsole ();
-	      pcon_attached_to = -1;
-	    }
+	  free_attached_console ();
 	}
     }
 }
@@ -3006,11 +3008,7 @@ fhandler_pty_slave::fixup_after_exec ()
       if (used == 1 /* About to close this tty */)
 	{
 	  init_console_handler (false);
-	  if (freeconsole_on_close)
-	    {
-	      FreeConsole ();
-	      pcon_attached_to = -1;
-	    }
+	  free_attached_console ();
 	}
     }


                 reply	other threads:[~2020-05-19 13:27 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20200519132755.638053851C0A@sourceware.org \
    --to=corinna@sourceware.org \
    --cc=cygwin-cvs@sourceware.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).