Skip to content

Commit

Permalink
add interrupt handling (#12)
Browse files Browse the repository at this point in the history
  • Loading branch information
jcosentino11 authored Jun 7, 2024
1 parent fa72c07 commit caadf6f
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 0 deletions.
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

0 comments on commit caadf6f

Please sign in to comment.