Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -274,3 +274,6 @@ check-cfg = [
'cfg(target_arch, values("xtensa"))',
'cfg(target_os, values("cygwin"))',
]

[patch.crates-io]
libc = { git = "https://github.com/rust-lang/libc", branch = "libc-0.2" }
6 changes: 3 additions & 3 deletions src/backend/libc/event/syscalls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -572,7 +572,7 @@ pub(crate) unsafe fn epoll_wait(
if let Some(epoll_pwait2_func) = epoll_pwait2.get() {
return ret_u32(epoll_pwait2_func(
borrowed_fd(epoll),
events.0.cast::<c::epoll_event>(),
events.0.cast(),
events.1.try_into().unwrap_or(i32::MAX),
crate::utils::option_as_ptr(timeout).cast(),
null(),
Expand All @@ -596,7 +596,7 @@ pub(crate) unsafe fn epoll_wait(

ret_u32(epoll_pwait2(
borrowed_fd(epoll),
events.0.cast::<c::epoll_event>(),
events.0.cast(),
events.1.try_into().unwrap_or(i32::MAX),
crate::utils::option_as_ptr(timeout).cast(),
null(),
Expand All @@ -614,7 +614,7 @@ pub(crate) unsafe fn epoll_wait(

ret_u32(c::epoll_wait(
borrowed_fd(epoll),
events.0.cast::<c::epoll_event>(),
events.0.cast(),
events.1.try_into().unwrap_or(i32::MAX),
timeout,
))
Expand Down
51 changes: 19 additions & 32 deletions src/backend/libc/fs/syscalls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,7 @@ pub(crate) fn getdents_uninit(
unsafe {
ret_usize(getdents64(
borrowed_fd(fd),
buf.as_mut_ptr().cast::<c::c_void>(),
buf.as_mut_ptr().cast(),
buf.len(),
))
}
Expand Down Expand Up @@ -2387,7 +2387,7 @@ pub(crate) unsafe fn getxattr(
ret_usize(c::getxattr(
path.as_ptr(),
name.as_ptr(),
value.0.cast::<c::c_void>(),
value.0.cast(),
value.1,
))
}
Expand All @@ -2399,7 +2399,7 @@ pub(crate) unsafe fn getxattr(
let ptr = if value.1 == 0 {
core::ptr::null_mut()
} else {
value.0.cast::<c::c_void>()
value.0.cast()
};
ret_usize(c::getxattr(
path.as_ptr(),
Expand All @@ -2423,7 +2423,7 @@ pub(crate) unsafe fn lgetxattr(
ret_usize(c::lgetxattr(
path.as_ptr(),
name.as_ptr(),
value.0.cast::<c::c_void>(),
value.0.cast(),
value.1,
))
}
Expand All @@ -2435,7 +2435,7 @@ pub(crate) unsafe fn lgetxattr(
let ptr = if value.1 == 0 {
core::ptr::null_mut()
} else {
value.0.cast::<c::c_void>()
value.0.cast()
};

ret_usize(c::getxattr(
Expand All @@ -2460,7 +2460,7 @@ pub(crate) unsafe fn fgetxattr(
ret_usize(c::fgetxattr(
borrowed_fd(fd),
name.as_ptr(),
value.0.cast::<c::c_void>(),
value.0.cast(),
value.1,
))
}
Expand All @@ -2472,7 +2472,7 @@ pub(crate) unsafe fn fgetxattr(
let ptr = if value.1 == 0 {
core::ptr::null_mut()
} else {
value.0.cast::<c::c_void>()
value.0.cast()
};
ret_usize(c::fgetxattr(
borrowed_fd(fd),
Expand All @@ -2497,7 +2497,7 @@ pub(crate) fn setxattr(
ret(c::setxattr(
path.as_ptr(),
name.as_ptr(),
value.as_ptr().cast::<c::c_void>(),
value.as_ptr().cast(),
value.len(),
flags.bits() as i32,
))
Expand All @@ -2508,7 +2508,7 @@ pub(crate) fn setxattr(
ret(c::setxattr(
path.as_ptr(),
name.as_ptr(),
value.as_ptr().cast::<c::c_void>(),
value.as_ptr().cast(),
value.len(),
0,
flags.bits() as i32,
Expand All @@ -2528,7 +2528,7 @@ pub(crate) fn lsetxattr(
ret(c::lsetxattr(
path.as_ptr(),
name.as_ptr(),
value.as_ptr().cast::<c::c_void>(),
value.as_ptr().cast(),
value.len(),
flags.bits() as i32,
))
Expand All @@ -2539,7 +2539,7 @@ pub(crate) fn lsetxattr(
ret(c::setxattr(
path.as_ptr(),
name.as_ptr(),
value.as_ptr().cast::<c::c_void>(),
value.as_ptr().cast(),
value.len(),
0,
flags.bits() as i32 | c::XATTR_NOFOLLOW,
Expand All @@ -2559,7 +2559,7 @@ pub(crate) fn fsetxattr(
ret(c::fsetxattr(
borrowed_fd(fd),
name.as_ptr(),
value.as_ptr().cast::<c::c_void>(),
value.as_ptr().cast(),
value.len(),
flags.bits() as i32,
))
Expand All @@ -2570,7 +2570,7 @@ pub(crate) fn fsetxattr(
ret(c::fsetxattr(
borrowed_fd(fd),
name.as_ptr(),
value.as_ptr().cast::<c::c_void>(),
value.as_ptr().cast(),
value.len(),
0,
flags.bits() as i32,
Expand All @@ -2582,40 +2582,27 @@ pub(crate) fn fsetxattr(
pub(crate) unsafe fn listxattr(path: &CStr, list: (*mut u8, usize)) -> io::Result<usize> {
#[cfg(not(apple))]
{
ret_usize(c::listxattr(
path.as_ptr(),
list.0.cast::<ffi::c_char>(),
list.1,
))
ret_usize(c::listxattr(path.as_ptr(), list.0.cast(), list.1))
}

#[cfg(apple)]
{
ret_usize(c::listxattr(
path.as_ptr(),
list.0.cast::<ffi::c_char>(),
list.1,
0,
))
ret_usize(c::listxattr(path.as_ptr(), list.0.cast(), list.1, 0))
}
}

#[cfg(any(apple, linux_kernel, target_os = "hurd"))]
pub(crate) unsafe fn llistxattr(path: &CStr, list: (*mut u8, usize)) -> io::Result<usize> {
#[cfg(not(apple))]
{
ret_usize(c::llistxattr(
path.as_ptr(),
list.0.cast::<ffi::c_char>(),
list.1,
))
ret_usize(c::llistxattr(path.as_ptr(), list.0.cast(), list.1))
}

#[cfg(apple)]
{
ret_usize(c::listxattr(
path.as_ptr(),
list.0.cast::<ffi::c_char>(),
list.0.cast(),
list.1,
c::XATTR_NOFOLLOW,
))
Expand All @@ -2628,12 +2615,12 @@ pub(crate) unsafe fn flistxattr(fd: BorrowedFd<'_>, list: (*mut u8, usize)) -> i

#[cfg(not(apple))]
{
ret_usize(c::flistxattr(fd, list.0.cast::<ffi::c_char>(), list.1))
ret_usize(c::flistxattr(fd, list.0.cast(), list.1))
}

#[cfg(apple)]
{
ret_usize(c::flistxattr(fd, list.0.cast::<ffi::c_char>(), list.1, 0))
ret_usize(c::flistxattr(fd, list.0.cast(), list.1, 0))
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/backend/libc/io/errno.rs
Original file line number Diff line number Diff line change
Expand Up @@ -791,7 +791,7 @@ impl Errno {
#[cfg(not(target_os = "l4re"))]
pub const NOTSOCK: Self = Self(c::ENOTSOCK);
/// `ENOTSUP`
#[cfg(not(any(windows, target_os = "redox")))]
#[cfg(not(any(windows, target_os = "hermit", target_os = "redox")))]
pub const NOTSUP: Self = Self(c::ENOTSUP);
/// `ENOTTY`
#[cfg(not(windows))]
Expand Down
27 changes: 17 additions & 10 deletions src/backend/libc/io/syscalls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ pub(crate) fn write(fd: BorrowedFd<'_>, buf: &[u8]) -> io::Result<usize> {
}
}

#[cfg(not(target_os = "hermit"))]
pub(crate) unsafe fn pread(
fd: BorrowedFd<'_>,
buf: (*mut u8, usize),
Expand All @@ -59,6 +60,7 @@ pub(crate) unsafe fn pread(
ret_usize(c::pread(borrowed_fd(fd), buf.0.cast(), len, offset))
}

#[cfg(not(target_os = "hermit"))]
pub(crate) fn pwrite(fd: BorrowedFd<'_>, buf: &[u8], offset: u64) -> io::Result<usize> {
let len = min(buf.len(), READ_LIMIT);

Expand All @@ -76,8 +78,8 @@ pub(crate) fn readv(fd: BorrowedFd<'_>, bufs: &mut [IoSliceMut<'_>]) -> io::Resu
unsafe {
ret_usize(c::readv(
borrowed_fd(fd),
bufs.as_ptr().cast::<c::iovec>(),
min(bufs.len(), MAX_IOV) as c::c_int,
bufs.as_ptr().cast(),
min(bufs.len(), MAX_IOV) as _,
))
}
}
Expand All @@ -87,8 +89,8 @@ pub(crate) fn writev(fd: BorrowedFd<'_>, bufs: &[IoSlice<'_>]) -> io::Result<usi
unsafe {
ret_usize(c::writev(
borrowed_fd(fd),
bufs.as_ptr().cast::<c::iovec>(),
min(bufs.len(), MAX_IOV) as c::c_int,
bufs.as_ptr().cast(),
min(bufs.len(), MAX_IOV) as _,
))
}
}
Expand All @@ -97,6 +99,7 @@ pub(crate) fn writev(fd: BorrowedFd<'_>, bufs: &[IoSlice<'_>]) -> io::Result<usi
target_os = "cygwin",
target_os = "espidf",
target_os = "haiku",
target_os = "hermit",
target_os = "horizon",
target_os = "nto",
target_os = "redox",
Expand All @@ -117,7 +120,7 @@ pub(crate) fn preadv(
unsafe {
ret_usize(c::preadv(
borrowed_fd(fd),
bufs.as_ptr().cast::<c::iovec>(),
bufs.as_ptr().cast(),
min(bufs.len(), MAX_IOV) as c::c_int,
offset,
))
Expand All @@ -128,6 +131,7 @@ pub(crate) fn preadv(
target_os = "cygwin",
target_os = "espidf",
target_os = "haiku",
target_os = "hermit",
target_os = "nto",
target_os = "horizon",
target_os = "redox",
Expand All @@ -144,7 +148,7 @@ pub(crate) fn pwritev(fd: BorrowedFd<'_>, bufs: &[IoSlice<'_>], offset: u64) ->
unsafe {
ret_usize(c::pwritev(
borrowed_fd(fd),
bufs.as_ptr().cast::<c::iovec>(),
bufs.as_ptr().cast(),
min(bufs.len(), MAX_IOV) as c::c_int,
offset,
))
Expand All @@ -163,7 +167,7 @@ pub(crate) fn preadv2(
unsafe {
ret_usize(c::preadv2(
borrowed_fd(fd),
bufs.as_ptr().cast::<c::iovec>(),
bufs.as_ptr().cast(),
min(bufs.len(), MAX_IOV) as c::c_int,
offset,
bitflags_bits!(flags),
Expand All @@ -183,7 +187,7 @@ pub(crate) fn pwritev2(
unsafe {
ret_usize(c::pwritev2(
borrowed_fd(fd),
bufs.as_ptr().cast::<c::iovec>(),
bufs.as_ptr().cast(),
min(bufs.len(), MAX_IOV) as c::c_int,
offset,
bitflags_bits!(flags),
Expand Down Expand Up @@ -234,16 +238,18 @@ pub(crate) unsafe fn ioctl_readonly(
ioctl(fd, request, arg)
}

#[cfg(not(target_os = "hermit"))]
pub(crate) fn fcntl_getfd(fd: BorrowedFd<'_>) -> io::Result<FdFlags> {
let flags = unsafe { ret_c_int(c::fcntl(borrowed_fd(fd), c::F_GETFD))? };
Ok(FdFlags::from_bits_retain(bitcast!(flags)))
}

#[cfg(not(target_os = "hermit"))]
pub(crate) fn fcntl_setfd(fd: BorrowedFd<'_>, flags: FdFlags) -> io::Result<()> {
unsafe { ret(c::fcntl(borrowed_fd(fd), c::F_SETFD, flags.bits())) }
}

#[cfg(not(any(target_os = "espidf", target_os = "wasi")))]
#[cfg(not(any(target_os = "espidf", target_os = "hermit", target_os = "wasi")))]
pub(crate) fn fcntl_dupfd_cloexec(fd: BorrowedFd<'_>, min: RawFd) -> io::Result<OwnedFd> {
unsafe { ret_owned_fd(c::fcntl(borrowed_fd(fd), c::F_DUPFD_CLOEXEC, min)) }
}
Expand All @@ -259,7 +265,7 @@ pub(crate) fn dup(fd: BorrowedFd<'_>) -> io::Result<OwnedFd> {
}

#[allow(clippy::needless_pass_by_ref_mut)]
#[cfg(not(target_os = "wasi"))]
#[cfg(not(any(target_os = "hermit", target_os = "wasi")))]
pub(crate) fn dup2(fd: BorrowedFd<'_>, new: &mut OwnedFd) -> io::Result<()> {
unsafe { ret_discarded_fd(c::dup2(borrowed_fd(fd), borrowed_fd(new.as_fd()))) }
}
Expand All @@ -272,6 +278,7 @@ pub(crate) fn dup2(fd: BorrowedFd<'_>, new: &mut OwnedFd) -> io::Result<()> {
target_os = "dragonfly",
target_os = "espidf",
target_os = "haiku",
target_os = "hermit",
target_os = "horizon",
target_os = "nto",
target_os = "redox",
Expand Down
1 change: 1 addition & 0 deletions src/backend/libc/io/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ bitflags! {
apple,
target_os = "aix",
target_os = "android",
target_os = "hermit",
target_os = "redox",
)))] // Android 5.0 has dup3, but libc doesn't have bindings
const CLOEXEC = bitcast!(c::O_CLOEXEC);
Expand Down
4 changes: 2 additions & 2 deletions src/backend/libc/net/addr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,7 @@ impl SocketAddrStorage {
// SAFETY: `self.0` is a `sockaddr_storage` so it has enough space.
unsafe {
AddressFamily::from_raw(crate::backend::net::read_sockaddr::read_sa_family(
crate::utils::as_ptr(&self.0).cast::<c::sockaddr>(),
crate::utils::as_ptr(&self.0).cast(),
))
}
}
Expand All @@ -332,7 +332,7 @@ impl SocketAddrStorage {
// SAFETY: `self.0` is a `sockaddr_storage` so it has enough space.
unsafe {
crate::backend::net::read_sockaddr::initialize_family_to_unspec(
crate::utils::as_mut_ptr(&mut self.0).cast::<c::sockaddr>(),
crate::utils::as_mut_ptr(&mut self.0).cast(),
)
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/backend/libc/net/read_sockaddr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ pub(crate) unsafe fn sockaddr_nonempty(storage: *const c::sockaddr, len: SocketA
}

assert!(len as usize >= size_of::<c::sa_family_t>());
let family: c::c_int = read_sa_family(storage.cast::<c::sockaddr>()).into();
let family: c::c_int = read_sa_family(storage.cast()).into();
if family == c::AF_UNSPEC {
return false;
}
Expand Down Expand Up @@ -240,7 +240,7 @@ pub(crate) fn read_sockaddr_xdp(addr: &SocketAddrAny) -> Result<SocketAddrXdp, E
return Err(Errno::AFNOSUPPORT);
}
assert!(addr.addr_len() as usize >= size_of::<c::sockaddr_xdp>());
let decode = unsafe { &*addr.as_ptr().cast::<c::sockaddr_xdp>() };
let decode: &c::sockaddr_xdp = unsafe { &*addr.as_ptr().cast() };

// This ignores the `sxdp_shared_umem_fd` field, which is only expected to
// be significant in `bind` calls, and not returned from `acceptfrom` or
Expand All @@ -259,6 +259,6 @@ pub(crate) fn read_sockaddr_netlink(addr: &SocketAddrAny) -> Result<SocketAddrNe
return Err(Errno::AFNOSUPPORT);
}
assert!(addr.addr_len() as usize >= size_of::<c::sockaddr_nl>());
let decode = unsafe { &*addr.as_ptr().cast::<c::sockaddr_nl>() };
let decode: &c::sockaddr_nl = unsafe { &*addr.as_ptr().cast() };
Ok(SocketAddrNetlink::new(decode.nl_pid, decode.nl_groups))
}
Loading
Loading