2011-02-18 10:36:52 +01:00
|
|
|
/**************************************************************************
|
|
|
|
|
**
|
|
|
|
|
** This file is part of Qt Creator
|
|
|
|
|
**
|
|
|
|
|
** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
|
|
|
|
|
**
|
2011-11-02 15:59:12 +01:00
|
|
|
** Contact: Nokia Corporation (qt-info@nokia.com)
|
2011-02-18 10:36:52 +01:00
|
|
|
**
|
|
|
|
|
**
|
|
|
|
|
** GNU Lesser General Public License Usage
|
|
|
|
|
**
|
2011-04-13 08:42:33 +02:00
|
|
|
** 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.
|
2011-02-18 10:36:52 +01:00
|
|
|
**
|
|
|
|
|
** In addition, as a special exception, Nokia gives you certain additional
|
2011-04-13 08:42:33 +02:00
|
|
|
** rights. These rights are described in the Nokia Qt LGPL Exception
|
2011-02-18 10:36:52 +01:00
|
|
|
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
|
|
|
|
|
**
|
2011-04-13 08:42:33 +02:00
|
|
|
** Other Usage
|
|
|
|
|
**
|
|
|
|
|
** Alternatively, this file may be used in accordance with the terms and
|
|
|
|
|
** conditions contained in a signed written agreement between you and Nokia.
|
|
|
|
|
**
|
2011-02-18 10:36:52 +01:00
|
|
|
** If you have questions regarding the use of this file, please contact
|
2011-11-02 15:59:12 +01:00
|
|
|
** Nokia at qt-info@nokia.com.
|
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
|
|
|
|
2011-11-16 16:54:50 +01:00
|
|
|
#include <utils/qtcassert.h>
|
|
|
|
|
|
2010-10-12 11:07:06 +02:00
|
|
|
|
2011-03-02 17:13:33 +01:00
|
|
|
/*!
|
|
|
|
|
\class Utils::SshRemoteProcessRunner
|
|
|
|
|
|
|
|
|
|
\brief Convenience class for running a remote process over an SSH connection.
|
|
|
|
|
*/
|
|
|
|
|
|
2011-02-14 16:34:17 +01:00
|
|
|
namespace Utils {
|
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;
|
2011-11-09 17:38:19 +01:00
|
|
|
SshConnection::Ptr 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;
|
2011-11-09 17:38:19 +01:00
|
|
|
Utils::SshError m_lastConnectionError;
|
|
|
|
|
QString m_lastConnectionErrorString;
|
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
|
|
|
{
|
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
|
|
|
{
|
2011-11-09 14:19:50 +01:00
|
|
|
setState(Inactive);
|
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();
|
|
|
|
|
d->m_command = command;
|
|
|
|
|
d->m_connection = SshConnectionManager::instance().acquireConnection(sshParams);
|
|
|
|
|
connect(d->m_connection.data(), SIGNAL(error(Utils::SshError)),
|
2011-02-14 16:34:17 +01:00
|
|
|
SLOT(handleConnectionError(Utils::SshError)));
|
2011-11-17 10:56:27 +01:00
|
|
|
connect(d->m_connection.data(), SIGNAL(disconnected()), SLOT(handleDisconnected()));
|
|
|
|
|
if (d->m_connection->state() == SshConnection::Connected) {
|
2010-10-12 11:07:06 +02:00
|
|
|
handleConnected();
|
|
|
|
|
} else {
|
2011-11-17 10:56:27 +01:00
|
|
|
connect(d->m_connection.data(), SIGNAL(connected()), SLOT(handleConnected()));
|
|
|
|
|
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
|
|
|
{
|
2011-11-17 10:56:27 +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);
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
2011-11-17 10:56:27 +01:00
|
|
|
void SshRemoteProcessRunner::handleConnectionError(Utils::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
|
|
|
{
|
2011-11-17 10:56:27 +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
|
|
|
{
|
2011-11-17 10:56:27 +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();
|
|
|
|
|
}
|
|
|
|
|
|
2011-11-17 10:56:27 +01:00
|
|
|
void SshRemoteProcessRunner::handleProcessFinished(int exitStatus)
|
2010-10-12 11:07:06 +02:00
|
|
|
{
|
|
|
|
|
switch (exitStatus) {
|
|
|
|
|
case SshRemoteProcess::FailedToStart:
|
2011-11-17 10:56:27 +01:00
|
|
|
QTC_ASSERT(d->m_state == Connected, return);
|
2010-10-12 11:07:06 +02:00
|
|
|
break;
|
|
|
|
|
case SshRemoteProcess::KilledBySignal:
|
|
|
|
|
case SshRemoteProcess::ExitedNormally:
|
2011-11-17 10:56:27 +01:00
|
|
|
QTC_ASSERT(d->m_state == ProcessRunning, return);
|
2010-10-12 11:07:06 +02:00
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
Q_ASSERT_X(false, Q_FUNC_INFO, "Impossible exit status.");
|
|
|
|
|
}
|
|
|
|
|
setState(Inactive);
|
|
|
|
|
emit processClosed(exitStatus);
|
|
|
|
|
}
|
|
|
|
|
|
2011-11-17 10:56:27 +01:00
|
|
|
void SshRemoteProcessRunner::handleStdout()
|
2011-11-14 17:23:51 +01:00
|
|
|
{
|
2011-11-17 10:56:27 +01:00
|
|
|
emit processOutputAvailable(d->m_process->readAllStandardOutput());
|
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
|
|
|
{
|
2011-11-17 10:56:27 +01:00
|
|
|
emit processErrorOutputAvailable(d->m_process->readAllStandardError());
|
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) {
|
|
|
|
|
if (d->m_process)
|
|
|
|
|
disconnect(d->m_process.data(), 0, this, 0);
|
|
|
|
|
if (d->m_connection) {
|
|
|
|
|
disconnect(d->m_connection.data(), 0, this, 0);
|
|
|
|
|
SshConnectionManager::instance().releaseConnection(d->m_connection);
|
|
|
|
|
d->m_connection.clear();
|
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; }
|
2010-10-12 11:07:06 +02:00
|
|
|
SshRemoteProcess::Ptr SshRemoteProcessRunner::process() const { return d->m_process; }
|
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-02-14 16:34:17 +01:00
|
|
|
} // namespace Utils
|