git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH 2/4] pager: do not close fd 2 unnecessarily

From
Rubén Justo <rjusto@gmail.com>
Date
Jul 25, 2024, 13:44 UTC
Message-ID
<3fcb30a2-8a97-4b34-aa3d-b19ee54dc97e@gmail.com>
In-Reply-To
<76936fb1-446d-455f-b4e7-6e24dda3c17d@gmail.com>

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>
---
 pager.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)
diff --git a/pager.c b/pager.c
index be6f4ee59f..251adfc2ad 100644
--- a/pager.c
+++ b/pager.c
@@ -14,6 +14,7 @@ int pager_use_color = 1;
 
 static struct child_process pager_process;
 static char *pager_program;
+static int close_fd2;
 
 /* Is the value coming back from term_columns() just a guess? */
 static int term_columns_guessed;
@@ -23,7 +24,8 @@ static void close_pager_fds(void)
 {
 	/* signal EOF to pager */
 	close(1);
-	close(2);
+	if (close_fd2)
+		close(2);
 }
 
 static void wait_for_pager_atexit(void)
@@ -141,8 +143,10 @@ void setup_pager(void)
 
 	/* original process continues, but writes to the pipe */
 	dup2(pager_process.in, 1);
-	if (isatty(2))
+	if (isatty(2)) {
+		close_fd2 = 1;
 		dup2(pager_process.in, 2);
+	}
 	close(pager_process.in);
 
 	/* this makes sure that the parent terminates after the pager */
-- 
2.46.0.rc0.4.g6f4990c0d4
Previous: Rubén JustoNext: Rubén Justo
Message 14 of 29 in “add-p P fixups”
  1. 0/2 add-p P fixupsRubén Justo, Jul 23, 2024
  2. 1/2 t3701: avoid one-shot export for shell functionsRubén Justo, Jul 23, 2024
  3. 2/2 pager: make wait_for_pager a no-op for "cat"Rubén Justo, Jul 23, 2024
  4. Junio C HamanoJul 23, 2024
  5. Phillip WoodJul 23, 2024
  6. Junio C HamanoJul 23, 2024
  7. Rubén JustoJul 23, 2024
  8. phillip.wood123@gmail.comJul 24, 2024
  9. Rubén JustoJul 24, 2024
  10. Phillip WoodJul 25, 2024
  11. Rubén JustoJul 25, 2024
  12. 0/4 squash fixups in rj/add-p-pagerRubén Justo, Jul 25, 2024
  13. 1/4 add-patch: test for 'p' commandRubén Justo, Jul 25, 2024
  14. 2/4 pager: do not close fd 2 unnecessarilyRubén Justo, Jul 25, 2024
  15. 3/4 pager: introduce wait_for_pagerRubén Justo, Jul 25, 2024
  16. 4/4 add-patch: render hunks through the pagerRubén Justo, Jul 25, 2024
  17. Junio C HamanoJul 25, 2024
  18. Re* [PATCH v2 0/2] add-p P fixupsRubén Justo, Jul 25, 2024
  19. 1/2 pager: introduce wait_for_pagerRubén Justo, Jul 25, 2024
  20. 2/2 add-patch: render hunks through the pagerRubén Justo, Jul 25, 2024
  21. Junio C HamanoJul 26, 2024
  22. Junio C HamanoJul 27, 2024
  23. Rubén JustoJul 27, 2024
  24. Junio C HamanoJul 26, 2024
  25. Rubén JustoJul 26, 2024
  26. Junio C HamanoJul 26, 2024
  27. Rubén JustoJul 26, 2024
  28. Rubén JustoJul 28, 2024
  29. Junio C HamanoJul 29, 2024

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.