blob: 7e787c0b7f3dc8f3fd70258cf7f44b9e5ea754cd [file] [log] [blame]
Tomáš Pecka33563f52021-01-25 14:31:41 +01001#pragma once
2
3#include <csignal>
4#include <sdbus-c++/sdbus-c++.h>
5#include <thread>
6#include "utils/log.h"
7
8#define DBUS_EVENTLOOP_INIT \
9 std::unique_ptr<sdbus::IConnection> g_dbusConnection; \
10 std::thread g_eventLoop;
11
12#define DBUS_EVENTLOOP_START \
Tomáš Pecka33563f52021-01-25 14:31:41 +010013 g_dbusConnection = sdbus::createSystemBusConnection(); \
14 /* Gracefully leave dbus event loop on SIGTERM */ \
15 struct sigaction sigact; \
16 memset(&sigact, 0, sizeof(sigact)); \
17 /* sdbus-c++'s implementation doesn't mind if called before entering the event loop. It simply leaves the loop on entry */ \
18 sigact.sa_flags = SA_SIGINFO; \
19 sigact.sa_handler = [](int) { g_dbusConnection->leaveEventLoop(); }; \
20 sigaction(SIGTERM, &sigact, nullptr); \
Tomáš Pecka33563f52021-01-25 14:31:41 +010021 g_eventLoop = std::thread([] { g_dbusConnection->enterEventLoop(); }); \
22
23#define DBUS_EVENTLOOP_END \
Jan Kundrátcbc7a0b2021-02-18 09:47:29 +010024 g_eventLoop.join();