Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
From 463ebcdca8feee7e92a2629fa3e0efbc6d28dc4a Mon Sep 17 00:00:00 2001
From: Takashi Yano <[email protected]>
Date: Mon, 3 Mar 2025 15:31:23 +0900
Subject: [PATCH 55/N] 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: https://github.com/microsoft/git/issues/730
Backported-from: 3312f2d21f (Cygwin: console: Redesign mode set strategy on close()., 2025-03-03)
Fixes: 30d266947842 ("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]>
---
winsup/cygwin/fhandler/console.cc | 47 ++++++++++++++++---------
winsup/cygwin/local_includes/fhandler.h | 1 +
2 files changed, 32 insertions(+), 16 deletions(-)

diff --git a/winsup/cygwin/fhandler/console.cc b/winsup/cygwin/fhandler/console.cc
index 2651e49..b095afa 100644
--- a/winsup/cygwin/fhandler/console.cc
+++ b/winsup/cygwin/fhandler/console.cc
@@ -916,8 +916,7 @@ fhandler_console::cleanup_for_non_cygwin_app (handle_set_t *p)
/* Cleaning-up console mode for non-cygwin app. */
/* conmode can be tty::restore when non-cygwin app is
exec'ed from login shell. */
- tty::cons_mode conmode =
- (con.owner == GetCurrentProcessId ()) ? tty::restore : tty::cygwin;
+ tty::cons_mode conmode = cons_mode_on_close (p);
set_output_mode (conmode, ti, p);
set_input_mode (conmode, ti, p);
set_disable_master_thread (con.owner == GetCurrentProcessId ());
@@ -1976,22 +1975,13 @@ fhandler_console::close ()

acquire_output_mutex (mutex_timeout);

- if (shared_console_info[unit] && !myself->cygstarted
+ if (shared_console_info[unit] && myself->ppid == 1
&& (dev_t) myself->ctty == get_device ())
{
- /* Restore console mode if this is the last closure. */
- OBJECT_BASIC_INFORMATION obi;
- NTSTATUS status;
- status = NtQueryObject (get_handle (), ObjectBasicInformation,
- &obi, sizeof obi, NULL);
- if (NT_SUCCESS (status)
- && obi.HandleCount == (con.owner == GetCurrentProcessId () ? 2 : 3))
- {
- /* Cleaning-up console mode for cygwin apps. */
- set_output_mode (tty::restore, &get_ttyp ()->ti, &handle_set);
- set_input_mode (tty::restore, &get_ttyp ()->ti, &handle_set);
- set_disable_master_thread (true, this);
- }
+ tty::cons_mode conmode = cons_mode_on_close (&handle_set);
+ set_output_mode (conmode, &get_ttyp ()->ti, &handle_set);
+ set_input_mode (conmode, &get_ttyp ()->ti, &handle_set);
+ set_disable_master_thread (true, this);
}

if (shared_console_info[unit] && con.owner == GetCurrentProcessId ())
@@ -4690,3 +4680,28 @@ fhandler_console::fstat (struct stat *st)
}
return 0;
}
+
+tty::cons_mode
+fhandler_console::cons_mode_on_close (handle_set_t *p)
+{
+ const _minor_t unit = p->unit;
+
+ if (myself->ppid != 1) /* Execed from normal cygwin process. */
+ return tty::cygwin;
+
+ if (!process_alive (con.owner)) /* The Master process already died. */
+ return tty::restore;
+ if (con.owner == GetCurrentProcessId ()) /* Master process */
+ return tty::restore;
+
+ PROCESS_BASIC_INFORMATION pbi;
+ NTSTATUS status =
+ NtQueryInformationProcess (GetCurrentProcess (), ProcessBasicInformation,
+ &pbi, sizeof (pbi), NULL);
+ if (NT_SUCCESS (status)
+ && con.owner == (DWORD) pbi.InheritedFromUniqueProcessId)
+ /* The parent is the stub process. */
+ return tty::restore;
+
+ return tty::native; /* Otherwise */
+}
diff --git a/winsup/cygwin/local_includes/fhandler.h b/winsup/cygwin/local_includes/fhandler.h
index 88a6b7d..e19aca5 100644
--- a/winsup/cygwin/local_includes/fhandler.h
+++ b/winsup/cygwin/local_includes/fhandler.h
@@ -2391,6 +2391,7 @@ private:

void setup_pcon_hand_over ();
static void pcon_hand_over_proc ();
+ static tty::cons_mode cons_mode_on_close (handle_set_t *);

friend tty_min * tty_list::get_cttyp ();
};
13 changes: 8 additions & 5 deletions msys2-runtime/PKGBUILD
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
pkgbase=msys2-runtime
pkgname=('msys2-runtime' 'msys2-runtime-devel')
pkgver=3.5.7
pkgrel=2
pkgrel=3
pkgdesc="Cygwin POSIX emulation engine"
arch=('x86_64')
url="https://www.cygwin.com/"
Expand Down Expand Up @@ -80,9 +80,10 @@ source=('msys2-runtime'::git+https://github.com/cygwin/cygwin#tag=cygwin-${pkgve
0051-Change-the-default-base-address-for-x86_64.patch
0052-msys2-runtime-restore-fast-path-for-current-user-pri.patch
0053-Fix-SSH-hangs.patch
0054-Cygwin-Adjust-CWD-magic-to-accommodate-for-the-lates.patch)
0054-Cygwin-Adjust-CWD-magic-to-accommodate-for-the-lates.patch
0055-Cygwin-console-Redesign-mode-set-strategy-on-close.patch)
sha256sums=('67c44fa7fcae9dc63c71d71934a5f2b7c584d967279b0492646df9bc63bc3f16'
'6fce92782f8832044f3caa1fe22ec9b0e1f719c0bf78e2cd9e7398d1ce4edbed'
'35e920e732e03193a1f7e5d2a1ceaff4dca3355655b395af1d6accd0bf9bae7c'
'87d86c95008274731cf560d929ae5fb319d962591303b2690e11bfd60f4818e7'
'c6d091cb51440638eb9fc59b35ba40f2b63e9e32a31a56c51a2aaec2445aa88e'
'81ec264a45c03e03e2b901037b73e5042b9d54e0454b29d372114d0c853b82e0'
Expand Down Expand Up @@ -136,7 +137,8 @@ sha256sums=('67c44fa7fcae9dc63c71d71934a5f2b7c584d967279b0492646df9bc63bc3f16'
'87e59ef31ae84f3d86f4d7dc632b8e0fc0a37d9efdb9422b712c76ec6088dc59'
'8c0603f8b1a2874488a43a9779be2c5ffd05ccf392b4e8fcc8b2a282470a68a2'
'd441808c2f6dbbe5a6fa33bd7231ddcff2a8027292bd8325448b58a42412fd52'
'3850bf314368132b8a12dc7df440563153b9da64a412d4a5a100a8b710cc29f0')
'3850bf314368132b8a12dc7df440563153b9da64a412d4a5a100a8b710cc29f0'
'1548f75ce54ff9f85dd7f8757055fcb24ec18e56641f28fc5e5ca70f65d3f18f')

# Helper macros to help make tasks easier #
apply_patch_with_msg() {
Expand Down Expand Up @@ -244,7 +246,8 @@ prepare() {
0051-Change-the-default-base-address-for-x86_64.patch \
0052-msys2-runtime-restore-fast-path-for-current-user-pri.patch \
0053-Fix-SSH-hangs.patch \
0054-Cygwin-Adjust-CWD-magic-to-accommodate-for-the-lates.patch
0054-Cygwin-Adjust-CWD-magic-to-accommodate-for-the-lates.patch \
0055-Cygwin-console-Redesign-mode-set-strategy-on-close.patch
}

build() {
Expand Down
2 changes: 1 addition & 1 deletion msys2-runtime/msys2-runtime.commit
Original file line number Diff line number Diff line change
@@ -1 +1 @@
b80da35c1f6a619acf53be8db4a6691937dbefef
463ebcdca8feee7e92a2629fa3e0efbc6d28dc4a