diff --git a/services/hilogd/include/log_persister.h b/services/hilogd/include/log_persister.h index 8af781c8a4c1a0f2ad4aea7e5c332c7850700556..b8fcd421bb3ce72e844fed729c918ba3cdfd569c 100644 --- a/services/hilogd/include/log_persister.h +++ b/services/hilogd/include/log_persister.h @@ -31,13 +31,6 @@ namespace OHOS { namespace HiviewDFX { using namespace std; -typedef struct { - uint8_t index; - uint16_t types; - uint8_t levels; - LogPersistStartMsg msg; -} PersistRecoveryInfo; - class LogPersister : public LogReader { public: LogPersister(uint32_t id, std::string path, uint32_t fileSize, uint16_t compressAlg, int sleepTime, @@ -59,8 +52,6 @@ public: bool writeUnCompressedBuffer(HilogData *data); uint8_t GetType() const; std::string getPath(); - int SaveInfo(LogPersistStartMsg& pMsg); - void SetRestore(bool flag); LogPersisterBuffer *buffer; LogPersisterBuffer *compressBuffer; private: @@ -79,13 +70,10 @@ private: bool hasExited; inline void WriteFile(); bool isExited(); - FILE *fdinfo = nullptr; - int fd = -1; + FILE* fd = nullptr; LogCompress *compressor; list persistList; uint32_t plainLogSize; - PersistRecoveryInfo info; - bool restore = false; }; int GenPersistLogHeader(HilogData *data, list& persistList); diff --git a/services/hilogd/include/log_persister_rotator.h b/services/hilogd/include/log_persister_rotator.h index a6ee48af9e9560796ed981ec911757cdf3efdffe..eda4ec9ff4a68d1d3957f49f3aacd5512b82ff49 100644 --- a/services/hilogd/include/log_persister_rotator.h +++ b/services/hilogd/include/log_persister_rotator.h @@ -16,10 +16,21 @@ #define _HILOG_PERSISTER_ROTATOR_H #include #include +#include #include "hilog_common.h" +#include "hilogtool_msg.h" +#include "log_buffer.h" namespace OHOS { namespace HiviewDFX { +typedef struct { + uint8_t index; + uint16_t types; + uint8_t levels; + LogPersistStartMsg msg; +} PersistRecoveryInfo; + const std::string ANXILLARY_FILE_NAME = "persisterInfo_"; +uLong GetInfoCRC32(const PersistRecoveryInfo &info); class LogPersisterRotator { public: LogPersisterRotator(std::string path, uint32_t fileSize, uint32_t fileNum, std::string suffix = ""); @@ -30,6 +41,12 @@ public: void FinishInput(); void SetIndex(int pIndex); void SetId(uint32_t pId); + void OpenInfoFile(); + void UpdateRotateNumber(); + int SaveInfo(const LogPersistStartMsg& pMsg, const QueryCondition queryCondition); + void WriteRecoveryInfo(); + void SetRestore(bool flag); + bool GetRestore(); protected: void InternalRotate(); uint32_t fileNum; @@ -41,8 +58,11 @@ protected: private: void Rotate(); bool needRotate = false; - FILE* fdinfo; + FILE* fdinfo = nullptr; uint32_t id = 0; + std::string infoPath; + PersistRecoveryInfo info; + bool restore = false; }; } // namespace HiviewDFX } // namespace OHOS diff --git a/services/hilogd/log_persister.cpp b/services/hilogd/log_persister.cpp index 056e64c40dc250570bab4a2e0d65c594daf1e233..9a571dcfdede7651b22c81e31e6e01fb5b8bd427 100644 --- a/services/hilogd/log_persister.cpp +++ b/services/hilogd/log_persister.cpp @@ -58,7 +58,6 @@ LogPersister::LogPersister(uint32_t id, string path, uint32_t fileSize, uint16_t hasExited = false; hilogBuffer = &_buffer; compressor = nullptr; - fdinfo = nullptr; buffer = nullptr; plainLogSize = 0; } @@ -94,6 +93,7 @@ int LogPersister::InitCompress() int LogPersister::Init() { + bool restore = rotator->GetRestore(); int nPos = path.find_last_of('/'); if (nPos == RET_FAIL) { return ERR_LOG_PERSIST_FILE_PATH_INVALID; @@ -118,42 +118,29 @@ int LogPersister::Init() if (InitCompress() == RET_FAIL) { return ERR_LOG_PERSIST_COMPRESS_INIT_FAIL; } - fd = open(mmapPath.c_str(), O_RDWR | O_CREAT | O_EXCL, S_IRUSR | S_IWUSR); - if (fd <= 0) { - if (errno == EEXIST) { - cout << "File already exists!" << endl; - fd = open(mmapPath.c_str(), O_RDWR, 0); - } + if (restore) { + fd = fopen(mmapPath.c_str(), "r+"); } else { -#ifdef DEBUG - cout << "New log file: " << mmapPath << endl; -#endif - lseek(fd, sizeof(LogPersisterBuffer) - 1, SEEK_SET); - write(fd, "", 1); + fd = fopen(mmapPath.c_str(), "w+"); + ftruncate(fileno(fd), sizeof(LogPersisterBuffer)); + fflush(fd); + fsync(fileno(fd)); } - if (fd < 0) { + + if (fd == nullptr) { #ifdef DEBUG cout << "open log file(" << mmapPath << ") failed: " << strerror(errno) << endl; #endif return ERR_LOG_PERSIST_FILE_OPEN_FAIL; } - fdinfo = fopen((mmapPath + ".info").c_str(), "a+"); - if (fdinfo == nullptr) { -#ifdef DEBUG - cout << "open loginfo file failed: " << strerror(errno) << endl; -#endif - close(fd); - return ERR_LOG_PERSIST_FILE_OPEN_FAIL; - } buffer = (LogPersisterBuffer *)mmap(nullptr, sizeof(LogPersisterBuffer), PROT_READ | PROT_WRITE, - MAP_SHARED, fd, 0); - close(fd); + MAP_SHARED, fileno(fd), 0); + fclose(fd); if (buffer == MAP_FAILED) { #ifdef DEBUG cout << "mmap file failed: " << strerror(errno) << endl; #endif - fclose(fdinfo); - return ERR_LOG_PERSIST_MMAP_FAIL; + return RET_FAIL; } if (restore == true) { #ifdef DEBUG @@ -274,13 +261,7 @@ int LogPersister::WriteData(HilogData *data) void LogPersister::Start() { - if (!restore) { - std::cout << "Save Info file!" << std::endl; - fseek(fdinfo, 0, SEEK_SET); - fwrite(&info, sizeof(PersistRecoveryInfo), 1, fdinfo); - fsync(fileno(fdinfo)); - } - fclose(fdinfo); + hilogBuffer->AddLogReader(weak_from_this()); auto newThread = thread(&LogPersister::ThreadFunc, static_pointer_cast(shared_from_this())); newThread.detach(); @@ -384,6 +365,7 @@ bool LogPersister::isExited() void LogPersister::Exit() { + std::cout << "LogPersister Exit!" << std::endl; toExit = true; condVariable.notify_all(); unique_lock lk(mutexForhasExited); @@ -395,7 +377,6 @@ void LogPersister::Exit() munmap(buffer, MAX_PERSISTER_BUFFER_SIZE); cout << "removed mmap file" << endl; remove(mmapPath.c_str()); - remove((mmapPath + ".info").c_str()); return; } bool LogPersister::Identify(uint32_t id) @@ -412,23 +393,5 @@ uint8_t LogPersister::GetType() const { return TYPE_PERSISTER; } - -int LogPersister::SaveInfo(LogPersistStartMsg& pMsg) -{ - info.msg = pMsg; - info.types = queryCondition.types; - info.levels = queryCondition.levels; - if (strcpy_s(info.msg.filePath, FILE_PATH_MAX_LEN, pMsg.filePath) != 0) { - cout << "Failed to save persister file path" << endl; - return ERR_LOG_PERSIST_FILE_PATH_INVALID; - } - 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 48f2df743a974f8f7131ff717a998bdacb9855ff..407172fc14f84f108cbc6cba8b9b03454474690c 100644 --- a/services/hilogd/log_persister_rotator.cpp +++ b/services/hilogd/log_persister_rotator.cpp @@ -13,18 +13,25 @@ * limitations under the License. */ #include "log_persister_rotator.h" - -#include -#include +#include +#include #include #include -#include -#include +#include +#include +#include namespace OHOS { namespace HiviewDFX { using namespace std; +uLong GetInfoCRC32(const PersistRecoveryInfo &info) +{ + uLong crc = crc32(0L, Z_NULL, 0); + crc = crc32(crc, (Bytef*)(&info), sizeof(PersistRecoveryInfo)); + return crc; +} + LogPersisterRotator::LogPersisterRotator(string path, uint32_t fileSize, uint32_t fileNum, string suffix) : fileNum(fileNum), fileSize(fileSize), fileName(path), fileSuffix(suffix) { @@ -37,19 +44,13 @@ LogPersisterRotator::~LogPersisterRotator() if (fdinfo != nullptr) { fclose(fdinfo); } + remove(infoPath.c_str()); } int LogPersisterRotator::Init() { - int nPos = fileName.find_last_of('/'); - std::string mmapPath = fileName.substr(0, nPos) + "/." + ANXILLARY_FILE_NAME + to_string(id); - if (access(fileName.substr(0, nPos).c_str(), F_OK) != 0) { - if (errno == ENOENT) { - mkdir(fileName.substr(0, nPos).c_str(), S_IRUSR | S_IWUSR | S_IXUSR | S_IRWXG | S_IRWXO); - } - } - fdinfo = fopen((mmapPath + ".info").c_str(), "r+"); - if (fdinfo == nullptr) return ERR_PERSIST_INFO_OPEN_FAIL; + OpenInfoFile(); + if (fdinfo == nullptr) return RET_FAIL; return RET_SUCCESS; } @@ -101,9 +102,7 @@ void LogPersisterRotator::Rotate() cout << "THE FILE NAME !!!!!!! " << ss.str() << endl; output.open(ss.str(), ios::out | ios::trunc); } - fseek(fdinfo, 0, SEEK_SET); - fwrite(&index, sizeof(uint8_t), 1, fdinfo); - fsync(fileno(fdinfo)); + UpdateRotateNumber(); } void LogPersisterRotator::FillInfo(uint32_t *size, uint32_t *num) @@ -126,5 +125,62 @@ void LogPersisterRotator::SetId(uint32_t pId) { id = pId; } + +void LogPersisterRotator::OpenInfoFile() +{ + int nPos = fileName.find_last_of('/'); + std::string mmapPath = fileName.substr(0, nPos) + "/." + ANXILLARY_FILE_NAME + to_string(id); + if (access(fileName.substr(0, nPos).c_str(), F_OK) != 0) { + if (errno == ENOENT) { + mkdir(fileName.substr(0, nPos).c_str(), S_IRUSR | S_IWUSR | S_IXUSR | S_IRWXG | S_IRWXO); + } + } + infoPath = mmapPath + ".info"; + if (restore) { + fdinfo = fopen(infoPath.c_str(), "r+"); + } else { + fdinfo = fopen(infoPath.c_str(), "w+"); + } +} + +void LogPersisterRotator::UpdateRotateNumber() +{ + info.index = index; + WriteRecoveryInfo(); +} + +int LogPersisterRotator::SaveInfo(const LogPersistStartMsg& pMsg, const QueryCondition queryCondition) +{ + info.msg = pMsg; + info.types = queryCondition.types; + info.levels = queryCondition.levels; + if (strcpy_s(info.msg.filePath, FILE_PATH_MAX_LEN, pMsg.filePath) != 0) { + cout << "Failed to save persister file path" << endl; + return RET_FAIL; + } + cout << "Saved Path=" << info.msg.filePath << endl; + return RET_SUCCESS; +} + +void LogPersisterRotator::WriteRecoveryInfo() +{ + std::cout << "Save Info file!" << std::endl; + uLong crc = GetInfoCRC32(info); + fseek(fdinfo, 0, SEEK_SET); + fwrite(&info, sizeof(PersistRecoveryInfo), 1, fdinfo); + fwrite(&crc, sizeof(uLong), 1, fdinfo); + fflush(fdinfo); + fsync(fileno(fdinfo)); +} + +void LogPersisterRotator::SetRestore(bool flag) +{ + restore = flag; +} + +bool LogPersisterRotator::GetRestore() +{ + return restore; +} } // namespace HiviewDFX } // namespace OHOS diff --git a/services/hilogd/log_querier.cpp b/services/hilogd/log_querier.cpp index 24ec6b782d36691faefabaa8760211909a3755fe..cdd88d9ff1cb04b9f288fe12e230e73ecf8e7ba2 100644 --- a/services/hilogd/log_querier.cpp +++ b/services/hilogd/log_querier.cpp @@ -62,7 +62,7 @@ inline bool IsValidFileName(const std::string& strFileName) bool bValid = !std::regex_search(strFileName, regExpress); return bValid; } -LogPersisterRotator* MakeRotator(LogPersistStartMsg& pLogPersistStartMsg) +LogPersisterRotator* MakeRotator(const LogPersistStartMsg& pLogPersistStartMsg) { string fileSuffix = ""; switch (pLogPersistStartMsg.compressAlg) { @@ -82,6 +82,34 @@ LogPersisterRotator* MakeRotator(LogPersistStartMsg& pLogPersistStartMsg) fileSuffix); } +int JobLauncher(const LogPersistStartMsg& pMsg, const HilogBuffer& buffer, bool restore = false, int index = -1) +{ + LogPersisterRotator* rotator = MakeRotator(pMsg); + rotator->SetId(pMsg.jobId); + rotator->SetIndex(index); + std::shared_ptr persister = make_shared( + pMsg.jobId, + pMsg.filePath, + pMsg.fileSize, + pMsg.compressAlg, + SLEEP_TIME, *rotator, const_cast(buffer)); + persister->queryCondition.types = pMsg.logType; + persister->queryCondition.levels = DEFAULT_LOG_LEVEL; + rotator->SetRestore(restore); + int rotatorRes = rotator->Init(); + int saveInfoRes = rotator->SaveInfo(pMsg, persister->queryCondition); + int persistRes = persister->Init(); + if (persistRes == RET_FAIL || saveInfoRes == RET_FAIL || rotatorRes == RET_FAIL) { + cout << "LogPersister failed to initialize!" << endl; + persister.reset(); + return RET_FAIL; + } else { + if (!restore) rotator->WriteRecoveryInfo(); + persister->Start(); + return RET_SUCCESS; + } +} + void HandleLogQueryRequest(std::shared_ptr logReader, HilogBuffer& buffer) { logReader->SetCmd(LOG_QUERY_RESPONSE); @@ -99,7 +127,6 @@ void HandlePersistStartRequest(char* reqMsg, std::shared_ptr logReade { char msgToSend[MAX_DATA_LEN]; const uint16_t sendMsgLen = sizeof(LogPersistStartResult); - LogPersisterRotator *rotator = nullptr; LogPersistStartRequest* pLogPersistStartReq = reinterpret_cast(reqMsg); LogPersistStartMsg* pLogPersistStartMsg @@ -138,27 +165,8 @@ void HandlePersistStartRequest(char* reqMsg, std::shared_ptr logReade return; } strcpy_s(pLogPersistStartMsg->filePath, FILE_PATH_MAX_LEN, logPersisterPath.c_str()); - rotator = MakeRotator(*pLogPersistStartMsg); - rotator->SetId(pLogPersistStartMsg->jobId); - std::shared_ptr persister = make_shared( - pLogPersistStartMsg->jobId, - pLogPersistStartMsg->filePath, - pLogPersistStartMsg->fileSize, - pLogPersistStartMsg->compressAlg, - SLEEP_TIME, *rotator, buffer); - persister->queryCondition.types = pLogPersistStartMsg->logType; - persister->queryCondition.levels = DEFAULT_LOG_LEVEL; - int saveInfoRes = persister->SaveInfo(*pLogPersistStartMsg); pLogPersistStartRst->jobId = pLogPersistStartMsg->jobId; - pLogPersistStartRst->result = persister->Init(); - int rotatorRes = rotator->Init(); - if (pLogPersistStartRst->result != 0 || saveInfoRes != 0 || rotatorRes != 0) { - cout << "LogPersister failed to initialize!" << endl; - persister.reset(); - } else { - persister->Start(); - buffer.AddLogReader(weak_ptr(persister)); - } + pLogPersistStartRst->result = JobLauncher(*pLogPersistStartMsg, buffer); SetMsgHead(&pLogPersistStartRsp->msgHeader, MC_RSP_LOG_PERSIST_START, sendMsgLen); logReader->hilogtoolConnectSocket->Write(msgToSend, sendMsgLen + sizeof(MessageHeader)); } @@ -594,30 +602,18 @@ int LogQuerier::RestorePersistJobs(HilogBuffer& _buffer) } PersistRecoveryInfo info; fread(&info, sizeof(PersistRecoveryInfo), 1, infile); + uLong crcSum = 0L; + fread(&crcSum, sizeof(uLong), 1, infile); fclose(infile); - LogPersisterRotator* rotator = rotator = MakeRotator(info.msg); - rotator->SetIndex(info.index + 1); - rotator->SetId(info.msg.jobId); - printf("Recovery Info:\njobId=%u\nfilePath=%s\n", - info.msg.jobId, info.msg.filePath); - std::shared_ptr persister = make_shared( - info.msg.jobId, - info.msg.filePath, - info.msg.fileSize, - info.msg.compressAlg, - SLEEP_TIME, *rotator, _buffer); - persister->SetRestore(true); - int persisterRes = persister->Init(); - int rotatorRes = rotator->Init(); - persister->queryCondition.types = info.types; - persister->queryCondition.levels = info.levels; - if (persisterRes != 0 || rotatorRes != 0) { - cout << "LogPersister failed to initialize!" << endl; - persister.reset(); - } else { - persister->Start(); - _buffer.AddLogReader(weak_ptr(persister)); + uLong crc = GetInfoCRC32(info); + if (crc != crcSum) { + std::cout << "Info file CRC Checksum Failed!" << std::endl; + continue; } + JobLauncher(info.msg, _buffer, true, info.index + 1); + std::cout << "Recovery Info:" << std::endl << + "jobId=" << (unsigned)(info.msg.jobId) << std::endl << + "filePath=" << (unsigned)(info.msg.filePath) << std::endl; } } closedir (dir);