1 /* 2 * Copyright (c) Huawei Technologies Co., Ltd. 2022. All rights reserved. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 11 * See the License for the specific language governing permissions and 12 * limitations under the License. 13 */ 14 15 #ifndef HISYSEVENT_PLUGIN_H 16 #define HISYSEVENT_PLUGIN_H 17 18 #include "hisysevent_plugin_config.pb.h" 19 #include "hisysevent_plugin_result.pb.h" 20 #include "common.h" 21 #include "logging.h" 22 #include "plugin_module_api.h" 23 24 #include <atomic> 25 #include <cstdio> 26 #include <mutex> 27 #include <thread> 28 #include <vector> 29 30 class HisyseventPlugin { 31 public: 32 HisyseventPlugin(); 33 ~HisyseventPlugin(); 34 int Start(const uint8_t* configData, uint32_t configSize); 35 int Stop(); 36 void Run(void); 37 38 int SetWriter(WriterStruct* writer); 39 40 private: 41 std::string GetFullCmd(); 42 bool InitHisyseventCmd(); 43 std::string GetCmdArgs(const HisyseventConfig& protoConfig); 44 45 template <typename T> bool ParseSyseventLineInfo(const char* data, size_t len, T hisyseventInfoProto); 46 47 template <typename T> bool WriteResult(const T hisyseventInfoProto); 48 49 template <typename T> void FlushDataOptimize(const T hisyseventInfoProto); 50 51 private: 52 std::atomic<uint64_t> id_; 53 std::vector<std::string> fullCmd_; 54 55 std::thread workThread_; 56 std::atomic<bool> running_ = true; 57 58 std::unique_ptr<FILE, std::function<int (FILE*)>> fp_; 59 60 HisyseventConfig protoConfig_; 61 std::vector<char> protoBuffer_; 62 WriterStruct* resultWriter_ = nullptr; 63 64 volatile pid_t childPid_ = -1; 65 int pipeFds_[2] = {-1, -1}; 66 }; 67 #endif // !HISYSEVENT_PLUGIN_H 68