diff --git a/src/fork_exec/spawn.rs b/src/fork_exec/spawn.rs index 838fadf78..6c3e421d0 100644 --- a/src/fork_exec/spawn.rs +++ b/src/fork_exec/spawn.rs @@ -3,7 +3,7 @@ use super::blocked_signals_for_job; use crate::proc::Job; use crate::redirection::Dup2List; -use crate::signal::get_signals_with_handlers; +use crate::signal::get_signals_to_default; use crate::{exec::is_thompson_shell_script, libc::_PATH_BSHELL}; use errno::Errno; use libc::{c_char, posix_spawn_file_actions_t, posix_spawnattr_t}; @@ -126,8 +126,7 @@ impl PosixSpawner { } // Everybody gets default handlers. - let mut sigdefault: libc::sigset_t = unsafe { std::mem::zeroed() }; - get_signals_with_handlers(&mut sigdefault); + let sigdefault = get_signals_to_default(); attr.set_sigdefault(&sigdefault)?; // Reset the sigmask. diff --git a/src/signal.rs b/src/signal.rs index 0d34c8b56..322cadb45 100644 --- a/src/signal.rs +++ b/src/signal.rs @@ -275,21 +275,23 @@ pub fn signal_handle(sig: Signal) { sigaction(sig, &act, std::ptr::null_mut()); } -pub fn get_signals_with_handlers(set: &mut libc::sigset_t) { - unsafe { libc::sigemptyset(set) }; +pub fn get_signals_to_default() -> libc::sigset_t { + let mut set: libc::sigset_t = unsafe { std::mem::zeroed() }; + unsafe { libc::sigemptyset(&mut set) }; for data in SIGNAL_TABLE.iter() { - let mut act: libc::sigaction = unsafe { std::mem::zeroed() }; - unsafe { libc::sigaction(data.signal.code(), std::ptr::null(), &mut act) }; // If SIGHUP is being ignored (e.g., because were were run via `nohup`) don't reset it. // We don't special case other signals because if they're being ignored that shouldn't // affect processes we spawn. They should get the default behavior for those signals. - if data.signal == libc::SIGHUP && act.sa_sigaction == libc::SIG_IGN { - continue; - } - if act.sa_sigaction != libc::SIG_DFL { - unsafe { libc::sigaddset(set, data.signal.code()) }; + if data.signal == libc::SIGHUP { + let mut act: libc::sigaction = unsafe { std::mem::zeroed() }; + unsafe { libc::sigaction(data.signal.code(), std::ptr::null(), &mut act) }; + if act.sa_sigaction == libc::SIG_IGN { + continue; + } } + unsafe { libc::sigaddset(&mut set, data.signal.code()) }; } + return set; } /// Ensure we did not inherit any blocked signals. See issue #3964.