diff --git a/src/bluez/main.cc b/src/bluez/main.cc index b25b42c..efc0e5d 100644 --- a/src/bluez/main.cc +++ b/src/bluez/main.cc @@ -12,7 +12,8 @@ // See the License for the specific language governing permissions and // limitations under the License. -#include "../utils/signal_handler.h" +#include "../utils/event_loop.h" +#include "../utils/signal_source.h" #include "bluez_client.h" int main() { @@ -20,29 +21,22 @@ int main() { // Initialize logging with environment variable configuration logging_config::initializeLogging("bluez_client"); - installSignalHandlers(); + // Single-threaded loop drives the D-Bus connection and signal delivery. + // Construct the SignalSource before any thread starts (e.g. spdlog's + // flush thread) so SIGINT/SIGTERM are blocked and delivered via the loop. + EventLoop loop; + SignalSource signals(loop); + loop.add(&signals); const auto connection = sdbus::createSystemBusConnection(); - connection->enterEventLoopAsync(); BluezClient client(*connection); LOG_INFO("BlueZ client running - Press Ctrl+C to exit"); - // Monitor loop with shared connection health timing defaults - auto result = monitorLoop(*connection); - - if (result) { - // Connection was lost - LOG_ERROR("Exiting due to: {}", *result); - } else { - // Graceful shutdown via signal - LOG_INFO("Shutting down..."); - } - - connection->leaveEventLoop(); - - return result ? 1 : 0; + const int rc = loop.run(*connection); + LOG_INFO("Shutting down..."); + return rc; } catch (const sdbus::Error& e) { LOG_ERROR("D-Bus error: {} - {}", e.getName(), e.getMessage()); diff --git a/src/geoclue2/main.cc b/src/geoclue2/main.cc index 4db855b..b898c13 100644 --- a/src/geoclue2/main.cc +++ b/src/geoclue2/main.cc @@ -12,17 +12,20 @@ // See the License for the specific language governing permissions and // limitations under the License. -#include - -#include "../utils/signal_handler.h" +#include "../utils/event_loop.h" +#include "../utils/signal_source.h" #include "geoclue2_manager.h" int main() { try { - installSignalHandlers(); + // Single-threaded loop drives the D-Bus connection and signal delivery. + // Construct the SignalSource before any thread starts so SIGINT/SIGTERM are + // blocked and delivered via the loop. + EventLoop loop; + SignalSource signals(loop); + loop.add(&signals); const auto connection = sdbus::createSystemBusConnection(); - connection->enterEventLoopAsync(); const GeoClue2Manager manager( *connection, [&](const GeoClue2Location& location) { @@ -40,7 +43,6 @@ int main() { const auto& client = manager.Client(); if (!client) { LOG_ERROR("GeoClue2 did not return a client object; cannot continue"); - connection->leaveEventLoop(); return 1; } @@ -50,16 +52,9 @@ int main() { LOG_INFO("Geoclue2 monitor daemon running - Press Ctrl+C to exit"); - auto result = monitorLoop(*connection); - - if (result) { - LOG_ERROR("Exiting due to: {}", *result); - } else { - LOG_INFO("Shutting down..."); - } - - connection->leaveEventLoop(); - return result ? 1 : 0; + const int rc = loop.run(*connection); + LOG_INFO("Shutting down..."); + return rc; } catch (const sdbus::Error& e) { LOG_ERROR("D-Bus error: {} - {}", e.getName(), e.getMessage()); diff --git a/src/login1/main.cc b/src/login1/main.cc index 8eba922..b4ba059 100644 --- a/src/login1/main.cc +++ b/src/login1/main.cc @@ -12,31 +12,28 @@ // See the License for the specific language governing permissions and // limitations under the License. -#include "../utils/signal_handler.h" +#include "../utils/event_loop.h" +#include "../utils/signal_source.h" #include "login1_manager_client.h" int main() { try { - installSignalHandlers(); + // Single-threaded loop drives the D-Bus connection and signal delivery. + // Construct the SignalSource before any thread starts (e.g. spdlog's + // flush thread) so SIGINT/SIGTERM are blocked and delivered via the loop. + EventLoop loop; + SignalSource signals(loop); + loop.add(&signals); const auto connection = sdbus::createSystemBusConnection(); - connection->enterEventLoopAsync(); Login1ManagerClient client(*connection); LOG_INFO("Login1 client running - Press Ctrl+C to exit"); - // Monitor loop with shared connection health timing defaults - auto result = monitorLoop(*connection); - - if (result) { - LOG_ERROR("Exiting due to: {}", *result); - } else { - LOG_INFO("Shutting down..."); - } - - connection->leaveEventLoop(); - return result ? 1 : 0; + const int rc = loop.run(*connection); + LOG_INFO("Shutting down..."); + return rc; } catch (const sdbus::Error& e) { LOG_ERROR("D-Bus error: {} - {}", e.getName(), e.getMessage()); diff --git a/src/network1/main.cc b/src/network1/main.cc index 47533c5..ac1bb9c 100644 --- a/src/network1/main.cc +++ b/src/network1/main.cc @@ -1,29 +1,25 @@ -#include "../utils/signal_handler.h" +#include "../utils/event_loop.h" +#include "../utils/signal_source.h" #include "network1_client.h" -#include - int main() { try { - installSignalHandlers(); + // Single-threaded loop drives the D-Bus connection and signal delivery. + // Construct the SignalSource before any thread starts (e.g. spdlog's + // flush thread) so SIGINT/SIGTERM are blocked and delivered via the loop. + EventLoop loop; + SignalSource signals(loop); + loop.add(&signals); const auto connection = sdbus::createSystemBusConnection(); - connection->enterEventLoopAsync(); Network1ManagerClient network_manager(*connection); LOG_INFO("network1 monitor daemon running - Press Ctrl+C to exit"); - auto result = monitorLoop(*connection); - - if (result) { - LOG_ERROR("Exiting due to: {}", *result); - } else { - LOG_INFO("Shutting down..."); - } - - connection->leaveEventLoop(); - return result ? 1 : 0; + const int rc = loop.run(*connection); + LOG_INFO("Shutting down..."); + return rc; } catch (const sdbus::Error& e) { LOG_ERROR("D-Bus error: {} - {}", e.getName(), e.getMessage()); diff --git a/src/networkmanager/main.cc b/src/networkmanager/main.cc index d257a34..5dfd760 100644 --- a/src/networkmanager/main.cc +++ b/src/networkmanager/main.cc @@ -12,30 +12,28 @@ // See the License for the specific language governing permissions and // limitations under the License. -#include "../utils/signal_handler.h" +#include "../utils/event_loop.h" +#include "../utils/signal_source.h" #include "networkmanager_client.h" int main() { try { - installSignalHandlers(); + // Single-threaded loop drives the D-Bus connection and signal delivery. + // Construct the SignalSource before any thread starts (e.g. spdlog's + // flush thread) so SIGINT/SIGTERM are blocked and delivered via the loop. + EventLoop loop; + SignalSource signals(loop); + loop.add(&signals); const auto connection = sdbus::createSystemBusConnection(); - connection->enterEventLoopAsync(); NetworkManagerClient client(*connection); LOG_INFO("NetworkManager monitor daemon running - Press Ctrl+C to exit"); - auto result = monitorLoop(*connection); - - if (result) { - LOG_ERROR("Exiting due to: {}", *result); - } else { - LOG_INFO("Shutting down..."); - } - - connection->leaveEventLoop(); - return result ? 1 : 0; + const int rc = loop.run(*connection); + LOG_INFO("Shutting down..."); + return rc; } catch (const sdbus::Error& e) { LOG_ERROR("D-Bus error: {} - {}", e.getName(), e.getMessage()); diff --git a/src/resolve1/main.cc b/src/resolve1/main.cc index d56f5e7..002e003 100644 --- a/src/resolve1/main.cc +++ b/src/resolve1/main.cc @@ -12,31 +12,28 @@ // See the License for the specific language governing permissions and // limitations under the License. -#include "../utils/signal_handler.h" +#include "../utils/event_loop.h" +#include "../utils/signal_source.h" #include "resolve1_manager.h" int main() { try { - installSignalHandlers(); + // Single-threaded loop drives the D-Bus connection and signal delivery. + // Construct the SignalSource before any thread starts (e.g. spdlog's + // flush thread) so SIGINT/SIGTERM are blocked and delivered via the loop. + EventLoop loop; + SignalSource signals(loop); + loop.add(&signals); const auto connection = sdbus::createSystemBusConnection(); - connection->enterEventLoopAsync(); Resolve1Manager manager(*connection); LOG_INFO("Resolved client running - Press Ctrl+C to exit"); - // Monitor loop with shared connection health timing defaults - auto result = monitorLoop(*connection); - - if (result) { - LOG_ERROR("Exiting due to: {}", *result); - } else { - LOG_INFO("Shutting down..."); - } - - connection->leaveEventLoop(); - return result ? 1 : 0; + const int rc = loop.run(*connection); + LOG_INFO("Shutting down..."); + return rc; } catch (const sdbus::Error& e) { LOG_ERROR("D-Bus error: {} - {}", e.getName(), e.getMessage()); diff --git a/src/systemd1/main.cc b/src/systemd1/main.cc index f15fa5a..71b2db9 100644 --- a/src/systemd1/main.cc +++ b/src/systemd1/main.cc @@ -1,29 +1,26 @@ -#include -#include "../utils/signal_handler.h" +#include "../utils/event_loop.h" +#include "../utils/signal_source.h" #include "systemd1_manager_client.h" int main() { try { - installSignalHandlers(); + // Single-threaded loop drives the D-Bus connection and signal delivery. + // Construct the SignalSource before any thread starts (e.g. spdlog's + // flush thread) so SIGINT/SIGTERM are blocked and delivered via the loop. + EventLoop loop; + SignalSource signals(loop); + loop.add(&signals); const auto connection = sdbus::createSystemBusConnection(); - connection->enterEventLoopAsync(); Systemd1ManagerClient manager(*connection); LOG_INFO("Systemd1 monitor daemon running - Press Ctrl+C to exit"); - auto result = monitorLoop(*connection); - - if (result) { - LOG_ERROR("Exiting due to: {}", *result); - } else { - LOG_INFO("Shutting down..."); - } - - connection->leaveEventLoop(); - return result ? 1 : 0; + const int rc = loop.run(*connection); + LOG_INFO("Shutting down..."); + return rc; } catch (const sdbus::Error& e) { LOG_ERROR("D-Bus error: {} - {}", e.getName(), e.getMessage()); diff --git a/src/udisks2/main.cc b/src/udisks2/main.cc index d896e44..9a79e96 100644 --- a/src/udisks2/main.cc +++ b/src/udisks2/main.cc @@ -12,31 +12,28 @@ // See the License for the specific language governing permissions and // limitations under the License. -#include "../utils/signal_handler.h" +#include "../utils/event_loop.h" +#include "../utils/signal_source.h" #include "udisks2_manager.h" int main() { try { - installSignalHandlers(); + // Single-threaded loop drives the D-Bus connection and signal delivery. + // Construct the SignalSource before any thread starts (e.g. spdlog's + // flush thread) so SIGINT/SIGTERM are blocked and delivered via the loop. + EventLoop loop; + SignalSource signals(loop); + loop.add(&signals); const auto connection = sdbus::createSystemBusConnection(); - connection->enterEventLoopAsync(); UDisks2Manager manager(*connection); LOG_INFO("UDisks2 client running - Press Ctrl+C to exit"); - // Monitor loop with shared connection health timing defaults - auto result = monitorLoop(*connection); - - if (result) { - LOG_ERROR("Exiting due to: {}", *result); - } else { - LOG_INFO("Shutting down..."); - } - - connection->leaveEventLoop(); - return result ? 1 : 0; + const int rc = loop.run(*connection); + LOG_INFO("Shutting down..."); + return rc; } catch (const sdbus::Error& e) { LOG_ERROR("D-Bus error: {} - {}", e.getName(), e.getMessage()); diff --git a/src/udisks2/udisks2_monitor_daemon.cc b/src/udisks2/udisks2_monitor_daemon.cc index b57d34c..f8adc79 100644 --- a/src/udisks2/udisks2_monitor_daemon.cc +++ b/src/udisks2/udisks2_monitor_daemon.cc @@ -16,24 +16,22 @@ #include #include -#include #include #include #include #include #include #include -#include #include #include #include -#include #include #include #include "../proxy/org/freedesktop/UDisks2/Block/block_proxy.h" +#include "../utils/event_loop.h" #include "../utils/logging.h" -#include "../utils/signal_handler.h" +#include "../utils/signal_source.h" // Forward declarations class RemovableDeviceMonitor; @@ -505,30 +503,25 @@ int main() { LOG_INFO("This daemon monitors for removable device insertions"); LOG_INFO("Press Ctrl+C to stop...\n"); + // Single-threaded loop drives the D-Bus connection and signal delivery. + // Construct the SignalSource before any thread starts (e.g. spdlog's + // flush thread) so SIGINT/SIGTERM are blocked and delivered via the loop. + EventLoop loop; + SignalSource signals(loop); + loop.add(&signals); + // Create the system bus connection const auto connection = sdbus::createSystemBusConnection(); - // Enter the event loop asynchronously - connection->enterEventLoopAsync(); - // Create the monitor with the default callback // Users can replace printDeviceInformation with their own lambda RemovableDeviceMonitor monitor(*connection, printDeviceInformation); - // Keep running until interrupted by signal (e.g., Ctrl+C) - // The event loop runs in a separate thread handling D-Bus messages - // Note: In a production daemon, implement proper signal handling (SIGTERM, - // SIGINT) to gracefully shut down and call connection->leaveEventLoop() - installSignalHandlers(); - auto result = monitorLoop(*connection); - if (result) { - LOG_ERROR("Monitor loop: {}", *result); - } - connection->leaveEventLoop(); + const int rc = loop.run(*connection); + LOG_INFO("Shutting down..."); + return rc; } catch (const std::exception& e) { LOG_ERROR("Fatal error: {}", e.what()); return 1; } - - return 0; } \ No newline at end of file diff --git a/src/upower/main.cc b/src/upower/main.cc index c201086..794f2e9 100644 --- a/src/upower/main.cc +++ b/src/upower/main.cc @@ -12,15 +12,20 @@ // See the License for the specific language governing permissions and // limitations under the License. -#include "../utils/signal_handler.h" +#include "../utils/event_loop.h" +#include "../utils/signal_source.h" #include "upower_client.h" int main() { try { - installSignalHandlers(); + // Single-threaded loop drives the D-Bus connection and signal delivery. + // Construct the SignalSource before any thread starts (e.g. spdlog's + // flush thread) so SIGINT/SIGTERM are blocked and delivered via the loop. + EventLoop loop; + SignalSource signals(loop); + loop.add(&signals); const auto connection = sdbus::createSystemBusConnection(); - connection->enterEventLoopAsync(); UPowerClient client( *connection, @@ -28,16 +33,9 @@ int main() { LOG_INFO("UPower monitor daemon running - Press Ctrl+C to exit"); - auto result = monitorLoop(*connection); - - if (result) { - LOG_ERROR("Exiting due to: {}", *result); - } else { - LOG_INFO("Shutting down..."); - } - - connection->leaveEventLoop(); - return result ? 1 : 0; + const int rc = loop.run(*connection); + LOG_INFO("Shutting down..."); + return rc; } catch (const sdbus::Error& e) { LOG_ERROR("D-Bus error: {} - {}", e.getName(), e.getMessage()); diff --git a/src/wpa_supplicant/main.cc b/src/wpa_supplicant/main.cc index ec5ce09..afa9fbc 100644 --- a/src/wpa_supplicant/main.cc +++ b/src/wpa_supplicant/main.cc @@ -1,29 +1,25 @@ -#include "../utils/signal_handler.h" +#include "../utils/event_loop.h" +#include "../utils/signal_source.h" #include "wpa_supplicant1_client.h" -#include - int main() { try { - installSignalHandlers(); + // Single-threaded loop drives the D-Bus connection and signal delivery. + // Construct the SignalSource before any thread starts (e.g. spdlog's + // flush thread) so SIGINT/SIGTERM are blocked and delivered via the loop. + EventLoop loop; + SignalSource signals(loop); + loop.add(&signals); const auto connection = sdbus::createSystemBusConnection(); - connection->enterEventLoopAsync(); WpaSupplicant1Client client(*connection); LOG_INFO("wpa_supplicant client running - Press Ctrl+C to exit"); - auto result = monitorLoop(*connection); - - if (result) { - LOG_ERROR("Exiting due to: {}", *result); - } else { - LOG_INFO("Shutting down..."); - } - - connection->leaveEventLoop(); - return result ? 1 : 0; + const int rc = loop.run(*connection); + LOG_INFO("Shutting down..."); + return rc; } catch (const sdbus::Error& e) { LOG_ERROR("D-Bus error: {} - {}", e.getName(), e.getMessage());