oxedyne/fe2o3/fe2o3_sys/src/inotify.rs
27.4 KiB, 1 run
created by r1870400018:63103, 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 | //! Watching a directory tree for change, as a wake signal and never as a record. |
| 2 | //! |
| 3 | //! The kernel's inotify queue is reduced to one fact, that something under the root |
| 4 | //! moved, together with whatever rename pairs the kernel's cookies offer. What changed |
| 5 | //! is for a re-reading of the tree to decide, so a dropped or late event costs a look |
| 6 | //! and never a lost change. Placement follows the caller's predicate, so a tree is |
| 7 | //! watched exactly where its owner would read it. |
| 8 | //! |
| 9 | //! The system calls come from `nix`, which owns the `unsafe` this crate forbids. |
| 10 | //! Nothing outside this file names it, so another system call crate would replace this |
| 11 | //! file alone. The kernel's own list of watches, `/proc/self/fdinfo/<fd>`, is the |
| 12 | //! oracle the tests check placement against. |
| 13 | |
| 14 | use oxedyne_fe2o3_core::prelude::*; |
| 15 | |
| 16 | use std::{ |
| 17 | collections::{ |
| 18 | HashMap, |
| 19 | HashSet, |
| 20 | }, |
| 21 | ffi::OsStr, |
| 22 | fs, |
| 23 | os::{ |
| 24 | fd::{ |
| 25 | AsFd, |
| 26 | AsRawFd, |
| 27 | RawFd, |
| 28 | }, |
| 29 | unix::ffi::OsStrExt, |
| 30 | }, |
| 31 | path::{ |
| 32 | Path, |
| 33 | PathBuf, |
| 34 | }, |
| 35 | time::{ |
| 36 | Duration, |
| 37 | Instant, |
| 38 | }, |
| 39 | }; |
| 40 | |
| 41 | use nix::{ |
| 42 | errno::Errno, |
| 43 | poll::{ |
| 44 | poll, |
| 45 | PollFd, |
| 46 | PollFlags, |
| 47 | PollTimeout, |
| 48 | }, |
| 49 | sys::inotify::{ |
| 50 | AddWatchFlags, |
| 51 | InitFlags, |
| 52 | Inotify, |
| 53 | InotifyEvent, |
| 54 | WatchDescriptor, |
| 55 | }, |
| 56 | }; |
| 57 | |
| 58 | pub const DEFAULT_BUDGET: usize = 8_192; // an eighth of the per-user 65,536 |
| 59 | |
| 60 | const DRAIN_READS: usize = 4_096; // reads per wait, so a flood cannot hold a caller |
| 61 | const POLL_CHUNK: u128 = 60_000; // longest single poll, in milliseconds |
| 62 | |
| 63 | // Why a tree could not be, or could no longer be, fully watched. |
| 64 | #[derive(Clone, Copy, Debug, Eq, PartialEq)] |
| 65 | pub enum Limit { |
| 66 | Watches, // the kernel's per-user watch count (ENOSPC) |
| 67 | Instances, // the instance or descriptor limit (EMFILE) |
| 68 | Budget, // this tree's own budget |
| 69 | } |
| 70 | |
| 71 | /// A rename the kernel paired by cookie, as paths relative to the root. |
| 72 | #[derive(Clone, Debug, Eq, PartialEq)] |
| 73 | pub struct Renamed { |
| 74 | pub from: Vec<u8>, |
| 75 | pub to: Vec<u8>, |
| 76 | pub dir: bool, |
| 77 | } |
| 78 | |
| 79 | /// What a [`Watch::wait`] woke for. `Moved` and `LookNow` are the only wakes that say |
| 80 | /// the tree changed; the rest are about the watch itself. |
| 81 | #[derive(Clone, Debug, Eq, PartialEq)] |
| 82 | pub enum Wake { |
| 83 | Moved(Vec<Renamed>), // something moved; the pairs may be empty |
| 84 | LookNow, // the queue overflowed, so only a full reading is safe |
| 85 | Exhausted(Limit), // every watch has been released; fall back to polling |
| 86 | RootGone, // the root was deleted or moved; every watch is released |
| 87 | Timeout, |
| 88 | } |
| 89 | |
| 90 | /// The result of placing the watches. |
| 91 | #[derive(Debug)] |
| 92 | pub enum Opened { |
| 93 | Watching(Watch), |
| 94 | Exhausted(Limit), // nothing is left watched, and no instance is kept |
| 95 | } |
| 96 | |
| 97 | #[derive(Debug)] |
| 98 | struct Dir { |
| 99 | path: Vec<u8>, // relative to the root, `/` separated, empty for the root |
| 100 | } |
| 101 | |
| 102 | #[derive(Debug)] |
| 103 | struct Pending { |
| 104 | from: Vec<u8>, |
| 105 | dir: bool, |
| 106 | } |
| 107 | |
| 108 | #[derive(Debug)] |
| 109 | enum Done { |
| 110 | Gone, |
| 111 | Short(Limit), |
| 112 | } |
| 113 | |
| 114 | enum Walked { |
| 115 | Held, |
| 116 | Short(Limit), |
| 117 | Gone, |
| 118 | } |
| 119 | |
| 120 | #[derive(Default)] |
| 121 | struct Reduced { |
| 122 | moved: bool, |
| 123 | overflow: bool, |
| 124 | dirty: bool, // directories moved, so the whole tree is reconciled |
| 125 | gone: bool, |
| 126 | grafts: Vec<Vec<u8>>, |
| 127 | pairs: Vec<Renamed>, |
| 128 | } |
| 129 | |
| 130 | /// An inotify instance holding one watch on each admitted directory of a tree. |
| 131 | /// |
| 132 | /// Whenever [`Watch::open`] or [`Watch::wait`] returns, every admitted directory under |
| 133 | /// the root holds exactly one watch and no other directory holds one, so far as the |
| 134 | /// events read can tell. A directory the process cannot read is not watched. |
| 135 | #[derive(Debug)] |
| 136 | pub struct Watch { |
| 137 | ino: Inotify, |
| 138 | root: PathBuf, |
| 139 | rootwd: Option<WatchDescriptor>, |
| 140 | budget: usize, |
| 141 | dirs: HashMap<WatchDescriptor, Dir>, |
| 142 | paths: HashMap<Vec<u8>, WatchDescriptor>, |
| 143 | carry: HashMap<u32, Pending>, // renames seen leaving, awaiting one more read |
| 144 | done: Option<Done>, |
| 145 | } |
| 146 | |
| 147 | impl AsRawFd for Watch { |
| 148 | fn as_raw_fd(&self) -> RawFd { |
| 149 | self.ino.as_fd().as_raw_fd() |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | impl Watch { |
| 154 | |
| 155 | /// Places a watch on `root` and on every directory beneath it that `descend` admits. |
| 156 | /// |
| 157 | /// `descend` is asked with each directory's path relative to the root, as bytes |
| 158 | /// with `/` separators, and is never asked about the root, which is always watched. |
| 159 | /// It must give the same answer for the same path every time. A symbolic link to a |
| 160 | /// directory is never descended. The budget bounds the watches held when a call |
| 161 | /// returns; a call that rearranges many directories at once can briefly hold more. |
| 162 | pub fn open<F>( |
| 163 | root: &Path, |
| 164 | budget: usize, |
| 165 | mut descend: F, |
| 166 | ) |
| 167 | -> Outcome<Opened> |
| 168 | where F: FnMut(&[u8]) -> bool, |
| 169 | { |
| 170 | let ino = match Inotify::init(InitFlags::IN_NONBLOCK | InitFlags::IN_CLOEXEC) { |
| 171 | Ok(ino) => ino, |
| 172 | Err(e) => match init_limit(e) { |
| 173 | Some(limit) => return Ok(Opened::Exhausted(limit)), |
| 174 | None => return Err(err!( |
| 175 | "Cannot initialise inotify for {:?}: {}.", root, e; |
| 176 | IO, System, Init)), |
| 177 | }, |
| 178 | }; |
| 179 | match fs::metadata(root) { |
| 180 | Ok(m) if m.is_dir() => (), |
| 181 | Ok(_) => return Err(err!( |
| 182 | "Cannot watch {:?}: not a directory.", root; |
| 183 | Input, Invalid, Path)), |
| 184 | Err(e) => return Err(err!( |
| 185 | "Cannot watch {:?}: {}.", root, e; |
| 186 | IO, File, Missing)), |
| 187 | } |
| 188 | let mut watch = Self { |
| 189 | ino, |
| 190 | root: root.to_path_buf(), |
| 191 | rootwd: None, |
| 192 | budget, |
| 193 | dirs: HashMap::new(), |
| 194 | paths: HashMap::new(), |
| 195 | carry: HashMap::new(), |
| 196 | done: None, |
| 197 | }; |
| 198 | match res!(watch.walk(vec![Vec::new()], true, &mut descend)) { |
| 199 | Walked::Held => Ok(Opened::Watching(watch)), |
| 200 | Walked::Short(limit) => { |
| 201 | watch.release(); |
| 202 | Ok(Opened::Exhausted(limit)) |
| 203 | }, |
| 204 | Walked::Gone => Err(err!( |
| 205 | "Cannot watch {:?}: it vanished while the watches were placed.", root; |
| 206 | IO, File, Missing)), |
| 207 | } |
| 208 | } |
| 209 | |
| 210 | /// Waits up to `timeout` for the tree to change, and reduces what the kernel queued. |
| 211 | /// |
| 212 | /// An overflow always gives `LookNow` and a full reconcile, and any other event |
| 213 | /// gives `Moved`. A rename is paired only when the kernel's cookie pairs it, and one |
| 214 | /// seen leaving is remembered for a single further read in case its arrival follows. |
| 215 | /// `Exhausted` and `RootGone` release every watch and repeat at once on every later call. |
| 216 | pub fn wait<F>( |
| 217 | &mut self, |
| 218 | timeout: Duration, |
| 219 | mut descend: F, |
| 220 | ) |
| 221 | -> Outcome<Wake> |
| 222 | where F: FnMut(&[u8]) -> bool, |
| 223 | { |
| 224 | match &self.done { |
| 225 | Some(Done::Gone) => return Ok(Wake::RootGone), |
| 226 | Some(Done::Short(l)) => return Ok(Wake::Exhausted(*l)), |
| 227 | None => (), |
| 228 | } |
| 229 | let deadline = Instant::now().checked_add(timeout); |
| 230 | loop { |
| 231 | if !res!(self.ready(deadline)) { |
| 232 | return Ok(Wake::Timeout); |
| 233 | } |
| 234 | if let Some(wake) = res!(self.drain(&mut descend)) { |
| 235 | return Ok(wake); |
| 236 | } |
| 237 | // Only bookkeeping was read, such as a release of ours; wait on. |
| 238 | } |
| 239 | } |
| 240 | |
| 241 | /// How many watches are held. |
| 242 | pub fn watches(&self) -> usize { |
| 243 | self.dirs.len() |
| 244 | } |
| 245 | |
| 246 | // Polls until the descriptor is readable or the deadline passes. |
| 247 | fn ready(&self, deadline: Option<Instant>) -> Outcome<bool> { |
| 248 | loop { |
| 249 | let ms = match deadline { |
| 250 | None => POLL_CHUNK, |
| 251 | Some(d) => { |
| 252 | // Rounded up, so the wait never ends before its time. |
| 253 | let rem = d.saturating_duration_since(Instant::now()); |
| 254 | let ms = rem.as_millis(); |
| 255 | if rem.subsec_nanos() % 1_000_000 != 0 { ms + 1 } else { ms } |
| 256 | }, |
| 257 | }; |
| 258 | let ms = ms.min(POLL_CHUNK) as u16; |
| 259 | let mut fds = [PollFd::new(self.ino.as_fd(), PollFlags::POLLIN)]; |
| 260 | match poll(&mut fds, PollTimeout::from(ms)) { |
| 261 | Ok(0) => match deadline { |
| 262 | Some(d) if Instant::now() >= d => return Ok(false), |
| 263 | _ => continue, |
| 264 | }, |
| 265 | Ok(_) => { |
| 266 | let rev = fds[0].revents().unwrap_or(PollFlags::empty()); |
| 267 | if rev.intersects(PollFlags::POLLIN) { |
| 268 | return Ok(true); |
| 269 | } |
| 270 | return Err(err!( |
| 271 | "Polling the inotify descriptor of {:?} reported {:?}.", |
| 272 | self.root, rev; |
| 273 | IO, System)); |
| 274 | }, |
| 275 | Err(Errno::EINTR) => continue, |
| 276 | Err(e) => return Err(err!( |
| 277 | "Polling the inotify descriptor of {:?} failed: {}.", self.root, e; |
| 278 | IO, System)), |
| 279 | } |
| 280 | } |
| 281 | } |
| 282 | |
| 283 | // Reads what is queued and acts on it. None means nothing worth a wake was read. |
| 284 | fn drain<F>(&mut self, descend: &mut F) -> Outcome<Option<Wake>> |
| 285 | where F: FnMut(&[u8]) -> bool, |
| 286 | { |
| 287 | let mut evs = Vec::new(); |
| 288 | for _ in 0..drain_reads() { |
| 289 | match self.ino.read_events() { |
| 290 | Ok(mut batch) => evs.append(&mut batch), |
| 291 | Err(Errno::EAGAIN) => break, |
| 292 | Err(Errno::EINTR) => continue, |
| 293 | Err(e) => return Err(err!( |
| 294 | "Reading inotify events for {:?} failed: {}.", self.root, e; |
| 295 | IO, Read, System)), |
| 296 | } |
| 297 | } |
| 298 | let red = self.reduce(evs); |
| 299 | if red.gone { |
| 300 | return Ok(Some(self.finish(Done::Gone))); |
| 301 | } |
| 302 | if !red.moved && !red.overflow { |
| 303 | return Ok(None); |
| 304 | } |
| 305 | let walked = if red.overflow || red.dirty { |
| 306 | res!(self.walk(vec![Vec::new()], true, descend)) |
| 307 | } else { |
| 308 | let mut starts = Vec::new(); |
| 309 | for graft in red.grafts { |
| 310 | if descend(&graft) { |
| 311 | starts.push(graft); |
| 312 | } |
| 313 | } |
| 314 | if starts.is_empty() { |
| 315 | Walked::Held |
| 316 | } else { |
| 317 | res!(self.walk(starts, false, descend)) |
| 318 | } |
| 319 | }; |
| 320 | match walked { |
| 321 | Walked::Held => (), |
| 322 | Walked::Short(l) => return Ok(Some(self.finish(Done::Short(l)))), |
| 323 | Walked::Gone => return Ok(Some(self.finish(Done::Gone))), |
| 324 | } |
| 325 | if red.overflow { |
| 326 | Ok(Some(Wake::LookNow)) |
| 327 | } else { |
| 328 | Ok(Some(Wake::Moved(red.pairs))) |
| 329 | } |
| 330 | } |
| 331 | |
| 332 | // Boils a batch of events down to the facts the walk and the caller need, keeping |
| 333 | // the maps of watched directories current as renames go by. |
| 334 | fn reduce(&mut self, evs: Vec<InotifyEvent>) -> Reduced { |
| 335 | let mut red = Reduced::default(); |
| 336 | let mut old = std::mem::take(&mut self.carry); |
| 337 | let mut new: HashMap<u32, Pending> = HashMap::new(); |
| 338 | for ev in evs { |
| 339 | let m = ev.mask; |
| 340 | if m.contains(AddWatchFlags::IN_Q_OVERFLOW) { |
| 341 | red.overflow = true; |
| 342 | continue; |
| 343 | } |
| 344 | if !self.dirs.contains_key(&ev.wd) { |
| 345 | continue; // from a watch already released |
| 346 | } |
| 347 | red.moved = true; |
| 348 | if m.contains(AddWatchFlags::IN_IGNORED) { |
| 349 | self.untrack(ev.wd); |
| 350 | continue; |
| 351 | } |
| 352 | if m.contains(AddWatchFlags::IN_UNMOUNT) { |
| 353 | red.dirty = true; |
| 354 | } |
| 355 | if m.intersects(AddWatchFlags::IN_DELETE_SELF | AddWatchFlags::IN_MOVE_SELF) |
| 356 | && Some(ev.wd) == self.rootwd |
| 357 | { |
| 358 | red.gone = true; |
| 359 | } |
| 360 | let name = match &ev.name { |
| 361 | Some(n) => n.as_os_str().as_bytes(), |
| 362 | None => continue, |
| 363 | }; |
| 364 | let isdir = m.contains(AddWatchFlags::IN_ISDIR); |
| 365 | if m.contains(AddWatchFlags::IN_CREATE) { |
| 366 | if isdir { |
| 367 | red.grafts.push(self.child(ev.wd, name)); |
| 368 | } |
| 369 | } else if m.contains(AddWatchFlags::IN_MOVED_FROM) { |
| 370 | let from = self.child(ev.wd, name); |
| 371 | new.insert(ev.cookie, Pending { from, dir: isdir }); |
| 372 | } else if m.contains(AddWatchFlags::IN_MOVED_TO) { |
| 373 | let to = self.child(ev.wd, name); |
| 374 | match new.remove(&ev.cookie).or_else(|| old.remove(&ev.cookie)) { |
| 375 | Some(p) => { |
| 376 | if isdir { |
| 377 | // Events that follow in this batch name the new paths. |
| 378 | self.relabel(&p.from, &to); |
| 379 | red.dirty = true; |
| 380 | } |
| 381 | red.pairs.push(Renamed { from: p.from, to, dir: isdir }); |
| 382 | }, |
| 383 | None => if isdir { |
| 384 | red.grafts.push(to); |
| 385 | }, |
| 386 | } |
| 387 | } |
| 388 | } |
| 389 | if red.overflow { |
| 390 | red.pairs.clear(); |
| 391 | new.clear(); |
| 392 | } else { |
| 393 | // A directory seen leaving may have left the tree, with its watch. |
| 394 | for p in new.values() { |
| 395 | if p.dir { |
| 396 | red.dirty = true; |
| 397 | } |
| 398 | } |
| 399 | } |
| 400 | self.carry = new; |
| 401 | red |
| 402 | } |
| 403 | |
| 404 | // Walks from each start, placing a watch on every admitted directory found. A |
| 405 | // full walk starts at the root and then releases every watch it did not reach, so |
| 406 | // the maps end as the tree is. A graft only adds. |
| 407 | fn walk<F>( |
| 408 | &mut self, |
| 409 | starts: Vec<Vec<u8>>, |
| 410 | full: bool, |
| 411 | descend: &mut F, |
| 412 | ) |
| 413 | -> Outcome<Walked> |
| 414 | where F: FnMut(&[u8]) -> bool, |
| 415 | { |
| 416 | let mut visited: HashSet<WatchDescriptor> = HashSet::new(); |
| 417 | let mut stack = starts; |
| 418 | while let Some(rel) = stack.pop() { |
| 419 | let is_root = rel.is_empty(); |
| 420 | let abs = if is_root { |
| 421 | self.root.clone() |
| 422 | } else { |
| 423 | self.root.join(OsStr::from_bytes(&rel)) |
| 424 | }; |
| 425 | // The watch goes on before the directory is listed, so an entry made |
| 426 | // after the listing is in the queue, and one made before it is in the list. |
| 427 | let wd = match self.add(&abs, is_root) { |
| 428 | Ok(wd) => wd, |
| 429 | Err(e) => { |
| 430 | if let Some(limit) = add_limit(e) { |
| 431 | return Ok(Walked::Short(limit)); |
| 432 | } |
| 433 | match e { |
| 434 | Errno::ENOENT | Errno::ENOTDIR if is_root => return Ok(Walked::Gone), |
| 435 | // Gone, replaced by a link, or closed to us: not watched. |
| 436 | Errno::ENOENT | Errno::ENOTDIR | Errno::EACCES | Errno::ELOOP |
| 437 | if !is_root => continue, |
| 438 | _ => return Err(err!( |
| 439 | "Cannot watch {:?}: {}.", abs, e; |
| 440 | IO, System)), |
| 441 | } |
| 442 | }, |
| 443 | }; |
| 444 | if is_root { |
| 445 | match self.rootwd { |
| 446 | // A different directory now sits at the root's path. |
| 447 | Some(rw) if rw != wd => return Ok(Walked::Gone), |
| 448 | _ => self.rootwd = Some(wd), |
| 449 | } |
| 450 | } |
| 451 | if !visited.insert(wd) { |
| 452 | continue; // reached twice, as by a bind mount: walked once |
| 453 | } |
| 454 | let fresh = !self.dirs.contains_key(&wd); |
| 455 | let count = if full { visited.len() } else { self.dirs.len() + fresh as usize }; |
| 456 | if count > self.budget { |
| 457 | if fresh { |
| 458 | // Not yet in the map, so release would miss it. |
| 459 | let _ = self.ino.rm_watch(wd); |
| 460 | } |
| 461 | return Ok(Walked::Short(Limit::Budget)); |
| 462 | } |
| 463 | self.track(wd, rel.clone()); |
| 464 | let rd = match fs::read_dir(&abs) { |
| 465 | Ok(rd) => rd, |
| 466 | Err(e) => match e.raw_os_error().map(Errno::from_raw) { |
| 467 | Some(Errno::EMFILE) | Some(Errno::ENFILE) => |
| 468 | return Ok(Walked::Short(Limit::Instances)), |
| 469 | _ => continue, |
| 470 | }, |
| 471 | }; |
| 472 | for ent in rd { |
| 473 | let ent = match ent { |
| 474 | Ok(ent) => ent, |
| 475 | Err(_) => continue, |
| 476 | }; |
| 477 | let is_dir = match ent.file_type() { |
| 478 | Ok(t) => t.is_dir(), // false for a link, whatever it points at |
| 479 | Err(_) => false, |
| 480 | }; |
| 481 | if !is_dir { |
| 482 | continue; |
| 483 | } |
| 484 | let child = join(&rel, ent.file_name().as_bytes()); |
| 485 | if descend(&child) { |
| 486 | stack.push(child); |
| 487 | } |
| 488 | } |
| 489 | } |
| 490 | if full { |
| 491 | let stale: Vec<WatchDescriptor> = self.dirs.keys() |
| 492 | .filter(|wd| !visited.contains(*wd)) |
| 493 | .copied() |
| 494 | .collect(); |
| 495 | for wd in stale { |
| 496 | // An error means the kernel dropped it already. |
| 497 | let _ = self.ino.rm_watch(wd); |
| 498 | self.untrack(wd); |
| 499 | } |
| 500 | } |
| 501 | Ok(Walked::Held) |
| 502 | } |
| 503 | |
| 504 | fn add(&self, abs: &Path, is_root: bool) -> Result<WatchDescriptor, Errno> { |
| 505 | if let Some(e) = injected() { |
| 506 | return Err(e); |
| 507 | } |
| 508 | // The root is followed if it is a link, since its owner named it. Beneath it a |
| 509 | // link is never followed, even if one replaces a directory after it was listed. |
| 510 | let mut flags = mask() | AddWatchFlags::IN_ONLYDIR; |
| 511 | if !is_root { |
| 512 | flags |= AddWatchFlags::IN_DONT_FOLLOW; |
| 513 | } |
| 514 | self.ino.add_watch(abs, flags) |
| 515 | } |
| 516 | |
| 517 | fn track(&mut self, wd: WatchDescriptor, path: Vec<u8>) { |
| 518 | if let Some(old) = self.dirs.get(&wd) { |
| 519 | if old.path != path && self.paths.get(&old.path) == Some(&wd) { |
| 520 | self.paths.remove(&old.path); |
| 521 | } |
| 522 | } |
| 523 | self.paths.insert(path.clone(), wd); |
| 524 | self.dirs.insert(wd, Dir { path }); |
| 525 | } |
| 526 | |
| 527 | fn untrack(&mut self, wd: WatchDescriptor) { |
| 528 | if let Some(dir) = self.dirs.remove(&wd) { |
| 529 | if self.paths.get(&dir.path) == Some(&wd) { |
| 530 | self.paths.remove(&dir.path); |
| 531 | } |
| 532 | } |
| 533 | } |
| 534 | |
| 535 | // Rewrites the paths of a directory and of everything beneath it. |
| 536 | fn relabel(&mut self, from: &[u8], to: &[u8]) { |
| 537 | let mut moves = Vec::new(); |
| 538 | for (wd, dir) in &self.dirs { |
| 539 | let rest = if dir.path == from { |
| 540 | Some(&dir.path[from.len()..]) |
| 541 | } else if dir.path.len() > from.len() |
| 542 | && dir.path.starts_with(from) |
| 543 | && dir.path[from.len()] == b'/' |
| 544 | { |
| 545 | Some(&dir.path[from.len()..]) |
| 546 | } else { |
| 547 | None |
| 548 | }; |
| 549 | if let Some(rest) = rest { |
| 550 | let mut path = to.to_vec(); |
| 551 | path.extend_from_slice(rest); |
| 552 | moves.push((*wd, path)); |
| 553 | } |
| 554 | } |
| 555 | for (wd, _) in &moves { |
| 556 | if let Some(dir) = self.dirs.get(wd) { |
| 557 | if self.paths.get(&dir.path) == Some(wd) { |
| 558 | self.paths.remove(&dir.path); |
| 559 | } |
| 560 | } |
| 561 | } |
| 562 | for (wd, path) in moves { |
| 563 | self.paths.insert(path.clone(), wd); |
| 564 | self.dirs.insert(wd, Dir { path }); |
| 565 | } |
| 566 | } |
| 567 | |
| 568 | fn child(&self, wd: WatchDescriptor, name: &[u8]) -> Vec<u8> { |
| 569 | match self.dirs.get(&wd) { |
| 570 | Some(dir) => join(&dir.path, name), |
| 571 | None => name.to_vec(), |
| 572 | } |
| 573 | } |
| 574 | |
| 575 | // Releases every watch this tree holds. The kernel may have dropped some already. |
| 576 | fn release(&mut self) { |
| 577 | let wds: Vec<WatchDescriptor> = self.dirs.keys().copied().collect(); |
| 578 | for wd in wds { |
| 579 | let _ = self.ino.rm_watch(wd); |
| 580 | } |
| 581 | self.dirs.clear(); |
| 582 | self.paths.clear(); |
| 583 | self.carry.clear(); |
| 584 | } |
| 585 | |
| 586 | fn finish(&mut self, done: Done) -> Wake { |
| 587 | self.release(); |
| 588 | let wake = match &done { |
| 589 | Done::Gone => Wake::RootGone, |
| 590 | Done::Short(limit) => Wake::Exhausted(*limit), |
| 591 | }; |
| 592 | self.done = Some(done); |
| 593 | wake |
| 594 | } |
| 595 | } |
| 596 | |
| 597 | fn mask() -> AddWatchFlags { |
| 598 | AddWatchFlags::IN_CREATE |
| 599 | | AddWatchFlags::IN_DELETE |
| 600 | | AddWatchFlags::IN_MODIFY |
| 601 | | AddWatchFlags::IN_CLOSE_WRITE |
| 602 | | AddWatchFlags::IN_ATTRIB |
| 603 | | AddWatchFlags::IN_MOVED_FROM |
| 604 | | AddWatchFlags::IN_MOVED_TO |
| 605 | | AddWatchFlags::IN_DELETE_SELF |
| 606 | | AddWatchFlags::IN_MOVE_SELF |
| 607 | } |
| 608 | |
| 609 | fn join(dir: &[u8], name: &[u8]) -> Vec<u8> { |
| 610 | let mut path = Vec::with_capacity(dir.len() + 1 + name.len()); |
| 611 | path.extend_from_slice(dir); |
| 612 | if !dir.is_empty() { |
| 613 | path.push(b'/'); |
| 614 | } |
| 615 | path.extend_from_slice(name); |
| 616 | path |
| 617 | } |
| 618 | |
| 619 | // What a failed `inotify_add_watch` says about the machine, if anything. ENOMEM is the |
| 620 | // same call failing for want of kernel memory, which a fall back to polling also cures. |
| 621 | fn add_limit(e: Errno) -> Option<Limit> { |
| 622 | match e { |
| 623 | Errno::ENOSPC | Errno::ENOMEM => Some(Limit::Watches), |
| 624 | _ => None, |
| 625 | } |
| 626 | } |
| 627 | |
| 628 | fn init_limit(e: Errno) -> Option<Limit> { |
| 629 | match e { |
| 630 | Errno::EMFILE | Errno::ENFILE | Errno::ENOMEM => Some(Limit::Instances), |
| 631 | _ => None, |
| 632 | } |
| 633 | } |
| 634 | |
| 635 | #[cfg(test)] |
| 636 | fn injected() -> Option<Errno> { |
| 637 | tests::inject_next() |
| 638 | } |
| 639 | |
| 640 | #[cfg(not(test))] |
| 641 | fn injected() -> Option<Errno> { |
| 642 | None |
| 643 | } |
| 644 | |
| 645 | #[cfg(test)] |
| 646 | fn drain_reads() -> usize { |
| 647 | tests::drain_cap().unwrap_or(DRAIN_READS) |
| 648 | } |
| 649 | |
| 650 | #[cfg(not(test))] |
| 651 | fn drain_reads() -> usize { |
| 652 | DRAIN_READS |
| 653 | } |
| 654 | |
| 655 | #[cfg(test)] |
| 656 | mod tests { |
| 657 | use super::*; |
| 658 | |
| 659 | use std::{ |
| 660 | cell::Cell, |
| 661 | fs::File, |
| 662 | process::Command, |
| 663 | }; |
| 664 | |
| 665 | thread_local! { |
| 666 | // Fails the nth `inotify_add_watch` from now with the given errno. |
| 667 | static INJECT: Cell<Option<(usize, Errno)>> = const { Cell::new(None) }; |
| 668 | static CAP: Cell<Option<usize>> = const { Cell::new(None) }; |
| 669 | } |
| 670 | |
| 671 | pub(super) fn inject_next() -> Option<Errno> { |
| 672 | match INJECT.get() { |
| 673 | Some((1, e)) => { INJECT.set(None); Some(e) }, |
| 674 | Some((n, e)) => { INJECT.set(Some((n - 1, e))); None }, |
| 675 | None => None, |
| 676 | } |
| 677 | } |
| 678 | |
| 679 | pub(super) fn drain_cap() -> Option<usize> { |
| 680 | CAP.get() |
| 681 | } |
| 682 | |
| 683 | // The kernel's own count of the watches on an inotify descriptor. |
| 684 | fn held(fd: RawFd) -> Outcome<usize> { |
| 685 | let body = res!(fs::read_to_string(format!("/proc/self/fdinfo/{}", fd)) |
| 686 | .map_err(|e| err!("Cannot read the fdinfo of {}: {}.", fd, e; IO, File, Read))); |
| 687 | Ok(body.lines().filter(|l| l.starts_with("inotify wd:")).count()) |
| 688 | } |
| 689 | |
| 690 | struct Scratch(PathBuf); |
| 691 | |
| 692 | impl Scratch { |
| 693 | fn new(tag: &str) -> Outcome<Self> { |
| 694 | let dir = std::env::temp_dir().join(format!("fe2o3-sys-ino-{}-{}", tag, std::process::id())); |
| 695 | let _ = fs::remove_dir_all(&dir); |
| 696 | res!(fs::create_dir_all(&dir) |
| 697 | .map_err(|e| err!("Cannot make {:?}: {}.", dir, e; IO, File, Create))); |
| 698 | Ok(Self(dir)) |
| 699 | } |
| 700 | } |
| 701 | |
| 702 | impl Drop for Scratch { |
| 703 | fn drop(&mut self) { |
| 704 | let _ = fs::remove_dir_all(&self.0); |
| 705 | } |
| 706 | } |
| 707 | |
| 708 | fn build(root: &Path, dirs: &[&str]) -> Outcome<()> { |
| 709 | for d in dirs { |
| 710 | res!(fs::create_dir_all(root.join(d)) |
| 711 | .map_err(|e| err!("Cannot make {}: {}.", d, e; IO, File, Create))); |
| 712 | } |
| 713 | Ok(()) |
| 714 | } |
| 715 | |
| 716 | #[test] |
| 717 | fn errno_mapping_names_the_limit() { |
| 718 | assert_eq!(add_limit(Errno::ENOSPC), Some(Limit::Watches)); |
| 719 | assert_eq!(add_limit(Errno::ENOMEM), Some(Limit::Watches)); |
| 720 | assert_eq!(add_limit(Errno::ENOENT), None); |
| 721 | assert_eq!(add_limit(Errno::EACCES), None); |
| 722 | assert_eq!(init_limit(Errno::EMFILE), Some(Limit::Instances)); |
| 723 | assert_eq!(init_limit(Errno::ENFILE), Some(Limit::Instances)); |
| 724 | assert_eq!(init_limit(Errno::ENOSPC), None); |
| 725 | } |
| 726 | |
| 727 | // ENOSPC cannot be produced for real without taking watches from Syncthing and the |
| 728 | // desktop, so the call is made to fail at the point the kernel would fail it. |
| 729 | #[test] |
| 730 | fn enospc_during_open_releases_the_tree() -> Outcome<()> { |
| 731 | let s = res!(Scratch::new("enospc-open")); |
| 732 | res!(build(&s.0, &["a/b", "c", "d/e", "f"])); |
| 733 | INJECT.set(Some((4, Errno::ENOSPC))); |
| 734 | let opened = res!(Watch::open(&s.0, 100, |_| true)); |
| 735 | INJECT.set(None); |
| 736 | match opened { |
| 737 | Opened::Exhausted(Limit::Watches) => (), |
| 738 | other => return Err(err!("Expected Exhausted(Watches), got {:?}.", other; Test)), |
| 739 | } |
| 740 | Ok(()) |
| 741 | } |
| 742 | |
| 743 | #[test] |
| 744 | fn enospc_during_wait_releases_every_watch() -> Outcome<()> { |
| 745 | let s = res!(Scratch::new("enospc-wait")); |
| 746 | res!(build(&s.0, &["a/b", "c"])); |
| 747 | let mut w = match res!(Watch::open(&s.0, 100, |_| true)) { |
| 748 | Opened::Watching(w) => w, |
| 749 | other => return Err(err!("Expected a watch, got {:?}.", other; Test)), |
| 750 | }; |
| 751 | assert_eq!(res!(held(w.as_raw_fd())), 4); |
| 752 | res!(build(&s.0, &["x/y", "z"])); |
| 753 | INJECT.set(Some((2, Errno::ENOSPC))); |
| 754 | let wake = res!(w.wait(Duration::from_secs(2), |_| true)); |
| 755 | INJECT.set(None); |
| 756 | assert_eq!(wake, Wake::Exhausted(Limit::Watches)); |
| 757 | assert_eq!(res!(held(w.as_raw_fd())), 0); |
| 758 | assert_eq!(w.watches(), 0); |
| 759 | // The answer repeats, and nothing is placed again behind the caller's back. |
| 760 | let again = res!(w.wait(Duration::from_millis(50), |_| true)); |
| 761 | assert_eq!(again, Wake::Exhausted(Limit::Watches)); |
| 762 | assert_eq!(res!(held(w.as_raw_fd())), 0); |
| 763 | Ok(()) |
| 764 | } |
| 765 | |
| 766 | // A rename arrives as two events. With one read per wait, 127 events ahead of it |
| 767 | // put the first of the pair last in the first 4,096-byte read and the second first |
| 768 | // in the next, so the pair straddles two waits as the carry exists to handle. |
| 769 | #[test] |
| 770 | fn a_rename_split_across_reads_is_paired_by_the_carry() -> Outcome<()> { |
| 771 | let s = res!(Scratch::new("carry")); |
| 772 | let mut w = match res!(Watch::open(&s.0, 100, |_| true)) { |
| 773 | Opened::Watching(w) => w, |
| 774 | other => return Err(err!("Expected a watch, got {:?}.", other; Test)), |
| 775 | }; |
| 776 | res!(File::create(s.0.join("fx")).map_err(|e| err!("{}", e; IO, File, Create))); |
| 777 | for i in 0..62 { |
| 778 | res!(File::create(s.0.join(format!("f{:02}", i))) |
| 779 | .map_err(|e| err!("{}", e; IO, File, Create))); |
| 780 | } |
| 781 | // Each file made is two events, CREATE and CLOSE_WRITE: 126, plus one ATTRIB. |
| 782 | let chmod = Command::new("chmod").arg("600").arg(s.0.join("fx")).status(); |
| 783 | assert!(chmod.map(|st| st.success()).unwrap_or(false)); |
| 784 | let mv = Command::new("mv").arg(s.0.join("fx")).arg(s.0.join("fy")).status(); |
| 785 | assert!(mv.map(|st| st.success()).unwrap_or(false)); |
| 786 | CAP.set(Some(1)); |
| 787 | let first = res!(w.wait(Duration::from_secs(2), |_| true)); |
| 788 | let second = res!(w.wait(Duration::from_secs(2), |_| true)); |
| 789 | CAP.set(None); |
| 790 | assert_eq!(first, Wake::Moved(Vec::new())); |
| 791 | assert_eq!(second, Wake::Moved(vec![Renamed { |
| 792 | from: b"fx".to_vec(), |
| 793 | to: b"fy".to_vec(), |
| 794 | dir: false, |
| 795 | }])); |
| 796 | Ok(()) |
| 797 | } |
| 798 | } |