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

Re: [PATCH v3 2/4] terminal: don't assume stdin is /dev/tty

From
Junio C Hamano <gitster@pobox.com>
Date
Mar 15, 2022, 17:42 UTC
Message-ID
<xmqqzglrdsd8.fsf@gitster.g>
In-Reply-To
<20220315105723.19398-3-phillip.wood123@gmail.com>
Phillip Wood <phillip.wood123@gmail.com> writes:
Show 6 quoted lines
> diff --git a/compat/terminal.c b/compat/terminal.c
> index da2f788137..bfbde3c1af 100644
> --- a/compat/terminal.c
> +++ b/compat/terminal.c
> @@ -23,21 +23,28 @@ static void restore_term_on_signal(int sig)
>  static int term_fd = -1;
The variable can be -1 to signal "no valid file descriptor".
Show 9 quoted lines
>  static struct termios old_term;
>  
> +static void close_term_fd(void)
> +{
> +	if (term_fd)
> +		close(term_fd);
> +	term_fd = -1;
> +}
> +
And we use that negative value after closing it.
The check before closing it is wrong.  It should be
	if (0 <= term_fd)
instead.  Or we could mimick the beginning of restore_term(), i.e.
	if (term_fd < 0)
		return;
	close(term_fd);
	term_fd = -1;
Show 9 quoted lines
>  void restore_term(void)
>  {
>  	if (term_fd < 0)
>  		return;
>  
>  	tcsetattr(term_fd, TCSAFLUSH, &old_term);
> -	close(term_fd);
> -	term_fd = -1;
> +	close_term_fd();

Because we come this far only when term_fd is valid, this change is a no-op. If we are adding more calls to close_term_fd(), it may be a good abstraction.

Show 9 quoted lines
>  	sigchain_pop_common();
>  }
>  
>  int save_term(enum save_term_flags flags)
>  {
>  	if (term_fd < 0)
> -		term_fd = open("/dev/tty", O_RDWR);
> +		term_fd = (flags & SAVE_TERM_STDIN) ? 0
> +						    : open("/dev/tty", O_RDWR);
We can avoid overly long line by wrapping differently:
		term_fd = ((flags & SAVE_TERM_STDIN)
			   ? 0
			   : open("/dev/tty", O_RDWR));
We can turn our head sideways to see the parse tree this way.
Show 7 quoted lines
> @@ -66,8 +73,7 @@ static int disable_bits(enum save_term_flags flags, tcflag_t bits)
>  
>  	sigchain_pop_common();
>  error:
> -	close(term_fd);
> -	term_fd = -1;
> +	close_term_fd();
OK.
Show 9 quoted lines
> @@ -362,7 +368,7 @@ int read_key_without_echo(struct strbuf *buf)
>  	static int warning_displayed;
>  	int ch;
>  
> -	if (warning_displayed || enable_non_canonical(0) < 0) {
> +	if (warning_displayed || enable_non_canonical(SAVE_TERM_STDIN) < 0) {
>  		if (!warning_displayed) {
>  			warning("reading single keystrokes not supported on "
>  				"this platform; reading line instead");

The validity of this change is harder to see with only what we have in the patch, but the control flow is

   enable_non_canonical(bits)
   -> disable_bits(bits, ...)
      -> save_term(bits)

And we are passing SAVE_TERM_STDIN to say "reuse fd #0 and save the terminal settings of it, instead of opening /dev/tty anew".

What happens if FD#0 is *not* connected to a tty, by the way? tcgetattr() in save_term() would fail, without clearing term_fd (i.e. term_fd is still 0 when save_term() returns a failure), and goes to the error code path, where close_term_fd() is called.

Because we have the "if (term_fd)" bug in save_term(), this bug is hidden, but I think save_term() upon seeing a failure from tcgetattr() should clear term_fd to limit the damage, especially when it is trying to futz with caller supplied FD#0, not the tty it opened itself?

Show 13 quoted lines
> diff --git a/compat/terminal.h b/compat/terminal.h
> index aeb24c9478..79ed00cf61 100644
> --- a/compat/terminal.h
> +++ b/compat/terminal.h
> @@ -4,6 +4,8 @@
>  enum save_term_flags {
>  	/* Save input and output settings */
>  	SAVE_TERM_DUPLEX = 1 << 0,
> +	/* Save stdin rather than /dev/tty (fails if stdin is not a terminal) */
> +	SAVE_TERM_STDIN  = 1 << 1,
>  };
>  
>  /*
Previous: Phillip WoodNext: rsbecker@nexbridge.com
Message 41 of 53 in “builtin add -p: hopefully final readkey fixes”
  1. 0/4 builtin add -p: hopefully final readkey fixesPhillip Wood, Mar 4, 2022
  2. 1/4 terminal: use flags for save_term()Phillip Wood, Mar 4, 2022
  3. Ramsay JonesMar 4, 2022
  4. Phillip WoodMar 7, 2022
  5. Ramsay JonesMar 7, 2022
  6. Phillip WoodMar 8, 2022
  7. Ævar Arnfjörð BjarmasonMar 5, 2022
  8. Phillip WoodMar 7, 2022
  9. Ævar Arnfjörð BjarmasonMar 7, 2022
  10. 3/4 terminal: work around macos poll() bugPhillip Wood, Mar 4, 2022
  11. 2/4 terminal: don't assume stdin is /dev/ttyPhillip Wood, Mar 4, 2022
  12. Ramsay JonesMar 4, 2022
  13. 4/4 terminal: restore settings on SIGTSTPPhillip Wood, Mar 4, 2022
  14. Ævar Arnfjörð BjarmasonMar 5, 2022
  15. Phillip WoodMar 7, 2022
  16. Ævar Arnfjörð BjarmasonMar 7, 2022
  17. Phillip WoodMar 7, 2022
  18. Ævar Arnfjörð BjarmasonMar 7, 2022
  19. Phillip WoodMar 8, 2022
  20. Johannes SchindelinMar 9, 2022
  21. Phillip WoodMar 10, 2022
  22. 0/4 builtin add -p: hopefully final readkey fixesPhillip Wood, Mar 9, 2022
  23. 1/4 terminal: use flags for save_term()Phillip Wood, Mar 9, 2022
  24. Carlo ArenasMar 11, 2022
  25. Phillip WoodMar 14, 2022
  26. 2/4 terminal: don't assume stdin is /dev/ttyPhillip Wood, Mar 9, 2022
  27. 3/4 terminal: work around macos poll() bugPhillip Wood, Mar 9, 2022
  28. Ævar Arnfjörð BjarmasonMar 10, 2022
  29. Phillip WoodMar 10, 2022
  30. Junio C HamanoMar 10, 2022
  31. 4/4 terminal: restore settings on SIGTSTPPhillip Wood, Mar 9, 2022
  32. Junio C HamanoMar 9, 2022
  33. Junio C HamanoMar 9, 2022
  34. Phillip WoodMar 10, 2022
  35. Phillip WoodMar 10, 2022
  36. Junio C HamanoMar 10, 2022
  37. Johannes SchindelinMar 10, 2022
  38. Phillip WoodMar 10, 2022
  39. 0/4 builtin add -p: hopefully final readkey fixesPhillip Wood, Mar 15, 2022
  40. 2/4 terminal: don't assume stdin is /dev/ttyPhillip Wood, Mar 15, 2022
  41. Junio C HamanoMar 15, 2022
  42. rsbecker@nexbridge.comMar 15, 2022
  43. Junio C HamanoMar 15, 2022
  44. rsbecker@nexbridge.comMar 15, 2022
  45. 1/4 terminal: use flags for save_term()Phillip Wood, Mar 15, 2022
  46. 4/4 terminal: restore settings on SIGTSTPPhillip Wood, Mar 15, 2022
  47. Junio C HamanoMar 15, 2022
  48. 3/4 terminal: work around macos poll() bugPhillip Wood, Mar 15, 2022
  49. 0/4 builtin add -p: hopefully final readkey fixesPhillip Wood, Mar 16, 2022
  50. 1/4 terminal: use flags for save_term()Phillip Wood, Mar 16, 2022
  51. 2/4 terminal: don't assume stdin is /dev/ttyPhillip Wood, Mar 16, 2022
  52. 3/4 terminal: work around macos poll() bugPhillip Wood, Mar 16, 2022
  53. 4/4 terminal: restore settings on SIGTSTPPhillip Wood, Mar 16, 2022

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.