Currently, open() tries to attach to the console which is owned by
the console owner process. However, when the owner process calls
exec(), AttachConsole() to dwProcessId may sometimes fail due to
unlucky timing. With this patch, open() tries to attach also to
exec_dwProcessId if attaching to dwProcessId fails. That is, open()
tries to attach to both the stub process and target process to
prevent the above situation.

Fixes: 3721a756b0d8 ("Cygwin: console: Make the console accessible from other 
terminals.")
Signed-off-by: Takashi Yano <takashi.y...@nifty.ne.jp>
---
 winsup/cygwin/fhandler/console.cc | 6 ++++++
 1 file changed, 6 insertions(+)

diff --git a/winsup/cygwin/fhandler/console.cc 
b/winsup/cygwin/fhandler/console.cc
index 9cdc13dd2..ac47c8374 100644
--- a/winsup/cygwin/fhandler/console.cc
+++ b/winsup/cygwin/fhandler/console.cc
@@ -84,6 +84,10 @@ fhandler_console::attach_console (pid_t owner, bool *err)
       DWORD attached =
        fhandler_pty_common::get_console_process_id (p->dwProcessId,
                                                     true, false, false);
+      if (!attached)
+       attached =
+         fhandler_pty_common::get_console_process_id (p->exec_dwProcessId,
+                                                      true, false, false);
       if (!attached)
        {
          resume_pid =
@@ -91,6 +95,8 @@ fhandler_console::attach_console (pid_t owner, bool *err)
                                                         false, false, false);
          FreeConsole ();
          BOOL r = AttachConsole (p->dwProcessId);
+         if (!r)
+           r = AttachConsole (p->exec_dwProcessId);
          if (!r)
            {
              if (resume_pid)
-- 
2.45.1

Reply via email to