1 #![unstable(reason = "not public", issue = "none", feature = "fd")]
4 use crate::io::{self, Initializer, IoSlice, IoSliceMut, Read};
7 use crate::sys_common::AsInner;
9 use libc::{c_int, c_void};
16 // The maximum read limit on most POSIX-like systems is `SSIZE_MAX`,
17 // with the man page quoting that if the count of bytes to read is
18 // greater than `SSIZE_MAX` the result is "unspecified".
20 // On macOS, however, apparently the 64-bit libc is either buggy or
21 // intentionally showing odd behavior by rejecting any read with a size
22 // larger than or equal to INT_MAX. To handle both of these the read
23 // size is capped on both platforms.
24 #[cfg(target_os = "macos")]
25 const READ_LIMIT: usize = c_int::MAX as usize - 1;
26 #[cfg(not(target_os = "macos"))]
27 const READ_LIMIT: usize = libc::ssize_t::MAX as usize;
29 #[cfg(any(target_os = "linux", target_os = "macos"))]
30 fn max_iov() -> c_int {
33 #[cfg(target_os = "linux")]
35 #[cfg(target_os = "macos")]
40 // 1024 is the default value on modern Linux systems
41 // and hopefully more useful than `c_int::MAX`.
42 if ret > 0 { ret as c_int } else { 1024 }
45 #[cfg(not(any(target_os = "linux", target_os = "macos")))]
46 fn max_iov() -> c_int {
51 pub fn new(fd: c_int) -> FileDesc {
55 pub fn raw(&self) -> c_int {
59 /// Extracts the actual file descriptor without closing it.
60 pub fn into_raw(self) -> c_int {
66 pub fn read(&self, buf: &mut [u8]) -> io::Result<usize> {
67 let ret = cvt(unsafe {
68 libc::read(self.fd, buf.as_mut_ptr() as *mut c_void, cmp::min(buf.len(), READ_LIMIT))
73 pub fn read_vectored(&self, bufs: &mut [IoSliceMut<'_>]) -> io::Result<usize> {
74 let ret = cvt(unsafe {
77 bufs.as_ptr() as *const libc::iovec,
78 cmp::min(bufs.len(), max_iov() as usize) as c_int,
85 pub fn is_read_vectored(&self) -> bool {
89 pub fn read_to_end(&self, buf: &mut Vec<u8>) -> io::Result<usize> {
91 (&mut me).read_to_end(buf)
94 pub fn read_at(&self, buf: &mut [u8], offset: u64) -> io::Result<usize> {
95 #[cfg(target_os = "android")]
96 use super::android::cvt_pread64;
98 #[cfg(not(target_os = "android"))]
99 unsafe fn cvt_pread64(
104 ) -> io::Result<isize> {
105 #[cfg(not(target_os = "linux"))]
106 use libc::pread as pread64;
107 #[cfg(target_os = "linux")]
109 cvt(pread64(fd, buf, count, offset))
115 buf.as_mut_ptr() as *mut c_void,
116 cmp::min(buf.len(), READ_LIMIT),
123 pub fn write(&self, buf: &[u8]) -> io::Result<usize> {
124 let ret = cvt(unsafe {
125 libc::write(self.fd, buf.as_ptr() as *const c_void, cmp::min(buf.len(), READ_LIMIT))
130 pub fn write_vectored(&self, bufs: &[IoSlice<'_>]) -> io::Result<usize> {
131 let ret = cvt(unsafe {
134 bufs.as_ptr() as *const libc::iovec,
135 cmp::min(bufs.len(), max_iov() as usize) as c_int,
142 pub fn is_write_vectored(&self) -> bool {
146 pub fn write_at(&self, buf: &[u8], offset: u64) -> io::Result<usize> {
147 #[cfg(target_os = "android")]
148 use super::android::cvt_pwrite64;
150 #[cfg(not(target_os = "android"))]
151 unsafe fn cvt_pwrite64(
156 ) -> io::Result<isize> {
157 #[cfg(not(target_os = "linux"))]
158 use libc::pwrite as pwrite64;
159 #[cfg(target_os = "linux")]
161 cvt(pwrite64(fd, buf, count, offset))
167 buf.as_ptr() as *const c_void,
168 cmp::min(buf.len(), READ_LIMIT),
175 #[cfg(target_os = "linux")]
176 pub fn get_cloexec(&self) -> io::Result<bool> {
177 unsafe { Ok((cvt(libc::fcntl(self.fd, libc::F_GETFD))? & libc::FD_CLOEXEC) != 0) }
181 target_env = "newlib",
182 target_os = "solaris",
183 target_os = "illumos",
184 target_os = "emscripten",
185 target_os = "fuchsia",
191 pub fn set_cloexec(&self) -> io::Result<()> {
193 cvt(libc::ioctl(self.fd, libc::FIOCLEX))?;
198 target_env = "newlib",
199 target_os = "solaris",
200 target_os = "illumos",
201 target_os = "emscripten",
202 target_os = "fuchsia",
208 pub fn set_cloexec(&self) -> io::Result<()> {
210 let previous = cvt(libc::fcntl(self.fd, libc::F_GETFD))?;
211 let new = previous | libc::FD_CLOEXEC;
213 cvt(libc::fcntl(self.fd, libc::F_SETFD, new))?;
219 #[cfg(target_os = "linux")]
220 pub fn set_nonblocking(&self, nonblocking: bool) -> io::Result<()> {
222 let v = nonblocking as c_int;
223 cvt(libc::ioctl(self.fd, libc::FIONBIO, &v))?;
228 #[cfg(not(target_os = "linux"))]
229 pub fn set_nonblocking(&self, nonblocking: bool) -> io::Result<()> {
231 let previous = cvt(libc::fcntl(self.fd, libc::F_GETFL))?;
232 let new = if nonblocking {
233 previous | libc::O_NONBLOCK
235 previous & !libc::O_NONBLOCK
238 cvt(libc::fcntl(self.fd, libc::F_SETFL, new))?;
244 pub fn duplicate(&self) -> io::Result<FileDesc> {
245 // We want to atomically duplicate this file descriptor and set the
246 // CLOEXEC flag, and currently that's done via F_DUPFD_CLOEXEC. This
247 // is a POSIX flag that was added to Linux in 2.6.24.
248 let fd = cvt(unsafe { libc::fcntl(self.raw(), libc::F_DUPFD_CLOEXEC, 0) })?;
249 Ok(FileDesc::new(fd))
253 impl<'a> Read for &'a FileDesc {
254 fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
259 unsafe fn initializer(&self) -> Initializer {
264 impl AsInner<c_int> for FileDesc {
265 fn as_inner(&self) -> &c_int {
270 impl Drop for FileDesc {
272 // Note that errors are ignored when closing a file descriptor. The
273 // reason for this is that if an error occurs we don't actually know if
274 // the file descriptor was closed or not, and if we retried (for
275 // something like EINTR), we might close another valid file descriptor
276 // opened after we closed ours.
277 let _ = unsafe { libc::close(self.fd) };
283 use super::{FileDesc, IoSlice};
286 fn limit_vector_count() {
287 let stdout = FileDesc { fd: 1 };
288 let bufs = (0..1500).map(|_| IoSlice::new(&[])).collect::<Vec<_>>();
290 assert!(stdout.write_vectored(&bufs).is_ok());