From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 7868) id CF2D83858D32; Sun, 8 May 2022 15:47:12 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org CF2D83858D32 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: pty: Avoid deadlock when pcon is started on console. X-Act-Checkin: newlib-cygwin X-Git-Author: Takashi Yano X-Git-Refname: refs/heads/master X-Git-Oldrev: aea6940043826f88f768d638dbbc6896ab9be494 X-Git-Newrev: cc94490f6e48b01f820e33d2461d05e72f1343bb Message-Id: <20220508154712.CF2D83858D32@sourceware.org> Date: Sun, 8 May 2022 15:47:12 +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: Sun, 08 May 2022 15:47:12 -0000 https://sourceware.org/git/gitweb.cgi?p=3Dnewlib-cygwin.git;h=3Dcc94490f6e4= 8b01f820e33d2461d05e72f1343bb commit cc94490f6e48b01f820e33d2461d05e72f1343bb Author: Takashi Yano Date: Mon May 9 00:19:47 2022 +0900 Cygwin: pty: Avoid deadlock when pcon is started on console. =20 - Previously, "env SHELL=3Dcmd script" command in console caused deadlock when starting cmd.exe. This patch fixes the issue. Diff: --- winsup/cygwin/fhandler_tty.cc | 34 +++++++++++++++++----------------- winsup/cygwin/select.cc | 2 -- 2 files changed, 17 insertions(+), 19 deletions(-) diff --git a/winsup/cygwin/fhandler_tty.cc b/winsup/cygwin/fhandler_tty.cc index 9ab681d6c..f6a7a6cf9 100644 --- a/winsup/cygwin/fhandler_tty.cc +++ b/winsup/cygwin/fhandler_tty.cc @@ -1171,11 +1171,7 @@ fhandler_pty_slave::reset_switch_to_nat_pipe (void) bool need_restore_handles =3D get_ttyp ()->pcon_activated; WaitForSingleObject (pipe_sw_mutex, INFINITE); if (get_ttyp ()->pcon_activated) - { - acquire_attach_mutex (mutex_timeout); - close_pseudoconsole (get_ttyp ()); - release_attach_mutex (); - } + close_pseudoconsole (get_ttyp ()); else hand_over_only (get_ttyp ()); ReleaseMutex (pipe_sw_mutex); @@ -3244,9 +3240,11 @@ fhandler_pty_slave::setup_pseudoconsole () GetCurrentProcess (), &hpConOut, 0, TRUE, DUPLICATE_SAME_ACCESS); CloseHandle (pcon_owner); + acquire_attach_mutex (mutex_timeout); FreeConsole (); AttachConsole (get_ttyp ()->nat_pipe_owner_pid); init_console_handler (false); + release_attach_mutex (); goto skip_create; } =20 @@ -3368,9 +3366,11 @@ fhandler_pty_slave::setup_pseudoconsole () HeapFree (GetProcessHeap (), 0, si.lpAttributeList); =20 /* Attach to pseudo console */ + acquire_attach_mutex (mutex_timeout); FreeConsole (); AttachConsole (pi.dwProcessId); init_console_handler (false); + release_attach_mutex (); =20 /* Terminate helper process */ SetEvent (goodbye); @@ -3535,8 +3535,10 @@ void fhandler_pty_slave::close_pseudoconsole (tty *ttyp, DWORD force_switch_to) { DWORD switch_to =3D get_winpid_to_hand_over (ttyp, force_switch_to); + acquire_attach_mutex (mutex_timeout); ttyp->previous_code_page =3D GetConsoleCP (); ttyp->previous_output_code_page =3D GetConsoleOutputCP (); + release_attach_mutex (); if (nat_pipe_owner_self (ttyp->nat_pipe_owner_pid)) { /* I am owner of the nat pipe. */ if (switch_to) @@ -3578,19 +3580,23 @@ fhandler_pty_slave::close_pseudoconsole (tty *ttyp,= DWORD force_switch_to) ttyp->h_pcon_conhost_process =3D new_conhost_process; ttyp->h_pcon_in =3D new_pcon_in; ttyp->h_pcon_out =3D new_pcon_out; + acquire_attach_mutex (mutex_timeout); FreeConsole (); pinfo p (myself->ppid); if (!p || !AttachConsole (p->dwProcessId)) AttachConsole (ATTACH_PARENT_PROCESS); init_console_handler (false); + release_attach_mutex (); } else { /* Close pseudo console and abandon the ownership of the nat pipe. */ + acquire_attach_mutex (mutex_timeout); FreeConsole (); pinfo p (myself->ppid); if (!p || !AttachConsole (p->dwProcessId)) AttachConsole (ATTACH_PARENT_PROCESS); init_console_handler (false); + release_attach_mutex (); /* Reconstruct pseudo console handler container here for close */ HPCON_INTERNAL *hp =3D (HPCON_INTERNAL *) HeapAlloc (GetProcessHeap (), 0, @@ -3610,11 +3616,13 @@ fhandler_pty_slave::close_pseudoconsole (tty *ttyp,= DWORD force_switch_to) } else { /* Just detach from the pseudo console if I am not owner. */ + acquire_attach_mutex (mutex_timeout); FreeConsole (); pinfo p (myself->ppid); if (!p || !AttachConsole (p->dwProcessId)) AttachConsole (ATTACH_PARENT_PROCESS); init_console_handler (false); + release_attach_mutex (); } } =20 @@ -4040,11 +4048,7 @@ fhandler_pty_slave::setup_for_non_cygwin_app (bool n= opcon, PWCHAR envblock, } bool pcon_enabled =3D false; if (!nopcon) - { - acquire_attach_mutex (mutex_timeout); - pcon_enabled =3D setup_pseudoconsole (); - release_attach_mutex (); - } + pcon_enabled =3D setup_pseudoconsole (); ReleaseMutex (pipe_sw_mutex); /* For pcon enabled case, transfer_input() is called in master::write() = */ if (!pcon_enabled && get_ttyp ()->getpgid () =3D=3D myself->pgid @@ -4077,11 +4081,7 @@ fhandler_pty_slave::cleanup_for_non_cygwin_app (hand= le_set_t *p, tty *ttyp, } WaitForSingleObject (p->pipe_sw_mutex, INFINITE); if (ttyp->pcon_activated) - { - acquire_attach_mutex (mutex_timeout); - close_pseudoconsole (ttyp, force_switch_to); - release_attach_mutex (); - } + close_pseudoconsole (ttyp, force_switch_to); else hand_over_only (ttyp, force_switch_to); ReleaseMutex (p->pipe_sw_mutex); @@ -4111,6 +4111,7 @@ fhandler_pty_slave::setpgid_aux (pid_t pid) bool attach_restore =3D false; HANDLE from =3D get_handle_nat (); DWORD resume_pid =3D 0; + WaitForSingleObject (input_mutex, mutex_timeout); if (get_ttyp ()->pcon_activated && get_ttyp ()->nat_pipe_owner_pid && !get_console_process_id (get_ttyp ()->nat_pipe_owner_pid, true)) { @@ -4126,13 +4127,12 @@ fhandler_pty_slave::setpgid_aux (pid_t pid) } else acquire_attach_mutex (mutex_timeout); - WaitForSingleObject (input_mutex, mutex_timeout); transfer_input (tty::to_cyg, from, get_ttyp (), input_available_even= t); - ReleaseMutex (input_mutex); if (attach_restore) resume_from_temporarily_attach (resume_pid); else release_attach_mutex (); + ReleaseMutex (input_mutex); } ReleaseMutex (pipe_sw_mutex); } diff --git a/winsup/cygwin/select.cc b/winsup/cygwin/select.cc index 4f23dfdef..0fa60bcdb 100644 --- a/winsup/cygwin/select.cc +++ b/winsup/cygwin/select.cc @@ -1125,7 +1125,6 @@ peek_console (select_record *me, bool) { if (fh->bg_check (SIGTTIN, true) <=3D bg_eof) { - release_attach_mutex (); fh->release_input_mutex (); return me->read_ready =3D true; } @@ -1142,7 +1141,6 @@ peek_console (select_record *me, bool) && global_sigs[SIGWINCH].sa_handler !=3D SIG_DFL) { set_sig_errno (EINTR); - release_attach_mutex (); fh->release_input_mutex (); return -1; }