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