Blame SOURCES/coreutils-8.32-tail-use-poll.patch

04161d
From c7a04cef4075da864a3468e63a5bb79334d8f556 Mon Sep 17 00:00:00 2001
04161d
From: Paul Eggert <eggert@cs.ucla.edu>
04161d
Date: Sat, 26 Jun 2021 18:23:52 -0700
04161d
Subject: [PATCH] tail: use poll, not select
04161d
MIME-Version: 1.0
04161d
Content-Type: text/plain; charset=UTF-8
04161d
Content-Transfer-Encoding: 8bit
04161d
04161d
This fixes an unlikely stack out-of-bounds write reported by
04161d
Stepan Broz via Kamil Dudka (Bug#49209).
04161d
* src/tail.c: Do not include <sys/select.h>.
04161d
[!_AIX]: Include poll.h.
04161d
(check_output_alive) [!_AIX]: Use poll instead of select.
04161d
(tail_forever_inotify): Likewise.  Simplify logic, as there is no
04161d
need for a ‘while (len <= evbuf_off)’ loop.
04161d
04161d
Upstream-commit: da0d448bca62c6305fc432f67e2c5ccc2da75346
04161d
Signed-off-by: Kamil Dudka <kdudka@redhat.com>
04161d
---
04161d
 src/tail.c | 100 +++++++++++++++++++++--------------------------------
04161d
 1 file changed, 39 insertions(+), 61 deletions(-)
04161d
04161d
diff --git a/src/tail.c b/src/tail.c
04161d
index 1c88723..5b4f21a 100644
04161d
--- a/src/tail.c
04161d
+++ b/src/tail.c
04161d
@@ -28,12 +28,9 @@
04161d
 #include <stdio.h>
04161d
 #include <assert.h>
04161d
 #include <getopt.h>
04161d
-#include <sys/select.h>
04161d
+#include <poll.h>
04161d
 #include <sys/types.h>
04161d
 #include <signal.h>
04161d
-#ifdef _AIX
04161d
-# include <poll.h>
04161d
-#endif
04161d
 
04161d
 #include "system.h"
04161d
 #include "argmatch.h"
04161d
@@ -351,27 +348,12 @@ check_output_alive (void)
04161d
   if (! monitor_output)
04161d
     return;
04161d
 
04161d
-#ifdef _AIX
04161d
-  /* select on AIX was seen to give a readable event immediately.  */
04161d
   struct pollfd pfd;
04161d
   pfd.fd = STDOUT_FILENO;
04161d
   pfd.events = POLLERR;
04161d
 
04161d
   if (poll (&pfd, 1, 0) >= 0 && (pfd.revents & POLLERR))
04161d
     die_pipe ();
04161d
-#else
04161d
-  struct timeval delay;
04161d
-  delay.tv_sec = delay.tv_usec = 0;
04161d
-
04161d
-  fd_set rfd;
04161d
-  FD_ZERO (&rfd;;
04161d
-  FD_SET (STDOUT_FILENO, &rfd;;
04161d
-
04161d
-  /* readable event on STDOUT is equivalent to POLLERR,
04161d
-     and implies an error condition on output like broken pipe.  */
04161d
-  if (select (STDOUT_FILENO + 1, &rfd, NULL, NULL, &delay) == 1)
04161d
-    die_pipe ();
04161d
-#endif
04161d
 }
04161d
 
04161d
 static bool
04161d
@@ -1612,7 +1594,7 @@ tail_forever_inotify (int wd, struct File_spec *f, size_t n_files,
04161d
   /* Wait for inotify events and handle them.  Events on directories
04161d
      ensure that watched files can be re-added when following by name.
04161d
      This loop blocks on the 'safe_read' call until a new event is notified.
04161d
-     But when --pid=P is specified, tail usually waits via the select.  */
04161d
+     But when --pid=P is specified, tail usually waits via poll.  */
04161d
   while (1)
04161d
     {
04161d
       struct File_spec *fspec;
04161d
@@ -1629,54 +1611,51 @@ tail_forever_inotify (int wd, struct File_spec *f, size_t n_files,
04161d
           return false;
04161d
         }
04161d
 
04161d
-      /* When watching a PID, ensure that a read from WD will not block
04161d
-         indefinitely.  */
04161d
-      while (len <= evbuf_off)
04161d
+      if (len <= evbuf_off)
04161d
         {
04161d
-          struct timeval delay; /* how long to wait for file changes.  */
04161d
+          /* Poll for inotify events.  When watching a PID, ensure
04161d
+             that a read from WD will not block indefinitely.
04161d
+             If MONITOR_OUTPUT, also poll for a broken output pipe.  */
04161d
 
04161d
-          if (pid)
04161d
+          int file_change;
04161d
+          struct pollfd pfd[2];
04161d
+          do
04161d
             {
04161d
-              if (writer_is_dead)
04161d
-                exit (EXIT_SUCCESS);
04161d
+              /* How many ms to wait for changes.  -1 means wait forever.  */
04161d
+              int delay = -1;
04161d
 
04161d
-              writer_is_dead = (kill (pid, 0) != 0 && errno != EPERM);
04161d
-
04161d
-              if (writer_is_dead)
04161d
-                delay.tv_sec = delay.tv_usec = 0;
04161d
-              else
04161d
+              if (pid)
04161d
                 {
04161d
-                  delay.tv_sec = (time_t) sleep_interval;
04161d
-                  delay.tv_usec = 1000000 * (sleep_interval - delay.tv_sec);
04161d
+                  if (writer_is_dead)
04161d
+                    exit (EXIT_SUCCESS);
04161d
+
04161d
+                  writer_is_dead = (kill (pid, 0) != 0 && errno != EPERM);
04161d
+
04161d
+                  if (writer_is_dead || sleep_interval <= 0)
04161d
+                    delay = 0;
04161d
+                  else if (sleep_interval < INT_MAX / 1000 - 1)
04161d
+                    {
04161d
+                      /* delay = ceil (sleep_interval * 1000), sans libm.  */
04161d
+                      double ddelay = sleep_interval * 1000;
04161d
+                      delay = ddelay;
04161d
+                      delay += delay < ddelay;
04161d
+                    }
04161d
                 }
04161d
+
04161d
+              pfd[0].fd = wd;
04161d
+              pfd[0].events = POLLIN;
04161d
+              pfd[1].fd = STDOUT_FILENO;
04161d
+              pfd[1].events = pfd[1].revents = 0;
04161d
+              file_change = poll (pfd, monitor_output + 1, delay);
04161d
             }
04161d
+          while (file_change == 0);
04161d
 
04161d
-           fd_set rfd;
04161d
-           FD_ZERO (&rfd;;
04161d
-           FD_SET (wd, &rfd;;
04161d
-           if (monitor_output)
04161d
-             FD_SET (STDOUT_FILENO, &rfd;;
04161d
-
04161d
-           int file_change = select (MAX (wd, STDOUT_FILENO) + 1,
04161d
-                                     &rfd, NULL, NULL, pid ? &delay: NULL);
04161d
-
04161d
-           if (file_change == 0)
04161d
-             continue;
04161d
-           else if (file_change == -1)
04161d
-             die (EXIT_FAILURE, errno,
04161d
-                  _("error waiting for inotify and output events"));
04161d
-           else if (FD_ISSET (STDOUT_FILENO, &rfd))
04161d
-             {
04161d
-               /* readable event on STDOUT is equivalent to POLLERR,
04161d
-                  and implies an error on output like broken pipe.  */
04161d
-               die_pipe ();
04161d
-             }
04161d
-           else
04161d
-             break;
04161d
-        }
04161d
+          if (file_change < 0)
04161d
+            die (EXIT_FAILURE, errno,
04161d
+                 _("error waiting for inotify and output events"));
04161d
+          if (pfd[1].revents)
04161d
+            die_pipe ();
04161d
 
04161d
-      if (len <= evbuf_off)
04161d
-        {
04161d
           len = safe_read (wd, evbuf, evlen);
04161d
           evbuf_off = 0;
04161d
 
04161d
@@ -2437,8 +2416,7 @@ main (int argc, char **argv)
04161d
   if (forever && ignore_fifo_and_pipe (F, n_files))
04161d
     {
04161d
       /* If stdout is a fifo or pipe, then monitor it
04161d
-         so that we exit if the reader goes away.
04161d
-         Note select() on a regular file is always readable.  */
04161d
+         so that we exit if the reader goes away.  */
04161d
       struct stat out_stat;
04161d
       if (fstat (STDOUT_FILENO, &out_stat) < 0)
04161d
         die (EXIT_FAILURE, errno, _("standard output"));
04161d
-- 
04161d
2.31.1
04161d