about summary refs log tree commit diff
path: root/src/libstd/sys/unix/pipe.rs
diff options
context:
space:
mode:
authorJosh Stone <jistone@redhat.com>2020-07-21 14:32:36 -0700
committerJosh Stone <jistone@redhat.com>2020-07-21 14:32:36 -0700
commit37dd7a023b8cef226c73bb9e17b37e379b4ffb48 (patch)
treebdc22036dac7caee8a652cbd609f5825f12cb428 /src/libstd/sys/unix/pipe.rs
parent8ad7bc3f428300aee6764f6e23527e19eb235e81 (diff)
downloadrust-37dd7a023b8cef226c73bb9e17b37e379b4ffb48.tar.gz
rust-37dd7a023b8cef226c73bb9e17b37e379b4ffb48.zip
Remove Linux workarounds for missing CLOEXEC support
Now that #74163 updated the minimum Linux kernel to 2.6.32, we can
assume the availability of APIs that open file descriptors that are
already set to close on exec, including the flags `O_CLOEXEC`,
`SOCK_CLOEXEC`, and `F_DUPFD_CLOEXEC`.
Diffstat (limited to 'src/libstd/sys/unix/pipe.rs')
-rw-r--r--src/libstd/sys/unix/pipe.rs43
1 files changed, 12 insertions, 31 deletions
diff --git a/src/libstd/sys/unix/pipe.rs b/src/libstd/sys/unix/pipe.rs
index f2a2eabef91..02a43923708 100644
--- a/src/libstd/sys/unix/pipe.rs
+++ b/src/libstd/sys/unix/pipe.rs
@@ -1,11 +1,8 @@
 use crate::io::{self, IoSlice, IoSliceMut};
 use crate::mem;
-use crate::sync::atomic::{AtomicBool, Ordering};
 use crate::sys::fd::FileDesc;
 use crate::sys::{cvt, cvt_r};
 
-use libc::c_int;
-
 ////////////////////////////////////////////////////////////////////////////////
 // Anonymous pipes
 ////////////////////////////////////////////////////////////////////////////////
@@ -13,15 +10,11 @@ use libc::c_int;
 pub struct AnonPipe(FileDesc);
 
 pub fn anon_pipe() -> io::Result<(AnonPipe, AnonPipe)> {
-    syscall! { fn pipe2(fds: *mut c_int, flags: c_int) -> c_int }
-    static INVALID: AtomicBool = AtomicBool::new(false);
-
     let mut fds = [0; 2];
 
     // Unfortunately the only known way right now to create atomically set the
     // CLOEXEC flag is to use the `pipe2` syscall on Linux. This was added in
-    // 2.6.27, however, and because we support 2.6.18 we must detect this
-    // support dynamically.
+    // 2.6.27, glibc 2.9 and musl 0.9.3.
     if cfg!(any(
         target_os = "dragonfly",
         target_os = "freebsd",
@@ -29,30 +22,18 @@ pub fn anon_pipe() -> io::Result<(AnonPipe, AnonPipe)> {
         target_os = "netbsd",
         target_os = "openbsd",
         target_os = "redox"
-    )) && !INVALID.load(Ordering::SeqCst)
-    {
-        // Note that despite calling a glibc function here we may still
-        // get ENOSYS. Glibc has `pipe2` since 2.9 and doesn't try to
-        // emulate on older kernels, so if you happen to be running on
-        // an older kernel you may see `pipe2` as a symbol but still not
-        // see the syscall.
-        match cvt(unsafe { pipe2(fds.as_mut_ptr(), libc::O_CLOEXEC) }) {
-            Ok(_) => {
-                return Ok((AnonPipe(FileDesc::new(fds[0])), AnonPipe(FileDesc::new(fds[1]))));
-            }
-            Err(ref e) if e.raw_os_error() == Some(libc::ENOSYS) => {
-                INVALID.store(true, Ordering::SeqCst);
-            }
-            Err(e) => return Err(e),
-        }
+    )) {
+        cvt(unsafe { libc::pipe2(fds.as_mut_ptr(), libc::O_CLOEXEC) })?;
+        Ok((AnonPipe(FileDesc::new(fds[0])), AnonPipe(FileDesc::new(fds[1]))))
+    } else {
+        cvt(unsafe { libc::pipe(fds.as_mut_ptr()) })?;
+
+        let fd0 = FileDesc::new(fds[0]);
+        let fd1 = FileDesc::new(fds[1]);
+        fd0.set_cloexec()?;
+        fd1.set_cloexec()?;
+        Ok((AnonPipe(fd0), AnonPipe(fd1)))
     }
-    cvt(unsafe { libc::pipe(fds.as_mut_ptr()) })?;
-
-    let fd0 = FileDesc::new(fds[0]);
-    let fd1 = FileDesc::new(fds[1]);
-    fd0.set_cloexec()?;
-    fd1.set_cloexec()?;
-    Ok((AnonPipe(fd0), AnonPipe(fd1)))
 }
 
 impl AnonPipe {