1use crate::io::{self, BorrowedCursor, IoSlice, IoSliceMut};
2use crate::mem;
3use crate::os::unix::io::{AsFd, AsRawFd, BorrowedFd, FromRawFd, IntoRawFd, RawFd};
4use crate::sys::fd::FileDesc;
5use crate::sys::{cvt, cvt_r};
6use crate::sys_common::{FromInner, IntoInner};
7
8////////////////////////////////////////////////////////////////////////////////
9// Anonymous pipes
10////////////////////////////////////////////////////////////////////////////////
11
12pub struct AnonPipe(FileDesc);
13
14pub fn anon_pipe() -> io::Result<(AnonPipe, AnonPipe)> {
15 let mut fds = [0; 2];
16
17 // The only known way right now to create atomically set the CLOEXEC flag is
18 // to use the `pipe2` syscall. This was added to Linux in 2.6.27, glibc 2.9
19 // and musl 0.9.3, and some other targets also have it.
20 cfg_if::cfg_if! {
21 if #[cfg(any(
22 target_os = "dragonfly",
23 target_os = "freebsd",
24 target_os = "hurd",
25 target_os = "linux",
26 target_os = "netbsd",
27 target_os = "openbsd",
28 target_os = "redox"
29 ))] {
30 unsafe {
31 cvt(libc::pipe2(fds.as_mut_ptr(), libc::O_CLOEXEC))?;
32 Ok((AnonPipe(FileDesc::from_raw_fd(fds[0])), AnonPipe(FileDesc::from_raw_fd(fds[1]))))
33 }
34 } else {
35 unsafe {
36 cvt(libc::pipe(fds.as_mut_ptr()))?;
37
38 let fd0 = FileDesc::from_raw_fd(fds[0]);
39 let fd1 = FileDesc::from_raw_fd(fds[1]);
40 fd0.set_cloexec()?;
41 fd1.set_cloexec()?;
42 Ok((AnonPipe(fd0), AnonPipe(fd1)))
43 }
44 }
45 }
46}
47
48impl AnonPipe {
49 pub fn read(&self, buf: &mut [u8]) -> io::Result<usize> {
50 self.0.read(buf)
51 }
52
53 pub fn read_buf(&self, buf: BorrowedCursor<'_>) -> io::Result<()> {
54 self.0.read_buf(buf)
55 }
56
57 pub fn read_vectored(&self, bufs: &mut [IoSliceMut<'_>]) -> io::Result<usize> {
58 self.0.read_vectored(bufs)
59 }
60
61 #[inline]
62 pub fn is_read_vectored(&self) -> bool {
63 self.0.is_read_vectored()
64 }
65
66 pub fn read_to_end(&self, buf: &mut Vec<u8>) -> io::Result<usize> {
67 self.0.read_to_end(buf)
68 }
69
70 pub fn write(&self, buf: &[u8]) -> io::Result<usize> {
71 self.0.write(buf)
72 }
73
74 pub fn write_vectored(&self, bufs: &[IoSlice<'_>]) -> io::Result<usize> {
75 self.0.write_vectored(bufs)
76 }
77
78 #[inline]
79 pub fn is_write_vectored(&self) -> bool {
80 self.0.is_write_vectored()
81 }
82}
83
84impl IntoInner<FileDesc> for AnonPipe {
85 fn into_inner(self) -> FileDesc {
86 self.0
87 }
88}
89
90pub fn read2(p1: AnonPipe, v1: &mut Vec<u8>, p2: AnonPipe, v2: &mut Vec<u8>) -> io::Result<()> {
91 // Set both pipes into nonblocking mode as we're gonna be reading from both
92 // in the `select` loop below, and we wouldn't want one to block the other!
93 let p1 = p1.into_inner();
94 let p2 = p2.into_inner();
95 p1.set_nonblocking(true)?;
96 p2.set_nonblocking(true)?;
97
98 let mut fds: [libc::pollfd; 2] = unsafe { mem::zeroed() };
99 fds[0].fd = p1.as_raw_fd();
100 fds[0].events = libc::POLLIN;
101 fds[1].fd = p2.as_raw_fd();
102 fds[1].events = libc::POLLIN;
103 loop {
104 // wait for either pipe to become readable using `poll`
105 cvt_r(|| unsafe { libc::poll(fds.as_mut_ptr(), 2, -1) })?;
106
107 if fds[0].revents != 0 && read(&p1, v1)? {
108 p2.set_nonblocking(false)?;
109 return p2.read_to_end(v2).map(drop);
110 }
111 if fds[1].revents != 0 && read(&p2, v2)? {
112 p1.set_nonblocking(false)?;
113 return p1.read_to_end(v1).map(drop);
114 }
115 }
116
117 // Read as much as we can from each pipe, ignoring EWOULDBLOCK or
118 // EAGAIN. If we hit EOF, then this will happen because the underlying
119 // reader will return Ok(0), in which case we'll see `Ok` ourselves. In
120 // this case we flip the other fd back into blocking mode and read
121 // whatever's leftover on that file descriptor.
122 fn read(fd: &FileDesc, dst: &mut Vec<u8>) -> Result<bool, io::Error> {
123 match fd.read_to_end(dst) {
124 Ok(_) => Ok(true),
125 Err(e) => {
126 if e.raw_os_error() == Some(libc::EWOULDBLOCK)
127 || e.raw_os_error() == Some(libc::EAGAIN)
128 {
129 Ok(false)
130 } else {
131 Err(e)
132 }
133 }
134 }
135 }
136}
137
138impl AsRawFd for AnonPipe {
139 #[inline]
140 fn as_raw_fd(&self) -> RawFd {
141 self.0.as_raw_fd()
142 }
143}
144
145impl AsFd for AnonPipe {
146 fn as_fd(&self) -> BorrowedFd<'_> {
147 self.0.as_fd()
148 }
149}
150
151impl IntoRawFd for AnonPipe {
152 fn into_raw_fd(self) -> RawFd {
153 self.0.into_raw_fd()
154 }
155}
156
157impl FromRawFd for AnonPipe {
158 unsafe fn from_raw_fd(raw_fd: RawFd) -> Self {
159 Self(FromRawFd::from_raw_fd(raw_fd))
160 }
161}
162
163impl FromInner<FileDesc> for AnonPipe {
164 fn from_inner(fd: FileDesc) -> Self {
165 Self(fd)
166 }
167}
168