1 /*
2 * Copyright (c) 2021 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_stub.h"
16 #include <message_parcel.h>
17 #include <unistd.h>
18 #include "dump_errors.h"
19 #include "hidumper_service_ipc_interface_code.h"
20 #include "hilog_wrapper.h"
21 namespace OHOS {
22 namespace HiviewDFX {
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)23 int DumpBrokerStub::OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
24 {
25 std::u16string descripter = DumpBrokerStub::GetDescriptor();
26 std::u16string remoteDescripter = data.ReadInterfaceToken();
27 if (descripter != remoteDescripter) {
28 return ERROR_GET_DUMPER_SERVICE;
29 }
30 int ret = ERR_OK;
31 switch (code) {
32 case static_cast<int>(HidumperServiceInterfaceCode::DUMP_REQUEST_FILEFD): {
33 DUMPER_HILOGD(MODULE_ZIDL, "debug|RequestFileFdStub");
34 ret = RequestFileFdStub(data, reply);
35 break;
36 }
37 case static_cast<int>(HidumperServiceInterfaceCode::SCAN_PID_OVER_LIMIT): {
38 DUMPER_HILOGD(MODULE_ZIDL, "debug|ScanPidOverLimitStub");
39 ret = ScanPidOverLimitStub(data, reply);
40 break;
41 }
42 case static_cast<int>(HidumperServiceInterfaceCode::COUNT_FD_NUMS): {
43 DUMPER_HILOGD(MODULE_ZIDL, "debug|CountFdNumsStub");
44 ret = CountFdNumsStub(data, reply);
45 break;
46 }
47 default: {
48 ret = IPCObjectStub::OnRemoteRequest(code, data, reply, option);
49 break;
50 }
51 }
52 return ret;
53 }
54
RequestFileFdStub(MessageParcel & data,MessageParcel & reply)55 int32_t DumpBrokerStub::RequestFileFdStub(MessageParcel& data, MessageParcel& reply)
56 {
57 int32_t ret = ERR_OK;
58 std::vector<std::u16string> args;
59 if (!data.ReadString16Vector(&args)) {
60 return ERROR_READ_PARCEL;
61 }
62 int outfd = data.ReadFileDescriptor();
63 if (outfd < 0) {
64 return ERROR_READ_PARCEL;
65 }
66 int32_t res = Request(args, outfd);
67 close(outfd);
68 if (!reply.WriteInt32(res)) {
69 return ERROR_WRITE_PARCEL;
70 }
71 return ret;
72 }
73
ScanPidOverLimitStub(MessageParcel & data,MessageParcel & reply)74 int32_t DumpBrokerStub::ScanPidOverLimitStub(MessageParcel& data, MessageParcel& reply)
75 {
76 int32_t ret = ERR_OK;
77 std::vector<int32_t> pidList;
78 std::string requestType = data.ReadString();
79 int32_t limitSize = data.ReadInt32();
80 ret = ScanPidOverLimit(requestType, limitSize, pidList);
81 if (!reply.WriteInt32Vector(pidList)) {
82 return ERROR_WRITE_PARCEL;
83 }
84 if (!reply.WriteInt32(ret)) {
85 return ERROR_WRITE_PARCEL;
86 }
87 return ret;
88 }
89
CountFdNumsStub(MessageParcel & data,MessageParcel & reply)90 int32_t DumpBrokerStub::CountFdNumsStub(MessageParcel& data, MessageParcel& reply)
91 {
92 int32_t ret = ERR_OK;
93 uint32_t fdNums = 0;
94 std::string detailFdInfo;
95 std::string topLeakedType;
96
97 int32_t pid = data.ReadInt32();
98 ret = CountFdNums(pid, fdNums, detailFdInfo, topLeakedType);
99 if (!reply.WriteInt32(fdNums)) {
100 return ERROR_WRITE_PARCEL;
101 }
102 if (!reply.WriteString(detailFdInfo)) {
103 return ERROR_WRITE_PARCEL;
104 }
105 if (!reply.WriteString(topLeakedType)) {
106 return ERROR_WRITE_PARCEL;
107 }
108 if (!reply.WriteInt32(ret)) {
109 return ERROR_WRITE_PARCEL;
110 }
111 return ret;
112 }
113 } // namespace HiviewDFX
114 } // namespace OHOS
115