blob: 1dc62334cbaaae3b15f44f68268ac337e8bea906 [file] [log] [blame]
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2005 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Mathias Agopianc5b2c0b2009-05-19 19:08:10 -070017#include <binder/Binder.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080018
Bailey Forrest6913c462015-08-18 17:15:10 -070019#include <atomic>
Yifan Hong8b890852021-06-10 13:44:09 -070020#include <set>
Yifan Hong84bedeb2021-04-21 21:37:17 -070021
Jiyong Park5970d0a2022-03-08 16:56:13 +090022#include <android-base/logging.h>
Yifan Hong84bedeb2021-04-21 21:37:17 -070023#include <android-base/unique_fd.h>
Mathias Agopianc5b2c0b2009-05-19 19:08:10 -070024#include <binder/BpBinder.h>
25#include <binder/IInterface.h>
Yifan Hong84bedeb2021-04-21 21:37:17 -070026#include <binder/IPCThreadState.h>
Dianne Hackborn23eb1e22015-10-07 17:35:27 -070027#include <binder/IResultReceiver.h>
Dianne Hackborn1941a402016-08-29 12:30:43 -070028#include <binder/IShellCallback.h>
Mathias Agopianc5b2c0b2009-05-19 19:08:10 -070029#include <binder/Parcel.h>
Yifan Hong84bedeb2021-04-21 21:37:17 -070030#include <binder/RpcServer.h>
31#include <private/android_filesystem_config.h>
32#include <utils/misc.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080033
Yifan Hong84bedeb2021-04-21 21:37:17 -070034#include <inttypes.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080035#include <stdio.h>
36
Andrei Homescuf55d6882022-04-30 00:50:20 +000037#ifdef __linux__
38#include <linux/sched.h>
39#endif
40
Steven Moreland32150282021-11-12 22:54:53 +000041#include "BuildFlags.h"
Yifan Hong84bedeb2021-04-21 21:37:17 -070042#include "RpcState.h"
43
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080044namespace android {
45
Steven Moreland90c1f9a2021-05-03 18:27:24 +000046// Service implementations inherit from BBinder and IBinder, and this is frozen
47// in prebuilts.
48#ifdef __LP64__
49static_assert(sizeof(IBinder) == 24);
50static_assert(sizeof(BBinder) == 40);
51#else
52static_assert(sizeof(IBinder) == 12);
53static_assert(sizeof(BBinder) == 20);
54#endif
55
Steven Morelandd9a74002022-06-02 00:03:18 +000056// global b/c b/230079120 - consistent symbol table
Yifan Hong84bedeb2021-04-21 21:37:17 -070057#ifdef BINDER_RPC_DEV_SERVERS
Steven Morelandd9a74002022-06-02 00:03:18 +000058bool kEnableRpcDevServers = true;
Yifan Hong84bedeb2021-04-21 21:37:17 -070059#else
Steven Morelandd9a74002022-06-02 00:03:18 +000060bool kEnableRpcDevServers = false;
Yifan Hong84bedeb2021-04-21 21:37:17 -070061#endif
62
Martijn Coenen1cad19c2021-10-04 09:19:01 +020063// Log any reply transactions for which the data exceeds this size
64#define LOG_REPLIES_OVER_SIZE (300 * 1024)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080065// ---------------------------------------------------------------------------
66
Mathias Agopian83c04462009-05-22 19:00:22 -070067IBinder::IBinder()
68 : RefBase()
69{
70}
71
72IBinder::~IBinder()
73{
74}
75
76// ---------------------------------------------------------------------------
77
Colin Cross6f4f3ab2014-02-05 17:42:44 -080078sp<IInterface> IBinder::queryLocalInterface(const String16& /*descriptor*/)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080079{
Yi Kongfdd8da92018-06-07 17:52:27 -070080 return nullptr;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080081}
82
83BBinder* IBinder::localBinder()
84{
Yi Kongfdd8da92018-06-07 17:52:27 -070085 return nullptr;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080086}
87
88BpBinder* IBinder::remoteBinder()
89{
Yi Kongfdd8da92018-06-07 17:52:27 -070090 return nullptr;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080091}
92
93bool IBinder::checkSubclass(const void* /*subclassID*/) const
94{
95 return false;
96}
97
Dianne Hackborn23eb1e22015-10-07 17:35:27 -070098
Dianne Hackbornf2bf93b2015-10-14 15:13:02 -070099status_t IBinder::shellCommand(const sp<IBinder>& target, int in, int out, int err,
Dianne Hackborn1941a402016-08-29 12:30:43 -0700100 Vector<String16>& args, const sp<IShellCallback>& callback,
101 const sp<IResultReceiver>& resultReceiver)
Dianne Hackborn23eb1e22015-10-07 17:35:27 -0700102{
Dianne Hackbornf2bf93b2015-10-14 15:13:02 -0700103 Parcel send;
104 Parcel reply;
105 send.writeFileDescriptor(in);
106 send.writeFileDescriptor(out);
107 send.writeFileDescriptor(err);
108 const size_t numArgs = args.size();
109 send.writeInt32(numArgs);
110 for (size_t i = 0; i < numArgs; i++) {
111 send.writeString16(args[i]);
Dianne Hackborn23eb1e22015-10-07 17:35:27 -0700112 }
Yi Kongfdd8da92018-06-07 17:52:27 -0700113 send.writeStrongBinder(callback != nullptr ? IInterface::asBinder(callback) : nullptr);
114 send.writeStrongBinder(resultReceiver != nullptr ? IInterface::asBinder(resultReceiver) : nullptr);
Dianne Hackbornf2bf93b2015-10-14 15:13:02 -0700115 return target->transact(SHELL_COMMAND_TRANSACTION, send, &reply);
Dianne Hackborn23eb1e22015-10-07 17:35:27 -0700116}
117
Steven Morelandb8ad08d2019-08-09 14:42:56 -0700118status_t IBinder::getExtension(sp<IBinder>* out) {
119 BBinder* local = this->localBinder();
120 if (local != nullptr) {
121 *out = local->getExtension();
122 return OK;
123 }
124
125 BpBinder* proxy = this->remoteBinder();
126 LOG_ALWAYS_FATAL_IF(proxy == nullptr);
127
128 Parcel data;
129 Parcel reply;
130 status_t status = transact(EXTENSION_TRANSACTION, data, &reply);
131 if (status != OK) return status;
132
133 return reply.readNullableStrongBinder(out);
134}
135
Steven Moreland86080b82019-09-23 15:41:18 -0700136status_t IBinder::getDebugPid(pid_t* out) {
137 BBinder* local = this->localBinder();
138 if (local != nullptr) {
139 *out = local->getDebugPid();
140 return OK;
141 }
142
143 BpBinder* proxy = this->remoteBinder();
144 LOG_ALWAYS_FATAL_IF(proxy == nullptr);
145
146 Parcel data;
147 Parcel reply;
148 status_t status = transact(DEBUG_PID_TRANSACTION, data, &reply);
149 if (status != OK) return status;
150
151 int32_t pid;
152 status = reply.readInt32(&pid);
153 if (status != OK) return status;
154
155 if (pid < 0 || pid > std::numeric_limits<pid_t>::max()) {
156 return BAD_VALUE;
157 }
158 *out = pid;
159 return OK;
160}
161
Yifan Hong02530ec2021-06-10 13:38:38 -0700162status_t IBinder::setRpcClientDebug(android::base::unique_fd socketFd,
163 const sp<IBinder>& keepAliveBinder) {
Steven Morelandd9a74002022-06-02 00:03:18 +0000164 if (!kEnableRpcDevServers) {
Yifan Hong84bedeb2021-04-21 21:37:17 -0700165 ALOGW("setRpcClientDebug disallowed because RPC is not enabled");
166 return INVALID_OPERATION;
167 }
Steven Moreland32150282021-11-12 22:54:53 +0000168 if (!kEnableKernelIpc) {
169 ALOGW("setRpcClientDebug disallowed because kernel binder is not enabled");
170 return INVALID_OPERATION;
171 }
Yifan Hong84bedeb2021-04-21 21:37:17 -0700172
173 BBinder* local = this->localBinder();
174 if (local != nullptr) {
Yifan Hong02530ec2021-06-10 13:38:38 -0700175 return local->BBinder::setRpcClientDebug(std::move(socketFd), keepAliveBinder);
Yifan Hong84bedeb2021-04-21 21:37:17 -0700176 }
177
178 BpBinder* proxy = this->remoteBinder();
179 LOG_ALWAYS_FATAL_IF(proxy == nullptr, "binder object must be either local or remote");
180
181 Parcel data;
182 Parcel reply;
183 status_t status;
184 if (status = data.writeBool(socketFd.ok()); status != OK) return status;
185 if (socketFd.ok()) {
186 // writeUniqueFileDescriptor currently makes an unnecessary dup().
187 status = data.writeFileDescriptor(socketFd.release(), true /* own */);
188 if (status != OK) return status;
189 }
Yifan Hong02530ec2021-06-10 13:38:38 -0700190 if (status = data.writeStrongBinder(keepAliveBinder); status != OK) return status;
Yifan Hong84bedeb2021-04-21 21:37:17 -0700191 return transact(SET_RPC_CLIENT_TRANSACTION, data, &reply);
192}
193
Steven Moreland9e759e82021-06-25 21:30:23 +0000194void IBinder::withLock(const std::function<void()>& doWithLock) {
195 BBinder* local = localBinder();
196 if (local) {
197 local->withLock(doWithLock);
198 return;
199 }
200 BpBinder* proxy = this->remoteBinder();
201 LOG_ALWAYS_FATAL_IF(proxy == nullptr, "binder object must be either local or remote");
202 proxy->withLock(doWithLock);
203}
204
Devin Moore3faaa002022-07-27 15:54:06 +0000205sp<IBinder> IBinder::lookupOrCreateWeak(const void* objectID, object_make_func make,
206 const void* makeArgs) {
207 BBinder* local = localBinder();
208 if (local) {
209 return local->lookupOrCreateWeak(objectID, make, makeArgs);
210 }
211 BpBinder* proxy = this->remoteBinder();
212 LOG_ALWAYS_FATAL_IF(proxy == nullptr, "binder object must be either local or remote");
213 return proxy->lookupOrCreateWeak(objectID, make, makeArgs);
214}
215
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800216// ---------------------------------------------------------------------------
217
Yifan Hong8b890852021-06-10 13:44:09 -0700218class BBinder::RpcServerLink : public IBinder::DeathRecipient {
219public:
220 // On binder died, calls RpcServer::shutdown on @a rpcServer, and removes itself from @a binder.
221 RpcServerLink(const sp<RpcServer>& rpcServer, const sp<IBinder>& keepAliveBinder,
222 const wp<BBinder>& binder)
223 : mRpcServer(rpcServer), mKeepAliveBinder(keepAliveBinder), mBinder(binder) {}
Steven Morelandd9a74002022-06-02 00:03:18 +0000224 virtual ~RpcServerLink();
Yifan Hong8b890852021-06-10 13:44:09 -0700225 void binderDied(const wp<IBinder>&) override {
226 LOG_RPC_DETAIL("RpcServerLink: binder died, shutting down RpcServer");
227 if (mRpcServer == nullptr) {
228 ALOGW("RpcServerLink: Unable to shut down RpcServer because it does not exist.");
229 } else {
230 ALOGW_IF(!mRpcServer->shutdown(),
231 "RpcServerLink: RpcServer did not shut down properly. Not started?");
232 }
233 mRpcServer.clear();
234
235 auto promoted = mBinder.promote();
236 if (promoted == nullptr) {
237 ALOGW("RpcServerLink: Unable to remove link from parent binder object because parent "
238 "binder object is gone.");
239 } else {
240 promoted->removeRpcServerLink(sp<RpcServerLink>::fromExisting(this));
241 }
242 mBinder.clear();
243 }
244
245private:
246 sp<RpcServer> mRpcServer;
247 sp<IBinder> mKeepAliveBinder; // hold to avoid automatically unlinking
248 wp<BBinder> mBinder;
249};
Steven Morelandd9a74002022-06-02 00:03:18 +0000250BBinder::RpcServerLink::~RpcServerLink() {}
Yifan Hong8b890852021-06-10 13:44:09 -0700251
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800252class BBinder::Extras
253{
254public:
Steven Morelandf0212002018-12-26 13:59:23 -0800255 // unlocked objects
Steven Morelandb8ad08d2019-08-09 14:42:56 -0700256 sp<IBinder> mExtension;
Andrei Homescuf55d6882022-04-30 00:50:20 +0000257#ifdef __linux__
Steven Morelandbf1915b2020-07-16 22:43:02 +0000258 int mPolicy = SCHED_NORMAL;
259 int mPriority = 0;
Andrei Homescuf55d6882022-04-30 00:50:20 +0000260#endif
261 bool mRequestingSid = false;
262 bool mInheritRt = false;
Steven Morelandf0212002018-12-26 13:59:23 -0800263
264 // for below objects
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800265 Mutex mLock;
Yifan Hong8b890852021-06-10 13:44:09 -0700266 std::set<sp<RpcServerLink>> mRpcServerLinks;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800267 BpBinder::ObjectManager mObjects;
268};
269
270// ---------------------------------------------------------------------------
271
Kalesh Singhd67c8e82020-12-29 15:46:25 -0500272BBinder::BBinder() : mExtras(nullptr), mStability(0), mParceled(false) {}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800273
274bool BBinder::isBinderAlive() const
275{
276 return true;
277}
278
279status_t BBinder::pingBinder()
280{
281 return NO_ERROR;
282}
283
Mathias Agopian83c04462009-05-22 19:00:22 -0700284const String16& BBinder::getInterfaceDescriptor() const
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800285{
Dan Egnor386a3322010-05-06 00:55:09 -0700286 // This is a local static rather than a global static,
287 // to avoid static initializer ordering issues.
288 static String16 sEmptyDescriptor;
Steve Block32397c12012-01-05 23:22:43 +0000289 ALOGW("reached BBinder::getInterfaceDescriptor (this=%p)", this);
Mathias Agopian83c04462009-05-22 19:00:22 -0700290 return sEmptyDescriptor;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800291}
292
Jiyong Parkb86c8662018-10-29 23:01:57 +0900293// NOLINTNEXTLINE(google-default-arguments)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800294status_t BBinder::transact(
295 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
296{
297 data.setDataPosition(0);
298
Steven Morelandf183fdd2020-10-27 00:12:12 +0000299 if (reply != nullptr && (flags & FLAG_CLEAR_BUF)) {
300 reply->markSensitive();
301 }
302
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800303 status_t err = NO_ERROR;
304 switch (code) {
305 case PING_TRANSACTION:
Steven Moreland6e69d652019-07-10 14:17:55 -0700306 err = pingBinder();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800307 break;
Steven Morelandb8ad08d2019-08-09 14:42:56 -0700308 case EXTENSION_TRANSACTION:
Jiyong Park5970d0a2022-03-08 16:56:13 +0900309 CHECK(reply != nullptr);
Steven Morelandb8ad08d2019-08-09 14:42:56 -0700310 err = reply->writeStrongBinder(getExtension());
311 break;
Steven Moreland86080b82019-09-23 15:41:18 -0700312 case DEBUG_PID_TRANSACTION:
Jiyong Park5970d0a2022-03-08 16:56:13 +0900313 CHECK(reply != nullptr);
Steven Moreland86080b82019-09-23 15:41:18 -0700314 err = reply->writeInt32(getDebugPid());
315 break;
Yifan Hong84bedeb2021-04-21 21:37:17 -0700316 case SET_RPC_CLIENT_TRANSACTION: {
317 err = setRpcClientDebug(data);
318 break;
319 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800320 default:
321 err = onTransact(code, data, reply, flags);
322 break;
323 }
324
Steven Morelanda86a3562019-08-01 23:28:34 +0000325 // In case this is being transacted on in the same process.
Yi Kongfdd8da92018-06-07 17:52:27 -0700326 if (reply != nullptr) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800327 reply->setDataPosition(0);
Martijn Coenen1cad19c2021-10-04 09:19:01 +0200328 if (reply->dataSize() > LOG_REPLIES_OVER_SIZE) {
329 ALOGW("Large reply transaction of %zu bytes, interface descriptor %s, code %d",
330 reply->dataSize(), String8(getInterfaceDescriptor()).c_str(), code);
331 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800332 }
333
334 return err;
335}
336
Jiyong Parkb86c8662018-10-29 23:01:57 +0900337// NOLINTNEXTLINE(google-default-arguments)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800338status_t BBinder::linkToDeath(
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800339 const sp<DeathRecipient>& /*recipient*/, void* /*cookie*/,
340 uint32_t /*flags*/)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800341{
342 return INVALID_OPERATION;
343}
344
Jiyong Parkb86c8662018-10-29 23:01:57 +0900345// NOLINTNEXTLINE(google-default-arguments)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800346status_t BBinder::unlinkToDeath(
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800347 const wp<DeathRecipient>& /*recipient*/, void* /*cookie*/,
348 uint32_t /*flags*/, wp<DeathRecipient>* /*outRecipient*/)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800349{
350 return INVALID_OPERATION;
351}
352
Dianne Hackborn23eb1e22015-10-07 17:35:27 -0700353status_t BBinder::dump(int /*fd*/, const Vector<String16>& /*args*/)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800354{
355 return NO_ERROR;
356}
357
Steven Moreland63a2d512021-06-25 01:10:15 +0000358void* BBinder::attachObject(const void* objectID, void* object, void* cleanupCookie,
359 object_cleanup_func func) {
Steven Morelandf0212002018-12-26 13:59:23 -0800360 Extras* e = getOrCreateExtras();
Steven Moreland5ce7ca52021-06-25 21:59:50 +0000361 LOG_ALWAYS_FATAL_IF(!e, "no memory");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800362
363 AutoMutex _l(e->mLock);
Steven Moreland63a2d512021-06-25 01:10:15 +0000364 return e->mObjects.attach(objectID, object, cleanupCookie, func);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800365}
366
367void* BBinder::findObject(const void* objectID) const
368{
Bailey Forrest6913c462015-08-18 17:15:10 -0700369 Extras* e = mExtras.load(std::memory_order_acquire);
Yi Kongfdd8da92018-06-07 17:52:27 -0700370 if (!e) return nullptr;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800371
372 AutoMutex _l(e->mLock);
373 return e->mObjects.find(objectID);
374}
375
Steven Moreland63a2d512021-06-25 01:10:15 +0000376void* BBinder::detachObject(const void* objectID) {
Bailey Forrest6913c462015-08-18 17:15:10 -0700377 Extras* e = mExtras.load(std::memory_order_acquire);
Steven Moreland63a2d512021-06-25 01:10:15 +0000378 if (!e) return nullptr;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800379
380 AutoMutex _l(e->mLock);
Steven Moreland63a2d512021-06-25 01:10:15 +0000381 return e->mObjects.detach(objectID);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800382}
383
Steven Moreland9e759e82021-06-25 21:30:23 +0000384void BBinder::withLock(const std::function<void()>& doWithLock) {
385 Extras* e = getOrCreateExtras();
386 LOG_ALWAYS_FATAL_IF(!e, "no memory");
387
388 AutoMutex _l(e->mLock);
389 doWithLock();
390}
391
Devin Moore3faaa002022-07-27 15:54:06 +0000392sp<IBinder> BBinder::lookupOrCreateWeak(const void* objectID, object_make_func make,
393 const void* makeArgs) {
394 Extras* e = getOrCreateExtras();
395 LOG_ALWAYS_FATAL_IF(!e, "no memory");
396 AutoMutex _l(e->mLock);
397 return e->mObjects.lookupOrCreateWeak(objectID, make, makeArgs);
398}
399
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800400BBinder* BBinder::localBinder()
401{
402 return this;
403}
404
Steven Morelandf0212002018-12-26 13:59:23 -0800405bool BBinder::isRequestingSid()
406{
407 Extras* e = mExtras.load(std::memory_order_acquire);
408
409 return e && e->mRequestingSid;
410}
411
412void BBinder::setRequestingSid(bool requestingSid)
413{
Steven Morelandd37e6dd2021-06-16 22:07:51 +0000414 LOG_ALWAYS_FATAL_IF(mParceled,
415 "setRequestingSid() should not be called after a binder object "
416 "is parceled/sent to another process");
Kalesh Singhd67c8e82020-12-29 15:46:25 -0500417
Steven Morelandf0212002018-12-26 13:59:23 -0800418 Extras* e = mExtras.load(std::memory_order_acquire);
419
420 if (!e) {
421 // default is false. Most things don't need sids, so avoiding allocations when possible.
422 if (!requestingSid) {
423 return;
424 }
425
426 e = getOrCreateExtras();
427 if (!e) return; // out of memory
428 }
429
Steven Moreland3668be62019-02-08 17:56:55 -0800430 e->mRequestingSid = requestingSid;
Steven Morelandf0212002018-12-26 13:59:23 -0800431}
432
Steven Morelandb8ad08d2019-08-09 14:42:56 -0700433sp<IBinder> BBinder::getExtension() {
434 Extras* e = mExtras.load(std::memory_order_acquire);
435 if (e == nullptr) return nullptr;
436 return e->mExtension;
437}
438
Andrei Homescuf55d6882022-04-30 00:50:20 +0000439#ifdef __linux__
Steven Morelandbf1915b2020-07-16 22:43:02 +0000440void BBinder::setMinSchedulerPolicy(int policy, int priority) {
Steven Morelandd37e6dd2021-06-16 22:07:51 +0000441 LOG_ALWAYS_FATAL_IF(mParceled,
442 "setMinSchedulerPolicy() should not be called after a binder object "
443 "is parceled/sent to another process");
Kalesh Singhd67c8e82020-12-29 15:46:25 -0500444
Steven Morelandbf1915b2020-07-16 22:43:02 +0000445 switch (policy) {
446 case SCHED_NORMAL:
447 LOG_ALWAYS_FATAL_IF(priority < -20 || priority > 19, "Invalid priority for SCHED_NORMAL: %d", priority);
448 break;
449 case SCHED_RR:
450 case SCHED_FIFO:
451 LOG_ALWAYS_FATAL_IF(priority < 1 || priority > 99, "Invalid priority for sched %d: %d", policy, priority);
452 break;
453 default:
454 LOG_ALWAYS_FATAL("Unrecognized scheduling policy: %d", policy);
455 }
456
457 Extras* e = mExtras.load(std::memory_order_acquire);
458
459 if (e == nullptr) {
460 // Avoid allocations if called with default.
461 if (policy == SCHED_NORMAL && priority == 0) {
462 return;
463 }
464
465 e = getOrCreateExtras();
466 if (!e) return; // out of memory
467 }
468
469 e->mPolicy = policy;
470 e->mPriority = priority;
471}
472
473int BBinder::getMinSchedulerPolicy() {
474 Extras* e = mExtras.load(std::memory_order_acquire);
475 if (e == nullptr) return SCHED_NORMAL;
476 return e->mPolicy;
477}
478
479int BBinder::getMinSchedulerPriority() {
480 Extras* e = mExtras.load(std::memory_order_acquire);
481 if (e == nullptr) return 0;
482 return e->mPriority;
483}
Andrei Homescuf55d6882022-04-30 00:50:20 +0000484#endif // __linux__
Steven Morelandbf1915b2020-07-16 22:43:02 +0000485
Steven Morelandcf03cf12020-12-04 02:58:40 +0000486bool BBinder::isInheritRt() {
487 Extras* e = mExtras.load(std::memory_order_acquire);
488
489 return e && e->mInheritRt;
490}
491
492void BBinder::setInheritRt(bool inheritRt) {
Steven Morelandd37e6dd2021-06-16 22:07:51 +0000493 LOG_ALWAYS_FATAL_IF(mParceled,
494 "setInheritRt() should not be called after a binder object "
495 "is parceled/sent to another process");
Kalesh Singhd67c8e82020-12-29 15:46:25 -0500496
Steven Morelandcf03cf12020-12-04 02:58:40 +0000497 Extras* e = mExtras.load(std::memory_order_acquire);
498
499 if (!e) {
500 if (!inheritRt) {
501 return;
502 }
503
504 e = getOrCreateExtras();
505 if (!e) return; // out of memory
506 }
507
508 e->mInheritRt = inheritRt;
509}
510
Steven Moreland86080b82019-09-23 15:41:18 -0700511pid_t BBinder::getDebugPid() {
Andrei Homescuf55d6882022-04-30 00:50:20 +0000512#ifdef __linux__
Steven Moreland86080b82019-09-23 15:41:18 -0700513 return getpid();
Andrei Homescuf55d6882022-04-30 00:50:20 +0000514#else
515 // TODO: handle other OSes
516 return 0;
517#endif // __linux__
Steven Moreland86080b82019-09-23 15:41:18 -0700518}
519
Steven Morelandb8ad08d2019-08-09 14:42:56 -0700520void BBinder::setExtension(const sp<IBinder>& extension) {
Steven Morelandd37e6dd2021-06-16 22:07:51 +0000521 LOG_ALWAYS_FATAL_IF(mParceled,
522 "setExtension() should not be called after a binder object "
523 "is parceled/sent to another process");
Kalesh Singhd67c8e82020-12-29 15:46:25 -0500524
Steven Morelandb8ad08d2019-08-09 14:42:56 -0700525 Extras* e = getOrCreateExtras();
526 e->mExtension = extension;
527}
528
Kalesh Singhd67c8e82020-12-29 15:46:25 -0500529bool BBinder::wasParceled() {
530 return mParceled;
531}
532
533void BBinder::setParceled() {
534 mParceled = true;
535}
536
Yifan Hong84bedeb2021-04-21 21:37:17 -0700537status_t BBinder::setRpcClientDebug(const Parcel& data) {
Steven Morelandd9a74002022-06-02 00:03:18 +0000538 if (!kEnableRpcDevServers) {
Yifan Hong84bedeb2021-04-21 21:37:17 -0700539 ALOGW("%s: disallowed because RPC is not enabled", __PRETTY_FUNCTION__);
540 return INVALID_OPERATION;
541 }
Steven Moreland32150282021-11-12 22:54:53 +0000542 if (!kEnableKernelIpc) {
543 ALOGW("setRpcClientDebug disallowed because kernel binder is not enabled");
544 return INVALID_OPERATION;
545 }
Yifan Hong84bedeb2021-04-21 21:37:17 -0700546 uid_t uid = IPCThreadState::self()->getCallingUid();
547 if (uid != AID_ROOT) {
548 ALOGE("%s: not allowed because client %" PRIu32 " is not root", __PRETTY_FUNCTION__, uid);
549 return PERMISSION_DENIED;
550 }
551 status_t status;
552 bool hasSocketFd;
553 android::base::unique_fd clientFd;
Yifan Hong84bedeb2021-04-21 21:37:17 -0700554
555 if (status = data.readBool(&hasSocketFd); status != OK) return status;
556 if (hasSocketFd) {
557 if (status = data.readUniqueFileDescriptor(&clientFd); status != OK) return status;
558 }
Yifan Hong02530ec2021-06-10 13:38:38 -0700559 sp<IBinder> keepAliveBinder;
560 if (status = data.readNullableStrongBinder(&keepAliveBinder); status != OK) return status;
Yifan Hong84bedeb2021-04-21 21:37:17 -0700561
Yifan Hong02530ec2021-06-10 13:38:38 -0700562 return setRpcClientDebug(std::move(clientFd), keepAliveBinder);
Yifan Hong84bedeb2021-04-21 21:37:17 -0700563}
564
Yifan Hong02530ec2021-06-10 13:38:38 -0700565status_t BBinder::setRpcClientDebug(android::base::unique_fd socketFd,
566 const sp<IBinder>& keepAliveBinder) {
Steven Morelandd9a74002022-06-02 00:03:18 +0000567 if (!kEnableRpcDevServers) {
Yifan Hong84bedeb2021-04-21 21:37:17 -0700568 ALOGW("%s: disallowed because RPC is not enabled", __PRETTY_FUNCTION__);
569 return INVALID_OPERATION;
570 }
Steven Moreland32150282021-11-12 22:54:53 +0000571 if (!kEnableKernelIpc) {
572 ALOGW("setRpcClientDebug disallowed because kernel binder is not enabled");
573 return INVALID_OPERATION;
574 }
Yifan Hong84bedeb2021-04-21 21:37:17 -0700575
576 const int socketFdForPrint = socketFd.get();
Yifan Hong34823232021-06-07 17:23:00 -0700577 LOG_RPC_DETAIL("%s(fd=%d)", __PRETTY_FUNCTION__, socketFdForPrint);
Yifan Hong84bedeb2021-04-21 21:37:17 -0700578
579 if (!socketFd.ok()) {
580 ALOGE("%s: No socket FD provided.", __PRETTY_FUNCTION__);
581 return BAD_VALUE;
582 }
Yifan Hong84bedeb2021-04-21 21:37:17 -0700583
Yifan Hong02530ec2021-06-10 13:38:38 -0700584 if (keepAliveBinder == nullptr) {
585 ALOGE("%s: No keepAliveBinder provided.", __PRETTY_FUNCTION__);
586 return UNEXPECTED_NULL;
587 }
588
Elie Kheirallah47431c12022-04-21 23:46:17 +0000589 size_t binderThreadPoolMaxCount = ProcessState::self()->getThreadPoolMaxTotalThreadCount();
Yifan Hong84bedeb2021-04-21 21:37:17 -0700590 if (binderThreadPoolMaxCount <= 1) {
591 ALOGE("%s: ProcessState thread pool max count is %zu. RPC is disabled for this service "
592 "because RPC requires the service to support multithreading.",
593 __PRETTY_FUNCTION__, binderThreadPoolMaxCount);
594 return INVALID_OPERATION;
595 }
596
Yifan Hong8b890852021-06-10 13:44:09 -0700597 // Weak ref to avoid circular dependency:
598 // BBinder -> RpcServerLink ----> RpcServer -X-> BBinder
599 // `-X-> BBinder
600 auto weakThis = wp<BBinder>::fromExisting(this);
601
Yifan Hong84bedeb2021-04-21 21:37:17 -0700602 Extras* e = getOrCreateExtras();
603 AutoMutex _l(e->mLock);
Yifan Hong8b890852021-06-10 13:44:09 -0700604 auto rpcServer = RpcServer::make();
605 LOG_ALWAYS_FATAL_IF(rpcServer == nullptr, "RpcServer::make returns null");
Yifan Hong8b890852021-06-10 13:44:09 -0700606 auto link = sp<RpcServerLink>::make(rpcServer, keepAliveBinder, weakThis);
607 if (auto status = keepAliveBinder->linkToDeath(link, nullptr, 0); status != OK) {
608 ALOGE("%s: keepAliveBinder->linkToDeath returns %s", __PRETTY_FUNCTION__,
609 statusToString(status).c_str());
610 return status;
Yifan Hong84bedeb2021-04-21 21:37:17 -0700611 }
Yifan Hong8b890852021-06-10 13:44:09 -0700612 rpcServer->setRootObjectWeak(weakThis);
Steven Moreland2372f9d2021-08-05 15:42:01 -0700613 if (auto status = rpcServer->setupExternalServer(std::move(socketFd)); status != OK) {
614 return status;
615 }
Yifan Hong8b890852021-06-10 13:44:09 -0700616 rpcServer->setMaxThreads(binderThreadPoolMaxCount);
617 rpcServer->start();
618 e->mRpcServerLinks.emplace(link);
Yifan Hong34823232021-06-07 17:23:00 -0700619 LOG_RPC_DETAIL("%s(fd=%d) successful", __PRETTY_FUNCTION__, socketFdForPrint);
Yifan Hong84bedeb2021-04-21 21:37:17 -0700620 return OK;
621}
622
Yifan Hong8b890852021-06-10 13:44:09 -0700623void BBinder::removeRpcServerLink(const sp<RpcServerLink>& link) {
624 Extras* e = mExtras.load(std::memory_order_acquire);
625 if (!e) return;
626 AutoMutex _l(e->mLock);
627 (void)e->mRpcServerLinks.erase(link);
628}
629
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800630BBinder::~BBinder()
631{
Steven Moreland1bd2bc72022-07-29 20:29:40 +0000632 if (!wasParceled()) {
633 if (getExtension()) {
634 ALOGW("Binder %p destroyed with extension attached before being parceled.", this);
635 }
636 if (isRequestingSid()) {
637 ALOGW("Binder %p destroyed when requesting SID before being parceled.", this);
638 }
639 if (isInheritRt()) {
640 ALOGW("Binder %p destroyed after setInheritRt before being parceled.", this);
641 }
Andrei Homescu40525502022-08-02 01:23:22 +0000642#ifdef __linux__
Steven Moreland1bd2bc72022-07-29 20:29:40 +0000643 if (getMinSchedulerPolicy() != SCHED_NORMAL) {
644 ALOGW("Binder %p destroyed after setMinSchedulerPolicy before being parceled.", this);
645 }
646 if (getMinSchedulerPriority() != 0) {
647 ALOGW("Binder %p destroyed after setMinSchedulerPolicy before being parceled.", this);
648 }
Andrei Homescu40525502022-08-02 01:23:22 +0000649#endif // __linux__
Steven Moreland55a12542022-03-31 21:53:11 +0000650 }
651
Bailey Forrest6913c462015-08-18 17:15:10 -0700652 Extras* e = mExtras.load(std::memory_order_relaxed);
Hans Boehm3effaba2014-08-12 22:56:00 +0000653 if (e) delete e;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800654}
655
656
Jiyong Parkb86c8662018-10-29 23:01:57 +0900657// NOLINTNEXTLINE(google-default-arguments)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800658status_t BBinder::onTransact(
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800659 uint32_t code, const Parcel& data, Parcel* reply, uint32_t /*flags*/)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800660{
661 switch (code) {
662 case INTERFACE_TRANSACTION:
Jiyong Park5970d0a2022-03-08 16:56:13 +0900663 CHECK(reply != nullptr);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800664 reply->writeString16(getInterfaceDescriptor());
665 return NO_ERROR;
666
667 case DUMP_TRANSACTION: {
668 int fd = data.readFileDescriptor();
669 int argc = data.readInt32();
670 Vector<String16> args;
671 for (int i = 0; i < argc && data.dataAvail() > 0; i++) {
672 args.add(data.readString16());
673 }
674 return dump(fd, args);
675 }
Dianne Hackborn555f89d2012-05-08 18:54:22 -0700676
Dianne Hackborn23eb1e22015-10-07 17:35:27 -0700677 case SHELL_COMMAND_TRANSACTION: {
678 int in = data.readFileDescriptor();
679 int out = data.readFileDescriptor();
680 int err = data.readFileDescriptor();
681 int argc = data.readInt32();
682 Vector<String16> args;
683 for (int i = 0; i < argc && data.dataAvail() > 0; i++) {
684 args.add(data.readString16());
685 }
Andrei Homescua543a842022-07-07 22:17:55 +0000686 sp<IBinder> shellCallbackBinder = data.readStrongBinder();
Dianne Hackborn23eb1e22015-10-07 17:35:27 -0700687 sp<IResultReceiver> resultReceiver = IResultReceiver::asInterface(
688 data.readStrongBinder());
689
Dianne Hackbornf2bf93b2015-10-14 15:13:02 -0700690 // XXX can't add virtuals until binaries are updated.
Andrei Homescua543a842022-07-07 22:17:55 +0000691 // sp<IShellCallback> shellCallback = IShellCallback::asInterface(
692 // shellCallbackBinder);
693 // return shellCommand(in, out, err, args, resultReceiver);
Christopher Wiley0a9a1c12016-07-20 08:28:14 -0700694 (void)in;
695 (void)out;
696 (void)err;
697
Yi Kongfdd8da92018-06-07 17:52:27 -0700698 if (resultReceiver != nullptr) {
Dianne Hackbornf2bf93b2015-10-14 15:13:02 -0700699 resultReceiver->send(INVALID_OPERATION);
700 }
Martijn Coenenaa6ee992017-08-17 15:38:08 +0200701
702 return NO_ERROR;
Dianne Hackborn23eb1e22015-10-07 17:35:27 -0700703 }
704
Dianne Hackborn555f89d2012-05-08 18:54:22 -0700705 case SYSPROPS_TRANSACTION: {
706 report_sysprop_change();
707 return NO_ERROR;
708 }
709
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800710 default:
711 return UNKNOWN_TRANSACTION;
712 }
713}
714
Steven Morelandf0212002018-12-26 13:59:23 -0800715BBinder::Extras* BBinder::getOrCreateExtras()
716{
717 Extras* e = mExtras.load(std::memory_order_acquire);
718
719 if (!e) {
720 e = new Extras;
721 Extras* expected = nullptr;
722 if (!mExtras.compare_exchange_strong(expected, e,
723 std::memory_order_release,
724 std::memory_order_acquire)) {
725 delete e;
726 e = expected; // Filled in by CAS
727 }
728 if (e == nullptr) return nullptr; // out of memory
729 }
730
731 return e;
732}
733
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800734// ---------------------------------------------------------------------------
735
736enum {
737 // This is used to transfer ownership of the remote binder from
738 // the BpRefBase object holding it (when it is constructed), to the
739 // owner of the BpRefBase object when it first acquires that BpRefBase.
740 kRemoteAcquired = 0x00000001
741};
742
743BpRefBase::BpRefBase(const sp<IBinder>& o)
Yi Kongfdd8da92018-06-07 17:52:27 -0700744 : mRemote(o.get()), mRefs(nullptr), mState(0)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800745{
746 extendObjectLifetime(OBJECT_LIFETIME_WEAK);
747
748 if (mRemote) {
749 mRemote->incStrong(this); // Removed on first IncStrong().
750 mRefs = mRemote->createWeak(this); // Held for our entire lifetime.
751 }
752}
753
754BpRefBase::~BpRefBase()
755{
756 if (mRemote) {
Bailey Forrest6913c462015-08-18 17:15:10 -0700757 if (!(mState.load(std::memory_order_relaxed)&kRemoteAcquired)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800758 mRemote->decStrong(this);
759 }
760 mRefs->decWeak(this);
761 }
762}
763
764void BpRefBase::onFirstRef()
765{
Bailey Forrest6913c462015-08-18 17:15:10 -0700766 mState.fetch_or(kRemoteAcquired, std::memory_order_relaxed);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800767}
768
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800769void BpRefBase::onLastStrongRef(const void* /*id*/)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800770{
771 if (mRemote) {
772 mRemote->decStrong(this);
773 }
774}
775
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800776bool BpRefBase::onIncStrongAttempted(uint32_t /*flags*/, const void* /*id*/)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800777{
778 return mRemote ? mRefs->attemptIncStrong(this) : false;
779}
780
781// ---------------------------------------------------------------------------
782
Steven Moreland61ff8492019-09-26 16:05:45 -0700783} // namespace android