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
25namespace COMMON {
26bool IsProcessRunning(int& lockFileFd); // add file lock, only one process can run
27bool IsProcessExist(const std::string& processName, int& pid); // Check if the process exists and get PID
28int StartProcess(const std::string& processBin, std::vector<char*>& argv);
29int KillProcess(int pid);
30void PrintMallinfoLog(const std::string& mallInfoPrefix, const struct mallinfo2& mi);
31inline int CustomFdClose(int& fd);
32inline int CustomFdFclose(FILE** fp);
33FILE* CustomPopen(const std::vector<std::string>& command, const char* type, int fds[],
34                  volatile pid_t& childPid, bool needUnblock = false);
35int CustomPclose(FILE* fp, int fds[], volatile pid_t& childPid, bool needUnblock = false);
36int CustomPUnblock(int fds[]);
37int GetServicePort();
38void SplitString(const std::string& str, const std::string &sep, std::vector<std::string>& ret);
39bool CheckApplicationPermission(int pid, const std::string& processName);
40bool CheckApplicationEncryped(int pid, const std::string& processName);
41bool VerifyPath(const std::string& filePath, const std::vector<std::string>& validPaths);
42bool ReadFile(const std::string& filePath, const std::vector<std::string>& validPaths, std::string& fileContent);
43std::string GetErrorMsg();
44std::string GetTimeStr();
45clockid_t GetClockId(const std::string& clockIdStr);
46std::string GetClockStr(const int32_t clockId);
47void AdaptSandboxPath(std::string& filePath, int pid);
48int32_t GetPackageUid(const std::string &name);
49bool GetCurrentUserId(int32_t &userId);
50bool IsNumeric(const std::string& str);
51bool IsUserMode();
52bool GetDeveloperMode();
53bool IsBetaVersion();
54std::pair<bool, std::string> CheckNotExistsFilePath(const std::string& filePath);
55bool CheckWhiteList(const std::string& cmdPath);
56bool CheckCmdLineArgValid(const std::string& cmdLine);
57} // COMMON
58#endif // COMMON_H