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