2012-10-02 09:12:39 +02:00
|
|
|
/****************************************************************************
|
2011-02-18 10:36:52 +01:00
|
|
|
**
|
2016-01-15 14:58:39 +01:00
|
|
|
** Copyright (C) 2016 The Qt Company Ltd.
|
|
|
|
|
** Contact: https://www.qt.io/licensing/
|
2011-02-18 10:36:52 +01:00
|
|
|
**
|
2012-10-02 09:12:39 +02:00
|
|
|
** This file is part of Qt Creator.
|
2011-02-18 10:36:52 +01:00
|
|
|
**
|
2012-10-02 09:12:39 +02:00
|
|
|
** 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:58:39 +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.
|
2011-02-18 10:36:52 +01:00
|
|
|
**
|
2016-01-15 14:58:39 +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.
|
2011-02-18 10:36:52 +01:00
|
|
|
**
|
2012-10-02 09:12:39 +02:00
|
|
|
****************************************************************************/
|
2011-02-18 10:36:52 +01:00
|
|
|
|
2010-10-12 11:07:06 +02:00
|
|
|
#include "sshremoteprocessrunner.h"
|
|
|
|
|
|
2011-03-09 12:07:35 +01:00
|
|
|
#include "sshconnectionmanager.h"
|
|
|
|
|
|
2018-11-23 11:07:57 +01:00
|
|
|
#include <utils/qtcassert.h>
|
2010-10-12 11:07:06 +02:00
|
|
|
|
2011-03-02 17:13:33 +01:00
|
|
|
/*!
|
2012-05-18 10:49:35 +02:00
|
|
|
\class QSsh::SshRemoteProcessRunner
|
2011-03-02 17:13:33 +01:00
|
|
|
|
2013-06-05 14:29:24 +02:00
|
|
|
\brief The SshRemoteProcessRunner class is a convenience class for
|
|
|
|
|
running a remote process over an SSH connection.
|
2011-03-02 17:13:33 +01:00
|
|
|
*/
|
|
|
|
|
|
2012-05-18 10:49:35 +02:00
|
|
|
namespace QSsh {
|
2011-04-06 10:31:27 +02:00
|
|
|
namespace Internal {
|
2011-11-17 10:56:27 +01:00
|
|
|
namespace {
|
|
|
|
|
enum State { Inactive, Connecting, Connected, ProcessRunning };
|
|
|
|
|
} // anonymous namespace
|
2010-10-12 11:07:06 +02:00
|
|
|
|
2011-11-17 10:56:27 +01:00
|
|
|
class SshRemoteProcessRunnerPrivate
|
2010-10-12 11:07:06 +02:00
|
|
|
{
|
|
|
|
|
public:
|
2011-11-17 10:56:27 +01:00
|
|
|
SshRemoteProcessRunnerPrivate() : m_state(Inactive) {}
|
2010-10-12 11:07:06 +02:00
|
|
|
|
2018-11-23 11:07:57 +01:00
|
|
|
SshRemoteProcessPtr m_process;
|
2012-05-29 13:22:33 +02:00
|
|
|
SshConnection *m_connection;
|
2011-04-06 10:26:45 +02:00
|
|
|
bool m_runInTerminal;
|
2019-05-21 16:59:29 +02:00
|
|
|
QString m_command;
|
2011-11-09 17:38:19 +01:00
|
|
|
QString m_lastConnectionErrorString;
|
2018-11-23 11:07:57 +01:00
|
|
|
QProcess::ExitStatus m_exitStatus;
|
2012-06-08 09:42:32 +02:00
|
|
|
QByteArray m_stdout;
|
|
|
|
|
QByteArray m_stderr;
|
2011-11-30 14:39:17 +01:00
|
|
|
int m_exitCode;
|
|
|
|
|
QString m_processErrorString;
|
2011-11-17 10:56:27 +01:00
|
|
|
State m_state;
|
2010-10-12 11:07:06 +02:00
|
|
|
};
|
|
|
|
|
|
2011-11-17 10:56:27 +01:00
|
|
|
} // namespace Internal
|
|
|
|
|
|
|
|
|
|
using namespace Internal;
|
2010-10-12 11:07:06 +02:00
|
|
|
|
2011-11-17 10:56:27 +01:00
|
|
|
SshRemoteProcessRunner::SshRemoteProcessRunner(QObject *parent)
|
|
|
|
|
: QObject(parent), d(new SshRemoteProcessRunnerPrivate)
|
2010-10-12 11:07:06 +02:00
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2011-11-17 10:56:27 +01:00
|
|
|
SshRemoteProcessRunner::~SshRemoteProcessRunner()
|
2011-03-09 12:07:35 +01:00
|
|
|
{
|
2011-11-17 10:56:27 +01:00
|
|
|
disconnect();
|
2011-03-09 12:07:35 +01:00
|
|
|
setState(Inactive);
|
2011-11-17 10:56:27 +01:00
|
|
|
delete d;
|
2011-03-09 12:07:35 +01:00
|
|
|
}
|
|
|
|
|
|
2019-05-21 16:59:29 +02:00
|
|
|
void SshRemoteProcessRunner::run(const QString &command, const SshConnectionParameters &sshParams)
|
2011-04-06 10:26:45 +02:00
|
|
|
{
|
2018-11-23 11:07:57 +01:00
|
|
|
QTC_ASSERT(d->m_state == Inactive, return);
|
2011-11-30 14:39:17 +01:00
|
|
|
|
2011-11-17 10:56:27 +01:00
|
|
|
d->m_runInTerminal = false;
|
|
|
|
|
runInternal(command, sshParams);
|
2011-04-06 10:26:45 +02:00
|
|
|
}
|
|
|
|
|
|
2019-05-21 16:59:29 +02:00
|
|
|
void SshRemoteProcessRunner::runInTerminal(const QString &command,
|
2018-11-23 11:07:57 +01:00
|
|
|
const SshConnectionParameters &sshParams)
|
2011-04-06 10:26:45 +02:00
|
|
|
{
|
2011-11-17 10:56:27 +01:00
|
|
|
d->m_runInTerminal = true;
|
|
|
|
|
runInternal(command, sshParams);
|
2011-04-06 10:26:45 +02:00
|
|
|
}
|
|
|
|
|
|
2019-05-21 16:59:29 +02:00
|
|
|
void SshRemoteProcessRunner::runInternal(const QString &command,
|
2011-11-09 14:19:50 +01:00
|
|
|
const SshConnectionParameters &sshParams)
|
2010-10-12 11:07:06 +02:00
|
|
|
{
|
|
|
|
|
setState(Connecting);
|
|
|
|
|
|
2011-11-17 10:56:27 +01:00
|
|
|
d->m_lastConnectionErrorString.clear();
|
2011-11-30 14:39:17 +01:00
|
|
|
d->m_processErrorString.clear();
|
|
|
|
|
d->m_exitCode = -1;
|
2011-11-17 10:56:27 +01:00
|
|
|
d->m_command = command;
|
2021-09-09 15:29:28 +02:00
|
|
|
d->m_connection = SshConnectionManager::acquireConnection(sshParams);
|
2018-11-23 11:07:57 +01:00
|
|
|
connect(d->m_connection, &SshConnection::errorOccurred,
|
2015-02-04 10:11:46 +01:00
|
|
|
this, &SshRemoteProcessRunner::handleConnectionError);
|
|
|
|
|
connect(d->m_connection, &SshConnection::disconnected,
|
|
|
|
|
this, &SshRemoteProcessRunner::handleDisconnected);
|
2011-11-17 10:56:27 +01:00
|
|
|
if (d->m_connection->state() == SshConnection::Connected) {
|
2010-10-12 11:07:06 +02:00
|
|
|
handleConnected();
|
|
|
|
|
} else {
|
2015-02-04 10:11:46 +01:00
|
|
|
connect(d->m_connection, &SshConnection::connected, this, &SshRemoteProcessRunner::handleConnected);
|
2011-11-17 10:56:27 +01:00
|
|
|
if (d->m_connection->state() == SshConnection::Unconnected)
|
|
|
|
|
d->m_connection->connectToHost();
|
2010-10-12 11:07:06 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2011-11-17 10:56:27 +01:00
|
|
|
void SshRemoteProcessRunner::handleConnected()
|
2010-10-12 11:07:06 +02:00
|
|
|
{
|
2018-11-23 11:07:57 +01:00
|
|
|
QTC_ASSERT(d->m_state == Connecting, return);
|
2010-10-12 11:07:06 +02:00
|
|
|
setState(Connected);
|
|
|
|
|
|
2011-11-17 10:56:27 +01:00
|
|
|
d->m_process = d->m_connection->createRemoteProcess(d->m_command);
|
2018-11-23 11:07:57 +01:00
|
|
|
connect(d->m_process.get(), &SshRemoteProcess::started,
|
2016-06-07 22:04:26 +03:00
|
|
|
this, &SshRemoteProcessRunner::handleProcessStarted);
|
2018-11-23 11:07:57 +01:00
|
|
|
connect(d->m_process.get(), &SshRemoteProcess::done,
|
2016-06-07 22:04:26 +03:00
|
|
|
this, &SshRemoteProcessRunner::handleProcessFinished);
|
2018-11-23 11:07:57 +01:00
|
|
|
connect(d->m_process.get(), &SshRemoteProcess::readyReadStandardOutput,
|
2016-06-07 22:04:26 +03:00
|
|
|
this, &SshRemoteProcessRunner::handleStdout);
|
2018-11-23 11:07:57 +01:00
|
|
|
connect(d->m_process.get(), &SshRemoteProcess::readyReadStandardError,
|
2016-06-07 22:04:26 +03:00
|
|
|
this, &SshRemoteProcessRunner::handleStderr);
|
2011-11-17 10:56:27 +01:00
|
|
|
if (d->m_runInTerminal)
|
2018-11-23 11:07:57 +01:00
|
|
|
d->m_process->requestTerminal();
|
2011-11-17 10:56:27 +01:00
|
|
|
d->m_process->start();
|
2010-10-12 11:07:06 +02:00
|
|
|
}
|
|
|
|
|
|
2018-11-23 11:07:57 +01:00
|
|
|
void SshRemoteProcessRunner::handleConnectionError()
|
2010-10-12 11:07:06 +02:00
|
|
|
{
|
2011-11-17 10:56:27 +01:00
|
|
|
d->m_lastConnectionErrorString = d->m_connection->errorString();
|
2010-10-12 11:07:06 +02:00
|
|
|
handleDisconnected();
|
2011-11-09 17:38:19 +01:00
|
|
|
emit connectionError();
|
2010-10-12 11:07:06 +02:00
|
|
|
}
|
|
|
|
|
|
2011-11-17 10:56:27 +01:00
|
|
|
void SshRemoteProcessRunner::handleDisconnected()
|
2010-10-12 11:07:06 +02:00
|
|
|
{
|
2018-11-23 11:07:57 +01:00
|
|
|
QTC_ASSERT(d->m_state == Connecting || d->m_state == Connected || d->m_state == ProcessRunning,
|
|
|
|
|
return);
|
2010-10-12 11:07:06 +02:00
|
|
|
setState(Inactive);
|
|
|
|
|
}
|
|
|
|
|
|
2011-11-17 10:56:27 +01:00
|
|
|
void SshRemoteProcessRunner::handleProcessStarted()
|
2010-10-12 11:07:06 +02:00
|
|
|
{
|
2018-11-23 11:07:57 +01:00
|
|
|
QTC_ASSERT(d->m_state == Connected, return);
|
2010-10-12 11:07:06 +02:00
|
|
|
|
2011-11-17 10:56:27 +01:00
|
|
|
setState(ProcessRunning);
|
2010-10-12 11:07:06 +02:00
|
|
|
emit processStarted();
|
|
|
|
|
}
|
|
|
|
|
|
2018-11-23 11:07:57 +01:00
|
|
|
void SshRemoteProcessRunner::handleProcessFinished(const QString &error)
|
2010-10-12 11:07:06 +02:00
|
|
|
{
|
2018-11-23 11:07:57 +01:00
|
|
|
d->m_exitStatus = d->m_process->exitStatus();
|
|
|
|
|
d->m_exitCode = d->m_process->exitCode();
|
|
|
|
|
d->m_processErrorString = error;
|
2010-10-12 11:07:06 +02:00
|
|
|
setState(Inactive);
|
2018-11-23 11:07:57 +01:00
|
|
|
emit processClosed(d->m_processErrorString);
|
2010-10-12 11:07:06 +02:00
|
|
|
}
|
|
|
|
|
|
2011-11-17 10:56:27 +01:00
|
|
|
void SshRemoteProcessRunner::handleStdout()
|
2011-11-14 17:23:51 +01:00
|
|
|
{
|
2012-06-08 09:42:32 +02:00
|
|
|
d->m_stdout += d->m_process->readAllStandardOutput();
|
|
|
|
|
emit readyReadStandardOutput();
|
2011-11-14 17:23:51 +01:00
|
|
|
}
|
|
|
|
|
|
2011-11-17 10:56:27 +01:00
|
|
|
void SshRemoteProcessRunner::handleStderr()
|
2011-11-14 17:23:51 +01:00
|
|
|
{
|
2012-06-08 09:42:32 +02:00
|
|
|
d->m_stderr += d->m_process->readAllStandardError();
|
|
|
|
|
emit readyReadStandardError();
|
2011-11-14 17:23:51 +01:00
|
|
|
}
|
|
|
|
|
|
2011-11-17 10:56:27 +01:00
|
|
|
void SshRemoteProcessRunner::setState(int newState)
|
2010-10-12 11:07:06 +02:00
|
|
|
{
|
2011-11-17 10:56:27 +01:00
|
|
|
if (d->m_state == newState)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
d->m_state = static_cast<State>(newState);
|
|
|
|
|
if (d->m_state == Inactive) {
|
2011-11-30 14:39:17 +01:00
|
|
|
if (d->m_process) {
|
2018-11-23 11:07:57 +01:00
|
|
|
disconnect(d->m_process.get(), nullptr, this, nullptr);
|
|
|
|
|
d->m_process->terminate();
|
|
|
|
|
d->m_process.release()->deleteLater();
|
2011-11-30 14:39:17 +01:00
|
|
|
}
|
2011-11-17 10:56:27 +01:00
|
|
|
if (d->m_connection) {
|
2019-07-31 17:21:41 +02:00
|
|
|
disconnect(d->m_connection, nullptr, this, nullptr);
|
2021-09-09 15:29:28 +02:00
|
|
|
SshConnectionManager::releaseConnection(d->m_connection);
|
2019-07-31 17:21:41 +02:00
|
|
|
d->m_connection = nullptr;
|
2010-10-12 11:07:06 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-21 16:59:29 +02:00
|
|
|
QString SshRemoteProcessRunner::command() const { return d->m_command; }
|
2011-11-17 10:56:27 +01:00
|
|
|
QString SshRemoteProcessRunner::lastConnectionErrorString() const {
|
|
|
|
|
return d->m_lastConnectionErrorString;
|
2011-11-09 17:38:19 +01:00
|
|
|
}
|
|
|
|
|
|
2011-11-30 14:39:17 +01:00
|
|
|
bool SshRemoteProcessRunner::isProcessRunning() const
|
|
|
|
|
{
|
|
|
|
|
return d->m_process && d->m_process->isRunning();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
SshRemoteProcess::ExitStatus SshRemoteProcessRunner::processExitStatus() const
|
|
|
|
|
{
|
2018-11-23 11:07:57 +01:00
|
|
|
QTC_CHECK(!isProcessRunning());
|
2011-11-30 14:39:17 +01:00
|
|
|
return d->m_exitStatus;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int SshRemoteProcessRunner::processExitCode() const
|
|
|
|
|
{
|
2018-11-23 11:07:57 +01:00
|
|
|
QTC_CHECK(processExitStatus() == SshRemoteProcess::NormalExit);
|
2011-11-30 14:39:17 +01:00
|
|
|
return d->m_exitCode;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QString SshRemoteProcessRunner::processErrorString() const
|
|
|
|
|
{
|
|
|
|
|
return d->m_processErrorString;
|
|
|
|
|
}
|
|
|
|
|
|
2012-06-08 09:42:32 +02:00
|
|
|
QByteArray SshRemoteProcessRunner::readAllStandardOutput()
|
|
|
|
|
{
|
|
|
|
|
const QByteArray data = d->m_stdout;
|
|
|
|
|
d->m_stdout.clear();
|
|
|
|
|
return data;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QByteArray SshRemoteProcessRunner::readAllStandardError()
|
|
|
|
|
{
|
|
|
|
|
const QByteArray data = d->m_stderr;
|
|
|
|
|
d->m_stderr.clear();
|
|
|
|
|
return data;
|
|
|
|
|
}
|
|
|
|
|
|
2011-11-30 14:39:17 +01:00
|
|
|
void SshRemoteProcessRunner::writeDataToProcess(const QByteArray &data)
|
|
|
|
|
{
|
2018-11-23 11:07:57 +01:00
|
|
|
QTC_CHECK(isProcessRunning());
|
2011-11-30 14:39:17 +01:00
|
|
|
d->m_process->write(data);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void SshRemoteProcessRunner::cancel()
|
|
|
|
|
{
|
|
|
|
|
setState(Inactive);
|
|
|
|
|
}
|
|
|
|
|
|
2012-05-18 10:49:35 +02:00
|
|
|
} // namespace QSsh
|