From 1b6a04ca5504955c571d1c97504fb45ea0befee4 Mon Sep 17 00:00:00 2001 From: Valentin Popov Date: Mon, 8 Jan 2024 01:21:28 +0400 Subject: Initial vendor packages Signed-off-by: Valentin Popov --- vendor/rustix/src/ioctl/patterns.rs | 256 ++++++++++++++++++++++++++++++++++++ 1 file changed, 256 insertions(+) create mode 100644 vendor/rustix/src/ioctl/patterns.rs (limited to 'vendor/rustix/src/ioctl/patterns.rs') diff --git a/vendor/rustix/src/ioctl/patterns.rs b/vendor/rustix/src/ioctl/patterns.rs new file mode 100644 index 0000000..6cf7ebd --- /dev/null +++ b/vendor/rustix/src/ioctl/patterns.rs @@ -0,0 +1,256 @@ +//! Implements typical patterns for `ioctl` usage. + +use super::{Ioctl, IoctlOutput, Opcode, RawOpcode}; + +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. +pub struct NoArg { + /// The opcode. + _opcode: PhantomData, +} + +impl fmt::Debug for NoArg { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + f.debug_tuple("NoArg").field(&Opcode::OPCODE).finish() + } +} + +impl NoArg { + /// Create a new no-argument `ioctl` object. + /// + /// # Safety + /// + /// - `Opcode` must provide a valid opcode. + #[inline] + pub unsafe fn new() -> Self { + Self { + _opcode: PhantomData, + } + } +} + +unsafe impl Ioctl for NoArg { + type Output = (); + + const IS_MUTATING: bool = false; + const OPCODE: self::Opcode = Opcode::OPCODE; + + fn as_ptr(&mut self) -> *mut c::c_void { + core::ptr::null_mut() + } + + unsafe fn output_from_ptr(_: IoctlOutput, _: *mut c::c_void) -> Result { + Ok(()) + } +} + +/// 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. +pub struct Getter { + /// The output data. + output: mem::MaybeUninit, + + /// The opcode. + _opcode: PhantomData, +} + +impl fmt::Debug for Getter { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + f.debug_tuple("Getter").field(&Opcode::OPCODE).finish() + } +} + +impl Getter { + /// Create a new getter-style `ioctl` object. + /// + /// # Safety + /// + /// - `Opcode` must provide a valid opcode. + /// - For this opcode, `Output` must be the type that the kernel expects to + /// write into. + #[inline] + pub unsafe fn new() -> Self { + Self { + output: mem::MaybeUninit::uninit(), + _opcode: PhantomData, + } + } +} + +unsafe impl Ioctl for Getter { + type Output = Output; + + const IS_MUTATING: bool = true; + const OPCODE: self::Opcode = Opcode::OPCODE; + + fn as_ptr(&mut self) -> *mut c::c_void { + self.output.as_mut_ptr().cast() + } + + unsafe fn output_from_ptr(_: IoctlOutput, ptr: *mut c::c_void) -> Result { + Ok(ptr.cast::().read()) + } +} + +/// Implements the pattern for `ioctl`s where a pointer argument is given to +/// the `ioctl`. +/// +/// The opcode must be read-only. +pub struct Setter { + /// The input data. + input: Input, + + /// The opcode. + _opcode: PhantomData, +} + +impl fmt::Debug for Setter { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + f.debug_tuple("Setter") + .field(&Opcode::OPCODE) + .field(&self.input) + .finish() + } +} + +impl Setter { + /// Create a new pointer setter-style `ioctl` object. + /// + /// # Safety + /// + /// - `Opcode` must provide a valid opcode. + /// - For this opcode, `Input` must be the type that the kernel expects to + /// get. + #[inline] + pub unsafe fn new(input: Input) -> Self { + Self { + input, + _opcode: PhantomData, + } + } +} + +unsafe impl Ioctl for Setter { + type Output = (); + + const IS_MUTATING: bool = false; + const OPCODE: self::Opcode = Opcode::OPCODE; + + fn as_ptr(&mut self) -> *mut c::c_void { + addr_of_mut!(self.input).cast::() + } + + unsafe fn output_from_ptr(_: IoctlOutput, _: *mut c::c_void) -> Result { + Ok(()) + } +} + +/// Implements an “updater” pattern for `ioctl`s. +/// +/// The ioctl takes a reference to a struct that it reads its input from, +/// then writes output to the same struct. +pub struct Updater<'a, Opcode, Value> { + /// Reference to input/output data. + value: &'a mut Value, + + /// The opcode. + _opcode: PhantomData, +} + +impl<'a, Opcode: CompileTimeOpcode, Value> Updater<'a, Opcode, Value> { + /// Create a new pointer updater-style `ioctl` object. + /// + /// # Safety + /// + /// - `Opcode` must provide a valid opcode. + /// - For this opcode, `Value` must be the type that the kernel expects to + /// get. + #[inline] + pub unsafe fn new(value: &'a mut Value) -> Self { + Self { + value, + _opcode: PhantomData, + } + } +} + +unsafe impl<'a, Opcode: CompileTimeOpcode, T> Ioctl for Updater<'a, Opcode, T> { + type Output = (); + + const IS_MUTATING: bool = true; + const OPCODE: self::Opcode = Opcode::OPCODE; + + fn as_ptr(&mut self) -> *mut c::c_void { + (self.value as *mut T).cast() + } + + unsafe fn output_from_ptr(_output: IoctlOutput, _ptr: *mut c::c_void) -> Result<()> { + Ok(()) + } +} + +/// Trait for something that provides an `ioctl` opcode at compile time. +pub trait CompileTimeOpcode { + /// The opcode. + const OPCODE: Opcode; +} + +/// Provides a bad opcode at compile time. +pub struct BadOpcode; + +impl CompileTimeOpcode for BadOpcode { + const OPCODE: Opcode = Opcode::old(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(Data); + +#[cfg(any(linux_kernel, bsd))] +impl CompileTimeOpcode for ReadOpcode { + const OPCODE: Opcode = Opcode::read::(GROUP, NUM); +} + +/// 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(Data); + +#[cfg(any(linux_kernel, bsd))] +impl CompileTimeOpcode for WriteOpcode { + const OPCODE: Opcode = Opcode::write::(GROUP, NUM); +} + +/// 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(Data); + +#[cfg(any(linux_kernel, bsd))] +impl CompileTimeOpcode for ReadWriteOpcode { + const OPCODE: Opcode = Opcode::read_write::(GROUP, NUM); +} + +/// 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(Data); + +#[cfg(any(linux_kernel, bsd))] +impl CompileTimeOpcode for NoneOpcode { + const OPCODE: Opcode = Opcode::none::(GROUP, NUM); +} -- cgit v1.2.3