Lukáš Zaoral 245be9
From 73d119f4f8052a9fb6cef13cd9e75d5a4e23311a Mon Sep 17 00:00:00 2001
Lukáš Zaoral 245be9
From: dann frazier <dann.frazier@canonical.com>
Lukáš Zaoral 245be9
Date: Wed, 29 Nov 2023 18:32:34 -0700
Lukáš Zaoral 245be9
Subject: [PATCH] tail: fix tailing sysfs files where PAGE_SIZE > BUFSIZ
Lukáš Zaoral 245be9
Lukáš Zaoral 245be9
* src/tail.c (file_lines): Ensure we use a buffer size >= PAGE_SIZE when
Lukáš Zaoral 245be9
searching backwards to avoid seeking within a file,
Lukáš Zaoral 245be9
which on sysfs files is accepted but also returns no data.
Lukáš Zaoral 245be9
* tests/tail/tail-sysfs.sh: Add a new test.
Lukáš Zaoral 245be9
* tests/local.mk: Reference the new test.
Lukáš Zaoral 245be9
* NEWS: Mention the bug fix.
Lukáš Zaoral 245be9
Fixes https://bugs.gnu.org/67490
Lukáš Zaoral 245be9
Lukáš Zaoral 245be9
Upstream-commit: 73d119f4f8052a9fb6cef13cd9e75d5a4e23311a
Lukáš Zaoral 245be9
Cherry-picked-by: Lukáš Zaoral <lzaoral@redhat.com>
Lukáš Zaoral 245be9
---
Lukáš Zaoral 245be9
 src/tail.c               | 57 +++++++++++++++++++++++++++++-----------
Lukáš Zaoral 245be9
 tests/local.mk           |  1 +
Lukáš Zaoral 245be9
 tests/tail/tail-sysfs.sh | 34 ++++++++++++++++++++++++
Lukáš Zaoral 245be9
 3 files changed, 77 insertions(+), 15 deletions(-)
Lukáš Zaoral 245be9
 create mode 100755 tests/tail/tail-sysfs.sh
Lukáš Zaoral 245be9
Lukáš Zaoral 245be9
diff --git a/src/tail.c b/src/tail.c
Lukáš Zaoral 245be9
index c45f3b65a..6979e0ba3 100644
Lukáš Zaoral 245be9
--- a/src/tail.c
Lukáš Zaoral 245be9
+++ b/src/tail.c
Lukáš Zaoral 245be9
@@ -208,6 +208,9 @@ static int nbpids = 0;
Lukáš Zaoral 245be9
    that is writing to all followed files.  */
Lukáš Zaoral 245be9
 static pid_t pid;
Lukáš Zaoral 245be9
 
Lukáš Zaoral 245be9
+/* Used to determine the buffer size when scanning backwards in a file.  */
Lukáš Zaoral 245be9
+static idx_t page_size;
Lukáš Zaoral 245be9
+
Lukáš Zaoral 245be9
 /* True if we have ever read standard input.  */
Lukáš Zaoral 245be9
 static bool have_read_stdin;
Lukáš Zaoral 245be9
 
Lukáš Zaoral 245be9
@@ -515,22 +518,40 @@ xlseek (int fd, off_t offset, int whence, char const *filename)
Lukáš Zaoral 245be9
    Return true if successful.  */
Lukáš Zaoral 245be9
 
Lukáš Zaoral 245be9
 static bool
Lukáš Zaoral 245be9
-file_lines (char const *pretty_filename, int fd, uintmax_t n_lines,
Lukáš Zaoral 245be9
-            off_t start_pos, off_t end_pos, uintmax_t *read_pos)
Lukáš Zaoral 245be9
+file_lines (char const *pretty_filename, int fd, struct stat const *sb,
Lukáš Zaoral 245be9
+            uintmax_t n_lines, off_t start_pos, off_t end_pos,
Lukáš Zaoral 245be9
+            uintmax_t *read_pos)
Lukáš Zaoral 245be9
 {
Lukáš Zaoral 245be9
-  char buffer[BUFSIZ];
Lukáš Zaoral 245be9
+  char *buffer;
Lukáš Zaoral 245be9
   size_t bytes_read;
Lukáš Zaoral 245be9
+  blksize_t bufsize = BUFSIZ;
Lukáš Zaoral 245be9
   off_t pos = end_pos;
Lukáš Zaoral 245be9
+  bool ok = true;
Lukáš Zaoral 245be9
 
Lukáš Zaoral 245be9
   if (n_lines == 0)
Lukáš Zaoral 245be9
     return true;
Lukáš Zaoral 245be9
 
Lukáš Zaoral 245be9
+  /* Be careful with files with sizes that are a multiple of the page size,
Lukáš Zaoral 245be9
+     as on /proc or /sys file systems these files accept seeking to within
Lukáš Zaoral 245be9
+     the file, but then return no data when read.  So use a buffer that's
Lukáš Zaoral 245be9
+     at least PAGE_SIZE to avoid seeking within such files.
Lukáš Zaoral 245be9
+
Lukáš Zaoral 245be9
+     We could also indirectly use a large enough buffer through io_blksize()
Lukáš Zaoral 245be9
+     however this would be less efficient in the common case, as it would
Lukáš Zaoral 245be9
+     generally pick a larger buffer size, resulting in reading more data
Lukáš Zaoral 245be9
+     from the end of the file.  */
Lukáš Zaoral 245be9
+  affirm (S_ISREG (sb->st_mode));
Lukáš Zaoral 245be9
+  if (sb->st_size % page_size == 0)
Lukáš Zaoral 245be9
+    bufsize = MAX (BUFSIZ, page_size);
Lukáš Zaoral 245be9
+
Lukáš Zaoral 245be9
+  buffer = xmalloc (bufsize);
Lukáš Zaoral 245be9
+
Lukáš Zaoral 245be9
   /* Set 'bytes_read' to the size of the last, probably partial, buffer;
Lukáš Zaoral 245be9
-     0 < 'bytes_read' <= 'BUFSIZ'.  */
Lukáš Zaoral 245be9
-  bytes_read = (pos - start_pos) % BUFSIZ;
Lukáš Zaoral 245be9
+     0 < 'bytes_read' <= 'bufsize'.  */
Lukáš Zaoral 245be9
+  bytes_read = (pos - start_pos) % bufsize;
Lukáš Zaoral 245be9
   if (bytes_read == 0)
Lukáš Zaoral 245be9
-    bytes_read = BUFSIZ;
Lukáš Zaoral 245be9
-  /* Make 'pos' a multiple of 'BUFSIZ' (0 if the file is short), so that all
Lukáš Zaoral 245be9
+    bytes_read = bufsize;
Lukáš Zaoral 245be9
+  /* Make 'pos' a multiple of 'bufsize' (0 if the file is short), so that all
Lukáš Zaoral 245be9
      reads will be on block boundaries, which might increase efficiency.  */
Lukáš Zaoral 245be9
   pos -= bytes_read;
Lukáš Zaoral 245be9
   xlseek (fd, pos, SEEK_SET, pretty_filename);
Lukáš Zaoral 245be9
@@ -538,7 +559,8 @@ file_lines (char const *pretty_filename, int fd, uintmax_t n_lines,
Lukáš Zaoral 245be9
   if (bytes_read == SAFE_READ_ERROR)
Lukáš Zaoral 245be9
     {
Lukáš Zaoral 245be9
       error (0, errno, _("error reading %s"), quoteaf (pretty_filename));
Lukáš Zaoral 245be9
-      return false;
Lukáš Zaoral 245be9
+      ok = false;
Lukáš Zaoral 245be9
+      goto free_buffer;
Lukáš Zaoral 245be9
     }
Lukáš Zaoral 245be9
   *read_pos = pos + bytes_read;
Lukáš Zaoral 245be9
 
Lukáš Zaoral 245be9
@@ -565,7 +587,7 @@ file_lines (char const *pretty_filename, int fd, uintmax_t n_lines,
Lukáš Zaoral 245be9
               xwrite_stdout (nl + 1, bytes_read - (n + 1));
Lukáš Zaoral 245be9
               *read_pos += dump_remainder (false, pretty_filename, fd,
Lukáš Zaoral 245be9
                                            end_pos - (pos + bytes_read));
Lukáš Zaoral 245be9
-              return true;
Lukáš Zaoral 245be9
+              goto free_buffer;
Lukáš Zaoral 245be9
             }
Lukáš Zaoral 245be9
         }
Lukáš Zaoral 245be9
 
Lukáš Zaoral 245be9
@@ -577,23 +599,26 @@ file_lines (char const *pretty_filename, int fd, uintmax_t n_lines,
Lukáš Zaoral 245be9
           xlseek (fd, start_pos, SEEK_SET, pretty_filename);
Lukáš Zaoral 245be9
           *read_pos = start_pos + dump_remainder (false, pretty_filename, fd,
Lukáš Zaoral 245be9
                                                   end_pos);
Lukáš Zaoral 245be9
-          return true;
Lukáš Zaoral 245be9
+          goto free_buffer;
Lukáš Zaoral 245be9
         }
Lukáš Zaoral 245be9
-      pos -= BUFSIZ;
Lukáš Zaoral 245be9
+      pos -= bufsize;
Lukáš Zaoral 245be9
       xlseek (fd, pos, SEEK_SET, pretty_filename);
Lukáš Zaoral 245be9
 
Lukáš Zaoral 245be9
-      bytes_read = safe_read (fd, buffer, BUFSIZ);
Lukáš Zaoral 245be9
+      bytes_read = safe_read (fd, buffer, bufsize);
Lukáš Zaoral 245be9
       if (bytes_read == SAFE_READ_ERROR)
Lukáš Zaoral 245be9
         {
Lukáš Zaoral 245be9
           error (0, errno, _("error reading %s"), quoteaf (pretty_filename));
Lukáš Zaoral 245be9
-          return false;
Lukáš Zaoral 245be9
+          ok = false;
Lukáš Zaoral 245be9
+          goto free_buffer;
Lukáš Zaoral 245be9
         }
Lukáš Zaoral 245be9
 
Lukáš Zaoral 245be9
       *read_pos = pos + bytes_read;
Lukáš Zaoral 245be9
     }
Lukáš Zaoral 245be9
   while (bytes_read > 0);
Lukáš Zaoral 245be9
 
Lukáš Zaoral 245be9
-  return true;
Lukáš Zaoral 245be9
+free_buffer:
Lukáš Zaoral 245be9
+  free (buffer);
Lukáš Zaoral 245be9
+  return ok;
Lukáš Zaoral 245be9
 }
Lukáš Zaoral 245be9
 
Lukáš Zaoral 245be9
 /* Print the last N_LINES lines from the end of the standard input,
Lukáš Zaoral 245be9
@@ -1915,7 +1940,7 @@ tail_lines (char const *pretty_filename, int fd, uintmax_t n_lines,
Lukáš Zaoral 245be9
         {
Lukáš Zaoral 245be9
           *read_pos = end_pos;
Lukáš Zaoral 245be9
           if (end_pos != 0
Lukáš Zaoral 245be9
-              && ! file_lines (pretty_filename, fd, n_lines,
Lukáš Zaoral 245be9
+              && ! file_lines (pretty_filename, fd, &stats, n_lines,
Lukáš Zaoral 245be9
                                start_pos, end_pos, read_pos))
Lukáš Zaoral 245be9
             return false;
Lukáš Zaoral 245be9
         }
Lukáš Zaoral 245be9
@@ -2337,6 +2362,8 @@ main (int argc, char **argv)
Lukáš Zaoral 245be9
 
Lukáš Zaoral 245be9
   atexit (close_stdout);
Lukáš Zaoral 245be9
 
Lukáš Zaoral 245be9
+  page_size = getpagesize ();
Lukáš Zaoral 245be9
+
Lukáš Zaoral 245be9
   have_read_stdin = false;
Lukáš Zaoral 245be9
 
Lukáš Zaoral 245be9
   count_lines = true;
Lukáš Zaoral 245be9
diff --git a/tests/local.mk b/tests/local.mk
Lukáš Zaoral 245be9
index db4ee7ed8..bf03238c3 100644
Lukáš Zaoral 245be9
--- a/tests/local.mk
Lukáš Zaoral 245be9
+++ b/tests/local.mk
Lukáš Zaoral 245be9
@@ -257,6 +257,7 @@ all_tests =					\
Lukáš Zaoral 245be9
   tests/seq/seq-precision.sh			\
Lukáš Zaoral 245be9
   tests/head/head.pl				\
Lukáš Zaoral 245be9
   tests/head/head-elide-tail.pl			\
Lukáš Zaoral 245be9
+  tests/tail/tail-sysfs.sh			\
Lukáš Zaoral 245be9
   tests/tail/tail-n0f.sh			\
Lukáš Zaoral 245be9
   tests/ls/ls-misc.pl				\
Lukáš Zaoral 245be9
   tests/date/date.pl				\
Lukáš Zaoral 245be9
diff --git a/tests/tail/tail-sysfs.sh b/tests/tail/tail-sysfs.sh
Lukáš Zaoral 245be9
new file mode 100755
Lukáš Zaoral 245be9
index 000000000..00874b3dc
Lukáš Zaoral 245be9
--- /dev/null
Lukáš Zaoral 245be9
+++ b/tests/tail/tail-sysfs.sh
Lukáš Zaoral 245be9
@@ -0,0 +1,34 @@
Lukáš Zaoral 245be9
+#!/bin/sh
Lukáš Zaoral 245be9
+# sysfs files have weird properties that can be influenced by page size
Lukáš Zaoral 245be9
+
Lukáš Zaoral 245be9
+# Copyright 2023 Free Software Foundation, Inc.
Lukáš Zaoral 245be9
+
Lukáš Zaoral 245be9
+# This program is free software: you can redistribute it and/or modify
Lukáš Zaoral 245be9
+# it under the terms of the GNU General Public License as published by
Lukáš Zaoral 245be9
+# the Free Software Foundation, either version 3 of the License, or
Lukáš Zaoral 245be9
+# (at your option) any later version.
Lukáš Zaoral 245be9
+
Lukáš Zaoral 245be9
+# This program is distributed in the hope that it will be useful,
Lukáš Zaoral 245be9
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
Lukáš Zaoral 245be9
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
Lukáš Zaoral 245be9
+# GNU General Public License for more details.
Lukáš Zaoral 245be9
+
Lukáš Zaoral 245be9
+# You should have received a copy of the GNU General Public License
Lukáš Zaoral 245be9
+# along with this program.  If not, see <https://www.gnu.org/licenses/>.
Lukáš Zaoral 245be9
+
Lukáš Zaoral 245be9
+. "${srcdir=.}/tests/init.sh"; path_prepend_ ./src
Lukáš Zaoral 245be9
+print_ver_ tail
Lukáš Zaoral 245be9
+
Lukáš Zaoral 245be9
+file='/sys/kernel/profiling'
Lukáš Zaoral 245be9
+
Lukáš Zaoral 245be9
+test -r "$file" || skip_ "No $file file"
Lukáš Zaoral 245be9
+
Lukáš Zaoral 245be9
+cp -f "$file" exp || framework_failure_
Lukáš Zaoral 245be9
+
Lukáš Zaoral 245be9
+tail -n1 "$file" > out || fail=1
Lukáš Zaoral 245be9
+compare exp out || fail=1
Lukáš Zaoral 245be9
+
Lukáš Zaoral 245be9
+tail -c2 "$file" > out || fail=1
Lukáš Zaoral 245be9
+compare exp out || fail=1
Lukáš Zaoral 245be9
+
Lukáš Zaoral 245be9
+Exit $fail