Ruben Brunk | 52f0407 | 2015-01-28 18:13:33 -0800 | [diff] [blame] | 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 | |
| 22 | namespace android { |
| 23 | |
| 24 | // ---------------------------------------------------------------------- |
| 25 | |
| 26 | class BpProcessInfoService : public BpInterface<IProcessInfoService> { |
| 27 | public: |
| 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 | |
Chong Zhang | c72a90a | 2015-12-01 14:08:22 -0800 | [diff] [blame^] | 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 | } |
Ruben Brunk | 52f0407 | 2015-01-28 18:13:33 -0800 | [diff] [blame] | 85 | }; |
| 86 | |
| 87 | IMPLEMENT_META_INTERFACE(ProcessInfoService, "android.os.IProcessInfoService"); |
| 88 | |
| 89 | // ---------------------------------------------------------------------- |
| 90 | |
| 91 | status_t BnProcessInfoService::onTransact( uint32_t code, const Parcel& data, Parcel* reply, |
| 92 | uint32_t flags) { |
| 93 | switch(code) { |
| 94 | case GET_PROCESS_STATES_FROM_PIDS: { |
| 95 | CHECK_INTERFACE(IProcessInfoService, data, reply); |
| 96 | int32_t arrayLen = data.readInt32(); |
| 97 | if (arrayLen <= 0) { |
| 98 | reply->writeNoException(); |
| 99 | reply->writeInt32(0); |
| 100 | reply->writeInt32(NOT_ENOUGH_DATA); |
| 101 | return NO_ERROR; |
| 102 | } |
| 103 | |
| 104 | size_t len = static_cast<size_t>(arrayLen); |
| 105 | int32_t pids[len]; |
| 106 | status_t res = data.read(pids, len * sizeof(*pids)); |
| 107 | |
| 108 | // Ignore output array length returned in the parcel here, as the states array must |
| 109 | // always be the same length as the input PIDs array. |
| 110 | int32_t states[len]; |
| 111 | for (size_t i = 0; i < len; i++) states[i] = -1; |
| 112 | if (res == NO_ERROR) { |
| 113 | res = getProcessStatesFromPids(len, /*in*/ pids, /*out*/ states); |
| 114 | } |
| 115 | reply->writeNoException(); |
| 116 | reply->writeInt32Array(len, states); |
| 117 | reply->writeInt32(res); |
| 118 | return NO_ERROR; |
| 119 | } break; |
Chong Zhang | c72a90a | 2015-12-01 14:08:22 -0800 | [diff] [blame^] | 120 | case GET_PROCESS_STATES_AND_OOM_SCORES_FROM_PIDS: { |
| 121 | CHECK_INTERFACE(IProcessInfoService, data, reply); |
| 122 | int32_t arrayLen = data.readInt32(); |
| 123 | if (arrayLen <= 0) { |
| 124 | reply->writeNoException(); |
| 125 | reply->writeInt32(0); |
| 126 | reply->writeInt32(NOT_ENOUGH_DATA); |
| 127 | return NO_ERROR; |
| 128 | } |
| 129 | |
| 130 | size_t len = static_cast<size_t>(arrayLen); |
| 131 | int32_t pids[len]; |
| 132 | status_t res = data.read(pids, len * sizeof(*pids)); |
| 133 | |
| 134 | // Ignore output array length returned in the parcel here, as the |
| 135 | // states array must always be the same length as the input PIDs array. |
| 136 | int32_t states[len]; |
| 137 | int32_t scores[len]; |
| 138 | for (size_t i = 0; i < len; i++) { |
| 139 | states[i] = -1; |
| 140 | scores[i] = -10000; |
| 141 | } |
| 142 | if (res == NO_ERROR) { |
| 143 | res = getProcessStatesAndOomScoresFromPids( |
| 144 | len, /*in*/ pids, /*out*/ states, /*out*/ scores); |
| 145 | } |
| 146 | reply->writeNoException(); |
| 147 | reply->writeInt32Array(len, states); |
| 148 | reply->writeInt32Array(len, scores); |
| 149 | reply->writeInt32(res); |
| 150 | return NO_ERROR; |
| 151 | } break; |
Ruben Brunk | 52f0407 | 2015-01-28 18:13:33 -0800 | [diff] [blame] | 152 | default: |
| 153 | return BBinder::onTransact(code, data, reply, flags); |
| 154 | } |
| 155 | } |
| 156 | |
| 157 | // ---------------------------------------------------------------------- |
| 158 | |
| 159 | }; // namespace android |