systemdcommunicator.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. /*
  2. * Copyright (C) 2015 Malte Veerman <maldela@halloarsch.de>
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU Lesser General Public License as published by
  6. * the Free Software Foundation; either version 2 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU Lesser General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU Lesser General Public License along
  15. * with this program; if not, write to the Free Software Foundation, Inc.,
  16. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  17. *
  18. */
  19. #include "systemdcommunicator.h"
  20. #include "fancontrolaction.h"
  21. #include <QtCore/QDebug>
  22. #include <QtCore/QVariant>
  23. #include <QtCore/QTimer>
  24. #include <QtDBus/QDBusArgument>
  25. #include <QtDBus/QDBusInterface>
  26. #include <KAuth/KAuthExecuteJob>
  27. #include <KI18n/KLocalizedString>
  28. #ifndef STANDARD_SERVICE_NAME
  29. #define STANDARD_SERVICE_NAME "fancontrol"
  30. #endif
  31. typedef struct
  32. {
  33. QString path;
  34. QString state;
  35. } SystemdUnitFile;
  36. Q_DECLARE_METATYPE(SystemdUnitFile)
  37. typedef QList<SystemdUnitFile> SystemdUnitFileList;
  38. Q_DECLARE_METATYPE(SystemdUnitFileList)
  39. QDBusArgument& operator <<(QDBusArgument &argument, const SystemdUnitFile &unitFile)
  40. {
  41. argument.beginStructure();
  42. argument << unitFile.path << unitFile.state;
  43. argument.endStructure();
  44. return argument;
  45. }
  46. const QDBusArgument& operator >>(const QDBusArgument &argument, SystemdUnitFile &unitFile)
  47. {
  48. argument.beginStructure();
  49. argument >> unitFile.path >> unitFile.state;
  50. argument.endStructure();
  51. return argument;
  52. }
  53. namespace Fancontrol
  54. {
  55. SystemdCommunicator::SystemdCommunicator(QObject *parent, const QString &serviceName) : QObject(parent),
  56. m_managerInterface(new QDBusInterface(QStringLiteral("org.freedesktop.systemd1"),
  57. QStringLiteral("/org/freedesktop/systemd1"),
  58. QStringLiteral("org.freedesktop.systemd1.Manager"),
  59. QDBusConnection::systemBus(),
  60. this)),
  61. m_serviceInterface(Q_NULLPTR)
  62. {
  63. if (serviceName.isEmpty())
  64. setServiceName(QStringLiteral(STANDARD_SERVICE_NAME));
  65. else
  66. setServiceName(serviceName);
  67. emit serviceNameChanged();
  68. emit serviceEnabledChanged();
  69. emit serviceActiveChanged();
  70. }
  71. void SystemdCommunicator::setServiceName(const QString &name)
  72. {
  73. if (name != m_serviceName)
  74. {
  75. if (m_serviceInterface)
  76. {
  77. QDBusConnection::systemBus().disconnect(QStringLiteral("org.freedesktop.systemd1"),
  78. m_serviceObjectPath,
  79. QStringLiteral("org.freedesktop.DBus.Properties"),
  80. QStringLiteral("PropertiesChanged"),
  81. this,
  82. SLOT(updateServiceProperties(QString, QVariantMap, QStringList)));
  83. m_serviceInterface->deleteLater();
  84. m_serviceInterface = Q_NULLPTR;
  85. }
  86. m_serviceName = name;
  87. if (serviceExists())
  88. {
  89. QVariantList arguments;
  90. arguments << QVariant(m_serviceName + ".service");
  91. const auto dbusreply = m_managerInterface->callWithArgumentList(QDBus::AutoDetect, QStringLiteral("LoadUnit"), arguments);
  92. if (dbusreply.type() == QDBusMessage::ErrorMessage)
  93. {
  94. m_error = dbusreply.errorMessage();
  95. emit errorChanged();
  96. m_serviceObjectPath.clear();
  97. }
  98. else
  99. {
  100. m_serviceObjectPath = qdbus_cast<QDBusObjectPath>(dbusreply.arguments().at(0)).path();
  101. m_serviceInterface = new QDBusInterface(QStringLiteral("org.freedesktop.systemd1"),
  102. m_serviceObjectPath,
  103. QStringLiteral("org.freedesktop.systemd1.Unit"),
  104. QDBusConnection::systemBus(),
  105. this);
  106. QDBusConnection::systemBus().connect(QStringLiteral("org.freedesktop.systemd1"),
  107. m_serviceObjectPath,
  108. QStringLiteral("org.freedesktop.DBus.Properties"),
  109. QStringLiteral("PropertiesChanged"),
  110. this,
  111. SLOT(updateServiceProperties(QString, QVariantMap, QStringList)));
  112. }
  113. }
  114. emit serviceNameChanged();
  115. emit serviceEnabledChanged();
  116. emit serviceActiveChanged();
  117. }
  118. }
  119. bool SystemdCommunicator::serviceExists()
  120. {
  121. if (m_serviceInterface)
  122. {
  123. if (m_serviceInterface->isValid())
  124. return true;
  125. }
  126. QDBusMessage dbusreply;
  127. if (m_managerInterface && m_managerInterface->isValid())
  128. dbusreply = m_managerInterface->call(QDBus::AutoDetect, QStringLiteral("ListUnitFiles"));
  129. if (dbusreply.type() == QDBusMessage::ErrorMessage)
  130. {
  131. setError(dbusreply.errorMessage());
  132. return false;
  133. }
  134. SystemdUnitFileList list = qdbus_cast<SystemdUnitFileList>(dbusreply.arguments().at(0));
  135. foreach (const auto &unitFile, list)
  136. {
  137. if (unitFile.path.contains(m_serviceName + ".service"))
  138. return true;
  139. }
  140. setError(i18n("Service %1 doesn't exist", m_serviceName));
  141. return false;
  142. }
  143. bool SystemdCommunicator::serviceActive()
  144. {
  145. if (serviceExists() && m_serviceInterface)
  146. {
  147. if (m_serviceInterface->property("ActiveState").toString() == QStringLiteral("active"))
  148. return true;
  149. }
  150. return false;
  151. }
  152. bool SystemdCommunicator::serviceEnabled()
  153. {
  154. if (serviceExists() && m_serviceInterface)
  155. {
  156. if (m_serviceInterface->property("UnitFileState").toString() == QStringLiteral("enabled"))
  157. return true;
  158. }
  159. return false;
  160. }
  161. bool SystemdCommunicator::setServiceEnabled(bool enabled)
  162. {
  163. if (serviceExists())
  164. {
  165. if (enabled != serviceEnabled())
  166. {
  167. const auto action = enabled ? QStringLiteral("EnableUnitFiles") : QStringLiteral("DisableUnitFiles");
  168. const auto files = QStringList() << m_serviceName + ".service";
  169. auto arguments = QVariantList() << files << false;
  170. if (enabled)
  171. arguments << true;
  172. if (dbusAction(action, arguments))
  173. {
  174. if (dbusAction(QStringLiteral("Reload")))
  175. {
  176. emit serviceEnabledChanged();
  177. return true;
  178. }
  179. }
  180. return false;
  181. }
  182. return true;
  183. }
  184. return false;
  185. }
  186. bool SystemdCommunicator::setServiceActive(bool active)
  187. {
  188. qDebug() << "Set service active:" << active;
  189. if (serviceExists())
  190. {
  191. if (active != serviceActive())
  192. {
  193. auto args = QVariantList() << m_serviceName + ".service" << "replace";
  194. const auto action = active ? QStringLiteral("ReloadOrRestartUnit") : QStringLiteral("StopUnit");
  195. if (dbusAction(action, args))
  196. {
  197. emit serviceActiveChanged();
  198. return true;
  199. }
  200. }
  201. return true;
  202. }
  203. return false;
  204. }
  205. bool SystemdCommunicator::dbusAction(const QString &method, const QVariantList &arguments)
  206. {
  207. if (!m_managerInterface->isValid())
  208. {
  209. qDebug() << "Invalid manager interface!";
  210. return false;
  211. }
  212. const auto dbusreply = arguments.isEmpty() ? m_managerInterface->call(QDBus::AutoDetect, method) : m_managerInterface->callWithArgumentList(QDBus::AutoDetect, method, arguments);
  213. if (dbusreply.type() != QDBusMessage::ErrorMessage)
  214. return true;
  215. if (dbusreply.errorMessage() == QStringLiteral("Interactive authentication required."))
  216. {
  217. auto action = newFancontrolAction();
  218. QVariantMap map;
  219. map[QStringLiteral("action")] = "dbusaction";
  220. map[QStringLiteral("method")] = method;
  221. map[QStringLiteral("arguments")] = arguments;
  222. action.setArguments(map);
  223. const auto job = action.execute();
  224. connect(job, &KAuth::ExecuteJob::result, this, &SystemdCommunicator::handleDbusActionResult);
  225. job->start();
  226. return true;
  227. }
  228. setError(dbusreply.errorMessage());
  229. return false;
  230. }
  231. void SystemdCommunicator::handleDbusActionResult(KJob *job)
  232. {
  233. if (job->error())
  234. {
  235. if (job->error() == KAuth::ActionReply::HelperBusyError)
  236. {
  237. qDebug() << "Helper busy...";
  238. const auto executeJob = static_cast<KAuth::ExecuteJob *>(job);
  239. if (executeJob)
  240. {
  241. const auto newJob = executeJob->action().execute();
  242. connect(newJob, &KAuth::ExecuteJob::result, this, &SystemdCommunicator::handleDbusActionResult);
  243. QTimer::singleShot(50, newJob, &KAuth::ExecuteJob::start);
  244. return;
  245. }
  246. }
  247. setError(job->errorText());
  248. }
  249. }
  250. bool SystemdCommunicator::restartService()
  251. {
  252. if (serviceExists())
  253. {
  254. QVariantList args;
  255. args << m_serviceName + ".service" << "replace";
  256. return dbusAction(QStringLiteral("ReloadOrRestartUnit"), args);
  257. }
  258. setError(i18n("Service does not exist"));
  259. return false;
  260. }
  261. void SystemdCommunicator::updateServiceProperties(QString, QVariantMap propchanged, QStringList)
  262. {
  263. if (propchanged.value(QStringLiteral("ActiveState")).isValid())
  264. emit serviceActiveChanged();
  265. if (propchanged.value(QStringLiteral("UnitFileState")).isValid())
  266. emit serviceEnabledChanged();
  267. }
  268. void SystemdCommunicator::setError(const QString &error)
  269. {
  270. qCritical() << error;
  271. m_error = error;
  272. emit errorChanged();
  273. }
  274. }