- The commit 12325677f73a did not fix enough. With this patch, more
  transfer_input() calls are skipped if stdin is redirected or piped.
---
 winsup/cygwin/fhandler_tty.cc | 10 ++++++++--
 winsup/cygwin/spawn.cc        |  9 +++++++--
 2 files changed, 15 insertions(+), 4 deletions(-)

diff --git a/winsup/cygwin/fhandler_tty.cc b/winsup/cygwin/fhandler_tty.cc
index 47d59e8c5..643a357ad 100644
--- a/winsup/cygwin/fhandler_tty.cc
+++ b/winsup/cygwin/fhandler_tty.cc
@@ -131,7 +131,9 @@ set_switch_to_pcon (HANDLE *in, HANDLE *out, HANDLE *err, 
bool iscygwin)
        {
          fhandler_base *fh = cfd;
          fhandler_pty_slave *ptys = (fhandler_pty_slave *) fh;
-         if (*in == ptys->get_handle ())
+         if (*in == ptys->get_handle ()
+             || *out == ptys->get_output_handle ()
+             || *err == ptys->get_output_handle ())
            ptys_pcon = ptys;
        }
     }
@@ -284,6 +286,7 @@ exit_Hooked (int e)
            HANDLE from = ptys->get_handle ();
            HANDLE input_available_event = ptys->get_input_available_event ();
            if (ttyp->getpgid () == myself->pgid
+               && GetStdHandle (STD_INPUT_HANDLE) == ptys->get_handle ()
                && ttyp->pcon_input_state_eq (tty::to_nat))
              {
                WaitForSingleObject (ptys->input_mutex, INFINITE);
@@ -1035,6 +1038,7 @@ fhandler_pty_slave::set_switch_to_pcon (void)
       bool pcon_enabled = setup_pseudoconsole (nopcon);
       ReleaseMutex (pcon_mutex);
       if (!pcon_enabled && get_ttyp ()->getpgid () == myself->pgid
+         && GetStdHandle (STD_INPUT_HANDLE) == get_handle ()
          && get_ttyp ()->pcon_input_state_eq (tty::to_cyg))
        {
          WaitForSingleObject (input_mutex, INFINITE);
@@ -1062,6 +1066,7 @@ fhandler_pty_slave::reset_switch_to_pcon (void)
          if (isHybrid)
            {
              if (get_ttyp ()->getpgid () == myself->pgid
+                 && GetStdHandle (STD_INPUT_HANDLE) == get_handle ()
                  && get_ttyp ()->pcon_input_state_eq (tty::to_nat))
                {
                  WaitForSingleObject (input_mutex, INFINITE);
@@ -1204,7 +1209,8 @@ fhandler_pty_slave::mask_switch_to_pcon_in (bool mask, 
bool xfer)
   /* In GDB, transfer input based on setpgid() does not work because
      GDB may not set terminal process group properly. Therefore,
      transfer input here if isHybrid is set. */
-  if (isHybrid && !!masked != mask && xfer)
+  if (isHybrid && !!masked != mask && xfer
+      && GetStdHandle (STD_INPUT_HANDLE) == get_handle ())
     {
       if (mask && get_ttyp ()->pcon_input_state_eq (tty::to_nat))
        {
diff --git a/winsup/cygwin/spawn.cc b/winsup/cygwin/spawn.cc
index 9d70cb2b7..7a585392a 100644
--- a/winsup/cygwin/spawn.cc
+++ b/winsup/cygwin/spawn.cc
@@ -665,6 +665,7 @@ child_info_spawn::worker (const char *prog_arg, const char 
*const *argv,
       HANDLE ptys_pcon_mutex = NULL;
       HANDLE ptys_input_mutex = NULL;
       tty *ptys_ttyp = NULL;
+      bool stdin_is_ptys = false;
       if (!iscygwin () && ptys_primary && is_console_app (runpath))
        {
          bool nopcon = mode != _P_OVERLAY && mode != _P_WAIT;
@@ -675,6 +676,9 @@ child_info_spawn::worker (const char *prog_arg, const char 
*const *argv,
          if (ptys_primary->setup_pseudoconsole (nopcon))
            enable_pcon = true;
          ReleaseMutex (ptys_primary->pcon_mutex);
+         HANDLE h_stdin = handle ((in__stdin < 0 ? 0 : in__stdin), false);
+         if (h_stdin == ptys_primary->get_handle ())
+           stdin_is_ptys = true;
          ptys_from_master = ptys_primary->get_handle ();
          DuplicateHandle (GetCurrentProcess (), ptys_from_master,
                           GetCurrentProcess (), &ptys_from_master,
@@ -691,6 +695,7 @@ child_info_spawn::worker (const char *prog_arg, const char 
*const *argv,
                           GetCurrentProcess (), &ptys_input_mutex,
                           0, 0, DUPLICATE_SAME_ACCESS);
          if (!enable_pcon && ptys_ttyp->getpgid () == myself->pgid
+             && stdin_is_ptys
              && ptys_ttyp->pcon_input_state_eq (tty::to_cyg))
            {
              WaitForSingleObject (ptys_input_mutex, INFINITE);
@@ -983,7 +988,7 @@ child_info_spawn::worker (const char *prog_arg, const char 
*const *argv,
          if (ptys_ttyp)
            {
              ptys_ttyp->wait_pcon_fwd ();
-             if (ptys_ttyp->getpgid () == myself->pgid
+             if (ptys_ttyp->getpgid () == myself->pgid && stdin_is_ptys
                  && ptys_ttyp->pcon_input_state_eq (tty::to_nat))
                {
                  WaitForSingleObject (ptys_input_mutex, INFINITE);
@@ -1020,7 +1025,7 @@ child_info_spawn::worker (const char *prog_arg, const 
char *const *argv,
          if (ptys_ttyp)
            {
              ptys_ttyp->wait_pcon_fwd ();
-             if (ptys_ttyp->getpgid () == myself->pgid
+             if (ptys_ttyp->getpgid () == myself->pgid && stdin_is_ptys
                  && ptys_ttyp->pcon_input_state_eq (tty::to_nat))
                {
                  WaitForSingleObject (ptys_input_mutex, INFINITE);
-- 
2.30.1

Reply via email to