BpBinder.cpp 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  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 "BpBinder"
  17. //#define LOG_NDEBUG 0
  18. #include <binder/BpBinder.h>
  19. #include <binder/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. // ---------------------------------------------------------------------------
  26. BpBinder::ObjectManager::ObjectManager()
  27. {
  28. }
  29. BpBinder::ObjectManager::~ObjectManager()
  30. {
  31. kill();
  32. }
  33. void BpBinder::ObjectManager::attach(
  34. const void* objectID, void* object, void* cleanupCookie,
  35. IBinder::object_cleanup_func func)
  36. {
  37. entry_t e;
  38. e.object = object;
  39. e.cleanupCookie = cleanupCookie;
  40. e.func = func;
  41. if (mObjects.indexOfKey(objectID) >= 0) {
  42. ALOGE("Trying to attach object ID %p to binder ObjectManager %p with object %p, but object ID already in use",
  43. objectID, this, object);
  44. return;
  45. }
  46. mObjects.add(objectID, e);
  47. }
  48. void* BpBinder::ObjectManager::find(const void* objectID) const
  49. {
  50. const ssize_t i = mObjects.indexOfKey(objectID);
  51. if (i < 0) return NULL;
  52. return mObjects.valueAt(i).object;
  53. }
  54. void BpBinder::ObjectManager::detach(const void* objectID)
  55. {
  56. mObjects.removeItem(objectID);
  57. }
  58. void BpBinder::ObjectManager::kill()
  59. {
  60. const size_t N = mObjects.size();
  61. ALOGV("Killing %zu objects in manager %p", N, this);
  62. for (size_t i=0; i<N; i++) {
  63. const entry_t& e = mObjects.valueAt(i);
  64. if (e.func != NULL) {
  65. e.func(mObjects.keyAt(i), e.object, e.cleanupCookie);
  66. }
  67. }
  68. mObjects.clear();
  69. }
  70. // ---------------------------------------------------------------------------
  71. BpBinder::BpBinder(int32_t handle)
  72. : mHandle(handle)
  73. , mAlive(1)
  74. , mObitsSent(0)
  75. , mObituaries(NULL)
  76. {
  77. ALOGV("Creating BpBinder %p handle %d\n", this, mHandle);
  78. extendObjectLifetime(OBJECT_LIFETIME_WEAK);
  79. IPCThreadState::self()->incWeakHandle(handle);
  80. }
  81. bool BpBinder::isDescriptorCached() const {
  82. Mutex::Autolock _l(mLock);
  83. return mDescriptorCache.size() ? true : false;
  84. }
  85. const String16& BpBinder::getInterfaceDescriptor() const
  86. {
  87. if (isDescriptorCached() == false) {
  88. Parcel send, reply;
  89. // do the IPC without a lock held.
  90. status_t err = const_cast<BpBinder*>(this)->transact(
  91. INTERFACE_TRANSACTION, send, &reply);
  92. if (err == NO_ERROR) {
  93. String16 res(reply.readString16());
  94. Mutex::Autolock _l(mLock);
  95. // mDescriptorCache could have been assigned while the lock was
  96. // released.
  97. if (mDescriptorCache.size() == 0)
  98. mDescriptorCache = res;
  99. }
  100. }
  101. // we're returning a reference to a non-static object here. Usually this
  102. // is not something smart to do, however, with binder objects it is
  103. // (usually) safe because they are reference-counted.
  104. return mDescriptorCache;
  105. }
  106. bool BpBinder::isBinderAlive() const
  107. {
  108. return mAlive != 0;
  109. }
  110. status_t BpBinder::pingBinder()
  111. {
  112. Parcel send;
  113. Parcel reply;
  114. status_t err = transact(PING_TRANSACTION, send, &reply);
  115. if (err != NO_ERROR) return err;
  116. if (reply.dataSize() < sizeof(status_t)) return NOT_ENOUGH_DATA;
  117. return (status_t)reply.readInt32();
  118. }
  119. status_t BpBinder::dump(int fd, const Vector<String16>& args)
  120. {
  121. Parcel send;
  122. Parcel reply;
  123. send.writeFileDescriptor(fd);
  124. const size_t numArgs = args.size();
  125. send.writeInt32(numArgs);
  126. for (size_t i = 0; i < numArgs; i++) {
  127. send.writeString16(args[i]);
  128. }
  129. status_t err = transact(DUMP_TRANSACTION, send, &reply);
  130. return err;
  131. }
  132. status_t BpBinder::transact(
  133. uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
  134. {
  135. // Once a binder has died, it will never come back to life.
  136. if (mAlive) {
  137. status_t status = IPCThreadState::self()->transact(
  138. mHandle, code, data, reply, flags);
  139. if (status == DEAD_OBJECT) mAlive = 0;
  140. return status;
  141. }
  142. return DEAD_OBJECT;
  143. }
  144. status_t BpBinder::linkToDeath(
  145. const sp<DeathRecipient>& recipient, void* cookie, uint32_t flags)
  146. {
  147. Obituary ob;
  148. ob.recipient = recipient;
  149. ob.cookie = cookie;
  150. ob.flags = flags;
  151. LOG_ALWAYS_FATAL_IF(recipient == NULL,
  152. "linkToDeath(): recipient must be non-NULL");
  153. {
  154. AutoMutex _l(mLock);
  155. if (!mObitsSent) {
  156. if (!mObituaries) {
  157. mObituaries = new Vector<Obituary>;
  158. if (!mObituaries) {
  159. return NO_MEMORY;
  160. }
  161. ALOGV("Requesting death notification: %p handle %d\n", this, mHandle);
  162. getWeakRefs()->incWeak(this);
  163. IPCThreadState* self = IPCThreadState::self();
  164. self->requestDeathNotification(mHandle, this);
  165. self->flushCommands();
  166. }
  167. ssize_t res = mObituaries->add(ob);
  168. return res >= (ssize_t)NO_ERROR ? (status_t)NO_ERROR : res;
  169. }
  170. }
  171. return DEAD_OBJECT;
  172. }
  173. status_t BpBinder::unlinkToDeath(
  174. const wp<DeathRecipient>& recipient, void* cookie, uint32_t flags,
  175. wp<DeathRecipient>* outRecipient)
  176. {
  177. AutoMutex _l(mLock);
  178. if (mObitsSent) {
  179. return DEAD_OBJECT;
  180. }
  181. const size_t N = mObituaries ? mObituaries->size() : 0;
  182. for (size_t i=0; i<N; i++) {
  183. const Obituary& obit = mObituaries->itemAt(i);
  184. if ((obit.recipient == recipient
  185. || (recipient == NULL && obit.cookie == cookie))
  186. && obit.flags == flags) {
  187. if (outRecipient != NULL) {
  188. *outRecipient = mObituaries->itemAt(i).recipient;
  189. }
  190. mObituaries->removeAt(i);
  191. if (mObituaries->size() == 0) {
  192. ALOGV("Clearing death notification: %p handle %d\n", this, mHandle);
  193. IPCThreadState* self = IPCThreadState::self();
  194. self->clearDeathNotification(mHandle, this);
  195. self->flushCommands();
  196. delete mObituaries;
  197. mObituaries = NULL;
  198. }
  199. return NO_ERROR;
  200. }
  201. }
  202. return NAME_NOT_FOUND;
  203. }
  204. void BpBinder::sendObituary()
  205. {
  206. ALOGV("Sending obituary for proxy %p handle %d, mObitsSent=%s\n",
  207. this, mHandle, mObitsSent ? "true" : "false");
  208. mAlive = 0;
  209. if (mObitsSent) return;
  210. mLock.lock();
  211. Vector<Obituary>* obits = mObituaries;
  212. if(obits != NULL) {
  213. ALOGV("Clearing sent death notification: %p handle %d\n", this, mHandle);
  214. IPCThreadState* self = IPCThreadState::self();
  215. self->clearDeathNotification(mHandle, this);
  216. self->flushCommands();
  217. mObituaries = NULL;
  218. }
  219. mObitsSent = 1;
  220. mLock.unlock();
  221. ALOGV("Reporting death of proxy %p for %zu recipients\n",
  222. this, obits ? obits->size() : 0U);
  223. if (obits != NULL) {
  224. const size_t N = obits->size();
  225. for (size_t i=0; i<N; i++) {
  226. reportOneDeath(obits->itemAt(i));
  227. }
  228. delete obits;
  229. }
  230. }
  231. void BpBinder::reportOneDeath(const Obituary& obit)
  232. {
  233. sp<DeathRecipient> recipient = obit.recipient.promote();
  234. ALOGV("Reporting death to recipient: %p\n", recipient.get());
  235. if (recipient == NULL) return;
  236. recipient->binderDied(this);
  237. }
  238. void BpBinder::attachObject(
  239. const void* objectID, void* object, void* cleanupCookie,
  240. object_cleanup_func func)
  241. {
  242. AutoMutex _l(mLock);
  243. ALOGV("Attaching object %p to binder %p (manager=%p)", object, this, &mObjects);
  244. mObjects.attach(objectID, object, cleanupCookie, func);
  245. }
  246. void* BpBinder::findObject(const void* objectID) const
  247. {
  248. AutoMutex _l(mLock);
  249. return mObjects.find(objectID);
  250. }
  251. void BpBinder::detachObject(const void* objectID)
  252. {
  253. AutoMutex _l(mLock);
  254. mObjects.detach(objectID);
  255. }
  256. BpBinder* BpBinder::remoteBinder()
  257. {
  258. return this;
  259. }
  260. BpBinder::~BpBinder()
  261. {
  262. ALOGV("Destroying BpBinder %p handle %d\n", this, mHandle);
  263. IPCThreadState* ipc = IPCThreadState::self();
  264. mLock.lock();
  265. Vector<Obituary>* obits = mObituaries;
  266. if(obits != NULL) {
  267. if (ipc) ipc->clearDeathNotification(mHandle, this);
  268. mObituaries = NULL;
  269. }
  270. mLock.unlock();
  271. if (obits != NULL) {
  272. // XXX Should we tell any remaining DeathRecipient
  273. // objects that the last strong ref has gone away, so they
  274. // are no longer linked?
  275. delete obits;
  276. }
  277. if (ipc) {
  278. ipc->expungeHandle(mHandle, this);
  279. ipc->decWeakHandle(mHandle);
  280. }
  281. }
  282. void BpBinder::onFirstRef()
  283. {
  284. ALOGV("onFirstRef BpBinder %p handle %d\n", this, mHandle);
  285. IPCThreadState* ipc = IPCThreadState::self();
  286. if (ipc) ipc->incStrongHandle(mHandle);
  287. }
  288. void BpBinder::onLastStrongRef(const void* /*id*/)
  289. {
  290. ALOGV("onLastStrongRef BpBinder %p handle %d\n", this, mHandle);
  291. IF_ALOGV() {
  292. printRefs();
  293. }
  294. IPCThreadState* ipc = IPCThreadState::self();
  295. if (ipc) ipc->decStrongHandle(mHandle);
  296. }
  297. bool BpBinder::onIncStrongAttempted(uint32_t /*flags*/, const void* /*id*/)
  298. {
  299. ALOGV("onIncStrongAttempted BpBinder %p handle %d\n", this, mHandle);
  300. IPCThreadState* ipc = IPCThreadState::self();
  301. return ipc ? ipc->attemptIncStrongHandle(mHandle) == NO_ERROR : false;
  302. }
  303. // ---------------------------------------------------------------------------
  304. }; // namespace android