Skip to content

Commit

Permalink
thread safety env set (thanks to @dmajda)
Browse files Browse the repository at this point in the history
  • Loading branch information
jreidinger committed Dec 17, 2015
1 parent 29ea1a3 commit 7586d49
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 19 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ Features
* 100% secure (shell expansion is impossible by design)
* Raises exceptions on errors (no more manual status code checks)
but allows to specify which non-zero codes are still a success run
* Thread-safety
* Allows overriding environment variables
* Optional logging for easy debugging

Expand Down
41 changes: 22 additions & 19 deletions lib/cheetah.rb
Original file line number Diff line number Diff line change
Expand Up @@ -393,19 +393,17 @@ def run(*args)

recorder.record_commands(commands)

with_env(options[:env]) do
pid, pipes = fork_commands(commands)
select_loop(streams, pipes, recorder)
_pid, status = Process.wait2(pid)

begin
check_errors(commands, status, streams, streamed, options)
ensure
recorder.record_status(status)
end

build_result(streams, status, options)
pid, pipes = fork_commands(commands, options)
select_loop(streams, pipes, recorder)
_pid, status = Process.wait2(pid)

begin
check_errors(commands, status, streams, streamed, options)
ensure
recorder.record_status(status)
end

build_result(streams, status, options)
end

private
Expand Down Expand Up @@ -469,7 +467,7 @@ def build_recorder(options)
end
end

def fork_commands_recursive(commands, pipes)
def fork_commands_recursive(commands, pipes, options)
fork do
begin
if commands.size == 1
Expand All @@ -480,9 +478,12 @@ def fork_commands_recursive(commands, pipes)
pipe_to_child = IO.pipe

fork_commands_recursive(commands[0..-2],
stdin: pipes[:stdin],
stdout: pipe_to_child,
stderr: pipes[:stderr]
{
stdin: pipes[:stdin],
stdout: pipe_to_child,
stderr: pipes[:stderr]
},
options
)

pipes[:stdin][READ].close
Expand All @@ -506,17 +507,19 @@ def fork_commands_recursive(commands, pipes)
# number portably in Ruby, I didn't implement it. Patches welcome.

command, *args = commands.last
exec([command, command], *args)
with_env(options[:env]) do
exec([command, command], *args)
end
rescue SystemCallError
exit!(127)
end
end
end

def fork_commands(commands)
def fork_commands(commands, options)
pipes = { stdin: IO.pipe, stdout: IO.pipe, stderr: IO.pipe }

pid = fork_commands_recursive(commands, pipes)
pid = fork_commands_recursive(commands, pipes, options)

[
pipes[:stdin][READ],
Expand Down

0 comments on commit 7586d49

Please sign in to comment.