summaryrefslogtreecommitdiffstats
path: root/src/processmanager/qremoteprocessbackend.cpp
blob: cfc58e92c768d6412fc8061f05aff318b0a1739b (plain)
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
/****************************************************************************
**
** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies).
** Contact: http://www.qt-project.org/legal
**
** This file is part of the QtAddOn.JsonStream module of the Qt.
**
** $QT_BEGIN_LICENSE:LGPL$
** GNU Lesser General Public License Usage
** This file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Digia gives you certain additional
** rights. These rights are described in the Digia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3.0 as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL included in the
** packaging of this file. Please review the following information to
** ensure the GNU General Public License version 3.0 requirements will be
** met: http://www.gnu.org/copyleft/gpl.html.
**
**
** $QT_END_LICENSE$
**
****************************************************************************/

#include "qremoteprocessbackend.h"
#include "qremoteprotocol.h"
#include "qprocutils.h"
#include <sys/resource.h>
#include <errno.h>
#include <signal.h>
#include <QDebug>

QT_BEGIN_NAMESPACE_PROCESSMANAGER

/*!
    \class QRemoteProcessBackend

    \brief The QRemoteProcessBackend class handles a process started by another process.
    \inmodule QtProcessManager

    The QRemoteProcessBackend is used to control a process that was started by
    a separate "controller" process.  The QRemoteProcessBackendFactory object handles
    the pipe or socket communication with the remote controller.  Communication is
    carried over serialized JSON messages.
*/

/*!
    Construct a QRemoteProcessBackend with QProcessInfo \a info, \a factory,
    \a id, and optional \a parent.
*/

QRemoteProcessBackend::QRemoteProcessBackend(const QProcessInfo& info,
                                           QRemoteProcessBackendFactory *factory,
                                           int id,
                                           QObject *parent)
    : QProcessBackend(info, parent)
    , m_factory(factory)
    , m_state(QProcess::NotRunning)
    , m_pid(-1)
    , m_id(id)
{
    Q_ASSERT(factory);
}

/*!
  Destroy this process object.
*/

QRemoteProcessBackend::~QRemoteProcessBackend()
{
    if (m_factory)
        m_factory->backendDestroyed(m_id);
}

/*!
    Returns the PID of this process if it is starting or running.
    Return the default value if it is not.
*/

Q_PID QRemoteProcessBackend::pid() const
{
    if (m_pid != -1)
        return m_pid;
    return QProcessBackend::pid();
}

/*!
    Return the actual process priority (if running)
*/

qint32 QRemoteProcessBackend::actualPriority() const
{
    if (m_pid != -1) {
        errno = 0;   // getpriority can return -1, so we clear errno
        int result = getpriority(PRIO_PROCESS, m_pid);
        if (!errno)
            return result;
    }
    return QProcessBackend::actualPriority();
}

/*!
    Set the process priority to \a priority
*/

void QRemoteProcessBackend::setDesiredPriority(qint32 priority)
{
    QProcessBackend::setDesiredPriority(priority);
    if (m_factory && m_pid != -1) {
        QJsonObject object;
        object.insert(QRemoteProtocol::command(), QRemoteProtocol::set());
        object.insert(QRemoteProtocol::id(), m_id);
        object.insert(QRemoteProtocol::key(), QRemoteProtocol::priority());
        object.insert(QRemoteProtocol::value(), priority);
        m_factory->send(object);
    }
}

#if defined(Q_OS_LINUX)

/*!
    Return the process oomAdjustment
*/

qint32 QRemoteProcessBackend::actualOomAdjustment() const
{
    if (m_pid != -1) {
        bool ok;
        qint32 result = QProcUtils::oomAdjustment(m_pid, &ok);
        if (ok)
            return result;
        qWarning() << "Unable to read oom adjustment for" << m_pid;
    }
    return QProcessBackend::actualOomAdjustment();
}

/*!
    Set the process /proc/<pid>/oom_score_adj to \a oomAdjustment
*/

void QRemoteProcessBackend::setDesiredOomAdjustment(qint32 oomAdjustment)
{
    QProcessBackend::setDesiredOomAdjustment(oomAdjustment);
    if (m_factory && m_pid != -1) {
        QJsonObject object;
        object.insert(QRemoteProtocol::command(), QRemoteProtocol::set());
        object.insert(QRemoteProtocol::id(), m_id);
        object.insert(QRemoteProtocol::key(), QRemoteProtocol::oomAdjustment());
        object.insert(QRemoteProtocol::value(), oomAdjustment);
        m_factory->send(object);
    }
}

#endif // defined(Q_OS_LINUX)


/*!
    Returns the state of the process.
*/
QProcess::ProcessState QRemoteProcessBackend::state() const
{
    return m_state;
}

/*!
    Start the process running
*/

void QRemoteProcessBackend::start()
{
    if (m_factory) {
        QJsonObject object;
        object.insert(QRemoteProtocol::command(), QRemoteProtocol::start());
        object.insert(QRemoteProtocol::id(), m_id);
        object.insert(QRemoteProtocol::info(), QJsonValue::fromVariant(m_info.toMap()));
        m_factory->send(object);
    }
}

/*!
    Attempts to stop a process by giving it a \a timeout time to die, measured in milliseconds.
    If the process does not die in the given time limit, it is killed.
    \sa finished()
*/

void QRemoteProcessBackend::stop(int timeout)
{
    if (m_factory) {
        QJsonObject object;
        object.insert(QRemoteProtocol::command(), QRemoteProtocol::stop());
        object.insert(QRemoteProtocol::id(), m_id);
        object.insert(QRemoteProtocol::timeout(), timeout);
        m_factory->send(object);
    }
}

/*!
  Writes at most \a maxSize bytes of data from \a data to the device.
  Returns the number of bytes that were actually written, or -1 if an error occurred.
*/
qint64 QRemoteProcessBackend::write(const char *data, qint64 maxSize)
{
    if (m_factory) {
        QJsonObject object;
        object.insert(QRemoteProtocol::command(), QRemoteProtocol::write());
        object.insert(QRemoteProtocol::id(), m_id);
        object.insert(QRemoteProtocol::data(), QString::fromLatin1(QByteArray(data, maxSize).toBase64()));
        if (m_factory->send(object))
            return maxSize;
    }
    return -1;
}

/*!
   Returns the most recent error string
*/
QString QRemoteProcessBackend::errorString() const
{
    return m_errorString;
}


/*!
  Message received from the remote.
  The \a message is encoded in JSON format.
*/

void QRemoteProcessBackend::receive(const QJsonObject& message)
{
    QString event = message.value(QRemoteProtocol::event()).toString();
    if (event == QRemoteProtocol::started()) {
        m_pid = message.value(QRemoteProtocol::pid()).toDouble();
        emit started();
    }
    else if (event == QRemoteProtocol::error()) {
        m_errorString = message.value(QRemoteProtocol::errorString()).toString();
        emit error(static_cast<QProcess::ProcessError>(message.value(QRemoteProtocol::error()).toDouble()));
    }
    else if (event == QRemoteProtocol::finished()) {
        emit finished(message.value(QRemoteProtocol::exitCode()).toDouble(),
                      static_cast<QProcess::ExitStatus>(message.value(QRemoteProtocol::exitStatus()).toDouble()));
    }
    else if (event == QRemoteProtocol::stateChanged()) {
        m_state = static_cast<QProcess::ProcessState>(message.value(QRemoteProtocol::stateChanged()).toDouble());
        emit stateChanged(m_state);
    }
    else if (event == QRemoteProtocol::output()) {
        if (message.contains(QRemoteProtocol::standardout())) {
            handleStandardOutput(message.value(QRemoteProtocol::standardout()).toString().toLocal8Bit());
        }
        if (message.contains(QRemoteProtocol::standarderror())) {
            handleStandardError(message.value(QRemoteProtocol::standarderror()).toString().toLocal8Bit());
        }
    }
    else
        qDebug() << Q_FUNC_INFO << "unrecognized message" << message;
}

/*!
    \internal
*/

void QRemoteProcessBackend::killTimeout()
{
    if (m_factory) {
        QJsonObject object;
        object.insert(QRemoteProtocol::command(), QRemoteProtocol::signal());
        object.insert(QRemoteProtocol::id(), m_id);
        object.insert(QRemoteProtocol::signal(), SIGKILL);
        m_factory->send(object);
    }
}

/*!
    \internal
*/

void QRemoteProcessBackend::factoryDestroyed()
{
    m_factory = NULL;
    if (m_state != QProcess::NotRunning) {
        m_state = QProcess::NotRunning;
        emit error(QProcess::UnknownError);
    }
}

#include "moc_qremoteprocessbackend.cpp"

QT_END_NAMESPACE_PROCESSMANAGER