Debian Codegraph @

debian/rust-vendor/inotify/src/fd_guard.rs

raw
1 use std::{
2 ops::Deref,
3 os::unix::io::{AsFd, AsRawFd, BorrowedFd, FromRawFd, IntoRawFd, RawFd},
4 sync::atomic::{AtomicBool, Ordering},
5 };
6
7 use inotify_sys as ffi;
8
9 use crate::util;
10
11 /// A RAII guard around a `RawFd` that closes it automatically on drop.
12 #[derive(Debug)]
13 pub struct FdGuard {
14 pub(crate) fd: RawFd,
15 pub(crate) close_on_drop: AtomicBool,
16 }
17
18 impl FdGuard {
19 /// Indicate that the wrapped file descriptor should _not_ be closed
20 /// when the guard is dropped.
21 ///
22 /// This should be called in cases where ownership of the wrapped file
23 /// descriptor has been "moved" out of the guard.
24 ///
25 /// This is factored out into a separate function to ensure that it's
26 /// always used consistently.
27 #[inline]
28 pub fn should_not_close(&self) {
29 self.close_on_drop.store(false, Ordering::Release);
30 }
31 }
32
33 impl Deref for FdGuard {
34 type Target = RawFd;
35
36 #[inline]
37 fn deref(&self) -> &Self::Target {
38 &self.fd
39 }
40 }
41
42 impl Drop for FdGuard {
43 fn drop(&mut self) {
44 if self.close_on_drop.load(Ordering::Acquire) {
45 unsafe {
46 ffi::close(self.fd);
47 }
48 }
49 }
50 }
51
52 impl FromRawFd for FdGuard {
53 unsafe fn from_raw_fd(fd: RawFd) -> Self {
54 FdGuard {
55 fd,
56 close_on_drop: AtomicBool::new(true),
57 }
58 }
59 }
60
61 impl IntoRawFd for FdGuard {
62 fn into_raw_fd(self) -> RawFd {
63 self.should_not_close();
64 self.fd
65 }
66 }
67
68 impl AsRawFd for FdGuard {
69 fn as_raw_fd(&self) -> RawFd {
70 self.fd
71 }
72 }
73
74 impl AsFd for FdGuard {
75 #[inline]
76 fn as_fd(&self) -> BorrowedFd<'_> {
77 unsafe { BorrowedFd::borrow_raw(self.fd) }
78 }
79 }
80
81 impl PartialEq for FdGuard {
82 fn eq(&self, other: &FdGuard) -> bool {
83 let initial = self.fd == other.fd;
84 if initial {
85 return true;
86 }
87 // This allows comparing duplicated Inotify descriptors that point to the
88 // same Inotify instance, which allows for scenarios where an Inotify
89 // wrapper both owns a file descriptor for control purposes and spawns a
90 // second thread that needs a separate unowned descriptor to use the `epoll-rs`
91 // crate.
92 let current_process = std::process::id();
93 kcmp_file_descriptors(current_process, self.fd, other.fd)
94 }
95 }
96
97 fn kcmp_file_descriptors(current_process: u32, fd1: RawFd, fd2: RawFd) -> bool {
98 match util::libc_convert(unsafe { kcmp_file(current_process, fd1, fd2) }) {
99 Err(_) => false,
100 Ok(cmp) => cmp == 0,
101 }
102 }
103
104 #[cfg(any(target_os = "linux", target_os = "android"))]
105 unsafe fn kcmp_file(current_process: u32, fd1: RawFd, fd2: RawFd) -> i64 {
106 const KCMP_FILE: i32 = 0;
107
108 libc::syscall(
109 libc::SYS_kcmp,
110 current_process,
111 current_process,
112 KCMP_FILE,
113 fd1,
114 fd2,
115 ) as i64
116 }
117
118 #[cfg(target_os = "freebsd")]
119 unsafe fn kcmp_file(current_process: u32, fd1: RawFd, fd2: RawFd) -> i64 {
120 libc::kcmp(
121 current_process as libc::pid_t,
122 current_process as libc::pid_t,
123 libc::KCMP_FILE,
124 fd1 as libc::c_ulong,
125 fd2 as libc::c_ulong,
126 ) as i64
127 }
128
129 #[cfg(not(any(target_os = "linux", target_os = "android", target_os = "freebsd")))]
130 compile_error!("FdGuard equality requires kcmp support; add a target-specific kcmp_file implementation for this platform");