From bac6533d5cc5f5fa05d7bdfdbdeaa29a55488bc8 Mon Sep 17 00:00:00 2001
From: Jack Nagel <jacknagel@gmail.com>
Date: Sat, 27 Jul 2013 23:53:53 -0500
Subject: [PATCH] Set close-on-exec on lock file descriptors

The formula locks used by the installer and commands like link and
unlink are backed by open files and flock(). The open file descriptors
are thus leaked to any subprocesses. This can result in weird behavior
in programs spawned from formula that do not expect to inherit these
descriptors.

Fix this by setting close-on-exec on the lock file descriptors.

Fixes Homebrew/homebrew#21486.
---
 Library/Homebrew/formula_lock.rb | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)

diff --git a/Library/Homebrew/formula_lock.rb b/Library/Homebrew/formula_lock.rb
index 3b3807fd85..0eeb27c84e 100644
--- a/Library/Homebrew/formula_lock.rb
+++ b/Library/Homebrew/formula_lock.rb
@@ -1,3 +1,5 @@
+require 'fcntl'
+
 class FormulaLock
   LOCKDIR = HOMEBREW_CACHE_FORMULA
 
@@ -33,7 +35,9 @@ class FormulaLock
 
   def get_or_create_lockfile
     if @lockfile.nil? || @lockfile.closed?
-      @path.open(File::RDWR | File::CREAT)
+      @lockfile = @path.open(File::RDWR | File::CREAT)
+      @lockfile.fcntl(Fcntl::F_SETFD, Fcntl::FD_CLOEXEC)
+      @lockfile
     else
       @lockfile
     end
-- 
GitLab