4
0
mirror of git://sourceware.org/git/newlib-cygwin.git synced 2025-02-15 05:29:10 +08:00

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.
This commit is contained in:
Takashi Yano via Cygwin-patches 2021-03-09 12:23:34 +09:00 committed by Ken Brown
parent f388dbe23c
commit 05b0001f4a
2 changed files with 15 additions and 4 deletions

View File

@ -131,7 +131,9 @@ set_switch_to_pcon (HANDLE *in, HANDLE *out, HANDLE *err, bool iscygwin)
{ {
fhandler_base *fh = cfd; fhandler_base *fh = cfd;
fhandler_pty_slave *ptys = (fhandler_pty_slave *) fh; 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; ptys_pcon = ptys;
} }
} }
@ -284,6 +286,7 @@ exit_Hooked (int e)
HANDLE from = ptys->get_handle (); HANDLE from = ptys->get_handle ();
HANDLE input_available_event = ptys->get_input_available_event (); HANDLE input_available_event = ptys->get_input_available_event ();
if (ttyp->getpgid () == myself->pgid if (ttyp->getpgid () == myself->pgid
&& GetStdHandle (STD_INPUT_HANDLE) == ptys->get_handle ()
&& ttyp->pcon_input_state_eq (tty::to_nat)) && ttyp->pcon_input_state_eq (tty::to_nat))
{ {
WaitForSingleObject (ptys->input_mutex, INFINITE); WaitForSingleObject (ptys->input_mutex, INFINITE);
@ -1035,6 +1038,7 @@ fhandler_pty_slave::set_switch_to_pcon (void)
bool pcon_enabled = setup_pseudoconsole (nopcon); bool pcon_enabled = setup_pseudoconsole (nopcon);
ReleaseMutex (pcon_mutex); ReleaseMutex (pcon_mutex);
if (!pcon_enabled && get_ttyp ()->getpgid () == myself->pgid if (!pcon_enabled && get_ttyp ()->getpgid () == myself->pgid
&& GetStdHandle (STD_INPUT_HANDLE) == get_handle ()
&& get_ttyp ()->pcon_input_state_eq (tty::to_cyg)) && get_ttyp ()->pcon_input_state_eq (tty::to_cyg))
{ {
WaitForSingleObject (input_mutex, INFINITE); WaitForSingleObject (input_mutex, INFINITE);
@ -1062,6 +1066,7 @@ fhandler_pty_slave::reset_switch_to_pcon (void)
if (isHybrid) if (isHybrid)
{ {
if (get_ttyp ()->getpgid () == myself->pgid if (get_ttyp ()->getpgid () == myself->pgid
&& GetStdHandle (STD_INPUT_HANDLE) == get_handle ()
&& get_ttyp ()->pcon_input_state_eq (tty::to_nat)) && get_ttyp ()->pcon_input_state_eq (tty::to_nat))
{ {
WaitForSingleObject (input_mutex, INFINITE); 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 /* In GDB, transfer input based on setpgid() does not work because
GDB may not set terminal process group properly. Therefore, GDB may not set terminal process group properly. Therefore,
transfer input here if isHybrid is set. */ 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)) if (mask && get_ttyp ()->pcon_input_state_eq (tty::to_nat))
{ {

View File

@ -665,6 +665,7 @@ child_info_spawn::worker (const char *prog_arg, const char *const *argv,
HANDLE ptys_pcon_mutex = NULL; HANDLE ptys_pcon_mutex = NULL;
HANDLE ptys_input_mutex = NULL; HANDLE ptys_input_mutex = NULL;
tty *ptys_ttyp = NULL; tty *ptys_ttyp = NULL;
bool stdin_is_ptys = false;
if (!iscygwin () && ptys_primary && is_console_app (runpath)) if (!iscygwin () && ptys_primary && is_console_app (runpath))
{ {
bool nopcon = mode != _P_OVERLAY && mode != _P_WAIT; 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)) if (ptys_primary->setup_pseudoconsole (nopcon))
enable_pcon = true; enable_pcon = true;
ReleaseMutex (ptys_primary->pcon_mutex); 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 (); ptys_from_master = ptys_primary->get_handle ();
DuplicateHandle (GetCurrentProcess (), ptys_from_master, DuplicateHandle (GetCurrentProcess (), ptys_from_master,
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, GetCurrentProcess (), &ptys_input_mutex,
0, 0, DUPLICATE_SAME_ACCESS); 0, 0, DUPLICATE_SAME_ACCESS);
if (!enable_pcon && ptys_ttyp->getpgid () == myself->pgid if (!enable_pcon && ptys_ttyp->getpgid () == myself->pgid
&& stdin_is_ptys
&& ptys_ttyp->pcon_input_state_eq (tty::to_cyg)) && ptys_ttyp->pcon_input_state_eq (tty::to_cyg))
{ {
WaitForSingleObject (ptys_input_mutex, INFINITE); WaitForSingleObject (ptys_input_mutex, INFINITE);
@ -983,7 +988,7 @@ child_info_spawn::worker (const char *prog_arg, const char *const *argv,
if (ptys_ttyp) if (ptys_ttyp)
{ {
ptys_ttyp->wait_pcon_fwd (); 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)) && ptys_ttyp->pcon_input_state_eq (tty::to_nat))
{ {
WaitForSingleObject (ptys_input_mutex, INFINITE); WaitForSingleObject (ptys_input_mutex, INFINITE);
@ -1020,7 +1025,7 @@ child_info_spawn::worker (const char *prog_arg, const char *const *argv,
if (ptys_ttyp) if (ptys_ttyp)
{ {
ptys_ttyp->wait_pcon_fwd (); 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)) && ptys_ttyp->pcon_input_state_eq (tty::to_nat))
{ {
WaitForSingleObject (ptys_input_mutex, INFINITE); WaitForSingleObject (ptys_input_mutex, INFINITE);