| 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> | 
| Steven Moreland | d138798 | 2019-07-24 00:12:19 +0000 | [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 |  | 
| Steven Moreland | bf1915b | 2020-07-16 22:43:02 +0000 | [diff] [blame] | 27 | #include <linux/sched.h> | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 28 | #include <stdio.h> | 
|  | 29 |  | 
|  | 30 | namespace android { | 
|  | 31 |  | 
|  | 32 | // --------------------------------------------------------------------------- | 
|  | 33 |  | 
| Mathias Agopian | 83c0446 | 2009-05-22 19:00:22 -0700 | [diff] [blame] | 34 | IBinder::IBinder() | 
|  | 35 | : RefBase() | 
|  | 36 | { | 
|  | 37 | } | 
|  | 38 |  | 
|  | 39 | IBinder::~IBinder() | 
|  | 40 | { | 
|  | 41 | } | 
|  | 42 |  | 
|  | 43 | // --------------------------------------------------------------------------- | 
|  | 44 |  | 
| Colin Cross | 6f4f3ab | 2014-02-05 17:42:44 -0800 | [diff] [blame] | 45 | sp<IInterface>  IBinder::queryLocalInterface(const String16& /*descriptor*/) | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 46 | { | 
| Yi Kong | fdd8da9 | 2018-06-07 17:52:27 -0700 | [diff] [blame] | 47 | return nullptr; | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 48 | } | 
|  | 49 |  | 
|  | 50 | BBinder* IBinder::localBinder() | 
|  | 51 | { | 
| Yi Kong | fdd8da9 | 2018-06-07 17:52:27 -0700 | [diff] [blame] | 52 | return nullptr; | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 53 | } | 
|  | 54 |  | 
|  | 55 | BpBinder* IBinder::remoteBinder() | 
|  | 56 | { | 
| Yi Kong | fdd8da9 | 2018-06-07 17:52:27 -0700 | [diff] [blame] | 57 | return nullptr; | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 58 | } | 
|  | 59 |  | 
|  | 60 | bool IBinder::checkSubclass(const void* /*subclassID*/) const | 
|  | 61 | { | 
|  | 62 | return false; | 
|  | 63 | } | 
|  | 64 |  | 
| Dianne Hackborn | 23eb1e2 | 2015-10-07 17:35:27 -0700 | [diff] [blame] | 65 |  | 
| Dianne Hackborn | f2bf93b | 2015-10-14 15:13:02 -0700 | [diff] [blame] | 66 | 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] | 67 | Vector<String16>& args, const sp<IShellCallback>& callback, | 
|  | 68 | const sp<IResultReceiver>& resultReceiver) | 
| Dianne Hackborn | 23eb1e2 | 2015-10-07 17:35:27 -0700 | [diff] [blame] | 69 | { | 
| Dianne Hackborn | f2bf93b | 2015-10-14 15:13:02 -0700 | [diff] [blame] | 70 | Parcel send; | 
|  | 71 | Parcel reply; | 
|  | 72 | send.writeFileDescriptor(in); | 
|  | 73 | send.writeFileDescriptor(out); | 
|  | 74 | send.writeFileDescriptor(err); | 
|  | 75 | const size_t numArgs = args.size(); | 
|  | 76 | send.writeInt32(numArgs); | 
|  | 77 | for (size_t i = 0; i < numArgs; i++) { | 
|  | 78 | send.writeString16(args[i]); | 
| Dianne Hackborn | 23eb1e2 | 2015-10-07 17:35:27 -0700 | [diff] [blame] | 79 | } | 
| Yi Kong | fdd8da9 | 2018-06-07 17:52:27 -0700 | [diff] [blame] | 80 | send.writeStrongBinder(callback != nullptr ? IInterface::asBinder(callback) : nullptr); | 
|  | 81 | send.writeStrongBinder(resultReceiver != nullptr ? IInterface::asBinder(resultReceiver) : nullptr); | 
| Dianne Hackborn | f2bf93b | 2015-10-14 15:13:02 -0700 | [diff] [blame] | 82 | return target->transact(SHELL_COMMAND_TRANSACTION, send, &reply); | 
| Dianne Hackborn | 23eb1e2 | 2015-10-07 17:35:27 -0700 | [diff] [blame] | 83 | } | 
|  | 84 |  | 
| Steven Moreland | b8ad08d | 2019-08-09 14:42:56 -0700 | [diff] [blame] | 85 | status_t IBinder::getExtension(sp<IBinder>* out) { | 
|  | 86 | BBinder* local = this->localBinder(); | 
|  | 87 | if (local != nullptr) { | 
|  | 88 | *out = local->getExtension(); | 
|  | 89 | return OK; | 
|  | 90 | } | 
|  | 91 |  | 
|  | 92 | BpBinder* proxy = this->remoteBinder(); | 
|  | 93 | LOG_ALWAYS_FATAL_IF(proxy == nullptr); | 
|  | 94 |  | 
|  | 95 | Parcel data; | 
|  | 96 | Parcel reply; | 
|  | 97 | status_t status = transact(EXTENSION_TRANSACTION, data, &reply); | 
|  | 98 | if (status != OK) return status; | 
|  | 99 |  | 
|  | 100 | return reply.readNullableStrongBinder(out); | 
|  | 101 | } | 
|  | 102 |  | 
| Steven Moreland | 86080b8 | 2019-09-23 15:41:18 -0700 | [diff] [blame] | 103 | status_t IBinder::getDebugPid(pid_t* out) { | 
|  | 104 | BBinder* local = this->localBinder(); | 
|  | 105 | if (local != nullptr) { | 
|  | 106 | *out = local->getDebugPid(); | 
|  | 107 | return OK; | 
|  | 108 | } | 
|  | 109 |  | 
|  | 110 | BpBinder* proxy = this->remoteBinder(); | 
|  | 111 | LOG_ALWAYS_FATAL_IF(proxy == nullptr); | 
|  | 112 |  | 
|  | 113 | Parcel data; | 
|  | 114 | Parcel reply; | 
|  | 115 | status_t status = transact(DEBUG_PID_TRANSACTION, data, &reply); | 
|  | 116 | if (status != OK) return status; | 
|  | 117 |  | 
|  | 118 | int32_t pid; | 
|  | 119 | status = reply.readInt32(&pid); | 
|  | 120 | if (status != OK) return status; | 
|  | 121 |  | 
|  | 122 | if (pid < 0 || pid > std::numeric_limits<pid_t>::max()) { | 
|  | 123 | return BAD_VALUE; | 
|  | 124 | } | 
|  | 125 | *out = pid; | 
|  | 126 | return OK; | 
|  | 127 | } | 
|  | 128 |  | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 129 | // --------------------------------------------------------------------------- | 
|  | 130 |  | 
|  | 131 | class BBinder::Extras | 
|  | 132 | { | 
|  | 133 | public: | 
| Steven Moreland | f021200 | 2018-12-26 13:59:23 -0800 | [diff] [blame] | 134 | // unlocked objects | 
|  | 135 | bool mRequestingSid = false; | 
| Steven Moreland | b8ad08d | 2019-08-09 14:42:56 -0700 | [diff] [blame] | 136 | sp<IBinder> mExtension; | 
| Steven Moreland | bf1915b | 2020-07-16 22:43:02 +0000 | [diff] [blame] | 137 | int mPolicy = SCHED_NORMAL; | 
|  | 138 | int mPriority = 0; | 
| Steven Moreland | f021200 | 2018-12-26 13:59:23 -0800 | [diff] [blame] | 139 |  | 
|  | 140 | // for below objects | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 141 | Mutex mLock; | 
|  | 142 | BpBinder::ObjectManager mObjects; | 
|  | 143 | }; | 
|  | 144 |  | 
|  | 145 | // --------------------------------------------------------------------------- | 
|  | 146 |  | 
| Steven Moreland | a7fb018 | 2020-02-26 16:02:08 -0800 | [diff] [blame] | 147 | BBinder::BBinder() : mExtras(nullptr), mStability(0) | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 148 | { | 
|  | 149 | } | 
|  | 150 |  | 
|  | 151 | bool BBinder::isBinderAlive() const | 
|  | 152 | { | 
|  | 153 | return true; | 
|  | 154 | } | 
|  | 155 |  | 
|  | 156 | status_t BBinder::pingBinder() | 
|  | 157 | { | 
|  | 158 | return NO_ERROR; | 
|  | 159 | } | 
|  | 160 |  | 
| Mathias Agopian | 83c0446 | 2009-05-22 19:00:22 -0700 | [diff] [blame] | 161 | const String16& BBinder::getInterfaceDescriptor() const | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 162 | { | 
| Dan Egnor | 386a332 | 2010-05-06 00:55:09 -0700 | [diff] [blame] | 163 | // This is a local static rather than a global static, | 
|  | 164 | // to avoid static initializer ordering issues. | 
|  | 165 | static String16 sEmptyDescriptor; | 
| Steve Block | 32397c1 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 166 | ALOGW("reached BBinder::getInterfaceDescriptor (this=%p)", this); | 
| Mathias Agopian | 83c0446 | 2009-05-22 19:00:22 -0700 | [diff] [blame] | 167 | return sEmptyDescriptor; | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 168 | } | 
|  | 169 |  | 
| Jiyong Park | b86c866 | 2018-10-29 23:01:57 +0900 | [diff] [blame] | 170 | // NOLINTNEXTLINE(google-default-arguments) | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 171 | status_t BBinder::transact( | 
|  | 172 | uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags) | 
|  | 173 | { | 
|  | 174 | data.setDataPosition(0); | 
|  | 175 |  | 
| Steven Moreland | f183fdd | 2020-10-27 00:12:12 +0000 | [diff] [blame] | 176 | if (reply != nullptr && (flags & FLAG_CLEAR_BUF)) { | 
|  | 177 | reply->markSensitive(); | 
|  | 178 | } | 
|  | 179 |  | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 180 | status_t err = NO_ERROR; | 
|  | 181 | switch (code) { | 
|  | 182 | case PING_TRANSACTION: | 
| Steven Moreland | 6e69d65 | 2019-07-10 14:17:55 -0700 | [diff] [blame] | 183 | err = pingBinder(); | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 184 | break; | 
| Steven Moreland | b8ad08d | 2019-08-09 14:42:56 -0700 | [diff] [blame] | 185 | case EXTENSION_TRANSACTION: | 
|  | 186 | err = reply->writeStrongBinder(getExtension()); | 
|  | 187 | break; | 
| Steven Moreland | 86080b8 | 2019-09-23 15:41:18 -0700 | [diff] [blame] | 188 | case DEBUG_PID_TRANSACTION: | 
|  | 189 | err = reply->writeInt32(getDebugPid()); | 
|  | 190 | break; | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 191 | default: | 
|  | 192 | err = onTransact(code, data, reply, flags); | 
|  | 193 | break; | 
|  | 194 | } | 
|  | 195 |  | 
| Steven Moreland | a86a356 | 2019-08-01 23:28:34 +0000 | [diff] [blame] | 196 | // In case this is being transacted on in the same process. | 
| Yi Kong | fdd8da9 | 2018-06-07 17:52:27 -0700 | [diff] [blame] | 197 | if (reply != nullptr) { | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 198 | reply->setDataPosition(0); | 
|  | 199 | } | 
|  | 200 |  | 
|  | 201 | return err; | 
|  | 202 | } | 
|  | 203 |  | 
| Jiyong Park | b86c866 | 2018-10-29 23:01:57 +0900 | [diff] [blame] | 204 | // NOLINTNEXTLINE(google-default-arguments) | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 205 | status_t BBinder::linkToDeath( | 
| Colin Cross | 6f4f3ab | 2014-02-05 17:42:44 -0800 | [diff] [blame] | 206 | const sp<DeathRecipient>& /*recipient*/, void* /*cookie*/, | 
|  | 207 | uint32_t /*flags*/) | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 208 | { | 
|  | 209 | return INVALID_OPERATION; | 
|  | 210 | } | 
|  | 211 |  | 
| Jiyong Park | b86c866 | 2018-10-29 23:01:57 +0900 | [diff] [blame] | 212 | // NOLINTNEXTLINE(google-default-arguments) | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 213 | status_t BBinder::unlinkToDeath( | 
| Colin Cross | 6f4f3ab | 2014-02-05 17:42:44 -0800 | [diff] [blame] | 214 | const wp<DeathRecipient>& /*recipient*/, void* /*cookie*/, | 
|  | 215 | uint32_t /*flags*/, wp<DeathRecipient>* /*outRecipient*/) | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 216 | { | 
|  | 217 | return INVALID_OPERATION; | 
|  | 218 | } | 
|  | 219 |  | 
| Dianne Hackborn | 23eb1e2 | 2015-10-07 17:35:27 -0700 | [diff] [blame] | 220 | 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] | 221 | { | 
|  | 222 | return NO_ERROR; | 
|  | 223 | } | 
|  | 224 |  | 
|  | 225 | void BBinder::attachObject( | 
|  | 226 | const void* objectID, void* object, void* cleanupCookie, | 
|  | 227 | object_cleanup_func func) | 
|  | 228 | { | 
| Steven Moreland | f021200 | 2018-12-26 13:59:23 -0800 | [diff] [blame] | 229 | Extras* e = getOrCreateExtras(); | 
|  | 230 | if (!e) return; // out of memory | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 231 |  | 
|  | 232 | AutoMutex _l(e->mLock); | 
|  | 233 | e->mObjects.attach(objectID, object, cleanupCookie, func); | 
|  | 234 | } | 
|  | 235 |  | 
|  | 236 | void* BBinder::findObject(const void* objectID) const | 
|  | 237 | { | 
| Bailey Forrest | 6913c46 | 2015-08-18 17:15:10 -0700 | [diff] [blame] | 238 | Extras* e = mExtras.load(std::memory_order_acquire); | 
| Yi Kong | fdd8da9 | 2018-06-07 17:52:27 -0700 | [diff] [blame] | 239 | if (!e) return nullptr; | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 240 |  | 
|  | 241 | AutoMutex _l(e->mLock); | 
|  | 242 | return e->mObjects.find(objectID); | 
|  | 243 | } | 
|  | 244 |  | 
|  | 245 | void BBinder::detachObject(const void* objectID) | 
|  | 246 | { | 
| Bailey Forrest | 6913c46 | 2015-08-18 17:15:10 -0700 | [diff] [blame] | 247 | Extras* e = mExtras.load(std::memory_order_acquire); | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 248 | if (!e) return; | 
|  | 249 |  | 
|  | 250 | AutoMutex _l(e->mLock); | 
|  | 251 | e->mObjects.detach(objectID); | 
|  | 252 | } | 
|  | 253 |  | 
|  | 254 | BBinder* BBinder::localBinder() | 
|  | 255 | { | 
|  | 256 | return this; | 
|  | 257 | } | 
|  | 258 |  | 
| Steven Moreland | f021200 | 2018-12-26 13:59:23 -0800 | [diff] [blame] | 259 | bool BBinder::isRequestingSid() | 
|  | 260 | { | 
|  | 261 | Extras* e = mExtras.load(std::memory_order_acquire); | 
|  | 262 |  | 
|  | 263 | return e && e->mRequestingSid; | 
|  | 264 | } | 
|  | 265 |  | 
|  | 266 | void BBinder::setRequestingSid(bool requestingSid) | 
|  | 267 | { | 
|  | 268 | Extras* e = mExtras.load(std::memory_order_acquire); | 
|  | 269 |  | 
|  | 270 | if (!e) { | 
|  | 271 | // default is false. Most things don't need sids, so avoiding allocations when possible. | 
|  | 272 | if (!requestingSid) { | 
|  | 273 | return; | 
|  | 274 | } | 
|  | 275 |  | 
|  | 276 | e = getOrCreateExtras(); | 
|  | 277 | if (!e) return; // out of memory | 
|  | 278 | } | 
|  | 279 |  | 
| Steven Moreland | 3668be6 | 2019-02-08 17:56:55 -0800 | [diff] [blame] | 280 | e->mRequestingSid = requestingSid; | 
| Steven Moreland | f021200 | 2018-12-26 13:59:23 -0800 | [diff] [blame] | 281 | } | 
|  | 282 |  | 
| Steven Moreland | b8ad08d | 2019-08-09 14:42:56 -0700 | [diff] [blame] | 283 | sp<IBinder> BBinder::getExtension() { | 
|  | 284 | Extras* e = mExtras.load(std::memory_order_acquire); | 
|  | 285 | if (e == nullptr) return nullptr; | 
|  | 286 | return e->mExtension; | 
|  | 287 | } | 
|  | 288 |  | 
| Steven Moreland | bf1915b | 2020-07-16 22:43:02 +0000 | [diff] [blame] | 289 | void BBinder::setMinSchedulerPolicy(int policy, int priority) { | 
|  | 290 | switch (policy) { | 
|  | 291 | case SCHED_NORMAL: | 
|  | 292 | LOG_ALWAYS_FATAL_IF(priority < -20 || priority > 19, "Invalid priority for SCHED_NORMAL: %d", priority); | 
|  | 293 | break; | 
|  | 294 | case SCHED_RR: | 
|  | 295 | case SCHED_FIFO: | 
|  | 296 | LOG_ALWAYS_FATAL_IF(priority < 1 || priority > 99, "Invalid priority for sched %d: %d", policy, priority); | 
|  | 297 | break; | 
|  | 298 | default: | 
|  | 299 | LOG_ALWAYS_FATAL("Unrecognized scheduling policy: %d", policy); | 
|  | 300 | } | 
|  | 301 |  | 
|  | 302 | Extras* e = mExtras.load(std::memory_order_acquire); | 
|  | 303 |  | 
|  | 304 | if (e == nullptr) { | 
|  | 305 | // Avoid allocations if called with default. | 
|  | 306 | if (policy == SCHED_NORMAL && priority == 0) { | 
|  | 307 | return; | 
|  | 308 | } | 
|  | 309 |  | 
|  | 310 | e = getOrCreateExtras(); | 
|  | 311 | if (!e) return; // out of memory | 
|  | 312 | } | 
|  | 313 |  | 
|  | 314 | e->mPolicy = policy; | 
|  | 315 | e->mPriority = priority; | 
|  | 316 | } | 
|  | 317 |  | 
|  | 318 | int BBinder::getMinSchedulerPolicy() { | 
|  | 319 | Extras* e = mExtras.load(std::memory_order_acquire); | 
|  | 320 | if (e == nullptr) return SCHED_NORMAL; | 
|  | 321 | return e->mPolicy; | 
|  | 322 | } | 
|  | 323 |  | 
|  | 324 | int BBinder::getMinSchedulerPriority() { | 
|  | 325 | Extras* e = mExtras.load(std::memory_order_acquire); | 
|  | 326 | if (e == nullptr) return 0; | 
|  | 327 | return e->mPriority; | 
|  | 328 | } | 
|  | 329 |  | 
| Steven Moreland | 86080b8 | 2019-09-23 15:41:18 -0700 | [diff] [blame] | 330 | pid_t BBinder::getDebugPid() { | 
|  | 331 | return getpid(); | 
|  | 332 | } | 
|  | 333 |  | 
| Steven Moreland | b8ad08d | 2019-08-09 14:42:56 -0700 | [diff] [blame] | 334 | void BBinder::setExtension(const sp<IBinder>& extension) { | 
|  | 335 | Extras* e = getOrCreateExtras(); | 
|  | 336 | e->mExtension = extension; | 
|  | 337 | } | 
|  | 338 |  | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 339 | BBinder::~BBinder() | 
|  | 340 | { | 
| Bailey Forrest | 6913c46 | 2015-08-18 17:15:10 -0700 | [diff] [blame] | 341 | Extras* e = mExtras.load(std::memory_order_relaxed); | 
| Hans Boehm | 3effaba | 2014-08-12 22:56:00 +0000 | [diff] [blame] | 342 | if (e) delete e; | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 343 | } | 
|  | 344 |  | 
|  | 345 |  | 
| Jiyong Park | b86c866 | 2018-10-29 23:01:57 +0900 | [diff] [blame] | 346 | // NOLINTNEXTLINE(google-default-arguments) | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 347 | status_t BBinder::onTransact( | 
| Colin Cross | 6f4f3ab | 2014-02-05 17:42:44 -0800 | [diff] [blame] | 348 | 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] | 349 | { | 
|  | 350 | switch (code) { | 
|  | 351 | case INTERFACE_TRANSACTION: | 
|  | 352 | reply->writeString16(getInterfaceDescriptor()); | 
|  | 353 | return NO_ERROR; | 
|  | 354 |  | 
|  | 355 | case DUMP_TRANSACTION: { | 
|  | 356 | int fd = data.readFileDescriptor(); | 
|  | 357 | int argc = data.readInt32(); | 
|  | 358 | Vector<String16> args; | 
|  | 359 | for (int i = 0; i < argc && data.dataAvail() > 0; i++) { | 
|  | 360 | args.add(data.readString16()); | 
|  | 361 | } | 
|  | 362 | return dump(fd, args); | 
|  | 363 | } | 
| Dianne Hackborn | 555f89d | 2012-05-08 18:54:22 -0700 | [diff] [blame] | 364 |  | 
| Dianne Hackborn | 23eb1e2 | 2015-10-07 17:35:27 -0700 | [diff] [blame] | 365 | case SHELL_COMMAND_TRANSACTION: { | 
|  | 366 | int in = data.readFileDescriptor(); | 
|  | 367 | int out = data.readFileDescriptor(); | 
|  | 368 | int err = data.readFileDescriptor(); | 
|  | 369 | int argc = data.readInt32(); | 
|  | 370 | Vector<String16> args; | 
|  | 371 | for (int i = 0; i < argc && data.dataAvail() > 0; i++) { | 
|  | 372 | args.add(data.readString16()); | 
|  | 373 | } | 
| Dianne Hackborn | 1941a40 | 2016-08-29 12:30:43 -0700 | [diff] [blame] | 374 | sp<IShellCallback> shellCallback = IShellCallback::asInterface( | 
|  | 375 | data.readStrongBinder()); | 
| Dianne Hackborn | 23eb1e2 | 2015-10-07 17:35:27 -0700 | [diff] [blame] | 376 | sp<IResultReceiver> resultReceiver = IResultReceiver::asInterface( | 
|  | 377 | data.readStrongBinder()); | 
|  | 378 |  | 
| Dianne Hackborn | f2bf93b | 2015-10-14 15:13:02 -0700 | [diff] [blame] | 379 | // XXX can't add virtuals until binaries are updated. | 
|  | 380 | //return shellCommand(in, out, err, args, resultReceiver); | 
| Christopher Wiley | 0a9a1c1 | 2016-07-20 08:28:14 -0700 | [diff] [blame] | 381 | (void)in; | 
|  | 382 | (void)out; | 
|  | 383 | (void)err; | 
|  | 384 |  | 
| Yi Kong | fdd8da9 | 2018-06-07 17:52:27 -0700 | [diff] [blame] | 385 | if (resultReceiver != nullptr) { | 
| Dianne Hackborn | f2bf93b | 2015-10-14 15:13:02 -0700 | [diff] [blame] | 386 | resultReceiver->send(INVALID_OPERATION); | 
|  | 387 | } | 
| Martijn Coenen | aa6ee99 | 2017-08-17 15:38:08 +0200 | [diff] [blame] | 388 |  | 
|  | 389 | return NO_ERROR; | 
| Dianne Hackborn | 23eb1e2 | 2015-10-07 17:35:27 -0700 | [diff] [blame] | 390 | } | 
|  | 391 |  | 
| Dianne Hackborn | 555f89d | 2012-05-08 18:54:22 -0700 | [diff] [blame] | 392 | case SYSPROPS_TRANSACTION: { | 
|  | 393 | report_sysprop_change(); | 
|  | 394 | return NO_ERROR; | 
|  | 395 | } | 
|  | 396 |  | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 397 | default: | 
|  | 398 | return UNKNOWN_TRANSACTION; | 
|  | 399 | } | 
|  | 400 | } | 
|  | 401 |  | 
| Steven Moreland | f021200 | 2018-12-26 13:59:23 -0800 | [diff] [blame] | 402 | BBinder::Extras* BBinder::getOrCreateExtras() | 
|  | 403 | { | 
|  | 404 | Extras* e = mExtras.load(std::memory_order_acquire); | 
|  | 405 |  | 
|  | 406 | if (!e) { | 
|  | 407 | e = new Extras; | 
|  | 408 | Extras* expected = nullptr; | 
|  | 409 | if (!mExtras.compare_exchange_strong(expected, e, | 
|  | 410 | std::memory_order_release, | 
|  | 411 | std::memory_order_acquire)) { | 
|  | 412 | delete e; | 
|  | 413 | e = expected;  // Filled in by CAS | 
|  | 414 | } | 
|  | 415 | if (e == nullptr) return nullptr; // out of memory | 
|  | 416 | } | 
|  | 417 |  | 
|  | 418 | return e; | 
|  | 419 | } | 
|  | 420 |  | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 421 | // --------------------------------------------------------------------------- | 
|  | 422 |  | 
|  | 423 | enum { | 
|  | 424 | // This is used to transfer ownership of the remote binder from | 
|  | 425 | // the BpRefBase object holding it (when it is constructed), to the | 
|  | 426 | // owner of the BpRefBase object when it first acquires that BpRefBase. | 
|  | 427 | kRemoteAcquired = 0x00000001 | 
|  | 428 | }; | 
|  | 429 |  | 
|  | 430 | BpRefBase::BpRefBase(const sp<IBinder>& o) | 
| Yi Kong | fdd8da9 | 2018-06-07 17:52:27 -0700 | [diff] [blame] | 431 | : mRemote(o.get()), mRefs(nullptr), mState(0) | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 432 | { | 
|  | 433 | extendObjectLifetime(OBJECT_LIFETIME_WEAK); | 
|  | 434 |  | 
|  | 435 | if (mRemote) { | 
|  | 436 | mRemote->incStrong(this);           // Removed on first IncStrong(). | 
|  | 437 | mRefs = mRemote->createWeak(this);  // Held for our entire lifetime. | 
|  | 438 | } | 
|  | 439 | } | 
|  | 440 |  | 
|  | 441 | BpRefBase::~BpRefBase() | 
|  | 442 | { | 
|  | 443 | if (mRemote) { | 
| Bailey Forrest | 6913c46 | 2015-08-18 17:15:10 -0700 | [diff] [blame] | 444 | if (!(mState.load(std::memory_order_relaxed)&kRemoteAcquired)) { | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 445 | mRemote->decStrong(this); | 
|  | 446 | } | 
|  | 447 | mRefs->decWeak(this); | 
|  | 448 | } | 
|  | 449 | } | 
|  | 450 |  | 
|  | 451 | void BpRefBase::onFirstRef() | 
|  | 452 | { | 
| Bailey Forrest | 6913c46 | 2015-08-18 17:15:10 -0700 | [diff] [blame] | 453 | mState.fetch_or(kRemoteAcquired, std::memory_order_relaxed); | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 454 | } | 
|  | 455 |  | 
| Colin Cross | 6f4f3ab | 2014-02-05 17:42:44 -0800 | [diff] [blame] | 456 | void BpRefBase::onLastStrongRef(const void* /*id*/) | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 457 | { | 
|  | 458 | if (mRemote) { | 
|  | 459 | mRemote->decStrong(this); | 
|  | 460 | } | 
|  | 461 | } | 
|  | 462 |  | 
| Colin Cross | 6f4f3ab | 2014-02-05 17:42:44 -0800 | [diff] [blame] | 463 | bool BpRefBase::onIncStrongAttempted(uint32_t /*flags*/, const void* /*id*/) | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 464 | { | 
|  | 465 | return mRemote ? mRefs->attemptIncStrong(this) : false; | 
|  | 466 | } | 
|  | 467 |  | 
|  | 468 | // --------------------------------------------------------------------------- | 
|  | 469 |  | 
| Steven Moreland | 61ff849 | 2019-09-26 16:05:45 -0700 | [diff] [blame] | 470 | } // namespace android |