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

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