Skip to content

Commit 13f66d0

Browse files
bors[bot]Bryant Mairs
andcommitted
Merge #833
833: Refactor the ioctl API and documentation r=posborne a=Susurrus I still need to flesh out the docs for the different `ioctl_*!` variants that now exist. I separated them into different macros so they can have their own docs, which should help discoverability. And when macro namespacing comes around this will be a pretty neatly documented API I think (though we'll likely want to rename these macros again at that point). * Split `ioctl!` into separate macros. This makes documentation easier to read. * Expose `request_code_*!` in the documentation to make the `ioctl_*_bad` macros easier to use. * Reorganize the file hierarchy to be simpler cc @gabrielesvelto @posborne @jethrogb Co-authored-by: Bryant Mairs <bryantmairs@google.com>
2 parents 31e901b + 5dad660 commit 13f66d0

File tree

8 files changed

+753
-302
lines changed

8 files changed

+753
-302
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ This project adheres to [Semantic Versioning](http://semver.org/).
2929
([#825](https://github.com/nix-rust/nix/pull/825))
3030
- Added `fchmod`, `fchmodat`.
3131
([#857](https://github.com/nix-rust/nix/pull/857))
32+
- Added `request_code_write_int!` on FreeBSD/DragonFlyBSD
33+
([#833](https://github.com/nix-rust/nix/pull/833))
3234

3335
### Changed
3436
- Display and Debug for SysControlAddr now includes all fields.
@@ -37,6 +39,10 @@ This project adheres to [Semantic Versioning](http://semver.org/).
3739
### Fixed
3840
- Properly exposed 460800 and 921600 baud rates on NetBSD
3941
([#837](https://github.com/nix-rust/nix/pull/837))
42+
- Fixed `ioctl_write_int!` on FreeBSD/DragonFlyBSD
43+
([#833](https://github.com/nix-rust/nix/pull/833))
44+
- `ioctl_write_int!` now properly supports passing a `c_ulong` as the parameter on Linux non-musl targets
45+
([#833](https://github.com/nix-rust/nix/pull/833))
4046

4147
### Removed
4248
- Removed explicit support for the `bytes` crate from the `sys::aio` module.

src/sys/ioctl/bsd.rs

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
/// The datatype used for the ioctl number
2+
#[doc(hidden)]
3+
pub type ioctl_num_type = ::libc::c_ulong;
4+
/// The datatype used for the 3rd argument
5+
#[doc(hidden)]
6+
pub type ioctl_param_type = ::libc::c_int;
7+
8+
mod consts {
9+
use ::sys::ioctl::ioctl_num_type;
10+
#[doc(hidden)]
11+
pub const VOID: ioctl_num_type = 0x2000_0000;
12+
#[doc(hidden)]
13+
pub const OUT: ioctl_num_type = 0x4000_0000;
14+
#[doc(hidden)]
15+
pub const IN: ioctl_num_type = 0x8000_0000;
16+
#[doc(hidden)]
17+
pub const INOUT: ioctl_num_type = (IN|OUT);
18+
#[doc(hidden)]
19+
pub const IOCPARM_MASK: ioctl_num_type = 0x1fff;
20+
}
21+
22+
pub use self::consts::*;
23+
24+
#[macro_export]
25+
#[doc(hidden)]
26+
macro_rules! ioc {
27+
($inout:expr, $group:expr, $num:expr, $len:expr) => (
28+
$inout | (($len as $crate::sys::ioctl::ioctl_num_type & $crate::sys::ioctl::IOCPARM_MASK) << 16) | (($group as $crate::sys::ioctl::ioctl_num_type) << 8) | ($num as $crate::sys::ioctl::ioctl_num_type)
29+
)
30+
}
31+
32+
/// Generate an ioctl request code for a command that passes no data.
33+
///
34+
/// This is equivalent to the `_IO()` macro exposed by the C ioctl API.
35+
///
36+
/// You should only use this macro directly if the `ioctl` you're working
37+
/// with is "bad" and you cannot use `ioctl_none!()` directly.
38+
///
39+
/// # Example
40+
///
41+
/// ```
42+
/// # #[macro_use] extern crate nix;
43+
/// const KVMIO: u8 = 0xAE;
44+
/// ioctl_write_int_bad!(kvm_create_vm, request_code_none!(KVMIO, 0x03));
45+
/// # fn main() {}
46+
/// ```
47+
#[macro_export]
48+
macro_rules! request_code_none {
49+
($g:expr, $n:expr) => (ioc!($crate::sys::ioctl::VOID, $g, $n, 0))
50+
}
51+
52+
/// Generate an ioctl request code for a command that passes an integer
53+
///
54+
/// This is equivalent to the `_IOWINT()` macro exposed by the C ioctl API.
55+
///
56+
/// You should only use this macro directly if the `ioctl` you're working
57+
/// with is "bad" and you cannot use `ioctl_write_int!()` directly.
58+
#[macro_export]
59+
macro_rules! request_code_write_int {
60+
($g:expr, $n:expr) => (ioc!($crate::sys::ioctl::VOID, $g, $n, ::std::mem::size_of::<$crate::libc::c_int>()))
61+
}
62+
63+
/// Generate an ioctl request code for a command that reads.
64+
///
65+
/// This is equivalent to the `_IOR()` macro exposed by the C ioctl API.
66+
///
67+
/// You should only use this macro directly if the `ioctl` you're working
68+
/// with is "bad" and you cannot use `ioctl_read!()` directly.
69+
///
70+
/// The read/write direction is relative to userland, so this
71+
/// command would be userland is reading and the kernel is
72+
/// writing.
73+
#[macro_export]
74+
macro_rules! request_code_read {
75+
($g:expr, $n:expr, $len:expr) => (ioc!($crate::sys::ioctl::OUT, $g, $n, $len))
76+
}
77+
78+
/// Generate an ioctl request code for a command that writes.
79+
///
80+
/// This is equivalent to the `_IOW()` 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_write!()` directly.
84+
///
85+
/// The read/write direction is relative to userland, so this
86+
/// command would be userland is writing and the kernel is
87+
/// reading.
88+
#[macro_export]
89+
macro_rules! request_code_write {
90+
($g:expr, $n:expr, $len:expr) => (ioc!($crate::sys::ioctl::IN, $g, $n, $len))
91+
}
92+
93+
/// Generate an ioctl request code for a command that reads and writes.
94+
///
95+
/// This is equivalent to the `_IOWR()` macro exposed by the C ioctl API.
96+
///
97+
/// You should only use this macro directly if the `ioctl` you're working
98+
/// with is "bad" and you cannot use `ioctl_readwrite!()` directly.
99+
#[macro_export]
100+
macro_rules! request_code_readwrite {
101+
($g:expr, $n:expr, $len:expr) => (ioc!($crate::sys::ioctl::INOUT, $g, $n, $len))
102+
}

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

Lines changed: 48 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ pub type ioctl_num_type = ::libc::c_int;
55
#[cfg(not(any(target_os = "android", target_env = "musl")))]
66
#[doc(hidden)]
77
pub type ioctl_num_type = ::libc::c_ulong;
8+
/// The datatype used for the 3rd argument
9+
#[doc(hidden)]
10+
pub type ioctl_param_type = ::libc::c_ulong;
811

912
#[doc(hidden)]
1013
pub const NRBITS: ioctl_num_type = 8;
@@ -25,17 +28,6 @@ mod consts {
2528
pub const DIRBITS: u8 = 3;
2629
}
2730

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-
3931
// "Generic" ioctl protocol
4032
#[cfg(any(target_arch = "x86",
4133
target_arch = "arm",
@@ -86,30 +78,63 @@ macro_rules! ioc {
8678
(($sz as $crate::sys::ioctl::ioctl_num_type & $crate::sys::ioctl::SIZEMASK) << $crate::sys::ioctl::SIZESHIFT))
8779
}
8880

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

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

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

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

0 commit comments

Comments
 (0)