mirror of
https://github.com/git/git.git
synced 2024-06-02 11:46:11 +02:00
terminal: pop signal handler when terminal is restored
When disable_bits() changes the terminal attributes it uses
sigchain_push_common() to restore the terminal if a signal is received
before restore_term() is called. However there is no corresponding
call to sigchain_pop_common() when the settings are restored so the
signal handler is left on the sigchain stack. This leaves the stack
unbalanced so code such as
sigchain_push_common(my_handler);
...
read_key_without_echo(...);
...
sigchain_pop_common();
pops the handler pushed by disable_bits() rather than the one it
intended to. Additionally "git add -p" changes the terminal settings
every time it reads a key press so the stack can grow significantly.
In order to fix this save_term() now sets up the signal handler so
restore_term() can unconditionally call sigchain_pop_common(). There
are no callers of save_term() outside of terminal.c as the only
external caller was removed by e3f7e01b50
("Revert "editor: save and
reset terminal after calling EDITOR"", 2021-11-22). Any future callers
of save_term() should benefit from having the signal handler set up
for them.
Signed-off-by: Phillip Wood <phillip.wood@dunelm.org.uk>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
This commit is contained in:
parent
24d7ce383a
commit
f7da756566
|
@ -11,7 +11,7 @@
|
||||||
static void restore_term_on_signal(int sig)
|
static void restore_term_on_signal(int sig)
|
||||||
{
|
{
|
||||||
restore_term();
|
restore_term();
|
||||||
sigchain_pop(sig);
|
/* restore_term calls sigchain_pop_common */
|
||||||
raise(sig);
|
raise(sig);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -31,14 +31,20 @@ void restore_term(void)
|
||||||
tcsetattr(term_fd, TCSAFLUSH, &old_term);
|
tcsetattr(term_fd, TCSAFLUSH, &old_term);
|
||||||
close(term_fd);
|
close(term_fd);
|
||||||
term_fd = -1;
|
term_fd = -1;
|
||||||
|
sigchain_pop_common();
|
||||||
}
|
}
|
||||||
|
|
||||||
int save_term(int full_duplex)
|
int save_term(int full_duplex)
|
||||||
{
|
{
|
||||||
if (term_fd < 0)
|
if (term_fd < 0)
|
||||||
term_fd = open("/dev/tty", O_RDWR);
|
term_fd = open("/dev/tty", O_RDWR);
|
||||||
|
if (term_fd < 0)
|
||||||
|
return -1;
|
||||||
|
if (tcgetattr(term_fd, &old_term) < 0)
|
||||||
|
return -1;
|
||||||
|
sigchain_push_common(restore_term_on_signal);
|
||||||
|
|
||||||
return (term_fd < 0) ? -1 : tcgetattr(term_fd, &old_term);
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int disable_bits(tcflag_t bits)
|
static int disable_bits(tcflag_t bits)
|
||||||
|
@ -49,12 +55,12 @@ static int disable_bits(tcflag_t bits)
|
||||||
goto error;
|
goto error;
|
||||||
|
|
||||||
t = old_term;
|
t = old_term;
|
||||||
sigchain_push_common(restore_term_on_signal);
|
|
||||||
|
|
||||||
t.c_lflag &= ~bits;
|
t.c_lflag &= ~bits;
|
||||||
if (!tcsetattr(term_fd, TCSAFLUSH, &t))
|
if (!tcsetattr(term_fd, TCSAFLUSH, &t))
|
||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
|
sigchain_pop_common();
|
||||||
error:
|
error:
|
||||||
close(term_fd);
|
close(term_fd);
|
||||||
term_fd = -1;
|
term_fd = -1;
|
||||||
|
@ -100,6 +106,8 @@ void restore_term(void)
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
sigchain_pop_common();
|
||||||
|
|
||||||
if (hconin == INVALID_HANDLE_VALUE)
|
if (hconin == INVALID_HANDLE_VALUE)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
|
@ -134,6 +142,7 @@ int save_term(int full_duplex)
|
||||||
|
|
||||||
GetConsoleMode(hconin, &cmode_in);
|
GetConsoleMode(hconin, &cmode_in);
|
||||||
use_stty = 0;
|
use_stty = 0;
|
||||||
|
sigchain_push_common(restore_term_on_signal);
|
||||||
return 0;
|
return 0;
|
||||||
error:
|
error:
|
||||||
CloseHandle(hconin);
|
CloseHandle(hconin);
|
||||||
|
@ -177,10 +186,10 @@ static int disable_bits(DWORD bits)
|
||||||
if (save_term(0) < 0)
|
if (save_term(0) < 0)
|
||||||
return -1;
|
return -1;
|
||||||
|
|
||||||
sigchain_push_common(restore_term_on_signal);
|
|
||||||
if (!SetConsoleMode(hconin, cmode_in & ~bits)) {
|
if (!SetConsoleMode(hconin, cmode_in & ~bits)) {
|
||||||
CloseHandle(hconin);
|
CloseHandle(hconin);
|
||||||
hconin = INVALID_HANDLE_VALUE;
|
hconin = INVALID_HANDLE_VALUE;
|
||||||
|
sigchain_pop_common();
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,7 +1,15 @@
|
||||||
#ifndef COMPAT_TERMINAL_H
|
#ifndef COMPAT_TERMINAL_H
|
||||||
#define COMPAT_TERMINAL_H
|
#define COMPAT_TERMINAL_H
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Save the terminal attributes so they can be restored later by a
|
||||||
|
* call to restore_term(). Note that every successful call to
|
||||||
|
* save_term() must be matched by a call to restore_term() even if the
|
||||||
|
* attributes have not been changed. Returns 0 on success, -1 on
|
||||||
|
* failure.
|
||||||
|
*/
|
||||||
int save_term(int full_duplex);
|
int save_term(int full_duplex);
|
||||||
|
/* Restore the terminal attributes that were saved with save_term() */
|
||||||
void restore_term(void);
|
void restore_term(void);
|
||||||
|
|
||||||
char *git_terminal_prompt(const char *prompt, int echo);
|
char *git_terminal_prompt(const char *prompt, int echo);
|
||||||
|
|
Loading…
Reference in New Issue