diff --git a/src/backend/libc/event/poll_fd.rs b/src/backend/libc/event/poll_fd.rs index 1b0d4ae1f..d9c16ad18 100644 --- a/src/backend/libc/event/poll_fd.rs +++ b/src/backend/libc/event/poll_fd.rs @@ -3,6 +3,7 @@ use crate::backend::conv::borrowed_fd; use crate::backend::fd::{AsFd, AsRawFd, BorrowedFd, LibcFd}; #[cfg(windows)] use crate::backend::fd::{AsSocket, RawFd}; +use crate::ffi; use bitflags::bitflags; use core::fmt; use core::marker::PhantomData; @@ -13,7 +14,7 @@ bitflags! { /// [`poll`]: crate::event::poll #[repr(transparent)] #[derive(Copy, Clone, Eq, PartialEq, Hash, Debug)] - pub struct PollFlags: c::c_short { + pub struct PollFlags: ffi::c_short { /// `POLLIN` const IN = c::POLLIN; /// `POLLPRI` diff --git a/src/backend/libc/fs/syscalls.rs b/src/backend/libc/fs/syscalls.rs index 74a9865cc..d81c67a79 100644 --- a/src/backend/libc/fs/syscalls.rs +++ b/src/backend/libc/fs/syscalls.rs @@ -9,9 +9,9 @@ use crate::backend::c; use crate::backend::conv::ret_usize; use crate::backend::conv::{borrowed_fd, c_str, ret, ret_c_int, ret_off_t, ret_owned_fd}; use crate::fd::{BorrowedFd, OwnedFd}; -use crate::ffi::CStr; #[cfg(all(apple, feature = "alloc"))] use crate::ffi::CString; +use crate::ffi::{self, CStr}; #[cfg(not(any(target_os = "espidf", target_os = "vita")))] use crate::fs::Access; #[cfg(not(any( @@ -88,7 +88,7 @@ use { }; #[cfg(all(target_env = "gnu", fix_y2038))] -weak!(fn __utimensat64(c::c_int, *const c::c_char, *const LibcTimespec, c::c_int) -> c::c_int); +weak!(fn __utimensat64(c::c_int, *const ffi::c_char, *const LibcTimespec, c::c_int) -> c::c_int); #[cfg(all(target_env = "gnu", fix_y2038))] weak!(fn __futimens64(c::c_int, *const LibcTimespec) -> c::c_int); @@ -121,7 +121,7 @@ fn open_via_syscall(path: &CStr, oflags: OFlags, mode: Mode) -> io::Result c::c_int @@ -181,7 +181,7 @@ fn openat_via_syscall( syscall! { fn openat( base_dirfd: c::c_int, - pathname: *const c::c_char, + pathname: *const ffi::c_char, oflags: c::c_int, mode: c::mode_t ) via SYS_openat -> c::c_int @@ -276,9 +276,11 @@ pub(crate) fn statvfs(filename: &CStr) -> io::Result { #[inline] pub(crate) fn readlink(path: &CStr, buf: &mut [u8]) -> io::Result { unsafe { - ret_usize( - c::readlink(c_str(path), buf.as_mut_ptr().cast::(), buf.len()) as isize, - ) + ret_usize(c::readlink( + c_str(path), + buf.as_mut_ptr().cast::(), + buf.len(), + ) as isize) } } @@ -293,7 +295,7 @@ pub(crate) fn readlinkat( ret_usize(c::readlinkat( borrowed_fd(dirfd), c_str(path), - buf.as_mut_ptr().cast::(), + buf.as_mut_ptr().cast::(), buf.len(), ) as isize) } @@ -353,9 +355,9 @@ pub(crate) fn linkat( weak! { fn linkat( c::c_int, - *const c::c_char, + *const ffi::c_char, c::c_int, - *const c::c_char, + *const ffi::c_char, c::c_int ) -> c::c_int } @@ -410,7 +412,7 @@ pub(crate) fn unlinkat(dirfd: BorrowedFd<'_>, path: &CStr, flags: AtFlags) -> io weak! { fn unlinkat( c::c_int, - *const c::c_char, + *const ffi::c_char, c::c_int ) -> c::c_int } @@ -463,9 +465,9 @@ pub(crate) fn renameat( weak! { fn renameat( c::c_int, - *const c::c_char, + *const ffi::c_char, c::c_int, - *const c::c_char + *const ffi::c_char ) -> c::c_int } // If we have `renameat`, use it. @@ -507,9 +509,9 @@ pub(crate) fn renameat2( weak_or_syscall! { fn renameat2( olddirfd: c::c_int, - oldpath: *const c::c_char, + oldpath: *const ffi::c_char, newdirfd: c::c_int, - newpath: *const c::c_char, + newpath: *const ffi::c_char, flags: c::c_uint ) via SYS_renameat2 -> c::c_int } @@ -546,9 +548,9 @@ pub(crate) fn renameat2( syscall! { fn renameat2( olddirfd: c::c_int, - oldpath: *const c::c_char, + oldpath: *const ffi::c_char, newdirfd: c::c_int, - newpath: *const c::c_char, + newpath: *const ffi::c_char, flags: c::c_uint ) via SYS_renameat2 -> c::c_int } @@ -744,7 +746,7 @@ pub(crate) fn accessat( weak! { fn faccessat( c::c_int, - *const c::c_char, + *const ffi::c_char, c::c_int, c::c_int ) -> c::c_int @@ -851,14 +853,14 @@ pub(crate) fn utimensat( weak! { fn utimensat( c::c_int, - *const c::c_char, + *const ffi::c_char, *const c::timespec, c::c_int ) -> c::c_int } extern "C" { fn setattrlist( - path: *const c::c_char, + path: *const ffi::c_char, attr_list: *const Attrlist, attr_buf: *const c::c_void, attr_buf_size: c::size_t, @@ -1045,7 +1047,7 @@ pub(crate) fn chmodat( syscall! { fn fchmodat( base_dirfd: c::c_int, - pathname: *const c::c_char, + pathname: *const ffi::c_char, mode: c::mode_t ) via SYS_fchmodat -> c::c_int } @@ -1075,7 +1077,7 @@ pub(crate) fn fclonefileat( fn fclonefileat( srcfd: BorrowedFd<'_>, dst_dirfd: BorrowedFd<'_>, - dst: *const c::c_char, + dst: *const ffi::c_char, flags: c::c_int ) via SYS_fclonefileat -> c::c_int } @@ -1714,7 +1716,7 @@ pub(crate) fn memfd_create(name: &CStr, flags: MemfdFlags) -> io::Result c::c_int } @@ -1722,7 +1724,7 @@ pub(crate) fn memfd_create(name: &CStr, flags: MemfdFlags) -> io::Result c::c_int } @@ -1743,7 +1745,7 @@ pub(crate) fn openat2( syscall! { fn openat2( base_dirfd: c::c_int, - pathname: *const c::c_char, + pathname: *const ffi::c_char, how: *mut open_how, size: usize ) via SYS_OPENAT2 -> c::c_int @@ -1911,12 +1913,12 @@ fn stat64_to_stat(s64: c::stat64) -> io::Result { #[cfg(linux_kernel)] #[allow(non_upper_case_globals)] mod sys { - use super::{c, BorrowedFd, Statx}; + use super::{c, ffi, BorrowedFd, Statx}; weak_or_syscall! { pub(super) fn statx( dirfd_: BorrowedFd<'_>, - path: *const c::c_char, + path: *const ffi::c_char, flags: c::c_int, mask: c::c_uint, buf: *mut Statx @@ -2443,7 +2445,7 @@ pub(crate) fn fsetxattr( } #[cfg(any(apple, linux_kernel, target_os = "hurd"))] -pub(crate) fn listxattr(path: &CStr, list: &mut [c::c_char]) -> io::Result { +pub(crate) fn listxattr(path: &CStr, list: &mut [ffi::c_char]) -> io::Result { #[cfg(not(apple))] unsafe { ret_usize(c::listxattr(path.as_ptr(), list.as_mut_ptr(), list.len())) @@ -2461,7 +2463,7 @@ pub(crate) fn listxattr(path: &CStr, list: &mut [c::c_char]) -> io::Result io::Result { +pub(crate) fn llistxattr(path: &CStr, list: &mut [ffi::c_char]) -> io::Result { #[cfg(not(apple))] unsafe { ret_usize(c::llistxattr(path.as_ptr(), list.as_mut_ptr(), list.len())) @@ -2479,7 +2481,7 @@ pub(crate) fn llistxattr(path: &CStr, list: &mut [c::c_char]) -> io::Result, list: &mut [c::c_char]) -> io::Result { +pub(crate) fn flistxattr(fd: BorrowedFd<'_>, list: &mut [ffi::c_char]) -> io::Result { let fd = borrowed_fd(fd); #[cfg(not(apple))] diff --git a/src/backend/libc/fs/types.rs b/src/backend/libc/fs/types.rs index 8d70615cc..aa9dce9c1 100644 --- a/src/backend/libc/fs/types.rs +++ b/src/backend/libc/fs/types.rs @@ -1,4 +1,5 @@ use crate::backend::c; +use crate::ffi; use bitflags::bitflags; #[cfg(not(any(target_os = "espidf", target_os = "vita")))] @@ -8,7 +9,7 @@ bitflags! { /// [`accessat`]: fn.accessat.html #[repr(transparent)] #[derive(Copy, Clone, Eq, PartialEq, Hash, Debug)] - pub struct Access: c::c_int { + pub struct Access: ffi::c_int { /// `R_OK` const READ_OK = c::R_OK; @@ -379,7 +380,7 @@ bitflags! { /// [`fcopyfile`]: crate::fs::fcopyfile #[repr(transparent)] #[derive(Copy, Clone, Eq, PartialEq, Hash, Debug)] - pub struct CopyfileFlags: c::c_uint { + pub struct CopyfileFlags: ffi::c_uint { /// `COPYFILE_ACL` const ACL = copyfile::ACL; @@ -444,7 +445,7 @@ bitflags! { /// [`renameat_with`]: crate::fs::renameat_with #[repr(transparent)] #[derive(Copy, Clone, Eq, PartialEq, Hash, Debug)] - pub struct RenameFlags: c::c_uint { + pub struct RenameFlags: ffi::c_uint { /// `RENAME_EXCHANGE` const EXCHANGE = bitcast!(c::RENAME_EXCHANGE); @@ -599,7 +600,7 @@ bitflags! { /// [`memfd_create`]: crate::fs::memfd_create #[repr(transparent)] #[derive(Copy, Clone, Eq, PartialEq, Hash, Debug)] - pub struct MemfdFlags: c::c_uint { + pub struct MemfdFlags: ffi::c_uint { /// `MFD_CLOEXEC` const CLOEXEC = c::MFD_CLOEXEC; @@ -1161,7 +1162,7 @@ pub type RawMode = c::mode_t; /// `mode_t` #[cfg(all(target_os = "android", target_pointer_width = "32"))] -pub type RawMode = c::c_uint; +pub type RawMode = ffi::c_uint; /// `dev_t` #[cfg(not(all(target_os = "android", target_pointer_width = "32")))] @@ -1169,7 +1170,7 @@ pub type Dev = c::dev_t; /// `dev_t` #[cfg(all(target_os = "android", target_pointer_width = "32"))] -pub type Dev = c::c_ulonglong; +pub type Dev = ffi::c_ulonglong; /// `__fsword_t` #[cfg(all( diff --git a/src/backend/libc/mount/types.rs b/src/backend/libc/mount/types.rs index e1d98ca71..c39405a49 100644 --- a/src/backend/libc/mount/types.rs +++ b/src/backend/libc/mount/types.rs @@ -1,4 +1,5 @@ use crate::backend::c; +use crate::ffi; use bitflags::bitflags; #[cfg(linux_kernel)] @@ -8,7 +9,7 @@ bitflags! { /// [`mount`]: crate::mount::mount #[repr(transparent)] #[derive(Copy, Clone, Eq, PartialEq, Hash, Debug)] - pub struct MountFlags: c::c_ulong { + pub struct MountFlags: ffi::c_ulong { /// `MS_BIND` const BIND = c::MS_BIND; @@ -90,7 +91,7 @@ bitflags! { /// [`fsopen`]: crate::mount::fsopen #[repr(transparent)] #[derive(Copy, Clone, Eq, PartialEq, Hash, Debug)] - pub struct FsOpenFlags: c::c_uint { + pub struct FsOpenFlags: ffi::c_uint { /// `FSOPEN_CLOEXEC` const FSOPEN_CLOEXEC = 0x0000_0001; @@ -107,7 +108,7 @@ bitflags! { /// [`fsmount`]: crate::mount::fsmount #[repr(transparent)] #[derive(Copy, Clone, Eq, PartialEq, Hash, Debug)] - pub struct FsMountFlags: c::c_uint { + pub struct FsMountFlags: ffi::c_uint { /// `FSMOUNT_CLOEXEC` const FSMOUNT_CLOEXEC = 0x0000_0001; @@ -155,7 +156,7 @@ bitflags! { /// [`fsmount`]: crate::mount::fsmount #[repr(transparent)] #[derive(Copy, Clone, Eq, PartialEq, Hash, Debug)] - pub struct MountAttrFlags: c::c_uint { + pub struct MountAttrFlags: ffi::c_uint { /// `MOUNT_ATTR_RDONLY` const MOUNT_ATTR_RDONLY = 0x0000_0001; @@ -205,7 +206,7 @@ bitflags! { /// [`move_mount`]: crate::mount::move_mount #[repr(transparent)] #[derive(Copy, Clone, Eq, PartialEq, Hash, Debug)] - pub struct MoveMountFlags: c::c_uint { + pub struct MoveMountFlags: ffi::c_uint { /// `MOVE_MOUNT_F_EMPTY_PATH` const MOVE_MOUNT_F_SYMLINKS = 0x0000_0001; @@ -246,7 +247,7 @@ bitflags! { /// [`open_tree`]: crate::mount::open_tree #[repr(transparent)] #[derive(Copy, Clone, Eq, PartialEq, Hash, Debug)] - pub struct OpenTreeFlags: c::c_uint { + pub struct OpenTreeFlags: ffi::c_uint { /// `OPENTREE_CLONE` const OPEN_TREE_CLONE = 1; @@ -278,7 +279,7 @@ bitflags! { /// [`fspick`]: crate::mount::fspick #[repr(transparent)] #[derive(Copy, Clone, Eq, PartialEq, Hash, Debug)] - pub struct FsPickFlags: c::c_uint { + pub struct FsPickFlags: ffi::c_uint { /// `FSPICK_CLOEXEC` const FSPICK_CLOEXEC = 0x0000_0001; @@ -303,7 +304,7 @@ bitflags! { /// [`mount_change`]: crate::mount::mount_change #[repr(transparent)] #[derive(Copy, Clone, Eq, PartialEq, Hash, Debug)] - pub struct MountPropagationFlags: c::c_ulong { + pub struct MountPropagationFlags: ffi::c_ulong { /// `MS_SILENT` const SILENT = c::MS_SILENT; /// `MS_SHARED` diff --git a/src/backend/libc/pipe/types.rs b/src/backend/libc/pipe/types.rs index 19c3e982c..8117443e4 100644 --- a/src/backend/libc/pipe/types.rs +++ b/src/backend/libc/pipe/types.rs @@ -1,4 +1,6 @@ #[cfg(linux_kernel)] +use crate::ffi; +#[cfg(linux_kernel)] use core::marker::PhantomData; #[cfg(not(any(apple, target_os = "wasi")))] use {crate::backend::c, bitflags::bitflags}; @@ -43,7 +45,7 @@ bitflags! { /// [`tee`]: crate::pipe::tee #[repr(transparent)] #[derive(Copy, Clone, Eq, PartialEq, Hash, Debug)] - pub struct SpliceFlags: c::c_uint { + pub struct SpliceFlags: ffi::c_uint { /// `SPLICE_F_MOVE` const MOVE = c::SPLICE_F_MOVE; /// `SPLICE_F_NONBLOCK` diff --git a/src/backend/libc/termios/mod.rs b/src/backend/libc/termios/mod.rs index ef944f04d..1e0181a99 100644 --- a/src/backend/libc/termios/mod.rs +++ b/src/backend/libc/termios/mod.rs @@ -1 +1,2 @@ pub(crate) mod syscalls; +pub(crate) mod types; diff --git a/src/backend/libc/termios/types.rs b/src/backend/libc/termios/types.rs new file mode 100644 index 000000000..ed9a8a3f6 --- /dev/null +++ b/src/backend/libc/termios/types.rs @@ -0,0 +1,15 @@ +#![allow(non_camel_case_types)] + +#[cfg(not(target_os = "redox"))] +use crate::ffi; + +// We don't want to use tcflag_t directly so we don't expose libc +// publicly. Redox uses u32, apple uses c_ulong, everything else +// seems to use c_uint. + +#[cfg(apple)] +pub type tcflag_t = ffi::c_ulong; +#[cfg(target_os = "redox")] +pub type tcflag_t = u32; +#[cfg(not(any(target_os = "wasi", apple, target_os = "redox")))] +pub type tcflag_t = ffi::c_uint; diff --git a/src/backend/libc/thread/syscalls.rs b/src/backend/libc/thread/syscalls.rs index 9ac228bfb..a6ee1d441 100644 --- a/src/backend/libc/thread/syscalls.rs +++ b/src/backend/libc/thread/syscalls.rs @@ -25,6 +25,8 @@ use crate::thread::{Cpuid, MembarrierCommand, MembarrierQuery}; use crate::thread::{NanosleepRelativeResult, Timespec}; #[cfg(all(target_env = "gnu", fix_y2038))] use crate::timespec::LibcTimespec; +#[cfg(not(fix_y2038))] +use crate::timespec::{as_libc_timespec_mut_ptr, as_libc_timespec_ptr}; #[cfg(all( linux_kernel, target_pointer_width = "32", @@ -95,7 +97,12 @@ pub(crate) fn clock_nanosleep_relative(id: ClockId, request: &Timespec) -> Nanos let flags = 0; let mut remain = MaybeUninit::::uninit(); - match c::clock_nanosleep(id as c::clockid_t, flags, request, remain.as_mut_ptr()) { + match c::clock_nanosleep( + id as c::clockid_t, + flags, + as_libc_timespec_ptr(request), + as_libc_timespec_mut_ptr(&mut remain), + ) { 0 => NanosleepRelativeResult::Ok, err if err == io::Errno::INTR.0 => { NanosleepRelativeResult::Interrupted(remain.assume_init()) @@ -199,7 +206,7 @@ pub(crate) fn clock_nanosleep_absolute(id: ClockId, request: &Timespec) -> io::R c::clock_nanosleep( id as c::clockid_t, flags as _, - request, + as_libc_timespec_ptr(request), core::ptr::null_mut(), ) } { @@ -267,7 +274,10 @@ pub(crate) fn nanosleep(request: &Timespec) -> NanosleepRelativeResult { unsafe { let mut remain = MaybeUninit::::uninit(); - match ret(c::nanosleep(request, remain.as_mut_ptr())) { + match ret(c::nanosleep( + as_libc_timespec_ptr(request), + as_libc_timespec_mut_ptr(&mut remain), + )) { Ok(()) => NanosleepRelativeResult::Ok, Err(io::Errno::INTR) => NanosleepRelativeResult::Interrupted(remain.assume_init()), Err(err) => NanosleepRelativeResult::Err(err), diff --git a/src/backend/libc/time/syscalls.rs b/src/backend/libc/time/syscalls.rs index 97eae5fd7..ed1e745c8 100644 --- a/src/backend/libc/time/syscalls.rs +++ b/src/backend/libc/time/syscalls.rs @@ -9,6 +9,15 @@ use crate::backend::time::types::LibcItimerspec; #[cfg(not(target_os = "wasi"))] use crate::clockid::{ClockId, DynamicClockId}; use crate::io; +#[cfg(not(fix_y2038))] +use crate::timespec::as_libc_timespec_mut_ptr; +#[cfg(not(fix_y2038))] +#[cfg(not(any( + target_os = "redox", + target_os = "wasi", + all(apple, not(target_os = "macos")) +)))] +use crate::timespec::as_libc_timespec_ptr; #[cfg(all(target_env = "gnu", fix_y2038))] use crate::timespec::LibcTimespec; use crate::timespec::Timespec; @@ -60,7 +69,7 @@ pub(crate) fn clock_getres(id: ClockId) -> Timespec { #[cfg(not(fix_y2038))] unsafe { let mut timespec = MaybeUninit::::uninit(); - let _ = c::clock_getres(id as c::clockid_t, timespec.as_mut_ptr()); + let _ = c::clock_getres(id as c::clockid_t, as_libc_timespec_mut_ptr(&mut timespec)); timespec.assume_init() } } @@ -116,7 +125,11 @@ pub(crate) fn clock_gettime(id: ClockId) -> Timespec { #[cfg(not(fix_y2038))] unsafe { let mut timespec = MaybeUninit::::uninit(); - ret(c::clock_gettime(id as c::clockid_t, timespec.as_mut_ptr())).unwrap(); + ret(c::clock_gettime( + id as c::clockid_t, + as_libc_timespec_mut_ptr(&mut timespec), + )) + .unwrap(); timespec.assume_init() } } @@ -203,7 +216,10 @@ pub(crate) fn clock_gettime_dynamic(id: DynamicClockId<'_>) -> io::Result::uninit(); - ret(c::clock_gettime(id as c::clockid_t, timespec.as_mut_ptr()))?; + ret(c::clock_gettime( + id as c::clockid_t, + as_libc_timespec_mut_ptr(&mut timespec), + ))?; Ok(timespec.assume_init()) } @@ -256,7 +272,10 @@ pub(crate) fn clock_settime(id: ClockId, timespec: Timespec) -> io::Result<()> { // Main version: libc is y2038 safe and has `clock_settime`. #[cfg(not(fix_y2038))] unsafe { - ret(c::clock_settime(id as c::clockid_t, ×pec)) + ret(c::clock_settime( + id as c::clockid_t, + as_libc_timespec_ptr(×pec), + )) } } @@ -314,12 +333,14 @@ pub(crate) fn timerfd_settime( #[cfg(not(fix_y2038))] unsafe { + use crate::backend::time::types::{as_libc_itimerspec_mut_ptr, as_libc_itimerspec_ptr}; + let mut result = MaybeUninit::::uninit(); ret(c::timerfd_settime( borrowed_fd(fd), bitflags_bits!(flags), - new_value, - result.as_mut_ptr(), + as_libc_itimerspec_ptr(new_value), + as_libc_itimerspec_mut_ptr(&mut result), ))?; Ok(result.assume_init()) } @@ -414,8 +435,13 @@ pub(crate) fn timerfd_gettime(fd: BorrowedFd<'_>) -> io::Result { #[cfg(not(fix_y2038))] unsafe { + use crate::backend::time::types::as_libc_itimerspec_mut_ptr; + let mut result = MaybeUninit::::uninit(); - ret(c::timerfd_gettime(borrowed_fd(fd), result.as_mut_ptr()))?; + ret(c::timerfd_gettime( + borrowed_fd(fd), + as_libc_itimerspec_mut_ptr(&mut result), + ))?; Ok(result.assume_init()) } } diff --git a/src/backend/libc/time/types.rs b/src/backend/libc/time/types.rs index 5254c6bc7..fca7db611 100644 --- a/src/backend/libc/time/types.rs +++ b/src/backend/libc/time/types.rs @@ -1,39 +1,13 @@ #[cfg(any(linux_kernel, target_os = "fuchsia"))] use crate::backend::c; #[cfg(any(linux_kernel, target_os = "fuchsia"))] -#[cfg(fix_y2038)] -use crate::timespec::LibcTimespec; +use crate::time::Itimerspec; #[cfg(any(linux_kernel, target_os = "fuchsia"))] #[cfg(fix_y2038)] -use crate::timespec::Timespec; +use crate::timespec::LibcTimespec; #[cfg(any(linux_kernel, target_os = "fuchsia"))] use bitflags::bitflags; -/// `struct itimerspec` for use with [`timerfd_gettime`] and -/// [`timerfd_settime`]. -/// -/// [`timerfd_gettime`]: crate::time::timerfd_gettime -/// [`timerfd_settime`]: crate::time::timerfd_settime -#[cfg(any(linux_kernel, target_os = "fuchsia"))] -#[cfg(not(fix_y2038))] -pub type Itimerspec = c::itimerspec; - -/// `struct itimerspec` for use with [`timerfd_gettime`] and -/// [`timerfd_settime`]. -/// -/// [`timerfd_gettime`]: crate::time::timerfd_gettime -/// [`timerfd_settime`]: crate::time::timerfd_settime -#[cfg(any(linux_kernel, target_os = "fuchsia"))] -#[cfg(fix_y2038)] -#[repr(C)] -#[derive(Debug, Clone)] -pub struct Itimerspec { - /// The interval of an interval timer. - pub it_interval: Timespec, - /// Time remaining in the current interval. - pub it_value: Timespec, -} - /// On most platforms, `LibcItimerspec` is just `Itimerspec`. #[cfg(any(linux_kernel, target_os = "fuchsia"))] #[cfg(not(fix_y2038))] @@ -74,6 +48,28 @@ impl From for LibcItimerspec { } } +#[cfg(any(linux_kernel, target_os = "fuchsia"))] +#[cfg(not(fix_y2038))] +pub(crate) fn as_libc_itimerspec_ptr(itimerspec: &Itimerspec) -> *const libc::itimerspec { + #[cfg(test)] + { + assert_eq_size!(Itimerspec, libc::itimerspec); + } + crate::utils::as_ptr(itimerspec).cast::() +} + +#[cfg(any(linux_kernel, target_os = "fuchsia"))] +#[cfg(not(fix_y2038))] +pub(crate) fn as_libc_itimerspec_mut_ptr( + itimerspec: &mut core::mem::MaybeUninit, +) -> *mut libc::itimerspec { + #[cfg(test)] + { + assert_eq_size!(Itimerspec, libc::itimerspec); + } + itimerspec.as_mut_ptr().cast::() +} + #[cfg(any(linux_kernel, target_os = "fuchsia"))] bitflags! { /// `TFD_*` flags for use with [`timerfd_create`]. diff --git a/src/backend/libc/ugid/syscalls.rs b/src/backend/libc/ugid/syscalls.rs index 0d3f622dc..f191116ef 100644 --- a/src/backend/libc/ugid/syscalls.rs +++ b/src/backend/libc/ugid/syscalls.rs @@ -1,12 +1,12 @@ use crate::backend::c; -use crate::ugid::{Gid, Uid}; +use crate::ugid::{Gid, RawGid, RawUid, Uid}; #[cfg(not(target_os = "wasi"))] #[inline] #[must_use] pub(crate) fn getuid() -> Uid { unsafe { - let uid = c::getuid(); + let uid = c::getuid() as RawUid; Uid::from_raw(uid) } } @@ -16,7 +16,7 @@ pub(crate) fn getuid() -> Uid { #[must_use] pub(crate) fn geteuid() -> Uid { unsafe { - let uid = c::geteuid(); + let uid = c::geteuid() as RawUid; Uid::from_raw(uid) } } @@ -26,7 +26,7 @@ pub(crate) fn geteuid() -> Uid { #[must_use] pub(crate) fn getgid() -> Gid { unsafe { - let gid = c::getgid(); + let gid = c::getgid() as RawGid; Gid::from_raw(gid) } } @@ -36,7 +36,7 @@ pub(crate) fn getgid() -> Gid { #[must_use] pub(crate) fn getegid() -> Gid { unsafe { - let gid = c::getegid(); + let gid = c::getegid() as RawGid; Gid::from_raw(gid) } } diff --git a/src/backend/linux_raw/event/epoll.rs b/src/backend/linux_raw/event/epoll.rs index fa3a46660..093129dbb 100644 --- a/src/backend/linux_raw/event/epoll.rs +++ b/src/backend/linux_raw/event/epoll.rs @@ -1,4 +1,4 @@ -use crate::backend::c; +use crate::ffi; use bitflags::bitflags; bitflags! { @@ -7,7 +7,7 @@ bitflags! { /// [`epoll::create`]: crate::event::epoll::create #[repr(transparent)] #[derive(Copy, Clone, Eq, PartialEq, Hash, Debug)] - pub struct CreateFlags: c::c_uint { + pub struct CreateFlags: ffi::c_uint { /// `EPOLL_CLOEXEC` const CLOEXEC = linux_raw_sys::general::EPOLL_CLOEXEC; diff --git a/src/backend/linux_raw/event/types.rs b/src/backend/linux_raw/event/types.rs index 01611f673..9d320dfec 100644 --- a/src/backend/linux_raw/event/types.rs +++ b/src/backend/linux_raw/event/types.rs @@ -1,4 +1,4 @@ -use crate::backend::c; +use crate::ffi; use bitflags::bitflags; bitflags! { @@ -7,7 +7,7 @@ bitflags! { /// [`eventfd`]: crate::event::eventfd #[repr(transparent)] #[derive(Copy, Clone, Eq, PartialEq, Hash, Debug)] - pub struct EventfdFlags: c::c_uint { + pub struct EventfdFlags: ffi::c_uint { /// `EFD_CLOEXEC` const CLOEXEC = linux_raw_sys::general::EFD_CLOEXEC; /// `EFD_NONBLOCK` diff --git a/src/backend/linux_raw/fs/inotify.rs b/src/backend/linux_raw/fs/inotify.rs index 8e8912924..ca102e985 100644 --- a/src/backend/linux_raw/fs/inotify.rs +++ b/src/backend/linux_raw/fs/inotify.rs @@ -1,6 +1,7 @@ //! inotify support for working with inotify objects. use crate::backend::c; +use crate::ffi; use bitflags::bitflags; bitflags! { @@ -9,7 +10,7 @@ bitflags! { /// [`inotify::init`]: crate::fs::inotify::init #[repr(transparent)] #[derive(Copy, Clone, Eq, PartialEq, Hash, Debug)] - pub struct CreateFlags: c::c_uint { + pub struct CreateFlags: ffi::c_uint { /// `IN_CLOEXEC` const CLOEXEC = linux_raw_sys::general::IN_CLOEXEC; /// `IN_NONBLOCK` @@ -26,7 +27,7 @@ bitflags! { /// [`inotify::add_watch`]: crate::fs::inotify::add_watch #[repr(transparent)] #[derive(Default, Copy, Clone, Eq, PartialEq, Hash, Debug)] - pub struct WatchFlags: c::c_uint { + pub struct WatchFlags: ffi::c_uint { /// `IN_ACCESS` const ACCESS = linux_raw_sys::general::IN_ACCESS; /// `IN_ATTRIB` diff --git a/src/backend/linux_raw/fs/syscalls.rs b/src/backend/linux_raw/fs/syscalls.rs index 81c36fe0e..5dc56bd80 100644 --- a/src/backend/linux_raw/fs/syscalls.rs +++ b/src/backend/linux_raw/fs/syscalls.rs @@ -31,7 +31,7 @@ use crate::backend::conv::{loff_t, loff_t_from_u64, ret_u64}; ))] use crate::fd::AsFd; use crate::fd::{BorrowedFd, OwnedFd}; -use crate::ffi::CStr; +use crate::ffi::{self, CStr}; #[cfg(any(target_arch = "aarch64", target_arch = "riscv64"))] use crate::fs::CWD; use crate::fs::{ @@ -1627,19 +1627,19 @@ pub(crate) fn fsetxattr( } #[inline] -pub(crate) fn listxattr(path: &CStr, list: &mut [c::c_char]) -> io::Result { +pub(crate) fn listxattr(path: &CStr, list: &mut [ffi::c_char]) -> io::Result { let (list_addr_mut, list_len) = slice_mut(list); unsafe { ret_usize(syscall!(__NR_listxattr, path, list_addr_mut, list_len)) } } #[inline] -pub(crate) fn llistxattr(path: &CStr, list: &mut [c::c_char]) -> io::Result { +pub(crate) fn llistxattr(path: &CStr, list: &mut [ffi::c_char]) -> io::Result { let (list_addr_mut, list_len) = slice_mut(list); unsafe { ret_usize(syscall!(__NR_llistxattr, path, list_addr_mut, list_len)) } } #[inline] -pub(crate) fn flistxattr(fd: BorrowedFd<'_>, list: &mut [c::c_char]) -> io::Result { +pub(crate) fn flistxattr(fd: BorrowedFd<'_>, list: &mut [ffi::c_char]) -> io::Result { let (list_addr_mut, list_len) = slice_mut(list); unsafe { ret_usize(syscall!(__NR_flistxattr, fd, list_addr_mut, list_len)) } } diff --git a/src/backend/linux_raw/fs/types.rs b/src/backend/linux_raw/fs/types.rs index ecadde06d..07498bf43 100644 --- a/src/backend/linux_raw/fs/types.rs +++ b/src/backend/linux_raw/fs/types.rs @@ -1,4 +1,4 @@ -use crate::backend::c; +use crate::ffi; use bitflags::bitflags; bitflags! { @@ -7,7 +7,7 @@ bitflags! { /// [`accessat`]: fn.accessat.html #[repr(transparent)] #[derive(Copy, Clone, Eq, PartialEq, Hash, Debug)] - pub struct Access: c::c_uint { + pub struct Access: ffi::c_uint { /// `R_OK` const READ_OK = linux_raw_sys::general::R_OK; @@ -33,7 +33,7 @@ bitflags! { /// [`statat`]: crate::fs::statat #[repr(transparent)] #[derive(Copy, Clone, Eq, PartialEq, Hash, Debug)] - pub struct AtFlags: c::c_uint { + pub struct AtFlags: ffi::c_uint { /// `AT_SYMLINK_NOFOLLOW` const SYMLINK_NOFOLLOW = linux_raw_sys::general::AT_SYMLINK_NOFOLLOW; @@ -172,7 +172,7 @@ bitflags! { /// [`openat`]: crate::fs::openat #[repr(transparent)] #[derive(Copy, Clone, Eq, PartialEq, Hash, Debug)] - pub struct OFlags: c::c_uint { + pub struct OFlags: ffi::c_uint { /// `O_ACCMODE` const ACCMODE = linux_raw_sys::general::O_ACCMODE; @@ -298,7 +298,7 @@ bitflags! { /// [`renameat_with`]: crate::fs::renameat_with #[repr(transparent)] #[derive(Copy, Clone, Eq, PartialEq, Hash, Debug)] - pub struct RenameFlags: c::c_uint { + pub struct RenameFlags: ffi::c_uint { /// `RENAME_EXCHANGE` const EXCHANGE = linux_raw_sys::general::RENAME_EXCHANGE; @@ -425,7 +425,7 @@ bitflags! { /// [`memfd_create`]: crate::fs::memfd_create #[repr(transparent)] #[derive(Copy, Clone, Eq, PartialEq, Hash, Debug)] - pub struct MemfdFlags: c::c_uint { + pub struct MemfdFlags: ffi::c_uint { /// `MFD_CLOEXEC` const CLOEXEC = linux_raw_sys::general::MFD_CLOEXEC; @@ -739,7 +739,7 @@ pub type RawMode = linux_raw_sys::general::__kernel_mode_t; target_arch = "arm", ))] // Don't use `__kernel_mode_t` since it's `u16` which differs from `st_size`. -pub type RawMode = c::c_uint; +pub type RawMode = ffi::c_uint; /// `dev_t` // Within the kernel the `dev_t` is 32-bit, but userspace uses a 64-bit field. diff --git a/src/backend/linux_raw/io/types.rs b/src/backend/linux_raw/io/types.rs index 4b3dfc686..533f97325 100644 --- a/src/backend/linux_raw/io/types.rs +++ b/src/backend/linux_raw/io/types.rs @@ -1,4 +1,4 @@ -use crate::backend::c; +use crate::ffi; use bitflags::bitflags; bitflags! { @@ -8,7 +8,7 @@ bitflags! { /// [`fcntl_setfd`]: crate::io::fcntl_setfd #[repr(transparent)] #[derive(Copy, Clone, Eq, PartialEq, Hash, Debug)] - pub struct FdFlags: c::c_uint { + pub struct FdFlags: ffi::c_uint { /// `FD_CLOEXEC` const CLOEXEC = linux_raw_sys::general::FD_CLOEXEC; @@ -24,7 +24,7 @@ bitflags! { /// [`pwritev2`]: crate::io::pwritev #[repr(transparent)] #[derive(Copy, Clone, Eq, PartialEq, Hash, Debug)] - pub struct ReadWriteFlags: c::c_uint { + pub struct ReadWriteFlags: ffi::c_uint { /// `RWF_DSYNC` (since Linux 4.7) const DSYNC = linux_raw_sys::general::RWF_DSYNC; /// `RWF_HIPRI` (since Linux 4.6) @@ -47,7 +47,7 @@ bitflags! { /// [`dup2`]: crate::io::dup2 #[repr(transparent)] #[derive(Copy, Clone, Eq, PartialEq, Hash, Debug)] - pub struct DupFlags: c::c_uint { + pub struct DupFlags: ffi::c_uint { /// `O_CLOEXEC` const CLOEXEC = linux_raw_sys::general::O_CLOEXEC; diff --git a/src/backend/linux_raw/mm/syscalls.rs b/src/backend/linux_raw/mm/syscalls.rs index 0aff9ab7e..d0dcfca43 100644 --- a/src/backend/linux_raw/mm/syscalls.rs +++ b/src/backend/linux_raw/mm/syscalls.rs @@ -15,11 +15,12 @@ use crate::backend::c; use crate::backend::conv::loff_t_from_u64; use crate::backend::conv::{c_uint, no_fd, pass_usize, ret, ret_owned_fd, ret_void_star}; use crate::fd::{BorrowedFd, OwnedFd}; +use crate::ffi::c_void; use crate::io; use linux_raw_sys::general::{MAP_ANONYMOUS, MREMAP_FIXED}; #[inline] -pub(crate) fn madvise(addr: *mut c::c_void, len: usize, advice: Advice) -> io::Result<()> { +pub(crate) fn madvise(addr: *mut c_void, len: usize, advice: Advice) -> io::Result<()> { unsafe { ret(syscall!( __NR_madvise, @@ -31,7 +32,7 @@ pub(crate) fn madvise(addr: *mut c::c_void, len: usize, advice: Advice) -> io::R } #[inline] -pub(crate) unsafe fn msync(addr: *mut c::c_void, len: usize, flags: MsyncFlags) -> io::Result<()> { +pub(crate) unsafe fn msync(addr: *mut c_void, len: usize, flags: MsyncFlags) -> io::Result<()> { ret(syscall!(__NR_msync, addr, pass_usize(len), flags)) } @@ -41,13 +42,13 @@ pub(crate) unsafe fn msync(addr: *mut c::c_void, len: usize, flags: MsyncFlags) /// with memory pointed to by raw pointers is unsafe. #[inline] pub(crate) unsafe fn mmap( - addr: *mut c::c_void, + addr: *mut c_void, length: usize, prot: ProtFlags, flags: MapFlags, fd: BorrowedFd<'_>, offset: u64, -) -> io::Result<*mut c::c_void> { +) -> io::Result<*mut c_void> { #[cfg(target_pointer_width = "32")] { ret_void_star(syscall!( @@ -83,11 +84,11 @@ pub(crate) unsafe fn mmap( /// with memory pointed to by raw pointers is unsafe. #[inline] pub(crate) unsafe fn mmap_anonymous( - addr: *mut c::c_void, + addr: *mut c_void, length: usize, prot: ProtFlags, flags: MapFlags, -) -> io::Result<*mut c::c_void> { +) -> io::Result<*mut c_void> { #[cfg(target_pointer_width = "32")] { ret_void_star(syscall!( @@ -116,7 +117,7 @@ pub(crate) unsafe fn mmap_anonymous( #[inline] pub(crate) unsafe fn mprotect( - ptr: *mut c::c_void, + ptr: *mut c_void, len: usize, flags: MprotectFlags, ) -> io::Result<()> { @@ -128,7 +129,7 @@ pub(crate) unsafe fn mprotect( /// `munmap` is primarily unsafe due to the `addr` parameter, as anything /// working with memory pointed to by raw pointers is unsafe. #[inline] -pub(crate) unsafe fn munmap(addr: *mut c::c_void, length: usize) -> io::Result<()> { +pub(crate) unsafe fn munmap(addr: *mut c_void, length: usize) -> io::Result<()> { ret(syscall!(__NR_munmap, addr, pass_usize(length))) } @@ -138,11 +139,11 @@ pub(crate) unsafe fn munmap(addr: *mut c::c_void, length: usize) -> io::Result<( /// anything working with memory pointed to by raw pointers is unsafe. #[inline] pub(crate) unsafe fn mremap( - old_address: *mut c::c_void, + old_address: *mut c_void, old_size: usize, new_size: usize, flags: MremapFlags, -) -> io::Result<*mut c::c_void> { +) -> io::Result<*mut c_void> { ret_void_star(syscall!( __NR_mremap, old_address, @@ -159,12 +160,12 @@ pub(crate) unsafe fn mremap( /// pointers is unsafe. #[inline] pub(crate) unsafe fn mremap_fixed( - old_address: *mut c::c_void, + old_address: *mut c_void, old_size: usize, new_size: usize, flags: MremapFlags, - new_address: *mut c::c_void, -) -> io::Result<*mut c::c_void> { + new_address: *mut c_void, +) -> io::Result<*mut c_void> { ret_void_star(syscall!( __NR_mremap, old_address, @@ -180,7 +181,7 @@ pub(crate) unsafe fn mremap_fixed( /// `mlock` operates on raw pointers and may round out to the nearest page /// boundaries. #[inline] -pub(crate) unsafe fn mlock(addr: *mut c::c_void, length: usize) -> io::Result<()> { +pub(crate) unsafe fn mlock(addr: *mut c_void, length: usize) -> io::Result<()> { ret(syscall!(__NR_mlock, addr, pass_usize(length))) } @@ -190,7 +191,7 @@ pub(crate) unsafe fn mlock(addr: *mut c::c_void, length: usize) -> io::Result<() /// boundaries. #[inline] pub(crate) unsafe fn mlock_with( - addr: *mut c::c_void, + addr: *mut c_void, length: usize, flags: MlockFlags, ) -> io::Result<()> { @@ -202,7 +203,7 @@ pub(crate) unsafe fn mlock_with( /// `munlock` operates on raw pointers and may round out to the nearest page /// boundaries. #[inline] -pub(crate) unsafe fn munlock(addr: *mut c::c_void, length: usize) -> io::Result<()> { +pub(crate) unsafe fn munlock(addr: *mut c_void, length: usize) -> io::Result<()> { ret(syscall!(__NR_munlock, addr, pass_usize(length))) } diff --git a/src/backend/linux_raw/mm/types.rs b/src/backend/linux_raw/mm/types.rs index 6960cf7f3..109909701 100644 --- a/src/backend/linux_raw/mm/types.rs +++ b/src/backend/linux_raw/mm/types.rs @@ -1,4 +1,5 @@ use crate::backend::c; +use crate::ffi; use bitflags::bitflags; bitflags! { @@ -254,7 +255,7 @@ bitflags! { /// [`userfaultfd`]: crate::mm::userfaultfd #[repr(transparent)] #[derive(Copy, Clone, Eq, PartialEq, Hash, Debug)] - pub struct UserfaultfdFlags: c::c_uint { + pub struct UserfaultfdFlags: ffi::c_uint { /// `O_CLOEXEC` const CLOEXEC = linux_raw_sys::general::O_CLOEXEC; /// `O_NONBLOCK` diff --git a/src/backend/linux_raw/mount/types.rs b/src/backend/linux_raw/mount/types.rs index c6617e6e1..0e1465d65 100644 --- a/src/backend/linux_raw/mount/types.rs +++ b/src/backend/linux_raw/mount/types.rs @@ -1,4 +1,5 @@ use crate::backend::c; +use crate::ffi; use bitflags::bitflags; bitflags! { @@ -7,7 +8,7 @@ bitflags! { /// [`mount`]: crate::mount::mount #[repr(transparent)] #[derive(Copy, Clone, Eq, PartialEq, Hash, Debug)] - pub struct MountFlags: c::c_uint { + pub struct MountFlags: ffi::c_uint { /// `MS_BIND` const BIND = linux_raw_sys::general::MS_BIND; @@ -68,7 +69,7 @@ bitflags! { /// [`unmount`]: crate::mount::unmount #[repr(transparent)] #[derive(Copy, Clone, Eq, PartialEq, Hash, Debug)] - pub struct UnmountFlags: c::c_uint { + pub struct UnmountFlags: ffi::c_uint { /// `MNT_FORCE` const FORCE = linux_raw_sys::general::MNT_FORCE; /// `MNT_DETACH` @@ -90,7 +91,7 @@ bitflags! { /// [`fsopen`]: crate::mount::fsopen #[repr(transparent)] #[derive(Copy, Clone, Eq, PartialEq, Hash, Debug)] - pub struct FsOpenFlags: c::c_uint { + pub struct FsOpenFlags: ffi::c_uint { /// `FSOPEN_CLOEXEC` const FSOPEN_CLOEXEC = linux_raw_sys::general::FSOPEN_CLOEXEC; @@ -106,7 +107,7 @@ bitflags! { /// [`fsmount`]: crate::mount::fsmount #[repr(transparent)] #[derive(Copy, Clone, Eq, PartialEq, Hash, Debug)] - pub struct FsMountFlags: c::c_uint { + pub struct FsMountFlags: ffi::c_uint { /// `FSMOUNT_CLOEXEC` const FSMOUNT_CLOEXEC = linux_raw_sys::general::FSMOUNT_CLOEXEC; @@ -152,7 +153,7 @@ bitflags! { /// [`fsmount`]: crate::mount::fsmount #[repr(transparent)] #[derive(Copy, Clone, Eq, PartialEq, Hash, Debug)] - pub struct MountAttrFlags: c::c_uint { + pub struct MountAttrFlags: ffi::c_uint { /// `MOUNT_ATTR_RDONLY` const MOUNT_ATTR_RDONLY = linux_raw_sys::general::MOUNT_ATTR_RDONLY; @@ -201,7 +202,7 @@ bitflags! { /// [`move_mount`]: crate::mount::move_mount #[repr(transparent)] #[derive(Copy, Clone, Eq, PartialEq, Hash, Debug)] - pub struct MoveMountFlags: c::c_uint { + pub struct MoveMountFlags: ffi::c_uint { /// `MOVE_MOUNT_F_EMPTY_PATH` const MOVE_MOUNT_F_SYMLINKS = linux_raw_sys::general::MOVE_MOUNT_F_SYMLINKS; @@ -241,7 +242,7 @@ bitflags! { /// [`open_tree`]: crate::mount::open_tree #[repr(transparent)] #[derive(Copy, Clone, Eq, PartialEq, Hash, Debug)] - pub struct OpenTreeFlags: c::c_uint { + pub struct OpenTreeFlags: ffi::c_uint { /// `OPENTREE_CLONE` const OPEN_TREE_CLONE = linux_raw_sys::general::OPEN_TREE_CLONE; @@ -272,7 +273,7 @@ bitflags! { /// [`fspick`]: crate::mount::fspick #[repr(transparent)] #[derive(Copy, Clone, Eq, PartialEq, Hash, Debug)] - pub struct FsPickFlags: c::c_uint { + pub struct FsPickFlags: ffi::c_uint { /// `FSPICK_CLOEXEC` const FSPICK_CLOEXEC = linux_raw_sys::general::FSPICK_CLOEXEC; @@ -296,7 +297,7 @@ bitflags! { /// [`mount_change`]: crate::mount::mount_change #[repr(transparent)] #[derive(Copy, Clone, Eq, PartialEq, Hash, Debug)] - pub struct MountPropagationFlags: c::c_uint { + pub struct MountPropagationFlags: ffi::c_uint { /// `MS_SILENT` const SILENT = linux_raw_sys::general::MS_SILENT; /// `MS_SHARED` @@ -326,7 +327,7 @@ bitflags! { bitflags! { #[repr(transparent)] #[derive(Copy, Clone, Eq, PartialEq, Hash, Debug)] - pub(crate) struct InternalMountFlags: c::c_uint { + pub(crate) struct InternalMountFlags: ffi::c_uint { const REMOUNT = linux_raw_sys::general::MS_REMOUNT; const MOVE = linux_raw_sys::general::MS_MOVE; @@ -336,4 +337,4 @@ bitflags! { } #[repr(transparent)] -pub(crate) struct MountFlagsArg(pub(crate) c::c_uint); +pub(crate) struct MountFlagsArg(pub(crate) ffi::c_uint); diff --git a/src/backend/linux_raw/pipe/types.rs b/src/backend/linux_raw/pipe/types.rs index a2a9e1cb1..ae1855626 100644 --- a/src/backend/linux_raw/pipe/types.rs +++ b/src/backend/linux_raw/pipe/types.rs @@ -1,4 +1,5 @@ use crate::backend::c; +use crate::ffi; use bitflags::bitflags; use core::marker::PhantomData; @@ -8,7 +9,7 @@ bitflags! { /// [`pipe_with`]: crate::pipe::pipe_with #[repr(transparent)] #[derive(Copy, Clone, Eq, PartialEq, Hash, Debug)] - pub struct PipeFlags: c::c_uint { + pub struct PipeFlags: ffi::c_uint { /// `O_CLOEXEC` const CLOEXEC = linux_raw_sys::general::O_CLOEXEC; /// `O_DIRECT` @@ -30,7 +31,7 @@ bitflags! { /// [`tee`]: crate::pipe::tee #[repr(transparent)] #[derive(Copy, Clone, Eq, PartialEq, Hash, Debug)] - pub struct SpliceFlags: c::c_uint { + pub struct SpliceFlags: ffi::c_uint { /// `SPLICE_F_MOVE` const MOVE = linux_raw_sys::general::SPLICE_F_MOVE; /// `SPLICE_F_NONBLOCK` @@ -64,7 +65,7 @@ impl<'a> IoSliceRaw<'a> { pub fn from_slice(buf: &'a [u8]) -> Self { IoSliceRaw { _buf: c::iovec { - iov_base: (buf.as_ptr() as *mut u8).cast::(), + iov_base: (buf.as_ptr() as *mut u8).cast::(), iov_len: buf.len() as _, }, _lifetime: PhantomData, @@ -75,7 +76,7 @@ impl<'a> IoSliceRaw<'a> { pub fn from_slice_mut(buf: &'a mut [u8]) -> Self { IoSliceRaw { _buf: c::iovec { - iov_base: buf.as_mut_ptr().cast::(), + iov_base: buf.as_mut_ptr().cast::(), iov_len: buf.len() as _, }, _lifetime: PhantomData, diff --git a/src/backend/linux_raw/shm/types.rs b/src/backend/linux_raw/shm/types.rs index 866e8b73b..e1e8fc028 100644 --- a/src/backend/linux_raw/shm/types.rs +++ b/src/backend/linux_raw/shm/types.rs @@ -1,4 +1,4 @@ -use crate::backend::c; +use crate::ffi; use bitflags::bitflags; bitflags! { @@ -7,7 +7,7 @@ bitflags! { /// [`shm::open`]: crate:shm::open #[repr(transparent)] #[derive(Copy, Clone, Eq, PartialEq, Hash, Debug)] - pub struct ShmOFlags: c::c_uint { + pub struct ShmOFlags: ffi::c_uint { /// `O_CREAT` #[doc(alias = "CREAT")] const CREATE = linux_raw_sys::general::O_CREAT; diff --git a/src/backend/linux_raw/termios/mod.rs b/src/backend/linux_raw/termios/mod.rs index ef944f04d..1e0181a99 100644 --- a/src/backend/linux_raw/termios/mod.rs +++ b/src/backend/linux_raw/termios/mod.rs @@ -1 +1,2 @@ pub(crate) mod syscalls; +pub(crate) mod types; diff --git a/src/backend/linux_raw/termios/types.rs b/src/backend/linux_raw/termios/types.rs new file mode 100644 index 000000000..15b36a0b3 --- /dev/null +++ b/src/backend/linux_raw/termios/types.rs @@ -0,0 +1,11 @@ +#![allow(non_camel_case_types)] + +use crate::ffi; + +// We don't want to use tcflag_t directly so we don't expose linux_raw_sys +// publicly. It appears to be c_ulong on SPARC and c_uint everywhere else. + +#[cfg(target_arch = "sparc")] +pub type tcflag_t = ffi::c_ulong; +#[cfg(not(target_arch = "sparc"))] +pub type tcflag_t = ffi::c_uint; diff --git a/src/backend/linux_raw/thread/syscalls.rs b/src/backend/linux_raw/thread/syscalls.rs index 3411a1ed5..bbc25c4d6 100644 --- a/src/backend/linux_raw/thread/syscalls.rs +++ b/src/backend/linux_raw/thread/syscalls.rs @@ -20,19 +20,16 @@ use crate::thread::{ use crate::utils::as_mut_ptr; use core::mem::MaybeUninit; use core::sync::atomic::AtomicU32; -use linux_raw_sys::general::{__kernel_timespec, TIMER_ABSTIME}; +use linux_raw_sys::general::TIMER_ABSTIME; use linux_raw_sys::general::{membarrier_cmd, membarrier_cmd_flag}; #[cfg(target_pointer_width = "32")] use {crate::utils::option_as_ptr, linux_raw_sys::general::timespec as __kernel_old_timespec}; #[inline] -pub(crate) fn clock_nanosleep_relative( - id: ClockId, - req: &__kernel_timespec, -) -> NanosleepRelativeResult { +pub(crate) fn clock_nanosleep_relative(id: ClockId, req: &Timespec) -> NanosleepRelativeResult { #[cfg(target_pointer_width = "32")] unsafe { - let mut rem = MaybeUninit::<__kernel_timespec>::uninit(); + let mut rem = MaybeUninit::::uninit(); match ret(syscall!( __NR_clock_nanosleep_time64, id, @@ -56,7 +53,7 @@ pub(crate) fn clock_nanosleep_relative( } #[cfg(target_pointer_width = "64")] unsafe { - let mut rem = MaybeUninit::<__kernel_timespec>::uninit(); + let mut rem = MaybeUninit::::uninit(); match ret(syscall!( __NR_clock_nanosleep, id, @@ -74,8 +71,8 @@ pub(crate) fn clock_nanosleep_relative( #[cfg(target_pointer_width = "32")] unsafe fn clock_nanosleep_relative_old( id: ClockId, - req: &__kernel_timespec, - rem: &mut MaybeUninit<__kernel_timespec>, + req: &Timespec, + rem: &mut MaybeUninit, ) -> io::Result<()> { let old_req = __kernel_old_timespec { tv_sec: req.tv_sec.try_into().map_err(|_| io::Errno::INVAL)?, @@ -90,7 +87,7 @@ unsafe fn clock_nanosleep_relative_old( &mut old_rem ))?; let old_rem = old_rem.assume_init(); - rem.write(__kernel_timespec { + rem.write(Timespec { tv_sec: old_rem.tv_sec.into(), tv_nsec: old_rem.tv_nsec.into(), }); @@ -98,7 +95,7 @@ unsafe fn clock_nanosleep_relative_old( } #[inline] -pub(crate) fn clock_nanosleep_absolute(id: ClockId, req: &__kernel_timespec) -> io::Result<()> { +pub(crate) fn clock_nanosleep_absolute(id: ClockId, req: &Timespec) -> io::Result<()> { #[cfg(target_pointer_width = "32")] unsafe { ret(syscall_readonly!( @@ -131,7 +128,7 @@ pub(crate) fn clock_nanosleep_absolute(id: ClockId, req: &__kernel_timespec) -> } #[cfg(target_pointer_width = "32")] -unsafe fn clock_nanosleep_absolute_old(id: ClockId, req: &__kernel_timespec) -> io::Result<()> { +unsafe fn clock_nanosleep_absolute_old(id: ClockId, req: &Timespec) -> io::Result<()> { let old_req = __kernel_old_timespec { tv_sec: req.tv_sec.try_into().map_err(|_| io::Errno::INVAL)?, tv_nsec: req.tv_nsec.try_into().map_err(|_| io::Errno::INVAL)?, @@ -146,10 +143,10 @@ unsafe fn clock_nanosleep_absolute_old(id: ClockId, req: &__kernel_timespec) -> } #[inline] -pub(crate) fn nanosleep(req: &__kernel_timespec) -> NanosleepRelativeResult { +pub(crate) fn nanosleep(req: &Timespec) -> NanosleepRelativeResult { #[cfg(target_pointer_width = "32")] unsafe { - let mut rem = MaybeUninit::<__kernel_timespec>::uninit(); + let mut rem = MaybeUninit::::uninit(); match ret(syscall!( __NR_clock_nanosleep_time64, ClockId::Realtime, @@ -173,7 +170,7 @@ pub(crate) fn nanosleep(req: &__kernel_timespec) -> NanosleepRelativeResult { } #[cfg(target_pointer_width = "64")] unsafe { - let mut rem = MaybeUninit::<__kernel_timespec>::uninit(); + let mut rem = MaybeUninit::::uninit(); match ret(syscall!(__NR_nanosleep, by_ref(req), &mut rem)) { Ok(()) => NanosleepRelativeResult::Ok, Err(io::Errno::INTR) => NanosleepRelativeResult::Interrupted(rem.assume_init()), @@ -183,10 +180,7 @@ pub(crate) fn nanosleep(req: &__kernel_timespec) -> NanosleepRelativeResult { } #[cfg(target_pointer_width = "32")] -unsafe fn nanosleep_old( - req: &__kernel_timespec, - rem: &mut MaybeUninit<__kernel_timespec>, -) -> io::Result<()> { +unsafe fn nanosleep_old(req: &Timespec, rem: &mut MaybeUninit) -> io::Result<()> { let old_req = __kernel_old_timespec { tv_sec: req.tv_sec.try_into().map_err(|_| io::Errno::INVAL)?, tv_nsec: req.tv_nsec.try_into().map_err(|_| io::Errno::INVAL)?, @@ -194,7 +188,7 @@ unsafe fn nanosleep_old( let mut old_rem = MaybeUninit::<__kernel_old_timespec>::uninit(); ret(syscall!(__NR_nanosleep, by_ref(&old_req), &mut old_rem))?; let old_rem = old_rem.assume_init(); - rem.write(__kernel_timespec { + rem.write(Timespec { tv_sec: old_rem.tv_sec.into(), tv_nsec: old_rem.tv_nsec.into(), }); diff --git a/src/backend/linux_raw/time/types.rs b/src/backend/linux_raw/time/types.rs index c26812c5d..ec6c91f56 100644 --- a/src/backend/linux_raw/time/types.rs +++ b/src/backend/linux_raw/time/types.rs @@ -1,20 +1,13 @@ -use crate::backend::c; +use crate::ffi; use bitflags::bitflags; -/// `struct itimerspec` for use with [`timerfd_gettime`] and -/// [`timerfd_settime`]. -/// -/// [`timerfd_gettime`]: crate::time::timerfd_gettime -/// [`timerfd_settime`]: crate::time::timerfd_settime -pub type Itimerspec = linux_raw_sys::general::__kernel_itimerspec; - bitflags! { /// `TFD_*` flags for use with [`timerfd_create`]. /// /// [`timerfd_create`]: crate::time::timerfd_create #[repr(transparent)] #[derive(Copy, Clone, Eq, PartialEq, Hash, Debug)] - pub struct TimerfdFlags: c::c_uint { + pub struct TimerfdFlags: ffi::c_uint { /// `TFD_NONBLOCK` #[doc(alias = "TFD_NONBLOCK")] const NONBLOCK = linux_raw_sys::general::TFD_NONBLOCK; @@ -34,7 +27,7 @@ bitflags! { /// [`timerfd_settime`]: crate::time::timerfd_settime #[repr(transparent)] #[derive(Copy, Clone, Eq, PartialEq, Hash, Debug)] - pub struct TimerfdTimerFlags: c::c_uint { + pub struct TimerfdTimerFlags: ffi::c_uint { /// `TFD_TIMER_ABSTIME` #[doc(alias = "TFD_TIMER_ABSTIME")] const ABSTIME = linux_raw_sys::general::TFD_TIMER_ABSTIME; diff --git a/src/backend/linux_raw/vdso_wrappers.rs b/src/backend/linux_raw/vdso_wrappers.rs index d32054f3d..fc6eea137 100644 --- a/src/backend/linux_raw/vdso_wrappers.rs +++ b/src/backend/linux_raw/vdso_wrappers.rs @@ -51,17 +51,17 @@ use { crate::clockid::{ClockId, DynamicClockId}, crate::io, crate::timespec::Timespec, - linux_raw_sys::general::{__kernel_clockid_t, __kernel_timespec}, + linux_raw_sys::general::__kernel_clockid_t, }; #[cfg(feature = "time")] #[inline] -pub(crate) fn clock_gettime(which_clock: ClockId) -> __kernel_timespec { +pub(crate) fn clock_gettime(which_clock: ClockId) -> Timespec { // SAFETY: `CLOCK_GETTIME` contains either null or the address of a // function with an ABI like libc `clock_gettime`, and calling it has the // side effect of writing to the result buffer, and no others. unsafe { - let mut result = MaybeUninit::<__kernel_timespec>::uninit(); + let mut result = MaybeUninit::::uninit(); let callee = match transmute(CLOCK_GETTIME.load(Relaxed)) { Some(callee) => callee, None => init_clock_gettime(), diff --git a/src/event/port.rs b/src/event/port.rs index 094618317..8a34a0180 100644 --- a/src/event/port.rs +++ b/src/event/port.rs @@ -3,6 +3,7 @@ use crate::backend::c; use crate::backend::event::syscalls; use crate::fd::{AsFd, AsRawFd, OwnedFd}; +use crate::ffi; use crate::io; use super::PollFlags; @@ -25,7 +26,7 @@ impl Event { } /// Get the userdata associated with this event. - pub fn userdata(&self) -> *mut c::c_void { + pub fn userdata(&self) -> *mut ffi::c_void { self.0.portev_user } } @@ -61,7 +62,7 @@ pub unsafe fn port_associate_fd( port: impl AsFd, object: impl AsRawFd, events: PollFlags, - userdata: *mut c::c_void, + userdata: *mut ffi::c_void, ) -> io::Result<()> { syscalls::port_associate( port.as_fd(), @@ -168,6 +169,6 @@ pub fn port_getn_query(port: impl AsFd) -> io::Result { /// /// [OpenSolaris]: https://www.unix.com/man-page/opensolaris/3C/port_send/ /// [illumos]: https://illumos.org/man/3C/port_send -pub fn port_send(port: impl AsFd, events: i32, userdata: *mut c::c_void) -> io::Result<()> { +pub fn port_send(port: impl AsFd, events: i32, userdata: *mut ffi::c_void) -> io::Result<()> { syscalls::port_send(port.as_fd(), events, userdata.cast()) } diff --git a/src/ffi.rs b/src/ffi.rs index 4f8751f04..72c573eb5 100644 --- a/src/ffi.rs +++ b/src/ffi.rs @@ -3,12 +3,16 @@ // If we have std, use it. #[cfg(windows)] #[cfg(feature = "std")] -pub use std::os::raw::{c_char, c_int, c_long, c_short, c_uint, c_ulong, c_ushort}; +pub use std::os::raw::{ + c_char, c_int, c_long, c_longlong, c_short, c_uint, c_ulong, c_ulonglong, c_ushort, c_void, +}; #[cfg(not(windows))] #[cfg(feature = "std")] pub use { std::ffi::{CStr, CString, FromBytesWithNulError, NulError}, - std::os::raw::c_char, + std::os::raw::{ + c_char, c_int, c_long, c_longlong, c_short, c_uint, c_ulong, c_ulonglong, c_ushort, c_void, + }, }; // If we don't have std, we can depend on core and alloc having these features @@ -18,7 +22,12 @@ pub use { pub use alloc::ffi::{CString, NulError}; #[cfg(windows)] #[cfg(not(feature = "std"))] -pub use core::ffi::{c_char, c_int, c_long, c_short, c_uint, c_ulong, c_ushort, c_void}; +pub use core::ffi::{ + c_char, c_int, c_long, c_longlong, c_short, c_uint, c_ulong, c_ulonglong, c_ushort, c_void, +}; #[cfg(not(windows))] #[cfg(not(feature = "std"))] -pub use core::ffi::{c_char, CStr, FromBytesWithNulError}; +pub use core::ffi::{ + c_char, c_int, c_long, c_longlong, c_short, c_uint, c_ulong, c_ulonglong, c_ushort, c_void, + CStr, FromBytesWithNulError, +}; diff --git a/src/fs/ioctl.rs b/src/fs/ioctl.rs index 718a2a404..c7e705f44 100644 --- a/src/fs/ioctl.rs +++ b/src/fs/ioctl.rs @@ -4,9 +4,9 @@ #[cfg(linux_kernel)] use { + crate::backend::c, crate::fd::AsFd, - crate::{backend, io, ioctl}, - backend::c, + crate::{backend, ffi, io, ioctl}, }; use bitflags::bitflags; @@ -98,7 +98,7 @@ bitflags! { /// `FS_*` constants for use with [`ioctl_getflags`]. /// /// [`ioctl_getflags`]: crate::fs::ioctl::ioctl_getflags - pub struct IFlags: c::c_uint { + pub struct IFlags: ffi::c_uint { /// `FS_APPEND_FL` const APPEND = linux_raw_sys::general::FS_APPEND_FL; /// `FS_COMPR_FL` diff --git a/src/fs/xattr.rs b/src/fs/xattr.rs index d5be7a34b..4a792c6e1 100644 --- a/src/fs/xattr.rs +++ b/src/fs/xattr.rs @@ -1,4 +1,4 @@ -use crate::{backend, io, path}; +use crate::{backend, ffi, io, path}; use backend::c; use backend::fd::AsFd; use bitflags::bitflags; @@ -8,7 +8,7 @@ bitflags! { /// functions. #[repr(transparent)] #[derive(Copy, Clone, Eq, PartialEq, Hash, Debug)] - pub struct XattrFlags: c::c_uint { + pub struct XattrFlags: ffi::c_uint { /// `XATTR_CREATE` const CREATE = c::XATTR_CREATE as c::c_uint; @@ -137,7 +137,7 @@ pub fn fsetxattr( /// /// [Linux]: https://man7.org/linux/man-pages/man2/listxattr.2.html #[inline] -pub fn listxattr(path: P, list: &mut [c::c_char]) -> io::Result { +pub fn listxattr(path: P, list: &mut [ffi::c_char]) -> io::Result { path.into_with_c_str(|path| backend::fs::syscalls::listxattr(path, list)) } @@ -149,7 +149,7 @@ pub fn listxattr(path: P, list: &mut [c::c_char]) -> io::Result(path: P, list: &mut [c::c_char]) -> io::Result { +pub fn llistxattr(path: P, list: &mut [ffi::c_char]) -> io::Result { path.into_with_c_str(|path| backend::fs::syscalls::llistxattr(path, list)) } @@ -161,7 +161,7 @@ pub fn llistxattr(path: P, list: &mut [c::c_char]) -> io::Result(fd: Fd, list: &mut [c::c_char]) -> io::Result { +pub fn flistxattr(fd: Fd, list: &mut [ffi::c_char]) -> io::Result { backend::fs::syscalls::flistxattr(fd.as_fd(), list) } diff --git a/src/io_uring.rs b/src/io_uring.rs index 74296caef..f888a90c9 100644 --- a/src/io_uring.rs +++ b/src/io_uring.rs @@ -35,6 +35,7 @@ use linux_raw_sys::net; pub use crate::event::epoll::{ Event as EpollEvent, EventData as EpollEventData, EventFlags as EpollEventFlags, }; +pub use crate::ffi::c_char; pub use crate::fs::{ Advice, AtFlags, Mode, OFlags, RenameFlags, ResolveFlags, Statx, StatxFlags, XattrFlags, }; @@ -45,37 +46,6 @@ pub use linux_raw_sys::general::sigset_t; pub use net::{__kernel_sockaddr_storage as sockaddr_storage, msghdr, sockaddr, socklen_t}; -// Declare the `c_char` type for use with entries that take pointers -// to C strings. Define it as unsigned or signed according to the platform -// so that we match what Rust's `CStr` uses. -// -// When we can update to linux-raw-sys 0.5, we can remove this, as its -// `c_char` type will declare this. -/// The C `char` type. -#[cfg(any( - target_arch = "aarch64", - target_arch = "arm", - target_arch = "msp430", - target_arch = "powerpc", - target_arch = "powerpc64", - target_arch = "riscv32", - target_arch = "riscv64", - target_arch = "s390x", -))] -#[allow(non_camel_case_types)] -pub type c_char = u8; -/// The C `char` type. -#[cfg(any( - target_arch = "mips", - target_arch = "mips64", - target_arch = "sparc64", - target_arch = "x86", - target_arch = "x86_64", - target_arch = "xtensa", -))] -#[allow(non_camel_case_types)] -pub type c_char = i8; - mod sys { pub(super) use linux_raw_sys::io_uring::*; #[cfg(test)] diff --git a/src/ioctl/mod.rs b/src/ioctl/mod.rs index 2f0c95188..dec108f69 100644 --- a/src/ioctl/mod.rs +++ b/src/ioctl/mod.rs @@ -14,8 +14,8 @@ #![allow(unsafe_code)] -use crate::backend::c; use crate::fd::{AsFd, BorrowedFd}; +use crate::ffi as c; use crate::io::Result; #[cfg(any(linux_kernel, bsd))] diff --git a/src/net/types.rs b/src/net/types.rs index f41fa1fd8..e71fb4565 100644 --- a/src/net/types.rs +++ b/src/net/types.rs @@ -1,6 +1,7 @@ //! Types and constants for `rustix::net`. use crate::backend::c; +use crate::ffi; use bitflags::bitflags; /// A type for holding raw integer socket types. @@ -47,7 +48,7 @@ impl SocketType { } /// A type for holding raw integer address families. -pub type RawAddressFamily = c::sa_family_t; +pub type RawAddressFamily = crate::ffi::c_ushort; /// `AF_*` constants for use with [`socket`], [`socket_with`], and /// [`socketpair`]. @@ -1454,7 +1455,7 @@ bitflags! { /// [`acceptfrom_with`]: crate::net::acceptfrom_with #[repr(transparent)] #[derive(Copy, Clone, Eq, PartialEq, Hash, Debug)] - pub struct SocketFlags: c::c_uint { + pub struct SocketFlags: ffi::c_uint { /// `SOCK_NONBLOCK` #[cfg(not(any( apple, @@ -1785,8 +1786,9 @@ pub struct UCred { #[test] fn test_sizes() { + #[cfg(target_os = "linux")] use crate::backend::c; - use c::c_int; + use crate::ffi::c_int; use core::mem::transmute; // Backend code needs to cast these to `c_int` so make sure that cast isn't diff --git a/src/pid.rs b/src/pid.rs index 5f2b9ad2a..184d25a49 100644 --- a/src/pid.rs +++ b/src/pid.rs @@ -2,11 +2,10 @@ #![allow(unsafe_code)] -use crate::backend::c; use core::num::NonZeroI32; /// A process identifier as a raw integer. -pub type RawPid = c::pid_t; +pub type RawPid = i32; /// `pid_t`—A non-zero Unix process ID. /// diff --git a/src/prctl.rs b/src/prctl.rs index 0ea83031b..004493333 100644 --- a/src/prctl.rs +++ b/src/prctl.rs @@ -2,8 +2,8 @@ #![allow(unsafe_code)] -use crate::backend::c::{c_int, c_void}; use crate::backend::prctl::syscalls; +use crate::ffi::{c_int, c_void}; use crate::io; use crate::utils::as_mut_ptr; use bitflags::bitflags; diff --git a/src/process/pidfd.rs b/src/process/pidfd.rs index 404848e79..0755055d2 100644 --- a/src/process/pidfd.rs +++ b/src/process/pidfd.rs @@ -1,6 +1,6 @@ use crate::fd::OwnedFd; use crate::process::{Pid, Signal}; -use crate::{backend, io}; +use crate::{backend, ffi, io}; use backend::fd::AsFd; bitflags::bitflags! { @@ -9,7 +9,7 @@ bitflags::bitflags! { /// [`pidfd_open`]: crate::process::pidfd_open #[repr(transparent)] #[derive(Copy, Clone, Eq, PartialEq, Hash, Debug)] - pub struct PidfdFlags: backend::c::c_uint { + pub struct PidfdFlags: ffi::c_uint { /// `PIDFD_NONBLOCK`. const NONBLOCK = backend::c::PIDFD_NONBLOCK; diff --git a/src/process/pidfd_getfd.rs b/src/process/pidfd_getfd.rs index 20e5163b9..1be215e0f 100644 --- a/src/process/pidfd_getfd.rs +++ b/src/process/pidfd_getfd.rs @@ -2,7 +2,7 @@ #![allow(unsafe_code)] use crate::fd::OwnedFd; -use crate::{backend, io}; +use crate::{backend, ffi, io}; use backend::fd::{AsFd, RawFd}; /// Raw file descriptor in another process. @@ -17,7 +17,7 @@ bitflags::bitflags! { /// All flags are reserved for future use. #[repr(transparent)] #[derive(Copy, Clone, Eq, PartialEq, Hash, Debug)] - pub struct PidfdGetfdFlags: backend::c::c_uint { + pub struct PidfdGetfdFlags: ffi::c_uint { /// const _ = !0; } diff --git a/src/process/prctl.rs b/src/process/prctl.rs index 6a2465b6b..13d9fcbc9 100644 --- a/src/process/prctl.rs +++ b/src/process/prctl.rs @@ -10,10 +10,9 @@ use core::ptr::{null, null_mut, NonNull}; use bitflags::bitflags; -use crate::backend::c::{c_int, c_uint, c_void}; use crate::backend::prctl::syscalls; use crate::fd::{AsRawFd, BorrowedFd}; -use crate::ffi::CStr; +use crate::ffi::{c_int, c_uint, c_void, CStr}; use crate::io; use crate::prctl::*; use crate::process::{Pid, RawPid}; diff --git a/src/process/procctl.rs b/src/process/procctl.rs index a56d116c8..3b549fc4c 100644 --- a/src/process/procctl.rs +++ b/src/process/procctl.rs @@ -12,9 +12,9 @@ use core::ptr; use bitflags::bitflags; -use crate::backend::c::{c_int, c_uint, c_void}; use crate::backend::process::syscalls; use crate::backend::process::types::RawId; +use crate::ffi::{c_int, c_uint, c_void}; use crate::io; use crate::process::{Pid, RawPid}; use crate::signal::Signal; diff --git a/src/signal.rs b/src/signal.rs index cc608e728..b652b1a0f 100644 --- a/src/signal.rs +++ b/src/signal.rs @@ -1,4 +1,5 @@ use crate::backend::c; +use crate::ffi; /// A signal number for use with [`kill_process`], [`kill_process_group`], and /// [`kill_current_process_group`]. @@ -148,7 +149,7 @@ pub enum Signal { impl Signal { /// Convert a raw signal number into a `Signal`, if possible. - pub fn from_raw(sig: c::c_int) -> Option { + pub fn from_raw(sig: ffi::c_int) -> Option { match sig { c::SIGHUP => Some(Self::Hup), c::SIGINT => Some(Self::Int), diff --git a/src/termios/types.rs b/src/termios/types.rs index fcc295cbc..e29e8a4a0 100644 --- a/src/termios/types.rs +++ b/src/termios/types.rs @@ -1,4 +1,6 @@ -use crate::backend::c; +use crate::backend::{c, termios::types}; +#[cfg(target_os = "nto")] +use crate::ffi; use crate::{backend, io}; use bitflags::bitflags; @@ -35,7 +37,7 @@ pub struct Termios { target_os = "haiku", target_os = "redox" ))] - pub line_discipline: c::cc_t, + pub line_discipline: u8, /// How are various special control codes handled? #[doc(alias = "c_cc")] @@ -43,7 +45,7 @@ pub struct Termios { pub special_codes: SpecialCodes, #[cfg(target_os = "nto")] - pub(crate) __reserved: [c::c_uint; 3], + pub(crate) __reserved: [ffi::c_uint; 3], /// Line discipline. // On PowerPC, this field comes after `c_cc`. @@ -235,7 +237,7 @@ bitflags! { /// Flags controlling terminal input. #[repr(transparent)] #[derive(Copy, Clone, Eq, PartialEq, Hash, Debug)] - pub struct InputModes: c::tcflag_t { + pub struct InputModes: types::tcflag_t { /// `IGNBRK` const IGNBRK = c::IGNBRK; @@ -303,7 +305,7 @@ bitflags! { /// Flags controlling terminal output. #[repr(transparent)] #[derive(Copy, Clone, Eq, PartialEq, Hash, Debug)] - pub struct OutputModes: c::tcflag_t { + pub struct OutputModes: types::tcflag_t { /// `OPOST` const OPOST = c::OPOST; @@ -522,7 +524,7 @@ bitflags! { /// related functions. #[repr(transparent)] #[derive(Copy, Clone, Eq, PartialEq, Hash, Debug)] - pub struct ControlModes: c::tcflag_t { + pub struct ControlModes: types::tcflag_t { /// `CSIZE` const CSIZE = c::CSIZE; @@ -582,7 +584,7 @@ bitflags! { /// Flags controlling “local” terminal modes. #[repr(transparent)] #[derive(Copy, Clone, Eq, PartialEq, Hash, Debug)] - pub struct LocalModes: c::tcflag_t { + pub struct LocalModes: types::tcflag_t { /// `XCASE` #[cfg(any(linux_kernel, target_arch = "s390x", target_os = "haiku"))] const XCASE = c::XCASE; @@ -1101,7 +1103,7 @@ pub mod speed { pub struct SpecialCodes(pub(crate) [c::cc_t; c::NCCS as usize]); impl core::ops::Index for SpecialCodes { - type Output = c::cc_t; + type Output = u8; fn index(&self, index: SpecialCodeIndex) -> &Self::Output { &self.0[index.0] @@ -1356,6 +1358,8 @@ fn termios_layouts() { check_renamed_type!(OutputModes, tcflag_t); check_renamed_type!(ControlModes, tcflag_t); check_renamed_type!(LocalModes, tcflag_t); + assert_eq_size!(u8, libc::cc_t); + assert_eq_size!(types::tcflag_t, libc::tcflag_t); // On platforms with a termios/termios2 split, check `termios`. #[cfg(linux_raw)] diff --git a/src/thread/prctl.rs b/src/thread/prctl.rs index 08ba0a2d6..370778690 100644 --- a/src/thread/prctl.rs +++ b/src/thread/prctl.rs @@ -16,11 +16,10 @@ use core::sync::atomic::AtomicU8; use bitflags::bitflags; -use crate::backend::c::{c_int, c_uint, c_void}; use crate::backend::prctl::syscalls; -use crate::ffi::CStr; #[cfg(feature = "alloc")] use crate::ffi::CString; +use crate::ffi::{c_int, c_uint, c_void, CStr}; use crate::io; use crate::pid::Pid; use crate::prctl::{ diff --git a/src/time/timerfd.rs b/src/time/timerfd.rs index 7f661f7d9..ba4d6e483 100644 --- a/src/time/timerfd.rs +++ b/src/time/timerfd.rs @@ -1,7 +1,21 @@ use crate::fd::{AsFd, OwnedFd}; +use crate::timespec::Timespec; use crate::{backend, io}; -pub use backend::time::types::{Itimerspec, TimerfdClockId, TimerfdFlags, TimerfdTimerFlags}; +pub use backend::time::types::{TimerfdClockId, TimerfdFlags, TimerfdTimerFlags}; + +/// `struct itimerspec` for use with [`timerfd_gettime`] and +/// [`timerfd_settime`]. +/// +/// [`timerfd_gettime`]: crate::time::timerfd_gettime +/// [`timerfd_settime`]: crate::time::timerfd_settime +#[derive(Debug, Clone)] +pub struct Itimerspec { + /// Interval between times. + pub it_interval: Timespec, + /// Value of the time. + pub it_value: Timespec, +} /// `timerfd_create(clockid, flags)`—Create a timer. /// diff --git a/src/timespec.rs b/src/timespec.rs index a2df0e7b9..1e9b9b40f 100644 --- a/src/timespec.rs +++ b/src/timespec.rs @@ -1,15 +1,14 @@ //! `Timespec` and related types, which are used by multiple public API //! modules. -#[cfg(not(fix_y2038))] -use crate::backend::c; +#![allow(dead_code)] -/// `struct timespec` #[cfg(not(fix_y2038))] -pub type Timespec = c::timespec; +use crate::backend::c; +#[allow(unused)] +use crate::ffi; /// `struct timespec` -#[cfg(fix_y2038)] #[derive(Debug, Clone, Copy)] #[repr(C)] pub struct Timespec { @@ -21,12 +20,6 @@ pub struct Timespec { } /// A type for the `tv_sec` field of [`Timespec`]. -#[cfg(not(fix_y2038))] -#[allow(deprecated)] -pub type Secs = c::time_t; - -/// A type for the `tv_sec` field of [`Timespec`]. -#[cfg(fix_y2038)] pub type Secs = i64; /// A type for the `tv_sec` field of [`Timespec`]. @@ -43,7 +36,7 @@ pub type Nsecs = i64; libc, not(all(target_arch = "x86_64", target_pointer_width = "32")) ))] -pub type Nsecs = c::c_long; +pub type Nsecs = ffi::c_long; /// On 32-bit glibc platforms, `timespec` has anonymous padding fields, which /// Rust doesn't support yet (see `unnamed_fields`), so we define our own @@ -86,6 +79,26 @@ impl From for LibcTimespec { } } +#[cfg(not(fix_y2038))] +pub(crate) fn as_libc_timespec_ptr(timespec: &Timespec) -> *const c::timespec { + #[cfg(test)] + { + assert_eq_size!(Timespec, c::timespec); + } + crate::utils::as_ptr(timespec).cast::() +} + +#[cfg(not(fix_y2038))] +pub(crate) fn as_libc_timespec_mut_ptr( + timespec: &mut core::mem::MaybeUninit, +) -> *mut c::timespec { + #[cfg(test)] + { + assert_eq_size!(Timespec, c::timespec); + } + timespec.as_mut_ptr().cast::() +} + #[test] fn test_sizes() { assert_eq_size!(Secs, u64); @@ -113,3 +126,12 @@ fn test_sizes() { fn test_fix_y2038() { assert_eq_size!(libc::time_t, u32); } + +#[cfg(not(fix_y2038))] +#[test] +fn timespec_layouts() { + use crate::backend::c; + check_renamed_type!(Timespec, timespec); + #[cfg(linux_raw)] + assert_eq_size!(Timespec, linux_raw_sys::general::__kernel_timespec); +} diff --git a/src/ugid.rs b/src/ugid.rs index 57cc666dd..e9b0ca4c4 100644 --- a/src/ugid.rs +++ b/src/ugid.rs @@ -3,13 +3,14 @@ #![allow(unsafe_code)] use crate::backend::c; +use crate::ffi; /// A group identifier as a raw integer. #[cfg(not(target_os = "wasi"))] -pub type RawGid = c::gid_t; +pub type RawGid = ffi::c_uint; /// A user identifier as a raw integer. #[cfg(not(target_os = "wasi"))] -pub type RawUid = c::uid_t; +pub type RawUid = ffi::c_uint; /// `uid_t`—A Unix user ID. #[repr(transparent)] @@ -77,7 +78,10 @@ impl Gid { // Return the raw value of the IDs. In case of `None` it returns `!0` since it // has the same bit pattern as `-1` indicating no change to the owner/group ID. -pub(crate) fn translate_fchown_args(owner: Option, group: Option) -> (RawUid, RawGid) { +pub(crate) fn translate_fchown_args( + owner: Option, + group: Option, +) -> (c::uid_t, c::gid_t) { let ow = match owner { Some(o) => o.as_raw(), None => !0, @@ -88,11 +92,13 @@ pub(crate) fn translate_fchown_args(owner: Option, group: Option) -> ( None => !0, }; - (ow, gr) + (ow as c::uid_t, gr as c::gid_t) } #[test] fn test_sizes() { assert_eq_size!(RawUid, u32); assert_eq_size!(RawGid, u32); + assert_eq_size!(RawUid, libc::uid_t); + assert_eq_size!(RawGid, libc::gid_t); }