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