]> git.proxmox.com Git - rustc.git/blob - src/libstd/sys/unix/pipe.rs
New upstream version 1.42.0+dfsg1
[rustc.git] / src / libstd / sys / unix / pipe.rs
1 use crate::io::{self, IoSlice, IoSliceMut};
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;
8
9 ////////////////////////////////////////////////////////////////////////////////
10 // Anonymous pipes
11 ////////////////////////////////////////////////////////////////////////////////
12
13 pub struct AnonPipe(FileDesc);
14
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);
18
19 let mut fds = [0; 2];
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.
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)
33 {
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(_) => {
41 return Ok((AnonPipe(FileDesc::new(fds[0])), AnonPipe(FileDesc::new(fds[1]))));
42 }
43 Err(ref e) if e.raw_os_error() == Some(libc::ENOSYS) => {
44 INVALID.store(true, Ordering::SeqCst);
45 }
46 Err(e) => return Err(e),
47 }
48 }
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)))
56 }
57
58 impl AnonPipe {
59 pub fn read(&self, buf: &mut [u8]) -> io::Result<usize> {
60 self.0.read(buf)
61 }
62
63 pub fn read_vectored(&self, bufs: &mut [IoSliceMut<'_>]) -> io::Result<usize> {
64 self.0.read_vectored(bufs)
65 }
66
67 pub fn write(&self, buf: &[u8]) -> io::Result<usize> {
68 self.0.write(buf)
69 }
70
71 pub fn write_vectored(&self, bufs: &[IoSlice<'_>]) -> io::Result<usize> {
72 self.0.write_vectored(bufs)
73 }
74
75 pub fn fd(&self) -> &FileDesc {
76 &self.0
77 }
78 pub fn into_fd(self) -> FileDesc {
79 self.0
80 }
81 }
82
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)?;
90
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;
96 loop {
97 // wait for either pipe to become readable using `poll`
98 cvt_r(|| unsafe { libc::poll(fds.as_mut_ptr(), 2, -1) })?;
99
100 if fds[0].revents != 0 && read(&p1, v1)? {
101 p2.set_nonblocking(false)?;
102 return p2.read_to_end(v2).map(drop);
103 }
104 if fds[1].revents != 0 && read(&p2, v2)? {
105 p1.set_nonblocking(false)?;
106 return p1.read_to_end(v1).map(drop);
107 }
108 }
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) => {
119 if e.raw_os_error() == Some(libc::EWOULDBLOCK)
120 || e.raw_os_error() == Some(libc::EAGAIN)
121 {
122 Ok(false)
123 } else {
124 Err(e)
125 }
126 }
127 }
128 }
129 }