Binder.cpp 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  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. #include <binder/Binder.h>
  17. #include <stdatomic.h>
  18. #include <utils/misc.h>
  19. #include <binder/BpBinder.h>
  20. #include <binder/IInterface.h>
  21. #include <binder/Parcel.h>
  22. #include <stdio.h>
  23. namespace android {
  24. // ---------------------------------------------------------------------------
  25. IBinder::IBinder()
  26. : RefBase()
  27. {
  28. }
  29. IBinder::~IBinder()
  30. {
  31. }
  32. // ---------------------------------------------------------------------------
  33. sp<IInterface> IBinder::queryLocalInterface(const String16& /*descriptor*/)
  34. {
  35. return NULL;
  36. }
  37. BBinder* IBinder::localBinder()
  38. {
  39. return NULL;
  40. }
  41. BpBinder* IBinder::remoteBinder()
  42. {
  43. return NULL;
  44. }
  45. bool IBinder::checkSubclass(const void* /*subclassID*/) const
  46. {
  47. return false;
  48. }
  49. // ---------------------------------------------------------------------------
  50. class BBinder::Extras
  51. {
  52. public:
  53. Mutex mLock;
  54. BpBinder::ObjectManager mObjects;
  55. };
  56. // ---------------------------------------------------------------------------
  57. BBinder::BBinder()
  58. {
  59. atomic_init(&mExtras, static_cast<uintptr_t>(0));
  60. }
  61. bool BBinder::isBinderAlive() const
  62. {
  63. return true;
  64. }
  65. status_t BBinder::pingBinder()
  66. {
  67. return NO_ERROR;
  68. }
  69. const String16& BBinder::getInterfaceDescriptor() const
  70. {
  71. // This is a local static rather than a global static,
  72. // to avoid static initializer ordering issues.
  73. static String16 sEmptyDescriptor;
  74. ALOGW("reached BBinder::getInterfaceDescriptor (this=%p)", this);
  75. return sEmptyDescriptor;
  76. }
  77. status_t BBinder::transact(
  78. uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
  79. {
  80. data.setDataPosition(0);
  81. status_t err = NO_ERROR;
  82. switch (code) {
  83. case PING_TRANSACTION:
  84. reply->writeInt32(pingBinder());
  85. break;
  86. default:
  87. err = onTransact(code, data, reply, flags);
  88. break;
  89. }
  90. if (reply != NULL) {
  91. reply->setDataPosition(0);
  92. }
  93. return err;
  94. }
  95. status_t BBinder::linkToDeath(
  96. const sp<DeathRecipient>& /*recipient*/, void* /*cookie*/,
  97. uint32_t /*flags*/)
  98. {
  99. return INVALID_OPERATION;
  100. }
  101. status_t BBinder::unlinkToDeath(
  102. const wp<DeathRecipient>& /*recipient*/, void* /*cookie*/,
  103. uint32_t /*flags*/, wp<DeathRecipient>* /*outRecipient*/)
  104. {
  105. return INVALID_OPERATION;
  106. }
  107. status_t BBinder::dump(int /*fd*/, const Vector<String16>& /*args*/)
  108. {
  109. return NO_ERROR;
  110. }
  111. void BBinder::attachObject(
  112. const void* objectID, void* object, void* cleanupCookie,
  113. object_cleanup_func func)
  114. {
  115. Extras* e = reinterpret_cast<Extras*>(
  116. atomic_load_explicit(&mExtras, memory_order_acquire));
  117. if (!e) {
  118. e = new Extras;
  119. uintptr_t expected = 0;
  120. if (!atomic_compare_exchange_strong_explicit(
  121. &mExtras, &expected,
  122. reinterpret_cast<uintptr_t>(e),
  123. memory_order_release,
  124. memory_order_acquire)) {
  125. delete e;
  126. e = reinterpret_cast<Extras*>(expected); // Filled in by CAS
  127. }
  128. if (e == 0) return; // out of memory
  129. }
  130. AutoMutex _l(e->mLock);
  131. e->mObjects.attach(objectID, object, cleanupCookie, func);
  132. }
  133. // The C11 standard doesn't allow atomic loads from const fields,
  134. // though C++11 does. Fudge it until standards get straightened out.
  135. static inline uintptr_t load_const_atomic(const atomic_uintptr_t* p,
  136. memory_order mo) {
  137. atomic_uintptr_t* non_const_p = const_cast<atomic_uintptr_t*>(p);
  138. return atomic_load_explicit(non_const_p, mo);
  139. }
  140. void* BBinder::findObject(const void* objectID) const
  141. {
  142. Extras* e = reinterpret_cast<Extras*>(
  143. load_const_atomic(&mExtras, memory_order_acquire));
  144. if (!e) return NULL;
  145. AutoMutex _l(e->mLock);
  146. return e->mObjects.find(objectID);
  147. }
  148. void BBinder::detachObject(const void* objectID)
  149. {
  150. Extras* e = reinterpret_cast<Extras*>(
  151. atomic_load_explicit(&mExtras, memory_order_acquire));
  152. if (!e) return;
  153. AutoMutex _l(e->mLock);
  154. e->mObjects.detach(objectID);
  155. }
  156. BBinder* BBinder::localBinder()
  157. {
  158. return this;
  159. }
  160. BBinder::~BBinder()
  161. {
  162. Extras* e = reinterpret_cast<Extras*>(
  163. atomic_load_explicit(&mExtras, memory_order_relaxed));
  164. if (e) delete e;
  165. }
  166. status_t BBinder::onTransact(
  167. uint32_t code, const Parcel& data, Parcel* reply, uint32_t /*flags*/)
  168. {
  169. switch (code) {
  170. case INTERFACE_TRANSACTION:
  171. reply->writeString16(getInterfaceDescriptor());
  172. return NO_ERROR;
  173. case DUMP_TRANSACTION: {
  174. int fd = data.readFileDescriptor();
  175. int argc = data.readInt32();
  176. Vector<String16> args;
  177. for (int i = 0; i < argc && data.dataAvail() > 0; i++) {
  178. args.add(data.readString16());
  179. }
  180. return dump(fd, args);
  181. }
  182. case SYSPROPS_TRANSACTION: {
  183. report_sysprop_change();
  184. return NO_ERROR;
  185. }
  186. default:
  187. return UNKNOWN_TRANSACTION;
  188. }
  189. }
  190. // ---------------------------------------------------------------------------
  191. enum {
  192. // This is used to transfer ownership of the remote binder from
  193. // the BpRefBase object holding it (when it is constructed), to the
  194. // owner of the BpRefBase object when it first acquires that BpRefBase.
  195. kRemoteAcquired = 0x00000001
  196. };
  197. BpRefBase::BpRefBase(const sp<IBinder>& o)
  198. : mRemote(o.get()), mRefs(NULL), mState(0)
  199. {
  200. extendObjectLifetime(OBJECT_LIFETIME_WEAK);
  201. if (mRemote) {
  202. mRemote->incStrong(this); // Removed on first IncStrong().
  203. mRefs = mRemote->createWeak(this); // Held for our entire lifetime.
  204. }
  205. }
  206. BpRefBase::~BpRefBase()
  207. {
  208. if (mRemote) {
  209. if (!(mState&kRemoteAcquired)) {
  210. mRemote->decStrong(this);
  211. }
  212. mRefs->decWeak(this);
  213. }
  214. }
  215. void BpRefBase::onFirstRef()
  216. {
  217. android_atomic_or(kRemoteAcquired, &mState);
  218. }
  219. void BpRefBase::onLastStrongRef(const void* /*id*/)
  220. {
  221. if (mRemote) {
  222. mRemote->decStrong(this);
  223. }
  224. }
  225. bool BpRefBase::onIncStrongAttempted(uint32_t /*flags*/, const void* /*id*/)
  226. {
  227. return mRemote ? mRefs->attemptIncStrong(this) : false;
  228. }
  229. // ---------------------------------------------------------------------------
  230. }; // namespace android