From 186ee9b67607deeb19993385bc2c07ba20050237 Mon Sep 17 00:00:00 2001 From: Takashi Yano Date: Sat, 6 Jul 2024 13:50:51 +0900 Subject: [PATCH] Cygwin: console: Fixes an issue that tmux can not run on the console. After the commit c77a5689f7bd, tmux can not run on the console. This patch replaces the countermeasure for the race issue between console setup and close with another mechanism using a mutex. Fixes: c77a5689f7bd ("Cygwin: console: Do not unmap shared console memory belonging to ctty.") Signed-off-by: Takashi Yano --- winsup/cygwin/fhandler/console.cc | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/winsup/cygwin/fhandler/console.cc b/winsup/cygwin/fhandler/console.cc index 7945a32eb..a870b741c 100644 --- a/winsup/cygwin/fhandler/console.cc +++ b/winsup/cygwin/fhandler/console.cc @@ -48,6 +48,9 @@ details. */ con.b.srWindow.Top + con.scroll_region.Bottom) #define con_is_legacy (shared_console_info[unit] && con.is_legacy) +static HANDLE NO_COPY shared_info_mutex; +static int NO_COPY shared_info_state; + #define CONS_THREAD_SYNC "cygcons.thread_sync" static bool NO_COPY master_thread_started = false; @@ -665,6 +668,12 @@ fhandler_console::set_unit () else if (myself->ctty != CTTY_UNINITIALIZED) unit = device::minor (myself->ctty); + if (!shared_info_mutex) + shared_info_mutex = CreateMutex (&sec_none_nih, FALSE, NULL); + + WaitForSingleObject (shared_info_mutex, INFINITE); + shared_info_state++; + if (shared_console_info[unit]) ; /* Do nothing */ else if (generic_console @@ -698,6 +707,8 @@ fhandler_console::set_unit () } } } + ReleaseMutex (shared_info_mutex); + if (shared_console_info[unit]) { devset = (fh_devices) shared_console_info[unit]->tty_min_state.getntty (); @@ -1963,11 +1974,13 @@ fhandler_console::close () CloseHandle (output_mutex); output_mutex = NULL; - if (shared_console_info[unit] && myself->ctty != tc ()->ntty) + WaitForSingleObject (shared_info_mutex, INFINITE); + if (--shared_info_state == 0 && shared_console_info[unit]) { UnmapViewOfFile ((void *) shared_console_info[unit]); shared_console_info[unit] = NULL; } + ReleaseMutex (shared_info_mutex); return 0; }