Skip to content

Commit 8ed79cf

Browse files
tyan0jeremyd2019
authored andcommitted
Cygwin: console: Fix the console states after the console is closed
Due to a bug introduced by the commit 3312f2d, 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: 3312f2d ("Cygwin: console: Redesign mode set strategy on close().") Reported-by: Jeremy Drake <[email protected]> Signed-off-by: Takashi Yano <[email protected]>
1 parent b8fee45 commit 8ed79cf

File tree

2 files changed

+21
-5
lines changed

2 files changed

+21
-5
lines changed

winsup/cygwin/fhandler/console.cc

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -932,7 +932,7 @@ fhandler_console::cleanup_for_non_cygwin_app (handle_set_t *p)
932932
/* Cleaning-up console mode for non-cygwin app. */
933933
/* conmode can be tty::restore when non-cygwin app is
934934
exec'ed from login shell. */
935-
tty::cons_mode conmode = cons_mode_on_close ();
935+
tty::cons_mode conmode = cons_mode_on_close (p);
936936
set_output_mode (conmode, ti, p);
937937
set_input_mode (conmode, ti, p);
938938
set_disable_master_thread (con.owner == GetCurrentProcessId ());
@@ -1991,8 +1991,9 @@ fhandler_console::close (int flag)
19911991

19921992
acquire_output_mutex (mutex_timeout);
19931993

1994-
if (shared_console_info[unit] && myself->ppid == 1
1995-
&& (dev_t) myself->ctty == get_device ())
1994+
if (shared_console_info[unit] && con.curr_input_mode != tty::restore
1995+
&& (dev_t) myself->ctty == get_device ()
1996+
&& cons_mode_on_close (&handle_set) == tty::restore)
19961997
{
19971998
set_output_mode (tty::restore, &get_ttyp ()->ti, &handle_set);
19981999
set_input_mode (tty::restore, &get_ttyp ()->ti, &handle_set);
@@ -4704,10 +4705,25 @@ fhandler_console::fstat (struct stat *st)
47044705
}
47054706

47064707
tty::cons_mode
4707-
fhandler_console::cons_mode_on_close ()
4708+
fhandler_console::cons_mode_on_close (handle_set_t *p)
47084709
{
4710+
int unit = p->unit;
47094711
if (myself->ppid != 1) /* Execed from normal cygwin process. */
47104712
return tty::cygwin;
47114713

4714+
if (!process_alive (con.owner)) /* The Master process already died. */
4715+
return tty::restore;
4716+
if (con.owner == GetCurrentProcessId ()) /* Master process */
4717+
return tty::restore;
4718+
4719+
PROCESS_BASIC_INFORMATION pbi;
4720+
NTSTATUS status =
4721+
NtQueryInformationProcess (GetCurrentProcess (), ProcessBasicInformation,
4722+
&pbi, sizeof (pbi), NULL);
4723+
if (NT_SUCCESS (status)
4724+
&& !process_alive ((DWORD) pbi.InheritedFromUniqueProcessId))
4725+
/* Execed from normal cygwin process and the parent has been exited. */
4726+
return tty::cygwin;
4727+
47124728
return tty::restore; /* otherwise, restore */
47134729
}

winsup/cygwin/local_includes/fhandler.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2368,7 +2368,7 @@ class fhandler_console: public fhandler_termios
23682368

23692369
void setup_pcon_hand_over ();
23702370
static void pcon_hand_over_proc ();
2371-
static tty::cons_mode cons_mode_on_close ();
2371+
static tty::cons_mode cons_mode_on_close (handle_set_t *);
23722372

23732373
friend tty_min * tty_list::get_cttyp ();
23742374
};

0 commit comments

Comments
 (0)