blob: e2db1a389ca281245e412ff1b5646e8819a80604 [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
Jiyong Park5970d0a2022-03-08 16:56:13 +090022#include <android-base/logging.h>
Yifan Hong84bedeb2021-04-21 21:37:17 -070023#include <android-base/unique_fd.h>
Mathias Agopianc5b2c0b2009-05-19 19:08:10 -070024#include <binder/BpBinder.h>
25#include <binder/IInterface.h>
Yifan Hong84bedeb2021-04-21 21:37:17 -070026#include <binder/IPCThreadState.h>
Dianne Hackborn23eb1e22015-10-07 17:35:27 -070027#include <binder/IResultReceiver.h>
Dianne Hackborn1941a402016-08-29 12:30:43 -070028#include <binder/IShellCallback.h>
Mathias Agopianc5b2c0b2009-05-19 19:08:10 -070029#include <binder/Parcel.h>
Yifan Hong84bedeb2021-04-21 21:37:17 -070030#include <binder/RpcServer.h>
31#include <private/android_filesystem_config.h>
32#include <utils/misc.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080033
Yifan Hong84bedeb2021-04-21 21:37:17 -070034#include <inttypes.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080035#include <stdio.h>
36
Andrei Homescuf55d6882022-04-30 00:50:20 +000037#ifdef __linux__
38#include <linux/sched.h>
39#endif
40
Yifan Hong84bedeb2021-04-21 21:37:17 -070041#include "RpcState.h"
42
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080043namespace android {
44
Steven Moreland90c1f9a2021-05-03 18:27:24 +000045// Service implementations inherit from BBinder and IBinder, and this is frozen
46// in prebuilts.
47#ifdef __LP64__
48static_assert(sizeof(IBinder) == 24);
49static_assert(sizeof(BBinder) == 40);
50#else
51static_assert(sizeof(IBinder) == 12);
52static_assert(sizeof(BBinder) == 20);
53#endif
54
Steven Morelandd9a74002022-06-02 00:03:18 +000055// global b/c b/230079120 - consistent symbol table
Yifan Hong84bedeb2021-04-21 21:37:17 -070056#ifdef BINDER_RPC_DEV_SERVERS
Steven Morelandd9a74002022-06-02 00:03:18 +000057bool kEnableRpcDevServers = true;
Yifan Hong84bedeb2021-04-21 21:37:17 -070058#else
Steven Morelandd9a74002022-06-02 00:03:18 +000059bool kEnableRpcDevServers = false;
Yifan Hong84bedeb2021-04-21 21:37:17 -070060#endif
61
Martijn Coenen1cad19c2021-10-04 09:19:01 +020062// Log any reply transactions for which the data exceeds this size
63#define LOG_REPLIES_OVER_SIZE (300 * 1024)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080064// ---------------------------------------------------------------------------
65
Mathias Agopian83c04462009-05-22 19:00:22 -070066IBinder::IBinder()
67 : RefBase()
68{
69}
70
71IBinder::~IBinder()
72{
73}
74
75// ---------------------------------------------------------------------------
76
Colin Cross6f4f3ab2014-02-05 17:42:44 -080077sp<IInterface> IBinder::queryLocalInterface(const String16& /*descriptor*/)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080078{
Yi Kongfdd8da92018-06-07 17:52:27 -070079 return nullptr;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080080}
81
82BBinder* IBinder::localBinder()
83{
Yi Kongfdd8da92018-06-07 17:52:27 -070084 return nullptr;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080085}
86
87BpBinder* IBinder::remoteBinder()
88{
Yi Kongfdd8da92018-06-07 17:52:27 -070089 return nullptr;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080090}
91
92bool IBinder::checkSubclass(const void* /*subclassID*/) const
93{
94 return false;
95}
96
Dianne Hackborn23eb1e22015-10-07 17:35:27 -070097
Dianne Hackbornf2bf93b2015-10-14 15:13:02 -070098status_t IBinder::shellCommand(const sp<IBinder>& target, int in, int out, int err,
Dianne Hackborn1941a402016-08-29 12:30:43 -070099 Vector<String16>& args, const sp<IShellCallback>& callback,
100 const sp<IResultReceiver>& resultReceiver)
Dianne Hackborn23eb1e22015-10-07 17:35:27 -0700101{
Dianne Hackbornf2bf93b2015-10-14 15:13:02 -0700102 Parcel send;
103 Parcel reply;
104 send.writeFileDescriptor(in);
105 send.writeFileDescriptor(out);
106 send.writeFileDescriptor(err);
107 const size_t numArgs = args.size();
108 send.writeInt32(numArgs);
109 for (size_t i = 0; i < numArgs; i++) {
110 send.writeString16(args[i]);
Dianne Hackborn23eb1e22015-10-07 17:35:27 -0700111 }
Yi Kongfdd8da92018-06-07 17:52:27 -0700112 send.writeStrongBinder(callback != nullptr ? IInterface::asBinder(callback) : nullptr);
113 send.writeStrongBinder(resultReceiver != nullptr ? IInterface::asBinder(resultReceiver) : nullptr);
Dianne Hackbornf2bf93b2015-10-14 15:13:02 -0700114 return target->transact(SHELL_COMMAND_TRANSACTION, send, &reply);
Dianne Hackborn23eb1e22015-10-07 17:35:27 -0700115}
116
Steven Morelandb8ad08d2019-08-09 14:42:56 -0700117status_t IBinder::getExtension(sp<IBinder>* out) {
118 BBinder* local = this->localBinder();
119 if (local != nullptr) {
120 *out = local->getExtension();
121 return OK;
122 }
123
124 BpBinder* proxy = this->remoteBinder();
125 LOG_ALWAYS_FATAL_IF(proxy == nullptr);
126
127 Parcel data;
128 Parcel reply;
129 status_t status = transact(EXTENSION_TRANSACTION, data, &reply);
130 if (status != OK) return status;
131
132 return reply.readNullableStrongBinder(out);
133}
134
Steven Moreland86080b82019-09-23 15:41:18 -0700135status_t IBinder::getDebugPid(pid_t* out) {
136 BBinder* local = this->localBinder();
137 if (local != nullptr) {
138 *out = local->getDebugPid();
139 return OK;
140 }
141
142 BpBinder* proxy = this->remoteBinder();
143 LOG_ALWAYS_FATAL_IF(proxy == nullptr);
144
145 Parcel data;
146 Parcel reply;
147 status_t status = transact(DEBUG_PID_TRANSACTION, data, &reply);
148 if (status != OK) return status;
149
150 int32_t pid;
151 status = reply.readInt32(&pid);
152 if (status != OK) return status;
153
154 if (pid < 0 || pid > std::numeric_limits<pid_t>::max()) {
155 return BAD_VALUE;
156 }
157 *out = pid;
158 return OK;
159}
160
Yifan Hong02530ec2021-06-10 13:38:38 -0700161status_t IBinder::setRpcClientDebug(android::base::unique_fd socketFd,
162 const sp<IBinder>& keepAliveBinder) {
Steven Morelandd9a74002022-06-02 00:03:18 +0000163 if (!kEnableRpcDevServers) {
Yifan Hong84bedeb2021-04-21 21:37:17 -0700164 ALOGW("setRpcClientDebug disallowed because RPC is not enabled");
165 return INVALID_OPERATION;
166 }
167
168 BBinder* local = this->localBinder();
169 if (local != nullptr) {
Yifan Hong02530ec2021-06-10 13:38:38 -0700170 return local->BBinder::setRpcClientDebug(std::move(socketFd), keepAliveBinder);
Yifan Hong84bedeb2021-04-21 21:37:17 -0700171 }
172
173 BpBinder* proxy = this->remoteBinder();
174 LOG_ALWAYS_FATAL_IF(proxy == nullptr, "binder object must be either local or remote");
175
176 Parcel data;
177 Parcel reply;
178 status_t status;
179 if (status = data.writeBool(socketFd.ok()); status != OK) return status;
180 if (socketFd.ok()) {
181 // writeUniqueFileDescriptor currently makes an unnecessary dup().
182 status = data.writeFileDescriptor(socketFd.release(), true /* own */);
183 if (status != OK) return status;
184 }
Yifan Hong02530ec2021-06-10 13:38:38 -0700185 if (status = data.writeStrongBinder(keepAliveBinder); status != OK) return status;
Yifan Hong84bedeb2021-04-21 21:37:17 -0700186 return transact(SET_RPC_CLIENT_TRANSACTION, data, &reply);
187}
188
Steven Moreland9e759e82021-06-25 21:30:23 +0000189void IBinder::withLock(const std::function<void()>& doWithLock) {
190 BBinder* local = localBinder();
191 if (local) {
192 local->withLock(doWithLock);
193 return;
194 }
195 BpBinder* proxy = this->remoteBinder();
196 LOG_ALWAYS_FATAL_IF(proxy == nullptr, "binder object must be either local or remote");
197 proxy->withLock(doWithLock);
198}
199
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800200// ---------------------------------------------------------------------------
201
Yifan Hong8b890852021-06-10 13:44:09 -0700202class BBinder::RpcServerLink : public IBinder::DeathRecipient {
203public:
204 // On binder died, calls RpcServer::shutdown on @a rpcServer, and removes itself from @a binder.
205 RpcServerLink(const sp<RpcServer>& rpcServer, const sp<IBinder>& keepAliveBinder,
206 const wp<BBinder>& binder)
207 : mRpcServer(rpcServer), mKeepAliveBinder(keepAliveBinder), mBinder(binder) {}
Steven Morelandd9a74002022-06-02 00:03:18 +0000208 virtual ~RpcServerLink();
Yifan Hong8b890852021-06-10 13:44:09 -0700209 void binderDied(const wp<IBinder>&) override {
210 LOG_RPC_DETAIL("RpcServerLink: binder died, shutting down RpcServer");
211 if (mRpcServer == nullptr) {
212 ALOGW("RpcServerLink: Unable to shut down RpcServer because it does not exist.");
213 } else {
214 ALOGW_IF(!mRpcServer->shutdown(),
215 "RpcServerLink: RpcServer did not shut down properly. Not started?");
216 }
217 mRpcServer.clear();
218
219 auto promoted = mBinder.promote();
220 if (promoted == nullptr) {
221 ALOGW("RpcServerLink: Unable to remove link from parent binder object because parent "
222 "binder object is gone.");
223 } else {
224 promoted->removeRpcServerLink(sp<RpcServerLink>::fromExisting(this));
225 }
226 mBinder.clear();
227 }
228
229private:
230 sp<RpcServer> mRpcServer;
231 sp<IBinder> mKeepAliveBinder; // hold to avoid automatically unlinking
232 wp<BBinder> mBinder;
233};
Steven Morelandd9a74002022-06-02 00:03:18 +0000234BBinder::RpcServerLink::~RpcServerLink() {}
Yifan Hong8b890852021-06-10 13:44:09 -0700235
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800236class BBinder::Extras
237{
238public:
Steven Morelandf0212002018-12-26 13:59:23 -0800239 // unlocked objects
Steven Morelandb8ad08d2019-08-09 14:42:56 -0700240 sp<IBinder> mExtension;
Andrei Homescuf55d6882022-04-30 00:50:20 +0000241#ifdef __linux__
Steven Morelandbf1915b2020-07-16 22:43:02 +0000242 int mPolicy = SCHED_NORMAL;
243 int mPriority = 0;
Andrei Homescuf55d6882022-04-30 00:50:20 +0000244#endif
245 bool mRequestingSid = false;
246 bool mInheritRt = false;
Steven Morelandf0212002018-12-26 13:59:23 -0800247
248 // for below objects
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800249 Mutex mLock;
Yifan Hong8b890852021-06-10 13:44:09 -0700250 std::set<sp<RpcServerLink>> mRpcServerLinks;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800251 BpBinder::ObjectManager mObjects;
252};
253
254// ---------------------------------------------------------------------------
255
Kalesh Singhd67c8e82020-12-29 15:46:25 -0500256BBinder::BBinder() : mExtras(nullptr), mStability(0), mParceled(false) {}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800257
258bool BBinder::isBinderAlive() const
259{
260 return true;
261}
262
263status_t BBinder::pingBinder()
264{
265 return NO_ERROR;
266}
267
Mathias Agopian83c04462009-05-22 19:00:22 -0700268const String16& BBinder::getInterfaceDescriptor() const
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800269{
Dan Egnor386a3322010-05-06 00:55:09 -0700270 // This is a local static rather than a global static,
271 // to avoid static initializer ordering issues.
272 static String16 sEmptyDescriptor;
Steve Block32397c12012-01-05 23:22:43 +0000273 ALOGW("reached BBinder::getInterfaceDescriptor (this=%p)", this);
Mathias Agopian83c04462009-05-22 19:00:22 -0700274 return sEmptyDescriptor;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800275}
276
Jiyong Parkb86c8662018-10-29 23:01:57 +0900277// NOLINTNEXTLINE(google-default-arguments)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800278status_t BBinder::transact(
279 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
280{
281 data.setDataPosition(0);
282
Steven Morelandf183fdd2020-10-27 00:12:12 +0000283 if (reply != nullptr && (flags & FLAG_CLEAR_BUF)) {
284 reply->markSensitive();
285 }
286
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800287 status_t err = NO_ERROR;
288 switch (code) {
289 case PING_TRANSACTION:
Steven Moreland6e69d652019-07-10 14:17:55 -0700290 err = pingBinder();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800291 break;
Steven Morelandb8ad08d2019-08-09 14:42:56 -0700292 case EXTENSION_TRANSACTION:
Jiyong Park5970d0a2022-03-08 16:56:13 +0900293 CHECK(reply != nullptr);
Steven Morelandb8ad08d2019-08-09 14:42:56 -0700294 err = reply->writeStrongBinder(getExtension());
295 break;
Steven Moreland86080b82019-09-23 15:41:18 -0700296 case DEBUG_PID_TRANSACTION:
Jiyong Park5970d0a2022-03-08 16:56:13 +0900297 CHECK(reply != nullptr);
Steven Moreland86080b82019-09-23 15:41:18 -0700298 err = reply->writeInt32(getDebugPid());
299 break;
Yifan Hong84bedeb2021-04-21 21:37:17 -0700300 case SET_RPC_CLIENT_TRANSACTION: {
301 err = setRpcClientDebug(data);
302 break;
303 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800304 default:
305 err = onTransact(code, data, reply, flags);
306 break;
307 }
308
Steven Morelanda86a3562019-08-01 23:28:34 +0000309 // In case this is being transacted on in the same process.
Yi Kongfdd8da92018-06-07 17:52:27 -0700310 if (reply != nullptr) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800311 reply->setDataPosition(0);
Martijn Coenen1cad19c2021-10-04 09:19:01 +0200312 if (reply->dataSize() > LOG_REPLIES_OVER_SIZE) {
313 ALOGW("Large reply transaction of %zu bytes, interface descriptor %s, code %d",
314 reply->dataSize(), String8(getInterfaceDescriptor()).c_str(), code);
315 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800316 }
317
318 return err;
319}
320
Jiyong Parkb86c8662018-10-29 23:01:57 +0900321// NOLINTNEXTLINE(google-default-arguments)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800322status_t BBinder::linkToDeath(
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800323 const sp<DeathRecipient>& /*recipient*/, void* /*cookie*/,
324 uint32_t /*flags*/)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800325{
326 return INVALID_OPERATION;
327}
328
Jiyong Parkb86c8662018-10-29 23:01:57 +0900329// NOLINTNEXTLINE(google-default-arguments)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800330status_t BBinder::unlinkToDeath(
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800331 const wp<DeathRecipient>& /*recipient*/, void* /*cookie*/,
332 uint32_t /*flags*/, wp<DeathRecipient>* /*outRecipient*/)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800333{
334 return INVALID_OPERATION;
335}
336
Dianne Hackborn23eb1e22015-10-07 17:35:27 -0700337status_t BBinder::dump(int /*fd*/, const Vector<String16>& /*args*/)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800338{
339 return NO_ERROR;
340}
341
Steven Moreland63a2d512021-06-25 01:10:15 +0000342void* BBinder::attachObject(const void* objectID, void* object, void* cleanupCookie,
343 object_cleanup_func func) {
Steven Morelandf0212002018-12-26 13:59:23 -0800344 Extras* e = getOrCreateExtras();
Steven Moreland5ce7ca52021-06-25 21:59:50 +0000345 LOG_ALWAYS_FATAL_IF(!e, "no memory");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800346
347 AutoMutex _l(e->mLock);
Steven Moreland63a2d512021-06-25 01:10:15 +0000348 return e->mObjects.attach(objectID, object, cleanupCookie, func);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800349}
350
351void* BBinder::findObject(const void* objectID) const
352{
Bailey Forrest6913c462015-08-18 17:15:10 -0700353 Extras* e = mExtras.load(std::memory_order_acquire);
Yi Kongfdd8da92018-06-07 17:52:27 -0700354 if (!e) return nullptr;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800355
356 AutoMutex _l(e->mLock);
357 return e->mObjects.find(objectID);
358}
359
Steven Moreland63a2d512021-06-25 01:10:15 +0000360void* BBinder::detachObject(const void* objectID) {
Bailey Forrest6913c462015-08-18 17:15:10 -0700361 Extras* e = mExtras.load(std::memory_order_acquire);
Steven Moreland63a2d512021-06-25 01:10:15 +0000362 if (!e) return nullptr;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800363
364 AutoMutex _l(e->mLock);
Steven Moreland63a2d512021-06-25 01:10:15 +0000365 return e->mObjects.detach(objectID);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800366}
367
Steven Moreland9e759e82021-06-25 21:30:23 +0000368void BBinder::withLock(const std::function<void()>& doWithLock) {
369 Extras* e = getOrCreateExtras();
370 LOG_ALWAYS_FATAL_IF(!e, "no memory");
371
372 AutoMutex _l(e->mLock);
373 doWithLock();
374}
375
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800376BBinder* BBinder::localBinder()
377{
378 return this;
379}
380
Steven Morelandf0212002018-12-26 13:59:23 -0800381bool BBinder::isRequestingSid()
382{
383 Extras* e = mExtras.load(std::memory_order_acquire);
384
385 return e && e->mRequestingSid;
386}
387
388void BBinder::setRequestingSid(bool requestingSid)
389{
Steven Morelandd37e6dd2021-06-16 22:07:51 +0000390 LOG_ALWAYS_FATAL_IF(mParceled,
391 "setRequestingSid() should not be called after a binder object "
392 "is parceled/sent to another process");
Kalesh Singhd67c8e82020-12-29 15:46:25 -0500393
Steven Morelandf0212002018-12-26 13:59:23 -0800394 Extras* e = mExtras.load(std::memory_order_acquire);
395
396 if (!e) {
397 // default is false. Most things don't need sids, so avoiding allocations when possible.
398 if (!requestingSid) {
399 return;
400 }
401
402 e = getOrCreateExtras();
403 if (!e) return; // out of memory
404 }
405
Steven Moreland3668be62019-02-08 17:56:55 -0800406 e->mRequestingSid = requestingSid;
Steven Morelandf0212002018-12-26 13:59:23 -0800407}
408
Steven Morelandb8ad08d2019-08-09 14:42:56 -0700409sp<IBinder> BBinder::getExtension() {
410 Extras* e = mExtras.load(std::memory_order_acquire);
411 if (e == nullptr) return nullptr;
412 return e->mExtension;
413}
414
Andrei Homescuf55d6882022-04-30 00:50:20 +0000415#ifdef __linux__
Steven Morelandbf1915b2020-07-16 22:43:02 +0000416void BBinder::setMinSchedulerPolicy(int policy, int priority) {
Steven Morelandd37e6dd2021-06-16 22:07:51 +0000417 LOG_ALWAYS_FATAL_IF(mParceled,
418 "setMinSchedulerPolicy() should not be called after a binder object "
419 "is parceled/sent to another process");
Kalesh Singhd67c8e82020-12-29 15:46:25 -0500420
Steven Morelandbf1915b2020-07-16 22:43:02 +0000421 switch (policy) {
422 case SCHED_NORMAL:
423 LOG_ALWAYS_FATAL_IF(priority < -20 || priority > 19, "Invalid priority for SCHED_NORMAL: %d", priority);
424 break;
425 case SCHED_RR:
426 case SCHED_FIFO:
427 LOG_ALWAYS_FATAL_IF(priority < 1 || priority > 99, "Invalid priority for sched %d: %d", policy, priority);
428 break;
429 default:
430 LOG_ALWAYS_FATAL("Unrecognized scheduling policy: %d", policy);
431 }
432
433 Extras* e = mExtras.load(std::memory_order_acquire);
434
435 if (e == nullptr) {
436 // Avoid allocations if called with default.
437 if (policy == SCHED_NORMAL && priority == 0) {
438 return;
439 }
440
441 e = getOrCreateExtras();
442 if (!e) return; // out of memory
443 }
444
445 e->mPolicy = policy;
446 e->mPriority = priority;
447}
448
449int BBinder::getMinSchedulerPolicy() {
450 Extras* e = mExtras.load(std::memory_order_acquire);
451 if (e == nullptr) return SCHED_NORMAL;
452 return e->mPolicy;
453}
454
455int BBinder::getMinSchedulerPriority() {
456 Extras* e = mExtras.load(std::memory_order_acquire);
457 if (e == nullptr) return 0;
458 return e->mPriority;
459}
Andrei Homescuf55d6882022-04-30 00:50:20 +0000460#endif // __linux__
Steven Morelandbf1915b2020-07-16 22:43:02 +0000461
Steven Morelandcf03cf12020-12-04 02:58:40 +0000462bool BBinder::isInheritRt() {
463 Extras* e = mExtras.load(std::memory_order_acquire);
464
465 return e && e->mInheritRt;
466}
467
468void BBinder::setInheritRt(bool inheritRt) {
Steven Morelandd37e6dd2021-06-16 22:07:51 +0000469 LOG_ALWAYS_FATAL_IF(mParceled,
470 "setInheritRt() should not be called after a binder object "
471 "is parceled/sent to another process");
Kalesh Singhd67c8e82020-12-29 15:46:25 -0500472
Steven Morelandcf03cf12020-12-04 02:58:40 +0000473 Extras* e = mExtras.load(std::memory_order_acquire);
474
475 if (!e) {
476 if (!inheritRt) {
477 return;
478 }
479
480 e = getOrCreateExtras();
481 if (!e) return; // out of memory
482 }
483
484 e->mInheritRt = inheritRt;
485}
486
Steven Moreland86080b82019-09-23 15:41:18 -0700487pid_t BBinder::getDebugPid() {
Andrei Homescuf55d6882022-04-30 00:50:20 +0000488#ifdef __linux__
Steven Moreland86080b82019-09-23 15:41:18 -0700489 return getpid();
Andrei Homescuf55d6882022-04-30 00:50:20 +0000490#else
491 // TODO: handle other OSes
492 return 0;
493#endif // __linux__
Steven Moreland86080b82019-09-23 15:41:18 -0700494}
495
Steven Morelandb8ad08d2019-08-09 14:42:56 -0700496void BBinder::setExtension(const sp<IBinder>& extension) {
Steven Morelandd37e6dd2021-06-16 22:07:51 +0000497 LOG_ALWAYS_FATAL_IF(mParceled,
498 "setExtension() should not be called after a binder object "
499 "is parceled/sent to another process");
Kalesh Singhd67c8e82020-12-29 15:46:25 -0500500
Steven Morelandb8ad08d2019-08-09 14:42:56 -0700501 Extras* e = getOrCreateExtras();
502 e->mExtension = extension;
503}
504
Kalesh Singhd67c8e82020-12-29 15:46:25 -0500505bool BBinder::wasParceled() {
506 return mParceled;
507}
508
509void BBinder::setParceled() {
510 mParceled = true;
511}
512
Yifan Hong84bedeb2021-04-21 21:37:17 -0700513status_t BBinder::setRpcClientDebug(const Parcel& data) {
Steven Morelandd9a74002022-06-02 00:03:18 +0000514 if (!kEnableRpcDevServers) {
Yifan Hong84bedeb2021-04-21 21:37:17 -0700515 ALOGW("%s: disallowed because RPC is not enabled", __PRETTY_FUNCTION__);
516 return INVALID_OPERATION;
517 }
518 uid_t uid = IPCThreadState::self()->getCallingUid();
519 if (uid != AID_ROOT) {
520 ALOGE("%s: not allowed because client %" PRIu32 " is not root", __PRETTY_FUNCTION__, uid);
521 return PERMISSION_DENIED;
522 }
523 status_t status;
524 bool hasSocketFd;
525 android::base::unique_fd clientFd;
Yifan Hong84bedeb2021-04-21 21:37:17 -0700526
527 if (status = data.readBool(&hasSocketFd); status != OK) return status;
528 if (hasSocketFd) {
529 if (status = data.readUniqueFileDescriptor(&clientFd); status != OK) return status;
530 }
Yifan Hong02530ec2021-06-10 13:38:38 -0700531 sp<IBinder> keepAliveBinder;
532 if (status = data.readNullableStrongBinder(&keepAliveBinder); status != OK) return status;
Yifan Hong84bedeb2021-04-21 21:37:17 -0700533
Yifan Hong02530ec2021-06-10 13:38:38 -0700534 return setRpcClientDebug(std::move(clientFd), keepAliveBinder);
Yifan Hong84bedeb2021-04-21 21:37:17 -0700535}
536
Yifan Hong02530ec2021-06-10 13:38:38 -0700537status_t BBinder::setRpcClientDebug(android::base::unique_fd socketFd,
538 const sp<IBinder>& keepAliveBinder) {
Steven Morelandd9a74002022-06-02 00:03:18 +0000539 if (!kEnableRpcDevServers) {
Yifan Hong84bedeb2021-04-21 21:37:17 -0700540 ALOGW("%s: disallowed because RPC is not enabled", __PRETTY_FUNCTION__);
541 return INVALID_OPERATION;
542 }
543
544 const int socketFdForPrint = socketFd.get();
Yifan Hong34823232021-06-07 17:23:00 -0700545 LOG_RPC_DETAIL("%s(fd=%d)", __PRETTY_FUNCTION__, socketFdForPrint);
Yifan Hong84bedeb2021-04-21 21:37:17 -0700546
547 if (!socketFd.ok()) {
548 ALOGE("%s: No socket FD provided.", __PRETTY_FUNCTION__);
549 return BAD_VALUE;
550 }
Yifan Hong84bedeb2021-04-21 21:37:17 -0700551
Yifan Hong02530ec2021-06-10 13:38:38 -0700552 if (keepAliveBinder == nullptr) {
553 ALOGE("%s: No keepAliveBinder provided.", __PRETTY_FUNCTION__);
554 return UNEXPECTED_NULL;
555 }
556
Elie Kheirallah47431c12022-04-21 23:46:17 +0000557 size_t binderThreadPoolMaxCount = ProcessState::self()->getThreadPoolMaxTotalThreadCount();
Yifan Hong84bedeb2021-04-21 21:37:17 -0700558 if (binderThreadPoolMaxCount <= 1) {
559 ALOGE("%s: ProcessState thread pool max count is %zu. RPC is disabled for this service "
560 "because RPC requires the service to support multithreading.",
561 __PRETTY_FUNCTION__, binderThreadPoolMaxCount);
562 return INVALID_OPERATION;
563 }
564
Yifan Hong8b890852021-06-10 13:44:09 -0700565 // Weak ref to avoid circular dependency:
566 // BBinder -> RpcServerLink ----> RpcServer -X-> BBinder
567 // `-X-> BBinder
568 auto weakThis = wp<BBinder>::fromExisting(this);
569
Yifan Hong84bedeb2021-04-21 21:37:17 -0700570 Extras* e = getOrCreateExtras();
571 AutoMutex _l(e->mLock);
Yifan Hong8b890852021-06-10 13:44:09 -0700572 auto rpcServer = RpcServer::make();
573 LOG_ALWAYS_FATAL_IF(rpcServer == nullptr, "RpcServer::make returns null");
Yifan Hong8b890852021-06-10 13:44:09 -0700574 auto link = sp<RpcServerLink>::make(rpcServer, keepAliveBinder, weakThis);
575 if (auto status = keepAliveBinder->linkToDeath(link, nullptr, 0); status != OK) {
576 ALOGE("%s: keepAliveBinder->linkToDeath returns %s", __PRETTY_FUNCTION__,
577 statusToString(status).c_str());
578 return status;
Yifan Hong84bedeb2021-04-21 21:37:17 -0700579 }
Yifan Hong8b890852021-06-10 13:44:09 -0700580 rpcServer->setRootObjectWeak(weakThis);
Steven Moreland2372f9d2021-08-05 15:42:01 -0700581 if (auto status = rpcServer->setupExternalServer(std::move(socketFd)); status != OK) {
582 return status;
583 }
Yifan Hong8b890852021-06-10 13:44:09 -0700584 rpcServer->setMaxThreads(binderThreadPoolMaxCount);
585 rpcServer->start();
586 e->mRpcServerLinks.emplace(link);
Yifan Hong34823232021-06-07 17:23:00 -0700587 LOG_RPC_DETAIL("%s(fd=%d) successful", __PRETTY_FUNCTION__, socketFdForPrint);
Yifan Hong84bedeb2021-04-21 21:37:17 -0700588 return OK;
589}
590
Yifan Hong8b890852021-06-10 13:44:09 -0700591void BBinder::removeRpcServerLink(const sp<RpcServerLink>& link) {
592 Extras* e = mExtras.load(std::memory_order_acquire);
593 if (!e) return;
594 AutoMutex _l(e->mLock);
595 (void)e->mRpcServerLinks.erase(link);
596}
597
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800598BBinder::~BBinder()
599{
Steven Moreland55a12542022-03-31 21:53:11 +0000600 if (!wasParceled() && getExtension()) {
601 ALOGW("Binder %p destroyed with extension attached before being parceled.", this);
602 }
603
Bailey Forrest6913c462015-08-18 17:15:10 -0700604 Extras* e = mExtras.load(std::memory_order_relaxed);
Hans Boehm3effaba2014-08-12 22:56:00 +0000605 if (e) delete e;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800606}
607
608
Jiyong Parkb86c8662018-10-29 23:01:57 +0900609// NOLINTNEXTLINE(google-default-arguments)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800610status_t BBinder::onTransact(
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800611 uint32_t code, const Parcel& data, Parcel* reply, uint32_t /*flags*/)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800612{
613 switch (code) {
614 case INTERFACE_TRANSACTION:
Jiyong Park5970d0a2022-03-08 16:56:13 +0900615 CHECK(reply != nullptr);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800616 reply->writeString16(getInterfaceDescriptor());
617 return NO_ERROR;
618
619 case DUMP_TRANSACTION: {
620 int fd = data.readFileDescriptor();
621 int argc = data.readInt32();
622 Vector<String16> args;
623 for (int i = 0; i < argc && data.dataAvail() > 0; i++) {
624 args.add(data.readString16());
625 }
626 return dump(fd, args);
627 }
Dianne Hackborn555f89d2012-05-08 18:54:22 -0700628
Dianne Hackborn23eb1e22015-10-07 17:35:27 -0700629 case SHELL_COMMAND_TRANSACTION: {
630 int in = data.readFileDescriptor();
631 int out = data.readFileDescriptor();
632 int err = data.readFileDescriptor();
633 int argc = data.readInt32();
634 Vector<String16> args;
635 for (int i = 0; i < argc && data.dataAvail() > 0; i++) {
636 args.add(data.readString16());
637 }
Dianne Hackborn1941a402016-08-29 12:30:43 -0700638 sp<IShellCallback> shellCallback = IShellCallback::asInterface(
639 data.readStrongBinder());
Dianne Hackborn23eb1e22015-10-07 17:35:27 -0700640 sp<IResultReceiver> resultReceiver = IResultReceiver::asInterface(
641 data.readStrongBinder());
642
Dianne Hackbornf2bf93b2015-10-14 15:13:02 -0700643 // XXX can't add virtuals until binaries are updated.
644 //return shellCommand(in, out, err, args, resultReceiver);
Christopher Wiley0a9a1c12016-07-20 08:28:14 -0700645 (void)in;
646 (void)out;
647 (void)err;
648
Yi Kongfdd8da92018-06-07 17:52:27 -0700649 if (resultReceiver != nullptr) {
Dianne Hackbornf2bf93b2015-10-14 15:13:02 -0700650 resultReceiver->send(INVALID_OPERATION);
651 }
Martijn Coenenaa6ee992017-08-17 15:38:08 +0200652
653 return NO_ERROR;
Dianne Hackborn23eb1e22015-10-07 17:35:27 -0700654 }
655
Dianne Hackborn555f89d2012-05-08 18:54:22 -0700656 case SYSPROPS_TRANSACTION: {
657 report_sysprop_change();
658 return NO_ERROR;
659 }
660
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800661 default:
662 return UNKNOWN_TRANSACTION;
663 }
664}
665
Steven Morelandf0212002018-12-26 13:59:23 -0800666BBinder::Extras* BBinder::getOrCreateExtras()
667{
668 Extras* e = mExtras.load(std::memory_order_acquire);
669
670 if (!e) {
671 e = new Extras;
672 Extras* expected = nullptr;
673 if (!mExtras.compare_exchange_strong(expected, e,
674 std::memory_order_release,
675 std::memory_order_acquire)) {
676 delete e;
677 e = expected; // Filled in by CAS
678 }
679 if (e == nullptr) return nullptr; // out of memory
680 }
681
682 return e;
683}
684
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800685// ---------------------------------------------------------------------------
686
687enum {
688 // This is used to transfer ownership of the remote binder from
689 // the BpRefBase object holding it (when it is constructed), to the
690 // owner of the BpRefBase object when it first acquires that BpRefBase.
691 kRemoteAcquired = 0x00000001
692};
693
694BpRefBase::BpRefBase(const sp<IBinder>& o)
Yi Kongfdd8da92018-06-07 17:52:27 -0700695 : mRemote(o.get()), mRefs(nullptr), mState(0)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800696{
697 extendObjectLifetime(OBJECT_LIFETIME_WEAK);
698
699 if (mRemote) {
700 mRemote->incStrong(this); // Removed on first IncStrong().
701 mRefs = mRemote->createWeak(this); // Held for our entire lifetime.
702 }
703}
704
705BpRefBase::~BpRefBase()
706{
707 if (mRemote) {
Bailey Forrest6913c462015-08-18 17:15:10 -0700708 if (!(mState.load(std::memory_order_relaxed)&kRemoteAcquired)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800709 mRemote->decStrong(this);
710 }
711 mRefs->decWeak(this);
712 }
713}
714
715void BpRefBase::onFirstRef()
716{
Bailey Forrest6913c462015-08-18 17:15:10 -0700717 mState.fetch_or(kRemoteAcquired, std::memory_order_relaxed);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800718}
719
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800720void BpRefBase::onLastStrongRef(const void* /*id*/)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800721{
722 if (mRemote) {
723 mRemote->decStrong(this);
724 }
725}
726
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800727bool BpRefBase::onIncStrongAttempted(uint32_t /*flags*/, const void* /*id*/)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800728{
729 return mRemote ? mRefs->attemptIncStrong(this) : false;
730}
731
732// ---------------------------------------------------------------------------
733
Steven Moreland61ff8492019-09-26 16:05:45 -0700734} // namespace android