process_wrap/tokio/
reset_sigmask.rs

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