|
| 1 | +From a86d33ef296849d7ef2e954e1f296236d2c2a5c3 Mon Sep 17 00:00:00 2001 |
| 2 | +From: Takashi Yano < [email protected]> |
| 3 | +Date: Wed, 7 May 2025 15:40:22 +0900 |
| 4 | +Subject: [PATCH 51/N] Cygwin: console: Store console mode only when console |
| 5 | + is opened |
| 6 | + |
| 7 | +... and restore it when app exits. The commit 0bfd91d57863 has a bug |
| 8 | +that the console mode is stored into the shared memory when both: |
| 9 | + (1) cygwin process is started from non-cygwin process. |
| 10 | + (2) cygwin process started from non-cygwin process exits. |
| 11 | +(1) is intended, but (2) is not. Due to (2), the stored console mode |
| 12 | +is unexpectedly broken when the cygwin process exits. Then the mode |
| 13 | +restored will be not as expected. This causes undesired console mode |
| 14 | +in the use case that cygwin and non-cygwin apps are mixed. |
| 15 | + |
| 16 | +With this patch, the console mode will stored only in the case (1). |
| 17 | +This is done by putting the code, which stores the console mode, into |
| 18 | +fhandler_console::open() rather than fhandler_console::set_input_mode() |
| 19 | +and fhandler_console::set_output_mode(). |
| 20 | + |
| 21 | +Fixes: 0bfd91d57863 ("Cygwin: console: tty::restore really restores the previous mode") |
| 22 | +Reported-by: Johannes Schindelin < [email protected]> |
| 23 | +Signed-off-by: Takashi Yano < [email protected]> |
| 24 | +(cherry picked from commit 09ae9f6ee99e2b58dcb17af563f15e8026b773ae) |
| 25 | +--- |
| 26 | + winsup/cygwin/fhandler/console.cc | 33 ++++++++++++++++++++----------- |
| 27 | + 1 file changed, 22 insertions(+), 11 deletions(-) |
| 28 | + |
| 29 | +diff --git a/winsup/cygwin/fhandler/console.cc b/winsup/cygwin/fhandler/console.cc |
| 30 | +index f33e354..2a224fe 100644 |
| 31 | +--- a/winsup/cygwin/fhandler/console.cc |
| 32 | ++++ b/winsup/cygwin/fhandler/console.cc |
| 33 | +@@ -771,6 +771,8 @@ fhandler_console::setup () |
| 34 | + con.disable_master_thread = true; |
| 35 | + con.master_thread_suspended = false; |
| 36 | + con.num_processed = 0; |
| 37 | ++ con.curr_input_mode = tty::restore; |
| 38 | ++ con.curr_output_mode = tty::restore; |
| 39 | + } |
| 40 | + } |
| 41 | + |
| 42 | +@@ -849,11 +851,6 @@ fhandler_console::set_input_mode (tty::cons_mode m, const termios *t, |
| 43 | + flags |= ENABLE_PROCESSED_INPUT; |
| 44 | + break; |
| 45 | + } |
| 46 | +- if (con.curr_input_mode != tty::cygwin && m == tty::cygwin) |
| 47 | +- { |
| 48 | +- prev_input_mode_backup = con.prev_input_mode; |
| 49 | +- con.prev_input_mode = oflags; |
| 50 | +- } |
| 51 | + con.curr_input_mode = m; |
| 52 | + SetConsoleMode (p->input_handle, flags); |
| 53 | + if (!(oflags & ENABLE_VIRTUAL_TERMINAL_INPUT) |
| 54 | +@@ -893,11 +890,6 @@ fhandler_console::set_output_mode (tty::cons_mode m, const termios *t, |
| 55 | + flags |= DISABLE_NEWLINE_AUTO_RETURN; |
| 56 | + break; |
| 57 | + } |
| 58 | +- if (con.curr_output_mode != tty::cygwin && m == tty::cygwin) |
| 59 | +- { |
| 60 | +- prev_output_mode_backup = con.prev_output_mode; |
| 61 | +- GetConsoleMode (p->output_handle, &con.prev_output_mode); |
| 62 | +- } |
| 63 | + con.curr_output_mode = m; |
| 64 | + acquire_attach_mutex (mutex_timeout); |
| 65 | + DWORD resume_pid = attach_console (con.owner); |
| 66 | +@@ -1836,6 +1828,12 @@ fhandler_console::open (int flags, mode_t) |
| 67 | + handle_set.output_handle = h; |
| 68 | + release_output_mutex (); |
| 69 | + |
| 70 | ++ if (con.owner == GetCurrentProcessId ()) |
| 71 | ++ { |
| 72 | ++ GetConsoleMode (get_handle (), &con.prev_input_mode); |
| 73 | ++ GetConsoleMode (get_output_handle (), &con.prev_output_mode); |
| 74 | ++ } |
| 75 | ++ |
| 76 | + wpbuf.init (); |
| 77 | + |
| 78 | + handle_set.input_mutex = input_mutex; |
| 79 | +@@ -1881,6 +1879,19 @@ fhandler_console::open (int flags, mode_t) |
| 80 | + setenv ("TERM", "cygwin", 1); |
| 81 | + } |
| 82 | + |
| 83 | ++ if (con.curr_input_mode != tty::cygwin) |
| 84 | ++ { |
| 85 | ++ prev_input_mode_backup = con.prev_input_mode; |
| 86 | ++ GetConsoleMode (get_handle (), &con.prev_input_mode); |
| 87 | ++ set_input_mode (tty::cygwin, &get_ttyp ()->ti, &handle_set); |
| 88 | ++ } |
| 89 | ++ if (con.curr_output_mode != tty::cygwin) |
| 90 | ++ { |
| 91 | ++ prev_output_mode_backup = con.prev_output_mode; |
| 92 | ++ GetConsoleMode (get_output_handle (), &con.prev_output_mode); |
| 93 | ++ set_output_mode (tty::cygwin, &get_ttyp ()->ti, &handle_set); |
| 94 | ++ } |
| 95 | ++ |
| 96 | + debug_printf ("opened conin$ %p, conout$ %p", get_handle (), |
| 97 | + get_output_handle ()); |
| 98 | + |
| 99 | +@@ -4720,7 +4731,7 @@ fhandler_console::cons_mode_on_close (handle_set_t *p) |
| 100 | + NTSTATUS status = |
| 101 | + NtQueryInformationProcess (GetCurrentProcess (), ProcessBasicInformation, |
| 102 | + &pbi, sizeof (pbi), NULL); |
| 103 | +- if (NT_SUCCESS (status) |
| 104 | ++ if (NT_SUCCESS (status) && cygwin_pid (con.owner) |
| 105 | + && !process_alive ((DWORD) pbi.InheritedFromUniqueProcessId)) |
| 106 | + /* Execed from normal cygwin process and the parent has been exited. */ |
| 107 | + return tty::cygwin; |
0 commit comments