Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add interrupt handling #12

Merged
merged 1 commit into from
Jun 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions include/Interrupt.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#pragma once
#include <atomic>

namespace MqttClient {
extern std::atomic<bool> interrupted;
void onInterrupt(int ignored);
} // namespace MqttClient
9 changes: 9 additions & 0 deletions src/Interrupt.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#include "Interrupt.h"
#include <atomic>

namespace MqttClient {

std::atomic<bool> interrupted(false);

void onInterrupt(int ignored) { interrupted.store(true); }
} // namespace MqttClient
11 changes: 11 additions & 0 deletions src/main.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#include "Command.h"
#include "Context.h"
#include "Interrupt.h"
#include <csignal>
#include <getopt.h>
#include <iostream>
#include <memory>
Expand All @@ -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:
Expand Down