]> git.proxmox.com Git - rustc.git/blame - src/libstd/sys/unix/pipe.rs
New upstream version 1.17.0+dfsg2
[rustc.git] / src / libstd / sys / unix / pipe.rs
CommitLineData
85aaf69f
SL
1// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
2// file at the top-level directory of this distribution and at
3// http://rust-lang.org/COPYRIGHT.
4//
5// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8// option. This file may not be copied, modified, or distributed
9// except according to those terms.
10
54a0048b 11use cmp;
85aaf69f 12use io;
7453a54e 13use libc::{self, c_int};
54a0048b 14use mem;
5bcae85e 15use ptr;
8bb4bdeb 16use sys::{cvt, cvt_r};
7453a54e 17use sys::fd::FileDesc;
85aaf69f
SL
18
19////////////////////////////////////////////////////////////////////////////////
20// Anonymous pipes
21////////////////////////////////////////////////////////////////////////////////
22
23pub struct AnonPipe(FileDesc);
24
9346a6ac 25pub fn anon_pipe() -> io::Result<(AnonPipe, AnonPipe)> {
85aaf69f 26 let mut fds = [0; 2];
7453a54e
SL
27
28 // Unfortunately the only known way right now to create atomically set the
29 // CLOEXEC flag is to use the `pipe2` syscall on Linux. This was added in
30 // 2.6.27, however, and because we support 2.6.18 we must detect this
31 // support dynamically.
8bb4bdeb
XL
32 if cfg!(any(target_os = "dragonfly",
33 target_os = "freebsd",
34 target_os = "linux",
35 target_os = "netbsd",
36 target_os = "openbsd"))
37 {
7453a54e
SL
38 weak! { fn pipe2(*mut c_int, c_int) -> c_int }
39 if let Some(pipe) = pipe2.get() {
8bb4bdeb
XL
40 cvt(unsafe { pipe(fds.as_mut_ptr(), libc::O_CLOEXEC) })?;
41 return Ok((AnonPipe(FileDesc::new(fds[0])),
42 AnonPipe(FileDesc::new(fds[1]))));
7453a54e
SL
43 }
44 }
8bb4bdeb
XL
45 cvt(unsafe { libc::pipe(fds.as_mut_ptr()) })?;
46
47 let fd0 = FileDesc::new(fds[0]);
48 let fd1 = FileDesc::new(fds[1]);
49 fd0.set_cloexec()?;
50 fd1.set_cloexec()?;
51 Ok((AnonPipe(fd0), AnonPipe(fd1)))
85aaf69f
SL
52}
53
54impl AnonPipe {
85aaf69f
SL
55 pub fn read(&self, buf: &mut [u8]) -> io::Result<usize> {
56 self.0.read(buf)
57 }
58
54a0048b
SL
59 pub fn read_to_end(&self, buf: &mut Vec<u8>) -> io::Result<usize> {
60 self.0.read_to_end(buf)
61 }
62
85aaf69f
SL
63 pub fn write(&self, buf: &[u8]) -> io::Result<usize> {
64 self.0.write(buf)
65 }
66
62682a34 67 pub fn fd(&self) -> &FileDesc { &self.0 }
c1a9b12d 68 pub fn into_fd(self) -> FileDesc { self.0 }
85aaf69f 69}
54a0048b
SL
70
71pub fn read2(p1: AnonPipe,
72 v1: &mut Vec<u8>,
73 p2: AnonPipe,
74 v2: &mut Vec<u8>) -> io::Result<()> {
476ff2be 75
54a0048b
SL
76 // Set both pipes into nonblocking mode as we're gonna be reading from both
77 // in the `select` loop below, and we wouldn't want one to block the other!
78 let p1 = p1.into_fd();
79 let p2 = p2.into_fd();
3157f602
XL
80 p1.set_nonblocking(true)?;
81 p2.set_nonblocking(true)?;
54a0048b
SL
82
83 let max = cmp::max(p1.raw(), p2.raw());
84 loop {
85 // wait for either pipe to become readable using `select`
86 cvt_r(|| unsafe {
87 let mut read: libc::fd_set = mem::zeroed();
88 libc::FD_SET(p1.raw(), &mut read);
89 libc::FD_SET(p2.raw(), &mut read);
5bcae85e
SL
90 libc::select(max + 1, &mut read, ptr::null_mut(), ptr::null_mut(),
91 ptr::null_mut())
54a0048b
SL
92 })?;
93
94 // Read as much as we can from each pipe, ignoring EWOULDBLOCK or
95 // EAGAIN. If we hit EOF, then this will happen because the underlying
96 // reader will return Ok(0), in which case we'll see `Ok` ourselves. In
97 // this case we flip the other fd back into blocking mode and read
98 // whatever's leftover on that file descriptor.
99 let read = |fd: &FileDesc, dst: &mut Vec<u8>| {
100 match fd.read_to_end(dst) {
101 Ok(_) => Ok(true),
102 Err(e) => {
103 if e.raw_os_error() == Some(libc::EWOULDBLOCK) ||
104 e.raw_os_error() == Some(libc::EAGAIN) {
105 Ok(false)
106 } else {
107 Err(e)
108 }
109 }
110 }
111 };
112 if read(&p1, v1)? {
3157f602 113 p2.set_nonblocking(false)?;
54a0048b
SL
114 return p2.read_to_end(v2).map(|_| ());
115 }
116 if read(&p2, v2)? {
3157f602 117 p1.set_nonblocking(false)?;
54a0048b
SL
118 return p1.read_to_end(v1).map(|_| ());
119 }
120 }
121}