feat: support cgroup v1 and v2

Signed-off-by: ComixHe <heyuming@deepin.org>
This commit is contained in:
ComixHe 2023-09-11 14:56:28 +08:00 committed by Comix
parent 98ef11dc27
commit 49c13f943e
2 changed files with 31 additions and 8 deletions

View File

@ -16,21 +16,43 @@ IdentifyRet CGroupsIdentifier::Identify(pid_t pid)
qWarning() << "open " << AppCgroupPath << "failed: " << AppCgroupFile.errorString();
return {};
}
auto UnitStr = parseCGroupsPath(QString::fromLocal8Bit(AppCgroupFile.readAll())
.split(':', Qt::SkipEmptyParts)
.last()
.trimmed()); // FIXME: support CGroup version detection and multi-line parsing
auto UnitStr = parseCGroupsPath(AppCgroupFile);
auto [appId, InstanceId] = processUnitName(UnitStr);
return {std::move(appId), std::move(InstanceId)};
}
QString CGroupsIdentifier::parseCGroupsPath(const QString &CGP) noexcept
QString CGroupsIdentifier::parseCGroupsPath(QFile &cgroupFile) noexcept
{
if (CGP.isEmpty()) {
qWarning() << "CGroupPath is empty.";
QTextStream stream{&cgroupFile};
if (stream.atEnd()) {
qWarning() << "read from cgroup file failed:" << stream.status();
return {};
}
stream.setEncoding(QStringConverter::Utf8);
QString CGP;
while (!stream.atEnd()) {
auto line = stream.readLine();
auto firstColon = line.indexOf(':');
auto secondColon = line.indexOf(':', firstColon + 1);
auto subSystemd = QStringView(line.constBegin() + firstColon + 1, secondColon - firstColon - 1);
if (subSystemd.isEmpty()) { // cgroup v2
CGP = line.last(line.size() - secondColon - 1);
break;
}
if (subSystemd == QString{"name=systemd"}) { // cgroup v1
CGP = line.last(line.size() - secondColon - 1); // shouldn't break, maybe v1 and v2 exists at the same time.
}
}
if (CGP.isEmpty()) {
qWarning() << "no systemd informations found.";
return {};
}
auto CGPSlices = CGP.split('/', Qt::SkipEmptyParts);
if (CGPSlices.first() != "user.slice") {

View File

@ -6,6 +6,7 @@
#define CGROUPSIDENTIFIER_H
#include "identifier.h"
#include <QFile>
class CGroupsIdentifier : public Identifier
{
@ -13,7 +14,7 @@ public:
IdentifyRet Identify(pid_t pid) override;
private:
[[nodiscard]] static QString parseCGroupsPath(const QString &CGP) noexcept;
[[nodiscard]] static QString parseCGroupsPath(QFile &file) noexcept;
};
#endif