Skip to content

Commit d5f5474

Browse files
committed
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.
2 parents 3b24c70 + 4b46d2a commit d5f5474

File tree

1 file changed

+17
-22
lines changed

1 file changed

+17
-22
lines changed

src/libstd/sys/unix/pipe.rs

+17-22
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use io;
1313
use libc::{self, c_int};
1414
use mem;
1515
use ptr;
16-
use sys::cvt_r;
16+
use sys::{cvt, cvt_r};
1717
use sys::fd::FileDesc;
1818

1919
////////////////////////////////////////////////////////////////////////////////
@@ -29,34 +29,29 @@ pub fn anon_pipe() -> io::Result<(AnonPipe, AnonPipe)> {
2929
// CLOEXEC flag is to use the `pipe2` syscall on Linux. This was added in
3030
// 2.6.27, however, and because we support 2.6.18 we must detect this
3131
// support dynamically.
32-
if cfg!(target_os = "linux") {
32+
if cfg!(any(target_os = "dragonfly",
33+
target_os = "freebsd",
34+
target_os = "linux",
35+
target_os = "netbsd",
36+
target_os = "openbsd"))
37+
{
3338
weak! { fn pipe2(*mut c_int, c_int) -> c_int }
3439
if let Some(pipe) = pipe2.get() {
35-
match cvt_r(|| unsafe { pipe(fds.as_mut_ptr(), libc::O_CLOEXEC) }) {
36-
Ok(_) => {
37-
return Ok((AnonPipe(FileDesc::new(fds[0])),
38-
AnonPipe(FileDesc::new(fds[1]))))
39-
}
40-
Err(ref e) if e.raw_os_error() == Some(libc::ENOSYS) => {}
41-
Err(e) => return Err(e),
42-
}
40+
cvt(unsafe { pipe(fds.as_mut_ptr(), libc::O_CLOEXEC) })?;
41+
return Ok((AnonPipe(FileDesc::new(fds[0])),
42+
AnonPipe(FileDesc::new(fds[1]))));
4343
}
4444
}
45-
if unsafe { libc::pipe(fds.as_mut_ptr()) == 0 } {
46-
let fd0 = FileDesc::new(fds[0]);
47-
let fd1 = FileDesc::new(fds[1]);
48-
Ok((AnonPipe::from_fd(fd0)?, AnonPipe::from_fd(fd1)?))
49-
} else {
50-
Err(io::Error::last_os_error())
51-
}
45+
cvt(unsafe { libc::pipe(fds.as_mut_ptr()) })?;
46+
47+
let fd0 = FileDesc::new(fds[0]);
48+
let fd1 = FileDesc::new(fds[1]);
49+
fd0.set_cloexec()?;
50+
fd1.set_cloexec()?;
51+
Ok((AnonPipe(fd0), AnonPipe(fd1)))
5252
}
5353

5454
impl AnonPipe {
55-
pub fn from_fd(fd: FileDesc) -> io::Result<AnonPipe> {
56-
fd.set_cloexec()?;
57-
Ok(AnonPipe(fd))
58-
}
59-
6055
pub fn read(&self, buf: &mut [u8]) -> io::Result<usize> {
6156
self.0.read(buf)
6257
}

0 commit comments

Comments
 (0)