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