2013-08-08 14:05:11 +02:00
|
|
|
/****************************************************************************
|
|
|
|
|
**
|
2016-01-15 14:57:40 +01:00
|
|
|
** Copyright (C) 2016 The Qt Company Ltd.
|
|
|
|
|
** Contact: https://www.qt.io/licensing/
|
2013-08-08 14:05:11 +02:00
|
|
|
**
|
|
|
|
|
** This file is part of Qt Creator.
|
|
|
|
|
**
|
|
|
|
|
** Commercial License Usage
|
|
|
|
|
** Licensees holding valid commercial Qt 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
|
2016-01-15 14:57:40 +01:00
|
|
|
** 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.
|
2013-08-08 14:05:11 +02:00
|
|
|
**
|
2016-01-15 14:57:40 +01:00
|
|
|
** GNU General Public License Usage
|
|
|
|
|
** Alternatively, this file may be used under the terms of the GNU
|
|
|
|
|
** General Public License version 3 as published by the Free Software
|
|
|
|
|
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
|
|
|
|
|
** 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-3.0.html.
|
2013-08-08 14:05:11 +02:00
|
|
|
**
|
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
|
|
#include "sshdeviceprocess.h"
|
|
|
|
|
|
|
|
|
|
#include "idevice.h"
|
2016-01-27 18:25:13 +01:00
|
|
|
#include "../runnables.h"
|
2013-08-08 14:05:11 +02:00
|
|
|
|
|
|
|
|
#include <ssh/sshconnection.h>
|
|
|
|
|
#include <ssh/sshconnectionmanager.h>
|
|
|
|
|
#include <ssh/sshremoteprocess.h>
|
|
|
|
|
#include <utils/environment.h>
|
|
|
|
|
#include <utils/qtcassert.h>
|
|
|
|
|
|
|
|
|
|
#include <QString>
|
2013-10-16 12:03:46 +02:00
|
|
|
#include <QTimer>
|
2013-08-08 14:05:11 +02:00
|
|
|
|
|
|
|
|
namespace ProjectExplorer {
|
|
|
|
|
|
|
|
|
|
class SshDeviceProcess::SshDeviceProcessPrivate
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
SshDeviceProcessPrivate(SshDeviceProcess *q) : q(q) {}
|
|
|
|
|
|
|
|
|
|
SshDeviceProcess * const q;
|
2017-08-25 15:50:42 +03:00
|
|
|
bool serverSupportsSignals = false;
|
|
|
|
|
QSsh::SshConnection *connection = nullptr;
|
2013-08-08 14:05:11 +02:00
|
|
|
QSsh::SshRemoteProcess::Ptr process;
|
2016-01-27 18:25:13 +01:00
|
|
|
StandardRunnable runnable;
|
2013-08-08 14:05:11 +02:00
|
|
|
QString errorMessage;
|
|
|
|
|
QSsh::SshRemoteProcess::ExitStatus exitStatus;
|
2013-10-16 12:03:46 +02:00
|
|
|
DeviceProcessSignalOperation::Ptr killOperation;
|
|
|
|
|
QTimer killTimer;
|
2013-08-08 14:05:11 +02:00
|
|
|
QByteArray stdOut;
|
|
|
|
|
QByteArray stdErr;
|
2017-08-25 15:50:42 +03:00
|
|
|
int exitCode = -1;
|
|
|
|
|
enum State { Inactive, Connecting, Connected, ProcessRunning } state = Inactive;
|
2013-08-08 14:05:11 +02:00
|
|
|
|
|
|
|
|
void setState(State newState);
|
|
|
|
|
void doSignal(QSsh::SshRemoteProcess::Signal signal);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
SshDeviceProcess::SshDeviceProcess(const IDevice::ConstPtr &device, QObject *parent)
|
|
|
|
|
: DeviceProcess(device, parent), d(new SshDeviceProcessPrivate(this))
|
|
|
|
|
{
|
2016-01-29 16:38:37 +02:00
|
|
|
connect(&d->killTimer, &QTimer::timeout, this, &SshDeviceProcess::handleKillOperationTimeout);
|
2013-08-08 14:05:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
SshDeviceProcess::~SshDeviceProcess()
|
|
|
|
|
{
|
|
|
|
|
d->setState(SshDeviceProcessPrivate::Inactive);
|
|
|
|
|
delete d;
|
|
|
|
|
}
|
|
|
|
|
|
2016-01-27 18:25:13 +01:00
|
|
|
void SshDeviceProcess::start(const Runnable &runnable)
|
2013-08-08 14:05:11 +02:00
|
|
|
{
|
|
|
|
|
QTC_ASSERT(d->state == SshDeviceProcessPrivate::Inactive, return);
|
2016-04-05 08:11:42 +02:00
|
|
|
if (!runnable.is<StandardRunnable>()) {
|
|
|
|
|
d->errorMessage = tr("Internal error");
|
|
|
|
|
error(QProcess::FailedToStart);
|
|
|
|
|
return;
|
|
|
|
|
}
|
2013-08-08 14:05:11 +02:00
|
|
|
d->setState(SshDeviceProcessPrivate::Connecting);
|
|
|
|
|
|
|
|
|
|
d->errorMessage.clear();
|
|
|
|
|
d->exitCode = -1;
|
2016-01-27 18:25:13 +01:00
|
|
|
d->runnable = runnable.as<StandardRunnable>();
|
2013-09-10 18:52:17 +02:00
|
|
|
d->connection = QSsh::acquireConnection(device()->sshParameters());
|
2016-01-29 16:38:37 +02:00
|
|
|
connect(d->connection, &QSsh::SshConnection::error,
|
|
|
|
|
this, &SshDeviceProcess::handleConnectionError);
|
|
|
|
|
connect(d->connection, &QSsh::SshConnection::disconnected,
|
|
|
|
|
this, &SshDeviceProcess::handleDisconnected);
|
2013-08-08 14:05:11 +02:00
|
|
|
if (d->connection->state() == QSsh::SshConnection::Connected) {
|
|
|
|
|
handleConnected();
|
|
|
|
|
} else {
|
2016-01-29 16:38:37 +02:00
|
|
|
connect(d->connection, &QSsh::SshConnection::connected,
|
|
|
|
|
this, &SshDeviceProcess::handleConnected);
|
2013-08-08 14:05:11 +02:00
|
|
|
if (d->connection->state() == QSsh::SshConnection::Unconnected)
|
|
|
|
|
d->connection->connectToHost();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void SshDeviceProcess::interrupt()
|
|
|
|
|
{
|
|
|
|
|
QTC_ASSERT(d->state == SshDeviceProcessPrivate::ProcessRunning, return);
|
|
|
|
|
d->doSignal(QSsh::SshRemoteProcess::IntSignal);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void SshDeviceProcess::terminate()
|
|
|
|
|
{
|
|
|
|
|
d->doSignal(QSsh::SshRemoteProcess::TermSignal);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void SshDeviceProcess::kill()
|
|
|
|
|
{
|
|
|
|
|
d->doSignal(QSsh::SshRemoteProcess::KillSignal);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QProcess::ProcessState SshDeviceProcess::state() const
|
|
|
|
|
{
|
|
|
|
|
switch (d->state) {
|
|
|
|
|
case SshDeviceProcessPrivate::Inactive:
|
|
|
|
|
return QProcess::NotRunning;
|
|
|
|
|
case SshDeviceProcessPrivate::Connecting:
|
|
|
|
|
case SshDeviceProcessPrivate::Connected:
|
|
|
|
|
return QProcess::Starting;
|
|
|
|
|
case SshDeviceProcessPrivate::ProcessRunning:
|
|
|
|
|
return QProcess::Running;
|
|
|
|
|
default:
|
|
|
|
|
QTC_CHECK(false);
|
|
|
|
|
return QProcess::NotRunning;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QProcess::ExitStatus SshDeviceProcess::exitStatus() const
|
|
|
|
|
{
|
|
|
|
|
return d->exitStatus == QSsh::SshRemoteProcess::NormalExit
|
|
|
|
|
? QProcess::NormalExit : QProcess::CrashExit;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int SshDeviceProcess::exitCode() const
|
|
|
|
|
{
|
|
|
|
|
return d->exitCode;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QString SshDeviceProcess::errorString() const
|
|
|
|
|
{
|
|
|
|
|
return d->errorMessage;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QByteArray SshDeviceProcess::readAllStandardOutput()
|
|
|
|
|
{
|
|
|
|
|
const QByteArray data = d->stdOut;
|
|
|
|
|
d->stdOut.clear();
|
|
|
|
|
return data;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QByteArray SshDeviceProcess::readAllStandardError()
|
|
|
|
|
{
|
|
|
|
|
const QByteArray data = d->stdErr;
|
|
|
|
|
d->stdErr.clear();
|
|
|
|
|
return data;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void SshDeviceProcess::setSshServerSupportsSignals(bool signalsSupported)
|
|
|
|
|
{
|
|
|
|
|
d->serverSupportsSignals = signalsSupported;
|
|
|
|
|
}
|
|
|
|
|
|
2016-04-21 11:49:10 +02:00
|
|
|
qint64 SshDeviceProcess::processId() const
|
|
|
|
|
{
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2013-08-08 14:05:11 +02:00
|
|
|
void SshDeviceProcess::handleConnected()
|
|
|
|
|
{
|
|
|
|
|
QTC_ASSERT(d->state == SshDeviceProcessPrivate::Connecting, return);
|
|
|
|
|
d->setState(SshDeviceProcessPrivate::Connected);
|
|
|
|
|
|
2016-01-27 18:25:13 +01:00
|
|
|
d->process = d->connection->createRemoteProcess(fullCommandLine(d->runnable).toUtf8());
|
2016-01-29 16:38:37 +02:00
|
|
|
connect(d->process.data(), &QSsh::SshRemoteProcess::started, this, &SshDeviceProcess::handleProcessStarted);
|
|
|
|
|
connect(d->process.data(), &QSsh::SshRemoteProcess::closed, this, &SshDeviceProcess::handleProcessFinished);
|
|
|
|
|
connect(d->process.data(), &QSsh::SshRemoteProcess::readyReadStandardOutput, this, &SshDeviceProcess::handleStdout);
|
|
|
|
|
connect(d->process.data(), &QSsh::SshRemoteProcess::readyReadStandardError, this, &SshDeviceProcess::handleStderr);
|
2013-08-08 14:05:11 +02:00
|
|
|
|
|
|
|
|
d->process->clearEnvironment();
|
2016-01-27 18:25:13 +01:00
|
|
|
const Utils::Environment env = d->runnable.environment;
|
2013-08-08 14:05:11 +02:00
|
|
|
for (Utils::Environment::const_iterator it = env.constBegin(); it != env.constEnd(); ++it)
|
|
|
|
|
d->process->addToEnvironment(env.key(it).toUtf8(), env.value(it).toUtf8());
|
|
|
|
|
d->process->start();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void SshDeviceProcess::handleConnectionError()
|
|
|
|
|
{
|
|
|
|
|
QTC_ASSERT(d->state != SshDeviceProcessPrivate::Inactive, return);
|
|
|
|
|
|
|
|
|
|
d->errorMessage = d->connection->errorString();
|
|
|
|
|
handleDisconnected();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void SshDeviceProcess::handleDisconnected()
|
|
|
|
|
{
|
|
|
|
|
QTC_ASSERT(d->state != SshDeviceProcessPrivate::Inactive, return);
|
|
|
|
|
const SshDeviceProcessPrivate::State oldState = d->state;
|
|
|
|
|
d->setState(SshDeviceProcessPrivate::Inactive);
|
|
|
|
|
switch (oldState) {
|
|
|
|
|
case SshDeviceProcessPrivate::Connecting:
|
|
|
|
|
case SshDeviceProcessPrivate::Connected:
|
|
|
|
|
emit error(QProcess::FailedToStart);
|
|
|
|
|
break;
|
|
|
|
|
case SshDeviceProcessPrivate::ProcessRunning:
|
2013-10-16 12:03:46 +02:00
|
|
|
d->exitStatus = QSsh::SshRemoteProcess::CrashExit;
|
2013-08-08 14:05:11 +02:00
|
|
|
emit finished();
|
|
|
|
|
default:
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void SshDeviceProcess::handleProcessStarted()
|
|
|
|
|
{
|
|
|
|
|
QTC_ASSERT(d->state == SshDeviceProcessPrivate::Connected, return);
|
|
|
|
|
|
|
|
|
|
d->setState(SshDeviceProcessPrivate::ProcessRunning);
|
|
|
|
|
emit started();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void SshDeviceProcess::handleProcessFinished(int exitStatus)
|
|
|
|
|
{
|
|
|
|
|
d->exitStatus = static_cast<QSsh::SshRemoteProcess::ExitStatus>(exitStatus);
|
|
|
|
|
switch (d->exitStatus) {
|
|
|
|
|
case QSsh::SshRemoteProcess::FailedToStart:
|
|
|
|
|
QTC_ASSERT(d->state == SshDeviceProcessPrivate::Connected, return);
|
|
|
|
|
break;
|
|
|
|
|
case QSsh::SshRemoteProcess::CrashExit:
|
|
|
|
|
QTC_ASSERT(d->state == SshDeviceProcessPrivate::ProcessRunning, return);
|
|
|
|
|
break;
|
|
|
|
|
case QSsh::SshRemoteProcess::NormalExit:
|
|
|
|
|
QTC_ASSERT(d->state == SshDeviceProcessPrivate::ProcessRunning, return);
|
|
|
|
|
d->exitCode = d->process->exitCode();
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
QTC_ASSERT(false, return);
|
|
|
|
|
}
|
|
|
|
|
d->errorMessage = d->process->errorString();
|
|
|
|
|
d->setState(SshDeviceProcessPrivate::Inactive);
|
|
|
|
|
emit finished();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void SshDeviceProcess::handleStdout()
|
|
|
|
|
{
|
|
|
|
|
d->stdOut += d->process->readAllStandardOutput();
|
|
|
|
|
emit readyReadStandardOutput();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void SshDeviceProcess::handleStderr()
|
|
|
|
|
{
|
|
|
|
|
d->stdErr += d->process->readAllStandardError();
|
|
|
|
|
emit readyReadStandardError();
|
|
|
|
|
}
|
|
|
|
|
|
2013-10-16 12:03:46 +02:00
|
|
|
void SshDeviceProcess::handleKillOperationFinished(const QString &errorMessage)
|
|
|
|
|
{
|
|
|
|
|
QTC_ASSERT(d->state == SshDeviceProcessPrivate::ProcessRunning, return);
|
|
|
|
|
if (errorMessage.isEmpty()) // Process will finish as expected; nothing to do here.
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
d->exitStatus = QSsh::SshRemoteProcess::CrashExit; // Not entirely true, but it will get the message across.
|
|
|
|
|
d->errorMessage = tr("Failed to kill remote process: %1").arg(errorMessage);
|
|
|
|
|
d->setState(SshDeviceProcessPrivate::Inactive);
|
|
|
|
|
emit finished();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void SshDeviceProcess::handleKillOperationTimeout()
|
|
|
|
|
{
|
|
|
|
|
d->exitStatus = QSsh::SshRemoteProcess::CrashExit; // Not entirely true, but it will get the message across.
|
|
|
|
|
d->errorMessage = tr("Timeout waiting for remote process to finish.");
|
|
|
|
|
d->setState(SshDeviceProcessPrivate::Inactive);
|
|
|
|
|
emit finished();
|
|
|
|
|
}
|
|
|
|
|
|
2016-01-27 18:25:13 +01:00
|
|
|
QString SshDeviceProcess::fullCommandLine(const StandardRunnable &runnable) const
|
2013-08-08 14:05:11 +02:00
|
|
|
{
|
2016-01-27 18:25:13 +01:00
|
|
|
QString cmdLine = runnable.executable;
|
|
|
|
|
if (!runnable.commandLineArguments.isEmpty())
|
|
|
|
|
cmdLine.append(QLatin1Char(' ')).append(runnable.commandLineArguments);
|
2013-08-08 14:05:11 +02:00
|
|
|
return cmdLine;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void SshDeviceProcess::SshDeviceProcessPrivate::doSignal(QSsh::SshRemoteProcess::Signal signal)
|
|
|
|
|
{
|
|
|
|
|
switch (state) {
|
|
|
|
|
case SshDeviceProcessPrivate::Inactive:
|
|
|
|
|
QTC_ASSERT(false, return);
|
|
|
|
|
break;
|
|
|
|
|
case SshDeviceProcessPrivate::Connecting:
|
|
|
|
|
errorMessage = tr("Terminated by request.");
|
|
|
|
|
setState(SshDeviceProcessPrivate::Inactive);
|
|
|
|
|
emit q->error(QProcess::FailedToStart);
|
|
|
|
|
break;
|
|
|
|
|
case SshDeviceProcessPrivate::Connected:
|
|
|
|
|
case SshDeviceProcessPrivate::ProcessRunning:
|
|
|
|
|
if (serverSupportsSignals) {
|
|
|
|
|
process->sendSignal(signal);
|
|
|
|
|
} else {
|
2013-09-16 15:30:30 +02:00
|
|
|
DeviceProcessSignalOperation::Ptr signalOperation = q->device()->signalOperation();
|
2016-04-21 11:49:10 +02:00
|
|
|
quint64 processId = q->processId();
|
2013-10-16 12:03:46 +02:00
|
|
|
if (signal == QSsh::SshRemoteProcess::IntSignal) {
|
2016-04-21 11:49:10 +02:00
|
|
|
if (processId != 0)
|
|
|
|
|
signalOperation->interruptProcess(processId);
|
|
|
|
|
else
|
|
|
|
|
signalOperation->interruptProcess(runnable.executable);
|
2013-10-16 12:03:46 +02:00
|
|
|
} else {
|
|
|
|
|
if (killOperation) // We are already in the process of killing the app.
|
|
|
|
|
return;
|
|
|
|
|
killOperation = signalOperation;
|
2016-01-29 16:38:37 +02:00
|
|
|
connect(signalOperation.data(), &DeviceProcessSignalOperation::finished, q,
|
|
|
|
|
&SshDeviceProcess::handleKillOperationFinished);
|
2013-10-16 12:03:46 +02:00
|
|
|
killTimer.start(5000);
|
2016-04-21 11:49:10 +02:00
|
|
|
if (processId != 0)
|
|
|
|
|
signalOperation->killProcess(processId);
|
|
|
|
|
else
|
|
|
|
|
signalOperation->killProcess(runnable.executable);
|
2013-10-16 12:03:46 +02:00
|
|
|
}
|
2013-08-08 14:05:11 +02:00
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void SshDeviceProcess::SshDeviceProcessPrivate::setState(SshDeviceProcess::SshDeviceProcessPrivate::State newState)
|
|
|
|
|
{
|
|
|
|
|
if (state == newState)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
state = newState;
|
|
|
|
|
if (state != Inactive)
|
|
|
|
|
return;
|
|
|
|
|
|
2013-10-16 12:03:46 +02:00
|
|
|
if (killOperation) {
|
|
|
|
|
killOperation->disconnect(q);
|
|
|
|
|
killOperation.clear();
|
|
|
|
|
}
|
|
|
|
|
killTimer.stop();
|
2013-08-08 14:05:11 +02:00
|
|
|
if (process)
|
|
|
|
|
process->disconnect(q);
|
|
|
|
|
if (connection) {
|
|
|
|
|
connection->disconnect(q);
|
2013-09-10 18:52:17 +02:00
|
|
|
QSsh::releaseConnection(connection);
|
2013-08-08 14:05:11 +02:00
|
|
|
connection = 0;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2013-09-09 17:32:21 +02:00
|
|
|
qint64 SshDeviceProcess::write(const QByteArray &data)
|
|
|
|
|
{
|
|
|
|
|
return d->process->write(data);
|
|
|
|
|
}
|
|
|
|
|
|
2013-08-08 14:05:11 +02:00
|
|
|
} // namespace ProjectExplorer
|