IProcessInfoService.cpp 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. #include <binder/IProcessInfoService.h>
  17. #include <binder/Parcel.h>
  18. #include <utils/Errors.h>
  19. #include <sys/types.h>
  20. namespace android {
  21. // ----------------------------------------------------------------------
  22. class BpProcessInfoService : public BpInterface<IProcessInfoService> {
  23. public:
  24. BpProcessInfoService(const sp<IBinder>& impl)
  25. : BpInterface<IProcessInfoService>(impl) {}
  26. virtual status_t getProcessStatesFromPids(size_t length, /*in*/ int32_t* pids,
  27. /*out*/ int32_t* states)
  28. {
  29. Parcel data, reply;
  30. data.writeInterfaceToken(IProcessInfoService::getInterfaceDescriptor());
  31. data.writeInt32Array(length, pids);
  32. data.writeInt32(length); // write length of output array, used by java AIDL stubs
  33. status_t err = remote()->transact(GET_PROCESS_STATES_FROM_PIDS, data, &reply);
  34. if (err != NO_ERROR || ((err = reply.readExceptionCode()) != NO_ERROR)) {
  35. return err;
  36. }
  37. int32_t replyLen = reply.readInt32();
  38. if (static_cast<size_t>(replyLen) != length) {
  39. return NOT_ENOUGH_DATA;
  40. }
  41. if (replyLen > 0 && (err = reply.read(states, length * sizeof(*states))) != NO_ERROR) {
  42. return err;
  43. }
  44. return reply.readInt32();
  45. }
  46. };
  47. IMPLEMENT_META_INTERFACE(ProcessInfoService, "android.os.IProcessInfoService");
  48. // ----------------------------------------------------------------------
  49. status_t BnProcessInfoService::onTransact( uint32_t code, const Parcel& data, Parcel* reply,
  50. uint32_t flags) {
  51. switch(code) {
  52. case GET_PROCESS_STATES_FROM_PIDS: {
  53. CHECK_INTERFACE(IProcessInfoService, data, reply);
  54. int32_t arrayLen = data.readInt32();
  55. if (arrayLen <= 0) {
  56. reply->writeNoException();
  57. reply->writeInt32(0);
  58. reply->writeInt32(NOT_ENOUGH_DATA);
  59. return NO_ERROR;
  60. }
  61. size_t len = static_cast<size_t>(arrayLen);
  62. int32_t pids[len];
  63. status_t res = data.read(pids, len * sizeof(*pids));
  64. // Ignore output array length returned in the parcel here, as the states array must
  65. // always be the same length as the input PIDs array.
  66. int32_t states[len];
  67. for (size_t i = 0; i < len; i++) states[i] = -1;
  68. if (res == NO_ERROR) {
  69. res = getProcessStatesFromPids(len, /*in*/ pids, /*out*/ states);
  70. }
  71. reply->writeNoException();
  72. reply->writeInt32Array(len, states);
  73. reply->writeInt32(res);
  74. return NO_ERROR;
  75. } break;
  76. default:
  77. return BBinder::onTransact(code, data, reply, flags);
  78. }
  79. }
  80. // ----------------------------------------------------------------------
  81. }; // namespace android