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