33 #include <semaphore.h> 35 #include <boost/format.hpp> 48 receivedSignal = signal;
54 if (strerror_r(errorNumber, buffer, 256) != 0) {
55 throw std::runtime_error(boost::str(boost::format(
"Error %1% (could not determine error message: %2%)")
56 % errorNumber % errno));
58 throw std::runtime_error(boost::str(boost::format(
"%1%: %2%")
59 % description % buffer));
66 throw std::logic_error(
"At least one signal has to be specified.");
68 requestedSignals = signals;
71 if (sem_init(&semaphore, 0, 0) != 0) {
82 struct sigaction action;
84 if (sigemptyset(&action.sa_mask) != 0) {
87 action.sa_flags = SA_RESETHAND;
89 if (sigaction(SIGINT, &action, 0) != 0) {
94 if (sigaction(SIGTERM, &action, 0) != 0) {
99 if (sigaction(SIGQUIT, &action, 0) != 0) {
107 if (receivedSignal == 0) {
109 }
else if (receivedSignal == SIGINT) {
111 }
else if (receivedSignal == SIGTERM) {
113 }
else if (receivedSignal == SIGQUIT) {
116 throw std::runtime_error(
118 boost::format(
"unexpected signal number %1%")
119 % (
int)receivedSignal));
125 if (requestedSignals == 0) {
126 throw std::logic_error(
"initSignalWaiter has to be called before" 134 while ((-1 == (result = sem_wait(&semaphore))) && (errno == EINTR));
144 assert(receivedSignal != 0);
149 if (requestedSignals == 0) {
150 throw std::logic_error(
"initSignalWaiter has to be called before" 151 " hasSignalArrived.");
167 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.