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