1 /* 2 * Copyright (c) 2021 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 #ifndef UTILS_H 16 #define UTILS_H 17 18 #include <sys/stat.h> 19 20 #include <string> 21 22 #include "errors.h" 23 #include "file_util.h" 24 25 namespace OHOS { 26 namespace HiviewDFX { 27 static constexpr mode_t DEFAULT_FILE_MODE = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH; // -rw-rw-r-- 28 static constexpr mode_t FILE_PERM_755 = S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH; 29 static constexpr mode_t FILE_PERM_775 = S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH; 30 static constexpr mode_t FILE_PERM_770 = S_IRWXU | S_IRWXG; 31 static constexpr uint32_t DEFAULT_DURATION_SECONDS = 60; 32 static constexpr uint32_t BUF_SIZE_64 = 64; 33 static constexpr uint32_t BUF_SIZE_128 = 128; 34 static constexpr uint32_t BUF_SIZE_256 = 256; 35 static constexpr uint32_t BUF_SIZE_512 = 512; 36 static constexpr uint32_t BUF_SIZE_4096 = 4096; 37 static constexpr uint64_t NS_PER_SECOND = 1000000000; 38 static constexpr uint32_t MAX_LINE_LEN = 1024; 39 namespace CommonUtils { 40 std::string GetProcNameByPid(int32_t pid); 41 pid_t GetPidByName(const std::string& processName); 42 int32_t ExecCommand(const std::string &cmd, const std::vector<std::string> &args); 43 bool IsSpecificCmdExist(const std::string& fullPath); 44 bool IsTheProcessExist(pid_t pid); 45 bool WriteCommandResultToFile(int fd, const std::string& cmd); 46 }; 47 } // namespace HiviewDFX 48 } // namespace OHOS 49 #endif 50