33 #include <semaphore.h> 35 #include <boost/format.hpp> 48 receivedSignal = signal;
54 strerror_r(errorNumber, buffer, 256);
55 throw std::runtime_error(boost::str(boost::format(
"%1%: %2%")
56 % description % buffer));
63 throw std::logic_error(
"At least one signal has to be specified.");
65 requestedSignals = signals;
68 if (sem_init(&semaphore, 0, 0) != 0) {
79 struct sigaction action;
81 if (sigemptyset(&action.sa_mask) != 0) {
84 action.sa_flags = SA_RESETHAND;
86 if (sigaction(SIGINT, &action, 0) != 0) {
91 if (sigaction(SIGTERM, &action, 0) != 0) {
96 if (sigaction(SIGQUIT, &action, 0) != 0) {
104 if (receivedSignal == 0) {
106 }
else if (receivedSignal == SIGINT) {
108 }
else if (receivedSignal == SIGTERM) {
110 }
else if (receivedSignal == SIGQUIT) {
113 throw std::runtime_error(
115 boost::format(
"unexpected signal number %1%")
116 % (
int)receivedSignal));
122 if (requestedSignals == 0) {
123 throw std::logic_error(
"initSignalWaiter has to be called before" 131 while ((-1 == (result = sem_wait(&semaphore))) && (errno == EINTR));
141 assert(receivedSignal != 0);
146 if (requestedSignals == 0) {
147 throw std::logic_error(
"initSignalWaiter has to be called before" 148 " hasSignalArrived.");
164 throw std::logic_error(
"Invalid signal number");
void initSignalWaiter(int signals)
Prepare waiting for signals.
Signal waitForSignal()
Block until one of the signals specified in initSignalWaiter arrives, then return the signal...
Signal
Signals waitForSignal can wait for.
Artificial enum entry to indicate that no signal is meant at all.
Signal lastArrivedSignal()
Returns the last signal that has arrived at this process or Signal::NO_SIGNAL in case no signal arriv...
Interrupting the program has be requested.
int suggestedExitCode(Signal signal)
Return suggested exit code for exiting the program after receiving signal signal. ...
Quitting the program has be requested.
void throwRuntimeError(int errorNumber, const std::string &description)
volatile sig_atomic_t receivedSignal
Terminating the program has be requested.