-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Split signal handler process in dedicated file
- Loading branch information
Showing
3 changed files
with
53 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
#pragma once | ||
|
||
namespace cct { | ||
|
||
// Tells whether a stop signal has been requested through a signal (Ctrl+C for instance) | ||
bool IsStopRequested(); | ||
|
||
} // namespace cct |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
#include "signal-handler.hpp" | ||
|
||
#include <csignal> | ||
|
||
#include "cct_log.hpp" | ||
|
||
namespace { | ||
|
||
extern "C" void SignalHandler(int sigNum); | ||
|
||
sig_atomic_t InitializeSignalStatus() { | ||
// Register the signal handler to gracefully shutdown the main loop for repeated requests. | ||
std::signal(SIGINT, SignalHandler); | ||
std::signal(SIGTERM, SignalHandler); | ||
std::signal(SIGQUIT, SignalHandler); | ||
|
||
return 0; | ||
} | ||
|
||
volatile sig_atomic_t g_signalStatus = InitializeSignalStatus(); | ||
|
||
} // namespace | ||
|
||
// According to the standard, 'SignalHandler' function should have C linkage: | ||
// https://en.cppreference.com/w/cpp/utility/program/signal | ||
// Thus it's not possible to use a lambda and pass some | ||
// objects to it. This is why for this rare occasion we will rely on a static variable. This solution has been inspired | ||
// by: https://wiki.sei.cmu.edu/confluence/display/cplusplus/MSC54-CPP.+A+signal+handler+must+be+a+plain+old+function | ||
extern "C" void SignalHandler(int sigNum) { | ||
cct::log::warn("Signal {} received, will stop after current command", sigNum); | ||
|
||
g_signalStatus = sigNum; | ||
|
||
// Revert to standard signal handler (to allow for standard kill in case program does not react) | ||
std::signal(sigNum, SIG_DFL); | ||
} | ||
|
||
namespace cct { | ||
|
||
bool IsStopRequested() { return g_signalStatus != 0; } | ||
|
||
} // namespace cct |