From 0a84dc95fd46ae4d331d76f76bad7cb411a68027 Mon Sep 17 00:00:00 2001 From: Joseph Cosentino Date: Thu, 6 Jun 2024 22:47:43 -0700 Subject: [PATCH] add interrupt handling --- include/Interrupt.h | 7 +++++++ src/Interrupt.cpp | 9 +++++++++ src/main.cpp | 11 +++++++++++ 3 files changed, 27 insertions(+) create mode 100644 include/Interrupt.h create mode 100644 src/Interrupt.cpp diff --git a/include/Interrupt.h b/include/Interrupt.h new file mode 100644 index 0000000..42f7b9e --- /dev/null +++ b/include/Interrupt.h @@ -0,0 +1,7 @@ +#pragma once +#include + +namespace MqttClient { +extern std::atomic interrupted; +void onInterrupt(int ignored); +} // namespace MqttClient diff --git a/src/Interrupt.cpp b/src/Interrupt.cpp new file mode 100644 index 0000000..aff8985 --- /dev/null +++ b/src/Interrupt.cpp @@ -0,0 +1,9 @@ +#include "Interrupt.h" +#include + +namespace MqttClient { + +std::atomic interrupted(false); + +void onInterrupt(int ignored) { interrupted.store(true); } +} // namespace MqttClient diff --git a/src/main.cpp b/src/main.cpp index 66ff56d..03952eb 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,5 +1,7 @@ #include "Command.h" #include "Context.h" +#include "Interrupt.h" +#include #include #include #include @@ -8,6 +10,15 @@ using namespace MqttClient; int main(int argc, char *argv[]) { + if (signal(SIGINT, onInterrupt) == SIG_ERR) { + std::cerr << "Unable to register SIGINT handler\n"; + return 1; + } + if (signal(SIGTERM, onInterrupt) == SIG_ERR) { + std::cerr << "Unable to register SIGTERM handler\n"; + return 1; + } + auto res = parseContext(argc, argv); switch (res.code) { case ParseResultCode::HELP: