1 /*
2 * Copyright (c) 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 #include "dump_broker_cpu_stub.h"
16 #include <message_parcel.h>
17 #include "dump_cpu_data.h"
18 #include "dump_errors.h"
19 #include "hidumper_cpu_service_ipc_interface_code.h"
20 #include "hilog_wrapper.h"
21 #include <ipc_skeleton.h>
22 namespace OHOS {
23 namespace HiviewDFX {
24 const int APP_UID = 10000;
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)25 int DumpBrokerCpuStub::OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
26 {
27 std::u16string descripter = DumpBrokerCpuStub::GetDescriptor();
28 std::u16string remoteDescripter = data.ReadInterfaceToken();
29 if (descripter != remoteDescripter) {
30 return ERROR_GET_DUMPER_SERVICE;
31 }
32 int ret = ERR_OK;
33 switch (code) {
34 case static_cast<int>(HidumperCpuServiceInterfaceCode::DUMP_REQUEST_CPUINFO): {
35 std::shared_ptr<DumpCpuData> dumpCpuData(data.ReadParcelable<DumpCpuData>());
36 if (dumpCpuData == nullptr) {
37 DUMPER_HILOGE(MODULE_CPU_ZIDL, "error|DumpBrokerCpuStub error");
38 return ERROR_READ_PARCEL;
39 }
40 int32_t res = Request(*dumpCpuData);
41 DumpCpuData dump = *dumpCpuData;
42 if (!reply.WriteParcelable(&dump)) {
43 return ERROR_WRITE_PARCEL;
44 }
45 if (!reply.WriteInt32(res)) {
46 return ERROR_WRITE_PARCEL;
47 }
48 break;
49 }
50 case static_cast<int>(HidumperCpuServiceInterfaceCode::DUMP_USAGE_ONLY): {
51 DUMPER_HILOGD(MODULE_CPU_ZIDL, "debug|DumpCpuUsageOnly");
52 ret = DumpCpuUsageOnly(data, reply);
53 break;
54 }
55 default: {
56 ret = IPCObjectStub::OnRemoteRequest(code, data, reply, option);
57 break;
58 }
59 }
60 return ret;
61 }
62
DumpCpuUsageOnly(MessageParcel & data,MessageParcel & reply)63 int DumpBrokerCpuStub::DumpCpuUsageOnly(MessageParcel& data, MessageParcel& reply)
64 {
65 int32_t pid = data.ReadInt32();
66 int32_t calllingUid = IPCSkeleton::GetCallingUid();
67 int32_t calllingPid = IPCSkeleton::GetCallingPid();
68 if (calllingUid >= APP_UID && pid != calllingPid) {
69 return ERROR_GET_DUMPER_SERVICE;
70 }
71 if (pid < 0) {
72 return ERROR_READ_PARCEL;
73 }
74 double usage = 0.00;
75 int32_t res = GetCpuUsageByPid(pid, usage);
76 if (!reply.WriteDouble(usage)) {
77 return ERROR_WRITE_PARCEL;
78 }
79 if (res != 0) {
80 return ERROR_WRITE_PARCEL;
81 }
82 return ERR_OK;
83 }
84 } // namespace HiviewDFX
85 } // namespace OHOS
86