From 36c58d7db48eb88ad53f7ff4e142e4c1b6fd84cb Mon Sep 17 00:00:00 2001
From: meizhigang <meizhigang@kylinsec.com.cn>
Date: Wed, 6 Sep 2023 12:00:30 +0800
Subject: [PATCH] fix(systeminfo):Fix to get pci info for multiple device
 subclass
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

 - 适配多个设备子类型的pci信息获取
---
 CMakeLists.txt                             |  4 +--
 plugins/systeminfo/systeminfo-hardware.cpp | 31 +++++++++++++---------
 2 files changed, 21 insertions(+), 14 deletions(-)

diff --git a/CMakeLists.txt b/CMakeLists.txt
index 18a370e..f314b3d 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -16,7 +16,7 @@ pkg_search_module(GIOUNIX REQUIRED gio-unix-2.0)
 pkg_search_module(LIBXML2 REQUIRED libxml++-2.6)
 pkg_search_module(KLOG_GTK3 REQUIRED klog-gtk3)
 pkg_search_module(JSONCPP REQUIRED jsoncpp)
-pkg_search_module(GTEST REQUIRED gtest)
+# pkg_search_module(GTEST REQUIRED gtest)
 pkg_search_module(FMT REQUIRED fmt)
 pkg_search_module(CRYPTOPP REQUIRED cryptopp)
 
@@ -57,4 +57,4 @@ add_subdirectory(data)
 add_subdirectory(lib)
 add_subdirectory(src)
 add_subdirectory(plugins)
-add_subdirectory(test)
+# add_subdirectory(test)
diff --git a/plugins/systeminfo/systeminfo-hardware.cpp b/plugins/systeminfo/systeminfo-hardware.cpp
index bc4aaa9..3cc11fb 100644
--- a/plugins/systeminfo/systeminfo-hardware.cpp
+++ b/plugins/systeminfo/systeminfo-hardware.cpp
@@ -85,16 +85,16 @@ HardwareInfo SystemInfoHardware::get_hardware_info()
     return hardware_info;
 }
 
-CPUInfo SystemInfoHardware::merge_cpu_infos(const std::vector<CPUInfo> &cpu_infos)
+CPUInfo SystemInfoHardware::merge_cpu_infos(const std::vector<CPUInfo>& cpu_infos)
 {
     CPUInfo cpu_info;
-    for(auto& iter : cpu_infos)
+    for (auto& iter : cpu_infos)
     {
-        if(cpu_info.model.empty())
+        if (cpu_info.model.empty())
         {
             cpu_info.model = iter.model;
         }
-        if(cpu_info.cores_number == 0)
+        if (cpu_info.cores_number == 0)
         {
             cpu_info.cores_number = iter.cores_number;
         }
@@ -157,7 +157,7 @@ CPUInfo SystemInfoHardware::read_cpu_info_by_conf()
     CPUInfo cpu_info;
     auto cpu_maps = this->parse_info_file(CPUINFO_FILE, CPUINFO_KEY_DELIMITER);
     //适配龙芯架构
-    if(cpu_info.model.empty())
+    if (cpu_info.model.empty())
     {
         cpu_info.model = cpu_maps[CPUINFO_KEY_MODEL_LS];
     }
@@ -359,15 +359,13 @@ KVList SystemInfoHardware::get_pcis_by_major_class_id(PCIMajorClassID major_clas
     RETURN_VAL_IF_TRUE(full_class_ids.size() == 0, KVList());
 
     // 根据full_class_id列表获取设备相关信息
+    std::string full_outputs;
+    for (auto& full_class_id : full_class_ids)
     {
         std::string cmd_output;
         std::vector<std::string> argv{PCIINFO_CMD, "-vmm"};
-
-        for (auto& full_class_id : full_class_ids)
-        {
-            argv.push_back("-d");
-            argv.push_back(fmt::format("::{:04x}", full_class_id));
-        }
+        argv.push_back("-d");
+        argv.push_back(fmt::format("::{:04x}", full_class_id));
 
         KLOG_DEBUG("cmdline: %s.", StrUtils::join(argv, " ").c_str());
         try
@@ -383,8 +381,17 @@ KVList SystemInfoHardware::get_pcis_by_major_class_id(PCIMajorClassID major_clas
             KLOG_WARNING("%s", e.what().c_str());
             return KVList();
         }
-        return this->format_to_kv_list(cmd_output);
+
+        full_outputs.append(cmd_output);
     }
+
+    if (full_outputs.empty())
+    {
+        KLOG_WARNING("Get empty pci info calss id:%d.", major_class_id);
+        return KVList();
+    }
+
+    return this->format_to_kv_list(full_outputs);
 }
 
 KVList SystemInfoHardware::format_to_kv_list(const std::string& contents)
-- 
2.27.0

