1 /* 2 * Copyright (c) 2021 Huawei Device Co., Ltd. 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 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #ifndef PROCESS_DATA_PLUGIN_H 17 #define PROCESS_DATA_PLUGIN_H 18 19 #include <algorithm> 20 #include <dirent.h> 21 #include <fcntl.h> 22 #include <inttypes.h> 23 #include <iomanip> 24 #include <string> 25 #include <sys/stat.h> 26 #include <sys/types.h> 27 #include <unistd.h> 28 #include <unordered_map> 29 #include <utility> 30 31 #include "logging.h" 32 #include "process_plugin_config.pb.h" 33 #include "process_plugin_result.pb.h" 34 35 enum ErrorType { 36 RET_NULL_ADDR, 37 RET_IVALID_PID, 38 RET_TGID_VALUE_NULL, 39 RET_FAIL = -1, 40 RET_SUCC = 0, 41 }; 42 43 class ProcessDataPlugin { 44 public: 45 ProcessDataPlugin(); 46 ~ProcessDataPlugin(); 47 int Start(const uint8_t* configData, uint32_t configSize); 48 int Report(uint8_t* configData, uint32_t configSize); 49 int Stop(); SetPath(char * path)50 void SetPath(char* path) 51 { 52 testpath_ = path; 53 }; 54 void WriteProcesseList(ProcessData& data); 55 void WriteProcinfoByPidfds(ProcessInfo* processinfo, int32_t pid); 56 DIR* OpenDestDir(const char* dirPath); 57 int32_t GetValidPid(DIR* dirp); 58 // for test change static 59 int ParseNumber(std::string line); 60 61 private: 62 ProcessConfig protoConfig_; 63 64 std::unique_ptr<uint8_t[]> buffer_; 65 66 std::unordered_map<int32_t, std::vector<int>> pidFds_; 67 std::vector<int32_t> seenPids_; 68 char* testpath_; 69 int32_t err_; 70 int32_t ReadFile(int fd); 71 std::vector<int> OpenProcPidFiles(int32_t pid); 72 int32_t ReadProcPidFile(int32_t pid, const char* pFileName); 73 void WriteProcessInfo(ProcessData& data, int32_t pid); 74 void SetEmptyProcessInfo(ProcessInfo* processinfo); 75 void WriteProcess(ProcessInfo* processinfo, const char* pFile, uint32_t fileLen, int32_t pid); 76 void SetProcessInfo(ProcessInfo* processinfo, int key, const char* word); 77 78 bool BufnCmp(const char* src, int srcLen, const char* key, int keyLen); 79 bool addPidBySort(int32_t pid); 80 int GetProcStatusId(const char* src, int srcLen); 81 }; 82 83 #endif 84