1 /*
2 * Copyright (c) 2024 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 "res_sched_exe_service.h"
17
18 #include <file_ex.h>
19 #include <parameters.h>
20 #include <string_ex.h>
21
22 #include "accesstoken_kit.h"
23 #include "ipc_skeleton.h"
24
25 #include "plugin_mgr.h"
26 #include "res_sched_exe_common_utils.h"
27 #include "res_sched_exe_constants.h"
28 #include "res_sched_exe_log.h"
29 #include "res_sched_exe_mgr.h"
30
31 namespace OHOS {
32 namespace ResourceSchedule {
33 namespace {
34 constexpr int32_t DUMP_OPTION = 0;
35 constexpr int32_t DUMP_PARAM_INDEX = 1;
36 const int32_t ENG_MODE = OHOS::system::GetIntParameter("const.debuggable", 0);
37 const std::string DUMP_PERMISSION = "ohos.permission.DUMP";
38 }
39
SendRequestSync(uint32_t resType,int64_t value,const nlohmann::json & context,nlohmann::json & reply)40 int32_t ResSchedExeService::SendRequestSync(uint32_t resType, int64_t value,
41 const nlohmann::json& context, nlohmann::json& reply)
42 {
43 return ResSchedExeMgr::GetInstance().SendRequestSync(resType, value, context, reply);
44 }
45
SendRequestAsync(uint32_t resType,int64_t value,const nlohmann::json & context)46 void ResSchedExeService::SendRequestAsync(uint32_t resType, int64_t value, const nlohmann::json& context)
47 {
48 ResSchedExeMgr::GetInstance().SendRequestAsync(resType, value, context);
49 }
50
KillProcess(pid_t pid)51 int32_t ResSchedExeService::KillProcess(pid_t pid)
52 {
53 return ResSchedExeMgr::GetInstance().KillProcess(pid);
54 }
55
AllowDump()56 bool ResSchedExeService::AllowDump()
57 {
58 if (ENG_MODE == 0) {
59 RSSEXE_LOGE("Not eng mode");
60 return false;
61 }
62 Security::AccessToken::AccessTokenID tokenId = IPCSkeleton::GetCallingTokenID();
63 int32_t ret = Security::AccessToken::AccessTokenKit::VerifyAccessToken(tokenId, DUMP_PERMISSION);
64 if (ret != Security::AccessToken::PermissionState::PERMISSION_GRANTED) {
65 RSSEXE_LOGE("CheckPermission failed");
66 return false;
67 }
68 return true;
69 }
70
Dump(int32_t fd,const std::vector<std::u16string> & args)71 int32_t ResSchedExeService::Dump(int32_t fd, const std::vector<std::u16string>& args)
72 {
73 if (!AllowDump()) {
74 return ResErrCode::RSSEXE_PERMISSION_DENIED;
75 }
76 RSSEXE_LOGI("Dump service.");
77 std::vector<std::string> argsInStr;
78 std::transform(args.begin(), args.end(), std::back_inserter(argsInStr),
79 [](const std::u16string &arg) {
80 std::string ret = Str16ToStr8(arg);
81 RSSEXE_LOGI("arg: %{public}s.", ret.c_str());
82 return ret;
83 });
84 std::string result;
85 if (argsInStr.size() == 0) {
86 // hidumper -s said '-h'
87 DumpUsage(result);
88 } else if (argsInStr.size() == DUMP_OPTION + 1) {
89 // hidumper -s said '-h' or hidumper -s said '-a'
90 if (argsInStr[DUMP_OPTION] == "-h") {
91 DumpUsage(result);
92 } else if (argsInStr[DUMP_OPTION] == "-a") {
93 DumpAllInfo(result);
94 } else if (argsInStr[DUMP_OPTION] == "-p") {
95 PluginMgr::GetInstance().DumpAllPlugin(result);
96 } else {
97 result.append("Error params.");
98 }
99 } else if (argsInStr.size() >= DUMP_PARAM_INDEX + 1) {
100 if (argsInStr[DUMP_OPTION] == "-p") {
101 std::vector<std::string> argsInStrToPlugin;
102 argsInStrToPlugin.assign(argsInStr.begin() + DUMP_PARAM_INDEX + 1, argsInStr.end());
103 PluginMgr::GetInstance().DumpOnePlugin(result, argsInStr[DUMP_PARAM_INDEX], argsInStrToPlugin);
104 }
105 }
106
107 if (!SaveStringToFd(fd, result)) {
108 RSSEXE_LOGE("save to fd failed.");
109 }
110 return ResErrCode::RSSEXE_NO_ERR;
111 }
112
DumpUsage(std::string & result)113 void ResSchedExeService::DumpUsage(std::string &result)
114 {
115 result.append("usage: resource schedule executor dump [<options>]\n")
116 .append(" -h: show the help.\n")
117 .append(" -a: show all info.\n")
118 .append(" -p: show the all plugin info.\n")
119 .append(" -p (plugin name): show one plugin info.\n");
120 PluginMgr::GetInstance().DumpHelpFromPlugin(result);
121 }
122
DumpAllInfo(std::string & result)123 void ResSchedExeService::DumpAllInfo(std::string &result)
124 {
125 result.append("================Resource Schedule Executor Infos================\n");
126 PluginMgr::GetInstance().DumpAllPlugin(result);
127 }
128 } // namespace ResourceSchedule
129 } // namespace OHOS
130