1 /*
2 * Copyright (c) 2023 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 #include "time_file_utils.h"
17
18 #include <fcntl.h>
19 #include <sys/stat.h>
20
21 #include "accesstoken_kit.h"
22 #include "ipc_skeleton.h"
23 #include "securec.h"
24 #include "time_hilog.h"
25 #include "parameters.h"
26
27 constexpr int CMDLINE_PATH_LEN = 32;
28 constexpr int CMDLINE_LEN = 128;
29
30 namespace OHOS {
31 namespace MiscServices {
32 using AccessTokenKit = OHOS::Security::AccessToken::AccessTokenKit;
33 using HapTokenInfo = OHOS::Security::AccessToken::HapTokenInfo;
34 using TypeATokenTypeEnum = OHOS::Security::AccessToken::TypeATokenTypeEnum;
35
GetBundleNameByTokenID(uint32_t tokenID)36 std::string TimeFileUtils::GetBundleNameByTokenID(uint32_t tokenID)
37 {
38 auto tokenType = AccessTokenKit::GetTokenTypeFlag(tokenID);
39 if (tokenType != TypeATokenTypeEnum::TOKEN_HAP) {
40 return "";
41 }
42 HapTokenInfo hapTokenInfo;
43 int result = AccessTokenKit::GetHapTokenInfo(tokenID, hapTokenInfo);
44 if (result != Security::AccessToken::AccessTokenKitRet::RET_SUCCESS) {
45 TIME_HILOGE(TIME_MODULE_SERVICE, "failed to get hap token info, result = %{public}d", result);
46 return "";
47 }
48 return hapTokenInfo.bundleName;
49 }
50
GetNameByPid(uint32_t pid)51 std::string TimeFileUtils::GetNameByPid(uint32_t pid)
52 {
53 char path[CMDLINE_PATH_LEN] = { 0 };
54 if (snprintf_s(path, CMDLINE_PATH_LEN, CMDLINE_PATH_LEN - 1, "/proc/%u/cmdline", pid) <= 0) {
55 return "";
56 }
57 char cmdline[CMDLINE_LEN] = { 0 };
58 int i = 0;
59 FILE *fp = fopen(path, "r");
60 if (fp == nullptr) {
61 return "";
62 }
63 while (i < (CMDLINE_LEN - 1)) {
64 char c = static_cast<char>(fgetc(fp));
65 // 0. don't need args of cmdline
66 // 1. ignore unvisible character
67 if (!isgraph(c)) {
68 break;
69 }
70 cmdline[i] = c;
71 i++;
72 }
73 (void)fclose(fp);
74 return cmdline;
75 }
76
GetParameterList(std::string parameterName)77 std::vector<std::string> TimeFileUtils::GetParameterList(std::string parameterName)
78 {
79 std::vector<std::string> bundleList;
80 std::string bundleStr = system::GetParameter(parameterName, "");
81 size_t start = 0;
82 do {
83 size_t end = bundleStr.find(',', start);
84 if (end < start) {
85 break;
86 }
87 std::string temp = bundleStr.substr(start, end - start);
88 if (temp.empty()) {
89 ++start;
90 continue;
91 }
92 bundleList.emplace_back(temp);
93 if (end == std::string::npos) {
94 break;
95 }
96 start = end + 1;
97 } while (start < bundleStr.size());
98 return bundleList;
99 }
100 } // namespace MiscServices
101 } // namespace OHOS