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