Skip to content

Commit 463ebcd

Browse files
tyan0dscho
authored andcommitted
Cygwin: console: Redesign mode set strategy on close().
The required console mode for a non-cygwin process is different from that for a cygwin process. There are currently three modes: tty::cygwin, tty::native, and tty::restore. The latter two are for the non-cygwin processes. tty::restore is the mode for the non-cygwin processes that started the cygwin process, used to restore the previous behaviour. tty::native is the mode that reflects some terminfo flags. The issue below is caused because the console mode fails to be restored to the previous console mode used by cmd.exe. This patch redesigns the strategy to determine which mode should be set on console close() to fix all similar problems. Previously, the number of handle count is used to determine the appropriate console mode. However, the handle count seems uncertain for that purpose. In the new design, the relation ship between the master process and the process that is about to close the console is mainly used. This can provide more certain result than previous one. Addresses: microsoft/git#730 Backported-from: 3312f2d (Cygwin: console: Redesign mode set strategy on close()., 2025-03-03) Fixes: 30d2669 ("Cygwin: console: Fix clean up conditions in close()") Reported-by: Mike Marcelais, Johannes Schindelin <[email protected]> Signed-off-by: Takashi Yano <[email protected]> Signed-off-by: Johannes Schindelin <[email protected]>
1 parent 052d66b commit 463ebcd

File tree

2 files changed

+32
-16
lines changed

2 files changed

+32
-16
lines changed

winsup/cygwin/fhandler/console.cc

Lines changed: 31 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -916,8 +916,7 @@ fhandler_console::cleanup_for_non_cygwin_app (handle_set_t *p)
916916
/* Cleaning-up console mode for non-cygwin app. */
917917
/* conmode can be tty::restore when non-cygwin app is
918918
exec'ed from login shell. */
919-
tty::cons_mode conmode =
920-
(con.owner == GetCurrentProcessId ()) ? tty::restore : tty::cygwin;
919+
tty::cons_mode conmode = cons_mode_on_close (p);
921920
set_output_mode (conmode, ti, p);
922921
set_input_mode (conmode, ti, p);
923922
set_disable_master_thread (con.owner == GetCurrentProcessId ());
@@ -1976,22 +1975,13 @@ fhandler_console::close ()
19761975

19771976
acquire_output_mutex (mutex_timeout);
19781977

1979-
if (shared_console_info[unit] && !myself->cygstarted
1978+
if (shared_console_info[unit] && myself->ppid == 1
19801979
&& (dev_t) myself->ctty == get_device ())
19811980
{
1982-
/* Restore console mode if this is the last closure. */
1983-
OBJECT_BASIC_INFORMATION obi;
1984-
NTSTATUS status;
1985-
status = NtQueryObject (get_handle (), ObjectBasicInformation,
1986-
&obi, sizeof obi, NULL);
1987-
if (NT_SUCCESS (status)
1988-
&& obi.HandleCount == (con.owner == GetCurrentProcessId () ? 2 : 3))
1989-
{
1990-
/* Cleaning-up console mode for cygwin apps. */
1991-
set_output_mode (tty::restore, &get_ttyp ()->ti, &handle_set);
1992-
set_input_mode (tty::restore, &get_ttyp ()->ti, &handle_set);
1993-
set_disable_master_thread (true, this);
1994-
}
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);
1984+
set_disable_master_thread (true, this);
19951985
}
19961986

19971987
if (shared_console_info[unit] && con.owner == GetCurrentProcessId ())
@@ -4690,3 +4680,28 @@ fhandler_console::fstat (struct stat *st)
46904680
}
46914681
return 0;
46924682
}
4683+
4684+
tty::cons_mode
4685+
fhandler_console::cons_mode_on_close (handle_set_t *p)
4686+
{
4687+
const _minor_t unit = p->unit;
4688+
4689+
if (myself->ppid != 1) /* Execed from normal cygwin process. */
4690+
return tty::cygwin;
4691+
4692+
if (!process_alive (con.owner)) /* The Master process already died. */
4693+
return tty::restore;
4694+
if (con.owner == GetCurrentProcessId ()) /* Master process */
4695+
return tty::restore;
4696+
4697+
PROCESS_BASIC_INFORMATION pbi;
4698+
NTSTATUS status =
4699+
NtQueryInformationProcess (GetCurrentProcess (), ProcessBasicInformation,
4700+
&pbi, sizeof (pbi), NULL);
4701+
if (NT_SUCCESS (status)
4702+
&& con.owner == (DWORD) pbi.InheritedFromUniqueProcessId)
4703+
/* The parent is the stub process. */
4704+
return tty::restore;
4705+
4706+
return tty::native; /* Otherwise */
4707+
}

winsup/cygwin/local_includes/fhandler.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2391,6 +2391,7 @@ class fhandler_console: public fhandler_termios
23912391

23922392
void setup_pcon_hand_over ();
23932393
static void pcon_hand_over_proc ();
2394+
static tty::cons_mode cons_mode_on_close (handle_set_t *);
23942395

23952396
friend tty_min * tty_list::get_cttyp ();
23962397
};

0 commit comments

Comments
 (0)