blob: c83c3835132aa5e30d898274e76c99dee3c47626 [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
Steven Morelanda7fb0182020-02-26 16:02:08 -0800200BBinder::BBinder() : mExtras(nullptr), mStability(0)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800201{
202}
203
204bool BBinder::isBinderAlive() const
205{
206 return true;
207}
208
209status_t BBinder::pingBinder()
210{
211 return NO_ERROR;
212}
213
Mathias Agopian83c04462009-05-22 19:00:22 -0700214const String16& BBinder::getInterfaceDescriptor() const
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800215{
Dan Egnor386a3322010-05-06 00:55:09 -0700216 // This is a local static rather than a global static,
217 // to avoid static initializer ordering issues.
218 static String16 sEmptyDescriptor;
Steve Block32397c12012-01-05 23:22:43 +0000219 ALOGW("reached BBinder::getInterfaceDescriptor (this=%p)", this);
Mathias Agopian83c04462009-05-22 19:00:22 -0700220 return sEmptyDescriptor;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800221}
222
Jiyong Parkb86c8662018-10-29 23:01:57 +0900223// NOLINTNEXTLINE(google-default-arguments)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800224status_t BBinder::transact(
225 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
226{
227 data.setDataPosition(0);
228
Steven Morelandf183fdd2020-10-27 00:12:12 +0000229 if (reply != nullptr && (flags & FLAG_CLEAR_BUF)) {
230 reply->markSensitive();
231 }
232
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800233 status_t err = NO_ERROR;
234 switch (code) {
235 case PING_TRANSACTION:
Steven Moreland6e69d652019-07-10 14:17:55 -0700236 err = pingBinder();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800237 break;
Steven Morelandb8ad08d2019-08-09 14:42:56 -0700238 case EXTENSION_TRANSACTION:
239 err = reply->writeStrongBinder(getExtension());
240 break;
Steven Moreland86080b82019-09-23 15:41:18 -0700241 case DEBUG_PID_TRANSACTION:
242 err = reply->writeInt32(getDebugPid());
243 break;
Yifan Hong84bedeb2021-04-21 21:37:17 -0700244 case SET_RPC_CLIENT_TRANSACTION: {
245 err = setRpcClientDebug(data);
246 break;
247 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800248 default:
249 err = onTransact(code, data, reply, flags);
250 break;
251 }
252
Steven Morelanda86a3562019-08-01 23:28:34 +0000253 // In case this is being transacted on in the same process.
Yi Kongfdd8da92018-06-07 17:52:27 -0700254 if (reply != nullptr) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800255 reply->setDataPosition(0);
256 }
257
258 return err;
259}
260
Jiyong Parkb86c8662018-10-29 23:01:57 +0900261// NOLINTNEXTLINE(google-default-arguments)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800262status_t BBinder::linkToDeath(
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800263 const sp<DeathRecipient>& /*recipient*/, void* /*cookie*/,
264 uint32_t /*flags*/)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800265{
266 return INVALID_OPERATION;
267}
268
Jiyong Parkb86c8662018-10-29 23:01:57 +0900269// NOLINTNEXTLINE(google-default-arguments)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800270status_t BBinder::unlinkToDeath(
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800271 const wp<DeathRecipient>& /*recipient*/, void* /*cookie*/,
272 uint32_t /*flags*/, wp<DeathRecipient>* /*outRecipient*/)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800273{
274 return INVALID_OPERATION;
275}
276
Dianne Hackborn23eb1e22015-10-07 17:35:27 -0700277status_t BBinder::dump(int /*fd*/, const Vector<String16>& /*args*/)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800278{
279 return NO_ERROR;
280}
281
282void BBinder::attachObject(
283 const void* objectID, void* object, void* cleanupCookie,
284 object_cleanup_func func)
285{
Steven Morelandf0212002018-12-26 13:59:23 -0800286 Extras* e = getOrCreateExtras();
287 if (!e) return; // out of memory
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800288
289 AutoMutex _l(e->mLock);
290 e->mObjects.attach(objectID, object, cleanupCookie, func);
291}
292
293void* BBinder::findObject(const void* objectID) const
294{
Bailey Forrest6913c462015-08-18 17:15:10 -0700295 Extras* e = mExtras.load(std::memory_order_acquire);
Yi Kongfdd8da92018-06-07 17:52:27 -0700296 if (!e) return nullptr;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800297
298 AutoMutex _l(e->mLock);
299 return e->mObjects.find(objectID);
300}
301
302void BBinder::detachObject(const void* objectID)
303{
Bailey Forrest6913c462015-08-18 17:15:10 -0700304 Extras* e = mExtras.load(std::memory_order_acquire);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800305 if (!e) return;
306
307 AutoMutex _l(e->mLock);
308 e->mObjects.detach(objectID);
309}
310
311BBinder* BBinder::localBinder()
312{
313 return this;
314}
315
Steven Morelandf0212002018-12-26 13:59:23 -0800316bool BBinder::isRequestingSid()
317{
318 Extras* e = mExtras.load(std::memory_order_acquire);
319
320 return e && e->mRequestingSid;
321}
322
323void BBinder::setRequestingSid(bool requestingSid)
324{
325 Extras* e = mExtras.load(std::memory_order_acquire);
326
327 if (!e) {
328 // default is false. Most things don't need sids, so avoiding allocations when possible.
329 if (!requestingSid) {
330 return;
331 }
332
333 e = getOrCreateExtras();
334 if (!e) return; // out of memory
335 }
336
Steven Moreland3668be62019-02-08 17:56:55 -0800337 e->mRequestingSid = requestingSid;
Steven Morelandf0212002018-12-26 13:59:23 -0800338}
339
Steven Morelandb8ad08d2019-08-09 14:42:56 -0700340sp<IBinder> BBinder::getExtension() {
341 Extras* e = mExtras.load(std::memory_order_acquire);
342 if (e == nullptr) return nullptr;
343 return e->mExtension;
344}
345
Steven Morelandbf1915b2020-07-16 22:43:02 +0000346void BBinder::setMinSchedulerPolicy(int policy, int priority) {
347 switch (policy) {
348 case SCHED_NORMAL:
349 LOG_ALWAYS_FATAL_IF(priority < -20 || priority > 19, "Invalid priority for SCHED_NORMAL: %d", priority);
350 break;
351 case SCHED_RR:
352 case SCHED_FIFO:
353 LOG_ALWAYS_FATAL_IF(priority < 1 || priority > 99, "Invalid priority for sched %d: %d", policy, priority);
354 break;
355 default:
356 LOG_ALWAYS_FATAL("Unrecognized scheduling policy: %d", policy);
357 }
358
359 Extras* e = mExtras.load(std::memory_order_acquire);
360
361 if (e == nullptr) {
362 // Avoid allocations if called with default.
363 if (policy == SCHED_NORMAL && priority == 0) {
364 return;
365 }
366
367 e = getOrCreateExtras();
368 if (!e) return; // out of memory
369 }
370
371 e->mPolicy = policy;
372 e->mPriority = priority;
373}
374
375int BBinder::getMinSchedulerPolicy() {
376 Extras* e = mExtras.load(std::memory_order_acquire);
377 if (e == nullptr) return SCHED_NORMAL;
378 return e->mPolicy;
379}
380
381int BBinder::getMinSchedulerPriority() {
382 Extras* e = mExtras.load(std::memory_order_acquire);
383 if (e == nullptr) return 0;
384 return e->mPriority;
385}
386
Steven Morelandcf03cf12020-12-04 02:58:40 +0000387bool BBinder::isInheritRt() {
388 Extras* e = mExtras.load(std::memory_order_acquire);
389
390 return e && e->mInheritRt;
391}
392
393void BBinder::setInheritRt(bool inheritRt) {
394 Extras* e = mExtras.load(std::memory_order_acquire);
395
396 if (!e) {
397 if (!inheritRt) {
398 return;
399 }
400
401 e = getOrCreateExtras();
402 if (!e) return; // out of memory
403 }
404
405 e->mInheritRt = inheritRt;
406}
407
Steven Moreland86080b82019-09-23 15:41:18 -0700408pid_t BBinder::getDebugPid() {
409 return getpid();
410}
411
Steven Morelandb8ad08d2019-08-09 14:42:56 -0700412void BBinder::setExtension(const sp<IBinder>& extension) {
413 Extras* e = getOrCreateExtras();
414 e->mExtension = extension;
415}
416
Yifan Hong84bedeb2021-04-21 21:37:17 -0700417status_t BBinder::setRpcClientDebug(const Parcel& data) {
418 if constexpr (!kEnableRpcDevServers) {
419 ALOGW("%s: disallowed because RPC is not enabled", __PRETTY_FUNCTION__);
420 return INVALID_OPERATION;
421 }
422 uid_t uid = IPCThreadState::self()->getCallingUid();
423 if (uid != AID_ROOT) {
424 ALOGE("%s: not allowed because client %" PRIu32 " is not root", __PRETTY_FUNCTION__, uid);
425 return PERMISSION_DENIED;
426 }
427 status_t status;
428 bool hasSocketFd;
429 android::base::unique_fd clientFd;
430 uint32_t maxRpcThreads;
431
432 if (status = data.readBool(&hasSocketFd); status != OK) return status;
433 if (hasSocketFd) {
434 if (status = data.readUniqueFileDescriptor(&clientFd); status != OK) return status;
435 }
436 if (status = data.readUint32(&maxRpcThreads); status != OK) return status;
437
438 return setRpcClientDebug(std::move(clientFd), maxRpcThreads);
439}
440
441status_t BBinder::setRpcClientDebug(android::base::unique_fd socketFd, uint32_t maxRpcThreads) {
442 if constexpr (!kEnableRpcDevServers) {
443 ALOGW("%s: disallowed because RPC is not enabled", __PRETTY_FUNCTION__);
444 return INVALID_OPERATION;
445 }
446
447 const int socketFdForPrint = socketFd.get();
448 LOG_RPC_DETAIL("%s(%d, %" PRIu32 ")", __PRETTY_FUNCTION__, socketFdForPrint, maxRpcThreads);
449
450 if (!socketFd.ok()) {
451 ALOGE("%s: No socket FD provided.", __PRETTY_FUNCTION__);
452 return BAD_VALUE;
453 }
454 if (maxRpcThreads <= 0) {
455 ALOGE("%s: RPC is useless with %" PRIu32 " threads.", __PRETTY_FUNCTION__, maxRpcThreads);
456 return BAD_VALUE;
457 }
458
459 // TODO(b/182914638): RPC and binder should share the same thread pool count.
460 size_t binderThreadPoolMaxCount = ProcessState::self()->getThreadPoolMaxThreadCount();
461 if (binderThreadPoolMaxCount <= 1) {
462 ALOGE("%s: ProcessState thread pool max count is %zu. RPC is disabled for this service "
463 "because RPC requires the service to support multithreading.",
464 __PRETTY_FUNCTION__, binderThreadPoolMaxCount);
465 return INVALID_OPERATION;
466 }
467
468 Extras* e = getOrCreateExtras();
469 AutoMutex _l(e->mLock);
470 if (e->mRpcServer != nullptr) {
471 ALOGE("%s: Already have RPC client", __PRETTY_FUNCTION__);
472 return ALREADY_EXISTS;
473 }
474 e->mRpcServer = RpcServer::make();
475 LOG_ALWAYS_FATAL_IF(e->mRpcServer == nullptr, "RpcServer::make returns null");
476 e->mRpcServer->iUnderstandThisCodeIsExperimentalAndIWillNotUseItInProduction();
477 // Weak ref to avoid circular dependency: BBinder -> RpcServer -X-> BBinder
478 e->mRpcServer->setRootObjectWeak(wp<BBinder>::fromExisting(this));
479 e->mRpcServer->setupExternalServer(std::move(socketFd));
480 e->mRpcServer->start();
481 LOG_RPC_DETAIL("%s(%d, %" PRIu32 ") successful", __PRETTY_FUNCTION__, socketFdForPrint,
482 maxRpcThreads);
483 return OK;
484}
485
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800486BBinder::~BBinder()
487{
Bailey Forrest6913c462015-08-18 17:15:10 -0700488 Extras* e = mExtras.load(std::memory_order_relaxed);
Hans Boehm3effaba2014-08-12 22:56:00 +0000489 if (e) delete e;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800490}
491
492
Jiyong Parkb86c8662018-10-29 23:01:57 +0900493// NOLINTNEXTLINE(google-default-arguments)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800494status_t BBinder::onTransact(
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800495 uint32_t code, const Parcel& data, Parcel* reply, uint32_t /*flags*/)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800496{
497 switch (code) {
498 case INTERFACE_TRANSACTION:
499 reply->writeString16(getInterfaceDescriptor());
500 return NO_ERROR;
501
502 case DUMP_TRANSACTION: {
503 int fd = data.readFileDescriptor();
504 int argc = data.readInt32();
505 Vector<String16> args;
506 for (int i = 0; i < argc && data.dataAvail() > 0; i++) {
507 args.add(data.readString16());
508 }
509 return dump(fd, args);
510 }
Dianne Hackborn555f89d2012-05-08 18:54:22 -0700511
Dianne Hackborn23eb1e22015-10-07 17:35:27 -0700512 case SHELL_COMMAND_TRANSACTION: {
513 int in = data.readFileDescriptor();
514 int out = data.readFileDescriptor();
515 int err = data.readFileDescriptor();
516 int argc = data.readInt32();
517 Vector<String16> args;
518 for (int i = 0; i < argc && data.dataAvail() > 0; i++) {
519 args.add(data.readString16());
520 }
Dianne Hackborn1941a402016-08-29 12:30:43 -0700521 sp<IShellCallback> shellCallback = IShellCallback::asInterface(
522 data.readStrongBinder());
Dianne Hackborn23eb1e22015-10-07 17:35:27 -0700523 sp<IResultReceiver> resultReceiver = IResultReceiver::asInterface(
524 data.readStrongBinder());
525
Dianne Hackbornf2bf93b2015-10-14 15:13:02 -0700526 // XXX can't add virtuals until binaries are updated.
527 //return shellCommand(in, out, err, args, resultReceiver);
Christopher Wiley0a9a1c12016-07-20 08:28:14 -0700528 (void)in;
529 (void)out;
530 (void)err;
531
Yi Kongfdd8da92018-06-07 17:52:27 -0700532 if (resultReceiver != nullptr) {
Dianne Hackbornf2bf93b2015-10-14 15:13:02 -0700533 resultReceiver->send(INVALID_OPERATION);
534 }
Martijn Coenenaa6ee992017-08-17 15:38:08 +0200535
536 return NO_ERROR;
Dianne Hackborn23eb1e22015-10-07 17:35:27 -0700537 }
538
Dianne Hackborn555f89d2012-05-08 18:54:22 -0700539 case SYSPROPS_TRANSACTION: {
540 report_sysprop_change();
541 return NO_ERROR;
542 }
543
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800544 default:
545 return UNKNOWN_TRANSACTION;
546 }
547}
548
Steven Morelandf0212002018-12-26 13:59:23 -0800549BBinder::Extras* BBinder::getOrCreateExtras()
550{
551 Extras* e = mExtras.load(std::memory_order_acquire);
552
553 if (!e) {
554 e = new Extras;
555 Extras* expected = nullptr;
556 if (!mExtras.compare_exchange_strong(expected, e,
557 std::memory_order_release,
558 std::memory_order_acquire)) {
559 delete e;
560 e = expected; // Filled in by CAS
561 }
562 if (e == nullptr) return nullptr; // out of memory
563 }
564
565 return e;
566}
567
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800568// ---------------------------------------------------------------------------
569
570enum {
571 // This is used to transfer ownership of the remote binder from
572 // the BpRefBase object holding it (when it is constructed), to the
573 // owner of the BpRefBase object when it first acquires that BpRefBase.
574 kRemoteAcquired = 0x00000001
575};
576
577BpRefBase::BpRefBase(const sp<IBinder>& o)
Yi Kongfdd8da92018-06-07 17:52:27 -0700578 : mRemote(o.get()), mRefs(nullptr), mState(0)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800579{
580 extendObjectLifetime(OBJECT_LIFETIME_WEAK);
581
582 if (mRemote) {
583 mRemote->incStrong(this); // Removed on first IncStrong().
584 mRefs = mRemote->createWeak(this); // Held for our entire lifetime.
585 }
586}
587
588BpRefBase::~BpRefBase()
589{
590 if (mRemote) {
Bailey Forrest6913c462015-08-18 17:15:10 -0700591 if (!(mState.load(std::memory_order_relaxed)&kRemoteAcquired)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800592 mRemote->decStrong(this);
593 }
594 mRefs->decWeak(this);
595 }
596}
597
598void BpRefBase::onFirstRef()
599{
Bailey Forrest6913c462015-08-18 17:15:10 -0700600 mState.fetch_or(kRemoteAcquired, std::memory_order_relaxed);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800601}
602
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800603void BpRefBase::onLastStrongRef(const void* /*id*/)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800604{
605 if (mRemote) {
606 mRemote->decStrong(this);
607 }
608}
609
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800610bool BpRefBase::onIncStrongAttempted(uint32_t /*flags*/, const void* /*id*/)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800611{
612 return mRemote ? mRefs->attemptIncStrong(this) : false;
613}
614
615// ---------------------------------------------------------------------------
616
Steven Moreland61ff8492019-09-26 16:05:45 -0700617} // namespace android