From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 7868) id 563593858D1E; Sat, 26 Feb 2022 10:52:23 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 563593858D1E Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable From: Takashi Yano To: cygwin-cvs@sourceware.org Subject: [newlib-cygwin/cygwin-3_3-branch] Cygwin: console: Fix issues of apps which open pty. X-Act-Checkin: newlib-cygwin X-Git-Author: Takashi Yano X-Git-Refname: refs/heads/cygwin-3_3-branch X-Git-Oldrev: cb9e68fd9be4fdec25dd340cb94f442c9848f2f2 X-Git-Newrev: 3ad4df8570d91dc156f98b5c72b64b796bfcd7af Message-Id: <20220226105223.563593858D1E@sourceware.org> Date: Sat, 26 Feb 2022 10:52:23 +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: Sat, 26 Feb 2022 10:52:23 -0000 https://sourceware.org/git/gitweb.cgi?p=3Dnewlib-cygwin.git;h=3D3ad4df8570d= 91dc156f98b5c72b64b796bfcd7af commit 3ad4df8570d91dc156f98b5c72b64b796bfcd7af Author: Takashi Yano Date: Fri Feb 25 16:23:58 2022 +0900 Cygwin: console: Fix issues of apps which open pty. =20 - After some recent changes for special keys handling break the apps which open pty (such as script command). If the app which opens pty is executed in console, the following issues occur. 1) If the script command was started from non-cygwin shell (such as cmd.exe), another cygwin app started in pty slave cannot receive Ctrl-C. 2) If non-cygwin app is executed in pty slave, the app which opened the pty (e.g. script command) crashes by Ctrl-C. This patch fixes these issues. Diff: --- winsup/cygwin/fhandler.h | 2 +- winsup/cygwin/fhandler_console.cc | 20 ++++++++++---------- winsup/cygwin/fhandler_termios.cc | 14 +++++++++----- winsup/cygwin/fhandler_tty.cc | 2 +- 4 files changed, 21 insertions(+), 17 deletions(-) diff --git a/winsup/cygwin/fhandler.h b/winsup/cygwin/fhandler.h index db45c7c73..3acb5986d 100644 --- a/winsup/cygwin/fhandler.h +++ b/winsup/cygwin/fhandler.h @@ -1921,7 +1921,7 @@ class fhandler_termios: public fhandler_base HANDLE& get_output_handle_nat () { return output_handle; } static process_sig_state process_sigs (char c, tty *ttyp, fhandler_termios *fh); - static bool process_stop_start (char c, tty *ttyp, bool on_ixany); + static bool process_stop_start (char c, tty *ttyp); line_edit_status line_edit (const char *rptr, size_t nread, termios&, ssize_t *bytes_read =3D NULL); void set_output_handle (HANDLE h) { output_handle =3D h; } diff --git a/winsup/cygwin/fhandler_console.cc b/winsup/cygwin/fhandler_con= sole.cc index f285542cc..c6c42b6a7 100644 --- a/winsup/cygwin/fhandler_console.cc +++ b/winsup/cygwin/fhandler_console.cc @@ -189,7 +189,7 @@ void fhandler_console::cons_master_thread (handle_set_t *p, tty *ttyp) { termios &ti =3D ttyp->ti; - DWORD output_stopped_at =3D 0; + int processed_up_to =3D -1; while (con.owner =3D=3D myself->pid) { DWORD total_read, n, i; @@ -210,6 +210,7 @@ fhandler_console::cons_master_thread (handle_set_t *p, = tty *ttyp) input_rec, INREC_SIZE, &total_read); break; case WAIT_TIMEOUT: + processed_up_to =3D -1; case WAIT_SIGNALED: case WAIT_CANCELED: break; @@ -232,11 +233,10 @@ fhandler_console::cons_master_thread (handle_set_t *p= , tty *ttyp) ttyp->kill_pgrp (SIGWINCH); } } - for (i =3D 0; i < total_read; i++) + for (i =3D processed_up_to + 1; i < total_read; i++) { wchar_t wc; char c; - bool was_output_stopped; bool processed =3D false; switch (input_rec[i].EventType) { @@ -256,14 +256,12 @@ fhandler_console::cons_master_thread (handle_set_t *p= , tty *ttyp) ttyp->output_stopped =3D false; if (ti.c_lflag & NOFLSH) goto remove_record; + processed_up_to =3D -1; goto skip_writeback; default: /* not signalled */ break; } - was_output_stopped =3D ttyp->output_stopped; - processed =3D process_stop_start (c, ttyp, i > output_stopped_at); - if (!was_output_stopped && ttyp->output_stopped) - output_stopped_at =3D i; + processed =3D process_stop_start (c, ttyp); break; case WINDOW_BUFFER_SIZE_EVENT: SHORT y =3D con.dwWinSize.Y; @@ -283,14 +281,16 @@ fhandler_console::cons_master_thread (handle_set_t *p= , tty *ttyp) remove_record: if (processed) { /* Remove corresponding record. */ - memmove (input_rec + i, input_rec + i + 1, - sizeof (INPUT_RECORD) * (total_read - i - 1)); + if (total_read > i + 1) + memmove (input_rec + i, input_rec + i + 1, + sizeof (INPUT_RECORD) * (total_read - i - 1)); total_read--; i--; } } + processed_up_to =3D total_read - 1; if (total_read) - /* Write back input records other than interrupt. */ + /* Writeback input records other than interrupt. */ WriteConsoleInputW (p->input_handle, input_rec, total_read, &n); skip_writeback: ReleaseMutex (p->input_mutex); diff --git a/winsup/cygwin/fhandler_termios.cc b/winsup/cygwin/fhandler_ter= mios.cc index 383e20764..b236c1b62 100644 --- a/winsup/cygwin/fhandler_termios.cc +++ b/winsup/cygwin/fhandler_termios.cc @@ -344,10 +344,12 @@ fhandler_termios::process_sigs (char c, tty* ttyp, fh= andler_termios *fh) (myself->dwProcessId, false); if (resume_pid && fh && !fh->is_console ()) { + if (::cygheap->ctty && ::cygheap->ctty->is_console ()) + init_console_handler (false); FreeConsole (); AttachConsole (p->dwProcessId); } - if (fh && p =3D=3D myself) + if (fh && p =3D=3D myself && being_debugged ()) { /* Avoid deadlock in gdb on console. */ fh->tcflush(TCIFLUSH); fh->release_input_mutex_if_necessary (); @@ -372,6 +374,8 @@ fhandler_termios::process_sigs (char c, tty* ttyp, fhan= dler_termios *fh) { FreeConsole (); AttachConsole (resume_pid); + if (::cygheap->ctty && ::cygheap->ctty->is_console ()) + init_console_handler (true); } } if (p && p->ctty =3D=3D ttyp->ntty && p->pgid =3D=3D pgid) @@ -447,7 +451,7 @@ fhandler_termios::process_sigs (char c, tty* ttyp, fhan= dler_termios *fh) return signalled; } not_a_sig: - if (need_discard_input) + if ((ti.c_lflag & ISIG) && need_discard_input) { if (!(ti.c_lflag & NOFLSH) && fh) { @@ -462,7 +466,7 @@ not_a_sig: } =20 bool -fhandler_termios::process_stop_start (char c, tty *ttyp, bool on_ixany) +fhandler_termios::process_stop_start (char c, tty *ttyp) { termios &ti =3D ttyp->ti; if (ti.c_iflag & IXON) @@ -478,7 +482,7 @@ restart_output: ttyp->output_stopped =3D false; return true; } - else if ((ti.c_iflag & IXANY) && ttyp->output_stopped && on_ixany) + else if ((ti.c_iflag & IXANY) && ttyp->output_stopped) goto restart_output; } if ((ti.c_lflag & ICANON) && (ti.c_lflag & IEXTEN) @@ -526,7 +530,7 @@ fhandler_termios::line_edit (const char *rptr, size_t n= read, termios& ti, default: /* Not signalled */ break; } - if (process_stop_start (c, get_ttyp (), true)) + if (process_stop_start (c, get_ttyp ())) continue; /* Check for special chars */ if (c =3D=3D '\r') diff --git a/winsup/cygwin/fhandler_tty.cc b/winsup/cygwin/fhandler_tty.cc index 9c30a6044..979232d96 100644 --- a/winsup/cygwin/fhandler_tty.cc +++ b/winsup/cygwin/fhandler_tty.cc @@ -2301,7 +2301,7 @@ fhandler_pty_master::write (const void *ptr, size_t l= en) nlen--; i--; } - process_stop_start (buf[i], get_ttyp (), true); + process_stop_start (buf[i], get_ttyp ()); } =20 DWORD n;