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