2022-10-11 00:52:32 +01:00
|
|
|
# typed: true
|
2019-04-19 15:38:03 +09:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2015-04-13 18:02:46 +08:00
|
|
|
require "fcntl"
|
|
|
|
require "socket"
|
|
|
|
|
|
|
|
module Utils
|
2018-08-17 22:42:37 -04:00
|
|
|
def self.rewrite_child_error(child_error)
|
2020-05-12 10:26:09 +01:00
|
|
|
error = if child_error.inner["cmd"] &&
|
|
|
|
child_error.inner_class == ErrorDuringExecution
|
2018-08-17 22:42:37 -04:00
|
|
|
ErrorDuringExecution.new(child_error.inner["cmd"],
|
|
|
|
status: child_error.inner["status"],
|
|
|
|
output: child_error.inner["output"])
|
2020-05-12 10:26:09 +01:00
|
|
|
elsif child_error.inner["cmd"] &&
|
|
|
|
child_error.inner_class == BuildError
|
2018-08-17 22:42:37 -04:00
|
|
|
# We fill `BuildError#formula` and `BuildError#options` in later,
|
|
|
|
# when we rescue this in `FormulaInstaller#build`.
|
|
|
|
BuildError.new(nil, child_error.inner["cmd"],
|
|
|
|
child_error.inner["args"], child_error.inner["env"])
|
2018-09-06 23:48:07 -04:00
|
|
|
elsif child_error.inner_class == Interrupt
|
|
|
|
Interrupt.new
|
2018-08-17 22:42:37 -04:00
|
|
|
else
|
|
|
|
# Everything other error in the child just becomes a RuntimeError.
|
|
|
|
RuntimeError.new(child_error.message)
|
|
|
|
end
|
|
|
|
|
|
|
|
error.set_backtrace child_error.backtrace
|
|
|
|
|
|
|
|
error
|
|
|
|
end
|
|
|
|
|
2021-02-02 11:53:51 +00:00
|
|
|
def self.safe_fork
|
2024-07-14 08:49:39 -04:00
|
|
|
require "json/add/exception"
|
|
|
|
|
2015-04-24 22:13:45 -04:00
|
|
|
Dir.mktmpdir("homebrew", HOMEBREW_TEMP) do |tmpdir|
|
|
|
|
UNIXServer.open("#{tmpdir}/socket") do |server|
|
|
|
|
read, write = IO.pipe
|
2015-04-13 18:02:46 +08:00
|
|
|
|
2015-04-24 22:13:45 -04:00
|
|
|
pid = fork do
|
2021-02-02 11:53:51 +00:00
|
|
|
# bootsnap doesn't like these forked processes
|
|
|
|
ENV["HOMEBREW_NO_BOOTSNAP"] = "1"
|
2024-04-13 00:23:42 -04:00
|
|
|
error_pipe = server.path
|
|
|
|
ENV["HOMEBREW_ERROR_PIPE"] = error_pipe
|
2019-10-13 10:03:26 +01:00
|
|
|
server.close
|
|
|
|
read.close
|
|
|
|
write.fcntl(Fcntl::F_SETFD, Fcntl::FD_CLOEXEC)
|
2024-03-21 03:25:49 +00:00
|
|
|
|
|
|
|
Process::UID.change_privilege(Process.euid) if Process.euid != Process.uid
|
|
|
|
|
2024-04-13 00:23:42 -04:00
|
|
|
yield(error_pipe)
|
2019-10-13 10:03:26 +01:00
|
|
|
rescue Exception => e # rubocop:disable Lint/RescueException
|
|
|
|
error_hash = JSON.parse e.to_json
|
|
|
|
|
|
|
|
# Special case: We need to recreate ErrorDuringExecutions
|
|
|
|
# for proper error messages and because other code expects
|
|
|
|
# to rescue them further down.
|
|
|
|
if e.is_a?(ErrorDuringExecution)
|
|
|
|
error_hash["cmd"] = e.cmd
|
2021-02-23 21:30:36 +00:00
|
|
|
error_hash["status"] = if e.status.is_a?(Process::Status)
|
|
|
|
{
|
|
|
|
exitstatus: e.status.exitstatus,
|
|
|
|
termsig: e.status.termsig,
|
|
|
|
}
|
|
|
|
else
|
|
|
|
e.status
|
|
|
|
end
|
2019-10-13 10:03:26 +01:00
|
|
|
error_hash["output"] = e.output
|
|
|
|
end
|
2018-08-17 22:42:37 -04:00
|
|
|
|
2019-10-13 10:03:26 +01:00
|
|
|
write.puts error_hash.to_json
|
|
|
|
write.close
|
2018-09-06 23:48:07 -04:00
|
|
|
|
2019-10-13 10:03:26 +01:00
|
|
|
exit!
|
2021-02-02 11:53:51 +00:00
|
|
|
else
|
2019-10-14 09:03:02 +01:00
|
|
|
exit!(true)
|
2015-04-24 22:13:45 -04:00
|
|
|
end
|
2015-04-13 18:02:46 +08:00
|
|
|
|
2024-07-14 13:18:33 -04:00
|
|
|
ignore_interrupts(quiet: true) do # the child will receive the interrupt and marshal it back
|
2015-04-24 22:13:45 -04:00
|
|
|
begin
|
|
|
|
socket = server.accept_nonblock
|
|
|
|
rescue Errno::EAGAIN, Errno::EWOULDBLOCK, Errno::ECONNABORTED, Errno::EPROTO, Errno::EINTR
|
2020-11-23 18:15:48 +01:00
|
|
|
retry unless Process.waitpid(T.must(pid), Process::WNOHANG)
|
2015-04-24 22:13:45 -04:00
|
|
|
else
|
|
|
|
socket.send_io(write)
|
2015-05-12 21:52:30 -04:00
|
|
|
socket.close
|
2015-04-24 22:13:45 -04:00
|
|
|
end
|
|
|
|
write.close
|
2018-09-06 23:48:07 -04:00
|
|
|
data = read.read
|
2015-04-24 22:13:45 -04:00
|
|
|
read.close
|
2020-11-23 18:15:48 +01:00
|
|
|
Process.wait(T.must(pid)) unless socket.nil?
|
2018-08-17 22:42:37 -04:00
|
|
|
|
2018-09-06 23:48:07 -04:00
|
|
|
# 130 is the exit status for a process interrupted via Ctrl-C.
|
|
|
|
# We handle it here because of the possibility of an interrupted process terminating
|
|
|
|
# without writing its Interrupt exception to the error pipe.
|
|
|
|
raise Interrupt if $CHILD_STATUS.exitstatus == 130
|
|
|
|
|
2020-12-01 17:04:59 +00:00
|
|
|
if data.present?
|
2020-11-23 18:15:48 +01:00
|
|
|
error_hash = JSON.parse(T.must(data.lines.first))
|
2018-08-17 22:42:37 -04:00
|
|
|
|
|
|
|
e = ChildProcessError.new(error_hash)
|
|
|
|
|
|
|
|
raise rewrite_child_error(e)
|
|
|
|
end
|
|
|
|
|
2017-08-25 12:35:03 -07:00
|
|
|
raise "Forked child process failed: #{$CHILD_STATUS}" unless $CHILD_STATUS.success?
|
2015-04-24 22:13:45 -04:00
|
|
|
end
|
2015-04-13 18:02:46 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|