• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
44     template <typename T> bool ParseSyseventLineInfo(const char* data, size_t len, T hisyseventInfoProto);
45 
46     template <typename T> bool WriteResult(const T hisyseventInfoProto);
47 
48     template <typename T> void FlushDataOptimize(const T hisyseventInfoProto);
49 
50 private:
51     std::atomic<uint64_t> id_;
52     std::vector<std::string> fullCmd_;
53 
54     std::thread workThread_;
55     std::atomic<bool> running_ = true;
56 
57     std::unique_ptr<FILE, std::function<int (FILE*)>> fp_;
58 
59     HisyseventConfig protoConfig_;
60     std::vector<char> protoBuffer_;
61     WriterStruct* resultWriter_ = nullptr;
62 
63     volatile pid_t childPid_ = -1;
64     int pipeFds_[2] = {-1, -1};
65 };
66 #endif // !HISYSEVENT_PLUGIN_H
67