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 GetUidGidFromPid(pid_t pid, uid_t& ruid, gid_t& rgid); 54 bool GetDeveloperMode(); 55 bool ContainsSpecialChars(const std::string& input); 56 bool IsBetaVersion(); 57 std::pair<bool, std::string> CheckNotExistsFilePath(const std::string& filePath); 58 bool CheckWhiteList(const std::string& cmdPath); 59 bool CheckCmdLineArgValid(const std::string& cmdLine); 60 int PluginWriteToHisysevent (const std::string& pluginName, const std::string& caller, const std::string& args, 61 const int errorCode, const std::string& errorMessage); 62 std::string GetProcessNameByPid(int32_t pid); 63 64 static const std::string STATE_VERSION = "1.0"; 65 enum ErrorType { 66 RET_NO_PERMISSION, 67 RET_NOT_SUPPORT, 68 RET_INVALID_PATH, 69 RET_INVALID_PID, 70 RET_MSG_EMPTY, 71 RET_FAIL = -1, 72 RET_SUCC = 0, 73 }; 74 75 class SpinLock { 76 public: Lock()77 void Lock() 78 { 79 while (flag.test_and_set(std::memory_order_acquire)) {} 80 } 81 Unlock()82 void Unlock() 83 { 84 flag.clear(std::memory_order_release); 85 } 86 private: 87 std::atomic_flag flag = ATOMIC_FLAG_INIT; 88 }; 89 } // COMMON 90 #endif // COMMON_H