1 From b552b05251980f693c729e251f93f5225b400714 Mon Sep 17 00:00:00 2001
2 From: Paul Smith <psmith@gnu.org>
3 Date: Sat, 3 Jun 2017 16:20:51 -0400
4 Subject: [SV 51159] Use a non-blocking read with pselect to avoid hangs.
6 * posixos.c (set_blocking): Set blocking on a file descriptor.
7 (jobserver_setup): Set non-blocking on the jobserver read side.
8 (jobserver_parse_auth): Ditto.
9 (jobserver_acquire_all): Set blocking to avoid a busy-wait loop.
10 (jobserver_acquire): If the non-blocking read() returns without
11 taking a token then try again.
13 posixos.c | 97 ++++++++++++++++++++++++++++++++++++++++++++++-----------------
14 1 file changed, 71 insertions(+), 26 deletions(-)
16 diff --git posixos.c posixos.c
17 index e642d7f..dbafa51 100644
20 @@ -62,6 +62,24 @@ make_job_rfd (void)
25 +set_blocking (int fd, int blocking)
27 + // If we're not using pselect() don't change the blocking
30 + EINTRLOOP (flags, fcntl (fd, F_GETFL));
34 + flags = blocking ? (flags & ~O_NONBLOCK) : (flags | O_NONBLOCK);
35 + EINTRLOOP (r, fcntl (fd, F_SETFL, flags));
37 + pfatal_with_name ("fcntl(O_NONBLOCK)");
43 jobserver_setup (int slots)
45 @@ -86,6 +104,9 @@ jobserver_setup (int slots)
46 pfatal_with_name (_("init jobserver pipe"));
49 + /* When using pselect() we want the read to be non-blocking. */
50 + set_blocking (job_fds[0], 0);
55 @@ -121,6 +142,9 @@ jobserver_parse_auth (const char *auth)
59 + /* When using pselect() we want the read to be non-blocking. */
60 + set_blocking (job_fds[0], 0);
65 @@ -169,7 +193,10 @@ jobserver_acquire_all (void)
67 unsigned int tokens = 0;
69 - /* Close the write side, so the read() won't hang. */
70 + /* Use blocking reads to wait for all outstanding jobs. */
71 + set_blocking (job_fds[0], 1);
73 + /* Close the write side, so the read() won't hang forever. */
77 @@ -236,18 +263,12 @@ jobserver_pre_acquire (void)
79 jobserver_acquire (int timeout)
84 struct timespec *specp = NULL;
92 - FD_SET (job_fds[0], &readfds);
96 /* Alarm after one second (is this too granular?) */
97 @@ -256,28 +277,52 @@ jobserver_acquire (int timeout)
101 - r = pselect (job_fds[0]+1, &readfds, NULL, NULL, specp, &empty);
106 - /* Better be SIGCHLD. */
107 - if (errno != EINTR)
108 - pfatal_with_name (_("pselect jobs pipe"));
118 + FD_ZERO (&readfds);
119 + FD_SET (job_fds[0], &readfds);
121 - /* The read FD is ready: read it! */
122 - EINTRLOOP (r, read (job_fds[0], &intake, 1));
124 - pfatal_with_name (_("read jobs pipe"));
125 + r = pselect (job_fds[0]+1, &readfds, NULL, NULL, specp, &empty);
130 + /* SIGCHLD will show up as an EINTR. */
134 + /* Someone closed the jobs pipe.
135 + That shouldn't happen but if it does we're done. */
136 + O (fatal, NILF, _("job server shut down"));
138 - /* What does it mean if read() returns 0? It shouldn't happen because only
139 - the master make can reap all the tokens and close the write side...?? */
142 + pfatal_with_name (_("pselect jobs pipe"));
149 + /* The read FD is ready: read it! This is non-blocking. */
150 + EINTRLOOP (r, read (job_fds[0], &intake, 1));
154 + /* Someone sniped our token! Try again. */
155 + if (errno == EAGAIN)
158 + pfatal_with_name (_("read jobs pipe"));
161 + /* read() should never return 0: only the master make can reap all the
162 + tokens and close the write side...?? */