[PATCH 0/1] Cygwin: pty: Make it sure to show up system error messages.

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

[PATCH 0/1] Cygwin: pty: Make it sure to show up system error messages.

Takashi Yano
- Forcibly attach to pseudo console in advance so that the error
  messages by system_printf() is displayed to screen reliably.
  This is needed when stdout is redirected to another pty. In this
  case, process has two ptys opened. However, process can attach
  to only one console. So it is necessary to change console attached.

Takashi Yano (1):
  Cygwin: pty: Make it sure to show up system error messages.

 winsup/cygwin/fhandler_tty.cc | 55 +++++++++++++++++++++++++++++++----
 1 file changed, 49 insertions(+), 6 deletions(-)

--
2.21.0

Reply | Threaded
Open this post in threaded view
|

[PATCH 1/1] Cygwin: pty: Make it sure to show up system error messages.

Takashi Yano
- Forcibly attach to pseudo console in advance so that the error
  messages by system_printf() is displayed to screen reliably.
  This is needed when stdout is redirected to another pty. In this
  case, process has two ptys opened. However, process can attach
  to only one console. So it is necessary to change console attached.
---
 winsup/cygwin/fhandler_tty.cc | 55 +++++++++++++++++++++++++++++++----
 1 file changed, 49 insertions(+), 6 deletions(-)

diff --git a/winsup/cygwin/fhandler_tty.cc b/winsup/cygwin/fhandler_tty.cc
index 78c9c9128..1a1ae1a5c 100644
--- a/winsup/cygwin/fhandler_tty.cc
+++ b/winsup/cygwin/fhandler_tty.cc
@@ -88,16 +88,59 @@ set_switch_to_pcon (void)
       }
 }
 
+static void
+force_attach_to_pcon (HANDLE h)
+{
+  bool attach_done = false;
+  for (int n = 0; n < 2; n ++)
+    {
+      /* First time, attach to the pty whoes handle value is match.
+ Second time, try to attach to any pty. */
+      cygheap_fdenum cfd (false);
+      while (cfd.next () >= 0)
+ if (cfd->get_major () == DEV_PTYS_MAJOR)
+  {
+    fhandler_base *fh = cfd;
+    fhandler_pty_slave *ptys = (fhandler_pty_slave *) fh;
+    if (n != 0
+ || h == ptys->get_handle ()
+ || h == ptys->get_output_handle ())
+      {
+ if (fhandler_console::get_console_process_id
+  (ptys->getHelperProcessId (), true))
+  attach_done = true;
+ else
+  {
+    FreeConsole ();
+    if (AttachConsole (ptys->getHelperProcessId ()))
+      {
+ pcon_attached_to = ptys->get_minor ();
+ attach_done = true;
+      }
+    else
+      pcon_attached_to = -1;
+  }
+ break;
+      }
+  }
+      if (attach_done)
+ break;
+    }
+}
+
 void
 set_ishybrid_and_switch_to_pcon (HANDLE h)
 {
-  DWORD dummy;
-  if (!isHybrid
-      && GetFileType (h) == FILE_TYPE_CHAR
-      && GetConsoleMode (h, &dummy))
+  if (GetFileType (h) == FILE_TYPE_CHAR)
     {
-      isHybrid = true;
-      set_switch_to_pcon ();
+      force_attach_to_pcon (h);
+      DWORD dummy;
+      if (!isHybrid && (GetConsoleMode (h, &dummy)
+ || GetLastError () != ERROR_INVALID_HANDLE))
+ {
+  isHybrid = true;
+  set_switch_to_pcon ();
+ }
     }
 }
 
--
2.21.0

Reply | Threaded
Open this post in threaded view
|

Re: [PATCH 0/1] Cygwin: pty: Make it sure to show up system error messages.

Ken Brown-6
In reply to this post by Takashi Yano
On 9/5/2019 9:22 AM, Takashi Yano wrote:

> - Forcibly attach to pseudo console in advance so that the error
>    messages by system_printf() is displayed to screen reliably.
>    This is needed when stdout is redirected to another pty. In this
>    case, process has two ptys opened. However, process can attach
>    to only one console. So it is necessary to change console attached.
>
> Takashi Yano (1):
>    Cygwin: pty: Make it sure to show up system error messages.
>
>   winsup/cygwin/fhandler_tty.cc | 55 +++++++++++++++++++++++++++++++----
>   1 file changed, 49 insertions(+), 6 deletions(-)

Pushed, with a couple of minor typo fixes.

Thanks.

Ken

P.S. Please let me know whenever you think I should make a new test release.
Also, I'll be mostly AFK for about a week starting on Saturday, so I probably
won't be reviewing any patches during that time.