refactor: Get process IDs by iterating /proc. Improve readability in JsonConnection option handling. (#843)

Co-authored-by: brindosch <edeltraud70@gmx.de>
This commit is contained in:
Murat Seker
2020-06-29 22:55:12 +02:00
committed by GitHub
parent 26813be36a
commit 6cf9dfaa68
7 changed files with 60 additions and 107 deletions

View File

@@ -1,8 +1,11 @@
#pragma once
#include <QString>
#include <QProcess>
#include <QByteArray>
#include <QDir>
#include <QFile>
#include <QRegExp>
#include <QString>
#include <QTextStream>
#ifdef WIN32
// psapi.h requires windows.h to be included
@@ -12,7 +15,6 @@
unsigned int getProcessIdsByProcessName(const char *processName, QStringList &listOfPids)
{
// Clear content of returned list of PIDS
listOfPids.clear();
@@ -57,33 +59,36 @@ unsigned int getProcessIdsByProcessName(const char *processName, QStringList &li
}
}
return listOfPids.count();
#else
// Run pgrep, which looks through the currently running processses and lists the process IDs
// which match the selection criteria to stdout.
QProcess process;
process.start("pgrep", QStringList() << processName);
process.waitForReadyRead();
QDir dir("/proc");
dir.setFilter(QDir::Dirs);
dir.setSorting(QDir::Name | QDir::Reversed);
QByteArray bytes = process.readAllStandardOutput();
for (const QString & pid : dir.entryList()) {
QRegExp regexp("\\d*");
if (!regexp.exactMatch(pid))
{
/* Not a number, can not be PID */
continue;
}
process.terminate();
process.waitForFinished();
process.kill();
QFile cmdline("/proc/" + pid + "/cmdline");
if (!cmdline.open(QFile::ReadOnly | QFile::Text))
{
/* Can not open cmdline file */
continue;
}
// Output is something like "2472\n2323" for multiple instances
if (bytes.isEmpty())
return 0;
#if (QT_VERSION >= QT_VERSION_CHECK(5, 14, 0))
listOfPids = QString(bytes).split("\n", Qt::SkipEmptyParts);
#else
listOfPids = QString(bytes).split("\n", QString::SkipEmptyParts);
#endif
return listOfPids.count();
QTextStream in(&cmdline);
QString command = in.readAll();
if (command.startsWith(processName))
{
listOfPids.push_back(pid);
}
}
#endif
return listOfPids.count();
}

View File

@@ -153,7 +153,6 @@ int main(int argc, char** argv)
Logger::setLogLevel(Logger::WARNING);
// check if we are running already an instance
// TODO Do not use pgrep on linux, instead iter /proc
// TODO Allow one session per user
// http://www.qtcentre.org/threads/44489-Get-Process-ID-for-a-running-application
QStringList listOfPids;
@@ -196,8 +195,8 @@ int main(int argc, char** argv)
BooleanOption & silentOption = parser.add<BooleanOption> ('s', "silent", "do not print any outputs");
BooleanOption & verboseOption = parser.add<BooleanOption> ('v', "verbose", "Increase verbosity");
BooleanOption & debugOption = parser.add<BooleanOption> ('d', "debug", "Show debug messages");
parser.add<BooleanOption> (0x0, "desktop", "show systray on desktop");
parser.add<BooleanOption> (0x0, "service", "force hyperion to start as console service");
parser.add<BooleanOption> (0x0, "desktop", "show systray on desktop");
parser.add<BooleanOption> (0x0, "service", "force hyperion to start as console service");
Option & exportEfxOption = parser.add<Option> (0x0, "export-effects", "export effects to given path");
parser.process(*qApp);

View File

@@ -88,7 +88,7 @@ void SysTray::createTrayIcon()
connect(efxAction, SIGNAL(triggered()), this, SLOT(setEffect()));
_trayIconEfxMenu->addAction(efxAction);
}
_trayIconMenu->addAction(settingsAction);
_trayIconMenu->addSeparator();
_trayIconMenu->addAction(colorAction);
@@ -104,7 +104,7 @@ void SysTray::createTrayIcon()
void SysTray::setColor(const QColor & color)
{
std::vector<ColorRgb> rgbColor{ ColorRgb{ (uint8_t)color.red(), (uint8_t)color.green(), (uint8_t)color.blue() } };
_hyperion->setColor(1 ,rgbColor, 0);
}
@@ -149,7 +149,7 @@ void SysTray::settings()
#endif
QDesktopServices::openUrl(QUrl("http://localhost:"+QString::number(_webPort)+"/", QUrl::TolerantMode));
#ifndef _WIN32
// restoring stdout
::dup2(saved_stdout, STDOUT_FILENO);