Skip to content

Commit

Permalink
sandbox: fallback to tput for winsize
Browse files Browse the repository at this point in the history
  • Loading branch information
Bo98 committed Sep 1, 2021
1 parent 008296f commit 9e42ddb
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 4 deletions.
17 changes: 15 additions & 2 deletions Library/Homebrew/sandbox.rb
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,21 @@ def exec(*args)
command = [SANDBOX_EXEC, "-f", seatbelt.path, *args]
# Start sandbox in a pseudoterminal to prevent access of the parent terminal.
T.unsafe(PTY).spawn(*command) do |r, w, pid|
old_winch = trap(:WINCH) { w.winsize = $stdout.winsize if $stdout.tty? }
w.winsize = $stdout.winsize if $stdout.tty?
# Set the PTY's window size to match the parent terminal.
# Some formula tests are sensitive to the terminal size and fail if this is not set.
winch = proc do |_sig|
w.winsize = if $stdout.tty?
# We can only use IO#winsize if the IO object is a TTY.
$stdout.winsize
else
# Otherwise, default to tput, if available.
# This relies on ncurses rather than the system's ioctl.
[Utils.popen_read("tput", "lines").to_i, Utils.popen_read("tput", "cols").to_i]
end
end
# Update the window size whenever the parent terminal's window size changes.
old_winch = trap(:WINCH, &winch)
winch.call(nil)

$stdin.raw! if $stdin.tty?
stdin_thread = Thread.new { IO.copy_stream($stdin, w) }
Expand Down
6 changes: 4 additions & 2 deletions Library/Homebrew/test/sandbox_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@
it "complains on failure" do
ENV["HOMEBREW_VERBOSE"] = "1"

expect(Utils).to receive(:popen_read).and_return("foo")
allow(Utils).to receive(:popen_read).and_call_original
allow(Utils).to receive(:popen_read).with("syslog", any_args).and_return("foo")

expect { sandbox.exec "false" }
.to raise_error(ErrorDuringExecution)
Expand All @@ -49,7 +50,8 @@
Mar 17 02:55:06 sandboxd[342]: Python(49765) deny file-write-unlink /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/distutils/errors.pyc
bar
EOS
expect(Utils).to receive(:popen_read).and_return(with_bogus_error)
allow(Utils).to receive(:popen_read).and_call_original
allow(Utils).to receive(:popen_read).with("syslog", any_args).and_return(with_bogus_error)

expect { sandbox.exec "false" }
.to raise_error(ErrorDuringExecution)
Expand Down

0 comments on commit 9e42ddb

Please sign in to comment.