From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 7868) id BA2013858D35; Sun, 27 Feb 2022 11:53:19 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org BA2013858D35 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: termios: Add comments to the code refactored recently. X-Act-Checkin: newlib-cygwin X-Git-Author: Takashi Yano X-Git-Refname: refs/heads/master X-Git-Oldrev: d857df6f088dc8402ec45bb5a15af2aaca254d64 X-Git-Newrev: 33d8578feb51f807e89d9592d5aaf28e88df4e0d Message-Id: <20220227115319.BA2013858D35@sourceware.org> Date: Sun, 27 Feb 2022 11:53:19 +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, 27 Feb 2022 11:53:19 -0000 https://sourceware.org/git/gitweb.cgi?p=3Dnewlib-cygwin.git;h=3D33d8578feb5= 1f807e89d9592d5aaf28e88df4e0d commit 33d8578feb51f807e89d9592d5aaf28e88df4e0d Author: Takashi Yano Date: Sun Feb 27 20:40:24 2022 +0900 Cygwin: termios: Add comments to the code refactored recently. =20 - This patch adds some comments to the source code whose intent may not be clear enough from the code which is refactored recently. Diff: --- winsup/cygwin/fhandler.h | 13 +++++---- winsup/cygwin/fhandler_termios.cc | 60 ++++++++++++++++++++++++++++-------= ---- 2 files changed, 52 insertions(+), 21 deletions(-) diff --git a/winsup/cygwin/fhandler.h b/winsup/cygwin/fhandler.h index fda5a4ec9..abd62d72a 100644 --- a/winsup/cygwin/fhandler.h +++ b/winsup/cygwin/fhandler.h @@ -1902,12 +1902,15 @@ class fhandler_termios: public fhandler_base virtual void release_input_mutex_if_necessary (void) {}; virtual void discard_input () {}; =20 + /* Result status of processing keys in process_sigs(). */ enum process_sig_state { - signalled, - not_signalled, - not_signalled_but_done, - not_signalled_with_nat_reader, - done_with_debugger + signalled, /* Signalled normally */ + not_signalled, /* Not signalled at all */ + not_signalled_but_done, /* Not signalled, but CTRL_C_EVENT was sent. */ + not_signalled_with_nat_reader, /* Not signalled, but detected non-cygw= in + process may be reading the tty. */ + done_with_debugger /* The key was processed (CTRL_C_EVENT was sent) + for inferior of GDB. */ }; =20 public: diff --git a/winsup/cygwin/fhandler_termios.cc b/winsup/cygwin/fhandler_ter= mios.cc index 767b28302..f82ac76dc 100644 --- a/winsup/cygwin/fhandler_termios.cc +++ b/winsup/cygwin/fhandler_termios.cc @@ -149,7 +149,8 @@ tty_min::kill_pgrp (int sig, pid_t target_pgid) if (!p || !p->exists () || p->ctty !=3D ntty || p->pgid !=3D target_= pgid) continue; if (p->process_state & PID_NOTCYGWIN) - continue; + continue; /* Do not send signal to non-cygwin process to prevent + cmd.exe from crash. */ if (p =3D=3D myself) killself =3D sig !=3D __SIGSETPGRP && !exit_state; else @@ -309,32 +310,52 @@ fhandler_termios::echo_erase (int force) doecho ("\b \b", 3); } =20 +/* The basic policy is as follows: + - The signal generated by key press will be sent only to cygwin process. + - For non-cygwin process, CTRL_C_EVENT will be sent on Ctrl-C. */ +/* The inferior of GDB is an exception. GDB does not support to hook signal + even if the inferior is a cygwin app. As a result, inferior cannot be + continued after interruption by Ctrl-C if SIGINT was sent. Therefore, + CTRL_C_EVENT rather than SIGINT is sent to the inferior of GDB. */ fhandler_termios::process_sig_state fhandler_termios::process_sigs (char c, tty* ttyp, fhandler_termios *fh) { termios &ti =3D ttyp->ti; pid_t pgid =3D ttyp->pgid; =20 + /* The name *_nat stands for 'native' which means non-cygwin apps. */ pinfo leader (pgid); - bool cyg_leader =3D leader && !(leader->process_state & PID_NOTCYGWIN); + bool cyg_leader =3D /* The process leader is a cygwin process. */ + leader && !(leader->process_state & PID_NOTCYGWIN); bool ctrl_c_event_sent =3D false; bool need_discard_input =3D false; - bool pg_with_nat =3D false; - bool need_send_sig =3D false; - bool nat_shell =3D false; - bool cyg_reader =3D false; - bool with_debugger =3D false; - bool with_debugger_nat =3D false; + bool pg_with_nat =3D false; /* The process group has non-cygwin processe= s. */ + bool need_send_sig =3D false; /* There is process which need the signal.= */ + bool nat_shell =3D false; /* The shell seems to be a non-cygwin process.= */ + bool cyg_reader =3D false; /* Cygwin process is reading the tty. */ + bool with_debugger =3D false; /* GDB is debugging cygwin app. */ + bool with_debugger_nat =3D false; /* GDB is debugging non-cygwin app. */ =20 winpids pids ((DWORD) 0); for (unsigned i =3D 0; i < pids.npids; i++) { _pinfo *p =3D pids[i]; + /* PID_NOTCYGWIN: check this for non-cygwin process. + PID_NEW_PG: check this ofr GDB with non-cygwin inferior in pty + without pcon enabled. In this case, the inferior is not + cygwin process list. PID_NEW_PG is set as a marker for + GDB with non-cygwin inferior in pty code. + !PID_CYGPARENT: check this for GDB with cygwin inferior. */ if (c =3D=3D '\003' && p && p->ctty =3D=3D ttyp->ntty && p->pgid =3D= =3D pgid && ((p->process_state & PID_NOTCYGWIN) || (p->process_state & PID_NEW_PG) || !(p->process_state & PID_CYGPARENT))) { + /* Ctrl-C event will be sent only to the processes attaching + to the same console. Therefore, attach to the console to + which the target process is attaching before sending the + CTRL_C_EVENT. After sending the event, reattach to the + console to which the process was previously attached. */ pinfo pinfo_resume =3D pinfo (myself->ppid); DWORD resume_pid =3D 0; if (pinfo_resume) @@ -361,7 +382,8 @@ fhandler_termios::process_sigs (char c, tty* ttyp, fhan= dler_termios *fh) && (p->process_state & PID_NOTCYGWIN)) GenerateConsoleCtrlEvent (CTRL_BREAK_EVENT, p->dwProcessId); else if ((!fh || fh->need_send_ctrl_c_event () || cyg_leader) - && !ctrl_c_event_sent) + && !ctrl_c_event_sent) /* cyg_leader is needed by GDB + with non-cygwin inferior */ { GenerateConsoleCtrlEvent (CTRL_C_EVENT, 0); ctrl_c_event_sent =3D true; @@ -378,13 +400,13 @@ fhandler_termios::process_sigs (char c, tty* ttyp, fh= andler_termios *fh) if (p && p->ctty =3D=3D ttyp->ntty && p->pgid =3D=3D pgid) { if (p->process_state & PID_NOTCYGWIN) - pg_with_nat =3D true; + pg_with_nat =3D true; /* The process group has non-cygwin process */ if (!(p->process_state & PID_NOTCYGWIN)) - need_send_sig =3D true; + need_send_sig =3D true; /* Process which needs signal exists */ if (!p->cygstarted) - nat_shell =3D true; + nat_shell =3D true; /* The shell seems to a non-cygwin shell */ if (p->process_state & PID_TTYIN) - cyg_reader =3D true; + cyg_reader =3D true; /* Theh process is reading the tty */ if (!p->cygstarted && !(p->process_state & PID_NOTCYGWIN) && (p !=3D myself || being_debugged ()) && cyg_leader) /* inferior is cygwin app */ @@ -406,8 +428,12 @@ fhandler_termios::process_sigs (char c, tty* ttyp, fha= ndler_termios *fh) return done_with_debugger; } if (with_debugger_nat) - return not_signalled; - /* Send SIGQUIT to non-cygwin process. */ + return not_signalled; /* Do not process slgnal keys further. + The non-cygwin inferior of GDB cannot receive + the signals. */ + /* Send SIGQUIT to non-cygwin process. Non-cygwin app will not be alerted + by kill_pgrp(), however, QUIT key should quit the non-cygwin app + if it is started along with cygwin process from cygwin shell. */ if ((ti.c_lflag & ISIG) && CCEQ (ti.c_cc[VQUIT], c) && pg_with_nat && need_send_sig && !nat_shell) { @@ -426,7 +452,9 @@ fhandler_termios::process_sigs (char c, tty* ttyp, fhan= dler_termios *fh) sig =3D SIGINT; else if (CCEQ (ti.c_cc[VQUIT], c)) sig =3D SIGQUIT; - else if (pg_with_nat) + else if (pg_with_nat) /* If the process group has a non-cygwin proce= ss, + it cannot be suspended correctly. Therefore, + do not send SIGTSTP. */ goto not_a_sig; else if (CCEQ (ti.c_cc[VSUSP], c)) sig =3D SIGTSTP;