From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 2155) id 2A0973857811; Mon, 25 Jan 2021 18:55:02 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 2A0973857811 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: console: Add missing guard regarding attach_mutex. X-Act-Checkin: newlib-cygwin X-Git-Author: Takashi Yano via Cygwin-patches X-Git-Refname: refs/heads/master X-Git-Oldrev: cb41c375a6e2c70e5146c1af68549c1a367320da X-Git-Newrev: 460eb128cb514c32a8c8b4be17eeb8ce3b888d6b Message-Id: <20210125185502.2A0973857811@sourceware.org> Date: Mon, 25 Jan 2021 18:55:02 +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: Mon, 25 Jan 2021 18:55:02 -0000 https://sourceware.org/git/gitweb.cgi?p=newlib-cygwin.git;h=460eb128cb514c32a8c8b4be17eeb8ce3b888d6b commit 460eb128cb514c32a8c8b4be17eeb8ce3b888d6b Author: Takashi Yano via Cygwin-patches Date: Mon Jan 25 18:18:11 2021 +0900 Cygwin: console: Add missing guard regarding attach_mutex. - The commit a5333345 did not fix the problem enough. This patch provides additional guard for the issue. Diff: --- winsup/cygwin/fhandler_console.cc | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/winsup/cygwin/fhandler_console.cc b/winsup/cygwin/fhandler_console.cc index 49963e719..02d0ac052 100644 --- a/winsup/cygwin/fhandler_console.cc +++ b/winsup/cygwin/fhandler_console.cc @@ -439,14 +439,18 @@ fhandler_console::set_raw_win32_keyboard_mode (bool new_mode) void fhandler_console::set_cursor_maybe () { + acquire_attach_mutex (INFINITE); con.fillin (get_output_handle ()); + release_attach_mutex (); /* Nothing to do for xterm compatible mode. */ if (wincap.has_con_24bit_colors () && !con_is_legacy) return; if (con.dwLastCursorPosition.X != con.b.dwCursorPosition.X || con.dwLastCursorPosition.Y != con.b.dwCursorPosition.Y) { + acquire_attach_mutex (INFINITE); SetConsoleCursorPosition (get_output_handle (), con.b.dwCursorPosition); + release_attach_mutex (); con.dwLastCursorPosition = con.b.dwCursorPosition; } } @@ -536,6 +540,7 @@ fhandler_console::read (void *pv, size_t& buflen) } set_cursor_maybe (); /* to make cursor appear on the screen immediately */ +wait_retry: switch (cygwait (get_handle (), timeout)) { case WAIT_OBJECT_0: @@ -551,6 +556,15 @@ fhandler_console::read (void *pv, size_t& buflen) buflen = (size_t) -1; return; default: + if (GetLastError () == ERROR_INVALID_HANDLE) + { /* Confirm the handle is still valid */ + DWORD mode; + acquire_attach_mutex (INFINITE); + BOOL res = GetConsoleMode (get_handle (), &mode); + release_attach_mutex (); + if (res) + goto wait_retry; + } goto err; } @@ -1243,7 +1257,9 @@ fhandler_console::ioctl (unsigned int cmd, void *arg) case TIOCGWINSZ: int st; + acquire_attach_mutex (INFINITE); st = con.fillin (get_output_handle ()); + release_attach_mutex (); if (st) { /* *not* the buffer size, the actual screen size... */ @@ -1301,12 +1317,15 @@ fhandler_console::ioctl (unsigned int cmd, void *arg) DWORD n; int ret = 0; INPUT_RECORD inp[INREC_SIZE]; + acquire_attach_mutex (INFINITE); if (!PeekConsoleInputW (get_handle (), inp, INREC_SIZE, &n)) { set_errno (EINVAL); + release_attach_mutex (); release_output_mutex (); return -1; } + release_attach_mutex (); bool saw_eol = false; for (DWORD i=0; ic_lflag, t->c_iflag); } + release_attach_mutex (); get_ttyp ()->rstcons (false); release_input_mutex (); @@ -1481,6 +1506,7 @@ fhandler_console::tcgetattr (struct termios *t) DWORD flags; + acquire_attach_mutex (INFINITE); if (!GetConsoleMode (get_handle (), &flags)) { __seterrno (); @@ -1505,6 +1531,7 @@ fhandler_console::tcgetattr (struct termios *t) /* All the output bits we can ignore */ res = 0; } + release_attach_mutex (); syscall_printf ("%d = tcgetattr(%p) enable flags %y, t->lflag %y, t->iflag %y", res, t, flags, t->c_lflag, t->c_iflag); return res;