]> git.lizzy.rs Git - rust.git/blob - library/std/src/sys/unix/pipe.rs
Rollup merge of #78111 - SNCPlay42:not-always-self, r=lcnr
[rust.git] / library / std / src / sys / unix / pipe.rs
1 use crate::io::{self, IoSlice, IoSliceMut};
2 use crate::mem;
3 use crate::sys::fd::FileDesc;
4 use crate::sys::{cvt, cvt_r};
5
6 ////////////////////////////////////////////////////////////////////////////////
7 // Anonymous pipes
8 ////////////////////////////////////////////////////////////////////////////////
9
10 pub struct AnonPipe(FileDesc);
11
12 pub fn anon_pipe() -> io::Result<(AnonPipe, AnonPipe)> {
13     let mut fds = [0; 2];
14
15     // The only known way right now to create atomically set the CLOEXEC flag is
16     // to use the `pipe2` syscall. This was added to Linux in 2.6.27, glibc 2.9
17     // and musl 0.9.3, and some other targets also have it.
18     cfg_if::cfg_if! {
19         if #[cfg(any(
20             target_os = "dragonfly",
21             target_os = "freebsd",
22             target_os = "linux",
23             target_os = "netbsd",
24             target_os = "openbsd",
25             target_os = "redox"
26         ))] {
27             cvt(unsafe { libc::pipe2(fds.as_mut_ptr(), libc::O_CLOEXEC) })?;
28             Ok((AnonPipe(FileDesc::new(fds[0])), AnonPipe(FileDesc::new(fds[1]))))
29         } else {
30             cvt(unsafe { libc::pipe(fds.as_mut_ptr()) })?;
31
32             let fd0 = FileDesc::new(fds[0]);
33             let fd1 = FileDesc::new(fds[1]);
34             fd0.set_cloexec()?;
35             fd1.set_cloexec()?;
36             Ok((AnonPipe(fd0), AnonPipe(fd1)))
37         }
38     }
39 }
40
41 impl AnonPipe {
42     pub fn read(&self, buf: &mut [u8]) -> io::Result<usize> {
43         self.0.read(buf)
44     }
45
46     pub fn read_vectored(&self, bufs: &mut [IoSliceMut<'_>]) -> io::Result<usize> {
47         self.0.read_vectored(bufs)
48     }
49
50     #[inline]
51     pub fn is_read_vectored(&self) -> bool {
52         self.0.is_read_vectored()
53     }
54
55     pub fn write(&self, buf: &[u8]) -> io::Result<usize> {
56         self.0.write(buf)
57     }
58
59     pub fn write_vectored(&self, bufs: &[IoSlice<'_>]) -> io::Result<usize> {
60         self.0.write_vectored(bufs)
61     }
62
63     #[inline]
64     pub fn is_write_vectored(&self) -> bool {
65         self.0.is_write_vectored()
66     }
67
68     pub fn fd(&self) -> &FileDesc {
69         &self.0
70     }
71     pub fn into_fd(self) -> FileDesc {
72         self.0
73     }
74 }
75
76 pub fn read2(p1: AnonPipe, v1: &mut Vec<u8>, p2: AnonPipe, v2: &mut Vec<u8>) -> io::Result<()> {
77     // Set both pipes into nonblocking mode as we're gonna be reading from both
78     // in the `select` loop below, and we wouldn't want one to block the other!
79     let p1 = p1.into_fd();
80     let p2 = p2.into_fd();
81     p1.set_nonblocking(true)?;
82     p2.set_nonblocking(true)?;
83
84     let mut fds: [libc::pollfd; 2] = unsafe { mem::zeroed() };
85     fds[0].fd = p1.raw();
86     fds[0].events = libc::POLLIN;
87     fds[1].fd = p2.raw();
88     fds[1].events = libc::POLLIN;
89     loop {
90         // wait for either pipe to become readable using `poll`
91         cvt_r(|| unsafe { libc::poll(fds.as_mut_ptr(), 2, -1) })?;
92
93         if fds[0].revents != 0 && read(&p1, v1)? {
94             p2.set_nonblocking(false)?;
95             return p2.read_to_end(v2).map(drop);
96         }
97         if fds[1].revents != 0 && read(&p2, v2)? {
98             p1.set_nonblocking(false)?;
99             return p1.read_to_end(v1).map(drop);
100         }
101     }
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     fn read(fd: &FileDesc, dst: &mut Vec<u8>) -> Result<bool, io::Error> {
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                 {
115                     Ok(false)
116                 } else {
117                     Err(e)
118                 }
119             }
120         }
121     }
122 }