Skip to content

Commit

Permalink
Fix Dialyzer warnings
Browse files Browse the repository at this point in the history
* Ignore return values that have comments saying that they're ignored
* Warn on error returns for things that were ignored but where crashing
  might be really bad
* Ignore the no_return from NervesRuntime.logged_shutdown since that
  function specifically isn't supposed to return
  • Loading branch information
fhunleth committed Jun 4, 2019
1 parent 6905045 commit e79f91f
Show file tree
Hide file tree
Showing 8 changed files with 64 additions and 32 deletions.
4 changes: 4 additions & 0 deletions .dialyzer_ignore.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[
# NervesRuntime.logged_shutdown isn't supposed to return
{"lib/nerves_runtime.ex", :no_return, 93}
]
2 changes: 1 addition & 1 deletion .formatter.exs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Used by "mix format"
[
inputs: ["{mix,.formatter}.exs", "{config,lib,test}/**/*.{ex,exs}"]
inputs: ["{mix,.formatter,.dialyzer_ignore}.exs", "{config,lib,test}/**/*.{ex,exs}"]
]
26 changes: 16 additions & 10 deletions lib/nerves_runtime/application.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,14 @@ defmodule Nerves.Runtime.Application do
@moduledoc false

use Application
require Logger

alias Nerves.Runtime.{
Init,
Kernel,
KV
}

alias Nerves.Runtime.{Init, Kernel, KV}
alias Nerves.Runtime.Log.{KmsgTailer, SyslogTailer}

@rngd_path "/usr/sbin/rngd"

@impl true
def start(_type, _args) do
# On systems with hardware random number generation, it is important that
# "rngd" gets started as soon as possible to start adding entropy to the
Expand Down Expand Up @@ -46,11 +45,18 @@ defmodule Nerves.Runtime.Application do
end

defp try_rngd() do
rngd_path = "/usr/sbin/rngd"

if File.exists?(rngd_path) do
if File.exists?(@rngd_path) do
# Launch rngd. It daemonizes itself so this should return quickly.
System.cmd(rngd_path, [])
case System.cmd(@rngd_path, []) do
{0, _} ->
:ok

{_non_zero, reason} ->
_ = Logger.warn("Failed to start rngd: #{reason}")
:ok
end
else
:ok
end
end
end
19 changes: 15 additions & 4 deletions lib/nerves_runtime/init.ex
Original file line number Diff line number Diff line change
Expand Up @@ -112,12 +112,12 @@ defmodule Nerves.Runtime.Init do
defp mount(%{mounted: :mounted} = s), do: s

defp mount(s) do
Runtime.cmd("mount", ["-t", s.fstype, "-o", "rw", s.devpath, s.target], :info)
check_cmd("mount", ["-t", s.fstype, "-o", "rw", s.devpath, s.target], :info)
mounted_state(s)
end

defp unmount_if_error(%{mounted: :mounted_with_error} = s) do
Runtime.cmd("umount", [s.target], :info)
check_cmd("umount", [s.target], :info)
mounted_state(s)
end

Expand All @@ -136,11 +136,22 @@ defmodule Nerves.Runtime.Init do
defp format_if_unmounted(s), do: s

defp mkfs("f2fs", devpath) do
Runtime.cmd("mkfs.f2fs", ["#{devpath}"], :info)
check_cmd("mkfs.f2fs", ["#{devpath}"], :info)
end

defp mkfs(fstype, devpath) do
Runtime.cmd("mkfs.#{fstype}", ["-U", @app_partition_uuid, "-F", "#{devpath}"], :info)
check_cmd("mkfs.#{fstype}", ["-U", @app_partition_uuid, "-F", "#{devpath}"], :info)
end

defp check_cmd(cmd, args, out) do
case Runtime.cmd(cmd, args, out) do
{0, _} ->
:ok

{status, _} ->
_ = Logger.warn("Ignoring non-zero exit status (#{status}) from #{cmd} #{inspect(args)}")
:ok
end
end

defp validate_mount(s), do: s.mounted
Expand Down
20 changes: 13 additions & 7 deletions lib/nerves_runtime/kernel/uevent.ex
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ defmodule Nerves.Runtime.Kernel.UEvent do
@impl true
def handle_info({port, {:data, message}}, %State{port: port} = s) do
{action, scope_no_state, kvmap} = :erlang.binary_to_term(message)
registry(action, [:state | scope_no_state], kvmap, s)
_ = registry(action, [:state | scope_no_state], kvmap, s)
{:noreply, s}
end

Expand All @@ -55,10 +55,13 @@ defmodule Nerves.Runtime.Kernel.UEvent do

if s.use_system_registry do
if subsystem = Map.get(kvmap, "subsystem") do
SystemRegistry.update_in(subsystem_scope(subsystem), fn v ->
v = if is_nil(v), do: [], else: v
[scope | v]
end)
_ =
SystemRegistry.update_in(subsystem_scope(subsystem), fn v ->
v = if is_nil(v), do: [], else: v
[scope | v]
end)

:ok
end

SystemRegistry.update(scope, kvmap)
Expand All @@ -67,7 +70,7 @@ defmodule Nerves.Runtime.Kernel.UEvent do

def registry("remove", scope, kvmap, %State{use_system_registry: true}) do
# Logger.debug("uevent remove: #{inspect(scope)}")
SystemRegistry.delete(scope)
_ = SystemRegistry.delete(scope)

if subsystem = Map.get(kvmap, "subsystem") do
SystemRegistry.update_in(subsystem_scope(subsystem), fn v ->
Expand Down Expand Up @@ -102,7 +105,10 @@ defmodule Nerves.Runtime.Kernel.UEvent do
end

defp modprobe(%{"modalias" => modalias}) do
System.cmd("/sbin/modprobe", [modalias], stderr_to_stdout: true)
# There's not necessarily a kernel module to be loaded for many
# modalias values. We don't know without trying, though.
_ = System.cmd("/sbin/modprobe", [modalias], stderr_to_stdout: true)
:ok
end

defp modprobe(_), do: :noop
Expand Down
2 changes: 1 addition & 1 deletion lib/nerves_runtime/log/kmsg_tailer.ex
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ defmodule Nerves.Runtime.Log.KmsgTailer do
{port, {:data, {:eol, fragment}}},
%{port: port, buffer: buffer} = state
) do
handle_message(buffer <> fragment)
_ = handle_message(buffer <> fragment)
{:noreply, %{state | buffer: ""}}
end

Expand Down
20 changes: 12 additions & 8 deletions lib/nerves_runtime/log/syslog_tailer.ex
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ defmodule Nerves.Runtime.Log.SyslogTailer do
@impl true
def init(_args) do
# Blindly try to remove an old file just in case it exists from a previous run
File.rm(@syslog_path)
_ = File.rm(@syslog_path)

{:ok, log_port} =
:gen_udp.open(0, [:local, :binary, {:active, true}, {:ip, {:local, @syslog_path}}])
Expand All @@ -37,19 +37,23 @@ defmodule Nerves.Runtime.Log.SyslogTailer do
def handle_info({:udp, log_port, _, 0, raw_entry}, log_port) do
case Parser.parse_syslog(raw_entry) do
%{facility: facility, severity: severity, message: message} ->
Logger.bare_log(
logger_level(severity),
message,
module: __MODULE__,
facility: facility,
severity: severity
)
_ =
Logger.bare_log(
logger_level(severity),
message,
module: __MODULE__,
facility: facility,
severity: severity
)

:ok

_ ->
# This is unlikely to ever happen, but if a message was somehow
# malformed and we couldn't parse the syslog priority, we should
# still do a best-effort to pass along the raw data.
_ = Logger.warn("Malformed syslog report: #{inspect(raw_entry)}")
:ok
end

{:noreply, log_port}
Expand Down
3 changes: 2 additions & 1 deletion mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@ defmodule Nerves.Runtime.MixProject do

defp dialyzer() do
[
flags: [:race_conditions, :unmatched_returns, :error_handling]
flags: [:race_conditions, :unmatched_returns, :error_handling],
ignore_warnings: ".dialyzer_ignore.exs"
]
end

Expand Down

0 comments on commit e79f91f

Please sign in to comment.