Skip to content

Commit df0d411

Browse files
authored
Fix 'Restart' RPC command (#894)
1 parent 30f04a3 commit df0d411

File tree

6 files changed

+61
-26
lines changed

6 files changed

+61
-26
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
7676
- Decrease compile time (#886)
7777
- Fix some data synchronization error (#890)
7878
- Fix Qt screenhot crash (#889)
79+
- Fix RPC restart of Hyperion (#894)
7980

8081
### Removed
8182

include/commandline/Parser.h

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,28 @@ class Parser : public QObject
7171
return *option;
7272
}
7373

74+
template<class OptionT>
75+
OptionT &addHidden(
76+
const char shortOption,
77+
const QString longOption,
78+
const QString description,
79+
const QString default_ = QString())
80+
{
81+
OptionT * option = new OptionT(
82+
_getNames(shortOption, longOption),
83+
_getDescription(description, default_),
84+
longOption,
85+
default_);
86+
#if (QT_VERSION >= QT_VERSION_CHECK(5, 8, 0))
87+
option->setFlags(QCommandLineOption::HiddenFromHelp);
88+
#else
89+
option->setHidden(true);
90+
#endif
91+
92+
addOption(option);
93+
return *option;
94+
}
95+
7496
Parser(QString description=QString())
7597
{
7698
if(description.size())setApplicationDescription(description);

libsrc/api/JsonAPI.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -868,12 +868,16 @@ void JsonAPI::handleConfigCommand(const QJsonObject &message, const QString &com
868868
{
869869
if (_adminAuthorized)
870870
{
871-
_hyperion->freeObjects(true);
871+
Debug(_log, "Restarting due to RPC command");
872+
872873
Process::restartHyperion();
873-
sendErrorReply("failed to restart hyperion", full_command, tan);
874+
875+
sendSuccessReply(command + "-" + subcommand, tan);
874876
}
875877
else
878+
{
876879
sendErrorReply("No Authorization", command, tan);
880+
}
877881
}
878882
else
879883
{

libsrc/utils/Process.cpp

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ QByteArray command_exec(QString /*cmd*/, QByteArray /*data*/)
1818
#include <utils/Logger.h>
1919

2020
#include <QCoreApplication>
21+
#include <QProcess>
2122
#include <QStringList>
2223

2324
#include <unistd.h>
@@ -31,26 +32,20 @@ namespace Process {
3132
void restartHyperion(bool asNewProcess)
3233
{
3334
Logger* log = Logger::getInstance("Process");
35+
Info(log, "Restarting hyperion ...");
36+
3437
std::cout << std::endl
3538
<< " *******************************************" << std::endl
3639
<< " * hyperion will restart now *" << std::endl
3740
<< " *******************************************" << std::endl << std::endl;
3841

42+
auto arguments = QCoreApplication::arguments();
43+
if (!arguments.contains("--wait-hyperion"))
44+
arguments << "--wait-hyperion";
3945

40-
QStringList qargs = QCoreApplication::arguments();
41-
int size = qargs.size();
42-
char *args[size+1];
43-
args[size] = nullptr;
44-
for(int i=0; i<size; i++)
45-
{
46-
int str_size = qargs[i].toLocal8Bit().size();
47-
args[i] = new char[str_size+1];
48-
strncpy(args[i], qargs[i].toLocal8Bit().constData(),str_size);
49-
args[i][str_size] = '\0';
50-
}
46+
QProcess::startDetached(QCoreApplication::applicationFilePath(), arguments);
5147

52-
execv(args[0],args);
53-
Error(log, "error while restarting hyperion");
48+
QCoreApplication::quit();
5449
}
5550

5651
QByteArray command_exec(QString cmd, QByteArray data)

src/hyperiond/detectProcess.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ QStringList getProcessIdsByProcessName(const char *processName)
6161
continue;
6262
}
6363

64-
QFile cmdline("/proc/" + pid + "/cmdline");
64+
QFile cmdline("/proc/" + pid + "/comm");
6565
if (!cmdline.open(QFile::ReadOnly | QFile::Text))
6666
{
6767
/* Can not open cmdline file */

src/hyperiond/main.cpp

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -151,12 +151,6 @@ int main(int argc, char** argv)
151151
#else
152152
const char* processName = "hyperiond";
153153
#endif
154-
const QStringList listOfPids = getProcessIdsByProcessName(processName);
155-
if (listOfPids.size() > 1)
156-
{
157-
Error(log, "The Hyperion Daemon is already running, abort start");
158-
return 0;
159-
}
160154

161155
// Initialising QCoreApplication
162156
QScopedPointer<QCoreApplication> app(createApplication(argc, argv));
@@ -181,18 +175,37 @@ int main(int argc, char** argv)
181175
Option & userDataOption = parser.add<Option> ('u', "userdata", "Overwrite user data path, defaults to home directory of current user (%1)", QDir::homePath() + "/.hyperion");
182176
BooleanOption & resetPassword = parser.add<BooleanOption> (0x0, "resetPassword", "Lost your password? Reset it with this option back to 'hyperion'");
183177
BooleanOption & deleteDB = parser.add<BooleanOption> (0x0, "deleteDatabase", "Start all over? This Option will delete the database");
184-
BooleanOption & silentOption = parser.add<BooleanOption> ('s', "silent", "do not print any outputs");
178+
BooleanOption & silentOption = parser.add<BooleanOption> ('s', "silent", "Do not print any outputs");
185179
BooleanOption & verboseOption = parser.add<BooleanOption> ('v', "verbose", "Increase verbosity");
186180
BooleanOption & debugOption = parser.add<BooleanOption> ('d', "debug", "Show debug messages");
187181
#ifdef WIN32
188182
BooleanOption & consoleOption = parser.add<BooleanOption> ('c', "console", "Open a console window to view log output");
189183
#endif
190-
parser.add<BooleanOption> (0x0, "desktop", "show systray on desktop");
191-
parser.add<BooleanOption> (0x0, "service", "force hyperion to start as console service");
192-
Option & exportEfxOption = parser.add<Option> (0x0, "export-effects", "export effects to given path");
184+
parser.add<BooleanOption> (0x0, "desktop", "Show systray on desktop");
185+
parser.add<BooleanOption> (0x0, "service", "Force hyperion to start as console service");
186+
Option & exportEfxOption = parser.add<Option> (0x0, "export-effects", "Export effects to given path");
187+
188+
/* Internal options, invisible to help */
189+
BooleanOption & waitOption = parser.addHidden<BooleanOption> (0x0, "wait-hyperion", "Do not exit if other Hyperion instances are running, wait them to finish");
193190

194191
parser.process(*qApp);
195192

193+
if (!parser.isSet(waitOption))
194+
{
195+
if (getProcessIdsByProcessName(processName).size() > 1)
196+
{
197+
Error(log, "The Hyperion Daemon is already running, abort start");
198+
return 0;
199+
}
200+
}
201+
else
202+
{
203+
while (getProcessIdsByProcessName(processName).size() > 1)
204+
{
205+
QThread::msleep(100);
206+
}
207+
}
208+
196209
#ifdef WIN32
197210
if (parser.isSet(consoleOption))
198211
{

0 commit comments

Comments
 (0)