1 #![unstable(reason = "not public", issue = "none", feature = "fd")]
4 use crate::io::{self, Initializer, IoSlice, IoSliceMut, Read};
6 use crate::sync::atomic::{AtomicUsize, Ordering};
8 use crate::sys_common::AsInner;
10 use libc::{c_int, c_void};
17 // The maximum read limit on most POSIX-like systems is `SSIZE_MAX`,
18 // with the man page quoting that if the count of bytes to read is
19 // greater than `SSIZE_MAX` the result is "unspecified".
21 // On macOS, however, apparently the 64-bit libc is either buggy or
22 // intentionally showing odd behavior by rejecting any read with a size
23 // larger than or equal to INT_MAX. To handle both of these the read
24 // size is capped on both platforms.
25 #[cfg(target_os = "macos")]
26 const READ_LIMIT: usize = c_int::MAX as usize - 1;
27 #[cfg(not(target_os = "macos"))]
28 const READ_LIMIT: usize = libc::ssize_t::MAX as usize;
30 fn max_iov() -> usize {
31 static LIM: AtomicUsize = AtomicUsize::new(0);
33 let mut lim = LIM.load(Ordering::Relaxed);
35 let ret = unsafe { libc::sysconf(libc::_SC_IOV_MAX) };
37 // 16 is the minimum value required by POSIX.
38 lim = if ret > 0 { ret as usize } else { 16 };
39 LIM.store(lim, Ordering::Relaxed);
46 pub fn new(fd: c_int) -> FileDesc {
50 pub fn raw(&self) -> c_int {
54 /// Extracts the actual file descriptor without closing it.
55 pub fn into_raw(self) -> c_int {
61 pub fn read(&self, buf: &mut [u8]) -> io::Result<usize> {
62 let ret = cvt(unsafe {
63 libc::read(self.fd, buf.as_mut_ptr() as *mut c_void, cmp::min(buf.len(), READ_LIMIT))
68 pub fn read_vectored(&self, bufs: &mut [IoSliceMut<'_>]) -> io::Result<usize> {
69 let ret = cvt(unsafe {
72 bufs.as_ptr() as *const libc::iovec,
73 cmp::min(bufs.len(), max_iov()) as c_int,
80 pub fn is_read_vectored(&self) -> bool {
84 pub fn read_to_end(&self, buf: &mut Vec<u8>) -> io::Result<usize> {
86 (&mut me).read_to_end(buf)
89 pub fn read_at(&self, buf: &mut [u8], offset: u64) -> io::Result<usize> {
90 #[cfg(target_os = "android")]
91 use super::android::cvt_pread64;
93 #[cfg(not(target_os = "android"))]
94 unsafe fn cvt_pread64(
99 ) -> io::Result<isize> {
100 #[cfg(not(target_os = "linux"))]
101 use libc::pread as pread64;
102 #[cfg(target_os = "linux")]
104 cvt(pread64(fd, buf, count, offset))
110 buf.as_mut_ptr() as *mut c_void,
111 cmp::min(buf.len(), READ_LIMIT),
118 pub fn write(&self, buf: &[u8]) -> io::Result<usize> {
119 let ret = cvt(unsafe {
120 libc::write(self.fd, buf.as_ptr() as *const c_void, cmp::min(buf.len(), READ_LIMIT))
125 pub fn write_vectored(&self, bufs: &[IoSlice<'_>]) -> io::Result<usize> {
126 let ret = cvt(unsafe {
129 bufs.as_ptr() as *const libc::iovec,
130 cmp::min(bufs.len(), max_iov()) as c_int,
137 pub fn is_write_vectored(&self) -> bool {
141 pub fn write_at(&self, buf: &[u8], offset: u64) -> io::Result<usize> {
142 #[cfg(target_os = "android")]
143 use super::android::cvt_pwrite64;
145 #[cfg(not(target_os = "android"))]
146 unsafe fn cvt_pwrite64(
151 ) -> io::Result<isize> {
152 #[cfg(not(target_os = "linux"))]
153 use libc::pwrite as pwrite64;
154 #[cfg(target_os = "linux")]
156 cvt(pwrite64(fd, buf, count, offset))
162 buf.as_ptr() as *const c_void,
163 cmp::min(buf.len(), READ_LIMIT),
170 #[cfg(target_os = "linux")]
171 pub fn get_cloexec(&self) -> io::Result<bool> {
172 unsafe { Ok((cvt(libc::fcntl(self.fd, libc::F_GETFD))? & libc::FD_CLOEXEC) != 0) }
176 target_env = "newlib",
177 target_os = "solaris",
178 target_os = "illumos",
179 target_os = "emscripten",
180 target_os = "fuchsia",
186 pub fn set_cloexec(&self) -> io::Result<()> {
188 cvt(libc::ioctl(self.fd, libc::FIOCLEX))?;
193 target_env = "newlib",
194 target_os = "solaris",
195 target_os = "illumos",
196 target_os = "emscripten",
197 target_os = "fuchsia",
203 pub fn set_cloexec(&self) -> io::Result<()> {
205 let previous = cvt(libc::fcntl(self.fd, libc::F_GETFD))?;
206 let new = previous | libc::FD_CLOEXEC;
208 cvt(libc::fcntl(self.fd, libc::F_SETFD, new))?;
214 #[cfg(target_os = "linux")]
215 pub fn set_nonblocking(&self, nonblocking: bool) -> io::Result<()> {
217 let v = nonblocking as c_int;
218 cvt(libc::ioctl(self.fd, libc::FIONBIO, &v))?;
223 #[cfg(not(target_os = "linux"))]
224 pub fn set_nonblocking(&self, nonblocking: bool) -> io::Result<()> {
226 let previous = cvt(libc::fcntl(self.fd, libc::F_GETFL))?;
227 let new = if nonblocking {
228 previous | libc::O_NONBLOCK
230 previous & !libc::O_NONBLOCK
233 cvt(libc::fcntl(self.fd, libc::F_SETFL, new))?;
239 pub fn duplicate(&self) -> io::Result<FileDesc> {
240 // We want to atomically duplicate this file descriptor and set the
241 // CLOEXEC flag, and currently that's done via F_DUPFD_CLOEXEC. This
242 // is a POSIX flag that was added to Linux in 2.6.24.
243 let fd = cvt(unsafe { libc::fcntl(self.raw(), libc::F_DUPFD_CLOEXEC, 0) })?;
244 Ok(FileDesc::new(fd))
248 impl<'a> Read for &'a FileDesc {
249 fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
254 unsafe fn initializer(&self) -> Initializer {
259 impl AsInner<c_int> for FileDesc {
260 fn as_inner(&self) -> &c_int {
265 impl Drop for FileDesc {
267 // Note that errors are ignored when closing a file descriptor. The
268 // reason for this is that if an error occurs we don't actually know if
269 // the file descriptor was closed or not, and if we retried (for
270 // something like EINTR), we might close another valid file descriptor
271 // opened after we closed ours.
272 let _ = unsafe { libc::close(self.fd) };
278 use super::{FileDesc, IoSlice};
281 fn limit_vector_count() {
282 let stdout = FileDesc { fd: 1 };
283 let bufs = (0..1500).map(|_| IoSlice::new(&[])).collect::<Vec<_>>();
285 assert!(stdout.write_vectored(&bufs).is_ok());