Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clean up signal handling a bit. #574

Merged
merged 3 commits into from
Dec 12, 2014
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this need wrapping in #if HAVE_DECL_RLIMIT_RTTIME?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nope, that was there because the function was only used if the signal was installed. Now that it's a common function we don't need it.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay.

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
6 changes: 3 additions & 3 deletions examples/ola-uni-stats.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -234,10 +234,10 @@ void UniverseTracker::RegisterComplete(const string &error) {

SelectServer *ss = NULL;

static void InteruptSignal(int unused) {
if (ss)
static void InteruptSignal(OLA_UNUSED int sig) {
if (ss) {
ss->Terminate();
(void) unused;
}
}


Expand Down
6 changes: 3 additions & 3 deletions tools/e133/basic-controller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -293,10 +293,10 @@ void SimpleE133Controller::SocketClosed(IPV4SocketAddress peer) {
/**
* Interupt handler
*/
static void InteruptSignal(int unused) {
if (controller)
static void InteruptSignal(OLA_UNUSED int unused) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Change to sig to be consistent.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

if (controller) {
controller->Stop();
(void) unused;
}
}

int main(int argc, char *argv[]) {
Expand Down
6 changes: 3 additions & 3 deletions tools/e133/basic-device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -185,10 +185,10 @@ SimpleE133Device *device = NULL;
/**
* Interupt handler
*/
static void InteruptSignal(int unused) {
if (device)
static void InteruptSignal(OLA_UNUSED int unused) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Again.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

if (device) {
device->Stop();
(void) unused;
}
}

int main(int argc, char *argv[]) {
Expand Down
6 changes: 3 additions & 3 deletions tools/e133/e133-receiver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,10 +83,10 @@ SimpleE133Node *simple_node;
/*
* Terminate cleanly on interrupt.
*/
static void InteruptSignal(int signo) {
if (simple_node)
static void InteruptSignal(OLA_UNUSED int signo) {
if (simple_node) {
simple_node->Stop();
(void) signo;
}
}

void HandleTriDMX(DmxBuffer *buffer, DmxTriWidget *widget) {
Expand Down
35 changes: 6 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 @@ -101,39 +103,14 @@ static void CatchSIGINT(OLA_UNUSED int signo) {
* 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