-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSignal.cpp
More file actions
83 lines (73 loc) · 1.63 KB
/
Signal.cpp
File metadata and controls
83 lines (73 loc) · 1.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#include "Signal.hpp"
#include "ErrorCode.hpp"
#include <signal.h>
using namespace Elucidate::Sys;
using namespace Elucidate::Util;
namespace {
void maskAllSignals(sigset_t& old) {
sigset_t target;
checkReturnAndThrow(0, sigemptyset(&target), "sigemptyset");
checkReturnAndThrow(0,
pthread_sigmask(SIG_SETMASK, &target, &old), "pthread_sigmask");
}
void restoreSignals(sigset_t& old) {
checkReturnAndThrow(0,
pthread_sigmask(SIG_SETMASK, &old, 0), "pthread_sigmask");
}
}
void Elucidate::Sys::processSignal() {
sigset_t set;
Util::checkReturnAndThrow(0, sigfillset(&set), "sigfillset");
siginfo_t info;
timespec timeout{0/*s*/, 10000000/*ns*/};
auto s = sigtimedwait(&set, &info, &timeout);
switch (s) {
case -1: { // errors
switch (errno) {
case EAGAIN:
default:
break;
case EINVAL:
TRACED_TERMINATE("invalid timeout");
}
} break;
case SIGINT:
case SIGTERM:
LOG_FATAL << "SIGINT/TERM";
exit(-1);
break;
case SIGQUIT:
TRACED_TERMINATE("SIGQUIT");
break;
case SIGCHLD:
case SIGALRM:
case SIGVTALRM:
case SIGPROF:
case SIGCONT:
case SIGHUP:
case SIGPIPE:
case SIGPOLL:
case SIGSTOP:
case SIGTSTP:
default:
LOG_INFO << "continuing after signal " << s;
break;
}
}
SignalHandler::SignalHandler()
: shutdownSignal_{0}
, stopper_(shutdownSignal_)
, signalThread_(stopper_, Sys::processSignal)
, oldSet_{}
{
// now there is a thread to deal with signals, mask them all
// child threads inherit
maskAllSignals(oldSet_);
}
SignalHandler::~SignalHandler()
{
// remove the mask before stopping the thread
restoreSignals(oldSet_);
shutdownSignal_.store(1, std::memory_order_relaxed);
signalThread_.join();
}