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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
|
/****************************************************************************
**
** Copyright (C) 2019 Luxoft Sweden AB
** Copyright (C) 2018 Pelagicore AG
** Contact: https://www.qt.io/licensing/
**
** This file is part of the Luxoft Application Manager.
**
** $QT_BEGIN_LICENSE:LGPL-QTAS$
** Commercial License Usage
** Licensees holding valid commercial Qt Automotive Suite licenses may use
** this file in accordance with the commercial license agreement provided
** with the Software or, alternatively, in accordance with the terms
** contained in a written agreement between you and The Qt Company. For
** licensing terms and conditions see https://www.qt.io/terms-conditions.
** For further information use the contact form at https://www.qt.io/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 3 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL3 included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 3 requirements
** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 2.0 or (at your option) the GNU General
** Public license version 3 or any later version approved by the KDE Free
** Qt Foundation. The licenses are as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-2.0.html and
** https://www.gnu.org/licenses/gpl-3.0.html.
**
** $QT_END_LICENSE$
**
** SPDX-License-Identifier: LGPL-3.0
**
****************************************************************************/
#include "global.h"
#include "logging.h"
#include "containerfactory.h"
#include "application.h"
#include "processcontainer.h"
#include "systemreader.h"
#include "debugwrapper.h"
#if defined(Q_OS_UNIX)
# include <csignal>
# include <unistd.h>
# include <fcntl.h>
#endif
QT_BEGIN_NAMESPACE_AM
class HostQProcess : public QProcess // clazy:exclude=missing-qobject-macro
{
protected:
void setupChildProcess() override
{
#if defined(Q_OS_UNIX)
if (m_stopBeforeExec) {
fprintf(stderr, "\n*** a 'process' container was started in stopped state ***\nthe process is suspended via SIGSTOP and you can attach a debugger to it via\n\n gdb -p %d\n\n", getpid());
raise(SIGSTOP);
}
// duplicate any requested redirections to the respective stdin/out/err fd. Also make sure to
// close the original fd: otherwise we would block the tty where the fds originated from.
for (int i = 0; i < 3; ++i) {
int fd = m_stdioRedirections.value(i, -1);
if (fd >= 0) {
dup2(fd, i);
::close(fd);
}
}
#endif
}
public:
bool m_stopBeforeExec = false;
QVector<int> m_stdioRedirections;
};
HostProcess::HostProcess()
: m_process(new HostQProcess)
{
m_process->setProcessChannelMode(QProcess::ForwardedChannels);
m_process->setInputChannelMode(QProcess::ForwardedInputChannel);
}
HostProcess::~HostProcess()
{
m_process->disconnect(this);
delete m_process;
}
void HostProcess::start(const QString &program, const QStringList &arguments)
{
connect(m_process, &QProcess::started, this, [this]() {
// we to cache the pid in order to have it available after the process crashed
m_pid = m_process->processId();
emit started();
});
connect(m_process, static_cast<void (QProcess::*)(QProcess::ProcessError)>(&QProcess::error),
this, [this](QProcess::ProcessError error) {
emit errorOccured(static_cast<Am::ProcessError>(error));
});
connect(m_process, static_cast<void (QProcess::*)(int,QProcess::ExitStatus)>(&QProcess::finished),
this, [this](int exitCode, QProcess::ExitStatus exitStatus) {
emit finished(exitCode, static_cast<Am::ExitStatus>(exitStatus));
});
connect(m_process, &QProcess::stateChanged,
this, [this](QProcess::ProcessState newState) {
emit stateChanged(static_cast<Am::RunState>(newState));
});
#if defined(Q_OS_UNIX)
// make sure that the redirection fds do not have a close-on-exec flag, since we need them
// in the child process.
for (int fd : qAsConst(m_process->m_stdioRedirections)) {
if (fd < 0)
continue;
int flags = fcntl(fd, F_GETFD);
if (flags & FD_CLOEXEC)
fcntl(fd, F_SETFD, flags & ~FD_CLOEXEC);
}
#endif
m_process->start(program, arguments);
#if defined(Q_OS_UNIX)
// we are forked now and the child process has received a copy of all redirected fds
// now it's time to close our fds, since we don't need them anymore (plus we would block
// the tty where they originated from)
for (int fd : qAsConst(m_process->m_stdioRedirections)) {
if (fd >= 0)
::close(fd);
}
#endif
}
void HostProcess::setWorkingDirectory(const QString &dir)
{
m_process->setWorkingDirectory(dir);
}
void HostProcess::setProcessEnvironment(const QProcessEnvironment &environment)
{
m_process->setProcessEnvironment(environment);
}
void HostProcess::kill()
{
m_process->kill();
}
void HostProcess::terminate()
{
m_process->terminate();
}
qint64 HostProcess::processId() const
{
return m_pid;
}
Am::RunState HostProcess::state() const
{
return static_cast<Am::RunState>(m_process->state());
}
void HostProcess::setStdioRedirections(const QVector<int> &stdioRedirections)
{
m_process->m_stdioRedirections = stdioRedirections;
}
void HostProcess::setStopBeforeExec(bool stopBeforeExec)
{
m_process->m_stopBeforeExec = stopBeforeExec;
}
ProcessContainer::ProcessContainer(ProcessContainerManager *manager, AbstractApplication *app,
const QVector<int> &stdioRedirections,
const QMap<QString, QString> &debugWrapperEnvironment,
const QStringList &debugWrapperCommand)
: AbstractContainer(manager, app)
, m_stdioRedirections(stdioRedirections)
, m_debugWrapperEnvironment(debugWrapperEnvironment)
, m_debugWrapperCommand(debugWrapperCommand)
{ }
ProcessContainer::~ProcessContainer()
{ }
QString ProcessContainer::controlGroup() const
{
return m_currentControlGroup;
}
bool ProcessContainer::setControlGroup(const QString &groupName)
{
if (groupName == m_currentControlGroup)
return true;
QVariantMap map = m_manager->configuration().value(qSL("controlGroups")).toMap();
auto git = map.constFind(groupName);
if (git != map.constEnd()) {
QVariantMap mapping = (*git).toMap();
QByteArray pidString = QByteArray::number(m_process->processId());
pidString.append('\n');
for (auto it = mapping.cbegin(); it != mapping.cend(); ++it) {
const QString &resource = it.key();
const QString &userclass = it.value().toString();
//qWarning() << "Setting cgroup for" << m_program << ", pid" << m_process->processId() << ":" << resource << "->" << userclass;
QString file = QString(qSL("/sys/fs/cgroup/%1/%2/cgroup.procs")).arg(resource, userclass);
QFile f(file);
bool ok = f.open(QFile::WriteOnly);
ok = ok && (f.write(pidString) == pidString.size());
if (!ok) {
qWarning() << "Failed setting cgroup for" << m_program << ", pid" << m_process->processId() << ":" << resource << "->" << userclass;
return false;
}
if (resource == qSL("memory")) {
if (!m_memWatcher) {
m_memWatcher = new MemoryWatcher(this);
connect(m_memWatcher, &MemoryWatcher::memoryLow,
this, &ProcessContainer::memoryLowWarning);
connect(m_memWatcher, &MemoryWatcher::memoryCritical,
this, &ProcessContainer::memoryCriticalWarning);
}
m_memWatcher->startWatching(userclass);
}
}
m_currentControlGroup = groupName;
return true;
}
return false;
}
bool ProcessContainer::isReady()
{
return true;
}
AbstractContainerProcess *ProcessContainer::start(const QStringList &arguments,
const QMap<QString, QString> &runtimeEnvironment,
const QVariantMap &amConfig)
{
Q_UNUSED(amConfig)
if (m_process) {
qWarning() << "Process" << m_program << "is already started and cannot be started again";
return nullptr;
}
if (!QFile::exists(m_program)) {
qCWarning(LogSystem) << "Program" << m_program << "not found";
return nullptr;
}
QProcessEnvironment penv = QProcessEnvironment::systemEnvironment();
for (auto it = runtimeEnvironment.cbegin(); it != runtimeEnvironment.cend(); ++it) {
if (it.value().isEmpty())
penv.remove(it.key());
else
penv.insert(it.key(), it.value());
}
for (auto it = m_debugWrapperEnvironment.cbegin(); it != m_debugWrapperEnvironment.cend(); ++it) {
if (it.value().isEmpty())
penv.remove(it.key());
else
penv.insert(it.key(), it.value());
}
HostProcess *process = new HostProcess();
process->setWorkingDirectory(m_baseDirectory);
process->setProcessEnvironment(penv);
process->setStopBeforeExec(configuration().value(qSL("stopBeforeExec")).toBool());
process->setStdioRedirections(m_stdioRedirections);
QString command = m_program;
QStringList args = arguments;
if (!m_debugWrapperCommand.isEmpty()) {
auto cmd = DebugWrapper::substituteCommand(m_debugWrapperCommand, m_program, arguments);
command = cmd.takeFirst();
args = cmd;
}
qCDebug(LogSystem) << "Running command:" << command << "arguments:" << args;
process->start(command, args);
m_process = process;
setControlGroup(configuration().value(qSL("defaultControlGroup")).toString());
return process;
}
ProcessContainerManager::ProcessContainerManager(QObject *parent)
: AbstractContainerManager(defaultIdentifier(), parent)
{ }
ProcessContainerManager::ProcessContainerManager(const QString &id, QObject *parent)
: AbstractContainerManager(id, parent)
{ }
QString ProcessContainerManager::defaultIdentifier()
{
return qSL("process");
}
bool ProcessContainerManager::supportsQuickLaunch() const
{
return true;
}
AbstractContainer *ProcessContainerManager::create(AbstractApplication *app, const QVector<int> &stdioRedirections,
const QMap<QString, QString> &debugWrapperEnvironment,
const QStringList &debugWrapperCommand)
{
return new ProcessContainer(this, app, stdioRedirections, debugWrapperEnvironment, debugWrapperCommand);
}
QT_END_NAMESPACE_AM
|