git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Phillip Wood <phillip.wood123@gmail.com>
To: "Rubén Justo" <rjusto@gmail.com>, "Git List" <git@vger.kernel.org>
Cc: Junio C Hamano <gitster@pobox.com>,
	Dragan Simic <dsimic@manjaro.org>, Jeff King <peff@peff.net>,
	Phillip Wood <phillip.wood@dunelm.org.uk>
Subject: Re: [PATCH 3/4] pager: introduce wait_for_pager
Date: Fri, 12 Jul 2024 14:17:08 +0100	[thread overview]
Message-ID: <8434fafe-f545-49bc-8cc1-d4e8fb634bec@gmail.com> (raw)
In-Reply-To: <4b5d0e7c-9492-4495-9bc1-40ebea850fde@gmail.com>

Hi Rubén

On 12/07/2024 02:00, Rubén Justo wrote:
> Since f67b45f862 (Introduce trivial new pager.c helper infrastructure,
> 2006-02-28) we have the machinery to send our output to a pager.
> 
> That machinery, once set up, does not allow us to regain the original
> stdio streams.
> 
> In the interactive commands (i.e.: add -p) we want to use the pager for
> some output, while maintaining the interaction with the user.
> 
> Modify the pager machinery so that we can use setup_pager and, once
> we've finished sending the desired output for the pager, wait for the
> pager termination using a new function wait_for_pager.   Make this
> function reset the pager machinery before returning.

This looks good and addresses my previous comments about leaking file 
descriptors and restoring signal dispositions. A couple of thoughts 
occurred to me as I was reading it again:

  - We ignore any errors when duplicating fds,
    "git grep '[^a-z_]dup2\{0,1\}(' shows that's not unusual in our
    code base, though if we cannot redirect the output to the pager or
    restore stdout when the pager exits that's a problem for "git add -p"

  - We should perhaps be marking old_fd[12] with O_CLOEXEC to stop them
    being passed to the pager.

Best Wishes

Phillip

> Signed-off-by: Rubén Justo <rjusto@gmail.com>
> ---
>   pager.c | 43 +++++++++++++++++++++++++++++++++++++------
>   pager.h |  1 +
>   2 files changed, 38 insertions(+), 6 deletions(-)
> 
> diff --git a/pager.c b/pager.c
> index 251adfc2ad..bea4345f6f 100644
> --- a/pager.c
> +++ b/pager.c
> @@ -14,7 +14,7 @@ int pager_use_color = 1;
>   
>   static struct child_process pager_process;
>   static char *pager_program;
> -static int close_fd2;
> +static int old_fd1 = -1, old_fd2 = -1;
>   
>   /* Is the value coming back from term_columns() just a guess? */
>   static int term_columns_guessed;
> @@ -24,11 +24,11 @@ static void close_pager_fds(void)
>   {
>   	/* signal EOF to pager */
>   	close(1);
> -	if (close_fd2)
> +	if (old_fd2 != -1)
>   		close(2);
>   }
>   
> -static void wait_for_pager_atexit(void)
> +static void finish_pager(void)
>   {
>   	fflush(stdout);
>   	fflush(stderr);
> @@ -36,8 +36,34 @@ static void wait_for_pager_atexit(void)
>   	finish_command(&pager_process);
>   }
>   
> +static void wait_for_pager_atexit(void)
> +{
> +	if (old_fd1 == -1)
> +		return;
> +
> +	finish_pager();
> +}
> +
> +void wait_for_pager(void)
> +{
> +	finish_pager();
> +	sigchain_pop_common();
> +	unsetenv("GIT_PAGER_IN_USE");
> +	dup2(old_fd1, 1);
> +	close(old_fd1);
> +	old_fd1 = -1;
> +	if (old_fd2 != -1) {
> +		dup2(old_fd2, 2);
> +		close(old_fd2);
> +		old_fd2 = -1;
> +	}
> +}
> +
>   static void wait_for_pager_signal(int signo)
>   {
> +	if (old_fd1 == -1)
> +		return;
> +
>   	close_pager_fds();
>   	finish_command_in_signal(&pager_process);
>   	sigchain_pop(signo);
> @@ -113,6 +139,7 @@ void prepare_pager_args(struct child_process *pager_process, const char *pager)
>   
>   void setup_pager(void)
>   {
> +	static int once = 0;
>   	const char *pager = git_pager(isatty(1));
>   
>   	if (!pager)
> @@ -142,16 +169,20 @@ void setup_pager(void)
>   		die("unable to execute pager '%s'", pager);
>   
>   	/* original process continues, but writes to the pipe */
> +	old_fd1 = dup(1);
>   	dup2(pager_process.in, 1);
>   	if (isatty(2)) {
> -		close_fd2 = 1;
> +		old_fd2 = dup(2);
>   		dup2(pager_process.in, 2);
>   	}
>   	close(pager_process.in);
>   
> -	/* this makes sure that the parent terminates after the pager */
>   	sigchain_push_common(wait_for_pager_signal);
> -	atexit(wait_for_pager_atexit);
> +
> +	if (!once) {
> +		once++;
> +		atexit(wait_for_pager_atexit);
> +	}
>   }
>   
>   int pager_in_use(void)
> diff --git a/pager.h b/pager.h
> index b77433026d..103ecac476 100644
> --- a/pager.h
> +++ b/pager.h
> @@ -5,6 +5,7 @@ struct child_process;
>   
>   const char *git_pager(int stdout_is_tty);
>   void setup_pager(void);
> +void wait_for_pager(void);
>   int pager_in_use(void);
>   int term_columns(void);
>   void term_clear_line(void);


  reply	other threads:[~2024-07-12 13:17 UTC|newest]

Thread overview: 70+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-07-12  0:57 [PATCH 0/4] use the pager in 'add -p' Rubén Justo
2024-07-12  1:00 ` [PATCH 1/4] add-patch: test for 'p' command Rubén Justo
2024-07-12  1:00 ` [PATCH 2/4] pager: do not close fd 2 unnecessarily Rubén Justo
2024-07-12  1:00 ` [PATCH 3/4] pager: introduce wait_for_pager Rubén Justo
2024-07-12 13:17   ` Phillip Wood [this message]
2024-07-12  1:00 ` [PATCH 4/4] add-patch: render hunks through the pager Rubén Justo
2024-07-12  8:58   ` Dragan Simic
2024-07-12 13:26   ` Phillip Wood
2024-07-12 16:24     ` Rubén Justo
2024-07-13  3:23       ` Rubén Justo
2024-07-13  9:12       ` Junio C Hamano
2024-07-13 13:17       ` phillip.wood123
2024-07-13 23:13         ` Rubén Justo
2024-07-12  8:56 ` [PATCH 0/4] use the pager in 'add -p' Dragan Simic
2024-07-13 16:26 ` [PATCH v2 " Rubén Justo
2024-07-13 16:29   ` [PATCH v2 1/4] add-patch: test for 'p' command Rubén Justo
2024-07-13 16:29   ` [PATCH v2 2/4] pager: do not close fd 2 unnecessarily Rubén Justo
2024-07-13 16:29   ` [PATCH v2 3/4] pager: introduce wait_for_pager Rubén Justo
2024-07-13 16:30   ` [PATCH v2 4/4] add-patch: render hunks through the pager Rubén Justo
2024-07-13 17:08   ` [PATCH v2 0/4] use the pager in 'add -p' Junio C Hamano
2024-07-13 23:21     ` Rubén Justo
2024-07-14  1:18       ` Junio C Hamano
2024-07-14 16:00   ` [PATCH v3 " Rubén Justo
2024-07-14 16:04     ` [PATCH v3 1/4] add-patch: test for 'p' command Rubén Justo
2024-07-14 16:04     ` [PATCH v3 2/4] pager: do not close fd 2 unnecessarily Rubén Justo
2024-07-14 16:04     ` [PATCH v3 4/4] add-patch: render hunks through the pager Rubén Justo
2024-07-15 14:10       ` Phillip Wood
2024-07-15 23:54       ` Junio C Hamano
2024-07-17 17:20         ` Rubén Justo
2024-07-17 19:39           ` phillip.wood123
2024-07-17 20:03             ` Junio C Hamano
2024-07-17 20:09               ` Eric Sunshine
2024-07-17 20:21                 ` Junio C Hamano
2024-07-20 22:37                 ` Rubén Justo
2024-07-22  7:18                   ` Eric Sunshine
2024-07-22 14:53                     ` Rubén Justo
2024-07-18  9:48               ` phillip.wood123
2024-07-17 20:31             ` Junio C Hamano
2024-07-18  9:56               ` phillip.wood123
2024-07-20 22:39               ` Rubén Justo
2024-07-20 22:29             ` Rubén Justo
2024-07-22 10:18               ` Phillip Wood
2024-07-22 16:45                 ` Rubén Justo
2024-07-22 17:22               ` Junio C Hamano
2024-07-22 19:06                 ` Rubén Justo
2024-07-22 19:27                   ` Junio C Hamano
2024-07-22 21:06                     ` Re* " Junio C Hamano
2024-07-22 22:00                       ` Rubén Justo
2024-07-22 23:12                       ` Kyle Lippincott
2024-07-22 23:28                         ` Junio C Hamano
2024-07-23  2:31                         ` Junio C Hamano
2024-07-22 21:25                   ` Junio C Hamano
2024-07-22 23:20                     ` Rubén Justo
2024-07-22 23:24                       ` [PATCH 1/2] t3701: avoid one-shot export for shell functions Rubén Justo
2024-07-22 23:44                         ` Junio C Hamano
2024-07-22 23:57                           ` Junio C Hamano
2024-07-22 23:24                       ` [PATCH 2/2] pager: make wait_for_pager a no-op for "cat" Rubén Justo
2024-07-22 23:54                         ` Junio C Hamano
2024-07-18  9:58           ` [PATCH v3 4/4] add-patch: render hunks through the pager phillip.wood123
2024-07-20 22:45             ` Rubén Justo
2024-07-14 16:04     ` [PATCH v3 3/4] pager: introduce wait_for_pager Rubén Justo
2024-07-15 14:13       ` Phillip Wood
2024-07-15 20:04         ` Rubén Justo
2024-07-17 14:58           ` phillip.wood123
2024-07-15 20:16     ` [PATCH v4 0/4] add-patch: render hunks through the pager Rubén Justo
2024-07-15 20:20       ` [PATCH v4 1/4] add-patch: test for 'p' command Rubén Justo
2024-07-15 20:21       ` [PATCH v4 2/4] pager: do not close fd 2 unnecessarily Rubén Justo
2024-07-15 20:21       ` [PATCH v4 3/4] pager: introduce wait_for_pager Rubén Justo
2024-07-15 20:22       ` [PATCH v4 4/4] add-patch: render hunks through the pager Rubén Justo
  -- strict thread matches above, loose matches on Subject: below --
2024-07-23  0:39 [PATCH v2 0/2] add-p P fixups Rubén Justo
2024-07-23  9:15 ` Phillip Wood
2024-07-23 22:08   ` Rubén Justo
2024-07-24 15:21     ` phillip.wood123
2024-07-24 16:12       ` Rubén Justo
2024-07-25  9:45         ` Phillip Wood
2024-07-25 12:16           ` Rubén Justo
2024-07-25 13:42             ` [PATCH 0/4] squash fixups in rj/add-p-pager Rubén Justo
2024-07-25 13:44               ` [PATCH 3/4] pager: introduce wait_for_pager Rubén Justo

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

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

  git send-email \
    --in-reply-to=8434fafe-f545-49bc-8cc1-d4e8fb634bec@gmail.com \
    --to=phillip.wood123@gmail.com \
    --cc=dsimic@manjaro.org \
    --cc=git@vger.kernel.org \
    --cc=gitster@pobox.com \
    --cc=peff@peff.net \
    --cc=phillip.wood@dunelm.org.uk \
    --cc=rjusto@gmail.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.
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 NNTP newsgroup(s).