1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
|
#include "servicecontrol.h"
#include <QFile>
#include <QDir>
#include <QDebug>
#include <QCoreApplication>
#include <QProcess>
#include <QDBusReply>
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();
getUnitProperties();
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();
}
}
void ServiceControl::onPropertiesChanged(QString interface, QMap<QString,QVariant> changed, QStringList invalidated)
{
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
{
return unitProperties["UnitFileState"].toString() == "enabled";
}
bool ServiceControl::setServiceRunning(bool running)
{
if (running && !serviceRunning()) {
return startService();
} else if (!running && serviceRunning()) {
return stopService();
}
return true; // Requested state is already the current state.
}
bool ServiceControl::startService()
{
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()
{
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()
{
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();
}
}
|