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