git@vger.kernel.org mailing list mirror (one of many)
 help / color / mirror / code / Atom feed
From: Phillip Wood <phillip.wood123@gmail.com>
To: Git Mailing List <git@vger.kernel.org>
Cc: "Phillip Wood" <phillip.wood@dunelm.org.uk>,
	"Ævar Arnfjörð Bjarmason" <avarab@gmail.com>,
	"Carlo Arenas" <carenas@gmail.com>,
	"Johannes Schindelin" <Johannes.Schindelin@gmx.de>,
	"Ramsay Jones" <ramsay@ramsayjones.plus.com>
Subject: [PATCH v2 4/4] terminal: restore settings on SIGTSTP
Date: Wed,  9 Mar 2022 11:03:25 +0000	[thread overview]
Message-ID: <20220309110325.36917-5-phillip.wood123@gmail.com> (raw)
In-Reply-To: <20220309110325.36917-1-phillip.wood123@gmail.com>

From: Phillip Wood <phillip.wood@dunelm.org.uk>

If the user suspends git while it is waiting for a keypress reset the
terminal before stopping and restore the settings when git resumes. If
the user tries to resume in the background print an error
message (taking care to use async safe functions) before stopping
again. Ideally we would reprint the prompt for the user when git
resumes but this patch just restarts the read().

The signal handler is established with sigaction() rather than using
sigchain_push() as this allows us to control the signal mask when the
handler is invoked and ensure SA_RESTART is used to restart the
read() when resuming.

Signed-off-by: Phillip Wood <phillip.wood@dunelm.org.uk>
---
 compat/terminal.c | 125 ++++++++++++++++++++++++++++++++++++++++++++--
 1 file changed, 121 insertions(+), 4 deletions(-)

diff --git a/compat/terminal.c b/compat/terminal.c
index 4189cbb12c..f425f7b339 100644
--- a/compat/terminal.c
+++ b/compat/terminal.c
@@ -1,4 +1,4 @@
-#include "git-compat-util.h"
+#include "cache.h"
 #include "compat/terminal.h"
 #include "sigchain.h"
 #include "strbuf.h"
@@ -23,6 +23,90 @@ static void restore_term_on_signal(int sig)
 static int term_fd = -1;
 static struct termios old_term;
 
+static const char *background_resume_msg;
+static const char *restore_error_msg;
+static volatile sig_atomic_t ttou_received;
+
+static void write_err(const char *msg)
+{
+	write_in_full(2, "error: ", strlen("error: "));
+	write_in_full(2, msg, strlen(msg));
+	write_in_full(2, "\n", 1);
+}
+
+static void print_background_resume_msg(int signo)
+{
+	int saved_errno = errno;
+	sigset_t mask;
+	struct sigaction old_sa;
+	struct sigaction sa = { .sa_handler = SIG_DFL };
+
+	ttou_received = 1;
+	write_err(background_resume_msg);
+	sigaction(signo, &sa, &old_sa);
+	raise(signo);
+	sigemptyset(&mask);
+	sigaddset(&mask, signo);
+	sigprocmask(SIG_UNBLOCK, &mask, NULL);
+	/* Stopped here */
+	sigprocmask(SIG_BLOCK, &mask, NULL);
+	sigaction(signo, &old_sa, NULL);
+	errno = saved_errno;
+}
+
+static void restore_terminal_on_suspend(int signo)
+{
+	int saved_errno = errno;
+	int res;
+	struct termios t;
+	sigset_t mask;
+	struct sigaction old_sa;
+	struct sigaction sa = { .sa_handler = SIG_DFL };
+	int can_restore = 1;
+
+	if (tcgetattr(term_fd, &t) < 0)
+		can_restore = 0;
+
+	if (tcsetattr(term_fd, TCSAFLUSH, &old_term) < 0)
+		write_err(restore_error_msg);
+
+	sigaction(signo, &sa, &old_sa);
+	raise(signo);
+	sigemptyset(&mask);
+	sigaddset(&mask, signo);
+	sigprocmask(SIG_UNBLOCK, &mask, NULL);
+	/* Stopped here */
+	sigprocmask(SIG_BLOCK, &mask, NULL);
+	sigaction(signo, &old_sa, NULL);
+	if (!can_restore) {
+		write_err(restore_error_msg);
+		goto out;
+	}
+	/*
+	 * If we resume in the background then we receive SIGTTOU when calling
+	 * tcsetattr() below. Set up a handler to print an error message in that
+	 * case.
+	 */
+	sigemptyset(&mask);
+	sigaddset(&mask, SIGTTOU);
+	sa.sa_mask = old_sa.sa_mask;
+	sa.sa_handler = print_background_resume_msg;
+	sa.sa_flags = SA_RESTART;
+	sigaction(SIGTTOU, &sa, &old_sa);
+ again:
+	ttou_received = 0;
+	sigprocmask(SIG_UNBLOCK, &mask, NULL);
+	res = tcsetattr(term_fd, TCSAFLUSH, &t);
+	sigprocmask(SIG_BLOCK, &mask, NULL);
+	if (ttou_received)
+		goto again;
+	else if (res < 0)
+		write_err(restore_error_msg);
+	sigaction(SIGTTOU, &old_sa, NULL);
+ out:
+	errno = saved_errno;
+}
+
 void restore_term(void)
 {
 	if (term_fd < 0)
@@ -32,10 +116,19 @@ void restore_term(void)
 	close(term_fd);
 	term_fd = -1;
 	sigchain_pop_common();
+	if (restore_error_msg) {
+		signal(SIGTTIN, SIG_DFL);
+		signal(SIGTTOU, SIG_DFL);
+		signal(SIGTSTP, SIG_DFL);
+		restore_error_msg = NULL;
+		background_resume_msg = NULL;
+	}
 }
 
 int save_term(enum save_term_flags flags)
 {
+	struct sigaction sa;
+
 	if (term_fd < 0)
 		term_fd = (flags & SAVE_TERM_STDIN) ? 0
 						    : open("/dev/tty", O_RDWR);
@@ -44,6 +137,26 @@ int save_term(enum save_term_flags flags)
 	if (tcgetattr(term_fd, &old_term) < 0)
 		return -1;
 	sigchain_push_common(restore_term_on_signal);
+	/*
+	 * If job control is disabled then the shell will have set the
+	 * disposition of SIGTSTP to SIG_IGN.
+	 */
+	sigaction(SIGTSTP, NULL, &sa);
+	if (sa.sa_handler == SIG_IGN)
+		return 0;
+
+	/* avoid calling gettext() from signal handler */
+	background_resume_msg = _("cannot resume in the background, please use 'fg' to resume");
+	restore_error_msg = _("cannot restore terminal settings");
+	sa.sa_handler = restore_terminal_on_suspend;
+	sa.sa_flags = SA_RESTART;
+	sigemptyset(&sa.sa_mask);
+	sigaddset(&sa.sa_mask, SIGTSTP);
+	sigaddset(&sa.sa_mask, SIGTTIN);
+	sigaddset(&sa.sa_mask, SIGTTOU);
+	sigaction(SIGTSTP, &sa, NULL);
+	sigaction(SIGTTIN, &sa, NULL);
+	sigaction(SIGTTOU, &sa, NULL);
 
 	return 0;
 }
@@ -93,6 +206,7 @@ static int getchar_with_timeout(int timeout)
 	fd_set readfds;
 	int res;
 
+ again:
 	if (timeout >= 0) {
 		tv.tv_sec = timeout / 1000;
 		tv.tv_usec = (timeout % 1000) * 1000;
@@ -102,9 +216,12 @@ static int getchar_with_timeout(int timeout)
 	FD_ZERO(&readfds);
 	FD_SET(0, &readfds);
 	res = select(1, &readfds, NULL, NULL, tvp);
-	if (res < 0)
-		return EOF;
-
+	if (res < 0) {
+		if (errno == EINTR)
+			goto again;
+		else
+			return EOF;
+	}
 	return getchar();
 }
 
-- 
2.35.1


  parent reply	other threads:[~2022-03-09 11:04 UTC|newest]

Thread overview: 53+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-03-04 13:11 [PATCH 0/4] builtin add -p: hopefully final readkey fixes Phillip Wood
2022-03-04 13:11 ` [PATCH 1/4] terminal: use flags for save_term() Phillip Wood
2022-03-04 20:40   ` Ramsay Jones
2022-03-07 11:11     ` Phillip Wood
2022-03-07 20:21       ` Ramsay Jones
2022-03-08 10:41         ` Phillip Wood
2022-03-05 14:02   ` Ævar Arnfjörð Bjarmason
2022-03-07 10:45     ` Phillip Wood
2022-03-07 12:06       ` Ævar Arnfjörð Bjarmason
2022-03-04 13:11 ` [PATCH 2/4] terminal: don't assume stdin is /dev/tty Phillip Wood
2022-03-04 20:42   ` Ramsay Jones
2022-03-04 13:11 ` [PATCH 3/4] terminal: work around macos poll() bug Phillip Wood
2022-03-04 13:11 ` [PATCH 4/4] terminal: restore settings on SIGTSTP Phillip Wood
2022-03-05 13:59   ` Ævar Arnfjörð Bjarmason
2022-03-07 10:53     ` Phillip Wood
2022-03-07 11:49       ` Ævar Arnfjörð Bjarmason
2022-03-07 13:49         ` Phillip Wood
2022-03-07 14:45           ` Ævar Arnfjörð Bjarmason
2022-03-08 10:54             ` Phillip Wood
2022-03-09 12:19   ` Johannes Schindelin
2022-03-10 16:06     ` Phillip Wood
2022-03-09 11:03 ` [PATCH v2 0/4] builtin add -p: hopefully final readkey fixes Phillip Wood
2022-03-09 11:03   ` [PATCH v2 1/4] terminal: use flags for save_term() Phillip Wood
2022-03-11 16:52     ` Carlo Arenas
2022-03-14 10:49       ` Phillip Wood
2022-03-09 11:03   ` [PATCH v2 2/4] terminal: don't assume stdin is /dev/tty Phillip Wood
2022-03-09 11:03   ` [PATCH v2 3/4] terminal: work around macos poll() bug Phillip Wood
2022-03-10 13:35     ` Ævar Arnfjörð Bjarmason
2022-03-10 16:02       ` Phillip Wood
2022-03-10 18:02         ` Junio C Hamano
2022-03-09 11:03   ` Phillip Wood [this message]
2022-03-09 23:10   ` [PATCH v2 0/4] builtin add -p: hopefully final readkey fixes Junio C Hamano
2022-03-09 23:37     ` Junio C Hamano
2022-03-10 13:28       ` Phillip Wood
2022-03-10 18:18         ` Phillip Wood
2022-03-10 18:53           ` Junio C Hamano
2022-03-10 13:25   ` Johannes Schindelin
2022-03-10 16:08     ` Phillip Wood
2022-03-15 10:57 ` [PATCH v3 " Phillip Wood
2022-03-15 10:57   ` [PATCH v3 1/4] terminal: use flags for save_term() Phillip Wood
2022-03-15 10:57   ` [PATCH v3 2/4] terminal: don't assume stdin is /dev/tty Phillip Wood
2022-03-15 17:42     ` Junio C Hamano
2022-03-15 18:01       ` rsbecker
2022-03-15 19:05         ` Junio C Hamano
2022-03-15 19:38           ` rsbecker
2022-03-15 10:57   ` [PATCH v3 3/4] terminal: work around macos poll() bug Phillip Wood
2022-03-15 10:57   ` [PATCH v3 4/4] terminal: restore settings on SIGTSTP Phillip Wood
2022-03-15 17:51     ` Junio C Hamano
2022-03-16 18:54 ` [PATCH v4 0/4] builtin add -p: hopefully final readkey fixes Phillip Wood
2022-03-16 18:54   ` [PATCH v4 1/4] terminal: use flags for save_term() Phillip Wood
2022-03-16 18:54   ` [PATCH v4 2/4] terminal: don't assume stdin is /dev/tty Phillip Wood
2022-03-16 18:54   ` [PATCH v4 3/4] terminal: work around macos poll() bug Phillip Wood
2022-03-16 18:54   ` [PATCH v4 4/4] terminal: restore settings on SIGTSTP Phillip Wood

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: http://vger.kernel.org/majordomo-info.html

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20220309110325.36917-5-phillip.wood123@gmail.com \
    --to=phillip.wood123@gmail.com \
    --cc=Johannes.Schindelin@gmx.de \
    --cc=avarab@gmail.com \
    --cc=carenas@gmail.com \
    --cc=git@vger.kernel.org \
    --cc=phillip.wood@dunelm.org.uk \
    --cc=ramsay@ramsayjones.plus.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://80x24.org/mirrors/git.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).