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