#ioctl #file-descriptor #control-system #command #call #syscalls #io


IO Control for POSIX-and-beyond systems (numerous ioctl specifications, see ioctl-sys for core functions & macros)

5 releases

Uses old Rust 2015

0.6.2 Apr 23, 2024
0.6.1 Mar 16, 2021
0.6.0 Nov 17, 2020
0.5.1 Sep 18, 2016
0.5.0 Sep 13, 2016

#250 in Unix APIs

Download history 913/week @ 2024-03-13 988/week @ 2024-03-20 1449/week @ 2024-03-27 174/week @ 2024-04-03 107/week @ 2024-04-10 237/week @ 2024-04-17 187/week @ 2024-04-24 113/week @ 2024-05-01 106/week @ 2024-05-08 62/week @ 2024-05-15 44/week @ 2024-05-22 108/week @ 2024-05-29 42/week @ 2024-06-05 44/week @ 2024-06-12 142/week @ 2024-06-19 124/week @ 2024-06-26

380 downloads per month



ioctl-sys & ioctls

ioctl-sys Crates.io ioctls Crates.io ioctl-sys ioctl-sys Documentation ioctls ioctls Documentation

Helpers for binding ioctls in Rust. Currently supports Linux on all architectures except SPARC and Alpha. Other platforms welcome!

The ioctl-sys crate provides a basic interface to write your own ioctl wrappers.

The ioctls crate provides wrappers for a bunch of ioctls.

This library is pretty low-level and messy. ioctl is not fun.

What is an ioctl?

The ioctl function is the grab-bag system call on POSIX systems. Don't want to add a new syscall? Make it an ioctl! ioctl refers to both the syscall, and the commands that can be send with it. ioctl stands for "IO control", and the commands are always sent to a file descriptor.

What does this library support?

This library provides the ioctl! macro, for binding ioctls. It also tries to bind every ioctl supported by the system with said macro, but many ioctls require some amount of manual work to support (usually by providing structs or other types) that this library does not support yet.

Additionally, in etc, there are scripts for scraping system headers for ioctl definitions, and generating calls to ioctl! corresponding to them.

How do I get the magic numbers?

Look at your system's headers. For example, /usr/include/linux/input.h has a lot of lines defining macros which use _IOR, _IOW, _IOC, and _IORW. These macros correspond to the ior!, iow!, ioc!, and iorw! macros defined in this crate. Additionally, there is the ioctl! macro for creating a wrapper around ioctl that is somewhat more type-safe.

Most ioctls have no or little documentation. You'll need to scrounge through the source to figure out what they do and how they should be used.

How do I figure out an ioctl's calling convention?

For linux, you must look at the ioctl handlers in the kernel itself to determine how the value passed is being used. Look for the copy_from_user() and get_user() calls, these copy memory from userspace and may indicate that the ioctl's arg is a pointer. In othercases, the ioctl argument may simply be cast to an integer of some sort.


use ioctl_sys::ioctl;

ioctl!(bad kiocsound with 0x4B2F);
ioctl!(none drm_ioctl_set_master with b'd', 0x1e);
ioctl!(read ev_get_version with b'E', 0x01; u32);
ioctl!(write ev_set_repeat with b'E', 0x03; [u32; 2]);

fn main() {
    let mut x = 0;
    let ret = unsafe { ev_get_version(0, &mut x) };
    println!("returned {}, x = {}", ret, x);


Licensed under either of

at your option.


Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.