Skip to content

Commit

Permalink
Merge pull request #574 from nomis52/master
Browse files Browse the repository at this point in the history
Clean up signal handling a bit.
  • Loading branch information
nomis52 committed Dec 12, 2014
2 parents 2779984 + 441b847 commit 1d2c766
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 74 deletions.
45 changes: 12 additions & 33 deletions common/base/Init.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
#include <execinfo.h>
#endif

#include <errno.h>
#include <fcntl.h>
#include <signal.h>
#include <stdlib.h>
Expand Down Expand Up @@ -74,13 +75,12 @@ using std::cout;
using std::endl;
using std::string;

#if HAVE_DECL_RLIMIT_RTTIME
/*
* @private
* @brief Print a stack trace if we exceed CPU time.
* @brief Print a stack trace.
*/
static void _SIGXCPU_Handler(OLA_UNUSED int signal) {
cout << "Received SIGXCPU" << endl;
static void _DumpStackAndExit(int sig) {
cout << "Received " << strsignal(sig) << endl;
#ifdef HAVE_EXECINFO_H
enum {STACK_SIZE = 64};
void *array[STACK_SIZE];
Expand All @@ -90,7 +90,6 @@ static void _SIGXCPU_Handler(OLA_UNUSED int signal) {
#endif
exit(ola::EXIT_SOFTWARE);
}
#endif

bool SetThreadScheduling() {
string policy_str = FLAGS_scheduler_policy.str();
Expand Down Expand Up @@ -163,8 +162,7 @@ bool SetThreadScheduling() {
return false;
}

if (!ola::InstallSignal(SIGXCPU, _SIGXCPU_Handler)) {
OLA_WARN << "Failed to install signal SIGXCPU";
if (!ola::InstallSignal(SIGXCPU, _DumpStackAndExit)) {
return false;
}
#endif
Expand All @@ -183,22 +181,6 @@ using std::cout;
using std::endl;
using std::string;

/**
* @private
* Print a stack trace on seg fault.
*/
static void _SIGSEGV_Handler(OLA_UNUSED int signal) {
cout << "Received SIGSEGV or SIGBUS" << endl;
#ifdef HAVE_EXECINFO_H
enum {STACK_SIZE = 64};
void *array[STACK_SIZE];
size_t size = backtrace(array, STACK_SIZE);

backtrace_symbols_fd(array, size, STDERR_FILENO);
#endif
exit(EXIT_SOFTWARE);
}

bool ServerInit(int argc, char *argv[], ExportMap *export_map) {
ola::math::InitRandom();
if (!InstallSEGVHandler())
Expand All @@ -209,7 +191,6 @@ bool ServerInit(int argc, char *argv[], ExportMap *export_map) {
return SetThreadScheduling() && NetworkInit();
}


bool ServerInit(int *argc,
char *argv[],
ExportMap *export_map,
Expand Down Expand Up @@ -263,10 +244,10 @@ bool NetworkInit() {
#endif
}

bool InstallSignal(int signal, void(*fp)(int signo)) {
bool InstallSignal(int sig, void(*fp)(int signo)) {
#ifdef _WIN32
if (::signal(signal, fp) == SIG_ERR) {
OLA_WARN << "Failed to install signal for " << signal;
if (::signal(sig, fp) == SIG_ERR) {
OLA_WARN << "signal(" << strsignal(sig) << ": " << strerror(errno);
return false;
}
#else
Expand All @@ -275,8 +256,8 @@ bool InstallSignal(int signal, void(*fp)(int signo)) {
sigemptyset(&action.sa_mask);
action.sa_flags = 0;

if (sigaction(signal, &action, NULL) < 0) {
OLA_WARN << "Failed to install signal for " << signal;
if (sigaction(sig, &action, NULL) < 0) {
OLA_WARN << "sigaction(" << strsignal(sig) << ": " << strerror(errno);
return false;
}
#endif // _WIN32
Expand All @@ -285,13 +266,11 @@ bool InstallSignal(int signal, void(*fp)(int signo)) {

bool InstallSEGVHandler() {
#ifndef _WIN32
if (!InstallSignal(SIGBUS, _SIGSEGV_Handler)) {
OLA_WARN << "Failed to install signal SIGBUS";
if (!InstallSignal(SIGBUS, _DumpStackAndExit)) {
return false;
}
#endif // !_WIN32
if (!InstallSignal(SIGSEGV, _SIGSEGV_Handler)) {
OLA_WARN << "Failed to install signal SIGSEGV";
if (!InstallSignal(SIGSEGV, _DumpStackAndExit)) {
return false;
}
return true;
Expand Down
8 changes: 5 additions & 3 deletions examples/ola-uni-stats.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -234,10 +234,12 @@ void UniverseTracker::RegisterComplete(const string &error) {

SelectServer *ss = NULL;

static void InteruptSignal(int unused) {
if (ss)
static void InteruptSignal(OLA_UNUSED int signo) {
int old_errno = errno;
if (ss) {
ss->Terminate();
(void) unused;
}
errno = old_errno;
}


Expand Down
9 changes: 6 additions & 3 deletions tools/e133/basic-controller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
* I'm using this for scale testing.
*/

#include <errno.h>
#include <ola/Callback.h>
#include <ola/Clock.h>
#include <ola/Constants.h>
Expand Down Expand Up @@ -293,10 +294,12 @@ void SimpleE133Controller::SocketClosed(IPV4SocketAddress peer) {
/**
* Interupt handler
*/
static void InteruptSignal(int unused) {
if (controller)
static void InteruptSignal(OLA_UNUSED int signo) {
int old_errno = errno;
if (controller) {
controller->Stop();
(void) unused;
}
errno = old_errno;
}

int main(int argc, char *argv[]) {
Expand Down
9 changes: 6 additions & 3 deletions tools/e133/basic-device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
* I'm using this for scale testing.
*/

#include <errno.h>
#include <ola/Callback.h>
#include <ola/Clock.h>
#include <ola/Constants.h>
Expand Down Expand Up @@ -185,10 +186,12 @@ SimpleE133Device *device = NULL;
/**
* Interupt handler
*/
static void InteruptSignal(int unused) {
if (device)
static void InteruptSignal(OLA_UNUSED int signo) {
int old_errno = errno;
if (device) {
device->Stop();
(void) unused;
}
errno = old_errno;
}

int main(int argc, char *argv[]) {
Expand Down
9 changes: 6 additions & 3 deletions tools/e133/e133-receiver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include <config.h>
#endif

#include <errno.h>
#include <signal.h>

#include <ola/acn/ACNPort.h>
Expand Down Expand Up @@ -83,10 +84,12 @@ SimpleE133Node *simple_node;
/*
* Terminate cleanly on interrupt.
*/
static void InteruptSignal(int signo) {
if (simple_node)
static void InteruptSignal(OLA_UNUSED int signo) {
int old_errno = errno;
if (simple_node) {
simple_node->Stop();
(void) signo;
}
errno = old_errno;
}

void HandleTriDMX(DmxBuffer *buffer, DmxTriWidget *widget) {
Expand Down
37 changes: 8 additions & 29 deletions tools/ola_trigger/ola-trigger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,11 @@ typedef vector<Slot*> SlotList;
#ifndef _WIN32
static void CatchSIGCHLD(OLA_UNUSED int signo) {
pid_t pid;
int old_errno = errno;
do {
pid = waitpid(-1, NULL, WNOHANG);
} while (pid > 0);
errno = old_errno;
}
#endif

Expand All @@ -91,49 +93,26 @@ static void CatchSIGCHLD(OLA_UNUSED int signo) {
static void CatchSIGINT(OLA_UNUSED int signo) {
// there is a race condition here if you send the signal before we call Run()
// it's not a huge deal though.
int old_errno = errno;
if (ss) {
ss->Terminate();
}
errno = old_errno;
}


/*
* Install the SIGCHLD handler.
*/
bool InstallSignals() {
#ifdef WIN32
#ifndef WIN32
// There's no SIGCHILD on Windows
if (signal(SIGINT, CatchSIGINT) == reinterpret_cast<void(*)(int)>(EINVAL)) {
OLA_WARN << "Failed to install signal SIGINT";
return false;
}
if (signal(SIGTERM, CatchSIGINT) == reinterpret_cast<void(*)(int)>(EINVAL)) {
OLA_WARN << "Failed to install signal SIGTERM";
return false;
}
#else
struct sigaction act, oact;

act.sa_handler = CatchSIGCHLD;
sigemptyset(&act.sa_mask);
act.sa_flags = 0;

if (sigaction(SIGCHLD, &act, &oact) < 0) {
OLA_WARN << "Failed to install signal SIGCHLD";
return false;
}

act.sa_handler = CatchSIGINT;
if (sigaction(SIGINT, &act, &oact) < 0) {
OLA_WARN << "Failed to install signal SIGINT";
return false;
}
if (sigaction(SIGTERM, &act, &oact) < 0) {
OLA_WARN << "Failed to install signal SIGTERM";
if (!ola::InstallSignal(SIGCHLD, CatchSIGCHLD)) {
return false;
}
#endif
return true;
return ola::InstallSignal(SIGINT, CatchSIGINT) &&
ola::InstallSignal(SIGTERM, CatchSIGINT);
}


Expand Down

0 comments on commit 1d2c766

Please sign in to comment.