From ed24981eaf5016149706b9176e405c9a7eb82512 Mon Sep 17 00:00:00 2001 From: Lars Milz Date: Tue, 17 Nov 2026 21:40:05 +0100 Subject: [PATCH] tail: watch the followed file itself under inotify With -F, tail only put an inotify watch on the directory of a followed file, while GNU also watches the file and drops that watch once the file is rotated away. The output was the same, but the GNU test tests/tail/inotify-rotate-resources.sh checks for these watches. Watch the file as well, and watch the new file again when it is reopened after a rotation. notify drops the old watch by itself once the name is renamed or removed. As both watches now report the name going away, report a missing file only once. The old watch is dropped before the old file is closed: closing a replaced file can free its inode, and the watch would then report the name as deleted. The polling backend keeps watching only the directory, since it reorders events when it watches both. Fixes #16650 --- src/uu/tail/src/follow/watch.rs | 64 ++++++++++++++++++-------- tests/by-util/test_tail.rs | 80 +++++++++++++++++++++++++++++++++ 2 files changed, 126 insertions(+), 18 deletions(-) diff --git a/src/uu/tail/src/follow/watch.rs b/src/uu/tail/src/follow/watch.rs index c57d954f891..53b9d8e3249 100644 --- a/src/uu/tail/src/follow/watch.rs +++ b/src/uu/tail/src/follow/watch.rs @@ -24,14 +24,31 @@ use uucore::show_error; pub struct WatcherRx { watcher: Box, receiver: Receiver>, + /// Whether `watcher` is a `notify::PollWatcher`. + #[cfg_attr(not(target_os = "linux"), allow(dead_code))] + polling: bool, } impl WatcherRx { fn new( watcher: Box, receiver: Receiver>, + polling: bool, ) -> Self { - Self { watcher, receiver } + Self { + watcher, + receiver, + polling, + } + } + + /// Whether a followed file is watched itself, not only through its directory. + /// With inotify every file is watched, as GNU does. With polling only symlinks + /// are, since the polling backend reorders events when it watches both a file + /// and its directory. + #[cfg(target_os = "linux")] + fn watches_file(&self, path: &Path) -> bool { + path.is_symlink() || !self.polling } /// Wrapper for `notify::Watcher::watch` to also add the parent directory of `path` if necessary. @@ -39,20 +56,20 @@ impl WatcherRx { let mut path = path.to_owned(); #[cfg(target_os = "linux")] if path.is_file() { - // The directory watch reports a write to a symlink's target under - // the target's name, not the link's, so also watch the link: it is - // another inode than the directory and nothing is reported twice. - if path.is_symlink() { + // Watch the file itself as well as its directory, as GNU does. + // The file watch also reports writes to a symlink's target under + // the link's name, which the directory watch does not. + if self.watches_file(&path) { self.watch(&path, RecursiveMode::NonRecursive)?; } /* - NOTE: Using the parent directory instead of the file is a workaround. - This workaround follows the recommendation of the notify crate authors: + NOTE: The parent directory is watched as well, following the recommendation of + the notify crate authors: > On some platforms, if the `path` is renamed or removed while being watched, behavior may > be unexpected. See discussions in [#165] and [#166]. If less surprising behavior is wanted > one may non-recursively watch the _parent_ directory as well and manage related events. - NOTE: Adding both: file and parent results in duplicate/wrong events. - Tested for notify::InotifyWatcher and for notify::PollWatcher. + NOTE: Both watches report a rename or removal of the file, so such events can + arrive twice for the same path. */ if let Some(parent) = path.parent() { if parent.is_dir() { @@ -71,7 +88,6 @@ impl WatcherRx { path = path.canonicalize()?; } - // for syscalls: 2x "inotify_add_watch" ("filename" and ".") and 1x "inotify_rm_watch" self.watch(&path, RecursiveMode::NonRecursive)?; Ok(()) } @@ -239,7 +255,7 @@ impl Observer { } } - self.watcher_rx = Some(WatcherRx::new(watcher, rx)); + self.watcher_rx = Some(WatcherRx::new(watcher, rx, self.use_polling)); self.init_files(&settings.inputs)?; Ok(()) @@ -298,15 +314,25 @@ impl Observer { } /// Open `path` again and, on Linux, watch the file it names now: a - /// watch on a symlink follows the link when it is added, so one added - /// before the link was changed still reports the old target. + /// watch is bound to the inode it was added for, so one added before + /// the file was replaced (or a symlink changed) still reports the old + /// file. The old watch is dropped before the old file is closed: + /// closing it may free a replaced inode, and its watch would then report + /// the name as deleted. fn reopen(&mut self, path: &Path) -> UResult<()> { - self.files.update_reader(path)?; #[cfg(target_os = "linux")] - if path.is_symlink() - && let Some(watcher_rx) = &mut self.watcher_rx - { + let watch_file = path.is_file() + && self + .watcher_rx + .as_ref() + .is_some_and(|watcher_rx| watcher_rx.watches_file(path)); + #[cfg(target_os = "linux")] + if watch_file && let Some(watcher_rx) = &mut self.watcher_rx { let _ = watcher_rx.unwatch(path); + } + self.files.update_reader(path)?; + #[cfg(target_os = "linux")] + if watch_file && let Some(watcher_rx) = &mut self.watcher_rx { watcher_rx.watch(path, RecursiveMode::NonRecursive)?; } Ok(()) @@ -461,7 +487,9 @@ impl Observer { // the link until it resolves again. self.orphans.push(event_path.clone()); } - } else { + } else if self.files.get(event_path).reader.is_some() { + // Both the directory and the file watch report the name going away; say so + // only once. show_error!( "{}: {}", display_name, diff --git a/tests/by-util/test_tail.rs b/tests/by-util/test_tail.rs index ca264360f33..e79c66d672f 100644 --- a/tests/by-util/test_tail.rs +++ b/tests/by-util/test_tail.rs @@ -7,6 +7,7 @@ // spell-checker:ignore (libs) kqueue ELOOP EISDIR FSIZE setrlimit SIGXFSZ // spell-checker:ignore (jargon) tailable untailable datasame runneradmin tmpi // spell-checker:ignore (cmd) taskkill +// spell-checker:ignore (proc) fdinfo #![allow( clippy::unicode_not_nfc, @@ -5953,3 +5954,82 @@ fn test_invalid_max_unchanged_stats_is_rejected() { .stderr_contains(value); } } + +/// Inodes that the inotify instance of process `pid` currently watches. +#[cfg(target_os = "linux")] +fn inotify_watched_inodes(pid: u32) -> Vec { + let mut inodes = vec![]; + let mut found = false; + for entry in std::fs::read_dir(format!("/proc/{pid}/fd")).unwrap() { + let entry = entry.unwrap(); + let Ok(target) = std::fs::read_link(entry.path()) else { + continue; + }; + + if target.as_os_str() != "anon_inode:inotify" { + continue; + } + found = true; + + let fdinfo = std::fs::read_to_string(format!( + "/proc/{pid}/fdinfo/{}", + entry.file_name().to_string_lossy() + )) + .unwrap(); + for line in fdinfo.lines().filter(|l| l.starts_with("inotify")) { + let ino = line + .split_whitespace() + .find_map(|field| field.strip_prefix("ino:")) + .unwrap(); + inodes.push(u64::from_str_radix(ino, 16).unwrap()); + } + } + + assert!(found, "tail has no inotify instance"); + + inodes +} + +#[test] +#[cfg(target_os = "linux")] // inotify and `/proc` only exist on Linux. +fn test_follow_name_watches_file_across_rotation() { + use std::os::unix::fs::MetadataExt; + + let ts = TestScenario::new(util_name!()); + let at = &ts.fixtures; + let delay = 500; // milliseconds + + at.touch("app.log"); + let mut old_ino = at.metadata("app.log").ino(); + + let mut p = ts.ucmd().args(&["-F", "-s.1", "app.log"]).run_no_wait(); + p.make_assertion_with_delay(delay).is_alive(); + + assert!( + inotify_watched_inodes(p.id()).contains(&old_ino), + "the followed file itself is not watched" + ); + + for i in 1..=3 { + at.rename("app.log", format!("app.log.{i}")); + p.delay(delay); + + assert!( + !inotify_watched_inodes(p.id()).contains(&old_ino), + "the watch on the rotated file was not removed" + ); + + at.write("app.log", &format!("line{i}\n")); + p.delay(delay); + let new_ino = at.metadata("app.log").ino(); + + assert!( + inotify_watched_inodes(p.id()).contains(&new_ino), + "the new file is not watched" + ); + old_ino = new_ino; + } + + p.make_assertion().is_alive(); + p.kill(); +}