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