pager: do not close fd 2 unnecessarily

We send errors to the pager since 61b80509e3 (sending errors to stdout under $PAGER, 2008-02-16). In a8335024c2 (pager: do not dup2 stderr if it is already redirected, 2008-12-15) an exception was introduced to avoid redirecting stderr if it is not connected to a terminal. In such exceptional cases, the close(STDERR_FILENO) we're doing in close_pager_fds, is unnecessary. Furthermore, in a subsequent commit we're going to introduce changes that will involve using close_pager_fds multiple times. With this in mind, controlling when we want to close stderr, become sensible. Let's close(STDERR_FILENO) only when necessary, and pave the way for the upcoming changes. Signed-off-by: Rubén Justo <rjusto@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Rubén Justo committed Jul 25, 2024 at 15:44 UTC da9ef60c8f79b9af93a39fbd07dfc4e56a150d63
1 file changed +6 -2
pager.c
+6 -2
@@ -14,6 +14,7 @@ int pager_use_color = 1;
14
15 static struct child_process pager_process;
16 static char *pager_program;
17 +static int close_fd2;
18
19 /* Is the value coming back from term_columns() just a guess? */
20 static int term_columns_guessed;
@@ -23,7 +24,8 @@ static void close_pager_fds(void)
24 {
25 /* signal EOF to pager */
26 close(1);
26 - close(2);
27 + if (close_fd2)
28 + close(2);
29 }
30
31 static void wait_for_pager_atexit(void)
@@ -141,8 +143,10 @@ void setup_pager(void)
143
144 /* original process continues, but writes to the pipe */
145 dup2(pager_process.in, 1);
144 - if (isatty(2))
146 + if (isatty(2)) {
147 + close_fd2 = 1;
148 dup2(pager_process.in, 2);
149 + }
150 close(pager_process.in);
151
152 /* this makes sure that the parent terminates after the pager */