1 // Copyright 2015 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
15 use libc::{self, c_int};
19 use sys::fd::FileDesc;
21 ////////////////////////////////////////////////////////////////////////////////
23 ////////////////////////////////////////////////////////////////////////////////
25 pub struct AnonPipe(FileDesc);
27 pub fn anon_pipe() -> io::Result<(AnonPipe, AnonPipe)> {
30 // Unfortunately the only known way right now to create atomically set the
31 // CLOEXEC flag is to use the `pipe2` syscall on Linux. This was added in
32 // 2.6.27, however, and because we support 2.6.18 we must detect this
33 // support dynamically.
34 if cfg!(target_os = "linux") {
35 weak! { fn pipe2(*mut c_int, c_int) -> c_int }
36 if let Some(pipe) = pipe2.get() {
37 match cvt_r(|| unsafe { pipe(fds.as_mut_ptr(), libc::O_CLOEXEC) }) {
39 return Ok((AnonPipe(FileDesc::new(fds[0])),
40 AnonPipe(FileDesc::new(fds[1]))))
42 Err(ref e) if e.raw_os_error() == Some(libc::ENOSYS) => {}
43 Err(e) => return Err(e),
47 if unsafe { libc::pipe(fds.as_mut_ptr()) == 0 } {
48 let fd0 = FileDesc::new(fds[0]);
49 let fd1 = FileDesc::new(fds[1]);
50 Ok((AnonPipe::from_fd(fd0)?, AnonPipe::from_fd(fd1)?))
52 Err(io::Error::last_os_error())
57 pub fn from_fd(fd: FileDesc) -> io::Result<AnonPipe> {
62 pub fn read(&self, buf: &mut [u8]) -> io::Result<usize> {
66 pub fn read_to_end(&self, buf: &mut Vec<u8>) -> io::Result<usize> {
67 self.0.read_to_end(buf)
70 pub fn write(&self, buf: &[u8]) -> io::Result<usize> {
74 pub fn fd(&self) -> &FileDesc { &self.0 }
75 pub fn into_fd(self) -> FileDesc { self.0 }
78 pub fn read2(p1: AnonPipe,
81 v2: &mut Vec<u8>) -> io::Result<()> {
82 // Set both pipes into nonblocking mode as we're gonna be reading from both
83 // in the `select` loop below, and we wouldn't want one to block the other!
84 let p1 = p1.into_fd();
85 let p2 = p2.into_fd();
86 p1.set_nonblocking(true)?;
87 p2.set_nonblocking(true)?;
89 let max = cmp::max(p1.raw(), p2.raw());
91 // wait for either pipe to become readable using `select`
93 let mut read: libc::fd_set = mem::zeroed();
94 libc::FD_SET(p1.raw(), &mut read);
95 libc::FD_SET(p2.raw(), &mut read);
96 libc::select(max + 1, &mut read, ptr::null_mut(), ptr::null_mut(),
100 // Read as much as we can from each pipe, ignoring EWOULDBLOCK or
101 // EAGAIN. If we hit EOF, then this will happen because the underlying
102 // reader will return Ok(0), in which case we'll see `Ok` ourselves. In
103 // this case we flip the other fd back into blocking mode and read
104 // whatever's leftover on that file descriptor.
105 let read = |fd: &FileDesc, dst: &mut Vec<u8>| {
106 match fd.read_to_end(dst) {
109 if e.raw_os_error() == Some(libc::EWOULDBLOCK) ||
110 e.raw_os_error() == Some(libc::EAGAIN) {
119 p2.set_nonblocking(false)?;
120 return p2.read_to_end(v2).map(|_| ());
123 p1.set_nonblocking(false)?;
124 return p1.read_to_end(v1).map(|_| ());