1 #![unstable(reason = "not public", issue = "none", feature = "fd")]
4 use crate::io::{self, Initializer, IoSlice, IoSliceMut, Read};
6 #[cfg(not(any(target_os = "redox", target_env = "newlib")))]
7 use crate::sync::atomic::{AtomicUsize, Ordering};
9 use crate::sys_common::AsInner;
11 use libc::{c_int, c_void};
18 // The maximum read limit on most POSIX-like systems is `SSIZE_MAX`,
19 // with the man page quoting that if the count of bytes to read is
20 // greater than `SSIZE_MAX` the result is "unspecified".
22 // On macOS, however, apparently the 64-bit libc is either buggy or
23 // intentionally showing odd behavior by rejecting any read with a size
24 // larger than or equal to INT_MAX. To handle both of these the read
25 // size is capped on both platforms.
26 #[cfg(target_os = "macos")]
27 const READ_LIMIT: usize = c_int::MAX as usize - 1;
28 #[cfg(not(target_os = "macos"))]
29 const READ_LIMIT: usize = libc::ssize_t::MAX as usize;
31 #[cfg(not(any(target_os = "redox", target_env = "newlib")))]
32 fn max_iov() -> usize {
33 static LIM: AtomicUsize = AtomicUsize::new(0);
35 let mut lim = LIM.load(Ordering::Relaxed);
37 let ret = unsafe { libc::sysconf(libc::_SC_IOV_MAX) };
39 // 16 is the minimum value required by POSIX.
40 lim = if ret > 0 { ret as usize } else { 16 };
41 LIM.store(lim, Ordering::Relaxed);
47 #[cfg(any(target_os = "redox", target_env = "newlib"))]
48 fn max_iov() -> usize {
49 16 // The minimum value required by POSIX.
53 pub fn new(fd: c_int) -> FileDesc {
57 pub fn raw(&self) -> c_int {
61 /// Extracts the actual file descriptor without closing it.
62 pub fn into_raw(self) -> c_int {
68 pub fn read(&self, buf: &mut [u8]) -> io::Result<usize> {
69 let ret = cvt(unsafe {
70 libc::read(self.fd, buf.as_mut_ptr() as *mut c_void, cmp::min(buf.len(), READ_LIMIT))
75 pub fn read_vectored(&self, bufs: &mut [IoSliceMut<'_>]) -> io::Result<usize> {
76 let ret = cvt(unsafe {
79 bufs.as_ptr() as *const libc::iovec,
80 cmp::min(bufs.len(), max_iov()) as c_int,
87 pub fn is_read_vectored(&self) -> bool {
91 pub fn read_to_end(&self, buf: &mut Vec<u8>) -> io::Result<usize> {
93 (&mut me).read_to_end(buf)
96 pub fn read_at(&self, buf: &mut [u8], offset: u64) -> io::Result<usize> {
97 #[cfg(target_os = "android")]
98 use super::android::cvt_pread64;
100 #[cfg(not(target_os = "android"))]
101 unsafe fn cvt_pread64(
106 ) -> io::Result<isize> {
107 #[cfg(not(target_os = "linux"))]
108 use libc::pread as pread64;
109 #[cfg(target_os = "linux")]
111 cvt(pread64(fd, buf, count, offset))
117 buf.as_mut_ptr() as *mut c_void,
118 cmp::min(buf.len(), READ_LIMIT),
125 pub fn write(&self, buf: &[u8]) -> io::Result<usize> {
126 let ret = cvt(unsafe {
127 libc::write(self.fd, buf.as_ptr() as *const c_void, cmp::min(buf.len(), READ_LIMIT))
132 pub fn write_vectored(&self, bufs: &[IoSlice<'_>]) -> io::Result<usize> {
133 let ret = cvt(unsafe {
136 bufs.as_ptr() as *const libc::iovec,
137 cmp::min(bufs.len(), max_iov()) as c_int,
144 pub fn is_write_vectored(&self) -> bool {
148 pub fn write_at(&self, buf: &[u8], offset: u64) -> io::Result<usize> {
149 #[cfg(target_os = "android")]
150 use super::android::cvt_pwrite64;
152 #[cfg(not(target_os = "android"))]
153 unsafe fn cvt_pwrite64(
158 ) -> io::Result<isize> {
159 #[cfg(not(target_os = "linux"))]
160 use libc::pwrite as pwrite64;
161 #[cfg(target_os = "linux")]
163 cvt(pwrite64(fd, buf, count, offset))
169 buf.as_ptr() as *const c_void,
170 cmp::min(buf.len(), READ_LIMIT),
177 #[cfg(target_os = "linux")]
178 pub fn get_cloexec(&self) -> io::Result<bool> {
179 unsafe { Ok((cvt(libc::fcntl(self.fd, libc::F_GETFD))? & libc::FD_CLOEXEC) != 0) }
183 target_env = "newlib",
184 target_os = "solaris",
185 target_os = "illumos",
186 target_os = "emscripten",
187 target_os = "fuchsia",
193 pub fn set_cloexec(&self) -> io::Result<()> {
195 cvt(libc::ioctl(self.fd, libc::FIOCLEX))?;
200 target_env = "newlib",
201 target_os = "solaris",
202 target_os = "illumos",
203 target_os = "emscripten",
204 target_os = "fuchsia",
210 pub fn set_cloexec(&self) -> io::Result<()> {
212 let previous = cvt(libc::fcntl(self.fd, libc::F_GETFD))?;
213 let new = previous | libc::FD_CLOEXEC;
215 cvt(libc::fcntl(self.fd, libc::F_SETFD, new))?;
221 #[cfg(target_os = "linux")]
222 pub fn set_nonblocking(&self, nonblocking: bool) -> io::Result<()> {
224 let v = nonblocking as c_int;
225 cvt(libc::ioctl(self.fd, libc::FIONBIO, &v))?;
230 #[cfg(not(target_os = "linux"))]
231 pub fn set_nonblocking(&self, nonblocking: bool) -> io::Result<()> {
233 let previous = cvt(libc::fcntl(self.fd, libc::F_GETFL))?;
234 let new = if nonblocking {
235 previous | libc::O_NONBLOCK
237 previous & !libc::O_NONBLOCK
240 cvt(libc::fcntl(self.fd, libc::F_SETFL, new))?;
246 pub fn duplicate(&self) -> io::Result<FileDesc> {
247 // We want to atomically duplicate this file descriptor and set the
248 // CLOEXEC flag, and currently that's done via F_DUPFD_CLOEXEC. This
249 // is a POSIX flag that was added to Linux in 2.6.24.
250 let fd = cvt(unsafe { libc::fcntl(self.raw(), libc::F_DUPFD_CLOEXEC, 0) })?;
251 Ok(FileDesc::new(fd))
255 impl<'a> Read for &'a FileDesc {
256 fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
261 unsafe fn initializer(&self) -> Initializer {
266 impl AsInner<c_int> for FileDesc {
267 fn as_inner(&self) -> &c_int {
272 impl Drop for FileDesc {
274 // Note that errors are ignored when closing a file descriptor. The
275 // reason for this is that if an error occurs we don't actually know if
276 // the file descriptor was closed or not, and if we retried (for
277 // something like EINTR), we might close another valid file descriptor
278 // opened after we closed ours.
279 let _ = unsafe { libc::close(self.fd) };
285 use super::{FileDesc, IoSlice};
286 use core::mem::ManuallyDrop;
289 fn limit_vector_count() {
290 let stdout = ManuallyDrop::new(FileDesc { fd: 1 });
291 let bufs = (0..1500).map(|_| IoSlice::new(&[])).collect::<Vec<_>>();
292 assert!(stdout.write_vectored(&bufs).is_ok());