1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
|
/**************************************************************************
**
** This file is part of Qt Creator
**
** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
**
** Contact: Nokia Corporation (info@qt.nokia.com)
**
** GNU Lesser General Public License Usage
**
** 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.
**
** In addition, as a special exception, Nokia gives you certain additional
** rights. These rights are described in the Nokia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** 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.
**
** If you have questions regarding the use of this file, please contact
** Nokia at info@qt.nokia.com.
**
**************************************************************************/
#include "maemosshrunner.h"
#include "maemoglobal.h"
#include "maemoqemumanager.h"
#include "maemoremotemounter.h"
#include "maemoremotemountsmodel.h"
#include "maemorunconfiguration.h"
#include "maemousedportsgatherer.h"
#include <utils/ssh/sshconnection.h>
#include <utils/ssh/sshconnectionmanager.h>
#include <utils/ssh/sshremoteprocess.h>
#include <QtCore/QFileInfo>
#include <limits>
#define ASSERT_STATE(state) ASSERT_STATE_GENERIC(State, state, m_state)
using namespace Utils;
namespace Qt4ProjectManager {
namespace Internal {
MaemoSshRunner::MaemoSshRunner(QObject *parent,
MaemoRunConfiguration *runConfig, bool debugging)
: QObject(parent),
m_mounter(new MaemoRemoteMounter(this)),
m_portsGatherer(new MaemoUsedPortsGatherer(this)),
m_devConfig(runConfig->deviceConfig()),
m_remoteExecutable(runConfig->remoteExecutableFilePath()),
m_appArguments(runConfig->arguments()),
m_userEnvChanges(runConfig->userEnvironmentChanges()),
m_initialFreePorts(runConfig->freePorts()),
m_mountSpecs(runConfig->remoteMounts()->mountSpecs()),
m_state(Inactive)
{
m_mounter->setBuildConfiguration(runConfig->activeQt4BuildConfiguration());
if (debugging && runConfig->useRemoteGdb()) {
m_mountSpecs << MaemoMountSpecification(runConfig->localDirToMountForRemoteGdb(),
runConfig->remoteProjectSourcesMountPoint());
}
m_procsToKill << QFileInfo(m_remoteExecutable).fileName();
connect(m_mounter, SIGNAL(mounted()), this, SLOT(handleMounted()));
connect(m_mounter, SIGNAL(unmounted()), this, SLOT(handleUnmounted()));
connect(m_mounter, SIGNAL(error(QString)), this,
SLOT(handleMounterError(QString)));
connect(m_mounter, SIGNAL(reportProgress(QString)), this,
SIGNAL(reportProgress(QString)));
connect(m_mounter, SIGNAL(debugOutput(QString)), this,
SIGNAL(mountDebugOutput(QString)));
connect(m_portsGatherer, SIGNAL(error(QString)), this,
SLOT(handlePortsGathererError(QString)));
connect(m_portsGatherer, SIGNAL(portListReady()), this,
SLOT(handleUsedPortsAvailable()));
}
MaemoSshRunner::~MaemoSshRunner() {}
void MaemoSshRunner::start()
{
ASSERT_STATE(QList<State>() << Inactive << StopRequested);
if (m_remoteExecutable.isEmpty()) {
emitError(tr("Cannot run: No remote executable set."), true);
return;
}
if (!m_devConfig) {
emitError(tr("Cannot run: No device configuration set."), true);
return;
}
if (m_devConfig->type() == MaemoDeviceConfig::Emulator
&& !MaemoQemuManager::instance().qemuIsRunning()) {
MaemoQemuManager::instance().startRuntime();
emitError(tr("Cannot run: Qemu was not running. "
"It has now been started up for you, but it will take "
"a bit of time until it is ready."), true);
return;
}
m_connection = SshConnectionManager::instance().acquireConnection(m_devConfig->sshParameters());
setState(Connecting);
m_exitStatus = -1;
m_freePorts = m_initialFreePorts;
connect(m_connection.data(), SIGNAL(connected()), this,
SLOT(handleConnected()));
connect(m_connection.data(), SIGNAL(error(Utils::SshError)), this,
SLOT(handleConnectionFailure()));
if (isConnectionUsable()) {
handleConnected();
} else {
emit reportProgress(tr("Connecting to device..."));
m_connection->connectToHost();
}
}
void MaemoSshRunner::stop()
{
if (m_state == PostRunCleaning || m_state == StopRequested
|| m_state == Inactive)
return;
if (m_state == Connecting) {
setState(Inactive);
emit remoteProcessFinished(InvalidExitCode);
return;
}
setState(StopRequested);
cleanup();
}
void MaemoSshRunner::handleConnected()
{
ASSERT_STATE(QList<State>() << Connecting << StopRequested);
if (m_state == StopRequested) {
setState(Inactive);
} else {
setState(PreRunCleaning);
cleanup();
}
}
void MaemoSshRunner::handleConnectionFailure()
{
if (m_state == Inactive)
qWarning("Unexpected state %d in %s.", m_state, Q_FUNC_INFO);
const QString errorMsg = m_state == Connecting
? MaemoGlobal::failedToConnectToServerMessage(m_connection, m_devConfig)
: tr("Connection error: %1").arg(m_connection->errorString());
emitError(errorMsg);
}
void MaemoSshRunner::cleanup()
{
ASSERT_STATE(QList<State>() << PreRunCleaning << PostRunCleaning
<< StopRequested);
emit reportProgress(tr("Killing remote process(es)..."));
// pkill behaves differently on Fremantle and Harmattan.
const char *const killTemplate = "pkill -%2 '^%1$'; pkill -%2 '/%1$';";
QString niceKill;
QString brutalKill;
foreach (const QString &proc, m_procsToKill) {
niceKill += QString::fromLocal8Bit(killTemplate).arg(proc).arg("SIGTERM");
brutalKill += QString::fromLocal8Bit(killTemplate).arg(proc).arg("SIGKILL");
}
QString remoteCall = niceKill + QLatin1String("sleep 1; ") + brutalKill;
remoteCall.remove(remoteCall.count() - 1, 1); // Get rid of trailing semicolon.
m_cleaner = m_connection->createRemoteProcess(remoteCall.toUtf8());
connect(m_cleaner.data(), SIGNAL(closed(int)), this,
SLOT(handleCleanupFinished(int)));
m_cleaner->start();
}
void MaemoSshRunner::handleCleanupFinished(int exitStatus)
{
Q_ASSERT(exitStatus == SshRemoteProcess::FailedToStart
|| exitStatus == SshRemoteProcess::KilledBySignal
|| exitStatus == SshRemoteProcess::ExitedNormally);
ASSERT_STATE(QList<State>() << PreRunCleaning << PostRunCleaning
<< StopRequested << Inactive);
if (m_state == Inactive)
return;
if (m_state == StopRequested || m_state == PostRunCleaning) {
unmount();
return;
}
if (exitStatus != SshRemoteProcess::ExitedNormally) {
emitError(tr("Initial cleanup failed: %1")
.arg(m_cleaner->errorString()));
} else {
m_mounter->setConnection(m_connection, m_devConfig);
unmount();
}
}
void MaemoSshRunner::handleUnmounted()
{
ASSERT_STATE(QList<State>() << PreRunCleaning << PreMountUnmounting
<< PostRunCleaning << StopRequested);
switch (m_state) {
case PreRunCleaning: {
for (int i = 0; i < m_mountSpecs.count(); ++i)
m_mounter->addMountSpecification(m_mountSpecs.at(i), false);
setState(PreMountUnmounting);
unmount();
break;
}
case PreMountUnmounting:
setState(GatheringPorts);
m_portsGatherer->start(m_connection, m_devConfig);
break;
case PostRunCleaning:
case StopRequested: {
m_mounter->resetMountSpecifications();
const bool stopRequested = m_state == StopRequested;
setState(Inactive);
if (stopRequested) {
emit remoteProcessFinished(InvalidExitCode);
} else if (m_exitStatus == SshRemoteProcess::ExitedNormally) {
emit remoteProcessFinished(m_runner->exitCode());
} else {
emit error(tr("Error running remote process: %1")
.arg(m_runner->errorString()));
}
break;
}
default: ;
}
}
void MaemoSshRunner::handleMounted()
{
ASSERT_STATE(QList<State>() << Mounting << StopRequested);
if (m_state == Mounting) {
setState(ReadyForExecution);
emit readyForExecution();
}
}
void MaemoSshRunner::handleMounterError(const QString &errorMsg)
{
ASSERT_STATE(QList<State>() << PreRunCleaning << PostRunCleaning
<< PreMountUnmounting << Mounting << StopRequested << Inactive);
emitError(errorMsg);
}
void MaemoSshRunner::startExecution(const QByteArray &remoteCall)
{
ASSERT_STATE(ReadyForExecution);
m_runner = m_connection->createRemoteProcess(remoteCall);
connect(m_runner.data(), SIGNAL(started()), this,
SIGNAL(remoteProcessStarted()));
connect(m_runner.data(), SIGNAL(closed(int)), this,
SLOT(handleRemoteProcessFinished(int)));
connect(m_runner.data(), SIGNAL(outputAvailable(QByteArray)), this,
SIGNAL(remoteOutput(QByteArray)));
connect(m_runner.data(), SIGNAL(errorOutputAvailable(QByteArray)), this,
SIGNAL(remoteErrorOutput(QByteArray)));
setState(ProcessStarting);
m_runner->start();
}
void MaemoSshRunner::handleRemoteProcessFinished(int exitStatus)
{
Q_ASSERT(exitStatus == SshRemoteProcess::FailedToStart
|| exitStatus == SshRemoteProcess::KilledBySignal
|| exitStatus == SshRemoteProcess::ExitedNormally);
ASSERT_STATE(QList<State>() << ProcessStarting << StopRequested << Inactive);
m_exitStatus = exitStatus;
if (m_state != StopRequested && m_state != Inactive) {
setState(PostRunCleaning);
cleanup();
}
}
bool MaemoSshRunner::isConnectionUsable() const
{
return m_connection && m_connection->state() == SshConnection::Connected
&& m_connection->connectionParameters() == m_devConfig->sshParameters();
}
void MaemoSshRunner::setState(State newState)
{
if (newState == Inactive) {
m_mounter->setConnection(SshConnection::Ptr(), m_devConfig);
m_portsGatherer->stop();
if (m_connection) {
disconnect(m_connection.data(), 0, this, 0);
SshConnectionManager::instance().releaseConnection(m_connection);
m_connection = SshConnection::Ptr();
}
if (m_cleaner)
disconnect(m_cleaner.data(), 0, this, 0);
}
m_state = newState;
}
void MaemoSshRunner::emitError(const QString &errorMsg, bool force)
{
if (m_state != Inactive) {
setState(Inactive);
emit error(errorMsg);
} else if (force) {
emit error(errorMsg);
}
}
void MaemoSshRunner::mount()
{
setState(Mounting);
if (m_mounter->hasValidMountSpecifications()) {
emit reportProgress(tr("Mounting host directories..."));
m_mounter->mount(freePorts(), m_portsGatherer);
} else {
handleMounted();
}
}
void MaemoSshRunner::unmount()
{
ASSERT_STATE(QList<State>() << PreRunCleaning << PreMountUnmounting
<< PostRunCleaning << StopRequested);
if (m_mounter->hasValidMountSpecifications()) {
QString message;
switch (m_state) {
case PreRunCleaning:
message = tr("Unmounting left-over host directory mounts...");
break;
case PreMountUnmounting:
message = tr("Potentially unmounting left-over host directory mounts...");
case StopRequested: case PostRunCleaning:
message = tr("Unmounting host directories...");
break;
default:
break;
}
emit reportProgress(message);
m_mounter->unmount();
} else {
handleUnmounted();
}
}
void MaemoSshRunner::handlePortsGathererError(const QString &errorMsg)
{
emitError(errorMsg);
}
void MaemoSshRunner::handleUsedPortsAvailable()
{
ASSERT_STATE(QList<State>() << GatheringPorts << StopRequested);
if (m_state == StopRequested) {
setState(Inactive);
} else {
mount();
}
}
const qint64 MaemoSshRunner::InvalidExitCode
= std::numeric_limits<qint64>::min();
} // namespace Internal
} // namespace Qt4ProjectManager
|