blob: 49653867b1f9fa6b13e389ad896acfb674157006 [file] [log] [blame]
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001/*
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 Agopianc5b2c0b2009-05-19 19:08:10 -070017#include <binder/Binder.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080018
Bailey Forrest6913c462015-08-18 17:15:10 -070019#include <atomic>
Yifan Hong8b890852021-06-10 13:44:09 -070020#include <set>
Yifan Hong84bedeb2021-04-21 21:37:17 -070021
22#include <android-base/unique_fd.h>
Mathias Agopianc5b2c0b2009-05-19 19:08:10 -070023#include <binder/BpBinder.h>
24#include <binder/IInterface.h>
Yifan Hong84bedeb2021-04-21 21:37:17 -070025#include <binder/IPCThreadState.h>
Dianne Hackborn23eb1e22015-10-07 17:35:27 -070026#include <binder/IResultReceiver.h>
Dianne Hackborn1941a402016-08-29 12:30:43 -070027#include <binder/IShellCallback.h>
Mathias Agopianc5b2c0b2009-05-19 19:08:10 -070028#include <binder/Parcel.h>
Yifan Hong84bedeb2021-04-21 21:37:17 -070029#include <binder/RpcServer.h>
30#include <private/android_filesystem_config.h>
31#include <utils/misc.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080032
Yifan Hong84bedeb2021-04-21 21:37:17 -070033#include <inttypes.h>
Steven Morelandbf1915b2020-07-16 22:43:02 +000034#include <linux/sched.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080035#include <stdio.h>
36
Yifan Hong84bedeb2021-04-21 21:37:17 -070037#include "RpcState.h"
38
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080039namespace android {
40
Steven Moreland90c1f9a2021-05-03 18:27:24 +000041// Service implementations inherit from BBinder and IBinder, and this is frozen
42// in prebuilts.
43#ifdef __LP64__
44static_assert(sizeof(IBinder) == 24);
45static_assert(sizeof(BBinder) == 40);
46#else
47static_assert(sizeof(IBinder) == 12);
48static_assert(sizeof(BBinder) == 20);
49#endif
50
Yifan Hong84bedeb2021-04-21 21:37:17 -070051#ifdef BINDER_RPC_DEV_SERVERS
52constexpr const bool kEnableRpcDevServers = true;
53#else
54constexpr const bool kEnableRpcDevServers = false;
55#endif
56
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080057// ---------------------------------------------------------------------------
58
Mathias Agopian83c04462009-05-22 19:00:22 -070059IBinder::IBinder()
60 : RefBase()
61{
62}
63
64IBinder::~IBinder()
65{
66}
67
68// ---------------------------------------------------------------------------
69
Colin Cross6f4f3ab2014-02-05 17:42:44 -080070sp<IInterface> IBinder::queryLocalInterface(const String16& /*descriptor*/)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080071{
Yi Kongfdd8da92018-06-07 17:52:27 -070072 return nullptr;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080073}
74
75BBinder* IBinder::localBinder()
76{
Yi Kongfdd8da92018-06-07 17:52:27 -070077 return nullptr;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080078}
79
80BpBinder* IBinder::remoteBinder()
81{
Yi Kongfdd8da92018-06-07 17:52:27 -070082 return nullptr;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080083}
84
85bool IBinder::checkSubclass(const void* /*subclassID*/) const
86{
87 return false;
88}
89
Dianne Hackborn23eb1e22015-10-07 17:35:27 -070090
Dianne Hackbornf2bf93b2015-10-14 15:13:02 -070091status_t IBinder::shellCommand(const sp<IBinder>& target, int in, int out, int err,
Dianne Hackborn1941a402016-08-29 12:30:43 -070092 Vector<String16>& args, const sp<IShellCallback>& callback,
93 const sp<IResultReceiver>& resultReceiver)
Dianne Hackborn23eb1e22015-10-07 17:35:27 -070094{
Dianne Hackbornf2bf93b2015-10-14 15:13:02 -070095 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 Hackborn23eb1e22015-10-07 17:35:27 -0700104 }
Yi Kongfdd8da92018-06-07 17:52:27 -0700105 send.writeStrongBinder(callback != nullptr ? IInterface::asBinder(callback) : nullptr);
106 send.writeStrongBinder(resultReceiver != nullptr ? IInterface::asBinder(resultReceiver) : nullptr);
Dianne Hackbornf2bf93b2015-10-14 15:13:02 -0700107 return target->transact(SHELL_COMMAND_TRANSACTION, send, &reply);
Dianne Hackborn23eb1e22015-10-07 17:35:27 -0700108}
109
Steven Morelandb8ad08d2019-08-09 14:42:56 -0700110status_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 Moreland86080b82019-09-23 15:41:18 -0700128status_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 Hong02530ec2021-06-10 13:38:38 -0700154status_t IBinder::setRpcClientDebug(android::base::unique_fd socketFd,
155 const sp<IBinder>& keepAliveBinder) {
Yifan Hong84bedeb2021-04-21 21:37:17 -0700156 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 Hong02530ec2021-06-10 13:38:38 -0700163 return local->BBinder::setRpcClientDebug(std::move(socketFd), keepAliveBinder);
Yifan Hong84bedeb2021-04-21 21:37:17 -0700164 }
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 Hong02530ec2021-06-10 13:38:38 -0700178 if (status = data.writeStrongBinder(keepAliveBinder); status != OK) return status;
Yifan Hong84bedeb2021-04-21 21:37:17 -0700179 return transact(SET_RPC_CLIENT_TRANSACTION, data, &reply);
180}
181
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800182// ---------------------------------------------------------------------------
183
Yifan Hong8b890852021-06-10 13:44:09 -0700184class BBinder::RpcServerLink : public IBinder::DeathRecipient {
185public:
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
210private:
211 sp<RpcServer> mRpcServer;
212 sp<IBinder> mKeepAliveBinder; // hold to avoid automatically unlinking
213 wp<BBinder> mBinder;
214};
215
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800216class BBinder::Extras
217{
218public:
Steven Morelandf0212002018-12-26 13:59:23 -0800219 // unlocked objects
220 bool mRequestingSid = false;
Steven Morelandcf03cf12020-12-04 02:58:40 +0000221 bool mInheritRt = false;
Steven Morelandb8ad08d2019-08-09 14:42:56 -0700222 sp<IBinder> mExtension;
Steven Morelandbf1915b2020-07-16 22:43:02 +0000223 int mPolicy = SCHED_NORMAL;
224 int mPriority = 0;
Steven Morelandf0212002018-12-26 13:59:23 -0800225
226 // for below objects
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800227 Mutex mLock;
Yifan Hong8b890852021-06-10 13:44:09 -0700228 std::set<sp<RpcServerLink>> mRpcServerLinks;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800229 BpBinder::ObjectManager mObjects;
230};
231
232// ---------------------------------------------------------------------------
233
Kalesh Singhd67c8e82020-12-29 15:46:25 -0500234BBinder::BBinder() : mExtras(nullptr), mStability(0), mParceled(false) {}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800235
236bool BBinder::isBinderAlive() const
237{
238 return true;
239}
240
241status_t BBinder::pingBinder()
242{
243 return NO_ERROR;
244}
245
Mathias Agopian83c04462009-05-22 19:00:22 -0700246const String16& BBinder::getInterfaceDescriptor() const
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800247{
Dan Egnor386a3322010-05-06 00:55:09 -0700248 // This is a local static rather than a global static,
249 // to avoid static initializer ordering issues.
250 static String16 sEmptyDescriptor;
Steve Block32397c12012-01-05 23:22:43 +0000251 ALOGW("reached BBinder::getInterfaceDescriptor (this=%p)", this);
Mathias Agopian83c04462009-05-22 19:00:22 -0700252 return sEmptyDescriptor;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800253}
254
Jiyong Parkb86c8662018-10-29 23:01:57 +0900255// NOLINTNEXTLINE(google-default-arguments)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800256status_t BBinder::transact(
257 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
258{
259 data.setDataPosition(0);
260
Steven Morelandf183fdd2020-10-27 00:12:12 +0000261 if (reply != nullptr && (flags & FLAG_CLEAR_BUF)) {
262 reply->markSensitive();
263 }
264
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800265 status_t err = NO_ERROR;
266 switch (code) {
267 case PING_TRANSACTION:
Steven Moreland6e69d652019-07-10 14:17:55 -0700268 err = pingBinder();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800269 break;
Steven Morelandb8ad08d2019-08-09 14:42:56 -0700270 case EXTENSION_TRANSACTION:
271 err = reply->writeStrongBinder(getExtension());
272 break;
Steven Moreland86080b82019-09-23 15:41:18 -0700273 case DEBUG_PID_TRANSACTION:
274 err = reply->writeInt32(getDebugPid());
275 break;
Yifan Hong84bedeb2021-04-21 21:37:17 -0700276 case SET_RPC_CLIENT_TRANSACTION: {
277 err = setRpcClientDebug(data);
278 break;
279 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800280 default:
281 err = onTransact(code, data, reply, flags);
282 break;
283 }
284
Steven Morelanda86a3562019-08-01 23:28:34 +0000285 // In case this is being transacted on in the same process.
Yi Kongfdd8da92018-06-07 17:52:27 -0700286 if (reply != nullptr) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800287 reply->setDataPosition(0);
288 }
289
290 return err;
291}
292
Jiyong Parkb86c8662018-10-29 23:01:57 +0900293// NOLINTNEXTLINE(google-default-arguments)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800294status_t BBinder::linkToDeath(
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800295 const sp<DeathRecipient>& /*recipient*/, void* /*cookie*/,
296 uint32_t /*flags*/)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800297{
298 return INVALID_OPERATION;
299}
300
Jiyong Parkb86c8662018-10-29 23:01:57 +0900301// NOLINTNEXTLINE(google-default-arguments)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800302status_t BBinder::unlinkToDeath(
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800303 const wp<DeathRecipient>& /*recipient*/, void* /*cookie*/,
304 uint32_t /*flags*/, wp<DeathRecipient>* /*outRecipient*/)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800305{
306 return INVALID_OPERATION;
307}
308
Dianne Hackborn23eb1e22015-10-07 17:35:27 -0700309status_t BBinder::dump(int /*fd*/, const Vector<String16>& /*args*/)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800310{
311 return NO_ERROR;
312}
313
Steven Moreland63a2d512021-06-25 01:10:15 +0000314void* BBinder::attachObject(const void* objectID, void* object, void* cleanupCookie,
315 object_cleanup_func func) {
Steven Morelandf0212002018-12-26 13:59:23 -0800316 Extras* e = getOrCreateExtras();
Steven Moreland63a2d512021-06-25 01:10:15 +0000317 if (!e) return nullptr; // out of memory
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800318
319 AutoMutex _l(e->mLock);
Steven Moreland63a2d512021-06-25 01:10:15 +0000320 return e->mObjects.attach(objectID, object, cleanupCookie, func);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800321}
322
323void* BBinder::findObject(const void* objectID) const
324{
Bailey Forrest6913c462015-08-18 17:15:10 -0700325 Extras* e = mExtras.load(std::memory_order_acquire);
Yi Kongfdd8da92018-06-07 17:52:27 -0700326 if (!e) return nullptr;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800327
328 AutoMutex _l(e->mLock);
329 return e->mObjects.find(objectID);
330}
331
Steven Moreland63a2d512021-06-25 01:10:15 +0000332void* BBinder::detachObject(const void* objectID) {
Bailey Forrest6913c462015-08-18 17:15:10 -0700333 Extras* e = mExtras.load(std::memory_order_acquire);
Steven Moreland63a2d512021-06-25 01:10:15 +0000334 if (!e) return nullptr;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800335
336 AutoMutex _l(e->mLock);
Steven Moreland63a2d512021-06-25 01:10:15 +0000337 return e->mObjects.detach(objectID);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800338}
339
340BBinder* BBinder::localBinder()
341{
342 return this;
343}
344
Steven Morelandf0212002018-12-26 13:59:23 -0800345bool BBinder::isRequestingSid()
346{
347 Extras* e = mExtras.load(std::memory_order_acquire);
348
349 return e && e->mRequestingSid;
350}
351
352void BBinder::setRequestingSid(bool requestingSid)
353{
Steven Morelandd37e6dd2021-06-16 22:07:51 +0000354 LOG_ALWAYS_FATAL_IF(mParceled,
355 "setRequestingSid() should not be called after a binder object "
356 "is parceled/sent to another process");
Kalesh Singhd67c8e82020-12-29 15:46:25 -0500357
Steven Morelandf0212002018-12-26 13:59:23 -0800358 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 Moreland3668be62019-02-08 17:56:55 -0800370 e->mRequestingSid = requestingSid;
Steven Morelandf0212002018-12-26 13:59:23 -0800371}
372
Steven Morelandb8ad08d2019-08-09 14:42:56 -0700373sp<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 Morelandbf1915b2020-07-16 22:43:02 +0000379void BBinder::setMinSchedulerPolicy(int policy, int priority) {
Steven Morelandd37e6dd2021-06-16 22:07:51 +0000380 LOG_ALWAYS_FATAL_IF(mParceled,
381 "setMinSchedulerPolicy() should not be called after a binder object "
382 "is parceled/sent to another process");
Kalesh Singhd67c8e82020-12-29 15:46:25 -0500383
Steven Morelandbf1915b2020-07-16 22:43:02 +0000384 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
412int BBinder::getMinSchedulerPolicy() {
413 Extras* e = mExtras.load(std::memory_order_acquire);
414 if (e == nullptr) return SCHED_NORMAL;
415 return e->mPolicy;
416}
417
418int BBinder::getMinSchedulerPriority() {
419 Extras* e = mExtras.load(std::memory_order_acquire);
420 if (e == nullptr) return 0;
421 return e->mPriority;
422}
423
Steven Morelandcf03cf12020-12-04 02:58:40 +0000424bool BBinder::isInheritRt() {
425 Extras* e = mExtras.load(std::memory_order_acquire);
426
427 return e && e->mInheritRt;
428}
429
430void BBinder::setInheritRt(bool inheritRt) {
Steven Morelandd37e6dd2021-06-16 22:07:51 +0000431 LOG_ALWAYS_FATAL_IF(mParceled,
432 "setInheritRt() should not be called after a binder object "
433 "is parceled/sent to another process");
Kalesh Singhd67c8e82020-12-29 15:46:25 -0500434
Steven Morelandcf03cf12020-12-04 02:58:40 +0000435 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 Moreland86080b82019-09-23 15:41:18 -0700449pid_t BBinder::getDebugPid() {
450 return getpid();
451}
452
Steven Morelandb8ad08d2019-08-09 14:42:56 -0700453void BBinder::setExtension(const sp<IBinder>& extension) {
Steven Morelandd37e6dd2021-06-16 22:07:51 +0000454 LOG_ALWAYS_FATAL_IF(mParceled,
455 "setExtension() should not be called after a binder object "
456 "is parceled/sent to another process");
Kalesh Singhd67c8e82020-12-29 15:46:25 -0500457
Steven Morelandb8ad08d2019-08-09 14:42:56 -0700458 Extras* e = getOrCreateExtras();
459 e->mExtension = extension;
460}
461
Kalesh Singhd67c8e82020-12-29 15:46:25 -0500462bool BBinder::wasParceled() {
463 return mParceled;
464}
465
466void BBinder::setParceled() {
467 mParceled = true;
468}
469
Yifan Hong84bedeb2021-04-21 21:37:17 -0700470status_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 Hong84bedeb2021-04-21 21:37:17 -0700483
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 Hong02530ec2021-06-10 13:38:38 -0700488 sp<IBinder> keepAliveBinder;
489 if (status = data.readNullableStrongBinder(&keepAliveBinder); status != OK) return status;
Yifan Hong84bedeb2021-04-21 21:37:17 -0700490
Yifan Hong02530ec2021-06-10 13:38:38 -0700491 return setRpcClientDebug(std::move(clientFd), keepAliveBinder);
Yifan Hong84bedeb2021-04-21 21:37:17 -0700492}
493
Yifan Hong02530ec2021-06-10 13:38:38 -0700494status_t BBinder::setRpcClientDebug(android::base::unique_fd socketFd,
495 const sp<IBinder>& keepAliveBinder) {
Yifan Hong84bedeb2021-04-21 21:37:17 -0700496 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 Hong34823232021-06-07 17:23:00 -0700502 LOG_RPC_DETAIL("%s(fd=%d)", __PRETTY_FUNCTION__, socketFdForPrint);
Yifan Hong84bedeb2021-04-21 21:37:17 -0700503
504 if (!socketFd.ok()) {
505 ALOGE("%s: No socket FD provided.", __PRETTY_FUNCTION__);
506 return BAD_VALUE;
507 }
Yifan Hong84bedeb2021-04-21 21:37:17 -0700508
Yifan Hong02530ec2021-06-10 13:38:38 -0700509 if (keepAliveBinder == nullptr) {
510 ALOGE("%s: No keepAliveBinder provided.", __PRETTY_FUNCTION__);
511 return UNEXPECTED_NULL;
512 }
513
Yifan Hong84bedeb2021-04-21 21:37:17 -0700514 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 Hong8b890852021-06-10 13:44:09 -0700522 // Weak ref to avoid circular dependency:
523 // BBinder -> RpcServerLink ----> RpcServer -X-> BBinder
524 // `-X-> BBinder
525 auto weakThis = wp<BBinder>::fromExisting(this);
526
Yifan Hong84bedeb2021-04-21 21:37:17 -0700527 Extras* e = getOrCreateExtras();
528 AutoMutex _l(e->mLock);
Yifan Hong8b890852021-06-10 13:44:09 -0700529 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 Hong84bedeb2021-04-21 21:37:17 -0700537 }
Yifan Hong8b890852021-06-10 13:44:09 -0700538 rpcServer->setRootObjectWeak(weakThis);
539 rpcServer->setupExternalServer(std::move(socketFd));
540 rpcServer->setMaxThreads(binderThreadPoolMaxCount);
541 rpcServer->start();
542 e->mRpcServerLinks.emplace(link);
Yifan Hong34823232021-06-07 17:23:00 -0700543 LOG_RPC_DETAIL("%s(fd=%d) successful", __PRETTY_FUNCTION__, socketFdForPrint);
Yifan Hong84bedeb2021-04-21 21:37:17 -0700544 return OK;
545}
546
Yifan Hong8b890852021-06-10 13:44:09 -0700547void 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 Projectedbf3b62009-03-03 19:31:44 -0800554BBinder::~BBinder()
555{
Bailey Forrest6913c462015-08-18 17:15:10 -0700556 Extras* e = mExtras.load(std::memory_order_relaxed);
Hans Boehm3effaba2014-08-12 22:56:00 +0000557 if (e) delete e;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800558}
559
560
Jiyong Parkb86c8662018-10-29 23:01:57 +0900561// NOLINTNEXTLINE(google-default-arguments)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800562status_t BBinder::onTransact(
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800563 uint32_t code, const Parcel& data, Parcel* reply, uint32_t /*flags*/)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800564{
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 Hackborn555f89d2012-05-08 18:54:22 -0700579
Dianne Hackborn23eb1e22015-10-07 17:35:27 -0700580 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 Hackborn1941a402016-08-29 12:30:43 -0700589 sp<IShellCallback> shellCallback = IShellCallback::asInterface(
590 data.readStrongBinder());
Dianne Hackborn23eb1e22015-10-07 17:35:27 -0700591 sp<IResultReceiver> resultReceiver = IResultReceiver::asInterface(
592 data.readStrongBinder());
593
Dianne Hackbornf2bf93b2015-10-14 15:13:02 -0700594 // XXX can't add virtuals until binaries are updated.
595 //return shellCommand(in, out, err, args, resultReceiver);
Christopher Wiley0a9a1c12016-07-20 08:28:14 -0700596 (void)in;
597 (void)out;
598 (void)err;
599
Yi Kongfdd8da92018-06-07 17:52:27 -0700600 if (resultReceiver != nullptr) {
Dianne Hackbornf2bf93b2015-10-14 15:13:02 -0700601 resultReceiver->send(INVALID_OPERATION);
602 }
Martijn Coenenaa6ee992017-08-17 15:38:08 +0200603
604 return NO_ERROR;
Dianne Hackborn23eb1e22015-10-07 17:35:27 -0700605 }
606
Dianne Hackborn555f89d2012-05-08 18:54:22 -0700607 case SYSPROPS_TRANSACTION: {
608 report_sysprop_change();
609 return NO_ERROR;
610 }
611
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800612 default:
613 return UNKNOWN_TRANSACTION;
614 }
615}
616
Steven Morelandf0212002018-12-26 13:59:23 -0800617BBinder::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 Projectedbf3b62009-03-03 19:31:44 -0800636// ---------------------------------------------------------------------------
637
638enum {
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
645BpRefBase::BpRefBase(const sp<IBinder>& o)
Yi Kongfdd8da92018-06-07 17:52:27 -0700646 : mRemote(o.get()), mRefs(nullptr), mState(0)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800647{
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
656BpRefBase::~BpRefBase()
657{
658 if (mRemote) {
Bailey Forrest6913c462015-08-18 17:15:10 -0700659 if (!(mState.load(std::memory_order_relaxed)&kRemoteAcquired)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800660 mRemote->decStrong(this);
661 }
662 mRefs->decWeak(this);
663 }
664}
665
666void BpRefBase::onFirstRef()
667{
Bailey Forrest6913c462015-08-18 17:15:10 -0700668 mState.fetch_or(kRemoteAcquired, std::memory_order_relaxed);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800669}
670
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800671void BpRefBase::onLastStrongRef(const void* /*id*/)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800672{
673 if (mRemote) {
674 mRemote->decStrong(this);
675 }
676}
677
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800678bool BpRefBase::onIncStrongAttempted(uint32_t /*flags*/, const void* /*id*/)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800679{
680 return mRemote ? mRefs->attemptIncStrong(this) : false;
681}
682
683// ---------------------------------------------------------------------------
684
Steven Moreland61ff8492019-09-26 16:05:45 -0700685} // namespace android