From f0060d5fa5272d9617640688483d6784b6c59561 Mon Sep 17 00:00:00 2001 From: Wu Shangwei <2826256824@qq.com> Date: Fri, 30 Jul 2021 15:01:31 +0800 Subject: [PATCH 1/2] LogPersister: Fix crash issue Signed-off-by: Wu Shangwei <2826256824@qq.com> Change-Id: I1a7e9ae780655d0a69b07b92eaa808382d3572b0 --- services/hilogd/include/log_persister.h | 1 + services/hilogd/include/log_persister_rotator.h | 8 +++----- services/hilogd/log_persister.cpp | 5 +++++ services/hilogd/log_persister_rotator.cpp | 13 +++++++++++-- services/hilogd/log_querier.cpp | 15 ++++++++------- 5 files changed, 28 insertions(+), 14 deletions(-) diff --git a/services/hilogd/include/log_persister.h b/services/hilogd/include/log_persister.h index bc80893..8af781c 100644 --- a/services/hilogd/include/log_persister.h +++ b/services/hilogd/include/log_persister.h @@ -60,6 +60,7 @@ public: uint8_t GetType() const; std::string getPath(); int SaveInfo(LogPersistStartMsg& pMsg); + void SetRestore(bool flag); LogPersisterBuffer *buffer; LogPersisterBuffer *compressBuffer; private: diff --git a/services/hilogd/include/log_persister_rotator.h b/services/hilogd/include/log_persister_rotator.h index fd2a147..a6ee48a 100644 --- a/services/hilogd/include/log_persister_rotator.h +++ b/services/hilogd/include/log_persister_rotator.h @@ -16,17 +16,15 @@ #define _HILOG_PERSISTER_ROTATOR_H #include #include +#include "hilog_common.h" namespace OHOS { namespace HiviewDFX { const std::string ANXILLARY_FILE_NAME = "persisterInfo_"; class LogPersisterRotator { public: LogPersisterRotator(std::string path, uint32_t fileSize, uint32_t fileNum, std::string suffix = ""); - ~LogPersisterRotator() - { - fclose(fdinfo); - } - void Init(); + ~LogPersisterRotator(); + int Init(); int Input(const char *buf, uint32_t length); void FillInfo(uint32_t *size, uint32_t *num); void FinishInput(); diff --git a/services/hilogd/log_persister.cpp b/services/hilogd/log_persister.cpp index 1ac6fe4..55f156c 100644 --- a/services/hilogd/log_persister.cpp +++ b/services/hilogd/log_persister.cpp @@ -426,5 +426,10 @@ int LogPersister::SaveInfo(LogPersistStartMsg& pMsg) cout << "Saved Path=" << info.msg.filePath << endl; return RET_SUCCESS; } + +void LogPersister::SetRestore(bool flag) +{ + restore = flag; +} } // namespace HiviewDFX } // namespace OHOS diff --git a/services/hilogd/log_persister_rotator.cpp b/services/hilogd/log_persister_rotator.cpp index 67e0982..0fadb5f 100644 --- a/services/hilogd/log_persister_rotator.cpp +++ b/services/hilogd/log_persister_rotator.cpp @@ -32,7 +32,14 @@ LogPersisterRotator::LogPersisterRotator(string path, uint32_t fileSize, uint32_ needRotate = false; } -void LogPersisterRotator::Init() +LogPersisterRotator::~LogPersisterRotator() +{ + if (fdinfo != nullptr) { + fclose(fdinfo); + } +} + +int LogPersisterRotator::Init() { int nPos = fileName.find_last_of('/'); std::string mmapPath = fileName.substr(0, nPos) + "/." + ANXILLARY_FILE_NAME + to_string(id); @@ -41,7 +48,9 @@ void LogPersisterRotator::Init() mkdir(fileName.substr(0, nPos).c_str(), S_IRUSR | S_IWUSR | S_IXUSR | S_IRWXG | S_IRWXO); } } - fdinfo = fopen((mmapPath + ".info").c_str(), "w+"); + fdinfo = fopen((mmapPath + ".info").c_str(), "r+"); + if (fdinfo == nullptr) return RET_FAIL; + return RET_SUCCESS; } int LogPersisterRotator::Input(const char *buf, uint32_t length) diff --git a/services/hilogd/log_querier.cpp b/services/hilogd/log_querier.cpp index e788b2d..d05fb8f 100644 --- a/services/hilogd/log_querier.cpp +++ b/services/hilogd/log_querier.cpp @@ -134,7 +134,6 @@ void HandlePersistStartRequest(char* reqMsg, std::shared_ptr logReade strcpy_s(pLogPersistStartMsg->filePath, FILE_PATH_MAX_LEN, logPersisterPath.c_str()); rotator = MakeRotator(*pLogPersistStartMsg); rotator->SetId(pLogPersistStartMsg->jobId); - rotator->Init(); std::shared_ptr persister = make_shared( pLogPersistStartMsg->jobId, pLogPersistStartMsg->filePath, @@ -146,8 +145,9 @@ void HandlePersistStartRequest(char* reqMsg, std::shared_ptr logReade int saveInfoRes = persister->SaveInfo(*pLogPersistStartMsg); pLogPersistStartRst->jobId = pLogPersistStartMsg->jobId; pLogPersistStartRst->result = persister->Init(); - if (pLogPersistStartRst->result == RET_FAIL || saveInfoRes == RET_FAIL) { - cout << "Error initializing log persister!" << endl; + int rotatorRes = rotator->Init(); + if (pLogPersistStartRst->result == RET_FAIL || saveInfoRes == RET_FAIL || rotatorRes == RET_FAIL) { + cout << "LogPersister failed to initialize!" << endl; persister.reset(); } else { persister->Start(); @@ -589,7 +589,6 @@ int LogQuerier::RestorePersistJobs(HilogBuffer& _buffer) LogPersisterRotator* rotator = rotator = MakeRotator(info.msg); rotator->SetIndex(info.index + 1); rotator->SetId(info.msg.jobId); - rotator->Init(); printf("Recovery Info:\njobId=%u\nfilePath=%s\n", info.msg.jobId, info.msg.filePath); std::shared_ptr persister = make_shared( @@ -598,11 +597,13 @@ int LogQuerier::RestorePersistJobs(HilogBuffer& _buffer) info.msg.fileSize, info.msg.compressAlg, SLEEP_TIME, *rotator, _buffer); - int result = persister->Init(); + persister->SetRestore(true); + int persisterRes = persister->Init(); + int rotatorRes = rotator->Init(); persister->queryCondition.types = info.types; persister->queryCondition.levels = info.levels; - if (result == RET_FAIL) { - cout << "LogPersister Start Failed, result=" << result << endl; + if (persisterRes == RET_FAIL || rotatorRes == RET_FAIL) { + cout << "LogPersister failed to initialize!" << endl; persister.reset(); } else { persister->Start(); -- Gitee From 097e62b0de453359054ec1116ca4b005a4c31170 Mon Sep 17 00:00:00 2001 From: Wu Shangwei <2826256824@qq.com> Date: Fri, 30 Jul 2021 16:06:02 +0800 Subject: [PATCH 2/2] Update LogPersister Signed-off-by: Wu Shangwei <2826256824@qq.com> Change-Id: I34e53586ac32d5bbd593ebe242735c569f387d71 --- services/hilogd/log_persister.cpp | 1 - services/hilogd/log_persister_rotator.cpp | 6 +++--- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/services/hilogd/log_persister.cpp b/services/hilogd/log_persister.cpp index 55f156c..7be6921 100644 --- a/services/hilogd/log_persister.cpp +++ b/services/hilogd/log_persister.cpp @@ -123,7 +123,6 @@ int LogPersister::Init() if (errno == EEXIST) { cout << "File already exists!" << endl; fd = open(mmapPath.c_str(), O_RDWR, 0); - restore = true; } } else { #ifdef DEBUG diff --git a/services/hilogd/log_persister_rotator.cpp b/services/hilogd/log_persister_rotator.cpp index 0fadb5f..1be6d5a 100644 --- a/services/hilogd/log_persister_rotator.cpp +++ b/services/hilogd/log_persister_rotator.cpp @@ -100,10 +100,10 @@ void LogPersisterRotator::Rotate() ss << fileName << "." << index << fileSuffix; cout << "THE FILE NAME !!!!!!! " << ss.str() << endl; output.open(ss.str(), ios::app); - fseek(fdinfo, 0, SEEK_SET); - fwrite(&index, sizeof(uint8_t), 1, fdinfo); - fsync(fileno(fdinfo)); } + fseek(fdinfo, 0, SEEK_SET); + fwrite(&index, sizeof(uint8_t), 1, fdinfo); + fsync(fileno(fdinfo)); } void LogPersisterRotator::FillInfo(uint32_t *size, uint32_t *num) -- Gitee