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 | 8b89085 | 2021-06-10 13:44:09 -0700 | [diff] [blame] | 20 | #include <set> |
Yifan Hong | 84bedeb | 2021-04-21 21:37:17 -0700 | [diff] [blame] | 21 | |
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> |
Fabián Cañas | 356a022 | 2023-01-13 16:32:53 -0500 | [diff] [blame] | 28 | #include <binder/RecordedTransaction.h> |
Yifan Hong | 84bedeb | 2021-04-21 21:37:17 -0700 | [diff] [blame] | 29 | #include <binder/RpcServer.h> |
Tomasz Wasilczyk | 639490b | 2023-11-01 13:49:41 -0700 | [diff] [blame] | 30 | #include <binder/unique_fd.h> |
Sahil Somani | 2522b07 | 2022-08-08 14:07:47 -0700 | [diff] [blame] | 31 | #include <pthread.h> |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 32 | |
Yifan Hong | 84bedeb | 2021-04-21 21:37:17 -0700 | [diff] [blame] | 33 | #include <inttypes.h> |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 34 | #include <stdio.h> |
| 35 | |
Andrei Homescu | f55d688 | 2022-04-30 00:50:20 +0000 | [diff] [blame] | 36 | #ifdef __linux__ |
| 37 | #include <linux/sched.h> |
| 38 | #endif |
| 39 | |
Steven Moreland | 3215028 | 2021-11-12 22:54:53 +0000 | [diff] [blame] | 40 | #include "BuildFlags.h" |
Steven Moreland | 54fa6c7 | 2025-01-28 01:02:15 +0000 | [diff] [blame^] | 41 | #include "Constants.h" |
Tomasz Wasilczyk | 346f104 | 2023-10-09 17:02:46 +0000 | [diff] [blame] | 42 | #include "OS.h" |
Yifan Hong | 84bedeb | 2021-04-21 21:37:17 -0700 | [diff] [blame] | 43 | #include "RpcState.h" |
| 44 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 45 | namespace android { |
| 46 | |
Tomasz Wasilczyk | 639490b | 2023-11-01 13:49:41 -0700 | [diff] [blame] | 47 | using android::binder::unique_fd; |
| 48 | |
Tomasz Wasilczyk | b8ebcca | 2023-10-09 19:54:20 +0000 | [diff] [blame] | 49 | constexpr uid_t kUidRoot = 0; |
| 50 | |
Steven Moreland | 90c1f9a | 2021-05-03 18:27:24 +0000 | [diff] [blame] | 51 | // Service implementations inherit from BBinder and IBinder, and this is frozen |
| 52 | // in prebuilts. |
| 53 | #ifdef __LP64__ |
| 54 | static_assert(sizeof(IBinder) == 24); |
| 55 | static_assert(sizeof(BBinder) == 40); |
| 56 | #else |
| 57 | static_assert(sizeof(IBinder) == 12); |
| 58 | static_assert(sizeof(BBinder) == 20); |
| 59 | #endif |
| 60 | |
Steven Moreland | d9a7400 | 2022-06-02 00:03:18 +0000 | [diff] [blame] | 61 | // global b/c b/230079120 - consistent symbol table |
Yifan Hong | 84bedeb | 2021-04-21 21:37:17 -0700 | [diff] [blame] | 62 | #ifdef BINDER_RPC_DEV_SERVERS |
Steven Moreland | 5255c97 | 2023-05-19 22:53:41 +0000 | [diff] [blame] | 63 | constexpr bool kEnableRpcDevServers = true; |
Yifan Hong | 84bedeb | 2021-04-21 21:37:17 -0700 | [diff] [blame] | 64 | #else |
Steven Moreland | 5255c97 | 2023-05-19 22:53:41 +0000 | [diff] [blame] | 65 | constexpr bool kEnableRpcDevServers = false; |
Yifan Hong | 84bedeb | 2021-04-21 21:37:17 -0700 | [diff] [blame] | 66 | #endif |
| 67 | |
Sahil Somani | 2522b07 | 2022-08-08 14:07:47 -0700 | [diff] [blame] | 68 | #ifdef BINDER_ENABLE_RECORDING |
Steven Moreland | 5255c97 | 2023-05-19 22:53:41 +0000 | [diff] [blame] | 69 | constexpr bool kEnableRecording = true; |
Sahil Somani | 2522b07 | 2022-08-08 14:07:47 -0700 | [diff] [blame] | 70 | #else |
Steven Moreland | 5255c97 | 2023-05-19 22:53:41 +0000 | [diff] [blame] | 71 | constexpr bool kEnableRecording = false; |
Sahil Somani | 2522b07 | 2022-08-08 14:07:47 -0700 | [diff] [blame] | 72 | #endif |
| 73 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 74 | // --------------------------------------------------------------------------- |
| 75 | |
Mathias Agopian | 83c0446 | 2009-05-22 19:00:22 -0700 | [diff] [blame] | 76 | IBinder::IBinder() |
| 77 | : RefBase() |
| 78 | { |
| 79 | } |
| 80 | |
| 81 | IBinder::~IBinder() |
| 82 | { |
| 83 | } |
| 84 | |
| 85 | // --------------------------------------------------------------------------- |
| 86 | |
Colin Cross | 6f4f3ab | 2014-02-05 17:42:44 -0800 | [diff] [blame] | 87 | sp<IInterface> IBinder::queryLocalInterface(const String16& /*descriptor*/) |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 88 | { |
Yi Kong | fdd8da9 | 2018-06-07 17:52:27 -0700 | [diff] [blame] | 89 | return nullptr; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | BBinder* IBinder::localBinder() |
| 93 | { |
Yi Kong | fdd8da9 | 2018-06-07 17:52:27 -0700 | [diff] [blame] | 94 | return nullptr; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | BpBinder* IBinder::remoteBinder() |
| 98 | { |
Yi Kong | fdd8da9 | 2018-06-07 17:52:27 -0700 | [diff] [blame] | 99 | return nullptr; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | bool IBinder::checkSubclass(const void* /*subclassID*/) const |
| 103 | { |
| 104 | return false; |
| 105 | } |
| 106 | |
Dianne Hackborn | 23eb1e2 | 2015-10-07 17:35:27 -0700 | [diff] [blame] | 107 | |
Dianne Hackborn | f2bf93b | 2015-10-14 15:13:02 -0700 | [diff] [blame] | 108 | 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] | 109 | Vector<String16>& args, const sp<IShellCallback>& callback, |
| 110 | const sp<IResultReceiver>& resultReceiver) |
Dianne Hackborn | 23eb1e2 | 2015-10-07 17:35:27 -0700 | [diff] [blame] | 111 | { |
Dianne Hackborn | f2bf93b | 2015-10-14 15:13:02 -0700 | [diff] [blame] | 112 | Parcel send; |
| 113 | Parcel reply; |
| 114 | send.writeFileDescriptor(in); |
| 115 | send.writeFileDescriptor(out); |
| 116 | send.writeFileDescriptor(err); |
| 117 | const size_t numArgs = args.size(); |
| 118 | send.writeInt32(numArgs); |
| 119 | for (size_t i = 0; i < numArgs; i++) { |
| 120 | send.writeString16(args[i]); |
Dianne Hackborn | 23eb1e2 | 2015-10-07 17:35:27 -0700 | [diff] [blame] | 121 | } |
Yi Kong | fdd8da9 | 2018-06-07 17:52:27 -0700 | [diff] [blame] | 122 | send.writeStrongBinder(callback != nullptr ? IInterface::asBinder(callback) : nullptr); |
| 123 | send.writeStrongBinder(resultReceiver != nullptr ? IInterface::asBinder(resultReceiver) : nullptr); |
Dianne Hackborn | f2bf93b | 2015-10-14 15:13:02 -0700 | [diff] [blame] | 124 | return target->transact(SHELL_COMMAND_TRANSACTION, send, &reply); |
Dianne Hackborn | 23eb1e2 | 2015-10-07 17:35:27 -0700 | [diff] [blame] | 125 | } |
| 126 | |
Steven Moreland | b8ad08d | 2019-08-09 14:42:56 -0700 | [diff] [blame] | 127 | status_t IBinder::getExtension(sp<IBinder>* out) { |
| 128 | BBinder* local = this->localBinder(); |
| 129 | if (local != nullptr) { |
| 130 | *out = local->getExtension(); |
| 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(EXTENSION_TRANSACTION, data, &reply); |
| 140 | if (status != OK) return status; |
| 141 | |
| 142 | return reply.readNullableStrongBinder(out); |
| 143 | } |
| 144 | |
Yu-Ting Tseng | d5fc446 | 2024-04-30 15:07:13 -0700 | [diff] [blame] | 145 | status_t IBinder::addFrozenStateChangeCallback(const wp<FrozenStateChangeCallback>& callback) { |
| 146 | BpBinder* proxy = this->remoteBinder(); |
| 147 | if (proxy != nullptr) { |
| 148 | return proxy->addFrozenStateChangeCallback(callback); |
| 149 | } |
| 150 | return INVALID_OPERATION; |
| 151 | } |
| 152 | |
| 153 | status_t IBinder::removeFrozenStateChangeCallback(const wp<FrozenStateChangeCallback>& callback) { |
| 154 | BpBinder* proxy = this->remoteBinder(); |
| 155 | if (proxy != nullptr) { |
| 156 | return proxy->removeFrozenStateChangeCallback(callback); |
| 157 | } |
| 158 | return INVALID_OPERATION; |
| 159 | } |
| 160 | |
Steven Moreland | 86080b8 | 2019-09-23 15:41:18 -0700 | [diff] [blame] | 161 | status_t IBinder::getDebugPid(pid_t* out) { |
| 162 | BBinder* local = this->localBinder(); |
| 163 | if (local != nullptr) { |
| 164 | *out = local->getDebugPid(); |
| 165 | return OK; |
| 166 | } |
| 167 | |
| 168 | BpBinder* proxy = this->remoteBinder(); |
| 169 | LOG_ALWAYS_FATAL_IF(proxy == nullptr); |
| 170 | |
| 171 | Parcel data; |
| 172 | Parcel reply; |
| 173 | status_t status = transact(DEBUG_PID_TRANSACTION, data, &reply); |
| 174 | if (status != OK) return status; |
| 175 | |
| 176 | int32_t pid; |
| 177 | status = reply.readInt32(&pid); |
| 178 | if (status != OK) return status; |
| 179 | |
| 180 | if (pid < 0 || pid > std::numeric_limits<pid_t>::max()) { |
| 181 | return BAD_VALUE; |
| 182 | } |
| 183 | *out = pid; |
| 184 | return OK; |
| 185 | } |
| 186 | |
Tomasz Wasilczyk | 639490b | 2023-11-01 13:49:41 -0700 | [diff] [blame] | 187 | status_t IBinder::setRpcClientDebug(unique_fd socketFd, const sp<IBinder>& keepAliveBinder) { |
Steven Moreland | d9a7400 | 2022-06-02 00:03:18 +0000 | [diff] [blame] | 188 | if (!kEnableRpcDevServers) { |
Yifan Hong | 84bedeb | 2021-04-21 21:37:17 -0700 | [diff] [blame] | 189 | ALOGW("setRpcClientDebug disallowed because RPC is not enabled"); |
| 190 | return INVALID_OPERATION; |
| 191 | } |
Steven Moreland | 3215028 | 2021-11-12 22:54:53 +0000 | [diff] [blame] | 192 | if (!kEnableKernelIpc) { |
| 193 | ALOGW("setRpcClientDebug disallowed because kernel binder is not enabled"); |
| 194 | return INVALID_OPERATION; |
| 195 | } |
Yifan Hong | 84bedeb | 2021-04-21 21:37:17 -0700 | [diff] [blame] | 196 | |
| 197 | BBinder* local = this->localBinder(); |
| 198 | if (local != nullptr) { |
Yifan Hong | 02530ec | 2021-06-10 13:38:38 -0700 | [diff] [blame] | 199 | return local->BBinder::setRpcClientDebug(std::move(socketFd), keepAliveBinder); |
Yifan Hong | 84bedeb | 2021-04-21 21:37:17 -0700 | [diff] [blame] | 200 | } |
| 201 | |
| 202 | BpBinder* proxy = this->remoteBinder(); |
| 203 | LOG_ALWAYS_FATAL_IF(proxy == nullptr, "binder object must be either local or remote"); |
| 204 | |
| 205 | Parcel data; |
| 206 | Parcel reply; |
| 207 | status_t status; |
| 208 | if (status = data.writeBool(socketFd.ok()); status != OK) return status; |
| 209 | if (socketFd.ok()) { |
| 210 | // writeUniqueFileDescriptor currently makes an unnecessary dup(). |
| 211 | status = data.writeFileDescriptor(socketFd.release(), true /* own */); |
| 212 | if (status != OK) return status; |
| 213 | } |
Yifan Hong | 02530ec | 2021-06-10 13:38:38 -0700 | [diff] [blame] | 214 | if (status = data.writeStrongBinder(keepAliveBinder); status != OK) return status; |
Yifan Hong | 84bedeb | 2021-04-21 21:37:17 -0700 | [diff] [blame] | 215 | return transact(SET_RPC_CLIENT_TRANSACTION, data, &reply); |
| 216 | } |
| 217 | |
Steven Moreland | 9e759e8 | 2021-06-25 21:30:23 +0000 | [diff] [blame] | 218 | void IBinder::withLock(const std::function<void()>& doWithLock) { |
| 219 | BBinder* local = localBinder(); |
| 220 | if (local) { |
| 221 | local->withLock(doWithLock); |
| 222 | return; |
| 223 | } |
| 224 | BpBinder* proxy = this->remoteBinder(); |
| 225 | LOG_ALWAYS_FATAL_IF(proxy == nullptr, "binder object must be either local or remote"); |
| 226 | proxy->withLock(doWithLock); |
| 227 | } |
| 228 | |
Devin Moore | 3faaa00 | 2022-07-27 15:54:06 +0000 | [diff] [blame] | 229 | sp<IBinder> IBinder::lookupOrCreateWeak(const void* objectID, object_make_func make, |
| 230 | const void* makeArgs) { |
| 231 | BBinder* local = localBinder(); |
| 232 | if (local) { |
| 233 | return local->lookupOrCreateWeak(objectID, make, makeArgs); |
| 234 | } |
| 235 | BpBinder* proxy = this->remoteBinder(); |
| 236 | LOG_ALWAYS_FATAL_IF(proxy == nullptr, "binder object must be either local or remote"); |
| 237 | return proxy->lookupOrCreateWeak(objectID, make, makeArgs); |
| 238 | } |
| 239 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 240 | // --------------------------------------------------------------------------- |
| 241 | |
Yifan Hong | 8b89085 | 2021-06-10 13:44:09 -0700 | [diff] [blame] | 242 | class BBinder::RpcServerLink : public IBinder::DeathRecipient { |
| 243 | public: |
| 244 | // On binder died, calls RpcServer::shutdown on @a rpcServer, and removes itself from @a binder. |
| 245 | RpcServerLink(const sp<RpcServer>& rpcServer, const sp<IBinder>& keepAliveBinder, |
| 246 | const wp<BBinder>& binder) |
| 247 | : mRpcServer(rpcServer), mKeepAliveBinder(keepAliveBinder), mBinder(binder) {} |
Steven Moreland | d9a7400 | 2022-06-02 00:03:18 +0000 | [diff] [blame] | 248 | virtual ~RpcServerLink(); |
Yifan Hong | 8b89085 | 2021-06-10 13:44:09 -0700 | [diff] [blame] | 249 | void binderDied(const wp<IBinder>&) override { |
Steven Moreland | f6957b7 | 2022-09-24 03:12:16 +0000 | [diff] [blame] | 250 | auto promoted = mBinder.promote(); |
| 251 | ALOGI("RpcBinder: binder died, shutting down RpcServer for %s", |
| 252 | promoted ? String8(promoted->getInterfaceDescriptor()).c_str() : "<NULL>"); |
| 253 | |
Yifan Hong | 8b89085 | 2021-06-10 13:44:09 -0700 | [diff] [blame] | 254 | if (mRpcServer == nullptr) { |
| 255 | ALOGW("RpcServerLink: Unable to shut down RpcServer because it does not exist."); |
| 256 | } else { |
| 257 | ALOGW_IF(!mRpcServer->shutdown(), |
| 258 | "RpcServerLink: RpcServer did not shut down properly. Not started?"); |
| 259 | } |
| 260 | mRpcServer.clear(); |
| 261 | |
Steven Moreland | f6957b7 | 2022-09-24 03:12:16 +0000 | [diff] [blame] | 262 | if (promoted) { |
Yifan Hong | 8b89085 | 2021-06-10 13:44:09 -0700 | [diff] [blame] | 263 | promoted->removeRpcServerLink(sp<RpcServerLink>::fromExisting(this)); |
| 264 | } |
| 265 | mBinder.clear(); |
| 266 | } |
| 267 | |
| 268 | private: |
| 269 | sp<RpcServer> mRpcServer; |
| 270 | sp<IBinder> mKeepAliveBinder; // hold to avoid automatically unlinking |
| 271 | wp<BBinder> mBinder; |
| 272 | }; |
Steven Moreland | d9a7400 | 2022-06-02 00:03:18 +0000 | [diff] [blame] | 273 | BBinder::RpcServerLink::~RpcServerLink() {} |
Yifan Hong | 8b89085 | 2021-06-10 13:44:09 -0700 | [diff] [blame] | 274 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 275 | class BBinder::Extras |
| 276 | { |
| 277 | public: |
Steven Moreland | f021200 | 2018-12-26 13:59:23 -0800 | [diff] [blame] | 278 | // unlocked objects |
Steven Moreland | b8ad08d | 2019-08-09 14:42:56 -0700 | [diff] [blame] | 279 | sp<IBinder> mExtension; |
Andrei Homescu | f55d688 | 2022-04-30 00:50:20 +0000 | [diff] [blame] | 280 | #ifdef __linux__ |
Steven Moreland | bf1915b | 2020-07-16 22:43:02 +0000 | [diff] [blame] | 281 | int mPolicy = SCHED_NORMAL; |
| 282 | int mPriority = 0; |
Andrei Homescu | f55d688 | 2022-04-30 00:50:20 +0000 | [diff] [blame] | 283 | #endif |
| 284 | bool mRequestingSid = false; |
| 285 | bool mInheritRt = false; |
Steven Moreland | f021200 | 2018-12-26 13:59:23 -0800 | [diff] [blame] | 286 | |
| 287 | // for below objects |
Tomasz Wasilczyk | 66ee192 | 2023-10-26 14:53:49 -0700 | [diff] [blame] | 288 | RpcMutex mLock; |
Yifan Hong | 8b89085 | 2021-06-10 13:44:09 -0700 | [diff] [blame] | 289 | std::set<sp<RpcServerLink>> mRpcServerLinks; |
Devin Moore | 3fea46f | 2025-01-09 00:17:12 +0000 | [diff] [blame] | 290 | BpBinder::ObjectManager mObjectMgr; |
Sahil Somani | 2522b07 | 2022-08-08 14:07:47 -0700 | [diff] [blame] | 291 | |
Tomasz Wasilczyk | 639490b | 2023-11-01 13:49:41 -0700 | [diff] [blame] | 292 | unique_fd mRecordingFd; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 293 | }; |
| 294 | |
| 295 | // --------------------------------------------------------------------------- |
| 296 | |
Sahil Somani | 2522b07 | 2022-08-08 14:07:47 -0700 | [diff] [blame] | 297 | BBinder::BBinder() : mExtras(nullptr), mStability(0), mParceled(false), mRecordingOn(false) {} |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 298 | |
| 299 | bool BBinder::isBinderAlive() const |
| 300 | { |
| 301 | return true; |
| 302 | } |
| 303 | |
| 304 | status_t BBinder::pingBinder() |
| 305 | { |
| 306 | return NO_ERROR; |
| 307 | } |
| 308 | |
Sahil Somani | 2522b07 | 2022-08-08 14:07:47 -0700 | [diff] [blame] | 309 | status_t BBinder::startRecordingTransactions(const Parcel& data) { |
| 310 | if (!kEnableRecording) { |
| 311 | ALOGW("Binder recording disallowed because recording is not enabled"); |
| 312 | return INVALID_OPERATION; |
| 313 | } |
| 314 | if (!kEnableKernelIpc) { |
| 315 | ALOGW("Binder recording disallowed because kernel binder is not enabled"); |
| 316 | return INVALID_OPERATION; |
| 317 | } |
| 318 | uid_t uid = IPCThreadState::self()->getCallingUid(); |
Tomasz Wasilczyk | b8ebcca | 2023-10-09 19:54:20 +0000 | [diff] [blame] | 319 | if (uid != kUidRoot) { |
Sahil Somani | 2522b07 | 2022-08-08 14:07:47 -0700 | [diff] [blame] | 320 | ALOGE("Binder recording not allowed because client %" PRIu32 " is not root", uid); |
| 321 | return PERMISSION_DENIED; |
| 322 | } |
| 323 | Extras* e = getOrCreateExtras(); |
Tomasz Wasilczyk | 66ee192 | 2023-10-26 14:53:49 -0700 | [diff] [blame] | 324 | RpcMutexUniqueLock lock(e->mLock); |
Sahil Somani | 2522b07 | 2022-08-08 14:07:47 -0700 | [diff] [blame] | 325 | if (mRecordingOn) { |
Tomasz Wasilczyk | e3de880 | 2023-11-01 11:05:27 -0700 | [diff] [blame] | 326 | ALOGI("Could not start Binder recording. Another is already in progress."); |
Sahil Somani | 2522b07 | 2022-08-08 14:07:47 -0700 | [diff] [blame] | 327 | return INVALID_OPERATION; |
| 328 | } else { |
| 329 | status_t readStatus = data.readUniqueFileDescriptor(&(e->mRecordingFd)); |
| 330 | if (readStatus != OK) { |
| 331 | return readStatus; |
| 332 | } |
| 333 | mRecordingOn = true; |
Tomasz Wasilczyk | e3de880 | 2023-11-01 11:05:27 -0700 | [diff] [blame] | 334 | ALOGI("Started Binder recording."); |
Sahil Somani | 2522b07 | 2022-08-08 14:07:47 -0700 | [diff] [blame] | 335 | return NO_ERROR; |
| 336 | } |
| 337 | } |
| 338 | |
| 339 | status_t BBinder::stopRecordingTransactions() { |
| 340 | if (!kEnableRecording) { |
| 341 | ALOGW("Binder recording disallowed because recording is not enabled"); |
| 342 | return INVALID_OPERATION; |
| 343 | } |
| 344 | if (!kEnableKernelIpc) { |
| 345 | ALOGW("Binder recording disallowed because kernel binder is not enabled"); |
| 346 | return INVALID_OPERATION; |
| 347 | } |
| 348 | uid_t uid = IPCThreadState::self()->getCallingUid(); |
Tomasz Wasilczyk | b8ebcca | 2023-10-09 19:54:20 +0000 | [diff] [blame] | 349 | if (uid != kUidRoot) { |
Sahil Somani | 2522b07 | 2022-08-08 14:07:47 -0700 | [diff] [blame] | 350 | ALOGE("Binder recording not allowed because client %" PRIu32 " is not root", uid); |
| 351 | return PERMISSION_DENIED; |
| 352 | } |
| 353 | Extras* e = getOrCreateExtras(); |
Tomasz Wasilczyk | 66ee192 | 2023-10-26 14:53:49 -0700 | [diff] [blame] | 354 | RpcMutexUniqueLock lock(e->mLock); |
Sahil Somani | 2522b07 | 2022-08-08 14:07:47 -0700 | [diff] [blame] | 355 | if (mRecordingOn) { |
| 356 | e->mRecordingFd.reset(); |
| 357 | mRecordingOn = false; |
Tomasz Wasilczyk | e3de880 | 2023-11-01 11:05:27 -0700 | [diff] [blame] | 358 | ALOGI("Stopped Binder recording."); |
Sahil Somani | 2522b07 | 2022-08-08 14:07:47 -0700 | [diff] [blame] | 359 | return NO_ERROR; |
| 360 | } else { |
Tomasz Wasilczyk | e3de880 | 2023-11-01 11:05:27 -0700 | [diff] [blame] | 361 | ALOGI("Could not stop Binder recording. One is not in progress."); |
Sahil Somani | 2522b07 | 2022-08-08 14:07:47 -0700 | [diff] [blame] | 362 | return INVALID_OPERATION; |
| 363 | } |
| 364 | } |
| 365 | |
Mathias Agopian | 83c0446 | 2009-05-22 19:00:22 -0700 | [diff] [blame] | 366 | const String16& BBinder::getInterfaceDescriptor() const |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 367 | { |
Steven Moreland | 1ef0eb6 | 2022-08-18 20:09:30 +0000 | [diff] [blame] | 368 | static StaticString16 sBBinder(u"BBinder"); |
| 369 | ALOGW("Reached BBinder::getInterfaceDescriptor (this=%p). Override?", this); |
| 370 | return sBBinder; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 371 | } |
| 372 | |
Jiyong Park | b86c866 | 2018-10-29 23:01:57 +0900 | [diff] [blame] | 373 | // NOLINTNEXTLINE(google-default-arguments) |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 374 | status_t BBinder::transact( |
| 375 | uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags) |
| 376 | { |
| 377 | data.setDataPosition(0); |
| 378 | |
Steven Moreland | f183fdd | 2020-10-27 00:12:12 +0000 | [diff] [blame] | 379 | if (reply != nullptr && (flags & FLAG_CLEAR_BUF)) { |
| 380 | reply->markSensitive(); |
| 381 | } |
| 382 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 383 | status_t err = NO_ERROR; |
| 384 | switch (code) { |
| 385 | case PING_TRANSACTION: |
Steven Moreland | 6e69d65 | 2019-07-10 14:17:55 -0700 | [diff] [blame] | 386 | err = pingBinder(); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 387 | break; |
Sahil Somani | 2522b07 | 2022-08-08 14:07:47 -0700 | [diff] [blame] | 388 | case START_RECORDING_TRANSACTION: |
| 389 | err = startRecordingTransactions(data); |
| 390 | break; |
| 391 | case STOP_RECORDING_TRANSACTION: |
| 392 | err = stopRecordingTransactions(); |
| 393 | break; |
Steven Moreland | b8ad08d | 2019-08-09 14:42:56 -0700 | [diff] [blame] | 394 | case EXTENSION_TRANSACTION: |
Tomasz Wasilczyk | e3de880 | 2023-11-01 11:05:27 -0700 | [diff] [blame] | 395 | LOG_ALWAYS_FATAL_IF(reply == nullptr, "reply == nullptr"); |
Steven Moreland | b8ad08d | 2019-08-09 14:42:56 -0700 | [diff] [blame] | 396 | err = reply->writeStrongBinder(getExtension()); |
| 397 | break; |
Steven Moreland | 86080b8 | 2019-09-23 15:41:18 -0700 | [diff] [blame] | 398 | case DEBUG_PID_TRANSACTION: |
Tomasz Wasilczyk | e3de880 | 2023-11-01 11:05:27 -0700 | [diff] [blame] | 399 | LOG_ALWAYS_FATAL_IF(reply == nullptr, "reply == nullptr"); |
Steven Moreland | 86080b8 | 2019-09-23 15:41:18 -0700 | [diff] [blame] | 400 | err = reply->writeInt32(getDebugPid()); |
| 401 | break; |
Yifan Hong | 84bedeb | 2021-04-21 21:37:17 -0700 | [diff] [blame] | 402 | case SET_RPC_CLIENT_TRANSACTION: { |
| 403 | err = setRpcClientDebug(data); |
| 404 | break; |
| 405 | } |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 406 | default: |
| 407 | err = onTransact(code, data, reply, flags); |
| 408 | break; |
| 409 | } |
| 410 | |
Steven Moreland | a86a356 | 2019-08-01 23:28:34 +0000 | [diff] [blame] | 411 | // In case this is being transacted on in the same process. |
Yi Kong | fdd8da9 | 2018-06-07 17:52:27 -0700 | [diff] [blame] | 412 | if (reply != nullptr) { |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 413 | reply->setDataPosition(0); |
Steven Moreland | 54fa6c7 | 2025-01-28 01:02:15 +0000 | [diff] [blame^] | 414 | if (reply->dataSize() > binder::kLogTransactionsOverBytes) { |
Martijn Coenen | 1cad19c | 2021-10-04 09:19:01 +0200 | [diff] [blame] | 415 | ALOGW("Large reply transaction of %zu bytes, interface descriptor %s, code %d", |
| 416 | reply->dataSize(), String8(getInterfaceDescriptor()).c_str(), code); |
| 417 | } |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 418 | } |
| 419 | |
Tomasz Wasilczyk | 32746ae | 2023-10-09 19:57:58 +0000 | [diff] [blame] | 420 | if (kEnableKernelIpc && mRecordingOn && code != START_RECORDING_TRANSACTION) [[unlikely]] { |
Sahil Somani | 2522b07 | 2022-08-08 14:07:47 -0700 | [diff] [blame] | 421 | Extras* e = mExtras.load(std::memory_order_acquire); |
Tomasz Wasilczyk | 66ee192 | 2023-10-26 14:53:49 -0700 | [diff] [blame] | 422 | RpcMutexUniqueLock lock(e->mLock); |
Sahil Somani | 2522b07 | 2022-08-08 14:07:47 -0700 | [diff] [blame] | 423 | if (mRecordingOn) { |
| 424 | Parcel emptyReply; |
Fabián Cañas | 68dbcfe | 2023-01-06 15:37:39 -0500 | [diff] [blame] | 425 | timespec ts; |
| 426 | timespec_get(&ts, TIME_UTC); |
Fabián Cañas | 6c0c963 | 2023-01-13 15:21:16 -0500 | [diff] [blame] | 427 | auto transaction = android::binder::debug::RecordedTransaction:: |
| 428 | fromDetails(getInterfaceDescriptor(), code, flags, ts, data, |
| 429 | reply ? *reply : emptyReply, err); |
Sahil Somani | 2522b07 | 2022-08-08 14:07:47 -0700 | [diff] [blame] | 430 | if (transaction) { |
| 431 | if (status_t err = transaction->dumpToFile(e->mRecordingFd); err != NO_ERROR) { |
Tomasz Wasilczyk | e3de880 | 2023-11-01 11:05:27 -0700 | [diff] [blame] | 432 | ALOGI("Failed to dump RecordedTransaction to file with error %d", err); |
Sahil Somani | 2522b07 | 2022-08-08 14:07:47 -0700 | [diff] [blame] | 433 | } |
| 434 | } else { |
Tomasz Wasilczyk | e3de880 | 2023-11-01 11:05:27 -0700 | [diff] [blame] | 435 | ALOGI("Failed to create RecordedTransaction object."); |
Sahil Somani | 2522b07 | 2022-08-08 14:07:47 -0700 | [diff] [blame] | 436 | } |
| 437 | } |
| 438 | } |
| 439 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 440 | return err; |
| 441 | } |
| 442 | |
Jiyong Park | b86c866 | 2018-10-29 23:01:57 +0900 | [diff] [blame] | 443 | // NOLINTNEXTLINE(google-default-arguments) |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 444 | status_t BBinder::linkToDeath( |
Colin Cross | 6f4f3ab | 2014-02-05 17:42:44 -0800 | [diff] [blame] | 445 | const sp<DeathRecipient>& /*recipient*/, void* /*cookie*/, |
| 446 | uint32_t /*flags*/) |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 447 | { |
| 448 | return INVALID_OPERATION; |
| 449 | } |
| 450 | |
Jiyong Park | b86c866 | 2018-10-29 23:01:57 +0900 | [diff] [blame] | 451 | // NOLINTNEXTLINE(google-default-arguments) |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 452 | status_t BBinder::unlinkToDeath( |
Colin Cross | 6f4f3ab | 2014-02-05 17:42:44 -0800 | [diff] [blame] | 453 | const wp<DeathRecipient>& /*recipient*/, void* /*cookie*/, |
| 454 | uint32_t /*flags*/, wp<DeathRecipient>* /*outRecipient*/) |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 455 | { |
| 456 | return INVALID_OPERATION; |
| 457 | } |
| 458 | |
Dianne Hackborn | 23eb1e2 | 2015-10-07 17:35:27 -0700 | [diff] [blame] | 459 | 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] | 460 | { |
| 461 | return NO_ERROR; |
| 462 | } |
| 463 | |
Steven Moreland | 63a2d51 | 2021-06-25 01:10:15 +0000 | [diff] [blame] | 464 | void* BBinder::attachObject(const void* objectID, void* object, void* cleanupCookie, |
| 465 | object_cleanup_func func) { |
Steven Moreland | f021200 | 2018-12-26 13:59:23 -0800 | [diff] [blame] | 466 | Extras* e = getOrCreateExtras(); |
Steven Moreland | 5ce7ca5 | 2021-06-25 21:59:50 +0000 | [diff] [blame] | 467 | LOG_ALWAYS_FATAL_IF(!e, "no memory"); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 468 | |
Tomasz Wasilczyk | 66ee192 | 2023-10-26 14:53:49 -0700 | [diff] [blame] | 469 | RpcMutexUniqueLock _l(e->mLock); |
Devin Moore | 3fea46f | 2025-01-09 00:17:12 +0000 | [diff] [blame] | 470 | return e->mObjectMgr.attach(objectID, object, cleanupCookie, func); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 471 | } |
| 472 | |
| 473 | void* BBinder::findObject(const void* objectID) const |
| 474 | { |
Bailey Forrest | 6913c46 | 2015-08-18 17:15:10 -0700 | [diff] [blame] | 475 | Extras* e = mExtras.load(std::memory_order_acquire); |
Yi Kong | fdd8da9 | 2018-06-07 17:52:27 -0700 | [diff] [blame] | 476 | if (!e) return nullptr; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 477 | |
Tomasz Wasilczyk | 66ee192 | 2023-10-26 14:53:49 -0700 | [diff] [blame] | 478 | RpcMutexUniqueLock _l(e->mLock); |
Devin Moore | 3fea46f | 2025-01-09 00:17:12 +0000 | [diff] [blame] | 479 | return e->mObjectMgr.find(objectID); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 480 | } |
| 481 | |
Steven Moreland | 63a2d51 | 2021-06-25 01:10:15 +0000 | [diff] [blame] | 482 | void* BBinder::detachObject(const void* objectID) { |
Bailey Forrest | 6913c46 | 2015-08-18 17:15:10 -0700 | [diff] [blame] | 483 | Extras* e = mExtras.load(std::memory_order_acquire); |
Steven Moreland | 63a2d51 | 2021-06-25 01:10:15 +0000 | [diff] [blame] | 484 | if (!e) return nullptr; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 485 | |
Tomasz Wasilczyk | 66ee192 | 2023-10-26 14:53:49 -0700 | [diff] [blame] | 486 | RpcMutexUniqueLock _l(e->mLock); |
Devin Moore | 3fea46f | 2025-01-09 00:17:12 +0000 | [diff] [blame] | 487 | return e->mObjectMgr.detach(objectID); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 488 | } |
| 489 | |
Steven Moreland | 9e759e8 | 2021-06-25 21:30:23 +0000 | [diff] [blame] | 490 | void BBinder::withLock(const std::function<void()>& doWithLock) { |
| 491 | Extras* e = getOrCreateExtras(); |
| 492 | LOG_ALWAYS_FATAL_IF(!e, "no memory"); |
| 493 | |
Tomasz Wasilczyk | 66ee192 | 2023-10-26 14:53:49 -0700 | [diff] [blame] | 494 | RpcMutexUniqueLock _l(e->mLock); |
Steven Moreland | 9e759e8 | 2021-06-25 21:30:23 +0000 | [diff] [blame] | 495 | doWithLock(); |
| 496 | } |
| 497 | |
Devin Moore | 3faaa00 | 2022-07-27 15:54:06 +0000 | [diff] [blame] | 498 | sp<IBinder> BBinder::lookupOrCreateWeak(const void* objectID, object_make_func make, |
| 499 | const void* makeArgs) { |
| 500 | Extras* e = getOrCreateExtras(); |
| 501 | LOG_ALWAYS_FATAL_IF(!e, "no memory"); |
Tomasz Wasilczyk | 66ee192 | 2023-10-26 14:53:49 -0700 | [diff] [blame] | 502 | RpcMutexUniqueLock _l(e->mLock); |
Devin Moore | 3fea46f | 2025-01-09 00:17:12 +0000 | [diff] [blame] | 503 | return e->mObjectMgr.lookupOrCreateWeak(objectID, make, makeArgs); |
Devin Moore | 3faaa00 | 2022-07-27 15:54:06 +0000 | [diff] [blame] | 504 | } |
| 505 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 506 | BBinder* BBinder::localBinder() |
| 507 | { |
| 508 | return this; |
| 509 | } |
| 510 | |
Steven Moreland | f021200 | 2018-12-26 13:59:23 -0800 | [diff] [blame] | 511 | bool BBinder::isRequestingSid() |
| 512 | { |
| 513 | Extras* e = mExtras.load(std::memory_order_acquire); |
| 514 | |
| 515 | return e && e->mRequestingSid; |
| 516 | } |
| 517 | |
| 518 | void BBinder::setRequestingSid(bool requestingSid) |
| 519 | { |
Steven Moreland | d37e6dd | 2021-06-16 22:07:51 +0000 | [diff] [blame] | 520 | LOG_ALWAYS_FATAL_IF(mParceled, |
| 521 | "setRequestingSid() should not be called after a binder object " |
| 522 | "is parceled/sent to another process"); |
Kalesh Singh | d67c8e8 | 2020-12-29 15:46:25 -0500 | [diff] [blame] | 523 | |
Steven Moreland | f021200 | 2018-12-26 13:59:23 -0800 | [diff] [blame] | 524 | Extras* e = mExtras.load(std::memory_order_acquire); |
| 525 | |
| 526 | if (!e) { |
| 527 | // default is false. Most things don't need sids, so avoiding allocations when possible. |
| 528 | if (!requestingSid) { |
| 529 | return; |
| 530 | } |
| 531 | |
| 532 | e = getOrCreateExtras(); |
| 533 | if (!e) return; // out of memory |
| 534 | } |
| 535 | |
Steven Moreland | 3668be6 | 2019-02-08 17:56:55 -0800 | [diff] [blame] | 536 | e->mRequestingSid = requestingSid; |
Steven Moreland | f021200 | 2018-12-26 13:59:23 -0800 | [diff] [blame] | 537 | } |
| 538 | |
Steven Moreland | b8ad08d | 2019-08-09 14:42:56 -0700 | [diff] [blame] | 539 | sp<IBinder> BBinder::getExtension() { |
| 540 | Extras* e = mExtras.load(std::memory_order_acquire); |
| 541 | if (e == nullptr) return nullptr; |
| 542 | return e->mExtension; |
| 543 | } |
| 544 | |
Andrei Homescu | f55d688 | 2022-04-30 00:50:20 +0000 | [diff] [blame] | 545 | #ifdef __linux__ |
Steven Moreland | bf1915b | 2020-07-16 22:43:02 +0000 | [diff] [blame] | 546 | void BBinder::setMinSchedulerPolicy(int policy, int priority) { |
Steven Moreland | d37e6dd | 2021-06-16 22:07:51 +0000 | [diff] [blame] | 547 | LOG_ALWAYS_FATAL_IF(mParceled, |
| 548 | "setMinSchedulerPolicy() should not be called after a binder object " |
| 549 | "is parceled/sent to another process"); |
Kalesh Singh | d67c8e8 | 2020-12-29 15:46:25 -0500 | [diff] [blame] | 550 | |
Steven Moreland | bf1915b | 2020-07-16 22:43:02 +0000 | [diff] [blame] | 551 | switch (policy) { |
| 552 | case SCHED_NORMAL: |
| 553 | LOG_ALWAYS_FATAL_IF(priority < -20 || priority > 19, "Invalid priority for SCHED_NORMAL: %d", priority); |
| 554 | break; |
| 555 | case SCHED_RR: |
| 556 | case SCHED_FIFO: |
| 557 | LOG_ALWAYS_FATAL_IF(priority < 1 || priority > 99, "Invalid priority for sched %d: %d", policy, priority); |
| 558 | break; |
| 559 | default: |
| 560 | LOG_ALWAYS_FATAL("Unrecognized scheduling policy: %d", policy); |
| 561 | } |
| 562 | |
| 563 | Extras* e = mExtras.load(std::memory_order_acquire); |
| 564 | |
| 565 | if (e == nullptr) { |
| 566 | // Avoid allocations if called with default. |
| 567 | if (policy == SCHED_NORMAL && priority == 0) { |
| 568 | return; |
| 569 | } |
| 570 | |
| 571 | e = getOrCreateExtras(); |
| 572 | if (!e) return; // out of memory |
| 573 | } |
| 574 | |
| 575 | e->mPolicy = policy; |
| 576 | e->mPriority = priority; |
| 577 | } |
| 578 | |
| 579 | int BBinder::getMinSchedulerPolicy() { |
| 580 | Extras* e = mExtras.load(std::memory_order_acquire); |
| 581 | if (e == nullptr) return SCHED_NORMAL; |
| 582 | return e->mPolicy; |
| 583 | } |
| 584 | |
| 585 | int BBinder::getMinSchedulerPriority() { |
| 586 | Extras* e = mExtras.load(std::memory_order_acquire); |
| 587 | if (e == nullptr) return 0; |
| 588 | return e->mPriority; |
| 589 | } |
Andrei Homescu | f55d688 | 2022-04-30 00:50:20 +0000 | [diff] [blame] | 590 | #endif // __linux__ |
Steven Moreland | bf1915b | 2020-07-16 22:43:02 +0000 | [diff] [blame] | 591 | |
Steven Moreland | cf03cf1 | 2020-12-04 02:58:40 +0000 | [diff] [blame] | 592 | bool BBinder::isInheritRt() { |
| 593 | Extras* e = mExtras.load(std::memory_order_acquire); |
| 594 | |
| 595 | return e && e->mInheritRt; |
| 596 | } |
| 597 | |
| 598 | void BBinder::setInheritRt(bool inheritRt) { |
Steven Moreland | d37e6dd | 2021-06-16 22:07:51 +0000 | [diff] [blame] | 599 | LOG_ALWAYS_FATAL_IF(mParceled, |
| 600 | "setInheritRt() should not be called after a binder object " |
| 601 | "is parceled/sent to another process"); |
Kalesh Singh | d67c8e8 | 2020-12-29 15:46:25 -0500 | [diff] [blame] | 602 | |
Steven Moreland | cf03cf1 | 2020-12-04 02:58:40 +0000 | [diff] [blame] | 603 | Extras* e = mExtras.load(std::memory_order_acquire); |
| 604 | |
| 605 | if (!e) { |
| 606 | if (!inheritRt) { |
| 607 | return; |
| 608 | } |
| 609 | |
| 610 | e = getOrCreateExtras(); |
| 611 | if (!e) return; // out of memory |
| 612 | } |
| 613 | |
| 614 | e->mInheritRt = inheritRt; |
| 615 | } |
| 616 | |
Steven Moreland | 86080b8 | 2019-09-23 15:41:18 -0700 | [diff] [blame] | 617 | pid_t BBinder::getDebugPid() { |
Andrei Homescu | f55d688 | 2022-04-30 00:50:20 +0000 | [diff] [blame] | 618 | #ifdef __linux__ |
Steven Moreland | 86080b8 | 2019-09-23 15:41:18 -0700 | [diff] [blame] | 619 | return getpid(); |
Andrei Homescu | f55d688 | 2022-04-30 00:50:20 +0000 | [diff] [blame] | 620 | #else |
| 621 | // TODO: handle other OSes |
| 622 | return 0; |
| 623 | #endif // __linux__ |
Steven Moreland | 86080b8 | 2019-09-23 15:41:18 -0700 | [diff] [blame] | 624 | } |
| 625 | |
Steven Moreland | b8ad08d | 2019-08-09 14:42:56 -0700 | [diff] [blame] | 626 | void BBinder::setExtension(const sp<IBinder>& extension) { |
Steven Moreland | d37e6dd | 2021-06-16 22:07:51 +0000 | [diff] [blame] | 627 | LOG_ALWAYS_FATAL_IF(mParceled, |
| 628 | "setExtension() should not be called after a binder object " |
| 629 | "is parceled/sent to another process"); |
Kalesh Singh | d67c8e8 | 2020-12-29 15:46:25 -0500 | [diff] [blame] | 630 | |
Steven Moreland | b8ad08d | 2019-08-09 14:42:56 -0700 | [diff] [blame] | 631 | Extras* e = getOrCreateExtras(); |
| 632 | e->mExtension = extension; |
| 633 | } |
| 634 | |
Kalesh Singh | d67c8e8 | 2020-12-29 15:46:25 -0500 | [diff] [blame] | 635 | bool BBinder::wasParceled() { |
| 636 | return mParceled; |
| 637 | } |
| 638 | |
| 639 | void BBinder::setParceled() { |
| 640 | mParceled = true; |
| 641 | } |
| 642 | |
Yifan Hong | 84bedeb | 2021-04-21 21:37:17 -0700 | [diff] [blame] | 643 | status_t BBinder::setRpcClientDebug(const Parcel& data) { |
Steven Moreland | d9a7400 | 2022-06-02 00:03:18 +0000 | [diff] [blame] | 644 | if (!kEnableRpcDevServers) { |
Yifan Hong | 84bedeb | 2021-04-21 21:37:17 -0700 | [diff] [blame] | 645 | ALOGW("%s: disallowed because RPC is not enabled", __PRETTY_FUNCTION__); |
| 646 | return INVALID_OPERATION; |
| 647 | } |
Steven Moreland | 3215028 | 2021-11-12 22:54:53 +0000 | [diff] [blame] | 648 | if (!kEnableKernelIpc) { |
| 649 | ALOGW("setRpcClientDebug disallowed because kernel binder is not enabled"); |
| 650 | return INVALID_OPERATION; |
| 651 | } |
Yifan Hong | 84bedeb | 2021-04-21 21:37:17 -0700 | [diff] [blame] | 652 | uid_t uid = IPCThreadState::self()->getCallingUid(); |
Tomasz Wasilczyk | b8ebcca | 2023-10-09 19:54:20 +0000 | [diff] [blame] | 653 | if (uid != kUidRoot) { |
Yifan Hong | 84bedeb | 2021-04-21 21:37:17 -0700 | [diff] [blame] | 654 | ALOGE("%s: not allowed because client %" PRIu32 " is not root", __PRETTY_FUNCTION__, uid); |
| 655 | return PERMISSION_DENIED; |
| 656 | } |
| 657 | status_t status; |
| 658 | bool hasSocketFd; |
Tomasz Wasilczyk | 639490b | 2023-11-01 13:49:41 -0700 | [diff] [blame] | 659 | unique_fd clientFd; |
Yifan Hong | 84bedeb | 2021-04-21 21:37:17 -0700 | [diff] [blame] | 660 | |
| 661 | if (status = data.readBool(&hasSocketFd); status != OK) return status; |
| 662 | if (hasSocketFd) { |
| 663 | if (status = data.readUniqueFileDescriptor(&clientFd); status != OK) return status; |
| 664 | } |
Yifan Hong | 02530ec | 2021-06-10 13:38:38 -0700 | [diff] [blame] | 665 | sp<IBinder> keepAliveBinder; |
| 666 | if (status = data.readNullableStrongBinder(&keepAliveBinder); status != OK) return status; |
Yifan Hong | 84bedeb | 2021-04-21 21:37:17 -0700 | [diff] [blame] | 667 | |
Yifan Hong | 02530ec | 2021-06-10 13:38:38 -0700 | [diff] [blame] | 668 | return setRpcClientDebug(std::move(clientFd), keepAliveBinder); |
Yifan Hong | 84bedeb | 2021-04-21 21:37:17 -0700 | [diff] [blame] | 669 | } |
| 670 | |
Tomasz Wasilczyk | 639490b | 2023-11-01 13:49:41 -0700 | [diff] [blame] | 671 | status_t BBinder::setRpcClientDebug(unique_fd socketFd, const sp<IBinder>& keepAliveBinder) { |
Steven Moreland | d9a7400 | 2022-06-02 00:03:18 +0000 | [diff] [blame] | 672 | if (!kEnableRpcDevServers) { |
Yifan Hong | 84bedeb | 2021-04-21 21:37:17 -0700 | [diff] [blame] | 673 | ALOGW("%s: disallowed because RPC is not enabled", __PRETTY_FUNCTION__); |
| 674 | return INVALID_OPERATION; |
| 675 | } |
Steven Moreland | 3215028 | 2021-11-12 22:54:53 +0000 | [diff] [blame] | 676 | if (!kEnableKernelIpc) { |
| 677 | ALOGW("setRpcClientDebug disallowed because kernel binder is not enabled"); |
| 678 | return INVALID_OPERATION; |
| 679 | } |
Yifan Hong | 84bedeb | 2021-04-21 21:37:17 -0700 | [diff] [blame] | 680 | |
| 681 | const int socketFdForPrint = socketFd.get(); |
Yifan Hong | 3482323 | 2021-06-07 17:23:00 -0700 | [diff] [blame] | 682 | LOG_RPC_DETAIL("%s(fd=%d)", __PRETTY_FUNCTION__, socketFdForPrint); |
Yifan Hong | 84bedeb | 2021-04-21 21:37:17 -0700 | [diff] [blame] | 683 | |
| 684 | if (!socketFd.ok()) { |
| 685 | ALOGE("%s: No socket FD provided.", __PRETTY_FUNCTION__); |
| 686 | return BAD_VALUE; |
| 687 | } |
Yifan Hong | 84bedeb | 2021-04-21 21:37:17 -0700 | [diff] [blame] | 688 | |
Yifan Hong | 02530ec | 2021-06-10 13:38:38 -0700 | [diff] [blame] | 689 | if (keepAliveBinder == nullptr) { |
| 690 | ALOGE("%s: No keepAliveBinder provided.", __PRETTY_FUNCTION__); |
| 691 | return UNEXPECTED_NULL; |
| 692 | } |
| 693 | |
Elie Kheirallah | 47431c1 | 2022-04-21 23:46:17 +0000 | [diff] [blame] | 694 | size_t binderThreadPoolMaxCount = ProcessState::self()->getThreadPoolMaxTotalThreadCount(); |
Yifan Hong | 84bedeb | 2021-04-21 21:37:17 -0700 | [diff] [blame] | 695 | if (binderThreadPoolMaxCount <= 1) { |
| 696 | ALOGE("%s: ProcessState thread pool max count is %zu. RPC is disabled for this service " |
| 697 | "because RPC requires the service to support multithreading.", |
| 698 | __PRETTY_FUNCTION__, binderThreadPoolMaxCount); |
| 699 | return INVALID_OPERATION; |
| 700 | } |
| 701 | |
Yifan Hong | 8b89085 | 2021-06-10 13:44:09 -0700 | [diff] [blame] | 702 | // Weak ref to avoid circular dependency: |
| 703 | // BBinder -> RpcServerLink ----> RpcServer -X-> BBinder |
| 704 | // `-X-> BBinder |
| 705 | auto weakThis = wp<BBinder>::fromExisting(this); |
| 706 | |
Yifan Hong | 84bedeb | 2021-04-21 21:37:17 -0700 | [diff] [blame] | 707 | Extras* e = getOrCreateExtras(); |
Tomasz Wasilczyk | 66ee192 | 2023-10-26 14:53:49 -0700 | [diff] [blame] | 708 | RpcMutexUniqueLock _l(e->mLock); |
Yifan Hong | 8b89085 | 2021-06-10 13:44:09 -0700 | [diff] [blame] | 709 | auto rpcServer = RpcServer::make(); |
| 710 | LOG_ALWAYS_FATAL_IF(rpcServer == nullptr, "RpcServer::make returns null"); |
Yifan Hong | 8b89085 | 2021-06-10 13:44:09 -0700 | [diff] [blame] | 711 | auto link = sp<RpcServerLink>::make(rpcServer, keepAliveBinder, weakThis); |
| 712 | if (auto status = keepAliveBinder->linkToDeath(link, nullptr, 0); status != OK) { |
| 713 | ALOGE("%s: keepAliveBinder->linkToDeath returns %s", __PRETTY_FUNCTION__, |
| 714 | statusToString(status).c_str()); |
| 715 | return status; |
Yifan Hong | 84bedeb | 2021-04-21 21:37:17 -0700 | [diff] [blame] | 716 | } |
Yifan Hong | 8b89085 | 2021-06-10 13:44:09 -0700 | [diff] [blame] | 717 | rpcServer->setRootObjectWeak(weakThis); |
Steven Moreland | 2372f9d | 2021-08-05 15:42:01 -0700 | [diff] [blame] | 718 | if (auto status = rpcServer->setupExternalServer(std::move(socketFd)); status != OK) { |
| 719 | return status; |
| 720 | } |
Yifan Hong | 8b89085 | 2021-06-10 13:44:09 -0700 | [diff] [blame] | 721 | rpcServer->setMaxThreads(binderThreadPoolMaxCount); |
Tomasz Wasilczyk | e3de880 | 2023-11-01 11:05:27 -0700 | [diff] [blame] | 722 | ALOGI("RpcBinder: Started Binder debug on %s", String8(getInterfaceDescriptor()).c_str()); |
Yifan Hong | 8b89085 | 2021-06-10 13:44:09 -0700 | [diff] [blame] | 723 | rpcServer->start(); |
| 724 | e->mRpcServerLinks.emplace(link); |
Yifan Hong | 3482323 | 2021-06-07 17:23:00 -0700 | [diff] [blame] | 725 | LOG_RPC_DETAIL("%s(fd=%d) successful", __PRETTY_FUNCTION__, socketFdForPrint); |
Yifan Hong | 84bedeb | 2021-04-21 21:37:17 -0700 | [diff] [blame] | 726 | return OK; |
| 727 | } |
| 728 | |
Yifan Hong | 8b89085 | 2021-06-10 13:44:09 -0700 | [diff] [blame] | 729 | void BBinder::removeRpcServerLink(const sp<RpcServerLink>& link) { |
| 730 | Extras* e = mExtras.load(std::memory_order_acquire); |
| 731 | if (!e) return; |
Tomasz Wasilczyk | 66ee192 | 2023-10-26 14:53:49 -0700 | [diff] [blame] | 732 | RpcMutexUniqueLock _l(e->mLock); |
Yifan Hong | 8b89085 | 2021-06-10 13:44:09 -0700 | [diff] [blame] | 733 | (void)e->mRpcServerLinks.erase(link); |
| 734 | } |
| 735 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 736 | BBinder::~BBinder() |
| 737 | { |
Steven Moreland | 1bd2bc7 | 2022-07-29 20:29:40 +0000 | [diff] [blame] | 738 | if (!wasParceled()) { |
| 739 | if (getExtension()) { |
Tomasz Wasilczyk | e3de880 | 2023-11-01 11:05:27 -0700 | [diff] [blame] | 740 | ALOGW("Binder %p destroyed with extension attached before being parceled.", this); |
Steven Moreland | 1bd2bc7 | 2022-07-29 20:29:40 +0000 | [diff] [blame] | 741 | } |
| 742 | if (isRequestingSid()) { |
Tomasz Wasilczyk | e3de880 | 2023-11-01 11:05:27 -0700 | [diff] [blame] | 743 | ALOGW("Binder %p destroyed when requesting SID before being parceled.", this); |
Steven Moreland | 1bd2bc7 | 2022-07-29 20:29:40 +0000 | [diff] [blame] | 744 | } |
| 745 | if (isInheritRt()) { |
Tomasz Wasilczyk | e3de880 | 2023-11-01 11:05:27 -0700 | [diff] [blame] | 746 | ALOGW("Binder %p destroyed after setInheritRt before being parceled.", this); |
Steven Moreland | 1bd2bc7 | 2022-07-29 20:29:40 +0000 | [diff] [blame] | 747 | } |
Andrei Homescu | 4052550 | 2022-08-02 01:23:22 +0000 | [diff] [blame] | 748 | #ifdef __linux__ |
Steven Moreland | 1bd2bc7 | 2022-07-29 20:29:40 +0000 | [diff] [blame] | 749 | if (getMinSchedulerPolicy() != SCHED_NORMAL) { |
Tomasz Wasilczyk | e3de880 | 2023-11-01 11:05:27 -0700 | [diff] [blame] | 750 | ALOGW("Binder %p destroyed after setMinSchedulerPolicy before being parceled.", this); |
Steven Moreland | 1bd2bc7 | 2022-07-29 20:29:40 +0000 | [diff] [blame] | 751 | } |
| 752 | if (getMinSchedulerPriority() != 0) { |
Tomasz Wasilczyk | e3de880 | 2023-11-01 11:05:27 -0700 | [diff] [blame] | 753 | ALOGW("Binder %p destroyed after setMinSchedulerPolicy before being parceled.", this); |
Steven Moreland | 1bd2bc7 | 2022-07-29 20:29:40 +0000 | [diff] [blame] | 754 | } |
Andrei Homescu | 4052550 | 2022-08-02 01:23:22 +0000 | [diff] [blame] | 755 | #endif // __linux__ |
Steven Moreland | 55a1254 | 2022-03-31 21:53:11 +0000 | [diff] [blame] | 756 | } |
| 757 | |
Bailey Forrest | 6913c46 | 2015-08-18 17:15:10 -0700 | [diff] [blame] | 758 | Extras* e = mExtras.load(std::memory_order_relaxed); |
Hans Boehm | 3effaba | 2014-08-12 22:56:00 +0000 | [diff] [blame] | 759 | if (e) delete e; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 760 | } |
| 761 | |
| 762 | |
Jiyong Park | b86c866 | 2018-10-29 23:01:57 +0900 | [diff] [blame] | 763 | // NOLINTNEXTLINE(google-default-arguments) |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 764 | status_t BBinder::onTransact( |
Colin Cross | 6f4f3ab | 2014-02-05 17:42:44 -0800 | [diff] [blame] | 765 | 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] | 766 | { |
| 767 | switch (code) { |
| 768 | case INTERFACE_TRANSACTION: |
Tomasz Wasilczyk | e3de880 | 2023-11-01 11:05:27 -0700 | [diff] [blame] | 769 | LOG_ALWAYS_FATAL_IF(reply == nullptr, "reply == nullptr"); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 770 | reply->writeString16(getInterfaceDescriptor()); |
| 771 | return NO_ERROR; |
| 772 | |
| 773 | case DUMP_TRANSACTION: { |
| 774 | int fd = data.readFileDescriptor(); |
| 775 | int argc = data.readInt32(); |
| 776 | Vector<String16> args; |
| 777 | for (int i = 0; i < argc && data.dataAvail() > 0; i++) { |
| 778 | args.add(data.readString16()); |
| 779 | } |
| 780 | return dump(fd, args); |
| 781 | } |
Dianne Hackborn | 555f89d | 2012-05-08 18:54:22 -0700 | [diff] [blame] | 782 | |
Dianne Hackborn | 23eb1e2 | 2015-10-07 17:35:27 -0700 | [diff] [blame] | 783 | case SHELL_COMMAND_TRANSACTION: { |
| 784 | int in = data.readFileDescriptor(); |
| 785 | int out = data.readFileDescriptor(); |
| 786 | int err = data.readFileDescriptor(); |
| 787 | int argc = data.readInt32(); |
| 788 | Vector<String16> args; |
| 789 | for (int i = 0; i < argc && data.dataAvail() > 0; i++) { |
| 790 | args.add(data.readString16()); |
| 791 | } |
Andrei Homescu | a543a84 | 2022-07-07 22:17:55 +0000 | [diff] [blame] | 792 | sp<IBinder> shellCallbackBinder = data.readStrongBinder(); |
Dianne Hackborn | 23eb1e2 | 2015-10-07 17:35:27 -0700 | [diff] [blame] | 793 | sp<IResultReceiver> resultReceiver = IResultReceiver::asInterface( |
| 794 | data.readStrongBinder()); |
| 795 | |
Dianne Hackborn | f2bf93b | 2015-10-14 15:13:02 -0700 | [diff] [blame] | 796 | // XXX can't add virtuals until binaries are updated. |
Andrei Homescu | a543a84 | 2022-07-07 22:17:55 +0000 | [diff] [blame] | 797 | // sp<IShellCallback> shellCallback = IShellCallback::asInterface( |
| 798 | // shellCallbackBinder); |
| 799 | // return shellCommand(in, out, err, args, resultReceiver); |
Christopher Wiley | 0a9a1c1 | 2016-07-20 08:28:14 -0700 | [diff] [blame] | 800 | (void)in; |
| 801 | (void)out; |
| 802 | (void)err; |
| 803 | |
Yi Kong | fdd8da9 | 2018-06-07 17:52:27 -0700 | [diff] [blame] | 804 | if (resultReceiver != nullptr) { |
Dianne Hackborn | f2bf93b | 2015-10-14 15:13:02 -0700 | [diff] [blame] | 805 | resultReceiver->send(INVALID_OPERATION); |
| 806 | } |
Martijn Coenen | aa6ee99 | 2017-08-17 15:38:08 +0200 | [diff] [blame] | 807 | |
| 808 | return NO_ERROR; |
Dianne Hackborn | 23eb1e2 | 2015-10-07 17:35:27 -0700 | [diff] [blame] | 809 | } |
| 810 | |
Dianne Hackborn | 555f89d | 2012-05-08 18:54:22 -0700 | [diff] [blame] | 811 | case SYSPROPS_TRANSACTION: { |
Tomasz Wasilczyk | 346f104 | 2023-10-09 17:02:46 +0000 | [diff] [blame] | 812 | if (!binder::os::report_sysprop_change()) return INVALID_OPERATION; |
Dianne Hackborn | 555f89d | 2012-05-08 18:54:22 -0700 | [diff] [blame] | 813 | return NO_ERROR; |
| 814 | } |
| 815 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 816 | default: |
| 817 | return UNKNOWN_TRANSACTION; |
| 818 | } |
| 819 | } |
| 820 | |
Steven Moreland | f021200 | 2018-12-26 13:59:23 -0800 | [diff] [blame] | 821 | BBinder::Extras* BBinder::getOrCreateExtras() |
| 822 | { |
| 823 | Extras* e = mExtras.load(std::memory_order_acquire); |
| 824 | |
| 825 | if (!e) { |
| 826 | e = new Extras; |
| 827 | Extras* expected = nullptr; |
| 828 | if (!mExtras.compare_exchange_strong(expected, e, |
| 829 | std::memory_order_release, |
| 830 | std::memory_order_acquire)) { |
| 831 | delete e; |
| 832 | e = expected; // Filled in by CAS |
| 833 | } |
| 834 | if (e == nullptr) return nullptr; // out of memory |
| 835 | } |
| 836 | |
| 837 | return e; |
| 838 | } |
| 839 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 840 | // --------------------------------------------------------------------------- |
| 841 | |
| 842 | enum { |
| 843 | // This is used to transfer ownership of the remote binder from |
| 844 | // the BpRefBase object holding it (when it is constructed), to the |
| 845 | // owner of the BpRefBase object when it first acquires that BpRefBase. |
| 846 | kRemoteAcquired = 0x00000001 |
| 847 | }; |
| 848 | |
| 849 | BpRefBase::BpRefBase(const sp<IBinder>& o) |
Yi Kong | fdd8da9 | 2018-06-07 17:52:27 -0700 | [diff] [blame] | 850 | : mRemote(o.get()), mRefs(nullptr), mState(0) |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 851 | { |
| 852 | extendObjectLifetime(OBJECT_LIFETIME_WEAK); |
| 853 | |
| 854 | if (mRemote) { |
| 855 | mRemote->incStrong(this); // Removed on first IncStrong(). |
| 856 | mRefs = mRemote->createWeak(this); // Held for our entire lifetime. |
| 857 | } |
| 858 | } |
| 859 | |
| 860 | BpRefBase::~BpRefBase() |
| 861 | { |
| 862 | if (mRemote) { |
Bailey Forrest | 6913c46 | 2015-08-18 17:15:10 -0700 | [diff] [blame] | 863 | if (!(mState.load(std::memory_order_relaxed)&kRemoteAcquired)) { |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 864 | mRemote->decStrong(this); |
| 865 | } |
| 866 | mRefs->decWeak(this); |
| 867 | } |
| 868 | } |
| 869 | |
| 870 | void BpRefBase::onFirstRef() |
| 871 | { |
Bailey Forrest | 6913c46 | 2015-08-18 17:15:10 -0700 | [diff] [blame] | 872 | mState.fetch_or(kRemoteAcquired, std::memory_order_relaxed); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 873 | } |
| 874 | |
Colin Cross | 6f4f3ab | 2014-02-05 17:42:44 -0800 | [diff] [blame] | 875 | void BpRefBase::onLastStrongRef(const void* /*id*/) |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 876 | { |
| 877 | if (mRemote) { |
| 878 | mRemote->decStrong(this); |
| 879 | } |
| 880 | } |
| 881 | |
Colin Cross | 6f4f3ab | 2014-02-05 17:42:44 -0800 | [diff] [blame] | 882 | bool BpRefBase::onIncStrongAttempted(uint32_t /*flags*/, const void* /*id*/) |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 883 | { |
| 884 | return mRemote ? mRefs->attemptIncStrong(this) : false; |
| 885 | } |
| 886 | |
| 887 | // --------------------------------------------------------------------------- |
| 888 | |
Steven Moreland | 61ff849 | 2019-09-26 16:05:45 -0700 | [diff] [blame] | 889 | } // namespace android |