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 #[cfg(any(target_os = "linux", target_os = "macos"))]
31 fn max_iov() -> usize {
32 static LIM: AtomicUsize = AtomicUsize::new(0);
34 let mut lim = LIM.load(Ordering::Relaxed);
38 #[cfg(target_os = "linux")]
40 #[cfg(target_os = "macos")]
45 // 1024 is the default value on modern Linux systems
46 // and hopefully more useful than `c_int::MAX`.
47 lim = if ret > 0 { ret as usize } else { 1024 };
48 LIM.store(lim, Ordering::Relaxed);
54 #[cfg(not(any(target_os = "linux", target_os = "macos")))]
55 fn max_iov() -> usize {
60 pub fn new(fd: c_int) -> FileDesc {
64 pub fn raw(&self) -> c_int {
68 /// Extracts the actual file descriptor without closing it.
69 pub fn into_raw(self) -> c_int {
75 pub fn read(&self, buf: &mut [u8]) -> io::Result<usize> {
76 let ret = cvt(unsafe {
77 libc::read(self.fd, buf.as_mut_ptr() as *mut c_void, cmp::min(buf.len(), READ_LIMIT))
82 pub fn read_vectored(&self, bufs: &mut [IoSliceMut<'_>]) -> io::Result<usize> {
83 let ret = cvt(unsafe {
86 bufs.as_ptr() as *const libc::iovec,
87 cmp::min(bufs.len(), max_iov()) as c_int,
94 pub fn is_read_vectored(&self) -> bool {
98 pub fn read_to_end(&self, buf: &mut Vec<u8>) -> io::Result<usize> {
100 (&mut me).read_to_end(buf)
103 pub fn read_at(&self, buf: &mut [u8], offset: u64) -> io::Result<usize> {
104 #[cfg(target_os = "android")]
105 use super::android::cvt_pread64;
107 #[cfg(not(target_os = "android"))]
108 unsafe fn cvt_pread64(
113 ) -> io::Result<isize> {
114 #[cfg(not(target_os = "linux"))]
115 use libc::pread as pread64;
116 #[cfg(target_os = "linux")]
118 cvt(pread64(fd, buf, count, offset))
124 buf.as_mut_ptr() as *mut c_void,
125 cmp::min(buf.len(), READ_LIMIT),
132 pub fn write(&self, buf: &[u8]) -> io::Result<usize> {
133 let ret = cvt(unsafe {
134 libc::write(self.fd, buf.as_ptr() as *const c_void, cmp::min(buf.len(), READ_LIMIT))
139 pub fn write_vectored(&self, bufs: &[IoSlice<'_>]) -> io::Result<usize> {
140 let ret = cvt(unsafe {
143 bufs.as_ptr() as *const libc::iovec,
144 cmp::min(bufs.len(), max_iov()) as c_int,
151 pub fn is_write_vectored(&self) -> bool {
155 pub fn write_at(&self, buf: &[u8], offset: u64) -> io::Result<usize> {
156 #[cfg(target_os = "android")]
157 use super::android::cvt_pwrite64;
159 #[cfg(not(target_os = "android"))]
160 unsafe fn cvt_pwrite64(
165 ) -> io::Result<isize> {
166 #[cfg(not(target_os = "linux"))]
167 use libc::pwrite as pwrite64;
168 #[cfg(target_os = "linux")]
170 cvt(pwrite64(fd, buf, count, offset))
176 buf.as_ptr() as *const c_void,
177 cmp::min(buf.len(), READ_LIMIT),
184 #[cfg(target_os = "linux")]
185 pub fn get_cloexec(&self) -> io::Result<bool> {
186 unsafe { Ok((cvt(libc::fcntl(self.fd, libc::F_GETFD))? & libc::FD_CLOEXEC) != 0) }
190 target_env = "newlib",
191 target_os = "solaris",
192 target_os = "illumos",
193 target_os = "emscripten",
194 target_os = "fuchsia",
200 pub fn set_cloexec(&self) -> io::Result<()> {
202 cvt(libc::ioctl(self.fd, libc::FIOCLEX))?;
207 target_env = "newlib",
208 target_os = "solaris",
209 target_os = "illumos",
210 target_os = "emscripten",
211 target_os = "fuchsia",
217 pub fn set_cloexec(&self) -> io::Result<()> {
219 let previous = cvt(libc::fcntl(self.fd, libc::F_GETFD))?;
220 let new = previous | libc::FD_CLOEXEC;
222 cvt(libc::fcntl(self.fd, libc::F_SETFD, new))?;
228 #[cfg(target_os = "linux")]
229 pub fn set_nonblocking(&self, nonblocking: bool) -> io::Result<()> {
231 let v = nonblocking as c_int;
232 cvt(libc::ioctl(self.fd, libc::FIONBIO, &v))?;
237 #[cfg(not(target_os = "linux"))]
238 pub fn set_nonblocking(&self, nonblocking: bool) -> io::Result<()> {
240 let previous = cvt(libc::fcntl(self.fd, libc::F_GETFL))?;
241 let new = if nonblocking {
242 previous | libc::O_NONBLOCK
244 previous & !libc::O_NONBLOCK
247 cvt(libc::fcntl(self.fd, libc::F_SETFL, new))?;
253 pub fn duplicate(&self) -> io::Result<FileDesc> {
254 // We want to atomically duplicate this file descriptor and set the
255 // CLOEXEC flag, and currently that's done via F_DUPFD_CLOEXEC. This
256 // is a POSIX flag that was added to Linux in 2.6.24.
257 let fd = cvt(unsafe { libc::fcntl(self.raw(), libc::F_DUPFD_CLOEXEC, 0) })?;
258 Ok(FileDesc::new(fd))
262 impl<'a> Read for &'a FileDesc {
263 fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
268 unsafe fn initializer(&self) -> Initializer {
273 impl AsInner<c_int> for FileDesc {
274 fn as_inner(&self) -> &c_int {
279 impl Drop for FileDesc {
281 // Note that errors are ignored when closing a file descriptor. The
282 // reason for this is that if an error occurs we don't actually know if
283 // the file descriptor was closed or not, and if we retried (for
284 // something like EINTR), we might close another valid file descriptor
285 // opened after we closed ours.
286 let _ = unsafe { libc::close(self.fd) };
292 use super::{FileDesc, IoSlice};
295 fn limit_vector_count() {
296 let stdout = FileDesc { fd: 1 };
297 let bufs = (0..1500).map(|_| IoSlice::new(&[])).collect::<Vec<_>>();
299 assert!(stdout.write_vectored(&bufs).is_ok());