2012-10-02 09:12:39 +02:00
|
|
|
/****************************************************************************
|
2011-02-18 10:36:52 +01:00
|
|
|
**
|
2014-01-07 13:27:11 +01:00
|
|
|
** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
|
2012-10-02 09:12:39 +02:00
|
|
|
** Contact: http://www.qt-project.org/legal
|
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
|
|
|
|
|
** a written agreement between you and Digia. For licensing terms and
|
2014-10-01 13:21:18 +02:00
|
|
|
** conditions see http://www.qt.io/licensing. For further information
|
|
|
|
|
** use the contact form at http://www.qt.io/contact-us.
|
2011-02-18 10:36:52 +01:00
|
|
|
**
|
|
|
|
|
** GNU Lesser General Public License Usage
|
2012-10-02 09:12:39 +02:00
|
|
|
** Alternatively, this file may be used under the terms of the GNU Lesser
|
2014-10-01 13:21:18 +02:00
|
|
|
** General Public License version 2.1 or version 3 as published by the Free
|
|
|
|
|
** Software Foundation and appearing in the file LICENSE.LGPLv21 and
|
|
|
|
|
** LICENSE.LGPLv3 included in the packaging of this file. Please review the
|
|
|
|
|
** following information to ensure the GNU Lesser General Public License
|
|
|
|
|
** requirements will be met: https://www.gnu.org/licenses/lgpl.html and
|
|
|
|
|
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
2012-10-02 09:12:39 +02:00
|
|
|
**
|
|
|
|
|
** In addition, as a special exception, Digia gives you certain additional
|
|
|
|
|
** rights. These rights are described in the Digia Qt LGPL Exception
|
2011-02-18 10:36:52 +01:00
|
|
|
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
|
|
|
|
|
**
|
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"
|
2011-04-06 10:26:45 +02:00
|
|
|
#include "sshpseudoterminal.h"
|
2011-03-09 12:07:35 +01:00
|
|
|
|
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
|
|
|
|
|
|
|
|
SshRemoteProcess::Ptr m_process;
|
2012-05-29 13:22:33 +02:00
|
|
|
SshConnection *m_connection;
|
2011-04-06 10:26:45 +02:00
|
|
|
bool m_runInTerminal;
|
|
|
|
|
SshPseudoTerminal m_terminal;
|
2010-10-12 11:07:06 +02:00
|
|
|
QByteArray m_command;
|
2012-05-18 10:49:35 +02:00
|
|
|
QSsh::SshError m_lastConnectionError;
|
2011-11-09 17:38:19 +01:00
|
|
|
QString m_lastConnectionErrorString;
|
2011-11-30 14:39:17 +01:00
|
|
|
SshRemoteProcess::ExitStatus m_exitStatus;
|
2012-01-20 14:35:13 +01:00
|
|
|
SshRemoteProcess::Signal m_exitSignal;
|
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
|
|
|
}
|
|
|
|
|
|
2011-11-17 10:56:27 +01:00
|
|
|
void SshRemoteProcessRunner::run(const QByteArray &command,
|
2011-11-09 14:19:50 +01:00
|
|
|
const SshConnectionParameters &sshParams)
|
2011-04-06 10:26:45 +02:00
|
|
|
{
|
2012-05-18 10:49:35 +02:00
|
|
|
QSSH_ASSERT_AND_RETURN(d->m_state == Inactive);
|
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
|
|
|
}
|
|
|
|
|
|
2011-11-17 10:56:27 +01:00
|
|
|
void SshRemoteProcessRunner::runInTerminal(const QByteArray &command,
|
2011-11-09 14:19:50 +01:00
|
|
|
const SshPseudoTerminal &terminal, const SshConnectionParameters &sshParams)
|
2011-04-06 10:26:45 +02:00
|
|
|
{
|
2011-11-17 10:56:27 +01:00
|
|
|
d->m_terminal = terminal;
|
|
|
|
|
d->m_runInTerminal = true;
|
|
|
|
|
runInternal(command, sshParams);
|
2011-04-06 10:26:45 +02:00
|
|
|
}
|
|
|
|
|
|
2011-11-17 10:56:27 +01:00
|
|
|
void SshRemoteProcessRunner::runInternal(const QByteArray &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_lastConnectionError = SshNoError;
|
|
|
|
|
d->m_lastConnectionErrorString.clear();
|
2011-11-30 14:39:17 +01:00
|
|
|
d->m_processErrorString.clear();
|
2012-01-20 14:35:13 +01:00
|
|
|
d->m_exitSignal = SshRemoteProcess::NoSignal;
|
2011-11-30 14:39:17 +01:00
|
|
|
d->m_exitCode = -1;
|
2011-11-17 10:56:27 +01:00
|
|
|
d->m_command = command;
|
2013-09-10 18:52:17 +02:00
|
|
|
d->m_connection = QSsh::acquireConnection(sshParams);
|
2012-05-29 13:22:33 +02:00
|
|
|
connect(d->m_connection, SIGNAL(error(QSsh::SshError)),
|
2012-05-18 10:49:35 +02:00
|
|
|
SLOT(handleConnectionError(QSsh::SshError)));
|
2012-05-29 13:22:33 +02:00
|
|
|
connect(d->m_connection, SIGNAL(disconnected()), SLOT(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 {
|
2012-05-29 13:22:33 +02:00
|
|
|
connect(d->m_connection, SIGNAL(connected()), SLOT(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
|
|
|
{
|
2012-05-18 10:49:35 +02:00
|
|
|
QSSH_ASSERT_AND_RETURN(d->m_state == Connecting);
|
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);
|
|
|
|
|
connect(d->m_process.data(), SIGNAL(started()), SLOT(handleProcessStarted()));
|
|
|
|
|
connect(d->m_process.data(), SIGNAL(closed(int)), SLOT(handleProcessFinished(int)));
|
|
|
|
|
connect(d->m_process.data(), SIGNAL(readyReadStandardOutput()), SLOT(handleStdout()));
|
|
|
|
|
connect(d->m_process.data(), SIGNAL(readyReadStandardError()), SLOT(handleStderr()));
|
|
|
|
|
if (d->m_runInTerminal)
|
|
|
|
|
d->m_process->requestTerminal(d->m_terminal);
|
|
|
|
|
d->m_process->start();
|
2010-10-12 11:07:06 +02:00
|
|
|
}
|
|
|
|
|
|
2012-05-18 10:49:35 +02:00
|
|
|
void SshRemoteProcessRunner::handleConnectionError(QSsh::SshError error)
|
2010-10-12 11:07:06 +02:00
|
|
|
{
|
2011-11-17 10:56:27 +01:00
|
|
|
d->m_lastConnectionError = error;
|
|
|
|
|
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
|
|
|
{
|
2012-05-18 10:49:35 +02:00
|
|
|
QSSH_ASSERT_AND_RETURN(d->m_state == Connecting || d->m_state == Connected
|
|
|
|
|
|| d->m_state == ProcessRunning);
|
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
|
|
|
{
|
2012-05-18 10:49:35 +02:00
|
|
|
QSSH_ASSERT_AND_RETURN(d->m_state == Connected);
|
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();
|
|
|
|
|
}
|
|
|
|
|
|
2011-11-17 10:56:27 +01:00
|
|
|
void SshRemoteProcessRunner::handleProcessFinished(int exitStatus)
|
2010-10-12 11:07:06 +02:00
|
|
|
{
|
2011-11-30 14:39:17 +01:00
|
|
|
d->m_exitStatus = static_cast<SshRemoteProcess::ExitStatus>(exitStatus);
|
|
|
|
|
switch (d->m_exitStatus) {
|
2010-10-12 11:07:06 +02:00
|
|
|
case SshRemoteProcess::FailedToStart:
|
2012-05-18 10:49:35 +02:00
|
|
|
QSSH_ASSERT_AND_RETURN(d->m_state == Connected);
|
2010-10-12 11:07:06 +02:00
|
|
|
break;
|
2012-06-08 08:23:32 +02:00
|
|
|
case SshRemoteProcess::CrashExit:
|
2012-05-18 10:49:35 +02:00
|
|
|
QSSH_ASSERT_AND_RETURN(d->m_state == ProcessRunning);
|
2011-11-30 14:39:17 +01:00
|
|
|
d->m_exitSignal = d->m_process->exitSignal();
|
|
|
|
|
break;
|
2012-06-08 08:23:32 +02:00
|
|
|
case SshRemoteProcess::NormalExit:
|
2012-05-18 10:49:35 +02:00
|
|
|
QSSH_ASSERT_AND_RETURN(d->m_state == ProcessRunning);
|
2011-11-30 14:39:17 +01:00
|
|
|
d->m_exitCode = d->m_process->exitCode();
|
2010-10-12 11:07:06 +02:00
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
Q_ASSERT_X(false, Q_FUNC_INFO, "Impossible exit status.");
|
|
|
|
|
}
|
2011-11-30 14:39:17 +01:00
|
|
|
d->m_processErrorString = d->m_process->errorString();
|
2010-10-12 11:07:06 +02:00
|
|
|
setState(Inactive);
|
|
|
|
|
emit processClosed(exitStatus);
|
|
|
|
|
}
|
|
|
|
|
|
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) {
|
2011-11-17 10:56:27 +01:00
|
|
|
disconnect(d->m_process.data(), 0, this, 0);
|
2011-11-30 14:39:17 +01:00
|
|
|
d->m_process->close();
|
|
|
|
|
d->m_process.clear();
|
|
|
|
|
}
|
2011-11-17 10:56:27 +01:00
|
|
|
if (d->m_connection) {
|
2012-05-29 13:22:33 +02:00
|
|
|
disconnect(d->m_connection, 0, this, 0);
|
2013-09-10 18:52:17 +02:00
|
|
|
QSsh::releaseConnection(d->m_connection);
|
2012-05-29 13:22:33 +02:00
|
|
|
d->m_connection = 0;
|
2010-10-12 11:07:06 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2011-11-17 10:56:27 +01:00
|
|
|
QByteArray SshRemoteProcessRunner::command() const { return d->m_command; }
|
|
|
|
|
SshError SshRemoteProcessRunner::lastConnectionError() const { return d->m_lastConnectionError; }
|
|
|
|
|
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
|
|
|
|
|
{
|
2012-05-18 10:49:35 +02:00
|
|
|
QSSH_ASSERT(!isProcessRunning());
|
2011-11-30 14:39:17 +01:00
|
|
|
return d->m_exitStatus;
|
|
|
|
|
}
|
|
|
|
|
|
2012-01-20 14:35:13 +01:00
|
|
|
SshRemoteProcess::Signal SshRemoteProcessRunner::processExitSignal() const
|
2011-11-30 14:39:17 +01:00
|
|
|
{
|
2012-06-08 08:23:32 +02:00
|
|
|
QSSH_ASSERT(processExitStatus() == SshRemoteProcess::CrashExit);
|
2011-11-30 14:39:17 +01:00
|
|
|
return d->m_exitSignal;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int SshRemoteProcessRunner::processExitCode() const
|
|
|
|
|
{
|
2012-06-08 08:23:32 +02:00
|
|
|
QSSH_ASSERT(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)
|
|
|
|
|
{
|
2012-05-18 10:49:35 +02:00
|
|
|
QSSH_ASSERT(isProcessRunning());
|
2011-11-30 14:39:17 +01:00
|
|
|
d->m_process->write(data);
|
|
|
|
|
}
|
|
|
|
|
|
2012-01-20 14:35:13 +01:00
|
|
|
void SshRemoteProcessRunner::sendSignalToProcess(SshRemoteProcess::Signal signal)
|
2011-11-30 14:39:17 +01:00
|
|
|
{
|
2012-05-18 10:49:35 +02:00
|
|
|
QSSH_ASSERT(isProcessRunning());
|
2011-11-30 14:39:17 +01:00
|
|
|
d->m_process->sendSignal(signal);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void SshRemoteProcessRunner::cancel()
|
|
|
|
|
{
|
|
|
|
|
setState(Inactive);
|
|
|
|
|
}
|
|
|
|
|
|
2012-05-18 10:49:35 +02:00
|
|
|
} // namespace QSsh
|