|
| 1 | +From 8ed79cf850b602a8fe6432027145f871f04d5690 Mon Sep 17 00:00:00 2001 |
| 2 | +From: Takashi Yano < [email protected]> |
| 3 | +Date: Thu, 10 Apr 2025 16:18:17 +0900 |
| 4 | +Subject: [PATCH 48/N] Cygwin: console: Fix the console states after the |
| 5 | + console is closed |
| 6 | + |
| 7 | +Due to a bug introduced by the commit 3312f2d21f13, when the parent |
| 8 | +process exits before the child process exits, disable_master_thread |
| 9 | +is wrongly set to true, that disables special key handling such as |
| 10 | +Ctrl-C. With this patch, the disable_master_thread is set to true if |
| 11 | +any of the following conditions is met. |
| 12 | + - The parent process is not a cygwin process. |
| 13 | + - The master process already died. |
| 14 | + - The current process is the master process. |
| 15 | +Otherwise, disable_master_thread remains false to keep special key |
| 16 | +handling enabled. |
| 17 | + |
| 18 | +Addresses: https://cygwin.com/pipermail/cygwin/2025-April/257909.html |
| 19 | +Fixed: 3312f2d21f13 ("Cygwin: console: Redesign mode set strategy on close().") |
| 20 | +Reported-by: Jeremy Drake < [email protected]> |
| 21 | +Signed-off-by: Takashi Yano < [email protected]> |
| 22 | +--- |
| 23 | + winsup/cygwin/fhandler/console.cc | 24 ++++++++++++++++++++---- |
| 24 | + winsup/cygwin/local_includes/fhandler.h | 2 +- |
| 25 | + 2 files changed, 21 insertions(+), 5 deletions(-) |
| 26 | + |
| 27 | +diff --git a/winsup/cygwin/fhandler/console.cc b/winsup/cygwin/fhandler/console.cc |
| 28 | +index a38487f..f33e354 100644 |
| 29 | +--- a/winsup/cygwin/fhandler/console.cc |
| 30 | ++++ b/winsup/cygwin/fhandler/console.cc |
| 31 | +@@ -932,7 +932,7 @@ fhandler_console::cleanup_for_non_cygwin_app (handle_set_t *p) |
| 32 | + /* Cleaning-up console mode for non-cygwin app. */ |
| 33 | + /* conmode can be tty::restore when non-cygwin app is |
| 34 | + exec'ed from login shell. */ |
| 35 | +- tty::cons_mode conmode = cons_mode_on_close (); |
| 36 | ++ tty::cons_mode conmode = cons_mode_on_close (p); |
| 37 | + set_output_mode (conmode, ti, p); |
| 38 | + set_input_mode (conmode, ti, p); |
| 39 | + set_disable_master_thread (con.owner == GetCurrentProcessId ()); |
| 40 | +@@ -1991,8 +1991,9 @@ fhandler_console::close (int flag) |
| 41 | + |
| 42 | + acquire_output_mutex (mutex_timeout); |
| 43 | + |
| 44 | +- if (shared_console_info[unit] && myself->ppid == 1 |
| 45 | +- && (dev_t) myself->ctty == get_device ()) |
| 46 | ++ if (shared_console_info[unit] && con.curr_input_mode != tty::restore |
| 47 | ++ && (dev_t) myself->ctty == get_device () |
| 48 | ++ && cons_mode_on_close (&handle_set) == tty::restore) |
| 49 | + { |
| 50 | + set_output_mode (tty::restore, &get_ttyp ()->ti, &handle_set); |
| 51 | + set_input_mode (tty::restore, &get_ttyp ()->ti, &handle_set); |
| 52 | +@@ -4704,10 +4705,25 @@ fhandler_console::fstat (struct stat *st) |
| 53 | + } |
| 54 | + |
| 55 | + tty::cons_mode |
| 56 | +-fhandler_console::cons_mode_on_close () |
| 57 | ++fhandler_console::cons_mode_on_close (handle_set_t *p) |
| 58 | + { |
| 59 | ++ int unit = p->unit; |
| 60 | + if (myself->ppid != 1) /* Execed from normal cygwin process. */ |
| 61 | + return tty::cygwin; |
| 62 | + |
| 63 | ++ if (!process_alive (con.owner)) /* The Master process already died. */ |
| 64 | ++ return tty::restore; |
| 65 | ++ if (con.owner == GetCurrentProcessId ()) /* Master process */ |
| 66 | ++ return tty::restore; |
| 67 | ++ |
| 68 | ++ PROCESS_BASIC_INFORMATION pbi; |
| 69 | ++ NTSTATUS status = |
| 70 | ++ NtQueryInformationProcess (GetCurrentProcess (), ProcessBasicInformation, |
| 71 | ++ &pbi, sizeof (pbi), NULL); |
| 72 | ++ if (NT_SUCCESS (status) |
| 73 | ++ && !process_alive ((DWORD) pbi.InheritedFromUniqueProcessId)) |
| 74 | ++ /* Execed from normal cygwin process and the parent has been exited. */ |
| 75 | ++ return tty::cygwin; |
| 76 | ++ |
| 77 | + return tty::restore; /* otherwise, restore */ |
| 78 | + } |
| 79 | +diff --git a/winsup/cygwin/local_includes/fhandler.h b/winsup/cygwin/local_includes/fhandler.h |
| 80 | +index 8c71d84..4c013dd 100644 |
| 81 | +--- a/winsup/cygwin/local_includes/fhandler.h |
| 82 | ++++ b/winsup/cygwin/local_includes/fhandler.h |
| 83 | +@@ -2368,7 +2368,7 @@ private: |
| 84 | + |
| 85 | + void setup_pcon_hand_over (); |
| 86 | + static void pcon_hand_over_proc (); |
| 87 | +- static tty::cons_mode cons_mode_on_close (); |
| 88 | ++ static tty::cons_mode cons_mode_on_close (handle_set_t *); |
| 89 | + |
| 90 | + friend tty_min * tty_list::get_cttyp (); |
| 91 | + }; |
0 commit comments