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