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     if (!reply.WriteInt32(res)) {
68         return ERROR_WRITE_PARCEL;
69     }
70     return ret;
71 }
72 
ScanPidOverLimitStub(MessageParcel & data,MessageParcel & reply)73 int32_t DumpBrokerStub::ScanPidOverLimitStub(MessageParcel& data, MessageParcel& reply)
74 {
75     int32_t ret = ERR_OK;
76     std::vector<int32_t> pidList;
77     std::string requestType = data.ReadString();
78     int32_t limitSize = data.ReadInt32();
79     ret = ScanPidOverLimit(requestType, limitSize, pidList);
80     if (!reply.WriteInt32Vector(pidList)) {
81         return ERROR_WRITE_PARCEL;
82     }
83     if (!reply.WriteInt32(ret)) {
84         return ERROR_WRITE_PARCEL;
85     }
86     return ret;
87 }
88 
CountFdNumsStub(MessageParcel & data,MessageParcel & reply)89 int32_t DumpBrokerStub::CountFdNumsStub(MessageParcel& data, MessageParcel& reply)
90 {
91     int32_t ret = ERR_OK;
92     uint32_t fdNums = 0;
93     std::string detailFdInfo;
94     std::string topLeakedType;
95 
96     int32_t pid = data.ReadInt32();
97     ret = CountFdNums(pid, fdNums, detailFdInfo, topLeakedType);
98     if (!reply.WriteInt32(fdNums)) {
99         return ERROR_WRITE_PARCEL;
100     }
101     if (!reply.WriteString(detailFdInfo)) {
102         return ERROR_WRITE_PARCEL;
103     }
104     if (!reply.WriteString(topLeakedType)) {
105         return ERROR_WRITE_PARCEL;
106     }
107     if (!reply.WriteInt32(ret)) {
108         return ERROR_WRITE_PARCEL;
109     }
110     return ret;
111 }
112 } // namespace HiviewDFX
113 } // namespace OHOS
114