summaryrefslogtreecommitdiffstats
path: root/vendor/rustix/src/backend/libc/io/syscalls.rs
diff options
context:
space:
mode:
Diffstat (limited to 'vendor/rustix/src/backend/libc/io/syscalls.rs')
-rw-r--r--vendor/rustix/src/backend/libc/io/syscalls.rs192
1 files changed, 103 insertions, 89 deletions
diff --git a/vendor/rustix/src/backend/libc/io/syscalls.rs b/vendor/rustix/src/backend/libc/io/syscalls.rs
index 03f9294d9..b74a63fa3 100644
--- a/vendor/rustix/src/backend/libc/io/syscalls.rs
+++ b/vendor/rustix/src/backend/libc/io/syscalls.rs
@@ -3,9 +3,12 @@
use super::super::c;
#[cfg(any(target_os = "android", target_os = "linux"))]
use super::super::conv::syscall_ret_owned_fd;
-use super::super::conv::{
- borrowed_fd, ret, ret_c_int, ret_discarded_fd, ret_owned_fd, ret_ssize_t,
-};
+#[cfg(any(
+ target_os = "android",
+ all(target_os = "linux", not(target_env = "gnu")),
+))]
+use super::super::conv::syscall_ret_usize;
+use super::super::conv::{borrowed_fd, ret, ret_c_int, ret_discarded_fd, ret_owned_fd, ret_usize};
use super::super::offset::{libc_pread, libc_pwrite};
#[cfg(not(any(target_os = "haiku", target_os = "redox", target_os = "solaris")))]
use super::super::offset::{libc_preadv, libc_pwritev};
@@ -18,11 +21,18 @@ use crate::io::kqueue::Event;
use crate::io::port::Event;
#[cfg(not(any(target_os = "aix", target_os = "wasi")))]
use crate::io::DupFlags;
+#[cfg(any(
+ target_os = "android",
+ target_os = "freebsd",
+ target_os = "illumos",
+ target_os = "linux"
+))]
+use crate::io::EventfdFlags;
#[cfg(not(any(apple, target_os = "aix", target_os = "haiku", target_os = "wasi")))]
use crate::io::PipeFlags;
use crate::io::{self, FdFlags, IoSlice, IoSliceMut, PollFd};
#[cfg(any(target_os = "android", target_os = "linux"))]
-use crate::io::{EventfdFlags, IoSliceRaw, ReadWriteFlags, SpliceFlags};
+use crate::io::{IoSliceRaw, ReadWriteFlags, SpliceFlags};
use core::cmp::min;
use core::convert::TryInto;
use core::mem::MaybeUninit;
@@ -32,25 +42,23 @@ use core::ptr;
use libc_errno::errno;
pub(crate) fn read(fd: BorrowedFd<'_>, buf: &mut [u8]) -> io::Result<usize> {
- let nread = unsafe {
- ret_ssize_t(c::read(
+ unsafe {
+ ret_usize(c::read(
borrowed_fd(fd),
buf.as_mut_ptr().cast(),
min(buf.len(), READ_LIMIT),
- ))?
- };
- Ok(nread as usize)
+ ))
+ }
}
pub(crate) fn write(fd: BorrowedFd<'_>, buf: &[u8]) -> io::Result<usize> {
- let nwritten = unsafe {
- ret_ssize_t(c::write(
+ unsafe {
+ ret_usize(c::write(
borrowed_fd(fd),
buf.as_ptr().cast(),
min(buf.len(), READ_LIMIT),
- ))?
- };
- Ok(nwritten as usize)
+ ))
+ }
}
pub(crate) fn pread(fd: BorrowedFd<'_>, buf: &mut [u8], offset: u64) -> io::Result<usize> {
@@ -59,15 +67,14 @@ pub(crate) fn pread(fd: BorrowedFd<'_>, buf: &mut [u8], offset: u64) -> io::Resu
// Silently cast; we'll get `EINVAL` if the value is negative.
let offset = offset as i64;
- let nread = unsafe {
- ret_ssize_t(libc_pread(
+ unsafe {
+ ret_usize(libc_pread(
borrowed_fd(fd),
buf.as_mut_ptr().cast(),
len,
offset,
- ))?
- };
- Ok(nread as usize)
+ ))
+ }
}
pub(crate) fn pwrite(fd: BorrowedFd<'_>, buf: &[u8], offset: u64) -> io::Result<usize> {
@@ -76,37 +83,34 @@ pub(crate) fn pwrite(fd: BorrowedFd<'_>, buf: &[u8], offset: u64) -> io::Result<
// Silently cast; we'll get `EINVAL` if the value is negative.
let offset = offset as i64;
- let nwritten = unsafe {
- ret_ssize_t(libc_pwrite(
+ unsafe {
+ ret_usize(libc_pwrite(
borrowed_fd(fd),
buf.as_ptr().cast(),
len,
offset,
- ))?
- };
- Ok(nwritten as usize)
+ ))
+ }
}
pub(crate) fn readv(fd: BorrowedFd<'_>, bufs: &mut [IoSliceMut]) -> io::Result<usize> {
- let nread = unsafe {
- ret_ssize_t(c::readv(
+ unsafe {
+ ret_usize(c::readv(
borrowed_fd(fd),
bufs.as_ptr().cast::<c::iovec>(),
min(bufs.len(), max_iov()) as c::c_int,
- ))?
- };
- Ok(nread as usize)
+ ))
+ }
}
pub(crate) fn writev(fd: BorrowedFd<'_>, bufs: &[IoSlice]) -> io::Result<usize> {
- let nwritten = unsafe {
- ret_ssize_t(c::writev(
+ unsafe {
+ ret_usize(c::writev(
borrowed_fd(fd),
bufs.as_ptr().cast::<c::iovec>(),
min(bufs.len(), max_iov()) as c::c_int,
- ))?
- };
- Ok(nwritten as usize)
+ ))
+ }
}
#[cfg(not(any(target_os = "haiku", target_os = "redox", target_os = "solaris")))]
@@ -117,30 +121,28 @@ pub(crate) fn preadv(
) -> io::Result<usize> {
// Silently cast; we'll get `EINVAL` if the value is negative.
let offset = offset as i64;
- let nread = unsafe {
- ret_ssize_t(libc_preadv(
+ unsafe {
+ ret_usize(libc_preadv(
borrowed_fd(fd),
bufs.as_ptr().cast::<c::iovec>(),
min(bufs.len(), max_iov()) as c::c_int,
offset,
- ))?
- };
- Ok(nread as usize)
+ ))
+ }
}
#[cfg(not(any(target_os = "haiku", target_os = "redox", target_os = "solaris")))]
pub(crate) fn pwritev(fd: BorrowedFd<'_>, bufs: &[IoSlice], offset: u64) -> io::Result<usize> {
// Silently cast; we'll get `EINVAL` if the value is negative.
let offset = offset as i64;
- let nwritten = unsafe {
- ret_ssize_t(libc_pwritev(
+ unsafe {
+ ret_usize(libc_pwritev(
borrowed_fd(fd),
bufs.as_ptr().cast::<c::iovec>(),
min(bufs.len(), max_iov()) as c::c_int,
offset,
- ))?
- };
- Ok(nwritten as usize)
+ ))
+ }
}
#[cfg(all(target_os = "linux", target_env = "gnu"))]
@@ -152,20 +154,19 @@ pub(crate) fn preadv2(
) -> io::Result<usize> {
// Silently cast; we'll get `EINVAL` if the value is negative.
let offset = offset as i64;
- let nread = unsafe {
- ret_ssize_t(libc_preadv2(
+ unsafe {
+ ret_usize(libc_preadv2(
borrowed_fd(fd),
bufs.as_ptr().cast::<c::iovec>(),
min(bufs.len(), max_iov()) as c::c_int,
offset,
flags.bits(),
- ))?
- };
- Ok(nread as usize)
+ ))
+ }
}
/// At present, `libc` only has `preadv2` defined for glibc. On other
-/// ABIs, use `libc::syscall`.
+/// ABIs, use `c::syscall`.
#[cfg(any(
target_os = "android",
all(target_os = "linux", not(target_env = "gnu")),
@@ -179,17 +180,16 @@ pub(crate) fn preadv2(
) -> io::Result<usize> {
// Silently cast; we'll get `EINVAL` if the value is negative.
let offset = offset as i64;
- let nread = unsafe {
- ret_ssize_t(libc::syscall(
- libc::SYS_preadv2,
+ unsafe {
+ syscall_ret_usize(c::syscall(
+ c::SYS_preadv2,
borrowed_fd(fd),
bufs.as_ptr().cast::<c::iovec>(),
min(bufs.len(), max_iov()) as c::c_int,
offset,
flags.bits(),
- ) as c::ssize_t)?
- };
- Ok(nread as usize)
+ ))
+ }
}
#[cfg(all(target_os = "linux", target_env = "gnu"))]
@@ -201,20 +201,19 @@ pub(crate) fn pwritev2(
) -> io::Result<usize> {
// Silently cast; we'll get `EINVAL` if the value is negative.
let offset = offset as i64;
- let nwritten = unsafe {
- ret_ssize_t(libc_pwritev2(
+ unsafe {
+ ret_usize(libc_pwritev2(
borrowed_fd(fd),
bufs.as_ptr().cast::<c::iovec>(),
min(bufs.len(), max_iov()) as c::c_int,
offset,
flags.bits(),
- ))?
- };
- Ok(nwritten as usize)
+ ))
+ }
}
/// At present, `libc` only has `pwritev2` defined for glibc. On other
-/// ABIs, use `libc::syscall`.
+/// ABIs, use `c::syscall`.
#[cfg(any(
target_os = "android",
all(target_os = "linux", not(target_env = "gnu")),
@@ -228,25 +227,24 @@ pub(crate) fn pwritev2(
) -> io::Result<usize> {
// Silently cast; we'll get `EINVAL` if the value is negative.
let offset = offset as i64;
- let nwritten = unsafe {
- ret_ssize_t(libc::syscall(
- libc::SYS_pwritev2,
+ unsafe {
+ syscall_ret_usize(c::syscall(
+ c::SYS_pwritev2,
borrowed_fd(fd),
bufs.as_ptr().cast::<c::iovec>(),
min(bufs.len(), max_iov()) as c::c_int,
offset,
flags.bits(),
- ) as c::ssize_t)?
- };
- Ok(nwritten as usize)
+ ))
+ }
}
// These functions are derived from Rust's library/std/src/sys/unix/fd.rs at
// revision 326ef470a8b379a180d6dc4bbef08990698a737a.
// 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".
+// manual 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
@@ -293,6 +291,11 @@ pub(crate) fn eventfd(initval: u32, flags: EventfdFlags) -> io::Result<OwnedFd>
unsafe { syscall_ret_owned_fd(c::syscall(c::SYS_eventfd2, initval, flags.bits())) }
}
+#[cfg(any(target_os = "freebsd", target_os = "illumos"))]
+pub(crate) fn eventfd(initval: u32, flags: EventfdFlags) -> io::Result<OwnedFd> {
+ unsafe { ret_owned_fd(c::eventfd(initval, flags.bits())) }
+}
+
#[cfg(any(target_os = "android", target_os = "linux"))]
#[inline]
pub(crate) fn ioctl_blksszget(fd: BorrowedFd) -> io::Result<u32> {
@@ -338,13 +341,15 @@ pub(crate) fn ioctl_fionbio(fd: BorrowedFd<'_>, value: bool) -> io::Result<()> {
#[cfg(any(target_os = "android", target_os = "linux"))]
pub(crate) fn ioctl_ficlone(fd: BorrowedFd<'_>, src_fd: BorrowedFd<'_>) -> io::Result<()> {
- // TODO: Enable `ioctl_ficlone` for android when upstream is updated.
- // TODO: Enable `ioctl_ficlone` for more architectures when upstream is
- // updated.
- #[cfg(all(
- target_os = "linux",
- any(target_arch = "x86", target_arch = "x86_64", target_arch = "aarch64")
- ))]
+ // TODO: Enable this on mips and power once libc is updated.
+ #[cfg(not(any(
+ target_arch = "mips",
+ target_arch = "mips64",
+ target_arch = "powerpc",
+ target_arch = "powerpc64",
+ target_arch = "sparc",
+ target_arch = "sparc64"
+ )))]
unsafe {
ret(c::ioctl(
borrowed_fd(fd),
@@ -352,10 +357,15 @@ pub(crate) fn ioctl_ficlone(fd: BorrowedFd<'_>, src_fd: BorrowedFd<'_>) -> io::R
borrowed_fd(src_fd),
))
}
- #[cfg(not(all(
- target_os = "linux",
- any(target_arch = "x86", target_arch = "x86_64", target_arch = "aarch64")
- )))]
+
+ #[cfg(any(
+ target_arch = "mips",
+ target_arch = "mips64",
+ target_arch = "powerpc",
+ target_arch = "powerpc64",
+ target_arch = "sparc",
+ target_arch = "sparc64"
+ ))]
{
let _ = fd;
let _ = src_fd;
@@ -475,7 +485,13 @@ pub(crate) fn dup3(fd: BorrowedFd<'_>, new: &mut OwnedFd, _flags: DupFlags) -> i
#[cfg(apple)]
pub(crate) fn ioctl_fioclex(fd: BorrowedFd<'_>) -> io::Result<()> {
- unsafe { ret(c::ioctl(borrowed_fd(fd), c::FIOCLEX)) }
+ unsafe {
+ ret(c::ioctl(
+ borrowed_fd(fd),
+ c::FIOCLEX,
+ core::ptr::null_mut::<u8>(),
+ ))
+ }
}
#[cfg(not(any(target_os = "haiku", target_os = "redox", target_os = "wasi")))]
@@ -565,17 +581,16 @@ pub fn splice(
.map(|off| (off as *mut u64).cast())
.unwrap_or(ptr::null_mut());
- ret_ssize_t(unsafe {
- c::splice(
+ unsafe {
+ ret_usize(c::splice(
borrowed_fd(fd_in),
off_in,
borrowed_fd(fd_out),
off_out,
len,
flags.bits(),
- )
- })
- .map(|spliced| spliced as usize)
+ ))
+ }
}
#[cfg(any(target_os = "android", target_os = "linux"))]
@@ -585,13 +600,12 @@ pub unsafe fn vmsplice(
bufs: &[IoSliceRaw],
flags: SpliceFlags,
) -> io::Result<usize> {
- ret_ssize_t(c::vmsplice(
+ ret_usize(c::vmsplice(
borrowed_fd(fd),
bufs.as_ptr().cast::<c::iovec>(),
min(bufs.len(), max_iov()),
flags.bits(),
))
- .map(|spliced| spliced as usize)
}
#[cfg(solarish)]