process_wrap/std/
process_session.rs1use std::{
2 io::{Error, Result},
3 os::unix::process::CommandExt,
4 process::Command,
5};
6
7use nix::unistd::{setsid, Pid};
8#[cfg(feature = "tracing")]
9use tracing::instrument;
10
11use super::{CommandWrap, CommandWrapper};
12
13#[derive(Clone, Copy, Debug)]
26pub struct ProcessSession;
27
28impl CommandWrapper for ProcessSession {
29 #[cfg_attr(feature = "tracing", instrument(level = "debug", skip(self)))]
30 fn pre_spawn(&mut self, command: &mut Command, _core: &CommandWrap) -> Result<()> {
31 unsafe {
32 command.pre_exec(move || setsid().map_err(Error::from).map(|_| ()));
33 }
34
35 Ok(())
36 }
37
38 #[cfg_attr(feature = "tracing", instrument(level = "debug", skip(self)))]
39 fn wrap_child(
40 &mut self,
41 inner: Box<dyn super::core::ChildWrapper>,
42 _core: &CommandWrap,
43 ) -> Result<Box<dyn super::core::ChildWrapper>> {
44 let pgid = Pid::from_raw(i32::try_from(inner.id()).expect("Command PID > i32::MAX"));
45
46 Ok(Box::new(super::ProcessGroupChild::new(inner, pgid)))
47 }
48}