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

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