-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
handle SIGINT and SIGTERM when pulling, and improve formatting of out…
…put (#25)
- Loading branch information
Showing
10 changed files
with
163 additions
and
172 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
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
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
This file was deleted.
Oops, something went wrong.
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,37 @@ | ||
#include <atomic> | ||
#include <csignal> | ||
|
||
#include <util/signal.h> | ||
|
||
namespace util { | ||
|
||
std::atomic<bool> signal_received{false}; | ||
std::atomic<int> last_signal{0}; | ||
|
||
void signal_handler(int signal) { | ||
if (signal == SIGINT || signal == SIGTERM) { | ||
signal_received.store(true); | ||
last_signal.store(signal); | ||
} | ||
} | ||
|
||
void set_signal_catcher() { | ||
signal_received.store(false); | ||
struct sigaction h; | ||
|
||
h.sa_handler = signal_handler; | ||
sigemptyset(&h.sa_mask); | ||
h.sa_flags = SA_RESETHAND; | ||
sigaction(SIGINT, &h, nullptr); | ||
sigaction(SIGTERM, &h, nullptr); | ||
} | ||
|
||
bool signal_raised() { | ||
return signal_received.exchange(false); | ||
} | ||
|
||
int last_signal_raised() { | ||
return last_signal.load(); | ||
} | ||
|
||
} // namespace util |
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,25 @@ | ||
#pragma once | ||
|
||
#include <exception> | ||
|
||
#include <fmt/format.h> | ||
|
||
namespace util { | ||
|
||
class signal_exception : public std::exception { | ||
public: | ||
const int signal; | ||
const std::string msg; | ||
signal_exception(int signal) | ||
: signal(signal), msg(fmt::format("signal {} raised", signal)) { | ||
} | ||
const char* what() const throw() { | ||
return msg.c_str(); | ||
} | ||
}; | ||
|
||
void set_signal_catcher(); | ||
bool signal_raised(); | ||
int last_signal_raised(); | ||
|
||
} // namespace util |
Oops, something went wrong.