From f1bde691316492b834ab0589481fe0d9c8d08188 Mon Sep 17 00:00:00 2001 From: Ian Manske Date: Fri, 4 Nov 2022 20:06:04 +0000 Subject: [PATCH] Fix panic when encountering ENOTTY. (#7001) --- src/main.rs | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/src/main.rs b/src/main.rs index b8c2dfae9d..26df5ab233 100644 --- a/src/main.rs +++ b/src/main.rs @@ -45,17 +45,18 @@ fn take_control(interactive: bool) { }; let shell_pgid = unistd::getpgrp(); - let owner_pgid = unistd::tcgetpgrp(nix::libc::STDIN_FILENO).expect("tcgetpgrp"); - // Common case, nothing to do - if owner_pgid == shell_pgid { - return; - } - - // This can apparently happen with sudo: https://github.com/fish-shell/fish-shell/issues/7388 - if owner_pgid == unistd::getpid() { - let _ = unistd::setpgid(owner_pgid, owner_pgid); - return; + match unistd::tcgetpgrp(nix::libc::STDIN_FILENO) { + Ok(owner_pgid) if owner_pgid == shell_pgid => { + // Common case, nothing to do + return; + } + Ok(owner_pgid) if owner_pgid == unistd::getpid() => { + // This can apparently happen with sudo: https://github.com/fish-shell/fish-shell/issues/7388 + let _ = unistd::setpgid(owner_pgid, owner_pgid); + return; + } + _ => (), } // Reset all signal handlers to default