From 4bf42e7885acbf52927eb030541ac7b2c8468d75 Mon Sep 17 00:00:00 2001 From: Christian Kandeler Date: Thu, 8 Sep 2011 10:15:38 +0200 Subject: [PATCH] RemoteLinux: Grab used ports information from /proc. The target host might not have lsof. Change-Id: Ic0bbaf933e145b94b4665a0bfad12721984c3cc9 Reviewed-on: http://codereview.qt-project.org/4409 Reviewed-by: Christian Kandeler --- src/plugins/remotelinux/portlist.cpp | 9 +++++++++ src/plugins/remotelinux/portlist.h | 1 + .../remotelinux/remotelinuxusedportsgatherer.cpp | 14 +++++++++----- 3 files changed, 19 insertions(+), 5 deletions(-) diff --git a/src/plugins/remotelinux/portlist.cpp b/src/plugins/remotelinux/portlist.cpp index c6cf651efd3..c066ab2f44c 100644 --- a/src/plugins/remotelinux/portlist.cpp +++ b/src/plugins/remotelinux/portlist.cpp @@ -163,6 +163,15 @@ void PortList::addRange(int startPort, int endPort) bool PortList::hasMore() const { return !m_d->ranges.isEmpty(); } +bool PortList::contains(int port) const +{ + foreach (const Internal::Range &r, m_d->ranges) { + if (port >= r.first && port <= r.second) + return true; + } + return false; +} + int PortList::count() const { int n = 0; diff --git a/src/plugins/remotelinux/portlist.h b/src/plugins/remotelinux/portlist.h index f2344a3587c..eb784914abf 100644 --- a/src/plugins/remotelinux/portlist.h +++ b/src/plugins/remotelinux/portlist.h @@ -51,6 +51,7 @@ public: void addPort(int port); void addRange(int startPort, int endPort); bool hasMore() const; + bool contains(int port) const; int count() const; int getNext(); QString toString() const; diff --git a/src/plugins/remotelinux/remotelinuxusedportsgatherer.cpp b/src/plugins/remotelinux/remotelinuxusedportsgatherer.cpp index bd790d06b3f..35aa0f42572 100644 --- a/src/plugins/remotelinux/remotelinuxusedportsgatherer.cpp +++ b/src/plugins/remotelinux/remotelinuxusedportsgatherer.cpp @@ -48,6 +48,7 @@ class RemoteLinuxUsedPortsGathererPrivate RemoteLinuxUsedPortsGathererPrivate() : running(false) {} SshRemoteProcessRunner::Ptr procRunner; + PortList portsToCheck; QList usedPorts; QByteArray remoteStdout; QByteArray remoteStderr; @@ -73,6 +74,7 @@ void RemoteLinuxUsedPortsGatherer::start(const Utils::SshConnection::Ptr &connec { if (m_d->running) qWarning("Unexpected call of %s in running state", Q_FUNC_INFO); + m_d->portsToCheck = devConf->freePorts(); m_d->usedPorts.clear(); m_d->remoteStdout.clear(); m_d->remoteStderr.clear(); @@ -85,8 +87,8 @@ void RemoteLinuxUsedPortsGatherer::start(const Utils::SshConnection::Ptr &connec SLOT(handleRemoteStdOut(QByteArray))); connect(m_d->procRunner.data(), SIGNAL(processErrorOutputAvailable(QByteArray)), SLOT(handleRemoteStdErr(QByteArray))); - const QString command = QLatin1String("lsof -nPi4tcp:") + devConf->freePorts().toString() - + QLatin1String(" -F n |grep '^n' |sed -r 's/[^:]*:([[:digit:]]+).*/\\1/g' |sort -n |uniq"); + const QString command = QLatin1String("sed " + "'s/.*: [[:xdigit:]]\\{8\\}:\\([[:xdigit:]]\\{4\\}\\).*/\\1/g' /proc/net/tcp"); m_d->procRunner->run(command.toUtf8()); m_d->running = true; } @@ -118,14 +120,16 @@ QList RemoteLinuxUsedPortsGatherer::usedPorts() const void RemoteLinuxUsedPortsGatherer::setupUsedPorts() { - const QList &portStrings = m_d->remoteStdout.split('\n'); + QList portStrings = m_d->remoteStdout.split('\n'); + portStrings.removeFirst(); foreach (const QByteArray &portString, portStrings) { if (portString.isEmpty()) continue; bool ok; - const int port = portString.toInt(&ok); + const int port = portString.toInt(&ok, 16); if (ok) { - m_d->usedPorts << port; + if (m_d->portsToCheck.contains(port) && !m_d->usedPorts.contains(port)) + m_d->usedPorts << port; } else { qWarning("%s: Unexpected string '%s' is not a port.", Q_FUNC_INFO, portString.data());