forked from qt-creator/qt-creator
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:
@@ -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;
|
||||
|
@@ -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;
|
||||
|
@@ -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,13 +120,15 @@ 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) {
|
||||
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.",
|
||||
|
Reference in New Issue
Block a user