2019-12-21 11:16:18 +00:00
|
|
|
#![unstable(reason = "not public", issue = "none", feature = "fd")]
|
2016-02-12 18:29:25 +00:00
|
|
|
|
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::sync::atomic::{AtomicBool, Ordering};
|
|
|
|
use crate::sys::cvt;
|
|
|
|
use crate::sys_common::AsInner;
|
|
|
|
|
|
|
|
use libc::{c_int, c_void, ssize_t};
|
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,
|
|
|
|
}
|
|
|
|
|
2016-12-26 05:57:32 +00:00
|
|
|
fn max_len() -> usize {
|
|
|
|
// 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".
|
|
|
|
//
|
2017-03-12 18:13:35 +00:00
|
|
|
// On macOS, however, apparently the 64-bit libc is either buggy or
|
2016-12-26 05:57:32 +00:00
|
|
|
// 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.
|
|
|
|
if cfg!(target_os = "macos") {
|
|
|
|
<c_int>::max_value() as usize - 1
|
|
|
|
} else {
|
|
|
|
<ssize_t>::max_value() as usize
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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 {
|
2019-11-27 18:28:39 +00:00
|
|
|
libc::read(self.fd, buf.as_mut_ptr() as *mut c_void, cmp::min(buf.len(), max_len()))
|
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,
|
|
|
|
cmp::min(bufs.len(), c_int::max_value() as usize) as c_int,
|
|
|
|
)
|
2019-02-08 19:42:34 +00:00
|
|
|
})?;
|
|
|
|
Ok(ret as usize)
|
|
|
|
}
|
|
|
|
|
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,
|
|
|
|
cmp::min(buf.len(), max_len()),
|
|
|
|
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 {
|
2019-11-27 18:28:39 +00:00
|
|
|
libc::write(self.fd, buf.as_ptr() as *const c_void, cmp::min(buf.len(), max_len()))
|
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,
|
|
|
|
cmp::min(bufs.len(), c_int::max_value() as usize) as c_int,
|
|
|
|
)
|
2019-02-08 19:42:34 +00:00
|
|
|
})?;
|
|
|
|
Ok(ret as usize)
|
|
|
|
}
|
|
|
|
|
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,
|
|
|
|
cmp::min(buf.len(), max_len()),
|
|
|
|
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",
|
|
|
|
target_os = "emscripten",
|
|
|
|
target_os = "fuchsia",
|
|
|
|
target_os = "l4re",
|
|
|
|
target_os = "linux",
|
|
|
|
target_os = "haiku",
|
|
|
|
target_os = "redox"
|
|
|
|
)))]
|
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",
|
|
|
|
target_os = "emscripten",
|
|
|
|
target_os = "fuchsia",
|
|
|
|
target_os = "l4re",
|
|
|
|
target_os = "linux",
|
|
|
|
target_os = "haiku",
|
|
|
|
target_os = "redox"
|
|
|
|
))]
|
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
|
|
|
|
// flag, however, isn't supported on older Linux kernels (earlier than
|
|
|
|
// 2.6.24).
|
|
|
|
//
|
|
|
|
// To detect this and ensure that CLOEXEC is still set, we
|
|
|
|
// follow a strategy similar to musl [1] where if passing
|
|
|
|
// F_DUPFD_CLOEXEC causes `fcntl` to return EINVAL it means it's not
|
|
|
|
// supported (the third parameter, 0, is always valid), so we stop
|
2016-02-04 21:22:51 +00:00
|
|
|
// trying that.
|
2016-01-21 05:24:23 +00:00
|
|
|
//
|
|
|
|
// Also note that Android doesn't have F_DUPFD_CLOEXEC, but get it to
|
|
|
|
// resolve so we at least compile this.
|
|
|
|
//
|
|
|
|
// [1]: http://comments.gmane.org/gmane.linux.lib.musl.general/2963
|
2016-09-25 04:38:56 +00:00
|
|
|
#[cfg(any(target_os = "android", target_os = "haiku"))]
|
2016-01-21 05:24:23 +00:00
|
|
|
use libc::F_DUPFD as F_DUPFD_CLOEXEC;
|
2019-11-27 18:28:39 +00:00
|
|
|
#[cfg(not(any(target_os = "android", target_os = "haiku")))]
|
2016-01-21 05:24:23 +00:00
|
|
|
use libc::F_DUPFD_CLOEXEC;
|
|
|
|
|
|
|
|
let make_filedesc = |fd| {
|
|
|
|
let fd = FileDesc::new(fd);
|
2016-06-24 09:31:58 +00:00
|
|
|
fd.set_cloexec()?;
|
|
|
|
Ok(fd)
|
2016-01-21 05:24:23 +00:00
|
|
|
};
|
2019-11-27 18:28:39 +00:00
|
|
|
static TRY_CLOEXEC: AtomicBool = AtomicBool::new(!cfg!(target_os = "android"));
|
2016-01-21 05:24:23 +00:00
|
|
|
let fd = self.raw();
|
2016-02-04 21:22:51 +00:00
|
|
|
if TRY_CLOEXEC.load(Ordering::Relaxed) {
|
2016-01-21 05:24:23 +00:00
|
|
|
match cvt(unsafe { libc::fcntl(fd, F_DUPFD_CLOEXEC, 0) }) {
|
2016-02-04 21:22:51 +00:00
|
|
|
// We *still* call the `set_cloexec` method as apparently some
|
|
|
|
// linux kernel at some point stopped setting CLOEXEC even
|
|
|
|
// though it reported doing so on F_DUPFD_CLOEXEC.
|
|
|
|
Ok(fd) => {
|
|
|
|
return Ok(if cfg!(target_os = "linux") {
|
2016-06-24 09:31:58 +00:00
|
|
|
make_filedesc(fd)?
|
2016-02-04 21:22:51 +00:00
|
|
|
} else {
|
|
|
|
FileDesc::new(fd)
|
2019-11-27 18:28:39 +00:00
|
|
|
});
|
2016-02-04 21:22:51 +00:00
|
|
|
}
|
2016-01-21 05:24:23 +00:00
|
|
|
Err(ref e) if e.raw_os_error() == Some(libc::EINVAL) => {
|
|
|
|
TRY_CLOEXEC.store(false, Ordering::Relaxed);
|
|
|
|
}
|
2016-02-04 21:22:51 +00:00
|
|
|
Err(e) => return Err(e),
|
2016-01-21 05:24:23 +00:00
|
|
|
}
|
|
|
|
}
|
2016-06-24 09:31:58 +00:00
|
|
|
cvt(unsafe { libc::fcntl(fd, libc::F_DUPFD, 0) }).and_then(make_filedesc)
|
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
|
|
|
}
|
|
|
|
}
|