1 /*
2 * Copyright (c) 2022 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 "distributed_ability_manager_dumper.h"
17
18 #include "accesstoken_kit.h"
19 #include "distributed_ability_manager_service.h"
20 #include "dtbschedmgr_log.h"
21 #include "ipc_skeleton.h"
22
23 namespace OHOS {
24 namespace DistributedSchedule {
25 namespace {
26 const std::string TAG = "DistributedAbilityManagerDumper";
27 const char* HIDUMPER_PROCESS_NAME = "hidumper_service";
28 constexpr size_t MIN_ARGS_SIZE = 1;
29 const std::string ARGS_HELP = "-h";
30 const std::string ARGS_CONNECT_REMOTE_ABILITY = "-connect";
31 }
32
Dump(const sptr<DmsDumper> & dmsDumper,const std::vector<std::string> & args,std::string & result)33 bool DistributedAbilityManagerDumper::Dump(const sptr<DmsDumper>& dmsDumper, const std::vector<std::string>& args,
34 std::string& result)
35 {
36 result.clear();
37 if (!CanDump()) {
38 result.append("Dump failed, not allowed");
39 return false;
40 }
41
42 if (args.size() < MIN_ARGS_SIZE) {
43 return ProcessDistributedSchedDump(dmsDumper, args, result);
44 }
45
46 if (args.size() == MIN_ARGS_SIZE) {
47 // -h
48 if (args[0] == ARGS_HELP) {
49 ShowHelp(result);
50 return true;
51 }
52 // -connect
53 if (args[0] == ARGS_CONNECT_REMOTE_ABILITY) {
54 return ProcessDistributedSchedDump(dmsDumper, args, result);
55 }
56 }
57 IllegalInput(result);
58 return false;
59 }
60
ProcessDistributedSchedDump(const sptr<DmsDumper> & dmsDumper,const std::vector<std::string> & args,std::string & result)61 bool DistributedAbilityManagerDumper::ProcessDistributedSchedDump(const sptr<DmsDumper>& dmsDumper,
62 const std::vector<std::string>& args, std::string& result)
63 {
64 if (dmsDumper == nullptr) {
65 HILOGE("dumper is nullptr");
66 return false;
67 }
68 return dmsDumper->ProcessDistributedSchedDump(args, result);
69 }
70
ShowHelp(std::string & result)71 void DistributedAbilityManagerDumper::ShowHelp(std::string& result)
72 {
73 result.append("DistributedSched Dump options:\n")
74 .append(" [-h] [cmd]...\n")
75 .append("cmd maybe one of:\n")
76 .append(" -connect: show all connected remote abilities.\n");
77 }
78
IllegalInput(std::string & result)79 void DistributedAbilityManagerDumper::IllegalInput(std::string& result)
80 {
81 result.append("The arguments are illegal and you can enter '-h' for help.\n");
82 }
83
CanDump()84 bool DistributedAbilityManagerDumper::CanDump()
85 {
86 uint32_t accessToken = IPCSkeleton::GetCallingTokenID();
87 Security::AccessToken::NativeTokenInfo nativeTokenInfo;
88 int32_t result = Security::AccessToken::AccessTokenKit::GetNativeTokenInfo(accessToken, nativeTokenInfo);
89 if (result == ERR_OK && nativeTokenInfo.processName == HIDUMPER_PROCESS_NAME) {
90 return true;
91 }
92 return false;
93 }
94 } // namespace DistributedSchedule
95 } // namespace OHOS