From 7c877f7b549fd1466ecf9684f74b70fae1c54709 Mon Sep 17 00:00:00 2001
From: wangtaozhi <wangtaozhi@kylinsec.com.cn>
Date: Thu, 11 May 2023 12:00:55 +0800
Subject: [PATCH] fix-identification-issues-in-control-centers

---
 lib/base/str-utils.cpp                     | 39 +++++++++++++++++-----
 lib/base/str-utils.h                       | 17 +++++-----
 plugins/systeminfo/systeminfo-hardware.cpp | 25 +++++++-------
 3 files changed, 53 insertions(+), 28 deletions(-)

diff --git a/lib/base/str-utils.cpp b/lib/base/str-utils.cpp
index 8a38b1e..52e028b 100644
--- a/lib/base/str-utils.cpp
+++ b/lib/base/str-utils.cpp
@@ -1,14 +1,14 @@
 /**
- * Copyright (c) 2020 ~ 2021 KylinSec Co., Ltd. 
+ * Copyright (c) 2020 ~ 2021 KylinSec Co., Ltd.
  * kiran-cc-daemon is licensed under Mulan PSL v2.
- * You can use this software according to the terms and conditions of the Mulan PSL v2. 
+ * You can use this software according to the terms and conditions of the Mulan PSL v2.
  * You may obtain a copy of Mulan PSL v2 at:
- *          http://license.coscl.org.cn/MulanPSL2 
- * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, 
- * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, 
- * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.  
- * See the Mulan PSL v2 for more details.  
- * 
+ *          http://license.coscl.org.cn/MulanPSL2
+ * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
+ * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
+ * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
+ * See the Mulan PSL v2 for more details.
+ *
  * Author:     tangjie02 <tangjie02@kylinos.com.cn>
  */
 
@@ -95,6 +95,29 @@ std::vector<std::string> StrUtils::split_with_char(const std::string &s, char de
     return v;
 }
 
+std::vector<std::string> StrUtils::split_once_with_char(const std::string &s, char delimiter)
+{
+    std::vector<std::string> v;
+    size_t i;
+    for (i = 0; i < s.length(); i++)
+    {
+        if (delimiter == s[i])
+        {
+            v.push_back(s.substr(0, i));
+            break;
+        }
+    }
+    if (s.length() == i)
+    {
+        v.push_back(s);
+    }
+    else
+    {
+        v.push_back(s.substr(i + 1, s.length() - i - 1));
+    }
+    return v;
+}
+
 std::string StrUtils::ltrim(const std::string &s)
 {
     auto iter = std::find_if(s.begin(), s.end(), [](char c) -> bool
diff --git a/lib/base/str-utils.h b/lib/base/str-utils.h
index e7221de..5c8be58 100644
--- a/lib/base/str-utils.h
+++ b/lib/base/str-utils.h
@@ -1,14 +1,14 @@
 /**
- * Copyright (c) 2020 ~ 2021 KylinSec Co., Ltd. 
+ * Copyright (c) 2020 ~ 2021 KylinSec Co., Ltd.
  * kiran-cc-daemon is licensed under Mulan PSL v2.
- * You can use this software according to the terms and conditions of the Mulan PSL v2. 
+ * You can use this software according to the terms and conditions of the Mulan PSL v2.
  * You may obtain a copy of Mulan PSL v2 at:
- *          http://license.coscl.org.cn/MulanPSL2 
- * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, 
- * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, 
- * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.  
- * See the Mulan PSL v2 for more details.  
- * 
+ *          http://license.coscl.org.cn/MulanPSL2
+ * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
+ * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
+ * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
+ * See the Mulan PSL v2 for more details.
+ *
  * Author:     tangjie02 <tangjie02@kylinos.com.cn>
  */
 
@@ -48,6 +48,7 @@ public:
     static std::string tolower(const std::string &str);
     static std::string toupper(const std::string &str);
     static std::vector<std::string> split_with_char(const std::string &s, char delimiter, bool is_merge_delimiter = false);
+    static std::vector<std::string> split_once_with_char(const std::string &s, char delimiter);
 
     // 去掉字符串前后的空白字符
     static std::string ltrim(const std::string &s);
diff --git a/plugins/systeminfo/systeminfo-hardware.cpp b/plugins/systeminfo/systeminfo-hardware.cpp
index 66b7b49..e9975c1 100644
--- a/plugins/systeminfo/systeminfo-hardware.cpp
+++ b/plugins/systeminfo/systeminfo-hardware.cpp
@@ -1,14 +1,14 @@
 /**
- * Copyright (c) 2020 ~ 2021 KylinSec Co., Ltd. 
+ * Copyright (c) 2020 ~ 2021 KylinSec Co., Ltd.
  * kiran-cc-daemon is licensed under Mulan PSL v2.
- * You can use this software according to the terms and conditions of the Mulan PSL v2. 
+ * You can use this software according to the terms and conditions of the Mulan PSL v2.
  * You may obtain a copy of Mulan PSL v2 at:
- *          http://license.coscl.org.cn/MulanPSL2 
- * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, 
- * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, 
- * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.  
- * See the Mulan PSL v2 for more details.  
- * 
+ *          http://license.coscl.org.cn/MulanPSL2
+ * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
+ * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
+ * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
+ * See the Mulan PSL v2 for more details.
+ *
  * Author:     tangjie02 <tangjie02@kylinos.com.cn>
  */
 
@@ -34,7 +34,8 @@ namespace Kiran
 
 #define DISKINFO_CMD "/usr/bin/lsblk"
 
-#define PCIINFO_CMD "/usr/sbin/lspci"
+// 使用相对路径，避免使用绝对路径时因系统版本导致的错误
+#define PCIINFO_CMD "lspci"
 #define PCIINFO_KEY_DELIMITER ':'
 
 SystemInfoHardware::SystemInfoHardware() : mem_size_lshw(0)
@@ -310,7 +311,7 @@ KVList SystemInfoHardware::get_pcis_by_major_class_id(PCIMajorClassID major_clas
         {
             Glib::spawn_sync("",
                              argv,
-                             Glib::SPAWN_DEFAULT,
+                             Glib::SPAWN_SEARCH_PATH,
                              sigc::mem_fun(this, &SystemInfoHardware::set_env),
                              &cmd_output);
         }
@@ -354,7 +355,7 @@ KVList SystemInfoHardware::get_pcis_by_major_class_id(PCIMajorClassID major_clas
         {
             Glib::spawn_sync("",
                              argv,
-                             Glib::SPAWN_DEFAULT,
+                             Glib::SPAWN_SEARCH_PATH,
                              sigc::mem_fun(this, &SystemInfoHardware::set_env),
                              &cmd_output);
         }
@@ -378,7 +379,7 @@ KVList SystemInfoHardware::format_to_kv_list(const std::string& contents)
         auto lines = StrUtils::split_lines(block);
         for (auto& line : lines)
         {
-            auto fields = StrUtils::split_with_char(line, PCIINFO_KEY_DELIMITER);
+            auto fields = StrUtils::split_once_with_char(line, PCIINFO_KEY_DELIMITER);
             if (fields.size() != 2)
             {
                 continue;
-- 
2.27.0

