Skip to content

Commit

Permalink
Remove bin_* paths
Browse files Browse the repository at this point in the history
  • Loading branch information
mattludwigs committed Jul 17, 2020
1 parent 690a2a8 commit a133c0e
Show file tree
Hide file tree
Showing 14 changed files with 131 additions and 163 deletions.
15 changes: 0 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,13 +86,6 @@ Then make sure that you have the following Busybox options enabled:
* `CONFIG_UDHCPC=y` - `udhcpc` DHCP Client
* `CONFIG_UDHCPD=y` - `udhcpd` DHCP Server (optional)

You can avoid making the Busybox changes by adding `:busybox` to your project's
mix dependencies:

```elixir
{:busybox, "~> 0.1", targets: @all_targets}
```

Finally, you'll need to choose what network connection technologies that you
want available in your firmware. If using `nerves_pack`, you'll get support for
wired Ethernet, WiFi, and USB gadget networking automatically. Otherwise, add
Expand Down Expand Up @@ -140,14 +133,6 @@ Key | Description
config | A list of default network configurations
tmpdir | Path to a temporary directory for VintageNet
to_elixir_socket | Name to use for the Unix domain socket for C to Elixir communication
bin_ifup | Path to `ifup`
bin_ifdown | Path to `ifdown`
bin_chat | Path to `chat`
bin_pppd | Path to `pppd`
bin_mknod | Path to `mknod`
bin_killall | Path to `killall`
bin_wpa_supplicant | Path to `wpa_supplicant`
bin_ip | Path to `ip`
udhcpc_handler | Module for handling notifications from `udhcpc`
resolvconf | Path to `/etc/resolv.conf`
persistence | Module for persisting network configurations
Expand Down
51 changes: 0 additions & 51 deletions lib/vintage_net/application.ex
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,6 @@ defmodule VintageNet.Application do
args = Application.get_all_env(:vintage_net)
socket_path = Path.join(Keyword.get(args, :tmpdir), Keyword.get(args, :to_elixir_socket))
hw_path_ifnames = Keyword.get(args, :ifnames, [])
# Resolve paths to all of the programs that might be used.
if using_elixir_busybox() do
args
|> resolve_paths(&resolve_busybox_path/1)
|> put_env()

Application.put_env(:vintage_net, :path, busybox_path() |> Enum.join(":"))
else
args
|> resolve_paths(&resolve_standard_path/1)
|> put_env()
end

# Load the initial interface configuration and store in the
# property table
Expand All @@ -44,45 +32,6 @@ defmodule VintageNet.Application do
Supervisor.start_link(children, opts)
end

defp using_elixir_busybox() do
Code.ensure_loaded?(Busybox)
end

defp put_env(list) do
Enum.each(list, fn {k, v} -> Application.put_env(:vintage_net, k, v) end)
end

defp resolve_paths(env, resolver) do
env
|> Enum.filter(fn {k, _v} -> String.starts_with?(to_string(k), "bin_") end)
|> Enum.filter(fn {_k, v} -> !String.starts_with?(v, "/") end)
|> Enum.map(resolver)
end

defp resolve_busybox_path({key, program_name}) do
case apply(Busybox, :find_executable, [program_name]) do
nil ->
resolve_standard_path({key, program_name})

path ->
{key, path}
end
end

defp busybox_path() do
apply(Busybox, :path, [])
end

defp resolve_standard_path({key, program_name}) do
case System.find_executable(program_name) do
nil ->
{key, program_name}

path ->
{key, path}
end
end

defp load_initial_configurations() do
# Get the default interface configurations
configs = Application.get_env(:vintage_net, :config) |> Map.new()
Expand Down
10 changes: 2 additions & 8 deletions lib/vintage_net/command.ex
Original file line number Diff line number Diff line change
@@ -1,18 +1,12 @@
defmodule VintageNet.Command do
@moduledoc false

@spec cmd(atom(), [binary()], keyword()) ::
@spec cmd(Path.t(), [binary()], keyword()) ::
{Collectable.t(), exit_status :: non_neg_integer()}
def cmd(command, args, opts \\ []) do
new_opts = force_path_env(opts)

case Application.get_env(:vintage_net, command) do
nil ->
raise "Unexpected command #{command}"

path ->
System.cmd(path, args, new_opts)
end
System.cmd(command, args, new_opts)
end

@spec muon_cmd(binary(), [binary()], keyword()) ::
Expand Down
6 changes: 3 additions & 3 deletions lib/vintage_net/interface/udhcpc.ex
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ defmodule VintageNet.Interface.Udhcpc do

# /sbin/ifconfig $interface up
# /sbin/ifconfig $interface 0.0.0.0
_ = Command.cmd(:bin_ifconfig, [ifname, "up"])
_ = Command.cmd(:bin_ifconfig, [ifname, "0.0.0.0"])
_ = Command.cmd("ifconfig", [ifname, "up"])
_ = Command.cmd("ifconfig", [ifname, "0.0.0.0"])

# # drop info from this interface
# # resolv.conf may be a symlink to /tmp/, so take care
Expand Down Expand Up @@ -96,7 +96,7 @@ defmodule VintageNet.Interface.Udhcpc do
# /sbin/ifconfig $interface $ip $BROADCAST $NETMASK

ifconfig_args = build_ifconfig_args(ifname, info)
_ = Command.cmd(:bin_ifconfig, ifconfig_args)
_ = Command.cmd("ifconfig", ifconfig_args)

case info[:router] do
routers when is_list(routers) ->
Expand Down
2 changes: 1 addition & 1 deletion lib/vintage_net/interface_renamer/ip.ex
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ defmodule VintageNet.InterfaceRenamer.IP do
def rename_interface(ifname, rename_to) do
args = ["link", "set", ifname, "name", rename_to]

case Command.cmd(:bin_ip, args, stderr_to_stdout: true) do
case Command.cmd("ip", args, stderr_to_stdout: true) do
{_, 0} -> :ok
{message, _error} -> {:error, message}
end
Expand Down
3 changes: 1 addition & 2 deletions lib/vintage_net/ip/dhcpd_config.ex
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,6 @@ defmodule VintageNet.IP.DhcpdConfig do
opts
) do
tmpdir = Keyword.fetch!(opts, :tmpdir)
udhcpd = Keyword.fetch!(opts, :bin_udhcpd)
udhcpd_conf_path = Path.join(tmpdir, "udhcpd.conf.#{ifname}")

new_files =
Expand All @@ -161,7 +160,7 @@ defmodule VintageNet.IP.DhcpdConfig do
Supervisor.child_spec(
{MuonTrap.Daemon,
[
udhcpd,
"udhcpd",
[
"-f",
udhcpd_conf_path
Expand Down
3 changes: 1 addition & 2 deletions lib/vintage_net/ip/dnsd_config.ex
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,6 @@ defmodule VintageNet.IP.DnsdConfig do
opts
) do
tmpdir = Keyword.fetch!(opts, :tmpdir)
dnsd = Keyword.fetch!(opts, :bin_dnsd)
dnsd_conf_path = Path.join(tmpdir, "dnsd.conf.#{ifname}")

new_files = [{dnsd_conf_path, dnsd_contents(dnsd_config)} | files]
Expand All @@ -93,7 +92,7 @@ defmodule VintageNet.IP.DnsdConfig do
Supervisor.child_spec(
{MuonTrap.Daemon,
[
dnsd,
"dnsd",
dnsd_args,
Command.add_muon_options(stderr_to_stdout: true, log_output: :debug)
]},
Expand Down
34 changes: 15 additions & 19 deletions lib/vintage_net/ip/ipv4_config.ex
Original file line number Diff line number Diff line change
Expand Up @@ -112,17 +112,16 @@ defmodule VintageNet.IP.IPv4Config do
down_cmds: down_cmds
} = raw_config,
%{ipv4: %{method: :disabled}},
opts
_opts
) do
# Even though IPv4 is disabled, the interface is still brought up
ip = Keyword.fetch!(opts, :bin_ip)
new_up_cmds = up_cmds ++ [{:run, ip, ["link", "set", ifname, "up"]}]
new_up_cmds = up_cmds ++ [{:run, "ip", ["link", "set", ifname, "up"]}]

new_down_cmds =
down_cmds ++
[
{:run_ignore_errors, ip, ["addr", "flush", "dev", ifname, "label", ifname]},
{:run, ip, ["link", "set", ifname, "down"]}
{:run_ignore_errors, "ip", ["addr", "flush", "dev", ifname, "label", ifname]},
{:run, "ip", ["link", "set", ifname, "down"]}
]

%RawConfig{
Expand All @@ -140,17 +139,15 @@ defmodule VintageNet.IP.IPv4Config do
down_cmds: down_cmds
} = raw_config,
%{ipv4: %{method: :dhcp}} = config,
opts
_opts
) do
udhcpc = Keyword.fetch!(opts, :bin_udhcpc)
ip = Keyword.fetch!(opts, :bin_ip)
new_up_cmds = up_cmds ++ [{:run, ip, ["link", "set", ifname, "up"]}]
new_up_cmds = up_cmds ++ [{:run, "ip", ["link", "set", ifname, "up"]}]

new_down_cmds =
down_cmds ++
[
{:run_ignore_errors, ip, ["addr", "flush", "dev", ifname, "label", ifname]},
{:run, ip, ["link", "set", ifname, "down"]}
{:run_ignore_errors, "ip", ["addr", "flush", "dev", ifname, "label", ifname]},
{:run, "ip", ["link", "set", ifname, "down"]}
]

hostname = config[:hostname] || get_hostname()
Expand All @@ -161,7 +158,7 @@ defmodule VintageNet.IP.IPv4Config do
Supervisor.child_spec(
{MuonTrap.Daemon,
[
udhcpc,
"udhcpc",
[
"-f",
"-i",
Expand Down Expand Up @@ -198,9 +195,8 @@ defmodule VintageNet.IP.IPv4Config do
child_specs: child_specs
} = raw_config,
%{ipv4: %{method: :static} = ipv4},
opts
_opts
) do
ip = Keyword.fetch!(opts, :bin_ip)
addr_subnet = IP.cidr_to_string(ipv4.address, ipv4.prefix_length)

route_manager_up =
Expand All @@ -223,9 +219,9 @@ defmodule VintageNet.IP.IPv4Config do
new_up_cmds =
up_cmds ++
[
{:run_ignore_errors, ip, ["addr", "flush", "dev", ifname, "label", ifname]},
{:run, ip, ["addr", "add", addr_subnet, "dev", ifname, "label", ifname]},
{:run, ip, ["link", "set", ifname, "up"]},
{:run_ignore_errors, "ip", ["addr", "flush", "dev", ifname, "label", ifname]},
{:run, "ip", ["addr", "add", addr_subnet, "dev", ifname, "label", ifname]},
{:run, "ip", ["link", "set", ifname, "up"]},
route_manager_up,
resolver_up
]
Expand All @@ -235,8 +231,8 @@ defmodule VintageNet.IP.IPv4Config do
[
{:fun, VintageNet.RouteManager, :clear_route, [ifname]},
{:fun, VintageNet.NameResolver, :clear, [ifname]},
{:run_ignore_errors, ip, ["addr", "flush", "dev", ifname, "label", ifname]},
{:run, ip, ["link", "set", ifname, "down"]}
{:run_ignore_errors, "ip", ["addr", "flush", "dev", ifname, "label", ifname]},
{:run, "ip", ["link", "set", ifname, "down"]}
]

# If there's a default gateway, then check for internet connectivity.
Expand Down
2 changes: 1 addition & 1 deletion lib/vintage_net/route/ip_route.ex
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ defmodule VintageNet.Route.IPRoute do
# Send iodata to the logger with the command and args interspersed with spaces
# Logger.debug(Enum.intersperse(["ip" | args], " "))

case Command.cmd(:bin_ip, args, stderr_to_stdout: true) do
case Command.cmd("ip", args, stderr_to_stdout: true) do
{_, 0} -> :ok
{message, _error} -> {:error, message}
end
Expand Down
15 changes: 0 additions & 15 deletions mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -35,24 +35,11 @@ defmodule VintageNet.MixProject do

def application do
[
# The bin_* variables are paths to programs. Set to absolute paths to pin.
# Program names are found at application start and converted to absolute.
env: [
config: [],
max_interface_count: 8,
tmpdir: "/tmp/vintage_net",
to_elixir_socket: "comms",
bin_dnsd: "dnsd",
bin_ifconfig: "ifconfig",
bin_chat: "chat",
bin_pppd: "pppd",
bin_mknod: "mknod",
bin_killall: "killall",
bin_wpa_supplicant: "/usr/sbin/wpa_supplicant",
bin_ip: "ip",
bin_udhcpc: "udhcpc",
bin_udhcpd: "udhcpd",
bin_brctl: "brctl",
path: "/usr/sbin:/usr/bin:/sbin:/bin",
udhcpc_handler: VintageNet.Interface.Udhcpc,
udhcpd_handler: VintageNet.Interface.Udhcpd,
Expand Down Expand Up @@ -105,15 +92,13 @@ defmodule VintageNet.MixProject do
{:dialyxir, "~> 1.0.0", only: [:dev, :test], runtime: false},
{:muontrap, "~> 0.5.1 or ~> 0.6.0"},
{:gen_state_machine, "~> 2.0.0 or ~> 2.1.0"},
{:busybox, "~> 0.1.5", optional: true},
{:credo, "~> 1.2", only: :test, runtime: false}
]
end

defp dialyzer() do
[
flags: [:race_conditions, :unmatched_returns, :error_handling, :underspecs],
plt_add_apps: [:busybox],
ignore_warnings: ".dialyzer_ignore.exs",
list_unused_filters: true
]
Expand Down

0 comments on commit a133c0e

Please sign in to comment.