|
| 1 | +defmodule Systemd.Signal do |
| 2 | + @moduledoc """ |
| 3 | + Helpers for receiving systemd D-Bus signals. |
| 4 | + """ |
| 5 | + |
| 6 | + alias Rebus.Message |
| 7 | + alias Systemd.{DBus, Error} |
| 8 | + |
| 9 | + @dbus_destination "org.freedesktop.DBus" |
| 10 | + @dbus_path "/org/freedesktop/DBus" |
| 11 | + @dbus_interface "org.freedesktop.DBus" |
| 12 | + @systemd_destination "org.freedesktop.systemd1" |
| 13 | + @manager_path "/org/freedesktop/systemd1" |
| 14 | + @manager_interface "org.freedesktop.systemd1.Manager" |
| 15 | + |
| 16 | + @type subscription :: %__MODULE__{conn: pid(), ref: reference(), match_rule: String.t()} |
| 17 | + @type job_removed :: %{ |
| 18 | + id: non_neg_integer(), |
| 19 | + job_path: String.t(), |
| 20 | + unit: String.t(), |
| 21 | + result: String.t() |
| 22 | + } |
| 23 | + |
| 24 | + @enforce_keys [:conn, :ref, :match_rule] |
| 25 | + defstruct [:conn, :ref, :match_rule] |
| 26 | + |
| 27 | + @doc """ |
| 28 | + Subscribes the current process to systemd manager signals. |
| 29 | + """ |
| 30 | + @spec subscribe_manager(pid()) :: {:ok, subscription()} | {:error, Error.t()} |
| 31 | + def subscribe_manager(conn) when is_pid(conn) do |
| 32 | + match_rule = |
| 33 | + "type='signal',sender='#{@systemd_destination}',path='#{@manager_path}',interface='#{@manager_interface}'" |
| 34 | + |
| 35 | + with :ok <- add_match(conn, match_rule), |
| 36 | + {:ok, []} <- manager_call(conn, "Subscribe"), |
| 37 | + ref when is_reference(ref) <- Rebus.add_signal_handler(conn) do |
| 38 | + {:ok, %__MODULE__{conn: conn, ref: ref, match_rule: match_rule}} |
| 39 | + else |
| 40 | + {:error, %Error{} = error} -> {:error, error} |
| 41 | + other -> {:error, Error.protocol_error(other)} |
| 42 | + end |
| 43 | + end |
| 44 | + |
| 45 | + @doc """ |
| 46 | + Removes a signal subscription returned by `subscribe_manager/1`. |
| 47 | + """ |
| 48 | + @spec unsubscribe(subscription()) :: :ok | {:error, Error.t()} |
| 49 | + def unsubscribe(%__MODULE__{conn: conn, ref: ref, match_rule: match_rule}) do |
| 50 | + :ok = Rebus.delete_signal_handler(conn, ref) |
| 51 | + remove_match(conn, match_rule) |
| 52 | + end |
| 53 | + |
| 54 | + @doc """ |
| 55 | + Waits for a `JobRemoved` signal matching a job object path. |
| 56 | + """ |
| 57 | + @spec await_job_removed(subscription(), String.t(), keyword()) :: |
| 58 | + {:ok, job_removed()} | {:error, Error.t()} |
| 59 | + def await_job_removed(%__MODULE__{ref: ref}, job_path, opts \\ []) when is_binary(job_path) do |
| 60 | + timeout = Keyword.get(opts, :timeout, 30_000) |
| 61 | + deadline = System.monotonic_time(:millisecond) + timeout |
| 62 | + do_await_job_removed(ref, job_path, deadline) |
| 63 | + end |
| 64 | + |
| 65 | + defp do_await_job_removed(ref, job_path, deadline) do |
| 66 | + remaining = max(deadline - System.monotonic_time(:millisecond), 0) |
| 67 | + |
| 68 | + receive do |
| 69 | + {^ref, %Message{} = message} -> |
| 70 | + case job_removed(message) do |
| 71 | + {:ok, %{job_path: ^job_path} = removed} -> {:ok, removed} |
| 72 | + {:ok, _other_job} -> do_await_job_removed(ref, job_path, deadline) |
| 73 | + :ignore -> do_await_job_removed(ref, job_path, deadline) |
| 74 | + end |
| 75 | + after |
| 76 | + remaining -> {:error, Error.connection_error(:timeout)} |
| 77 | + end |
| 78 | + end |
| 79 | + |
| 80 | + defp job_removed(%Message{ |
| 81 | + type: :signal, |
| 82 | + header_fields: headers, |
| 83 | + body: [id, job_path, unit, result] |
| 84 | + }) do |
| 85 | + if Map.get(headers, :member) == "JobRemoved" and |
| 86 | + Map.get(headers, :interface) == @manager_interface do |
| 87 | + {:ok, %{id: id, job_path: job_path, unit: unit, result: result}} |
| 88 | + else |
| 89 | + :ignore |
| 90 | + end |
| 91 | + end |
| 92 | + |
| 93 | + defp job_removed(_message), do: :ignore |
| 94 | + |
| 95 | + defp add_match(conn, match_rule), do: dbus_void_call(conn, "AddMatch", [match_rule], "s") |
| 96 | + defp remove_match(conn, match_rule), do: dbus_void_call(conn, "RemoveMatch", [match_rule], "s") |
| 97 | + |
| 98 | + defp manager_call(conn, member) do |
| 99 | + DBus.call_body(conn, |
| 100 | + destination: @systemd_destination, |
| 101 | + path: @manager_path, |
| 102 | + interface: @manager_interface, |
| 103 | + member: member |
| 104 | + ) |
| 105 | + end |
| 106 | + |
| 107 | + defp dbus_void_call(conn, member, body, signature) do |
| 108 | + with {:ok, []} <- |
| 109 | + DBus.call_body(conn, |
| 110 | + destination: @dbus_destination, |
| 111 | + path: @dbus_path, |
| 112 | + interface: @dbus_interface, |
| 113 | + member: member, |
| 114 | + signature: signature, |
| 115 | + body: body |
| 116 | + ) do |
| 117 | + :ok |
| 118 | + end |
| 119 | + end |
| 120 | +end |
0 commit comments