1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
use std::fs::File;
use std::io;
use std::os::unix::io::FromRawFd;
mod raw {
use libc::c_int;
pub const O_CLOEXEC: c_int = 0o2000000;
#[repr(C)]
pub struct PipeFds {
pub reader: c_int,
pub writer: c_int,
}
extern {
pub fn pipe2(fds: *mut PipeFds, flags: c_int) -> c_int;
}
}
pub struct Pipe {
pub reader: File,
pub writer: File,
}
impl Pipe {
pub fn new() -> io::Result<Pipe> {
let mut fds = raw::PipeFds {
reader: -1,
writer: -1
};
if unsafe { raw::pipe2(&mut fds, raw::O_CLOEXEC) } != 0 {
return Err(io::Error::last_os_error());
}
Ok(Pipe {
reader: unsafe { File::from_raw_fd(fds.reader) },
writer: unsafe { File::from_raw_fd(fds.writer) },
})
}
}