Skip to content

Commit 412e096

Browse files
authored
Rollup merge of rust-lang#93858 - krallin:process-process_group, r=joshtriplett
Add a `process_group` method to UNIX `CommandExt` - Tracking issue: rust-lang#93857 - RFC: rust-lang/rfcs#3228 Add a `process_group` method to `std::os::unix::process::CommandExt` that allows setting the process group id (i.e. calling `setpgid`) in the child, thus enabling users to set process groups while leveraging the `posix_spawn` fast path.
2 parents 1c742bf + abb4a0e commit 412e096

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
@@ -317,6 +317,10 @@ impl Command {
317317
cvt(libc::chdir(cwd.as_ptr()))?;
318318
}
319319

320+
if let Some(pgroup) = self.get_pgroup() {
321+
cvt(libc::setpgid(0, pgroup))?;
322+
}
323+
320324
// emscripten has no signal support.
321325
#[cfg(not(target_os = "emscripten"))]
322326
{
@@ -456,6 +460,8 @@ impl Command {
456460
None => None,
457461
};
458462

463+
let pgroup = self.get_pgroup();
464+
459465
// Safety: -1 indicates we don't have a pidfd.
460466
let mut p = unsafe { Process::new(0, -1) };
461467

@@ -484,6 +490,8 @@ impl Command {
484490
cvt_nz(libc::posix_spawnattr_init(attrs.as_mut_ptr()))?;
485491
let attrs = PosixSpawnattr(&mut attrs);
486492

493+
let mut flags = 0;
494+
487495
let mut file_actions = MaybeUninit::uninit();
488496
cvt_nz(libc::posix_spawn_file_actions_init(file_actions.as_mut_ptr()))?;
489497
let file_actions = PosixSpawnFileActions(&mut file_actions);
@@ -513,13 +521,18 @@ impl Command {
513521
cvt_nz(f(file_actions.0.as_mut_ptr(), cwd.as_ptr()))?;
514522
}
515523

524+
if let Some(pgroup) = pgroup {
525+
flags |= libc::POSIX_SPAWN_SETPGROUP;
526+
cvt_nz(libc::posix_spawnattr_setpgroup(attrs.0.as_mut_ptr(), pgroup))?;
527+
}
528+
516529
let mut set = MaybeUninit::<libc::sigset_t>::uninit();
517530
cvt(sigemptyset(set.as_mut_ptr()))?;
518531
cvt_nz(libc::posix_spawnattr_setsigmask(attrs.0.as_mut_ptr(), set.as_ptr()))?;
519532
cvt(sigaddset(set.as_mut_ptr(), libc::SIGPIPE))?;
520533
cvt_nz(libc::posix_spawnattr_setsigdefault(attrs.0.as_mut_ptr(), set.as_ptr()))?;
521534

522-
let flags = libc::POSIX_SPAWN_SETSIGDEF | libc::POSIX_SPAWN_SETSIGMASK;
535+
flags |= libc::POSIX_SPAWN_SETSIGDEF | libc::POSIX_SPAWN_SETSIGMASK;
523536
cvt_nz(libc::posix_spawnattr_setflags(attrs.0.as_mut_ptr(), flags as _))?;
524537

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

0 commit comments

Comments
 (0)