blob: f2d223dad38cb540905b4318ccb1cd109f9a213a [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>
Steven Morelandd1387982019-07-24 00:12:19 +000020#include <utils/misc.h>
Mathias Agopianc5b2c0b2009-05-19 19:08:10 -070021#include <binder/BpBinder.h>
22#include <binder/IInterface.h>
Dianne Hackborn23eb1e22015-10-07 17:35:27 -070023#include <binder/IResultReceiver.h>
Dianne Hackborn1941a402016-08-29 12:30:43 -070024#include <binder/IShellCallback.h>
Mathias Agopianc5b2c0b2009-05-19 19:08:10 -070025#include <binder/Parcel.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080026
Steven Morelandbf1915b2020-07-16 22:43:02 +000027#include <linux/sched.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080028#include <stdio.h>
29
30namespace android {
31
32// ---------------------------------------------------------------------------
33
Mathias Agopian83c04462009-05-22 19:00:22 -070034IBinder::IBinder()
35 : RefBase()
36{
37}
38
39IBinder::~IBinder()
40{
41}
42
43// ---------------------------------------------------------------------------
44
Colin Cross6f4f3ab2014-02-05 17:42:44 -080045sp<IInterface> IBinder::queryLocalInterface(const String16& /*descriptor*/)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080046{
Yi Kongfdd8da92018-06-07 17:52:27 -070047 return nullptr;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080048}
49
50BBinder* IBinder::localBinder()
51{
Yi Kongfdd8da92018-06-07 17:52:27 -070052 return nullptr;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080053}
54
55BpBinder* IBinder::remoteBinder()
56{
Yi Kongfdd8da92018-06-07 17:52:27 -070057 return nullptr;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080058}
59
60bool IBinder::checkSubclass(const void* /*subclassID*/) const
61{
62 return false;
63}
64
Dianne Hackborn23eb1e22015-10-07 17:35:27 -070065
Dianne Hackbornf2bf93b2015-10-14 15:13:02 -070066status_t IBinder::shellCommand(const sp<IBinder>& target, int in, int out, int err,
Dianne Hackborn1941a402016-08-29 12:30:43 -070067 Vector<String16>& args, const sp<IShellCallback>& callback,
68 const sp<IResultReceiver>& resultReceiver)
Dianne Hackborn23eb1e22015-10-07 17:35:27 -070069{
Dianne Hackbornf2bf93b2015-10-14 15:13:02 -070070 Parcel send;
71 Parcel reply;
72 send.writeFileDescriptor(in);
73 send.writeFileDescriptor(out);
74 send.writeFileDescriptor(err);
75 const size_t numArgs = args.size();
76 send.writeInt32(numArgs);
77 for (size_t i = 0; i < numArgs; i++) {
78 send.writeString16(args[i]);
Dianne Hackborn23eb1e22015-10-07 17:35:27 -070079 }
Yi Kongfdd8da92018-06-07 17:52:27 -070080 send.writeStrongBinder(callback != nullptr ? IInterface::asBinder(callback) : nullptr);
81 send.writeStrongBinder(resultReceiver != nullptr ? IInterface::asBinder(resultReceiver) : nullptr);
Dianne Hackbornf2bf93b2015-10-14 15:13:02 -070082 return target->transact(SHELL_COMMAND_TRANSACTION, send, &reply);
Dianne Hackborn23eb1e22015-10-07 17:35:27 -070083}
84
Steven Morelandb8ad08d2019-08-09 14:42:56 -070085status_t IBinder::getExtension(sp<IBinder>* out) {
86 BBinder* local = this->localBinder();
87 if (local != nullptr) {
88 *out = local->getExtension();
89 return OK;
90 }
91
92 BpBinder* proxy = this->remoteBinder();
93 LOG_ALWAYS_FATAL_IF(proxy == nullptr);
94
95 Parcel data;
96 Parcel reply;
97 status_t status = transact(EXTENSION_TRANSACTION, data, &reply);
98 if (status != OK) return status;
99
100 return reply.readNullableStrongBinder(out);
101}
102
Steven Moreland86080b82019-09-23 15:41:18 -0700103status_t IBinder::getDebugPid(pid_t* out) {
104 BBinder* local = this->localBinder();
105 if (local != nullptr) {
106 *out = local->getDebugPid();
107 return OK;
108 }
109
110 BpBinder* proxy = this->remoteBinder();
111 LOG_ALWAYS_FATAL_IF(proxy == nullptr);
112
113 Parcel data;
114 Parcel reply;
115 status_t status = transact(DEBUG_PID_TRANSACTION, data, &reply);
116 if (status != OK) return status;
117
118 int32_t pid;
119 status = reply.readInt32(&pid);
120 if (status != OK) return status;
121
122 if (pid < 0 || pid > std::numeric_limits<pid_t>::max()) {
123 return BAD_VALUE;
124 }
125 *out = pid;
126 return OK;
127}
128
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800129// ---------------------------------------------------------------------------
130
131class BBinder::Extras
132{
133public:
Steven Morelandf0212002018-12-26 13:59:23 -0800134 // unlocked objects
135 bool mRequestingSid = false;
Steven Morelandb8ad08d2019-08-09 14:42:56 -0700136 sp<IBinder> mExtension;
Steven Morelandbf1915b2020-07-16 22:43:02 +0000137 int mPolicy = SCHED_NORMAL;
138 int mPriority = 0;
Steven Morelandf0212002018-12-26 13:59:23 -0800139
140 // for below objects
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800141 Mutex mLock;
142 BpBinder::ObjectManager mObjects;
143};
144
145// ---------------------------------------------------------------------------
146
Steven Morelanda7fb0182020-02-26 16:02:08 -0800147BBinder::BBinder() : mExtras(nullptr), mStability(0)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800148{
149}
150
151bool BBinder::isBinderAlive() const
152{
153 return true;
154}
155
156status_t BBinder::pingBinder()
157{
158 return NO_ERROR;
159}
160
Mathias Agopian83c04462009-05-22 19:00:22 -0700161const String16& BBinder::getInterfaceDescriptor() const
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800162{
Dan Egnor386a3322010-05-06 00:55:09 -0700163 // This is a local static rather than a global static,
164 // to avoid static initializer ordering issues.
165 static String16 sEmptyDescriptor;
Steve Block32397c12012-01-05 23:22:43 +0000166 ALOGW("reached BBinder::getInterfaceDescriptor (this=%p)", this);
Mathias Agopian83c04462009-05-22 19:00:22 -0700167 return sEmptyDescriptor;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800168}
169
Jiyong Parkb86c8662018-10-29 23:01:57 +0900170// NOLINTNEXTLINE(google-default-arguments)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800171status_t BBinder::transact(
172 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
173{
174 data.setDataPosition(0);
175
Steven Morelandf183fdd2020-10-27 00:12:12 +0000176 if (reply != nullptr && (flags & FLAG_CLEAR_BUF)) {
177 reply->markSensitive();
178 }
179
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800180 status_t err = NO_ERROR;
181 switch (code) {
182 case PING_TRANSACTION:
Steven Moreland6e69d652019-07-10 14:17:55 -0700183 err = pingBinder();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800184 break;
Steven Morelandb8ad08d2019-08-09 14:42:56 -0700185 case EXTENSION_TRANSACTION:
186 err = reply->writeStrongBinder(getExtension());
187 break;
Steven Moreland86080b82019-09-23 15:41:18 -0700188 case DEBUG_PID_TRANSACTION:
189 err = reply->writeInt32(getDebugPid());
190 break;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800191 default:
192 err = onTransact(code, data, reply, flags);
193 break;
194 }
195
Steven Morelanda86a3562019-08-01 23:28:34 +0000196 // In case this is being transacted on in the same process.
Yi Kongfdd8da92018-06-07 17:52:27 -0700197 if (reply != nullptr) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800198 reply->setDataPosition(0);
199 }
200
201 return err;
202}
203
Jiyong Parkb86c8662018-10-29 23:01:57 +0900204// NOLINTNEXTLINE(google-default-arguments)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800205status_t BBinder::linkToDeath(
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800206 const sp<DeathRecipient>& /*recipient*/, void* /*cookie*/,
207 uint32_t /*flags*/)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800208{
209 return INVALID_OPERATION;
210}
211
Jiyong Parkb86c8662018-10-29 23:01:57 +0900212// NOLINTNEXTLINE(google-default-arguments)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800213status_t BBinder::unlinkToDeath(
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800214 const wp<DeathRecipient>& /*recipient*/, void* /*cookie*/,
215 uint32_t /*flags*/, wp<DeathRecipient>* /*outRecipient*/)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800216{
217 return INVALID_OPERATION;
218}
219
Dianne Hackborn23eb1e22015-10-07 17:35:27 -0700220status_t BBinder::dump(int /*fd*/, const Vector<String16>& /*args*/)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800221{
222 return NO_ERROR;
223}
224
225void BBinder::attachObject(
226 const void* objectID, void* object, void* cleanupCookie,
227 object_cleanup_func func)
228{
Steven Morelandf0212002018-12-26 13:59:23 -0800229 Extras* e = getOrCreateExtras();
230 if (!e) return; // out of memory
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800231
232 AutoMutex _l(e->mLock);
233 e->mObjects.attach(objectID, object, cleanupCookie, func);
234}
235
236void* BBinder::findObject(const void* objectID) const
237{
Bailey Forrest6913c462015-08-18 17:15:10 -0700238 Extras* e = mExtras.load(std::memory_order_acquire);
Yi Kongfdd8da92018-06-07 17:52:27 -0700239 if (!e) return nullptr;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800240
241 AutoMutex _l(e->mLock);
242 return e->mObjects.find(objectID);
243}
244
245void BBinder::detachObject(const void* objectID)
246{
Bailey Forrest6913c462015-08-18 17:15:10 -0700247 Extras* e = mExtras.load(std::memory_order_acquire);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800248 if (!e) return;
249
250 AutoMutex _l(e->mLock);
251 e->mObjects.detach(objectID);
252}
253
254BBinder* BBinder::localBinder()
255{
256 return this;
257}
258
Steven Morelandf0212002018-12-26 13:59:23 -0800259bool BBinder::isRequestingSid()
260{
261 Extras* e = mExtras.load(std::memory_order_acquire);
262
263 return e && e->mRequestingSid;
264}
265
266void BBinder::setRequestingSid(bool requestingSid)
267{
268 Extras* e = mExtras.load(std::memory_order_acquire);
269
270 if (!e) {
271 // default is false. Most things don't need sids, so avoiding allocations when possible.
272 if (!requestingSid) {
273 return;
274 }
275
276 e = getOrCreateExtras();
277 if (!e) return; // out of memory
278 }
279
Steven Moreland3668be62019-02-08 17:56:55 -0800280 e->mRequestingSid = requestingSid;
Steven Morelandf0212002018-12-26 13:59:23 -0800281}
282
Steven Morelandb8ad08d2019-08-09 14:42:56 -0700283sp<IBinder> BBinder::getExtension() {
284 Extras* e = mExtras.load(std::memory_order_acquire);
285 if (e == nullptr) return nullptr;
286 return e->mExtension;
287}
288
Steven Morelandbf1915b2020-07-16 22:43:02 +0000289void BBinder::setMinSchedulerPolicy(int policy, int priority) {
290 switch (policy) {
291 case SCHED_NORMAL:
292 LOG_ALWAYS_FATAL_IF(priority < -20 || priority > 19, "Invalid priority for SCHED_NORMAL: %d", priority);
293 break;
294 case SCHED_RR:
295 case SCHED_FIFO:
296 LOG_ALWAYS_FATAL_IF(priority < 1 || priority > 99, "Invalid priority for sched %d: %d", policy, priority);
297 break;
298 default:
299 LOG_ALWAYS_FATAL("Unrecognized scheduling policy: %d", policy);
300 }
301
302 Extras* e = mExtras.load(std::memory_order_acquire);
303
304 if (e == nullptr) {
305 // Avoid allocations if called with default.
306 if (policy == SCHED_NORMAL && priority == 0) {
307 return;
308 }
309
310 e = getOrCreateExtras();
311 if (!e) return; // out of memory
312 }
313
314 e->mPolicy = policy;
315 e->mPriority = priority;
316}
317
318int BBinder::getMinSchedulerPolicy() {
319 Extras* e = mExtras.load(std::memory_order_acquire);
320 if (e == nullptr) return SCHED_NORMAL;
321 return e->mPolicy;
322}
323
324int BBinder::getMinSchedulerPriority() {
325 Extras* e = mExtras.load(std::memory_order_acquire);
326 if (e == nullptr) return 0;
327 return e->mPriority;
328}
329
Steven Moreland86080b82019-09-23 15:41:18 -0700330pid_t BBinder::getDebugPid() {
331 return getpid();
332}
333
Steven Morelandb8ad08d2019-08-09 14:42:56 -0700334void BBinder::setExtension(const sp<IBinder>& extension) {
335 Extras* e = getOrCreateExtras();
336 e->mExtension = extension;
337}
338
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800339BBinder::~BBinder()
340{
Bailey Forrest6913c462015-08-18 17:15:10 -0700341 Extras* e = mExtras.load(std::memory_order_relaxed);
Hans Boehm3effaba2014-08-12 22:56:00 +0000342 if (e) delete e;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800343}
344
345
Jiyong Parkb86c8662018-10-29 23:01:57 +0900346// NOLINTNEXTLINE(google-default-arguments)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800347status_t BBinder::onTransact(
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800348 uint32_t code, const Parcel& data, Parcel* reply, uint32_t /*flags*/)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800349{
350 switch (code) {
351 case INTERFACE_TRANSACTION:
352 reply->writeString16(getInterfaceDescriptor());
353 return NO_ERROR;
354
355 case DUMP_TRANSACTION: {
356 int fd = data.readFileDescriptor();
357 int argc = data.readInt32();
358 Vector<String16> args;
359 for (int i = 0; i < argc && data.dataAvail() > 0; i++) {
360 args.add(data.readString16());
361 }
362 return dump(fd, args);
363 }
Dianne Hackborn555f89d2012-05-08 18:54:22 -0700364
Dianne Hackborn23eb1e22015-10-07 17:35:27 -0700365 case SHELL_COMMAND_TRANSACTION: {
366 int in = data.readFileDescriptor();
367 int out = data.readFileDescriptor();
368 int err = data.readFileDescriptor();
369 int argc = data.readInt32();
370 Vector<String16> args;
371 for (int i = 0; i < argc && data.dataAvail() > 0; i++) {
372 args.add(data.readString16());
373 }
Dianne Hackborn1941a402016-08-29 12:30:43 -0700374 sp<IShellCallback> shellCallback = IShellCallback::asInterface(
375 data.readStrongBinder());
Dianne Hackborn23eb1e22015-10-07 17:35:27 -0700376 sp<IResultReceiver> resultReceiver = IResultReceiver::asInterface(
377 data.readStrongBinder());
378
Dianne Hackbornf2bf93b2015-10-14 15:13:02 -0700379 // XXX can't add virtuals until binaries are updated.
380 //return shellCommand(in, out, err, args, resultReceiver);
Christopher Wiley0a9a1c12016-07-20 08:28:14 -0700381 (void)in;
382 (void)out;
383 (void)err;
384
Yi Kongfdd8da92018-06-07 17:52:27 -0700385 if (resultReceiver != nullptr) {
Dianne Hackbornf2bf93b2015-10-14 15:13:02 -0700386 resultReceiver->send(INVALID_OPERATION);
387 }
Martijn Coenenaa6ee992017-08-17 15:38:08 +0200388
389 return NO_ERROR;
Dianne Hackborn23eb1e22015-10-07 17:35:27 -0700390 }
391
Dianne Hackborn555f89d2012-05-08 18:54:22 -0700392 case SYSPROPS_TRANSACTION: {
393 report_sysprop_change();
394 return NO_ERROR;
395 }
396
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800397 default:
398 return UNKNOWN_TRANSACTION;
399 }
400}
401
Steven Morelandf0212002018-12-26 13:59:23 -0800402BBinder::Extras* BBinder::getOrCreateExtras()
403{
404 Extras* e = mExtras.load(std::memory_order_acquire);
405
406 if (!e) {
407 e = new Extras;
408 Extras* expected = nullptr;
409 if (!mExtras.compare_exchange_strong(expected, e,
410 std::memory_order_release,
411 std::memory_order_acquire)) {
412 delete e;
413 e = expected; // Filled in by CAS
414 }
415 if (e == nullptr) return nullptr; // out of memory
416 }
417
418 return e;
419}
420
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800421// ---------------------------------------------------------------------------
422
423enum {
424 // This is used to transfer ownership of the remote binder from
425 // the BpRefBase object holding it (when it is constructed), to the
426 // owner of the BpRefBase object when it first acquires that BpRefBase.
427 kRemoteAcquired = 0x00000001
428};
429
430BpRefBase::BpRefBase(const sp<IBinder>& o)
Yi Kongfdd8da92018-06-07 17:52:27 -0700431 : mRemote(o.get()), mRefs(nullptr), mState(0)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800432{
433 extendObjectLifetime(OBJECT_LIFETIME_WEAK);
434
435 if (mRemote) {
436 mRemote->incStrong(this); // Removed on first IncStrong().
437 mRefs = mRemote->createWeak(this); // Held for our entire lifetime.
438 }
439}
440
441BpRefBase::~BpRefBase()
442{
443 if (mRemote) {
Bailey Forrest6913c462015-08-18 17:15:10 -0700444 if (!(mState.load(std::memory_order_relaxed)&kRemoteAcquired)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800445 mRemote->decStrong(this);
446 }
447 mRefs->decWeak(this);
448 }
449}
450
451void BpRefBase::onFirstRef()
452{
Bailey Forrest6913c462015-08-18 17:15:10 -0700453 mState.fetch_or(kRemoteAcquired, std::memory_order_relaxed);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800454}
455
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800456void BpRefBase::onLastStrongRef(const void* /*id*/)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800457{
458 if (mRemote) {
459 mRemote->decStrong(this);
460 }
461}
462
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800463bool BpRefBase::onIncStrongAttempted(uint32_t /*flags*/, const void* /*id*/)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800464{
465 return mRemote ? mRefs->attemptIncStrong(this) : false;
466}
467
468// ---------------------------------------------------------------------------
469
Steven Moreland61ff8492019-09-26 16:05:45 -0700470} // namespace android