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