Skip to content

Commit

Permalink
Cygwin: console: Fixes an issue that tmux can not run on the console.
Browse files Browse the repository at this point in the history
After the commit c77a568, 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: c77a568 ("Cygwin: console: Do not unmap shared console memory belonging to ctty.")
Signed-off-by: Takashi Yano <[email protected]>
  • Loading branch information
tyan0 committed Jul 6, 2024
1 parent a8300a6 commit 186ee9b
Showing 1 changed file with 14 additions and 1 deletion.
15 changes: 14 additions & 1 deletion winsup/cygwin/fhandler/console.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 ();
Expand Down Expand Up @@ -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;
}
Expand Down

0 comments on commit 186ee9b

Please sign in to comment.