Skip to content

Commit 95cdffb

Browse files
gurchetansinghgsingh408
authored andcommitted
rustix: add ioctl wrappers
These ioctl wrappers are used by rustix users. For example: https://github.com/Smithay/drm-rs/blob/develop/drm-ffi/src/ioctl.rs#L6 Roughly, they match the ones provided by nix. I'm upstreaming the portion I have personally tested.
1 parent 3832726 commit 95cdffb

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

src/ioctl/mod.rs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -344,6 +344,36 @@ type _Opcode = c::c_uint;
344344
#[cfg(windows)]
345345
type _Opcode = i32;
346346

347+
/// Convenience macro for Rustix's ioctl write
348+
#[macro_export]
349+
macro_rules! ioctl_write_ptr {
350+
($name:ident, $ioty:expr, $nr:expr, $ty:ty) => {
351+
pub unsafe fn $name(fd: std::os::fd::BorrowedFd, data: &$ty) -> std::io::Result<()> {
352+
const OPCODE: $crate::ioctl::Opcode =
353+
$crate::ioctl::opcode::write::<$ty>($ioty as u8, $nr as u8);
354+
Ok(rustix::ioctl::ioctl(
355+
fd,
356+
$crate::ioctl::Setter::<OPCODE, $ty>::new(*data),
357+
)?)
358+
}
359+
};
360+
}
361+
362+
/// Convenience macro for Rustix's ioctl read
363+
#[macro_export]
364+
macro_rules! ioctl_readwrite {
365+
($name:ident, $ioty:expr, $nr:expr, $ty:ty) => {
366+
pub unsafe fn $name(fd: std::os::fd::BorrowedFd, data: &mut $ty) -> std::io::Result<()> {
367+
const OPCODE: $crate::ioctl::Opcode =
368+
$crate::ioctl::opcode::read_write::<$ty>($ioty as u8, $nr as u8);
369+
Ok($crate::ioctl::ioctl(
370+
fd,
371+
$crate::ioctl::Updater::<OPCODE, $ty>::new(data),
372+
)?)
373+
}
374+
};
375+
}
376+
347377
#[cfg(linux_raw_dep)]
348378
#[cfg(not(any(target_arch = "sparc", target_arch = "sparc64")))]
349379
#[cfg(test)]

0 commit comments

Comments
 (0)