Skip to content

Commit 85b55ce

Browse files
committed
Only use clone3 when needed for pidfd
In rust-lang#89522 we learned that `clone3` is interacting poorly with Gentoo's `sandbox` tool. We only need that for the unstable pidfd extensions, so otherwise avoid that and use a normal `fork`.
1 parent 045612b commit 85b55ce

File tree

1 file changed

+6
-7
lines changed

1 file changed

+6
-7
lines changed

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

+6-7
Original file line numberDiff line numberDiff line change
@@ -166,21 +166,20 @@ impl Command {
166166
fn clone3(cl_args: *mut clone_args, len: libc::size_t) -> libc::c_long
167167
}
168168

169+
// Bypassing libc for `clone3` can make further libc calls unsafe,
170+
// so we use it sparingly for now. See #89522 for details.
171+
let want_clone3_pidfd = self.get_create_pidfd();
172+
169173
// If we fail to create a pidfd for any reason, this will
170174
// stay as -1, which indicates an error.
171175
let mut pidfd: pid_t = -1;
172176

173177
// Attempt to use the `clone3` syscall, which supports more arguments
174178
// (in particular, the ability to create a pidfd). If this fails,
175179
// we will fall through this block to a call to `fork()`
176-
if HAS_CLONE3.load(Ordering::Relaxed) {
177-
let mut flags = 0;
178-
if self.get_create_pidfd() {
179-
flags |= CLONE_PIDFD;
180-
}
181-
180+
if want_clone3_pidfd && HAS_CLONE3.load(Ordering::Relaxed) {
182181
let mut args = clone_args {
183-
flags,
182+
flags: CLONE_PIDFD,
184183
pidfd: &mut pidfd as *mut pid_t as u64,
185184
child_tid: 0,
186185
parent_tid: 0,

0 commit comments

Comments
 (0)