blob: 6a12e65fc0f6b798b323fcd537c7f0b7f849a58f [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>
Steven Morelandbf1915b2020-07-16 22:43:02 +000035#include <linux/sched.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080036#include <stdio.h>
37
Yifan Hong84bedeb2021-04-21 21:37:17 -070038#include "RpcState.h"
39
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080040namespace android {
41
Steven Moreland90c1f9a2021-05-03 18:27:24 +000042// Service implementations inherit from BBinder and IBinder, and this is frozen
43// in prebuilts.
44#ifdef __LP64__
45static_assert(sizeof(IBinder) == 24);
46static_assert(sizeof(BBinder) == 40);
47#else
48static_assert(sizeof(IBinder) == 12);
49static_assert(sizeof(BBinder) == 20);
50#endif
51
Steven Morelandd9a74002022-06-02 00:03:18 +000052// global b/c b/230079120 - consistent symbol table
Yifan Hong84bedeb2021-04-21 21:37:17 -070053#ifdef BINDER_RPC_DEV_SERVERS
Steven Morelandd9a74002022-06-02 00:03:18 +000054bool kEnableRpcDevServers = true;
Yifan Hong84bedeb2021-04-21 21:37:17 -070055#else
Steven Morelandd9a74002022-06-02 00:03:18 +000056bool kEnableRpcDevServers = false;
Yifan Hong84bedeb2021-04-21 21:37:17 -070057#endif
58
Martijn Coenen1cad19c2021-10-04 09:19:01 +020059// Log any reply transactions for which the data exceeds this size
60#define LOG_REPLIES_OVER_SIZE (300 * 1024)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080061// ---------------------------------------------------------------------------
62
Mathias Agopian83c04462009-05-22 19:00:22 -070063IBinder::IBinder()
64 : RefBase()
65{
66}
67
68IBinder::~IBinder()
69{
70}
71
72// ---------------------------------------------------------------------------
73
Colin Cross6f4f3ab2014-02-05 17:42:44 -080074sp<IInterface> IBinder::queryLocalInterface(const String16& /*descriptor*/)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080075{
Yi Kongfdd8da92018-06-07 17:52:27 -070076 return nullptr;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080077}
78
79BBinder* IBinder::localBinder()
80{
Yi Kongfdd8da92018-06-07 17:52:27 -070081 return nullptr;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080082}
83
84BpBinder* IBinder::remoteBinder()
85{
Yi Kongfdd8da92018-06-07 17:52:27 -070086 return nullptr;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080087}
88
89bool IBinder::checkSubclass(const void* /*subclassID*/) const
90{
91 return false;
92}
93
Dianne Hackborn23eb1e22015-10-07 17:35:27 -070094
Dianne Hackbornf2bf93b2015-10-14 15:13:02 -070095status_t IBinder::shellCommand(const sp<IBinder>& target, int in, int out, int err,
Dianne Hackborn1941a402016-08-29 12:30:43 -070096 Vector<String16>& args, const sp<IShellCallback>& callback,
97 const sp<IResultReceiver>& resultReceiver)
Dianne Hackborn23eb1e22015-10-07 17:35:27 -070098{
Dianne Hackbornf2bf93b2015-10-14 15:13:02 -070099 Parcel send;
100 Parcel reply;
101 send.writeFileDescriptor(in);
102 send.writeFileDescriptor(out);
103 send.writeFileDescriptor(err);
104 const size_t numArgs = args.size();
105 send.writeInt32(numArgs);
106 for (size_t i = 0; i < numArgs; i++) {
107 send.writeString16(args[i]);
Dianne Hackborn23eb1e22015-10-07 17:35:27 -0700108 }
Yi Kongfdd8da92018-06-07 17:52:27 -0700109 send.writeStrongBinder(callback != nullptr ? IInterface::asBinder(callback) : nullptr);
110 send.writeStrongBinder(resultReceiver != nullptr ? IInterface::asBinder(resultReceiver) : nullptr);
Dianne Hackbornf2bf93b2015-10-14 15:13:02 -0700111 return target->transact(SHELL_COMMAND_TRANSACTION, send, &reply);
Dianne Hackborn23eb1e22015-10-07 17:35:27 -0700112}
113
Steven Morelandb8ad08d2019-08-09 14:42:56 -0700114status_t IBinder::getExtension(sp<IBinder>* out) {
115 BBinder* local = this->localBinder();
116 if (local != nullptr) {
117 *out = local->getExtension();
118 return OK;
119 }
120
121 BpBinder* proxy = this->remoteBinder();
122 LOG_ALWAYS_FATAL_IF(proxy == nullptr);
123
124 Parcel data;
125 Parcel reply;
126 status_t status = transact(EXTENSION_TRANSACTION, data, &reply);
127 if (status != OK) return status;
128
129 return reply.readNullableStrongBinder(out);
130}
131
Steven Moreland86080b82019-09-23 15:41:18 -0700132status_t IBinder::getDebugPid(pid_t* out) {
133 BBinder* local = this->localBinder();
134 if (local != nullptr) {
135 *out = local->getDebugPid();
136 return OK;
137 }
138
139 BpBinder* proxy = this->remoteBinder();
140 LOG_ALWAYS_FATAL_IF(proxy == nullptr);
141
142 Parcel data;
143 Parcel reply;
144 status_t status = transact(DEBUG_PID_TRANSACTION, data, &reply);
145 if (status != OK) return status;
146
147 int32_t pid;
148 status = reply.readInt32(&pid);
149 if (status != OK) return status;
150
151 if (pid < 0 || pid > std::numeric_limits<pid_t>::max()) {
152 return BAD_VALUE;
153 }
154 *out = pid;
155 return OK;
156}
157
Yifan Hong02530ec2021-06-10 13:38:38 -0700158status_t IBinder::setRpcClientDebug(android::base::unique_fd socketFd,
159 const sp<IBinder>& keepAliveBinder) {
Steven Morelandd9a74002022-06-02 00:03:18 +0000160 if (!kEnableRpcDevServers) {
Yifan Hong84bedeb2021-04-21 21:37:17 -0700161 ALOGW("setRpcClientDebug disallowed because RPC is not enabled");
162 return INVALID_OPERATION;
163 }
164
165 BBinder* local = this->localBinder();
166 if (local != nullptr) {
Yifan Hong02530ec2021-06-10 13:38:38 -0700167 return local->BBinder::setRpcClientDebug(std::move(socketFd), keepAliveBinder);
Yifan Hong84bedeb2021-04-21 21:37:17 -0700168 }
169
170 BpBinder* proxy = this->remoteBinder();
171 LOG_ALWAYS_FATAL_IF(proxy == nullptr, "binder object must be either local or remote");
172
173 Parcel data;
174 Parcel reply;
175 status_t status;
176 if (status = data.writeBool(socketFd.ok()); status != OK) return status;
177 if (socketFd.ok()) {
178 // writeUniqueFileDescriptor currently makes an unnecessary dup().
179 status = data.writeFileDescriptor(socketFd.release(), true /* own */);
180 if (status != OK) return status;
181 }
Yifan Hong02530ec2021-06-10 13:38:38 -0700182 if (status = data.writeStrongBinder(keepAliveBinder); status != OK) return status;
Yifan Hong84bedeb2021-04-21 21:37:17 -0700183 return transact(SET_RPC_CLIENT_TRANSACTION, data, &reply);
184}
185
Steven Moreland9e759e82021-06-25 21:30:23 +0000186void IBinder::withLock(const std::function<void()>& doWithLock) {
187 BBinder* local = localBinder();
188 if (local) {
189 local->withLock(doWithLock);
190 return;
191 }
192 BpBinder* proxy = this->remoteBinder();
193 LOG_ALWAYS_FATAL_IF(proxy == nullptr, "binder object must be either local or remote");
194 proxy->withLock(doWithLock);
195}
196
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800197// ---------------------------------------------------------------------------
198
Yifan Hong8b890852021-06-10 13:44:09 -0700199class BBinder::RpcServerLink : public IBinder::DeathRecipient {
200public:
201 // On binder died, calls RpcServer::shutdown on @a rpcServer, and removes itself from @a binder.
202 RpcServerLink(const sp<RpcServer>& rpcServer, const sp<IBinder>& keepAliveBinder,
203 const wp<BBinder>& binder)
204 : mRpcServer(rpcServer), mKeepAliveBinder(keepAliveBinder), mBinder(binder) {}
Steven Morelandd9a74002022-06-02 00:03:18 +0000205 virtual ~RpcServerLink();
Yifan Hong8b890852021-06-10 13:44:09 -0700206 void binderDied(const wp<IBinder>&) override {
207 LOG_RPC_DETAIL("RpcServerLink: binder died, shutting down RpcServer");
208 if (mRpcServer == nullptr) {
209 ALOGW("RpcServerLink: Unable to shut down RpcServer because it does not exist.");
210 } else {
211 ALOGW_IF(!mRpcServer->shutdown(),
212 "RpcServerLink: RpcServer did not shut down properly. Not started?");
213 }
214 mRpcServer.clear();
215
216 auto promoted = mBinder.promote();
217 if (promoted == nullptr) {
218 ALOGW("RpcServerLink: Unable to remove link from parent binder object because parent "
219 "binder object is gone.");
220 } else {
221 promoted->removeRpcServerLink(sp<RpcServerLink>::fromExisting(this));
222 }
223 mBinder.clear();
224 }
225
226private:
227 sp<RpcServer> mRpcServer;
228 sp<IBinder> mKeepAliveBinder; // hold to avoid automatically unlinking
229 wp<BBinder> mBinder;
230};
Steven Morelandd9a74002022-06-02 00:03:18 +0000231BBinder::RpcServerLink::~RpcServerLink() {}
Yifan Hong8b890852021-06-10 13:44:09 -0700232
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800233class BBinder::Extras
234{
235public:
Steven Morelandf0212002018-12-26 13:59:23 -0800236 // unlocked objects
237 bool mRequestingSid = false;
Steven Morelandcf03cf12020-12-04 02:58:40 +0000238 bool mInheritRt = false;
Steven Morelandb8ad08d2019-08-09 14:42:56 -0700239 sp<IBinder> mExtension;
Steven Morelandbf1915b2020-07-16 22:43:02 +0000240 int mPolicy = SCHED_NORMAL;
241 int mPriority = 0;
Steven Morelandf0212002018-12-26 13:59:23 -0800242
243 // for below objects
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800244 Mutex mLock;
Yifan Hong8b890852021-06-10 13:44:09 -0700245 std::set<sp<RpcServerLink>> mRpcServerLinks;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800246 BpBinder::ObjectManager mObjects;
247};
248
249// ---------------------------------------------------------------------------
250
Kalesh Singhd67c8e82020-12-29 15:46:25 -0500251BBinder::BBinder() : mExtras(nullptr), mStability(0), mParceled(false) {}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800252
253bool BBinder::isBinderAlive() const
254{
255 return true;
256}
257
258status_t BBinder::pingBinder()
259{
260 return NO_ERROR;
261}
262
Mathias Agopian83c04462009-05-22 19:00:22 -0700263const String16& BBinder::getInterfaceDescriptor() const
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800264{
Dan Egnor386a3322010-05-06 00:55:09 -0700265 // This is a local static rather than a global static,
266 // to avoid static initializer ordering issues.
267 static String16 sEmptyDescriptor;
Steve Block32397c12012-01-05 23:22:43 +0000268 ALOGW("reached BBinder::getInterfaceDescriptor (this=%p)", this);
Mathias Agopian83c04462009-05-22 19:00:22 -0700269 return sEmptyDescriptor;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800270}
271
Jiyong Parkb86c8662018-10-29 23:01:57 +0900272// NOLINTNEXTLINE(google-default-arguments)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800273status_t BBinder::transact(
274 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
275{
276 data.setDataPosition(0);
277
Steven Morelandf183fdd2020-10-27 00:12:12 +0000278 if (reply != nullptr && (flags & FLAG_CLEAR_BUF)) {
279 reply->markSensitive();
280 }
281
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800282 status_t err = NO_ERROR;
283 switch (code) {
284 case PING_TRANSACTION:
Steven Moreland6e69d652019-07-10 14:17:55 -0700285 err = pingBinder();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800286 break;
Steven Morelandb8ad08d2019-08-09 14:42:56 -0700287 case EXTENSION_TRANSACTION:
Jiyong Park5970d0a2022-03-08 16:56:13 +0900288 CHECK(reply != nullptr);
Steven Morelandb8ad08d2019-08-09 14:42:56 -0700289 err = reply->writeStrongBinder(getExtension());
290 break;
Steven Moreland86080b82019-09-23 15:41:18 -0700291 case DEBUG_PID_TRANSACTION:
Jiyong Park5970d0a2022-03-08 16:56:13 +0900292 CHECK(reply != nullptr);
Steven Moreland86080b82019-09-23 15:41:18 -0700293 err = reply->writeInt32(getDebugPid());
294 break;
Yifan Hong84bedeb2021-04-21 21:37:17 -0700295 case SET_RPC_CLIENT_TRANSACTION: {
296 err = setRpcClientDebug(data);
297 break;
298 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800299 default:
300 err = onTransact(code, data, reply, flags);
301 break;
302 }
303
Steven Morelanda86a3562019-08-01 23:28:34 +0000304 // In case this is being transacted on in the same process.
Yi Kongfdd8da92018-06-07 17:52:27 -0700305 if (reply != nullptr) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800306 reply->setDataPosition(0);
Martijn Coenen1cad19c2021-10-04 09:19:01 +0200307 if (reply->dataSize() > LOG_REPLIES_OVER_SIZE) {
308 ALOGW("Large reply transaction of %zu bytes, interface descriptor %s, code %d",
309 reply->dataSize(), String8(getInterfaceDescriptor()).c_str(), code);
310 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800311 }
312
313 return err;
314}
315
Jiyong Parkb86c8662018-10-29 23:01:57 +0900316// NOLINTNEXTLINE(google-default-arguments)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800317status_t BBinder::linkToDeath(
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800318 const sp<DeathRecipient>& /*recipient*/, void* /*cookie*/,
319 uint32_t /*flags*/)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800320{
321 return INVALID_OPERATION;
322}
323
Jiyong Parkb86c8662018-10-29 23:01:57 +0900324// NOLINTNEXTLINE(google-default-arguments)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800325status_t BBinder::unlinkToDeath(
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800326 const wp<DeathRecipient>& /*recipient*/, void* /*cookie*/,
327 uint32_t /*flags*/, wp<DeathRecipient>* /*outRecipient*/)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800328{
329 return INVALID_OPERATION;
330}
331
Dianne Hackborn23eb1e22015-10-07 17:35:27 -0700332status_t BBinder::dump(int /*fd*/, const Vector<String16>& /*args*/)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800333{
334 return NO_ERROR;
335}
336
Steven Moreland63a2d512021-06-25 01:10:15 +0000337void* BBinder::attachObject(const void* objectID, void* object, void* cleanupCookie,
338 object_cleanup_func func) {
Steven Morelandf0212002018-12-26 13:59:23 -0800339 Extras* e = getOrCreateExtras();
Steven Moreland5ce7ca52021-06-25 21:59:50 +0000340 LOG_ALWAYS_FATAL_IF(!e, "no memory");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800341
342 AutoMutex _l(e->mLock);
Steven Moreland63a2d512021-06-25 01:10:15 +0000343 return e->mObjects.attach(objectID, object, cleanupCookie, func);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800344}
345
346void* BBinder::findObject(const void* objectID) const
347{
Bailey Forrest6913c462015-08-18 17:15:10 -0700348 Extras* e = mExtras.load(std::memory_order_acquire);
Yi Kongfdd8da92018-06-07 17:52:27 -0700349 if (!e) return nullptr;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800350
351 AutoMutex _l(e->mLock);
352 return e->mObjects.find(objectID);
353}
354
Steven Moreland63a2d512021-06-25 01:10:15 +0000355void* BBinder::detachObject(const void* objectID) {
Bailey Forrest6913c462015-08-18 17:15:10 -0700356 Extras* e = mExtras.load(std::memory_order_acquire);
Steven Moreland63a2d512021-06-25 01:10:15 +0000357 if (!e) return nullptr;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800358
359 AutoMutex _l(e->mLock);
Steven Moreland63a2d512021-06-25 01:10:15 +0000360 return e->mObjects.detach(objectID);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800361}
362
Steven Moreland9e759e82021-06-25 21:30:23 +0000363void BBinder::withLock(const std::function<void()>& doWithLock) {
364 Extras* e = getOrCreateExtras();
365 LOG_ALWAYS_FATAL_IF(!e, "no memory");
366
367 AutoMutex _l(e->mLock);
368 doWithLock();
369}
370
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800371BBinder* BBinder::localBinder()
372{
373 return this;
374}
375
Steven Morelandf0212002018-12-26 13:59:23 -0800376bool BBinder::isRequestingSid()
377{
378 Extras* e = mExtras.load(std::memory_order_acquire);
379
380 return e && e->mRequestingSid;
381}
382
383void BBinder::setRequestingSid(bool requestingSid)
384{
Steven Morelandd37e6dd2021-06-16 22:07:51 +0000385 LOG_ALWAYS_FATAL_IF(mParceled,
386 "setRequestingSid() should not be called after a binder object "
387 "is parceled/sent to another process");
Kalesh Singhd67c8e82020-12-29 15:46:25 -0500388
Steven Morelandf0212002018-12-26 13:59:23 -0800389 Extras* e = mExtras.load(std::memory_order_acquire);
390
391 if (!e) {
392 // default is false. Most things don't need sids, so avoiding allocations when possible.
393 if (!requestingSid) {
394 return;
395 }
396
397 e = getOrCreateExtras();
398 if (!e) return; // out of memory
399 }
400
Steven Moreland3668be62019-02-08 17:56:55 -0800401 e->mRequestingSid = requestingSid;
Steven Morelandf0212002018-12-26 13:59:23 -0800402}
403
Steven Morelandb8ad08d2019-08-09 14:42:56 -0700404sp<IBinder> BBinder::getExtension() {
405 Extras* e = mExtras.load(std::memory_order_acquire);
406 if (e == nullptr) return nullptr;
407 return e->mExtension;
408}
409
Steven Morelandbf1915b2020-07-16 22:43:02 +0000410void BBinder::setMinSchedulerPolicy(int policy, int priority) {
Steven Morelandd37e6dd2021-06-16 22:07:51 +0000411 LOG_ALWAYS_FATAL_IF(mParceled,
412 "setMinSchedulerPolicy() should not be called after a binder object "
413 "is parceled/sent to another process");
Kalesh Singhd67c8e82020-12-29 15:46:25 -0500414
Steven Morelandbf1915b2020-07-16 22:43:02 +0000415 switch (policy) {
416 case SCHED_NORMAL:
417 LOG_ALWAYS_FATAL_IF(priority < -20 || priority > 19, "Invalid priority for SCHED_NORMAL: %d", priority);
418 break;
419 case SCHED_RR:
420 case SCHED_FIFO:
421 LOG_ALWAYS_FATAL_IF(priority < 1 || priority > 99, "Invalid priority for sched %d: %d", policy, priority);
422 break;
423 default:
424 LOG_ALWAYS_FATAL("Unrecognized scheduling policy: %d", policy);
425 }
426
427 Extras* e = mExtras.load(std::memory_order_acquire);
428
429 if (e == nullptr) {
430 // Avoid allocations if called with default.
431 if (policy == SCHED_NORMAL && priority == 0) {
432 return;
433 }
434
435 e = getOrCreateExtras();
436 if (!e) return; // out of memory
437 }
438
439 e->mPolicy = policy;
440 e->mPriority = priority;
441}
442
443int BBinder::getMinSchedulerPolicy() {
444 Extras* e = mExtras.load(std::memory_order_acquire);
445 if (e == nullptr) return SCHED_NORMAL;
446 return e->mPolicy;
447}
448
449int BBinder::getMinSchedulerPriority() {
450 Extras* e = mExtras.load(std::memory_order_acquire);
451 if (e == nullptr) return 0;
452 return e->mPriority;
453}
454
Steven Morelandcf03cf12020-12-04 02:58:40 +0000455bool BBinder::isInheritRt() {
456 Extras* e = mExtras.load(std::memory_order_acquire);
457
458 return e && e->mInheritRt;
459}
460
461void BBinder::setInheritRt(bool inheritRt) {
Steven Morelandd37e6dd2021-06-16 22:07:51 +0000462 LOG_ALWAYS_FATAL_IF(mParceled,
463 "setInheritRt() should not be called after a binder object "
464 "is parceled/sent to another process");
Kalesh Singhd67c8e82020-12-29 15:46:25 -0500465
Steven Morelandcf03cf12020-12-04 02:58:40 +0000466 Extras* e = mExtras.load(std::memory_order_acquire);
467
468 if (!e) {
469 if (!inheritRt) {
470 return;
471 }
472
473 e = getOrCreateExtras();
474 if (!e) return; // out of memory
475 }
476
477 e->mInheritRt = inheritRt;
478}
479
Steven Moreland86080b82019-09-23 15:41:18 -0700480pid_t BBinder::getDebugPid() {
481 return getpid();
482}
483
Steven Morelandb8ad08d2019-08-09 14:42:56 -0700484void BBinder::setExtension(const sp<IBinder>& extension) {
Steven Morelandd37e6dd2021-06-16 22:07:51 +0000485 LOG_ALWAYS_FATAL_IF(mParceled,
486 "setExtension() should not be called after a binder object "
487 "is parceled/sent to another process");
Kalesh Singhd67c8e82020-12-29 15:46:25 -0500488
Steven Morelandb8ad08d2019-08-09 14:42:56 -0700489 Extras* e = getOrCreateExtras();
490 e->mExtension = extension;
491}
492
Kalesh Singhd67c8e82020-12-29 15:46:25 -0500493bool BBinder::wasParceled() {
494 return mParceled;
495}
496
497void BBinder::setParceled() {
498 mParceled = true;
499}
500
Yifan Hong84bedeb2021-04-21 21:37:17 -0700501status_t BBinder::setRpcClientDebug(const Parcel& data) {
Steven Morelandd9a74002022-06-02 00:03:18 +0000502 if (!kEnableRpcDevServers) {
Yifan Hong84bedeb2021-04-21 21:37:17 -0700503 ALOGW("%s: disallowed because RPC is not enabled", __PRETTY_FUNCTION__);
504 return INVALID_OPERATION;
505 }
506 uid_t uid = IPCThreadState::self()->getCallingUid();
507 if (uid != AID_ROOT) {
508 ALOGE("%s: not allowed because client %" PRIu32 " is not root", __PRETTY_FUNCTION__, uid);
509 return PERMISSION_DENIED;
510 }
511 status_t status;
512 bool hasSocketFd;
513 android::base::unique_fd clientFd;
Yifan Hong84bedeb2021-04-21 21:37:17 -0700514
515 if (status = data.readBool(&hasSocketFd); status != OK) return status;
516 if (hasSocketFd) {
517 if (status = data.readUniqueFileDescriptor(&clientFd); status != OK) return status;
518 }
Yifan Hong02530ec2021-06-10 13:38:38 -0700519 sp<IBinder> keepAliveBinder;
520 if (status = data.readNullableStrongBinder(&keepAliveBinder); status != OK) return status;
Yifan Hong84bedeb2021-04-21 21:37:17 -0700521
Yifan Hong02530ec2021-06-10 13:38:38 -0700522 return setRpcClientDebug(std::move(clientFd), keepAliveBinder);
Yifan Hong84bedeb2021-04-21 21:37:17 -0700523}
524
Yifan Hong02530ec2021-06-10 13:38:38 -0700525status_t BBinder::setRpcClientDebug(android::base::unique_fd socketFd,
526 const sp<IBinder>& keepAliveBinder) {
Steven Morelandd9a74002022-06-02 00:03:18 +0000527 if (!kEnableRpcDevServers) {
Yifan Hong84bedeb2021-04-21 21:37:17 -0700528 ALOGW("%s: disallowed because RPC is not enabled", __PRETTY_FUNCTION__);
529 return INVALID_OPERATION;
530 }
531
532 const int socketFdForPrint = socketFd.get();
Yifan Hong34823232021-06-07 17:23:00 -0700533 LOG_RPC_DETAIL("%s(fd=%d)", __PRETTY_FUNCTION__, socketFdForPrint);
Yifan Hong84bedeb2021-04-21 21:37:17 -0700534
535 if (!socketFd.ok()) {
536 ALOGE("%s: No socket FD provided.", __PRETTY_FUNCTION__);
537 return BAD_VALUE;
538 }
Yifan Hong84bedeb2021-04-21 21:37:17 -0700539
Yifan Hong02530ec2021-06-10 13:38:38 -0700540 if (keepAliveBinder == nullptr) {
541 ALOGE("%s: No keepAliveBinder provided.", __PRETTY_FUNCTION__);
542 return UNEXPECTED_NULL;
543 }
544
Elie Kheirallah47431c12022-04-21 23:46:17 +0000545 size_t binderThreadPoolMaxCount = ProcessState::self()->getThreadPoolMaxTotalThreadCount();
Yifan Hong84bedeb2021-04-21 21:37:17 -0700546 if (binderThreadPoolMaxCount <= 1) {
547 ALOGE("%s: ProcessState thread pool max count is %zu. RPC is disabled for this service "
548 "because RPC requires the service to support multithreading.",
549 __PRETTY_FUNCTION__, binderThreadPoolMaxCount);
550 return INVALID_OPERATION;
551 }
552
Yifan Hong8b890852021-06-10 13:44:09 -0700553 // Weak ref to avoid circular dependency:
554 // BBinder -> RpcServerLink ----> RpcServer -X-> BBinder
555 // `-X-> BBinder
556 auto weakThis = wp<BBinder>::fromExisting(this);
557
Yifan Hong84bedeb2021-04-21 21:37:17 -0700558 Extras* e = getOrCreateExtras();
559 AutoMutex _l(e->mLock);
Yifan Hong8b890852021-06-10 13:44:09 -0700560 auto rpcServer = RpcServer::make();
561 LOG_ALWAYS_FATAL_IF(rpcServer == nullptr, "RpcServer::make returns null");
Yifan Hong8b890852021-06-10 13:44:09 -0700562 auto link = sp<RpcServerLink>::make(rpcServer, keepAliveBinder, weakThis);
563 if (auto status = keepAliveBinder->linkToDeath(link, nullptr, 0); status != OK) {
564 ALOGE("%s: keepAliveBinder->linkToDeath returns %s", __PRETTY_FUNCTION__,
565 statusToString(status).c_str());
566 return status;
Yifan Hong84bedeb2021-04-21 21:37:17 -0700567 }
Yifan Hong8b890852021-06-10 13:44:09 -0700568 rpcServer->setRootObjectWeak(weakThis);
Steven Moreland2372f9d2021-08-05 15:42:01 -0700569 if (auto status = rpcServer->setupExternalServer(std::move(socketFd)); status != OK) {
570 return status;
571 }
Yifan Hong8b890852021-06-10 13:44:09 -0700572 rpcServer->setMaxThreads(binderThreadPoolMaxCount);
573 rpcServer->start();
574 e->mRpcServerLinks.emplace(link);
Yifan Hong34823232021-06-07 17:23:00 -0700575 LOG_RPC_DETAIL("%s(fd=%d) successful", __PRETTY_FUNCTION__, socketFdForPrint);
Yifan Hong84bedeb2021-04-21 21:37:17 -0700576 return OK;
577}
578
Yifan Hong8b890852021-06-10 13:44:09 -0700579void BBinder::removeRpcServerLink(const sp<RpcServerLink>& link) {
580 Extras* e = mExtras.load(std::memory_order_acquire);
581 if (!e) return;
582 AutoMutex _l(e->mLock);
583 (void)e->mRpcServerLinks.erase(link);
584}
585
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800586BBinder::~BBinder()
587{
Steven Moreland55a12542022-03-31 21:53:11 +0000588 if (!wasParceled() && getExtension()) {
589 ALOGW("Binder %p destroyed with extension attached before being parceled.", this);
590 }
591
Bailey Forrest6913c462015-08-18 17:15:10 -0700592 Extras* e = mExtras.load(std::memory_order_relaxed);
Hans Boehm3effaba2014-08-12 22:56:00 +0000593 if (e) delete e;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800594}
595
596
Jiyong Parkb86c8662018-10-29 23:01:57 +0900597// NOLINTNEXTLINE(google-default-arguments)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800598status_t BBinder::onTransact(
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800599 uint32_t code, const Parcel& data, Parcel* reply, uint32_t /*flags*/)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800600{
601 switch (code) {
602 case INTERFACE_TRANSACTION:
Jiyong Park5970d0a2022-03-08 16:56:13 +0900603 CHECK(reply != nullptr);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800604 reply->writeString16(getInterfaceDescriptor());
605 return NO_ERROR;
606
607 case DUMP_TRANSACTION: {
608 int fd = data.readFileDescriptor();
609 int argc = data.readInt32();
610 Vector<String16> args;
611 for (int i = 0; i < argc && data.dataAvail() > 0; i++) {
612 args.add(data.readString16());
613 }
614 return dump(fd, args);
615 }
Dianne Hackborn555f89d2012-05-08 18:54:22 -0700616
Dianne Hackborn23eb1e22015-10-07 17:35:27 -0700617 case SHELL_COMMAND_TRANSACTION: {
618 int in = data.readFileDescriptor();
619 int out = data.readFileDescriptor();
620 int err = 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 }
Dianne Hackborn1941a402016-08-29 12:30:43 -0700626 sp<IShellCallback> shellCallback = IShellCallback::asInterface(
627 data.readStrongBinder());
Dianne Hackborn23eb1e22015-10-07 17:35:27 -0700628 sp<IResultReceiver> resultReceiver = IResultReceiver::asInterface(
629 data.readStrongBinder());
630
Dianne Hackbornf2bf93b2015-10-14 15:13:02 -0700631 // XXX can't add virtuals until binaries are updated.
632 //return shellCommand(in, out, err, args, resultReceiver);
Christopher Wiley0a9a1c12016-07-20 08:28:14 -0700633 (void)in;
634 (void)out;
635 (void)err;
636
Yi Kongfdd8da92018-06-07 17:52:27 -0700637 if (resultReceiver != nullptr) {
Dianne Hackbornf2bf93b2015-10-14 15:13:02 -0700638 resultReceiver->send(INVALID_OPERATION);
639 }
Martijn Coenenaa6ee992017-08-17 15:38:08 +0200640
641 return NO_ERROR;
Dianne Hackborn23eb1e22015-10-07 17:35:27 -0700642 }
643
Dianne Hackborn555f89d2012-05-08 18:54:22 -0700644 case SYSPROPS_TRANSACTION: {
645 report_sysprop_change();
646 return NO_ERROR;
647 }
648
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800649 default:
650 return UNKNOWN_TRANSACTION;
651 }
652}
653
Steven Morelandf0212002018-12-26 13:59:23 -0800654BBinder::Extras* BBinder::getOrCreateExtras()
655{
656 Extras* e = mExtras.load(std::memory_order_acquire);
657
658 if (!e) {
659 e = new Extras;
660 Extras* expected = nullptr;
661 if (!mExtras.compare_exchange_strong(expected, e,
662 std::memory_order_release,
663 std::memory_order_acquire)) {
664 delete e;
665 e = expected; // Filled in by CAS
666 }
667 if (e == nullptr) return nullptr; // out of memory
668 }
669
670 return e;
671}
672
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800673// ---------------------------------------------------------------------------
674
675enum {
676 // This is used to transfer ownership of the remote binder from
677 // the BpRefBase object holding it (when it is constructed), to the
678 // owner of the BpRefBase object when it first acquires that BpRefBase.
679 kRemoteAcquired = 0x00000001
680};
681
682BpRefBase::BpRefBase(const sp<IBinder>& o)
Yi Kongfdd8da92018-06-07 17:52:27 -0700683 : mRemote(o.get()), mRefs(nullptr), mState(0)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800684{
685 extendObjectLifetime(OBJECT_LIFETIME_WEAK);
686
687 if (mRemote) {
688 mRemote->incStrong(this); // Removed on first IncStrong().
689 mRefs = mRemote->createWeak(this); // Held for our entire lifetime.
690 }
691}
692
693BpRefBase::~BpRefBase()
694{
695 if (mRemote) {
Bailey Forrest6913c462015-08-18 17:15:10 -0700696 if (!(mState.load(std::memory_order_relaxed)&kRemoteAcquired)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800697 mRemote->decStrong(this);
698 }
699 mRefs->decWeak(this);
700 }
701}
702
703void BpRefBase::onFirstRef()
704{
Bailey Forrest6913c462015-08-18 17:15:10 -0700705 mState.fetch_or(kRemoteAcquired, std::memory_order_relaxed);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800706}
707
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800708void BpRefBase::onLastStrongRef(const void* /*id*/)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800709{
710 if (mRemote) {
711 mRemote->decStrong(this);
712 }
713}
714
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800715bool BpRefBase::onIncStrongAttempted(uint32_t /*flags*/, const void* /*id*/)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800716{
717 return mRemote ? mRefs->attemptIncStrong(this) : false;
718}
719
720// ---------------------------------------------------------------------------
721
Steven Moreland61ff8492019-09-26 16:05:45 -0700722} // namespace android