From b03e8e940a6c57be0a2473c833e9128991a3d9b6 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Tobias=20N=C3=A4tterlund?= <tobias.naetterlund.qnx@kdab.com>
Date: Thu, 7 Mar 2013 10:36:28 +0100
Subject: [PATCH] QNX: Only kill tail/slog2info process associated with the
 application

Change-Id: I477d44cc83beb017b12deb9dfb81b3be386ea4e0
Reviewed-by: Mehdi Fekari <mfekari@rim.com>
Reviewed-by: Nicolas Arnaud-Cormos <nicolas@kdab.com>
---
 .../qnx/blackberryapplicationrunner.cpp       | 42 ++++++++--------
 src/plugins/qnx/blackberryapplicationrunner.h |  6 ++-
 .../qnx/blackberrydeviceconfiguration.cpp     |  6 +++
 .../qnx/blackberrydeviceconfiguration.h       |  2 +
 .../qnx/blackberrydeviceprocesssupport.cpp    | 45 +++++++++++++++++
 .../qnx/blackberrydeviceprocesssupport.h      | 49 +++++++++++++++++++
 src/plugins/qnx/qnx.pro                       |  6 ++-
 src/plugins/qnx/qnx.qbs                       |  2 +
 8 files changed, 132 insertions(+), 26 deletions(-)
 create mode 100644 src/plugins/qnx/blackberrydeviceprocesssupport.cpp
 create mode 100644 src/plugins/qnx/blackberrydeviceprocesssupport.h

diff --git a/src/plugins/qnx/blackberryapplicationrunner.cpp b/src/plugins/qnx/blackberryapplicationrunner.cpp
index 34d661d91b0..e49fc6a9d5a 100644
--- a/src/plugins/qnx/blackberryapplicationrunner.cpp
+++ b/src/plugins/qnx/blackberryapplicationrunner.cpp
@@ -89,6 +89,7 @@ BlackBerryApplicationRunner::BlackBerryApplicationRunner(bool debugMode, BlackBe
     , m_appId(QString())
     , m_running(false)
     , m_stopping(false)
+    , m_tailCommand(QString())
     , m_launchProcess(0)
     , m_stopProcess(0)
     , m_tailProcess(0)
@@ -104,13 +105,11 @@ BlackBerryApplicationRunner::BlackBerryApplicationRunner(bool debugMode, BlackBe
     m_environment = buildConfig->environment();
     m_deployCmd = m_environment.searchInPath(QLatin1String(DEPLOY_CMD));
 
-    BlackBerryDeviceConfiguration::ConstPtr device = BlackBerryDeviceConfiguration::device(target->kit());
-    m_deviceHost = device->sshParameters().host;
-    m_password = device->sshParameters().password;
+    m_device = BlackBerryDeviceConfiguration::device(target->kit());
     m_barPackage = runConfiguration->barPackage();
 
-    m_sshParams = device->sshParameters();
     // The BlackBerry device always uses key authentication
+    m_sshParams = m_device->sshParameters();
     m_sshParams.authenticationType = QSsh::SshConnectionParameters::AuthenticationByKey;
 
     m_runningStateTimer->setInterval(3000);
@@ -125,9 +124,9 @@ void BlackBerryApplicationRunner::start()
     args << QLatin1String("-launchApp");
     if (m_debugMode)
         args << QLatin1String("-debugNative");
-    args << QLatin1String("-device") << m_deviceHost;
-    if (!m_password.isEmpty())
-        args << QLatin1String("-password") << m_password;
+    args << QLatin1String("-device") << m_sshParams.host;
+    if (!m_sshParams.password.isEmpty())
+        args << QLatin1String("-password") << m_sshParams.password;
     args << QDir::toNativeSeparators(m_barPackage);
 
     if (!m_launchProcess) {
@@ -188,9 +187,9 @@ ProjectExplorer::RunControl::StopResult BlackBerryApplicationRunner::stop()
 
     QStringList args;
     args << QLatin1String("-terminateApp");
-    args << QLatin1String("-device") << m_deviceHost;
-    if (!m_password.isEmpty())
-        args << QLatin1String("-password") << m_password;
+    args << QLatin1String("-device") << m_sshParams.host;
+    if (!m_sshParams.password.isEmpty())
+        args << QLatin1String("-password") << m_sshParams.password;
     args << m_barPackage;
 
     if (!m_stopProcess) {
@@ -252,13 +251,13 @@ void BlackBerryApplicationRunner::readStandardError()
 
 void BlackBerryApplicationRunner::killTailProcess()
 {
+    QTC_ASSERT(!m_tailCommand.isEmpty(), return);
+
+    QString killCommand = m_device->processSupport()->killProcessByNameCommandLine(m_tailCommand);
+
     QSsh::SshRemoteProcessRunner *slayProcess = new QSsh::SshRemoteProcessRunner(this);
     connect(slayProcess, SIGNAL(processClosed(int)), this, SIGNAL(finished()));
-
-    if (m_slog2infoFound)
-        slayProcess->run("slay slog2info", m_sshParams);
-    else
-        slayProcess->run("slay tail", m_sshParams);
+    slayProcess->run(killCommand.toLatin1(), m_sshParams);
 
     // Not supported by OpenSSH server
     //m_tailProcess->sendSignalToProcess(Utils::SshRemoteProcess::KillSignal);
@@ -292,14 +291,13 @@ void BlackBerryApplicationRunner::tailApplicationLog()
                 this, SLOT(handleTailConnectionError()));
     }
 
-    QString command;
     if (m_slog2infoFound) {
-        command = QString::fromLatin1("slog2info -w -b ") + m_appId;
+        m_tailCommand = QString::fromLatin1("slog2info -w -b ") + m_appId;
     } else {
-        command = QLatin1String("tail -c +1 -f /accounts/1000/appdata/") + m_appId
+        m_tailCommand = QLatin1String("tail -c +1 -f /accounts/1000/appdata/") + m_appId
                 + QLatin1String("/logs/log");
     }
-    m_tailProcess->run(command.toLatin1(), m_sshParams);
+    m_tailProcess->run(m_tailCommand.toLatin1(), m_sshParams);
 }
 
 void BlackBerryApplicationRunner::handleSlog2InfoFound()
@@ -386,9 +384,9 @@ void BlackBerryApplicationRunner::determineRunningState()
 {
     QStringList args;
     args << QLatin1String("-isAppRunning");
-    args << QLatin1String("-device") << m_deviceHost;
-    if (!m_password.isEmpty())
-        args << QLatin1String("-password") << m_password;
+    args << QLatin1String("-device") << m_sshParams.host;
+    if (!m_sshParams.password.isEmpty())
+        args << QLatin1String("-password") << m_sshParams.password;
     args << m_barPackage;
 
     if (!m_runningStateProcess) {
diff --git a/src/plugins/qnx/blackberryapplicationrunner.h b/src/plugins/qnx/blackberryapplicationrunner.h
index f9bf0161d89..c6c3929b74e 100644
--- a/src/plugins/qnx/blackberryapplicationrunner.h
+++ b/src/plugins/qnx/blackberryapplicationrunner.h
@@ -32,6 +32,8 @@
 #ifndef QNX_INTERNAL_BLACKBERRYAPPLICATIONRUNNER_H
 #define QNX_INTERNAL_BLACKBERRYAPPLICATIONRUNNER_H
 
+#include "blackberrydeviceconfiguration.h"
+
 #include <projectexplorer/runconfiguration.h>
 
 #include <ssh/sshconnection.h>
@@ -109,10 +111,10 @@ private:
 
     Utils::Environment m_environment;
     QString m_deployCmd;
-    QString m_deviceHost;
-    QString m_password;
+    BlackBerryDeviceConfiguration::ConstPtr m_device;
     QString m_barPackage;
     QSsh::SshConnectionParameters m_sshParams;
+    QString m_tailCommand;
 
     QProcess *m_launchProcess;
     QProcess *m_stopProcess;
diff --git a/src/plugins/qnx/blackberrydeviceconfiguration.cpp b/src/plugins/qnx/blackberrydeviceconfiguration.cpp
index 957e3a2e54c..3013f321e46 100644
--- a/src/plugins/qnx/blackberrydeviceconfiguration.cpp
+++ b/src/plugins/qnx/blackberrydeviceconfiguration.cpp
@@ -33,6 +33,7 @@
 
 #include "qnxconstants.h"
 #include "blackberrydeviceconfigurationwidget.h"
+#include "blackberrydeviceprocesssupport.h"
 
 #include <projectexplorer/kitinformation.h>
 
@@ -97,6 +98,11 @@ BlackBerryDeviceConfiguration::ConstPtr BlackBerryDeviceConfiguration::device(co
     return dev.dynamicCast<const BlackBerryDeviceConfiguration>();
 }
 
+DeviceProcessSupport::Ptr BlackBerryDeviceConfiguration::processSupport() const
+{
+    return ProjectExplorer::DeviceProcessSupport::Ptr(new BlackBerryDeviceProcessSupport);
+}
+
 QString BlackBerryDeviceConfiguration::displayType() const
 {
     return tr("BlackBerry");
diff --git a/src/plugins/qnx/blackberrydeviceconfiguration.h b/src/plugins/qnx/blackberrydeviceconfiguration.h
index 16cf3fe520e..154dc0af2ef 100644
--- a/src/plugins/qnx/blackberrydeviceconfiguration.h
+++ b/src/plugins/qnx/blackberrydeviceconfiguration.h
@@ -67,6 +67,8 @@ public:
 
     static ConstPtr device(const ProjectExplorer::Kit *k);
 
+    ProjectExplorer::DeviceProcessSupport::Ptr processSupport() const;
+
 protected:
     BlackBerryDeviceConfiguration();
     BlackBerryDeviceConfiguration(const QString &name, Core::Id type, MachineType machineType,
diff --git a/src/plugins/qnx/blackberrydeviceprocesssupport.cpp b/src/plugins/qnx/blackberrydeviceprocesssupport.cpp
new file mode 100644
index 00000000000..bedf0cc618a
--- /dev/null
+++ b/src/plugins/qnx/blackberrydeviceprocesssupport.cpp
@@ -0,0 +1,45 @@
+/**************************************************************************
+**
+** Copyright (C) 2011 - 2013 Research In Motion
+**
+** Contact: Research In Motion (blackberry-qt@qnx.com)
+** Contact: KDAB (info@kdab.com)
+**
+** This file is part of Qt Creator.
+**
+** 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
+** conditions see http://qt.digia.com/licensing.  For further information
+** use the contact form at http://qt.digia.com/contact-us.
+**
+** GNU Lesser General Public License Usage
+** Alternatively, 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, Digia gives you certain additional
+** rights.  These rights are described in the Digia Qt LGPL Exception
+** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
+**
+****************************************************************************/
+
+#include "blackberrydeviceprocesssupport.h"
+
+using namespace Qnx;
+using namespace Qnx::Internal;
+
+QString BlackBerryDeviceProcessSupport::killProcessByNameCommandLine(const QString &filePath) const
+{
+    QString executable = filePath;
+
+    return QString::fromLatin1("for PID in $(pidin -F \"%a %A\" | grep \"%1\" | awk '/%1/ {print $1}'); "
+        "do "
+            "kill $PID; sleep 1; kill -9 $PID; "
+        "done").arg(executable.replace(QLatin1String("/"), QLatin1String("\\/")));
+}
diff --git a/src/plugins/qnx/blackberrydeviceprocesssupport.h b/src/plugins/qnx/blackberrydeviceprocesssupport.h
new file mode 100644
index 00000000000..e79f647305e
--- /dev/null
+++ b/src/plugins/qnx/blackberrydeviceprocesssupport.h
@@ -0,0 +1,49 @@
+/**************************************************************************
+**
+** Copyright (C) 2011 - 2013 Research In Motion
+**
+** Contact: Research In Motion (blackberry-qt@qnx.com)
+** Contact: KDAB (info@kdab.com)
+**
+** This file is part of Qt Creator.
+**
+** 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
+** conditions see http://qt.digia.com/licensing.  For further information
+** use the contact form at http://qt.digia.com/contact-us.
+**
+** GNU Lesser General Public License Usage
+** Alternatively, 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, Digia gives you certain additional
+** rights.  These rights are described in the Digia Qt LGPL Exception
+** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
+**
+****************************************************************************/
+
+#ifndef QNX_INTERNAL_BLACKBERRYDEVICEPROCESSSUPPORT_H
+#define QNX_INTERNAL_BLACKBERRYDEVICEPROCESSSUPPORT_H
+
+#include <remotelinux/linuxdevice.h>
+
+namespace Qnx {
+namespace Internal {
+
+class BlackBerryDeviceProcessSupport : public RemoteLinux::LinuxDeviceProcessSupport
+{
+public:
+    QString killProcessByNameCommandLine(const QString &filePath) const;
+};
+
+} // namespace Internal
+} // namespace Qnx
+
+#endif // QNX_INTERNAL_BLACKBERRYDEVICEPROCESSSUPPORT_H
diff --git a/src/plugins/qnx/qnx.pro b/src/plugins/qnx/qnx.pro
index 882954c40e1..95099958810 100644
--- a/src/plugins/qnx/qnx.pro
+++ b/src/plugins/qnx/qnx.pro
@@ -73,7 +73,8 @@ SOURCES += qnxplugin.cpp \
     blackberrydebugtokenrequestdialog.cpp \
     blackberrydebugtokenuploader.cpp \
     blackberrydebugtokenreader.cpp \
-    blackberryndkprocess.cpp
+    blackberryndkprocess.cpp \
+    blackberrydeviceprocesssupport.cpp
 
 HEADERS += qnxplugin.h\
     qnxconstants.h \
@@ -143,7 +144,8 @@ HEADERS += qnxplugin.h\
     blackberrydebugtokenrequestdialog.h \
     blackberrydebugtokenuploader.h \
     blackberrydebugtokenreader.h \
-    blackberryndkprocess.h
+    blackberryndkprocess.h \
+    blackberrydeviceprocesssupport.h
 
 FORMS += \
     blackberrydeviceconfigurationwizardsetuppage.ui \
diff --git a/src/plugins/qnx/qnx.qbs b/src/plugins/qnx/qnx.qbs
index 28339146efa..41f699c2e5d 100644
--- a/src/plugins/qnx/qnx.qbs
+++ b/src/plugins/qnx/qnx.qbs
@@ -77,6 +77,8 @@ QtcPlugin {
         "blackberrydeviceconfigurationwizardpages.h",
         "blackberrydeviceconfigurationwizardsetuppage.ui",
         "blackberrydeviceconfigurationwizardsshkeypage.ui",
+        "blackberrydeviceprocesssupport.h",
+        "blackberrydeviceprocesssupport.cpp",
         "blackberryqtversion.cpp",
         "blackberryqtversion.h",
         "blackberryqtversionfactory.cpp",
-- 
GitLab