From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 7868) id C318D3858C54; Sun, 8 May 2022 11:07:11 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org C318D3858C54 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: pty: Fix acquiring attach_mutex timing. X-Act-Checkin: newlib-cygwin X-Git-Author: Takashi Yano X-Git-Refname: refs/heads/cygwin-3_3-branch X-Git-Oldrev: 5c7d81b75ea8dae64afb56e38c49063d5776fab9 X-Git-Newrev: ac2f7f888c3eba20941f5e0cc1c4bb5fd523e730 Message-Id: <20220508110711.C318D3858C54@sourceware.org> Date: Sun, 8 May 2022 11:07:11 +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 11:07:11 -0000 https://sourceware.org/git/gitweb.cgi?p=3Dnewlib-cygwin.git;h=3Dac2f7f888c3= eba20941f5e0cc1c4bb5fd523e730 commit ac2f7f888c3eba20941f5e0cc1c4bb5fd523e730 Author: Takashi Yano Date: Sun May 8 17:48:06 2022 +0900 Cygwin: pty: Fix acquiring attach_mutex timing. =20 - When temporarily attaching a console, the timing of acquiring attach_mutex was not appropriate. This sometimes caused master forwarding thread to crash on Ctrl-C in Windows 7. This patch fixes the issue. Diff: --- winsup/cygwin/fhandler_termios.cc | 2 +- winsup/cygwin/fhandler_tty.cc | 12 ++++++------ 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/winsup/cygwin/fhandler_termios.cc b/winsup/cygwin/fhandler_ter= mios.cc index 0d53ad25f..0bc360078 100644 --- a/winsup/cygwin/fhandler_termios.cc +++ b/winsup/cygwin/fhandler_termios.cc @@ -359,6 +359,7 @@ fhandler_termios::process_sigs (char c, tty* ttyp, fhan= dler_termios *fh) CTRL_C_EVENT. After sending the event, reattach to the console to which the process was previously attached. */ bool console_exists =3D fhandler_console::exists (); + acquire_attach_mutex (mutex_timeout); pinfo pinfo_resume =3D pinfo (myself->ppid); DWORD resume_pid =3D 0; if (pinfo_resume) @@ -366,7 +367,6 @@ fhandler_termios::process_sigs (char c, tty* ttyp, fhan= dler_termios *fh) else resume_pid =3D fhandler_pty_common::get_console_process_id (myself->dwProcessId, false); - acquire_attach_mutex (mutex_timeout); if ((!console_exists || resume_pid) && fh && !fh->is_console ()) { FreeConsole (); diff --git a/winsup/cygwin/fhandler_tty.cc b/winsup/cygwin/fhandler_tty.cc index c91f40044..b9cc9736d 100644 --- a/winsup/cygwin/fhandler_tty.cc +++ b/winsup/cygwin/fhandler_tty.cc @@ -533,6 +533,7 @@ fhandler_pty_master::accept_input () DWORD target_pid =3D 0; if (pinfo_target) target_pid =3D pinfo_target->dwProcessId; + acquire_attach_mutex (mutex_timeout); pinfo pinfo_resume =3D pinfo (myself->ppid); DWORD resume_pid; if (pinfo_resume) @@ -544,7 +545,6 @@ fhandler_pty_master::accept_input () { /* Slave attaches to a different console than master. Therefore reattach here. */ - acquire_attach_mutex (mutex_timeout); FreeConsole (); AttachConsole (target_pid); cp_to =3D GetConsoleCP (); @@ -552,10 +552,10 @@ fhandler_pty_master::accept_input () if (resume_pid && console_exists) AttachConsole (resume_pid); init_console_handler (false); - release_attach_mutex (); } else cp_to =3D GetConsoleCP (); + release_attach_mutex (); =20 if (get_ttyp ()->term_code_page !=3D cp_to) { @@ -1244,6 +1244,7 @@ fhandler_pty_slave::reset_switch_to_pcon (void) OpenProcess (PROCESS_DUP_HANDLE, FALSE, get_ttyp ()->pcon_pid); if (pcon_owner) { + acquire_attach_mutex (mutex_timeout); pinfo pinfo_resume =3D pinfo (myself->ppid); DWORD resume_pid; if (pinfo_resume) @@ -1257,7 +1258,6 @@ fhandler_pty_slave::reset_switch_to_pcon (void) DuplicateHandle (pcon_owner, get_ttyp ()->h_pcon_in, GetCurrentProcess (), &h_pcon_in, 0, TRUE, DUPLICATE_SAME_ACCESS); - acquire_attach_mutex (mutex_timeout); FreeConsole (); AttachConsole (get_ttyp ()->pcon_pid); init_console_handler (false); @@ -1268,9 +1268,9 @@ fhandler_pty_slave::reset_switch_to_pcon (void) FreeConsole (); AttachConsole (resume_pid); init_console_handler (false); - release_attach_mutex (); CloseHandle (h_pcon_in); } + release_attach_mutex (); CloseHandle (pcon_owner); } } @@ -2878,6 +2878,7 @@ fhandler_pty_master::pty_master_fwd_thread (const mas= ter_fwd_thread_param_t *p) DWORD target_pid =3D 0; if (pinfo_target) target_pid =3D pinfo_target->dwProcessId; + acquire_attach_mutex (mutex_timeout); pinfo pinfo_resume =3D pinfo (myself->ppid); DWORD resume_pid; if (pinfo_resume) @@ -2889,7 +2890,6 @@ fhandler_pty_master::pty_master_fwd_thread (const mas= ter_fwd_thread_param_t *p) { /* Slave attaches to a different console than master. Therefore reattach here. */ - acquire_attach_mutex (mutex_timeout); FreeConsole (); AttachConsole (target_pid); cp_from =3D GetConsoleOutputCP (); @@ -2897,10 +2897,10 @@ fhandler_pty_master::pty_master_fwd_thread (const m= aster_fwd_thread_param_t *p) if (resume_pid && console_exists) AttachConsole (resume_pid); init_console_handler (false); - release_attach_mutex (); } else cp_from =3D GetConsoleOutputCP (); + release_attach_mutex (); =20 if (p->ttyp->term_code_page !=3D cp_from) {