2016-06-17 01:25:40 +02:00
|
|
|
#include <cassert>
|
|
|
|
#include <csignal>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <sys/prctl.h>
|
2016-07-10 12:18:40 +02:00
|
|
|
#include <exception>
|
2016-06-17 01:25:40 +02:00
|
|
|
|
|
|
|
#include <QCoreApplication>
|
|
|
|
#include <QLocale>
|
|
|
|
#include <QFile>
|
2016-08-06 08:28:42 +02:00
|
|
|
#include <QString>
|
2016-06-17 01:25:40 +02:00
|
|
|
|
|
|
|
#include "HyperionConfig.h"
|
|
|
|
|
|
|
|
#include <getoptPlusPlus/getoptpp.h>
|
|
|
|
#include <utils/Logger.h>
|
2016-06-20 23:41:07 +02:00
|
|
|
#include <webconfig/WebConfig.h>
|
2016-06-17 01:25:40 +02:00
|
|
|
|
|
|
|
#include "hyperiond.h"
|
|
|
|
|
|
|
|
using namespace vlofgren;
|
|
|
|
|
|
|
|
void signal_handler(const int signum)
|
|
|
|
{
|
|
|
|
QCoreApplication::quit();
|
|
|
|
|
|
|
|
// reset signal handler to default (in case this handler is not capable of stopping)
|
|
|
|
signal(signum, SIG_DFL);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void startNewHyperion(int parentPid, std::string hyperionFile, std::string configFile)
|
|
|
|
{
|
|
|
|
if ( fork() == 0 )
|
|
|
|
{
|
|
|
|
sleep(3);
|
|
|
|
execl(hyperionFile.c_str(), hyperionFile.c_str(), "--parent", QString::number(parentPid).toStdString().c_str(), configFile.c_str(), NULL);
|
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int main(int argc, char** argv)
|
|
|
|
{
|
2016-06-21 21:41:26 +02:00
|
|
|
// initialize main logger and set global log level
|
|
|
|
Logger* log = Logger::getInstance("MAIN");
|
2016-06-23 13:48:49 +02:00
|
|
|
Logger::setLogLevel(Logger::WARNING);
|
2016-06-17 01:25:40 +02:00
|
|
|
|
|
|
|
// Initialising QCoreApplication
|
|
|
|
QCoreApplication app(argc, argv);
|
|
|
|
|
|
|
|
signal(SIGINT, signal_handler);
|
|
|
|
signal(SIGTERM, signal_handler);
|
|
|
|
signal(SIGCHLD, signal_handler);
|
2016-07-18 09:23:55 +02:00
|
|
|
signal(SIGPIPE, signal_handler);
|
2016-06-17 01:25:40 +02:00
|
|
|
|
|
|
|
// force the locale
|
|
|
|
setlocale(LC_ALL, "C");
|
|
|
|
QLocale::setDefault(QLocale::c());
|
|
|
|
|
|
|
|
OptionsParser optionParser("Hyperion Daemon");
|
|
|
|
ParameterSet & parameters = optionParser.getParameters();
|
|
|
|
|
|
|
|
SwitchParameter<> & argVersion = parameters.add<SwitchParameter<>> (0x0, "version", "Show version information");
|
|
|
|
IntParameter & argParentPid = parameters.add<IntParameter> (0x0, "parent", "pid of parent hyperiond");
|
2016-06-23 13:48:49 +02:00
|
|
|
SwitchParameter<> & argSilent = parameters.add<SwitchParameter<>> (0x0, "silent", "do not print any outputs");
|
|
|
|
SwitchParameter<> & argVerbose = parameters.add<SwitchParameter<>> (0x0, "verbose", "Increase verbosity");
|
|
|
|
SwitchParameter<> & argDebug = parameters.add<SwitchParameter<>> (0x0, "debug", "Show debug messages");
|
2016-06-17 01:25:40 +02:00
|
|
|
SwitchParameter<> & argHelp = parameters.add<SwitchParameter<>> ('h', "help", "Show this help message and exit");
|
|
|
|
|
|
|
|
argParentPid.setDefault(0);
|
|
|
|
optionParser.parse(argc, const_cast<const char **>(argv));
|
|
|
|
const std::vector<std::string> configFiles = optionParser.getFiles();
|
|
|
|
|
2016-06-23 13:48:49 +02:00
|
|
|
int logLevelCheck = 0;
|
|
|
|
if (argSilent.isSet())
|
|
|
|
{
|
|
|
|
Logger::setLogLevel(Logger::OFF);
|
|
|
|
logLevelCheck++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (argVerbose.isSet())
|
|
|
|
{
|
|
|
|
Logger::setLogLevel(Logger::INFO);
|
|
|
|
logLevelCheck++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (argDebug.isSet())
|
|
|
|
{
|
|
|
|
Logger::setLogLevel(Logger::DEBUG);
|
|
|
|
logLevelCheck++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (logLevelCheck > 1)
|
|
|
|
{
|
|
|
|
Error(log, "aborting, because options --silent --verbose --debug can't used together");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-06-17 01:25:40 +02:00
|
|
|
// check if we need to display the usage. exit if we do.
|
|
|
|
if (argHelp.isSet())
|
|
|
|
{
|
|
|
|
optionParser.usage();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (argVersion.isSet())
|
|
|
|
{
|
|
|
|
std::cout
|
|
|
|
<< "Hyperion Ambilight Deamon (" << getpid() << ")" << std::endl
|
2016-06-24 23:22:31 +02:00
|
|
|
<< "\tVersion : " << HYPERION_VERSION << " (" << HYPERION_BUILD_ID << ")" << std::endl
|
2016-06-17 01:25:40 +02:00
|
|
|
<< "\tBuild Time: " << __DATE__ << " " << __TIME__ << std::endl;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (configFiles.size() == 0)
|
|
|
|
{
|
|
|
|
Error(log, "Missing required configuration file. Usage: hyperiond <options ...> [config.file ...]");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (argParentPid.getValue() > 0 )
|
|
|
|
{
|
|
|
|
Info(log, "hyperiond client, parent is pid %d",argParentPid.getValue());
|
|
|
|
prctl(PR_SET_PDEATHSIG, SIGHUP);
|
|
|
|
}
|
|
|
|
|
|
|
|
int argvId = -1;
|
|
|
|
for(size_t idx=0; idx < configFiles.size(); idx++) {
|
|
|
|
if ( QFile::exists(configFiles[idx].c_str()))
|
|
|
|
{
|
|
|
|
if (argvId < 0) argvId=idx;
|
|
|
|
else startNewHyperion(getpid(), argv[0], configFiles[idx].c_str());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( argvId < 0)
|
|
|
|
{
|
|
|
|
Error(log, "No valid config found");
|
|
|
|
return 1;
|
|
|
|
}
|
2016-06-23 13:48:49 +02:00
|
|
|
|
2016-06-20 23:41:07 +02:00
|
|
|
HyperionDaemon* hyperiond = nullptr;
|
|
|
|
try
|
|
|
|
{
|
2016-08-06 08:28:42 +02:00
|
|
|
hyperiond = new HyperionDaemon(QString::fromStdString(configFiles[argvId]), &app);
|
2016-06-20 23:41:07 +02:00
|
|
|
hyperiond->run();
|
|
|
|
}
|
2016-07-10 12:18:40 +02:00
|
|
|
catch (std::exception& e)
|
2016-06-20 23:41:07 +02:00
|
|
|
{
|
2016-07-10 12:18:40 +02:00
|
|
|
Error(log, "Hyperion Daemon aborted:\n %s", e.what());
|
2016-06-20 23:41:07 +02:00
|
|
|
}
|
2016-06-17 01:25:40 +02:00
|
|
|
|
2016-07-10 12:18:40 +02:00
|
|
|
int rc = 1;
|
|
|
|
WebConfig* webConfig = nullptr;
|
|
|
|
try
|
|
|
|
{
|
|
|
|
webConfig = new WebConfig(&app);
|
|
|
|
// run the application
|
|
|
|
rc = app.exec();
|
|
|
|
Info(log, "INFO: Application closed with code %d", rc);
|
|
|
|
}
|
|
|
|
catch (std::exception& e)
|
|
|
|
{
|
|
|
|
Error(log, "Hyperion aborted:\n %s", e.what());
|
|
|
|
}
|
2016-06-17 01:25:40 +02:00
|
|
|
|
2016-06-21 21:41:26 +02:00
|
|
|
// delete components
|
2016-06-20 23:41:07 +02:00
|
|
|
delete webConfig;
|
2016-06-19 00:56:47 +02:00
|
|
|
delete hyperiond;
|
2016-06-21 21:41:26 +02:00
|
|
|
Logger::deleteInstance();
|
2016-06-17 01:25:40 +02:00
|
|
|
|
|
|
|
return rc;
|
|
|
|
}
|