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