https://sourceware.org/git/gitweb.cgi?p=newlib-cygwin.git;h=f7a77d014693a88b8a3dbecfd792956e99a7323e

commit f7a77d014693a88b8a3dbecfd792956e99a7323e
Author: Takashi Yano <[email protected]>
Date:   Wed Jul 3 22:35:20 2024 +0900

    Cygwin: console: Fix conflict on shared names between sessions.
    
    Previously, shared names in the console were created using get_minor().
    However, get_minor() was not unique to the console across sessions.
    This is because EnumWindows(), which is used to look for console windows,
    cannot enumerate windows across sessions. This causes conflict on the
    shared names between sessions (e.g. sessions of different users,
    different services, a service and a user session, etc.).
    
    With this patch, GetConsoleWindow() is used instead of get_minor().
    GetConsoleWindow() has been used for the name of shared memory, which
    should be unique to each console.
    
    Addresses: https://cygwin.com/pipermail/cygwin/2024-April/255893.html
    Fixes: ff4440fcf768 ("Cygwin: console: Introduce new thread which handles 
input signal.");
    Reported-by: Johannes Khoshnazar-Thoma <[email protected]>
    Signed-off-by: Takashi Yano <[email protected]>

Diff:
---
 winsup/cygwin/fhandler/console.cc | 15 +++++++++++----
 winsup/cygwin/release/3.5.4       |  3 +++
 2 files changed, 14 insertions(+), 4 deletions(-)

diff --git a/winsup/cygwin/fhandler/console.cc 
b/winsup/cygwin/fhandler/console.cc
index dbf6ce8a6..7945a32eb 100644
--- a/winsup/cygwin/fhandler/console.cc
+++ b/winsup/cygwin/fhandler/console.cc
@@ -66,6 +66,13 @@ static struct fhandler_base::rabuf_t con_ra;
    in xterm compatible mode */
 static wchar_t last_char;
 
+static char *
+cons_shared_name (char *ret_buf, const char *str, HWND hw)
+{
+  __small_sprintf (ret_buf, "%s.%p", str, hw);
+  return ret_buf;
+}
+
 DWORD
 fhandler_console::attach_console (pid_t owner, bool *err)
 {
@@ -922,7 +929,7 @@ fhandler_console::setup_io_mutex (void)
   res = WAIT_FAILED;
   if (!input_mutex || WAIT_FAILED == (res = acquire_input_mutex (0)))
     {
-      shared_name (buf, "cygcons.input.mutex", get_minor ());
+      cons_shared_name (buf, "cygcons.input.mutex", GetConsoleWindow ());
       input_mutex = OpenMutex (MAXIMUM_ALLOWED, TRUE, buf);
       if (!input_mutex)
        input_mutex = CreateMutex (&sec_none, FALSE, buf);
@@ -938,7 +945,7 @@ fhandler_console::setup_io_mutex (void)
   res = WAIT_FAILED;
   if (!output_mutex || WAIT_FAILED == (res = acquire_output_mutex (0)))
     {
-      shared_name (buf, "cygcons.output.mutex", get_minor ());
+      cons_shared_name (buf, "cygcons.output.mutex", GetConsoleWindow ());
       output_mutex = OpenMutex (MAXIMUM_ALLOWED, TRUE, buf);
       if (!output_mutex)
        output_mutex = CreateMutex (&sec_none, FALSE, buf);
@@ -1853,7 +1860,7 @@ fhandler_console::open (int flags, mode_t)
       if (GetModuleHandle ("ConEmuHk64.dll"))
        hook_conemu_cygwin_connector ();
       char name[MAX_PATH];
-      shared_name (name, CONS_THREAD_SYNC, get_minor ());
+      cons_shared_name (name, CONS_THREAD_SYNC, GetConsoleWindow ());
       thread_sync_event = CreateEvent(NULL, FALSE, FALSE, name);
       if (thread_sync_event)
        {
@@ -1922,7 +1929,7 @@ fhandler_console::close ()
       if (master_thread_started)
        {
          char name[MAX_PATH];
-         shared_name (name, CONS_THREAD_SYNC, get_minor ());
+         cons_shared_name (name, CONS_THREAD_SYNC, GetConsoleWindow ());
          thread_sync_event = OpenEvent (MAXIMUM_ALLOWED, FALSE, name);
          if (thread_sync_event)
            {
diff --git a/winsup/cygwin/release/3.5.4 b/winsup/cygwin/release/3.5.4
index 17db61146..c95ef4635 100644
--- a/winsup/cygwin/release/3.5.4
+++ b/winsup/cygwin/release/3.5.4
@@ -19,3 +19,6 @@ Fixes:
 - Fix a problem that pty slave hangs on writing when pty master stops
   to read.
   Addresses: https://cygwin.com/pipermail/cygwin/2024-June/256178.html
+
+- Fix conflict on shared name in console between sessions.
+  Addresses: https://cygwin.com/pipermail/cygwin/2024-April/255893.html

Reply via email to