diff options
| author | Jack Nagel | 2015-04-24 22:13:45 -0400 |
|---|---|---|
| committer | Jack Nagel | 2015-04-25 23:17:13 -0400 |
| commit | 9c928a431b5945ed8c9e82e257119a71e54ca667 (patch) | |
| tree | 0898e4912298610dc910ebc04652b73496e7e112 | |
| parent | be74724ceb0b29fb75d322be50773eb2e30b0c10 (diff) | |
| download | homebrew-9c928a431b5945ed8c9e82e257119a71e54ca667.tar.bz2 | |
Clean up socket and filesystem resources separately
| -rw-r--r-- | Library/Homebrew/utils/fork.rb | 66 |
1 files changed, 33 insertions, 33 deletions
diff --git a/Library/Homebrew/utils/fork.rb b/Library/Homebrew/utils/fork.rb index defe1fa79..5504a576a 100644 --- a/Library/Homebrew/utils/fork.rb +++ b/Library/Homebrew/utils/fork.rb @@ -3,42 +3,42 @@ require "socket" module Utils def self.safe_fork(&block) - socket_path = "#{Dir.mktmpdir("homebrew", HOMEBREW_TEMP)}/socket" - server = UNIXServer.new(socket_path) - ENV["HOMEBREW_ERROR_PIPE"] = socket_path - read, write = IO.pipe + Dir.mktmpdir("homebrew", HOMEBREW_TEMP) do |tmpdir| + UNIXServer.open("#{tmpdir}/socket") do |server| + read, write = IO.pipe - pid = fork do - begin - server.close - read.close - write.fcntl(Fcntl::F_SETFD, Fcntl::FD_CLOEXEC) - yield - rescue Exception => e - Marshal.dump(e, write) - write.close - exit! 1 - end - end + pid = fork do + ENV["HOMEBREW_ERROR_PIPE"] = server.path + + begin + server.close + read.close + write.fcntl(Fcntl::F_SETFD, Fcntl::FD_CLOEXEC) + yield + rescue Exception => e + Marshal.dump(e, write) + write.close + exit! 1 + end + end - ignore_interrupts(:quietly) do # the child will receive the interrupt and marshal it back - begin - socket = server.accept_nonblock - rescue Errno::EAGAIN, Errno::EWOULDBLOCK, Errno::ECONNABORTED, Errno::EPROTO, Errno::EINTR - retry unless Process.waitpid(pid, Process::WNOHANG) - else - socket.send_io(write) + ignore_interrupts(:quietly) do # the child will receive the interrupt and marshal it back + begin + socket = server.accept_nonblock + rescue Errno::EAGAIN, Errno::EWOULDBLOCK, Errno::ECONNABORTED, Errno::EPROTO, Errno::EINTR + retry unless Process.waitpid(pid, Process::WNOHANG) + else + socket.send_io(write) + end + write.close + data = read.read + read.close + Process.wait(pid) unless socket.nil? + raise Marshal.load(data) unless data.nil? or data.empty? + raise Interrupt if $?.exitstatus == 130 + raise "Suspicious failure" unless $?.success? + end end - write.close - data = read.read - read.close - Process.wait(pid) unless socket.nil? - raise Marshal.load(data) unless data.nil? or data.empty? - raise Interrupt if $?.exitstatus == 130 - raise "Suspicious failure" unless $?.success? end - ensure - server.close - FileUtils.rm_r File.dirname(socket_path) end end |
