diff --git a/test/unittest/common/include/system_info.h b/test/unittest/common/include/system_info.h old mode 100755 new mode 100644 index a3ad5de74d5db04960fc44b3d1d99fb9a677c126..d9afcfea63787cd8cbbfa95b521fb2100d3078fb --- a/test/unittest/common/include/system_info.h +++ b/test/unittest/common/include/system_info.h @@ -57,7 +57,6 @@ private: int32_t cstime { 0 }; }; int32_t GetTaskPidFile(const std::string& process_name); - int32_t GetTaskPidCmd(const std::string& process_name, int32_t flag = 0, std::string user = ""); int32_t GetProcOccupy(int32_t pid); int32_t GetSystemCpuStatInfo(Total_Cpu_Occupy& info); diff --git a/test/unittest/common/src/system_info.cpp b/test/unittest/common/src/system_info.cpp old mode 100755 new mode 100644 index f21a27ccc692ec0b483493464034f8c8cec5119f..97784bbb36340299ab7a06865d8c3779e01d40dd --- a/test/unittest/common/src/system_info.cpp +++ b/test/unittest/common/src/system_info.cpp @@ -65,7 +65,11 @@ int32_t CpuInfo::GetTaskPidFile(const std::string& process_name) continue; } std::string strLine; - std::getline(filePath, strLine); + if (!std::getline(filePath, strLine)) { + SEN_HILOGE("getline fail"); + filePath.close(); + continue; + } if (strLine.empty()) { filePath.close(); continue; @@ -76,7 +80,7 @@ int32_t CpuInfo::GetTaskPidFile(const std::string& process_name) } while (std::getline(filePath, strLine)) { if ((strLine.find("Pid")) != std::string::npos) { - if (sscanf_s(strLine.c_str(), "%*s%d", &pid) == -1) { + if (::sscanf_s(strLine.c_str(), "%*s%d", &pid, sizeof(pid)) != 1) { SEN_HILOGE("sscanf_s failed"); } break; @@ -90,34 +94,6 @@ int32_t CpuInfo::GetTaskPidFile(const std::string& process_name) return pid; } -int32_t CpuInfo::GetTaskPidCmd(const std::string& process_name, int32_t flag, std::string user) -{ - std::string command; - if (flag) { - if (user.empty()) { - user = ::getlogin(); - } - command = "pgrep " + process_name + " -u " + user; - } else { - command = "pidof -s " + process_name; - } - ::FILE *fp = nullptr; - if ((fp = ::popen(command.c_str(), "r")) == nullptr) { - SEN_HILOGE("Failed to open, cmd:%{public}s", command.c_str()); - fp = nullptr; - return DEFAULT_PID; - } - char buf[100] = { 0 }; - if (::fgets(buf, sizeof(buf), fp) == nullptr) { - SEN_HILOGE("Failed to read content"); - ::pclose(fp); - fp = nullptr; - return DEFAULT_PID; - } - ::pclose(fp); - return ::atoi(buf); -} - int32_t CpuInfo::GetProcOccupy(int32_t pid) { Proc_Cpu_Occupy info; @@ -129,7 +105,11 @@ int32_t CpuInfo::GetProcOccupy(int32_t pid) } std::string strLine; - std::getline(file, strLine); + if (!std::getline(file, strLine)) { + SEN_HILOGE("getline fail"); + file.close(); + return OHOS::Sensors::ERROR; + } if (strLine.empty()) { SEN_HILOGE("Failed to read content"); file.close(); @@ -170,7 +150,11 @@ int32_t CpuInfo::GetSystemCpuStatInfo(Total_Cpu_Occupy& info) return FILE_OPEN_FAIL; } std::string strLine; - std::getline(statFile, strLine); + if (!std::getline(statFile, strLine)) { + SEN_HILOGE("getline fail"); + statFile.close(); + return STREAM_BUF_READ_FAIL; + } if (strLine.empty()) { SEN_HILOGE("No valid content was read"); statFile.close();