Acquire tty if interactive when running builtins

When running a builtin, if we are an interactive shell and stdin is a tty,
then acquire ownership of the terminal via tcgetpgrp() before running the
builtin, and set it back after.

Fixes #4540
This commit is contained in:
ridiculousfish
2018-08-04 17:32:04 -07:00
parent c0a332743f
commit fa66ac8d8c
4 changed files with 32 additions and 1 deletions

View File

@@ -870,6 +870,17 @@ bool terminal_give_to_job(const job_t *j, bool cont) {
return true;
}
pid_t terminal_acquire_before_builtin() {
pid_t selfpid = getpid();
pid_t current_owner = tcgetpgrp(STDIN_FILENO);
if (current_owner >= 0 && current_owner != selfpid) {
if (tcsetpgrp(STDIN_FILENO, selfpid) == 0) {
return current_owner;
}
}
return -1;
}
/// Returns control of the terminal to the shell, and saves the terminal attribute state to the job,
/// so that we can restore the terminal ownership to the job at a later time.
static bool terminal_return_from_job(job_t *j) {