]>
Commit | Line | Data |
---|---|---|
85aaf69f SL |
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 | ||
85aaf69f | 11 | use io; |
7453a54e | 12 | use libc::{self, c_int}; |
54a0048b | 13 | use mem; |
7cac9316 | 14 | use sync::atomic::{AtomicBool, ATOMIC_BOOL_INIT, Ordering}; |
7453a54e | 15 | use sys::fd::FileDesc; |
7cac9316 | 16 | use sys::{cvt, cvt_r}; |
85aaf69f SL |
17 | |
18 | //////////////////////////////////////////////////////////////////////////////// | |
19 | // Anonymous pipes | |
20 | //////////////////////////////////////////////////////////////////////////////// | |
21 | ||
22 | pub struct AnonPipe(FileDesc); | |
23 | ||
9346a6ac | 24 | pub fn anon_pipe() -> io::Result<(AnonPipe, AnonPipe)> { |
7cac9316 XL |
25 | weak! { fn pipe2(*mut c_int, c_int) -> c_int } |
26 | static INVALID: AtomicBool = ATOMIC_BOOL_INIT; | |
27 | ||
85aaf69f | 28 | let mut fds = [0; 2]; |
7453a54e SL |
29 | |
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. | |
8bb4bdeb XL |
34 | if cfg!(any(target_os = "dragonfly", |
35 | target_os = "freebsd", | |
36 | target_os = "linux", | |
37 | target_os = "netbsd", | |
7cac9316 XL |
38 | target_os = "openbsd")) && |
39 | !INVALID.load(Ordering::SeqCst) | |
8bb4bdeb | 40 | { |
7cac9316 | 41 | |
7453a54e | 42 | if let Some(pipe) = pipe2.get() { |
7cac9316 XL |
43 | // Note that despite calling a glibc function here we may still |
44 | // get ENOSYS. Glibc has `pipe2` since 2.9 and doesn't try to | |
45 | // emulate on older kernels, so if you happen to be running on | |
46 | // an older kernel you may see `pipe2` as a symbol but still not | |
47 | // see the syscall. | |
48 | match cvt(unsafe { pipe(fds.as_mut_ptr(), libc::O_CLOEXEC) }) { | |
49 | Ok(_) => { | |
50 | return Ok((AnonPipe(FileDesc::new(fds[0])), | |
51 | AnonPipe(FileDesc::new(fds[1])))); | |
52 | } | |
53 | Err(ref e) if e.raw_os_error() == Some(libc::ENOSYS) => { | |
54 | INVALID.store(true, Ordering::SeqCst); | |
55 | } | |
56 | Err(e) => return Err(e), | |
57 | } | |
7453a54e SL |
58 | } |
59 | } | |
8bb4bdeb XL |
60 | cvt(unsafe { libc::pipe(fds.as_mut_ptr()) })?; |
61 | ||
62 | let fd0 = FileDesc::new(fds[0]); | |
63 | let fd1 = FileDesc::new(fds[1]); | |
64 | fd0.set_cloexec()?; | |
65 | fd1.set_cloexec()?; | |
66 | Ok((AnonPipe(fd0), AnonPipe(fd1))) | |
85aaf69f SL |
67 | } |
68 | ||
69 | impl AnonPipe { | |
85aaf69f SL |
70 | pub fn read(&self, buf: &mut [u8]) -> io::Result<usize> { |
71 | self.0.read(buf) | |
72 | } | |
73 | ||
74 | pub fn write(&self, buf: &[u8]) -> io::Result<usize> { | |
75 | self.0.write(buf) | |
76 | } | |
77 | ||
62682a34 | 78 | pub fn fd(&self) -> &FileDesc { &self.0 } |
c1a9b12d | 79 | pub fn into_fd(self) -> FileDesc { self.0 } |
85aaf69f | 80 | } |
54a0048b SL |
81 | |
82 | pub fn read2(p1: AnonPipe, | |
83 | v1: &mut Vec<u8>, | |
84 | p2: AnonPipe, | |
85 | v2: &mut Vec<u8>) -> io::Result<()> { | |
476ff2be | 86 | |
54a0048b SL |
87 | // Set both pipes into nonblocking mode as we're gonna be reading from both |
88 | // in the `select` loop below, and we wouldn't want one to block the other! | |
89 | let p1 = p1.into_fd(); | |
90 | let p2 = p2.into_fd(); | |
3157f602 XL |
91 | p1.set_nonblocking(true)?; |
92 | p2.set_nonblocking(true)?; | |
54a0048b | 93 | |
cc61c64b XL |
94 | let mut fds: [libc::pollfd; 2] = unsafe { mem::zeroed() }; |
95 | fds[0].fd = p1.raw(); | |
96 | fds[0].events = libc::POLLIN; | |
97 | fds[1].fd = p2.raw(); | |
98 | fds[1].events = libc::POLLIN; | |
54a0048b | 99 | loop { |
cc61c64b XL |
100 | // wait for either pipe to become readable using `poll` |
101 | cvt_r(|| unsafe { libc::poll(fds.as_mut_ptr(), 2, -1) })?; | |
54a0048b SL |
102 | |
103 | // Read as much as we can from each pipe, ignoring EWOULDBLOCK or | |
104 | // EAGAIN. If we hit EOF, then this will happen because the underlying | |
105 | // reader will return Ok(0), in which case we'll see `Ok` ourselves. In | |
106 | // this case we flip the other fd back into blocking mode and read | |
107 | // whatever's leftover on that file descriptor. | |
108 | let read = |fd: &FileDesc, dst: &mut Vec<u8>| { | |
109 | match fd.read_to_end(dst) { | |
110 | Ok(_) => Ok(true), | |
111 | Err(e) => { | |
112 | if e.raw_os_error() == Some(libc::EWOULDBLOCK) || | |
113 | e.raw_os_error() == Some(libc::EAGAIN) { | |
114 | Ok(false) | |
115 | } else { | |
116 | Err(e) | |
117 | } | |
118 | } | |
119 | } | |
120 | }; | |
cc61c64b | 121 | if fds[0].revents != 0 && read(&p1, v1)? { |
3157f602 | 122 | p2.set_nonblocking(false)?; |
54a0048b SL |
123 | return p2.read_to_end(v2).map(|_| ()); |
124 | } | |
cc61c64b | 125 | if fds[1].revents != 0 && read(&p2, v2)? { |
3157f602 | 126 | p1.set_nonblocking(false)?; |
54a0048b SL |
127 | return p1.read_to_end(v1).map(|_| ()); |
128 | } | |
129 | } | |
130 | } |