1 #![unstable(reason = "not public", issue = "none", feature = "fd")]
7 use crate::io::{self, Initializer, IoSlice, IoSliceMut, Read};
10 use crate::sys_common::AsInner;
12 use libc::{c_int, c_void};
15 #[rustc_layout_scalar_valid_range_start(0)]
16 // libstd/os/raw/mod.rs assures me that every libstd-supported platform has a
17 // 32-bit c_int. Below is -2, in two's complement, but that only works out
18 // because c_int is 32 bits.
19 #[rustc_layout_scalar_valid_range_end(0xFF_FF_FF_FE)]
24 // The maximum read limit on most POSIX-like systems is `SSIZE_MAX`,
25 // with the man page quoting that if the count of bytes to read is
26 // greater than `SSIZE_MAX` the result is "unspecified".
28 // On macOS, however, apparently the 64-bit libc is either buggy or
29 // intentionally showing odd behavior by rejecting any read with a size
30 // larger than or equal to INT_MAX. To handle both of these the read
31 // size is capped on both platforms.
32 #[cfg(target_os = "macos")]
33 const READ_LIMIT: usize = c_int::MAX as usize - 1;
34 #[cfg(not(target_os = "macos"))]
35 const READ_LIMIT: usize = libc::ssize_t::MAX as usize;
38 target_os = "dragonfly",
39 target_os = "freebsd",
43 target_os = "openbsd",
45 const fn max_iov() -> usize {
46 libc::IOV_MAX as usize
49 #[cfg(any(target_os = "android", target_os = "emscripten", target_os = "linux"))]
50 const fn max_iov() -> usize {
51 libc::UIO_MAXIOV as usize
55 target_os = "android",
56 target_os = "dragonfly",
57 target_os = "emscripten",
58 target_os = "freebsd",
63 target_os = "openbsd",
65 const fn max_iov() -> usize {
66 16 // The minimum value required by POSIX.
70 pub fn new(fd: c_int) -> FileDesc {
71 assert_ne!(fd, -1i32);
72 // SAFETY: we just asserted that the value is in the valid range and isn't `-1` (the only value bigger than `0xFF_FF_FF_FE` unsigned)
73 unsafe { FileDesc { fd } }
76 pub fn raw(&self) -> c_int {
80 /// Extracts the actual file descriptor without closing it.
81 pub fn into_raw(self) -> c_int {
87 pub fn read(&self, buf: &mut [u8]) -> io::Result<usize> {
88 let ret = cvt(unsafe {
89 libc::read(self.fd, buf.as_mut_ptr() as *mut c_void, cmp::min(buf.len(), READ_LIMIT))
94 pub fn read_vectored(&self, bufs: &mut [IoSliceMut<'_>]) -> io::Result<usize> {
95 let ret = cvt(unsafe {
98 bufs.as_ptr() as *const libc::iovec,
99 cmp::min(bufs.len(), max_iov()) as c_int,
106 pub fn is_read_vectored(&self) -> bool {
110 pub fn read_to_end(&self, buf: &mut Vec<u8>) -> io::Result<usize> {
112 (&mut me).read_to_end(buf)
115 pub fn read_at(&self, buf: &mut [u8], offset: u64) -> io::Result<usize> {
116 #[cfg(target_os = "android")]
117 use super::android::cvt_pread64;
119 #[cfg(not(target_os = "android"))]
120 unsafe fn cvt_pread64(
125 ) -> io::Result<isize> {
126 #[cfg(not(target_os = "linux"))]
127 use libc::pread as pread64;
128 #[cfg(target_os = "linux")]
130 cvt(pread64(fd, buf, count, offset))
136 buf.as_mut_ptr() as *mut c_void,
137 cmp::min(buf.len(), READ_LIMIT),
144 pub fn write(&self, buf: &[u8]) -> io::Result<usize> {
145 let ret = cvt(unsafe {
146 libc::write(self.fd, buf.as_ptr() as *const c_void, cmp::min(buf.len(), READ_LIMIT))
151 pub fn write_vectored(&self, bufs: &[IoSlice<'_>]) -> io::Result<usize> {
152 let ret = cvt(unsafe {
155 bufs.as_ptr() as *const libc::iovec,
156 cmp::min(bufs.len(), max_iov()) as c_int,
163 pub fn is_write_vectored(&self) -> bool {
167 pub fn write_at(&self, buf: &[u8], offset: u64) -> io::Result<usize> {
168 #[cfg(target_os = "android")]
169 use super::android::cvt_pwrite64;
171 #[cfg(not(target_os = "android"))]
172 unsafe fn cvt_pwrite64(
177 ) -> io::Result<isize> {
178 #[cfg(not(target_os = "linux"))]
179 use libc::pwrite as pwrite64;
180 #[cfg(target_os = "linux")]
182 cvt(pwrite64(fd, buf, count, offset))
188 buf.as_ptr() as *const c_void,
189 cmp::min(buf.len(), READ_LIMIT),
196 #[cfg(target_os = "linux")]
197 pub fn get_cloexec(&self) -> io::Result<bool> {
198 unsafe { Ok((cvt(libc::fcntl(self.fd, libc::F_GETFD))? & libc::FD_CLOEXEC) != 0) }
202 target_env = "newlib",
203 target_os = "solaris",
204 target_os = "illumos",
205 target_os = "emscripten",
206 target_os = "fuchsia",
211 target_os = "vxworks"
213 pub fn set_cloexec(&self) -> io::Result<()> {
215 cvt(libc::ioctl(self.fd, libc::FIOCLEX))?;
220 target_env = "newlib",
221 target_os = "solaris",
222 target_os = "illumos",
223 target_os = "emscripten",
224 target_os = "fuchsia",
229 target_os = "vxworks"
231 pub fn set_cloexec(&self) -> io::Result<()> {
233 let previous = cvt(libc::fcntl(self.fd, libc::F_GETFD))?;
234 let new = previous | libc::FD_CLOEXEC;
236 cvt(libc::fcntl(self.fd, libc::F_SETFD, new))?;
242 #[cfg(target_os = "linux")]
243 pub fn set_nonblocking(&self, nonblocking: bool) -> io::Result<()> {
245 let v = nonblocking as c_int;
246 cvt(libc::ioctl(self.fd, libc::FIONBIO, &v))?;
251 #[cfg(not(target_os = "linux"))]
252 pub fn set_nonblocking(&self, nonblocking: bool) -> io::Result<()> {
254 let previous = cvt(libc::fcntl(self.fd, libc::F_GETFL))?;
255 let new = if nonblocking {
256 previous | libc::O_NONBLOCK
258 previous & !libc::O_NONBLOCK
261 cvt(libc::fcntl(self.fd, libc::F_SETFL, new))?;
267 pub fn duplicate(&self) -> io::Result<FileDesc> {
268 // We want to atomically duplicate this file descriptor and set the
269 // CLOEXEC flag, and currently that's done via F_DUPFD_CLOEXEC. This
270 // is a POSIX flag that was added to Linux in 2.6.24.
271 let fd = cvt(unsafe { libc::fcntl(self.raw(), libc::F_DUPFD_CLOEXEC, 0) })?;
272 Ok(FileDesc::new(fd))
276 impl<'a> Read for &'a FileDesc {
277 fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
282 unsafe fn initializer(&self) -> Initializer {
287 impl AsInner<c_int> for FileDesc {
288 fn as_inner(&self) -> &c_int {
293 impl Drop for FileDesc {
295 // Note that errors are ignored when closing a file descriptor. The
296 // reason for this is that if an error occurs we don't actually know if
297 // the file descriptor was closed or not, and if we retried (for
298 // something like EINTR), we might close another valid file descriptor
299 // opened after we closed ours.
300 let _ = unsafe { libc::close(self.fd) };