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.
This commit is contained in:
Takashi Yano via Cygwin-patches 2021-01-25 18:18:11 +09:00 committed by Corinna Vinschen
parent cb41c375a6
commit 460eb128cb
1 changed files with 27 additions and 0 deletions

View File

@ -439,14 +439,18 @@ fhandler_console::set_raw_win32_keyboard_mode (bool new_mode)
void void
fhandler_console::set_cursor_maybe () fhandler_console::set_cursor_maybe ()
{ {
acquire_attach_mutex (INFINITE);
con.fillin (get_output_handle ()); con.fillin (get_output_handle ());
release_attach_mutex ();
/* Nothing to do for xterm compatible mode. */ /* Nothing to do for xterm compatible mode. */
if (wincap.has_con_24bit_colors () && !con_is_legacy) if (wincap.has_con_24bit_colors () && !con_is_legacy)
return; return;
if (con.dwLastCursorPosition.X != con.b.dwCursorPosition.X || if (con.dwLastCursorPosition.X != con.b.dwCursorPosition.X ||
con.dwLastCursorPosition.Y != con.b.dwCursorPosition.Y) con.dwLastCursorPosition.Y != con.b.dwCursorPosition.Y)
{ {
acquire_attach_mutex (INFINITE);
SetConsoleCursorPosition (get_output_handle (), con.b.dwCursorPosition); SetConsoleCursorPosition (get_output_handle (), con.b.dwCursorPosition);
release_attach_mutex ();
con.dwLastCursorPosition = con.b.dwCursorPosition; 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 */ set_cursor_maybe (); /* to make cursor appear on the screen immediately */
wait_retry:
switch (cygwait (get_handle (), timeout)) switch (cygwait (get_handle (), timeout))
{ {
case WAIT_OBJECT_0: case WAIT_OBJECT_0:
@ -551,6 +556,15 @@ fhandler_console::read (void *pv, size_t& buflen)
buflen = (size_t) -1; buflen = (size_t) -1;
return; return;
default: 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; goto err;
} }
@ -1243,7 +1257,9 @@ fhandler_console::ioctl (unsigned int cmd, void *arg)
case TIOCGWINSZ: case TIOCGWINSZ:
int st; int st;
acquire_attach_mutex (INFINITE);
st = con.fillin (get_output_handle ()); st = con.fillin (get_output_handle ());
release_attach_mutex ();
if (st) if (st)
{ {
/* *not* the buffer size, the actual screen size... */ /* *not* the buffer size, the actual screen size... */
@ -1301,12 +1317,15 @@ fhandler_console::ioctl (unsigned int cmd, void *arg)
DWORD n; DWORD n;
int ret = 0; int ret = 0;
INPUT_RECORD inp[INREC_SIZE]; INPUT_RECORD inp[INREC_SIZE];
acquire_attach_mutex (INFINITE);
if (!PeekConsoleInputW (get_handle (), inp, INREC_SIZE, &n)) if (!PeekConsoleInputW (get_handle (), inp, INREC_SIZE, &n))
{ {
set_errno (EINVAL); set_errno (EINVAL);
release_attach_mutex ();
release_output_mutex (); release_output_mutex ();
return -1; return -1;
} }
release_attach_mutex ();
bool saw_eol = false; bool saw_eol = false;
for (DWORD i=0; i<n; i++) for (DWORD i=0; i<n; i++)
if (inp[i].EventType == KEY_EVENT && if (inp[i].EventType == KEY_EVENT &&
@ -1357,11 +1376,13 @@ fhandler_console::tcflush (int queue)
if (queue == TCIFLUSH if (queue == TCIFLUSH
|| queue == TCIOFLUSH) || queue == TCIOFLUSH)
{ {
acquire_attach_mutex (INFINITE);
if (!FlushConsoleInputBuffer (get_handle ())) if (!FlushConsoleInputBuffer (get_handle ()))
{ {
__seterrno (); __seterrno ();
res = -1; res = -1;
} }
release_attach_mutex ();
} }
return res; return res;
} }
@ -1375,12 +1396,14 @@ fhandler_console::output_tcsetattr (int, struct termios const *t)
DWORD flags = ENABLE_PROCESSED_OUTPUT | ENABLE_WRAP_AT_EOL_OUTPUT; DWORD flags = ENABLE_PROCESSED_OUTPUT | ENABLE_WRAP_AT_EOL_OUTPUT;
DWORD oflags; DWORD oflags;
acquire_attach_mutex (INFINITE);
GetConsoleMode (get_output_handle (), &oflags); GetConsoleMode (get_output_handle (), &oflags);
if (wincap.has_con_24bit_colors () && !con_is_legacy if (wincap.has_con_24bit_colors () && !con_is_legacy
&& (oflags & ENABLE_VIRTUAL_TERMINAL_PROCESSING)) && (oflags & ENABLE_VIRTUAL_TERMINAL_PROCESSING))
flags |= ENABLE_VIRTUAL_TERMINAL_PROCESSING; flags |= ENABLE_VIRTUAL_TERMINAL_PROCESSING;
int res = SetConsoleMode (get_output_handle (), flags) ? 0 : -1; int res = SetConsoleMode (get_output_handle (), flags) ? 0 : -1;
release_attach_mutex ();
if (res) if (res)
__seterrno_from_win_error (GetLastError ()); __seterrno_from_win_error (GetLastError ());
release_output_mutex (); release_output_mutex ();
@ -1398,6 +1421,7 @@ fhandler_console::input_tcsetattr (int, struct termios const *t)
DWORD oflags; DWORD oflags;
acquire_attach_mutex (INFINITE);
if (!GetConsoleMode (get_handle (), &oflags)) if (!GetConsoleMode (get_handle (), &oflags))
oflags = 0; oflags = 0;
DWORD flags = 0; DWORD flags = 0;
@ -1456,6 +1480,7 @@ fhandler_console::input_tcsetattr (int, struct termios const *t)
syscall_printf ("%d = tcsetattr(,%p) enable flags %y, c_lflag %y iflag %y", syscall_printf ("%d = tcsetattr(,%p) enable flags %y, c_lflag %y iflag %y",
res, t, flags, t->c_lflag, t->c_iflag); res, t, flags, t->c_lflag, t->c_iflag);
} }
release_attach_mutex ();
get_ttyp ()->rstcons (false); get_ttyp ()->rstcons (false);
release_input_mutex (); release_input_mutex ();
@ -1481,6 +1506,7 @@ fhandler_console::tcgetattr (struct termios *t)
DWORD flags; DWORD flags;
acquire_attach_mutex (INFINITE);
if (!GetConsoleMode (get_handle (), &flags)) if (!GetConsoleMode (get_handle (), &flags))
{ {
__seterrno (); __seterrno ();
@ -1505,6 +1531,7 @@ fhandler_console::tcgetattr (struct termios *t)
/* All the output bits we can ignore */ /* All the output bits we can ignore */
res = 0; res = 0;
} }
release_attach_mutex ();
syscall_printf ("%d = tcgetattr(%p) enable flags %y, t->lflag %y, t->iflag %y", syscall_printf ("%d = tcgetattr(%p) enable flags %y, t->lflag %y, t->iflag %y",
res, t, flags, t->c_lflag, t->c_iflag); res, t, flags, t->c_lflag, t->c_iflag);
return res; return res;