From 32401ad98ec6f04534f6a60d47566f8c8e500605 Mon Sep 17 00:00:00 2001 From: Takashi Yano Date: Fri, 25 Feb 2022 16:23:58 +0900 Subject: [PATCH] Cygwin: console: Fix issues of apps which open pty. - 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. --- 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 7ddf7e450..0652075b0 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 = NULL); void set_output_handle (HANDLE h) { output_handle = h; } diff --git a/winsup/cygwin/fhandler_console.cc b/winsup/cygwin/fhandler_console.cc index 843a96f9a..aa0f26450 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 = ttyp->ti; - DWORD output_stopped_at = 0; + int processed_up_to = -1; while (con.owner == 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 = -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 = 0; i < total_read; i++) + for (i = processed_up_to + 1; i < total_read; i++) { wchar_t wc; char c; - bool was_output_stopped; bool processed = false; switch (input_rec[i].EventType) { @@ -256,14 +256,12 @@ fhandler_console::cons_master_thread (handle_set_t *p, tty *ttyp) ttyp->output_stopped = false; if (ti.c_lflag & NOFLSH) goto remove_record; + processed_up_to = -1; goto skip_writeback; default: /* not signalled */ break; } - was_output_stopped = ttyp->output_stopped; - processed = process_stop_start (c, ttyp, i > output_stopped_at); - if (!was_output_stopped && ttyp->output_stopped) - output_stopped_at = i; + processed = process_stop_start (c, ttyp); break; case WINDOW_BUFFER_SIZE_EVENT: SHORT y = con.dwWinSize.Y; @@ -284,14 +282,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 = 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_termios.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, fhandler_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 == myself) + if (fh && p == 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, fhandler_termios *fh) { FreeConsole (); AttachConsole (resume_pid); + if (::cygheap->ctty && ::cygheap->ctty->is_console ()) + init_console_handler (true); } } if (p && p->ctty == ttyp->ntty && p->pgid == pgid) @@ -447,7 +451,7 @@ fhandler_termios::process_sigs (char c, tty* ttyp, fhandler_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: } bool -fhandler_termios::process_stop_start (char c, tty *ttyp, bool on_ixany) +fhandler_termios::process_stop_start (char c, tty *ttyp) { termios &ti = ttyp->ti; if (ti.c_iflag & IXON) @@ -478,7 +482,7 @@ restart_output: ttyp->output_stopped = 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 nread, 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 == '\r') diff --git a/winsup/cygwin/fhandler_tty.cc b/winsup/cygwin/fhandler_tty.cc index 4aafe08fa..2440318b8 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 len) nlen--; i--; } - process_stop_start (buf[i], get_ttyp (), true); + process_stop_start (buf[i], get_ttyp ()); } DWORD n; -- 2.43.5