1 //! The Unix `fcntl` function is effectively lots of different functions hidden
2 //! behind a single dynamic dispatch interface. In order to provide a type-safe
3 //! API, rustix makes them all separate functions so that they can have
4 //! dedicated static type signatures.
5 
6 #[cfg(not(any(
7     target_os = "emscripten",
8     target_os = "espidf",
9     target_os = "fuchsia",
10     target_os = "redox",
11     target_os = "vita",
12     target_os = "wasi"
13 )))]
14 use crate::fs::FlockOperation;
15 use crate::{backend, io};
16 use backend::fd::AsFd;
17 use backend::fs::types::OFlags;
18 
19 // These `fcntl` functions live in the `io` module because they're not specific
20 // to files, directories, or memfd objects. We re-export them here in the `fs`
21 // module because the other the `fcntl` functions are here.
22 #[cfg(not(any(target_os = "espidf", target_os = "wasi")))]
23 pub use crate::io::fcntl_dupfd_cloexec;
24 pub use crate::io::{fcntl_getfd, fcntl_setfd};
25 
26 /// `fcntl(fd, F_GETFL)`—Returns a file descriptor's access mode and status.
27 ///
28 /// # References
29 ///  - [POSIX]
30 ///  - [Linux]
31 ///
32 /// [POSIX]: https://pubs.opengroup.org/onlinepubs/9699919799/functions/fcntl.html
33 /// [Linux]: https://man7.org/linux/man-pages/man2/fcntl.2.html
34 #[inline]
35 #[doc(alias = "F_GETFL")]
fcntl_getfl<Fd: AsFd>(fd: Fd) -> io::Result<OFlags>36 pub fn fcntl_getfl<Fd: AsFd>(fd: Fd) -> io::Result<OFlags> {
37     backend::fs::syscalls::fcntl_getfl(fd.as_fd())
38 }
39 
40 /// `fcntl(fd, F_SETFL, flags)`—Sets a file descriptor's status.
41 ///
42 /// # References
43 ///  - [POSIX]
44 ///  - [Linux]
45 ///
46 /// [POSIX]: https://pubs.opengroup.org/onlinepubs/9699919799/functions/fcntl.html
47 /// [Linux]: https://man7.org/linux/man-pages/man2/fcntl.2.html
48 #[inline]
49 #[doc(alias = "F_SETFL")]
fcntl_setfl<Fd: AsFd>(fd: Fd, flags: OFlags) -> io::Result<()>50 pub fn fcntl_setfl<Fd: AsFd>(fd: Fd, flags: OFlags) -> io::Result<()> {
51     backend::fs::syscalls::fcntl_setfl(fd.as_fd(), flags)
52 }
53 
54 /// `fcntl(fd, F_GET_SEALS)`
55 ///
56 /// # References
57 ///  - [Linux]
58 ///
59 /// [Linux]: https://man7.org/linux/man-pages/man2/fcntl.2.html
60 #[cfg(any(linux_kernel, target_os = "freebsd", target_os = "fuchsia"))]
61 #[inline]
62 #[doc(alias = "F_GET_SEALS")]
fcntl_get_seals<Fd: AsFd>(fd: Fd) -> io::Result<SealFlags>63 pub fn fcntl_get_seals<Fd: AsFd>(fd: Fd) -> io::Result<SealFlags> {
64     backend::fs::syscalls::fcntl_get_seals(fd.as_fd())
65 }
66 
67 #[cfg(any(linux_kernel, target_os = "freebsd", target_os = "fuchsia"))]
68 use backend::fs::types::SealFlags;
69 
70 /// `fcntl(fd, F_ADD_SEALS)`
71 ///
72 /// # References
73 ///  - [Linux]
74 ///
75 /// [Linux]: https://man7.org/linux/man-pages/man2/fcntl.2.html
76 #[cfg(any(linux_kernel, target_os = "freebsd", target_os = "fuchsia"))]
77 #[inline]
78 #[doc(alias = "F_ADD_SEALS")]
fcntl_add_seals<Fd: AsFd>(fd: Fd, seals: SealFlags) -> io::Result<()>79 pub fn fcntl_add_seals<Fd: AsFd>(fd: Fd, seals: SealFlags) -> io::Result<()> {
80     backend::fs::syscalls::fcntl_add_seals(fd.as_fd(), seals)
81 }
82 
83 /// `fcntl(fd, F_SETLK)`—Acquire or release an `fcntl`-style lock.
84 ///
85 /// This function doesn't currently have an offset or len; it currently always
86 /// sets the `l_len` field to 0, which is a special case that means the entire
87 /// file should be locked.
88 ///
89 /// Unlike `flock`-style locks, `fcntl`-style locks are process-associated,
90 /// meaning that they don't guard against being acquired by two threads in the
91 /// same process.
92 ///
93 /// # References
94 ///  - [POSIX]
95 ///  - [Linux]
96 ///
97 /// [POSIX]: https://pubs.opengroup.org/onlinepubs/9699919799/functions/fcntl.html
98 /// [Linux]: https://man7.org/linux/man-pages/man2/fcntl.2.html
99 #[cfg(not(any(
100     target_os = "emscripten",
101     target_os = "espidf",
102     target_os = "fuchsia",
103     target_os = "redox",
104     target_os = "vita",
105     target_os = "wasi"
106 )))]
107 #[inline]
108 #[doc(alias = "F_SETLK")]
109 #[doc(alias = "F_SETLKW")]
fcntl_lock<Fd: AsFd>(fd: Fd, operation: FlockOperation) -> io::Result<()>110 pub fn fcntl_lock<Fd: AsFd>(fd: Fd, operation: FlockOperation) -> io::Result<()> {
111     backend::fs::syscalls::fcntl_lock(fd.as_fd(), operation)
112 }
113