Skip to content

Commit 02e8367

Browse files
author
Bryant Mairs
committed
Refactor the ioctl API and documentation
* Split `ioctl!` into separate macros. This makes documentation easier to read. * For every `ioctl_*!` macro include a description of the macro arguments as, the function prototype for the generated wrapper function, and an example if we have one. * Expose `request_code_*!` in the documentation to make the `ioctl_*_bad` macros easier to use. * Reorganize the file hierarchy to be simpler
1 parent 2f09d4c commit 02e8367

File tree

6 files changed

+619
-252
lines changed

6 files changed

+619
-252
lines changed

src/sys/ioctl/platform/bsd.rs renamed to src/sys/ioctl/bsd.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
pub type ioctl_num_type = ::libc::c_ulong;
44

55
mod consts {
6-
use ::sys::ioctl::platform::ioctl_num_type;
6+
use ::sys::ioctl::ioctl_num_type;
77
#[doc(hidden)]
88
pub const VOID: ioctl_num_type = 0x2000_0000;
99
#[doc(hidden)]
@@ -28,24 +28,24 @@ macro_rules! ioc {
2828

2929
#[macro_export]
3030
#[doc(hidden)]
31-
macro_rules! io {
31+
macro_rules! request_code_none {
3232
($g:expr, $n:expr) => (ioc!($crate::sys::ioctl::VOID, $g, $n, 0))
3333
}
3434

3535
#[macro_export]
3636
#[doc(hidden)]
37-
macro_rules! ior {
37+
macro_rules! request_code_read {
3838
($g:expr, $n:expr, $len:expr) => (ioc!($crate::sys::ioctl::OUT, $g, $n, $len))
3939
}
4040

4141
#[macro_export]
4242
#[doc(hidden)]
43-
macro_rules! iow {
43+
macro_rules! request_code_write {
4444
($g:expr, $n:expr, $len:expr) => (ioc!($crate::sys::ioctl::IN, $g, $n, $len))
4545
}
4646

4747
#[macro_export]
4848
#[doc(hidden)]
49-
macro_rules! iorw {
49+
macro_rules! request_code_readwrite {
5050
($g:expr, $n:expr, $len:expr) => (ioc!($crate::sys::ioctl::INOUT, $g, $n, $len))
5151
}

src/sys/ioctl/platform/linux.rs renamed to src/sys/ioctl/linux.rs

Lines changed: 45 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -25,17 +25,6 @@ mod consts {
2525
pub const DIRBITS: u8 = 3;
2626
}
2727

28-
#[cfg(not(any(target_arch = "powerpc",
29-
target_arch = "mips",
30-
target_arch = "mips64",
31-
target_arch = "x86",
32-
target_arch = "arm",
33-
target_arch = "x86_64",
34-
target_arch = "powerpc64",
35-
target_arch = "s390x",
36-
target_arch = "aarch64")))]
37-
use this_arch_not_supported;
38-
3928
// "Generic" ioctl protocol
4029
#[cfg(any(target_arch = "x86",
4130
target_arch = "arm",
@@ -86,30 +75,63 @@ macro_rules! ioc {
8675
(($sz as $crate::sys::ioctl::ioctl_num_type & $crate::sys::ioctl::SIZEMASK) << $crate::sys::ioctl::SIZESHIFT))
8776
}
8877

89-
/// Encode an ioctl command that has no associated data.
78+
/// Generate an ioctl request code for a command that passes no data.
79+
///
80+
/// This is equivalent to the `_IO()` macro exposed by the C ioctl API.
81+
///
82+
/// You should only use this macro directly if the `ioctl` you're working
83+
/// with is "bad" and you cannot use `ioctl_none!()` directly.
84+
///
85+
/// # Example
86+
///
87+
/// ```
88+
/// # #[macro_use] extern crate nix;
89+
/// const KVMIO: u8 = 0xAE;
90+
/// ioctl_write_int_bad!(kvm_create_vm, request_code_none!(KVMIO, 0x03));
91+
/// # fn main() {}
92+
/// ```
9093
#[macro_export]
91-
#[doc(hidden)]
92-
macro_rules! io {
94+
macro_rules! request_code_none {
9395
($ty:expr, $nr:expr) => (ioc!($crate::sys::ioctl::NONE, $ty, $nr, 0))
9496
}
9597

96-
/// Encode an ioctl command that reads.
98+
/// Generate an ioctl request code for a command that reads.
99+
///
100+
/// This is equivalent to the `_IOR()` macro exposed by the C ioctl API.
101+
///
102+
/// You should only use this macro directly if the `ioctl` you're working
103+
/// with is "bad" and you cannot use `ioctl_read!()` directly.
104+
///
105+
/// The read/write direction is relative to userland, so this
106+
/// command would be userland is reading and the kernel is
107+
/// writing.
97108
#[macro_export]
98-
#[doc(hidden)]
99-
macro_rules! ior {
109+
macro_rules! request_code_read {
100110
($ty:expr, $nr:expr, $sz:expr) => (ioc!($crate::sys::ioctl::READ, $ty, $nr, $sz))
101111
}
102112

103-
/// Encode an ioctl command that writes.
113+
/// Generate an ioctl request code for a command that writes.
114+
///
115+
/// This is equivalent to the `_IOW()` macro exposed by the C ioctl API.
116+
///
117+
/// You should only use this macro directly if the `ioctl` you're working
118+
/// with is "bad" and you cannot use `ioctl_write!()` directly.
119+
///
120+
/// The read/write direction is relative to userland, so this
121+
/// command would be userland is writing and the kernel is
122+
/// reading.
104123
#[macro_export]
105-
#[doc(hidden)]
106-
macro_rules! iow {
124+
macro_rules! request_code_write {
107125
($ty:expr, $nr:expr, $sz:expr) => (ioc!($crate::sys::ioctl::WRITE, $ty, $nr, $sz))
108126
}
109127

110-
/// Encode an ioctl command that both reads and writes.
128+
/// Generate an ioctl request code for a command that reads and writes.
129+
///
130+
/// This is equivalent to the `_IOWR()` macro exposed by the C ioctl API.
131+
///
132+
/// You should only use this macro directly if the `ioctl` you're working
133+
/// with is "bad" and you cannot use `ioctl_readwrite!()` directly.
111134
#[macro_export]
112-
#[doc(hidden)]
113-
macro_rules! iorw {
135+
macro_rules! request_code_readwrite {
114136
($ty:expr, $nr:expr, $sz:expr) => (ioc!($crate::sys::ioctl::READ | $crate::sys::ioctl::WRITE, $ty, $nr, $sz))
115137
}

0 commit comments

Comments
 (0)