Function _IOW

Source
pub const fn _IOW<T>(ty: u8, nr: u8) -> Ioctl<*const T>
Expand description

Creates an Ioctl that writes data of type T to the kernel.

A pointer to the data will be passed to ioctl(2), and the kernel will read the argument from that location.

§Errors

This method will cause a compile-time assertion failure if the size of T exceeds the ioctl argument size limit. This typically means that the wrong type T was specified.

§Example

The uinput ioctl UI_DEV_SETUP can be invoked using _IOW.

From linux/uinput.h:

/* ioctl */
#define UINPUT_IOCTL_BASE	'U'
#define UI_DEV_CREATE		_IO(UINPUT_IOCTL_BASE, 1)
#define UI_DEV_DESTROY		_IO(UINPUT_IOCTL_BASE, 2)
...
#define UI_DEV_SETUP _IOW(UINPUT_IOCTL_BASE, 3, struct uinput_setup)
use std::{mem, fs::File, ffi::c_char};
use libc::uinput_setup;
use linux_ioctl::*;

const UINPUT_IOCTL_BASE: u8 = b'U';
const UI_DEV_CREATE: Ioctl<NoArgs> = _IO(UINPUT_IOCTL_BASE, 1);
const UI_DEV_DESTROY: Ioctl<NoArgs> = _IO(UINPUT_IOCTL_BASE, 2);
const UI_DEV_SETUP: Ioctl<*const uinput_setup> = _IOW(UINPUT_IOCTL_BASE, 3);

let uinput = File::options().write(true).open("/dev/uinput")?;

let mut setup: libc::uinput_setup = unsafe { mem::zeroed() };
setup.name[0] = b'A' as c_char; // (must not be blank)
unsafe {
    UI_DEV_SETUP.ioctl(&uinput, &setup)?;
    UI_DEV_CREATE.ioctl(&uinput)?;
    // ...use the device...
    UI_DEV_DESTROY.ioctl(&uinput)?;
}