[newlib-cygwin] Cygwin: console: Fix the console states after the console is closed

Takashi Yano via Cygwin-cvs <[email protected]>
Newsgroups gmane.os.cygwin.cvs
Message-ID <[email protected]>
https://sourceware.org/git/gitweb.cgi?p=newlib-cygwin.git;h=dc6aa87761e2720181c27b06b537e9e9c995c293

commit dc6aa87761e2720181c27b06b537e9e9c995c293
Author: Takashi Yano <[email protected]>
Date:   Thu Apr 10 16:18:17 2025 +0900

    Cygwin: console: Fix the console states after the console is closed
    
    Due to a bug introduced by the commit 3312f2d21f13, when the parent
    process exits before the child process exits, disable_master_thread
    is wrongly set to true, that disables special key handling such as
    Ctrl-C. With this patch, the disable_master_thread is set to true if
    any of the following conditions is met.
      - The parent process is not a cygwin process.
      - The master process already died.
      - The current process is the master process.
    Otherwise, disable_master_thread remains false to keep special key
    handling enabled.
    
    Addresses: https://cygwin.com/pipermail/cygwin/2025-April/257909.html
    Fixed: 3312f2d21f13 ("Cygwin: console: Redesign mode set strategy on close().")
    Reported-by: Jeremy Drake <[email protected]>
    Signed-off-by: Takashi Yano <[email protected]>

Diff:
---
 winsup/cygwin/fhandler/console.cc       | 24 ++++++++++++++++++++----
 winsup/cygwin/local_includes/fhandler.h |  2 +-
 winsup/cygwin/release/3.6.2             |  3 +++
 3 files changed, 24 insertions(+), 5 deletions(-)

diff --git a/winsup/cygwin/fhandler/console.cc b/winsup/cygwin/fhandler/console.cc
index c61364391..2e19e0dd7 100644
--- a/winsup/cygwin/fhandler/console.cc
+++ b/winsup/cygwin/fhandler/console.cc
@@ -932,7 +932,7 @@ fhandler_console::cleanup_for_non_cygwin_app (handle_set_t *p)
   /* Cleaning-up console mode for non-cygwin app. */
   /* conmode can be tty::restore when non-cygwin app is
      exec'ed from login shell. */
-  tty::cons_mode conmode = cons_mode_on_close ();
+  tty::cons_mode conmode = cons_mode_on_close (p);
   set_output_mode (conmode, ti, p);
   set_input_mode (conmode, ti, p);
   set_disable_master_thread (con.owner == GetCurrentProcessId ());
@@ -2000,8 +2000,9 @@ fhandler_console::close (int flag)
 
   acquire_output_mutex (mutex_timeout);
 
-  if (shared_console_info[unit] && myself->ppid == 1
-      && (dev_t) myself->ctty == get_device ())
+  if (shared_console_info[unit] && con.curr_input_mode != tty::restore
+      && (dev_t) myself->ctty == get_device ()
+      && cons_mode_on_close (&handle_set) == tty::restore)
     {
       set_output_mode (tty::restore, &get_ttyp ()->ti, &handle_set);
       set_input_mode (tty::restore, &get_ttyp ()->ti, &handle_set);
@@ -4722,11 +4723,26 @@ fhandler_console::fstat (struct stat *st)
 }
 
 tty::cons_mode
-fhandler_console::cons_mode_on_close ()
+fhandler_console::cons_mode_on_close (handle_set_t *p)
 {
+  int unit = p->unit;
   if (myself->ppid != 1) /* Execed from normal cygwin process. */
     return tty::cygwin;
 
+  if (!process_alive (con.owner)) /* The Master process already died. */
+    return tty::restore;
+  if (con.owner == GetCurrentProcessId ()) /* Master process */
+    return tty::restore;
+
+  PROCESS_BASIC_INFORMATION pbi;
+  NTSTATUS status =
+    NtQueryInformationProcess (GetCurrentProcess (), ProcessBasicInformation,
+			       &pbi, sizeof (pbi), NULL);
+  if (NT_SUCCESS (status)
+      && !process_alive ((DWORD) pbi.InheritedFromUniqueProcessId))
+    /* Execed from normal cygwin process and the parent has been exited. */
+    return tty::cygwin;
+
   return tty::restore; /* otherwise, restore */
 }
 
diff --git a/winsup/cygwin/local_includes/fhandler.h b/winsup/cygwin/local_includes/fhandler.h
index 2177cec87..03f20c8fd 100644
--- a/winsup/cygwin/local_includes/fhandler.h
+++ b/winsup/cygwin/local_includes/fhandler.h
@@ -2370,7 +2370,7 @@ private:
 
   void setup_pcon_hand_over ();
   static void pcon_hand_over_proc ();
-  static tty::cons_mode cons_mode_on_close ();
+  static tty::cons_mode cons_mode_on_close (handle_set_t *);
 
   friend tty_min * tty_list::get_cttyp ();
 };
diff --git a/winsup/cygwin/release/3.6.2 b/winsup/cygwin/release/3.6.2
index ef56a74ff..f10a947cd 100644
--- a/winsup/cygwin/release/3.6.2
+++ b/winsup/cygwin/release/3.6.2
@@ -7,3 +7,6 @@ Fixes:
 
 - Fix connect(2) returning WSAEPROTOTYPE on abstract sockets.
   Addresses: https://sourceware.org/pipermail/cygwin-patches/2025q2/013638.html
+
+- Fix the console states after the console is closed.
+  Addresses: https://cygwin.com/pipermail/cygwin/2025-April/257909.html
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.