diff options
Diffstat (limited to 'rockwork/servicecontrol.cpp')
| -rw-r--r-- | rockwork/servicecontrol.cpp | 133 |
1 files changed, 59 insertions, 74 deletions
diff --git a/rockwork/servicecontrol.cpp b/rockwork/servicecontrol.cpp index 4d6903f..ced7f1a 100644 --- a/rockwork/servicecontrol.cpp +++ b/rockwork/servicecontrol.cpp @@ -5,84 +5,42 @@ #include <QDebug> #include <QCoreApplication> #include <QProcess> +#include <QDBusReply> -ServiceControl::ServiceControl(QObject *parent) : QObject(parent) +ServiceControl::ServiceControl(QObject *parent) : QObject(parent), + systemd(new QDBusInterface("org.freedesktop.systemd1", + "/org/freedesktop/systemd1", + "org.freedesktop.systemd1.Manager", + QDBusConnection::sessionBus(), this)) { + systemd->call("Subscribe"); -} + QDBusReply<QDBusObjectPath> unit = systemd->call("LoadUnit", ROCKPOOLD_SYSTEMD_UNIT); + if (unit.isValid()) { + unitPath = unit.value(); -QString ServiceControl::serviceName() const -{ - return m_serviceName; -} + getUnitProperties(); -void ServiceControl::setServiceName(const QString &serviceName) -{ - if (m_serviceName != serviceName) { - m_serviceName = serviceName; - emit serviceNameChanged(); + QDBusConnection::sessionBus().connect( + "org.freedesktop.systemd1", unitPath.path(), + "org.freedesktop.DBus.Properties", "PropertiesChanged", + this, SLOT(onPropertiesChanged(QString,QMap<QString,QVariant>,QStringList))); + } else { + qWarning() << unit.error().message(); } } -bool ServiceControl::serviceFileInstalled() const +void ServiceControl::onPropertiesChanged(QString interface, QMap<QString,QVariant> changed, QStringList invalidated) { - if (m_serviceName.isEmpty()) { - qDebug() << "Service name not set."; - return false; - } - QFile f(QDir::homePath() + "/.config/upstart/" + m_serviceName + ".conf"); - return f.exists(); -} - -bool ServiceControl::installServiceFile() -{ - if (m_serviceName.isEmpty()) { - qDebug() << "Service name not set. Cannot generate service file."; - return false; - } - - QFile f(QDir::homePath() + "/.config/upstart/" + m_serviceName + ".conf"); - if (f.exists()) { - qDebug() << "Service file already existing..."; - return false; - } - - if (!f.open(QFile::WriteOnly | QFile::Truncate)) { - qDebug() << "Cannot create service file"; - return false; - } - - QString appDir = qApp->applicationDirPath(); - // Try to replace version with "current" to be more robust against updates - appDir.replace(QRegExp("rockwork.mzanetti\/[0-9.]*\/"), "rockwork.mzanetti/current/"); - - f.write("start on started unity8\n"); - f.write("pre-start script\n"); - f.write(" initctl set-env LD_LIBRARY_PATH=" + appDir.toUtf8() + "/../:$LD_LIBRARY_PATH\n"); - f.write("end script\n"); - f.write("exec " + appDir.toUtf8() + "/" + m_serviceName.toUtf8() + "\n"); - f.close(); - return true; -} - -bool ServiceControl::removeServiceFile() -{ - if (m_serviceName.isEmpty()) { - qDebug() << "Service name not set."; - return false; - } - QFile f(QDir::homePath() + "/.config/upstart/" + m_serviceName + ".conf"); - return f.remove(); + qDebug() << Q_FUNC_INFO << interface << changed << invalidated; + if (interface != "org.freedesktop.systemd1.Unit") return; + if (invalidated.contains("UnitFileState") || invalidated.contains("ActiveState")) + getUnitProperties(); } bool ServiceControl::serviceRunning() const { - QProcess p; - p.start("initctl", {"status", m_serviceName}); - p.waitForFinished(); - QByteArray output = p.readAll(); - qDebug() << output; - return output.contains("running"); + return unitProperties["UnitFileState"].toString() == "enabled"; } bool ServiceControl::setServiceRunning(bool running) @@ -97,22 +55,49 @@ bool ServiceControl::setServiceRunning(bool running) bool ServiceControl::startService() { - qDebug() << "should start service"; - int ret = QProcess::execute("start", {m_serviceName}); - return ret == 0; + QDBusError reply; + systemd->call("EnableUnitFiles", QStringList() << ROCKPOOLD_SYSTEMD_UNIT, false, true); + if (reply.isValid()) { + qWarning() << reply.message(); + return false; + } else { + systemd->call("Reload"); + return true; + } } bool ServiceControl::stopService() { - qDebug() << "should stop service"; - int ret = QProcess::execute("stop", {m_serviceName}); - return ret == 0; + QDBusError reply; + systemd->call("DisableUnitFiles", QStringList() << ROCKPOOLD_SYSTEMD_UNIT, false); + if (reply.isValid()) { + qWarning() << reply.message(); + return false; + } else { + systemd->call("Reload"); + return true; + } } bool ServiceControl::restartService() { - qDebug() << "should stop service"; - int ret = QProcess::execute("restart", {m_serviceName}); - return ret == 0; + return stopService() && startService(); } +void ServiceControl::getUnitProperties() +{ + QDBusMessage request = QDBusMessage::createMethodCall( + "org.freedesktop.systemd1", unitPath.path(), + "org.freedesktop.DBus.Properties", "GetAll"); + request << "org.freedesktop.systemd1.Unit"; + QDBusReply<QVariantMap> reply = QDBusConnection::sessionBus().call(request); + if (reply.isValid()) { + QVariantMap newProperties = reply.value(); + //bool emitEnabledChanged = (unitProperties["UnitFileState"] != newProperties["UnitFileState"]); + bool emitRunningChanged = (unitProperties["ActiveState"] != newProperties["ActiveState"]); + unitProperties = newProperties; + if (emitRunningChanged) emit serviceRunning(); + } else { + qWarning() << reply.error().message(); + } +} |
