xref: /aosp_15_r20/frameworks/native/libs/binder/IProcessInfoService.cpp (revision 38e8c45f13ce32b0dcecb25141ffecaf386fa17f)
1*38e8c45fSAndroid Build Coastguard Worker /*
2*38e8c45fSAndroid Build Coastguard Worker  * Copyright 2015 The Android Open Source Project
3*38e8c45fSAndroid Build Coastguard Worker  *
4*38e8c45fSAndroid Build Coastguard Worker  * Licensed under the Apache License, Version 2.0 (the "License");
5*38e8c45fSAndroid Build Coastguard Worker  * you may not use this file except in compliance with the License.
6*38e8c45fSAndroid Build Coastguard Worker  * You may obtain a copy of the License at
7*38e8c45fSAndroid Build Coastguard Worker  *
8*38e8c45fSAndroid Build Coastguard Worker  *      http://www.apache.org/licenses/LICENSE-2.0
9*38e8c45fSAndroid Build Coastguard Worker  *
10*38e8c45fSAndroid Build Coastguard Worker  * Unless required by applicable law or agreed to in writing, software
11*38e8c45fSAndroid Build Coastguard Worker  * distributed under the License is distributed on an "AS IS" BASIS,
12*38e8c45fSAndroid Build Coastguard Worker  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*38e8c45fSAndroid Build Coastguard Worker  * See the License for the specific language governing permissions and
14*38e8c45fSAndroid Build Coastguard Worker  * limitations under the License.
15*38e8c45fSAndroid Build Coastguard Worker  */
16*38e8c45fSAndroid Build Coastguard Worker 
17*38e8c45fSAndroid Build Coastguard Worker #include <processinfo/IProcessInfoService.h>
18*38e8c45fSAndroid Build Coastguard Worker #include <binder/Parcel.h>
19*38e8c45fSAndroid Build Coastguard Worker #include <utils/Errors.h>
20*38e8c45fSAndroid Build Coastguard Worker #include <sys/types.h>
21*38e8c45fSAndroid Build Coastguard Worker 
22*38e8c45fSAndroid Build Coastguard Worker namespace android {
23*38e8c45fSAndroid Build Coastguard Worker 
24*38e8c45fSAndroid Build Coastguard Worker // ----------------------------------------------------------------------
25*38e8c45fSAndroid Build Coastguard Worker 
26*38e8c45fSAndroid Build Coastguard Worker class BpProcessInfoService : public BpInterface<IProcessInfoService> {
27*38e8c45fSAndroid Build Coastguard Worker public:
BpProcessInfoService(const sp<IBinder> & impl)28*38e8c45fSAndroid Build Coastguard Worker     explicit BpProcessInfoService(const sp<IBinder>& impl)
29*38e8c45fSAndroid Build Coastguard Worker         : BpInterface<IProcessInfoService>(impl) {}
30*38e8c45fSAndroid Build Coastguard Worker 
getProcessStatesFromPids(size_t length,int32_t * pids,int32_t * states)31*38e8c45fSAndroid Build Coastguard Worker     virtual status_t getProcessStatesFromPids(size_t length, /*in*/ int32_t* pids,
32*38e8c45fSAndroid Build Coastguard Worker             /*out*/ int32_t* states)
33*38e8c45fSAndroid Build Coastguard Worker     {
34*38e8c45fSAndroid Build Coastguard Worker         Parcel data, reply;
35*38e8c45fSAndroid Build Coastguard Worker         data.writeInterfaceToken(IProcessInfoService::getInterfaceDescriptor());
36*38e8c45fSAndroid Build Coastguard Worker         data.writeInt32Array(length, pids);
37*38e8c45fSAndroid Build Coastguard Worker         data.writeInt32(length); // write length of output array, used by java AIDL stubs
38*38e8c45fSAndroid Build Coastguard Worker         status_t err = remote()->transact(GET_PROCESS_STATES_FROM_PIDS, data, &reply);
39*38e8c45fSAndroid Build Coastguard Worker         if (err != NO_ERROR || ((err = reply.readExceptionCode()) != NO_ERROR)) {
40*38e8c45fSAndroid Build Coastguard Worker             return err;
41*38e8c45fSAndroid Build Coastguard Worker         }
42*38e8c45fSAndroid Build Coastguard Worker         int32_t replyLen = reply.readInt32();
43*38e8c45fSAndroid Build Coastguard Worker         if (static_cast<size_t>(replyLen) != length) {
44*38e8c45fSAndroid Build Coastguard Worker             return NOT_ENOUGH_DATA;
45*38e8c45fSAndroid Build Coastguard Worker         }
46*38e8c45fSAndroid Build Coastguard Worker         if (replyLen > 0 && (err = reply.read(states, length * sizeof(*states))) != NO_ERROR) {
47*38e8c45fSAndroid Build Coastguard Worker             return err;
48*38e8c45fSAndroid Build Coastguard Worker         }
49*38e8c45fSAndroid Build Coastguard Worker         return reply.readInt32();
50*38e8c45fSAndroid Build Coastguard Worker     }
51*38e8c45fSAndroid Build Coastguard Worker 
getProcessStatesAndOomScoresFromPids(size_t length,int32_t * pids,int32_t * states,int32_t * scores)52*38e8c45fSAndroid Build Coastguard Worker     virtual status_t getProcessStatesAndOomScoresFromPids(size_t length,
53*38e8c45fSAndroid Build Coastguard Worker             /*in*/ int32_t* pids, /*out*/ int32_t* states, /*out*/ int32_t* scores)
54*38e8c45fSAndroid Build Coastguard Worker     {
55*38e8c45fSAndroid Build Coastguard Worker         Parcel data, reply;
56*38e8c45fSAndroid Build Coastguard Worker         data.writeInterfaceToken(IProcessInfoService::getInterfaceDescriptor());
57*38e8c45fSAndroid Build Coastguard Worker         data.writeInt32Array(length, pids);
58*38e8c45fSAndroid Build Coastguard Worker         // write length of output arrays, used by java AIDL stubs
59*38e8c45fSAndroid Build Coastguard Worker         data.writeInt32(length);
60*38e8c45fSAndroid Build Coastguard Worker         data.writeInt32(length);
61*38e8c45fSAndroid Build Coastguard Worker         status_t err = remote()->transact(
62*38e8c45fSAndroid Build Coastguard Worker                 GET_PROCESS_STATES_AND_OOM_SCORES_FROM_PIDS, data, &reply);
63*38e8c45fSAndroid Build Coastguard Worker         if (err != NO_ERROR
64*38e8c45fSAndroid Build Coastguard Worker                 || ((err = reply.readExceptionCode()) != NO_ERROR)) {
65*38e8c45fSAndroid Build Coastguard Worker             return err;
66*38e8c45fSAndroid Build Coastguard Worker         }
67*38e8c45fSAndroid Build Coastguard Worker         int32_t replyLen = reply.readInt32();
68*38e8c45fSAndroid Build Coastguard Worker         if (static_cast<size_t>(replyLen) != length) {
69*38e8c45fSAndroid Build Coastguard Worker             return NOT_ENOUGH_DATA;
70*38e8c45fSAndroid Build Coastguard Worker         }
71*38e8c45fSAndroid Build Coastguard Worker         if (replyLen > 0 && (err = reply.read(
72*38e8c45fSAndroid Build Coastguard Worker                 states, length * sizeof(*states))) != NO_ERROR) {
73*38e8c45fSAndroid Build Coastguard Worker             return err;
74*38e8c45fSAndroid Build Coastguard Worker         }
75*38e8c45fSAndroid Build Coastguard Worker         replyLen = reply.readInt32();
76*38e8c45fSAndroid Build Coastguard Worker         if (static_cast<size_t>(replyLen) != length) {
77*38e8c45fSAndroid Build Coastguard Worker             return NOT_ENOUGH_DATA;
78*38e8c45fSAndroid Build Coastguard Worker         }
79*38e8c45fSAndroid Build Coastguard Worker         if (replyLen > 0 && (err = reply.read(
80*38e8c45fSAndroid Build Coastguard Worker                 scores, length * sizeof(*scores))) != NO_ERROR) {
81*38e8c45fSAndroid Build Coastguard Worker             return err;
82*38e8c45fSAndroid Build Coastguard Worker         }
83*38e8c45fSAndroid Build Coastguard Worker         return reply.readInt32();
84*38e8c45fSAndroid Build Coastguard Worker     }
85*38e8c45fSAndroid Build Coastguard Worker };
86*38e8c45fSAndroid Build Coastguard Worker 
87*38e8c45fSAndroid Build Coastguard Worker IMPLEMENT_META_INTERFACE(ProcessInfoService, "android.os.IProcessInfoService")
88*38e8c45fSAndroid Build Coastguard Worker 
89*38e8c45fSAndroid Build Coastguard Worker // ----------------------------------------------------------------------
90*38e8c45fSAndroid Build Coastguard Worker 
91*38e8c45fSAndroid Build Coastguard Worker } // namespace android
92