blob: 19a22a51b2f989e71a833121d2ee88498359ee78 [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 Hong84bedeb2021-04-21 21:37:17 -070020
21#include <android-base/unique_fd.h>
Mathias Agopianc5b2c0b2009-05-19 19:08:10 -070022#include <binder/BpBinder.h>
23#include <binder/IInterface.h>
Yifan Hong84bedeb2021-04-21 21:37:17 -070024#include <binder/IPCThreadState.h>
Dianne Hackborn23eb1e22015-10-07 17:35:27 -070025#include <binder/IResultReceiver.h>
Dianne Hackborn1941a402016-08-29 12:30:43 -070026#include <binder/IShellCallback.h>
Mathias Agopianc5b2c0b2009-05-19 19:08:10 -070027#include <binder/Parcel.h>
Yifan Hong84bedeb2021-04-21 21:37:17 -070028#include <binder/RpcServer.h>
29#include <private/android_filesystem_config.h>
30#include <utils/misc.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080031
Yifan Hong84bedeb2021-04-21 21:37:17 -070032#include <inttypes.h>
Steven Morelandbf1915b2020-07-16 22:43:02 +000033#include <linux/sched.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080034#include <stdio.h>
35
Yifan Hong84bedeb2021-04-21 21:37:17 -070036#include "RpcState.h"
37
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080038namespace android {
39
Steven Moreland90c1f9a2021-05-03 18:27:24 +000040// Service implementations inherit from BBinder and IBinder, and this is frozen
41// in prebuilts.
42#ifdef __LP64__
43static_assert(sizeof(IBinder) == 24);
44static_assert(sizeof(BBinder) == 40);
45#else
46static_assert(sizeof(IBinder) == 12);
47static_assert(sizeof(BBinder) == 20);
48#endif
49
Yifan Hong84bedeb2021-04-21 21:37:17 -070050#ifdef BINDER_RPC_DEV_SERVERS
51constexpr const bool kEnableRpcDevServers = true;
52#else
53constexpr const bool kEnableRpcDevServers = false;
54#endif
55
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080056// ---------------------------------------------------------------------------
57
Mathias Agopian83c04462009-05-22 19:00:22 -070058IBinder::IBinder()
59 : RefBase()
60{
61}
62
63IBinder::~IBinder()
64{
65}
66
67// ---------------------------------------------------------------------------
68
Colin Cross6f4f3ab2014-02-05 17:42:44 -080069sp<IInterface> IBinder::queryLocalInterface(const String16& /*descriptor*/)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080070{
Yi Kongfdd8da92018-06-07 17:52:27 -070071 return nullptr;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080072}
73
74BBinder* IBinder::localBinder()
75{
Yi Kongfdd8da92018-06-07 17:52:27 -070076 return nullptr;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080077}
78
79BpBinder* IBinder::remoteBinder()
80{
Yi Kongfdd8da92018-06-07 17:52:27 -070081 return nullptr;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080082}
83
84bool IBinder::checkSubclass(const void* /*subclassID*/) const
85{
86 return false;
87}
88
Dianne Hackborn23eb1e22015-10-07 17:35:27 -070089
Dianne Hackbornf2bf93b2015-10-14 15:13:02 -070090status_t IBinder::shellCommand(const sp<IBinder>& target, int in, int out, int err,
Dianne Hackborn1941a402016-08-29 12:30:43 -070091 Vector<String16>& args, const sp<IShellCallback>& callback,
92 const sp<IResultReceiver>& resultReceiver)
Dianne Hackborn23eb1e22015-10-07 17:35:27 -070093{
Dianne Hackbornf2bf93b2015-10-14 15:13:02 -070094 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 Hackborn23eb1e22015-10-07 17:35:27 -0700103 }
Yi Kongfdd8da92018-06-07 17:52:27 -0700104 send.writeStrongBinder(callback != nullptr ? IInterface::asBinder(callback) : nullptr);
105 send.writeStrongBinder(resultReceiver != nullptr ? IInterface::asBinder(resultReceiver) : nullptr);
Dianne Hackbornf2bf93b2015-10-14 15:13:02 -0700106 return target->transact(SHELL_COMMAND_TRANSACTION, send, &reply);
Dianne Hackborn23eb1e22015-10-07 17:35:27 -0700107}
108
Steven Morelandb8ad08d2019-08-09 14:42:56 -0700109status_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 Moreland86080b82019-09-23 15:41:18 -0700127status_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 Hong02530ec2021-06-10 13:38:38 -0700153status_t IBinder::setRpcClientDebug(android::base::unique_fd socketFd,
154 const sp<IBinder>& keepAliveBinder) {
Yifan Hong84bedeb2021-04-21 21:37:17 -0700155 if constexpr (!kEnableRpcDevServers) {
156 ALOGW("setRpcClientDebug disallowed because RPC is not enabled");
157 return INVALID_OPERATION;
158 }
159
160 BBinder* local = this->localBinder();
161 if (local != nullptr) {
Yifan Hong02530ec2021-06-10 13:38:38 -0700162 return local->BBinder::setRpcClientDebug(std::move(socketFd), keepAliveBinder);
Yifan Hong84bedeb2021-04-21 21:37:17 -0700163 }
164
165 BpBinder* proxy = this->remoteBinder();
166 LOG_ALWAYS_FATAL_IF(proxy == nullptr, "binder object must be either local or remote");
167
168 Parcel data;
169 Parcel reply;
170 status_t status;
171 if (status = data.writeBool(socketFd.ok()); status != OK) return status;
172 if (socketFd.ok()) {
173 // writeUniqueFileDescriptor currently makes an unnecessary dup().
174 status = data.writeFileDescriptor(socketFd.release(), true /* own */);
175 if (status != OK) return status;
176 }
Yifan Hong02530ec2021-06-10 13:38:38 -0700177 if (status = data.writeStrongBinder(keepAliveBinder); status != OK) return status;
Yifan Hong84bedeb2021-04-21 21:37:17 -0700178 return transact(SET_RPC_CLIENT_TRANSACTION, data, &reply);
179}
180
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800181// ---------------------------------------------------------------------------
182
183class BBinder::Extras
184{
185public:
Steven Morelandf0212002018-12-26 13:59:23 -0800186 // unlocked objects
187 bool mRequestingSid = false;
Steven Morelandcf03cf12020-12-04 02:58:40 +0000188 bool mInheritRt = false;
Steven Morelandb8ad08d2019-08-09 14:42:56 -0700189 sp<IBinder> mExtension;
Steven Morelandbf1915b2020-07-16 22:43:02 +0000190 int mPolicy = SCHED_NORMAL;
191 int mPriority = 0;
Steven Morelandf0212002018-12-26 13:59:23 -0800192
193 // for below objects
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800194 Mutex mLock;
Yifan Hong84bedeb2021-04-21 21:37:17 -0700195 sp<RpcServer> mRpcServer;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800196 BpBinder::ObjectManager mObjects;
197};
198
199// ---------------------------------------------------------------------------
200
Kalesh Singhd67c8e82020-12-29 15:46:25 -0500201BBinder::BBinder() : mExtras(nullptr), mStability(0), mParceled(false) {}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800202
203bool BBinder::isBinderAlive() const
204{
205 return true;
206}
207
208status_t BBinder::pingBinder()
209{
210 return NO_ERROR;
211}
212
Mathias Agopian83c04462009-05-22 19:00:22 -0700213const String16& BBinder::getInterfaceDescriptor() const
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800214{
Dan Egnor386a3322010-05-06 00:55:09 -0700215 // This is a local static rather than a global static,
216 // to avoid static initializer ordering issues.
217 static String16 sEmptyDescriptor;
Steve Block32397c12012-01-05 23:22:43 +0000218 ALOGW("reached BBinder::getInterfaceDescriptor (this=%p)", this);
Mathias Agopian83c04462009-05-22 19:00:22 -0700219 return sEmptyDescriptor;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800220}
221
Jiyong Parkb86c8662018-10-29 23:01:57 +0900222// NOLINTNEXTLINE(google-default-arguments)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800223status_t BBinder::transact(
224 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
225{
226 data.setDataPosition(0);
227
Steven Morelandf183fdd2020-10-27 00:12:12 +0000228 if (reply != nullptr && (flags & FLAG_CLEAR_BUF)) {
229 reply->markSensitive();
230 }
231
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800232 status_t err = NO_ERROR;
233 switch (code) {
234 case PING_TRANSACTION:
Steven Moreland6e69d652019-07-10 14:17:55 -0700235 err = pingBinder();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800236 break;
Steven Morelandb8ad08d2019-08-09 14:42:56 -0700237 case EXTENSION_TRANSACTION:
238 err = reply->writeStrongBinder(getExtension());
239 break;
Steven Moreland86080b82019-09-23 15:41:18 -0700240 case DEBUG_PID_TRANSACTION:
241 err = reply->writeInt32(getDebugPid());
242 break;
Yifan Hong84bedeb2021-04-21 21:37:17 -0700243 case SET_RPC_CLIENT_TRANSACTION: {
244 err = setRpcClientDebug(data);
245 break;
246 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800247 default:
248 err = onTransact(code, data, reply, flags);
249 break;
250 }
251
Steven Morelanda86a3562019-08-01 23:28:34 +0000252 // In case this is being transacted on in the same process.
Yi Kongfdd8da92018-06-07 17:52:27 -0700253 if (reply != nullptr) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800254 reply->setDataPosition(0);
255 }
256
257 return err;
258}
259
Jiyong Parkb86c8662018-10-29 23:01:57 +0900260// NOLINTNEXTLINE(google-default-arguments)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800261status_t BBinder::linkToDeath(
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800262 const sp<DeathRecipient>& /*recipient*/, void* /*cookie*/,
263 uint32_t /*flags*/)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800264{
265 return INVALID_OPERATION;
266}
267
Jiyong Parkb86c8662018-10-29 23:01:57 +0900268// NOLINTNEXTLINE(google-default-arguments)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800269status_t BBinder::unlinkToDeath(
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800270 const wp<DeathRecipient>& /*recipient*/, void* /*cookie*/,
271 uint32_t /*flags*/, wp<DeathRecipient>* /*outRecipient*/)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800272{
273 return INVALID_OPERATION;
274}
275
Dianne Hackborn23eb1e22015-10-07 17:35:27 -0700276status_t BBinder::dump(int /*fd*/, const Vector<String16>& /*args*/)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800277{
278 return NO_ERROR;
279}
280
281void BBinder::attachObject(
282 const void* objectID, void* object, void* cleanupCookie,
283 object_cleanup_func func)
284{
Steven Morelandf0212002018-12-26 13:59:23 -0800285 Extras* e = getOrCreateExtras();
286 if (!e) return; // out of memory
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800287
288 AutoMutex _l(e->mLock);
289 e->mObjects.attach(objectID, object, cleanupCookie, func);
290}
291
292void* BBinder::findObject(const void* objectID) const
293{
Bailey Forrest6913c462015-08-18 17:15:10 -0700294 Extras* e = mExtras.load(std::memory_order_acquire);
Yi Kongfdd8da92018-06-07 17:52:27 -0700295 if (!e) return nullptr;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800296
297 AutoMutex _l(e->mLock);
298 return e->mObjects.find(objectID);
299}
300
301void BBinder::detachObject(const void* objectID)
302{
Bailey Forrest6913c462015-08-18 17:15:10 -0700303 Extras* e = mExtras.load(std::memory_order_acquire);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800304 if (!e) return;
305
306 AutoMutex _l(e->mLock);
307 e->mObjects.detach(objectID);
308}
309
310BBinder* BBinder::localBinder()
311{
312 return this;
313}
314
Steven Morelandf0212002018-12-26 13:59:23 -0800315bool BBinder::isRequestingSid()
316{
317 Extras* e = mExtras.load(std::memory_order_acquire);
318
319 return e && e->mRequestingSid;
320}
321
322void BBinder::setRequestingSid(bool requestingSid)
323{
Kalesh Singhd67c8e82020-12-29 15:46:25 -0500324 ALOGW_IF(mParceled,
325 "setRequestingSid() should not be called after a binder object "
326 "is parceled/sent to another process");
327
Steven Morelandf0212002018-12-26 13:59:23 -0800328 Extras* e = mExtras.load(std::memory_order_acquire);
329
330 if (!e) {
331 // default is false. Most things don't need sids, so avoiding allocations when possible.
332 if (!requestingSid) {
333 return;
334 }
335
336 e = getOrCreateExtras();
337 if (!e) return; // out of memory
338 }
339
Steven Moreland3668be62019-02-08 17:56:55 -0800340 e->mRequestingSid = requestingSid;
Steven Morelandf0212002018-12-26 13:59:23 -0800341}
342
Steven Morelandb8ad08d2019-08-09 14:42:56 -0700343sp<IBinder> BBinder::getExtension() {
344 Extras* e = mExtras.load(std::memory_order_acquire);
345 if (e == nullptr) return nullptr;
346 return e->mExtension;
347}
348
Steven Morelandbf1915b2020-07-16 22:43:02 +0000349void BBinder::setMinSchedulerPolicy(int policy, int priority) {
Kalesh Singhd67c8e82020-12-29 15:46:25 -0500350 ALOGW_IF(mParceled,
351 "setMinSchedulerPolicy() should not be called after a binder object "
352 "is parceled/sent to another process");
353
Steven Morelandbf1915b2020-07-16 22:43:02 +0000354 switch (policy) {
355 case SCHED_NORMAL:
356 LOG_ALWAYS_FATAL_IF(priority < -20 || priority > 19, "Invalid priority for SCHED_NORMAL: %d", priority);
357 break;
358 case SCHED_RR:
359 case SCHED_FIFO:
360 LOG_ALWAYS_FATAL_IF(priority < 1 || priority > 99, "Invalid priority for sched %d: %d", policy, priority);
361 break;
362 default:
363 LOG_ALWAYS_FATAL("Unrecognized scheduling policy: %d", policy);
364 }
365
366 Extras* e = mExtras.load(std::memory_order_acquire);
367
368 if (e == nullptr) {
369 // Avoid allocations if called with default.
370 if (policy == SCHED_NORMAL && priority == 0) {
371 return;
372 }
373
374 e = getOrCreateExtras();
375 if (!e) return; // out of memory
376 }
377
378 e->mPolicy = policy;
379 e->mPriority = priority;
380}
381
382int BBinder::getMinSchedulerPolicy() {
383 Extras* e = mExtras.load(std::memory_order_acquire);
384 if (e == nullptr) return SCHED_NORMAL;
385 return e->mPolicy;
386}
387
388int BBinder::getMinSchedulerPriority() {
389 Extras* e = mExtras.load(std::memory_order_acquire);
390 if (e == nullptr) return 0;
391 return e->mPriority;
392}
393
Steven Morelandcf03cf12020-12-04 02:58:40 +0000394bool BBinder::isInheritRt() {
395 Extras* e = mExtras.load(std::memory_order_acquire);
396
397 return e && e->mInheritRt;
398}
399
400void BBinder::setInheritRt(bool inheritRt) {
Kalesh Singhd67c8e82020-12-29 15:46:25 -0500401 ALOGW_IF(mParceled,
402 "setInheritRt() should not be called after a binder object "
403 "is parceled/sent to another process");
404
Steven Morelandcf03cf12020-12-04 02:58:40 +0000405 Extras* e = mExtras.load(std::memory_order_acquire);
406
407 if (!e) {
408 if (!inheritRt) {
409 return;
410 }
411
412 e = getOrCreateExtras();
413 if (!e) return; // out of memory
414 }
415
416 e->mInheritRt = inheritRt;
417}
418
Steven Moreland86080b82019-09-23 15:41:18 -0700419pid_t BBinder::getDebugPid() {
420 return getpid();
421}
422
Steven Morelandb8ad08d2019-08-09 14:42:56 -0700423void BBinder::setExtension(const sp<IBinder>& extension) {
Kalesh Singhd67c8e82020-12-29 15:46:25 -0500424 ALOGW_IF(mParceled,
425 "setExtension() should not be called after a binder object "
426 "is parceled/sent to another process");
427
Steven Morelandb8ad08d2019-08-09 14:42:56 -0700428 Extras* e = getOrCreateExtras();
429 e->mExtension = extension;
430}
431
Kalesh Singhd67c8e82020-12-29 15:46:25 -0500432bool BBinder::wasParceled() {
433 return mParceled;
434}
435
436void BBinder::setParceled() {
437 mParceled = true;
438}
439
Yifan Hong84bedeb2021-04-21 21:37:17 -0700440status_t BBinder::setRpcClientDebug(const Parcel& data) {
441 if constexpr (!kEnableRpcDevServers) {
442 ALOGW("%s: disallowed because RPC is not enabled", __PRETTY_FUNCTION__);
443 return INVALID_OPERATION;
444 }
445 uid_t uid = IPCThreadState::self()->getCallingUid();
446 if (uid != AID_ROOT) {
447 ALOGE("%s: not allowed because client %" PRIu32 " is not root", __PRETTY_FUNCTION__, uid);
448 return PERMISSION_DENIED;
449 }
450 status_t status;
451 bool hasSocketFd;
452 android::base::unique_fd clientFd;
Yifan Hong84bedeb2021-04-21 21:37:17 -0700453
454 if (status = data.readBool(&hasSocketFd); status != OK) return status;
455 if (hasSocketFd) {
456 if (status = data.readUniqueFileDescriptor(&clientFd); status != OK) return status;
457 }
Yifan Hong02530ec2021-06-10 13:38:38 -0700458 sp<IBinder> keepAliveBinder;
459 if (status = data.readNullableStrongBinder(&keepAliveBinder); status != OK) return status;
Yifan Hong84bedeb2021-04-21 21:37:17 -0700460
Yifan Hong02530ec2021-06-10 13:38:38 -0700461 return setRpcClientDebug(std::move(clientFd), keepAliveBinder);
Yifan Hong84bedeb2021-04-21 21:37:17 -0700462}
463
Yifan Hong02530ec2021-06-10 13:38:38 -0700464status_t BBinder::setRpcClientDebug(android::base::unique_fd socketFd,
465 const sp<IBinder>& keepAliveBinder) {
Yifan Hong84bedeb2021-04-21 21:37:17 -0700466 if constexpr (!kEnableRpcDevServers) {
467 ALOGW("%s: disallowed because RPC is not enabled", __PRETTY_FUNCTION__);
468 return INVALID_OPERATION;
469 }
470
471 const int socketFdForPrint = socketFd.get();
Yifan Hong34823232021-06-07 17:23:00 -0700472 LOG_RPC_DETAIL("%s(fd=%d)", __PRETTY_FUNCTION__, socketFdForPrint);
Yifan Hong84bedeb2021-04-21 21:37:17 -0700473
474 if (!socketFd.ok()) {
475 ALOGE("%s: No socket FD provided.", __PRETTY_FUNCTION__);
476 return BAD_VALUE;
477 }
Yifan Hong84bedeb2021-04-21 21:37:17 -0700478
Yifan Hong02530ec2021-06-10 13:38:38 -0700479 if (keepAliveBinder == nullptr) {
480 ALOGE("%s: No keepAliveBinder provided.", __PRETTY_FUNCTION__);
481 return UNEXPECTED_NULL;
482 }
483
Yifan Hong84bedeb2021-04-21 21:37:17 -0700484 size_t binderThreadPoolMaxCount = ProcessState::self()->getThreadPoolMaxThreadCount();
485 if (binderThreadPoolMaxCount <= 1) {
486 ALOGE("%s: ProcessState thread pool max count is %zu. RPC is disabled for this service "
487 "because RPC requires the service to support multithreading.",
488 __PRETTY_FUNCTION__, binderThreadPoolMaxCount);
489 return INVALID_OPERATION;
490 }
491
492 Extras* e = getOrCreateExtras();
493 AutoMutex _l(e->mLock);
494 if (e->mRpcServer != nullptr) {
495 ALOGE("%s: Already have RPC client", __PRETTY_FUNCTION__);
496 return ALREADY_EXISTS;
497 }
498 e->mRpcServer = RpcServer::make();
499 LOG_ALWAYS_FATAL_IF(e->mRpcServer == nullptr, "RpcServer::make returns null");
500 e->mRpcServer->iUnderstandThisCodeIsExperimentalAndIWillNotUseItInProduction();
501 // Weak ref to avoid circular dependency: BBinder -> RpcServer -X-> BBinder
502 e->mRpcServer->setRootObjectWeak(wp<BBinder>::fromExisting(this));
503 e->mRpcServer->setupExternalServer(std::move(socketFd));
Yifan Hong7a20a3f2021-06-09 13:22:37 -0700504 e->mRpcServer->setMaxThreads(binderThreadPoolMaxCount);
Yifan Hong84bedeb2021-04-21 21:37:17 -0700505 e->mRpcServer->start();
Yifan Hong34823232021-06-07 17:23:00 -0700506 LOG_RPC_DETAIL("%s(fd=%d) successful", __PRETTY_FUNCTION__, socketFdForPrint);
Yifan Hong84bedeb2021-04-21 21:37:17 -0700507 return OK;
508}
509
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800510BBinder::~BBinder()
511{
Bailey Forrest6913c462015-08-18 17:15:10 -0700512 Extras* e = mExtras.load(std::memory_order_relaxed);
Hans Boehm3effaba2014-08-12 22:56:00 +0000513 if (e) delete e;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800514}
515
516
Jiyong Parkb86c8662018-10-29 23:01:57 +0900517// NOLINTNEXTLINE(google-default-arguments)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800518status_t BBinder::onTransact(
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800519 uint32_t code, const Parcel& data, Parcel* reply, uint32_t /*flags*/)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800520{
521 switch (code) {
522 case INTERFACE_TRANSACTION:
523 reply->writeString16(getInterfaceDescriptor());
524 return NO_ERROR;
525
526 case DUMP_TRANSACTION: {
527 int fd = data.readFileDescriptor();
528 int argc = data.readInt32();
529 Vector<String16> args;
530 for (int i = 0; i < argc && data.dataAvail() > 0; i++) {
531 args.add(data.readString16());
532 }
533 return dump(fd, args);
534 }
Dianne Hackborn555f89d2012-05-08 18:54:22 -0700535
Dianne Hackborn23eb1e22015-10-07 17:35:27 -0700536 case SHELL_COMMAND_TRANSACTION: {
537 int in = data.readFileDescriptor();
538 int out = data.readFileDescriptor();
539 int err = data.readFileDescriptor();
540 int argc = data.readInt32();
541 Vector<String16> args;
542 for (int i = 0; i < argc && data.dataAvail() > 0; i++) {
543 args.add(data.readString16());
544 }
Dianne Hackborn1941a402016-08-29 12:30:43 -0700545 sp<IShellCallback> shellCallback = IShellCallback::asInterface(
546 data.readStrongBinder());
Dianne Hackborn23eb1e22015-10-07 17:35:27 -0700547 sp<IResultReceiver> resultReceiver = IResultReceiver::asInterface(
548 data.readStrongBinder());
549
Dianne Hackbornf2bf93b2015-10-14 15:13:02 -0700550 // XXX can't add virtuals until binaries are updated.
551 //return shellCommand(in, out, err, args, resultReceiver);
Christopher Wiley0a9a1c12016-07-20 08:28:14 -0700552 (void)in;
553 (void)out;
554 (void)err;
555
Yi Kongfdd8da92018-06-07 17:52:27 -0700556 if (resultReceiver != nullptr) {
Dianne Hackbornf2bf93b2015-10-14 15:13:02 -0700557 resultReceiver->send(INVALID_OPERATION);
558 }
Martijn Coenenaa6ee992017-08-17 15:38:08 +0200559
560 return NO_ERROR;
Dianne Hackborn23eb1e22015-10-07 17:35:27 -0700561 }
562
Dianne Hackborn555f89d2012-05-08 18:54:22 -0700563 case SYSPROPS_TRANSACTION: {
564 report_sysprop_change();
565 return NO_ERROR;
566 }
567
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800568 default:
569 return UNKNOWN_TRANSACTION;
570 }
571}
572
Steven Morelandf0212002018-12-26 13:59:23 -0800573BBinder::Extras* BBinder::getOrCreateExtras()
574{
575 Extras* e = mExtras.load(std::memory_order_acquire);
576
577 if (!e) {
578 e = new Extras;
579 Extras* expected = nullptr;
580 if (!mExtras.compare_exchange_strong(expected, e,
581 std::memory_order_release,
582 std::memory_order_acquire)) {
583 delete e;
584 e = expected; // Filled in by CAS
585 }
586 if (e == nullptr) return nullptr; // out of memory
587 }
588
589 return e;
590}
591
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800592// ---------------------------------------------------------------------------
593
594enum {
595 // This is used to transfer ownership of the remote binder from
596 // the BpRefBase object holding it (when it is constructed), to the
597 // owner of the BpRefBase object when it first acquires that BpRefBase.
598 kRemoteAcquired = 0x00000001
599};
600
601BpRefBase::BpRefBase(const sp<IBinder>& o)
Yi Kongfdd8da92018-06-07 17:52:27 -0700602 : mRemote(o.get()), mRefs(nullptr), mState(0)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800603{
604 extendObjectLifetime(OBJECT_LIFETIME_WEAK);
605
606 if (mRemote) {
607 mRemote->incStrong(this); // Removed on first IncStrong().
608 mRefs = mRemote->createWeak(this); // Held for our entire lifetime.
609 }
610}
611
612BpRefBase::~BpRefBase()
613{
614 if (mRemote) {
Bailey Forrest6913c462015-08-18 17:15:10 -0700615 if (!(mState.load(std::memory_order_relaxed)&kRemoteAcquired)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800616 mRemote->decStrong(this);
617 }
618 mRefs->decWeak(this);
619 }
620}
621
622void BpRefBase::onFirstRef()
623{
Bailey Forrest6913c462015-08-18 17:15:10 -0700624 mState.fetch_or(kRemoteAcquired, std::memory_order_relaxed);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800625}
626
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800627void BpRefBase::onLastStrongRef(const void* /*id*/)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800628{
629 if (mRemote) {
630 mRemote->decStrong(this);
631 }
632}
633
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800634bool BpRefBase::onIncStrongAttempted(uint32_t /*flags*/, const void* /*id*/)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800635{
636 return mRemote ? mRefs->attemptIncStrong(this) : false;
637}
638
639// ---------------------------------------------------------------------------
640
Steven Moreland61ff8492019-09-26 16:05:45 -0700641} // namespace android