Module: Utils::Disclaim Private

Defined in:
utils/disclaim.rb

Overview

This module is part of a private API. This module may only be used in the Homebrew/brew repository. Third parties should avoid using this module if possible, as it may be removed or changed without warning.

Disclaim the parent's TCC responsibility for macOS sandbox workers where supported.

Constant Summary collapse

POSIX_SPAWN_SETEXEC =

This constant is part of a private API. This constant may only be used in the Homebrew/brew repository. Third parties should avoid using this constant if possible, as it may be removed or changed without warning.

0x0040

Class Method Summary collapse

Class Method Details

.exec(args) ⇒ void

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

This method returns an undefined value.

Replaces this process, preserving its PID, environment and inherited descriptors.

Parameters:

Raises:

  • (SystemCallError)


14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'utils/disclaim.rb', line 14

def self.exec(args)
  args.each do |arg|
    raise ArgumentError, "Argument contains null byte" if arg.include?("\0")
  end

  # Private libSystem API, also used by LLDB and Chromium.
  disclaim = begin
    Fiddle::Function.new(Fiddle::Handle::DEFAULT["responsibility_spawnattrs_setdisclaim"],
                         [Fiddle::TYPE_VOIDP, Fiddle::TYPE_INT], Fiddle::TYPE_INT)
  rescue Fiddle::DLError
    nil
  end
  return exec_without_disclaim(args) if disclaim.nil?

  functions = {
    "posix_spawnattr_init"     => [Fiddle::TYPE_VOIDP],
    "posix_spawnattr_setflags" => [Fiddle::TYPE_VOIDP, Fiddle::TYPE_SHORT],
    "posix_spawn"              => [Fiddle::TYPE_VOIDP] * 6,
    "posix_spawnattr_destroy"  => [Fiddle::TYPE_VOIDP],
  }.to_h do |name, types|
    [name, Fiddle::Function.new(Fiddle::Handle::DEFAULT[name], types, Fiddle::TYPE_INT)]
  end

  attributes = "\0" * Fiddle::SIZEOF_VOIDP
  error = functions.fetch("posix_spawnattr_init").call(attributes)
  raise SystemCallError.new("posix_spawnattr_init", error) unless error.zero?

  begin
    # POSIX_SPAWN_SETEXEC preserves Homebrew's signal and timeout handling.
    error = functions.fetch("posix_spawnattr_setflags").call(attributes, POSIX_SPAWN_SETEXEC)
    raise SystemCallError.new("posix_spawnattr_setflags", error) unless error.zero?

    return exec_without_disclaim(args) unless disclaim.call(attributes, 1).zero?

    error = functions.fetch("posix_spawn").call(
      nil, args.fetch(0), nil, attributes,
      [*args, nil].pack("p*"), [*ENV.map { |key, value| "#{key}=#{value}" }, nil].pack("p*")
    )
    # POSIX_SPAWN_SETEXEC means posix_spawn only returns on failure.
    raise SystemCallError.new("posix_spawn", error)
  ensure
    functions.fetch("posix_spawnattr_destroy").call(attributes)
  end
end