Skip to content

Commit c1770e1

Browse files
tyan0dscho
authored andcommitted
Cygwin: console: tty::restore really restores the previous mode
Previously, tty::restore sets the console mode to a predetermined console mode that is widely common for many non-cygwin console apps. So, if a non-cygwin app that is started from cygwin process changes the console mode and executes cygwin sub-process, the console mode is changed to the predetermined mode rather than being restored the original mode that is set by the non-cygwin app. With this patch, the console mode is stored when a cygwin process is started from non-cygwin app, then tty::restore restores the previous console mode that is used by the previous non-cygwin app when the cygwin app exits. Addresses: msys2/msys2-runtime#268 Fixes: 3312f2d ("Cygwin: console: Redesign mode set strategy on close().") Reported-by: Eu Pin Tien, Jeremy Drake <[email protected]> Signed-off-by: Takashi Yano <[email protected]> Signed-off-by: Eu-Pin Tien <[email protected]> Signed-off-by: Johannes Schindelin <[email protected]>
1 parent a937131 commit c1770e1

File tree

3 files changed

+32
-26
lines changed

3 files changed

+32
-26
lines changed

winsup/cygwin/fhandler/console.cc

Lines changed: 24 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -804,6 +804,9 @@ fhandler_console::rabuflen ()
804804
return con_ra.rabuflen;
805805
}
806806

807+
static DWORD prev_input_mode_backup;
808+
static DWORD prev_output_mode_backup;
809+
807810
/* The function set_{in,out}put_mode() should be static so that they
808811
can be called even after the fhandler_console instance is deleted. */
809812
void
@@ -818,11 +821,11 @@ fhandler_console::set_input_mode (tty::cons_mode m, const termios *t,
818821
GetConsoleMode (p->input_handle, &oflags);
819822
DWORD flags = oflags
820823
& (ENABLE_EXTENDED_FLAGS | ENABLE_INSERT_MODE | ENABLE_QUICK_EDIT_MODE);
821-
con.curr_input_mode = m;
822824
switch (m)
823825
{
824826
case tty::restore:
825-
flags |= ENABLE_ECHO_INPUT | ENABLE_LINE_INPUT | ENABLE_PROCESSED_INPUT;
827+
flags = con.prev_input_mode;
828+
con.prev_input_mode = prev_input_mode_backup;
826829
break;
827830
case tty::cygwin:
828831
flags |= ENABLE_WINDOW_INPUT;
@@ -846,6 +849,12 @@ fhandler_console::set_input_mode (tty::cons_mode m, const termios *t,
846849
flags |= ENABLE_PROCESSED_INPUT;
847850
break;
848851
}
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+
}
857+
con.curr_input_mode = m;
849858
SetConsoleMode (p->input_handle, flags);
850859
if (!(oflags & ENABLE_VIRTUAL_TERMINAL_INPUT)
851860
&& (flags & ENABLE_VIRTUAL_TERMINAL_INPUT)
@@ -868,10 +877,11 @@ fhandler_console::set_output_mode (tty::cons_mode m, const termios *t,
868877
if (con.orig_virtual_terminal_processing_mode)
869878
flags |= ENABLE_VIRTUAL_TERMINAL_PROCESSING;
870879
WaitForSingleObject (p->output_mutex, mutex_timeout);
871-
con.curr_output_mode = m;
872880
switch (m)
873881
{
874882
case tty::restore:
883+
flags = con.prev_output_mode;
884+
con.prev_output_mode = prev_output_mode_backup;
875885
break;
876886
case tty::cygwin:
877887
if (wincap.has_con_24bit_colors () && !con_is_legacy)
@@ -883,6 +893,12 @@ fhandler_console::set_output_mode (tty::cons_mode m, const termios *t,
883893
flags |= DISABLE_NEWLINE_AUTO_RETURN;
884894
break;
885895
}
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+
}
901+
con.curr_output_mode = m;
886902
acquire_attach_mutex (mutex_timeout);
887903
DWORD resume_pid = attach_console (con.owner);
888904
SetConsoleMode (p->output_handle, flags);
@@ -916,7 +932,7 @@ fhandler_console::cleanup_for_non_cygwin_app (handle_set_t *p)
916932
/* Cleaning-up console mode for non-cygwin app. */
917933
/* conmode can be tty::restore when non-cygwin app is
918934
exec'ed from login shell. */
919-
tty::cons_mode conmode = cons_mode_on_close (p);
935+
tty::cons_mode conmode = cons_mode_on_close ();
920936
set_output_mode (conmode, ti, p);
921937
set_input_mode (conmode, ti, p);
922938
set_disable_master_thread (con.owner == GetCurrentProcessId ());
@@ -1978,9 +1994,8 @@ fhandler_console::close (int flag)
19781994
if (shared_console_info[unit] && myself->ppid == 1
19791995
&& (dev_t) myself->ctty == get_device ())
19801996
{
1981-
tty::cons_mode conmode = cons_mode_on_close (&handle_set);
1982-
set_output_mode (conmode, &get_ttyp ()->ti, &handle_set);
1983-
set_input_mode (conmode, &get_ttyp ()->ti, &handle_set);
1997+
set_output_mode (tty::restore, &get_ttyp ()->ti, &handle_set);
1998+
set_input_mode (tty::restore, &get_ttyp ()->ti, &handle_set);
19841999
set_disable_master_thread (true, this);
19852000
}
19862001

@@ -4687,26 +4702,10 @@ fhandler_console::fstat (struct stat *st)
46874702
}
46884703

46894704
tty::cons_mode
4690-
fhandler_console::cons_mode_on_close (handle_set_t *p)
4705+
fhandler_console::cons_mode_on_close ()
46914706
{
4692-
const _minor_t unit = p->unit;
4693-
46944707
if (myself->ppid != 1) /* Execed from normal cygwin process. */
46954708
return tty::cygwin;
46964709

4697-
if (!process_alive (con.owner)) /* The Master process already died. */
4698-
return tty::restore;
4699-
if (con.owner == GetCurrentProcessId ()) /* Master process */
4700-
return tty::restore;
4701-
4702-
PROCESS_BASIC_INFORMATION pbi;
4703-
NTSTATUS status =
4704-
NtQueryInformationProcess (GetCurrentProcess (), ProcessBasicInformation,
4705-
&pbi, sizeof (pbi), NULL);
4706-
if (NT_SUCCESS (status)
4707-
&& con.owner == (DWORD) pbi.InheritedFromUniqueProcessId)
4708-
/* The parent is the stub process. */
4709-
return tty::restore;
4710-
4711-
return tty::native; /* Otherwise */
4710+
return tty::restore; /* otherwise, restore */
47124711
}

winsup/cygwin/local_includes/fhandler.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2146,6 +2146,8 @@ class dev_console
21462146
bool disable_master_thread;
21472147
tty::cons_mode curr_input_mode;
21482148
tty::cons_mode curr_output_mode;
2149+
DWORD prev_input_mode;
2150+
DWORD prev_output_mode;
21492151
bool master_thread_suspended;
21502152
int num_processed; /* Number of input events in the current input buffer
21512153
already processed by cons_master_thread(). */
@@ -2366,7 +2368,7 @@ class fhandler_console: public fhandler_termios
23662368

23672369
void setup_pcon_hand_over ();
23682370
static void pcon_hand_over_proc ();
2369-
static tty::cons_mode cons_mode_on_close (handle_set_t *);
2371+
static tty::cons_mode cons_mode_on_close ();
23702372

23712373
friend tty_min * tty_list::get_cttyp ();
23722374
};

winsup/cygwin/release/3.6.1

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
Fixes:
2+
------
3+
4+
- Console mode is really restored to the previous mode.
5+
Addresses: https://github.com/msys2/msys2-runtime/issues/268

0 commit comments

Comments
 (0)