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};
19 // The maximum read limit on most POSIX-like systems is `SSIZE_MAX`,
20 // with the man page quoting that if the count of bytes to read is
21 // greater than `SSIZE_MAX` the result is "unspecified".
23 // On macOS, however, apparently the 64-bit libc is either buggy or
24 // intentionally showing odd behavior by rejecting any read with a size
25 // larger than or equal to INT_MAX. To handle both of these the read
26 // size is capped on both platforms.
27 #[cfg(target_os = "macos")]
28 const READ_LIMIT: usize = c_int::MAX as usize - 1;
29 #[cfg(not(target_os = "macos"))]
30 const READ_LIMIT: usize = libc::ssize_t::MAX as usize;
33 target_os = "dragonfly",
34 target_os = "freebsd",
38 target_os = "openbsd",
40 const fn max_iov() -> usize {
41 libc::IOV_MAX as usize
44 #[cfg(any(target_os = "android", target_os = "emscripten", target_os = "linux"))]
45 const fn max_iov() -> usize {
46 libc::UIO_MAXIOV as usize
50 target_os = "android",
51 target_os = "dragonfly",
52 target_os = "emscripten",
53 target_os = "freebsd",
58 target_os = "openbsd",
60 const fn max_iov() -> usize {
61 16 // The minimum value required by POSIX.
65 pub fn new(fd: c_int) -> FileDesc {
69 pub fn raw(&self) -> c_int {
73 /// Extracts the actual file descriptor without closing it.
74 pub fn into_raw(self) -> c_int {
80 pub fn read(&self, buf: &mut [u8]) -> io::Result<usize> {
81 let ret = cvt(unsafe {
82 libc::read(self.fd, buf.as_mut_ptr() as *mut c_void, cmp::min(buf.len(), READ_LIMIT))
87 pub fn read_vectored(&self, bufs: &mut [IoSliceMut<'_>]) -> io::Result<usize> {
88 let ret = cvt(unsafe {
91 bufs.as_ptr() as *const libc::iovec,
92 cmp::min(bufs.len(), max_iov()) as c_int,
99 pub fn is_read_vectored(&self) -> bool {
103 pub fn read_to_end(&self, buf: &mut Vec<u8>) -> io::Result<usize> {
105 (&mut me).read_to_end(buf)
108 pub fn read_at(&self, buf: &mut [u8], offset: u64) -> io::Result<usize> {
109 #[cfg(target_os = "android")]
110 use super::android::cvt_pread64;
112 #[cfg(not(target_os = "android"))]
113 unsafe fn cvt_pread64(
118 ) -> io::Result<isize> {
119 #[cfg(not(target_os = "linux"))]
120 use libc::pread as pread64;
121 #[cfg(target_os = "linux")]
123 cvt(pread64(fd, buf, count, offset))
129 buf.as_mut_ptr() as *mut c_void,
130 cmp::min(buf.len(), READ_LIMIT),
137 pub fn write(&self, buf: &[u8]) -> io::Result<usize> {
138 let ret = cvt(unsafe {
139 libc::write(self.fd, buf.as_ptr() as *const c_void, cmp::min(buf.len(), READ_LIMIT))
144 pub fn write_vectored(&self, bufs: &[IoSlice<'_>]) -> io::Result<usize> {
145 let ret = cvt(unsafe {
148 bufs.as_ptr() as *const libc::iovec,
149 cmp::min(bufs.len(), max_iov()) as c_int,
156 pub fn is_write_vectored(&self) -> bool {
160 pub fn write_at(&self, buf: &[u8], offset: u64) -> io::Result<usize> {
161 #[cfg(target_os = "android")]
162 use super::android::cvt_pwrite64;
164 #[cfg(not(target_os = "android"))]
165 unsafe fn cvt_pwrite64(
170 ) -> io::Result<isize> {
171 #[cfg(not(target_os = "linux"))]
172 use libc::pwrite as pwrite64;
173 #[cfg(target_os = "linux")]
175 cvt(pwrite64(fd, buf, count, offset))
181 buf.as_ptr() as *const c_void,
182 cmp::min(buf.len(), READ_LIMIT),
189 #[cfg(target_os = "linux")]
190 pub fn get_cloexec(&self) -> io::Result<bool> {
191 unsafe { Ok((cvt(libc::fcntl(self.fd, libc::F_GETFD))? & libc::FD_CLOEXEC) != 0) }
195 target_env = "newlib",
196 target_os = "solaris",
197 target_os = "illumos",
198 target_os = "emscripten",
199 target_os = "fuchsia",
204 target_os = "vxworks"
206 pub fn set_cloexec(&self) -> io::Result<()> {
208 cvt(libc::ioctl(self.fd, libc::FIOCLEX))?;
213 target_env = "newlib",
214 target_os = "solaris",
215 target_os = "illumos",
216 target_os = "emscripten",
217 target_os = "fuchsia",
222 target_os = "vxworks"
224 pub fn set_cloexec(&self) -> io::Result<()> {
226 let previous = cvt(libc::fcntl(self.fd, libc::F_GETFD))?;
227 let new = previous | libc::FD_CLOEXEC;
229 cvt(libc::fcntl(self.fd, libc::F_SETFD, new))?;
235 #[cfg(target_os = "linux")]
236 pub fn set_nonblocking(&self, nonblocking: bool) -> io::Result<()> {
238 let v = nonblocking as c_int;
239 cvt(libc::ioctl(self.fd, libc::FIONBIO, &v))?;
244 #[cfg(not(target_os = "linux"))]
245 pub fn set_nonblocking(&self, nonblocking: bool) -> io::Result<()> {
247 let previous = cvt(libc::fcntl(self.fd, libc::F_GETFL))?;
248 let new = if nonblocking {
249 previous | libc::O_NONBLOCK
251 previous & !libc::O_NONBLOCK
254 cvt(libc::fcntl(self.fd, libc::F_SETFL, new))?;
260 pub fn duplicate(&self) -> io::Result<FileDesc> {
261 // We want to atomically duplicate this file descriptor and set the
262 // CLOEXEC flag, and currently that's done via F_DUPFD_CLOEXEC. This
263 // is a POSIX flag that was added to Linux in 2.6.24.
264 let fd = cvt(unsafe { libc::fcntl(self.raw(), libc::F_DUPFD_CLOEXEC, 0) })?;
265 Ok(FileDesc::new(fd))
269 impl<'a> Read for &'a FileDesc {
270 fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
275 unsafe fn initializer(&self) -> Initializer {
280 impl AsInner<c_int> for FileDesc {
281 fn as_inner(&self) -> &c_int {
286 impl Drop for FileDesc {
288 // Note that errors are ignored when closing a file descriptor. The
289 // reason for this is that if an error occurs we don't actually know if
290 // the file descriptor was closed or not, and if we retried (for
291 // something like EINTR), we might close another valid file descriptor
292 // opened after we closed ours.
293 let _ = unsafe { libc::close(self.fd) };