2019-12-21 11:16:18 +00:00
|
|
|
#![unstable(reason = "not public", issue = "none", feature = "fd")]
|
2016-02-12 18:29:25 +00:00
|
|
|
|
2020-08-27 13:45:01 +00:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests;
|
|
|
|
|
2019-02-10 19:23:21 +00:00
|
|
|
use crate::cmp;
|
2019-11-27 18:28:39 +00:00
|
|
|
use crate::io::{self, Initializer, IoSlice, IoSliceMut, Read};
|
2019-02-10 19:23:21 +00:00
|
|
|
use crate::mem;
|
|
|
|
use crate::sys::cvt;
|
|
|
|
use crate::sys_common::AsInner;
|
|
|
|
|
2020-07-23 02:49:40 +00:00
|
|
|
use libc::{c_int, c_void};
|
2015-02-03 05:39:14 +00:00
|
|
|
|
2016-11-25 18:21:49 +00:00
|
|
|
#[derive(Debug)]
|
2015-02-03 05:39:14 +00:00
|
|
|
pub struct FileDesc {
|
|
|
|
fd: c_int,
|
|
|
|
}
|
|
|
|
|
2020-07-23 02:49:40 +00:00
|
|
|
// The maximum read limit on most POSIX-like systems is `SSIZE_MAX`,
|
|
|
|
// with the man page quoting that if the count of bytes to read is
|
|
|
|
// greater than `SSIZE_MAX` the result is "unspecified".
|
|
|
|
//
|
|
|
|
// On macOS, however, apparently the 64-bit libc is either buggy or
|
|
|
|
// intentionally showing odd behavior by rejecting any read with a size
|
|
|
|
// larger than or equal to INT_MAX. To handle both of these the read
|
|
|
|
// size is capped on both platforms.
|
|
|
|
#[cfg(target_os = "macos")]
|
|
|
|
const READ_LIMIT: usize = c_int::MAX as usize - 1;
|
|
|
|
#[cfg(not(target_os = "macos"))]
|
|
|
|
const READ_LIMIT: usize = libc::ssize_t::MAX as usize;
|
2016-12-26 05:57:32 +00:00
|
|
|
|
2020-09-10 09:35:25 +00:00
|
|
|
#[cfg(any(
|
|
|
|
target_os = "dragonfly",
|
|
|
|
target_os = "freebsd",
|
|
|
|
target_os = "ios",
|
|
|
|
target_os = "macos",
|
|
|
|
target_os = "netbsd",
|
|
|
|
target_os = "openbsd",
|
|
|
|
))]
|
|
|
|
const fn max_iov() -> usize {
|
|
|
|
libc::IOV_MAX as usize
|
|
|
|
}
|
2020-08-01 13:38:08 +00:00
|
|
|
|
2020-09-10 09:35:25 +00:00
|
|
|
#[cfg(any(target_os = "android", target_os = "emscripten", target_os = "linux"))]
|
|
|
|
const fn max_iov() -> usize {
|
|
|
|
libc::UIO_MAXIOV as usize
|
2020-08-01 12:18:11 +00:00
|
|
|
}
|
|
|
|
|
2020-09-10 09:35:25 +00:00
|
|
|
#[cfg(not(any(
|
|
|
|
target_os = "android",
|
|
|
|
target_os = "dragonfly",
|
|
|
|
target_os = "emscripten",
|
|
|
|
target_os = "freebsd",
|
|
|
|
target_os = "ios",
|
|
|
|
target_os = "linux",
|
|
|
|
target_os = "macos",
|
|
|
|
target_os = "netbsd",
|
|
|
|
target_os = "openbsd",
|
|
|
|
)))]
|
|
|
|
const fn max_iov() -> usize {
|
2020-08-05 14:56:51 +00:00
|
|
|
16 // The minimum value required by POSIX.
|
|
|
|
}
|
|
|
|
|
2015-02-03 05:39:14 +00:00
|
|
|
impl FileDesc {
|
|
|
|
pub fn new(fd: c_int) -> FileDesc {
|
2018-11-06 20:05:44 +00:00
|
|
|
FileDesc { fd }
|
2015-02-03 05:39:14 +00:00
|
|
|
}
|
|
|
|
|
2019-11-27 18:28:39 +00:00
|
|
|
pub fn raw(&self) -> c_int {
|
|
|
|
self.fd
|
|
|
|
}
|
2015-02-03 05:39:14 +00:00
|
|
|
|
2018-11-12 18:05:20 +00:00
|
|
|
/// Extracts the actual file descriptor without closing it.
|
2015-02-03 05:39:14 +00:00
|
|
|
pub fn into_raw(self) -> c_int {
|
|
|
|
let fd = self.fd;
|
2015-05-07 17:49:39 +00:00
|
|
|
mem::forget(self);
|
2015-02-03 05:39:14 +00:00
|
|
|
fd
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn read(&self, buf: &mut [u8]) -> io::Result<usize> {
|
2016-03-23 03:01:37 +00:00
|
|
|
let ret = cvt(unsafe {
|
2020-07-23 02:49:40 +00:00
|
|
|
libc::read(self.fd, buf.as_mut_ptr() as *mut c_void, cmp::min(buf.len(), READ_LIMIT))
|
2016-03-23 03:01:37 +00:00
|
|
|
})?;
|
2015-02-03 05:39:14 +00:00
|
|
|
Ok(ret as usize)
|
|
|
|
}
|
|
|
|
|
2019-04-27 15:34:08 +00:00
|
|
|
pub fn read_vectored(&self, bufs: &mut [IoSliceMut<'_>]) -> io::Result<usize> {
|
2019-02-08 19:42:34 +00:00
|
|
|
let ret = cvt(unsafe {
|
2019-11-27 18:28:39 +00:00
|
|
|
libc::readv(
|
|
|
|
self.fd,
|
|
|
|
bufs.as_ptr() as *const libc::iovec,
|
2020-08-01 13:38:08 +00:00
|
|
|
cmp::min(bufs.len(), max_iov()) as c_int,
|
2019-11-27 18:28:39 +00:00
|
|
|
)
|
2019-02-08 19:42:34 +00:00
|
|
|
})?;
|
|
|
|
Ok(ret as usize)
|
|
|
|
}
|
|
|
|
|
2020-01-03 19:26:05 +00:00
|
|
|
#[inline]
|
2020-03-12 01:02:52 +00:00
|
|
|
pub fn is_read_vectored(&self) -> bool {
|
2020-01-03 19:26:05 +00:00
|
|
|
true
|
|
|
|
}
|
|
|
|
|
2016-02-12 08:17:24 +00:00
|
|
|
pub fn read_to_end(&self, buf: &mut Vec<u8>) -> io::Result<usize> {
|
|
|
|
let mut me = self;
|
|
|
|
(&mut me).read_to_end(buf)
|
|
|
|
}
|
|
|
|
|
2016-08-15 23:11:33 +00:00
|
|
|
pub fn read_at(&self, buf: &mut [u8], offset: u64) -> io::Result<usize> {
|
2016-10-08 23:06:55 +00:00
|
|
|
#[cfg(target_os = "android")]
|
|
|
|
use super::android::cvt_pread64;
|
|
|
|
|
2019-10-17 10:09:11 +00:00
|
|
|
#[cfg(not(target_os = "android"))]
|
2019-11-27 18:28:39 +00:00
|
|
|
unsafe fn cvt_pread64(
|
|
|
|
fd: c_int,
|
|
|
|
buf: *mut c_void,
|
|
|
|
count: usize,
|
|
|
|
offset: i64,
|
|
|
|
) -> io::Result<isize> {
|
2017-07-17 16:24:05 +00:00
|
|
|
#[cfg(not(target_os = "linux"))]
|
2016-10-08 23:06:55 +00:00
|
|
|
use libc::pread as pread64;
|
2019-11-27 18:28:39 +00:00
|
|
|
#[cfg(target_os = "linux")]
|
|
|
|
use libc::pread64;
|
2016-10-08 23:06:55 +00:00
|
|
|
cvt(pread64(fd, buf, count, offset))
|
|
|
|
}
|
|
|
|
|
2016-10-08 12:32:57 +00:00
|
|
|
unsafe {
|
2019-11-27 18:28:39 +00:00
|
|
|
cvt_pread64(
|
|
|
|
self.fd,
|
|
|
|
buf.as_mut_ptr() as *mut c_void,
|
2020-07-23 02:49:40 +00:00
|
|
|
cmp::min(buf.len(), READ_LIMIT),
|
2019-11-27 18:28:39 +00:00
|
|
|
offset as i64,
|
|
|
|
)
|
|
|
|
.map(|n| n as usize)
|
2016-10-08 12:32:57 +00:00
|
|
|
}
|
2016-08-15 23:11:33 +00:00
|
|
|
}
|
|
|
|
|
2015-02-03 05:39:14 +00:00
|
|
|
pub fn write(&self, buf: &[u8]) -> io::Result<usize> {
|
2016-03-23 03:01:37 +00:00
|
|
|
let ret = cvt(unsafe {
|
2020-07-23 02:49:40 +00:00
|
|
|
libc::write(self.fd, buf.as_ptr() as *const c_void, cmp::min(buf.len(), READ_LIMIT))
|
2016-03-23 03:01:37 +00:00
|
|
|
})?;
|
2015-02-03 05:39:14 +00:00
|
|
|
Ok(ret as usize)
|
|
|
|
}
|
2015-04-03 22:30:10 +00:00
|
|
|
|
2019-04-27 15:34:08 +00:00
|
|
|
pub fn write_vectored(&self, bufs: &[IoSlice<'_>]) -> io::Result<usize> {
|
2019-02-08 19:42:34 +00:00
|
|
|
let ret = cvt(unsafe {
|
2019-11-27 18:28:39 +00:00
|
|
|
libc::writev(
|
|
|
|
self.fd,
|
|
|
|
bufs.as_ptr() as *const libc::iovec,
|
2020-08-01 13:38:08 +00:00
|
|
|
cmp::min(bufs.len(), max_iov()) as c_int,
|
2019-11-27 18:28:39 +00:00
|
|
|
)
|
2019-02-08 19:42:34 +00:00
|
|
|
})?;
|
|
|
|
Ok(ret as usize)
|
|
|
|
}
|
|
|
|
|
2020-01-03 19:26:05 +00:00
|
|
|
#[inline]
|
2020-03-12 01:02:52 +00:00
|
|
|
pub fn is_write_vectored(&self) -> bool {
|
2020-01-03 19:26:05 +00:00
|
|
|
true
|
|
|
|
}
|
|
|
|
|
2016-08-15 23:11:33 +00:00
|
|
|
pub fn write_at(&self, buf: &[u8], offset: u64) -> io::Result<usize> {
|
2016-10-08 23:06:55 +00:00
|
|
|
#[cfg(target_os = "android")]
|
|
|
|
use super::android::cvt_pwrite64;
|
|
|
|
|
2019-10-17 10:09:11 +00:00
|
|
|
#[cfg(not(target_os = "android"))]
|
2019-11-27 18:28:39 +00:00
|
|
|
unsafe fn cvt_pwrite64(
|
|
|
|
fd: c_int,
|
|
|
|
buf: *const c_void,
|
|
|
|
count: usize,
|
|
|
|
offset: i64,
|
|
|
|
) -> io::Result<isize> {
|
2017-07-17 16:24:05 +00:00
|
|
|
#[cfg(not(target_os = "linux"))]
|
2016-10-08 23:06:55 +00:00
|
|
|
use libc::pwrite as pwrite64;
|
2019-11-27 18:28:39 +00:00
|
|
|
#[cfg(target_os = "linux")]
|
|
|
|
use libc::pwrite64;
|
2016-10-08 23:06:55 +00:00
|
|
|
cvt(pwrite64(fd, buf, count, offset))
|
|
|
|
}
|
|
|
|
|
2016-10-08 12:32:57 +00:00
|
|
|
unsafe {
|
2019-11-27 18:28:39 +00:00
|
|
|
cvt_pwrite64(
|
|
|
|
self.fd,
|
|
|
|
buf.as_ptr() as *const c_void,
|
2020-07-23 02:49:40 +00:00
|
|
|
cmp::min(buf.len(), READ_LIMIT),
|
2019-11-27 18:28:39 +00:00
|
|
|
offset as i64,
|
|
|
|
)
|
|
|
|
.map(|n| n as usize)
|
2016-10-08 12:32:57 +00:00
|
|
|
}
|
2016-08-15 23:11:33 +00:00
|
|
|
}
|
|
|
|
|
Don't unconditionally set CLOEXEC twice on every fd we open on Linux
Previously, every `open64` was accompanied by a `ioctl(…, FIOCLEX)`,
because some old Linux version would ignore the `O_CLOEXEC` flag we pass
to the `open64` function.
Now, we check whether the `CLOEXEC` flag is set on the first file we
open – if it is, we won't do extra syscalls for every opened file. If it
is not set, we fall back to the old behavior of unconditionally calling
`ioctl(…, FIOCLEX)` on newly opened files.
On old Linuxes, this amounts to one extra syscall per process, namely
the `fcntl(…, F_GETFD)` call to check the `CLOEXEC` flag.
On new Linuxes, this reduces the number of syscalls per opened file by
one, except for the first file, where it does the same number of
syscalls as before (`fcntl(…, F_GETFD)` to check the flag instead of
`ioctl(…, FIOCLEX)` to set it).
2018-05-14 11:20:39 +00:00
|
|
|
#[cfg(target_os = "linux")]
|
|
|
|
pub fn get_cloexec(&self) -> io::Result<bool> {
|
2019-11-27 18:28:39 +00:00
|
|
|
unsafe { Ok((cvt(libc::fcntl(self.fd, libc::F_GETFD))? & libc::FD_CLOEXEC) != 0) }
|
Don't unconditionally set CLOEXEC twice on every fd we open on Linux
Previously, every `open64` was accompanied by a `ioctl(…, FIOCLEX)`,
because some old Linux version would ignore the `O_CLOEXEC` flag we pass
to the `open64` function.
Now, we check whether the `CLOEXEC` flag is set on the first file we
open – if it is, we won't do extra syscalls for every opened file. If it
is not set, we fall back to the old behavior of unconditionally calling
`ioctl(…, FIOCLEX)` on newly opened files.
On old Linuxes, this amounts to one extra syscall per process, namely
the `fcntl(…, F_GETFD)` call to check the `CLOEXEC` flag.
On new Linuxes, this reduces the number of syscalls per opened file by
one, except for the first file, where it does the same number of
syscalls as before (`fcntl(…, F_GETFD)` to check the flag instead of
`ioctl(…, FIOCLEX)` to set it).
2018-05-14 11:20:39 +00:00
|
|
|
}
|
|
|
|
|
2019-11-27 18:28:39 +00:00
|
|
|
#[cfg(not(any(
|
|
|
|
target_env = "newlib",
|
|
|
|
target_os = "solaris",
|
2020-04-13 23:37:22 +00:00
|
|
|
target_os = "illumos",
|
2019-11-27 18:28:39 +00:00
|
|
|
target_os = "emscripten",
|
|
|
|
target_os = "fuchsia",
|
|
|
|
target_os = "l4re",
|
|
|
|
target_os = "linux",
|
|
|
|
target_os = "haiku",
|
2020-10-07 18:38:25 +00:00
|
|
|
target_os = "redox",
|
|
|
|
target_os = "vxworks"
|
2019-11-27 18:28:39 +00:00
|
|
|
)))]
|
2016-06-24 09:31:58 +00:00
|
|
|
pub fn set_cloexec(&self) -> io::Result<()> {
|
2015-04-03 22:30:10 +00:00
|
|
|
unsafe {
|
2016-06-24 09:31:58 +00:00
|
|
|
cvt(libc::ioctl(self.fd, libc::FIOCLEX))?;
|
|
|
|
Ok(())
|
2015-04-03 22:30:10 +00:00
|
|
|
}
|
|
|
|
}
|
2019-11-27 18:28:39 +00:00
|
|
|
#[cfg(any(
|
|
|
|
target_env = "newlib",
|
|
|
|
target_os = "solaris",
|
2020-04-13 23:37:22 +00:00
|
|
|
target_os = "illumos",
|
2019-11-27 18:28:39 +00:00
|
|
|
target_os = "emscripten",
|
|
|
|
target_os = "fuchsia",
|
|
|
|
target_os = "l4re",
|
|
|
|
target_os = "linux",
|
|
|
|
target_os = "haiku",
|
2020-10-07 18:38:25 +00:00
|
|
|
target_os = "redox",
|
|
|
|
target_os = "vxworks"
|
2019-11-27 18:28:39 +00:00
|
|
|
))]
|
2016-06-24 09:31:58 +00:00
|
|
|
pub fn set_cloexec(&self) -> io::Result<()> {
|
2015-10-25 01:51:34 +00:00
|
|
|
unsafe {
|
2016-06-24 09:31:58 +00:00
|
|
|
let previous = cvt(libc::fcntl(self.fd, libc::F_GETFD))?;
|
2017-02-04 00:10:12 +00:00
|
|
|
let new = previous | libc::FD_CLOEXEC;
|
|
|
|
if new != previous {
|
|
|
|
cvt(libc::fcntl(self.fd, libc::F_SETFD, new))?;
|
|
|
|
}
|
2016-06-24 09:31:58 +00:00
|
|
|
Ok(())
|
2015-10-25 01:51:34 +00:00
|
|
|
}
|
|
|
|
}
|
2016-01-21 05:24:23 +00:00
|
|
|
|
2017-02-04 00:10:12 +00:00
|
|
|
#[cfg(target_os = "linux")]
|
|
|
|
pub fn set_nonblocking(&self, nonblocking: bool) -> io::Result<()> {
|
|
|
|
unsafe {
|
|
|
|
let v = nonblocking as c_int;
|
|
|
|
cvt(libc::ioctl(self.fd, libc::FIONBIO, &v))?;
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(not(target_os = "linux"))]
|
2016-06-24 09:31:58 +00:00
|
|
|
pub fn set_nonblocking(&self, nonblocking: bool) -> io::Result<()> {
|
2016-02-12 18:29:25 +00:00
|
|
|
unsafe {
|
2016-06-24 09:31:58 +00:00
|
|
|
let previous = cvt(libc::fcntl(self.fd, libc::F_GETFL))?;
|
2016-02-12 18:29:25 +00:00
|
|
|
let new = if nonblocking {
|
|
|
|
previous | libc::O_NONBLOCK
|
|
|
|
} else {
|
|
|
|
previous & !libc::O_NONBLOCK
|
|
|
|
};
|
2017-02-04 00:10:12 +00:00
|
|
|
if new != previous {
|
|
|
|
cvt(libc::fcntl(self.fd, libc::F_SETFL, new))?;
|
|
|
|
}
|
2016-06-24 09:31:58 +00:00
|
|
|
Ok(())
|
2016-02-12 18:29:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-21 05:24:23 +00:00
|
|
|
pub fn duplicate(&self) -> io::Result<FileDesc> {
|
|
|
|
// We want to atomically duplicate this file descriptor and set the
|
|
|
|
// CLOEXEC flag, and currently that's done via F_DUPFD_CLOEXEC. This
|
2020-07-21 21:32:36 +00:00
|
|
|
// is a POSIX flag that was added to Linux in 2.6.24.
|
|
|
|
let fd = cvt(unsafe { libc::fcntl(self.raw(), libc::F_DUPFD_CLOEXEC, 0) })?;
|
|
|
|
Ok(FileDesc::new(fd))
|
2016-01-21 05:24:23 +00:00
|
|
|
}
|
2015-02-03 05:39:14 +00:00
|
|
|
}
|
|
|
|
|
2016-02-12 08:17:24 +00:00
|
|
|
impl<'a> Read for &'a FileDesc {
|
|
|
|
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
|
|
|
|
(**self).read(buf)
|
|
|
|
}
|
2018-09-06 03:09:58 +00:00
|
|
|
|
|
|
|
#[inline]
|
|
|
|
unsafe fn initializer(&self) -> Initializer {
|
|
|
|
Initializer::nop()
|
|
|
|
}
|
2016-02-12 08:17:24 +00:00
|
|
|
}
|
|
|
|
|
2015-02-06 00:50:11 +00:00
|
|
|
impl AsInner<c_int> for FileDesc {
|
2019-11-27 18:28:39 +00:00
|
|
|
fn as_inner(&self) -> &c_int {
|
|
|
|
&self.fd
|
|
|
|
}
|
2015-02-06 00:50:11 +00:00
|
|
|
}
|
|
|
|
|
2015-02-03 05:39:14 +00:00
|
|
|
impl Drop for FileDesc {
|
|
|
|
fn drop(&mut self) {
|
2015-04-03 22:44:14 +00:00
|
|
|
// Note that errors are ignored when closing a file descriptor. The
|
|
|
|
// reason for this is that if an error occurs we don't actually know if
|
|
|
|
// the file descriptor was closed or not, and if we retried (for
|
|
|
|
// something like EINTR), we might close another valid file descriptor
|
2018-08-28 09:06:40 +00:00
|
|
|
// opened after we closed ours.
|
2015-04-03 22:44:14 +00:00
|
|
|
let _ = unsafe { libc::close(self.fd) };
|
2015-02-03 05:39:14 +00:00
|
|
|
}
|
|
|
|
}
|