process_wrap/std/
reset_sigmask.rs

1use std::{io::Result, os::unix::process::CommandExt, process::Command};
2
3use nix::sys::signal::{sigprocmask, SigSet, SigmaskHow};
4#[cfg(feature = "tracing")]
5use tracing::trace;
6
7use super::{CommandWrap, CommandWrapper};
8
9/// Wrapper which resets the process signal mask.
10///
11/// By default a Command on Unix inherits its parent's [signal mask]. However, in some cases this
12/// is not what you want. This wrapper resets the command's sigmask by unblocking all signals.
13#[derive(Clone, Copy, Debug)]
14pub struct ResetSigmask;
15
16impl CommandWrapper for ResetSigmask {
17	fn pre_spawn(&mut self, command: &mut Command, _core: &CommandWrap) -> Result<()> {
18		unsafe {
19			command.pre_exec(|| {
20				let mut oldset = SigSet::empty();
21				let newset = SigSet::all();
22
23				#[cfg(feature = "tracing")]
24				trace!(unblocking=?newset, "resetting process sigmask");
25
26				sigprocmask(SigmaskHow::SIG_UNBLOCK, Some(&newset), Some(&mut oldset))?;
27
28				#[cfg(feature = "tracing")]
29				trace!(?oldset, "sigmask reset");
30				Ok(())
31			});
32		}
33		Ok(())
34	}
35}