]> git.lizzy.rs Git - rust.git/blob - src/libstd/sys/unix/pipe.rs
Auto merge of #37789 - arielb1:length-limit, r=nikomatsakis
[rust.git] / src / libstd / sys / unix / pipe.rs
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.
4 //
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.
10
11 use cmp;
12 use io;
13 use libc::{self, c_int};
14 use mem;
15 use ptr;
16 use sys::cvt_r;
17 use sys::fd::FileDesc;
18
19 ////////////////////////////////////////////////////////////////////////////////
20 // Anonymous pipes
21 ////////////////////////////////////////////////////////////////////////////////
22
23 pub struct AnonPipe(FileDesc);
24
25 pub fn anon_pipe() -> io::Result<(AnonPipe, AnonPipe)> {
26     let mut fds = [0; 2];
27
28     // Unfortunately the only known way right now to create atomically set the
29     // CLOEXEC flag is to use the `pipe2` syscall on Linux. This was added in
30     // 2.6.27, however, and because we support 2.6.18 we must detect this
31     // support dynamically.
32     if cfg!(target_os = "linux") {
33         weak! { fn pipe2(*mut c_int, c_int) -> c_int }
34         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             }
43         }
44     }
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     }
52 }
53
54 impl AnonPipe {
55     pub fn from_fd(fd: FileDesc) -> io::Result<AnonPipe> {
56         fd.set_cloexec()?;
57         Ok(AnonPipe(fd))
58     }
59
60     pub fn read(&self, buf: &mut [u8]) -> io::Result<usize> {
61         self.0.read(buf)
62     }
63
64     pub fn read_to_end(&self, buf: &mut Vec<u8>) -> io::Result<usize> {
65         self.0.read_to_end(buf)
66     }
67
68     pub fn write(&self, buf: &[u8]) -> io::Result<usize> {
69         self.0.write(buf)
70     }
71
72     pub fn fd(&self) -> &FileDesc { &self.0 }
73     pub fn into_fd(self) -> FileDesc { self.0 }
74 }
75
76 pub fn read2(p1: AnonPipe,
77              v1: &mut Vec<u8>,
78              p2: AnonPipe,
79              v2: &mut Vec<u8>) -> io::Result<()> {
80     // Set both pipes into nonblocking mode as we're gonna be reading from both
81     // in the `select` loop below, and we wouldn't want one to block the other!
82     let p1 = p1.into_fd();
83     let p2 = p2.into_fd();
84     p1.set_nonblocking(true)?;
85     p2.set_nonblocking(true)?;
86
87     let max = cmp::max(p1.raw(), p2.raw());
88     loop {
89         // wait for either pipe to become readable using `select`
90         cvt_r(|| unsafe {
91             let mut read: libc::fd_set = mem::zeroed();
92             libc::FD_SET(p1.raw(), &mut read);
93             libc::FD_SET(p2.raw(), &mut read);
94             libc::select(max + 1, &mut read, ptr::null_mut(), ptr::null_mut(),
95                          ptr::null_mut())
96         })?;
97
98         // Read as much as we can from each pipe, ignoring EWOULDBLOCK or
99         // EAGAIN. If we hit EOF, then this will happen because the underlying
100         // reader will return Ok(0), in which case we'll see `Ok` ourselves. In
101         // this case we flip the other fd back into blocking mode and read
102         // whatever's leftover on that file descriptor.
103         let read = |fd: &FileDesc, dst: &mut Vec<u8>| {
104             match fd.read_to_end(dst) {
105                 Ok(_) => Ok(true),
106                 Err(e) => {
107                     if e.raw_os_error() == Some(libc::EWOULDBLOCK) ||
108                        e.raw_os_error() == Some(libc::EAGAIN) {
109                         Ok(false)
110                     } else {
111                         Err(e)
112                     }
113                 }
114             }
115         };
116         if read(&p1, v1)? {
117             p2.set_nonblocking(false)?;
118             return p2.read_to_end(v2).map(|_| ());
119         }
120         if read(&p2, v2)? {
121             p1.set_nonblocking(false)?;
122             return p1.read_to_end(v1).map(|_| ());
123         }
124     }
125 }