BpHwBinder.cpp 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. /*
  2. * Copyright (C) 2005 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. #define LOG_TAG "hw-BpHwBinder"
  17. //#define LOG_NDEBUG 0
  18. #include <hwbinder/BpHwBinder.h>
  19. #include <hwbinder/IPCThreadState.h>
  20. #include <utils/Log.h>
  21. #include <stdio.h>
  22. //#undef ALOGV
  23. //#define ALOGV(...) fprintf(stderr, __VA_ARGS__)
  24. namespace android {
  25. namespace hardware {
  26. // ---------------------------------------------------------------------------
  27. BpHwBinder::ObjectManager::ObjectManager()
  28. {
  29. }
  30. BpHwBinder::ObjectManager::~ObjectManager()
  31. {
  32. kill();
  33. }
  34. void BpHwBinder::ObjectManager::attach(
  35. const void* objectID, void* object, void* cleanupCookie,
  36. IBinder::object_cleanup_func func)
  37. {
  38. entry_t e;
  39. e.object = object;
  40. e.cleanupCookie = cleanupCookie;
  41. e.func = func;
  42. if (mObjects.indexOfKey(objectID) >= 0) {
  43. ALOGE("Trying to attach object ID %p to binder ObjectManager %p with object %p, but object ID already in use",
  44. objectID, this, object);
  45. return;
  46. }
  47. mObjects.add(objectID, e);
  48. }
  49. void* BpHwBinder::ObjectManager::find(const void* objectID) const
  50. {
  51. const ssize_t i = mObjects.indexOfKey(objectID);
  52. if (i < 0) return nullptr;
  53. return mObjects.valueAt(i).object;
  54. }
  55. void BpHwBinder::ObjectManager::detach(const void* objectID)
  56. {
  57. mObjects.removeItem(objectID);
  58. }
  59. void BpHwBinder::ObjectManager::kill()
  60. {
  61. const size_t N = mObjects.size();
  62. ALOGV("Killing %zu objects in manager %p", N, this);
  63. for (size_t i=0; i<N; i++) {
  64. const entry_t& e = mObjects.valueAt(i);
  65. if (e.func != nullptr) {
  66. e.func(mObjects.keyAt(i), e.object, e.cleanupCookie);
  67. }
  68. }
  69. mObjects.clear();
  70. }
  71. // ---------------------------------------------------------------------------
  72. BpHwBinder::BpHwBinder(int32_t handle)
  73. : mHandle(handle)
  74. , mAlive(1)
  75. , mObitsSent(0)
  76. , mObituaries(nullptr)
  77. {
  78. ALOGV("Creating BpHwBinder %p handle %d\n", this, mHandle);
  79. extendObjectLifetime(OBJECT_LIFETIME_WEAK);
  80. IPCThreadState::self()->incWeakHandle(handle, this);
  81. }
  82. status_t BpHwBinder::transact(
  83. uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags, TransactCallback /*callback*/)
  84. {
  85. // Once a binder has died, it will never come back to life.
  86. if (mAlive) {
  87. status_t status = IPCThreadState::self()->transact(
  88. mHandle, code, data, reply, flags);
  89. if (status == DEAD_OBJECT) mAlive = 0;
  90. return status;
  91. }
  92. return DEAD_OBJECT;
  93. }
  94. status_t BpHwBinder::linkToDeath(
  95. const sp<DeathRecipient>& recipient, void* cookie, uint32_t flags)
  96. {
  97. Obituary ob;
  98. ob.recipient = recipient;
  99. ob.cookie = cookie;
  100. ob.flags = flags;
  101. LOG_ALWAYS_FATAL_IF(recipient == nullptr,
  102. "linkToDeath(): recipient must be non-NULL");
  103. {
  104. AutoMutex _l(mLock);
  105. if (!mObitsSent) {
  106. if (!mObituaries) {
  107. mObituaries = new Vector<Obituary>;
  108. if (!mObituaries) {
  109. return NO_MEMORY;
  110. }
  111. ALOGV("Requesting death notification: %p handle %d\n", this, mHandle);
  112. getWeakRefs()->incWeak(this);
  113. IPCThreadState* self = IPCThreadState::self();
  114. self->requestDeathNotification(mHandle, this);
  115. self->flushCommands();
  116. }
  117. ssize_t res = mObituaries->add(ob);
  118. return res >= (ssize_t)NO_ERROR ? (status_t)NO_ERROR : res;
  119. }
  120. }
  121. return DEAD_OBJECT;
  122. }
  123. status_t BpHwBinder::unlinkToDeath(
  124. const wp<DeathRecipient>& recipient, void* cookie, uint32_t flags,
  125. wp<DeathRecipient>* outRecipient)
  126. {
  127. AutoMutex _l(mLock);
  128. if (mObitsSent) {
  129. return DEAD_OBJECT;
  130. }
  131. const size_t N = mObituaries ? mObituaries->size() : 0;
  132. for (size_t i=0; i<N; i++) {
  133. const Obituary& obit = mObituaries->itemAt(i);
  134. if ((obit.recipient == recipient
  135. || (recipient == nullptr && obit.cookie == cookie))
  136. && obit.flags == flags) {
  137. if (outRecipient != nullptr) {
  138. *outRecipient = mObituaries->itemAt(i).recipient;
  139. }
  140. mObituaries->removeAt(i);
  141. if (mObituaries->size() == 0) {
  142. ALOGV("Clearing death notification: %p handle %d\n", this, mHandle);
  143. IPCThreadState* self = IPCThreadState::self();
  144. self->clearDeathNotification(mHandle, this);
  145. self->flushCommands();
  146. delete mObituaries;
  147. mObituaries = nullptr;
  148. }
  149. return NO_ERROR;
  150. }
  151. }
  152. return NAME_NOT_FOUND;
  153. }
  154. void BpHwBinder::sendObituary()
  155. {
  156. ALOGV("Sending obituary for proxy %p handle %d, mObitsSent=%s\n",
  157. this, mHandle, mObitsSent ? "true" : "false");
  158. mAlive = 0;
  159. if (mObitsSent) return;
  160. mLock.lock();
  161. Vector<Obituary>* obits = mObituaries;
  162. if(obits != nullptr) {
  163. ALOGV("Clearing sent death notification: %p handle %d\n", this, mHandle);
  164. IPCThreadState* self = IPCThreadState::self();
  165. self->clearDeathNotification(mHandle, this);
  166. self->flushCommands();
  167. mObituaries = nullptr;
  168. }
  169. mObitsSent = 1;
  170. mLock.unlock();
  171. ALOGV("Reporting death of proxy %p for %zu recipients\n",
  172. this, obits ? obits->size() : 0U);
  173. if (obits != nullptr) {
  174. const size_t N = obits->size();
  175. for (size_t i=0; i<N; i++) {
  176. reportOneDeath(obits->itemAt(i));
  177. }
  178. delete obits;
  179. }
  180. }
  181. // Returns the strong refcount on the object this proxy points to, or
  182. // -1 in case of failure.
  183. ssize_t BpHwBinder::getNodeStrongRefCount()
  184. {
  185. return ProcessState::self()->getStrongRefCountForNodeByHandle(mHandle);
  186. }
  187. void BpHwBinder::reportOneDeath(const Obituary& obit)
  188. {
  189. sp<DeathRecipient> recipient = obit.recipient.promote();
  190. ALOGV("Reporting death to recipient: %p\n", recipient.get());
  191. if (recipient == nullptr) return;
  192. recipient->binderDied(this);
  193. }
  194. void BpHwBinder::attachObject(
  195. const void* objectID, void* object, void* cleanupCookie,
  196. object_cleanup_func func)
  197. {
  198. AutoMutex _l(mLock);
  199. ALOGV("Attaching object %p to binder %p (manager=%p)", object, this, &mObjects);
  200. mObjects.attach(objectID, object, cleanupCookie, func);
  201. }
  202. void* BpHwBinder::findObject(const void* objectID) const
  203. {
  204. AutoMutex _l(mLock);
  205. return mObjects.find(objectID);
  206. }
  207. void BpHwBinder::detachObject(const void* objectID)
  208. {
  209. AutoMutex _l(mLock);
  210. mObjects.detach(objectID);
  211. }
  212. BpHwBinder* BpHwBinder::remoteBinder()
  213. {
  214. return this;
  215. }
  216. BpHwBinder::~BpHwBinder()
  217. {
  218. ALOGV("Destroying BpHwBinder %p handle %d\n", this, mHandle);
  219. IPCThreadState* ipc = IPCThreadState::self();
  220. mLock.lock();
  221. Vector<Obituary>* obits = mObituaries;
  222. if(obits != nullptr) {
  223. if (ipc) ipc->clearDeathNotification(mHandle, this);
  224. mObituaries = nullptr;
  225. }
  226. mLock.unlock();
  227. if (obits != nullptr) {
  228. // XXX Should we tell any remaining DeathRecipient
  229. // objects that the last strong ref has gone away, so they
  230. // are no longer linked?
  231. delete obits;
  232. }
  233. if (ipc) {
  234. ipc->expungeHandle(mHandle, this);
  235. ipc->decWeakHandle(mHandle);
  236. }
  237. }
  238. void BpHwBinder::onFirstRef()
  239. {
  240. ALOGV("onFirstRef BpHwBinder %p handle %d\n", this, mHandle);
  241. IPCThreadState* ipc = IPCThreadState::self();
  242. if (ipc) ipc->incStrongHandle(mHandle, this);
  243. }
  244. void BpHwBinder::onLastStrongRef(const void* /*id*/)
  245. {
  246. ALOGV("onLastStrongRef BpHwBinder %p handle %d\n", this, mHandle);
  247. IF_ALOGV() {
  248. printRefs();
  249. }
  250. IPCThreadState* ipc = IPCThreadState::self();
  251. if (ipc) {
  252. ipc->decStrongHandle(mHandle);
  253. ipc->flushCommands();
  254. }
  255. }
  256. bool BpHwBinder::onIncStrongAttempted(uint32_t /*flags*/, const void* /*id*/)
  257. {
  258. ALOGV("onIncStrongAttempted BpHwBinder %p handle %d\n", this, mHandle);
  259. IPCThreadState* ipc = IPCThreadState::self();
  260. return ipc ? ipc->attemptIncStrongHandle(mHandle) == NO_ERROR : false;
  261. }
  262. // ---------------------------------------------------------------------------
  263. }; // namespace hardware
  264. }; // namespace android