1 /* 2 * Copyright (c) Huawei Technologies Co., Ltd. 2021-2023. 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 * 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 #ifndef COMMON_H 16 #define COMMON_H 17 18 #include <malloc.h> 19 #include <string> 20 #include <vector> 21 #include <sys/types.h> 22 #include <unistd.h> 23 24 25 namespace COMMON { 26 bool IsProcessRunning(int& lockFileFd); // add file lock, only one process can run 27 bool IsProcessExist(const std::string& processName, int& pid); // Check if the process exists and get PID 28 int StartProcess(const std::string& processBin, std::vector<char*>& argv); 29 int KillProcess(int pid); 30 bool CheckSubscribeVersion(const std::string& version); 31 void PrintMallinfoLog(const std::string& mallInfoPrefix, const struct mallinfo2& mi); 32 inline int CustomFdClose(int& fd); 33 inline int CustomFdFclose(FILE** fp); 34 FILE* CustomPopen(const std::vector<std::string>& command, const char* type, int fds[], 35 volatile pid_t& childPid, bool needUnblock = false); 36 int CustomPclose(FILE* fp, int fds[], volatile pid_t& childPid, bool needUnblock = false); 37 int CustomPUnblock(int fds[]); 38 int GetServicePort(); 39 void SplitString(const std::string& str, const std::string &sep, std::vector<std::string>& ret); 40 bool CheckApplicationPermission(int pid, const std::string& processName); 41 bool CheckApplicationEncryped(int pid, const std::string& processName); 42 bool VerifyPath(const std::string& filePath, const std::vector<std::string>& validPaths); 43 bool ReadFile(const std::string& filePath, const std::vector<std::string>& validPaths, std::string& fileContent); 44 std::string GetErrorMsg(); 45 std::string GetTimeStr(); 46 clockid_t GetClockId(const std::string& clockIdStr); 47 std::string GetClockStr(const int32_t clockId); 48 void AdaptSandboxPath(std::string& filePath, int pid); 49 int32_t GetPackageUid(const std::string &name); 50 bool GetCurrentUserId(int32_t &userId); 51 bool IsNumeric(const std::string& str); 52 bool IsUserMode(); 53 bool GetDeveloperMode(); 54 bool IsBetaVersion(); 55 std::pair<bool, std::string> CheckNotExistsFilePath(const std::string& filePath); 56 bool CheckWhiteList(const std::string& cmdPath); 57 bool CheckCmdLineArgValid(const std::string& cmdLine); 58 int PluginWriteToHisysevent (const std::string& pluginName, const std::string& caller, const std::string& args, 59 const int errorCode, const std::string& errorMessage); 60 std::string GetProcessNameByPid(int32_t pid); 61 62 static const std::string STATE_VERSION = "1.0"; 63 enum ErrorType { 64 RET_NO_PERMISSION, 65 RET_NOT_SUPPORT, 66 RET_INVALID_PATH, 67 RET_INVALID_PID, 68 RET_MSG_EMPTY, 69 RET_FAIL = -1, 70 RET_SUCC = 0, 71 }; 72 } // COMMON 73 #endif // COMMON_H