---
title: llarp/linux/dbus.hpp

---

# llarp/linux/dbus.hpp



## Namespaces

| Name           |
| -------------- |
| **[llarp](Namespaces/namespacellarp.md)** <br>[crypto.hpp] |
| **[llarp::linux](Namespaces/namespacellarp_1_1linux.md)**  |

## Classes

|                | Name           |
| -------------- | -------------- |
| class | **[llarp::linux::system_bus_exception](Classes/classllarp_1_1linux_1_1system__bus__exception.md)** <br>exception for connecting to system bus  |
| class | **[llarp::linux::dbus_call_exception](Classes/classllarp_1_1linux_1_1dbus__call__exception.md)** <br>exception for a failed calling of a dbus method  |
| class | **[llarp::linux::DBUS](Classes/classllarp_1_1linux_1_1DBUS.md)**  |




## Source code

```cpp
#pragma once

extern "C"
{
#include <systemd/sd-bus.h>
}
#include <memory>
#include <stdexcept>
#include <string>

namespace llarp::linux
{
  class system_bus_exception : public std::runtime_error
  {
   public:
    explicit system_bus_exception(int err);
  };

  class dbus_call_exception : public std::runtime_error
  {
   public:
    explicit dbus_call_exception(std::string meth, int err);
  };

  class DBUS
  {
    struct sd_bus_deleter
    {
      void
      operator()(sd_bus* ptr) const
      {
        sd_bus_unref(ptr);
      }
    };
    std::unique_ptr<sd_bus, sd_bus_deleter> m_ptr;
    const std::string m_interface;
    const std::string m_instance;
    const std::string m_call;

   public:
    DBUS(std::string _interface, std::string _instance, std::string _call);

    template <typename... T>
    void
    operator()(std::string method, const char* arg_format, T... args)
    {
      sd_bus_error error = SD_BUS_ERROR_NULL;
      sd_bus_message* msg = nullptr;
      auto r = sd_bus_call_method(
          m_ptr.get(),
          m_interface.c_str(),
          m_instance.c_str(),
          m_call.c_str(),
          method.c_str(),
          &error,
          &msg,
          arg_format,
          args...);

      if (r < 0)
        throw dbus_call_exception{std::move(method), r};

      sd_bus_message_unref(msg);
      sd_bus_error_free(&error);
    }
  };
}  // namespace llarp::linux
```


-------------------------------

Updated on 2022-10-17 at 13:14:50 +0000
