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 void PrintMallinfoLog(const std::string& mallInfoPrefix, const struct mallinfo2& mi);
31 inline int CustomFdClose(int& fd);
32 inline int CustomFdFclose(FILE** fp);
33 FILE* CustomPopen(const std::vector<std::string>& command, const char* type, int fds[],
34                   volatile pid_t& childPid, bool needUnblock = false);
35 int CustomPclose(FILE* fp, int fds[], volatile pid_t& childPid, bool needUnblock = false);
36 int CustomPUnblock(int fds[]);
37 int GetServicePort();
38 void SplitString(const std::string& str, const std::string &sep, std::vector<std::string>& ret);
39 bool CheckApplicationPermission(int pid, const std::string& processName);
40 bool CheckApplicationEncryped(int pid, const std::string& processName);
41 bool VerifyPath(const std::string& filePath, const std::vector<std::string>& validPaths);
42 bool ReadFile(const std::string& filePath, const std::vector<std::string>& validPaths, std::string& fileContent);
43 std::string GetErrorMsg();
44 std::string GetTimeStr();
45 clockid_t GetClockId(const std::string& clockIdStr);
46 std::string GetClockStr(const int32_t clockId);
47 void AdaptSandboxPath(std::string& filePath, int pid);
48 int32_t GetPackageUid(const std::string &name);
49 bool GetCurrentUserId(int32_t &userId);
50 bool IsNumeric(const std::string& str);
51 bool IsUserMode();
52 bool GetDeveloperMode();
53 bool IsBetaVersion();
54 std::pair<bool, std::string> CheckNotExistsFilePath(const std::string& filePath);
55 bool CheckWhiteList(const std::string& cmdPath);
56 bool CheckCmdLineArgValid(const std::string& cmdLine);
57 } // COMMON
58 #endif // COMMON_H