Skip to content

Commit dffd011

Browse files
keesJonathan Corbet
authored andcommitted
docs, parallelism: Do not leak blocking mode to other readers
Setting non-blocking via a local copy of the jobserver file descriptor is safer than just assuming other reader processes with the same fd open are prepared for it to be non-blocking. Suggested-by: Rasmus Villemoes <[email protected]> Link: https://lore.kernel.org/lkml/[email protected] Signed-off-by: Kees Cook <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Jonathan Corbet <[email protected]>
1 parent 4920323 commit dffd011

File tree

1 file changed

+6
-8
lines changed

1 file changed

+6
-8
lines changed

scripts/jobserver-count

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,6 @@ default="1"
1212
if len(sys.argv) > 1:
1313
default=sys.argv[1]
1414

15-
# Set non-blocking for a given file descriptor.
16-
def nonblock(fd):
17-
flags = fcntl.fcntl(fd, fcntl.F_GETFL)
18-
fcntl.fcntl(fd, fcntl.F_SETFL, flags | os.O_NONBLOCK)
19-
return fd
20-
2115
# Extract and prepare jobserver file descriptors from envirnoment.
2216
try:
2317
# Fetch the make environment options.
@@ -31,8 +25,12 @@ try:
3125
# Parse out R,W file descriptor numbers and set them nonblocking.
3226
fds = opts[0].split("=", 1)[1]
3327
reader, writer = [int(x) for x in fds.split(",", 1)]
34-
reader = nonblock(reader)
35-
except (KeyError, IndexError, ValueError, IOError):
28+
# Open a private copy of reader to avoid setting nonblocking
29+
# on an unexpecting process with the same reader fd.
30+
reader = os.open("/proc/self/fd/%d" % (reader),
31+
os.O_RDONLY | os.O_NONBLOCK)
32+
except (KeyError, IndexError, ValueError, IOError, OSError) as e:
33+
print(e, file=sys.stderr)
3634
# Any missing environment strings or bad fds should result in just
3735
# using the default specified parallelism.
3836
print(default)

0 commit comments

Comments
 (0)