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::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 PortList::count() const
|
||||||
{
|
{
|
||||||
int n = 0;
|
int n = 0;
|
||||||
|
@@ -51,6 +51,7 @@ public:
|
|||||||
void addPort(int port);
|
void addPort(int port);
|
||||||
void addRange(int startPort, int endPort);
|
void addRange(int startPort, int endPort);
|
||||||
bool hasMore() const;
|
bool hasMore() const;
|
||||||
|
bool contains(int port) const;
|
||||||
int count() const;
|
int count() const;
|
||||||
int getNext();
|
int getNext();
|
||||||
QString toString() const;
|
QString toString() const;
|
||||||
|
@@ -48,6 +48,7 @@ class RemoteLinuxUsedPortsGathererPrivate
|
|||||||
RemoteLinuxUsedPortsGathererPrivate() : running(false) {}
|
RemoteLinuxUsedPortsGathererPrivate() : running(false) {}
|
||||||
|
|
||||||
SshRemoteProcessRunner::Ptr procRunner;
|
SshRemoteProcessRunner::Ptr procRunner;
|
||||||
|
PortList portsToCheck;
|
||||||
QList<int> usedPorts;
|
QList<int> usedPorts;
|
||||||
QByteArray remoteStdout;
|
QByteArray remoteStdout;
|
||||||
QByteArray remoteStderr;
|
QByteArray remoteStderr;
|
||||||
@@ -73,6 +74,7 @@ void RemoteLinuxUsedPortsGatherer::start(const Utils::SshConnection::Ptr &connec
|
|||||||
{
|
{
|
||||||
if (m_d->running)
|
if (m_d->running)
|
||||||
qWarning("Unexpected call of %s in running state", Q_FUNC_INFO);
|
qWarning("Unexpected call of %s in running state", Q_FUNC_INFO);
|
||||||
|
m_d->portsToCheck = devConf->freePorts();
|
||||||
m_d->usedPorts.clear();
|
m_d->usedPorts.clear();
|
||||||
m_d->remoteStdout.clear();
|
m_d->remoteStdout.clear();
|
||||||
m_d->remoteStderr.clear();
|
m_d->remoteStderr.clear();
|
||||||
@@ -85,8 +87,8 @@ void RemoteLinuxUsedPortsGatherer::start(const Utils::SshConnection::Ptr &connec
|
|||||||
SLOT(handleRemoteStdOut(QByteArray)));
|
SLOT(handleRemoteStdOut(QByteArray)));
|
||||||
connect(m_d->procRunner.data(), SIGNAL(processErrorOutputAvailable(QByteArray)),
|
connect(m_d->procRunner.data(), SIGNAL(processErrorOutputAvailable(QByteArray)),
|
||||||
SLOT(handleRemoteStdErr(QByteArray)));
|
SLOT(handleRemoteStdErr(QByteArray)));
|
||||||
const QString command = QLatin1String("lsof -nPi4tcp:") + devConf->freePorts().toString()
|
const QString command = QLatin1String("sed "
|
||||||
+ QLatin1String(" -F n |grep '^n' |sed -r 's/[^:]*:([[:digit:]]+).*/\\1/g' |sort -n |uniq");
|
"'s/.*: [[:xdigit:]]\\{8\\}:\\([[:xdigit:]]\\{4\\}\\).*/\\1/g' /proc/net/tcp");
|
||||||
m_d->procRunner->run(command.toUtf8());
|
m_d->procRunner->run(command.toUtf8());
|
||||||
m_d->running = true;
|
m_d->running = true;
|
||||||
}
|
}
|
||||||
@@ -118,14 +120,16 @@ QList<int> RemoteLinuxUsedPortsGatherer::usedPorts() const
|
|||||||
|
|
||||||
void RemoteLinuxUsedPortsGatherer::setupUsedPorts()
|
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) {
|
foreach (const QByteArray &portString, portStrings) {
|
||||||
if (portString.isEmpty())
|
if (portString.isEmpty())
|
||||||
continue;
|
continue;
|
||||||
bool ok;
|
bool ok;
|
||||||
const int port = portString.toInt(&ok);
|
const int port = portString.toInt(&ok, 16);
|
||||||
if (ok) {
|
if (ok) {
|
||||||
m_d->usedPorts << port;
|
if (m_d->portsToCheck.contains(port) && !m_d->usedPorts.contains(port))
|
||||||
|
m_d->usedPorts << port;
|
||||||
} else {
|
} else {
|
||||||
qWarning("%s: Unexpected string '%s' is not a port.",
|
qWarning("%s: Unexpected string '%s' is not a port.",
|
||||||
Q_FUNC_INFO, portString.data());
|
Q_FUNC_INFO, portString.data());
|
||||||
|
Reference in New Issue
Block a user