KIO

systemdprocessrunner.cpp
1/*
2 This file is part of the KDE libraries
3 SPDX-FileCopyrightText: 2020 Henri Chain <henri.chain@enioka.com>
4
5 SPDX-License-Identifier: LGPL-2.0-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
6*/
7
8#include "kiogui_debug.h"
9#include "systemdprocessrunner_p.h"
10
11#include "managerinterface.h"
12#include "propertiesinterface.h"
13#include "unitinterface.h"
14
15#include <QTimer>
16
17#include <algorithm>
18#include <mutex>
19#include <signal.h>
20
21using namespace org::freedesktop;
22using namespace Qt::Literals::StringLiterals;
23
24KProcessRunner::LaunchMode calculateLaunchMode()
25{
26 // overrides for unit test purposes. These are considered internal, private and may change in the future.
27 if (Q_UNLIKELY(qEnvironmentVariableIntValue("_KDE_APPLICATIONS_AS_SERVICE"))) {
28 return KProcessRunner::SystemdAsService;
29 }
30 if (Q_UNLIKELY(qEnvironmentVariableIntValue("_KDE_APPLICATIONS_AS_SCOPE"))) {
31 return KProcessRunner::SystemdAsScope;
32 }
33 if (Q_UNLIKELY(qEnvironmentVariableIntValue("_KDE_APPLICATIONS_AS_FORKING"))) {
34 return KProcessRunner::Forking;
35 }
36
38 auto queryVersionMessage = QDBusMessage::createMethodCall(systemdService, systemdPath, u"org.freedesktop.DBus.Properties"_s, u"Get"_s);
39 queryVersionMessage << u"org.freedesktop.systemd1.Manager"_s << u"Version"_s;
40 QDBusReply<QDBusVariant> reply = bus.call(queryVersionMessage);
41 QVersionNumber systemdVersion = QVersionNumber::fromString(reply.value().variant().toString());
42 if (systemdVersion.isNull()) {
43 qCWarning(KIO_GUI) << "Failed to determine systemd version, falling back to extremely legacy forking mode.";
44 return KProcessRunner::Forking;
45 }
46 if (systemdVersion.majorVersion() < 250) { // first version with ExitType=cgroup, which won't cleanup when the first process exits
47 return KProcessRunner::SystemdAsScope;
48 }
49 return KProcessRunner::SystemdAsService;
50}
51
52KProcessRunner::LaunchMode SystemdProcessRunner::modeAvailable()
53{
54 static std::once_flag launchModeCalculated;
55 static KProcessRunner::LaunchMode launchMode = Forking;
56 std::call_once(launchModeCalculated, [] {
57 launchMode = calculateLaunchMode();
58 qCDebug(KIO_GUI) << "Launching processes via" << launchMode;
59 qDBusRegisterMetaType<QVariantMultiItem>();
60 qDBusRegisterMetaType<QVariantMultiMap>();
61 qDBusRegisterMetaType<TransientAux>();
62 qDBusRegisterMetaType<TransientAuxList>();
63 qDBusRegisterMetaType<ExecCommand>();
64 qDBusRegisterMetaType<ExecCommandList>();
65 });
66 return launchMode;
67}
68
69SystemdProcessRunner::SystemdProcessRunner()
70 : KProcessRunner()
71{
72}
73
74bool SystemdProcessRunner::waitForStarted(int timeout)
75{
76 if (m_pid || m_exited) {
77 return true;
78 }
79 QEventLoop loop;
80 bool success = false;
81 loop.connect(this, &KProcessRunner::processStarted, this, [&loop, &success]() {
82 loop.quit();
83 success = true;
84 });
85 QTimer::singleShot(timeout, &loop, &QEventLoop::quit);
86 QObject::connect(this, &KProcessRunner::error, &loop, &QEventLoop::quit);
87 loop.exec();
88 return success;
89}
90
91static QStringList prepareEnvironment(const QProcessEnvironment &environment)
92{
93 QProcessEnvironment allowedEnvironment = environment.inheritsFromParent() ? QProcessEnvironment::systemEnvironment() : environment;
94 auto allowedBySystemd = [](const QChar c) {
95 return c.isDigit() || c.isLetter() || c == u'_';
96 };
97 for (const auto variables = allowedEnvironment.keys(); const auto &variable : variables) {
98 if (!std::ranges::all_of(variable, allowedBySystemd)) {
99 qCWarning(KIO_GUI) << "Not passing environment variable" << variable << "to systemd because its name contains illegal characters";
100 allowedEnvironment.remove(variable);
101 }
102 }
103 return allowedEnvironment.toStringList();
104}
105
106// systemd performs substitution of $ variables, we don't want this
107// $ should be replaced with $$
108static QStringList escapeArguments(const QStringList &in)
109{
110 QStringList escaped = in;
111 std::transform(escaped.begin(), escaped.end(), escaped.begin(), [](QString &item) {
112 return item.replace(QLatin1Char('$'), QLatin1String("$$"));
113 });
114 return escaped;
115}
116
117void SystemdProcessRunner::startProcess()
118{
119 // As specified in "XDG standardization for applications" in https://systemd.io/DESKTOP_ENVIRONMENTS/
120 m_serviceName = QStringLiteral("app-%1@%2.service").arg(escapeUnitName(resolveServiceAlias()), QUuid::createUuid().toString(QUuid::Id128));
121
122 // Watch for new services
123 m_manager = new systemd1::Manager(systemdService, systemdPath, QDBusConnection::sessionBus(), this);
124 m_manager->Subscribe();
125 connect(m_manager, &systemd1::Manager::UnitNew, this, &SystemdProcessRunner::handleUnitNew);
126
127 // Watch for service creation job error
128 connect(m_manager,
129 &systemd1::Manager::JobRemoved,
130 this,
131 [this](uint jobId, const QDBusObjectPath &jobPath, const QString &unitName, const QString &result) {
132 Q_UNUSED(jobId)
133 if (jobPath.path() == m_jobPath && unitName == m_serviceName && result != QLatin1String("done")) {
134 qCWarning(KIO_GUI) << "Failed to launch process as service:" << m_serviceName << ", result " << result;
135 // result=failed is not a fatal error, service is actually created in this case
136 if (result != QLatin1String("failed")) {
137 systemdError(result);
138 }
139 }
140 });
141
142 const QStringList argv = escapeArguments(m_process->program());
143
144 // Ask systemd for a new transient service
145 const auto startReply =
146 m_manager->StartTransientUnit(m_serviceName,
147 QStringLiteral("fail"), // mode defines what to do in the case of a name conflict, in this case, just do nothing
148 {
149 // Properties of the transient service unit
150 {QStringLiteral("Type"), QStringLiteral("simple")},
151 {QStringLiteral("ExitType"), QStringLiteral("cgroup")},
152 {QStringLiteral("Slice"), QStringLiteral("app.slice")},
153 {QStringLiteral("Description"), m_description},
154 {QStringLiteral("SourcePath"), m_desktopFilePath},
155 {QStringLiteral("AddRef"), true}, // Asks systemd to avoid garbage collecting the service if it immediately crashes,
156 // so we can be notified (see https://github.com/systemd/systemd/pull/3984)
157 {QStringLiteral("Environment"), prepareEnvironment(m_process->processEnvironment())},
158 {QStringLiteral("WorkingDirectory"), m_process->workingDirectory()},
159 {QStringLiteral("ExecStart"), QVariant::fromValue(ExecCommandList{{m_process->program().first(), argv, false}})},
160 },
161 {} // aux is currently unused and should be passed as empty array.
162 );
163 connect(new QDBusPendingCallWatcher(startReply, this), &QDBusPendingCallWatcher::finished, this, [this](QDBusPendingCallWatcher *watcher) {
164 QDBusPendingReply<QDBusObjectPath> reply = *watcher;
165 watcher->deleteLater();
166 if (reply.isError()) {
167 qCWarning(KIO_GUI) << "Failed to launch process as service:" << m_serviceName << reply.error().name() << reply.error().message();
168 return systemdError(reply.error().message());
169 }
170 qCDebug(KIO_GUI) << "Successfully asked systemd to launch process as service:" << m_serviceName;
171 m_jobPath = reply.argumentAt<0>().path();
172 });
173}
174
175void SystemdProcessRunner::handleProperties(QDBusPendingCallWatcher *watcher)
176{
177 const QDBusPendingReply<QVariantMap> reply = *watcher;
178 watcher->deleteLater();
179 if (reply.isError()) {
180 qCWarning(KIO_GUI) << "Failed to get properties for service:" << m_serviceName << reply.error().name() << reply.error().message();
181 return systemdError(reply.error().message());
182 }
183 qCDebug(KIO_GUI) << "Successfully retrieved properties for service:" << m_serviceName;
184 if (m_exited) {
185 return;
186 }
187 const auto properties = reply.argumentAt<0>();
188 if (!m_pid) {
189 setPid(properties[QStringLiteral("ExecMainPID")].value<quint32>());
190 return;
191 }
192 const auto activeState = properties[QStringLiteral("ActiveState")].toString();
193 if (activeState != QLatin1String("inactive") && activeState != QLatin1String("failed")) {
194 return;
195 }
196 m_exited = true;
197
198 // ExecMainCode/Status correspond to si_code/si_status in the siginfo_t structure
199 // ExecMainCode is the signal code: CLD_EXITED (1) means normal exit
200 // ExecMainStatus is the process exit code in case of normal exit, otherwise it is the signal number
201 const auto signalCode = properties[QStringLiteral("ExecMainCode")].value<qint32>();
202 const auto exitCodeOrSignalNumber = properties[QStringLiteral("ExecMainStatus")].value<qint32>();
203 const auto exitStatus = signalCode == CLD_EXITED ? QProcess::ExitStatus::NormalExit : QProcess::ExitStatus::CrashExit;
204
205 qCDebug(KIO_GUI) << m_serviceName << "pid=" << m_pid << "exitCode=" << exitCodeOrSignalNumber << "exitStatus=" << exitStatus;
206 terminateStartupNotification();
207 deleteLater();
208
209 systemd1::Unit unitInterface(systemdService, m_servicePath, QDBusConnection::sessionBus(), this);
210 connect(new QDBusPendingCallWatcher(unitInterface.Unref(), this), &QDBusPendingCallWatcher::finished, this, [this](QDBusPendingCallWatcher *watcher) {
211 QDBusPendingReply<> reply = *watcher;
212 watcher->deleteLater();
213 if (reply.isError()) {
214 qCWarning(KIO_GUI) << "Failed to unref service:" << m_serviceName << reply.error().name() << reply.error().message();
215 return systemdError(reply.error().message());
216 }
217 qCDebug(KIO_GUI) << "Successfully unref'd service:" << m_serviceName;
218 });
219}
220
221void SystemdProcessRunner::handleUnitNew(const QString &newName, const QDBusObjectPath &newPath)
222{
223 if (newName != m_serviceName) {
224 return;
225 }
226 qCDebug(KIO_GUI) << "Successfully launched process as service:" << m_serviceName;
227
228 // Get PID (and possibly exit code) from systemd service properties
229 m_servicePath = newPath.path();
230 m_serviceProperties = new DBus::Properties(systemdService, m_servicePath, QDBusConnection::sessionBus(), this);
231 auto propReply = m_serviceProperties->GetAll(QString());
232 connect(new QDBusPendingCallWatcher(propReply, this), &QDBusPendingCallWatcher::finished, this, &SystemdProcessRunner::handleProperties);
233
234 // Watch for status change
235 connect(m_serviceProperties, &DBus::Properties::PropertiesChanged, this, [this]() {
236 if (m_exited) {
237 return;
238 }
239 qCDebug(KIO_GUI) << "Got PropertiesChanged signal:" << m_serviceName;
240 // We need to look at the full list of properties rather than only those which changed
241 auto reply = m_serviceProperties->GetAll(QString());
242 connect(new QDBusPendingCallWatcher(reply, this), &QDBusPendingCallWatcher::finished, this, &SystemdProcessRunner::handleProperties);
243 });
244}
245
246void SystemdProcessRunner::systemdError(const QString &message)
247{
248 Q_EMIT error(message);
249 deleteLater();
250}
251
252#include "moc_systemdprocessrunner_p.cpp"
char * toString(const EngineQuery &query)
QString path(const QString &relativePath)
void error(QWidget *parent, const QString &text, const QString &title, const KGuiItem &buttonOk, Options options=Notify)
KGuiItem properties()
QDBusMessage call(const QDBusMessage &message, QDBus::CallMode mode, int timeout) const const
QDBusConnection sessionBus()
QString message() const const
QString name() const const
QDBusMessage createMethodCall(const QString &service, const QString &path, const QString &interface, const QString &method)
QString path() const const
void finished(QDBusPendingCallWatcher *self)
QVariant argumentAt(int index) const const
QDBusError error() const const
bool isError() const const
int exec(ProcessEventsFlags flags)
void quit()
iterator begin()
iterator end()
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
void deleteLater()
bool inheritsFromParent() const const
QStringList keys() const const
void remove(const QString &name)
QProcessEnvironment systemEnvironment()
QStringList toStringList() const const
QFuture< ArgsType< Signal > > connect(Sender *sender, Signal signal)
QUuid createUuid()
QVariant fromValue(T &&value)
QVersionNumber fromString(QAnyStringView string, qsizetype *suffixIndex)
bool isNull() const const
int majorVersion() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Mon Nov 18 2024 12:16:28 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.