Skip to content

Commit 3545003

Browse files
authored
Rollup merge of #93858 - krallin:process-process_group, r=dtolnay
Add a `process_group` method to UNIX `CommandExt` - Tracking issue: #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 3153584 + b628497 commit 3545003

File tree

4 files changed

+90
-2
lines changed

4 files changed

+90
-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) {
@@ -265,6 +271,10 @@ impl Command {
265271
pub fn get_groups(&self) -> Option<&[gid_t]> {
266272
self.groups.as_deref()
267273
}
274+
#[allow(dead_code)]
275+
pub fn get_pgroup(&self) -> Option<pid_t> {
276+
self.pgroup
277+
}
268278

269279
pub fn get_closures(&mut self) -> &mut Vec<Box<dyn FnMut() -> io::Result<()> + Send + Sync>> {
270280
&mut self.closures

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

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

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)