about summary refs log tree commit diff
path: root/src/libstd/sys
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2017-02-02 13:04:53 +0000
committerbors <bors@rust-lang.org>2017-02-02 13:04:53 +0000
commitd5f54743dbad00aa4eb76c557c8d4824a7b770de (patch)
treee3d4b322630b159068aae31bb91bb65aae49574d /src/libstd/sys
parent3b24c70012b4d7269722efacb80203813ed9822a (diff)
parent4b46d2a3a222f090b07b019df0e9346b08c40ae1 (diff)
downloadrust-d5f54743dbad00aa4eb76c557c8d4824a7b770de.tar.gz
rust-d5f54743dbad00aa4eb76c557c8d4824a7b770de.zip
Auto merge of #39386 - tbu-:pr_pipe_less_syscalls, r=alexcrichton
Use less syscalls in `anon_pipe()`

Save a `ENOSYS` failure from `pipe2` and don't try again.

Use `cvt` instead of `cvt_r` for `pipe2` - `EINTR` is not an error
`pipe2` can return.
Diffstat (limited to 'src/libstd/sys')
-rw-r--r--src/libstd/sys/unix/pipe.rs39
1 files changed, 17 insertions, 22 deletions
diff --git a/src/libstd/sys/unix/pipe.rs b/src/libstd/sys/unix/pipe.rs
index a8ed415b7f4..51e00fc1ab9 100644
--- a/src/libstd/sys/unix/pipe.rs
+++ b/src/libstd/sys/unix/pipe.rs
@@ -13,7 +13,7 @@ use io;
 use libc::{self, c_int};
 use mem;
 use ptr;
-use sys::cvt_r;
+use sys::{cvt, cvt_r};
 use sys::fd::FileDesc;
 
 ////////////////////////////////////////////////////////////////////////////////
@@ -29,34 +29,29 @@ pub fn anon_pipe() -> io::Result<(AnonPipe, AnonPipe)> {
     // 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.
-    if cfg!(target_os = "linux") {
+    if cfg!(any(target_os = "dragonfly",
+                target_os = "freebsd",
+                target_os = "linux",
+                target_os = "netbsd",
+                target_os = "openbsd"))
+    {
         weak! { fn pipe2(*mut c_int, c_int) -> c_int }
         if let Some(pipe) = pipe2.get() {
-            match cvt_r(|| unsafe { pipe(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) => {}
-                Err(e) => return Err(e),
-            }
+            cvt(unsafe { pipe(fds.as_mut_ptr(), libc::O_CLOEXEC) })?;
+            return Ok((AnonPipe(FileDesc::new(fds[0])),
+                       AnonPipe(FileDesc::new(fds[1]))));
         }
     }
-    if unsafe { libc::pipe(fds.as_mut_ptr()) == 0 } {
-        let fd0 = FileDesc::new(fds[0]);
-        let fd1 = FileDesc::new(fds[1]);
-        Ok((AnonPipe::from_fd(fd0)?, AnonPipe::from_fd(fd1)?))
-    } else {
-        Err(io::Error::last_os_error())
-    }
+    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 {
-    pub fn from_fd(fd: FileDesc) -> io::Result<AnonPipe> {
-        fd.set_cloexec()?;
-        Ok(AnonPipe(fd))
-    }
-
     pub fn read(&self, buf: &mut [u8]) -> io::Result<usize> {
         self.0.read(buf)
     }