1 /*
2 * Copyright (c) 2022-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 <cstdint>
17 #include "res_sched_service.h"
18 #include <file_ex.h>
19 #include <string_ex.h>
20 #include "accesstoken_kit.h"
21 #include "ipc_skeleton.h"
22 #include "plugin_mgr.h"
23 #include "res_sched_errors.h"
24 #include "res_sched_log.h"
25 #include "res_sched_mgr.h"
26 #include "tokenid_kit.h"
27
28 namespace OHOS {
29 namespace ResourceSchedule {
30 using namespace OHOS::Security;
31 namespace {
32 constexpr int32_t DUMP_OPTION = 0;
33 constexpr int32_t DUMP_PARAM_INDEX = 1;
34 }
35
ReportData(uint32_t resType,int64_t value,const nlohmann::json & payload)36 void ResSchedService::ReportData(uint32_t resType, int64_t value, const nlohmann::json& payload)
37 {
38 RESSCHED_LOGI("ResSchedService::ReportData from ipc receive data resType = %{public}u, value = %{public}lld.",
39 resType, (long long)value);
40 const nlohmann::json* payloadP = &payload;
41 int32_t callingUid = IPCSkeleton::GetCallingUid();
42 nlohmann::json* payloadM = const_cast<nlohmann::json*>(payloadP);
43 (*payloadM)["callingUid"] = std::to_string(callingUid);
44 ResSchedMgr::GetInstance().ReportData(resType, value, *payloadM);
45 }
46
KillProcess(const nlohmann::json & payload)47 int32_t ResSchedService::KillProcess(const nlohmann::json& payload)
48 {
49 uint32_t accessToken = IPCSkeleton::GetCallingTokenID();
50 AccessToken::NativeTokenInfo nativeTokenInfo;
51 int32_t result = AccessToken::AccessTokenKit::GetNativeTokenInfo(accessToken, nativeTokenInfo);
52 if (result == ERR_OK) {
53 return ResSchedMgr::GetInstance().KillProcessByClient(payload, nativeTokenInfo.processName);
54 } else {
55 RESSCHED_LOGE("Kill process get token info fail.");
56 return RES_SCHED_ACCESS_TOKEN_FAIL;
57 }
58 }
59
Dump(int32_t fd,const std::vector<std::u16string> & args)60 int32_t ResSchedService::Dump(int32_t fd, const std::vector<std::u16string>& args)
61 {
62 RESSCHED_LOGI("%{public}s Dump service.", __func__);
63 std::vector<std::string> argsInStr;
64 std::transform(args.begin(), args.end(), std::back_inserter(argsInStr),
65 [](const std::u16string &arg) {
66 std::string ret = Str16ToStr8(arg);
67 RESSCHED_LOGI("%{public}s arg: %{public}s.", __func__, ret.c_str());
68 return ret;
69 });
70 std::string result;
71 if (argsInStr.size() == 0) {
72 // hidumper -s said '-h'
73 DumpUsage(result);
74 } else if (argsInStr.size() == DUMP_OPTION + 1) {
75 // hidumper -s said '-h' or hidumper -s said '-a'
76 if (argsInStr[DUMP_OPTION] == "-h") {
77 DumpUsage(result);
78 } else if (argsInStr[DUMP_OPTION] == "-a") {
79 DumpAllInfo(result);
80 } else if (argsInStr[DUMP_OPTION] == "-p") {
81 PluginMgr::GetInstance().DumpAllPlugin(result);
82 } else {
83 result.append("Error params.");
84 }
85 } else if (argsInStr.size() >= DUMP_PARAM_INDEX + 1) {
86 if (argsInStr[DUMP_OPTION] == "-p") {
87 std::vector<std::string> argsInStrToPlugin;
88 argsInStrToPlugin.assign(argsInStr.begin() + DUMP_PARAM_INDEX + 1, argsInStr.end());
89 PluginMgr::GetInstance().DumpOnePlugin(result, argsInStr[DUMP_PARAM_INDEX], argsInStrToPlugin);
90 }
91 }
92
93 if (!SaveStringToFd(fd, result)) {
94 RESSCHED_LOGE("%{public}s save to fd failed.", __func__);
95 }
96 return ERR_OK;
97 }
DumpUsage(std::string & result)98 void ResSchedService::DumpUsage(std::string &result)
99 {
100 result.append("usage: resource schedule service dump [<options>]\n")
101 .append(" -h: show the help.\n")
102 .append(" -a: show all info.\n")
103 .append(" -p: show the all plugin info.\n")
104 .append(" -p (plugin name): show one plugin info.\n");
105 PluginMgr::GetInstance().DumpHelpFromPlugin(result);
106 }
107
DumpAllInfo(std::string & result)108 void ResSchedService::DumpAllInfo(std::string &result)
109 {
110 result.append("================Resource Schedule Service Infos================\n");
111 PluginMgr::GetInstance().DumpAllPlugin(result);
112 }
113 } // namespace ResourceSchedule
114 } // namespace OHOS
115
116