blob: 02321cdf9719053b02e7c045f17d27af6531ce33 [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
314void BBinder::attachObject(
315 const void* objectID, void* object, void* cleanupCookie,
316 object_cleanup_func func)
317{
Steven Morelandf0212002018-12-26 13:59:23 -0800318 Extras* e = getOrCreateExtras();
319 if (!e) return; // out of memory
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800320
321 AutoMutex _l(e->mLock);
322 e->mObjects.attach(objectID, object, cleanupCookie, func);
323}
324
325void* BBinder::findObject(const void* objectID) const
326{
Bailey Forrest6913c462015-08-18 17:15:10 -0700327 Extras* e = mExtras.load(std::memory_order_acquire);
Yi Kongfdd8da92018-06-07 17:52:27 -0700328 if (!e) return nullptr;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800329
330 AutoMutex _l(e->mLock);
331 return e->mObjects.find(objectID);
332}
333
334void BBinder::detachObject(const void* objectID)
335{
Bailey Forrest6913c462015-08-18 17:15:10 -0700336 Extras* e = mExtras.load(std::memory_order_acquire);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800337 if (!e) return;
338
339 AutoMutex _l(e->mLock);
340 e->mObjects.detach(objectID);
341}
342
343BBinder* BBinder::localBinder()
344{
345 return this;
346}
347
Steven Morelandf0212002018-12-26 13:59:23 -0800348bool BBinder::isRequestingSid()
349{
350 Extras* e = mExtras.load(std::memory_order_acquire);
351
352 return e && e->mRequestingSid;
353}
354
355void BBinder::setRequestingSid(bool requestingSid)
356{
Steven Morelandd37e6dd2021-06-16 22:07:51 +0000357 LOG_ALWAYS_FATAL_IF(mParceled,
358 "setRequestingSid() should not be called after a binder object "
359 "is parceled/sent to another process");
Kalesh Singhd67c8e82020-12-29 15:46:25 -0500360
Steven Morelandf0212002018-12-26 13:59:23 -0800361 Extras* e = mExtras.load(std::memory_order_acquire);
362
363 if (!e) {
364 // default is false. Most things don't need sids, so avoiding allocations when possible.
365 if (!requestingSid) {
366 return;
367 }
368
369 e = getOrCreateExtras();
370 if (!e) return; // out of memory
371 }
372
Steven Moreland3668be62019-02-08 17:56:55 -0800373 e->mRequestingSid = requestingSid;
Steven Morelandf0212002018-12-26 13:59:23 -0800374}
375
Steven Morelandb8ad08d2019-08-09 14:42:56 -0700376sp<IBinder> BBinder::getExtension() {
377 Extras* e = mExtras.load(std::memory_order_acquire);
378 if (e == nullptr) return nullptr;
379 return e->mExtension;
380}
381
Steven Morelandbf1915b2020-07-16 22:43:02 +0000382void BBinder::setMinSchedulerPolicy(int policy, int priority) {
Steven Morelandd37e6dd2021-06-16 22:07:51 +0000383 LOG_ALWAYS_FATAL_IF(mParceled,
384 "setMinSchedulerPolicy() should not be called after a binder object "
385 "is parceled/sent to another process");
Kalesh Singhd67c8e82020-12-29 15:46:25 -0500386
Steven Morelandbf1915b2020-07-16 22:43:02 +0000387 switch (policy) {
388 case SCHED_NORMAL:
389 LOG_ALWAYS_FATAL_IF(priority < -20 || priority > 19, "Invalid priority for SCHED_NORMAL: %d", priority);
390 break;
391 case SCHED_RR:
392 case SCHED_FIFO:
393 LOG_ALWAYS_FATAL_IF(priority < 1 || priority > 99, "Invalid priority for sched %d: %d", policy, priority);
394 break;
395 default:
396 LOG_ALWAYS_FATAL("Unrecognized scheduling policy: %d", policy);
397 }
398
399 Extras* e = mExtras.load(std::memory_order_acquire);
400
401 if (e == nullptr) {
402 // Avoid allocations if called with default.
403 if (policy == SCHED_NORMAL && priority == 0) {
404 return;
405 }
406
407 e = getOrCreateExtras();
408 if (!e) return; // out of memory
409 }
410
411 e->mPolicy = policy;
412 e->mPriority = priority;
413}
414
415int BBinder::getMinSchedulerPolicy() {
416 Extras* e = mExtras.load(std::memory_order_acquire);
417 if (e == nullptr) return SCHED_NORMAL;
418 return e->mPolicy;
419}
420
421int BBinder::getMinSchedulerPriority() {
422 Extras* e = mExtras.load(std::memory_order_acquire);
423 if (e == nullptr) return 0;
424 return e->mPriority;
425}
426
Steven Morelandcf03cf12020-12-04 02:58:40 +0000427bool BBinder::isInheritRt() {
428 Extras* e = mExtras.load(std::memory_order_acquire);
429
430 return e && e->mInheritRt;
431}
432
433void BBinder::setInheritRt(bool inheritRt) {
Steven Morelandd37e6dd2021-06-16 22:07:51 +0000434 LOG_ALWAYS_FATAL_IF(mParceled,
435 "setInheritRt() should not be called after a binder object "
436 "is parceled/sent to another process");
Kalesh Singhd67c8e82020-12-29 15:46:25 -0500437
Steven Morelandcf03cf12020-12-04 02:58:40 +0000438 Extras* e = mExtras.load(std::memory_order_acquire);
439
440 if (!e) {
441 if (!inheritRt) {
442 return;
443 }
444
445 e = getOrCreateExtras();
446 if (!e) return; // out of memory
447 }
448
449 e->mInheritRt = inheritRt;
450}
451
Steven Moreland86080b82019-09-23 15:41:18 -0700452pid_t BBinder::getDebugPid() {
453 return getpid();
454}
455
Steven Morelandb8ad08d2019-08-09 14:42:56 -0700456void BBinder::setExtension(const sp<IBinder>& extension) {
Steven Morelandd37e6dd2021-06-16 22:07:51 +0000457 LOG_ALWAYS_FATAL_IF(mParceled,
458 "setExtension() should not be called after a binder object "
459 "is parceled/sent to another process");
Kalesh Singhd67c8e82020-12-29 15:46:25 -0500460
Steven Morelandb8ad08d2019-08-09 14:42:56 -0700461 Extras* e = getOrCreateExtras();
462 e->mExtension = extension;
463}
464
Kalesh Singhd67c8e82020-12-29 15:46:25 -0500465bool BBinder::wasParceled() {
466 return mParceled;
467}
468
469void BBinder::setParceled() {
470 mParceled = true;
471}
472
Yifan Hong84bedeb2021-04-21 21:37:17 -0700473status_t BBinder::setRpcClientDebug(const Parcel& data) {
474 if constexpr (!kEnableRpcDevServers) {
475 ALOGW("%s: disallowed because RPC is not enabled", __PRETTY_FUNCTION__);
476 return INVALID_OPERATION;
477 }
478 uid_t uid = IPCThreadState::self()->getCallingUid();
479 if (uid != AID_ROOT) {
480 ALOGE("%s: not allowed because client %" PRIu32 " is not root", __PRETTY_FUNCTION__, uid);
481 return PERMISSION_DENIED;
482 }
483 status_t status;
484 bool hasSocketFd;
485 android::base::unique_fd clientFd;
Yifan Hong84bedeb2021-04-21 21:37:17 -0700486
487 if (status = data.readBool(&hasSocketFd); status != OK) return status;
488 if (hasSocketFd) {
489 if (status = data.readUniqueFileDescriptor(&clientFd); status != OK) return status;
490 }
Yifan Hong02530ec2021-06-10 13:38:38 -0700491 sp<IBinder> keepAliveBinder;
492 if (status = data.readNullableStrongBinder(&keepAliveBinder); status != OK) return status;
Yifan Hong84bedeb2021-04-21 21:37:17 -0700493
Yifan Hong02530ec2021-06-10 13:38:38 -0700494 return setRpcClientDebug(std::move(clientFd), keepAliveBinder);
Yifan Hong84bedeb2021-04-21 21:37:17 -0700495}
496
Yifan Hong02530ec2021-06-10 13:38:38 -0700497status_t BBinder::setRpcClientDebug(android::base::unique_fd socketFd,
498 const sp<IBinder>& keepAliveBinder) {
Yifan Hong84bedeb2021-04-21 21:37:17 -0700499 if constexpr (!kEnableRpcDevServers) {
500 ALOGW("%s: disallowed because RPC is not enabled", __PRETTY_FUNCTION__);
501 return INVALID_OPERATION;
502 }
503
504 const int socketFdForPrint = socketFd.get();
Yifan Hong34823232021-06-07 17:23:00 -0700505 LOG_RPC_DETAIL("%s(fd=%d)", __PRETTY_FUNCTION__, socketFdForPrint);
Yifan Hong84bedeb2021-04-21 21:37:17 -0700506
507 if (!socketFd.ok()) {
508 ALOGE("%s: No socket FD provided.", __PRETTY_FUNCTION__);
509 return BAD_VALUE;
510 }
Yifan Hong84bedeb2021-04-21 21:37:17 -0700511
Yifan Hong02530ec2021-06-10 13:38:38 -0700512 if (keepAliveBinder == nullptr) {
513 ALOGE("%s: No keepAliveBinder provided.", __PRETTY_FUNCTION__);
514 return UNEXPECTED_NULL;
515 }
516
Yifan Hong84bedeb2021-04-21 21:37:17 -0700517 size_t binderThreadPoolMaxCount = ProcessState::self()->getThreadPoolMaxThreadCount();
518 if (binderThreadPoolMaxCount <= 1) {
519 ALOGE("%s: ProcessState thread pool max count is %zu. RPC is disabled for this service "
520 "because RPC requires the service to support multithreading.",
521 __PRETTY_FUNCTION__, binderThreadPoolMaxCount);
522 return INVALID_OPERATION;
523 }
524
Yifan Hong8b890852021-06-10 13:44:09 -0700525 // Weak ref to avoid circular dependency:
526 // BBinder -> RpcServerLink ----> RpcServer -X-> BBinder
527 // `-X-> BBinder
528 auto weakThis = wp<BBinder>::fromExisting(this);
529
Yifan Hong84bedeb2021-04-21 21:37:17 -0700530 Extras* e = getOrCreateExtras();
531 AutoMutex _l(e->mLock);
Yifan Hong8b890852021-06-10 13:44:09 -0700532 auto rpcServer = RpcServer::make();
533 LOG_ALWAYS_FATAL_IF(rpcServer == nullptr, "RpcServer::make returns null");
534 rpcServer->iUnderstandThisCodeIsExperimentalAndIWillNotUseItInProduction();
535 auto link = sp<RpcServerLink>::make(rpcServer, keepAliveBinder, weakThis);
536 if (auto status = keepAliveBinder->linkToDeath(link, nullptr, 0); status != OK) {
537 ALOGE("%s: keepAliveBinder->linkToDeath returns %s", __PRETTY_FUNCTION__,
538 statusToString(status).c_str());
539 return status;
Yifan Hong84bedeb2021-04-21 21:37:17 -0700540 }
Yifan Hong8b890852021-06-10 13:44:09 -0700541 rpcServer->setRootObjectWeak(weakThis);
542 rpcServer->setupExternalServer(std::move(socketFd));
543 rpcServer->setMaxThreads(binderThreadPoolMaxCount);
544 rpcServer->start();
545 e->mRpcServerLinks.emplace(link);
Yifan Hong34823232021-06-07 17:23:00 -0700546 LOG_RPC_DETAIL("%s(fd=%d) successful", __PRETTY_FUNCTION__, socketFdForPrint);
Yifan Hong84bedeb2021-04-21 21:37:17 -0700547 return OK;
548}
549
Yifan Hong8b890852021-06-10 13:44:09 -0700550void BBinder::removeRpcServerLink(const sp<RpcServerLink>& link) {
551 Extras* e = mExtras.load(std::memory_order_acquire);
552 if (!e) return;
553 AutoMutex _l(e->mLock);
554 (void)e->mRpcServerLinks.erase(link);
555}
556
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800557BBinder::~BBinder()
558{
Bailey Forrest6913c462015-08-18 17:15:10 -0700559 Extras* e = mExtras.load(std::memory_order_relaxed);
Hans Boehm3effaba2014-08-12 22:56:00 +0000560 if (e) delete e;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800561}
562
563
Jiyong Parkb86c8662018-10-29 23:01:57 +0900564// NOLINTNEXTLINE(google-default-arguments)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800565status_t BBinder::onTransact(
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800566 uint32_t code, const Parcel& data, Parcel* reply, uint32_t /*flags*/)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800567{
568 switch (code) {
569 case INTERFACE_TRANSACTION:
570 reply->writeString16(getInterfaceDescriptor());
571 return NO_ERROR;
572
573 case DUMP_TRANSACTION: {
574 int fd = data.readFileDescriptor();
575 int argc = data.readInt32();
576 Vector<String16> args;
577 for (int i = 0; i < argc && data.dataAvail() > 0; i++) {
578 args.add(data.readString16());
579 }
580 return dump(fd, args);
581 }
Dianne Hackborn555f89d2012-05-08 18:54:22 -0700582
Dianne Hackborn23eb1e22015-10-07 17:35:27 -0700583 case SHELL_COMMAND_TRANSACTION: {
584 int in = data.readFileDescriptor();
585 int out = data.readFileDescriptor();
586 int err = data.readFileDescriptor();
587 int argc = data.readInt32();
588 Vector<String16> args;
589 for (int i = 0; i < argc && data.dataAvail() > 0; i++) {
590 args.add(data.readString16());
591 }
Dianne Hackborn1941a402016-08-29 12:30:43 -0700592 sp<IShellCallback> shellCallback = IShellCallback::asInterface(
593 data.readStrongBinder());
Dianne Hackborn23eb1e22015-10-07 17:35:27 -0700594 sp<IResultReceiver> resultReceiver = IResultReceiver::asInterface(
595 data.readStrongBinder());
596
Dianne Hackbornf2bf93b2015-10-14 15:13:02 -0700597 // XXX can't add virtuals until binaries are updated.
598 //return shellCommand(in, out, err, args, resultReceiver);
Christopher Wiley0a9a1c12016-07-20 08:28:14 -0700599 (void)in;
600 (void)out;
601 (void)err;
602
Yi Kongfdd8da92018-06-07 17:52:27 -0700603 if (resultReceiver != nullptr) {
Dianne Hackbornf2bf93b2015-10-14 15:13:02 -0700604 resultReceiver->send(INVALID_OPERATION);
605 }
Martijn Coenenaa6ee992017-08-17 15:38:08 +0200606
607 return NO_ERROR;
Dianne Hackborn23eb1e22015-10-07 17:35:27 -0700608 }
609
Dianne Hackborn555f89d2012-05-08 18:54:22 -0700610 case SYSPROPS_TRANSACTION: {
611 report_sysprop_change();
612 return NO_ERROR;
613 }
614
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800615 default:
616 return UNKNOWN_TRANSACTION;
617 }
618}
619
Steven Morelandf0212002018-12-26 13:59:23 -0800620BBinder::Extras* BBinder::getOrCreateExtras()
621{
622 Extras* e = mExtras.load(std::memory_order_acquire);
623
624 if (!e) {
625 e = new Extras;
626 Extras* expected = nullptr;
627 if (!mExtras.compare_exchange_strong(expected, e,
628 std::memory_order_release,
629 std::memory_order_acquire)) {
630 delete e;
631 e = expected; // Filled in by CAS
632 }
633 if (e == nullptr) return nullptr; // out of memory
634 }
635
636 return e;
637}
638
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800639// ---------------------------------------------------------------------------
640
641enum {
642 // This is used to transfer ownership of the remote binder from
643 // the BpRefBase object holding it (when it is constructed), to the
644 // owner of the BpRefBase object when it first acquires that BpRefBase.
645 kRemoteAcquired = 0x00000001
646};
647
648BpRefBase::BpRefBase(const sp<IBinder>& o)
Yi Kongfdd8da92018-06-07 17:52:27 -0700649 : mRemote(o.get()), mRefs(nullptr), mState(0)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800650{
651 extendObjectLifetime(OBJECT_LIFETIME_WEAK);
652
653 if (mRemote) {
654 mRemote->incStrong(this); // Removed on first IncStrong().
655 mRefs = mRemote->createWeak(this); // Held for our entire lifetime.
656 }
657}
658
659BpRefBase::~BpRefBase()
660{
661 if (mRemote) {
Bailey Forrest6913c462015-08-18 17:15:10 -0700662 if (!(mState.load(std::memory_order_relaxed)&kRemoteAcquired)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800663 mRemote->decStrong(this);
664 }
665 mRefs->decWeak(this);
666 }
667}
668
669void BpRefBase::onFirstRef()
670{
Bailey Forrest6913c462015-08-18 17:15:10 -0700671 mState.fetch_or(kRemoteAcquired, std::memory_order_relaxed);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800672}
673
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800674void BpRefBase::onLastStrongRef(const void* /*id*/)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800675{
676 if (mRemote) {
677 mRemote->decStrong(this);
678 }
679}
680
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800681bool BpRefBase::onIncStrongAttempted(uint32_t /*flags*/, const void* /*id*/)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800682{
683 return mRemote ? mRefs->attemptIncStrong(this) : false;
684}
685
686// ---------------------------------------------------------------------------
687
Steven Moreland61ff8492019-09-26 16:05:45 -0700688} // namespace android