Skip to content

Commit 74d719c

Browse files
committed
Set CLOEXEC with fcntl, Apple does not have pipe2
1 parent 4af683f commit 74d719c

File tree

1 file changed

+14
-4
lines changed

1 file changed

+14
-4
lines changed

cargo-miri/bin.rs

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -280,11 +280,21 @@ fn exec_with_pipe(mut cmd: Command, input: &[u8]) {
280280
use std::process::Stdio;
281281

282282
let mut fds: [libc::c_int; 2] = [0, 0];
283-
// We need to set close-on-exec, otherwise our pipe isn't readable after exec
284283
// SAFETY: fds is an array of 2 libc::c_int
285-
let res = unsafe { libc::pipe2(fds.as_mut_ptr(), libc::O_CLOEXEC) };
286-
if res != 0 {
287-
panic!("failed to create a pipe");
284+
let res = unsafe { libc::pipe(fds.as_mut_ptr()) };
285+
assert_eq!(res, 0, "failed to create pipe");
286+
287+
// We need to set close-on-exec, otherwise our pipe isn't readable after exec
288+
// SAFETY: fcntl has no preconditions
289+
unsafe {
290+
for fd in &fds {
291+
let res = libc::fcntl(
292+
*fd,
293+
libc::F_SETFD,
294+
libc::fcntl(*fd, libc::F_GETFD) | libc::FD_CLOEXEC,
295+
);
296+
assert_eq!(res, 0, "failed to set close-on-exec for pipe");
297+
}
288298
}
289299

290300
// SAFETY: Both elements of fds are open file descriptors, because pipe2 returned 0

0 commit comments

Comments
 (0)