xref: /third_party/rust/crates/rustix/src/io/ioctl.rs (revision b8a62b91)
1//! The Unix `ioctl` function is effectively lots of different functions
2//! hidden behind a single dynamic dispatch interface. In order to provide
3//! a type-safe API, rustix makes them all separate functions so that they
4//! can have dedicated static type signatures.
5
6use crate::{backend, io};
7use backend::fd::AsFd;
8
9/// `ioctl(fd, TIOCEXCL)`—Enables exclusive mode on a terminal.
10///
11/// # References
12///  - [Linux]
13///
14/// [Linux]: https://man7.org/linux/man-pages/man4/tty_ioctl.4.html
15#[cfg(not(any(windows, target_os = "haiku", target_os = "redox", target_os = "wasi")))]
16#[inline]
17#[doc(alias = "TIOCEXCL")]
18pub fn ioctl_tiocexcl<Fd: AsFd>(fd: Fd) -> io::Result<()> {
19    backend::io::syscalls::ioctl_tiocexcl(fd.as_fd())
20}
21
22/// `ioctl(fd, TIOCNXCL)`—Disables exclusive mode on a terminal.
23///
24/// # References
25///  - [Linux]
26///
27/// [Linux]: https://man7.org/linux/man-pages/man4/tty_ioctl.4.html
28#[cfg(not(any(windows, target_os = "haiku", target_os = "redox", target_os = "wasi")))]
29#[inline]
30#[doc(alias = "TIOCNXCL")]
31pub fn ioctl_tiocnxcl<Fd: AsFd>(fd: Fd) -> io::Result<()> {
32    backend::io::syscalls::ioctl_tiocnxcl(fd.as_fd())
33}
34
35/// `ioctl(fd, FIOCLEX)`—Set the close-on-exec flag.
36///
37/// Also known as `fcntl(fd, F_SETFD, FD_CLOEXEC)`.
38///
39/// # References
40///  - [Linux]
41///  - [Winsock2]
42///
43/// [Linux]: https://man7.org/linux/man-pages/man2/ioctl.2.html
44/// [Winsock2]: https://docs.microsoft.com/en-us/windows/win32/api/winsock2/nf-winsock2-ioctlsocket
45#[cfg(any(target_os = "ios", target_os = "macos"))]
46#[inline]
47#[doc(alias = "FIOCLEX")]
48#[doc(alias = "FD_CLOEXEC")]
49pub fn ioctl_fioclex<Fd: AsFd>(fd: Fd) -> io::Result<()> {
50    backend::io::syscalls::ioctl_fioclex(fd.as_fd())
51}
52
53/// `ioctl(fd, FIONBIO, &value)`—Enables or disables non-blocking mode.
54///
55/// # References
56///  - [Linux]
57///  - [Winsock2]
58///
59/// [Linux]: https://man7.org/linux/man-pages/man2/ioctl.2.html
60/// [Winsock2]: https://docs.microsoft.com/en-us/windows/win32/winsock/winsock-ioctls#unix-ioctl-codes
61#[inline]
62#[doc(alias = "FIONBIO")]
63pub fn ioctl_fionbio<Fd: AsFd>(fd: Fd, value: bool) -> io::Result<()> {
64    backend::io::syscalls::ioctl_fionbio(fd.as_fd(), value)
65}
66
67/// `ioctl(fd, FIONREAD)`—Returns the number of bytes ready to be read.
68///
69/// The result of this function gets silently coerced into a C `int`
70/// by the OS, so it may contain a wrapped value.
71///
72/// # References
73///  - [Linux]
74///  - [Winsock2]
75///
76/// [Linux]: https://man7.org/linux/man-pages/man2/ioctl_tty.2.html
77/// [Winsock2]: https://docs.microsoft.com/en-us/windows/win32/winsock/winsock-ioctls#unix-ioctl-codes
78#[cfg(not(target_os = "redox"))]
79#[inline]
80#[doc(alias = "FIONREAD")]
81pub fn ioctl_fionread<Fd: AsFd>(fd: Fd) -> io::Result<u64> {
82    backend::io::syscalls::ioctl_fionread(fd.as_fd())
83}
84
85/// `ioctl(fd, BLKSSZGET)`—Returns the logical block size of a block device.
86#[cfg(any(target_os = "android", target_os = "linux"))]
87#[inline]
88#[doc(alias = "BLKSSZGET")]
89pub fn ioctl_blksszget<Fd: AsFd>(fd: Fd) -> io::Result<u32> {
90    backend::io::syscalls::ioctl_blksszget(fd.as_fd())
91}
92
93/// `ioctl(fd, BLKPBSZGET)`—Returns the physical block size of a block device.
94#[cfg(any(target_os = "android", target_os = "linux"))]
95#[inline]
96#[doc(alias = "BLKPBSZGET")]
97pub fn ioctl_blkpbszget<Fd: AsFd>(fd: Fd) -> io::Result<u32> {
98    backend::io::syscalls::ioctl_blkpbszget(fd.as_fd())
99}
100