2019-12-21 13:16:18 +02:00
|
|
|
#![unstable(reason = "not public", issue = "none", feature = "fd")]
|
2016-02-12 10:29:25 -08:00
|
|
|
|
2019-02-11 04:23:21 +09:00
|
|
|
use crate::cmp;
|
2019-11-27 10:28:39 -08:00
|
|
|
use crate::io::{self, Initializer, IoSlice, IoSliceMut, Read};
|
2019-02-11 04:23:21 +09:00
|
|
|
use crate::mem;
|
2020-08-05 16:56:51 +02:00
|
|
|
#[cfg(not(any(target_os = "redox", target_env = "newlib")))]
|
2020-08-01 15:38:08 +02:00
|
|
|
use crate::sync::atomic::{AtomicUsize, Ordering};
|
2019-02-11 04:23:21 +09:00
|
|
|
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-02 21:39:14 -08:00
|
|
|
|
2016-11-25 13:21:49 -05:00
|
|
|
#[derive(Debug)]
|
2015-02-02 21:39:14 -08: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-25 21:57:32 -08:00
|
|
|
|
2020-08-05 16:56:51 +02:00
|
|
|
#[cfg(not(any(target_os = "redox", target_env = "newlib")))]
|
2020-08-01 15:38:08 +02:00
|
|
|
fn max_iov() -> usize {
|
|
|
|
|
static LIM: AtomicUsize = AtomicUsize::new(0);
|
2020-08-01 14:18:11 +02:00
|
|
|
|
2020-08-01 15:38:08 +02:00
|
|
|
let mut lim = LIM.load(Ordering::Relaxed);
|
|
|
|
|
if lim == 0 {
|
2020-08-01 16:06:00 +02:00
|
|
|
let ret = unsafe { libc::sysconf(libc::_SC_IOV_MAX) };
|
2020-08-01 14:29:42 +02:00
|
|
|
|
2020-08-01 16:06:00 +02:00
|
|
|
// 16 is the minimum value required by POSIX.
|
|
|
|
|
lim = if ret > 0 { ret as usize } else { 16 };
|
2020-08-01 15:38:08 +02:00
|
|
|
LIM.store(lim, Ordering::Relaxed);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
lim
|
2020-08-01 14:18:11 +02:00
|
|
|
}
|
|
|
|
|
|
2020-08-05 16:56:51 +02:00
|
|
|
#[cfg(any(target_os = "redox", target_env = "newlib"))]
|
|
|
|
|
fn max_iov() -> usize {
|
|
|
|
|
16 // The minimum value required by POSIX.
|
|
|
|
|
}
|
|
|
|
|
|
2015-02-02 21:39:14 -08:00
|
|
|
impl FileDesc {
|
|
|
|
|
pub fn new(fd: c_int) -> FileDesc {
|
2018-11-06 15:05:44 -05:00
|
|
|
FileDesc { fd }
|
2015-02-02 21:39:14 -08:00
|
|
|
}
|
|
|
|
|
|
2019-11-27 10:28:39 -08:00
|
|
|
pub fn raw(&self) -> c_int {
|
|
|
|
|
self.fd
|
|
|
|
|
}
|
2015-02-02 21:39:14 -08:00
|
|
|
|
2018-11-12 13:05:20 -05:00
|
|
|
/// Extracts the actual file descriptor without closing it.
|
2015-02-02 21:39:14 -08:00
|
|
|
pub fn into_raw(self) -> c_int {
|
|
|
|
|
let fd = self.fd;
|
2015-05-07 10:49:39 -07:00
|
|
|
mem::forget(self);
|
2015-02-02 21:39:14 -08:00
|
|
|
fd
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn read(&self, buf: &mut [u8]) -> io::Result<usize> {
|
2016-03-22 22:01:37 -05: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-22 22:01:37 -05:00
|
|
|
})?;
|
2015-02-02 21:39:14 -08:00
|
|
|
Ok(ret as usize)
|
|
|
|
|
}
|
|
|
|
|
|
2019-04-27 08:34:08 -07:00
|
|
|
pub fn read_vectored(&self, bufs: &mut [IoSliceMut<'_>]) -> io::Result<usize> {
|
2019-02-08 20:42:34 +01:00
|
|
|
let ret = cvt(unsafe {
|
2019-11-27 10:28:39 -08:00
|
|
|
libc::readv(
|
|
|
|
|
self.fd,
|
|
|
|
|
bufs.as_ptr() as *const libc::iovec,
|
2020-08-01 15:38:08 +02:00
|
|
|
cmp::min(bufs.len(), max_iov()) as c_int,
|
2019-11-27 10:28:39 -08:00
|
|
|
)
|
2019-02-08 20:42:34 +01:00
|
|
|
})?;
|
|
|
|
|
Ok(ret as usize)
|
|
|
|
|
}
|
|
|
|
|
|
2020-01-03 11:26:05 -08:00
|
|
|
#[inline]
|
2020-03-11 18:02:52 -07:00
|
|
|
pub fn is_read_vectored(&self) -> bool {
|
2020-01-03 11:26:05 -08:00
|
|
|
true
|
|
|
|
|
}
|
|
|
|
|
|
2016-02-12 00:17:24 -08: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-16 01:11:33 +02:00
|
|
|
pub fn read_at(&self, buf: &mut [u8], offset: u64) -> io::Result<usize> {
|
2016-10-09 01:06:55 +02:00
|
|
|
#[cfg(target_os = "android")]
|
|
|
|
|
use super::android::cvt_pread64;
|
|
|
|
|
|
2019-10-17 12:09:11 +02:00
|
|
|
#[cfg(not(target_os = "android"))]
|
2019-11-27 10:28:39 -08:00
|
|
|
unsafe fn cvt_pread64(
|
|
|
|
|
fd: c_int,
|
|
|
|
|
buf: *mut c_void,
|
|
|
|
|
count: usize,
|
|
|
|
|
offset: i64,
|
|
|
|
|
) -> io::Result<isize> {
|
2017-07-17 09:24:05 -07:00
|
|
|
#[cfg(not(target_os = "linux"))]
|
2016-10-09 01:06:55 +02:00
|
|
|
use libc::pread as pread64;
|
2019-11-27 10:28:39 -08:00
|
|
|
#[cfg(target_os = "linux")]
|
|
|
|
|
use libc::pread64;
|
2016-10-09 01:06:55 +02:00
|
|
|
cvt(pread64(fd, buf, count, offset))
|
|
|
|
|
}
|
|
|
|
|
|
2016-10-08 14:32:57 +02:00
|
|
|
unsafe {
|
2019-11-27 10:28:39 -08: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 10:28:39 -08:00
|
|
|
offset as i64,
|
|
|
|
|
)
|
|
|
|
|
.map(|n| n as usize)
|
2016-10-08 14:32:57 +02:00
|
|
|
}
|
2016-08-16 01:11:33 +02:00
|
|
|
}
|
|
|
|
|
|
2015-02-02 21:39:14 -08:00
|
|
|
pub fn write(&self, buf: &[u8]) -> io::Result<usize> {
|
2016-03-22 22:01:37 -05: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-22 22:01:37 -05:00
|
|
|
})?;
|
2015-02-02 21:39:14 -08:00
|
|
|
Ok(ret as usize)
|
|
|
|
|
}
|
2015-04-03 15:30:10 -07:00
|
|
|
|
2019-04-27 08:34:08 -07:00
|
|
|
pub fn write_vectored(&self, bufs: &[IoSlice<'_>]) -> io::Result<usize> {
|
2019-02-08 20:42:34 +01:00
|
|
|
let ret = cvt(unsafe {
|
2019-11-27 10:28:39 -08:00
|
|
|
libc::writev(
|
|
|
|
|
self.fd,
|
|
|
|
|
bufs.as_ptr() as *const libc::iovec,
|
2020-08-01 15:38:08 +02:00
|
|
|
cmp::min(bufs.len(), max_iov()) as c_int,
|
2019-11-27 10:28:39 -08:00
|
|
|
)
|
2019-02-08 20:42:34 +01:00
|
|
|
})?;
|
|
|
|
|
Ok(ret as usize)
|
|
|
|
|
}
|
|
|
|
|
|
2020-01-03 11:26:05 -08:00
|
|
|
#[inline]
|
2020-03-11 18:02:52 -07:00
|
|
|
pub fn is_write_vectored(&self) -> bool {
|
2020-01-03 11:26:05 -08:00
|
|
|
true
|
|
|
|
|
}
|
|
|
|
|
|
2016-08-16 01:11:33 +02:00
|
|
|
pub fn write_at(&self, buf: &[u8], offset: u64) -> io::Result<usize> {
|
2016-10-09 01:06:55 +02:00
|
|
|
#[cfg(target_os = "android")]
|
|
|
|
|
use super::android::cvt_pwrite64;
|
|
|
|
|
|
2019-10-17 12:09:11 +02:00
|
|
|
#[cfg(not(target_os = "android"))]
|
2019-11-27 10:28:39 -08:00
|
|
|
unsafe fn cvt_pwrite64(
|
|
|
|
|
fd: c_int,
|
|
|
|
|
buf: *const c_void,
|
|
|
|
|
count: usize,
|
|
|
|
|
offset: i64,
|
|
|
|
|
) -> io::Result<isize> {
|
2017-07-17 09:24:05 -07:00
|
|
|
#[cfg(not(target_os = "linux"))]
|
2016-10-09 01:06:55 +02:00
|
|
|
use libc::pwrite as pwrite64;
|
2019-11-27 10:28:39 -08:00
|
|
|
#[cfg(target_os = "linux")]
|
|
|
|
|
use libc::pwrite64;
|
2016-10-09 01:06:55 +02:00
|
|
|
cvt(pwrite64(fd, buf, count, offset))
|
|
|
|
|
}
|
|
|
|
|
|
2016-10-08 14:32:57 +02:00
|
|
|
unsafe {
|
2019-11-27 10:28:39 -08: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 10:28:39 -08:00
|
|
|
offset as i64,
|
|
|
|
|
)
|
|
|
|
|
.map(|n| n as usize)
|
2016-10-08 14:32:57 +02:00
|
|
|
}
|
2016-08-16 01:11:33 +02: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 13:20:39 +02:00
|
|
|
#[cfg(target_os = "linux")]
|
|
|
|
|
pub fn get_cloexec(&self) -> io::Result<bool> {
|
2019-11-27 10:28:39 -08: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 13:20:39 +02:00
|
|
|
}
|
|
|
|
|
|
2019-11-27 10:28:39 -08:00
|
|
|
#[cfg(not(any(
|
|
|
|
|
target_env = "newlib",
|
|
|
|
|
target_os = "solaris",
|
2020-04-13 23:37:22 +00:00
|
|
|
target_os = "illumos",
|
2019-11-27 10:28:39 -08:00
|
|
|
target_os = "emscripten",
|
|
|
|
|
target_os = "fuchsia",
|
|
|
|
|
target_os = "l4re",
|
|
|
|
|
target_os = "linux",
|
|
|
|
|
target_os = "haiku",
|
|
|
|
|
target_os = "redox"
|
|
|
|
|
)))]
|
2016-06-24 11:31:58 +02:00
|
|
|
pub fn set_cloexec(&self) -> io::Result<()> {
|
2015-04-03 15:30:10 -07:00
|
|
|
unsafe {
|
2016-06-24 11:31:58 +02:00
|
|
|
cvt(libc::ioctl(self.fd, libc::FIOCLEX))?;
|
|
|
|
|
Ok(())
|
2015-04-03 15:30:10 -07:00
|
|
|
}
|
|
|
|
|
}
|
2019-11-27 10:28:39 -08:00
|
|
|
#[cfg(any(
|
|
|
|
|
target_env = "newlib",
|
|
|
|
|
target_os = "solaris",
|
2020-04-13 23:37:22 +00:00
|
|
|
target_os = "illumos",
|
2019-11-27 10:28:39 -08:00
|
|
|
target_os = "emscripten",
|
|
|
|
|
target_os = "fuchsia",
|
|
|
|
|
target_os = "l4re",
|
|
|
|
|
target_os = "linux",
|
|
|
|
|
target_os = "haiku",
|
|
|
|
|
target_os = "redox"
|
|
|
|
|
))]
|
2016-06-24 11:31:58 +02:00
|
|
|
pub fn set_cloexec(&self) -> io::Result<()> {
|
2015-10-24 20:51:34 -05:00
|
|
|
unsafe {
|
2016-06-24 11:31:58 +02:00
|
|
|
let previous = cvt(libc::fcntl(self.fd, libc::F_GETFD))?;
|
2017-02-04 01:10:12 +01:00
|
|
|
let new = previous | libc::FD_CLOEXEC;
|
|
|
|
|
if new != previous {
|
|
|
|
|
cvt(libc::fcntl(self.fd, libc::F_SETFD, new))?;
|
|
|
|
|
}
|
2016-06-24 11:31:58 +02:00
|
|
|
Ok(())
|
2015-10-24 20:51:34 -05:00
|
|
|
}
|
|
|
|
|
}
|
2016-01-20 21:24:23 -08:00
|
|
|
|
2017-02-04 01:10:12 +01: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 11:31:58 +02:00
|
|
|
pub fn set_nonblocking(&self, nonblocking: bool) -> io::Result<()> {
|
2016-02-12 10:29:25 -08:00
|
|
|
unsafe {
|
2016-06-24 11:31:58 +02:00
|
|
|
let previous = cvt(libc::fcntl(self.fd, libc::F_GETFL))?;
|
2016-02-12 10:29:25 -08:00
|
|
|
let new = if nonblocking {
|
|
|
|
|
previous | libc::O_NONBLOCK
|
|
|
|
|
} else {
|
|
|
|
|
previous & !libc::O_NONBLOCK
|
|
|
|
|
};
|
2017-02-04 01:10:12 +01:00
|
|
|
if new != previous {
|
|
|
|
|
cvt(libc::fcntl(self.fd, libc::F_SETFL, new))?;
|
|
|
|
|
}
|
2016-06-24 11:31:58 +02:00
|
|
|
Ok(())
|
2016-02-12 10:29:25 -08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-01-20 21:24:23 -08: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 14:32:36 -07: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-20 21:24:23 -08:00
|
|
|
}
|
2015-02-02 21:39:14 -08:00
|
|
|
}
|
|
|
|
|
|
2016-02-12 00:17:24 -08:00
|
|
|
impl<'a> Read for &'a FileDesc {
|
|
|
|
|
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
|
|
|
|
|
(**self).read(buf)
|
|
|
|
|
}
|
2018-09-05 23:09:58 -04:00
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
|
unsafe fn initializer(&self) -> Initializer {
|
|
|
|
|
Initializer::nop()
|
|
|
|
|
}
|
2016-02-12 00:17:24 -08:00
|
|
|
}
|
|
|
|
|
|
2015-02-05 16:50:11 -08:00
|
|
|
impl AsInner<c_int> for FileDesc {
|
2019-11-27 10:28:39 -08:00
|
|
|
fn as_inner(&self) -> &c_int {
|
|
|
|
|
&self.fd
|
|
|
|
|
}
|
2015-02-05 16:50:11 -08:00
|
|
|
}
|
|
|
|
|
|
2015-02-02 21:39:14 -08:00
|
|
|
impl Drop for FileDesc {
|
|
|
|
|
fn drop(&mut self) {
|
2015-04-03 15:44:14 -07: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 11:06:40 +02:00
|
|
|
// opened after we closed ours.
|
2015-04-03 15:44:14 -07:00
|
|
|
let _ = unsafe { libc::close(self.fd) };
|
2015-02-02 21:39:14 -08:00
|
|
|
}
|
|
|
|
|
}
|
2020-08-01 14:18:11 +02:00
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
|
mod tests {
|
|
|
|
|
use super::{FileDesc, IoSlice};
|
2020-08-06 00:00:00 +00:00
|
|
|
use core::mem::ManuallyDrop;
|
2020-08-01 14:18:11 +02:00
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn limit_vector_count() {
|
2020-08-06 00:00:00 +00:00
|
|
|
let stdout = ManuallyDrop::new(FileDesc { fd: 1 });
|
2020-08-01 14:18:11 +02:00
|
|
|
let bufs = (0..1500).map(|_| IoSlice::new(&[])).collect::<Vec<_>>();
|
|
|
|
|
assert!(stdout.write_vectored(&bufs).is_ok());
|
|
|
|
|
}
|
|
|
|
|
}
|