blob: b66e89ebed612d31445ef9d3558a74151a129f94 [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
Yifan Hong84bedeb2021-04-21 21:37:17 -070052#ifdef BINDER_RPC_DEV_SERVERS
53constexpr const bool kEnableRpcDevServers = true;
54#else
55constexpr const bool kEnableRpcDevServers = false;
56#endif
57
Martijn Coenen1cad19c2021-10-04 09:19:01 +020058// Log any reply transactions for which the data exceeds this size
59#define LOG_REPLIES_OVER_SIZE (300 * 1024)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080060// ---------------------------------------------------------------------------
61
Mathias Agopian83c04462009-05-22 19:00:22 -070062IBinder::IBinder()
63 : RefBase()
64{
65}
66
67IBinder::~IBinder()
68{
69}
70
71// ---------------------------------------------------------------------------
72
Colin Cross6f4f3ab2014-02-05 17:42:44 -080073sp<IInterface> IBinder::queryLocalInterface(const String16& /*descriptor*/)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080074{
Yi Kongfdd8da92018-06-07 17:52:27 -070075 return nullptr;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080076}
77
78BBinder* IBinder::localBinder()
79{
Yi Kongfdd8da92018-06-07 17:52:27 -070080 return nullptr;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080081}
82
83BpBinder* IBinder::remoteBinder()
84{
Yi Kongfdd8da92018-06-07 17:52:27 -070085 return nullptr;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080086}
87
88bool IBinder::checkSubclass(const void* /*subclassID*/) const
89{
90 return false;
91}
92
Dianne Hackborn23eb1e22015-10-07 17:35:27 -070093
Dianne Hackbornf2bf93b2015-10-14 15:13:02 -070094status_t IBinder::shellCommand(const sp<IBinder>& target, int in, int out, int err,
Dianne Hackborn1941a402016-08-29 12:30:43 -070095 Vector<String16>& args, const sp<IShellCallback>& callback,
96 const sp<IResultReceiver>& resultReceiver)
Dianne Hackborn23eb1e22015-10-07 17:35:27 -070097{
Dianne Hackbornf2bf93b2015-10-14 15:13:02 -070098 Parcel send;
99 Parcel reply;
100 send.writeFileDescriptor(in);
101 send.writeFileDescriptor(out);
102 send.writeFileDescriptor(err);
103 const size_t numArgs = args.size();
104 send.writeInt32(numArgs);
105 for (size_t i = 0; i < numArgs; i++) {
106 send.writeString16(args[i]);
Dianne Hackborn23eb1e22015-10-07 17:35:27 -0700107 }
Yi Kongfdd8da92018-06-07 17:52:27 -0700108 send.writeStrongBinder(callback != nullptr ? IInterface::asBinder(callback) : nullptr);
109 send.writeStrongBinder(resultReceiver != nullptr ? IInterface::asBinder(resultReceiver) : nullptr);
Dianne Hackbornf2bf93b2015-10-14 15:13:02 -0700110 return target->transact(SHELL_COMMAND_TRANSACTION, send, &reply);
Dianne Hackborn23eb1e22015-10-07 17:35:27 -0700111}
112
Steven Morelandb8ad08d2019-08-09 14:42:56 -0700113status_t IBinder::getExtension(sp<IBinder>* out) {
114 BBinder* local = this->localBinder();
115 if (local != nullptr) {
116 *out = local->getExtension();
117 return OK;
118 }
119
120 BpBinder* proxy = this->remoteBinder();
121 LOG_ALWAYS_FATAL_IF(proxy == nullptr);
122
123 Parcel data;
124 Parcel reply;
125 status_t status = transact(EXTENSION_TRANSACTION, data, &reply);
126 if (status != OK) return status;
127
128 return reply.readNullableStrongBinder(out);
129}
130
Steven Moreland86080b82019-09-23 15:41:18 -0700131status_t IBinder::getDebugPid(pid_t* out) {
132 BBinder* local = this->localBinder();
133 if (local != nullptr) {
134 *out = local->getDebugPid();
135 return OK;
136 }
137
138 BpBinder* proxy = this->remoteBinder();
139 LOG_ALWAYS_FATAL_IF(proxy == nullptr);
140
141 Parcel data;
142 Parcel reply;
143 status_t status = transact(DEBUG_PID_TRANSACTION, data, &reply);
144 if (status != OK) return status;
145
146 int32_t pid;
147 status = reply.readInt32(&pid);
148 if (status != OK) return status;
149
150 if (pid < 0 || pid > std::numeric_limits<pid_t>::max()) {
151 return BAD_VALUE;
152 }
153 *out = pid;
154 return OK;
155}
156
Yifan Hong02530ec2021-06-10 13:38:38 -0700157status_t IBinder::setRpcClientDebug(android::base::unique_fd socketFd,
158 const sp<IBinder>& keepAliveBinder) {
Yifan Hong84bedeb2021-04-21 21:37:17 -0700159 if constexpr (!kEnableRpcDevServers) {
160 ALOGW("setRpcClientDebug disallowed because RPC is not enabled");
161 return INVALID_OPERATION;
162 }
163
164 BBinder* local = this->localBinder();
165 if (local != nullptr) {
Yifan Hong02530ec2021-06-10 13:38:38 -0700166 return local->BBinder::setRpcClientDebug(std::move(socketFd), keepAliveBinder);
Yifan Hong84bedeb2021-04-21 21:37:17 -0700167 }
168
169 BpBinder* proxy = this->remoteBinder();
170 LOG_ALWAYS_FATAL_IF(proxy == nullptr, "binder object must be either local or remote");
171
172 Parcel data;
173 Parcel reply;
174 status_t status;
175 if (status = data.writeBool(socketFd.ok()); status != OK) return status;
176 if (socketFd.ok()) {
177 // writeUniqueFileDescriptor currently makes an unnecessary dup().
178 status = data.writeFileDescriptor(socketFd.release(), true /* own */);
179 if (status != OK) return status;
180 }
Yifan Hong02530ec2021-06-10 13:38:38 -0700181 if (status = data.writeStrongBinder(keepAliveBinder); status != OK) return status;
Yifan Hong84bedeb2021-04-21 21:37:17 -0700182 return transact(SET_RPC_CLIENT_TRANSACTION, data, &reply);
183}
184
Steven Moreland9e759e82021-06-25 21:30:23 +0000185void IBinder::withLock(const std::function<void()>& doWithLock) {
186 BBinder* local = localBinder();
187 if (local) {
188 local->withLock(doWithLock);
189 return;
190 }
191 BpBinder* proxy = this->remoteBinder();
192 LOG_ALWAYS_FATAL_IF(proxy == nullptr, "binder object must be either local or remote");
193 proxy->withLock(doWithLock);
194}
195
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800196// ---------------------------------------------------------------------------
197
Yifan Hong8b890852021-06-10 13:44:09 -0700198class BBinder::RpcServerLink : public IBinder::DeathRecipient {
199public:
200 // On binder died, calls RpcServer::shutdown on @a rpcServer, and removes itself from @a binder.
201 RpcServerLink(const sp<RpcServer>& rpcServer, const sp<IBinder>& keepAliveBinder,
202 const wp<BBinder>& binder)
203 : mRpcServer(rpcServer), mKeepAliveBinder(keepAliveBinder), mBinder(binder) {}
204 void binderDied(const wp<IBinder>&) override {
205 LOG_RPC_DETAIL("RpcServerLink: binder died, shutting down RpcServer");
206 if (mRpcServer == nullptr) {
207 ALOGW("RpcServerLink: Unable to shut down RpcServer because it does not exist.");
208 } else {
209 ALOGW_IF(!mRpcServer->shutdown(),
210 "RpcServerLink: RpcServer did not shut down properly. Not started?");
211 }
212 mRpcServer.clear();
213
214 auto promoted = mBinder.promote();
215 if (promoted == nullptr) {
216 ALOGW("RpcServerLink: Unable to remove link from parent binder object because parent "
217 "binder object is gone.");
218 } else {
219 promoted->removeRpcServerLink(sp<RpcServerLink>::fromExisting(this));
220 }
221 mBinder.clear();
222 }
223
224private:
225 sp<RpcServer> mRpcServer;
226 sp<IBinder> mKeepAliveBinder; // hold to avoid automatically unlinking
227 wp<BBinder> mBinder;
228};
229
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800230class BBinder::Extras
231{
232public:
Steven Morelandf0212002018-12-26 13:59:23 -0800233 // unlocked objects
234 bool mRequestingSid = false;
Steven Morelandcf03cf12020-12-04 02:58:40 +0000235 bool mInheritRt = false;
Steven Morelandb8ad08d2019-08-09 14:42:56 -0700236 sp<IBinder> mExtension;
Steven Morelandbf1915b2020-07-16 22:43:02 +0000237 int mPolicy = SCHED_NORMAL;
238 int mPriority = 0;
Steven Morelandf0212002018-12-26 13:59:23 -0800239
240 // for below objects
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800241 Mutex mLock;
Yifan Hong8b890852021-06-10 13:44:09 -0700242 std::set<sp<RpcServerLink>> mRpcServerLinks;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800243 BpBinder::ObjectManager mObjects;
244};
245
246// ---------------------------------------------------------------------------
247
Kalesh Singhd67c8e82020-12-29 15:46:25 -0500248BBinder::BBinder() : mExtras(nullptr), mStability(0), mParceled(false) {}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800249
250bool BBinder::isBinderAlive() const
251{
252 return true;
253}
254
255status_t BBinder::pingBinder()
256{
257 return NO_ERROR;
258}
259
Mathias Agopian83c04462009-05-22 19:00:22 -0700260const String16& BBinder::getInterfaceDescriptor() const
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800261{
Dan Egnor386a3322010-05-06 00:55:09 -0700262 // This is a local static rather than a global static,
263 // to avoid static initializer ordering issues.
264 static String16 sEmptyDescriptor;
Steve Block32397c12012-01-05 23:22:43 +0000265 ALOGW("reached BBinder::getInterfaceDescriptor (this=%p)", this);
Mathias Agopian83c04462009-05-22 19:00:22 -0700266 return sEmptyDescriptor;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800267}
268
Jiyong Parkb86c8662018-10-29 23:01:57 +0900269// NOLINTNEXTLINE(google-default-arguments)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800270status_t BBinder::transact(
271 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
272{
273 data.setDataPosition(0);
274
Steven Morelandf183fdd2020-10-27 00:12:12 +0000275 if (reply != nullptr && (flags & FLAG_CLEAR_BUF)) {
276 reply->markSensitive();
277 }
278
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800279 status_t err = NO_ERROR;
280 switch (code) {
281 case PING_TRANSACTION:
Steven Moreland6e69d652019-07-10 14:17:55 -0700282 err = pingBinder();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800283 break;
Steven Morelandb8ad08d2019-08-09 14:42:56 -0700284 case EXTENSION_TRANSACTION:
Jiyong Park5970d0a2022-03-08 16:56:13 +0900285 CHECK(reply != nullptr);
Steven Morelandb8ad08d2019-08-09 14:42:56 -0700286 err = reply->writeStrongBinder(getExtension());
287 break;
Steven Moreland86080b82019-09-23 15:41:18 -0700288 case DEBUG_PID_TRANSACTION:
Jiyong Park5970d0a2022-03-08 16:56:13 +0900289 CHECK(reply != nullptr);
Steven Moreland86080b82019-09-23 15:41:18 -0700290 err = reply->writeInt32(getDebugPid());
291 break;
Yifan Hong84bedeb2021-04-21 21:37:17 -0700292 case SET_RPC_CLIENT_TRANSACTION: {
293 err = setRpcClientDebug(data);
294 break;
295 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800296 default:
297 err = onTransact(code, data, reply, flags);
298 break;
299 }
300
Steven Morelanda86a3562019-08-01 23:28:34 +0000301 // In case this is being transacted on in the same process.
Yi Kongfdd8da92018-06-07 17:52:27 -0700302 if (reply != nullptr) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800303 reply->setDataPosition(0);
Martijn Coenen1cad19c2021-10-04 09:19:01 +0200304 if (reply->dataSize() > LOG_REPLIES_OVER_SIZE) {
305 ALOGW("Large reply transaction of %zu bytes, interface descriptor %s, code %d",
306 reply->dataSize(), String8(getInterfaceDescriptor()).c_str(), code);
307 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800308 }
309
310 return err;
311}
312
Jiyong Parkb86c8662018-10-29 23:01:57 +0900313// NOLINTNEXTLINE(google-default-arguments)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800314status_t BBinder::linkToDeath(
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800315 const sp<DeathRecipient>& /*recipient*/, void* /*cookie*/,
316 uint32_t /*flags*/)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800317{
318 return INVALID_OPERATION;
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::unlinkToDeath(
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800323 const wp<DeathRecipient>& /*recipient*/, void* /*cookie*/,
324 uint32_t /*flags*/, wp<DeathRecipient>* /*outRecipient*/)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800325{
326 return INVALID_OPERATION;
327}
328
Dianne Hackborn23eb1e22015-10-07 17:35:27 -0700329status_t BBinder::dump(int /*fd*/, const Vector<String16>& /*args*/)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800330{
331 return NO_ERROR;
332}
333
Steven Moreland63a2d512021-06-25 01:10:15 +0000334void* BBinder::attachObject(const void* objectID, void* object, void* cleanupCookie,
335 object_cleanup_func func) {
Steven Morelandf0212002018-12-26 13:59:23 -0800336 Extras* e = getOrCreateExtras();
Steven Moreland5ce7ca52021-06-25 21:59:50 +0000337 LOG_ALWAYS_FATAL_IF(!e, "no memory");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800338
339 AutoMutex _l(e->mLock);
Steven Moreland63a2d512021-06-25 01:10:15 +0000340 return e->mObjects.attach(objectID, object, cleanupCookie, func);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800341}
342
343void* BBinder::findObject(const void* objectID) const
344{
Bailey Forrest6913c462015-08-18 17:15:10 -0700345 Extras* e = mExtras.load(std::memory_order_acquire);
Yi Kongfdd8da92018-06-07 17:52:27 -0700346 if (!e) return nullptr;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800347
348 AutoMutex _l(e->mLock);
349 return e->mObjects.find(objectID);
350}
351
Steven Moreland63a2d512021-06-25 01:10:15 +0000352void* BBinder::detachObject(const void* objectID) {
Bailey Forrest6913c462015-08-18 17:15:10 -0700353 Extras* e = mExtras.load(std::memory_order_acquire);
Steven Moreland63a2d512021-06-25 01:10:15 +0000354 if (!e) return nullptr;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800355
356 AutoMutex _l(e->mLock);
Steven Moreland63a2d512021-06-25 01:10:15 +0000357 return e->mObjects.detach(objectID);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800358}
359
Steven Moreland9e759e82021-06-25 21:30:23 +0000360void BBinder::withLock(const std::function<void()>& doWithLock) {
361 Extras* e = getOrCreateExtras();
362 LOG_ALWAYS_FATAL_IF(!e, "no memory");
363
364 AutoMutex _l(e->mLock);
365 doWithLock();
366}
367
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800368BBinder* BBinder::localBinder()
369{
370 return this;
371}
372
Steven Morelandf0212002018-12-26 13:59:23 -0800373bool BBinder::isRequestingSid()
374{
375 Extras* e = mExtras.load(std::memory_order_acquire);
376
377 return e && e->mRequestingSid;
378}
379
380void BBinder::setRequestingSid(bool requestingSid)
381{
Steven Morelandd37e6dd2021-06-16 22:07:51 +0000382 LOG_ALWAYS_FATAL_IF(mParceled,
383 "setRequestingSid() should not be called after a binder object "
384 "is parceled/sent to another process");
Kalesh Singhd67c8e82020-12-29 15:46:25 -0500385
Steven Morelandf0212002018-12-26 13:59:23 -0800386 Extras* e = mExtras.load(std::memory_order_acquire);
387
388 if (!e) {
389 // default is false. Most things don't need sids, so avoiding allocations when possible.
390 if (!requestingSid) {
391 return;
392 }
393
394 e = getOrCreateExtras();
395 if (!e) return; // out of memory
396 }
397
Steven Moreland3668be62019-02-08 17:56:55 -0800398 e->mRequestingSid = requestingSid;
Steven Morelandf0212002018-12-26 13:59:23 -0800399}
400
Steven Morelandb8ad08d2019-08-09 14:42:56 -0700401sp<IBinder> BBinder::getExtension() {
402 Extras* e = mExtras.load(std::memory_order_acquire);
403 if (e == nullptr) return nullptr;
404 return e->mExtension;
405}
406
Steven Morelandbf1915b2020-07-16 22:43:02 +0000407void BBinder::setMinSchedulerPolicy(int policy, int priority) {
Steven Morelandd37e6dd2021-06-16 22:07:51 +0000408 LOG_ALWAYS_FATAL_IF(mParceled,
409 "setMinSchedulerPolicy() should not be called after a binder object "
410 "is parceled/sent to another process");
Kalesh Singhd67c8e82020-12-29 15:46:25 -0500411
Steven Morelandbf1915b2020-07-16 22:43:02 +0000412 switch (policy) {
413 case SCHED_NORMAL:
414 LOG_ALWAYS_FATAL_IF(priority < -20 || priority > 19, "Invalid priority for SCHED_NORMAL: %d", priority);
415 break;
416 case SCHED_RR:
417 case SCHED_FIFO:
418 LOG_ALWAYS_FATAL_IF(priority < 1 || priority > 99, "Invalid priority for sched %d: %d", policy, priority);
419 break;
420 default:
421 LOG_ALWAYS_FATAL("Unrecognized scheduling policy: %d", policy);
422 }
423
424 Extras* e = mExtras.load(std::memory_order_acquire);
425
426 if (e == nullptr) {
427 // Avoid allocations if called with default.
428 if (policy == SCHED_NORMAL && priority == 0) {
429 return;
430 }
431
432 e = getOrCreateExtras();
433 if (!e) return; // out of memory
434 }
435
436 e->mPolicy = policy;
437 e->mPriority = priority;
438}
439
440int BBinder::getMinSchedulerPolicy() {
441 Extras* e = mExtras.load(std::memory_order_acquire);
442 if (e == nullptr) return SCHED_NORMAL;
443 return e->mPolicy;
444}
445
446int BBinder::getMinSchedulerPriority() {
447 Extras* e = mExtras.load(std::memory_order_acquire);
448 if (e == nullptr) return 0;
449 return e->mPriority;
450}
451
Steven Morelandcf03cf12020-12-04 02:58:40 +0000452bool BBinder::isInheritRt() {
453 Extras* e = mExtras.load(std::memory_order_acquire);
454
455 return e && e->mInheritRt;
456}
457
458void BBinder::setInheritRt(bool inheritRt) {
Steven Morelandd37e6dd2021-06-16 22:07:51 +0000459 LOG_ALWAYS_FATAL_IF(mParceled,
460 "setInheritRt() should not be called after a binder object "
461 "is parceled/sent to another process");
Kalesh Singhd67c8e82020-12-29 15:46:25 -0500462
Steven Morelandcf03cf12020-12-04 02:58:40 +0000463 Extras* e = mExtras.load(std::memory_order_acquire);
464
465 if (!e) {
466 if (!inheritRt) {
467 return;
468 }
469
470 e = getOrCreateExtras();
471 if (!e) return; // out of memory
472 }
473
474 e->mInheritRt = inheritRt;
475}
476
Steven Moreland86080b82019-09-23 15:41:18 -0700477pid_t BBinder::getDebugPid() {
478 return getpid();
479}
480
Steven Morelandb8ad08d2019-08-09 14:42:56 -0700481void BBinder::setExtension(const sp<IBinder>& extension) {
Steven Morelandd37e6dd2021-06-16 22:07:51 +0000482 LOG_ALWAYS_FATAL_IF(mParceled,
483 "setExtension() should not be called after a binder object "
484 "is parceled/sent to another process");
Kalesh Singhd67c8e82020-12-29 15:46:25 -0500485
Steven Morelandb8ad08d2019-08-09 14:42:56 -0700486 Extras* e = getOrCreateExtras();
487 e->mExtension = extension;
488}
489
Kalesh Singhd67c8e82020-12-29 15:46:25 -0500490bool BBinder::wasParceled() {
491 return mParceled;
492}
493
494void BBinder::setParceled() {
495 mParceled = true;
496}
497
Yifan Hong84bedeb2021-04-21 21:37:17 -0700498status_t BBinder::setRpcClientDebug(const Parcel& data) {
499 if constexpr (!kEnableRpcDevServers) {
500 ALOGW("%s: disallowed because RPC is not enabled", __PRETTY_FUNCTION__);
501 return INVALID_OPERATION;
502 }
503 uid_t uid = IPCThreadState::self()->getCallingUid();
504 if (uid != AID_ROOT) {
505 ALOGE("%s: not allowed because client %" PRIu32 " is not root", __PRETTY_FUNCTION__, uid);
506 return PERMISSION_DENIED;
507 }
508 status_t status;
509 bool hasSocketFd;
510 android::base::unique_fd clientFd;
Yifan Hong84bedeb2021-04-21 21:37:17 -0700511
512 if (status = data.readBool(&hasSocketFd); status != OK) return status;
513 if (hasSocketFd) {
514 if (status = data.readUniqueFileDescriptor(&clientFd); status != OK) return status;
515 }
Yifan Hong02530ec2021-06-10 13:38:38 -0700516 sp<IBinder> keepAliveBinder;
517 if (status = data.readNullableStrongBinder(&keepAliveBinder); status != OK) return status;
Yifan Hong84bedeb2021-04-21 21:37:17 -0700518
Yifan Hong02530ec2021-06-10 13:38:38 -0700519 return setRpcClientDebug(std::move(clientFd), keepAliveBinder);
Yifan Hong84bedeb2021-04-21 21:37:17 -0700520}
521
Yifan Hong02530ec2021-06-10 13:38:38 -0700522status_t BBinder::setRpcClientDebug(android::base::unique_fd socketFd,
523 const sp<IBinder>& keepAliveBinder) {
Yifan Hong84bedeb2021-04-21 21:37:17 -0700524 if constexpr (!kEnableRpcDevServers) {
525 ALOGW("%s: disallowed because RPC is not enabled", __PRETTY_FUNCTION__);
526 return INVALID_OPERATION;
527 }
528
529 const int socketFdForPrint = socketFd.get();
Yifan Hong34823232021-06-07 17:23:00 -0700530 LOG_RPC_DETAIL("%s(fd=%d)", __PRETTY_FUNCTION__, socketFdForPrint);
Yifan Hong84bedeb2021-04-21 21:37:17 -0700531
532 if (!socketFd.ok()) {
533 ALOGE("%s: No socket FD provided.", __PRETTY_FUNCTION__);
534 return BAD_VALUE;
535 }
Yifan Hong84bedeb2021-04-21 21:37:17 -0700536
Yifan Hong02530ec2021-06-10 13:38:38 -0700537 if (keepAliveBinder == nullptr) {
538 ALOGE("%s: No keepAliveBinder provided.", __PRETTY_FUNCTION__);
539 return UNEXPECTED_NULL;
540 }
541
Elie Kheirallah47431c12022-04-21 23:46:17 +0000542 size_t binderThreadPoolMaxCount = ProcessState::self()->getThreadPoolMaxTotalThreadCount();
Yifan Hong84bedeb2021-04-21 21:37:17 -0700543 if (binderThreadPoolMaxCount <= 1) {
544 ALOGE("%s: ProcessState thread pool max count is %zu. RPC is disabled for this service "
545 "because RPC requires the service to support multithreading.",
546 __PRETTY_FUNCTION__, binderThreadPoolMaxCount);
547 return INVALID_OPERATION;
548 }
549
Yifan Hong8b890852021-06-10 13:44:09 -0700550 // Weak ref to avoid circular dependency:
551 // BBinder -> RpcServerLink ----> RpcServer -X-> BBinder
552 // `-X-> BBinder
553 auto weakThis = wp<BBinder>::fromExisting(this);
554
Yifan Hong84bedeb2021-04-21 21:37:17 -0700555 Extras* e = getOrCreateExtras();
556 AutoMutex _l(e->mLock);
Yifan Hong8b890852021-06-10 13:44:09 -0700557 auto rpcServer = RpcServer::make();
558 LOG_ALWAYS_FATAL_IF(rpcServer == nullptr, "RpcServer::make returns null");
Yifan Hong8b890852021-06-10 13:44:09 -0700559 auto link = sp<RpcServerLink>::make(rpcServer, keepAliveBinder, weakThis);
560 if (auto status = keepAliveBinder->linkToDeath(link, nullptr, 0); status != OK) {
561 ALOGE("%s: keepAliveBinder->linkToDeath returns %s", __PRETTY_FUNCTION__,
562 statusToString(status).c_str());
563 return status;
Yifan Hong84bedeb2021-04-21 21:37:17 -0700564 }
Yifan Hong8b890852021-06-10 13:44:09 -0700565 rpcServer->setRootObjectWeak(weakThis);
Steven Moreland2372f9d2021-08-05 15:42:01 -0700566 if (auto status = rpcServer->setupExternalServer(std::move(socketFd)); status != OK) {
567 return status;
568 }
Yifan Hong8b890852021-06-10 13:44:09 -0700569 rpcServer->setMaxThreads(binderThreadPoolMaxCount);
570 rpcServer->start();
571 e->mRpcServerLinks.emplace(link);
Yifan Hong34823232021-06-07 17:23:00 -0700572 LOG_RPC_DETAIL("%s(fd=%d) successful", __PRETTY_FUNCTION__, socketFdForPrint);
Yifan Hong84bedeb2021-04-21 21:37:17 -0700573 return OK;
574}
575
Yifan Hong8b890852021-06-10 13:44:09 -0700576void BBinder::removeRpcServerLink(const sp<RpcServerLink>& link) {
577 Extras* e = mExtras.load(std::memory_order_acquire);
578 if (!e) return;
579 AutoMutex _l(e->mLock);
580 (void)e->mRpcServerLinks.erase(link);
581}
582
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800583BBinder::~BBinder()
584{
Steven Moreland55a12542022-03-31 21:53:11 +0000585 if (!wasParceled() && getExtension()) {
586 ALOGW("Binder %p destroyed with extension attached before being parceled.", this);
587 }
588
Bailey Forrest6913c462015-08-18 17:15:10 -0700589 Extras* e = mExtras.load(std::memory_order_relaxed);
Hans Boehm3effaba2014-08-12 22:56:00 +0000590 if (e) delete e;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800591}
592
593
Jiyong Parkb86c8662018-10-29 23:01:57 +0900594// NOLINTNEXTLINE(google-default-arguments)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800595status_t BBinder::onTransact(
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800596 uint32_t code, const Parcel& data, Parcel* reply, uint32_t /*flags*/)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800597{
598 switch (code) {
599 case INTERFACE_TRANSACTION:
Jiyong Park5970d0a2022-03-08 16:56:13 +0900600 CHECK(reply != nullptr);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800601 reply->writeString16(getInterfaceDescriptor());
602 return NO_ERROR;
603
604 case DUMP_TRANSACTION: {
605 int fd = data.readFileDescriptor();
606 int argc = data.readInt32();
607 Vector<String16> args;
608 for (int i = 0; i < argc && data.dataAvail() > 0; i++) {
609 args.add(data.readString16());
610 }
611 return dump(fd, args);
612 }
Dianne Hackborn555f89d2012-05-08 18:54:22 -0700613
Dianne Hackborn23eb1e22015-10-07 17:35:27 -0700614 case SHELL_COMMAND_TRANSACTION: {
615 int in = data.readFileDescriptor();
616 int out = data.readFileDescriptor();
617 int err = data.readFileDescriptor();
618 int argc = data.readInt32();
619 Vector<String16> args;
620 for (int i = 0; i < argc && data.dataAvail() > 0; i++) {
621 args.add(data.readString16());
622 }
Dianne Hackborn1941a402016-08-29 12:30:43 -0700623 sp<IShellCallback> shellCallback = IShellCallback::asInterface(
624 data.readStrongBinder());
Dianne Hackborn23eb1e22015-10-07 17:35:27 -0700625 sp<IResultReceiver> resultReceiver = IResultReceiver::asInterface(
626 data.readStrongBinder());
627
Dianne Hackbornf2bf93b2015-10-14 15:13:02 -0700628 // XXX can't add virtuals until binaries are updated.
629 //return shellCommand(in, out, err, args, resultReceiver);
Christopher Wiley0a9a1c12016-07-20 08:28:14 -0700630 (void)in;
631 (void)out;
632 (void)err;
633
Yi Kongfdd8da92018-06-07 17:52:27 -0700634 if (resultReceiver != nullptr) {
Dianne Hackbornf2bf93b2015-10-14 15:13:02 -0700635 resultReceiver->send(INVALID_OPERATION);
636 }
Martijn Coenenaa6ee992017-08-17 15:38:08 +0200637
638 return NO_ERROR;
Dianne Hackborn23eb1e22015-10-07 17:35:27 -0700639 }
640
Dianne Hackborn555f89d2012-05-08 18:54:22 -0700641 case SYSPROPS_TRANSACTION: {
642 report_sysprop_change();
643 return NO_ERROR;
644 }
645
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800646 default:
647 return UNKNOWN_TRANSACTION;
648 }
649}
650
Steven Morelandf0212002018-12-26 13:59:23 -0800651BBinder::Extras* BBinder::getOrCreateExtras()
652{
653 Extras* e = mExtras.load(std::memory_order_acquire);
654
655 if (!e) {
656 e = new Extras;
657 Extras* expected = nullptr;
658 if (!mExtras.compare_exchange_strong(expected, e,
659 std::memory_order_release,
660 std::memory_order_acquire)) {
661 delete e;
662 e = expected; // Filled in by CAS
663 }
664 if (e == nullptr) return nullptr; // out of memory
665 }
666
667 return e;
668}
669
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800670// ---------------------------------------------------------------------------
671
672enum {
673 // This is used to transfer ownership of the remote binder from
674 // the BpRefBase object holding it (when it is constructed), to the
675 // owner of the BpRefBase object when it first acquires that BpRefBase.
676 kRemoteAcquired = 0x00000001
677};
678
679BpRefBase::BpRefBase(const sp<IBinder>& o)
Yi Kongfdd8da92018-06-07 17:52:27 -0700680 : mRemote(o.get()), mRefs(nullptr), mState(0)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800681{
682 extendObjectLifetime(OBJECT_LIFETIME_WEAK);
683
684 if (mRemote) {
685 mRemote->incStrong(this); // Removed on first IncStrong().
686 mRefs = mRemote->createWeak(this); // Held for our entire lifetime.
687 }
688}
689
690BpRefBase::~BpRefBase()
691{
692 if (mRemote) {
Bailey Forrest6913c462015-08-18 17:15:10 -0700693 if (!(mState.load(std::memory_order_relaxed)&kRemoteAcquired)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800694 mRemote->decStrong(this);
695 }
696 mRefs->decWeak(this);
697 }
698}
699
700void BpRefBase::onFirstRef()
701{
Bailey Forrest6913c462015-08-18 17:15:10 -0700702 mState.fetch_or(kRemoteAcquired, std::memory_order_relaxed);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800703}
704
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800705void BpRefBase::onLastStrongRef(const void* /*id*/)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800706{
707 if (mRemote) {
708 mRemote->decStrong(this);
709 }
710}
711
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800712bool BpRefBase::onIncStrongAttempted(uint32_t /*flags*/, const void* /*id*/)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800713{
714 return mRemote ? mRefs->attemptIncStrong(this) : false;
715}
716
717// ---------------------------------------------------------------------------
718
Steven Moreland61ff8492019-09-26 16:05:45 -0700719} // namespace android