33 #include <semaphore.h> 35 #include <boost/format.hpp> 36 #include <boost/lexical_cast.hpp> 52 receivedSignal = signal;
58 strerror_r(errorNumber, buffer, 256);
59 throw std::runtime_error(boost::str(boost::format(
"%1%: %2%")
60 % description % buffer));
67 throw std::logic_error(
"At least one signal has to be specified.");
70 requestedSignals = signals;
75 if (SEM_FAILED == (semaphore = sem_open(semaphore_name.c_str(), O_CREAT | O_EXCL, 0644, 0))) {
86 struct sigaction action;
88 if (sigemptyset(&action.sa_mask) != 0) {
91 action.sa_flags = SA_RESETHAND;
93 if (sigaction(SIGINT, &action, 0) != 0) {
98 if (sigaction(SIGTERM, &action, 0) != 0) {
103 if (sigaction(SIGQUIT, &action, 0) != 0) {
111 if (receivedSignal == 0) {
113 }
else if (receivedSignal == SIGINT) {
115 }
else if (receivedSignal == SIGTERM) {
117 }
else if (receivedSignal == SIGQUIT) {
120 throw std::runtime_error(
122 boost::format(
"unexpected signal number %1%")
123 % (
int) receivedSignal));
129 if (requestedSignals == 0) {
130 throw std::logic_error(
"initSignalWaiter has to be called before" 138 while ((-1 == (result = sem_wait(semaphore))) && (errno == EINTR));
148 if (sem_unlink(semaphore_name.c_str()) == -1) {
151 if (sem_close(semaphore) == -1) {
155 assert(receivedSignal != 0);
160 if (requestedSignals == 0) {
161 throw std::logic_error(
"initSignalWaiter has to be called before" 162 " hasSignalArrived.");
178 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...
std::string randAlnumStr(const std::string::size_type &length)
Generates a random alpha-numeric string with fixed length.
Signal
Signals waitForSignal can wait for.
boost::uint64_t currentTimeMicros()
Returns the current system time as microseconds.
Artificial enum entry to indicate that no signal is meant at all.
static std::string semaphore_name
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.