Skip to content

Commit ae06e13

Browse files
committed
Move the pipe2 call behind a hard target #[cfg]
1 parent 37dd7a0 commit ae06e13

File tree

3 files changed

+29
-21
lines changed

3 files changed

+29
-21
lines changed

Diff for: src/libstd/sys/unix/os.rs

+1
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ pub fn errno() -> i32 {
7171

7272
/// Sets the platform-specific value of errno
7373
#[cfg(all(not(target_os = "linux"), not(target_os = "dragonfly")))] // needed for readdir and syscall!
74+
#[allow(dead_code)] // but not all target cfgs actually end up using it
7475
pub fn set_errno(e: i32) {
7576
unsafe { *errno_location() = e as c_int }
7677
}

Diff for: src/libstd/sys/unix/pipe.rs

+23-21
Original file line numberDiff line numberDiff line change
@@ -12,27 +12,29 @@ pub struct AnonPipe(FileDesc);
1212
pub fn anon_pipe() -> io::Result<(AnonPipe, AnonPipe)> {
1313
let mut fds = [0; 2];
1414

15-
// Unfortunately the only known way right now to create atomically set the
16-
// CLOEXEC flag is to use the `pipe2` syscall on Linux. This was added in
17-
// 2.6.27, glibc 2.9 and musl 0.9.3.
18-
if cfg!(any(
19-
target_os = "dragonfly",
20-
target_os = "freebsd",
21-
target_os = "linux",
22-
target_os = "netbsd",
23-
target_os = "openbsd",
24-
target_os = "redox"
25-
)) {
26-
cvt(unsafe { libc::pipe2(fds.as_mut_ptr(), libc::O_CLOEXEC) })?;
27-
Ok((AnonPipe(FileDesc::new(fds[0])), AnonPipe(FileDesc::new(fds[1]))))
28-
} else {
29-
cvt(unsafe { libc::pipe(fds.as_mut_ptr()) })?;
30-
31-
let fd0 = FileDesc::new(fds[0]);
32-
let fd1 = FileDesc::new(fds[1]);
33-
fd0.set_cloexec()?;
34-
fd1.set_cloexec()?;
35-
Ok((AnonPipe(fd0), AnonPipe(fd1)))
15+
// The only known way right now to create atomically set the CLOEXEC flag is
16+
// to use the `pipe2` syscall. This was added to Linux in 2.6.27, glibc 2.9
17+
// and musl 0.9.3, and some other targets also have it.
18+
cfg_if::cfg_if! {
19+
if #[cfg(any(
20+
target_os = "dragonfly",
21+
target_os = "freebsd",
22+
target_os = "linux",
23+
target_os = "netbsd",
24+
target_os = "openbsd",
25+
target_os = "redox"
26+
))] {
27+
cvt(unsafe { libc::pipe2(fds.as_mut_ptr(), libc::O_CLOEXEC) })?;
28+
Ok((AnonPipe(FileDesc::new(fds[0])), AnonPipe(FileDesc::new(fds[1]))))
29+
} else {
30+
cvt(unsafe { libc::pipe(fds.as_mut_ptr()) })?;
31+
32+
let fd0 = FileDesc::new(fds[0]);
33+
let fd1 = FileDesc::new(fds[1]);
34+
fd0.set_cloexec()?;
35+
fd1.set_cloexec()?;
36+
Ok((AnonPipe(fd0), AnonPipe(fd1)))
37+
}
3638
}
3739
}
3840

Diff for: src/libstd/sys/unix/weak.rs

+5
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,11 @@
1616
//! symbol, but that caused Debian to detect an unnecessarily strict versioned
1717
//! dependency on libc6 (#23628).
1818
19+
// There are a variety of `#[cfg]`s controlling which targets are involved in
20+
// each instance of `weak!` and `syscall!`. Rather than trying to unify all of
21+
// that, we'll just allow that some unix targets don't use this module at all.
22+
#![allow(dead_code, unused_macros)]
23+
1924
use crate::ffi::CStr;
2025
use crate::marker;
2126
use crate::mem;

0 commit comments

Comments
 (0)