blob: 0970ca5aa5de5c7f1695533078466e7b4f169c12 [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
22#include <android-base/unique_fd.h>
Mathias Agopianc5b2c0b2009-05-19 19:08:10 -070023#include <binder/BpBinder.h>
24#include <binder/IInterface.h>
Yifan Hong84bedeb2021-04-21 21:37:17 -070025#include <binder/IPCThreadState.h>
Dianne Hackborn23eb1e22015-10-07 17:35:27 -070026#include <binder/IResultReceiver.h>
Dianne Hackborn1941a402016-08-29 12:30:43 -070027#include <binder/IShellCallback.h>
Mathias Agopianc5b2c0b2009-05-19 19:08:10 -070028#include <binder/Parcel.h>
Yifan Hong84bedeb2021-04-21 21:37:17 -070029#include <binder/RpcServer.h>
30#include <private/android_filesystem_config.h>
31#include <utils/misc.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080032
Yifan Hong84bedeb2021-04-21 21:37:17 -070033#include <inttypes.h>
Steven Morelandbf1915b2020-07-16 22:43:02 +000034#include <linux/sched.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080035#include <stdio.h>
36
Yifan Hong84bedeb2021-04-21 21:37:17 -070037#include "RpcState.h"
38
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080039namespace android {
40
Steven Moreland90c1f9a2021-05-03 18:27:24 +000041// Service implementations inherit from BBinder and IBinder, and this is frozen
42// in prebuilts.
43#ifdef __LP64__
44static_assert(sizeof(IBinder) == 24);
45static_assert(sizeof(BBinder) == 40);
46#else
47static_assert(sizeof(IBinder) == 12);
48static_assert(sizeof(BBinder) == 20);
49#endif
50
Yifan Hong84bedeb2021-04-21 21:37:17 -070051#ifdef BINDER_RPC_DEV_SERVERS
52constexpr const bool kEnableRpcDevServers = true;
53#else
54constexpr const bool kEnableRpcDevServers = false;
55#endif
56
Martijn Coenen1cad19c2021-10-04 09:19:01 +020057// Log any reply transactions for which the data exceeds this size
58#define LOG_REPLIES_OVER_SIZE (300 * 1024)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080059// ---------------------------------------------------------------------------
60
Mathias Agopian83c04462009-05-22 19:00:22 -070061IBinder::IBinder()
62 : RefBase()
63{
64}
65
66IBinder::~IBinder()
67{
68}
69
70// ---------------------------------------------------------------------------
71
Colin Cross6f4f3ab2014-02-05 17:42:44 -080072sp<IInterface> IBinder::queryLocalInterface(const String16& /*descriptor*/)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080073{
Yi Kongfdd8da92018-06-07 17:52:27 -070074 return nullptr;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080075}
76
77BBinder* IBinder::localBinder()
78{
Yi Kongfdd8da92018-06-07 17:52:27 -070079 return nullptr;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080080}
81
82BpBinder* IBinder::remoteBinder()
83{
Yi Kongfdd8da92018-06-07 17:52:27 -070084 return nullptr;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080085}
86
87bool IBinder::checkSubclass(const void* /*subclassID*/) const
88{
89 return false;
90}
91
Dianne Hackborn23eb1e22015-10-07 17:35:27 -070092
Dianne Hackbornf2bf93b2015-10-14 15:13:02 -070093status_t IBinder::shellCommand(const sp<IBinder>& target, int in, int out, int err,
Dianne Hackborn1941a402016-08-29 12:30:43 -070094 Vector<String16>& args, const sp<IShellCallback>& callback,
95 const sp<IResultReceiver>& resultReceiver)
Dianne Hackborn23eb1e22015-10-07 17:35:27 -070096{
Dianne Hackbornf2bf93b2015-10-14 15:13:02 -070097 Parcel send;
98 Parcel reply;
99 send.writeFileDescriptor(in);
100 send.writeFileDescriptor(out);
101 send.writeFileDescriptor(err);
102 const size_t numArgs = args.size();
103 send.writeInt32(numArgs);
104 for (size_t i = 0; i < numArgs; i++) {
105 send.writeString16(args[i]);
Dianne Hackborn23eb1e22015-10-07 17:35:27 -0700106 }
Yi Kongfdd8da92018-06-07 17:52:27 -0700107 send.writeStrongBinder(callback != nullptr ? IInterface::asBinder(callback) : nullptr);
108 send.writeStrongBinder(resultReceiver != nullptr ? IInterface::asBinder(resultReceiver) : nullptr);
Dianne Hackbornf2bf93b2015-10-14 15:13:02 -0700109 return target->transact(SHELL_COMMAND_TRANSACTION, send, &reply);
Dianne Hackborn23eb1e22015-10-07 17:35:27 -0700110}
111
Steven Morelandb8ad08d2019-08-09 14:42:56 -0700112status_t IBinder::getExtension(sp<IBinder>* out) {
113 BBinder* local = this->localBinder();
114 if (local != nullptr) {
115 *out = local->getExtension();
116 return OK;
117 }
118
119 BpBinder* proxy = this->remoteBinder();
120 LOG_ALWAYS_FATAL_IF(proxy == nullptr);
121
122 Parcel data;
123 Parcel reply;
124 status_t status = transact(EXTENSION_TRANSACTION, data, &reply);
125 if (status != OK) return status;
126
127 return reply.readNullableStrongBinder(out);
128}
129
Steven Moreland86080b82019-09-23 15:41:18 -0700130status_t IBinder::getDebugPid(pid_t* out) {
131 BBinder* local = this->localBinder();
132 if (local != nullptr) {
133 *out = local->getDebugPid();
134 return OK;
135 }
136
137 BpBinder* proxy = this->remoteBinder();
138 LOG_ALWAYS_FATAL_IF(proxy == nullptr);
139
140 Parcel data;
141 Parcel reply;
142 status_t status = transact(DEBUG_PID_TRANSACTION, data, &reply);
143 if (status != OK) return status;
144
145 int32_t pid;
146 status = reply.readInt32(&pid);
147 if (status != OK) return status;
148
149 if (pid < 0 || pid > std::numeric_limits<pid_t>::max()) {
150 return BAD_VALUE;
151 }
152 *out = pid;
153 return OK;
154}
155
Yifan Hong02530ec2021-06-10 13:38:38 -0700156status_t IBinder::setRpcClientDebug(android::base::unique_fd socketFd,
157 const sp<IBinder>& keepAliveBinder) {
Yifan Hong84bedeb2021-04-21 21:37:17 -0700158 if constexpr (!kEnableRpcDevServers) {
159 ALOGW("setRpcClientDebug disallowed because RPC is not enabled");
160 return INVALID_OPERATION;
161 }
162
163 BBinder* local = this->localBinder();
164 if (local != nullptr) {
Yifan Hong02530ec2021-06-10 13:38:38 -0700165 return local->BBinder::setRpcClientDebug(std::move(socketFd), keepAliveBinder);
Yifan Hong84bedeb2021-04-21 21:37:17 -0700166 }
167
168 BpBinder* proxy = this->remoteBinder();
169 LOG_ALWAYS_FATAL_IF(proxy == nullptr, "binder object must be either local or remote");
170
171 Parcel data;
172 Parcel reply;
173 status_t status;
174 if (status = data.writeBool(socketFd.ok()); status != OK) return status;
175 if (socketFd.ok()) {
176 // writeUniqueFileDescriptor currently makes an unnecessary dup().
177 status = data.writeFileDescriptor(socketFd.release(), true /* own */);
178 if (status != OK) return status;
179 }
Yifan Hong02530ec2021-06-10 13:38:38 -0700180 if (status = data.writeStrongBinder(keepAliveBinder); status != OK) return status;
Yifan Hong84bedeb2021-04-21 21:37:17 -0700181 return transact(SET_RPC_CLIENT_TRANSACTION, data, &reply);
182}
183
Steven Moreland9e759e82021-06-25 21:30:23 +0000184void IBinder::withLock(const std::function<void()>& doWithLock) {
185 BBinder* local = localBinder();
186 if (local) {
187 local->withLock(doWithLock);
188 return;
189 }
190 BpBinder* proxy = this->remoteBinder();
191 LOG_ALWAYS_FATAL_IF(proxy == nullptr, "binder object must be either local or remote");
192 proxy->withLock(doWithLock);
193}
194
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800195// ---------------------------------------------------------------------------
196
Yifan Hong8b890852021-06-10 13:44:09 -0700197class BBinder::RpcServerLink : public IBinder::DeathRecipient {
198public:
199 // On binder died, calls RpcServer::shutdown on @a rpcServer, and removes itself from @a binder.
200 RpcServerLink(const sp<RpcServer>& rpcServer, const sp<IBinder>& keepAliveBinder,
201 const wp<BBinder>& binder)
202 : mRpcServer(rpcServer), mKeepAliveBinder(keepAliveBinder), mBinder(binder) {}
203 void binderDied(const wp<IBinder>&) override {
204 LOG_RPC_DETAIL("RpcServerLink: binder died, shutting down RpcServer");
205 if (mRpcServer == nullptr) {
206 ALOGW("RpcServerLink: Unable to shut down RpcServer because it does not exist.");
207 } else {
208 ALOGW_IF(!mRpcServer->shutdown(),
209 "RpcServerLink: RpcServer did not shut down properly. Not started?");
210 }
211 mRpcServer.clear();
212
213 auto promoted = mBinder.promote();
214 if (promoted == nullptr) {
215 ALOGW("RpcServerLink: Unable to remove link from parent binder object because parent "
216 "binder object is gone.");
217 } else {
218 promoted->removeRpcServerLink(sp<RpcServerLink>::fromExisting(this));
219 }
220 mBinder.clear();
221 }
222
223private:
224 sp<RpcServer> mRpcServer;
225 sp<IBinder> mKeepAliveBinder; // hold to avoid automatically unlinking
226 wp<BBinder> mBinder;
227};
228
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800229class BBinder::Extras
230{
231public:
Steven Morelandf0212002018-12-26 13:59:23 -0800232 // unlocked objects
233 bool mRequestingSid = false;
Steven Morelandcf03cf12020-12-04 02:58:40 +0000234 bool mInheritRt = false;
Steven Morelandb8ad08d2019-08-09 14:42:56 -0700235 sp<IBinder> mExtension;
Steven Morelandbf1915b2020-07-16 22:43:02 +0000236 int mPolicy = SCHED_NORMAL;
237 int mPriority = 0;
Steven Morelandf0212002018-12-26 13:59:23 -0800238
239 // for below objects
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800240 Mutex mLock;
Yifan Hong8b890852021-06-10 13:44:09 -0700241 std::set<sp<RpcServerLink>> mRpcServerLinks;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800242 BpBinder::ObjectManager mObjects;
243};
244
245// ---------------------------------------------------------------------------
246
Kalesh Singhd67c8e82020-12-29 15:46:25 -0500247BBinder::BBinder() : mExtras(nullptr), mStability(0), mParceled(false) {}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800248
249bool BBinder::isBinderAlive() const
250{
251 return true;
252}
253
254status_t BBinder::pingBinder()
255{
256 return NO_ERROR;
257}
258
Mathias Agopian83c04462009-05-22 19:00:22 -0700259const String16& BBinder::getInterfaceDescriptor() const
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800260{
Dan Egnor386a3322010-05-06 00:55:09 -0700261 // This is a local static rather than a global static,
262 // to avoid static initializer ordering issues.
263 static String16 sEmptyDescriptor;
Steve Block32397c12012-01-05 23:22:43 +0000264 ALOGW("reached BBinder::getInterfaceDescriptor (this=%p)", this);
Mathias Agopian83c04462009-05-22 19:00:22 -0700265 return sEmptyDescriptor;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800266}
267
Jiyong Parkb86c8662018-10-29 23:01:57 +0900268// NOLINTNEXTLINE(google-default-arguments)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800269status_t BBinder::transact(
270 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
271{
272 data.setDataPosition(0);
273
Steven Morelandf183fdd2020-10-27 00:12:12 +0000274 if (reply != nullptr && (flags & FLAG_CLEAR_BUF)) {
275 reply->markSensitive();
276 }
277
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800278 status_t err = NO_ERROR;
279 switch (code) {
280 case PING_TRANSACTION:
Steven Moreland6e69d652019-07-10 14:17:55 -0700281 err = pingBinder();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800282 break;
Steven Morelandb8ad08d2019-08-09 14:42:56 -0700283 case EXTENSION_TRANSACTION:
284 err = reply->writeStrongBinder(getExtension());
285 break;
Steven Moreland86080b82019-09-23 15:41:18 -0700286 case DEBUG_PID_TRANSACTION:
287 err = reply->writeInt32(getDebugPid());
288 break;
Yifan Hong84bedeb2021-04-21 21:37:17 -0700289 case SET_RPC_CLIENT_TRANSACTION: {
290 err = setRpcClientDebug(data);
291 break;
292 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800293 default:
294 err = onTransact(code, data, reply, flags);
295 break;
296 }
297
Steven Morelanda86a3562019-08-01 23:28:34 +0000298 // In case this is being transacted on in the same process.
Yi Kongfdd8da92018-06-07 17:52:27 -0700299 if (reply != nullptr) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800300 reply->setDataPosition(0);
Martijn Coenen1cad19c2021-10-04 09:19:01 +0200301 if (reply->dataSize() > LOG_REPLIES_OVER_SIZE) {
302 ALOGW("Large reply transaction of %zu bytes, interface descriptor %s, code %d",
303 reply->dataSize(), String8(getInterfaceDescriptor()).c_str(), code);
304 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800305 }
306
307 return err;
308}
309
Jiyong Parkb86c8662018-10-29 23:01:57 +0900310// NOLINTNEXTLINE(google-default-arguments)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800311status_t BBinder::linkToDeath(
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800312 const sp<DeathRecipient>& /*recipient*/, void* /*cookie*/,
313 uint32_t /*flags*/)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800314{
315 return INVALID_OPERATION;
316}
317
Jiyong Parkb86c8662018-10-29 23:01:57 +0900318// NOLINTNEXTLINE(google-default-arguments)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800319status_t BBinder::unlinkToDeath(
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800320 const wp<DeathRecipient>& /*recipient*/, void* /*cookie*/,
321 uint32_t /*flags*/, wp<DeathRecipient>* /*outRecipient*/)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800322{
323 return INVALID_OPERATION;
324}
325
Dianne Hackborn23eb1e22015-10-07 17:35:27 -0700326status_t BBinder::dump(int /*fd*/, const Vector<String16>& /*args*/)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800327{
328 return NO_ERROR;
329}
330
Steven Moreland63a2d512021-06-25 01:10:15 +0000331void* BBinder::attachObject(const void* objectID, void* object, void* cleanupCookie,
332 object_cleanup_func func) {
Steven Morelandf0212002018-12-26 13:59:23 -0800333 Extras* e = getOrCreateExtras();
Steven Moreland5ce7ca52021-06-25 21:59:50 +0000334 LOG_ALWAYS_FATAL_IF(!e, "no memory");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800335
336 AutoMutex _l(e->mLock);
Steven Moreland63a2d512021-06-25 01:10:15 +0000337 return e->mObjects.attach(objectID, object, cleanupCookie, func);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800338}
339
340void* BBinder::findObject(const void* objectID) const
341{
Bailey Forrest6913c462015-08-18 17:15:10 -0700342 Extras* e = mExtras.load(std::memory_order_acquire);
Yi Kongfdd8da92018-06-07 17:52:27 -0700343 if (!e) return nullptr;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800344
345 AutoMutex _l(e->mLock);
346 return e->mObjects.find(objectID);
347}
348
Steven Moreland63a2d512021-06-25 01:10:15 +0000349void* BBinder::detachObject(const void* objectID) {
Bailey Forrest6913c462015-08-18 17:15:10 -0700350 Extras* e = mExtras.load(std::memory_order_acquire);
Steven Moreland63a2d512021-06-25 01:10:15 +0000351 if (!e) return nullptr;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800352
353 AutoMutex _l(e->mLock);
Steven Moreland63a2d512021-06-25 01:10:15 +0000354 return e->mObjects.detach(objectID);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800355}
356
Steven Moreland9e759e82021-06-25 21:30:23 +0000357void BBinder::withLock(const std::function<void()>& doWithLock) {
358 Extras* e = getOrCreateExtras();
359 LOG_ALWAYS_FATAL_IF(!e, "no memory");
360
361 AutoMutex _l(e->mLock);
362 doWithLock();
363}
364
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800365BBinder* BBinder::localBinder()
366{
367 return this;
368}
369
Steven Morelandf0212002018-12-26 13:59:23 -0800370bool BBinder::isRequestingSid()
371{
372 Extras* e = mExtras.load(std::memory_order_acquire);
373
374 return e && e->mRequestingSid;
375}
376
377void BBinder::setRequestingSid(bool requestingSid)
378{
Steven Morelandd37e6dd2021-06-16 22:07:51 +0000379 LOG_ALWAYS_FATAL_IF(mParceled,
380 "setRequestingSid() should not be called after a binder object "
381 "is parceled/sent to another process");
Kalesh Singhd67c8e82020-12-29 15:46:25 -0500382
Steven Morelandf0212002018-12-26 13:59:23 -0800383 Extras* e = mExtras.load(std::memory_order_acquire);
384
385 if (!e) {
386 // default is false. Most things don't need sids, so avoiding allocations when possible.
387 if (!requestingSid) {
388 return;
389 }
390
391 e = getOrCreateExtras();
392 if (!e) return; // out of memory
393 }
394
Steven Moreland3668be62019-02-08 17:56:55 -0800395 e->mRequestingSid = requestingSid;
Steven Morelandf0212002018-12-26 13:59:23 -0800396}
397
Steven Morelandb8ad08d2019-08-09 14:42:56 -0700398sp<IBinder> BBinder::getExtension() {
399 Extras* e = mExtras.load(std::memory_order_acquire);
400 if (e == nullptr) return nullptr;
401 return e->mExtension;
402}
403
Steven Morelandbf1915b2020-07-16 22:43:02 +0000404void BBinder::setMinSchedulerPolicy(int policy, int priority) {
Steven Morelandd37e6dd2021-06-16 22:07:51 +0000405 LOG_ALWAYS_FATAL_IF(mParceled,
406 "setMinSchedulerPolicy() should not be called after a binder object "
407 "is parceled/sent to another process");
Kalesh Singhd67c8e82020-12-29 15:46:25 -0500408
Steven Morelandbf1915b2020-07-16 22:43:02 +0000409 switch (policy) {
410 case SCHED_NORMAL:
411 LOG_ALWAYS_FATAL_IF(priority < -20 || priority > 19, "Invalid priority for SCHED_NORMAL: %d", priority);
412 break;
413 case SCHED_RR:
414 case SCHED_FIFO:
415 LOG_ALWAYS_FATAL_IF(priority < 1 || priority > 99, "Invalid priority for sched %d: %d", policy, priority);
416 break;
417 default:
418 LOG_ALWAYS_FATAL("Unrecognized scheduling policy: %d", policy);
419 }
420
421 Extras* e = mExtras.load(std::memory_order_acquire);
422
423 if (e == nullptr) {
424 // Avoid allocations if called with default.
425 if (policy == SCHED_NORMAL && priority == 0) {
426 return;
427 }
428
429 e = getOrCreateExtras();
430 if (!e) return; // out of memory
431 }
432
433 e->mPolicy = policy;
434 e->mPriority = priority;
435}
436
437int BBinder::getMinSchedulerPolicy() {
438 Extras* e = mExtras.load(std::memory_order_acquire);
439 if (e == nullptr) return SCHED_NORMAL;
440 return e->mPolicy;
441}
442
443int BBinder::getMinSchedulerPriority() {
444 Extras* e = mExtras.load(std::memory_order_acquire);
445 if (e == nullptr) return 0;
446 return e->mPriority;
447}
448
Steven Morelandcf03cf12020-12-04 02:58:40 +0000449bool BBinder::isInheritRt() {
450 Extras* e = mExtras.load(std::memory_order_acquire);
451
452 return e && e->mInheritRt;
453}
454
455void BBinder::setInheritRt(bool inheritRt) {
Steven Morelandd37e6dd2021-06-16 22:07:51 +0000456 LOG_ALWAYS_FATAL_IF(mParceled,
457 "setInheritRt() should not be called after a binder object "
458 "is parceled/sent to another process");
Kalesh Singhd67c8e82020-12-29 15:46:25 -0500459
Steven Morelandcf03cf12020-12-04 02:58:40 +0000460 Extras* e = mExtras.load(std::memory_order_acquire);
461
462 if (!e) {
463 if (!inheritRt) {
464 return;
465 }
466
467 e = getOrCreateExtras();
468 if (!e) return; // out of memory
469 }
470
471 e->mInheritRt = inheritRt;
472}
473
Steven Moreland86080b82019-09-23 15:41:18 -0700474pid_t BBinder::getDebugPid() {
475 return getpid();
476}
477
Steven Morelandb8ad08d2019-08-09 14:42:56 -0700478void BBinder::setExtension(const sp<IBinder>& extension) {
Steven Morelandd37e6dd2021-06-16 22:07:51 +0000479 LOG_ALWAYS_FATAL_IF(mParceled,
480 "setExtension() should not be called after a binder object "
481 "is parceled/sent to another process");
Kalesh Singhd67c8e82020-12-29 15:46:25 -0500482
Steven Morelandb8ad08d2019-08-09 14:42:56 -0700483 Extras* e = getOrCreateExtras();
484 e->mExtension = extension;
485}
486
Kalesh Singhd67c8e82020-12-29 15:46:25 -0500487bool BBinder::wasParceled() {
488 return mParceled;
489}
490
491void BBinder::setParceled() {
492 mParceled = true;
493}
494
Yifan Hong84bedeb2021-04-21 21:37:17 -0700495status_t BBinder::setRpcClientDebug(const Parcel& data) {
496 if constexpr (!kEnableRpcDevServers) {
497 ALOGW("%s: disallowed because RPC is not enabled", __PRETTY_FUNCTION__);
498 return INVALID_OPERATION;
499 }
500 uid_t uid = IPCThreadState::self()->getCallingUid();
501 if (uid != AID_ROOT) {
502 ALOGE("%s: not allowed because client %" PRIu32 " is not root", __PRETTY_FUNCTION__, uid);
503 return PERMISSION_DENIED;
504 }
505 status_t status;
506 bool hasSocketFd;
507 android::base::unique_fd clientFd;
Yifan Hong84bedeb2021-04-21 21:37:17 -0700508
509 if (status = data.readBool(&hasSocketFd); status != OK) return status;
510 if (hasSocketFd) {
511 if (status = data.readUniqueFileDescriptor(&clientFd); status != OK) return status;
512 }
Yifan Hong02530ec2021-06-10 13:38:38 -0700513 sp<IBinder> keepAliveBinder;
514 if (status = data.readNullableStrongBinder(&keepAliveBinder); status != OK) return status;
Yifan Hong84bedeb2021-04-21 21:37:17 -0700515
Yifan Hong02530ec2021-06-10 13:38:38 -0700516 return setRpcClientDebug(std::move(clientFd), keepAliveBinder);
Yifan Hong84bedeb2021-04-21 21:37:17 -0700517}
518
Yifan Hong02530ec2021-06-10 13:38:38 -0700519status_t BBinder::setRpcClientDebug(android::base::unique_fd socketFd,
520 const sp<IBinder>& keepAliveBinder) {
Yifan Hong84bedeb2021-04-21 21:37:17 -0700521 if constexpr (!kEnableRpcDevServers) {
522 ALOGW("%s: disallowed because RPC is not enabled", __PRETTY_FUNCTION__);
523 return INVALID_OPERATION;
524 }
525
526 const int socketFdForPrint = socketFd.get();
Yifan Hong34823232021-06-07 17:23:00 -0700527 LOG_RPC_DETAIL("%s(fd=%d)", __PRETTY_FUNCTION__, socketFdForPrint);
Yifan Hong84bedeb2021-04-21 21:37:17 -0700528
529 if (!socketFd.ok()) {
530 ALOGE("%s: No socket FD provided.", __PRETTY_FUNCTION__);
531 return BAD_VALUE;
532 }
Yifan Hong84bedeb2021-04-21 21:37:17 -0700533
Yifan Hong02530ec2021-06-10 13:38:38 -0700534 if (keepAliveBinder == nullptr) {
535 ALOGE("%s: No keepAliveBinder provided.", __PRETTY_FUNCTION__);
536 return UNEXPECTED_NULL;
537 }
538
Yifan Hong84bedeb2021-04-21 21:37:17 -0700539 size_t binderThreadPoolMaxCount = ProcessState::self()->getThreadPoolMaxThreadCount();
540 if (binderThreadPoolMaxCount <= 1) {
541 ALOGE("%s: ProcessState thread pool max count is %zu. RPC is disabled for this service "
542 "because RPC requires the service to support multithreading.",
543 __PRETTY_FUNCTION__, binderThreadPoolMaxCount);
544 return INVALID_OPERATION;
545 }
546
Yifan Hong8b890852021-06-10 13:44:09 -0700547 // Weak ref to avoid circular dependency:
548 // BBinder -> RpcServerLink ----> RpcServer -X-> BBinder
549 // `-X-> BBinder
550 auto weakThis = wp<BBinder>::fromExisting(this);
551
Yifan Hong84bedeb2021-04-21 21:37:17 -0700552 Extras* e = getOrCreateExtras();
553 AutoMutex _l(e->mLock);
Yifan Hong8b890852021-06-10 13:44:09 -0700554 auto rpcServer = RpcServer::make();
555 LOG_ALWAYS_FATAL_IF(rpcServer == nullptr, "RpcServer::make returns null");
Yifan Hong8b890852021-06-10 13:44:09 -0700556 auto link = sp<RpcServerLink>::make(rpcServer, keepAliveBinder, weakThis);
557 if (auto status = keepAliveBinder->linkToDeath(link, nullptr, 0); status != OK) {
558 ALOGE("%s: keepAliveBinder->linkToDeath returns %s", __PRETTY_FUNCTION__,
559 statusToString(status).c_str());
560 return status;
Yifan Hong84bedeb2021-04-21 21:37:17 -0700561 }
Yifan Hong8b890852021-06-10 13:44:09 -0700562 rpcServer->setRootObjectWeak(weakThis);
Steven Moreland2372f9d2021-08-05 15:42:01 -0700563 if (auto status = rpcServer->setupExternalServer(std::move(socketFd)); status != OK) {
564 return status;
565 }
Yifan Hong8b890852021-06-10 13:44:09 -0700566 rpcServer->setMaxThreads(binderThreadPoolMaxCount);
567 rpcServer->start();
568 e->mRpcServerLinks.emplace(link);
Yifan Hong34823232021-06-07 17:23:00 -0700569 LOG_RPC_DETAIL("%s(fd=%d) successful", __PRETTY_FUNCTION__, socketFdForPrint);
Yifan Hong84bedeb2021-04-21 21:37:17 -0700570 return OK;
571}
572
Yifan Hong8b890852021-06-10 13:44:09 -0700573void BBinder::removeRpcServerLink(const sp<RpcServerLink>& link) {
574 Extras* e = mExtras.load(std::memory_order_acquire);
575 if (!e) return;
576 AutoMutex _l(e->mLock);
577 (void)e->mRpcServerLinks.erase(link);
578}
579
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800580BBinder::~BBinder()
581{
Bailey Forrest6913c462015-08-18 17:15:10 -0700582 Extras* e = mExtras.load(std::memory_order_relaxed);
Hans Boehm3effaba2014-08-12 22:56:00 +0000583 if (e) delete e;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800584}
585
586
Jiyong Parkb86c8662018-10-29 23:01:57 +0900587// NOLINTNEXTLINE(google-default-arguments)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800588status_t BBinder::onTransact(
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800589 uint32_t code, const Parcel& data, Parcel* reply, uint32_t /*flags*/)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800590{
591 switch (code) {
592 case INTERFACE_TRANSACTION:
593 reply->writeString16(getInterfaceDescriptor());
594 return NO_ERROR;
595
596 case DUMP_TRANSACTION: {
597 int fd = data.readFileDescriptor();
598 int argc = data.readInt32();
599 Vector<String16> args;
600 for (int i = 0; i < argc && data.dataAvail() > 0; i++) {
601 args.add(data.readString16());
602 }
603 return dump(fd, args);
604 }
Dianne Hackborn555f89d2012-05-08 18:54:22 -0700605
Dianne Hackborn23eb1e22015-10-07 17:35:27 -0700606 case SHELL_COMMAND_TRANSACTION: {
607 int in = data.readFileDescriptor();
608 int out = data.readFileDescriptor();
609 int err = data.readFileDescriptor();
610 int argc = data.readInt32();
611 Vector<String16> args;
612 for (int i = 0; i < argc && data.dataAvail() > 0; i++) {
613 args.add(data.readString16());
614 }
Dianne Hackborn1941a402016-08-29 12:30:43 -0700615 sp<IShellCallback> shellCallback = IShellCallback::asInterface(
616 data.readStrongBinder());
Dianne Hackborn23eb1e22015-10-07 17:35:27 -0700617 sp<IResultReceiver> resultReceiver = IResultReceiver::asInterface(
618 data.readStrongBinder());
619
Dianne Hackbornf2bf93b2015-10-14 15:13:02 -0700620 // XXX can't add virtuals until binaries are updated.
621 //return shellCommand(in, out, err, args, resultReceiver);
Christopher Wiley0a9a1c12016-07-20 08:28:14 -0700622 (void)in;
623 (void)out;
624 (void)err;
625
Yi Kongfdd8da92018-06-07 17:52:27 -0700626 if (resultReceiver != nullptr) {
Dianne Hackbornf2bf93b2015-10-14 15:13:02 -0700627 resultReceiver->send(INVALID_OPERATION);
628 }
Martijn Coenenaa6ee992017-08-17 15:38:08 +0200629
630 return NO_ERROR;
Dianne Hackborn23eb1e22015-10-07 17:35:27 -0700631 }
632
Dianne Hackborn555f89d2012-05-08 18:54:22 -0700633 case SYSPROPS_TRANSACTION: {
634 report_sysprop_change();
635 return NO_ERROR;
636 }
637
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800638 default:
639 return UNKNOWN_TRANSACTION;
640 }
641}
642
Steven Morelandf0212002018-12-26 13:59:23 -0800643BBinder::Extras* BBinder::getOrCreateExtras()
644{
645 Extras* e = mExtras.load(std::memory_order_acquire);
646
647 if (!e) {
648 e = new Extras;
649 Extras* expected = nullptr;
650 if (!mExtras.compare_exchange_strong(expected, e,
651 std::memory_order_release,
652 std::memory_order_acquire)) {
653 delete e;
654 e = expected; // Filled in by CAS
655 }
656 if (e == nullptr) return nullptr; // out of memory
657 }
658
659 return e;
660}
661
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800662// ---------------------------------------------------------------------------
663
664enum {
665 // This is used to transfer ownership of the remote binder from
666 // the BpRefBase object holding it (when it is constructed), to the
667 // owner of the BpRefBase object when it first acquires that BpRefBase.
668 kRemoteAcquired = 0x00000001
669};
670
671BpRefBase::BpRefBase(const sp<IBinder>& o)
Yi Kongfdd8da92018-06-07 17:52:27 -0700672 : mRemote(o.get()), mRefs(nullptr), mState(0)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800673{
674 extendObjectLifetime(OBJECT_LIFETIME_WEAK);
675
676 if (mRemote) {
677 mRemote->incStrong(this); // Removed on first IncStrong().
678 mRefs = mRemote->createWeak(this); // Held for our entire lifetime.
679 }
680}
681
682BpRefBase::~BpRefBase()
683{
684 if (mRemote) {
Bailey Forrest6913c462015-08-18 17:15:10 -0700685 if (!(mState.load(std::memory_order_relaxed)&kRemoteAcquired)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800686 mRemote->decStrong(this);
687 }
688 mRefs->decWeak(this);
689 }
690}
691
692void BpRefBase::onFirstRef()
693{
Bailey Forrest6913c462015-08-18 17:15:10 -0700694 mState.fetch_or(kRemoteAcquired, std::memory_order_relaxed);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800695}
696
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800697void BpRefBase::onLastStrongRef(const void* /*id*/)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800698{
699 if (mRemote) {
700 mRemote->decStrong(this);
701 }
702}
703
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800704bool BpRefBase::onIncStrongAttempted(uint32_t /*flags*/, const void* /*id*/)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800705{
706 return mRemote ? mRefs->attemptIncStrong(this) : false;
707}
708
709// ---------------------------------------------------------------------------
710
Steven Moreland61ff8492019-09-26 16:05:45 -0700711} // namespace android