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 <christian.kandeler@nokia.com>
This commit is contained in:
Christian Kandeler
2011-09-08 10:15:38 +02:00
parent 107544bd7b
commit 4bf42e7885
3 changed files with 19 additions and 5 deletions

View File

@@ -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;

View File

@@ -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;

View File

@@ -48,6 +48,7 @@ class RemoteLinuxUsedPortsGathererPrivate
RemoteLinuxUsedPortsGathererPrivate() : running(false) {}
SshRemoteProcessRunner::Ptr procRunner;
PortList portsToCheck;
QList<int> 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<int> RemoteLinuxUsedPortsGatherer::usedPorts() const
void RemoteLinuxUsedPortsGatherer::setupUsedPorts()
{
const QList<QByteArray> &portStrings = m_d->remoteStdout.split('\n');
QList<QByteArray> 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());