• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 crate::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(local_inner_macros)]
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(local_inner_macros)]
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(local_inner_macros)]
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(local_inner_macros)]
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(local_inner_macros)]
100 macro_rules! request_code_readwrite {
101     ($g:expr, $n:expr, $len:expr) => (ioc!($crate::sys::ioctl::INOUT, $g, $n, $len))
102 }
103