1 //! Provide helpers for making ioctl system calls.
2 //!
3 //! This library is pretty low-level and messy. `ioctl` is not fun.
4 //!
5 //! What is an `ioctl`?
6 //! ===================
7 //!
8 //! The `ioctl` syscall is the grab-bag syscall on POSIX systems. Don't want to add a new
9 //! syscall? Make it an `ioctl`! `ioctl` refers to both the syscall, and the commands that can be
10 //! sent with it. `ioctl` stands for "IO control", and the commands are always sent to a file
11 //! descriptor.
12 //!
13 //! It is common to see `ioctl`s used for the following purposes:
14 //!
15 //!   * Provide read/write access to out-of-band data related to a device such as configuration
16 //!     (for instance, setting serial port options)
17 //!   * Provide a mechanism for performing full-duplex data transfers (for instance, xfer on SPI
18 //!     devices).
19 //!   * Provide access to control functions on a device (for example, on Linux you can send
20 //!     commands like pause, resume, and eject to the CDROM device.
21 //!   * Do whatever else the device driver creator thought made most sense.
22 //!
23 //! `ioctl`s are synchronous system calls and are similar to read and write calls in that regard.
24 //! They operate on file descriptors and have an identifier that specifies what the ioctl is.
25 //! Additionally they may read or write data and therefore need to pass along a data pointer.
26 //! Besides the semantics of the ioctls being confusing, the generation of this identifer can also
27 //! be difficult.
28 //!
29 //! Historically `ioctl` numbers were arbitrary hard-coded values. In Linux (before 2.6) and some
30 //! unices this has changed to a more-ordered system where the ioctl numbers are partitioned into
31 //! subcomponents (For linux this is documented in
32 //! [`Documentation/ioctl/ioctl-number.rst`](https://elixir.bootlin.com/linux/latest/source/Documentation/userspace-api/ioctl/ioctl-number.rst)):
33 //!
34 //!   * Number: The actual ioctl ID
35 //!   * Type: A grouping of ioctls for a common purpose or driver
36 //!   * Size: The size in bytes of the data that will be transferred
37 //!   * Direction: Whether there is any data and if it's read, write, or both
38 //!
39 //! Newer drivers should not generate complete integer identifiers for their `ioctl`s instead
40 //! preferring to use the 4 components above to generate the final ioctl identifier. Because of
41 //! how old `ioctl`s are, however, there are many hard-coded `ioctl` identifiers. These are
42 //! commonly referred to as "bad" in `ioctl` documentation.
43 //!
44 //! Defining `ioctl`s
45 //! =================
46 //!
47 //! This library provides several `ioctl_*!` macros for binding `ioctl`s. These generate public
48 //! unsafe functions that can then be used for calling the ioctl. This macro has a few different
49 //! ways it can be used depending on the specific ioctl you're working with.
50 //!
51 //! A simple `ioctl` is `SPI_IOC_RD_MODE`. This ioctl works with the SPI interface on Linux. This
52 //! specific `ioctl` reads the mode of the SPI device as a `u8`. It's declared in
53 //! `/include/uapi/linux/spi/spidev.h` as `_IOR(SPI_IOC_MAGIC, 1, __u8)`. Since it uses the `_IOR`
54 //! macro, we know it's a `read` ioctl and can use the `ioctl_read!` macro as follows:
55 //!
56 //! ```
57 //! # #[macro_use] extern crate nix;
58 //! const SPI_IOC_MAGIC: u8 = b'k'; // Defined in linux/spi/spidev.h
59 //! const SPI_IOC_TYPE_MODE: u8 = 1;
60 //! ioctl_read!(spi_read_mode, SPI_IOC_MAGIC, SPI_IOC_TYPE_MODE, u8);
61 //! # fn main() {}
62 //! ```
63 //!
64 //! This generates the function:
65 //!
66 //! ```
67 //! # #[macro_use] extern crate nix;
68 //! # use std::mem;
69 //! # use nix::{libc, Result};
70 //! # use nix::errno::Errno;
71 //! # use nix::libc::c_int as c_int;
72 //! # const SPI_IOC_MAGIC: u8 = b'k'; // Defined in linux/spi/spidev.h
73 //! # const SPI_IOC_TYPE_MODE: u8 = 1;
74 //! pub unsafe fn spi_read_mode(fd: c_int, data: *mut u8) -> Result<c_int> {
75 //!     let res = libc::ioctl(fd, request_code_read!(SPI_IOC_MAGIC, SPI_IOC_TYPE_MODE, mem::size_of::<u8>()), data);
76 //!     Errno::result(res)
77 //! }
78 //! # fn main() {}
79 //! ```
80 //!
81 //! The return value for the wrapper functions generated by the `ioctl_*!` macros are `nix::Error`s.
82 //! These are generated by assuming the return value of the ioctl is `-1` on error and everything
83 //! else is a valid return value. If this is not the case, `Result::map` can be used to map some
84 //! of the range of "good" values (-Inf..-2, 0..Inf) into a smaller range in a helper function.
85 //!
86 //! Writing `ioctl`s generally use pointers as their data source and these should use the
87 //! `ioctl_write_ptr!`. But in some cases an `int` is passed directly. For these `ioctl`s use the
88 //! `ioctl_write_int!` macro. This variant does not take a type as the last argument:
89 //!
90 //! ```
91 //! # #[macro_use] extern crate nix;
92 //! const HCI_IOC_MAGIC: u8 = b'k';
93 //! const HCI_IOC_HCIDEVUP: u8 = 1;
94 //! ioctl_write_int!(hci_dev_up, HCI_IOC_MAGIC, HCI_IOC_HCIDEVUP);
95 //! # fn main() {}
96 //! ```
97 //!
98 //! Some `ioctl`s don't transfer any data, and those should use `ioctl_none!`. This macro
99 //! doesn't take a type and so it is declared similar to the `write_int` variant shown above.
100 //!
101 //! The mode for a given `ioctl` should be clear from the documentation if it has good
102 //! documentation. Otherwise it will be clear based on the macro used to generate the `ioctl`
103 //! number where `_IO`, `_IOR`, `_IOW`, and `_IOWR` map to "none", "read", "write_*", and "readwrite"
104 //! respectively. To determine the specific `write_` variant to use you'll need to find
105 //! what the argument type is supposed to be. If it's an `int`, then `write_int` should be used,
106 //! otherwise it should be a pointer and `write_ptr` should be used. On Linux the
107 //! [`ioctl_list` man page](http://man7.org/linux/man-pages/man2/ioctl_list.2.html) describes a
108 //! large number of `ioctl`s and describes their argument data type.
109 //!
110 //! Using "bad" `ioctl`s
111 //! --------------------
112 //!
113 //! As mentioned earlier, there are many old `ioctl`s that do not use the newer method of
114 //! generating `ioctl` numbers and instead use hardcoded values. These can be used with the
115 //! `ioctl_*_bad!` macros. This naming comes from the Linux kernel which refers to these
116 //! `ioctl`s as "bad". These are a different variant as they bypass calling the macro that generates
117 //! the ioctl number and instead use the defined value directly.
118 //!
119 //! For example the `TCGETS` `ioctl` reads a `termios` data structure for a given file descriptor.
120 //! It's defined as `0x5401` in `ioctls.h` on Linux and can be implemented as:
121 //!
122 //! ```
123 //! # #[macro_use] extern crate nix;
124 //! # #[cfg(any(target_os = "android", target_os = "linux"))]
125 //! # use nix::libc::TCGETS as TCGETS;
126 //! # #[cfg(any(target_os = "android", target_os = "linux"))]
127 //! # use nix::libc::termios as termios;
128 //! # #[cfg(any(target_os = "android", target_os = "linux"))]
129 //! ioctl_read_bad!(tcgets, TCGETS, termios);
130 //! # fn main() {}
131 //! ```
132 //!
133 //! The generated function has the same form as that generated by `ioctl_read!`:
134 //!
135 //! ```text
136 //! pub unsafe fn tcgets(fd: c_int, data: *mut termios) -> Result<c_int>;
137 //! ```
138 //!
139 //! Working with Arrays
140 //! -------------------
141 //!
142 //! Some `ioctl`s work with entire arrays of elements. These are supported by the `ioctl_*_buf`
143 //! family of macros: `ioctl_read_buf`, `ioctl_write_buf`, and `ioctl_readwrite_buf`. Note that
144 //! there are no "bad" versions for working with buffers. The generated functions include a `len`
145 //! argument to specify the number of elements (where the type of each element is specified in the
146 //! macro).
147 //!
148 //! Again looking to the SPI `ioctl`s on Linux for an example, there is a `SPI_IOC_MESSAGE` `ioctl`
149 //! that queues up multiple SPI messages by writing an entire array of `spi_ioc_transfer` structs.
150 //! `linux/spi/spidev.h` defines a macro to calculate the `ioctl` number like:
151 //!
152 //! ```C
153 //! #define SPI_IOC_MAGIC 'k'
154 //! #define SPI_MSGSIZE(N) ...
155 //! #define SPI_IOC_MESSAGE(N) _IOW(SPI_IOC_MAGIC, 0, char[SPI_MSGSIZE(N)])
156 //! ```
157 //!
158 //! The `SPI_MSGSIZE(N)` calculation is already handled by the `ioctl_*!` macros, so all that's
159 //! needed to define this `ioctl` is:
160 //!
161 //! ```
162 //! # #[macro_use] extern crate nix;
163 //! const SPI_IOC_MAGIC: u8 = b'k'; // Defined in linux/spi/spidev.h
164 //! const SPI_IOC_TYPE_MESSAGE: u8 = 0;
165 //! # pub struct spi_ioc_transfer(u64);
166 //! ioctl_write_buf!(spi_transfer, SPI_IOC_MAGIC, SPI_IOC_TYPE_MESSAGE, spi_ioc_transfer);
167 //! # fn main() {}
168 //! ```
169 //!
170 //! This generates a function like:
171 //!
172 //! ```
173 //! # #[macro_use] extern crate nix;
174 //! # use std::mem;
175 //! # use nix::{libc, Result};
176 //! # use nix::errno::Errno;
177 //! # use nix::libc::c_int as c_int;
178 //! # const SPI_IOC_MAGIC: u8 = b'k';
179 //! # const SPI_IOC_TYPE_MESSAGE: u8 = 0;
180 //! # pub struct spi_ioc_transfer(u64);
181 //! pub unsafe fn spi_message(fd: c_int, data: &mut [spi_ioc_transfer]) -> Result<c_int> {
182 //!     let res = libc::ioctl(fd,
183 //!                           request_code_write!(SPI_IOC_MAGIC, SPI_IOC_TYPE_MESSAGE, data.len() * mem::size_of::<spi_ioc_transfer>()),
184 //!                           data);
185 //!     Errno::result(res)
186 //! }
187 //! # fn main() {}
188 //! ```
189 //!
190 //! Finding `ioctl` Documentation
191 //! -----------------------------
192 //!
193 //! For Linux, look at your system's headers. For example, `/usr/include/linux/input.h` has a lot
194 //! of lines defining macros which use `_IO`, `_IOR`, `_IOW`, `_IOC`, and `_IOWR`. Some `ioctl`s are
195 //! documented directly in the headers defining their constants, but others have more extensive
196 //! documentation in man pages (like termios' `ioctl`s which are in `tty_ioctl(4)`).
197 //!
198 //! Documenting the Generated Functions
199 //! ===================================
200 //!
201 //! In many cases, users will wish for the functions generated by the `ioctl`
202 //! macro to be public and documented. For this reason, the generated functions
203 //! are public by default. If you wish to hide the ioctl, you will need to put
204 //! them in a private module.
205 //!
206 //! For documentation, it is possible to use doc comments inside the `ioctl_*!` macros. Here is an
207 //! example :
208 //!
209 //! ```
210 //! # #[macro_use] extern crate nix;
211 //! # use nix::libc::c_int;
212 //! ioctl_read! {
213 //!     /// Make the given terminal the controlling terminal of the calling process. The calling
214 //!     /// process must be a session leader and not have a controlling terminal already. If the
215 //!     /// terminal is already the controlling terminal of a different session group then the
216 //!     /// ioctl will fail with **EPERM**, unless the caller is root (more precisely: has the
217 //!     /// **CAP_SYS_ADMIN** capability) and arg equals 1, in which case the terminal is stolen
218 //!     /// and all processes that had it as controlling terminal lose it.
219 //!     tiocsctty, b't', 19, c_int
220 //! }
221 //!
222 //! # fn main() {}
223 //! ```
224 use cfg_if::cfg_if;
225 
226 #[cfg(any(target_os = "android", target_os = "linux", target_os = "redox"))]
227 #[macro_use]
228 mod linux;
229 
230 #[cfg(any(target_os = "android", target_os = "linux", target_os = "redox"))]
231 pub use self::linux::*;
232 
233 #[cfg(any(target_os = "dragonfly",
234           target_os = "freebsd",
235           target_os = "ios",
236           target_os = "macos",
237           target_os = "netbsd",
238           target_os = "openbsd"))]
239 #[macro_use]
240 mod bsd;
241 
242 #[cfg(any(target_os = "dragonfly",
243           target_os = "freebsd",
244           target_os = "ios",
245           target_os = "macos",
246           target_os = "netbsd",
247           target_os = "openbsd"))]
248 pub use self::bsd::*;
249 
250 /// Convert raw ioctl return value to a Nix result
251 #[macro_export]
252 #[doc(hidden)]
253 macro_rules! convert_ioctl_res {
254     ($w:expr) => (
255         {
256             $crate::errno::Errno::result($w)
257         }
258     );
259 }
260 
261 /// Generates a wrapper function for an ioctl that passes no data to the kernel.
262 ///
263 /// The arguments to this macro are:
264 ///
265 /// * The function name
266 /// * The ioctl identifier
267 /// * The ioctl sequence number
268 ///
269 /// The generated function has the following signature:
270 ///
271 /// ```rust,ignore
272 /// pub unsafe fn FUNCTION_NAME(fd: libc::c_int) -> Result<libc::c_int>
273 /// ```
274 ///
275 /// For a more in-depth explanation of ioctls, see [`::sys::ioctl`](sys/ioctl/index.html).
276 ///
277 /// # Example
278 ///
279 /// The `videodev2` driver on Linux defines the `log_status` `ioctl` as:
280 ///
281 /// ```C
282 /// #define VIDIOC_LOG_STATUS         _IO('V', 70)
283 /// ```
284 ///
285 /// This can be implemented in Rust like:
286 ///
287 /// ```no_run
288 /// # #[macro_use] extern crate nix;
289 /// ioctl_none!(log_status, b'V', 70);
290 /// fn main() {}
291 /// ```
292 #[macro_export(local_inner_macros)]
293 macro_rules! ioctl_none {
294     ($(#[$attr:meta])* $name:ident, $ioty:expr, $nr:expr) => (
295         $(#[$attr])*
296         pub unsafe fn $name(fd: $crate::libc::c_int)
297                             -> $crate::Result<$crate::libc::c_int> {
298             convert_ioctl_res!($crate::libc::ioctl(fd, request_code_none!($ioty, $nr) as $crate::sys::ioctl::ioctl_num_type))
299         }
300     )
301 }
302 
303 /// Generates a wrapper function for a "bad" ioctl that passes no data to the kernel.
304 ///
305 /// The arguments to this macro are:
306 ///
307 /// * The function name
308 /// * The ioctl request code
309 ///
310 /// The generated function has the following signature:
311 ///
312 /// ```rust,ignore
313 /// pub unsafe fn FUNCTION_NAME(fd: libc::c_int) -> Result<libc::c_int>
314 /// ```
315 ///
316 /// For a more in-depth explanation of ioctls, see [`::sys::ioctl`](sys/ioctl/index.html).
317 ///
318 /// # Example
319 ///
320 /// ```no_run
321 /// # #[macro_use] extern crate nix;
322 /// # use libc::TIOCNXCL;
323 /// # use std::fs::File;
324 /// # use std::os::unix::io::AsRawFd;
325 /// ioctl_none_bad!(tiocnxcl, TIOCNXCL);
326 /// fn main() {
327 ///     let file = File::open("/dev/ttyUSB0").unwrap();
328 ///     unsafe { tiocnxcl(file.as_raw_fd()) }.unwrap();
329 /// }
330 /// ```
331 // TODO: add an example using request_code_*!()
332 #[macro_export(local_inner_macros)]
333 macro_rules! ioctl_none_bad {
334     ($(#[$attr:meta])* $name:ident, $nr:expr) => (
335         $(#[$attr])*
336         pub unsafe fn $name(fd: $crate::libc::c_int)
337                             -> $crate::Result<$crate::libc::c_int> {
338             convert_ioctl_res!($crate::libc::ioctl(fd, $nr as $crate::sys::ioctl::ioctl_num_type))
339         }
340     )
341 }
342 
343 /// Generates a wrapper function for an ioctl that reads data from the kernel.
344 ///
345 /// The arguments to this macro are:
346 ///
347 /// * The function name
348 /// * The ioctl identifier
349 /// * The ioctl sequence number
350 /// * The data type passed by this ioctl
351 ///
352 /// The generated function has the following signature:
353 ///
354 /// ```rust,ignore
355 /// pub unsafe fn FUNCTION_NAME(fd: libc::c_int, data: *mut DATA_TYPE) -> Result<libc::c_int>
356 /// ```
357 ///
358 /// For a more in-depth explanation of ioctls, see [`::sys::ioctl`](sys/ioctl/index.html).
359 ///
360 /// # Example
361 ///
362 /// ```
363 /// # #[macro_use] extern crate nix;
364 /// const SPI_IOC_MAGIC: u8 = b'k'; // Defined in linux/spi/spidev.h
365 /// const SPI_IOC_TYPE_MODE: u8 = 1;
366 /// ioctl_read!(spi_read_mode, SPI_IOC_MAGIC, SPI_IOC_TYPE_MODE, u8);
367 /// # fn main() {}
368 /// ```
369 #[macro_export(local_inner_macros)]
370 macro_rules! ioctl_read {
371     ($(#[$attr:meta])* $name:ident, $ioty:expr, $nr:expr, $ty:ty) => (
372         $(#[$attr])*
373         pub unsafe fn $name(fd: $crate::libc::c_int,
374                             data: *mut $ty)
375                             -> $crate::Result<$crate::libc::c_int> {
376             convert_ioctl_res!($crate::libc::ioctl(fd, request_code_read!($ioty, $nr, ::std::mem::size_of::<$ty>()) as $crate::sys::ioctl::ioctl_num_type, data))
377         }
378     )
379 }
380 
381 /// Generates a wrapper function for a "bad" ioctl that reads data from the kernel.
382 ///
383 /// The arguments to this macro are:
384 ///
385 /// * The function name
386 /// * The ioctl request code
387 /// * The data type passed by this ioctl
388 ///
389 /// The generated function has the following signature:
390 ///
391 /// ```rust,ignore
392 /// pub unsafe fn FUNCTION_NAME(fd: libc::c_int, data: *mut DATA_TYPE) -> Result<libc::c_int>
393 /// ```
394 ///
395 /// For a more in-depth explanation of ioctls, see [`::sys::ioctl`](sys/ioctl/index.html).
396 ///
397 /// # Example
398 ///
399 /// ```
400 /// # #[macro_use] extern crate nix;
401 /// # #[cfg(any(target_os = "android", target_os = "linux"))]
402 /// ioctl_read_bad!(tcgets, libc::TCGETS, libc::termios);
403 /// # fn main() {}
404 /// ```
405 #[macro_export(local_inner_macros)]
406 macro_rules! ioctl_read_bad {
407     ($(#[$attr:meta])* $name:ident, $nr:expr, $ty:ty) => (
408         $(#[$attr])*
409         pub unsafe fn $name(fd: $crate::libc::c_int,
410                             data: *mut $ty)
411                             -> $crate::Result<$crate::libc::c_int> {
412             convert_ioctl_res!($crate::libc::ioctl(fd, $nr as $crate::sys::ioctl::ioctl_num_type, data))
413         }
414     )
415 }
416 
417 /// Generates a wrapper function for an ioctl that writes data through a pointer to the kernel.
418 ///
419 /// The arguments to this macro are:
420 ///
421 /// * The function name
422 /// * The ioctl identifier
423 /// * The ioctl sequence number
424 /// * The data type passed by this ioctl
425 ///
426 /// The generated function has the following signature:
427 ///
428 /// ```rust,ignore
429 /// pub unsafe fn FUNCTION_NAME(fd: libc::c_int, data: *const DATA_TYPE) -> Result<libc::c_int>
430 /// ```
431 ///
432 /// For a more in-depth explanation of ioctls, see [`::sys::ioctl`](sys/ioctl/index.html).
433 ///
434 /// # Example
435 ///
436 /// ```
437 /// # #[macro_use] extern crate nix;
438 /// # pub struct v4l2_audio {}
439 /// ioctl_write_ptr!(s_audio, b'V', 34, v4l2_audio);
440 /// # fn main() {}
441 /// ```
442 #[macro_export(local_inner_macros)]
443 macro_rules! ioctl_write_ptr {
444     ($(#[$attr:meta])* $name:ident, $ioty:expr, $nr:expr, $ty:ty) => (
445         $(#[$attr])*
446         pub unsafe fn $name(fd: $crate::libc::c_int,
447                             data: *const $ty)
448                             -> $crate::Result<$crate::libc::c_int> {
449             convert_ioctl_res!($crate::libc::ioctl(fd, request_code_write!($ioty, $nr, ::std::mem::size_of::<$ty>()) as $crate::sys::ioctl::ioctl_num_type, data))
450         }
451     )
452 }
453 
454 /// Generates a wrapper function for a "bad" ioctl that writes data through a pointer to the kernel.
455 ///
456 /// The arguments to this macro are:
457 ///
458 /// * The function name
459 /// * The ioctl request code
460 /// * The data type passed by this ioctl
461 ///
462 /// The generated function has the following signature:
463 ///
464 /// ```rust,ignore
465 /// pub unsafe fn FUNCTION_NAME(fd: libc::c_int, data: *const DATA_TYPE) -> Result<libc::c_int>
466 /// ```
467 ///
468 /// For a more in-depth explanation of ioctls, see [`::sys::ioctl`](sys/ioctl/index.html).
469 ///
470 /// # Example
471 ///
472 /// ```
473 /// # #[macro_use] extern crate nix;
474 /// # #[cfg(any(target_os = "android", target_os = "linux"))]
475 /// ioctl_write_ptr_bad!(tcsets, libc::TCSETS, libc::termios);
476 /// # fn main() {}
477 /// ```
478 #[macro_export(local_inner_macros)]
479 macro_rules! ioctl_write_ptr_bad {
480     ($(#[$attr:meta])* $name:ident, $nr:expr, $ty:ty) => (
481         $(#[$attr])*
482         pub unsafe fn $name(fd: $crate::libc::c_int,
483                             data: *const $ty)
484                             -> $crate::Result<$crate::libc::c_int> {
485             convert_ioctl_res!($crate::libc::ioctl(fd, $nr as $crate::sys::ioctl::ioctl_num_type, data))
486         }
487     )
488 }
489 
490 cfg_if!{
491     if #[cfg(any(target_os = "dragonfly", target_os = "freebsd"))] {
492         /// Generates a wrapper function for a ioctl that writes an integer to the kernel.
493         ///
494         /// The arguments to this macro are:
495         ///
496         /// * The function name
497         /// * The ioctl identifier
498         /// * The ioctl sequence number
499         ///
500         /// The generated function has the following signature:
501         ///
502         /// ```rust,ignore
503         /// pub unsafe fn FUNCTION_NAME(fd: libc::c_int, data: nix::sys::ioctl::ioctl_param_type) -> Result<libc::c_int>
504         /// ```
505         ///
506         /// `nix::sys::ioctl::ioctl_param_type` depends on the OS:
507         /// *   BSD - `libc::c_int`
508         /// *   Linux - `libc::c_ulong`
509         ///
510         /// For a more in-depth explanation of ioctls, see [`::sys::ioctl`](sys/ioctl/index.html).
511         ///
512         /// # Example
513         ///
514         /// ```
515         /// # #[macro_use] extern crate nix;
516         /// ioctl_write_int!(vt_activate, b'v', 4);
517         /// # fn main() {}
518         /// ```
519         #[macro_export(local_inner_macros)]
520         macro_rules! ioctl_write_int {
521             ($(#[$attr:meta])* $name:ident, $ioty:expr, $nr:expr) => (
522                 $(#[$attr])*
523                 pub unsafe fn $name(fd: $crate::libc::c_int,
524                                     data: $crate::sys::ioctl::ioctl_param_type)
525                                     -> $crate::Result<$crate::libc::c_int> {
526                     convert_ioctl_res!($crate::libc::ioctl(fd, request_code_write_int!($ioty, $nr) as $crate::sys::ioctl::ioctl_num_type, data))
527                 }
528             )
529         }
530     } else {
531         /// Generates a wrapper function for a ioctl that writes an integer to the kernel.
532         ///
533         /// The arguments to this macro are:
534         ///
535         /// * The function name
536         /// * The ioctl identifier
537         /// * The ioctl sequence number
538         ///
539         /// The generated function has the following signature:
540         ///
541         /// ```rust,ignore
542         /// pub unsafe fn FUNCTION_NAME(fd: libc::c_int, data: nix::sys::ioctl::ioctl_param_type) -> Result<libc::c_int>
543         /// ```
544         ///
545         /// `nix::sys::ioctl::ioctl_param_type` depends on the OS:
546         /// *   BSD - `libc::c_int`
547         /// *   Linux - `libc::c_ulong`
548         ///
549         /// For a more in-depth explanation of ioctls, see [`::sys::ioctl`](sys/ioctl/index.html).
550         ///
551         /// # Example
552         ///
553         /// ```
554         /// # #[macro_use] extern crate nix;
555         /// const HCI_IOC_MAGIC: u8 = b'k';
556         /// const HCI_IOC_HCIDEVUP: u8 = 1;
557         /// ioctl_write_int!(hci_dev_up, HCI_IOC_MAGIC, HCI_IOC_HCIDEVUP);
558         /// # fn main() {}
559         /// ```
560         #[macro_export(local_inner_macros)]
561         macro_rules! ioctl_write_int {
562             ($(#[$attr:meta])* $name:ident, $ioty:expr, $nr:expr) => (
563                 $(#[$attr])*
564                 pub unsafe fn $name(fd: $crate::libc::c_int,
565                                     data: $crate::sys::ioctl::ioctl_param_type)
566                                     -> $crate::Result<$crate::libc::c_int> {
567                     convert_ioctl_res!($crate::libc::ioctl(fd, request_code_write!($ioty, $nr, ::std::mem::size_of::<$crate::libc::c_int>()) as $crate::sys::ioctl::ioctl_num_type, data))
568                 }
569             )
570         }
571     }
572 }
573 
574 /// Generates a wrapper function for a "bad" ioctl that writes an integer to the kernel.
575 ///
576 /// The arguments to this macro are:
577 ///
578 /// * The function name
579 /// * The ioctl request code
580 ///
581 /// The generated function has the following signature:
582 ///
583 /// ```rust,ignore
584 /// pub unsafe fn FUNCTION_NAME(fd: libc::c_int, data: libc::c_int) -> Result<libc::c_int>
585 /// ```
586 ///
587 /// For a more in-depth explanation of ioctls, see [`::sys::ioctl`](sys/ioctl/index.html).
588 ///
589 /// # Examples
590 ///
591 /// ```
592 /// # #[macro_use] extern crate nix;
593 /// # #[cfg(any(target_os = "android", target_os = "linux"))]
594 /// ioctl_write_int_bad!(tcsbrk, libc::TCSBRK);
595 /// # fn main() {}
596 /// ```
597 ///
598 /// ```rust
599 /// # #[macro_use] extern crate nix;
600 /// const KVMIO: u8 = 0xAE;
601 /// ioctl_write_int_bad!(kvm_create_vm, request_code_none!(KVMIO, 0x03));
602 /// # fn main() {}
603 /// ```
604 #[macro_export(local_inner_macros)]
605 macro_rules! ioctl_write_int_bad {
606     ($(#[$attr:meta])* $name:ident, $nr:expr) => (
607         $(#[$attr])*
608         pub unsafe fn $name(fd: $crate::libc::c_int,
609                             data: $crate::libc::c_int)
610                             -> $crate::Result<$crate::libc::c_int> {
611             convert_ioctl_res!($crate::libc::ioctl(fd, $nr as $crate::sys::ioctl::ioctl_num_type, data))
612         }
613     )
614 }
615 
616 /// Generates a wrapper function for an ioctl that reads and writes data to the kernel.
617 ///
618 /// The arguments to this macro are:
619 ///
620 /// * The function name
621 /// * The ioctl identifier
622 /// * The ioctl sequence number
623 /// * The data type passed by this ioctl
624 ///
625 /// The generated function has the following signature:
626 ///
627 /// ```rust,ignore
628 /// pub unsafe fn FUNCTION_NAME(fd: libc::c_int, data: *mut DATA_TYPE) -> Result<libc::c_int>
629 /// ```
630 ///
631 /// For a more in-depth explanation of ioctls, see [`::sys::ioctl`](sys/ioctl/index.html).
632 ///
633 /// # Example
634 ///
635 /// ```
636 /// # #[macro_use] extern crate nix;
637 /// # pub struct v4l2_audio {}
638 /// ioctl_readwrite!(enum_audio, b'V', 65, v4l2_audio);
639 /// # fn main() {}
640 /// ```
641 #[macro_export(local_inner_macros)]
642 macro_rules! ioctl_readwrite {
643     ($(#[$attr:meta])* $name:ident, $ioty:expr, $nr:expr, $ty:ty) => (
644         $(#[$attr])*
645         pub unsafe fn $name(fd: $crate::libc::c_int,
646                             data: *mut $ty)
647                             -> $crate::Result<$crate::libc::c_int> {
648             convert_ioctl_res!($crate::libc::ioctl(fd, request_code_readwrite!($ioty, $nr, ::std::mem::size_of::<$ty>()) as $crate::sys::ioctl::ioctl_num_type, data))
649         }
650     )
651 }
652 
653 /// Generates a wrapper function for a "bad" ioctl that reads and writes data to the kernel.
654 ///
655 /// The arguments to this macro are:
656 ///
657 /// * The function name
658 /// * The ioctl request code
659 /// * The data type passed by this ioctl
660 ///
661 /// The generated function has the following signature:
662 ///
663 /// ```rust,ignore
664 /// pub unsafe fn FUNCTION_NAME(fd: libc::c_int, data: *mut DATA_TYPE) -> Result<libc::c_int>
665 /// ```
666 ///
667 /// For a more in-depth explanation of ioctls, see [`::sys::ioctl`](sys/ioctl/index.html).
668 // TODO: Find an example for ioctl_readwrite_bad
669 #[macro_export(local_inner_macros)]
670 macro_rules! ioctl_readwrite_bad {
671     ($(#[$attr:meta])* $name:ident, $nr:expr, $ty:ty) => (
672         $(#[$attr])*
673         pub unsafe fn $name(fd: $crate::libc::c_int,
674                             data: *mut $ty)
675                             -> $crate::Result<$crate::libc::c_int> {
676             convert_ioctl_res!($crate::libc::ioctl(fd, $nr as $crate::sys::ioctl::ioctl_num_type, data))
677         }
678     )
679 }
680 
681 /// Generates a wrapper function for an ioctl that reads an array of elements from the kernel.
682 ///
683 /// The arguments to this macro are:
684 ///
685 /// * The function name
686 /// * The ioctl identifier
687 /// * The ioctl sequence number
688 /// * The data type passed by this ioctl
689 ///
690 /// The generated function has the following signature:
691 ///
692 /// ```rust,ignore
693 /// pub unsafe fn FUNCTION_NAME(fd: libc::c_int, data: &mut [DATA_TYPE]) -> Result<libc::c_int>
694 /// ```
695 ///
696 /// For a more in-depth explanation of ioctls, see [`::sys::ioctl`](sys/ioctl/index.html).
697 // TODO: Find an example for ioctl_read_buf
698 #[macro_export(local_inner_macros)]
699 macro_rules! ioctl_read_buf {
700     ($(#[$attr:meta])* $name:ident, $ioty:expr, $nr:expr, $ty:ty) => (
701         $(#[$attr])*
702         pub unsafe fn $name(fd: $crate::libc::c_int,
703                             data: &mut [$ty])
704                             -> $crate::Result<$crate::libc::c_int> {
705             convert_ioctl_res!($crate::libc::ioctl(fd, request_code_read!($ioty, $nr, data.len() * ::std::mem::size_of::<$ty>()) as $crate::sys::ioctl::ioctl_num_type, data))
706         }
707     )
708 }
709 
710 /// Generates a wrapper function for an ioctl that writes an array of elements to the kernel.
711 ///
712 /// The arguments to this macro are:
713 ///
714 /// * The function name
715 /// * The ioctl identifier
716 /// * The ioctl sequence number
717 /// * The data type passed by this ioctl
718 ///
719 /// The generated function has the following signature:
720 ///
721 /// ```rust,ignore
722 /// pub unsafe fn FUNCTION_NAME(fd: libc::c_int, data: &[DATA_TYPE]) -> Result<libc::c_int>
723 /// ```
724 ///
725 /// For a more in-depth explanation of ioctls, see [`::sys::ioctl`](sys/ioctl/index.html).
726 ///
727 /// # Examples
728 ///
729 /// ```
730 /// # #[macro_use] extern crate nix;
731 /// const SPI_IOC_MAGIC: u8 = b'k'; // Defined in linux/spi/spidev.h
732 /// const SPI_IOC_TYPE_MESSAGE: u8 = 0;
733 /// # pub struct spi_ioc_transfer(u64);
734 /// ioctl_write_buf!(spi_transfer, SPI_IOC_MAGIC, SPI_IOC_TYPE_MESSAGE, spi_ioc_transfer);
735 /// # fn main() {}
736 /// ```
737 #[macro_export(local_inner_macros)]
738 macro_rules! ioctl_write_buf {
739     ($(#[$attr:meta])* $name:ident, $ioty:expr, $nr:expr, $ty:ty) => (
740         $(#[$attr])*
741         pub unsafe fn $name(fd: $crate::libc::c_int,
742                             data: &[$ty])
743                             -> $crate::Result<$crate::libc::c_int> {
744             convert_ioctl_res!($crate::libc::ioctl(fd, request_code_write!($ioty, $nr, data.len() * ::std::mem::size_of::<$ty>()) as $crate::sys::ioctl::ioctl_num_type, data))
745         }
746     )
747 }
748 
749 /// Generates a wrapper function for an ioctl that reads and writes an array of elements to the kernel.
750 ///
751 /// The arguments to this macro are:
752 ///
753 /// * The function name
754 /// * The ioctl identifier
755 /// * The ioctl sequence number
756 /// * The data type passed by this ioctl
757 ///
758 /// The generated function has the following signature:
759 ///
760 /// ```rust,ignore
761 /// pub unsafe fn FUNCTION_NAME(fd: libc::c_int, data: &mut [DATA_TYPE]) -> Result<libc::c_int>
762 /// ```
763 ///
764 /// For a more in-depth explanation of ioctls, see [`::sys::ioctl`](sys/ioctl/index.html).
765 // TODO: Find an example for readwrite_buf
766 #[macro_export(local_inner_macros)]
767 macro_rules! ioctl_readwrite_buf {
768     ($(#[$attr:meta])* $name:ident, $ioty:expr, $nr:expr, $ty:ty) => (
769         $(#[$attr])*
770         pub unsafe fn $name(fd: $crate::libc::c_int,
771                             data: &mut [$ty])
772                             -> $crate::Result<$crate::libc::c_int> {
773             convert_ioctl_res!($crate::libc::ioctl(fd, request_code_readwrite!($ioty, $nr, data.len() * ::std::mem::size_of::<$ty>()) as $crate::sys::ioctl::ioctl_num_type, data))
774         }
775     )
776 }
777