oxedyne/fe2o3/fe2o3_sys/src/notify/fan.rs
8.8 KiB, 1 run
created by r1870400018:76747, which is this file's identity for as long as the history lasts, whatever it is later renamed to
download · who wrote it · its history
| 1 | //! A fanotify group that hears every close after a write on whole filesystems. |
| 2 | //! |
| 3 | //! The group is a notification group (`FAN_CLASS_NOTIF`) on the kernel's bounded queue, never |
| 4 | //! `FAN_UNLIMITED_QUEUE`, so a reader that lags behind a big build cannot grow its own memory: when |
| 5 | //! the queue (`fs.fanotify.max_queued_events`) fills, the kernel drops events and the reader gets |
| 6 | //! one [`FanEvent::Overflow`] in their place. Marks are `FAN_MARK_FILESYSTEM`, so a write through any |
| 7 | //! bind mount, container or namespace of a marked filesystem is heard. |
| 8 | //! |
| 9 | //! Each written file is resolved to a host path (see [`super::mounts`]), and the writer is read |
| 10 | //! from `/proc/<pid>` when the event is read. `FAN_REPORT_PIDFD` is not used: nix 0.31 leaves the |
| 11 | //! pidfd it carries open (BR-4e S1, probe 3: open fds grew 6 to 8 over five events). A writer |
| 12 | //! that has exited by then is reported as [`Writer::Exited`], and its file may be |
| 13 | //! [`Unresolved::WriterGone`]; both are losses the caller makes good by a rescan. A pid reused in |
| 14 | //! the meantime can still give a wrong uid on one of the reader's own mounts; a mapped path is |
| 15 | //! guarded, since the mount must appear in that process's table. |
| 16 | //! The root fix is the pidfd: once nix's `FAN_REPORT_PIDFD` leak is fixed upstream, this code |
| 17 | //! takes the pidfd and the residual goes. |
| 18 | //! |
| 19 | //! The system calls come from `nix`, which owns the `unsafe` this crate forbids, and `statx` from |
| 20 | //! `rustix`. |
| 21 | //! |
| 22 | //! [`Unresolved::WriterGone`]: super::mounts::Unresolved::WriterGone |
| 23 | |
| 24 | use crate::notify::mounts::{ |
| 25 | mount_id, |
| 26 | resolve, |
| 27 | HostPath, |
| 28 | Mount, |
| 29 | MountTable, |
| 30 | }; |
| 31 | |
| 32 | use oxedyne_fe2o3_core::prelude::*; |
| 33 | |
| 34 | use nix::{ |
| 35 | errno::Errno, |
| 36 | sys::fanotify::{ |
| 37 | EventFFlags, |
| 38 | Fanotify, |
| 39 | FanotifyEvent, |
| 40 | InitFlags, |
| 41 | MarkFlags, |
| 42 | MaskFlags, |
| 43 | }, |
| 44 | }; |
| 45 | |
| 46 | use std::{ |
| 47 | fs::{ |
| 48 | self, |
| 49 | File, |
| 50 | }, |
| 51 | io::{ |
| 52 | Read, |
| 53 | Seek, |
| 54 | SeekFrom, |
| 55 | }, |
| 56 | os::fd::{ |
| 57 | AsFd, |
| 58 | AsRawFd, |
| 59 | BorrowedFd, |
| 60 | }, |
| 61 | path::Path, |
| 62 | }; |
| 63 | |
| 64 | /// Who wrote a file, as `/proc/<pid>` showed when the event was read. |
| 65 | #[derive(Clone, Debug, Eq, PartialEq)] |
| 66 | pub enum Writer { |
| 67 | Live { |
| 68 | pid: i32, |
| 69 | uid: u32, // Real uid. |
| 70 | fsuid: u32, // The uid the write was checked against. |
| 71 | cgroup: String, // The cgroup v2 path, from `0::` in /proc/<pid>/cgroup. |
| 72 | }, |
| 73 | Exited { pid: i32 }, |
| 74 | } |
| 75 | |
| 76 | /// One write the group heard. The event's descriptor stays open while this is held, so a caller |
| 77 | /// can follow the file through it ([`super::follow::Follower::follow_fd`]); drop it to close it. |
| 78 | #[derive(Debug)] |
| 79 | pub struct Written { |
| 80 | pub mnt: u64, |
| 81 | pub ino: u64, |
| 82 | pub link: String, // The descriptor's path in the writer's view. |
| 83 | pub host: HostPath, |
| 84 | pub writer: Writer, |
| 85 | event: FanotifyEvent, |
| 86 | } |
| 87 | |
| 88 | impl Written { |
| 89 | pub fn fd(&self) -> Option<BorrowedFd<'_>> { |
| 90 | self.event.fd() |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | #[derive(Debug)] |
| 95 | pub enum FanEvent { |
| 96 | Written(Written), |
| 97 | Overflow, // The kernel's queue filled and events were dropped. |
| 98 | NoFd { pid: i32 }, // An event with no descriptor that is not an overflow: also a loss. |
| 99 | } |
| 100 | |
| 101 | /// A fanotify notification group with its own view of the mounts. |
| 102 | pub struct FanGroup { |
| 103 | fan: Fanotify, |
| 104 | mountinfo: File, // Held for `POLLPRI` on a mount or unmount. |
| 105 | own: MountTable, |
| 106 | } |
| 107 | |
| 108 | impl FanGroup { |
| 109 | /// Needs `CAP_SYS_ADMIN`. |
| 110 | pub fn new() -> Outcome<Self> { |
| 111 | let flags = InitFlags::FAN_CLASS_NOTIF | InitFlags::FAN_CLOEXEC | InitFlags::FAN_NONBLOCK; |
| 112 | let ev = EventFFlags::O_RDONLY | EventFFlags::O_LARGEFILE | EventFFlags::O_CLOEXEC; |
| 113 | let fan = res!(Fanotify::init(flags, ev).map_err(|e| |
| 114 | err!("fanotify_init (needs CAP_SYS_ADMIN): {}.", e; IO, System))); |
| 115 | let mut mountinfo = res!(File::open("/proc/self/mountinfo").map_err(|e| |
| 116 | err!("Opening /proc/self/mountinfo: {}.", e; IO, File, Read))); |
| 117 | let own = res!(read_table(&mut mountinfo)); |
| 118 | Ok(Self { fan, mountinfo, own }) |
| 119 | } |
| 120 | |
| 121 | /// The kernel's queue length for a group, `fs.fanotify.max_queued_events`. |
| 122 | pub fn max_queued_events() -> Outcome<u64> { |
| 123 | let p = "/proc/sys/fs/fanotify/max_queued_events"; |
| 124 | let s = res!(fs::read_to_string(p).map_err(|e| err!("Reading '{}': {}.", p, e; IO, File, Read))); |
| 125 | Ok(res!(s.trim().parse::<u64>().map_err(|_| |
| 126 | err!("'{}' holds '{}', not a count.", p, s.trim(); Invalid, Input)))) |
| 127 | } |
| 128 | |
| 129 | /// Marks the whole filesystem that holds `path` for `FAN_CLOSE_WRITE`. |
| 130 | pub fn mark_filesystem(&self, path: &Path) -> Outcome<()> { |
| 131 | self.mark(path, MarkFlags::FAN_MARK_ADD) |
| 132 | } |
| 133 | |
| 134 | pub fn unmark_filesystem(&self, path: &Path) -> Outcome<()> { |
| 135 | self.mark(path, MarkFlags::FAN_MARK_REMOVE) |
| 136 | } |
| 137 | |
| 138 | fn mark(&self, path: &Path, how: MarkFlags) -> Outcome<()> { |
| 139 | let at = res!(File::open(path).map_err(|e| |
| 140 | err!("Opening '{}' to mark its filesystem: {}.", path.display(), e; IO, File, Read))); |
| 141 | res!(self.fan.mark(how | MarkFlags::FAN_MARK_FILESYSTEM, MaskFlags::FAN_CLOSE_WRITE, |
| 142 | at.as_fd(), None::<&Path>).map_err(|e| |
| 143 | err!("fanotify_mark {:?} on the filesystem of '{}': {}.", how, path.display(), e; IO, System))); |
| 144 | Ok(()) |
| 145 | } |
| 146 | |
| 147 | /// Readable (`POLLIN`) when events wait. |
| 148 | pub fn fd(&self) -> BorrowedFd<'_> { |
| 149 | self.fan.as_fd() |
| 150 | } |
| 151 | |
| 152 | /// Raises `POLLPRI` when a mount or unmount changes the reader's table; then call |
| 153 | /// [`Self::refresh_mounts`]. |
| 154 | pub fn mounts_fd(&self) -> BorrowedFd<'_> { |
| 155 | self.mountinfo.as_fd() |
| 156 | } |
| 157 | |
| 158 | pub fn mounts(&self) -> &MountTable { |
| 159 | &self.own |
| 160 | } |
| 161 | |
| 162 | /// Re-reads the reader's mount table and answers the mounts that are new, which the caller |
| 163 | /// marks if it wants them heard. |
| 164 | pub fn refresh_mounts(&mut self) -> Outcome<Vec<Mount>> { |
| 165 | let now = res!(read_table(&mut self.mountinfo)); |
| 166 | let added = now.added_since(&self.own); |
| 167 | self.own = now; |
| 168 | Ok(added) |
| 169 | } |
| 170 | |
| 171 | /// Reads the events waiting, none if there are none. |
| 172 | pub fn read(&self) -> Outcome<Vec<FanEvent>> { |
| 173 | let evs = match self.fan.read_events() { |
| 174 | Ok(v) => v, |
| 175 | Err(Errno::EAGAIN) => return Ok(Vec::new()), |
| 176 | Err(e) => return Err(err!("Reading fanotify events: {}.", e; IO, Read)), |
| 177 | }; |
| 178 | let mut out = Vec::with_capacity(evs.len()); |
| 179 | for ev in evs { |
| 180 | if !ev.check_version() { |
| 181 | return Err(err!("fanotify metadata version {} is not the one built against.", |
| 182 | ev.version(); Invalid, System)); |
| 183 | } |
| 184 | if ev.mask().contains(MaskFlags::FAN_Q_OVERFLOW) { |
| 185 | out.push(FanEvent::Overflow); |
| 186 | continue; |
| 187 | } |
| 188 | let pid = ev.pid(); |
| 189 | let raw = match ev.fd() { |
| 190 | Some(fd) => fd.as_raw_fd(), |
| 191 | None => { out.push(FanEvent::NoFd { pid }); continue; } |
| 192 | }; |
| 193 | let link = match fs::read_link(format!("/proc/self/fd/{}", raw)) { |
| 194 | Ok(p) => p.to_string_lossy().into_owned(), |
| 195 | Err(e) => return Err(err!("Reading the path of event fd {}: {}.", raw, e; IO, Read)), |
| 196 | }; |
| 197 | let (mnt, ino) = match ev.fd() { |
| 198 | Some(fd) => res!(mount_id(fd)), |
| 199 | None => (0, 0), |
| 200 | }; |
| 201 | let host = if self.own.get(mnt).is_some() { |
| 202 | resolve(&link, mnt, &self.own, None) |
| 203 | } else { |
| 204 | resolve(&link, mnt, &self.own, MountTable::of_pid(pid).as_ref()) |
| 205 | }; |
| 206 | let writer = writer_of(pid); |
| 207 | out.push(FanEvent::Written(Written { mnt, ino, link, host, writer, event: ev })); |
| 208 | } |
| 209 | Ok(out) |
| 210 | } |
| 211 | } |
| 212 | |
| 213 | fn read_table(f: &mut File) -> Outcome<MountTable> { |
| 214 | let mut s = String::new(); |
| 215 | res!(f.seek(SeekFrom::Start(0)).map_err(|e| err!("Rewinding /proc/self/mountinfo: {}.", e; IO, Read))); |
| 216 | res!(f.read_to_string(&mut s).map_err(|e| err!("Reading /proc/self/mountinfo: {}.", e; IO, Read))); |
| 217 | MountTable::parse(&s) |
| 218 | } |
| 219 | |
| 220 | /// The writer as `/proc/<pid>` shows it now, or exited if it cannot be read. |
| 221 | pub fn writer_of(pid: i32) -> Writer { |
| 222 | let status = match fs::read_to_string(format!("/proc/{}/status", pid)) { |
| 223 | Ok(s) => s, |
| 224 | Err(_) => return Writer::Exited { pid }, |
| 225 | }; |
| 226 | let uids: Vec<u32> = status.lines() |
| 227 | .find_map(|l| l.strip_prefix("Uid:")) |
| 228 | .map(|r| r.split_whitespace().filter_map(|x| x.parse().ok()).collect()) |
| 229 | .unwrap_or_default(); |
| 230 | if uids.len() != 4 { return Writer::Exited { pid }; } |
| 231 | let cgroup = fs::read_to_string(format!("/proc/{}/cgroup", pid)).ok() |
| 232 | .and_then(|s| s.lines().find_map(|l| l.strip_prefix("0::").map(|c| c.to_string()))) |
| 233 | .unwrap_or_default(); |
| 234 | Writer::Live { pid, uid: uids[0], fsuid: uids[3], cgroup } |
| 235 | } |