diff --git a/NEWS b/NEWS index d1cb92c8b..02bb0d05c 100644 --- a/NEWS +++ b/NEWS @@ -129,7 +129,12 @@ GNU coreutils NEWS -*- outline -*- and now reverts to polling in that case. Previously tail could fail to notice new data, or that a file followed by name was replaced, if events arrived faster than tail could process them. - [bug introduced in coreutils-7.5] + [bug introduced with inotify support added in coreutils-7.5] + + 'tail -F' no longer fails with "error reading inotify event" when a separate + file with a longer name is created, removed, or renamed in the directory + of a followed file. + [bug introduced with inotify support added in coreutils-7.5] 'timeout --signal=ALRM -k' again honors the -k option. Similarly 'timeout --signal=CHLD' no longer reports "No child processes", diff --git a/src/tail.c b/src/tail.c index 945e129a4..32d4d26e0 100644 --- a/src/tail.c +++ b/src/tail.c @@ -1534,9 +1534,6 @@ tail_forever_inotify (int wd, struct File_spec *f, int n_files, bool found_unwatchable_dir = false; bool no_inotify_resources = false; struct File_spec *prev_fspec; - idx_t evlen = 0; - char *evbuf; - idx_t evbuf_off = 0; *parent_by_namep = NULL; @@ -1569,10 +1566,6 @@ tail_forever_inotify (int wd, struct File_spec *f, int n_files, { if (!f[i].ignore) { - idx_t fnlen = strlen (f[i].name); - if (evlen < fnlen) - evlen = fnlen; - f[i].wd = -1; if (follow_mode == Follow_name) @@ -1680,8 +1673,13 @@ tail_forever_inotify (int wd, struct File_spec *f, int n_files, if (debug) error (0, 0, "%s", _("using notification mode")); - evlen += sizeof (struct inotify_event) + 1; - evbuf = ximalloc (evlen); + idx_t evbuf_off = 0; + + /* NAME_MAX covers that max length of all file systems + that might be mounted in the parent dir, and the define + should be available any where that inotify is. */ + idx_t evlen = sizeof (struct inotify_event) + NAME_MAX + 1; + char *evbuf = ximalloc (evlen); /* Wait for inotify events and handle them. Events on directories ensure that watched files can be re-added when following by name. diff --git a/tests/local.mk b/tests/local.mk index 8f206b88d..a8662acb5 100644 --- a/tests/local.mk +++ b/tests/local.mk @@ -207,6 +207,7 @@ all_tests = \ tests/tail/inotify-rotate.sh \ tests/tail/inotify-rotate-resources.sh \ tests/tail/inotify-dir-recreate.sh \ + tests/tail/inotify-long-name.sh \ tests/tail/inotify-only-regular.sh \ tests/tail/inotify-overflow.sh \ tests/tail/quote-headers.sh \ diff --git a/tests/tail/inotify-long-name.sh b/tests/tail/inotify-long-name.sh new file mode 100755 index 000000000..3f8cef7b6 --- /dev/null +++ b/tests/tail/inotify-long-name.sh @@ -0,0 +1,61 @@ +#!/bin/sh +# Check that the inotify event buffers are allocated with a large enough size. + +# Copyright (C) 2026 Free Software Foundation, Inc. + +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. + +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. + +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . + +. "${srcdir=.}/tests/init.sh"; path_prepend_ ./src +print_ver_ tail +require_inotify_supported_ + +check_tail_output() +{ + local delay="$1" + grep "$tail_re" out > /dev/null || + { sleep $delay; return 1; } +} + +# Make sure we can create a long file name. +long_name=$(printf '%0255d' | tr '0' 'a') || framework_failure_ +touch "$long_name" || skip_ 'cannot create long file name' +rm -f "$long_name" || framework_failure_ + +# Create an empty file to tail. +touch file || framework_failure_ + +cleanup_() { kill "$pid" 2>/dev/null; wait "$pid" 2>/dev/null; pid=; } + +# Now tail it and make sure we are using notification mode. +timeout 60 tail --debug -F file >out 2>&1 & pid=$! +tail_re='tail: using notification mode' retry_delay_ check_tail_output .1 7 || + { cat out; skip_ 'tail did not switch to notification mode'; } + +# Create a long file name and then write to the file being tailed. +# coreutils-9.12 and earlier only allocated enough memory to follow +# events for its operands. Creating a file name longer than that would +# cause it to fail and exit. +touch "$long_name" && +echo abc >file || framework_failure_ +tail_re='^abc$' retry_delay_ check_tail_output .1 7 || { cat out; fail=1; } + +echo 1 >"$long_name" && +echo def >>file || framework_failure_ +tail_re='^def$' retry_delay_ check_tail_output .1 7 || { cat out; fail=1; } + +mv "$long_name" new && +echo ghi >>file || framework_failure_ +tail_re='^ghi$' retry_delay_ check_tail_output .1 7 || { cat out; fail=1; } + +Exit $fail