Tomáš Pecka | 33563f5 | 2021-01-25 14:31:41 +0100 | [diff] [blame] | 1 | #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áš Pecka | 33563f5 | 2021-01-25 14:31:41 +0100 | [diff] [blame] | 13 | 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áš Pecka | 33563f5 | 2021-01-25 14:31:41 +0100 | [diff] [blame] | 21 | g_eventLoop = std::thread([] { g_dbusConnection->enterEventLoop(); }); \ |
| 22 | |
| 23 | #define DBUS_EVENTLOOP_END \ |
Jan Kundrát | cbc7a0b | 2021-02-18 09:47:29 +0100 | [diff] [blame] | 24 | g_eventLoop.join(); |