1 /* 2 * Copyright (c) 2022 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 OHOS_MEMORY_MEMMGR_COMMON_INCLUDE_KERNEL_INTERFACE_H 17 #define OHOS_MEMORY_MEMMGR_COMMON_INCLUDE_KERNEL_INTERFACE_H 18 19 #include <sys/types.h> 20 #include <fcntl.h> 21 22 #include <map> 23 #include <string> 24 #include <vector> 25 26 #include "single_instance.h" 27 28 namespace OHOS { 29 namespace Memory { 30 #define PAGE_TO_KB 4 31 #define KB_PER_MB 1024 32 #define MAX_BUFFER_KB 0x7fffffff 33 34 struct ProcInfo { 35 int tgid; 36 int pid; 37 int pidfd; 38 int size; 39 std::string name; 40 std::string status; 41 }; 42 43 class KernelInterface { 44 DECLARE_SINGLE_INSTANCE(KernelInterface); 45 46 public: 47 bool EchoToPath(const char* path, const char* content); 48 49 // file operations 50 bool IsFileExists(const std::string& path); 51 bool CreateFile(const std::string& path); // default mode 644(-rw-r--r--) 52 bool CreateFile(const std::string& path, const mode_t& mode); 53 bool RemoveFile(const std::string& path); 54 bool WriteToFile(const std::string& path, const std::string& content, bool truncated = true); 55 bool ReadFromFile(const std::string& path, std::string& content); 56 bool ReadLinesFromFile(const std::string& path, std::vector<std::string>& lines); 57 // dir operations 58 bool IsDirExists(const std::string& path); 59 bool IsExists(const std::string& path); // file or dir 60 bool IsEmptyDir(const std::string& path); 61 bool CreateDir(const std::string& path); // create dir recursively 755 62 bool RemoveDirRecursively(const std::string& path); 63 std::string AddDelimiter(const std::string& path); // IncludeTrailingPathDelimiter 64 std::string RmDelimiter(const std::string& path); // ExcludeTrailingPathDelimiter 65 std::string JoinPath(const std::string& prefixPath, const std::string& subPath); 66 std::string JoinPath(const std::string& prefixPath, const std::string& midPath, const std::string& subPath); 67 68 bool GetPidProcInfo(struct ProcInfo &procInfo); 69 bool GetProcNameByPid(int pid, std::string &name); 70 void ReadZswapdPressureShow(std::map<std::string, std::string>& result); 71 int GetCurrentBuffer(); 72 int KillOneProcessByPid(int pid); 73 bool GetAllProcPids(std::vector<unsigned int>& pids); 74 bool GetUidByPid(unsigned int pid, unsigned int& uid); 75 bool ReadSwapOutKBSinceKernelBoot(const std::string &path, const std::string &tagStr, unsigned long long &ret); 76 77 static const std::string ROOT_PROC_PATH; 78 static const std::string MEMCG_BASE_PATH; 79 static const std::string ZWAPD_PRESSURE_SHOW_PATH; 80 static const std::string ZWAPD_PRESSURE_SHOW_BUFFER_SIZE; 81 static constexpr mode_t FILE_MODE_666 = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH; // -rw-rw-rw-- 82 static constexpr mode_t FILE_MODE_664 = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH; // -rw-rw-r-- 83 static constexpr mode_t FILE_MODE_644 = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH; // -rw-r--r-- 84 static constexpr mode_t FILE_MODE_660 = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP; 85 static constexpr mode_t FILE_MODE_755 = S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH; 86 static constexpr mode_t FILE_MODE_775 = S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH; 87 static constexpr mode_t FILE_MODE_770 = S_IRWXU | S_IRWXG; 88 static constexpr mode_t FILE_MODE_700 = S_IRWXU; 89 }; 90 } // namespace Memory 91 } // namespace OHOS 92 #endif // OHOS_MEMORY_MEMMGR_COMMON_INCLUDE_KERNEL_INTERFACE_H 93