diff --git a/services/include/updater/updater.h b/services/include/updater/updater.h index 99e0a829e6f798998a2a75f8c9c67cc4f96edb16..cf5aa9087696225260f6d7245f9712fb6c4e24e3 100644 --- a/services/include/updater/updater.h +++ b/services/include/updater/updater.h @@ -60,6 +60,7 @@ struct UpdaterParams { PackageUpdateMode updateMode = HOTA_UPDATE; int retryCount = 0; int panicCount = 0; + pid_t binaryPid = -1; float initialProgress = 0; /* The upgrade starts at the progress bar location */ float currentPercentage = 0; /* The proportion of progress bars occupied by the upgrade process */ unsigned int pkgLocation = 0; diff --git a/services/updater.cpp b/services/updater.cpp index a5b2429e8eaa9c5a5bfe7274722cb735e9b282ea..b4083f65ffc1d9fe39ce3c2180300f16a35e19bd 100644 --- a/services/updater.cpp +++ b/services/updater.cpp @@ -515,10 +515,13 @@ UpdaterStatus HandlePipeMsg(UpdaterParams &upParams, int pipeRead, bool &retryUp return UPDATE_SUCCESS; } -UpdaterStatus CheckProcStatus(pid_t pid, bool retryUpdate) +UpdaterStatus CheckProcStatus(UpdaterParams &upParams, bool retryUpdate) { int status; - if (waitpid(pid, &status, 0) == -1) { + ON_SCOPE_EXIT(resetBinaryPid) { + upParams.binaryPid = -1; + }; + if (waitpid(upParams.binaryPid, &status, 0) == -1) { LOG(ERROR) << "waitpid error"; return UPDATE_ERROR; } @@ -605,6 +608,7 @@ UpdaterStatus StartUpdaterProc(PkgManager::PkgManagerPtr pkgManager, UpdaterPara pid_t pid = fork(); if (pid < 0) { ERROR_CODE(CODE_FORK_FAIL); + upParams.binaryPid = -1; UPDATER_LAST_WORD(UPDATE_ERROR, "fork failed"); return UPDATE_ERROR; } @@ -617,6 +621,7 @@ UpdaterStatus StartUpdaterProc(PkgManager::PkgManagerPtr pkgManager, UpdaterPara ExcuteSubProc(upParams, fullPath, pipeWrite); } + upParams.binaryPid = pid; close(pipeWrite); // close write endpoint bool retryUpdate = false; if (HandlePipeMsg(upParams, pipeRead, retryUpdate) != UPDATE_SUCCESS) { @@ -624,7 +629,7 @@ UpdaterStatus StartUpdaterProc(PkgManager::PkgManagerPtr pkgManager, UpdaterPara return UPDATE_ERROR; } - return CheckProcStatus(pid, retryUpdate); + return CheckProcStatus(upParams, retryUpdate); } std::string GetWorkPath() diff --git a/utils/include/utils.h b/utils/include/utils.h index 11170ddac0cabf48a86ecee9f8685adddfa3366c..51c969ac42e6e9b1d394c80daeb5276762d2b754 100644 --- a/utils/include/utils.h +++ b/utils/include/utils.h @@ -71,6 +71,7 @@ std::string GetCertName(); bool WriteFully(int fd, const uint8_t *data, size_t size); bool ReadFully(int fd, void* data, size_t size); bool ReadFileToString(int fd, std::string &content); +bool ReadStringFromProcFile(const std::string &filePath, std::string &content); bool CopyFile(const std::string &src, const std::string &dest, bool isAppend = false); bool CopyDir(const std::string &srcPath, const std::string &dstPath); bool WriteStringToFile(int fd, const std::string& content); diff --git a/utils/utils.cpp b/utils/utils.cpp index 96341e9159b3d6635262b897e0c10d6a468f3656..15d4193551fe56360bff8061029d5718697268e0 100644 --- a/utils/utils.cpp +++ b/utils/utils.cpp @@ -51,6 +51,7 @@ constexpr int MAX_TIME_SIZE = 20; constexpr size_t PARAM_SIZE = 32; constexpr const char *PREFIX_PARTITION_NODE = "/dev/block/by-name/"; constexpr mode_t DEFAULT_DIR_MODE = 0775; +constexpr long MAX_FILE_LENGTH = 4096; namespace { void UpdateInfoInMisc(const std::string headInfo, const std::optional message, bool isRemove) @@ -342,6 +343,27 @@ bool ReadFileToString(int fd, std::string &content) return true; } +bool ReadStringFromProcFile(const std::string &filePath, std::string &content) +{ + std::ifstream file(filePath.c_str()); + if (!file.is_open()) { + LOG(ERROR) << "failed to open " << filePath << ", err: " << strerror(errno); + return false; + } + + file.seekg(0, std::ios::end); + const long fileLength = file.tellg(); + if (fileLength > MAX_FILE_LENGTH) { + LOG(ERROR) << "file oversize " << fileLength << ", max is " << MAX_FILE_LENGTH; + return false; + } + + content.clear(); + file.seekg(0, std::ios::beg); + std::copy(std::istreambuf_iterator(file), std::istreambuf_iterator(), std::back_inserter(content)); + return true; +} + bool WriteStringToFile(int fd, const std::string& content) { const char *p = content.data();