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