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 | |
| 52 | }; |
| 53 | |
| 54 | IMPLEMENT_META_INTERFACE(ProcessInfoService, "android.os.IProcessInfoService"); |
| 55 | |
| 56 | // ---------------------------------------------------------------------- |
| 57 | |
| 58 | status_t BnProcessInfoService::onTransact( uint32_t code, const Parcel& data, Parcel* reply, |
| 59 | uint32_t flags) { |
| 60 | switch(code) { |
| 61 | case GET_PROCESS_STATES_FROM_PIDS: { |
| 62 | CHECK_INTERFACE(IProcessInfoService, data, reply); |
| 63 | int32_t arrayLen = data.readInt32(); |
| 64 | if (arrayLen <= 0) { |
| 65 | reply->writeNoException(); |
| 66 | reply->writeInt32(0); |
| 67 | reply->writeInt32(NOT_ENOUGH_DATA); |
| 68 | return NO_ERROR; |
| 69 | } |
| 70 | |
| 71 | size_t len = static_cast<size_t>(arrayLen); |
| 72 | int32_t pids[len]; |
| 73 | status_t res = data.read(pids, len * sizeof(*pids)); |
| 74 | |
| 75 | // Ignore output array length returned in the parcel here, as the states array must |
| 76 | // always be the same length as the input PIDs array. |
| 77 | int32_t states[len]; |
| 78 | for (size_t i = 0; i < len; i++) states[i] = -1; |
| 79 | if (res == NO_ERROR) { |
| 80 | res = getProcessStatesFromPids(len, /*in*/ pids, /*out*/ states); |
| 81 | } |
| 82 | reply->writeNoException(); |
| 83 | reply->writeInt32Array(len, states); |
| 84 | reply->writeInt32(res); |
| 85 | return NO_ERROR; |
| 86 | } break; |
| 87 | default: |
| 88 | return BBinder::onTransact(code, data, reply, flags); |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | // ---------------------------------------------------------------------- |
| 93 | |
| 94 | }; // namespace android |