Skip to content

Commit abb4a0e

Browse files
committed
Add a process_group method to UNIX CommandExt
1 parent dca1e7a commit abb4a0e

File tree

4 files changed

+70
-2
lines changed

4 files changed

+70
-2
lines changed

library/std/src/os/unix/process.rs

+10
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,11 @@ pub trait CommandExt: Sealed {
149149
fn arg0<S>(&mut self, arg: S) -> &mut process::Command
150150
where
151151
S: AsRef<OsStr>;
152+
153+
/// Sets the process group ID of the child process. Translates to a `setpgid` call in the child
154+
/// process.
155+
#[unstable(feature = "process_set_process_group", issue = "93857")]
156+
fn process_group(&mut self, pgroup: i32) -> &mut process::Command;
152157
}
153158

154159
#[stable(feature = "rust1", since = "1.0.0")]
@@ -201,6 +206,11 @@ impl CommandExt for process::Command {
201206
self.as_inner_mut().set_arg_0(arg.as_ref());
202207
self
203208
}
209+
210+
fn process_group(&mut self, pgroup: i32) -> &mut process::Command {
211+
self.as_inner_mut().pgroup(pgroup);
212+
self
213+
}
204214
}
205215

206216
/// Unix-specific extensions to [`process::ExitStatus`] and

library/std/src/sys/unix/process/process_common.rs

+11-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ use crate::sys_common::IntoInner;
1818
#[cfg(not(target_os = "fuchsia"))]
1919
use crate::sys::fs::OpenOptions;
2020

21-
use libc::{c_char, c_int, gid_t, uid_t, EXIT_FAILURE, EXIT_SUCCESS};
21+
use libc::{c_char, c_int, gid_t, pid_t, uid_t, EXIT_FAILURE, EXIT_SUCCESS};
2222

2323
cfg_if::cfg_if! {
2424
if #[cfg(target_os = "fuchsia")] {
@@ -82,6 +82,7 @@ pub struct Command {
8282
stderr: Option<Stdio>,
8383
#[cfg(target_os = "linux")]
8484
create_pidfd: bool,
85+
pgroup: Option<pid_t>,
8586
}
8687

8788
// Create a new type for argv, so that we can make it `Send` and `Sync`
@@ -145,6 +146,7 @@ impl Command {
145146
stdin: None,
146147
stdout: None,
147148
stderr: None,
149+
pgroup: None,
148150
}
149151
}
150152

@@ -167,6 +169,7 @@ impl Command {
167169
stdout: None,
168170
stderr: None,
169171
create_pidfd: false,
172+
pgroup: None,
170173
}
171174
}
172175

@@ -202,6 +205,9 @@ impl Command {
202205
pub fn groups(&mut self, groups: &[gid_t]) {
203206
self.groups = Some(Box::from(groups));
204207
}
208+
pub fn pgroup(&mut self, pgroup: pid_t) {
209+
self.pgroup = Some(pgroup);
210+
}
205211

206212
#[cfg(target_os = "linux")]
207213
pub fn create_pidfd(&mut self, val: bool) {
@@ -266,6 +272,10 @@ impl Command {
266272
self.groups.as_deref()
267273
}
268274

275+
pub fn get_pgroup(&self) -> Option<pid_t> {
276+
self.pgroup
277+
}
278+
269279
pub fn get_closures(&mut self) -> &mut Vec<Box<dyn FnMut() -> io::Result<()> + Send + Sync>> {
270280
&mut self.closures
271281
}

library/std/src/sys/unix/process/process_common/tests.rs

+35
Original file line numberDiff line numberDiff line change
@@ -67,3 +67,38 @@ fn test_process_mask() {
6767
t!(cat.wait());
6868
}
6969
}
70+
71+
#[test]
72+
fn test_process_group_posix_spawn() {
73+
unsafe {
74+
// Spawn a cat subprocess that's just going to hang since there is no I/O.
75+
let mut cmd = Command::new(OsStr::new("cat"));
76+
cmd.pgroup(0);
77+
cmd.stdin(Stdio::MakePipe);
78+
cmd.stdout(Stdio::MakePipe);
79+
let (mut cat, _pipes) = t!(cmd.spawn(Stdio::Null, true));
80+
81+
// Check that we can kill its process group, which means there *is* one.
82+
t!(cvt(libc::kill(-(cat.id() as libc::pid_t), libc::SIGINT)));
83+
84+
t!(cat.wait());
85+
}
86+
}
87+
88+
#[test]
89+
fn test_process_group_no_posix_spawn() {
90+
unsafe {
91+
// Same as above, create hang-y cat. This time, force using the non-posix_spawnp path.
92+
let mut cmd = Command::new(OsStr::new("cat"));
93+
cmd.pgroup(0);
94+
cmd.pre_exec(Box::new(|| Ok(()))); // pre_exec forces fork + exec
95+
cmd.stdin(Stdio::MakePipe);
96+
cmd.stdout(Stdio::MakePipe);
97+
let (mut cat, _pipes) = t!(cmd.spawn(Stdio::Null, true));
98+
99+
// Check that we can kill its process group, which means there *is* one.
100+
t!(cvt(libc::kill(-(cat.id() as libc::pid_t), libc::SIGINT)));
101+
102+
t!(cat.wait());
103+
}
104+
}

library/std/src/sys/unix/process/process_unix.rs

+14-1
Original file line numberDiff line numberDiff line change
@@ -320,6 +320,10 @@ impl Command {
320320
cvt(libc::chdir(cwd.as_ptr()))?;
321321
}
322322

323+
if let Some(pgroup) = self.get_pgroup() {
324+
cvt(libc::setpgid(0, pgroup))?;
325+
}
326+
323327
// emscripten has no signal support.
324328
#[cfg(not(target_os = "emscripten"))]
325329
{
@@ -459,6 +463,8 @@ impl Command {
459463
None => None,
460464
};
461465

466+
let pgroup = self.get_pgroup();
467+
462468
// Safety: -1 indicates we don't have a pidfd.
463469
let mut p = unsafe { Process::new(0, -1) };
464470

@@ -487,6 +493,8 @@ impl Command {
487493
cvt_nz(libc::posix_spawnattr_init(attrs.as_mut_ptr()))?;
488494
let attrs = PosixSpawnattr(&mut attrs);
489495

496+
let mut flags = 0;
497+
490498
let mut file_actions = MaybeUninit::uninit();
491499
cvt_nz(libc::posix_spawn_file_actions_init(file_actions.as_mut_ptr()))?;
492500
let file_actions = PosixSpawnFileActions(&mut file_actions);
@@ -516,13 +524,18 @@ impl Command {
516524
cvt_nz(f(file_actions.0.as_mut_ptr(), cwd.as_ptr()))?;
517525
}
518526

527+
if let Some(pgroup) = pgroup {
528+
flags |= libc::POSIX_SPAWN_SETPGROUP;
529+
cvt_nz(libc::posix_spawnattr_setpgroup(attrs.0.as_mut_ptr(), pgroup))?;
530+
}
531+
519532
let mut set = MaybeUninit::<libc::sigset_t>::uninit();
520533
cvt(sigemptyset(set.as_mut_ptr()))?;
521534
cvt_nz(libc::posix_spawnattr_setsigmask(attrs.0.as_mut_ptr(), set.as_ptr()))?;
522535
cvt(sigaddset(set.as_mut_ptr(), libc::SIGPIPE))?;
523536
cvt_nz(libc::posix_spawnattr_setsigdefault(attrs.0.as_mut_ptr(), set.as_ptr()))?;
524537

525-
let flags = libc::POSIX_SPAWN_SETSIGDEF | libc::POSIX_SPAWN_SETSIGMASK;
538+
flags |= libc::POSIX_SPAWN_SETSIGDEF | libc::POSIX_SPAWN_SETSIGMASK;
526539
cvt_nz(libc::posix_spawnattr_setflags(attrs.0.as_mut_ptr(), flags as _))?;
527540

528541
// Make sure we synchronize access to the global `environ` resource

0 commit comments

Comments
 (0)