1 /****************************************************************************
3 ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: Nokia Corporation (qt-info@nokia.com)
7 ** This file is part of the Qt Creator.
9 ** $QT_BEGIN_LICENSE:LGPL$
10 ** No Commercial Usage
11 ** This file contains pre-release code and may not be distributed.
12 ** You may use this file in accordance with the terms and conditions
13 ** contained in the Technology Preview License Agreement accompanying
16 ** GNU Lesser General Public License Usage
17 ** Alternatively, this file may be used under the terms of the GNU Lesser
18 ** General Public License version 2.1 as published by the Free Software
19 ** Foundation and appearing in the file LICENSE.LGPL included in the
20 ** packaging of this file. Please review the following information to
21 ** ensure the GNU Lesser General Public License version 2.1 requirements
22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 ** In addition, as a special exception, Nokia gives you certain additional
25 ** rights. These rights are described in the Nokia Qt LGPL Exception
26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 ** If you have questions regarding the use of this file, please contact
29 ** Nokia at qt-info@nokia.com.
33 ****************************************************************************/
35 #include "maemosshrunner.h"
37 #include "maemodeploystep.h"
38 #include "maemoglobal.h"
39 #include "maemoqemumanager.h"
40 #include "maemoremotemounter.h"
41 #include "maemoremotemountsmodel.h"
42 #include "maemorunconfiguration.h"
43 #include "maemousedportsgatherer.h"
45 #include <utils/ssh/sshconnection.h>
46 #include <utils/ssh/sshremoteprocess.h>
48 #include <QtCore/QFileInfo>
52 #define ASSERT_STATE(state) ASSERT_STATE_GENERIC(State, state, m_state)
54 using namespace Utils;
56 namespace Qt4ProjectManager {
59 MaemoSshRunner::MaemoSshRunner(QObject *parent,
60 MaemoRunConfiguration *runConfig, bool debugging)
62 m_mounter(new MaemoRemoteMounter(this)),
63 m_portsGatherer(new MaemoUsedPortsGatherer(this)),
64 m_devConfig(runConfig->deviceConfig()),
65 m_remoteExecutable(runConfig->remoteExecutableFilePath()),
66 m_appArguments(runConfig->arguments()),
67 m_userEnvChanges(runConfig->userEnvironmentChanges()),
68 m_initialFreePorts(runConfig->freePorts()),
69 m_mountSpecs(runConfig->remoteMounts()->mountSpecs()),
72 m_connection = runConfig->deployStep()->sshConnection();
73 m_mounter->setBuildConfiguration(runConfig->activeQt4BuildConfiguration());
74 if (debugging && runConfig->useRemoteGdb()) {
75 m_mountSpecs << MaemoMountSpecification(runConfig->localDirToMountForRemoteGdb(),
76 runConfig->remoteProjectSourcesMountPoint());
79 m_procsToKill << QFileInfo(m_remoteExecutable).fileName();
80 connect(m_mounter, SIGNAL(mounted()), this, SLOT(handleMounted()));
81 connect(m_mounter, SIGNAL(unmounted()), this, SLOT(handleUnmounted()));
82 connect(m_mounter, SIGNAL(error(QString)), this,
83 SLOT(handleMounterError(QString)));
84 connect(m_mounter, SIGNAL(reportProgress(QString)), this,
85 SIGNAL(reportProgress(QString)));
86 connect(m_mounter, SIGNAL(debugOutput(QString)), this,
87 SIGNAL(mountDebugOutput(QString)));
88 connect(m_portsGatherer, SIGNAL(error(QString)), this,
89 SLOT(handlePortsGathererError(QString)));
90 connect(m_portsGatherer, SIGNAL(portListReady()), this,
91 SLOT(handleUsedPortsAvailable()));
94 MaemoSshRunner::~MaemoSshRunner() {}
96 void MaemoSshRunner::start()
98 ASSERT_STATE(QList<State>() << Inactive << StopRequested);
100 if (m_remoteExecutable.isEmpty()) {
101 emitError(tr("Cannot run: No remote executable set."), true);
105 emitError(tr("Cannot run: No device configuration set."), true);
109 if (m_devConfig->type() == MaemoDeviceConfig::Emulator
110 && !MaemoQemuManager::instance().qemuIsRunning()) {
111 MaemoQemuManager::instance().startRuntime();
112 emitError(tr("Cannot run: Qemu was not running. "
113 "It has now been started up for you, but it will take "
114 "a bit of time until it is ready."), true);
118 setState(Connecting);
120 m_freePorts = m_initialFreePorts;
122 disconnect(m_connection.data(), 0, this, 0);
123 const bool reUse = isConnectionUsable();
125 m_connection = SshConnection::create();
126 connect(m_connection.data(), SIGNAL(connected()), this,
127 SLOT(handleConnected()));
128 connect(m_connection.data(), SIGNAL(error(Utils::SshError)), this,
129 SLOT(handleConnectionFailure()));
133 emit reportProgress(tr("Connecting to device..."));
134 m_connection->connectToHost(m_devConfig->sshParameters());
138 void MaemoSshRunner::stop()
140 if (m_state == PostRunCleaning || m_state == StopRequested
141 || m_state == Inactive)
143 if (m_state == Connecting) {
145 emit remoteProcessFinished(InvalidExitCode);
149 setState(StopRequested);
153 void MaemoSshRunner::handleConnected()
155 ASSERT_STATE(QList<State>() << Connecting << StopRequested);
156 if (m_state == StopRequested) {
159 setState(PreRunCleaning);
164 void MaemoSshRunner::handleConnectionFailure()
166 if (m_state == Inactive)
167 qWarning("Unexpected state %d in %s.", m_state, Q_FUNC_INFO);
169 const QString errorMsg = m_state == Connecting
170 ? MaemoGlobal::failedToConnectToServerMessage(m_connection, m_devConfig)
171 : tr("Connection error: %1").arg(m_connection->errorString());
175 void MaemoSshRunner::cleanup()
177 ASSERT_STATE(QList<State>() << PreRunCleaning << PostRunCleaning
180 emit reportProgress(tr("Killing remote process(es)..."));
182 // pkill behaves differently on Fremantle and Harmattan.
183 const char *const killTemplate = "pkill -%2 '^%1$'; pkill -%2 '/%1$';";
186 foreach (const QString &proc, m_procsToKill) {
187 niceKill += QString::fromLocal8Bit(killTemplate).arg(proc).arg("SIGTERM");
188 brutalKill += QString::fromLocal8Bit(killTemplate).arg(proc).arg("SIGKILL");
190 QString remoteCall = niceKill + QLatin1String("sleep 1; ") + brutalKill;
191 remoteCall.remove(remoteCall.count() - 1, 1); // Get rid of trailing semicolon.
193 m_cleaner = m_connection->createRemoteProcess(remoteCall.toUtf8());
194 connect(m_cleaner.data(), SIGNAL(closed(int)), this,
195 SLOT(handleCleanupFinished(int)));
199 void MaemoSshRunner::handleCleanupFinished(int exitStatus)
201 Q_ASSERT(exitStatus == SshRemoteProcess::FailedToStart
202 || exitStatus == SshRemoteProcess::KilledBySignal
203 || exitStatus == SshRemoteProcess::ExitedNormally);
205 ASSERT_STATE(QList<State>() << PreRunCleaning << PostRunCleaning
206 << StopRequested << Inactive);
208 if (m_state == Inactive)
210 if (m_state == StopRequested || m_state == PostRunCleaning) {
215 if (exitStatus != SshRemoteProcess::ExitedNormally) {
216 emitError(tr("Initial cleanup failed: %1")
217 .arg(m_cleaner->errorString()));
219 m_mounter->setConnection(m_connection);
224 void MaemoSshRunner::handleUnmounted()
226 ASSERT_STATE(QList<State>() << PreRunCleaning << PreMountUnmounting
227 << PostRunCleaning << StopRequested);
230 case PreRunCleaning: {
231 for (int i = 0; i < m_mountSpecs.count(); ++i)
232 m_mounter->addMountSpecification(m_mountSpecs.at(i), false);
233 setState(PreMountUnmounting);
237 case PreMountUnmounting:
238 setState(GatheringPorts);
239 m_portsGatherer->start(m_connection, m_freePorts);
241 case PostRunCleaning:
242 case StopRequested: {
243 m_mounter->resetMountSpecifications();
244 const bool stopRequested = m_state == StopRequested;
247 emit remoteProcessFinished(InvalidExitCode);
248 } else if (m_exitStatus == SshRemoteProcess::ExitedNormally) {
249 emit remoteProcessFinished(m_runner->exitCode());
251 emit error(tr("Error running remote process: %1")
252 .arg(m_runner->errorString()));
260 void MaemoSshRunner::handleMounted()
262 ASSERT_STATE(QList<State>() << Mounting << StopRequested);
264 if (m_state == Mounting) {
265 setState(ReadyForExecution);
266 emit readyForExecution();
270 void MaemoSshRunner::handleMounterError(const QString &errorMsg)
272 ASSERT_STATE(QList<State>() << PreRunCleaning << PostRunCleaning
273 << PreMountUnmounting << Mounting << StopRequested << Inactive);
278 void MaemoSshRunner::startExecution(const QByteArray &remoteCall)
280 ASSERT_STATE(ReadyForExecution);
282 m_runner = m_connection->createRemoteProcess(remoteCall);
283 connect(m_runner.data(), SIGNAL(started()), this,
284 SIGNAL(remoteProcessStarted()));
285 connect(m_runner.data(), SIGNAL(closed(int)), this,
286 SLOT(handleRemoteProcessFinished(int)));
287 connect(m_runner.data(), SIGNAL(outputAvailable(QByteArray)), this,
288 SIGNAL(remoteOutput(QByteArray)));
289 connect(m_runner.data(), SIGNAL(errorOutputAvailable(QByteArray)), this,
290 SIGNAL(remoteErrorOutput(QByteArray)));
291 setState(ProcessStarting);
295 void MaemoSshRunner::handleRemoteProcessFinished(int exitStatus)
297 Q_ASSERT(exitStatus == SshRemoteProcess::FailedToStart
298 || exitStatus == SshRemoteProcess::KilledBySignal
299 || exitStatus == SshRemoteProcess::ExitedNormally);
300 ASSERT_STATE(QList<State>() << ProcessStarting << StopRequested << Inactive);
302 m_exitStatus = exitStatus;
303 if (m_state != StopRequested && m_state != Inactive) {
304 setState(PostRunCleaning);
309 bool MaemoSshRunner::isConnectionUsable() const
311 return m_connection && m_connection->state() == SshConnection::Connected
312 && m_connection->connectionParameters() == m_devConfig->sshParameters();
315 void MaemoSshRunner::setState(State newState)
317 if (newState == Inactive) {
318 m_mounter->setConnection(SshConnection::Ptr());
319 m_portsGatherer->stop();
321 disconnect(m_connection.data(), 0, this, 0);
322 m_connection = SshConnection::Ptr();
325 disconnect(m_cleaner.data(), 0, this, 0);
330 void MaemoSshRunner::emitError(const QString &errorMsg, bool force)
332 if (m_state != Inactive) {
334 emit error(errorMsg);
336 emit error(errorMsg);
340 void MaemoSshRunner::mount()
343 if (m_mounter->hasValidMountSpecifications()) {
344 emit reportProgress(tr("Mounting host directories..."));
345 m_mounter->mount(freePorts(), m_portsGatherer);
351 void MaemoSshRunner::unmount()
353 ASSERT_STATE(QList<State>() << PreRunCleaning << PreMountUnmounting
354 << PostRunCleaning << StopRequested);
355 if (m_mounter->hasValidMountSpecifications()) {
359 message = tr("Unmounting left-over host directory mounts...");
361 case PreMountUnmounting:
362 message = tr("Potentially unmounting left-over host directory mounts...");
363 case StopRequested: case PostRunCleaning:
364 message = tr("Unmounting host directories...");
369 emit reportProgress(message);
370 m_mounter->unmount();
376 void MaemoSshRunner::handlePortsGathererError(const QString &errorMsg)
381 void MaemoSshRunner::handleUsedPortsAvailable()
383 ASSERT_STATE(QList<State>() << GatheringPorts << StopRequested);
385 if (m_state == StopRequested) {
392 const qint64 MaemoSshRunner::InvalidExitCode
393 = std::numeric_limits<qint64>::min();
395 } // namespace Internal
396 } // namespace Qt4ProjectManager