Blame SOURCES/make-4.2.1-nonblocking-reads.patch

595530
From b552b05251980f693c729e251f93f5225b400714 Mon Sep 17 00:00:00 2001
595530
From: Paul Smith <psmith@gnu.org>
595530
Date: Sat, 3 Jun 2017 16:20:51 -0400
595530
Subject: [SV 51159] Use a non-blocking read with pselect to avoid hangs.
595530
595530
* posixos.c (set_blocking): Set blocking on a file descriptor.
595530
(jobserver_setup): Set non-blocking on the jobserver read side.
595530
(jobserver_parse_auth): Ditto.
595530
(jobserver_acquire_all): Set blocking to avoid a busy-wait loop.
595530
(jobserver_acquire): If the non-blocking read() returns without
595530
taking a token then try again.
595530
595530
diff --git a/posixos.c b/posixos.c
595530
index e642d7f..dbafa51 100644
595530
--- a/posixos.c
595530
+++ b/posixos.c
595530
@@ -62,6 +62,24 @@ make_job_rfd (void)
595530
 #endif
595530
 }
595530
 
595530
+static void
595530
+set_blocking (int fd, int blocking)
595530
+{
595530
+  // If we're not using pselect() don't change the blocking
595530
+#ifdef HAVE_PSELECT
595530
+  int flags;
595530
+  EINTRLOOP (flags, fcntl (fd, F_GETFL));
595530
+  if (flags >= 0)
595530
+    {
595530
+      int r;
595530
+      flags = blocking ? (flags & ~O_NONBLOCK) : (flags | O_NONBLOCK);
595530
+      EINTRLOOP (r, fcntl (fd, F_SETFL, flags));
595530
+      if (r < 0)
595530
+        pfatal_with_name ("fcntl(O_NONBLOCK)");
595530
+    }
595530
+#endif
595530
+}
595530
+
595530
 unsigned int
595530
 jobserver_setup (int slots)
595530
 {
595530
@@ -86,6 +104,9 @@ jobserver_setup (int slots)
595530
         pfatal_with_name (_("init jobserver pipe"));
595530
     }
595530
 
595530
+  /* When using pselect() we want the read to be non-blocking.  */
595530
+  set_blocking (job_fds[0], 0);
595530
+
595530
   return 1;
595530
 }
595530
 
595530
@@ -121,6 +142,9 @@ jobserver_parse_auth (const char *auth)
595530
       return 0;
595530
     }
595530
 
595530
+  /* When using pselect() we want the read to be non-blocking.  */
595530
+  set_blocking (job_fds[0], 0);
595530
+
595530
   return 1;
595530
 }
595530
 
595530
@@ -169,7 +193,10 @@ jobserver_acquire_all (void)
595530
 {
595530
   unsigned int tokens = 0;
595530
 
595530
-  /* Close the write side, so the read() won't hang.  */
595530
+  /* Use blocking reads to wait for all outstanding jobs.  */
595530
+  set_blocking (job_fds[0], 1);
595530
+
595530
+  /* Close the write side, so the read() won't hang forever.  */
595530
   close (job_fds[1]);
595530
   job_fds[1] = -1;
595530
 
595530
@@ -236,18 +263,12 @@ jobserver_pre_acquire (void)
595530
 unsigned int
595530
 jobserver_acquire (int timeout)
595530
 {
595530
-  sigset_t empty;
595530
-  fd_set readfds;
595530
   struct timespec spec;
595530
   struct timespec *specp = NULL;
595530
-  int r;
595530
-  char intake;
595530
+  sigset_t empty;
595530
 
595530
   sigemptyset (&empty);
595530
 
595530
-  FD_ZERO (&readfds);
595530
-  FD_SET (job_fds[0], &readfds);
595530
-
595530
   if (timeout)
595530
     {
595530
       /* Alarm after one second (is this too granular?)  */
595530
@@ -256,28 +277,52 @@ jobserver_acquire (int timeout)
595530
       specp = &spe;;
595530
     }
595530
 
595530
-  r = pselect (job_fds[0]+1, &readfds, NULL, NULL, specp, &empty);
595530
-
595530
-  if (r == -1)
595530
+  while (1)
595530
     {
595530
-      /* Better be SIGCHLD.  */
595530
-      if (errno != EINTR)
595530
-        pfatal_with_name (_("pselect jobs pipe"));
595530
-      return 0;
595530
-    }
595530
+      fd_set readfds;
595530
+      int r;
595530
+      char intake;
595530
 
595530
-  if (r == 0)
595530
-    /* Timeout.  */
595530
-    return 0;
595530
+      FD_ZERO (&readfds);
595530
+      FD_SET (job_fds[0], &readfds);
595530
 
595530
-  /* The read FD is ready: read it!  */
595530
-  EINTRLOOP (r, read (job_fds[0], &intake, 1));
595530
-  if (r < 0)
595530
-    pfatal_with_name (_("read jobs pipe"));
595530
+      r = pselect (job_fds[0]+1, &readfds, NULL, NULL, specp, &empty);
595530
+      if (r < 0)
595530
+        switch (errno)
595530
+          {
595530
+          case EINTR:
595530
+            /* SIGCHLD will show up as an EINTR.  */
595530
+            return 0;
595530
+
595530
+          case EBADF:
595530
+            /* Someone closed the jobs pipe.
595530
+               That shouldn't happen but if it does we're done.  */
595530
+              O (fatal, NILF, _("job server shut down"));
595530
 
595530
-  /* What does it mean if read() returns 0?  It shouldn't happen because only
595530
-     the master make can reap all the tokens and close the write side...??  */
595530
-  return r > 0;
595530
+          default:
595530
+            pfatal_with_name (_("pselect jobs pipe"));
595530
+          }
595530
+
595530
+      if (r == 0)
595530
+        /* Timeout.  */
595530
+        return 0;
595530
+
595530
+      /* The read FD is ready: read it!  This is non-blocking.  */
595530
+      EINTRLOOP (r, read (job_fds[0], &intake, 1));
595530
+
595530
+      if (r < 0)
595530
+        {
595530
+          /* Someone sniped our token!  Try again.  */
595530
+          if (errno == EAGAIN)
595530
+            continue;
595530
+
595530
+          pfatal_with_name (_("read jobs pipe"));
595530
+        }
595530
+
595530
+      /* read() should never return 0: only the master make can reap all the
595530
+         tokens and close the write side...??  */
595530
+      return r > 0;
595530
+    }
595530
 }
595530
 
595530
 #else