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};
|
2021-07-01 04:44:30 +00:00
|
|
|
use crate::os::unix::io::{AsFd, AsRawFd, BorrowedFd, FromRawFd, IntoRawFd, OwnedFd, RawFd};
|
2019-02-10 19:23:21 +00:00
|
|
|
use crate::sys::cvt;
|
2021-07-01 04:44:30 +00:00
|
|
|
use crate::sys_common::{AsInner, FromInner, IntoInner};
|
2019-02-10 19:23:21 +00:00
|
|
|
|
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)]
|
2021-07-01 04:44:30 +00:00
|
|
|
pub struct FileDesc(OwnedFd);
|
2015-02-03 05:39:14 +00: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")]
|
|
|
|
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 read(&self, buf: &mut [u8]) -> io::Result<usize> {
|
2016-03-23 03:01:37 +00:00
|
|
|
let ret = cvt(unsafe {
|
2021-07-01 04:44:30 +00:00
|
|
|
libc::read(
|
|
|
|
self.as_raw_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)
|
|
|
|
}
|
|
|
|
|
2021-07-29 17:18:22 +00:00
|
|
|
#[cfg(not(target_os = "espidf"))]
|
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(
|
2021-07-01 04:44:30 +00:00
|
|
|
self.as_raw_fd(),
|
2019-11-27 18:28:39 +00:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2021-07-29 17:18:22 +00:00
|
|
|
#[cfg(target_os = "espidf")]
|
|
|
|
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 19:26:05 +00:00
|
|
|
#[inline]
|
2020-03-12 01:02:52 +00:00
|
|
|
pub fn is_read_vectored(&self) -> bool {
|
2021-07-29 17:18:22 +00:00
|
|
|
cfg!(not(target_os = "espidf"))
|
2020-01-03 19:26:05 +00:00
|
|
|
}
|
|
|
|
|
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> {
|
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 20:58:38 +00: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-08 23:06:55 +00:00
|
|
|
|
2016-10-08 12:32:57 +00: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 20:58:38 +00:00
|
|
|
cvt(pread64(
|
2021-07-01 04:44:30 +00:00
|
|
|
self.as_raw_fd(),
|
2019-11-27 18:28:39 +00:00
|
|
|
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,
|
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 20:58:38 +00:00
|
|
|
))
|
2019-11-27 18:28:39 +00:00
|
|
|
.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 {
|
2021-07-01 04:44:30 +00:00
|
|
|
libc::write(
|
|
|
|
self.as_raw_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
|
|
|
|
2021-07-29 17:18:22 +00:00
|
|
|
#[cfg(not(target_os = "espidf"))]
|
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(
|
2021-07-01 04:44:30 +00:00
|
|
|
self.as_raw_fd(),
|
2019-11-27 18:28:39 +00:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2021-07-29 17:18:22 +00:00
|
|
|
#[cfg(target_os = "espidf")]
|
|
|
|
pub fn write_vectored(&self, bufs: &[IoSlice<'_>]) -> io::Result<usize> {
|
|
|
|
return crate::io::default_write_vectored(|b| self.write(b), bufs);
|
|
|
|
}
|
|
|
|
|
2020-01-03 19:26:05 +00:00
|
|
|
#[inline]
|
2020-03-12 01:02:52 +00:00
|
|
|
pub fn is_write_vectored(&self) -> bool {
|
2021-07-29 17:18:22 +00:00
|
|
|
cfg!(not(target_os = "espidf"))
|
2020-01-03 19:26:05 +00:00
|
|
|
}
|
|
|
|
|
2016-08-15 23:11:33 +00: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 20:58:38 +00: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-08 23:06:55 +00:00
|
|
|
|
2016-10-08 12:32:57 +00: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 20:58:38 +00:00
|
|
|
cvt(pwrite64(
|
2021-07-01 04:44:30 +00:00
|
|
|
self.as_raw_fd(),
|
2019-11-27 18:28:39 +00:00
|
|
|
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,
|
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 20:58:38 +00:00
|
|
|
))
|
2019-11-27 18:28:39 +00:00
|
|
|
.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> {
|
2021-07-01 04:44:30 +00: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 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 {
|
2021-07-01 04:44:30 +00:00
|
|
|
cvt(libc::ioctl(self.as_raw_fd(), libc::FIOCLEX))?;
|
2016-06-24 09:31:58 +00:00
|
|
|
Ok(())
|
2015-04-03 22:30:10 +00:00
|
|
|
}
|
|
|
|
}
|
2019-11-27 18:28:39 +00:00
|
|
|
#[cfg(any(
|
2021-07-29 17:18:22 +00:00
|
|
|
all(target_env = "newlib", not(target_os = "espidf")),
|
2019-11-27 18:28:39 +00:00
|
|
|
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 {
|
2021-07-01 04:44:30 +00:00
|
|
|
let previous = cvt(libc::fcntl(self.as_raw_fd(), libc::F_GETFD))?;
|
2017-02-04 00:10:12 +00:00
|
|
|
let new = previous | libc::FD_CLOEXEC;
|
|
|
|
if new != previous {
|
2021-07-01 04:44:30 +00:00
|
|
|
cvt(libc::fcntl(self.as_raw_fd(), libc::F_SETFD, new))?;
|
2017-02-04 00:10:12 +00:00
|
|
|
}
|
2016-06-24 09:31:58 +00:00
|
|
|
Ok(())
|
2015-10-25 01:51:34 +00:00
|
|
|
}
|
|
|
|
}
|
2021-07-29 17:18:22 +00:00
|
|
|
#[cfg(target_os = "espidf")]
|
|
|
|
pub fn set_cloexec(&self) -> io::Result<()> {
|
|
|
|
// FD_CLOEXEC is not supported in ESP-IDF but there's no need to,
|
|
|
|
// because ESP-IDF does not support spawning processes either.
|
|
|
|
Ok(())
|
|
|
|
}
|
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;
|
2021-07-01 04:44:30 +00:00
|
|
|
cvt(libc::ioctl(self.as_raw_fd(), libc::FIONBIO, &v))?;
|
2017-02-04 00:10:12 +00:00
|
|
|
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 {
|
2021-07-01 04:44:30 +00:00
|
|
|
let previous = cvt(libc::fcntl(self.as_raw_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 {
|
2021-07-01 04:44:30 +00:00
|
|
|
cvt(libc::fcntl(self.as_raw_fd(), libc::F_SETFL, new))?;
|
2017-02-04 00:10:12 +00:00
|
|
|
}
|
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.
|
2021-07-29 17:18:22 +00:00
|
|
|
#[cfg(not(target_os = "espidf"))]
|
|
|
|
let cmd = libc::F_DUPFD_CLOEXEC;
|
|
|
|
|
|
|
|
// For ESP-IDF, F_DUPFD is used instead, because the CLOEXEC semantics
|
|
|
|
// will never be supported, as this is a bare metal framework with
|
|
|
|
// no capabilities for multi-process execution. While F_DUPFD is also
|
|
|
|
// not supported yet, it might be (currently it returns ENOSYS).
|
|
|
|
#[cfg(target_os = "espidf")]
|
|
|
|
let cmd = libc::F_DUPFD;
|
|
|
|
|
2021-07-01 04:44:30 +00:00
|
|
|
let fd = cvt(unsafe { libc::fcntl(self.as_raw_fd(), cmd, 0) })?;
|
|
|
|
Ok(unsafe { FileDesc::from_raw_fd(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
|
|
|
}
|
|
|
|
|
2021-07-01 04:44:30 +00: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 18:28:39 +00:00
|
|
|
}
|
2015-02-06 00:50:11 +00:00
|
|
|
}
|
|
|
|
|
2021-07-01 04:44:30 +00:00
|
|
|
impl FromRawFd for FileDesc {
|
|
|
|
unsafe fn from_raw_fd(raw_fd: RawFd) -> Self {
|
|
|
|
Self(FromRawFd::from_raw_fd(raw_fd))
|
2015-02-03 05:39:14 +00:00
|
|
|
}
|
|
|
|
}
|