command_group/tokio/unix.rs
1use crate::builder::CommandGroupBuilder;
2use crate::AsyncGroupChild;
3
4impl CommandGroupBuilder<'_, tokio::process::Command> {
5 /// Executes the command as a child process group, returning a handle to it.
6 ///
7 /// By default, stdin, stdout and stderr are inherited from the parent.
8 ///
9 /// On Windows, this creates a job object instead of a POSIX process group.
10 ///
11 /// # Examples
12 ///
13 /// Basic usage:
14 ///
15 /// ```no_run
16 /// use tokio::process::Command;
17 /// use command_group::AsyncCommandGroup;
18 ///
19 /// Command::new("ls")
20 /// .group()
21 /// .spawn()
22 /// .expect("ls command failed to start");
23 /// ```
24 pub fn spawn(&mut self) -> std::io::Result<AsyncGroupChild> {
25 #[cfg(tokio_unstable)]
26 {
27 self.command.process_group(0);
28 }
29
30 #[cfg(not(tokio_unstable))]
31 unsafe {
32 use nix::unistd::{setpgid, Pid};
33 use std::io::Error;
34 self.command.pre_exec(|| {
35 setpgid(Pid::this(), Pid::from_raw(0))
36 .map_err(Error::from)
37 .map(|_| ())
38 });
39 }
40
41 self.command.spawn().map(AsyncGroupChild::new)
42 }
43}