Use systemd for stopping lokinet, also some DRY

This commit is contained in:
Stephen Shelton 2020-03-23 16:53:45 -06:00
parent 5fe01a510d
commit 560a0478e3
No known key found for this signature in database
GPG key ID: 09082714F73FAA22
4 changed files with 47 additions and 20 deletions

View file

@ -65,6 +65,22 @@ bool LokinetProcessManager::stopLokinetProcess()
qDebug("warning, lokinet process is already stopping, we'll try to stop again anyway");
}
bool success = doStopLokinetProcess();
if (not success)
{
qDebug() << "doStopLokinetProcess() failed";
return false;
}
// note that we don't touch m_didLaunchProcess here because we don't
// know whether or not lokinet will gracefully exit
setLastKnownStatus(ProcessStatus::Stopping);
return true;
}
bool LokinetProcessManager::doStopLokinetProcess()
{
bool success = m_apiClient.llarpAdminDie([](QNetworkReply* reply) {
qDebug() << "llarp.admin.die response: " << reply->readAll();
});
@ -74,11 +90,7 @@ bool LokinetProcessManager::stopLokinetProcess()
return false;
}
// note that we don't touch m_didLaunchProcess here because we don't
// know whether or not lokinet will gracefully exit
setLastKnownStatus(ProcessStatus::Stopping);
return true;
return success;
}
bool LokinetProcessManager::forciblyStopLokinetProcess()

View file

@ -138,6 +138,16 @@ protected:
*/
virtual bool doStartLokinetProcess() = 0;
/**
* Subclasses may provide platform-specific means of stopping the
* lokinet process. Note, however, that this has an "intelligent default"
* which will use the 'llarp.admin.die' API call, which should cause
* lokinet to gracefully exit.
*
* @return true on successful request to stop, false otherwise
*/
virtual bool doStopLokinetProcess();
/**
* Subclasses should provide platform-specific means of forcibly stopping
* the lokinet process.

View file

@ -26,32 +26,36 @@ SystemdStatus fromString(const QString& str) {
return SystemdStatus::unknown;
}
bool SystemdLokinetProcessManager::doStartLokinetProcess()
bool invoke(const QString& cmd, const QStringList& args)
{
QStringList args = { "--noblock", "start", "lokinet.service" };
int result = QProcess::execute("systemctl", args);
int result = QProcess::execute(cmd, args);
if (result)
{
qDebug("Failed to 'systemctl start lokinet': %d", result);
qDebug() << "Failed to invoke " << cmd;
qDebug() << " Args:";
for (const auto arg : args)
{
qDebug() << " " << arg;
}
return false;
}
return true;
}
bool SystemdLokinetProcessManager::doStopLokinetProcess()
{
return invoke("systemctl", { "--no-block", "stop", "lokinet.service" });
}
bool SystemdLokinetProcessManager::doStartLokinetProcess()
{
return invoke("systemctl", { "--no-block", "start", "lokinet.service" });
}
bool SystemdLokinetProcessManager::doForciblyStopLokinetProcess()
{
// note that this will do its own graceful attempt before doing a forceful kill.
// "kill" instead of "stop" should perform an immediate, forceful kill
QStringList args = { "stop", "lokinet.service" };
int result = QProcess::execute("systemctl", args);
if (result)
{
qDebug("Failed to 'systemctl stop lokinet': %d", result);
return false;
}
return true;
return invoke("systemctl", { "--noblock", "kill", "lokinet.service" });
}
bool SystemdLokinetProcessManager::doGetProcessPid(int& pid)

View file

@ -15,6 +15,7 @@ class SystemdLokinetProcessManager : public LokinetProcessManager
protected:
bool doStopLokinetProcess() override;
bool doStartLokinetProcess() override;
bool doForciblyStopLokinetProcess() override;
bool doGetProcessPid(int& pid) override;