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 | |
21 | using namespace org::freedesktop; |
22 | using namespace Qt::Literals::StringLiterals; |
23 | |
24 | KProcessRunner::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 | |
37 | QDBusConnection bus = QDBusConnection::sessionBus(); |
38 | auto queryVersionMessage = QDBusMessage::createMethodCall(destination: systemdService, path: systemdPath, interface: u"org.freedesktop.DBus.Properties"_s , method: u"Get"_s ); |
39 | queryVersionMessage << u"org.freedesktop.systemd1.Manager"_s << u"Version"_s ; |
40 | QDBusReply<QDBusVariant> reply = bus.call(message: queryVersionMessage); |
41 | QVersionNumber systemdVersion = QVersionNumber::fromString(string: 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 | |
52 | KProcessRunner::LaunchMode SystemdProcessRunner::modeAvailable() |
53 | { |
54 | static std::once_flag launchModeCalculated; |
55 | static KProcessRunner::LaunchMode launchMode = Forking; |
56 | std::call_once(once&: launchModeCalculated, f: [] { |
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 | |
69 | SystemdProcessRunner::SystemdProcessRunner() |
70 | : KProcessRunner() |
71 | { |
72 | } |
73 | |
74 | bool 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(sender: this, signal: &KProcessRunner::processStarted, context: this, slot: [&loop, &success]() { |
82 | loop.quit(); |
83 | success = true; |
84 | }); |
85 | QTimer::singleShot(interval: timeout, receiver: &loop, slot: &QEventLoop::quit); |
86 | QObject::connect(sender: this, signal: &KProcessRunner::error, context: &loop, slot: &QEventLoop::quit); |
87 | loop.exec(); |
88 | return success; |
89 | } |
90 | |
91 | static 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(name: 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 $$ |
108 | static QStringList escapeArguments(const QStringList &in) |
109 | { |
110 | QStringList escaped = in; |
111 | std::transform(first: escaped.begin(), last: escaped.end(), result: escaped.begin(), unary_op: [](QString &item) { |
112 | return item.replace(c: QLatin1Char('$'), after: QLatin1String("$$" )); |
113 | }); |
114 | return escaped; |
115 | } |
116 | |
117 | void 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(args: escapeUnitName(input: resolveServiceAlias()), args: QUuid::createUuid().toString(mode: 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(sender: m_manager, signal: &systemd1::Manager::UnitNew, context: this, slot: &SystemdProcessRunner::handleUnitNew); |
126 | |
127 | // Watch for service creation job error |
128 | connect(sender: m_manager, |
129 | signal: &systemd1::Manager::JobRemoved, |
130 | context: this, |
131 | slot: [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(error: result); |
138 | } |
139 | } |
140 | }); |
141 | |
142 | const QStringList argv = escapeArguments(in: m_process->program()); |
143 | |
144 | // Ask systemd for a new transient service |
145 | const auto startReply = |
146 | m_manager->StartTransientUnit(name: m_serviceName, |
147 | QStringLiteral("fail" ), // mode defines what to do in the case of a name conflict, in this case, just do nothing |
148 | properties: { |
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" ), .value: m_description}, |
154 | {QStringLiteral("SourcePath" ), .value: m_desktopFilePath}, |
155 | {QStringLiteral("AddRef" ), .value: 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" ), .value: prepareEnvironment(environment: m_process->processEnvironment())}, |
158 | {QStringLiteral("WorkingDirectory" ), .value: m_process->workingDirectory()}, |
159 | {QStringLiteral("ExecStart" ), .value: QVariant::fromValue(value: ExecCommandList{{.path: m_process->program().first(), .argv: argv, .ignoreFailure: false}})}, |
160 | }, |
161 | aux: {} // aux is currently unused and should be passed as empty array. |
162 | ); |
163 | connect(sender: new QDBusPendingCallWatcher(startReply, this), signal: &QDBusPendingCallWatcher::finished, context: this, slot: [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(error: 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 | |
175 | void 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(error: 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(sender: new QDBusPendingCallWatcher(unitInterface.Unref(), this), signal: &QDBusPendingCallWatcher::finished, context: this, slot: [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(error: reply.error().message()); |
216 | } |
217 | qCDebug(KIO_GUI) << "Successfully unref'd service:" << m_serviceName; |
218 | }); |
219 | } |
220 | |
221 | void 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(interface: QString()); |
232 | connect(sender: new QDBusPendingCallWatcher(propReply, this), signal: &QDBusPendingCallWatcher::finished, context: this, slot: &SystemdProcessRunner::handleProperties); |
233 | |
234 | // Watch for status change |
235 | connect(sender: m_serviceProperties, signal: &DBus::Properties::PropertiesChanged, context: this, slot: [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(interface: QString()); |
242 | connect(sender: new QDBusPendingCallWatcher(reply, this), signal: &QDBusPendingCallWatcher::finished, context: this, slot: &SystemdProcessRunner::handleProperties); |
243 | }); |
244 | } |
245 | |
246 | void SystemdProcessRunner::systemdError(const QString &message) |
247 | { |
248 | Q_EMIT error(errorString: message); |
249 | deleteLater(); |
250 | } |
251 | |
252 | #include "moc_systemdprocessrunner_p.cpp" |
253 | |