Skip to content

Commit a86d33e

Browse files
tyan0dscho
authored andcommitted
Cygwin: console: Store console mode only when console is opened
... and restore it when app exits. The commit 0bfd91d has a bug that the console mode is stored into the shared memory when both: (1) cygwin process is started from non-cygwin process. (2) cygwin process started from non-cygwin process exits. (1) is intended, but (2) is not. Due to (2), the stored console mode is unexpectedly broken when the cygwin process exits. Then the mode restored will be not as expected. This causes undesired console mode in the use case that cygwin and non-cygwin apps are mixed. With this patch, the console mode will stored only in the case (1). This is done by putting the code, which stores the console mode, into fhandler_console::open() rather than fhandler_console::set_input_mode() and fhandler_console::set_output_mode(). Fixes: 0bfd91d ("Cygwin: console: tty::restore really restores the previous mode") Reported-by: Johannes Schindelin <[email protected]> Signed-off-by: Takashi Yano <[email protected]> (cherry picked from commit 09ae9f6)
1 parent 3819160 commit a86d33e

File tree

1 file changed

+22
-11
lines changed

1 file changed

+22
-11
lines changed

winsup/cygwin/fhandler/console.cc

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -771,6 +771,8 @@ fhandler_console::setup ()
771771
con.disable_master_thread = true;
772772
con.master_thread_suspended = false;
773773
con.num_processed = 0;
774+
con.curr_input_mode = tty::restore;
775+
con.curr_output_mode = tty::restore;
774776
}
775777
}
776778

@@ -849,11 +851,6 @@ fhandler_console::set_input_mode (tty::cons_mode m, const termios *t,
849851
flags |= ENABLE_PROCESSED_INPUT;
850852
break;
851853
}
852-
if (con.curr_input_mode != tty::cygwin && m == tty::cygwin)
853-
{
854-
prev_input_mode_backup = con.prev_input_mode;
855-
con.prev_input_mode = oflags;
856-
}
857854
con.curr_input_mode = m;
858855
SetConsoleMode (p->input_handle, flags);
859856
if (!(oflags & ENABLE_VIRTUAL_TERMINAL_INPUT)
@@ -893,11 +890,6 @@ fhandler_console::set_output_mode (tty::cons_mode m, const termios *t,
893890
flags |= DISABLE_NEWLINE_AUTO_RETURN;
894891
break;
895892
}
896-
if (con.curr_output_mode != tty::cygwin && m == tty::cygwin)
897-
{
898-
prev_output_mode_backup = con.prev_output_mode;
899-
GetConsoleMode (p->output_handle, &con.prev_output_mode);
900-
}
901893
con.curr_output_mode = m;
902894
acquire_attach_mutex (mutex_timeout);
903895
DWORD resume_pid = attach_console (con.owner);
@@ -1836,6 +1828,12 @@ fhandler_console::open (int flags, mode_t)
18361828
handle_set.output_handle = h;
18371829
release_output_mutex ();
18381830

1831+
if (con.owner == GetCurrentProcessId ())
1832+
{
1833+
GetConsoleMode (get_handle (), &con.prev_input_mode);
1834+
GetConsoleMode (get_output_handle (), &con.prev_output_mode);
1835+
}
1836+
18391837
wpbuf.init ();
18401838

18411839
handle_set.input_mutex = input_mutex;
@@ -1881,6 +1879,19 @@ fhandler_console::open (int flags, mode_t)
18811879
setenv ("TERM", "cygwin", 1);
18821880
}
18831881

1882+
if (con.curr_input_mode != tty::cygwin)
1883+
{
1884+
prev_input_mode_backup = con.prev_input_mode;
1885+
GetConsoleMode (get_handle (), &con.prev_input_mode);
1886+
set_input_mode (tty::cygwin, &get_ttyp ()->ti, &handle_set);
1887+
}
1888+
if (con.curr_output_mode != tty::cygwin)
1889+
{
1890+
prev_output_mode_backup = con.prev_output_mode;
1891+
GetConsoleMode (get_output_handle (), &con.prev_output_mode);
1892+
set_output_mode (tty::cygwin, &get_ttyp ()->ti, &handle_set);
1893+
}
1894+
18841895
debug_printf ("opened conin$ %p, conout$ %p", get_handle (),
18851896
get_output_handle ());
18861897

@@ -4720,7 +4731,7 @@ fhandler_console::cons_mode_on_close (handle_set_t *p)
47204731
NTSTATUS status =
47214732
NtQueryInformationProcess (GetCurrentProcess (), ProcessBasicInformation,
47224733
&pbi, sizeof (pbi), NULL);
4723-
if (NT_SUCCESS (status)
4734+
if (NT_SUCCESS (status) && cygwin_pid (con.owner)
47244735
&& !process_alive ((DWORD) pbi.InheritedFromUniqueProcessId))
47254736
/* Execed from normal cygwin process and the parent has been exited. */
47264737
return tty::cygwin;

0 commit comments

Comments
 (0)