Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Linux custom ioctl opcode computation #861

Merged
merged 3 commits into from
Sep 30, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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
36 changes: 35 additions & 1 deletion src/ioctl/linux.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ pub(super) const fn compose_opcode(
) -> RawOpcode {
macro_rules! shift_and_mask {
($val:expr, $shift:expr, $mask:expr) => {{
($val << $shift) & $mask
sunfishcode marked this conversation as resolved.
Show resolved Hide resolved
($val & $mask) << $shift
}};
}

Expand Down Expand Up @@ -81,3 +81,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
);
}
10 changes: 10 additions & 0 deletions src/ioctl/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,8 @@ impl Opcode {
}

/// 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(
Expand All @@ -227,6 +229,8 @@ impl Opcode {

/// 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 {
Expand All @@ -235,6 +239,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 {
Expand All @@ -243,6 +249,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 {
Expand All @@ -251,6 +259,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 {
Expand Down
8 changes: 8 additions & 0 deletions src/ioctl/patterns.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,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);

Expand All @@ -175,6 +177,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);

Expand All @@ -184,6 +188,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);

Expand All @@ -193,6 +199,8 @@ 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);

Expand Down