1 use crate::io
::{self, IoSlice, IoSliceMut}
;
3 use crate::sync
::atomic
::{AtomicBool, Ordering}
;
4 use crate::sys
::fd
::FileDesc
;
5 use crate::sys
::{cvt, cvt_r}
;
9 ////////////////////////////////////////////////////////////////////////////////
11 ////////////////////////////////////////////////////////////////////////////////
13 pub struct AnonPipe(FileDesc
);
15 pub fn anon_pipe() -> io
::Result
<(AnonPipe
, AnonPipe
)> {
16 syscall
! { fn pipe2(fds: *mut c_int, flags: c_int) -> c_int }
17 static INVALID
: AtomicBool
= AtomicBool
::new(false);
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.
26 target_os
= "dragonfly",
27 target_os
= "freebsd",
30 target_os
= "openbsd",
32 )) && !INVALID
.load(Ordering
::SeqCst
)
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
39 match cvt(unsafe { pipe2(fds.as_mut_ptr(), libc::O_CLOEXEC) }
) {
41 return Ok((AnonPipe(FileDesc
::new(fds
[0])), AnonPipe(FileDesc
::new(fds
[1]))));
43 Err(ref e
) if e
.raw_os_error() == Some(libc
::ENOSYS
) => {
44 INVALID
.store(true, Ordering
::SeqCst
);
46 Err(e
) => return Err(e
),
49 cvt(unsafe { libc::pipe(fds.as_mut_ptr()) }
)?
;
51 let fd0
= FileDesc
::new(fds
[0]);
52 let fd1
= FileDesc
::new(fds
[1]);
55 Ok((AnonPipe(fd0
), AnonPipe(fd1
)))
59 pub fn read(&self, buf
: &mut [u8]) -> io
::Result
<usize> {
63 pub fn read_vectored(&self, bufs
: &mut [IoSliceMut
<'_
>]) -> io
::Result
<usize> {
64 self.0.read_vectored(bufs
)
67 pub fn write(&self, buf
: &[u8]) -> io
::Result
<usize> {
71 pub fn write_vectored(&self, bufs
: &[IoSlice
<'_
>]) -> io
::Result
<usize> {
72 self.0.write_vectored(bufs
)
75 pub fn fd(&self) -> &FileDesc
{
78 pub fn into_fd(self) -> FileDesc
{
83 pub fn read2(p1
: AnonPipe
, v1
: &mut Vec
<u8>, p2
: AnonPipe
, v2
: &mut Vec
<u8>) -> io
::Result
<()> {
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();
88 p1
.set_nonblocking(true)?
;
89 p2
.set_nonblocking(true)?
;
91 let mut fds
: [libc
::pollfd
; 2] = unsafe { mem::zeroed() }
;
93 fds
[0].events
= libc
::POLLIN
;
95 fds
[1].events
= libc
::POLLIN
;
97 // wait for either pipe to become readable using `poll`
98 cvt_r(|| unsafe { libc::poll(fds.as_mut_ptr(), 2, -1) }
)?
;
100 if fds
[0].revents
!= 0 && read(&p1
, v1
)?
{
101 p2
.set_nonblocking(false)?
;
102 return p2
.read_to_end(v2
).map(drop
);
104 if fds
[1].revents
!= 0 && read(&p2
, v2
)?
{
105 p1
.set_nonblocking(false)?
;
106 return p1
.read_to_end(v1
).map(drop
);
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
) {
119 if e
.raw_os_error() == Some(libc
::EWOULDBLOCK
)
120 || e
.raw_os_error() == Some(libc
::EAGAIN
)