Skip to content

Commit

Permalink
Resolve binary command's path to be absolute
Browse files Browse the repository at this point in the history
  • Loading branch information
mattludwigs authored and fhunleth committed Jul 29, 2020
1 parent 1e0e174 commit 09a3d08
Show file tree
Hide file tree
Showing 11 changed files with 49 additions and 6 deletions.
2 changes: 2 additions & 0 deletions config/config.exs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use Mix.Config
# * udhcpd_handler: capture whatever happens with udhcpd
# * interface_renamer: capture interfaces that get renamed
# * resolvconf: don't update the real resolv.conf
# * path: limit search for tools to our test harness
# * persistence_dir: use the current directory
# * power_managers: register a manager for test0 so that tests
# that need to validate power management calls can use it.
Expand All @@ -21,6 +22,7 @@ config :vintage_net,
udhcpd_handler: VintageNetTest.CapturingUdhcpdHandler,
interface_renamer: VintageNetTest.CapturingInterfaceRenamer,
resolvconf: "/dev/null",
path: "#{File.cwd!()}/test/fixtures/root/bin",
persistence_dir: "./test_tmp/persistence",
power_managers: [
{VintageNetTest.TestPowerManager, [ifname: "test0", watchdog_timeout: 50]},
Expand Down
25 changes: 21 additions & 4 deletions lib/vintage_net/command.ex
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@ defmodule VintageNet.Command do
def cmd(command, args, opts \\ []) when is_binary(command) do
new_opts = force_path_env(opts)

System.cmd(command, args, new_opts)
System.cmd(find_executable!(command), args, new_opts)
end

@spec muon_cmd(Path.t(), [binary()], keyword()) ::
{Collectable.t(), exit_status :: non_neg_integer()}
def muon_cmd(command, args, opts \\ []) when is_binary(command) do
new_opts = opts |> force_path_env() |> add_muon_options()

MuonTrap.cmd(command, args, new_opts)
MuonTrap.cmd(find_executable!(command), args, new_opts)
end

@doc """
Expand All @@ -28,15 +28,32 @@ defmodule VintageNet.Command do
)
end

defp find_executable!(command) do
find_executable(command) ||
raise(RuntimeError, "Can't find '#{command}' in '#{path_env()}'")
end

defp find_executable(command) do
paths = String.split(path_env(), ":")

Enum.find_value(paths, fn path ->
full_path = Path.join(path, command)

if File.exists?(full_path) do
full_path
end
end)
end

defp force_path_env(opts) do
original_env = Keyword.get(opts, :env, [])

new_env = [path_env() | List.keydelete(original_env, "PATH", 0)]
new_env = [{"PATH", path_env()} | List.keydelete(original_env, "PATH", 0)]

Keyword.put(opts, :env, new_env)
end

defp path_env() do
{"PATH", Application.get_env(:vintage_net, :path)}
Application.get_env(:vintage_net, :path)
end
end
3 changes: 3 additions & 0 deletions test/fixtures/root/bin/echo
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/bin/sh

/bin/echo "$@"
3 changes: 3 additions & 0 deletions test/fixtures/root/bin/false
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/bin/sh

exit 1
3 changes: 3 additions & 0 deletions test/fixtures/root/bin/ip
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/bin/sh

exit 1
3 changes: 3 additions & 0 deletions test/fixtures/root/bin/rm
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/bin/sh

/bin/rm "$@"
3 changes: 3 additions & 0 deletions test/fixtures/root/bin/sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/bin/sh

/bin/sh "$@"
3 changes: 3 additions & 0 deletions test/fixtures/root/bin/sleep
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/bin/sh

/bin/sleep "$@"
3 changes: 3 additions & 0 deletions test/fixtures/root/bin/test
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/bin/sh

/bin/test "$@"
3 changes: 3 additions & 0 deletions test/fixtures/root/bin/touch
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/bin/sh

/usr/bin/touch "$@"
4 changes: 2 additions & 2 deletions test/vintage_net/interface_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -227,12 +227,12 @@ defmodule VintageNet.InterfaceTest do
{"run.sh",
"""
#!/bin/sh
if [ -e first_try ]; then
if test -e first_try; then
touch i_am_configured
else
# Hang the first time
touch first_try
/bin/sleep 10000
sleep 10000
echo "Should not get here"
fi
"""}
Expand Down

0 comments on commit 09a3d08

Please sign in to comment.