]>
git.proxmox.com Git - rustc.git/blob - vendor/rustix/src/ioctl/mod.rs
1 //! Unsafe `ioctl` API.
3 //! Unix systems expose a number of `ioctl`'s. `ioctl`s have been adopted as a
4 //! general purpose system call for making calls into the kernel. In addition
5 //! to the wide variety of system calls that are included by default in the
6 //! kernel, many drivers expose their own `ioctl`'s for controlling their
7 //! behavior, some of which are proprietary. Therefore it is impossible to make
8 //! a safe interface for every `ioctl` call, as they all have wildly varying
11 //! This module provides an unsafe interface to write your own `ioctl` API. To
12 //! start, create a type that implements [`Ioctl`]. Then, pass it to [`ioctl`]
13 //! to make the `ioctl` call.
15 #![allow(unsafe_code)]
17 use crate::backend
::c
;
18 use crate::fd
::{AsFd, BorrowedFd}
;
19 use crate::io
::Result
;
21 #[cfg(any(linux_kernel, bsd))]
35 use linux
as platform
;
40 /// Perform an `ioctl` call.
42 /// `ioctl` was originally intended to act as a way of modifying the behavior
43 /// of files, but has since been adopted as a general purpose system call for
44 /// making calls into the kernel. In addition to the default calls exposed by
45 /// generic file descriptors, many drivers expose their own `ioctl` calls for
46 /// controlling their behavior, some of which are proprietary.
48 /// This crate exposes many other `ioctl` interfaces with safe and idiomatic
49 /// wrappers, like [`ioctl_fionbio`] and [`ioctl_fionread`]. It is recommended
50 /// to use those instead of this function, as they are safer and more
51 /// idiomatic. For other cases, implement the [`Ioctl`] API and pass it to this
54 /// See documentation for [`Ioctl`] for more information.
56 /// [`ioctl_fionbio`]: crate::io::ioctl_fionbio
57 /// [`ioctl_fionread`]: crate::io::ioctl_fionread
61 /// While [`Ioctl`] takes much of the unsafety out of `ioctl` calls, it is
62 /// still unsafe to call this code with arbitrary device drivers, as it is up
63 /// to the device driver to implement the `ioctl` call correctly. It is on the
64 /// onus of the protocol between the user and the driver to ensure that the
65 /// `ioctl` call is safe to make.
77 /// [Linux]: https://man7.org/linux/man-pages/man2/ioctl.2.html
78 /// [Winsock2]: https://learn.microsoft.com/en-us/windows/win32/api/winsock/nf-winsock-ioctlsocket
79 /// [FreeBSD]: https://man.freebsd.org/cgi/man.cgi?query=ioctl&sektion=2
80 /// [NetBSD]: https://man.netbsd.org/ioctl.2
81 /// [OpenBSD]: https://man.openbsd.org/ioctl.2
82 /// [Apple]: https://developer.apple.com/library/archive/documentation/System/Conceptual/ManPages_iPhoneOS/man2/ioctl.2.html
83 /// [Solaris]: https://docs.oracle.com/cd/E23824_01/html/821-1463/ioctl-2.html
84 /// [illumos]: https://illumos.org/man/2/ioctl
86 pub unsafe fn ioctl
<F
: AsFd
, I
: Ioctl
>(fd
: F
, mut ioctl
: I
) -> Result
<I
::Output
> {
88 let request
= I
::OPCODE
.raw();
89 let arg
= ioctl
.as_ptr();
91 // SAFETY: The variant of `Ioctl` asserts that this is a valid IOCTL call
93 let output
= if I
::IS_MUTATING
{
94 _ioctl(fd
, request
, arg
)?
96 _ioctl_readonly(fd
, request
, arg
)?
99 // SAFETY: The variant of `Ioctl` asserts that this is a valid pointer to
101 I
::output_from_ptr(output
, arg
)
108 ) -> Result
<IoctlOutput
> {
109 crate::backend
::io
::syscalls
::ioctl(fd
, request
, arg
)
112 unsafe fn _ioctl_readonly(
116 ) -> Result
<IoctlOutput
> {
117 crate::backend
::io
::syscalls
::ioctl_readonly(fd
, request
, arg
)
120 /// A trait defining the properties of an `ioctl` command.
122 /// Objects implementing this trait can be passed to [`ioctl`] to make an
123 /// `ioctl` call. The contents of the object represent the inputs to the
124 /// `ioctl` call. The inputs must be convertible to a pointer through the
125 /// `as_ptr` method. In most cases, this involves either casting a number to a
126 /// pointer, or creating a pointer to the actual data. The latter case is
127 /// necessary for `ioctl` calls that modify userspace data.
131 /// This trait is unsafe to implement because it is impossible to guarantee
132 /// that the `ioctl` call is safe. The `ioctl` call may be proprietary, or it
133 /// may be unsafe to call in certain circumstances.
135 /// By implementing this trait, you guarantee that:
137 /// - The `ioctl` call expects the input provided by `as_ptr` and produces the
138 /// output as indicated by `output`.
139 /// - That `output_from_ptr` can safely take the pointer from `as_ptr` and cast
140 /// it to the correct type, *only* after the `ioctl` call.
141 /// - That `OPCODE` uniquely identifies the `ioctl` call.
142 /// - That, for whatever platforms you are targeting, the `ioctl` call is safe
144 /// - If `IS_MUTATING` is false, that no userspace data will be modified by the
146 pub unsafe trait Ioctl
{
147 /// The type of the output data.
149 /// Given a pointer, one should be able to construct an instance of this
153 /// The opcode used by this `ioctl` command.
155 /// There are different types of opcode depending on the operation. See
156 /// documentation for the [`Opcode`] struct for more information.
157 const OPCODE
: Opcode
;
159 /// Does the `ioctl` mutate any data in the userspace?
161 /// If the `ioctl` call does not mutate any data in the userspace, then
162 /// making this `false` enables optimizations that can make the call
163 /// faster. When in doubt, set this to `true`.
167 /// This should only be set to `false` if the `ioctl` call does not mutate
168 /// any data in the userspace. Undefined behavior may occur if this is set
169 /// to `false` when it should be `true`.
170 const IS_MUTATING
: bool
;
172 /// Get a pointer to the data to be passed to the `ioctl` command.
174 /// See trait-level documentation for more information.
175 fn as_ptr(&mut self) -> *mut c
::c_void
;
177 /// Cast the output data to the correct type.
181 /// The `extract_output` value must be the resulting value after a
182 /// successful `ioctl` call, and `out` is the direct return value of an
183 /// `ioctl` call that did not fail. In this case `extract_output` is the
184 /// pointer that was passed to the `ioctl` call.
185 unsafe fn output_from_ptr(
187 extract_output
: *mut c
::c_void
,
188 ) -> Result
<Self::Output
>;
191 /// The opcode used by an `Ioctl`.
192 #[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
199 /// Create a new old `Opcode` from a raw opcode.
201 /// Rather than being a composition of several attributes, old opcodes are
202 /// just numbers. In general most drivers follow stricter conventions, but
203 /// older drivers may still use this strategy.
205 pub const fn old(raw
: RawOpcode
) -> Self {
209 /// Create a new opcode from a direction, group, number, and size.
211 /// This corresponds to the C macro `_IOC(direction, group, number, size)`
212 #[cfg(any(linux_kernel, bsd))]
214 pub const fn from_components(
215 direction
: Direction
,
220 if data_size
> RawOpcode
::MAX
as usize {
221 panic
!("data size is too large");
224 Self::old(platform
::compose_opcode(
228 data_size
as RawOpcode
,
232 /// Create a new non-mutating opcode from a group, a number, and the type
235 /// This corresponds to the C macro `_IO(group, number)` when `T` is zero
237 #[cfg(any(linux_kernel, bsd))]
239 pub const fn none
<T
>(group
: u8, number
: u8) -> Self {
240 Self::from_components(Direction
::None
, group
, number
, mem
::size_of
::<T
>())
243 /// Create a new reading opcode from a group, a number and the type of
246 /// This corresponds to the C macro `_IOR(group, number, T)`.
247 #[cfg(any(linux_kernel, bsd))]
249 pub const fn read
<T
>(group
: u8, number
: u8) -> Self {
250 Self::from_components(Direction
::Read
, group
, number
, mem
::size_of
::<T
>())
253 /// Create a new writing opcode from a group, a number and the type of
256 /// This corresponds to the C macro `_IOW(group, number, T)`.
257 #[cfg(any(linux_kernel, bsd))]
259 pub const fn write
<T
>(group
: u8, number
: u8) -> Self {
260 Self::from_components(Direction
::Write
, group
, number
, mem
::size_of
::<T
>())
263 /// Create a new reading and writing opcode from a group, a number and the
266 /// This corresponds to the C macro `_IOWR(group, number, T)`.
267 #[cfg(any(linux_kernel, bsd))]
269 pub const fn read_write
<T
>(group
: u8, number
: u8) -> Self {
270 Self::from_components(Direction
::ReadWrite
, group
, number
, mem
::size_of
::<T
>())
273 /// Get the raw opcode.
275 pub fn raw(self) -> RawOpcode
{
280 /// The direction that an `ioctl` is going.
282 /// Note that this is relative to userspace. `Read` means reading data from the
283 /// kernel, and write means the kernel writing data to userspace.
284 #[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
286 /// None of the above.
289 /// Read data from the kernel.
292 /// Write data to the kernel.
295 /// Read and write data to the kernel.
299 /// The type used by the `ioctl` to signify the output.
300 pub type IoctlOutput
= c
::c_int
;
302 /// The type used by the `ioctl` to signify the command.
303 pub type RawOpcode
= _RawOpcode
;
305 // Under raw Linux, this is an `unsigned int`.
307 type _RawOpcode
= c
::c_uint
;
309 // On libc Linux with GNU libc or uclibc, this is an `unsigned long`.
313 any(target_env
= "gnu", target_env
= "uclibc")
315 type _RawOpcode
= c
::c_ulong
;
317 // Musl uses `c_int`.
321 not(target_env
= "gnu"),
322 not(target_env
= "uclibc")
324 type _RawOpcode
= c
::c_int
;
326 // Android uses `c_int`.
327 #[cfg(all(not(linux_raw), target_os = "android"))]
328 type _RawOpcode
= c
::c_int
;
330 // BSD, Haiku, Hurd, Redox, and Vita use `unsigned long`.
338 type _RawOpcode
= c
::c_ulong
;
340 // AIX, Emscripten, Fuchsia, Solaris, and WASI use a `int`.
344 target_os
= "fuchsia",
345 target_os
= "emscripten",
349 type _RawOpcode
= c
::c_int
;
351 // ESP-IDF uses a `c_uint`.
352 #[cfg(target_os = "espidf")]
353 type _RawOpcode
= c
::c_uint
;
355 // Windows has `ioctlsocket`, which uses `i32`.
357 type _RawOpcode
= i32;