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