]> git.lizzy.rs Git - rust.git/blob - library/std/src/sys/unix/pipe.rs
Rollup merge of #105598 - RalfJung:more-comments, r=the8472
[rust.git] / library / std / src / sys / unix / pipe.rs
1 use crate::io::{self, IoSlice, IoSliceMut};
2 use crate::mem;
3 use crate::os::unix::io::{AsFd, AsRawFd, BorrowedFd, FromRawFd, IntoRawFd, RawFd};
4 use crate::sys::fd::FileDesc;
5 use crate::sys::{cvt, cvt_r};
6 use crate::sys_common::IntoInner;
7
8 ////////////////////////////////////////////////////////////////////////////////
9 // Anonymous pipes
10 ////////////////////////////////////////////////////////////////////////////////
11
12 pub struct AnonPipe(FileDesc);
13
14 pub 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 = "linux",
25             target_os = "netbsd",
26             target_os = "openbsd",
27             target_os = "redox"
28         ))] {
29             unsafe {
30                 cvt(libc::pipe2(fds.as_mut_ptr(), libc::O_CLOEXEC))?;
31                 Ok((AnonPipe(FileDesc::from_raw_fd(fds[0])), AnonPipe(FileDesc::from_raw_fd(fds[1]))))
32             }
33         } else {
34             unsafe {
35                 cvt(libc::pipe(fds.as_mut_ptr()))?;
36
37                 let fd0 = FileDesc::from_raw_fd(fds[0]);
38                 let fd1 = FileDesc::from_raw_fd(fds[1]);
39                 fd0.set_cloexec()?;
40                 fd1.set_cloexec()?;
41                 Ok((AnonPipe(fd0), AnonPipe(fd1)))
42             }
43         }
44     }
45 }
46
47 impl AnonPipe {
48     pub fn read(&self, buf: &mut [u8]) -> io::Result<usize> {
49         self.0.read(buf)
50     }
51
52     pub fn read_vectored(&self, bufs: &mut [IoSliceMut<'_>]) -> io::Result<usize> {
53         self.0.read_vectored(bufs)
54     }
55
56     #[inline]
57     pub fn is_read_vectored(&self) -> bool {
58         self.0.is_read_vectored()
59     }
60
61     pub fn write(&self, buf: &[u8]) -> io::Result<usize> {
62         self.0.write(buf)
63     }
64
65     pub fn write_vectored(&self, bufs: &[IoSlice<'_>]) -> io::Result<usize> {
66         self.0.write_vectored(bufs)
67     }
68
69     #[inline]
70     pub fn is_write_vectored(&self) -> bool {
71         self.0.is_write_vectored()
72     }
73 }
74
75 impl IntoInner<FileDesc> for AnonPipe {
76     fn into_inner(self) -> FileDesc {
77         self.0
78     }
79 }
80
81 pub fn read2(p1: AnonPipe, v1: &mut Vec<u8>, p2: AnonPipe, v2: &mut Vec<u8>) -> io::Result<()> {
82     // Set both pipes into nonblocking mode as we're gonna be reading from both
83     // in the `select` loop below, and we wouldn't want one to block the other!
84     let p1 = p1.into_inner();
85     let p2 = p2.into_inner();
86     p1.set_nonblocking(true)?;
87     p2.set_nonblocking(true)?;
88
89     let mut fds: [libc::pollfd; 2] = unsafe { mem::zeroed() };
90     fds[0].fd = p1.as_raw_fd();
91     fds[0].events = libc::POLLIN;
92     fds[1].fd = p2.as_raw_fd();
93     fds[1].events = libc::POLLIN;
94     loop {
95         // wait for either pipe to become readable using `poll`
96         cvt_r(|| unsafe { libc::poll(fds.as_mut_ptr(), 2, -1) })?;
97
98         if fds[0].revents != 0 && read(&p1, v1)? {
99             p2.set_nonblocking(false)?;
100             return p2.read_to_end(v2).map(drop);
101         }
102         if fds[1].revents != 0 && read(&p2, v2)? {
103             p1.set_nonblocking(false)?;
104             return p1.read_to_end(v1).map(drop);
105         }
106     }
107
108     // Read as much as we can from each pipe, ignoring EWOULDBLOCK or
109     // EAGAIN. If we hit EOF, then this will happen because the underlying
110     // reader will return Ok(0), in which case we'll see `Ok` ourselves. In
111     // this case we flip the other fd back into blocking mode and read
112     // whatever's leftover on that file descriptor.
113     fn read(fd: &FileDesc, dst: &mut Vec<u8>) -> Result<bool, io::Error> {
114         match fd.read_to_end(dst) {
115             Ok(_) => Ok(true),
116             Err(e) => {
117                 if e.raw_os_error() == Some(libc::EWOULDBLOCK)
118                     || e.raw_os_error() == Some(libc::EAGAIN)
119                 {
120                     Ok(false)
121                 } else {
122                     Err(e)
123                 }
124             }
125         }
126     }
127 }
128
129 impl AsRawFd for AnonPipe {
130     fn as_raw_fd(&self) -> RawFd {
131         self.0.as_raw_fd()
132     }
133 }
134
135 impl AsFd for AnonPipe {
136     fn as_fd(&self) -> BorrowedFd<'_> {
137         self.0.as_fd()
138     }
139 }
140
141 impl IntoRawFd for AnonPipe {
142     fn into_raw_fd(self) -> RawFd {
143         self.0.into_raw_fd()
144     }
145 }
146
147 impl FromRawFd for AnonPipe {
148     unsafe fn from_raw_fd(raw_fd: RawFd) -> Self {
149         Self(FromRawFd::from_raw_fd(raw_fd))
150     }
151 }