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