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

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