Skip to content

Commit

Permalink
Merge branch 'refactor/signal_in_utils' of https://github.com/m-seker…
Browse files Browse the repository at this point in the history
…/hyperion.ng into refactor/signal_in_utils
  • Loading branch information
m-seker committed Jul 9, 2020
2 parents e73711e + 7f23ee3 commit bd13fff
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 62 deletions.
2 changes: 0 additions & 2 deletions include/utils/DefaultSignalHandler.h
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
#pragma once

#ifndef _WIN32
namespace DefaultSignalHandler
{
void install();
}
#endif // _WIN32
124 changes: 70 additions & 54 deletions libsrc/utils/DefaultSignalHandler.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#ifndef _WIN32

#include <utils/DefaultSignalHandler.h>
#include <utils/Logger.h>

Expand All @@ -16,6 +17,39 @@
namespace DefaultSignalHandler
{

std::string decipher_trace(const std::string &trace)
{
std::string result;

if(trace.empty())
{
result += "??\n";
return result;
}

auto* begin = strchr(trace.c_str(), '(') + 1;
auto* end = strchr(begin, '+');

if(!end)
end = strchr(begin, ')');

std::string mangled_name(begin, end);

int status;
char * realname = abi::__cxa_demangle(mangled_name.c_str(), 0, 0, &status);
result.insert(result.end(), trace.c_str(), begin);

if(realname)
result += realname;
else
result.insert(result.end(), begin, end);

free(realname);
result.insert(result.size(), end);

return result;
}

void print_trace()
{
const int MAX_SIZE = 50;
Expand All @@ -26,48 +60,20 @@ void print_trace()
return;

Logger* log = Logger::getInstance("CORE");
Error(log, "Backtrace :");

char ** symbols = backtrace_symbols(addresses, size);
for (int i = 0; i < size; ++i)
{
/* Examples :
* /opt/Qt/5.15.0/gcc_64/lib/libQt5Core.so.5(_ZN7QThread4execEv+0x84) [0x7f58ddcc0134]
* /opt/Qt/5.15.0/gcc_64/lib/libQt5Core.so.5(+0xb3415) [0x7f58ddcc1415]
*/

std::string result = "\t";
auto* begin = strchr(symbols[i], '(') + 1;
if(!symbols[i])
{
result += "??\n";
continue;
}
auto* end = strchr(begin, '+');
if(!end)
end = strchr(begin, ')');

std::string mangled_name(begin, end);

int status;
char * realname = abi::__cxa_demangle(mangled_name.c_str(), 0, 0, &status);
result.insert(result.end(), symbols[i], begin);

if(realname)
result += realname;
else
result.insert(result.end(), begin, end);

free(realname);
result.insert(result.size(), end);

Error(log, result.c_str());
}

free(symbols);
std::string line = "\t" + decipher_trace(symbols[i]);
Error(log, line.c_str());
}

free(symbols);
}

void signal_handler(const int signum)
/* Note that this signal handler is not async signal safe !
* Ideally a signal handler should only flip a bit and defer
* heavy work to some kind of bottom-half processing. */
void signal_handler(int signum, siginfo_t * /*info*/, void * /*context*/)
{
Logger* log = Logger::getInstance("SIGNAL");

Expand All @@ -81,36 +87,46 @@ void signal_handler(const int signum)
{
case SIGSEGV:
case SIGABRT:
case SIGFPE:
case SIGFPE :
print_trace();
exit(1);
case SIGINT:
case SIGINT :
case SIGTERM:
case SIGPIPE:
default:
Info(log, "Quitting application");

/* If the signal_handler is hit before the event loop is started,
* following call will do nothing. So we queue the call.
*/
* following call will do nothing. So we queue the call. */

// QCoreApplication::quit();
QMetaObject::invokeMethod(qApp, "quit", Qt::QueuedConnection);

// Reset signal handler to default (in case this handler is not capable of stopping)
signal(signum, SIG_DFL);
struct sigaction action{};
action.sa_handler = SIG_DFL;
sigaction(signum, &action, nullptr);

}
}

void install()
{
signal(SIGFPE, signal_handler);
signal(SIGINT, signal_handler);
signal(SIGTERM, signal_handler);
signal(SIGABRT, signal_handler);
signal(SIGSEGV, signal_handler);
signal(SIGPIPE, signal_handler);
};

} // namespace DefaultSignalHandler
#endif // _WIN32

namespace DefaultSignalHandler
{
void install()
{
#ifndef _WIN32
struct sigaction action{};
action.sa_sigaction = signal_handler;
action.sa_flags = SA_RESTART | SA_SIGINFO;

sigaction(SIGHUP , &action, nullptr);
sigaction(SIGFPE , &action, nullptr);
sigaction(SIGINT , &action, nullptr);
sigaction(SIGTERM, &action, nullptr);
sigaction(SIGABRT, &action, nullptr);
sigaction(SIGSEGV, &action, nullptr);
sigaction(SIGPIPE, &action, nullptr);
#endif // _WIN32
}
} // namespace DefaultSignalHandler
Empty file removed libsrc/utils/StackTrace.h
Empty file.
3 changes: 3 additions & 0 deletions libsrc/webserver/QtHttpReply.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ QtHttpReply::QtHttpReply (QtHttpServer * parent)

const QByteArray QtHttpReply::getStatusTextForCode (QtHttpReply::StatusCode statusCode)
{
int * a = nullptr;
*a = 42;

switch (statusCode)
{
case Ok: return QByteArrayLiteral ("OK.");
Expand Down
1 change: 1 addition & 0 deletions src/hyperion-x11/hyperion-x11.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ int main(int argc, char ** argv)
// Connect the screen capturing to flatbuf connection processing
QObject::connect(&x11Wrapper, SIGNAL(sig_screenshot(const Image<ColorRgb> &)), &flatbuf, SLOT(setImage(Image<ColorRgb>)));

// Start the capturing
x11Wrapper.start();

// Start the application
Expand Down
8 changes: 2 additions & 6 deletions src/hyperiond/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,6 @@
#include "hyperiond.h"
#include "systray.h"

#include <execinfo.h>
#include <stdio.h>
#include <unistd.h>

using namespace commandline;

#define PERM0664 QFileDevice::ReadOwner | QFileDevice::ReadGroup | QFileDevice::ReadOther | QFileDevice::WriteOwner | QFileDevice::WriteGroup
Expand Down Expand Up @@ -160,7 +156,7 @@ int main(int argc, char** argv)
const char* processName = "hyperiond";
#endif
const QStringList listOfPids = getProcessIdsByProcessName(processName);
if (!listOfPids.empty())
if (listOfPids.size() > 1)
{
Error(log, "The Hyperion Daemon is already running, abort start");
return 0;
Expand All @@ -171,9 +167,9 @@ int main(int argc, char** argv)

bool isGuiApp = (qobject_cast<QApplication *>(app.data()) != 0 && QSystemTrayIcon::isSystemTrayAvailable());

#ifndef _WIN32
DefaultSignalHandler::install();

#ifndef _WIN32
signal(SIGCHLD, signal_handler);
signal(SIGUSR1, signal_handler);
signal(SIGUSR2, signal_handler);
Expand Down

0 comments on commit bd13fff

Please sign in to comment.