blob: 194be218e86995ad53be032f10fdf5c665d090b4 [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 Hong34823232021-06-07 17:23:00 -0700153status_t IBinder::setRpcClientDebug(android::base::unique_fd socketFd) {
Yifan Hong84bedeb2021-04-21 21:37:17 -0700154 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 Hong34823232021-06-07 17:23:00 -0700161 return local->BBinder::setRpcClientDebug(std::move(socketFd));
Yifan Hong84bedeb2021-04-21 21:37:17 -0700162 }
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 Hong84bedeb2021-04-21 21:37:17 -0700176 return transact(SET_RPC_CLIENT_TRANSACTION, data, &reply);
177}
178
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800179// ---------------------------------------------------------------------------
180
181class BBinder::Extras
182{
183public:
Steven Morelandf0212002018-12-26 13:59:23 -0800184 // unlocked objects
185 bool mRequestingSid = false;
Steven Morelandcf03cf12020-12-04 02:58:40 +0000186 bool mInheritRt = false;
Steven Morelandb8ad08d2019-08-09 14:42:56 -0700187 sp<IBinder> mExtension;
Steven Morelandbf1915b2020-07-16 22:43:02 +0000188 int mPolicy = SCHED_NORMAL;
189 int mPriority = 0;
Steven Morelandf0212002018-12-26 13:59:23 -0800190
191 // for below objects
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800192 Mutex mLock;
Yifan Hong84bedeb2021-04-21 21:37:17 -0700193 sp<RpcServer> mRpcServer;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800194 BpBinder::ObjectManager mObjects;
195};
196
197// ---------------------------------------------------------------------------
198
Kalesh Singhd67c8e82020-12-29 15:46:25 -0500199BBinder::BBinder() : mExtras(nullptr), mStability(0), mParceled(false) {}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800200
201bool BBinder::isBinderAlive() const
202{
203 return true;
204}
205
206status_t BBinder::pingBinder()
207{
208 return NO_ERROR;
209}
210
Mathias Agopian83c04462009-05-22 19:00:22 -0700211const String16& BBinder::getInterfaceDescriptor() const
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800212{
Dan Egnor386a3322010-05-06 00:55:09 -0700213 // This is a local static rather than a global static,
214 // to avoid static initializer ordering issues.
215 static String16 sEmptyDescriptor;
Steve Block32397c12012-01-05 23:22:43 +0000216 ALOGW("reached BBinder::getInterfaceDescriptor (this=%p)", this);
Mathias Agopian83c04462009-05-22 19:00:22 -0700217 return sEmptyDescriptor;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800218}
219
Jiyong Parkb86c8662018-10-29 23:01:57 +0900220// NOLINTNEXTLINE(google-default-arguments)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800221status_t BBinder::transact(
222 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
223{
224 data.setDataPosition(0);
225
Steven Morelandf183fdd2020-10-27 00:12:12 +0000226 if (reply != nullptr && (flags & FLAG_CLEAR_BUF)) {
227 reply->markSensitive();
228 }
229
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800230 status_t err = NO_ERROR;
231 switch (code) {
232 case PING_TRANSACTION:
Steven Moreland6e69d652019-07-10 14:17:55 -0700233 err = pingBinder();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800234 break;
Steven Morelandb8ad08d2019-08-09 14:42:56 -0700235 case EXTENSION_TRANSACTION:
236 err = reply->writeStrongBinder(getExtension());
237 break;
Steven Moreland86080b82019-09-23 15:41:18 -0700238 case DEBUG_PID_TRANSACTION:
239 err = reply->writeInt32(getDebugPid());
240 break;
Yifan Hong84bedeb2021-04-21 21:37:17 -0700241 case SET_RPC_CLIENT_TRANSACTION: {
242 err = setRpcClientDebug(data);
243 break;
244 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800245 default:
246 err = onTransact(code, data, reply, flags);
247 break;
248 }
249
Steven Morelanda86a3562019-08-01 23:28:34 +0000250 // In case this is being transacted on in the same process.
Yi Kongfdd8da92018-06-07 17:52:27 -0700251 if (reply != nullptr) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800252 reply->setDataPosition(0);
253 }
254
255 return err;
256}
257
Jiyong Parkb86c8662018-10-29 23:01:57 +0900258// NOLINTNEXTLINE(google-default-arguments)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800259status_t BBinder::linkToDeath(
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800260 const sp<DeathRecipient>& /*recipient*/, void* /*cookie*/,
261 uint32_t /*flags*/)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800262{
263 return INVALID_OPERATION;
264}
265
Jiyong Parkb86c8662018-10-29 23:01:57 +0900266// NOLINTNEXTLINE(google-default-arguments)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800267status_t BBinder::unlinkToDeath(
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800268 const wp<DeathRecipient>& /*recipient*/, void* /*cookie*/,
269 uint32_t /*flags*/, wp<DeathRecipient>* /*outRecipient*/)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800270{
271 return INVALID_OPERATION;
272}
273
Dianne Hackborn23eb1e22015-10-07 17:35:27 -0700274status_t BBinder::dump(int /*fd*/, const Vector<String16>& /*args*/)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800275{
276 return NO_ERROR;
277}
278
279void BBinder::attachObject(
280 const void* objectID, void* object, void* cleanupCookie,
281 object_cleanup_func func)
282{
Steven Morelandf0212002018-12-26 13:59:23 -0800283 Extras* e = getOrCreateExtras();
284 if (!e) return; // out of memory
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800285
286 AutoMutex _l(e->mLock);
287 e->mObjects.attach(objectID, object, cleanupCookie, func);
288}
289
290void* BBinder::findObject(const void* objectID) const
291{
Bailey Forrest6913c462015-08-18 17:15:10 -0700292 Extras* e = mExtras.load(std::memory_order_acquire);
Yi Kongfdd8da92018-06-07 17:52:27 -0700293 if (!e) return nullptr;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800294
295 AutoMutex _l(e->mLock);
296 return e->mObjects.find(objectID);
297}
298
299void BBinder::detachObject(const void* objectID)
300{
Bailey Forrest6913c462015-08-18 17:15:10 -0700301 Extras* e = mExtras.load(std::memory_order_acquire);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800302 if (!e) return;
303
304 AutoMutex _l(e->mLock);
305 e->mObjects.detach(objectID);
306}
307
308BBinder* BBinder::localBinder()
309{
310 return this;
311}
312
Steven Morelandf0212002018-12-26 13:59:23 -0800313bool BBinder::isRequestingSid()
314{
315 Extras* e = mExtras.load(std::memory_order_acquire);
316
317 return e && e->mRequestingSid;
318}
319
320void BBinder::setRequestingSid(bool requestingSid)
321{
Kalesh Singhd67c8e82020-12-29 15:46:25 -0500322 ALOGW_IF(mParceled,
323 "setRequestingSid() should not be called after a binder object "
324 "is parceled/sent to another process");
325
Steven Morelandf0212002018-12-26 13:59:23 -0800326 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 Moreland3668be62019-02-08 17:56:55 -0800338 e->mRequestingSid = requestingSid;
Steven Morelandf0212002018-12-26 13:59:23 -0800339}
340
Steven Morelandb8ad08d2019-08-09 14:42:56 -0700341sp<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 Morelandbf1915b2020-07-16 22:43:02 +0000347void BBinder::setMinSchedulerPolicy(int policy, int priority) {
Kalesh Singhd67c8e82020-12-29 15:46:25 -0500348 ALOGW_IF(mParceled,
349 "setMinSchedulerPolicy() should not be called after a binder object "
350 "is parceled/sent to another process");
351
Steven Morelandbf1915b2020-07-16 22:43:02 +0000352 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
380int BBinder::getMinSchedulerPolicy() {
381 Extras* e = mExtras.load(std::memory_order_acquire);
382 if (e == nullptr) return SCHED_NORMAL;
383 return e->mPolicy;
384}
385
386int BBinder::getMinSchedulerPriority() {
387 Extras* e = mExtras.load(std::memory_order_acquire);
388 if (e == nullptr) return 0;
389 return e->mPriority;
390}
391
Steven Morelandcf03cf12020-12-04 02:58:40 +0000392bool BBinder::isInheritRt() {
393 Extras* e = mExtras.load(std::memory_order_acquire);
394
395 return e && e->mInheritRt;
396}
397
398void BBinder::setInheritRt(bool inheritRt) {
Kalesh Singhd67c8e82020-12-29 15:46:25 -0500399 ALOGW_IF(mParceled,
400 "setInheritRt() should not be called after a binder object "
401 "is parceled/sent to another process");
402
Steven Morelandcf03cf12020-12-04 02:58:40 +0000403 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 Moreland86080b82019-09-23 15:41:18 -0700417pid_t BBinder::getDebugPid() {
418 return getpid();
419}
420
Steven Morelandb8ad08d2019-08-09 14:42:56 -0700421void BBinder::setExtension(const sp<IBinder>& extension) {
Kalesh Singhd67c8e82020-12-29 15:46:25 -0500422 ALOGW_IF(mParceled,
423 "setExtension() should not be called after a binder object "
424 "is parceled/sent to another process");
425
Steven Morelandb8ad08d2019-08-09 14:42:56 -0700426 Extras* e = getOrCreateExtras();
427 e->mExtension = extension;
428}
429
Kalesh Singhd67c8e82020-12-29 15:46:25 -0500430bool BBinder::wasParceled() {
431 return mParceled;
432}
433
434void BBinder::setParceled() {
435 mParceled = true;
436}
437
Yifan Hong84bedeb2021-04-21 21:37:17 -0700438status_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 Hong84bedeb2021-04-21 21:37:17 -0700451
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 Hong84bedeb2021-04-21 21:37:17 -0700456
Yifan Hong34823232021-06-07 17:23:00 -0700457 return setRpcClientDebug(std::move(clientFd));
Yifan Hong84bedeb2021-04-21 21:37:17 -0700458}
459
Yifan Hong34823232021-06-07 17:23:00 -0700460status_t BBinder::setRpcClientDebug(android::base::unique_fd socketFd) {
Yifan Hong84bedeb2021-04-21 21:37:17 -0700461 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 Hong34823232021-06-07 17:23:00 -0700467 LOG_RPC_DETAIL("%s(fd=%d)", __PRETTY_FUNCTION__, socketFdForPrint);
Yifan Hong84bedeb2021-04-21 21:37:17 -0700468
469 if (!socketFd.ok()) {
470 ALOGE("%s: No socket FD provided.", __PRETTY_FUNCTION__);
471 return BAD_VALUE;
472 }
Yifan Hong84bedeb2021-04-21 21:37:17 -0700473
Yifan Hong84bedeb2021-04-21 21:37:17 -0700474 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));
Yifan Hong7a20a3f2021-06-09 13:22:37 -0700494 e->mRpcServer->setMaxThreads(binderThreadPoolMaxCount);
Yifan Hong84bedeb2021-04-21 21:37:17 -0700495 e->mRpcServer->start();
Yifan Hong34823232021-06-07 17:23:00 -0700496 LOG_RPC_DETAIL("%s(fd=%d) successful", __PRETTY_FUNCTION__, socketFdForPrint);
Yifan Hong84bedeb2021-04-21 21:37:17 -0700497 return OK;
498}
499
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800500BBinder::~BBinder()
501{
Bailey Forrest6913c462015-08-18 17:15:10 -0700502 Extras* e = mExtras.load(std::memory_order_relaxed);
Hans Boehm3effaba2014-08-12 22:56:00 +0000503 if (e) delete e;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800504}
505
506
Jiyong Parkb86c8662018-10-29 23:01:57 +0900507// NOLINTNEXTLINE(google-default-arguments)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800508status_t BBinder::onTransact(
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800509 uint32_t code, const Parcel& data, Parcel* reply, uint32_t /*flags*/)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800510{
511 switch (code) {
512 case INTERFACE_TRANSACTION:
513 reply->writeString16(getInterfaceDescriptor());
514 return NO_ERROR;
515
516 case DUMP_TRANSACTION: {
517 int fd = data.readFileDescriptor();
518 int argc = data.readInt32();
519 Vector<String16> args;
520 for (int i = 0; i < argc && data.dataAvail() > 0; i++) {
521 args.add(data.readString16());
522 }
523 return dump(fd, args);
524 }
Dianne Hackborn555f89d2012-05-08 18:54:22 -0700525
Dianne Hackborn23eb1e22015-10-07 17:35:27 -0700526 case SHELL_COMMAND_TRANSACTION: {
527 int in = data.readFileDescriptor();
528 int out = data.readFileDescriptor();
529 int err = data.readFileDescriptor();
530 int argc = data.readInt32();
531 Vector<String16> args;
532 for (int i = 0; i < argc && data.dataAvail() > 0; i++) {
533 args.add(data.readString16());
534 }
Dianne Hackborn1941a402016-08-29 12:30:43 -0700535 sp<IShellCallback> shellCallback = IShellCallback::asInterface(
536 data.readStrongBinder());
Dianne Hackborn23eb1e22015-10-07 17:35:27 -0700537 sp<IResultReceiver> resultReceiver = IResultReceiver::asInterface(
538 data.readStrongBinder());
539
Dianne Hackbornf2bf93b2015-10-14 15:13:02 -0700540 // XXX can't add virtuals until binaries are updated.
541 //return shellCommand(in, out, err, args, resultReceiver);
Christopher Wiley0a9a1c12016-07-20 08:28:14 -0700542 (void)in;
543 (void)out;
544 (void)err;
545
Yi Kongfdd8da92018-06-07 17:52:27 -0700546 if (resultReceiver != nullptr) {
Dianne Hackbornf2bf93b2015-10-14 15:13:02 -0700547 resultReceiver->send(INVALID_OPERATION);
548 }
Martijn Coenenaa6ee992017-08-17 15:38:08 +0200549
550 return NO_ERROR;
Dianne Hackborn23eb1e22015-10-07 17:35:27 -0700551 }
552
Dianne Hackborn555f89d2012-05-08 18:54:22 -0700553 case SYSPROPS_TRANSACTION: {
554 report_sysprop_change();
555 return NO_ERROR;
556 }
557
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800558 default:
559 return UNKNOWN_TRANSACTION;
560 }
561}
562
Steven Morelandf0212002018-12-26 13:59:23 -0800563BBinder::Extras* BBinder::getOrCreateExtras()
564{
565 Extras* e = mExtras.load(std::memory_order_acquire);
566
567 if (!e) {
568 e = new Extras;
569 Extras* expected = nullptr;
570 if (!mExtras.compare_exchange_strong(expected, e,
571 std::memory_order_release,
572 std::memory_order_acquire)) {
573 delete e;
574 e = expected; // Filled in by CAS
575 }
576 if (e == nullptr) return nullptr; // out of memory
577 }
578
579 return e;
580}
581
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800582// ---------------------------------------------------------------------------
583
584enum {
585 // This is used to transfer ownership of the remote binder from
586 // the BpRefBase object holding it (when it is constructed), to the
587 // owner of the BpRefBase object when it first acquires that BpRefBase.
588 kRemoteAcquired = 0x00000001
589};
590
591BpRefBase::BpRefBase(const sp<IBinder>& o)
Yi Kongfdd8da92018-06-07 17:52:27 -0700592 : mRemote(o.get()), mRefs(nullptr), mState(0)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800593{
594 extendObjectLifetime(OBJECT_LIFETIME_WEAK);
595
596 if (mRemote) {
597 mRemote->incStrong(this); // Removed on first IncStrong().
598 mRefs = mRemote->createWeak(this); // Held for our entire lifetime.
599 }
600}
601
602BpRefBase::~BpRefBase()
603{
604 if (mRemote) {
Bailey Forrest6913c462015-08-18 17:15:10 -0700605 if (!(mState.load(std::memory_order_relaxed)&kRemoteAcquired)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800606 mRemote->decStrong(this);
607 }
608 mRefs->decWeak(this);
609 }
610}
611
612void BpRefBase::onFirstRef()
613{
Bailey Forrest6913c462015-08-18 17:15:10 -0700614 mState.fetch_or(kRemoteAcquired, std::memory_order_relaxed);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800615}
616
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800617void BpRefBase::onLastStrongRef(const void* /*id*/)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800618{
619 if (mRemote) {
620 mRemote->decStrong(this);
621 }
622}
623
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800624bool BpRefBase::onIncStrongAttempted(uint32_t /*flags*/, const void* /*id*/)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800625{
626 return mRemote ? mRefs->attemptIncStrong(this) : false;
627}
628
629// ---------------------------------------------------------------------------
630
Steven Moreland61ff8492019-09-26 16:05:45 -0700631} // namespace android