xref: /third_party/rust/crates/rustix/src/io/fcntl.rs (revision b8a62b91)
1//! The Unix `fcntl` 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//!
6//! `fcntl` functions which are not specific to files or directories live
7//! in the [`io`] module instead.
8//!
9//! [`io`]: crate::io
10
11use crate::{backend, io};
12use backend::fd::{AsFd, OwnedFd, RawFd};
13
14pub use backend::io::types::FdFlags;
15
16/// `fcntl(fd, F_GETFD)`—Returns a file descriptor's flags.
17///
18/// # References
19///  - [POSIX]
20///  - [Linux]
21///
22/// [POSIX]: https://pubs.opengroup.org/onlinepubs/9699919799/functions/fcntl.html
23/// [Linux]: https://man7.org/linux/man-pages/man2/fcntl.2.html
24#[inline]
25#[doc(alias = "F_GETFD")]
26pub fn fcntl_getfd<Fd: AsFd>(fd: Fd) -> io::Result<FdFlags> {
27    backend::io::syscalls::fcntl_getfd(fd.as_fd())
28}
29
30/// `fcntl(fd, F_SETFD, flags)`—Sets a file descriptor's flags.
31///
32/// # References
33///  - [POSIX]
34///  - [Linux]
35///
36/// [POSIX]: https://pubs.opengroup.org/onlinepubs/9699919799/functions/fcntl.html
37/// [Linux]: https://man7.org/linux/man-pages/man2/fcntl.2.html
38#[inline]
39#[doc(alias = "F_SETFD")]
40pub fn fcntl_setfd<Fd: AsFd>(fd: Fd, flags: FdFlags) -> io::Result<()> {
41    backend::io::syscalls::fcntl_setfd(fd.as_fd(), flags)
42}
43
44/// `fcntl(fd, F_DUPFD_CLOEXEC)`—Creates a new `OwnedFd` instance, with value
45/// at least `min`, that has `O_CLOEXEC` set and that shares the same
46/// underlying [file description] as `fd`.
47///
48/// POSIX guarantees that `F_DUPFD_CLOEXEC` will use the lowest unused file
49/// descriptor which is at least `min`, however it is not safe in general to
50/// rely on this, as file descriptors may be unexpectedly allocated on other
51/// threads or in libraries.
52///
53/// # References
54///  - [POSIX]
55///  - [Linux]
56///
57/// [POSIX]: https://pubs.opengroup.org/onlinepubs/9699919799/functions/fcntl.html
58/// [Linux]: https://man7.org/linux/man-pages/man2/fcntl.2.html
59#[cfg(not(any(target_os = "wasi", target_os = "espidf")))]
60#[inline]
61#[doc(alias = "F_DUPFD_CLOEXEC")]
62pub fn fcntl_dupfd_cloexec<Fd: AsFd>(fd: Fd, min: RawFd) -> io::Result<OwnedFd> {
63    backend::io::syscalls::fcntl_dupfd_cloexec(fd.as_fd(), min)
64}
65
66/// `fcntl(fd, F_DUPFD)`—Creates a new `OwnedFd` instance, with value at least
67/// `min`, that shares the same underlying [file description] as `fd`.
68///
69/// POSIX guarantees that `F_DUPFD` will use the lowest unused file descriptor
70/// which is at least `min`, however it is not safe in general to rely on this,
71/// as file descriptors may be unexpectedly allocated on other threads or in
72/// libraries.
73///
74/// # References
75///  - [POSIX]
76///  - [Linux]
77///
78/// [POSIX]: https://pubs.opengroup.org/onlinepubs/9699919799/functions/fcntl.html
79/// [Linux]: https://man7.org/linux/man-pages/man2/fcntl.2.html
80#[cfg(target_os = "espidf")]
81#[inline]
82#[doc(alias = "F_DUPFD")]
83pub fn fcntl_dupfd<Fd: AsFd>(fd: Fd, min: RawFd) -> io::Result<OwnedFd> {
84    backend::io::syscalls::fcntl_dupfd(fd.as_fd(), min)
85}
86