From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 2155) id 52CCC3877021; Tue, 9 Mar 2021 10:21:38 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 52CCC3877021 Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: Corinna Vinschen To: cygwin-cvs@sourceware.org Subject: [newlib-cygwin] Cygwin: pty: Transfer input only if the stdin is a pty. X-Act-Checkin: newlib-cygwin X-Git-Author: Takashi Yano via Cygwin-patches X-Git-Refname: refs/heads/master X-Git-Oldrev: 11ae6943eb107a3ca7a2da988c94a2bebaca425a X-Git-Newrev: d10d0d9b03f858b10fa4b9a5dd0dd0c5364ca439 Message-Id: <20210309102138.52CCC3877021@sourceware.org> Date: Tue, 9 Mar 2021 10:21:38 +0000 (GMT) X-BeenThere: cygwin-cvs@cygwin.com X-Mailman-Version: 2.1.29 Precedence: list List-Id: Cygwin core component git logs List-Unsubscribe: , List-Archive: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 09 Mar 2021 10:21:38 -0000 https://sourceware.org/git/gitweb.cgi?p=newlib-cygwin.git;h=d10d0d9b03f858b10fa4b9a5dd0dd0c5364ca439 commit d10d0d9b03f858b10fa4b9a5dd0dd0c5364ca439 Author: Takashi Yano via Cygwin-patches Date: Tue Mar 9 12:23:34 2021 +0900 Cygwin: pty: Transfer input only if the stdin is a pty. - The commit 12325677f73a did not fix enough. With this patch, more transfer_input() calls are skipped if stdin is redirected or piped. Diff: --- 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);