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

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