• 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 <cctype>
17 #include <string>
18 #include <cerrno>
19 #include "time_file_utils.h"
20 
21 #include "accesstoken_kit.h"
22 #include "securec.h"
23 #include "time_hilog.h"
24 #include "parameters.h"
25 
26 constexpr int CMDLINE_PATH_LEN = 32;
27 constexpr int CMDLINE_LEN = 128;
28 
29 namespace OHOS {
30 namespace MiscServices {
31 using AccessTokenKit = OHOS::Security::AccessToken::AccessTokenKit;
32 using HapTokenInfo = OHOS::Security::AccessToken::HapTokenInfo;
33 using TypeATokenTypeEnum = OHOS::Security::AccessToken::TypeATokenTypeEnum;
34 
GetBundleNameByTokenID(uint32_t tokenID)35 std::string TimeFileUtils::GetBundleNameByTokenID(uint32_t tokenID)
36 {
37     auto tokenType = AccessTokenKit::GetTokenTypeFlag(tokenID);
38     if (tokenType != TypeATokenTypeEnum::TOKEN_HAP) {
39         return "";
40     }
41     HapTokenInfo hapTokenInfo;
42     int result = AccessTokenKit::GetHapTokenInfo(tokenID, hapTokenInfo);
43     if (result != Security::AccessToken::AccessTokenKitRet::RET_SUCCESS) {
44         TIME_HILOGE(TIME_MODULE_SERVICE, "failed to get hap token info, result = %{public}d", result);
45         return "";
46     }
47     return hapTokenInfo.bundleName;
48 }
49 
GetNameByPid(uint32_t pid)50 std::string TimeFileUtils::GetNameByPid(uint32_t pid)
51 {
52     char path[CMDLINE_PATH_LEN] = { 0 };
53     if (snprintf_s(path, CMDLINE_PATH_LEN, CMDLINE_PATH_LEN - 1, "/proc/%u/cmdline", pid) <= 0) {
54         return "";
55     }
56     char cmdline[CMDLINE_LEN] = { 0 };
57     int i = 0;
58     FILE *fp = fopen(path, "r");
59     if (fp == nullptr) {
60         return "";
61     }
62     while (i < (CMDLINE_LEN - 1)) {
63         char c = static_cast<char>(fgetc(fp));
64         // 0. don't need args of cmdline
65         // 1. ignore unvisible character
66         if (!isgraph(c)) {
67             break;
68         }
69         cmdline[i] = c;
70         i++;
71     }
72     (void)fclose(fp);
73     return cmdline;
74 }
75 
GetParameterList(const std::string & parameterName)76 std::vector<std::string> TimeFileUtils::GetParameterList(const std::string& parameterName)
77 {
78     std::vector<std::string> bundleList;
79     std::string bundleStr = system::GetParameter(parameterName, "");
80     size_t start = 0;
81     do {
82         size_t end = bundleStr.find(',', start);
83         if (end < start) {
84             break;
85         }
86         std::string temp = bundleStr.substr(start, end - start);
87         if (temp.empty()) {
88             ++start;
89             continue;
90         }
91         bundleList.emplace_back(temp);
92         if (end == std::string::npos) {
93             break;
94         }
95         start = end + 1;
96     } while (start < bundleStr.size());
97     return bundleList;
98 }
99 
GetIntParameter(const std::string & parameterName,int64_t def)100 int64_t TimeFileUtils::GetIntParameter(const std::string& parameterName, int64_t def)
101 {
102     std::string value = system::GetParameter(parameterName, "");
103     if (value.empty()) {
104         return def;
105     }
106     for (auto ch : value) {
107         if (!std::isdigit(ch)) {
108             TIME_HILOGE(TIME_MODULE_SERVICE, "Wrong para in %{public}s", parameterName.c_str());
109             return def;
110         }
111     }
112     errno = 0;
113     char *end = nullptr;
114     int64_t num = std::strtoll(value.c_str(), &end, 10);
115     if (end == nullptr || *end != '\0') {
116         TIME_HILOGE(TIME_MODULE_SERVICE, "Wrong para in %{public}s", parameterName.c_str());
117         return def;
118     }
119     if (errno == ERANGE) {
120         TIME_HILOGE(TIME_MODULE_SERVICE, "Overflow in %{public}s", parameterName.c_str());
121         return def;
122     }
123     return num;
124 }
125 } // namespace MiscServices
126 } // namespace OHOS