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