blob: 9883eb2672faf32a7beb6ab9cac092800bf5c09c [file] [log] [blame]
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2005 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Mathias Agopianc5b2c0b2009-05-19 19:08:10 -070017#include <binder/Binder.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080018
Bailey Forrest6913c462015-08-18 17:15:10 -070019#include <atomic>
Yifan Hong8b890852021-06-10 13:44:09 -070020#include <set>
Yifan Hong84bedeb2021-04-21 21:37:17 -070021
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>
Fabián Cañas356a0222023-01-13 16:32:53 -050028#include <binder/RecordedTransaction.h>
Yifan Hong84bedeb2021-04-21 21:37:17 -070029#include <binder/RpcServer.h>
Tomasz Wasilczyk639490b2023-11-01 13:49:41 -070030#include <binder/unique_fd.h>
Sahil Somani2522b072022-08-08 14:07:47 -070031#include <pthread.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080032
Yifan Hong84bedeb2021-04-21 21:37:17 -070033#include <inttypes.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080034#include <stdio.h>
35
Andrei Homescuf55d6882022-04-30 00:50:20 +000036#ifdef __linux__
37#include <linux/sched.h>
38#endif
39
Steven Moreland32150282021-11-12 22:54:53 +000040#include "BuildFlags.h"
Steven Moreland54fa6c72025-01-28 01:02:15 +000041#include "Constants.h"
Tomasz Wasilczyk346f1042023-10-09 17:02:46 +000042#include "OS.h"
Yifan Hong84bedeb2021-04-21 21:37:17 -070043#include "RpcState.h"
44
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080045namespace android {
46
Tomasz Wasilczyk639490b2023-11-01 13:49:41 -070047using android::binder::unique_fd;
48
Tomasz Wasilczykb8ebcca2023-10-09 19:54:20 +000049constexpr uid_t kUidRoot = 0;
50
Steven Moreland90c1f9a2021-05-03 18:27:24 +000051// Service implementations inherit from BBinder and IBinder, and this is frozen
52// in prebuilts.
53#ifdef __LP64__
54static_assert(sizeof(IBinder) == 24);
55static_assert(sizeof(BBinder) == 40);
56#else
57static_assert(sizeof(IBinder) == 12);
58static_assert(sizeof(BBinder) == 20);
59#endif
60
Steven Morelandd9a74002022-06-02 00:03:18 +000061// global b/c b/230079120 - consistent symbol table
Yifan Hong84bedeb2021-04-21 21:37:17 -070062#ifdef BINDER_RPC_DEV_SERVERS
Steven Moreland5255c972023-05-19 22:53:41 +000063constexpr bool kEnableRpcDevServers = true;
Yifan Hong84bedeb2021-04-21 21:37:17 -070064#else
Steven Moreland5255c972023-05-19 22:53:41 +000065constexpr bool kEnableRpcDevServers = false;
Yifan Hong84bedeb2021-04-21 21:37:17 -070066#endif
67
Sahil Somani2522b072022-08-08 14:07:47 -070068#ifdef BINDER_ENABLE_RECORDING
Steven Moreland5255c972023-05-19 22:53:41 +000069constexpr bool kEnableRecording = true;
Sahil Somani2522b072022-08-08 14:07:47 -070070#else
Steven Moreland5255c972023-05-19 22:53:41 +000071constexpr bool kEnableRecording = false;
Sahil Somani2522b072022-08-08 14:07:47 -070072#endif
73
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080074// ---------------------------------------------------------------------------
75
Mathias Agopian83c04462009-05-22 19:00:22 -070076IBinder::IBinder()
77 : RefBase()
78{
79}
80
81IBinder::~IBinder()
82{
83}
84
85// ---------------------------------------------------------------------------
86
Colin Cross6f4f3ab2014-02-05 17:42:44 -080087sp<IInterface> IBinder::queryLocalInterface(const String16& /*descriptor*/)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080088{
Yi Kongfdd8da92018-06-07 17:52:27 -070089 return nullptr;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080090}
91
92BBinder* IBinder::localBinder()
93{
Yi Kongfdd8da92018-06-07 17:52:27 -070094 return nullptr;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080095}
96
97BpBinder* IBinder::remoteBinder()
98{
Yi Kongfdd8da92018-06-07 17:52:27 -070099 return nullptr;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800100}
101
102bool IBinder::checkSubclass(const void* /*subclassID*/) const
103{
104 return false;
105}
106
Dianne Hackborn23eb1e22015-10-07 17:35:27 -0700107
Dianne Hackbornf2bf93b2015-10-14 15:13:02 -0700108status_t IBinder::shellCommand(const sp<IBinder>& target, int in, int out, int err,
Dianne Hackborn1941a402016-08-29 12:30:43 -0700109 Vector<String16>& args, const sp<IShellCallback>& callback,
110 const sp<IResultReceiver>& resultReceiver)
Dianne Hackborn23eb1e22015-10-07 17:35:27 -0700111{
Dianne Hackbornf2bf93b2015-10-14 15:13:02 -0700112 Parcel send;
113 Parcel reply;
114 send.writeFileDescriptor(in);
115 send.writeFileDescriptor(out);
116 send.writeFileDescriptor(err);
117 const size_t numArgs = args.size();
118 send.writeInt32(numArgs);
119 for (size_t i = 0; i < numArgs; i++) {
120 send.writeString16(args[i]);
Dianne Hackborn23eb1e22015-10-07 17:35:27 -0700121 }
Yi Kongfdd8da92018-06-07 17:52:27 -0700122 send.writeStrongBinder(callback != nullptr ? IInterface::asBinder(callback) : nullptr);
123 send.writeStrongBinder(resultReceiver != nullptr ? IInterface::asBinder(resultReceiver) : nullptr);
Dianne Hackbornf2bf93b2015-10-14 15:13:02 -0700124 return target->transact(SHELL_COMMAND_TRANSACTION, send, &reply);
Dianne Hackborn23eb1e22015-10-07 17:35:27 -0700125}
126
Steven Morelandb8ad08d2019-08-09 14:42:56 -0700127status_t IBinder::getExtension(sp<IBinder>* out) {
128 BBinder* local = this->localBinder();
129 if (local != nullptr) {
130 *out = local->getExtension();
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(EXTENSION_TRANSACTION, data, &reply);
140 if (status != OK) return status;
141
142 return reply.readNullableStrongBinder(out);
143}
144
Yu-Ting Tsengd5fc4462024-04-30 15:07:13 -0700145status_t IBinder::addFrozenStateChangeCallback(const wp<FrozenStateChangeCallback>& callback) {
146 BpBinder* proxy = this->remoteBinder();
147 if (proxy != nullptr) {
148 return proxy->addFrozenStateChangeCallback(callback);
149 }
150 return INVALID_OPERATION;
151}
152
153status_t IBinder::removeFrozenStateChangeCallback(const wp<FrozenStateChangeCallback>& callback) {
154 BpBinder* proxy = this->remoteBinder();
155 if (proxy != nullptr) {
156 return proxy->removeFrozenStateChangeCallback(callback);
157 }
158 return INVALID_OPERATION;
159}
160
Steven Moreland86080b82019-09-23 15:41:18 -0700161status_t IBinder::getDebugPid(pid_t* out) {
162 BBinder* local = this->localBinder();
163 if (local != nullptr) {
164 *out = local->getDebugPid();
165 return OK;
166 }
167
168 BpBinder* proxy = this->remoteBinder();
169 LOG_ALWAYS_FATAL_IF(proxy == nullptr);
170
171 Parcel data;
172 Parcel reply;
173 status_t status = transact(DEBUG_PID_TRANSACTION, data, &reply);
174 if (status != OK) return status;
175
176 int32_t pid;
177 status = reply.readInt32(&pid);
178 if (status != OK) return status;
179
180 if (pid < 0 || pid > std::numeric_limits<pid_t>::max()) {
181 return BAD_VALUE;
182 }
183 *out = pid;
184 return OK;
185}
186
Tomasz Wasilczyk639490b2023-11-01 13:49:41 -0700187status_t IBinder::setRpcClientDebug(unique_fd socketFd, const sp<IBinder>& keepAliveBinder) {
Steven Morelandd9a74002022-06-02 00:03:18 +0000188 if (!kEnableRpcDevServers) {
Yifan Hong84bedeb2021-04-21 21:37:17 -0700189 ALOGW("setRpcClientDebug disallowed because RPC is not enabled");
190 return INVALID_OPERATION;
191 }
Steven Moreland32150282021-11-12 22:54:53 +0000192 if (!kEnableKernelIpc) {
193 ALOGW("setRpcClientDebug disallowed because kernel binder is not enabled");
194 return INVALID_OPERATION;
195 }
Yifan Hong84bedeb2021-04-21 21:37:17 -0700196
197 BBinder* local = this->localBinder();
198 if (local != nullptr) {
Yifan Hong02530ec2021-06-10 13:38:38 -0700199 return local->BBinder::setRpcClientDebug(std::move(socketFd), keepAliveBinder);
Yifan Hong84bedeb2021-04-21 21:37:17 -0700200 }
201
202 BpBinder* proxy = this->remoteBinder();
203 LOG_ALWAYS_FATAL_IF(proxy == nullptr, "binder object must be either local or remote");
204
205 Parcel data;
206 Parcel reply;
207 status_t status;
208 if (status = data.writeBool(socketFd.ok()); status != OK) return status;
209 if (socketFd.ok()) {
210 // writeUniqueFileDescriptor currently makes an unnecessary dup().
211 status = data.writeFileDescriptor(socketFd.release(), true /* own */);
212 if (status != OK) return status;
213 }
Yifan Hong02530ec2021-06-10 13:38:38 -0700214 if (status = data.writeStrongBinder(keepAliveBinder); status != OK) return status;
Yifan Hong84bedeb2021-04-21 21:37:17 -0700215 return transact(SET_RPC_CLIENT_TRANSACTION, data, &reply);
216}
217
Steven Moreland9e759e82021-06-25 21:30:23 +0000218void IBinder::withLock(const std::function<void()>& doWithLock) {
219 BBinder* local = localBinder();
220 if (local) {
221 local->withLock(doWithLock);
222 return;
223 }
224 BpBinder* proxy = this->remoteBinder();
225 LOG_ALWAYS_FATAL_IF(proxy == nullptr, "binder object must be either local or remote");
226 proxy->withLock(doWithLock);
227}
228
Devin Moore3faaa002022-07-27 15:54:06 +0000229sp<IBinder> IBinder::lookupOrCreateWeak(const void* objectID, object_make_func make,
230 const void* makeArgs) {
231 BBinder* local = localBinder();
232 if (local) {
233 return local->lookupOrCreateWeak(objectID, make, makeArgs);
234 }
235 BpBinder* proxy = this->remoteBinder();
236 LOG_ALWAYS_FATAL_IF(proxy == nullptr, "binder object must be either local or remote");
237 return proxy->lookupOrCreateWeak(objectID, make, makeArgs);
238}
239
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800240// ---------------------------------------------------------------------------
241
Yifan Hong8b890852021-06-10 13:44:09 -0700242class BBinder::RpcServerLink : public IBinder::DeathRecipient {
243public:
244 // On binder died, calls RpcServer::shutdown on @a rpcServer, and removes itself from @a binder.
245 RpcServerLink(const sp<RpcServer>& rpcServer, const sp<IBinder>& keepAliveBinder,
246 const wp<BBinder>& binder)
247 : mRpcServer(rpcServer), mKeepAliveBinder(keepAliveBinder), mBinder(binder) {}
Steven Morelandd9a74002022-06-02 00:03:18 +0000248 virtual ~RpcServerLink();
Yifan Hong8b890852021-06-10 13:44:09 -0700249 void binderDied(const wp<IBinder>&) override {
Steven Morelandf6957b72022-09-24 03:12:16 +0000250 auto promoted = mBinder.promote();
251 ALOGI("RpcBinder: binder died, shutting down RpcServer for %s",
252 promoted ? String8(promoted->getInterfaceDescriptor()).c_str() : "<NULL>");
253
Yifan Hong8b890852021-06-10 13:44:09 -0700254 if (mRpcServer == nullptr) {
255 ALOGW("RpcServerLink: Unable to shut down RpcServer because it does not exist.");
256 } else {
257 ALOGW_IF(!mRpcServer->shutdown(),
258 "RpcServerLink: RpcServer did not shut down properly. Not started?");
259 }
260 mRpcServer.clear();
261
Steven Morelandf6957b72022-09-24 03:12:16 +0000262 if (promoted) {
Yifan Hong8b890852021-06-10 13:44:09 -0700263 promoted->removeRpcServerLink(sp<RpcServerLink>::fromExisting(this));
264 }
265 mBinder.clear();
266 }
267
268private:
269 sp<RpcServer> mRpcServer;
270 sp<IBinder> mKeepAliveBinder; // hold to avoid automatically unlinking
271 wp<BBinder> mBinder;
272};
Steven Morelandd9a74002022-06-02 00:03:18 +0000273BBinder::RpcServerLink::~RpcServerLink() {}
Yifan Hong8b890852021-06-10 13:44:09 -0700274
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800275class BBinder::Extras
276{
277public:
Steven Morelandf0212002018-12-26 13:59:23 -0800278 // unlocked objects
Steven Morelandb8ad08d2019-08-09 14:42:56 -0700279 sp<IBinder> mExtension;
Andrei Homescuf55d6882022-04-30 00:50:20 +0000280#ifdef __linux__
Steven Morelandbf1915b2020-07-16 22:43:02 +0000281 int mPolicy = SCHED_NORMAL;
282 int mPriority = 0;
Andrei Homescuf55d6882022-04-30 00:50:20 +0000283#endif
284 bool mRequestingSid = false;
285 bool mInheritRt = false;
Steven Morelandf0212002018-12-26 13:59:23 -0800286
287 // for below objects
Tomasz Wasilczyk66ee1922023-10-26 14:53:49 -0700288 RpcMutex mLock;
Yifan Hong8b890852021-06-10 13:44:09 -0700289 std::set<sp<RpcServerLink>> mRpcServerLinks;
Devin Moore3fea46f2025-01-09 00:17:12 +0000290 BpBinder::ObjectManager mObjectMgr;
Sahil Somani2522b072022-08-08 14:07:47 -0700291
Tomasz Wasilczyk639490b2023-11-01 13:49:41 -0700292 unique_fd mRecordingFd;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800293};
294
295// ---------------------------------------------------------------------------
296
Sahil Somani2522b072022-08-08 14:07:47 -0700297BBinder::BBinder() : mExtras(nullptr), mStability(0), mParceled(false), mRecordingOn(false) {}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800298
299bool BBinder::isBinderAlive() const
300{
301 return true;
302}
303
304status_t BBinder::pingBinder()
305{
306 return NO_ERROR;
307}
308
Sahil Somani2522b072022-08-08 14:07:47 -0700309status_t BBinder::startRecordingTransactions(const Parcel& data) {
310 if (!kEnableRecording) {
311 ALOGW("Binder recording disallowed because recording is not enabled");
312 return INVALID_OPERATION;
313 }
314 if (!kEnableKernelIpc) {
315 ALOGW("Binder recording disallowed because kernel binder is not enabled");
316 return INVALID_OPERATION;
317 }
318 uid_t uid = IPCThreadState::self()->getCallingUid();
Tomasz Wasilczykb8ebcca2023-10-09 19:54:20 +0000319 if (uid != kUidRoot) {
Sahil Somani2522b072022-08-08 14:07:47 -0700320 ALOGE("Binder recording not allowed because client %" PRIu32 " is not root", uid);
321 return PERMISSION_DENIED;
322 }
323 Extras* e = getOrCreateExtras();
Tomasz Wasilczyk66ee1922023-10-26 14:53:49 -0700324 RpcMutexUniqueLock lock(e->mLock);
Sahil Somani2522b072022-08-08 14:07:47 -0700325 if (mRecordingOn) {
Tomasz Wasilczyke3de8802023-11-01 11:05:27 -0700326 ALOGI("Could not start Binder recording. Another is already in progress.");
Sahil Somani2522b072022-08-08 14:07:47 -0700327 return INVALID_OPERATION;
328 } else {
329 status_t readStatus = data.readUniqueFileDescriptor(&(e->mRecordingFd));
330 if (readStatus != OK) {
331 return readStatus;
332 }
333 mRecordingOn = true;
Tomasz Wasilczyke3de8802023-11-01 11:05:27 -0700334 ALOGI("Started Binder recording.");
Sahil Somani2522b072022-08-08 14:07:47 -0700335 return NO_ERROR;
336 }
337}
338
339status_t BBinder::stopRecordingTransactions() {
340 if (!kEnableRecording) {
341 ALOGW("Binder recording disallowed because recording is not enabled");
342 return INVALID_OPERATION;
343 }
344 if (!kEnableKernelIpc) {
345 ALOGW("Binder recording disallowed because kernel binder is not enabled");
346 return INVALID_OPERATION;
347 }
348 uid_t uid = IPCThreadState::self()->getCallingUid();
Tomasz Wasilczykb8ebcca2023-10-09 19:54:20 +0000349 if (uid != kUidRoot) {
Sahil Somani2522b072022-08-08 14:07:47 -0700350 ALOGE("Binder recording not allowed because client %" PRIu32 " is not root", uid);
351 return PERMISSION_DENIED;
352 }
353 Extras* e = getOrCreateExtras();
Tomasz Wasilczyk66ee1922023-10-26 14:53:49 -0700354 RpcMutexUniqueLock lock(e->mLock);
Sahil Somani2522b072022-08-08 14:07:47 -0700355 if (mRecordingOn) {
356 e->mRecordingFd.reset();
357 mRecordingOn = false;
Tomasz Wasilczyke3de8802023-11-01 11:05:27 -0700358 ALOGI("Stopped Binder recording.");
Sahil Somani2522b072022-08-08 14:07:47 -0700359 return NO_ERROR;
360 } else {
Tomasz Wasilczyke3de8802023-11-01 11:05:27 -0700361 ALOGI("Could not stop Binder recording. One is not in progress.");
Sahil Somani2522b072022-08-08 14:07:47 -0700362 return INVALID_OPERATION;
363 }
364}
365
Mathias Agopian83c04462009-05-22 19:00:22 -0700366const String16& BBinder::getInterfaceDescriptor() const
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800367{
Steven Moreland1ef0eb62022-08-18 20:09:30 +0000368 static StaticString16 sBBinder(u"BBinder");
369 ALOGW("Reached BBinder::getInterfaceDescriptor (this=%p). Override?", this);
370 return sBBinder;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800371}
372
Jiyong Parkb86c8662018-10-29 23:01:57 +0900373// NOLINTNEXTLINE(google-default-arguments)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800374status_t BBinder::transact(
375 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
376{
377 data.setDataPosition(0);
378
Steven Morelandf183fdd2020-10-27 00:12:12 +0000379 if (reply != nullptr && (flags & FLAG_CLEAR_BUF)) {
380 reply->markSensitive();
381 }
382
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800383 status_t err = NO_ERROR;
384 switch (code) {
385 case PING_TRANSACTION:
Steven Moreland6e69d652019-07-10 14:17:55 -0700386 err = pingBinder();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800387 break;
Sahil Somani2522b072022-08-08 14:07:47 -0700388 case START_RECORDING_TRANSACTION:
389 err = startRecordingTransactions(data);
390 break;
391 case STOP_RECORDING_TRANSACTION:
392 err = stopRecordingTransactions();
393 break;
Steven Morelandb8ad08d2019-08-09 14:42:56 -0700394 case EXTENSION_TRANSACTION:
Tomasz Wasilczyke3de8802023-11-01 11:05:27 -0700395 LOG_ALWAYS_FATAL_IF(reply == nullptr, "reply == nullptr");
Steven Morelandb8ad08d2019-08-09 14:42:56 -0700396 err = reply->writeStrongBinder(getExtension());
397 break;
Steven Moreland86080b82019-09-23 15:41:18 -0700398 case DEBUG_PID_TRANSACTION:
Tomasz Wasilczyke3de8802023-11-01 11:05:27 -0700399 LOG_ALWAYS_FATAL_IF(reply == nullptr, "reply == nullptr");
Steven Moreland86080b82019-09-23 15:41:18 -0700400 err = reply->writeInt32(getDebugPid());
401 break;
Yifan Hong84bedeb2021-04-21 21:37:17 -0700402 case SET_RPC_CLIENT_TRANSACTION: {
403 err = setRpcClientDebug(data);
404 break;
405 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800406 default:
407 err = onTransact(code, data, reply, flags);
408 break;
409 }
410
Steven Morelanda86a3562019-08-01 23:28:34 +0000411 // In case this is being transacted on in the same process.
Yi Kongfdd8da92018-06-07 17:52:27 -0700412 if (reply != nullptr) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800413 reply->setDataPosition(0);
Steven Moreland54fa6c72025-01-28 01:02:15 +0000414 if (reply->dataSize() > binder::kLogTransactionsOverBytes) {
Martijn Coenen1cad19c2021-10-04 09:19:01 +0200415 ALOGW("Large reply transaction of %zu bytes, interface descriptor %s, code %d",
416 reply->dataSize(), String8(getInterfaceDescriptor()).c_str(), code);
417 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800418 }
419
Tomasz Wasilczyk32746ae2023-10-09 19:57:58 +0000420 if (kEnableKernelIpc && mRecordingOn && code != START_RECORDING_TRANSACTION) [[unlikely]] {
Sahil Somani2522b072022-08-08 14:07:47 -0700421 Extras* e = mExtras.load(std::memory_order_acquire);
Tomasz Wasilczyk66ee1922023-10-26 14:53:49 -0700422 RpcMutexUniqueLock lock(e->mLock);
Sahil Somani2522b072022-08-08 14:07:47 -0700423 if (mRecordingOn) {
424 Parcel emptyReply;
Fabián Cañas68dbcfe2023-01-06 15:37:39 -0500425 timespec ts;
426 timespec_get(&ts, TIME_UTC);
Fabián Cañas6c0c9632023-01-13 15:21:16 -0500427 auto transaction = android::binder::debug::RecordedTransaction::
428 fromDetails(getInterfaceDescriptor(), code, flags, ts, data,
429 reply ? *reply : emptyReply, err);
Sahil Somani2522b072022-08-08 14:07:47 -0700430 if (transaction) {
431 if (status_t err = transaction->dumpToFile(e->mRecordingFd); err != NO_ERROR) {
Tomasz Wasilczyke3de8802023-11-01 11:05:27 -0700432 ALOGI("Failed to dump RecordedTransaction to file with error %d", err);
Sahil Somani2522b072022-08-08 14:07:47 -0700433 }
434 } else {
Tomasz Wasilczyke3de8802023-11-01 11:05:27 -0700435 ALOGI("Failed to create RecordedTransaction object.");
Sahil Somani2522b072022-08-08 14:07:47 -0700436 }
437 }
438 }
439
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800440 return err;
441}
442
Jiyong Parkb86c8662018-10-29 23:01:57 +0900443// NOLINTNEXTLINE(google-default-arguments)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800444status_t BBinder::linkToDeath(
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800445 const sp<DeathRecipient>& /*recipient*/, void* /*cookie*/,
446 uint32_t /*flags*/)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800447{
Devin Moore7ba43782025-03-04 21:42:54 +0000448 // BBinder::linkToDeath is invalid because this process owns this binder.
449 // The DeathRecipient is called on BpBinders when the process owning the
450 // binder node dies.
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800451 return INVALID_OPERATION;
452}
453
Jiyong Parkb86c8662018-10-29 23:01:57 +0900454// NOLINTNEXTLINE(google-default-arguments)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800455status_t BBinder::unlinkToDeath(
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800456 const wp<DeathRecipient>& /*recipient*/, void* /*cookie*/,
457 uint32_t /*flags*/, wp<DeathRecipient>* /*outRecipient*/)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800458{
459 return INVALID_OPERATION;
460}
461
Dianne Hackborn23eb1e22015-10-07 17:35:27 -0700462status_t BBinder::dump(int /*fd*/, const Vector<String16>& /*args*/)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800463{
464 return NO_ERROR;
465}
466
Steven Moreland63a2d512021-06-25 01:10:15 +0000467void* BBinder::attachObject(const void* objectID, void* object, void* cleanupCookie,
468 object_cleanup_func func) {
Steven Morelandf0212002018-12-26 13:59:23 -0800469 Extras* e = getOrCreateExtras();
Steven Moreland5ce7ca52021-06-25 21:59:50 +0000470 LOG_ALWAYS_FATAL_IF(!e, "no memory");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800471
Tomasz Wasilczyk66ee1922023-10-26 14:53:49 -0700472 RpcMutexUniqueLock _l(e->mLock);
Devin Moore3fea46f2025-01-09 00:17:12 +0000473 return e->mObjectMgr.attach(objectID, object, cleanupCookie, func);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800474}
475
476void* BBinder::findObject(const void* objectID) const
477{
Bailey Forrest6913c462015-08-18 17:15:10 -0700478 Extras* e = mExtras.load(std::memory_order_acquire);
Yi Kongfdd8da92018-06-07 17:52:27 -0700479 if (!e) return nullptr;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800480
Tomasz Wasilczyk66ee1922023-10-26 14:53:49 -0700481 RpcMutexUniqueLock _l(e->mLock);
Devin Moore3fea46f2025-01-09 00:17:12 +0000482 return e->mObjectMgr.find(objectID);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800483}
484
Steven Moreland63a2d512021-06-25 01:10:15 +0000485void* BBinder::detachObject(const void* objectID) {
Bailey Forrest6913c462015-08-18 17:15:10 -0700486 Extras* e = mExtras.load(std::memory_order_acquire);
Steven Moreland63a2d512021-06-25 01:10:15 +0000487 if (!e) return nullptr;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800488
Tomasz Wasilczyk66ee1922023-10-26 14:53:49 -0700489 RpcMutexUniqueLock _l(e->mLock);
Devin Moore3fea46f2025-01-09 00:17:12 +0000490 return e->mObjectMgr.detach(objectID);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800491}
492
Steven Moreland9e759e82021-06-25 21:30:23 +0000493void BBinder::withLock(const std::function<void()>& doWithLock) {
494 Extras* e = getOrCreateExtras();
495 LOG_ALWAYS_FATAL_IF(!e, "no memory");
496
Tomasz Wasilczyk66ee1922023-10-26 14:53:49 -0700497 RpcMutexUniqueLock _l(e->mLock);
Steven Moreland9e759e82021-06-25 21:30:23 +0000498 doWithLock();
499}
500
Devin Moore3faaa002022-07-27 15:54:06 +0000501sp<IBinder> BBinder::lookupOrCreateWeak(const void* objectID, object_make_func make,
502 const void* makeArgs) {
503 Extras* e = getOrCreateExtras();
504 LOG_ALWAYS_FATAL_IF(!e, "no memory");
Tomasz Wasilczyk66ee1922023-10-26 14:53:49 -0700505 RpcMutexUniqueLock _l(e->mLock);
Devin Moore3fea46f2025-01-09 00:17:12 +0000506 return e->mObjectMgr.lookupOrCreateWeak(objectID, make, makeArgs);
Devin Moore3faaa002022-07-27 15:54:06 +0000507}
508
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800509BBinder* BBinder::localBinder()
510{
511 return this;
512}
513
Steven Morelandf0212002018-12-26 13:59:23 -0800514bool BBinder::isRequestingSid()
515{
516 Extras* e = mExtras.load(std::memory_order_acquire);
517
518 return e && e->mRequestingSid;
519}
520
521void BBinder::setRequestingSid(bool requestingSid)
522{
Steven Morelandd37e6dd2021-06-16 22:07:51 +0000523 LOG_ALWAYS_FATAL_IF(mParceled,
524 "setRequestingSid() should not be called after a binder object "
525 "is parceled/sent to another process");
Kalesh Singhd67c8e82020-12-29 15:46:25 -0500526
Steven Morelandf0212002018-12-26 13:59:23 -0800527 Extras* e = mExtras.load(std::memory_order_acquire);
528
529 if (!e) {
530 // default is false. Most things don't need sids, so avoiding allocations when possible.
531 if (!requestingSid) {
532 return;
533 }
534
535 e = getOrCreateExtras();
536 if (!e) return; // out of memory
537 }
538
Steven Moreland3668be62019-02-08 17:56:55 -0800539 e->mRequestingSid = requestingSid;
Steven Morelandf0212002018-12-26 13:59:23 -0800540}
541
Steven Morelandb8ad08d2019-08-09 14:42:56 -0700542sp<IBinder> BBinder::getExtension() {
543 Extras* e = mExtras.load(std::memory_order_acquire);
544 if (e == nullptr) return nullptr;
545 return e->mExtension;
546}
547
Andrei Homescuf55d6882022-04-30 00:50:20 +0000548#ifdef __linux__
Steven Morelandbf1915b2020-07-16 22:43:02 +0000549void BBinder::setMinSchedulerPolicy(int policy, int priority) {
Steven Morelandd37e6dd2021-06-16 22:07:51 +0000550 LOG_ALWAYS_FATAL_IF(mParceled,
551 "setMinSchedulerPolicy() should not be called after a binder object "
552 "is parceled/sent to another process");
Kalesh Singhd67c8e82020-12-29 15:46:25 -0500553
Steven Morelandbf1915b2020-07-16 22:43:02 +0000554 switch (policy) {
555 case SCHED_NORMAL:
556 LOG_ALWAYS_FATAL_IF(priority < -20 || priority > 19, "Invalid priority for SCHED_NORMAL: %d", priority);
557 break;
558 case SCHED_RR:
559 case SCHED_FIFO:
560 LOG_ALWAYS_FATAL_IF(priority < 1 || priority > 99, "Invalid priority for sched %d: %d", policy, priority);
561 break;
562 default:
563 LOG_ALWAYS_FATAL("Unrecognized scheduling policy: %d", policy);
564 }
565
566 Extras* e = mExtras.load(std::memory_order_acquire);
567
568 if (e == nullptr) {
569 // Avoid allocations if called with default.
570 if (policy == SCHED_NORMAL && priority == 0) {
571 return;
572 }
573
574 e = getOrCreateExtras();
575 if (!e) return; // out of memory
576 }
577
578 e->mPolicy = policy;
579 e->mPriority = priority;
580}
581
582int BBinder::getMinSchedulerPolicy() {
583 Extras* e = mExtras.load(std::memory_order_acquire);
584 if (e == nullptr) return SCHED_NORMAL;
585 return e->mPolicy;
586}
587
588int BBinder::getMinSchedulerPriority() {
589 Extras* e = mExtras.load(std::memory_order_acquire);
590 if (e == nullptr) return 0;
591 return e->mPriority;
592}
Andrei Homescuf55d6882022-04-30 00:50:20 +0000593#endif // __linux__
Steven Morelandbf1915b2020-07-16 22:43:02 +0000594
Steven Morelandcf03cf12020-12-04 02:58:40 +0000595bool BBinder::isInheritRt() {
596 Extras* e = mExtras.load(std::memory_order_acquire);
597
598 return e && e->mInheritRt;
599}
600
601void BBinder::setInheritRt(bool inheritRt) {
Steven Morelandd37e6dd2021-06-16 22:07:51 +0000602 LOG_ALWAYS_FATAL_IF(mParceled,
603 "setInheritRt() should not be called after a binder object "
604 "is parceled/sent to another process");
Kalesh Singhd67c8e82020-12-29 15:46:25 -0500605
Steven Morelandcf03cf12020-12-04 02:58:40 +0000606 Extras* e = mExtras.load(std::memory_order_acquire);
607
608 if (!e) {
609 if (!inheritRt) {
610 return;
611 }
612
613 e = getOrCreateExtras();
614 if (!e) return; // out of memory
615 }
616
617 e->mInheritRt = inheritRt;
618}
619
Steven Moreland86080b82019-09-23 15:41:18 -0700620pid_t BBinder::getDebugPid() {
Andrei Homescuf55d6882022-04-30 00:50:20 +0000621#ifdef __linux__
Steven Moreland86080b82019-09-23 15:41:18 -0700622 return getpid();
Andrei Homescuf55d6882022-04-30 00:50:20 +0000623#else
624 // TODO: handle other OSes
625 return 0;
626#endif // __linux__
Steven Moreland86080b82019-09-23 15:41:18 -0700627}
628
Steven Morelandb8ad08d2019-08-09 14:42:56 -0700629void BBinder::setExtension(const sp<IBinder>& extension) {
Steven Morelandd37e6dd2021-06-16 22:07:51 +0000630 LOG_ALWAYS_FATAL_IF(mParceled,
631 "setExtension() should not be called after a binder object "
632 "is parceled/sent to another process");
Kalesh Singhd67c8e82020-12-29 15:46:25 -0500633
Steven Morelandb8ad08d2019-08-09 14:42:56 -0700634 Extras* e = getOrCreateExtras();
635 e->mExtension = extension;
636}
637
Kalesh Singhd67c8e82020-12-29 15:46:25 -0500638bool BBinder::wasParceled() {
639 return mParceled;
640}
641
642void BBinder::setParceled() {
643 mParceled = true;
644}
645
Yifan Hong84bedeb2021-04-21 21:37:17 -0700646status_t BBinder::setRpcClientDebug(const Parcel& data) {
Steven Morelandd9a74002022-06-02 00:03:18 +0000647 if (!kEnableRpcDevServers) {
Yifan Hong84bedeb2021-04-21 21:37:17 -0700648 ALOGW("%s: disallowed because RPC is not enabled", __PRETTY_FUNCTION__);
649 return INVALID_OPERATION;
650 }
Steven Moreland32150282021-11-12 22:54:53 +0000651 if (!kEnableKernelIpc) {
652 ALOGW("setRpcClientDebug disallowed because kernel binder is not enabled");
653 return INVALID_OPERATION;
654 }
Yifan Hong84bedeb2021-04-21 21:37:17 -0700655 uid_t uid = IPCThreadState::self()->getCallingUid();
Tomasz Wasilczykb8ebcca2023-10-09 19:54:20 +0000656 if (uid != kUidRoot) {
Yifan Hong84bedeb2021-04-21 21:37:17 -0700657 ALOGE("%s: not allowed because client %" PRIu32 " is not root", __PRETTY_FUNCTION__, uid);
658 return PERMISSION_DENIED;
659 }
660 status_t status;
661 bool hasSocketFd;
Tomasz Wasilczyk639490b2023-11-01 13:49:41 -0700662 unique_fd clientFd;
Yifan Hong84bedeb2021-04-21 21:37:17 -0700663
664 if (status = data.readBool(&hasSocketFd); status != OK) return status;
665 if (hasSocketFd) {
666 if (status = data.readUniqueFileDescriptor(&clientFd); status != OK) return status;
667 }
Yifan Hong02530ec2021-06-10 13:38:38 -0700668 sp<IBinder> keepAliveBinder;
669 if (status = data.readNullableStrongBinder(&keepAliveBinder); status != OK) return status;
Yifan Hong84bedeb2021-04-21 21:37:17 -0700670
Yifan Hong02530ec2021-06-10 13:38:38 -0700671 return setRpcClientDebug(std::move(clientFd), keepAliveBinder);
Yifan Hong84bedeb2021-04-21 21:37:17 -0700672}
673
Tomasz Wasilczyk639490b2023-11-01 13:49:41 -0700674status_t BBinder::setRpcClientDebug(unique_fd socketFd, const sp<IBinder>& keepAliveBinder) {
Steven Morelandd9a74002022-06-02 00:03:18 +0000675 if (!kEnableRpcDevServers) {
Yifan Hong84bedeb2021-04-21 21:37:17 -0700676 ALOGW("%s: disallowed because RPC is not enabled", __PRETTY_FUNCTION__);
677 return INVALID_OPERATION;
678 }
Steven Moreland32150282021-11-12 22:54:53 +0000679 if (!kEnableKernelIpc) {
680 ALOGW("setRpcClientDebug disallowed because kernel binder is not enabled");
681 return INVALID_OPERATION;
682 }
Yifan Hong84bedeb2021-04-21 21:37:17 -0700683
684 const int socketFdForPrint = socketFd.get();
Yifan Hong34823232021-06-07 17:23:00 -0700685 LOG_RPC_DETAIL("%s(fd=%d)", __PRETTY_FUNCTION__, socketFdForPrint);
Yifan Hong84bedeb2021-04-21 21:37:17 -0700686
687 if (!socketFd.ok()) {
688 ALOGE("%s: No socket FD provided.", __PRETTY_FUNCTION__);
689 return BAD_VALUE;
690 }
Yifan Hong84bedeb2021-04-21 21:37:17 -0700691
Yifan Hong02530ec2021-06-10 13:38:38 -0700692 if (keepAliveBinder == nullptr) {
693 ALOGE("%s: No keepAliveBinder provided.", __PRETTY_FUNCTION__);
694 return UNEXPECTED_NULL;
695 }
696
Elie Kheirallah47431c12022-04-21 23:46:17 +0000697 size_t binderThreadPoolMaxCount = ProcessState::self()->getThreadPoolMaxTotalThreadCount();
Yifan Hong84bedeb2021-04-21 21:37:17 -0700698 if (binderThreadPoolMaxCount <= 1) {
699 ALOGE("%s: ProcessState thread pool max count is %zu. RPC is disabled for this service "
700 "because RPC requires the service to support multithreading.",
701 __PRETTY_FUNCTION__, binderThreadPoolMaxCount);
702 return INVALID_OPERATION;
703 }
704
Yifan Hong8b890852021-06-10 13:44:09 -0700705 // Weak ref to avoid circular dependency:
706 // BBinder -> RpcServerLink ----> RpcServer -X-> BBinder
707 // `-X-> BBinder
708 auto weakThis = wp<BBinder>::fromExisting(this);
709
Yifan Hong84bedeb2021-04-21 21:37:17 -0700710 Extras* e = getOrCreateExtras();
Tomasz Wasilczyk66ee1922023-10-26 14:53:49 -0700711 RpcMutexUniqueLock _l(e->mLock);
Yifan Hong8b890852021-06-10 13:44:09 -0700712 auto rpcServer = RpcServer::make();
713 LOG_ALWAYS_FATAL_IF(rpcServer == nullptr, "RpcServer::make returns null");
Yifan Hong8b890852021-06-10 13:44:09 -0700714 auto link = sp<RpcServerLink>::make(rpcServer, keepAliveBinder, weakThis);
715 if (auto status = keepAliveBinder->linkToDeath(link, nullptr, 0); status != OK) {
716 ALOGE("%s: keepAliveBinder->linkToDeath returns %s", __PRETTY_FUNCTION__,
717 statusToString(status).c_str());
718 return status;
Yifan Hong84bedeb2021-04-21 21:37:17 -0700719 }
Yifan Hong8b890852021-06-10 13:44:09 -0700720 rpcServer->setRootObjectWeak(weakThis);
Steven Moreland2372f9d2021-08-05 15:42:01 -0700721 if (auto status = rpcServer->setupExternalServer(std::move(socketFd)); status != OK) {
722 return status;
723 }
Yifan Hong8b890852021-06-10 13:44:09 -0700724 rpcServer->setMaxThreads(binderThreadPoolMaxCount);
Tomasz Wasilczyke3de8802023-11-01 11:05:27 -0700725 ALOGI("RpcBinder: Started Binder debug on %s", String8(getInterfaceDescriptor()).c_str());
Yifan Hong8b890852021-06-10 13:44:09 -0700726 rpcServer->start();
727 e->mRpcServerLinks.emplace(link);
Yifan Hong34823232021-06-07 17:23:00 -0700728 LOG_RPC_DETAIL("%s(fd=%d) successful", __PRETTY_FUNCTION__, socketFdForPrint);
Yifan Hong84bedeb2021-04-21 21:37:17 -0700729 return OK;
730}
731
Yifan Hong8b890852021-06-10 13:44:09 -0700732void BBinder::removeRpcServerLink(const sp<RpcServerLink>& link) {
733 Extras* e = mExtras.load(std::memory_order_acquire);
734 if (!e) return;
Tomasz Wasilczyk66ee1922023-10-26 14:53:49 -0700735 RpcMutexUniqueLock _l(e->mLock);
Yifan Hong8b890852021-06-10 13:44:09 -0700736 (void)e->mRpcServerLinks.erase(link);
737}
738
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800739BBinder::~BBinder()
740{
Steven Moreland1bd2bc72022-07-29 20:29:40 +0000741 if (!wasParceled()) {
742 if (getExtension()) {
Tomasz Wasilczyke3de8802023-11-01 11:05:27 -0700743 ALOGW("Binder %p destroyed with extension attached before being parceled.", this);
Steven Moreland1bd2bc72022-07-29 20:29:40 +0000744 }
745 if (isRequestingSid()) {
Tomasz Wasilczyke3de8802023-11-01 11:05:27 -0700746 ALOGW("Binder %p destroyed when requesting SID before being parceled.", this);
Steven Moreland1bd2bc72022-07-29 20:29:40 +0000747 }
748 if (isInheritRt()) {
Tomasz Wasilczyke3de8802023-11-01 11:05:27 -0700749 ALOGW("Binder %p destroyed after setInheritRt before being parceled.", this);
Steven Moreland1bd2bc72022-07-29 20:29:40 +0000750 }
Andrei Homescu40525502022-08-02 01:23:22 +0000751#ifdef __linux__
Steven Moreland1bd2bc72022-07-29 20:29:40 +0000752 if (getMinSchedulerPolicy() != SCHED_NORMAL) {
Tomasz Wasilczyke3de8802023-11-01 11:05:27 -0700753 ALOGW("Binder %p destroyed after setMinSchedulerPolicy before being parceled.", this);
Steven Moreland1bd2bc72022-07-29 20:29:40 +0000754 }
755 if (getMinSchedulerPriority() != 0) {
Tomasz Wasilczyke3de8802023-11-01 11:05:27 -0700756 ALOGW("Binder %p destroyed after setMinSchedulerPolicy before being parceled.", this);
Steven Moreland1bd2bc72022-07-29 20:29:40 +0000757 }
Andrei Homescu40525502022-08-02 01:23:22 +0000758#endif // __linux__
Steven Moreland55a12542022-03-31 21:53:11 +0000759 }
760
Bailey Forrest6913c462015-08-18 17:15:10 -0700761 Extras* e = mExtras.load(std::memory_order_relaxed);
Hans Boehm3effaba2014-08-12 22:56:00 +0000762 if (e) delete e;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800763}
764
765
Jiyong Parkb86c8662018-10-29 23:01:57 +0900766// NOLINTNEXTLINE(google-default-arguments)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800767status_t BBinder::onTransact(
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800768 uint32_t code, const Parcel& data, Parcel* reply, uint32_t /*flags*/)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800769{
770 switch (code) {
771 case INTERFACE_TRANSACTION:
Tomasz Wasilczyke3de8802023-11-01 11:05:27 -0700772 LOG_ALWAYS_FATAL_IF(reply == nullptr, "reply == nullptr");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800773 reply->writeString16(getInterfaceDescriptor());
774 return NO_ERROR;
775
776 case DUMP_TRANSACTION: {
777 int fd = data.readFileDescriptor();
778 int argc = data.readInt32();
779 Vector<String16> args;
780 for (int i = 0; i < argc && data.dataAvail() > 0; i++) {
781 args.add(data.readString16());
782 }
783 return dump(fd, args);
784 }
Dianne Hackborn555f89d2012-05-08 18:54:22 -0700785
Dianne Hackborn23eb1e22015-10-07 17:35:27 -0700786 case SHELL_COMMAND_TRANSACTION: {
787 int in = data.readFileDescriptor();
788 int out = data.readFileDescriptor();
789 int err = data.readFileDescriptor();
790 int argc = data.readInt32();
791 Vector<String16> args;
792 for (int i = 0; i < argc && data.dataAvail() > 0; i++) {
793 args.add(data.readString16());
794 }
Andrei Homescua543a842022-07-07 22:17:55 +0000795 sp<IBinder> shellCallbackBinder = data.readStrongBinder();
Dianne Hackborn23eb1e22015-10-07 17:35:27 -0700796 sp<IResultReceiver> resultReceiver = IResultReceiver::asInterface(
797 data.readStrongBinder());
798
Dianne Hackbornf2bf93b2015-10-14 15:13:02 -0700799 // XXX can't add virtuals until binaries are updated.
Andrei Homescua543a842022-07-07 22:17:55 +0000800 // sp<IShellCallback> shellCallback = IShellCallback::asInterface(
801 // shellCallbackBinder);
802 // return shellCommand(in, out, err, args, resultReceiver);
Christopher Wiley0a9a1c12016-07-20 08:28:14 -0700803 (void)in;
804 (void)out;
805 (void)err;
806
Yi Kongfdd8da92018-06-07 17:52:27 -0700807 if (resultReceiver != nullptr) {
Dianne Hackbornf2bf93b2015-10-14 15:13:02 -0700808 resultReceiver->send(INVALID_OPERATION);
809 }
Martijn Coenenaa6ee992017-08-17 15:38:08 +0200810
811 return NO_ERROR;
Dianne Hackborn23eb1e22015-10-07 17:35:27 -0700812 }
813
Dianne Hackborn555f89d2012-05-08 18:54:22 -0700814 case SYSPROPS_TRANSACTION: {
Tomasz Wasilczyk346f1042023-10-09 17:02:46 +0000815 if (!binder::os::report_sysprop_change()) return INVALID_OPERATION;
Dianne Hackborn555f89d2012-05-08 18:54:22 -0700816 return NO_ERROR;
817 }
818
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800819 default:
820 return UNKNOWN_TRANSACTION;
821 }
822}
823
Steven Morelandf0212002018-12-26 13:59:23 -0800824BBinder::Extras* BBinder::getOrCreateExtras()
825{
826 Extras* e = mExtras.load(std::memory_order_acquire);
827
828 if (!e) {
829 e = new Extras;
830 Extras* expected = nullptr;
831 if (!mExtras.compare_exchange_strong(expected, e,
832 std::memory_order_release,
833 std::memory_order_acquire)) {
834 delete e;
835 e = expected; // Filled in by CAS
836 }
837 if (e == nullptr) return nullptr; // out of memory
838 }
839
840 return e;
841}
842
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800843// ---------------------------------------------------------------------------
844
845enum {
846 // This is used to transfer ownership of the remote binder from
847 // the BpRefBase object holding it (when it is constructed), to the
848 // owner of the BpRefBase object when it first acquires that BpRefBase.
849 kRemoteAcquired = 0x00000001
850};
851
852BpRefBase::BpRefBase(const sp<IBinder>& o)
Yi Kongfdd8da92018-06-07 17:52:27 -0700853 : mRemote(o.get()), mRefs(nullptr), mState(0)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800854{
855 extendObjectLifetime(OBJECT_LIFETIME_WEAK);
856
857 if (mRemote) {
858 mRemote->incStrong(this); // Removed on first IncStrong().
859 mRefs = mRemote->createWeak(this); // Held for our entire lifetime.
860 }
861}
862
863BpRefBase::~BpRefBase()
864{
865 if (mRemote) {
Bailey Forrest6913c462015-08-18 17:15:10 -0700866 if (!(mState.load(std::memory_order_relaxed)&kRemoteAcquired)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800867 mRemote->decStrong(this);
868 }
869 mRefs->decWeak(this);
870 }
871}
872
873void BpRefBase::onFirstRef()
874{
Bailey Forrest6913c462015-08-18 17:15:10 -0700875 mState.fetch_or(kRemoteAcquired, std::memory_order_relaxed);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800876}
877
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800878void BpRefBase::onLastStrongRef(const void* /*id*/)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800879{
880 if (mRemote) {
881 mRemote->decStrong(this);
882 }
883}
884
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800885bool BpRefBase::onIncStrongAttempted(uint32_t /*flags*/, const void* /*id*/)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800886{
887 return mRemote ? mRefs->attemptIncStrong(this) : false;
888}
889
890// ---------------------------------------------------------------------------
891
Steven Moreland61ff8492019-09-26 16:05:45 -0700892} // namespace android