Skip to content

Commit 695d47e

Browse files
committed
Add a process_group method to UNIX CommandExt
1 parent 4f235d3 commit 695d47e

File tree

1 file changed

+87
-0
lines changed

1 file changed

+87
-0
lines changed

text/3228-process-process_group.md

+87
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
- Feature Name: `process_set_process_group`
2+
- Start Date: 02-02-2022
3+
- RFC PR: [rust-lang/rfcs#0000](https://github.com/rust-lang/rfcs/pull/3228)
4+
- Rust Issue: [rust-lang/rust#0000](https://github.com/rust-lang/rust/issues/3228)
5+
6+
# Summary
7+
[summary]: #summary
8+
9+
Add a `process_group` method to `std::os::unix::process::CommandExt` that
10+
allows setting the process group id (i.e. calling `setpgid`) in the child, thus
11+
enabling users to set process groups while leveraging the `posix_spawn` fast
12+
path.
13+
14+
# Motivation
15+
[motivation]: #motivation
16+
17+
The Unix process spawn code has two paths: a fast path that uses `posix_spawn`,
18+
and a slow path that uses `fork` and `exec`.
19+
20+
The performance between the two APIs has been shown to be very noticeable:
21+
22+
https://github.com/rust-lang/rust/commit/8fe61546696b626ecf68ef838d5d82e393719e80
23+
24+
Currently, users can set the process group on the commands they spawn via:
25+
26+
```
27+
let pre_exec = || nix::unistd::setpgid( ... );
28+
unsafe {
29+
cmd.pre_exec(pre_exec)
30+
};
31+
```
32+
33+
This approach forces the slow path because of the usage of `pre_exec`.
34+
35+
However, `posix_spawn` supports setting the process group
36+
(`posix_spawnattr_setpgroup`), this RFC proposes exposing that functionality,
37+
which allows users to set the process group id without forcing the slow path.
38+
39+
# Guide-level explanation
40+
[guide-level-explanation]: #guide-level-explanation
41+
42+
`std::os::unix::process::CommandExt::process_group` allows you to set the
43+
process group ID of the child process. This translates to a `setpgid` call
44+
in the child.
45+
46+
# Reference-level explanation
47+
[reference-level-explanation]: #reference-level-explanation
48+
49+
The changes needed are:
50+
51+
- Expose a `CommandExt` a `process_group` method on `CommandExt` that takes a
52+
PID as argument.
53+
- Add a call to `posix_spawnattr_setpgroup` on the fast path.
54+
- Add a call to `setpgid` on the slow path.
55+
56+
# Drawbacks
57+
[drawbacks]: #drawbacks
58+
59+
This marginally expands the API surface on `CommandExt`.
60+
61+
# Rationale and alternatives
62+
[rationale-and-alternatives]: #rationale-and-alternatives
63+
64+
- Using `pre_exec` this is a viable alternative for programs where `fork` is
65+
either sufficiently fast or infrequent.
66+
- Not using `std::process`, and rolling your own instead, is an alternative.
67+
This would however break interoperability with e.g. Tokio's
68+
`tokio::process::Command`, which currently can be created using a
69+
`Command` from the std lib.
70+
71+
# Prior art
72+
[prior-art]: #prior-art
73+
74+
The primary prior art here is all the other calls that already exist on
75+
`CommandExt` that translate to parameterizing `posix_spawn`, such as
76+
configuring groups, signal mask, current working directory, open pipes.
77+
78+
# Unresolved questions
79+
[unresolved-questions]: #unresolved-questions
80+
81+
- None known at this point.
82+
83+
# Future possibilities
84+
[future-possibilities]: #future-possibilities
85+
86+
There are a few other `posix_spawn` options that are not supported, such as
87+
`setsid` (which is a GNU extension). Those might warrant inclusion as well.

0 commit comments

Comments
 (0)