Skip to content

Commit c8cf6eb

Browse files
committed
Add 'bad' prefexis for read, write, and readwrite ioctls
1 parent e501b00 commit c8cf6eb

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
@@ -87,14 +87,15 @@
8787
//!
8888
//! More examples on using `ioctl!` can be found in the [rust-spidev crate](https://github.com/rust-embedded/rust-spidev).
8989
//!
90-
//! ```text
91-
//! pub unsafe fn $NAME(fd: c_int, val: *mut u8, len: usize) -> Result<c_int>;
92-
//! ```
90+
//! Using hard-coded ioctl numbers
91+
//! ------------------------------
9392
//!
9493
//! As mentioned earlier, there are many old `ioctl`s that do not use the newer method of
9594
//! generating `ioctl` numbers and instead use hardcoded values. These can be used with the `bad`
9695
//! form of the `ioctl!` macro (there is no data transfer direction used with `bad`). The naming of
97-
//! this comes from the Linux kernel which refers to these `ioctl`s as "bad".
96+
//! this comes from the Linux kernel which refers to these `ioctl`s as "bad". These are a different
97+
//! form as they bypass calling the macro that generates that ioctl number and instead use the
98+
//! defined value directly. These are supported by the `bad` prefix with the `ioctl!` macro.
9899
//!
99100
//! For example the `TCGETS` `ioctl` reads a `termios` data structure for a given file descriptor.
100101
//! It can be implemented as:
@@ -104,18 +105,20 @@
104105
//! # #[cfg(any(target_os = "android", target_os = "linux"))]
105106
//! # use nix::libc::TCGETS as TCGETS;
106107
//! # #[cfg(any(target_os = "android", target_os = "linux"))]
107-
//! ioctl!(bad tcgets with TCGETS);
108+
//! # use nix::libc::termios as termios;
109+
//! # #[cfg(any(target_os = "android", target_os = "linux"))]
110+
//! ioctl!(bad read tcgets with TCGETS; termios);
108111
//! # fn main() {}
109112
//! ```
110113
//!
111114
//! The generated function has the same form as that generated by `read`:
112115
//!
113116
//! ```text
114-
//! pub unsafe fn tcgets(fd: c_int, val: *mut u8) -> Result<c_int>;
117+
//! pub unsafe fn tcgets(fd: c_int, val: *mut termios) -> Result<c_int>;
115118
//! ```
116119
//!
117-
//! There is also a `bad none` form for use with hard-coded `ioctl`s that do not transfer data.
118-
//! The `TIOCEXCL` `ioctl` that's part of the termios API can be implemented as:
120+
//! There is also a `bad none`, `bad write`, and `bad readwrite` form that work similar to the
121+
//! standard `none`, `write`, and `readwrite` forms.
119122
//!
120123
//! ```
121124
//! # #[macro_use] extern crate nix;
@@ -164,17 +167,31 @@ macro_rules! convert_ioctl_res {
164167
/// Generates ioctl functions. See [::sys::ioctl](sys/ioctl/index.html).
165168
#[macro_export]
166169
macro_rules! ioctl {
167-
(bad $name:ident with $nr:expr) => (
170+
(bad none $name:ident with $nr:expr) => (
171+
pub unsafe fn $name(fd: $crate::libc::c_int)
172+
-> $crate::Result<$crate::libc::c_int> {
173+
convert_ioctl_res!($crate::libc::ioctl(fd, $nr as $crate::sys::ioctl::ioctl_num_type))
174+
}
175+
);
176+
(bad read $name:ident with $nr:expr; $ty:ty) => (
168177
pub unsafe fn $name(fd: $crate::libc::c_int,
169-
data: *mut u8)
178+
data: *mut $ty)
170179
-> $crate::Result<$crate::libc::c_int> {
171180
convert_ioctl_res!($crate::libc::ioctl(fd, $nr as $crate::sys::ioctl::ioctl_num_type, data))
172181
}
173182
);
174-
(bad none $name:ident with $nr:expr) => (
175-
pub unsafe fn $name(fd: $crate::libc::c_int)
183+
(bad write $name:ident with $nr:expr; $ty:ty) => (
184+
pub unsafe fn $name(fd: $crate::libc::c_int,
185+
data: &$ty)
176186
-> $crate::Result<$crate::libc::c_int> {
177-
convert_ioctl_res!($crate::libc::ioctl(fd, $nr as $crate::sys::ioctl::ioctl_num_type))
187+
convert_ioctl_res!($crate::libc::ioctl(fd, $nr as $crate::sys::ioctl::ioctl_num_type, data))
188+
}
189+
);
190+
(bad readwrite $name:ident with $nr:expr; $ty:ty) => (
191+
pub unsafe fn $name(fd: $crate::libc::c_int,
192+
data: *mut $ty)
193+
-> $crate::Result<$crate::libc::c_int> {
194+
convert_ioctl_res!($crate::libc::ioctl(fd, $nr as $crate::sys::ioctl::ioctl_num_type, data))
178195
}
179196
);
180197
(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 with 0, 0; u64);

0 commit comments

Comments
 (0)