Skip to content

Commit

Permalink
Make Ioctl::opcode a method instead of a constant
Browse files Browse the repository at this point in the history
  • Loading branch information
coolreader18 committed Jan 24, 2025
1 parent f377e85 commit 0df7462
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 16 deletions.
5 changes: 4 additions & 1 deletion src/fs/ioctl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,10 @@ unsafe impl ioctl::Ioctl for Ficlone<'_> {
type Output = ();

const IS_MUTATING: bool = false;
const OPCODE: ioctl::Opcode = ioctl::Opcode::old(c::FICLONE as ioctl::RawOpcode);

fn opcode(&self) -> ioctl::Opcode {
ioctl::Opcode::old(c::FICLONE as ioctl::RawOpcode)
}

fn as_ptr(&mut self) -> *mut c::c_void {
self.0.as_raw_fd() as *mut c::c_void
Expand Down
16 changes: 8 additions & 8 deletions src/ioctl/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ use bsd as platform;
#[inline]
pub unsafe fn ioctl<F: AsFd, I: Ioctl>(fd: F, mut ioctl: I) -> Result<I::Output> {
let fd = fd.as_fd();
let request = I::OPCODE.raw();
let request = ioctl.opcode().raw();
let arg = ioctl.as_ptr();

// SAFETY: The variant of `Ioctl` asserts that this is a valid IOCTL call
Expand Down Expand Up @@ -138,7 +138,7 @@ unsafe fn _ioctl_readonly(
/// output as indicated by `output`.
/// - That `output_from_ptr` can safely take the pointer from `as_ptr` and cast
/// it to the correct type, *only* after the `ioctl` call.
/// - That `OPCODE` uniquely identifies the `ioctl` call.
/// - That the return value of `opcode` uniquely identifies the `ioctl` call.
/// - That, for whatever platforms you are targeting, the `ioctl` call is safe
/// to make.
/// - If `IS_MUTATING` is false, that no userspace data will be modified by the
Expand All @@ -150,12 +150,6 @@ pub unsafe trait Ioctl {
/// type.
type Output;

/// The opcode used by this `ioctl` command.
///
/// There are different types of opcode depending on the operation. See
/// documentation for the [`Opcode`] struct for more information.
const OPCODE: Opcode;

/// Does the `ioctl` mutate any data in the userspace?
///
/// If the `ioctl` call does not mutate any data in the userspace, then
Expand All @@ -169,6 +163,12 @@ pub unsafe trait Ioctl {
/// to `false` when it should be `true`.
const IS_MUTATING: bool;

/// Get the opcode used by this `ioctl` command.
///
/// There are different types of opcode depending on the operation. See
/// documentation for the [`Opcode`] struct for more information.
fn opcode(&self) -> Opcode;

/// Get a pointer to the data to be passed to the `ioctl` command.
///
/// See trait-level documentation for more information.
Expand Down
25 changes: 20 additions & 5 deletions src/ioctl/patterns.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,10 @@ unsafe impl<Opcode: CompileTimeOpcode> Ioctl for NoArg<Opcode> {
type Output = ();

const IS_MUTATING: bool = false;
const OPCODE: self::Opcode = Opcode::OPCODE;

fn opcode(&self) -> self::Opcode {
Opcode::OPCODE
}

fn as_ptr(&mut self) -> *mut c::c_void {
core::ptr::null_mut()
Expand Down Expand Up @@ -89,7 +92,10 @@ unsafe impl<Opcode: CompileTimeOpcode, Output> Ioctl for Getter<Opcode, Output>
type Output = Output;

const IS_MUTATING: bool = true;
const OPCODE: self::Opcode = Opcode::OPCODE;

fn opcode(&self) -> self::Opcode {
Opcode::OPCODE
}

fn as_ptr(&mut self) -> *mut c::c_void {
self.output.as_mut_ptr().cast()
Expand Down Expand Up @@ -142,7 +148,10 @@ unsafe impl<Opcode: CompileTimeOpcode, Input> Ioctl for Setter<Opcode, Input> {
type Output = ();

const IS_MUTATING: bool = false;
const OPCODE: self::Opcode = Opcode::OPCODE;

fn opcode(&self) -> self::Opcode {
Opcode::OPCODE
}

fn as_ptr(&mut self) -> *mut c::c_void {
addr_of_mut!(self.input).cast::<c::c_void>()
Expand Down Expand Up @@ -186,7 +195,10 @@ 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 opcode(&self) -> self::Opcode {
Opcode::OPCODE
}

fn as_ptr(&mut self) -> *mut c::c_void {
(self.value as *mut T).cast()
Expand Down Expand Up @@ -244,7 +256,10 @@ unsafe impl<Opcode: CompileTimeOpcode> Ioctl for IntegerSetter<Opcode> {
type Output = ();

const IS_MUTATING: bool = false;
const OPCODE: self::Opcode = Opcode::OPCODE;

fn opcode(&self) -> self::Opcode {
Opcode::OPCODE
}

fn as_ptr(&mut self) -> *mut c::c_void {
self.value
Expand Down
5 changes: 4 additions & 1 deletion src/process/ioctl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,10 @@ unsafe impl ioctl::Ioctl for Tiocsctty {
type Output = ();

const IS_MUTATING: bool = false;
const OPCODE: ioctl::Opcode = ioctl::Opcode::old(c::TIOCSCTTY as ioctl::RawOpcode);

fn opcode(&self) -> ioctl::Opcode {
ioctl::Opcode::old(c::TIOCSCTTY as ioctl::RawOpcode)
}

fn as_ptr(&mut self) -> *mut c::c_void {
(&0_u32) as *const u32 as *mut c::c_void
Expand Down
5 changes: 4 additions & 1 deletion src/pty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,10 @@ unsafe impl ioctl::Ioctl for Tiocgptpeer {
type Output = OwnedFd;

const IS_MUTATING: bool = false;
const OPCODE: ioctl::Opcode = ioctl::Opcode::old(c::TIOCGPTPEER as ioctl::RawOpcode);

fn opcode(&self) -> ioctl::Opcode {
ioctl::Opcode::old(c::TIOCGPTPEER as ioctl::RawOpcode)
}

fn as_ptr(&mut self) -> *mut c::c_void {
self.0.bits() as *mut c::c_void
Expand Down

0 comments on commit 0df7462

Please sign in to comment.