Module: Reviser::Helpers::System

Included in:
Criteria::Compilation, Extensions::Valgrind
Defined in:
lib/reviser/helpers/system.rb

Overview

Wraps methods for system calls  (external programs execution)

Author:

  • Renan Strauss

Instance Method Summary collapse

Instance Method Details

#exec_with_timeout(cmd, timeout = Cfg[:timeout]) ⇒ Object

Executes the given command and kills it if its execution time > timeout

Returns:

  • stdout, stderr & process_status



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/reviser/helpers/system.rb', line 46

def exec_with_timeout(cmd, timeout = Cfg[:timeout])
	stdin, stdout, stderr, wait_thr = Open3.popen3(cmd)
	process_status = -1

	stdin.close
	#
	# We try to wait for the thread to join
	# during the given timeout.
	# When the thread has joined, process_status
	# will be an object, so we can check and
	# return at the end if it failed to complete
	# before time runs out.
	#
	begin
		Timeout.timeout(timeout) do
			process_status = wait_thr.value
		end
	rescue Timeout::Error
		#
		# Then whether it suceeded or not,
		# we kill the process
		#
		begin
			Process.kill('KILL', wait_thr[:pid])
		rescue Object => e
			$stderr << "Unable to kill process : #{e.to_s}"
		end
	end

	result = {
		:stdout => process_status == -1 && 'Timeout' || stdout.read,
		:stderr => process_status == -1 && 'Timeout' || stderr.read,
		:process_status => process_status == -1 && 'Timeout' || process_status
	}
	
	result.delete :process_status unless process_status != -1

	stdout.close
	stderr.close

	result
end

#find_executableObject

Returns the first executable found.

Returns:

  • the first executable found



36
37
38
# File 'lib/reviser/helpers/system.rb', line 36

def find_executable
	Dir['*'].select { |f| File.executable?(f) && !File.directory?(f) }.first
end