diff --git a/frameworks/libhilog/include/hilog_common.h b/frameworks/libhilog/include/hilog_common.h index 367638ca892368e48dd535d1b79e118955860ed8..d95c24b7e0f7dbb741424f384fe0316fed1bbad4 100644 --- a/frameworks/libhilog/include/hilog_common.h +++ b/frameworks/libhilog/include/hilog_common.h @@ -48,6 +48,7 @@ constexpr uint32_t DOMAIN_APP_MIN = 0x0; constexpr uint32_t DOMAIN_APP_MAX = 0xFFFF; constexpr uint32_t JOB_ID_MIN = 10; constexpr uint32_t JOB_ID_MAX = UINT_MAX; +constexpr uint32_t WAITING_DATA_MS = 5000; /* * header of log message from libhilog to hilogd diff --git a/frameworks/libhilog/utils/include/log_utils.h b/frameworks/libhilog/utils/include/log_utils.h index 5493d1bbcf99134d578be5fdcdaa1e1fd7b27f62..f7db0dd6ca6f088c4286b7b6baca1fb7fb0af236 100644 --- a/frameworks/libhilog/utils/include/log_utils.h +++ b/frameworks/libhilog/utils/include/log_utils.h @@ -104,6 +104,12 @@ std::string GetNameByPid(uint32_t pid); uint32_t GetPPidByPid(uint32_t pid); uint64_t GenerateHash(const char *p, size_t size); void PrintErrorno(int err); + +#if !defined(__WINDOWS__) +int WaitingDataMounted(const std::string &path); +int WaitingCgroupMounted(const std::string &path); +int WaitingToDo(int max, const std::string& path, std::function func); +#endif } // namespace HiviewDFX } // namespace OHOS #endif // LOG_UTILS_H \ No newline at end of file diff --git a/frameworks/libhilog/utils/log_utils.cpp b/frameworks/libhilog/utils/log_utils.cpp index c30f1c10a7573bc93cd360842c42c6da89e36f04..8634e6d12cd3138fd12279d3e1e828b7566b2ced 100644 --- a/frameworks/libhilog/utils/log_utils.cpp +++ b/frameworks/libhilog/utils/log_utils.cpp @@ -14,13 +14,17 @@ */ #include #include +#include #include #include #include +#include #include +#include #include #include - +#include +#include #include "hilog_common.h" #include "hilog_cmd.h" #include "log_utils.h" @@ -42,6 +46,7 @@ namespace { namespace OHOS { namespace HiviewDFX { using namespace std; +using namespace std::chrono; // Buffer Size&Char Map static const KVMap g_SizeMap({ @@ -496,5 +501,47 @@ void PrintErrorno(int err) #endif std::cerr << "Errno: " << err << ", " << buf << std::endl; } + +#if !defined(__WINDOWS__) +int WaitingDataMounted(const string &path) +{ + struct stat st; + if (stat(path.c_str(), &st) != -1) { + return 0; + } + return -1; +} + +int WaitingCgroupMounted(const string &path) +{ + int fd; + if (!access(path.c_str(), W_OK)) { + fd = open(path.c_str(), O_WRONLY | O_CLOEXEC); + if (fd >= 0) { + close(fd); + return 0; + } + } + return -1; +} + +int WaitingToDo(int max, const string& path, function func) +{ + chrono::steady_clock::time_point start = chrono::steady_clock::now(); + chrono::milliseconds wait(max); + while (true) { + if (func(path) != -1) { + cout << "waiting for " << path << " successfully!" << endl; + return 0; + } + + std::this_thread::sleep_for(10ms); + if ((chrono::steady_clock::now() - start) > wait) { + cerr << "waiting for " << path << " failed!" << endl; + return -1; + } + } +} +#endif } // namespace HiviewDFX } // namespace OHOS diff --git a/services/hilogd/etc/hilogd.cfg b/services/hilogd/etc/hilogd.cfg index 5adc3d1a078b5b63035d80383d4614e46056651c..f317775439ffcb462a77a5a4e8435fb70f469e0e 100644 --- a/services/hilogd/etc/hilogd.cfg +++ b/services/hilogd/etc/hilogd.cfg @@ -2,11 +2,7 @@ "jobs" : [{ "name" : "init", "cmds" : [ - "start hilogd" - ] - },{ - "name" : "post-fs-data", - "cmds" : [ + "start hilogd", "mkdir /data/log/ 0775 system log", "mkdir /data/log/hilog/ 0755 logd log" ] diff --git a/services/hilogd/main.cpp b/services/hilogd/main.cpp index f20bd52d73a5180a7f828fc5f08c26d0a4413ea5..0c4d27c1a185e1159b58e2e6ce3b45048ef30a32 100644 --- a/services/hilogd/main.cpp +++ b/services/hilogd/main.cpp @@ -16,11 +16,10 @@ #include #include #include -#include + #include #include #include -#include #include #include @@ -34,7 +33,6 @@ #include "service_controller.h" #ifdef DEBUG -#include #include #include #endif @@ -42,12 +40,10 @@ namespace OHOS { namespace HiviewDFX { using namespace std; -using namespace std::chrono; static const string SYSTEM_BG_STUNE = "/dev/stune/system-background/cgroup.procs"; static const string SYSTEM_BG_CPUSET = "/dev/cpuset/system-background/cgroup.procs"; static const string SYSTEM_BG_BLKIO = "/dev/blkio/system-background/cgroup.procs"; -static constexpr int WAITING_DATA_MS = 5000; #ifdef DEBUG static int g_fd = -1; @@ -66,46 +62,6 @@ static void SigHandler(int sig) } } -static int WaitingToDo(int max, const string& path, function func) -{ - chrono::steady_clock::time_point start = chrono::steady_clock::now(); - chrono::milliseconds wait(max); - while (true) { - if (func(path) != -1) { - cout << "waiting for " << path << " successfully!" << endl; - return 0; - } - - std::this_thread::sleep_for(10ms); - if ((chrono::steady_clock::now() - start) > wait) { - cerr << "waiting for " << path << " failed!" << endl; - return -1; - } - } -} - -static int WaitingDataMounted(const string &path) -{ - struct stat st; - if (stat(path.c_str(), &st) != -1) { - return 0; - } - return -1; -} - -static int WaitingCgroupMounted(const string &path) -{ - int fd; - if (!access(path.c_str(), W_OK)) { - fd = open(path.c_str(), O_WRONLY | O_CLOEXEC); - if (fd >= 0) { - close(fd); - return 0; - } - } - return -1; -} - static bool WriteStringToFile(int fd, const std::string& content) { const char *p = content.data(); diff --git a/services/hilogd/service_controller.cpp b/services/hilogd/service_controller.cpp index 9623023abc7c84a29e09a04829cd697d0a5cbb70..1d4251a661b18e5f254b754ced0063bfebb69262 100644 --- a/services/hilogd/service_controller.cpp +++ b/services/hilogd/service_controller.cpp @@ -577,6 +577,10 @@ int StartPersistStoreJob(const PersistRecoveryInfo& info, HilogBuffer& hilogBuff void ServiceController::HandlePersistStartRqst(const PersistStartRqst &rqst) { + if (WaitingToDo(WAITING_DATA_MS, HILOG_FILE_DIR, WaitingDataMounted) == -1) { + WriteErrorRsp(ERR_LOG_PERSIST_FILE_PATH_INVALID); + return; + } int ret = CheckPersistStartRqst(rqst); if (ret != RET_SUCCESS) { WriteErrorRsp(ret);