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