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