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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
use IntoInner;
use io::FileDesc;
use libc::{self, c_void, size_t};
use sys::cvt_r;
use std::fs::File;
use std::io::{Result, SeekFrom};
use std::mem;
use std::os::unix::io::{RawFd, AsRawFd, FromRawFd, IntoRawFd};
use std::process::Stdio;
mod fd_ext;
pub use self::fd_ext::{EventedFileDesc, FileDescExt, MaybeEventedFd};
#[derive(Debug, Eq)]
pub struct RawIo {
fd: RawFd,
}
impl PartialEq<RawIo> for RawIo {
fn eq(&self, other: &RawIo) -> bool {
self.fd == other.fd
}
}
impl Into<Stdio> for RawIo {
fn into(self) -> Stdio {
unsafe { FromRawFd::from_raw_fd(self.into_inner()) }
}
}
impl FromRawFd for FileDesc {
unsafe fn from_raw_fd(fd: RawFd) -> Self {
Self::new(fd)
}
}
impl AsRawFd for FileDesc {
fn as_raw_fd(&self) -> RawFd { self.inner().inner() }
}
impl IntoRawFd for FileDesc {
fn into_raw_fd(self) -> RawFd { unsafe { self.into_inner().into_inner() } }
}
impl From<File> for FileDesc {
fn from(file: File) -> Self {
unsafe { FromRawFd::from_raw_fd(file.into_raw_fd()) }
}
}
impl RawIo {
pub unsafe fn new(fd: RawFd) -> Self {
RawIo {
fd: fd,
}
}
pub unsafe fn into_inner(self) -> RawFd {
let fd = self.fd;
mem::forget(self);
fd
}
pub fn inner(&self) -> RawFd { self.fd }
pub fn duplicate(&self) -> Result<Self> {
unsafe {
Ok(RawIo::new(try!(cvt_r(|| { libc::dup(self.fd) }))))
}
}
pub fn read_inner(&self, buf: &mut [u8]) -> Result<usize> {
let ret = try!(cvt_r(|| unsafe {
libc::read(self.fd,
buf.as_mut_ptr() as *mut c_void,
buf.len() as size_t)
}));
Ok(ret as usize)
}
pub fn write_inner(&self, buf: &[u8]) -> Result<usize> {
let ret = try!(cvt_r(|| unsafe {
libc::write(self.fd,
buf.as_ptr() as *const c_void,
buf.len() as size_t)
}));
Ok(ret as usize)
}
pub fn flush_inner(&self) -> Result<()> {
Ok(())
}
pub fn seek(&self, pos: SeekFrom) -> Result<u64> {
let (whence, pos) = match pos {
SeekFrom::Start(off) => (libc::SEEK_SET, off as libc::off_t),
SeekFrom::End(off) => (libc::SEEK_END, off as libc::off_t),
SeekFrom::Current(off) => (libc::SEEK_CUR, off as libc::off_t),
};
let n = try!(cvt_r(|| unsafe { libc::lseek(self.fd, pos, whence) }));
Ok(n as u64)
}
pub fn set_cloexec(&self, set: bool) -> Result<()> {
unsafe {
let flags = try!(cvt_r(|| libc::fcntl(self.fd, libc::F_GETFD)));
let new_flags = if set {
flags | libc::FD_CLOEXEC
} else {
flags & !libc::FD_CLOEXEC
};
cvt_r(|| libc::fcntl(self.fd, libc::F_SETFD, new_flags)).map(|_| ())
}
}
pub fn set_nonblock(&self, set: bool) -> Result<()> {
unsafe {
let flags = try!(cvt_r(|| libc::fcntl(self.fd, libc::F_GETFL)));
let new_flags = if set {
flags | libc::O_NONBLOCK
} else {
flags & !libc::O_NONBLOCK
};
cvt_r(|| libc::fcntl(self.fd, libc::F_SETFL, new_flags)).map(|_| ())
}
}
}
impl Drop for RawIo {
fn drop(&mut self) {
let _ = unsafe { libc::close(self.fd) };
}
}
unsafe fn dup_fd_cloexec(fd: RawFd) -> Result<RawIo> {
let min_fd = libc::STDERR_FILENO + 1;
Ok(RawIo::new(try!(cvt_r(|| { libc::fcntl(fd, libc::F_DUPFD_CLOEXEC, min_fd) }))))
}
#[cfg(any(target_os = "linux", target_os = "android", target_os = "emscripten"))]
pub fn pipe() -> Result<(RawIo, RawIo)> {
unsafe {
let mut fds = [0; 2];
try!(cvt_r(|| {
libc::pipe2(fds.as_mut_ptr(), libc::O_CLOEXEC)
}));
let reader = RawIo::new(fds[0]);
let writer = RawIo::new(fds[1]);
Ok((reader, writer))
}
}
#[cfg(not(any(target_os = "linux", target_os = "android", target_os = "emscripten")))]
pub fn pipe() -> Result<(RawIo, RawIo)> {
unsafe {
let mut fds = [0; 2];
try!(cvt_r(|| {
libc::pipe(fds.as_mut_ptr())
}));
let reader = RawIo::new(fds[0]);
let writer = RawIo::new(fds[1]);
try!(reader.set_cloexec(true));
try!(writer.set_cloexec(true));
Ok((reader, writer))
}
}
pub fn dup_stdio() -> Result<(RawIo, RawIo, RawIo)> {
unsafe {
Ok((
try!(dup_fd_cloexec(libc::STDIN_FILENO)),
try!(dup_fd_cloexec(libc::STDOUT_FILENO)),
try!(dup_fd_cloexec(libc::STDERR_FILENO))
))
}
}
pub fn getpid() -> libc::pid_t {
unsafe {
libc::getpid()
}
}