Skip to content

tail: watch the followed file itself under inotify - #16693

Open
lmilz wants to merge 1 commit into
uutils:mainfrom
lmilz:tail-watch-followed-file
Open

lmilz wants to merge 1 commit into
uutils:mainfrom
lmilz:tail-watch-followed-file

Conversation

@lmilz

@lmilz lmilz commented Nov 18, 2026 •

Copy link
Copy Markdown
Contributor

When following a regular file on Linux, tail previously only placed an inotify watch on the parent directory (watching the file itself only for symlinks), whereas GNU tail also watches the file and drops that watch once the file is rotated.

Under inotify (leaving the polling backend unchanged), tail now also watches the followed file across rotations (ignoring errors when adding the file watch, e.g. on unreadable files, since the directory watch still covers them). New tests verify the watched inodes via /proc/<pid>/fdinfo and that tail -F keeps running on unreadable files. The GNU CI run also reports tests/tail/follow-name and tests/tail/pid-pipe as newly passing.

Note: tests/tail/inotify-rotate-resources.sh still fails: notify runs its inotify syscalls in a background thread, and the test runs strace without -f, so it only traces the main thread. See the comment below.

Part of #16650

Comment thread src/uu/tail/src/follow/watch.rs Outdated
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))]

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please avoid the allow(dead_code), #[cfg(target_os = "linux")] on the field should do it

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done, thanks. This needed a few more changes to keep it compiling on FreeBSD and macOS: WatcherRx::new is removed, and Oberserver::start now builds the structure directly.

@github-actions

Copy link
Copy Markdown

Binary size comparison:

Individual binary size comparison VS main (threshold: >=5% AND >=4 KB).

Total size of compared binaries: 155.48 MB (+100 KB, +0.06%)

Significant per-binary changes:
  env     1.20 MB ->    1.28 MB  (+80 KB, +6.51%)

@github-actions

github-actions Bot commented Nov 19, 2026 •

Copy link
Copy Markdown

GNU testsuite comparison:

GNU test failed: tests/tail/wait. tests/tail/wait is passing on 'main'. Maybe you have to rebase?
Congrats! The gnu test tests/tail/follow-name is no longer failing!
Congrats! The gnu test tests/tail/pid-pipe is no longer failing!

@sylvestre
sylvestre force-pushed the tail-watch-followed-file branch from ed24981 to 5b650d3 Compare November 19, 2026 11:44
@lmilz
lmilz force-pushed the tail-watch-followed-file branch from 5b650d3 to 19b2902 Compare November 19, 2026 16:23
@lmilz
lmilz requested a review from sylvestre November 19, 2026 16:32
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.

A file that cannot be watched, such as an unreadable one, is still followed through its directory.

Part of uutils#16650
@lmilz

lmilz commented Nov 20, 2026

Copy link
Copy Markdown
Contributor Author

With this change, the inotify watches on the followed file are now added and removed across rotations as expected (verified with strace -f and via /proc/<pid>/fdinfo in the new Rust test).

However, tests/tail/inotify-rotate-resources.sh still fails with an empty trace. I looked at how the test invokes strace from the outside (/proc/<pid>/cmdline, without reading the script): it runs strace -e inotify_add_watch,inotify_rm_watch -o strace.out tail -F ..., without -f. With that exact invocation, GNU tail shows 4 inotify calls and ours shows none, because notify performs its inotify syscalls in its own thread ("notify-rs inotify loop"). With -f, the same add/rm calls as GNU show up.

How would you prefer to proceed: adjusting the GNU test to use strace -f, or a different approach?

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants