public inbox for cygwin-patches@cygwin.com
 help / color / mirror / Atom feed
* [PATCH] Cygwin: console: Fix for GNU screen/tmux in ConEmu
@ 2024-07-08 14:31 Takashi Yano
  0 siblings, 0 replies; only message in thread
From: Takashi Yano @ 2024-07-08 14:31 UTC (permalink / raw)
  To: cygwin-patches; +Cc: Takashi Yano

If the master process of GNU screen or tmux is started in ConEmu
and ConEmu is closed, reataching to the GNU screen/tmux in another
console will not work correctly. This is because the ConEmu master
process was already closed even though some console APIs are hooked
by ConEmuHk64.dll to communicate with ConEmu master process. With
this patch, to make them unhooked, DllMain() of ConEmuHk64.dll is
called with DLL_PROCESS_DETACH.

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

diff --git a/winsup/cygwin/fhandler/console.cc b/winsup/cygwin/fhandler/console.cc
index 8c08a8af8..d4c3f1020 100644
--- a/winsup/cygwin/fhandler/console.cc
+++ b/winsup/cygwin/fhandler/console.cc
@@ -33,6 +33,7 @@ details. */
 #include "child_info.h"
 #include "cygwait.h"
 #include "winf.h"
+#include "psapi.h"
 
 /* Don't make this bigger than NT_MAX_PATH as long as the temporary buffer
    is allocated using tmp_pathbuf!!! */
@@ -1970,7 +1971,23 @@ fhandler_console::close ()
   if (!have_execed && !invisible_console
       && (!CTTY_IS_VALID (myself->ctty)
 	  || get_device () == (dev_t) myself->ctty))
-    free_console ();
+    {
+      /* ConEmu hack. Detach from ConEmu to unhook console APIs. */
+      HMODULE h = GetModuleHandle ("ConEmuHk64.dll");
+      if (h)
+	{
+	  MODULEINFO mi;
+	  if (GetModuleInformation (GetCurrentProcess (), h, &mi, sizeof (mi)))
+	    {
+	      BOOL (*DllMain)(HINSTANCE, DWORD, LPVOID) =
+		(BOOL (*)(HINSTANCE, DWORD, LPVOID)) mi.EntryPoint;
+	      DllMain (h, DLL_PROCESS_DETACH, NULL);
+	    }
+	}
+
+      /* Freeing console to detach the process from the console. */
+      free_console ();
+    }
 
   release_output_mutex ();
 
-- 
2.45.1


^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2024-07-08 14:31 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-07-08 14:31 [PATCH] Cygwin: console: Fix for GNU screen/tmux in ConEmu Takashi Yano

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).