blob: 415b44e6833b296349db112d6cc21e5c69bbb95b [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 Hong84bedeb2021-04-21 21:37:17 -0700153status_t IBinder::setRpcClientDebug(android::base::unique_fd socketFd, uint32_t maxRpcThreads) {
154 if constexpr (!kEnableRpcDevServers) {
155 ALOGW("setRpcClientDebug disallowed because RPC is not enabled");
156 return INVALID_OPERATION;
157 }
158
159 BBinder* local = this->localBinder();
160 if (local != nullptr) {
161 return local->BBinder::setRpcClientDebug(std::move(socketFd), maxRpcThreads);
162 }
163
164 BpBinder* proxy = this->remoteBinder();
165 LOG_ALWAYS_FATAL_IF(proxy == nullptr, "binder object must be either local or remote");
166
167 Parcel data;
168 Parcel reply;
169 status_t status;
170 if (status = data.writeBool(socketFd.ok()); status != OK) return status;
171 if (socketFd.ok()) {
172 // writeUniqueFileDescriptor currently makes an unnecessary dup().
173 status = data.writeFileDescriptor(socketFd.release(), true /* own */);
174 if (status != OK) return status;
175 }
176 if (status = data.writeUint32(maxRpcThreads); status != OK) return status;
177 return transact(SET_RPC_CLIENT_TRANSACTION, data, &reply);
178}
179
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800180// ---------------------------------------------------------------------------
181
182class BBinder::Extras
183{
184public:
Steven Morelandf0212002018-12-26 13:59:23 -0800185 // unlocked objects
186 bool mRequestingSid = false;
Steven Morelandcf03cf12020-12-04 02:58:40 +0000187 bool mInheritRt = false;
Steven Morelandb8ad08d2019-08-09 14:42:56 -0700188 sp<IBinder> mExtension;
Steven Morelandbf1915b2020-07-16 22:43:02 +0000189 int mPolicy = SCHED_NORMAL;
190 int mPriority = 0;
Steven Morelandf0212002018-12-26 13:59:23 -0800191
192 // for below objects
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800193 Mutex mLock;
Yifan Hong84bedeb2021-04-21 21:37:17 -0700194 sp<RpcServer> mRpcServer;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800195 BpBinder::ObjectManager mObjects;
196};
197
198// ---------------------------------------------------------------------------
199
Kalesh Singhd67c8e82020-12-29 15:46:25 -0500200BBinder::BBinder() : mExtras(nullptr), mStability(0), mParceled(false) {}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800201
202bool BBinder::isBinderAlive() const
203{
204 return true;
205}
206
207status_t BBinder::pingBinder()
208{
209 return NO_ERROR;
210}
211
Mathias Agopian83c04462009-05-22 19:00:22 -0700212const String16& BBinder::getInterfaceDescriptor() const
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800213{
Dan Egnor386a3322010-05-06 00:55:09 -0700214 // This is a local static rather than a global static,
215 // to avoid static initializer ordering issues.
216 static String16 sEmptyDescriptor;
Steve Block32397c12012-01-05 23:22:43 +0000217 ALOGW("reached BBinder::getInterfaceDescriptor (this=%p)", this);
Mathias Agopian83c04462009-05-22 19:00:22 -0700218 return sEmptyDescriptor;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800219}
220
Jiyong Parkb86c8662018-10-29 23:01:57 +0900221// NOLINTNEXTLINE(google-default-arguments)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800222status_t BBinder::transact(
223 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
224{
225 data.setDataPosition(0);
226
Steven Morelandf183fdd2020-10-27 00:12:12 +0000227 if (reply != nullptr && (flags & FLAG_CLEAR_BUF)) {
228 reply->markSensitive();
229 }
230
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800231 status_t err = NO_ERROR;
232 switch (code) {
233 case PING_TRANSACTION:
Steven Moreland6e69d652019-07-10 14:17:55 -0700234 err = pingBinder();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800235 break;
Steven Morelandb8ad08d2019-08-09 14:42:56 -0700236 case EXTENSION_TRANSACTION:
237 err = reply->writeStrongBinder(getExtension());
238 break;
Steven Moreland86080b82019-09-23 15:41:18 -0700239 case DEBUG_PID_TRANSACTION:
240 err = reply->writeInt32(getDebugPid());
241 break;
Yifan Hong84bedeb2021-04-21 21:37:17 -0700242 case SET_RPC_CLIENT_TRANSACTION: {
243 err = setRpcClientDebug(data);
244 break;
245 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800246 default:
247 err = onTransact(code, data, reply, flags);
248 break;
249 }
250
Steven Morelanda86a3562019-08-01 23:28:34 +0000251 // In case this is being transacted on in the same process.
Yi Kongfdd8da92018-06-07 17:52:27 -0700252 if (reply != nullptr) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800253 reply->setDataPosition(0);
254 }
255
256 return err;
257}
258
Jiyong Parkb86c8662018-10-29 23:01:57 +0900259// NOLINTNEXTLINE(google-default-arguments)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800260status_t BBinder::linkToDeath(
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800261 const sp<DeathRecipient>& /*recipient*/, void* /*cookie*/,
262 uint32_t /*flags*/)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800263{
264 return INVALID_OPERATION;
265}
266
Jiyong Parkb86c8662018-10-29 23:01:57 +0900267// NOLINTNEXTLINE(google-default-arguments)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800268status_t BBinder::unlinkToDeath(
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800269 const wp<DeathRecipient>& /*recipient*/, void* /*cookie*/,
270 uint32_t /*flags*/, wp<DeathRecipient>* /*outRecipient*/)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800271{
272 return INVALID_OPERATION;
273}
274
Dianne Hackborn23eb1e22015-10-07 17:35:27 -0700275status_t BBinder::dump(int /*fd*/, const Vector<String16>& /*args*/)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800276{
277 return NO_ERROR;
278}
279
280void BBinder::attachObject(
281 const void* objectID, void* object, void* cleanupCookie,
282 object_cleanup_func func)
283{
Steven Morelandf0212002018-12-26 13:59:23 -0800284 Extras* e = getOrCreateExtras();
285 if (!e) return; // out of memory
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800286
287 AutoMutex _l(e->mLock);
288 e->mObjects.attach(objectID, object, cleanupCookie, func);
289}
290
291void* BBinder::findObject(const void* objectID) const
292{
Bailey Forrest6913c462015-08-18 17:15:10 -0700293 Extras* e = mExtras.load(std::memory_order_acquire);
Yi Kongfdd8da92018-06-07 17:52:27 -0700294 if (!e) return nullptr;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800295
296 AutoMutex _l(e->mLock);
297 return e->mObjects.find(objectID);
298}
299
300void BBinder::detachObject(const void* objectID)
301{
Bailey Forrest6913c462015-08-18 17:15:10 -0700302 Extras* e = mExtras.load(std::memory_order_acquire);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800303 if (!e) return;
304
305 AutoMutex _l(e->mLock);
306 e->mObjects.detach(objectID);
307}
308
309BBinder* BBinder::localBinder()
310{
311 return this;
312}
313
Steven Morelandf0212002018-12-26 13:59:23 -0800314bool BBinder::isRequestingSid()
315{
316 Extras* e = mExtras.load(std::memory_order_acquire);
317
318 return e && e->mRequestingSid;
319}
320
321void BBinder::setRequestingSid(bool requestingSid)
322{
Kalesh Singhd67c8e82020-12-29 15:46:25 -0500323 ALOGW_IF(mParceled,
324 "setRequestingSid() should not be called after a binder object "
325 "is parceled/sent to another process");
326
Steven Morelandf0212002018-12-26 13:59:23 -0800327 Extras* e = mExtras.load(std::memory_order_acquire);
328
329 if (!e) {
330 // default is false. Most things don't need sids, so avoiding allocations when possible.
331 if (!requestingSid) {
332 return;
333 }
334
335 e = getOrCreateExtras();
336 if (!e) return; // out of memory
337 }
338
Steven Moreland3668be62019-02-08 17:56:55 -0800339 e->mRequestingSid = requestingSid;
Steven Morelandf0212002018-12-26 13:59:23 -0800340}
341
Steven Morelandb8ad08d2019-08-09 14:42:56 -0700342sp<IBinder> BBinder::getExtension() {
343 Extras* e = mExtras.load(std::memory_order_acquire);
344 if (e == nullptr) return nullptr;
345 return e->mExtension;
346}
347
Steven Morelandbf1915b2020-07-16 22:43:02 +0000348void BBinder::setMinSchedulerPolicy(int policy, int priority) {
Kalesh Singhd67c8e82020-12-29 15:46:25 -0500349 ALOGW_IF(mParceled,
350 "setMinSchedulerPolicy() should not be called after a binder object "
351 "is parceled/sent to another process");
352
Steven Morelandbf1915b2020-07-16 22:43:02 +0000353 switch (policy) {
354 case SCHED_NORMAL:
355 LOG_ALWAYS_FATAL_IF(priority < -20 || priority > 19, "Invalid priority for SCHED_NORMAL: %d", priority);
356 break;
357 case SCHED_RR:
358 case SCHED_FIFO:
359 LOG_ALWAYS_FATAL_IF(priority < 1 || priority > 99, "Invalid priority for sched %d: %d", policy, priority);
360 break;
361 default:
362 LOG_ALWAYS_FATAL("Unrecognized scheduling policy: %d", policy);
363 }
364
365 Extras* e = mExtras.load(std::memory_order_acquire);
366
367 if (e == nullptr) {
368 // Avoid allocations if called with default.
369 if (policy == SCHED_NORMAL && priority == 0) {
370 return;
371 }
372
373 e = getOrCreateExtras();
374 if (!e) return; // out of memory
375 }
376
377 e->mPolicy = policy;
378 e->mPriority = priority;
379}
380
381int BBinder::getMinSchedulerPolicy() {
382 Extras* e = mExtras.load(std::memory_order_acquire);
383 if (e == nullptr) return SCHED_NORMAL;
384 return e->mPolicy;
385}
386
387int BBinder::getMinSchedulerPriority() {
388 Extras* e = mExtras.load(std::memory_order_acquire);
389 if (e == nullptr) return 0;
390 return e->mPriority;
391}
392
Steven Morelandcf03cf12020-12-04 02:58:40 +0000393bool BBinder::isInheritRt() {
394 Extras* e = mExtras.load(std::memory_order_acquire);
395
396 return e && e->mInheritRt;
397}
398
399void BBinder::setInheritRt(bool inheritRt) {
Kalesh Singhd67c8e82020-12-29 15:46:25 -0500400 ALOGW_IF(mParceled,
401 "setInheritRt() should not be called after a binder object "
402 "is parceled/sent to another process");
403
Steven Morelandcf03cf12020-12-04 02:58:40 +0000404 Extras* e = mExtras.load(std::memory_order_acquire);
405
406 if (!e) {
407 if (!inheritRt) {
408 return;
409 }
410
411 e = getOrCreateExtras();
412 if (!e) return; // out of memory
413 }
414
415 e->mInheritRt = inheritRt;
416}
417
Steven Moreland86080b82019-09-23 15:41:18 -0700418pid_t BBinder::getDebugPid() {
419 return getpid();
420}
421
Steven Morelandb8ad08d2019-08-09 14:42:56 -0700422void BBinder::setExtension(const sp<IBinder>& extension) {
Kalesh Singhd67c8e82020-12-29 15:46:25 -0500423 ALOGW_IF(mParceled,
424 "setExtension() should not be called after a binder object "
425 "is parceled/sent to another process");
426
Steven Morelandb8ad08d2019-08-09 14:42:56 -0700427 Extras* e = getOrCreateExtras();
428 e->mExtension = extension;
429}
430
Kalesh Singhd67c8e82020-12-29 15:46:25 -0500431bool BBinder::wasParceled() {
432 return mParceled;
433}
434
435void BBinder::setParceled() {
436 mParceled = true;
437}
438
Yifan Hong84bedeb2021-04-21 21:37:17 -0700439status_t BBinder::setRpcClientDebug(const Parcel& data) {
440 if constexpr (!kEnableRpcDevServers) {
441 ALOGW("%s: disallowed because RPC is not enabled", __PRETTY_FUNCTION__);
442 return INVALID_OPERATION;
443 }
444 uid_t uid = IPCThreadState::self()->getCallingUid();
445 if (uid != AID_ROOT) {
446 ALOGE("%s: not allowed because client %" PRIu32 " is not root", __PRETTY_FUNCTION__, uid);
447 return PERMISSION_DENIED;
448 }
449 status_t status;
450 bool hasSocketFd;
451 android::base::unique_fd clientFd;
452 uint32_t maxRpcThreads;
453
454 if (status = data.readBool(&hasSocketFd); status != OK) return status;
455 if (hasSocketFd) {
456 if (status = data.readUniqueFileDescriptor(&clientFd); status != OK) return status;
457 }
458 if (status = data.readUint32(&maxRpcThreads); status != OK) return status;
459
460 return setRpcClientDebug(std::move(clientFd), maxRpcThreads);
461}
462
463status_t BBinder::setRpcClientDebug(android::base::unique_fd socketFd, uint32_t maxRpcThreads) {
464 if constexpr (!kEnableRpcDevServers) {
465 ALOGW("%s: disallowed because RPC is not enabled", __PRETTY_FUNCTION__);
466 return INVALID_OPERATION;
467 }
468
469 const int socketFdForPrint = socketFd.get();
470 LOG_RPC_DETAIL("%s(%d, %" PRIu32 ")", __PRETTY_FUNCTION__, socketFdForPrint, maxRpcThreads);
471
472 if (!socketFd.ok()) {
473 ALOGE("%s: No socket FD provided.", __PRETTY_FUNCTION__);
474 return BAD_VALUE;
475 }
476 if (maxRpcThreads <= 0) {
477 ALOGE("%s: RPC is useless with %" PRIu32 " threads.", __PRETTY_FUNCTION__, maxRpcThreads);
478 return BAD_VALUE;
479 }
480
481 // TODO(b/182914638): RPC and binder should share the same thread pool count.
482 size_t binderThreadPoolMaxCount = ProcessState::self()->getThreadPoolMaxThreadCount();
483 if (binderThreadPoolMaxCount <= 1) {
484 ALOGE("%s: ProcessState thread pool max count is %zu. RPC is disabled for this service "
485 "because RPC requires the service to support multithreading.",
486 __PRETTY_FUNCTION__, binderThreadPoolMaxCount);
487 return INVALID_OPERATION;
488 }
489
490 Extras* e = getOrCreateExtras();
491 AutoMutex _l(e->mLock);
492 if (e->mRpcServer != nullptr) {
493 ALOGE("%s: Already have RPC client", __PRETTY_FUNCTION__);
494 return ALREADY_EXISTS;
495 }
496 e->mRpcServer = RpcServer::make();
497 LOG_ALWAYS_FATAL_IF(e->mRpcServer == nullptr, "RpcServer::make returns null");
498 e->mRpcServer->iUnderstandThisCodeIsExperimentalAndIWillNotUseItInProduction();
499 // Weak ref to avoid circular dependency: BBinder -> RpcServer -X-> BBinder
500 e->mRpcServer->setRootObjectWeak(wp<BBinder>::fromExisting(this));
501 e->mRpcServer->setupExternalServer(std::move(socketFd));
502 e->mRpcServer->start();
503 LOG_RPC_DETAIL("%s(%d, %" PRIu32 ") successful", __PRETTY_FUNCTION__, socketFdForPrint,
504 maxRpcThreads);
505 return OK;
506}
507
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800508BBinder::~BBinder()
509{
Bailey Forrest6913c462015-08-18 17:15:10 -0700510 Extras* e = mExtras.load(std::memory_order_relaxed);
Hans Boehm3effaba2014-08-12 22:56:00 +0000511 if (e) delete e;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800512}
513
514
Jiyong Parkb86c8662018-10-29 23:01:57 +0900515// NOLINTNEXTLINE(google-default-arguments)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800516status_t BBinder::onTransact(
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800517 uint32_t code, const Parcel& data, Parcel* reply, uint32_t /*flags*/)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800518{
519 switch (code) {
520 case INTERFACE_TRANSACTION:
521 reply->writeString16(getInterfaceDescriptor());
522 return NO_ERROR;
523
524 case DUMP_TRANSACTION: {
525 int fd = data.readFileDescriptor();
526 int argc = data.readInt32();
527 Vector<String16> args;
528 for (int i = 0; i < argc && data.dataAvail() > 0; i++) {
529 args.add(data.readString16());
530 }
531 return dump(fd, args);
532 }
Dianne Hackborn555f89d2012-05-08 18:54:22 -0700533
Dianne Hackborn23eb1e22015-10-07 17:35:27 -0700534 case SHELL_COMMAND_TRANSACTION: {
535 int in = data.readFileDescriptor();
536 int out = data.readFileDescriptor();
537 int err = data.readFileDescriptor();
538 int argc = data.readInt32();
539 Vector<String16> args;
540 for (int i = 0; i < argc && data.dataAvail() > 0; i++) {
541 args.add(data.readString16());
542 }
Dianne Hackborn1941a402016-08-29 12:30:43 -0700543 sp<IShellCallback> shellCallback = IShellCallback::asInterface(
544 data.readStrongBinder());
Dianne Hackborn23eb1e22015-10-07 17:35:27 -0700545 sp<IResultReceiver> resultReceiver = IResultReceiver::asInterface(
546 data.readStrongBinder());
547
Dianne Hackbornf2bf93b2015-10-14 15:13:02 -0700548 // XXX can't add virtuals until binaries are updated.
549 //return shellCommand(in, out, err, args, resultReceiver);
Christopher Wiley0a9a1c12016-07-20 08:28:14 -0700550 (void)in;
551 (void)out;
552 (void)err;
553
Yi Kongfdd8da92018-06-07 17:52:27 -0700554 if (resultReceiver != nullptr) {
Dianne Hackbornf2bf93b2015-10-14 15:13:02 -0700555 resultReceiver->send(INVALID_OPERATION);
556 }
Martijn Coenenaa6ee992017-08-17 15:38:08 +0200557
558 return NO_ERROR;
Dianne Hackborn23eb1e22015-10-07 17:35:27 -0700559 }
560
Dianne Hackborn555f89d2012-05-08 18:54:22 -0700561 case SYSPROPS_TRANSACTION: {
562 report_sysprop_change();
563 return NO_ERROR;
564 }
565
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800566 default:
567 return UNKNOWN_TRANSACTION;
568 }
569}
570
Steven Morelandf0212002018-12-26 13:59:23 -0800571BBinder::Extras* BBinder::getOrCreateExtras()
572{
573 Extras* e = mExtras.load(std::memory_order_acquire);
574
575 if (!e) {
576 e = new Extras;
577 Extras* expected = nullptr;
578 if (!mExtras.compare_exchange_strong(expected, e,
579 std::memory_order_release,
580 std::memory_order_acquire)) {
581 delete e;
582 e = expected; // Filled in by CAS
583 }
584 if (e == nullptr) return nullptr; // out of memory
585 }
586
587 return e;
588}
589
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800590// ---------------------------------------------------------------------------
591
592enum {
593 // This is used to transfer ownership of the remote binder from
594 // the BpRefBase object holding it (when it is constructed), to the
595 // owner of the BpRefBase object when it first acquires that BpRefBase.
596 kRemoteAcquired = 0x00000001
597};
598
599BpRefBase::BpRefBase(const sp<IBinder>& o)
Yi Kongfdd8da92018-06-07 17:52:27 -0700600 : mRemote(o.get()), mRefs(nullptr), mState(0)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800601{
602 extendObjectLifetime(OBJECT_LIFETIME_WEAK);
603
604 if (mRemote) {
605 mRemote->incStrong(this); // Removed on first IncStrong().
606 mRefs = mRemote->createWeak(this); // Held for our entire lifetime.
607 }
608}
609
610BpRefBase::~BpRefBase()
611{
612 if (mRemote) {
Bailey Forrest6913c462015-08-18 17:15:10 -0700613 if (!(mState.load(std::memory_order_relaxed)&kRemoteAcquired)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800614 mRemote->decStrong(this);
615 }
616 mRefs->decWeak(this);
617 }
618}
619
620void BpRefBase::onFirstRef()
621{
Bailey Forrest6913c462015-08-18 17:15:10 -0700622 mState.fetch_or(kRemoteAcquired, std::memory_order_relaxed);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800623}
624
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800625void BpRefBase::onLastStrongRef(const void* /*id*/)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800626{
627 if (mRemote) {
628 mRemote->decStrong(this);
629 }
630}
631
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800632bool BpRefBase::onIncStrongAttempted(uint32_t /*flags*/, const void* /*id*/)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800633{
634 return mRemote ? mRefs->attemptIncStrong(this) : false;
635}
636
637// ---------------------------------------------------------------------------
638
Steven Moreland61ff8492019-09-26 16:05:45 -0700639} // namespace android