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> |
Yifan Hong | 84bedeb | 2021-04-21 21:37:17 -0700 | [diff] [blame] | 20 | |
| 21 | #include <android-base/unique_fd.h> |
Mathias Agopian | c5b2c0b | 2009-05-19 19:08:10 -0700 | [diff] [blame] | 22 | #include <binder/BpBinder.h> |
| 23 | #include <binder/IInterface.h> |
Yifan Hong | 84bedeb | 2021-04-21 21:37:17 -0700 | [diff] [blame] | 24 | #include <binder/IPCThreadState.h> |
Dianne Hackborn | 23eb1e2 | 2015-10-07 17:35:27 -0700 | [diff] [blame] | 25 | #include <binder/IResultReceiver.h> |
Dianne Hackborn | 1941a40 | 2016-08-29 12:30:43 -0700 | [diff] [blame] | 26 | #include <binder/IShellCallback.h> |
Mathias Agopian | c5b2c0b | 2009-05-19 19:08:10 -0700 | [diff] [blame] | 27 | #include <binder/Parcel.h> |
Yifan Hong | 84bedeb | 2021-04-21 21:37:17 -0700 | [diff] [blame] | 28 | #include <binder/RpcServer.h> |
| 29 | #include <private/android_filesystem_config.h> |
| 30 | #include <utils/misc.h> |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 31 | |
Yifan Hong | 84bedeb | 2021-04-21 21:37:17 -0700 | [diff] [blame] | 32 | #include <inttypes.h> |
Steven Moreland | bf1915b | 2020-07-16 22:43:02 +0000 | [diff] [blame] | 33 | #include <linux/sched.h> |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 34 | #include <stdio.h> |
| 35 | |
Yifan Hong | 84bedeb | 2021-04-21 21:37:17 -0700 | [diff] [blame] | 36 | #include "RpcState.h" |
| 37 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 38 | namespace android { |
| 39 | |
Steven Moreland | 90c1f9a | 2021-05-03 18:27:24 +0000 | [diff] [blame] | 40 | // Service implementations inherit from BBinder and IBinder, and this is frozen |
| 41 | // in prebuilts. |
| 42 | #ifdef __LP64__ |
| 43 | static_assert(sizeof(IBinder) == 24); |
| 44 | static_assert(sizeof(BBinder) == 40); |
| 45 | #else |
| 46 | static_assert(sizeof(IBinder) == 12); |
| 47 | static_assert(sizeof(BBinder) == 20); |
| 48 | #endif |
| 49 | |
Yifan Hong | 84bedeb | 2021-04-21 21:37:17 -0700 | [diff] [blame] | 50 | #ifdef BINDER_RPC_DEV_SERVERS |
| 51 | constexpr const bool kEnableRpcDevServers = true; |
| 52 | #else |
| 53 | constexpr const bool kEnableRpcDevServers = false; |
| 54 | #endif |
| 55 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 56 | // --------------------------------------------------------------------------- |
| 57 | |
Mathias Agopian | 83c0446 | 2009-05-22 19:00:22 -0700 | [diff] [blame] | 58 | IBinder::IBinder() |
| 59 | : RefBase() |
| 60 | { |
| 61 | } |
| 62 | |
| 63 | IBinder::~IBinder() |
| 64 | { |
| 65 | } |
| 66 | |
| 67 | // --------------------------------------------------------------------------- |
| 68 | |
Colin Cross | 6f4f3ab | 2014-02-05 17:42:44 -0800 | [diff] [blame] | 69 | sp<IInterface> IBinder::queryLocalInterface(const String16& /*descriptor*/) |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 70 | { |
Yi Kong | fdd8da9 | 2018-06-07 17:52:27 -0700 | [diff] [blame] | 71 | return nullptr; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | BBinder* IBinder::localBinder() |
| 75 | { |
Yi Kong | fdd8da9 | 2018-06-07 17:52:27 -0700 | [diff] [blame] | 76 | return nullptr; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | BpBinder* IBinder::remoteBinder() |
| 80 | { |
Yi Kong | fdd8da9 | 2018-06-07 17:52:27 -0700 | [diff] [blame] | 81 | return nullptr; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | bool IBinder::checkSubclass(const void* /*subclassID*/) const |
| 85 | { |
| 86 | return false; |
| 87 | } |
| 88 | |
Dianne Hackborn | 23eb1e2 | 2015-10-07 17:35:27 -0700 | [diff] [blame] | 89 | |
Dianne Hackborn | f2bf93b | 2015-10-14 15:13:02 -0700 | [diff] [blame] | 90 | 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] | 91 | Vector<String16>& args, const sp<IShellCallback>& callback, |
| 92 | const sp<IResultReceiver>& resultReceiver) |
Dianne Hackborn | 23eb1e2 | 2015-10-07 17:35:27 -0700 | [diff] [blame] | 93 | { |
Dianne Hackborn | f2bf93b | 2015-10-14 15:13:02 -0700 | [diff] [blame] | 94 | Parcel send; |
| 95 | Parcel reply; |
| 96 | send.writeFileDescriptor(in); |
| 97 | send.writeFileDescriptor(out); |
| 98 | send.writeFileDescriptor(err); |
| 99 | const size_t numArgs = args.size(); |
| 100 | send.writeInt32(numArgs); |
| 101 | for (size_t i = 0; i < numArgs; i++) { |
| 102 | send.writeString16(args[i]); |
Dianne Hackborn | 23eb1e2 | 2015-10-07 17:35:27 -0700 | [diff] [blame] | 103 | } |
Yi Kong | fdd8da9 | 2018-06-07 17:52:27 -0700 | [diff] [blame] | 104 | send.writeStrongBinder(callback != nullptr ? IInterface::asBinder(callback) : nullptr); |
| 105 | send.writeStrongBinder(resultReceiver != nullptr ? IInterface::asBinder(resultReceiver) : nullptr); |
Dianne Hackborn | f2bf93b | 2015-10-14 15:13:02 -0700 | [diff] [blame] | 106 | return target->transact(SHELL_COMMAND_TRANSACTION, send, &reply); |
Dianne Hackborn | 23eb1e2 | 2015-10-07 17:35:27 -0700 | [diff] [blame] | 107 | } |
| 108 | |
Steven Moreland | b8ad08d | 2019-08-09 14:42:56 -0700 | [diff] [blame] | 109 | status_t IBinder::getExtension(sp<IBinder>* out) { |
| 110 | BBinder* local = this->localBinder(); |
| 111 | if (local != nullptr) { |
| 112 | *out = local->getExtension(); |
| 113 | return OK; |
| 114 | } |
| 115 | |
| 116 | BpBinder* proxy = this->remoteBinder(); |
| 117 | LOG_ALWAYS_FATAL_IF(proxy == nullptr); |
| 118 | |
| 119 | Parcel data; |
| 120 | Parcel reply; |
| 121 | status_t status = transact(EXTENSION_TRANSACTION, data, &reply); |
| 122 | if (status != OK) return status; |
| 123 | |
| 124 | return reply.readNullableStrongBinder(out); |
| 125 | } |
| 126 | |
Steven Moreland | 86080b8 | 2019-09-23 15:41:18 -0700 | [diff] [blame] | 127 | status_t IBinder::getDebugPid(pid_t* out) { |
| 128 | BBinder* local = this->localBinder(); |
| 129 | if (local != nullptr) { |
| 130 | *out = local->getDebugPid(); |
| 131 | return OK; |
| 132 | } |
| 133 | |
| 134 | BpBinder* proxy = this->remoteBinder(); |
| 135 | LOG_ALWAYS_FATAL_IF(proxy == nullptr); |
| 136 | |
| 137 | Parcel data; |
| 138 | Parcel reply; |
| 139 | status_t status = transact(DEBUG_PID_TRANSACTION, data, &reply); |
| 140 | if (status != OK) return status; |
| 141 | |
| 142 | int32_t pid; |
| 143 | status = reply.readInt32(&pid); |
| 144 | if (status != OK) return status; |
| 145 | |
| 146 | if (pid < 0 || pid > std::numeric_limits<pid_t>::max()) { |
| 147 | return BAD_VALUE; |
| 148 | } |
| 149 | *out = pid; |
| 150 | return OK; |
| 151 | } |
| 152 | |
Yifan Hong | 84bedeb | 2021-04-21 21:37:17 -0700 | [diff] [blame] | 153 | status_t IBinder::setRpcClientDebug(android::base::unique_fd socketFd, uint32_t maxRpcThreads) { |
| 154 | if constexpr (!kEnableRpcDevServers) { |
| 155 | ALOGW("setRpcClientDebug disallowed because RPC is not enabled"); |
| 156 | return INVALID_OPERATION; |
| 157 | } |
| 158 | |
| 159 | BBinder* local = this->localBinder(); |
| 160 | if (local != nullptr) { |
| 161 | return local->BBinder::setRpcClientDebug(std::move(socketFd), maxRpcThreads); |
| 162 | } |
| 163 | |
| 164 | BpBinder* proxy = this->remoteBinder(); |
| 165 | LOG_ALWAYS_FATAL_IF(proxy == nullptr, "binder object must be either local or remote"); |
| 166 | |
| 167 | Parcel data; |
| 168 | Parcel reply; |
| 169 | status_t status; |
| 170 | if (status = data.writeBool(socketFd.ok()); status != OK) return status; |
| 171 | if (socketFd.ok()) { |
| 172 | // writeUniqueFileDescriptor currently makes an unnecessary dup(). |
| 173 | status = data.writeFileDescriptor(socketFd.release(), true /* own */); |
| 174 | if (status != OK) return status; |
| 175 | } |
| 176 | if (status = data.writeUint32(maxRpcThreads); status != OK) return status; |
| 177 | return transact(SET_RPC_CLIENT_TRANSACTION, data, &reply); |
| 178 | } |
| 179 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 180 | // --------------------------------------------------------------------------- |
| 181 | |
| 182 | class BBinder::Extras |
| 183 | { |
| 184 | public: |
Steven Moreland | f021200 | 2018-12-26 13:59:23 -0800 | [diff] [blame] | 185 | // unlocked objects |
| 186 | bool mRequestingSid = false; |
Steven Moreland | cf03cf1 | 2020-12-04 02:58:40 +0000 | [diff] [blame] | 187 | bool mInheritRt = false; |
Steven Moreland | b8ad08d | 2019-08-09 14:42:56 -0700 | [diff] [blame] | 188 | sp<IBinder> mExtension; |
Steven Moreland | bf1915b | 2020-07-16 22:43:02 +0000 | [diff] [blame] | 189 | int mPolicy = SCHED_NORMAL; |
| 190 | int mPriority = 0; |
Steven Moreland | f021200 | 2018-12-26 13:59:23 -0800 | [diff] [blame] | 191 | |
| 192 | // for below objects |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 193 | Mutex mLock; |
Yifan Hong | 84bedeb | 2021-04-21 21:37:17 -0700 | [diff] [blame] | 194 | sp<RpcServer> mRpcServer; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 195 | BpBinder::ObjectManager mObjects; |
| 196 | }; |
| 197 | |
| 198 | // --------------------------------------------------------------------------- |
| 199 | |
Steven Moreland | a7fb018 | 2020-02-26 16:02:08 -0800 | [diff] [blame] | 200 | BBinder::BBinder() : mExtras(nullptr), mStability(0) |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 201 | { |
| 202 | } |
| 203 | |
| 204 | bool BBinder::isBinderAlive() const |
| 205 | { |
| 206 | return true; |
| 207 | } |
| 208 | |
| 209 | status_t BBinder::pingBinder() |
| 210 | { |
| 211 | return NO_ERROR; |
| 212 | } |
| 213 | |
Mathias Agopian | 83c0446 | 2009-05-22 19:00:22 -0700 | [diff] [blame] | 214 | const String16& BBinder::getInterfaceDescriptor() const |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 215 | { |
Dan Egnor | 386a332 | 2010-05-06 00:55:09 -0700 | [diff] [blame] | 216 | // This is a local static rather than a global static, |
| 217 | // to avoid static initializer ordering issues. |
| 218 | static String16 sEmptyDescriptor; |
Steve Block | 32397c1 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 219 | ALOGW("reached BBinder::getInterfaceDescriptor (this=%p)", this); |
Mathias Agopian | 83c0446 | 2009-05-22 19:00:22 -0700 | [diff] [blame] | 220 | return sEmptyDescriptor; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 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::transact( |
| 225 | uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags) |
| 226 | { |
| 227 | data.setDataPosition(0); |
| 228 | |
Steven Moreland | f183fdd | 2020-10-27 00:12:12 +0000 | [diff] [blame] | 229 | if (reply != nullptr && (flags & FLAG_CLEAR_BUF)) { |
| 230 | reply->markSensitive(); |
| 231 | } |
| 232 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 233 | status_t err = NO_ERROR; |
| 234 | switch (code) { |
| 235 | case PING_TRANSACTION: |
Steven Moreland | 6e69d65 | 2019-07-10 14:17:55 -0700 | [diff] [blame] | 236 | err = pingBinder(); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 237 | break; |
Steven Moreland | b8ad08d | 2019-08-09 14:42:56 -0700 | [diff] [blame] | 238 | case EXTENSION_TRANSACTION: |
| 239 | err = reply->writeStrongBinder(getExtension()); |
| 240 | break; |
Steven Moreland | 86080b8 | 2019-09-23 15:41:18 -0700 | [diff] [blame] | 241 | case DEBUG_PID_TRANSACTION: |
| 242 | err = reply->writeInt32(getDebugPid()); |
| 243 | break; |
Yifan Hong | 84bedeb | 2021-04-21 21:37:17 -0700 | [diff] [blame] | 244 | case SET_RPC_CLIENT_TRANSACTION: { |
| 245 | err = setRpcClientDebug(data); |
| 246 | break; |
| 247 | } |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 248 | default: |
| 249 | err = onTransact(code, data, reply, flags); |
| 250 | break; |
| 251 | } |
| 252 | |
Steven Moreland | a86a356 | 2019-08-01 23:28:34 +0000 | [diff] [blame] | 253 | // In case this is being transacted on in the same process. |
Yi Kong | fdd8da9 | 2018-06-07 17:52:27 -0700 | [diff] [blame] | 254 | if (reply != nullptr) { |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 255 | reply->setDataPosition(0); |
| 256 | } |
| 257 | |
| 258 | return err; |
| 259 | } |
| 260 | |
Jiyong Park | b86c866 | 2018-10-29 23:01:57 +0900 | [diff] [blame] | 261 | // NOLINTNEXTLINE(google-default-arguments) |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 262 | status_t BBinder::linkToDeath( |
Colin Cross | 6f4f3ab | 2014-02-05 17:42:44 -0800 | [diff] [blame] | 263 | const sp<DeathRecipient>& /*recipient*/, void* /*cookie*/, |
| 264 | uint32_t /*flags*/) |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 265 | { |
| 266 | return INVALID_OPERATION; |
| 267 | } |
| 268 | |
Jiyong Park | b86c866 | 2018-10-29 23:01:57 +0900 | [diff] [blame] | 269 | // NOLINTNEXTLINE(google-default-arguments) |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 270 | status_t BBinder::unlinkToDeath( |
Colin Cross | 6f4f3ab | 2014-02-05 17:42:44 -0800 | [diff] [blame] | 271 | const wp<DeathRecipient>& /*recipient*/, void* /*cookie*/, |
| 272 | uint32_t /*flags*/, wp<DeathRecipient>* /*outRecipient*/) |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 273 | { |
| 274 | return INVALID_OPERATION; |
| 275 | } |
| 276 | |
Dianne Hackborn | 23eb1e2 | 2015-10-07 17:35:27 -0700 | [diff] [blame] | 277 | 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] | 278 | { |
| 279 | return NO_ERROR; |
| 280 | } |
| 281 | |
| 282 | void BBinder::attachObject( |
| 283 | const void* objectID, void* object, void* cleanupCookie, |
| 284 | object_cleanup_func func) |
| 285 | { |
Steven Moreland | f021200 | 2018-12-26 13:59:23 -0800 | [diff] [blame] | 286 | Extras* e = getOrCreateExtras(); |
| 287 | if (!e) return; // out of memory |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 288 | |
| 289 | AutoMutex _l(e->mLock); |
| 290 | e->mObjects.attach(objectID, object, cleanupCookie, func); |
| 291 | } |
| 292 | |
| 293 | void* BBinder::findObject(const void* objectID) const |
| 294 | { |
Bailey Forrest | 6913c46 | 2015-08-18 17:15:10 -0700 | [diff] [blame] | 295 | Extras* e = mExtras.load(std::memory_order_acquire); |
Yi Kong | fdd8da9 | 2018-06-07 17:52:27 -0700 | [diff] [blame] | 296 | if (!e) return nullptr; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 297 | |
| 298 | AutoMutex _l(e->mLock); |
| 299 | return e->mObjects.find(objectID); |
| 300 | } |
| 301 | |
| 302 | void BBinder::detachObject(const void* objectID) |
| 303 | { |
Bailey Forrest | 6913c46 | 2015-08-18 17:15:10 -0700 | [diff] [blame] | 304 | Extras* e = mExtras.load(std::memory_order_acquire); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 305 | if (!e) return; |
| 306 | |
| 307 | AutoMutex _l(e->mLock); |
| 308 | e->mObjects.detach(objectID); |
| 309 | } |
| 310 | |
| 311 | BBinder* BBinder::localBinder() |
| 312 | { |
| 313 | return this; |
| 314 | } |
| 315 | |
Steven Moreland | f021200 | 2018-12-26 13:59:23 -0800 | [diff] [blame] | 316 | bool BBinder::isRequestingSid() |
| 317 | { |
| 318 | Extras* e = mExtras.load(std::memory_order_acquire); |
| 319 | |
| 320 | return e && e->mRequestingSid; |
| 321 | } |
| 322 | |
| 323 | void BBinder::setRequestingSid(bool requestingSid) |
| 324 | { |
| 325 | Extras* e = mExtras.load(std::memory_order_acquire); |
| 326 | |
| 327 | if (!e) { |
| 328 | // default is false. Most things don't need sids, so avoiding allocations when possible. |
| 329 | if (!requestingSid) { |
| 330 | return; |
| 331 | } |
| 332 | |
| 333 | e = getOrCreateExtras(); |
| 334 | if (!e) return; // out of memory |
| 335 | } |
| 336 | |
Steven Moreland | 3668be6 | 2019-02-08 17:56:55 -0800 | [diff] [blame] | 337 | e->mRequestingSid = requestingSid; |
Steven Moreland | f021200 | 2018-12-26 13:59:23 -0800 | [diff] [blame] | 338 | } |
| 339 | |
Steven Moreland | b8ad08d | 2019-08-09 14:42:56 -0700 | [diff] [blame] | 340 | sp<IBinder> BBinder::getExtension() { |
| 341 | Extras* e = mExtras.load(std::memory_order_acquire); |
| 342 | if (e == nullptr) return nullptr; |
| 343 | return e->mExtension; |
| 344 | } |
| 345 | |
Steven Moreland | bf1915b | 2020-07-16 22:43:02 +0000 | [diff] [blame] | 346 | void BBinder::setMinSchedulerPolicy(int policy, int priority) { |
| 347 | switch (policy) { |
| 348 | case SCHED_NORMAL: |
| 349 | LOG_ALWAYS_FATAL_IF(priority < -20 || priority > 19, "Invalid priority for SCHED_NORMAL: %d", priority); |
| 350 | break; |
| 351 | case SCHED_RR: |
| 352 | case SCHED_FIFO: |
| 353 | LOG_ALWAYS_FATAL_IF(priority < 1 || priority > 99, "Invalid priority for sched %d: %d", policy, priority); |
| 354 | break; |
| 355 | default: |
| 356 | LOG_ALWAYS_FATAL("Unrecognized scheduling policy: %d", policy); |
| 357 | } |
| 358 | |
| 359 | Extras* e = mExtras.load(std::memory_order_acquire); |
| 360 | |
| 361 | if (e == nullptr) { |
| 362 | // Avoid allocations if called with default. |
| 363 | if (policy == SCHED_NORMAL && priority == 0) { |
| 364 | return; |
| 365 | } |
| 366 | |
| 367 | e = getOrCreateExtras(); |
| 368 | if (!e) return; // out of memory |
| 369 | } |
| 370 | |
| 371 | e->mPolicy = policy; |
| 372 | e->mPriority = priority; |
| 373 | } |
| 374 | |
| 375 | int BBinder::getMinSchedulerPolicy() { |
| 376 | Extras* e = mExtras.load(std::memory_order_acquire); |
| 377 | if (e == nullptr) return SCHED_NORMAL; |
| 378 | return e->mPolicy; |
| 379 | } |
| 380 | |
| 381 | int BBinder::getMinSchedulerPriority() { |
| 382 | Extras* e = mExtras.load(std::memory_order_acquire); |
| 383 | if (e == nullptr) return 0; |
| 384 | return e->mPriority; |
| 385 | } |
| 386 | |
Steven Moreland | cf03cf1 | 2020-12-04 02:58:40 +0000 | [diff] [blame] | 387 | bool BBinder::isInheritRt() { |
| 388 | Extras* e = mExtras.load(std::memory_order_acquire); |
| 389 | |
| 390 | return e && e->mInheritRt; |
| 391 | } |
| 392 | |
| 393 | void BBinder::setInheritRt(bool inheritRt) { |
| 394 | Extras* e = mExtras.load(std::memory_order_acquire); |
| 395 | |
| 396 | if (!e) { |
| 397 | if (!inheritRt) { |
| 398 | return; |
| 399 | } |
| 400 | |
| 401 | e = getOrCreateExtras(); |
| 402 | if (!e) return; // out of memory |
| 403 | } |
| 404 | |
| 405 | e->mInheritRt = inheritRt; |
| 406 | } |
| 407 | |
Steven Moreland | 86080b8 | 2019-09-23 15:41:18 -0700 | [diff] [blame] | 408 | pid_t BBinder::getDebugPid() { |
| 409 | return getpid(); |
| 410 | } |
| 411 | |
Steven Moreland | b8ad08d | 2019-08-09 14:42:56 -0700 | [diff] [blame] | 412 | void BBinder::setExtension(const sp<IBinder>& extension) { |
| 413 | Extras* e = getOrCreateExtras(); |
| 414 | e->mExtension = extension; |
| 415 | } |
| 416 | |
Yifan Hong | 84bedeb | 2021-04-21 21:37:17 -0700 | [diff] [blame] | 417 | status_t BBinder::setRpcClientDebug(const Parcel& data) { |
| 418 | if constexpr (!kEnableRpcDevServers) { |
| 419 | ALOGW("%s: disallowed because RPC is not enabled", __PRETTY_FUNCTION__); |
| 420 | return INVALID_OPERATION; |
| 421 | } |
| 422 | uid_t uid = IPCThreadState::self()->getCallingUid(); |
| 423 | if (uid != AID_ROOT) { |
| 424 | ALOGE("%s: not allowed because client %" PRIu32 " is not root", __PRETTY_FUNCTION__, uid); |
| 425 | return PERMISSION_DENIED; |
| 426 | } |
| 427 | status_t status; |
| 428 | bool hasSocketFd; |
| 429 | android::base::unique_fd clientFd; |
| 430 | uint32_t maxRpcThreads; |
| 431 | |
| 432 | if (status = data.readBool(&hasSocketFd); status != OK) return status; |
| 433 | if (hasSocketFd) { |
| 434 | if (status = data.readUniqueFileDescriptor(&clientFd); status != OK) return status; |
| 435 | } |
| 436 | if (status = data.readUint32(&maxRpcThreads); status != OK) return status; |
| 437 | |
| 438 | return setRpcClientDebug(std::move(clientFd), maxRpcThreads); |
| 439 | } |
| 440 | |
| 441 | status_t BBinder::setRpcClientDebug(android::base::unique_fd socketFd, uint32_t maxRpcThreads) { |
| 442 | if constexpr (!kEnableRpcDevServers) { |
| 443 | ALOGW("%s: disallowed because RPC is not enabled", __PRETTY_FUNCTION__); |
| 444 | return INVALID_OPERATION; |
| 445 | } |
| 446 | |
| 447 | const int socketFdForPrint = socketFd.get(); |
| 448 | LOG_RPC_DETAIL("%s(%d, %" PRIu32 ")", __PRETTY_FUNCTION__, socketFdForPrint, maxRpcThreads); |
| 449 | |
| 450 | if (!socketFd.ok()) { |
| 451 | ALOGE("%s: No socket FD provided.", __PRETTY_FUNCTION__); |
| 452 | return BAD_VALUE; |
| 453 | } |
| 454 | if (maxRpcThreads <= 0) { |
| 455 | ALOGE("%s: RPC is useless with %" PRIu32 " threads.", __PRETTY_FUNCTION__, maxRpcThreads); |
| 456 | return BAD_VALUE; |
| 457 | } |
| 458 | |
| 459 | // TODO(b/182914638): RPC and binder should share the same thread pool count. |
| 460 | size_t binderThreadPoolMaxCount = ProcessState::self()->getThreadPoolMaxThreadCount(); |
| 461 | if (binderThreadPoolMaxCount <= 1) { |
| 462 | ALOGE("%s: ProcessState thread pool max count is %zu. RPC is disabled for this service " |
| 463 | "because RPC requires the service to support multithreading.", |
| 464 | __PRETTY_FUNCTION__, binderThreadPoolMaxCount); |
| 465 | return INVALID_OPERATION; |
| 466 | } |
| 467 | |
| 468 | Extras* e = getOrCreateExtras(); |
| 469 | AutoMutex _l(e->mLock); |
| 470 | if (e->mRpcServer != nullptr) { |
| 471 | ALOGE("%s: Already have RPC client", __PRETTY_FUNCTION__); |
| 472 | return ALREADY_EXISTS; |
| 473 | } |
| 474 | e->mRpcServer = RpcServer::make(); |
| 475 | LOG_ALWAYS_FATAL_IF(e->mRpcServer == nullptr, "RpcServer::make returns null"); |
| 476 | e->mRpcServer->iUnderstandThisCodeIsExperimentalAndIWillNotUseItInProduction(); |
| 477 | // Weak ref to avoid circular dependency: BBinder -> RpcServer -X-> BBinder |
| 478 | e->mRpcServer->setRootObjectWeak(wp<BBinder>::fromExisting(this)); |
| 479 | e->mRpcServer->setupExternalServer(std::move(socketFd)); |
| 480 | e->mRpcServer->start(); |
| 481 | LOG_RPC_DETAIL("%s(%d, %" PRIu32 ") successful", __PRETTY_FUNCTION__, socketFdForPrint, |
| 482 | maxRpcThreads); |
| 483 | return OK; |
| 484 | } |
| 485 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 486 | BBinder::~BBinder() |
| 487 | { |
Bailey Forrest | 6913c46 | 2015-08-18 17:15:10 -0700 | [diff] [blame] | 488 | Extras* e = mExtras.load(std::memory_order_relaxed); |
Hans Boehm | 3effaba | 2014-08-12 22:56:00 +0000 | [diff] [blame] | 489 | if (e) delete e; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 490 | } |
| 491 | |
| 492 | |
Jiyong Park | b86c866 | 2018-10-29 23:01:57 +0900 | [diff] [blame] | 493 | // NOLINTNEXTLINE(google-default-arguments) |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 494 | status_t BBinder::onTransact( |
Colin Cross | 6f4f3ab | 2014-02-05 17:42:44 -0800 | [diff] [blame] | 495 | 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] | 496 | { |
| 497 | switch (code) { |
| 498 | case INTERFACE_TRANSACTION: |
| 499 | reply->writeString16(getInterfaceDescriptor()); |
| 500 | return NO_ERROR; |
| 501 | |
| 502 | case DUMP_TRANSACTION: { |
| 503 | int fd = data.readFileDescriptor(); |
| 504 | int argc = data.readInt32(); |
| 505 | Vector<String16> args; |
| 506 | for (int i = 0; i < argc && data.dataAvail() > 0; i++) { |
| 507 | args.add(data.readString16()); |
| 508 | } |
| 509 | return dump(fd, args); |
| 510 | } |
Dianne Hackborn | 555f89d | 2012-05-08 18:54:22 -0700 | [diff] [blame] | 511 | |
Dianne Hackborn | 23eb1e2 | 2015-10-07 17:35:27 -0700 | [diff] [blame] | 512 | case SHELL_COMMAND_TRANSACTION: { |
| 513 | int in = data.readFileDescriptor(); |
| 514 | int out = data.readFileDescriptor(); |
| 515 | int err = data.readFileDescriptor(); |
| 516 | int argc = data.readInt32(); |
| 517 | Vector<String16> args; |
| 518 | for (int i = 0; i < argc && data.dataAvail() > 0; i++) { |
| 519 | args.add(data.readString16()); |
| 520 | } |
Dianne Hackborn | 1941a40 | 2016-08-29 12:30:43 -0700 | [diff] [blame] | 521 | sp<IShellCallback> shellCallback = IShellCallback::asInterface( |
| 522 | data.readStrongBinder()); |
Dianne Hackborn | 23eb1e2 | 2015-10-07 17:35:27 -0700 | [diff] [blame] | 523 | sp<IResultReceiver> resultReceiver = IResultReceiver::asInterface( |
| 524 | data.readStrongBinder()); |
| 525 | |
Dianne Hackborn | f2bf93b | 2015-10-14 15:13:02 -0700 | [diff] [blame] | 526 | // XXX can't add virtuals until binaries are updated. |
| 527 | //return shellCommand(in, out, err, args, resultReceiver); |
Christopher Wiley | 0a9a1c1 | 2016-07-20 08:28:14 -0700 | [diff] [blame] | 528 | (void)in; |
| 529 | (void)out; |
| 530 | (void)err; |
| 531 | |
Yi Kong | fdd8da9 | 2018-06-07 17:52:27 -0700 | [diff] [blame] | 532 | if (resultReceiver != nullptr) { |
Dianne Hackborn | f2bf93b | 2015-10-14 15:13:02 -0700 | [diff] [blame] | 533 | resultReceiver->send(INVALID_OPERATION); |
| 534 | } |
Martijn Coenen | aa6ee99 | 2017-08-17 15:38:08 +0200 | [diff] [blame] | 535 | |
| 536 | return NO_ERROR; |
Dianne Hackborn | 23eb1e2 | 2015-10-07 17:35:27 -0700 | [diff] [blame] | 537 | } |
| 538 | |
Dianne Hackborn | 555f89d | 2012-05-08 18:54:22 -0700 | [diff] [blame] | 539 | case SYSPROPS_TRANSACTION: { |
| 540 | report_sysprop_change(); |
| 541 | return NO_ERROR; |
| 542 | } |
| 543 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 544 | default: |
| 545 | return UNKNOWN_TRANSACTION; |
| 546 | } |
| 547 | } |
| 548 | |
Steven Moreland | f021200 | 2018-12-26 13:59:23 -0800 | [diff] [blame] | 549 | BBinder::Extras* BBinder::getOrCreateExtras() |
| 550 | { |
| 551 | Extras* e = mExtras.load(std::memory_order_acquire); |
| 552 | |
| 553 | if (!e) { |
| 554 | e = new Extras; |
| 555 | Extras* expected = nullptr; |
| 556 | if (!mExtras.compare_exchange_strong(expected, e, |
| 557 | std::memory_order_release, |
| 558 | std::memory_order_acquire)) { |
| 559 | delete e; |
| 560 | e = expected; // Filled in by CAS |
| 561 | } |
| 562 | if (e == nullptr) return nullptr; // out of memory |
| 563 | } |
| 564 | |
| 565 | return e; |
| 566 | } |
| 567 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 568 | // --------------------------------------------------------------------------- |
| 569 | |
| 570 | enum { |
| 571 | // This is used to transfer ownership of the remote binder from |
| 572 | // the BpRefBase object holding it (when it is constructed), to the |
| 573 | // owner of the BpRefBase object when it first acquires that BpRefBase. |
| 574 | kRemoteAcquired = 0x00000001 |
| 575 | }; |
| 576 | |
| 577 | BpRefBase::BpRefBase(const sp<IBinder>& o) |
Yi Kong | fdd8da9 | 2018-06-07 17:52:27 -0700 | [diff] [blame] | 578 | : mRemote(o.get()), mRefs(nullptr), mState(0) |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 579 | { |
| 580 | extendObjectLifetime(OBJECT_LIFETIME_WEAK); |
| 581 | |
| 582 | if (mRemote) { |
| 583 | mRemote->incStrong(this); // Removed on first IncStrong(). |
| 584 | mRefs = mRemote->createWeak(this); // Held for our entire lifetime. |
| 585 | } |
| 586 | } |
| 587 | |
| 588 | BpRefBase::~BpRefBase() |
| 589 | { |
| 590 | if (mRemote) { |
Bailey Forrest | 6913c46 | 2015-08-18 17:15:10 -0700 | [diff] [blame] | 591 | if (!(mState.load(std::memory_order_relaxed)&kRemoteAcquired)) { |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 592 | mRemote->decStrong(this); |
| 593 | } |
| 594 | mRefs->decWeak(this); |
| 595 | } |
| 596 | } |
| 597 | |
| 598 | void BpRefBase::onFirstRef() |
| 599 | { |
Bailey Forrest | 6913c46 | 2015-08-18 17:15:10 -0700 | [diff] [blame] | 600 | mState.fetch_or(kRemoteAcquired, std::memory_order_relaxed); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 601 | } |
| 602 | |
Colin Cross | 6f4f3ab | 2014-02-05 17:42:44 -0800 | [diff] [blame] | 603 | void BpRefBase::onLastStrongRef(const void* /*id*/) |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 604 | { |
| 605 | if (mRemote) { |
| 606 | mRemote->decStrong(this); |
| 607 | } |
| 608 | } |
| 609 | |
Colin Cross | 6f4f3ab | 2014-02-05 17:42:44 -0800 | [diff] [blame] | 610 | bool BpRefBase::onIncStrongAttempted(uint32_t /*flags*/, const void* /*id*/) |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 611 | { |
| 612 | return mRemote ? mRefs->attemptIncStrong(this) : false; |
| 613 | } |
| 614 | |
| 615 | // --------------------------------------------------------------------------- |
| 616 | |
Steven Moreland | 61ff849 | 2019-09-26 16:05:45 -0700 | [diff] [blame] | 617 | } // namespace android |