kinfocenter6/0002-refactor-dmidecode-helper-support-multiple-methods.patch
Christophe Marin f8298c7b5e - Add patches to fix the memory info page by switching from pkexec
to KAuth:
  * 0001-kcms-pull-dmidecode-helper-into-a-separate-dir.patch
  * 0002-refactor-dmidecode-helper-support-multiple-methods.patch
  * 0003-kcms-memory-use-KAuth-dmidecode-helper.patch

OBS-URL: https://build.opensuse.org/package/show/KDE:Frameworks/kinfocenter6?expand=0&rev=34
2024-10-17 08:46:29 +00:00

151 lines
5.8 KiB
Diff

From e6f449a1aa0bf5b1dceaff13390955bd3d21de9d Mon Sep 17 00:00:00 2001
From: Kristen McWilliam <kmcwilliampublic@gmail.com>
Date: Thu, 5 Sep 2024 13:03:45 -0400
Subject: [PATCH 2/3] refactor(dmidecode-helper): support multiple methods
This allows the helper to extend and support other
types of information, eg. memory information.
---
kcms/helpers/dmidecode-helper/helper.cpp | 60 +++++++++++++++++-------
kcms/helpers/dmidecode-helper/helper.h | 15 ++++++
2 files changed, 58 insertions(+), 17 deletions(-)
diff --git a/kcms/helpers/dmidecode-helper/helper.cpp b/kcms/helpers/dmidecode-helper/helper.cpp
index 396b2d74..e9d068dc 100644
--- a/kcms/helpers/dmidecode-helper/helper.cpp
+++ b/kcms/helpers/dmidecode-helper/helper.cpp
@@ -1,5 +1,6 @@
// SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
// SPDX-FileCopyrightText: 2021-2022 Harald Sitter <sitter@kde.org>
+// SPDX-FileCopyrightText: 2024 Kristen McWilliam <kmcwilliampublic@gmail.com>
#include "helper.h"
@@ -16,16 +17,18 @@ auto make_array(Input &&...args) -> std::array<Output, sizeof...(args)> // NB: w
return {std::forward<Input>(args)...};
}
-KAuth::ActionReply DMIDecodeHelper::systeminformation(const QVariantMap &args)
+DMIDecodeHelper::DMIDecodeHelper(QObject *parent)
+ : QObject(parent)
{
- Q_UNUSED(args);
-
// PATH is super minimal when invoked through dbus
setenv("PATH", "/usr/sbin:/sbin:/usr/local/sbin", 1);
- const QString dmidecode = QStandardPaths::findExecutable("dmidecode");
- if (dmidecode.isEmpty()) {
- return KAuth::ActionReply::HelperErrorReply();
- }
+
+ m_dmidecodePath = QStandardPaths::findExecutable("dmidecode");
+}
+
+KAuth::ActionReply DMIDecodeHelper::systeminformation(const QVariantMap &args)
+{
+ Q_UNUSED(args);
// NB: Microsoft also outlines a limited set of DMI values to be required for IOT OEM licensing, as such we
// can rely on the same fields to have sound content . Since this only applies to OEMs we still need to filter
@@ -33,19 +36,20 @@ KAuth::ActionReply DMIDecodeHelper::systeminformation(const QVariantMap &args)
// https://docs.microsoft.com/en-us/windows-hardware/manufacture/iot/license-requirements?view=windows-11#smbios-support
KAuth::ActionReply reply;
- for (const auto &key : {QStringLiteral("system-manufacturer"),
- QStringLiteral("system-product-name"),
- QStringLiteral("system-version"),
- QStringLiteral("system-serial-number")}) {
- QProcess proc;
- proc.start(dmidecode, {QStringLiteral("--string"), key});
- proc.waitForFinished();
- const QByteArray output = proc.readAllStandardOutput().trimmed();
-
- if (output.isEmpty() || proc.error() != QProcess::UnknownError || proc.exitStatus() != QProcess::NormalExit) {
+
+ const auto keys = {QStringLiteral("system-manufacturer"),
+ QStringLiteral("system-product-name"),
+ QStringLiteral("system-version"),
+ QStringLiteral("system-serial-number")};
+
+ for (const auto &key : keys) {
+ auto result = executeDmidecode({QStringLiteral("--string"), key});
+ if (result.failed()) {
continue;
}
+ const auto output = result.data().value("result").toString();
+
// Fairly exhaustive filter list based on a dozen or so samples gathered from reddit and other places.
// These are values that may appear in the DMI system information but aren't really useful.
static const auto dummyData = make_array<QString>(QStringLiteral("system version"),
@@ -59,12 +63,34 @@ KAuth::ActionReply DMIDecodeHelper::systeminformation(const QVariantMap &args)
QStringLiteral("not specified"),
QStringLiteral("not applicable")
/* may also be empty, but that is filtered above already */);
+
if (std::find(dummyData.cbegin(), dummyData.cend(), output.toLower()) != dummyData.cend()) {
continue;
}
reply.addData(key, output);
}
+
+ return reply;
+}
+
+KAuth::ActionReply DMIDecodeHelper::executeDmidecode(const QStringList &arguments)
+{
+ if (m_dmidecodePath.isEmpty()) {
+ return KAuth::ActionReply::HelperErrorReply();
+ }
+
+ QProcess proc;
+ proc.start(m_dmidecodePath, arguments);
+ proc.waitForFinished();
+ const QByteArray output = proc.readAllStandardOutput().trimmed();
+
+ if (output.isEmpty() || proc.error() != QProcess::UnknownError || proc.exitStatus() != QProcess::NormalExit) {
+ return KAuth::ActionReply::HelperErrorReply();
+ }
+
+ KAuth::ActionReply reply;
+ reply.addData("result", output);
return reply;
}
diff --git a/kcms/helpers/dmidecode-helper/helper.h b/kcms/helpers/dmidecode-helper/helper.h
index dd8fe90d..c1b588e8 100644
--- a/kcms/helpers/dmidecode-helper/helper.h
+++ b/kcms/helpers/dmidecode-helper/helper.h
@@ -1,5 +1,6 @@
// SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
// SPDX-FileCopyrightText: 2021 Harald Sitter <sitter@kde.org>
+// SPDX-FileCopyrightText: 2024 Kristen McWilliam <kmcwilliampublic@gmail.com>
#pragma once
@@ -8,6 +9,20 @@
class DMIDecodeHelper : public QObject
{
Q_OBJECT
+
+public:
+ explicit DMIDecodeHelper(QObject *parent = nullptr);
+
public Q_SLOTS:
KAuth::ActionReply systeminformation(const QVariantMap &args);
+
+private:
+ KAuth::ActionReply executeDmidecode(const QStringList &arguments);
+
+ /**
+ * Path to the dmidecode binary.
+ *
+ * If the binary is not found, this helper will not work.
+ */
+ QString m_dmidecodePath;
};
--
2.47.0