Skip to content

Commit b44bc15

Browse files
committed
Add 'bad' prefixes for read, write, and readwrite ioctls
1 parent b84b79a commit b44bc15

File tree

2 files changed

+34
-15
lines changed

2 files changed

+34
-15
lines changed

src/sys/ioctl/mod.rs

Lines changed: 30 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -92,14 +92,15 @@
9292
//!
9393
//! More examples on using `ioctl!` can be found in the [rust-spidev crate](https://github.com/rust-embedded/rust-spidev).
9494
//!
95-
//! ```text
96-
//! pub unsafe fn $NAME(fd: c_int, data: *mut u8, len: usize) -> Result<c_int>;
97-
//! ```
95+
//! Using hard-coded ioctl numbers
96+
//! ------------------------------
9897
//!
9998
//! As mentioned earlier, there are many old `ioctl`s that do not use the newer method of
10099
//! generating `ioctl` numbers and instead use hardcoded values. These can be used with the `bad`
101100
//! form of the `ioctl!` macro (there is no data transfer direction used with `bad`). The naming of
102-
//! this comes from the Linux kernel which refers to these `ioctl`s as "bad".
101+
//! this comes from the Linux kernel which refers to these `ioctl`s as "bad". These are a different
102+
//! form as they bypass calling the macro that generates that ioctl number and instead use the
103+
//! defined value directly. These are supported by the `bad` prefix with the `ioctl!` macro.
103104
//!
104105
//! For example the `TCGETS` `ioctl` reads a `termios` data structure for a given file descriptor.
105106
//! It can be implemented as:
@@ -109,18 +110,20 @@
109110
//! # #[cfg(any(target_os = "android", target_os = "linux"))]
110111
//! # use nix::libc::TCGETS as TCGETS;
111112
//! # #[cfg(any(target_os = "android", target_os = "linux"))]
112-
//! ioctl!(bad tcgets with TCGETS);
113+
//! # use nix::libc::termios as termios;
114+
//! # #[cfg(any(target_os = "android", target_os = "linux"))]
115+
//! ioctl!(bad read tcgets with TCGETS; termios);
113116
//! # fn main() {}
114117
//! ```
115118
//!
116119
//! The generated function has the same form as that generated by `read`:
117120
//!
118121
//! ```text
119-
//! pub unsafe fn tcgets(fd: c_int, data: *mut u8) -> Result<c_int>;
122+
//! pub unsafe fn tcgets(fd: c_int, data: *mut termios) -> Result<c_int>;
120123
//! ```
121124
//!
122-
//! There is also a `bad none` form for use with hard-coded `ioctl`s that do not transfer data.
123-
//! The `TIOCEXCL` `ioctl` that's part of the termios API can be implemented as:
125+
//! There is also a `bad none`, `bad write`, and `bad readwrite` form that work similar to the
126+
//! standard `none`, `write`, and `readwrite` forms.
124127
//!
125128
//! ```
126129
//! # #[macro_use] extern crate nix;
@@ -169,17 +172,31 @@ macro_rules! convert_ioctl_res {
169172
/// Generates ioctl functions. See [::sys::ioctl](sys/ioctl/index.html).
170173
#[macro_export]
171174
macro_rules! ioctl {
172-
(bad $name:ident with $nr:expr) => (
175+
(bad none $name:ident with $nr:expr) => (
176+
pub unsafe fn $name(fd: $crate::libc::c_int)
177+
-> $crate::Result<$crate::libc::c_int> {
178+
convert_ioctl_res!($crate::libc::ioctl(fd, $nr as $crate::sys::ioctl::ioctl_num_type))
179+
}
180+
);
181+
(bad read $name:ident with $nr:expr; $ty:ty) => (
173182
pub unsafe fn $name(fd: $crate::libc::c_int,
174-
data: *mut u8)
183+
data: *mut $ty)
175184
-> $crate::Result<$crate::libc::c_int> {
176185
convert_ioctl_res!($crate::libc::ioctl(fd, $nr as $crate::sys::ioctl::ioctl_num_type, data))
177186
}
178187
);
179-
(bad none $name:ident with $nr:expr) => (
180-
pub unsafe fn $name(fd: $crate::libc::c_int)
188+
(bad write $name:ident with $nr:expr; $ty:ty) => (
189+
pub unsafe fn $name(fd: $crate::libc::c_int,
190+
data: *const $ty)
181191
-> $crate::Result<$crate::libc::c_int> {
182-
convert_ioctl_res!($crate::libc::ioctl(fd, $nr as $crate::sys::ioctl::ioctl_num_type))
192+
convert_ioctl_res!($crate::libc::ioctl(fd, $nr as $crate::sys::ioctl::ioctl_num_type, data))
193+
}
194+
);
195+
(bad readwrite $name:ident with $nr:expr; $ty:ty) => (
196+
pub unsafe fn $name(fd: $crate::libc::c_int,
197+
data: *mut $ty)
198+
-> $crate::Result<$crate::libc::c_int> {
199+
convert_ioctl_res!($crate::libc::ioctl(fd, $nr as $crate::sys::ioctl::ioctl_num_type, data))
183200
}
184201
);
185202
(none $name:ident with $ioty:expr, $nr:expr) => (

test/sys/test_ioctl.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
#![allow(dead_code)]
22

33
// Simple tests to ensure macro generated fns compile
4-
ioctl!(bad do_bad with 0x1234);
5-
ioctl!(bad none do_bad_none with 0x1234);
4+
ioctl!(bad none do_bad with 0x1234);
5+
ioctl!(bad read do_bad_read with 0x1234; u16);
6+
ioctl!(bad write do_bad_write with 0x1234; u8);
7+
ioctl!(bad readwrite do_bad_readwrite with 0x1234; u32);
68
ioctl!(none do_none with 0, 0);
79
ioctl!(read read_test with 0, 0; u32);
810
ioctl!(write write_test_u8 with 0, 0; u8);

0 commit comments

Comments
 (0)