2012-11-28 21:58:34 -05:00
|
|
|
# -*- coding: us-ascii -*-
|
2015-12-16 00:07:31 -05:00
|
|
|
# frozen_string_literal: false
|
2008-04-23 11:22:13 -04:00
|
|
|
require "open3"
|
|
|
|
require "timeout"
|
2014-07-07 04:42:57 -04:00
|
|
|
require_relative "find_executable"
|
2016-05-01 11:16:17 -04:00
|
|
|
require "rbconfig/sizeof"
|
|
|
|
|
|
|
|
class Integer
|
|
|
|
FIXNUM_MIN = -(1 << (8 * RbConfig::SIZEOF['long'] - 2))
|
|
|
|
FIXNUM_MAX = (1 << (8 * RbConfig::SIZEOF['long'] - 2)) - 1
|
|
|
|
end
|
2008-04-23 11:22:13 -04:00
|
|
|
|
2003-10-04 22:56:42 -04:00
|
|
|
module EnvUtil
|
|
|
|
def rubybin
|
2004-05-19 10:45:49 -04:00
|
|
|
if ruby = ENV["RUBY"]
|
|
|
|
return ruby
|
|
|
|
end
|
2005-12-29 03:05:26 -05:00
|
|
|
ruby = "ruby"
|
2013-12-23 01:52:35 -05:00
|
|
|
exeext = RbConfig::CONFIG["EXEEXT"]
|
|
|
|
rubyexe = (ruby + exeext if exeext and !exeext.empty?)
|
2003-10-13 09:05:24 -04:00
|
|
|
3.times do
|
2006-02-03 04:15:42 -05:00
|
|
|
if File.exist? ruby and File.executable? ruby and !File.directory? ruby
|
|
|
|
return File.expand_path(ruby)
|
|
|
|
end
|
2013-12-23 01:52:35 -05:00
|
|
|
if rubyexe and File.exist? rubyexe and File.executable? rubyexe
|
2008-05-12 08:35:37 -04:00
|
|
|
return File.expand_path(rubyexe)
|
2003-10-13 09:05:24 -04:00
|
|
|
end
|
2005-12-29 03:05:26 -05:00
|
|
|
ruby = File.join("..", ruby)
|
2003-10-13 10:59:25 -04:00
|
|
|
end
|
2010-01-12 02:41:40 -05:00
|
|
|
if defined?(RbConfig.ruby)
|
2009-12-31 10:00:04 -05:00
|
|
|
RbConfig.ruby
|
2010-01-12 02:41:40 -05:00
|
|
|
else
|
2003-10-13 10:59:25 -04:00
|
|
|
"ruby"
|
2003-10-04 22:56:42 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
module_function :rubybin
|
2008-04-23 11:22:13 -04:00
|
|
|
|
|
|
|
LANG_ENVS = %w"LANG LC_ALL LC_CTYPE"
|
2009-11-19 17:47:53 -05:00
|
|
|
|
2015-03-02 01:44:22 -05:00
|
|
|
DEFAULT_SIGNALS = Signal.list
|
|
|
|
DEFAULT_SIGNALS.delete("TERM") if /mswin|mingw/ =~ RUBY_PLATFORM
|
|
|
|
|
2013-06-20 00:08:41 -04:00
|
|
|
def invoke_ruby(args, stdin_data = "", capture_stdout = false, capture_stderr = false,
|
2015-03-02 01:44:34 -05:00
|
|
|
encoding: nil, timeout: 10, reprieve: 1, timeout_error: Timeout::Error,
|
2014-04-10 21:31:00 -04:00
|
|
|
stdout_filter: nil, stderr_filter: nil,
|
2015-03-02 01:44:22 -05:00
|
|
|
signal: :TERM,
|
2014-05-10 08:15:09 -04:00
|
|
|
rubybin: EnvUtil.rubybin,
|
2014-04-10 21:31:00 -04:00
|
|
|
**opt)
|
2010-07-01 08:37:58 -04:00
|
|
|
in_c, in_p = IO.pipe
|
|
|
|
out_p, out_c = IO.pipe if capture_stdout
|
2010-08-07 06:53:13 -04:00
|
|
|
err_p, err_c = IO.pipe if capture_stderr && capture_stderr != :merge_to_stdout
|
2010-07-01 08:37:58 -04:00
|
|
|
opt[:in] = in_c
|
|
|
|
opt[:out] = out_c if capture_stdout
|
2010-08-07 06:53:13 -04:00
|
|
|
opt[:err] = capture_stderr == :merge_to_stdout ? out_c : err_c if capture_stderr
|
2013-06-20 00:08:41 -04:00
|
|
|
if encoding
|
|
|
|
out_p.set_encoding(encoding) if out_p
|
|
|
|
err_p.set_encoding(encoding) if err_p
|
2010-07-01 08:37:58 -04:00
|
|
|
end
|
2008-04-23 11:22:13 -04:00
|
|
|
c = "C"
|
2010-07-01 08:37:58 -04:00
|
|
|
child_env = {}
|
|
|
|
LANG_ENVS.each {|lc| child_env[lc] = c}
|
|
|
|
if Array === args and Hash === args.first
|
|
|
|
child_env.update(args.shift)
|
2008-04-23 11:22:13 -04:00
|
|
|
end
|
2010-06-05 09:47:09 -04:00
|
|
|
args = [args] if args.kind_of?(String)
|
2014-05-10 08:15:09 -04:00
|
|
|
pid = spawn(child_env, rubybin, *args, **opt)
|
2010-07-01 08:37:58 -04:00
|
|
|
in_c.close
|
|
|
|
out_c.close if capture_stdout
|
2010-08-07 06:53:13 -04:00
|
|
|
err_c.close if capture_stderr && capture_stderr != :merge_to_stdout
|
2010-07-01 08:37:58 -04:00
|
|
|
if block_given?
|
2012-01-31 00:27:27 -05:00
|
|
|
return yield in_p, out_p, err_p, pid
|
2010-07-01 08:37:58 -04:00
|
|
|
else
|
2009-11-19 17:47:53 -05:00
|
|
|
th_stdout = Thread.new { out_p.read } if capture_stdout
|
2010-08-07 06:53:13 -04:00
|
|
|
th_stderr = Thread.new { err_p.read } if capture_stderr && capture_stderr != :merge_to_stdout
|
2013-06-17 06:24:48 -04:00
|
|
|
in_p.write stdin_data.to_str unless stdin_data.empty?
|
2010-06-24 20:57:03 -04:00
|
|
|
in_p.close
|
2015-08-11 23:50:59 -04:00
|
|
|
if (!th_stdout || th_stdout.join(timeout)) && (!th_stderr || th_stderr.join(timeout))
|
|
|
|
timeout_error = nil
|
|
|
|
else
|
2015-03-02 01:44:22 -05:00
|
|
|
signals = Array(signal).select do |sig|
|
|
|
|
DEFAULT_SIGNALS[sig.to_s] or
|
2015-03-03 08:26:40 -05:00
|
|
|
DEFAULT_SIGNALS[Signal.signame(sig)] rescue false
|
2015-03-02 01:44:22 -05:00
|
|
|
end
|
2015-08-11 23:50:59 -04:00
|
|
|
signals |= [:ABRT, :KILL]
|
2014-12-11 08:54:02 -05:00
|
|
|
case pgroup = opt[:pgroup]
|
|
|
|
when 0, true
|
|
|
|
pgroup = -pid
|
|
|
|
when nil, false
|
|
|
|
pgroup = pid
|
|
|
|
end
|
2015-03-02 01:44:22 -05:00
|
|
|
while signal = signals.shift
|
|
|
|
begin
|
|
|
|
Process.kill signal, pgroup
|
2015-03-02 02:54:30 -05:00
|
|
|
rescue Errno::EINVAL
|
2015-03-02 01:44:22 -05:00
|
|
|
next
|
|
|
|
rescue Errno::ESRCH
|
|
|
|
break
|
|
|
|
end
|
|
|
|
if signals.empty? or !reprieve
|
2013-06-19 03:47:10 -04:00
|
|
|
Process.wait(pid)
|
2015-03-02 01:44:22 -05:00
|
|
|
else
|
|
|
|
begin
|
|
|
|
Timeout.timeout(reprieve) {Process.wait(pid)}
|
|
|
|
rescue Timeout::Error
|
|
|
|
end
|
2013-06-19 03:47:10 -04:00
|
|
|
end
|
2015-03-02 01:44:22 -05:00
|
|
|
end
|
2015-03-02 01:44:34 -05:00
|
|
|
status = $?
|
2009-11-19 17:47:53 -05:00
|
|
|
end
|
2015-03-02 01:44:34 -05:00
|
|
|
stdout = th_stdout.value if capture_stdout
|
|
|
|
stderr = th_stderr.value if capture_stderr && capture_stderr != :merge_to_stdout
|
2009-11-19 17:47:53 -05:00
|
|
|
out_p.close if capture_stdout
|
2010-08-07 06:53:13 -04:00
|
|
|
err_p.close if capture_stderr && capture_stderr != :merge_to_stdout
|
2015-03-02 01:44:34 -05:00
|
|
|
status ||= Process.wait2(pid)[1]
|
2014-04-10 21:31:00 -04:00
|
|
|
stdout = stdout_filter.call(stdout) if stdout_filter
|
|
|
|
stderr = stderr_filter.call(stderr) if stderr_filter
|
2015-08-11 23:50:59 -04:00
|
|
|
if timeout_error
|
|
|
|
bt = caller_locations
|
|
|
|
msg = "execution of #{bt.shift.label} expired"
|
|
|
|
msg = Test::Unit::Assertions::FailDesc[status, msg, [stdout, stderr].join("\n")].()
|
|
|
|
raise timeout_error, msg, bt.map(&:to_s)
|
|
|
|
end
|
2010-07-01 08:37:58 -04:00
|
|
|
return stdout, stderr, status
|
|
|
|
end
|
|
|
|
ensure
|
2012-09-04 08:45:31 -04:00
|
|
|
[th_stdout, th_stderr].each do |th|
|
|
|
|
th.kill if th
|
|
|
|
end
|
2010-07-01 08:37:58 -04:00
|
|
|
[in_c, in_p, out_c, out_p, err_c, err_p].each do |io|
|
|
|
|
io.close if io && !io.closed?
|
|
|
|
end
|
|
|
|
[th_stdout, th_stderr].each do |th|
|
2012-09-04 08:45:31 -04:00
|
|
|
th.join if th
|
2009-11-19 17:47:53 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
module_function :invoke_ruby
|
2009-09-29 02:26:46 -04:00
|
|
|
|
2010-07-01 08:37:58 -04:00
|
|
|
alias rubyexec invoke_ruby
|
|
|
|
class << self
|
|
|
|
alias rubyexec invoke_ruby
|
|
|
|
end
|
|
|
|
|
2009-09-29 02:26:46 -04:00
|
|
|
def verbose_warning
|
|
|
|
class << (stderr = "")
|
|
|
|
alias write <<
|
|
|
|
end
|
|
|
|
stderr, $stderr, verbose, $VERBOSE = $stderr, stderr, $VERBOSE, true
|
|
|
|
yield stderr
|
2012-03-16 04:38:24 -04:00
|
|
|
return $stderr
|
2009-09-29 02:26:46 -04:00
|
|
|
ensure
|
|
|
|
stderr, $stderr, $VERBOSE = $stderr, stderr, verbose
|
|
|
|
end
|
|
|
|
module_function :verbose_warning
|
2010-06-05 21:44:38 -04:00
|
|
|
|
2014-06-01 18:31:30 -04:00
|
|
|
def default_warning
|
|
|
|
verbose, $VERBOSE = $VERBOSE, false
|
|
|
|
yield
|
|
|
|
ensure
|
|
|
|
$VERBOSE = verbose
|
|
|
|
end
|
|
|
|
module_function :default_warning
|
|
|
|
|
2010-07-24 16:26:34 -04:00
|
|
|
def suppress_warning
|
|
|
|
verbose, $VERBOSE = $VERBOSE, nil
|
|
|
|
yield
|
|
|
|
ensure
|
|
|
|
$VERBOSE = verbose
|
|
|
|
end
|
|
|
|
module_function :suppress_warning
|
|
|
|
|
2013-11-09 11:46:17 -05:00
|
|
|
def under_gc_stress(stress = true)
|
|
|
|
stress, GC.stress = GC.stress, stress
|
2010-06-05 21:44:38 -04:00
|
|
|
yield
|
|
|
|
ensure
|
|
|
|
GC.stress = stress
|
|
|
|
end
|
|
|
|
module_function :under_gc_stress
|
2013-03-11 04:29:46 -04:00
|
|
|
|
|
|
|
def with_default_external(enc)
|
|
|
|
verbose, $VERBOSE = $VERBOSE, nil
|
|
|
|
origenc, Encoding.default_external = Encoding.default_external, enc
|
|
|
|
$VERBOSE = verbose
|
|
|
|
yield
|
|
|
|
ensure
|
|
|
|
verbose, $VERBOSE = $VERBOSE, nil
|
|
|
|
Encoding.default_external = origenc
|
|
|
|
$VERBOSE = verbose
|
|
|
|
end
|
|
|
|
module_function :with_default_external
|
|
|
|
|
|
|
|
def with_default_internal(enc)
|
|
|
|
verbose, $VERBOSE = $VERBOSE, nil
|
|
|
|
origenc, Encoding.default_internal = Encoding.default_internal, enc
|
|
|
|
$VERBOSE = verbose
|
|
|
|
yield
|
|
|
|
ensure
|
|
|
|
verbose, $VERBOSE = $VERBOSE, nil
|
|
|
|
Encoding.default_internal = origenc
|
|
|
|
$VERBOSE = verbose
|
|
|
|
end
|
|
|
|
module_function :with_default_internal
|
2013-11-15 17:14:09 -05:00
|
|
|
|
2014-02-25 23:26:09 -05:00
|
|
|
def labeled_module(name, &block)
|
|
|
|
Module.new do
|
|
|
|
singleton_class.class_eval {define_method(:to_s) {name}; alias inspect to_s}
|
|
|
|
class_eval(&block) if block
|
|
|
|
end
|
|
|
|
end
|
|
|
|
module_function :labeled_module
|
|
|
|
|
|
|
|
def labeled_class(name, superclass = Object, &block)
|
|
|
|
Class.new(superclass) do
|
|
|
|
singleton_class.class_eval {define_method(:to_s) {name}; alias inspect to_s}
|
|
|
|
class_eval(&block) if block
|
|
|
|
end
|
|
|
|
end
|
|
|
|
module_function :labeled_class
|
|
|
|
|
2013-11-15 17:14:09 -05:00
|
|
|
if /darwin/ =~ RUBY_PLATFORM
|
|
|
|
DIAGNOSTIC_REPORTS_PATH = File.expand_path("~/Library/Logs/DiagnosticReports")
|
|
|
|
DIAGNOSTIC_REPORTS_TIMEFORMAT = '%Y-%m-%d-%H%M%S'
|
|
|
|
def self.diagnostic_reports(signame, cmd, pid, now)
|
2015-03-03 19:24:06 -05:00
|
|
|
return unless %w[ABRT QUIT SEGV ILL TRAP].include?(signame)
|
2013-11-15 17:14:09 -05:00
|
|
|
cmd = File.basename(cmd)
|
|
|
|
path = DIAGNOSTIC_REPORTS_PATH
|
|
|
|
timeformat = DIAGNOSTIC_REPORTS_TIMEFORMAT
|
|
|
|
pat = "#{path}/#{cmd}_#{now.strftime(timeformat)}[-_]*.crash"
|
|
|
|
first = true
|
2013-12-14 17:34:47 -05:00
|
|
|
30.times do
|
|
|
|
first ? (first = false) : sleep(0.1)
|
2013-11-15 17:14:09 -05:00
|
|
|
Dir.glob(pat) do |name|
|
|
|
|
log = File.read(name) rescue next
|
|
|
|
if /\AProcess:\s+#{cmd} \[#{pid}\]$/ =~ log
|
|
|
|
File.unlink(name)
|
2014-06-10 10:44:26 -04:00
|
|
|
File.unlink("#{path}/.#{File.basename(name)}.plist") rescue nil
|
2013-11-15 17:14:09 -05:00
|
|
|
return log
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
nil
|
|
|
|
end
|
|
|
|
else
|
|
|
|
def self.diagnostic_reports(signame, cmd, pid, now)
|
|
|
|
end
|
|
|
|
end
|
2015-05-27 04:02:20 -04:00
|
|
|
|
|
|
|
def self.gc_stress_to_class?
|
|
|
|
unless defined?(@gc_stress_to_class)
|
|
|
|
_, _, status = invoke_ruby(["-e""exit GC.respond_to?(:add_stress_to_class)"])
|
|
|
|
@gc_stress_to_class = status.success?
|
|
|
|
end
|
|
|
|
@gc_stress_to_class
|
|
|
|
end
|
2003-10-04 22:56:42 -04:00
|
|
|
end
|
2008-05-03 07:57:55 -04:00
|
|
|
|
2010-01-12 02:41:40 -05:00
|
|
|
begin
|
|
|
|
require 'rbconfig'
|
|
|
|
rescue LoadError
|
|
|
|
else
|
|
|
|
module RbConfig
|
|
|
|
@ruby = EnvUtil.rubybin
|
|
|
|
class << self
|
2010-01-25 08:33:02 -05:00
|
|
|
undef ruby if method_defined?(:ruby)
|
2010-01-12 02:41:40 -05:00
|
|
|
attr_reader :ruby
|
|
|
|
end
|
|
|
|
dir = File.dirname(ruby)
|
|
|
|
name = File.basename(ruby, CONFIG['EXEEXT'])
|
|
|
|
CONFIG['bindir'] = dir
|
|
|
|
CONFIG['ruby_install_name'] = name
|
|
|
|
CONFIG['RUBY_INSTALL_NAME'] = name
|
2011-02-10 22:03:26 -05:00
|
|
|
Gem::ConfigMap[:bindir] = dir if defined?(Gem::ConfigMap)
|
2010-01-12 02:41:40 -05:00
|
|
|
end
|
|
|
|
end
|