Binder.cpp 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  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 <atomic>
  18. #include <utils/misc.h>
  19. #include <binder/BpBinder.h>
  20. #include <binder/IInterface.h>
  21. #include <binder/IResultReceiver.h>
  22. #include <binder/IShellCallback.h>
  23. #include <binder/Parcel.h>
  24. #include <stdio.h>
  25. namespace android {
  26. // ---------------------------------------------------------------------------
  27. IBinder::IBinder()
  28. : RefBase()
  29. {
  30. }
  31. IBinder::~IBinder()
  32. {
  33. }
  34. // ---------------------------------------------------------------------------
  35. sp<IInterface> IBinder::queryLocalInterface(const String16& /*descriptor*/)
  36. {
  37. return nullptr;
  38. }
  39. BBinder* IBinder::localBinder()
  40. {
  41. return nullptr;
  42. }
  43. BpBinder* IBinder::remoteBinder()
  44. {
  45. return nullptr;
  46. }
  47. bool IBinder::checkSubclass(const void* /*subclassID*/) const
  48. {
  49. return false;
  50. }
  51. status_t IBinder::shellCommand(const sp<IBinder>& target, int in, int out, int err,
  52. Vector<String16>& args, const sp<IShellCallback>& callback,
  53. const sp<IResultReceiver>& resultReceiver)
  54. {
  55. Parcel send;
  56. Parcel reply;
  57. send.writeFileDescriptor(in);
  58. send.writeFileDescriptor(out);
  59. send.writeFileDescriptor(err);
  60. const size_t numArgs = args.size();
  61. send.writeInt32(numArgs);
  62. for (size_t i = 0; i < numArgs; i++) {
  63. send.writeString16(args[i]);
  64. }
  65. send.writeStrongBinder(callback != nullptr ? IInterface::asBinder(callback) : nullptr);
  66. send.writeStrongBinder(resultReceiver != nullptr ? IInterface::asBinder(resultReceiver) : nullptr);
  67. return target->transact(SHELL_COMMAND_TRANSACTION, send, &reply);
  68. }
  69. // ---------------------------------------------------------------------------
  70. class BBinder::Extras
  71. {
  72. public:
  73. // unlocked objects
  74. bool mRequestingSid = false;
  75. // for below objects
  76. Mutex mLock;
  77. BpBinder::ObjectManager mObjects;
  78. };
  79. // ---------------------------------------------------------------------------
  80. BBinder::BBinder() : mExtras(nullptr)
  81. {
  82. }
  83. bool BBinder::isBinderAlive() const
  84. {
  85. return true;
  86. }
  87. status_t BBinder::pingBinder()
  88. {
  89. return NO_ERROR;
  90. }
  91. const String16& BBinder::getInterfaceDescriptor() const
  92. {
  93. // This is a local static rather than a global static,
  94. // to avoid static initializer ordering issues.
  95. static String16 sEmptyDescriptor;
  96. ALOGW("reached BBinder::getInterfaceDescriptor (this=%p)", this);
  97. return sEmptyDescriptor;
  98. }
  99. // NOLINTNEXTLINE(google-default-arguments)
  100. status_t BBinder::transact(
  101. uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
  102. {
  103. data.setDataPosition(0);
  104. status_t err = NO_ERROR;
  105. switch (code) {
  106. case PING_TRANSACTION:
  107. reply->writeInt32(pingBinder());
  108. break;
  109. default:
  110. err = onTransact(code, data, reply, flags);
  111. break;
  112. }
  113. if (reply != nullptr) {
  114. reply->setDataPosition(0);
  115. }
  116. return err;
  117. }
  118. // NOLINTNEXTLINE(google-default-arguments)
  119. status_t BBinder::linkToDeath(
  120. const sp<DeathRecipient>& /*recipient*/, void* /*cookie*/,
  121. uint32_t /*flags*/)
  122. {
  123. return INVALID_OPERATION;
  124. }
  125. // NOLINTNEXTLINE(google-default-arguments)
  126. status_t BBinder::unlinkToDeath(
  127. const wp<DeathRecipient>& /*recipient*/, void* /*cookie*/,
  128. uint32_t /*flags*/, wp<DeathRecipient>* /*outRecipient*/)
  129. {
  130. return INVALID_OPERATION;
  131. }
  132. status_t BBinder::dump(int /*fd*/, const Vector<String16>& /*args*/)
  133. {
  134. return NO_ERROR;
  135. }
  136. void BBinder::attachObject(
  137. const void* objectID, void* object, void* cleanupCookie,
  138. object_cleanup_func func)
  139. {
  140. Extras* e = getOrCreateExtras();
  141. if (!e) return; // out of memory
  142. AutoMutex _l(e->mLock);
  143. e->mObjects.attach(objectID, object, cleanupCookie, func);
  144. }
  145. void* BBinder::findObject(const void* objectID) const
  146. {
  147. Extras* e = mExtras.load(std::memory_order_acquire);
  148. if (!e) return nullptr;
  149. AutoMutex _l(e->mLock);
  150. return e->mObjects.find(objectID);
  151. }
  152. void BBinder::detachObject(const void* objectID)
  153. {
  154. Extras* e = mExtras.load(std::memory_order_acquire);
  155. if (!e) return;
  156. AutoMutex _l(e->mLock);
  157. e->mObjects.detach(objectID);
  158. }
  159. BBinder* BBinder::localBinder()
  160. {
  161. return this;
  162. }
  163. bool BBinder::isRequestingSid()
  164. {
  165. Extras* e = mExtras.load(std::memory_order_acquire);
  166. return e && e->mRequestingSid;
  167. }
  168. void BBinder::setRequestingSid(bool requestingSid)
  169. {
  170. Extras* e = mExtras.load(std::memory_order_acquire);
  171. if (!e) {
  172. // default is false. Most things don't need sids, so avoiding allocations when possible.
  173. if (!requestingSid) {
  174. return;
  175. }
  176. e = getOrCreateExtras();
  177. if (!e) return; // out of memory
  178. }
  179. e->mRequestingSid = requestingSid;
  180. }
  181. BBinder::~BBinder()
  182. {
  183. Extras* e = mExtras.load(std::memory_order_relaxed);
  184. if (e) delete e;
  185. }
  186. // NOLINTNEXTLINE(google-default-arguments)
  187. status_t BBinder::onTransact(
  188. uint32_t code, const Parcel& data, Parcel* reply, uint32_t /*flags*/)
  189. {
  190. switch (code) {
  191. case INTERFACE_TRANSACTION:
  192. reply->writeString16(getInterfaceDescriptor());
  193. return NO_ERROR;
  194. case DUMP_TRANSACTION: {
  195. int fd = data.readFileDescriptor();
  196. int argc = data.readInt32();
  197. Vector<String16> args;
  198. for (int i = 0; i < argc && data.dataAvail() > 0; i++) {
  199. args.add(data.readString16());
  200. }
  201. return dump(fd, args);
  202. }
  203. case SHELL_COMMAND_TRANSACTION: {
  204. int in = data.readFileDescriptor();
  205. int out = data.readFileDescriptor();
  206. int err = data.readFileDescriptor();
  207. int argc = data.readInt32();
  208. Vector<String16> args;
  209. for (int i = 0; i < argc && data.dataAvail() > 0; i++) {
  210. args.add(data.readString16());
  211. }
  212. sp<IShellCallback> shellCallback = IShellCallback::asInterface(
  213. data.readStrongBinder());
  214. sp<IResultReceiver> resultReceiver = IResultReceiver::asInterface(
  215. data.readStrongBinder());
  216. // XXX can't add virtuals until binaries are updated.
  217. //return shellCommand(in, out, err, args, resultReceiver);
  218. (void)in;
  219. (void)out;
  220. (void)err;
  221. if (resultReceiver != nullptr) {
  222. resultReceiver->send(INVALID_OPERATION);
  223. }
  224. return NO_ERROR;
  225. }
  226. case SYSPROPS_TRANSACTION: {
  227. report_sysprop_change();
  228. return NO_ERROR;
  229. }
  230. default:
  231. return UNKNOWN_TRANSACTION;
  232. }
  233. }
  234. BBinder::Extras* BBinder::getOrCreateExtras()
  235. {
  236. Extras* e = mExtras.load(std::memory_order_acquire);
  237. if (!e) {
  238. e = new Extras;
  239. Extras* expected = nullptr;
  240. if (!mExtras.compare_exchange_strong(expected, e,
  241. std::memory_order_release,
  242. std::memory_order_acquire)) {
  243. delete e;
  244. e = expected; // Filled in by CAS
  245. }
  246. if (e == nullptr) return nullptr; // out of memory
  247. }
  248. return e;
  249. }
  250. // ---------------------------------------------------------------------------
  251. enum {
  252. // This is used to transfer ownership of the remote binder from
  253. // the BpRefBase object holding it (when it is constructed), to the
  254. // owner of the BpRefBase object when it first acquires that BpRefBase.
  255. kRemoteAcquired = 0x00000001
  256. };
  257. BpRefBase::BpRefBase(const sp<IBinder>& o)
  258. : mRemote(o.get()), mRefs(nullptr), mState(0)
  259. {
  260. extendObjectLifetime(OBJECT_LIFETIME_WEAK);
  261. if (mRemote) {
  262. mRemote->incStrong(this); // Removed on first IncStrong().
  263. mRefs = mRemote->createWeak(this); // Held for our entire lifetime.
  264. }
  265. }
  266. BpRefBase::~BpRefBase()
  267. {
  268. if (mRemote) {
  269. if (!(mState.load(std::memory_order_relaxed)&kRemoteAcquired)) {
  270. mRemote->decStrong(this);
  271. }
  272. mRefs->decWeak(this);
  273. }
  274. }
  275. void BpRefBase::onFirstRef()
  276. {
  277. mState.fetch_or(kRemoteAcquired, std::memory_order_relaxed);
  278. }
  279. void BpRefBase::onLastStrongRef(const void* /*id*/)
  280. {
  281. if (mRemote) {
  282. mRemote->decStrong(this);
  283. }
  284. }
  285. bool BpRefBase::onIncStrongAttempted(uint32_t /*flags*/, const void* /*id*/)
  286. {
  287. return mRemote ? mRefs->attemptIncStrong(this) : false;
  288. }
  289. // ---------------------------------------------------------------------------
  290. }; // namespace android