blob: ec9d5544f9f614143726ee47e80c2424e3da8916 [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
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080057// ---------------------------------------------------------------------------
58
Mathias Agopian83c04462009-05-22 19:00:22 -070059IBinder::IBinder()
60 : RefBase()
61{
62}
63
64IBinder::~IBinder()
65{
66}
67
68// ---------------------------------------------------------------------------
69
Colin Cross6f4f3ab2014-02-05 17:42:44 -080070sp<IInterface> IBinder::queryLocalInterface(const String16& /*descriptor*/)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080071{
Yi Kongfdd8da92018-06-07 17:52:27 -070072 return nullptr;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080073}
74
75BBinder* IBinder::localBinder()
76{
Yi Kongfdd8da92018-06-07 17:52:27 -070077 return nullptr;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080078}
79
80BpBinder* IBinder::remoteBinder()
81{
Yi Kongfdd8da92018-06-07 17:52:27 -070082 return nullptr;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080083}
84
85bool IBinder::checkSubclass(const void* /*subclassID*/) const
86{
87 return false;
88}
89
Dianne Hackborn23eb1e22015-10-07 17:35:27 -070090
Dianne Hackbornf2bf93b2015-10-14 15:13:02 -070091status_t IBinder::shellCommand(const sp<IBinder>& target, int in, int out, int err,
Dianne Hackborn1941a402016-08-29 12:30:43 -070092 Vector<String16>& args, const sp<IShellCallback>& callback,
93 const sp<IResultReceiver>& resultReceiver)
Dianne Hackborn23eb1e22015-10-07 17:35:27 -070094{
Dianne Hackbornf2bf93b2015-10-14 15:13:02 -070095 Parcel send;
96 Parcel reply;
97 send.writeFileDescriptor(in);
98 send.writeFileDescriptor(out);
99 send.writeFileDescriptor(err);
100 const size_t numArgs = args.size();
101 send.writeInt32(numArgs);
102 for (size_t i = 0; i < numArgs; i++) {
103 send.writeString16(args[i]);
Dianne Hackborn23eb1e22015-10-07 17:35:27 -0700104 }
Yi Kongfdd8da92018-06-07 17:52:27 -0700105 send.writeStrongBinder(callback != nullptr ? IInterface::asBinder(callback) : nullptr);
106 send.writeStrongBinder(resultReceiver != nullptr ? IInterface::asBinder(resultReceiver) : nullptr);
Dianne Hackbornf2bf93b2015-10-14 15:13:02 -0700107 return target->transact(SHELL_COMMAND_TRANSACTION, send, &reply);
Dianne Hackborn23eb1e22015-10-07 17:35:27 -0700108}
109
Steven Morelandb8ad08d2019-08-09 14:42:56 -0700110status_t IBinder::getExtension(sp<IBinder>* out) {
111 BBinder* local = this->localBinder();
112 if (local != nullptr) {
113 *out = local->getExtension();
114 return OK;
115 }
116
117 BpBinder* proxy = this->remoteBinder();
118 LOG_ALWAYS_FATAL_IF(proxy == nullptr);
119
120 Parcel data;
121 Parcel reply;
122 status_t status = transact(EXTENSION_TRANSACTION, data, &reply);
123 if (status != OK) return status;
124
125 return reply.readNullableStrongBinder(out);
126}
127
Steven Moreland86080b82019-09-23 15:41:18 -0700128status_t IBinder::getDebugPid(pid_t* out) {
129 BBinder* local = this->localBinder();
130 if (local != nullptr) {
131 *out = local->getDebugPid();
132 return OK;
133 }
134
135 BpBinder* proxy = this->remoteBinder();
136 LOG_ALWAYS_FATAL_IF(proxy == nullptr);
137
138 Parcel data;
139 Parcel reply;
140 status_t status = transact(DEBUG_PID_TRANSACTION, data, &reply);
141 if (status != OK) return status;
142
143 int32_t pid;
144 status = reply.readInt32(&pid);
145 if (status != OK) return status;
146
147 if (pid < 0 || pid > std::numeric_limits<pid_t>::max()) {
148 return BAD_VALUE;
149 }
150 *out = pid;
151 return OK;
152}
153
Yifan Hong02530ec2021-06-10 13:38:38 -0700154status_t IBinder::setRpcClientDebug(android::base::unique_fd socketFd,
155 const sp<IBinder>& keepAliveBinder) {
Yifan Hong84bedeb2021-04-21 21:37:17 -0700156 if constexpr (!kEnableRpcDevServers) {
157 ALOGW("setRpcClientDebug disallowed because RPC is not enabled");
158 return INVALID_OPERATION;
159 }
160
161 BBinder* local = this->localBinder();
162 if (local != nullptr) {
Yifan Hong02530ec2021-06-10 13:38:38 -0700163 return local->BBinder::setRpcClientDebug(std::move(socketFd), keepAliveBinder);
Yifan Hong84bedeb2021-04-21 21:37:17 -0700164 }
165
166 BpBinder* proxy = this->remoteBinder();
167 LOG_ALWAYS_FATAL_IF(proxy == nullptr, "binder object must be either local or remote");
168
169 Parcel data;
170 Parcel reply;
171 status_t status;
172 if (status = data.writeBool(socketFd.ok()); status != OK) return status;
173 if (socketFd.ok()) {
174 // writeUniqueFileDescriptor currently makes an unnecessary dup().
175 status = data.writeFileDescriptor(socketFd.release(), true /* own */);
176 if (status != OK) return status;
177 }
Yifan Hong02530ec2021-06-10 13:38:38 -0700178 if (status = data.writeStrongBinder(keepAliveBinder); status != OK) return status;
Yifan Hong84bedeb2021-04-21 21:37:17 -0700179 return transact(SET_RPC_CLIENT_TRANSACTION, data, &reply);
180}
181
Steven Moreland9e759e82021-06-25 21:30:23 +0000182void IBinder::withLock(const std::function<void()>& doWithLock) {
183 BBinder* local = localBinder();
184 if (local) {
185 local->withLock(doWithLock);
186 return;
187 }
188 BpBinder* proxy = this->remoteBinder();
189 LOG_ALWAYS_FATAL_IF(proxy == nullptr, "binder object must be either local or remote");
190 proxy->withLock(doWithLock);
191}
192
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800193// ---------------------------------------------------------------------------
194
Yifan Hong8b890852021-06-10 13:44:09 -0700195class BBinder::RpcServerLink : public IBinder::DeathRecipient {
196public:
197 // On binder died, calls RpcServer::shutdown on @a rpcServer, and removes itself from @a binder.
198 RpcServerLink(const sp<RpcServer>& rpcServer, const sp<IBinder>& keepAliveBinder,
199 const wp<BBinder>& binder)
200 : mRpcServer(rpcServer), mKeepAliveBinder(keepAliveBinder), mBinder(binder) {}
201 void binderDied(const wp<IBinder>&) override {
202 LOG_RPC_DETAIL("RpcServerLink: binder died, shutting down RpcServer");
203 if (mRpcServer == nullptr) {
204 ALOGW("RpcServerLink: Unable to shut down RpcServer because it does not exist.");
205 } else {
206 ALOGW_IF(!mRpcServer->shutdown(),
207 "RpcServerLink: RpcServer did not shut down properly. Not started?");
208 }
209 mRpcServer.clear();
210
211 auto promoted = mBinder.promote();
212 if (promoted == nullptr) {
213 ALOGW("RpcServerLink: Unable to remove link from parent binder object because parent "
214 "binder object is gone.");
215 } else {
216 promoted->removeRpcServerLink(sp<RpcServerLink>::fromExisting(this));
217 }
218 mBinder.clear();
219 }
220
221private:
222 sp<RpcServer> mRpcServer;
223 sp<IBinder> mKeepAliveBinder; // hold to avoid automatically unlinking
224 wp<BBinder> mBinder;
225};
226
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800227class BBinder::Extras
228{
229public:
Steven Morelandf0212002018-12-26 13:59:23 -0800230 // unlocked objects
231 bool mRequestingSid = false;
Steven Morelandcf03cf12020-12-04 02:58:40 +0000232 bool mInheritRt = false;
Steven Morelandb8ad08d2019-08-09 14:42:56 -0700233 sp<IBinder> mExtension;
Steven Morelandbf1915b2020-07-16 22:43:02 +0000234 int mPolicy = SCHED_NORMAL;
235 int mPriority = 0;
Steven Morelandf0212002018-12-26 13:59:23 -0800236
237 // for below objects
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800238 Mutex mLock;
Yifan Hong8b890852021-06-10 13:44:09 -0700239 std::set<sp<RpcServerLink>> mRpcServerLinks;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800240 BpBinder::ObjectManager mObjects;
241};
242
243// ---------------------------------------------------------------------------
244
Kalesh Singhd67c8e82020-12-29 15:46:25 -0500245BBinder::BBinder() : mExtras(nullptr), mStability(0), mParceled(false) {}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800246
247bool BBinder::isBinderAlive() const
248{
249 return true;
250}
251
252status_t BBinder::pingBinder()
253{
254 return NO_ERROR;
255}
256
Mathias Agopian83c04462009-05-22 19:00:22 -0700257const String16& BBinder::getInterfaceDescriptor() const
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800258{
Dan Egnor386a3322010-05-06 00:55:09 -0700259 // This is a local static rather than a global static,
260 // to avoid static initializer ordering issues.
261 static String16 sEmptyDescriptor;
Steve Block32397c12012-01-05 23:22:43 +0000262 ALOGW("reached BBinder::getInterfaceDescriptor (this=%p)", this);
Mathias Agopian83c04462009-05-22 19:00:22 -0700263 return sEmptyDescriptor;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800264}
265
Jiyong Parkb86c8662018-10-29 23:01:57 +0900266// NOLINTNEXTLINE(google-default-arguments)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800267status_t BBinder::transact(
268 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
269{
270 data.setDataPosition(0);
271
Steven Morelandf183fdd2020-10-27 00:12:12 +0000272 if (reply != nullptr && (flags & FLAG_CLEAR_BUF)) {
273 reply->markSensitive();
274 }
275
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800276 status_t err = NO_ERROR;
277 switch (code) {
278 case PING_TRANSACTION:
Steven Moreland6e69d652019-07-10 14:17:55 -0700279 err = pingBinder();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800280 break;
Steven Morelandb8ad08d2019-08-09 14:42:56 -0700281 case EXTENSION_TRANSACTION:
282 err = reply->writeStrongBinder(getExtension());
283 break;
Steven Moreland86080b82019-09-23 15:41:18 -0700284 case DEBUG_PID_TRANSACTION:
285 err = reply->writeInt32(getDebugPid());
286 break;
Yifan Hong84bedeb2021-04-21 21:37:17 -0700287 case SET_RPC_CLIENT_TRANSACTION: {
288 err = setRpcClientDebug(data);
289 break;
290 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800291 default:
292 err = onTransact(code, data, reply, flags);
293 break;
294 }
295
Steven Morelanda86a3562019-08-01 23:28:34 +0000296 // In case this is being transacted on in the same process.
Yi Kongfdd8da92018-06-07 17:52:27 -0700297 if (reply != nullptr) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800298 reply->setDataPosition(0);
299 }
300
301 return err;
302}
303
Jiyong Parkb86c8662018-10-29 23:01:57 +0900304// NOLINTNEXTLINE(google-default-arguments)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800305status_t BBinder::linkToDeath(
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800306 const sp<DeathRecipient>& /*recipient*/, void* /*cookie*/,
307 uint32_t /*flags*/)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800308{
309 return INVALID_OPERATION;
310}
311
Jiyong Parkb86c8662018-10-29 23:01:57 +0900312// NOLINTNEXTLINE(google-default-arguments)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800313status_t BBinder::unlinkToDeath(
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800314 const wp<DeathRecipient>& /*recipient*/, void* /*cookie*/,
315 uint32_t /*flags*/, wp<DeathRecipient>* /*outRecipient*/)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800316{
317 return INVALID_OPERATION;
318}
319
Dianne Hackborn23eb1e22015-10-07 17:35:27 -0700320status_t BBinder::dump(int /*fd*/, const Vector<String16>& /*args*/)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800321{
322 return NO_ERROR;
323}
324
Steven Moreland63a2d512021-06-25 01:10:15 +0000325void* BBinder::attachObject(const void* objectID, void* object, void* cleanupCookie,
326 object_cleanup_func func) {
Steven Morelandf0212002018-12-26 13:59:23 -0800327 Extras* e = getOrCreateExtras();
Steven Moreland5ce7ca52021-06-25 21:59:50 +0000328 LOG_ALWAYS_FATAL_IF(!e, "no memory");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800329
330 AutoMutex _l(e->mLock);
Steven Moreland63a2d512021-06-25 01:10:15 +0000331 return e->mObjects.attach(objectID, object, cleanupCookie, func);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800332}
333
334void* BBinder::findObject(const void* objectID) const
335{
Bailey Forrest6913c462015-08-18 17:15:10 -0700336 Extras* e = mExtras.load(std::memory_order_acquire);
Yi Kongfdd8da92018-06-07 17:52:27 -0700337 if (!e) return nullptr;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800338
339 AutoMutex _l(e->mLock);
340 return e->mObjects.find(objectID);
341}
342
Steven Moreland63a2d512021-06-25 01:10:15 +0000343void* BBinder::detachObject(const void* objectID) {
Bailey Forrest6913c462015-08-18 17:15:10 -0700344 Extras* e = mExtras.load(std::memory_order_acquire);
Steven Moreland63a2d512021-06-25 01:10:15 +0000345 if (!e) return nullptr;
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.detach(objectID);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800349}
350
Steven Moreland9e759e82021-06-25 21:30:23 +0000351void BBinder::withLock(const std::function<void()>& doWithLock) {
352 Extras* e = getOrCreateExtras();
353 LOG_ALWAYS_FATAL_IF(!e, "no memory");
354
355 AutoMutex _l(e->mLock);
356 doWithLock();
357}
358
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800359BBinder* BBinder::localBinder()
360{
361 return this;
362}
363
Steven Morelandf0212002018-12-26 13:59:23 -0800364bool BBinder::isRequestingSid()
365{
366 Extras* e = mExtras.load(std::memory_order_acquire);
367
368 return e && e->mRequestingSid;
369}
370
371void BBinder::setRequestingSid(bool requestingSid)
372{
Steven Morelandd37e6dd2021-06-16 22:07:51 +0000373 LOG_ALWAYS_FATAL_IF(mParceled,
374 "setRequestingSid() should not be called after a binder object "
375 "is parceled/sent to another process");
Kalesh Singhd67c8e82020-12-29 15:46:25 -0500376
Steven Morelandf0212002018-12-26 13:59:23 -0800377 Extras* e = mExtras.load(std::memory_order_acquire);
378
379 if (!e) {
380 // default is false. Most things don't need sids, so avoiding allocations when possible.
381 if (!requestingSid) {
382 return;
383 }
384
385 e = getOrCreateExtras();
386 if (!e) return; // out of memory
387 }
388
Steven Moreland3668be62019-02-08 17:56:55 -0800389 e->mRequestingSid = requestingSid;
Steven Morelandf0212002018-12-26 13:59:23 -0800390}
391
Steven Morelandb8ad08d2019-08-09 14:42:56 -0700392sp<IBinder> BBinder::getExtension() {
393 Extras* e = mExtras.load(std::memory_order_acquire);
394 if (e == nullptr) return nullptr;
395 return e->mExtension;
396}
397
Steven Morelandbf1915b2020-07-16 22:43:02 +0000398void BBinder::setMinSchedulerPolicy(int policy, int priority) {
Steven Morelandd37e6dd2021-06-16 22:07:51 +0000399 LOG_ALWAYS_FATAL_IF(mParceled,
400 "setMinSchedulerPolicy() should not be called after a binder object "
401 "is parceled/sent to another process");
Kalesh Singhd67c8e82020-12-29 15:46:25 -0500402
Steven Morelandbf1915b2020-07-16 22:43:02 +0000403 switch (policy) {
404 case SCHED_NORMAL:
405 LOG_ALWAYS_FATAL_IF(priority < -20 || priority > 19, "Invalid priority for SCHED_NORMAL: %d", priority);
406 break;
407 case SCHED_RR:
408 case SCHED_FIFO:
409 LOG_ALWAYS_FATAL_IF(priority < 1 || priority > 99, "Invalid priority for sched %d: %d", policy, priority);
410 break;
411 default:
412 LOG_ALWAYS_FATAL("Unrecognized scheduling policy: %d", policy);
413 }
414
415 Extras* e = mExtras.load(std::memory_order_acquire);
416
417 if (e == nullptr) {
418 // Avoid allocations if called with default.
419 if (policy == SCHED_NORMAL && priority == 0) {
420 return;
421 }
422
423 e = getOrCreateExtras();
424 if (!e) return; // out of memory
425 }
426
427 e->mPolicy = policy;
428 e->mPriority = priority;
429}
430
431int BBinder::getMinSchedulerPolicy() {
432 Extras* e = mExtras.load(std::memory_order_acquire);
433 if (e == nullptr) return SCHED_NORMAL;
434 return e->mPolicy;
435}
436
437int BBinder::getMinSchedulerPriority() {
438 Extras* e = mExtras.load(std::memory_order_acquire);
439 if (e == nullptr) return 0;
440 return e->mPriority;
441}
442
Steven Morelandcf03cf12020-12-04 02:58:40 +0000443bool BBinder::isInheritRt() {
444 Extras* e = mExtras.load(std::memory_order_acquire);
445
446 return e && e->mInheritRt;
447}
448
449void BBinder::setInheritRt(bool inheritRt) {
Steven Morelandd37e6dd2021-06-16 22:07:51 +0000450 LOG_ALWAYS_FATAL_IF(mParceled,
451 "setInheritRt() should not be called after a binder object "
452 "is parceled/sent to another process");
Kalesh Singhd67c8e82020-12-29 15:46:25 -0500453
Steven Morelandcf03cf12020-12-04 02:58:40 +0000454 Extras* e = mExtras.load(std::memory_order_acquire);
455
456 if (!e) {
457 if (!inheritRt) {
458 return;
459 }
460
461 e = getOrCreateExtras();
462 if (!e) return; // out of memory
463 }
464
465 e->mInheritRt = inheritRt;
466}
467
Steven Moreland86080b82019-09-23 15:41:18 -0700468pid_t BBinder::getDebugPid() {
469 return getpid();
470}
471
Steven Morelandb8ad08d2019-08-09 14:42:56 -0700472void BBinder::setExtension(const sp<IBinder>& extension) {
Steven Morelandd37e6dd2021-06-16 22:07:51 +0000473 LOG_ALWAYS_FATAL_IF(mParceled,
474 "setExtension() should not be called after a binder object "
475 "is parceled/sent to another process");
Kalesh Singhd67c8e82020-12-29 15:46:25 -0500476
Steven Morelandb8ad08d2019-08-09 14:42:56 -0700477 Extras* e = getOrCreateExtras();
478 e->mExtension = extension;
479}
480
Kalesh Singhd67c8e82020-12-29 15:46:25 -0500481bool BBinder::wasParceled() {
482 return mParceled;
483}
484
485void BBinder::setParceled() {
486 mParceled = true;
487}
488
Yifan Hong84bedeb2021-04-21 21:37:17 -0700489status_t BBinder::setRpcClientDebug(const Parcel& data) {
490 if constexpr (!kEnableRpcDevServers) {
491 ALOGW("%s: disallowed because RPC is not enabled", __PRETTY_FUNCTION__);
492 return INVALID_OPERATION;
493 }
494 uid_t uid = IPCThreadState::self()->getCallingUid();
495 if (uid != AID_ROOT) {
496 ALOGE("%s: not allowed because client %" PRIu32 " is not root", __PRETTY_FUNCTION__, uid);
497 return PERMISSION_DENIED;
498 }
499 status_t status;
500 bool hasSocketFd;
501 android::base::unique_fd clientFd;
Yifan Hong84bedeb2021-04-21 21:37:17 -0700502
503 if (status = data.readBool(&hasSocketFd); status != OK) return status;
504 if (hasSocketFd) {
505 if (status = data.readUniqueFileDescriptor(&clientFd); status != OK) return status;
506 }
Yifan Hong02530ec2021-06-10 13:38:38 -0700507 sp<IBinder> keepAliveBinder;
508 if (status = data.readNullableStrongBinder(&keepAliveBinder); status != OK) return status;
Yifan Hong84bedeb2021-04-21 21:37:17 -0700509
Yifan Hong02530ec2021-06-10 13:38:38 -0700510 return setRpcClientDebug(std::move(clientFd), keepAliveBinder);
Yifan Hong84bedeb2021-04-21 21:37:17 -0700511}
512
Yifan Hong02530ec2021-06-10 13:38:38 -0700513status_t BBinder::setRpcClientDebug(android::base::unique_fd socketFd,
514 const sp<IBinder>& keepAliveBinder) {
Yifan Hong84bedeb2021-04-21 21:37:17 -0700515 if constexpr (!kEnableRpcDevServers) {
516 ALOGW("%s: disallowed because RPC is not enabled", __PRETTY_FUNCTION__);
517 return INVALID_OPERATION;
518 }
519
520 const int socketFdForPrint = socketFd.get();
Yifan Hong34823232021-06-07 17:23:00 -0700521 LOG_RPC_DETAIL("%s(fd=%d)", __PRETTY_FUNCTION__, socketFdForPrint);
Yifan Hong84bedeb2021-04-21 21:37:17 -0700522
523 if (!socketFd.ok()) {
524 ALOGE("%s: No socket FD provided.", __PRETTY_FUNCTION__);
525 return BAD_VALUE;
526 }
Yifan Hong84bedeb2021-04-21 21:37:17 -0700527
Yifan Hong02530ec2021-06-10 13:38:38 -0700528 if (keepAliveBinder == nullptr) {
529 ALOGE("%s: No keepAliveBinder provided.", __PRETTY_FUNCTION__);
530 return UNEXPECTED_NULL;
531 }
532
Yifan Hong84bedeb2021-04-21 21:37:17 -0700533 size_t binderThreadPoolMaxCount = ProcessState::self()->getThreadPoolMaxThreadCount();
534 if (binderThreadPoolMaxCount <= 1) {
535 ALOGE("%s: ProcessState thread pool max count is %zu. RPC is disabled for this service "
536 "because RPC requires the service to support multithreading.",
537 __PRETTY_FUNCTION__, binderThreadPoolMaxCount);
538 return INVALID_OPERATION;
539 }
540
Yifan Hong8b890852021-06-10 13:44:09 -0700541 // Weak ref to avoid circular dependency:
542 // BBinder -> RpcServerLink ----> RpcServer -X-> BBinder
543 // `-X-> BBinder
544 auto weakThis = wp<BBinder>::fromExisting(this);
545
Yifan Hong84bedeb2021-04-21 21:37:17 -0700546 Extras* e = getOrCreateExtras();
547 AutoMutex _l(e->mLock);
Yifan Hong8b890852021-06-10 13:44:09 -0700548 auto rpcServer = RpcServer::make();
549 LOG_ALWAYS_FATAL_IF(rpcServer == nullptr, "RpcServer::make returns null");
Yifan Hong8b890852021-06-10 13:44:09 -0700550 auto link = sp<RpcServerLink>::make(rpcServer, keepAliveBinder, weakThis);
551 if (auto status = keepAliveBinder->linkToDeath(link, nullptr, 0); status != OK) {
552 ALOGE("%s: keepAliveBinder->linkToDeath returns %s", __PRETTY_FUNCTION__,
553 statusToString(status).c_str());
554 return status;
Yifan Hong84bedeb2021-04-21 21:37:17 -0700555 }
Yifan Hong8b890852021-06-10 13:44:09 -0700556 rpcServer->setRootObjectWeak(weakThis);
Steven Moreland2372f9d2021-08-05 15:42:01 -0700557 if (auto status = rpcServer->setupExternalServer(std::move(socketFd)); status != OK) {
558 return status;
559 }
Yifan Hong8b890852021-06-10 13:44:09 -0700560 rpcServer->setMaxThreads(binderThreadPoolMaxCount);
561 rpcServer->start();
562 e->mRpcServerLinks.emplace(link);
Yifan Hong34823232021-06-07 17:23:00 -0700563 LOG_RPC_DETAIL("%s(fd=%d) successful", __PRETTY_FUNCTION__, socketFdForPrint);
Yifan Hong84bedeb2021-04-21 21:37:17 -0700564 return OK;
565}
566
Yifan Hong8b890852021-06-10 13:44:09 -0700567void BBinder::removeRpcServerLink(const sp<RpcServerLink>& link) {
568 Extras* e = mExtras.load(std::memory_order_acquire);
569 if (!e) return;
570 AutoMutex _l(e->mLock);
571 (void)e->mRpcServerLinks.erase(link);
572}
573
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800574BBinder::~BBinder()
575{
Bailey Forrest6913c462015-08-18 17:15:10 -0700576 Extras* e = mExtras.load(std::memory_order_relaxed);
Hans Boehm3effaba2014-08-12 22:56:00 +0000577 if (e) delete e;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800578}
579
580
Jiyong Parkb86c8662018-10-29 23:01:57 +0900581// NOLINTNEXTLINE(google-default-arguments)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800582status_t BBinder::onTransact(
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800583 uint32_t code, const Parcel& data, Parcel* reply, uint32_t /*flags*/)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800584{
585 switch (code) {
586 case INTERFACE_TRANSACTION:
587 reply->writeString16(getInterfaceDescriptor());
588 return NO_ERROR;
589
590 case DUMP_TRANSACTION: {
591 int fd = data.readFileDescriptor();
592 int argc = data.readInt32();
593 Vector<String16> args;
594 for (int i = 0; i < argc && data.dataAvail() > 0; i++) {
595 args.add(data.readString16());
596 }
597 return dump(fd, args);
598 }
Dianne Hackborn555f89d2012-05-08 18:54:22 -0700599
Dianne Hackborn23eb1e22015-10-07 17:35:27 -0700600 case SHELL_COMMAND_TRANSACTION: {
601 int in = data.readFileDescriptor();
602 int out = data.readFileDescriptor();
603 int err = data.readFileDescriptor();
604 int argc = data.readInt32();
605 Vector<String16> args;
606 for (int i = 0; i < argc && data.dataAvail() > 0; i++) {
607 args.add(data.readString16());
608 }
Dianne Hackborn1941a402016-08-29 12:30:43 -0700609 sp<IShellCallback> shellCallback = IShellCallback::asInterface(
610 data.readStrongBinder());
Dianne Hackborn23eb1e22015-10-07 17:35:27 -0700611 sp<IResultReceiver> resultReceiver = IResultReceiver::asInterface(
612 data.readStrongBinder());
613
Dianne Hackbornf2bf93b2015-10-14 15:13:02 -0700614 // XXX can't add virtuals until binaries are updated.
615 //return shellCommand(in, out, err, args, resultReceiver);
Christopher Wiley0a9a1c12016-07-20 08:28:14 -0700616 (void)in;
617 (void)out;
618 (void)err;
619
Yi Kongfdd8da92018-06-07 17:52:27 -0700620 if (resultReceiver != nullptr) {
Dianne Hackbornf2bf93b2015-10-14 15:13:02 -0700621 resultReceiver->send(INVALID_OPERATION);
622 }
Martijn Coenenaa6ee992017-08-17 15:38:08 +0200623
624 return NO_ERROR;
Dianne Hackborn23eb1e22015-10-07 17:35:27 -0700625 }
626
Dianne Hackborn555f89d2012-05-08 18:54:22 -0700627 case SYSPROPS_TRANSACTION: {
628 report_sysprop_change();
629 return NO_ERROR;
630 }
631
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800632 default:
633 return UNKNOWN_TRANSACTION;
634 }
635}
636
Steven Morelandf0212002018-12-26 13:59:23 -0800637BBinder::Extras* BBinder::getOrCreateExtras()
638{
639 Extras* e = mExtras.load(std::memory_order_acquire);
640
641 if (!e) {
642 e = new Extras;
643 Extras* expected = nullptr;
644 if (!mExtras.compare_exchange_strong(expected, e,
645 std::memory_order_release,
646 std::memory_order_acquire)) {
647 delete e;
648 e = expected; // Filled in by CAS
649 }
650 if (e == nullptr) return nullptr; // out of memory
651 }
652
653 return e;
654}
655
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800656// ---------------------------------------------------------------------------
657
658enum {
659 // This is used to transfer ownership of the remote binder from
660 // the BpRefBase object holding it (when it is constructed), to the
661 // owner of the BpRefBase object when it first acquires that BpRefBase.
662 kRemoteAcquired = 0x00000001
663};
664
665BpRefBase::BpRefBase(const sp<IBinder>& o)
Yi Kongfdd8da92018-06-07 17:52:27 -0700666 : mRemote(o.get()), mRefs(nullptr), mState(0)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800667{
668 extendObjectLifetime(OBJECT_LIFETIME_WEAK);
669
670 if (mRemote) {
671 mRemote->incStrong(this); // Removed on first IncStrong().
672 mRefs = mRemote->createWeak(this); // Held for our entire lifetime.
673 }
674}
675
676BpRefBase::~BpRefBase()
677{
678 if (mRemote) {
Bailey Forrest6913c462015-08-18 17:15:10 -0700679 if (!(mState.load(std::memory_order_relaxed)&kRemoteAcquired)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800680 mRemote->decStrong(this);
681 }
682 mRefs->decWeak(this);
683 }
684}
685
686void BpRefBase::onFirstRef()
687{
Bailey Forrest6913c462015-08-18 17:15:10 -0700688 mState.fetch_or(kRemoteAcquired, std::memory_order_relaxed);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800689}
690
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800691void BpRefBase::onLastStrongRef(const void* /*id*/)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800692{
693 if (mRemote) {
694 mRemote->decStrong(this);
695 }
696}
697
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800698bool BpRefBase::onIncStrongAttempted(uint32_t /*flags*/, const void* /*id*/)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800699{
700 return mRemote ? mRefs->attemptIncStrong(this) : false;
701}
702
703// ---------------------------------------------------------------------------
704
Steven Moreland61ff8492019-09-26 16:05:45 -0700705} // namespace android