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

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