xref: /third_party/rust/crates/rustix/src/fs/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
6use crate::{backend, io};
7use backend::fd::AsFd;
8use backend::fs::types::OFlags;
9
10// These `fcntl` functions like in the `io` module because they're not specific
11// to files, directories, or memfd objects. We re-export them here in the `fs`
12// module because the other the `fcntl` functions are here.
13#[cfg(not(target_os = "wasi"))]
14pub use crate::io::fcntl_dupfd_cloexec;
15pub use crate::io::{fcntl_getfd, fcntl_setfd};
16
17/// `fcntl(fd, F_GETFL)`—Returns a file descriptor's access mode and status.
18///
19/// # References
20///  - [POSIX]
21///  - [Linux]
22///
23/// [POSIX]: https://pubs.opengroup.org/onlinepubs/9699919799/functions/fcntl.html
24/// [Linux]: https://man7.org/linux/man-pages/man2/fcntl.2.html
25#[inline]
26#[doc(alias = "F_GETFL")]
27pub fn fcntl_getfl<Fd: AsFd>(fd: Fd) -> io::Result<OFlags> {
28    backend::fs::syscalls::fcntl_getfl(fd.as_fd())
29}
30
31/// `fcntl(fd, F_SETFL, flags)`—Sets a file descriptor's status.
32///
33/// # References
34///  - [POSIX]
35///  - [Linux]
36///
37/// [POSIX]: https://pubs.opengroup.org/onlinepubs/9699919799/functions/fcntl.html
38/// [Linux]: https://man7.org/linux/man-pages/man2/fcntl.2.html
39#[inline]
40#[doc(alias = "F_SETFL")]
41pub fn fcntl_setfl<Fd: AsFd>(fd: Fd, flags: OFlags) -> io::Result<()> {
42    backend::fs::syscalls::fcntl_setfl(fd.as_fd(), flags)
43}
44
45/// `fcntl(fd, F_GET_SEALS)`
46///
47/// # References
48///  - [Linux]
49///
50/// [Linux]: https://man7.org/linux/man-pages/man2/fcntl.2.html
51#[cfg(any(
52    target_os = "android",
53    target_os = "freebsd",
54    target_os = "fuchsia",
55    target_os = "linux",
56))]
57#[inline]
58#[doc(alias = "F_GET_SEALS")]
59pub fn fcntl_get_seals<Fd: AsFd>(fd: Fd) -> io::Result<SealFlags> {
60    backend::fs::syscalls::fcntl_get_seals(fd.as_fd())
61}
62
63#[cfg(any(
64    target_os = "android",
65    target_os = "freebsd",
66    target_os = "fuchsia",
67    target_os = "linux",
68))]
69pub use backend::fs::types::SealFlags;
70
71/// `fcntl(fd, F_ADD_SEALS)`
72///
73/// # References
74///  - [Linux]
75///
76/// [Linux]: https://man7.org/linux/man-pages/man2/fcntl.2.html
77#[cfg(any(
78    target_os = "android",
79    target_os = "freebsd",
80    target_os = "fuchsia",
81    target_os = "linux",
82))]
83#[inline]
84#[doc(alias = "F_ADD_SEALS")]
85pub fn fcntl_add_seals<Fd: AsFd>(fd: Fd, seals: SealFlags) -> io::Result<()> {
86    backend::fs::syscalls::fcntl_add_seals(fd.as_fd(), seals)
87}
88