2019-12-21 13:16:18 +02:00
|
|
|
#![unstable(reason = "not public", issue = "none", feature = "fd")]
|
2016-02-12 10:29:25 -08:00
|
|
|
|
2020-08-27 13:45:01 +00:00
|
|
|
#[cfg(test)]
|
|
|
|
|
mod tests;
|
|
|
|
|
|
2019-02-11 04:23:21 +09:00
|
|
|
use crate::cmp;
|
2022-05-13 15:06:36 +01:00
|
|
|
use crate::io::{self, BorrowCursor, IoSlice, IoSliceMut, Read};
|
2021-06-30 21:44:30 -07:00
|
|
|
use crate::os::unix::io::{AsFd, AsRawFd, BorrowedFd, FromRawFd, IntoRawFd, OwnedFd, RawFd};
|
2019-02-11 04:23:21 +09:00
|
|
|
use crate::sys::cvt;
|
2021-06-30 21:44:30 -07:00
|
|
|
use crate::sys_common::{AsInner, FromInner, IntoInner};
|
2019-02-11 04:23:21 +09:00
|
|
|
|
2022-04-07 12:07:46 +01:00
|
|
|
#[cfg(any(
|
|
|
|
|
target_os = "android",
|
|
|
|
|
target_os = "linux",
|
|
|
|
|
target_os = "emscripten",
|
|
|
|
|
target_os = "l4re"
|
|
|
|
|
))]
|
|
|
|
|
use libc::off64_t;
|
|
|
|
|
#[cfg(not(any(
|
|
|
|
|
target_os = "linux",
|
|
|
|
|
target_os = "emscripten",
|
|
|
|
|
target_os = "l4re",
|
|
|
|
|
target_os = "android"
|
|
|
|
|
)))]
|
|
|
|
|
use libc::off_t as off64_t;
|
|
|
|
|
|
2016-11-25 13:21:49 -05:00
|
|
|
#[derive(Debug)]
|
2021-06-30 21:44:30 -07:00
|
|
|
pub struct FileDesc(OwnedFd);
|
2015-02-02 21:39:14 -08:00
|
|
|
|
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")]
|
2021-10-19 17:59:59 +02:00
|
|
|
const READ_LIMIT: usize = libc::c_int::MAX as usize - 1;
|
2020-07-23 02:49:40 +00:00
|
|
|
#[cfg(not(target_os = "macos"))]
|
|
|
|
|
const READ_LIMIT: usize = libc::ssize_t::MAX as usize;
|
2016-12-25 21:57:32 -08:00
|
|
|
|
2020-09-10 11:35:25 +02:00
|
|
|
#[cfg(any(
|
|
|
|
|
target_os = "dragonfly",
|
|
|
|
|
target_os = "freebsd",
|
|
|
|
|
target_os = "ios",
|
|
|
|
|
target_os = "macos",
|
|
|
|
|
target_os = "netbsd",
|
|
|
|
|
target_os = "openbsd",
|
2022-03-23 16:05:01 +00:00
|
|
|
target_os = "watchos",
|
2020-09-10 11:35:25 +02:00
|
|
|
))]
|
|
|
|
|
const fn max_iov() -> usize {
|
|
|
|
|
libc::IOV_MAX as usize
|
|
|
|
|
}
|
2020-08-01 15:38:08 +02:00
|
|
|
|
2020-09-10 11:35:25 +02: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 14:18:11 +02:00
|
|
|
}
|
|
|
|
|
|
2020-09-10 11:35:25 +02: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",
|
2022-03-23 16:05:01 +00:00
|
|
|
target_os = "horizon",
|
|
|
|
|
target_os = "watchos",
|
2020-09-10 11:35:25 +02:00
|
|
|
)))]
|
|
|
|
|
const fn max_iov() -> usize {
|
2020-08-05 16:56:51 +02:00
|
|
|
16 // The minimum value required by POSIX.
|
|
|
|
|
}
|
|
|
|
|
|
2015-02-02 21:39:14 -08:00
|
|
|
impl FileDesc {
|
|
|
|
|
pub fn read(&self, buf: &mut [u8]) -> io::Result<usize> {
|
2016-03-22 22:01:37 -05:00
|
|
|
let ret = cvt(unsafe {
|
2021-06-30 21:44:30 -07:00
|
|
|
libc::read(
|
|
|
|
|
self.as_raw_fd(),
|
2021-10-19 17:59:59 +02:00
|
|
|
buf.as_mut_ptr() as *mut libc::c_void,
|
2021-06-30 21:44:30 -07:00
|
|
|
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)
|
|
|
|
|
}
|
|
|
|
|
|
2021-10-19 17:59:59 +02:00
|
|
|
#[cfg(not(any(target_os = "espidf", target_os = "horizon")))]
|
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(
|
2021-06-30 21:44:30 -07:00
|
|
|
self.as_raw_fd(),
|
2019-11-27 10:28:39 -08:00
|
|
|
bufs.as_ptr() as *const libc::iovec,
|
2021-10-19 17:59:59 +02:00
|
|
|
cmp::min(bufs.len(), max_iov()) as libc::c_int,
|
2019-11-27 10:28:39 -08:00
|
|
|
)
|
2019-02-08 20:42:34 +01:00
|
|
|
})?;
|
|
|
|
|
Ok(ret as usize)
|
|
|
|
|
}
|
|
|
|
|
|
2021-10-19 17:59:59 +02:00
|
|
|
#[cfg(any(target_os = "espidf", target_os = "horizon"))]
|
2021-07-29 20:18:22 +03:00
|
|
|
pub fn read_vectored(&self, bufs: &mut [IoSliceMut<'_>]) -> io::Result<usize> {
|
|
|
|
|
return crate::io::default_read_vectored(|b| self.read(b), bufs);
|
|
|
|
|
}
|
|
|
|
|
|
2020-01-03 11:26:05 -08:00
|
|
|
#[inline]
|
2020-03-11 18:02:52 -07:00
|
|
|
pub fn is_read_vectored(&self) -> bool {
|
2021-10-19 17:59:59 +02:00
|
|
|
cfg!(not(any(target_os = "espidf", target_os = "horizon")))
|
2020-01-03 11:26:05 -08:00
|
|
|
}
|
|
|
|
|
|
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> {
|
Refactor weak symbols in std::sys::unix
This makes a few changes to the weak symbol macros in `sys::unix`:
- `dlsym!` is added to keep the functionality for runtime `dlsym`
lookups, like for `__pthread_get_minstack@GLIBC_PRIVATE` that we don't
want to show up in ELF symbol tables.
- `weak!` now uses `#[linkage = "extern_weak"]` symbols, so its runtime
behavior is just a simple null check. This is also used by `syscall!`.
- On non-ELF targets (macos/ios) where that linkage is not known to
behave, `weak!` is just an alias to `dlsym!` for the old behavior.
- `raw_syscall!` is added to always call `libc::syscall` on linux and
android, for cases like `clone3` that have no known libc wrapper.
The new `weak!` linkage does mean that you'll get versioned symbols if
you build with a newer glibc, like `WEAK DEFAULT UND statx@GLIBC_2.28`.
This might seem problematic, but old non-weak symbols can tie the build
to new versions too, like `dlsym@GLIBC_2.34` from their recent library
unification. If you build with an old glibc like `dist-x86_64-linux`
does, you'll still get unversioned `WEAK DEFAULT UND statx`, which may
be resolved based on the runtime glibc.
I also found a few functions that don't need to be weak anymore:
- Android can directly use `ftruncate64`, `pread64`, and `pwrite64`, as
these were added in API 12, and our baseline is API 14.
- Linux can directly use `splice`, added way back in glibc 2.5 and
similarly old musl. Android only added it in API 21 though.
2021-11-12 12:58:38 -08:00
|
|
|
#[cfg(not(any(target_os = "linux", target_os = "android")))]
|
|
|
|
|
use libc::pread as pread64;
|
|
|
|
|
#[cfg(any(target_os = "linux", target_os = "android"))]
|
|
|
|
|
use libc::pread64;
|
2016-10-09 01:06:55 +02:00
|
|
|
|
2016-10-08 14:32:57 +02:00
|
|
|
unsafe {
|
Refactor weak symbols in std::sys::unix
This makes a few changes to the weak symbol macros in `sys::unix`:
- `dlsym!` is added to keep the functionality for runtime `dlsym`
lookups, like for `__pthread_get_minstack@GLIBC_PRIVATE` that we don't
want to show up in ELF symbol tables.
- `weak!` now uses `#[linkage = "extern_weak"]` symbols, so its runtime
behavior is just a simple null check. This is also used by `syscall!`.
- On non-ELF targets (macos/ios) where that linkage is not known to
behave, `weak!` is just an alias to `dlsym!` for the old behavior.
- `raw_syscall!` is added to always call `libc::syscall` on linux and
android, for cases like `clone3` that have no known libc wrapper.
The new `weak!` linkage does mean that you'll get versioned symbols if
you build with a newer glibc, like `WEAK DEFAULT UND statx@GLIBC_2.28`.
This might seem problematic, but old non-weak symbols can tie the build
to new versions too, like `dlsym@GLIBC_2.34` from their recent library
unification. If you build with an old glibc like `dist-x86_64-linux`
does, you'll still get unversioned `WEAK DEFAULT UND statx`, which may
be resolved based on the runtime glibc.
I also found a few functions that don't need to be weak anymore:
- Android can directly use `ftruncate64`, `pread64`, and `pwrite64`, as
these were added in API 12, and our baseline is API 14.
- Linux can directly use `splice`, added way back in glibc 2.5 and
similarly old musl. Android only added it in API 21 though.
2021-11-12 12:58:38 -08:00
|
|
|
cvt(pread64(
|
2021-06-30 21:44:30 -07:00
|
|
|
self.as_raw_fd(),
|
2021-10-19 17:59:59 +02:00
|
|
|
buf.as_mut_ptr() as *mut libc::c_void,
|
2020-07-23 02:49:40 +00:00
|
|
|
cmp::min(buf.len(), READ_LIMIT),
|
2022-04-07 12:07:46 +01:00
|
|
|
offset as off64_t,
|
Refactor weak symbols in std::sys::unix
This makes a few changes to the weak symbol macros in `sys::unix`:
- `dlsym!` is added to keep the functionality for runtime `dlsym`
lookups, like for `__pthread_get_minstack@GLIBC_PRIVATE` that we don't
want to show up in ELF symbol tables.
- `weak!` now uses `#[linkage = "extern_weak"]` symbols, so its runtime
behavior is just a simple null check. This is also used by `syscall!`.
- On non-ELF targets (macos/ios) where that linkage is not known to
behave, `weak!` is just an alias to `dlsym!` for the old behavior.
- `raw_syscall!` is added to always call `libc::syscall` on linux and
android, for cases like `clone3` that have no known libc wrapper.
The new `weak!` linkage does mean that you'll get versioned symbols if
you build with a newer glibc, like `WEAK DEFAULT UND statx@GLIBC_2.28`.
This might seem problematic, but old non-weak symbols can tie the build
to new versions too, like `dlsym@GLIBC_2.34` from their recent library
unification. If you build with an old glibc like `dist-x86_64-linux`
does, you'll still get unversioned `WEAK DEFAULT UND statx`, which may
be resolved based on the runtime glibc.
I also found a few functions that don't need to be weak anymore:
- Android can directly use `ftruncate64`, `pread64`, and `pwrite64`, as
these were added in API 12, and our baseline is API 14.
- Linux can directly use `splice`, added way back in glibc 2.5 and
similarly old musl. Android only added it in API 21 though.
2021-11-12 12:58:38 -08:00
|
|
|
))
|
2019-11-27 10:28:39 -08:00
|
|
|
.map(|n| n as usize)
|
2016-10-08 14:32:57 +02:00
|
|
|
}
|
2016-08-16 01:11:33 +02:00
|
|
|
}
|
|
|
|
|
|
2022-05-13 15:06:36 +01:00
|
|
|
pub fn read_buf(&self, mut cursor: BorrowCursor<'_, '_>) -> io::Result<()> {
|
2021-05-08 22:04:38 -07:00
|
|
|
let ret = cvt(unsafe {
|
|
|
|
|
libc::read(
|
|
|
|
|
self.as_raw_fd(),
|
2022-05-13 15:06:36 +01:00
|
|
|
cursor.as_mut().as_mut_ptr() as *mut libc::c_void,
|
|
|
|
|
cmp::min(cursor.capacity(), READ_LIMIT),
|
2021-05-08 22:04:38 -07:00
|
|
|
)
|
|
|
|
|
})?;
|
|
|
|
|
|
|
|
|
|
// Safety: `ret` bytes were written to the initialized portion of the buffer
|
|
|
|
|
unsafe {
|
2022-05-13 15:06:36 +01:00
|
|
|
cursor.advance(ret as usize);
|
2021-05-08 22:04:38 -07:00
|
|
|
}
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
|
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 {
|
2021-06-30 21:44:30 -07:00
|
|
|
libc::write(
|
|
|
|
|
self.as_raw_fd(),
|
2021-10-19 17:59:59 +02:00
|
|
|
buf.as_ptr() as *const libc::c_void,
|
2021-06-30 21:44:30 -07:00
|
|
|
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
|
|
|
|
2021-10-19 17:59:59 +02:00
|
|
|
#[cfg(not(any(target_os = "espidf", target_os = "horizon")))]
|
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(
|
2021-06-30 21:44:30 -07:00
|
|
|
self.as_raw_fd(),
|
2019-11-27 10:28:39 -08:00
|
|
|
bufs.as_ptr() as *const libc::iovec,
|
2021-10-19 17:59:59 +02:00
|
|
|
cmp::min(bufs.len(), max_iov()) as libc::c_int,
|
2019-11-27 10:28:39 -08:00
|
|
|
)
|
2019-02-08 20:42:34 +01:00
|
|
|
})?;
|
|
|
|
|
Ok(ret as usize)
|
|
|
|
|
}
|
|
|
|
|
|
2021-10-19 17:59:59 +02:00
|
|
|
#[cfg(any(target_os = "espidf", target_os = "horizon"))]
|
2021-07-29 20:18:22 +03:00
|
|
|
pub fn write_vectored(&self, bufs: &[IoSlice<'_>]) -> io::Result<usize> {
|
|
|
|
|
return crate::io::default_write_vectored(|b| self.write(b), bufs);
|
|
|
|
|
}
|
|
|
|
|
|
2020-01-03 11:26:05 -08:00
|
|
|
#[inline]
|
2020-03-11 18:02:52 -07:00
|
|
|
pub fn is_write_vectored(&self) -> bool {
|
2021-10-19 17:59:59 +02:00
|
|
|
cfg!(not(any(target_os = "espidf", target_os = "horizon")))
|
2020-01-03 11:26:05 -08:00
|
|
|
}
|
|
|
|
|
|
2016-08-16 01:11:33 +02:00
|
|
|
pub fn write_at(&self, buf: &[u8], offset: u64) -> io::Result<usize> {
|
Refactor weak symbols in std::sys::unix
This makes a few changes to the weak symbol macros in `sys::unix`:
- `dlsym!` is added to keep the functionality for runtime `dlsym`
lookups, like for `__pthread_get_minstack@GLIBC_PRIVATE` that we don't
want to show up in ELF symbol tables.
- `weak!` now uses `#[linkage = "extern_weak"]` symbols, so its runtime
behavior is just a simple null check. This is also used by `syscall!`.
- On non-ELF targets (macos/ios) where that linkage is not known to
behave, `weak!` is just an alias to `dlsym!` for the old behavior.
- `raw_syscall!` is added to always call `libc::syscall` on linux and
android, for cases like `clone3` that have no known libc wrapper.
The new `weak!` linkage does mean that you'll get versioned symbols if
you build with a newer glibc, like `WEAK DEFAULT UND statx@GLIBC_2.28`.
This might seem problematic, but old non-weak symbols can tie the build
to new versions too, like `dlsym@GLIBC_2.34` from their recent library
unification. If you build with an old glibc like `dist-x86_64-linux`
does, you'll still get unversioned `WEAK DEFAULT UND statx`, which may
be resolved based on the runtime glibc.
I also found a few functions that don't need to be weak anymore:
- Android can directly use `ftruncate64`, `pread64`, and `pwrite64`, as
these were added in API 12, and our baseline is API 14.
- Linux can directly use `splice`, added way back in glibc 2.5 and
similarly old musl. Android only added it in API 21 though.
2021-11-12 12:58:38 -08:00
|
|
|
#[cfg(not(any(target_os = "linux", target_os = "android")))]
|
|
|
|
|
use libc::pwrite as pwrite64;
|
|
|
|
|
#[cfg(any(target_os = "linux", target_os = "android"))]
|
|
|
|
|
use libc::pwrite64;
|
2016-10-09 01:06:55 +02:00
|
|
|
|
2016-10-08 14:32:57 +02:00
|
|
|
unsafe {
|
Refactor weak symbols in std::sys::unix
This makes a few changes to the weak symbol macros in `sys::unix`:
- `dlsym!` is added to keep the functionality for runtime `dlsym`
lookups, like for `__pthread_get_minstack@GLIBC_PRIVATE` that we don't
want to show up in ELF symbol tables.
- `weak!` now uses `#[linkage = "extern_weak"]` symbols, so its runtime
behavior is just a simple null check. This is also used by `syscall!`.
- On non-ELF targets (macos/ios) where that linkage is not known to
behave, `weak!` is just an alias to `dlsym!` for the old behavior.
- `raw_syscall!` is added to always call `libc::syscall` on linux and
android, for cases like `clone3` that have no known libc wrapper.
The new `weak!` linkage does mean that you'll get versioned symbols if
you build with a newer glibc, like `WEAK DEFAULT UND statx@GLIBC_2.28`.
This might seem problematic, but old non-weak symbols can tie the build
to new versions too, like `dlsym@GLIBC_2.34` from their recent library
unification. If you build with an old glibc like `dist-x86_64-linux`
does, you'll still get unversioned `WEAK DEFAULT UND statx`, which may
be resolved based on the runtime glibc.
I also found a few functions that don't need to be weak anymore:
- Android can directly use `ftruncate64`, `pread64`, and `pwrite64`, as
these were added in API 12, and our baseline is API 14.
- Linux can directly use `splice`, added way back in glibc 2.5 and
similarly old musl. Android only added it in API 21 though.
2021-11-12 12:58:38 -08:00
|
|
|
cvt(pwrite64(
|
2021-06-30 21:44:30 -07:00
|
|
|
self.as_raw_fd(),
|
2021-10-19 17:59:59 +02:00
|
|
|
buf.as_ptr() as *const libc::c_void,
|
2020-07-23 02:49:40 +00:00
|
|
|
cmp::min(buf.len(), READ_LIMIT),
|
2022-04-07 12:07:46 +01:00
|
|
|
offset as off64_t,
|
Refactor weak symbols in std::sys::unix
This makes a few changes to the weak symbol macros in `sys::unix`:
- `dlsym!` is added to keep the functionality for runtime `dlsym`
lookups, like for `__pthread_get_minstack@GLIBC_PRIVATE` that we don't
want to show up in ELF symbol tables.
- `weak!` now uses `#[linkage = "extern_weak"]` symbols, so its runtime
behavior is just a simple null check. This is also used by `syscall!`.
- On non-ELF targets (macos/ios) where that linkage is not known to
behave, `weak!` is just an alias to `dlsym!` for the old behavior.
- `raw_syscall!` is added to always call `libc::syscall` on linux and
android, for cases like `clone3` that have no known libc wrapper.
The new `weak!` linkage does mean that you'll get versioned symbols if
you build with a newer glibc, like `WEAK DEFAULT UND statx@GLIBC_2.28`.
This might seem problematic, but old non-weak symbols can tie the build
to new versions too, like `dlsym@GLIBC_2.34` from their recent library
unification. If you build with an old glibc like `dist-x86_64-linux`
does, you'll still get unversioned `WEAK DEFAULT UND statx`, which may
be resolved based on the runtime glibc.
I also found a few functions that don't need to be weak anymore:
- Android can directly use `ftruncate64`, `pread64`, and `pwrite64`, as
these were added in API 12, and our baseline is API 14.
- Linux can directly use `splice`, added way back in glibc 2.5 and
similarly old musl. Android only added it in API 21 though.
2021-11-12 12:58:38 -08:00
|
|
|
))
|
2019-11-27 10:28:39 -08:00
|
|
|
.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> {
|
2021-06-30 21:44:30 -07:00
|
|
|
unsafe { Ok((cvt(libc::fcntl(self.as_raw_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",
|
2020-10-07 20:38:25 +02:00
|
|
|
target_os = "redox",
|
|
|
|
|
target_os = "vxworks"
|
2019-11-27 10:28:39 -08:00
|
|
|
)))]
|
2016-06-24 11:31:58 +02:00
|
|
|
pub fn set_cloexec(&self) -> io::Result<()> {
|
2015-04-03 15:30:10 -07:00
|
|
|
unsafe {
|
2021-06-30 21:44:30 -07:00
|
|
|
cvt(libc::ioctl(self.as_raw_fd(), libc::FIOCLEX))?;
|
2016-06-24 11:31:58 +02:00
|
|
|
Ok(())
|
2015-04-03 15:30:10 -07:00
|
|
|
}
|
|
|
|
|
}
|
2019-11-27 10:28:39 -08:00
|
|
|
#[cfg(any(
|
2021-10-19 17:59:59 +02:00
|
|
|
all(target_env = "newlib", not(any(target_os = "espidf", target_os = "horizon"))),
|
2019-11-27 10:28:39 -08:00
|
|
|
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",
|
2020-10-07 20:38:25 +02:00
|
|
|
target_os = "redox",
|
|
|
|
|
target_os = "vxworks"
|
2019-11-27 10:28:39 -08:00
|
|
|
))]
|
2016-06-24 11:31:58 +02:00
|
|
|
pub fn set_cloexec(&self) -> io::Result<()> {
|
2015-10-24 20:51:34 -05:00
|
|
|
unsafe {
|
2021-06-30 21:44:30 -07:00
|
|
|
let previous = cvt(libc::fcntl(self.as_raw_fd(), libc::F_GETFD))?;
|
2017-02-04 01:10:12 +01:00
|
|
|
let new = previous | libc::FD_CLOEXEC;
|
|
|
|
|
if new != previous {
|
2021-06-30 21:44:30 -07:00
|
|
|
cvt(libc::fcntl(self.as_raw_fd(), libc::F_SETFD, new))?;
|
2017-02-04 01:10:12 +01:00
|
|
|
}
|
2016-06-24 11:31:58 +02:00
|
|
|
Ok(())
|
2015-10-24 20:51:34 -05:00
|
|
|
}
|
|
|
|
|
}
|
2021-10-19 17:59:59 +02:00
|
|
|
#[cfg(any(target_os = "espidf", target_os = "horizon"))]
|
2021-07-29 20:18:22 +03:00
|
|
|
pub fn set_cloexec(&self) -> io::Result<()> {
|
2021-10-19 17:59:59 +02:00
|
|
|
// FD_CLOEXEC is not supported in ESP-IDF and Horizon OS but there's no need to,
|
|
|
|
|
// because neither supports spawning processes.
|
2021-07-29 20:18:22 +03:00
|
|
|
Ok(())
|
|
|
|
|
}
|
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 {
|
2021-10-19 17:59:59 +02:00
|
|
|
let v = nonblocking as libc::c_int;
|
2021-06-30 21:44:30 -07:00
|
|
|
cvt(libc::ioctl(self.as_raw_fd(), libc::FIONBIO, &v))?;
|
2017-02-04 01:10:12 +01:00
|
|
|
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 {
|
2021-06-30 21:44:30 -07:00
|
|
|
let previous = cvt(libc::fcntl(self.as_raw_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 {
|
2021-06-30 21:44:30 -07:00
|
|
|
cvt(libc::fcntl(self.as_raw_fd(), libc::F_SETFL, new))?;
|
2017-02-04 01:10:12 +01:00
|
|
|
}
|
2016-06-24 11:31:58 +02:00
|
|
|
Ok(())
|
2016-02-12 10:29:25 -08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-09 14:05:10 -07:00
|
|
|
#[inline]
|
2016-01-20 21:24:23 -08:00
|
|
|
pub fn duplicate(&self) -> io::Result<FileDesc> {
|
2021-09-09 14:05:10 -07:00
|
|
|
Ok(Self(self.0.try_clone()?))
|
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)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-30 21:44:30 -07:00
|
|
|
impl AsInner<OwnedFd> for FileDesc {
|
|
|
|
|
fn as_inner(&self) -> &OwnedFd {
|
|
|
|
|
&self.0
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl IntoInner<OwnedFd> for FileDesc {
|
|
|
|
|
fn into_inner(self) -> OwnedFd {
|
|
|
|
|
self.0
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl FromInner<OwnedFd> for FileDesc {
|
|
|
|
|
fn from_inner(owned_fd: OwnedFd) -> Self {
|
|
|
|
|
Self(owned_fd)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl AsFd for FileDesc {
|
|
|
|
|
fn as_fd(&self) -> BorrowedFd<'_> {
|
|
|
|
|
self.0.as_fd()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl AsRawFd for FileDesc {
|
|
|
|
|
fn as_raw_fd(&self) -> RawFd {
|
|
|
|
|
self.0.as_raw_fd()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl IntoRawFd for FileDesc {
|
|
|
|
|
fn into_raw_fd(self) -> RawFd {
|
|
|
|
|
self.0.into_raw_fd()
|
2019-11-27 10:28:39 -08:00
|
|
|
}
|
2015-02-05 16:50:11 -08:00
|
|
|
}
|
|
|
|
|
|
2021-06-30 21:44:30 -07:00
|
|
|
impl FromRawFd for FileDesc {
|
|
|
|
|
unsafe fn from_raw_fd(raw_fd: RawFd) -> Self {
|
|
|
|
|
Self(FromRawFd::from_raw_fd(raw_fd))
|
2015-02-02 21:39:14 -08:00
|
|
|
}
|
|
|
|
|
}
|