diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-06-07 05:48:48 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-06-07 05:48:48 +0000 |
commit | ef24de24a82fe681581cc130f342363c47c0969a (patch) | |
tree | 0d494f7e1a38b95c92426f58fe6eaa877303a86c /vendor/rustix/src/ioctl | |
parent | Releasing progress-linux version 1.74.1+dfsg1-1~progress7.99u1. (diff) | |
download | rustc-ef24de24a82fe681581cc130f342363c47c0969a.tar.xz rustc-ef24de24a82fe681581cc130f342363c47c0969a.zip |
Merging upstream version 1.75.0+dfsg1.
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'vendor/rustix/src/ioctl')
-rw-r--r-- | vendor/rustix/src/ioctl/linux.rs | 49 | ||||
-rw-r--r-- | vendor/rustix/src/ioctl/mod.rs | 46 | ||||
-rw-r--r-- | vendor/rustix/src/ioctl/patterns.rs | 14 |
3 files changed, 87 insertions, 22 deletions
diff --git a/vendor/rustix/src/ioctl/linux.rs b/vendor/rustix/src/ioctl/linux.rs index 9dc278656..2f3599fc2 100644 --- a/vendor/rustix/src/ioctl/linux.rs +++ b/vendor/rustix/src/ioctl/linux.rs @@ -10,9 +10,9 @@ pub(super) const fn compose_opcode( num: RawOpcode, size: RawOpcode, ) -> RawOpcode { - macro_rules! shift_and_mask { + macro_rules! mask_and_shift { ($val:expr, $shift:expr, $mask:expr) => {{ - ($val << $shift) & $mask + ($val & $mask) << $shift }}; } @@ -23,10 +23,10 @@ pub(super) const fn compose_opcode( Direction::ReadWrite => READ | WRITE, }; - shift_and_mask!(group, GROUP_SHIFT, GROUP_MASK) - | shift_and_mask!(num, NUM_SHIFT, NUM_MASK) - | shift_and_mask!(size, SIZE_SHIFT, SIZE_MASK) - | shift_and_mask!(dir, DIR_SHIFT, DIR_MASK) + mask_and_shift!(group, GROUP_SHIFT, GROUP_MASK) + | mask_and_shift!(num, NUM_SHIFT, NUM_MASK) + | mask_and_shift!(size, SIZE_SHIFT, SIZE_MASK) + | mask_and_shift!(dir, DIR_SHIFT, DIR_MASK) } const NUM_BITS: RawOpcode = 8; @@ -50,7 +50,8 @@ const DIR_MASK: RawOpcode = (1 << DIR_BITS) - 1; target_arch = "aarch64", target_arch = "riscv32", target_arch = "riscv64", - target_arch = "loongarch64" + target_arch = "loongarch64", + target_arch = "csky" ))] mod consts { use super::RawOpcode; @@ -81,3 +82,37 @@ mod consts { pub(super) const SIZE_BITS: RawOpcode = 13; pub(super) const DIR_BITS: RawOpcode = 3; } + +#[cfg(not(any( + // These have no ioctl opcodes defined in linux_raw_sys + // so can't use that as a known-good value for this test. + target_arch = "sparc", + target_arch = "sparc64" +)))] +#[test] +fn check_known_opcodes() { + use crate::backend::c::{c_long, c_uint}; + use core::mem::size_of; + + // _IOR('U', 15, unsigned int) + assert_eq!( + compose_opcode( + Direction::Read, + b'U' as RawOpcode, + 15, + size_of::<c_uint>() as RawOpcode + ), + linux_raw_sys::ioctl::USBDEVFS_CLAIMINTERFACE as RawOpcode + ); + + // _IOW('v', 2, long) + assert_eq!( + compose_opcode( + Direction::Write, + b'v' as RawOpcode, + 2, + size_of::<c_long>() as RawOpcode + ), + linux_raw_sys::ioctl::FS_IOC_SETVERSION as RawOpcode + ); +} diff --git a/vendor/rustix/src/ioctl/mod.rs b/vendor/rustix/src/ioctl/mod.rs index bf2215ab6..ce62d75c4 100644 --- a/vendor/rustix/src/ioctl/mod.rs +++ b/vendor/rustix/src/ioctl/mod.rs @@ -46,13 +46,16 @@ use bsd as platform; /// controlling their behavior, some of which are proprietary. /// /// This crate exposes many other `ioctl` interfaces with safe and idiomatic -/// wrappers, like [`ioctl_fionbio`](crate::io::ioctl_fionbio) and -/// [`ioctl_fionread`](crate::io::ioctl_fionread). It is recommended to use -/// those instead of this function, as they are safer and more idiomatic. -/// For other cases, implement the [`Ioctl`] API and pass it to this function. +/// wrappers, like [`ioctl_fionbio`] and [`ioctl_fionread`]. It is recommended +/// to use those instead of this function, as they are safer and more +/// idiomatic. For other cases, implement the [`Ioctl`] API and pass it to this +/// function. /// /// See documentation for [`Ioctl`] for more information. /// +/// [`ioctl_fionbio`]: crate::io::ioctl_fionbio +/// [`ioctl_fionread`]: crate::io::ioctl_fionread +/// /// # Safety /// /// While [`Ioctl`] takes much of the unsafety out of `ioctl` calls, it is @@ -86,16 +89,16 @@ pub unsafe fn ioctl<F: AsFd, I: Ioctl>(fd: F, mut ioctl: I) -> Result<I::Output> let request = I::OPCODE.raw(); let arg = ioctl.as_ptr(); - // SAFETY: The variant of `Ioctl` asserts that this is a valid IOCTL call to - // make. + // SAFETY: The variant of `Ioctl` asserts that this is a valid IOCTL call + // to make. let output = if I::IS_MUTATING { _ioctl(fd, request, arg)? } else { _ioctl_readonly(fd, request, arg)? }; - // SAFETY: The variant of `Ioctl` asserts that this is a valid pointer to the - // output data. + // SAFETY: The variant of `Ioctl` asserts that this is a valid pointer to + // the output data. I::output_from_ptr(output, arg) } @@ -204,7 +207,9 @@ impl Opcode { Self { raw } } - /// Create a new opcode from a direction, group, number and size. + /// Create a new opcode from a direction, group, number, and size. + /// + /// This corresponds to the C macro `_IOC(direction, group, number, size)` #[cfg(any(linux_kernel, bsd))] #[inline] pub const fn from_components( @@ -225,8 +230,11 @@ impl Opcode { )) } - /// Create a new non-mutating opcode from a group, a number and the type of - /// data. + /// Create a new non-mutating opcode from a group, a number, and the type + /// of data. + /// + /// This corresponds to the C macro `_IO(group, number)` when `T` is zero + /// sized. #[cfg(any(linux_kernel, bsd))] #[inline] pub const fn none<T>(group: u8, number: u8) -> Self { @@ -235,6 +243,8 @@ impl Opcode { /// Create a new reading opcode from a group, a number and the type of /// data. + /// + /// This corresponds to the C macro `_IOR(group, number, T)`. #[cfg(any(linux_kernel, bsd))] #[inline] pub const fn read<T>(group: u8, number: u8) -> Self { @@ -243,6 +253,8 @@ impl Opcode { /// Create a new writing opcode from a group, a number and the type of /// data. + /// + /// This corresponds to the C macro `_IOW(group, number, T)`. #[cfg(any(linux_kernel, bsd))] #[inline] pub const fn write<T>(group: u8, number: u8) -> Self { @@ -251,6 +263,8 @@ impl Opcode { /// Create a new reading and writing opcode from a group, a number and the /// type of data. + /// + /// This corresponds to the C macro `_IOWR(group, number, T)`. #[cfg(any(linux_kernel, bsd))] #[inline] pub const fn read_write<T>(group: u8, number: u8) -> Self { @@ -314,8 +328,14 @@ type _RawOpcode = c::c_int; #[cfg(all(not(linux_raw), target_os = "android"))] type _RawOpcode = c::c_int; -// BSD, Haiku, and Redox use `unsigned long`. -#[cfg(any(bsd, target_os = "redox", target_os = "haiku"))] +// BSD, Haiku, Hurd, Redox, and Vita use `unsigned long`. +#[cfg(any( + bsd, + target_os = "redox", + target_os = "haiku", + target_os = "hurd", + target_os = "vita" +))] type _RawOpcode = c::c_ulong; // AIX, Emscripten, Fuchsia, Solaris, and WASI use a `int`. diff --git a/vendor/rustix/src/ioctl/patterns.rs b/vendor/rustix/src/ioctl/patterns.rs index 4b33d7d80..7bc5c4e86 100644 --- a/vendor/rustix/src/ioctl/patterns.rs +++ b/vendor/rustix/src/ioctl/patterns.rs @@ -6,6 +6,7 @@ use crate::backend::c; use crate::io::Result; use core::marker::PhantomData; +use core::ptr::addr_of_mut; use core::{fmt, mem}; /// Implements an `ioctl` with no real arguments. @@ -49,7 +50,7 @@ unsafe impl<Opcode: CompileTimeOpcode> Ioctl for NoArg<Opcode> { } } -/// Implements the traditional "getter" pattern for `ioctl`s. +/// Implements the traditional “getter” pattern for `ioctl`s. /// /// Some `ioctl`s just read data into the userspace. As this is a popular /// pattern this structure implements it. @@ -144,7 +145,7 @@ unsafe impl<Opcode: CompileTimeOpcode, Input> Ioctl for Setter<Opcode, Input> { const OPCODE: self::Opcode = Opcode::OPCODE; fn as_ptr(&mut self) -> *mut c::c_void { - &mut self.input as *mut Input as *mut c::c_void + addr_of_mut!(self.input).cast::<c::c_void>() } unsafe fn output_from_ptr(_: IoctlOutput, _: *mut c::c_void) -> Result<Self::Output> { @@ -166,6 +167,8 @@ impl<const OPCODE: RawOpcode> CompileTimeOpcode for BadOpcode<OPCODE> { } /// Provides a read code at compile time. +/// +/// This corresponds to the C macro `_IOR(GROUP, NUM, Data)`. #[cfg(any(linux_kernel, bsd))] pub struct ReadOpcode<const GROUP: u8, const NUM: u8, Data>(Data); @@ -175,6 +178,8 @@ impl<const GROUP: u8, const NUM: u8, Data> CompileTimeOpcode for ReadOpcode<GROU } /// Provides a write code at compile time. +/// +/// This corresponds to the C macro `_IOW(GROUP, NUM, Data)`. #[cfg(any(linux_kernel, bsd))] pub struct WriteOpcode<const GROUP: u8, const NUM: u8, Data>(Data); @@ -184,6 +189,8 @@ impl<const GROUP: u8, const NUM: u8, Data> CompileTimeOpcode for WriteOpcode<GRO } /// Provides a read/write code at compile time. +/// +/// This corresponds to the C macro `_IOWR(GROUP, NUM, Data)`. #[cfg(any(linux_kernel, bsd))] pub struct ReadWriteOpcode<const GROUP: u8, const NUM: u8, Data>(Data); @@ -193,6 +200,9 @@ impl<const GROUP: u8, const NUM: u8, Data> CompileTimeOpcode for ReadWriteOpcode } /// Provides a `None` code at compile time. +/// +/// This corresponds to the C macro `_IO(GROUP, NUM)` when `Data` is zero +/// sized. #[cfg(any(linux_kernel, bsd))] pub struct NoneOpcode<const GROUP: u8, const NUM: u8, Data>(Data); |