Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 46 additions & 18 deletions src/uu/tail/src/follow/watch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,35 +24,52 @@ use uucore::show_error;
pub struct WatcherRx {
watcher: Box<dyn Watcher>,
receiver: Receiver<Result<notify::Event, notify::Error>>,
/// Whether `watcher` is a `notify::PollWatcher`.
#[cfg_attr(not(target_os = "linux"), allow(dead_code))]
polling: bool,
}

impl WatcherRx {
fn new(
watcher: Box<dyn Watcher>,
receiver: Receiver<Result<notify::Event, notify::Error>>,
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.
fn watch_with_parent(&mut self, path: &Path) -> UResult<()> {
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() {
Expand All @@ -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(())
}
Expand Down Expand Up @@ -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(())
Expand Down Expand Up @@ -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(())
Expand Down Expand Up @@ -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,
Expand Down
80 changes: 80 additions & 0 deletions tests/by-util/test_tail.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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<u64> {
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();
}