blob: d964d255fed5eb54407a8b82e22107587e88d9ed [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 Morelandcf03cf12020-12-04 02:58:40 +0000136 bool mInheritRt = false;
Steven Morelandb8ad08d2019-08-09 14:42:56 -0700137 sp<IBinder> mExtension;
Steven Morelandbf1915b2020-07-16 22:43:02 +0000138 int mPolicy = SCHED_NORMAL;
139 int mPriority = 0;
Steven Morelandf0212002018-12-26 13:59:23 -0800140
141 // for below objects
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800142 Mutex mLock;
143 BpBinder::ObjectManager mObjects;
144};
145
146// ---------------------------------------------------------------------------
147
Steven Morelanda7fb0182020-02-26 16:02:08 -0800148BBinder::BBinder() : mExtras(nullptr), mStability(0)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800149{
150}
151
152bool BBinder::isBinderAlive() const
153{
154 return true;
155}
156
157status_t BBinder::pingBinder()
158{
159 return NO_ERROR;
160}
161
Mathias Agopian83c04462009-05-22 19:00:22 -0700162const String16& BBinder::getInterfaceDescriptor() const
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800163{
Dan Egnor386a3322010-05-06 00:55:09 -0700164 // This is a local static rather than a global static,
165 // to avoid static initializer ordering issues.
166 static String16 sEmptyDescriptor;
Steve Block32397c12012-01-05 23:22:43 +0000167 ALOGW("reached BBinder::getInterfaceDescriptor (this=%p)", this);
Mathias Agopian83c04462009-05-22 19:00:22 -0700168 return sEmptyDescriptor;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800169}
170
Jiyong Parkb86c8662018-10-29 23:01:57 +0900171// NOLINTNEXTLINE(google-default-arguments)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800172status_t BBinder::transact(
173 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
174{
175 data.setDataPosition(0);
176
Steven Morelandf183fdd2020-10-27 00:12:12 +0000177 if (reply != nullptr && (flags & FLAG_CLEAR_BUF)) {
178 reply->markSensitive();
179 }
180
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800181 status_t err = NO_ERROR;
182 switch (code) {
183 case PING_TRANSACTION:
Steven Moreland6e69d652019-07-10 14:17:55 -0700184 err = pingBinder();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800185 break;
Steven Morelandb8ad08d2019-08-09 14:42:56 -0700186 case EXTENSION_TRANSACTION:
187 err = reply->writeStrongBinder(getExtension());
188 break;
Steven Moreland86080b82019-09-23 15:41:18 -0700189 case DEBUG_PID_TRANSACTION:
190 err = reply->writeInt32(getDebugPid());
191 break;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800192 default:
193 err = onTransact(code, data, reply, flags);
194 break;
195 }
196
Steven Morelanda86a3562019-08-01 23:28:34 +0000197 // In case this is being transacted on in the same process.
Yi Kongfdd8da92018-06-07 17:52:27 -0700198 if (reply != nullptr) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800199 reply->setDataPosition(0);
200 }
201
202 return err;
203}
204
Jiyong Parkb86c8662018-10-29 23:01:57 +0900205// NOLINTNEXTLINE(google-default-arguments)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800206status_t BBinder::linkToDeath(
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800207 const sp<DeathRecipient>& /*recipient*/, void* /*cookie*/,
208 uint32_t /*flags*/)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800209{
210 return INVALID_OPERATION;
211}
212
Jiyong Parkb86c8662018-10-29 23:01:57 +0900213// NOLINTNEXTLINE(google-default-arguments)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800214status_t BBinder::unlinkToDeath(
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800215 const wp<DeathRecipient>& /*recipient*/, void* /*cookie*/,
216 uint32_t /*flags*/, wp<DeathRecipient>* /*outRecipient*/)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800217{
218 return INVALID_OPERATION;
219}
220
Dianne Hackborn23eb1e22015-10-07 17:35:27 -0700221status_t BBinder::dump(int /*fd*/, const Vector<String16>& /*args*/)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800222{
223 return NO_ERROR;
224}
225
226void BBinder::attachObject(
227 const void* objectID, void* object, void* cleanupCookie,
228 object_cleanup_func func)
229{
Steven Morelandf0212002018-12-26 13:59:23 -0800230 Extras* e = getOrCreateExtras();
231 if (!e) return; // out of memory
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800232
233 AutoMutex _l(e->mLock);
234 e->mObjects.attach(objectID, object, cleanupCookie, func);
235}
236
237void* BBinder::findObject(const void* objectID) const
238{
Bailey Forrest6913c462015-08-18 17:15:10 -0700239 Extras* e = mExtras.load(std::memory_order_acquire);
Yi Kongfdd8da92018-06-07 17:52:27 -0700240 if (!e) return nullptr;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800241
242 AutoMutex _l(e->mLock);
243 return e->mObjects.find(objectID);
244}
245
246void BBinder::detachObject(const void* objectID)
247{
Bailey Forrest6913c462015-08-18 17:15:10 -0700248 Extras* e = mExtras.load(std::memory_order_acquire);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800249 if (!e) return;
250
251 AutoMutex _l(e->mLock);
252 e->mObjects.detach(objectID);
253}
254
255BBinder* BBinder::localBinder()
256{
257 return this;
258}
259
Steven Morelandf0212002018-12-26 13:59:23 -0800260bool BBinder::isRequestingSid()
261{
262 Extras* e = mExtras.load(std::memory_order_acquire);
263
264 return e && e->mRequestingSid;
265}
266
267void BBinder::setRequestingSid(bool requestingSid)
268{
269 Extras* e = mExtras.load(std::memory_order_acquire);
270
271 if (!e) {
272 // default is false. Most things don't need sids, so avoiding allocations when possible.
273 if (!requestingSid) {
274 return;
275 }
276
277 e = getOrCreateExtras();
278 if (!e) return; // out of memory
279 }
280
Steven Moreland3668be62019-02-08 17:56:55 -0800281 e->mRequestingSid = requestingSid;
Steven Morelandf0212002018-12-26 13:59:23 -0800282}
283
Steven Morelandb8ad08d2019-08-09 14:42:56 -0700284sp<IBinder> BBinder::getExtension() {
285 Extras* e = mExtras.load(std::memory_order_acquire);
286 if (e == nullptr) return nullptr;
287 return e->mExtension;
288}
289
Steven Morelandbf1915b2020-07-16 22:43:02 +0000290void BBinder::setMinSchedulerPolicy(int policy, int priority) {
291 switch (policy) {
292 case SCHED_NORMAL:
293 LOG_ALWAYS_FATAL_IF(priority < -20 || priority > 19, "Invalid priority for SCHED_NORMAL: %d", priority);
294 break;
295 case SCHED_RR:
296 case SCHED_FIFO:
297 LOG_ALWAYS_FATAL_IF(priority < 1 || priority > 99, "Invalid priority for sched %d: %d", policy, priority);
298 break;
299 default:
300 LOG_ALWAYS_FATAL("Unrecognized scheduling policy: %d", policy);
301 }
302
303 Extras* e = mExtras.load(std::memory_order_acquire);
304
305 if (e == nullptr) {
306 // Avoid allocations if called with default.
307 if (policy == SCHED_NORMAL && priority == 0) {
308 return;
309 }
310
311 e = getOrCreateExtras();
312 if (!e) return; // out of memory
313 }
314
315 e->mPolicy = policy;
316 e->mPriority = priority;
317}
318
319int BBinder::getMinSchedulerPolicy() {
320 Extras* e = mExtras.load(std::memory_order_acquire);
321 if (e == nullptr) return SCHED_NORMAL;
322 return e->mPolicy;
323}
324
325int BBinder::getMinSchedulerPriority() {
326 Extras* e = mExtras.load(std::memory_order_acquire);
327 if (e == nullptr) return 0;
328 return e->mPriority;
329}
330
Steven Morelandcf03cf12020-12-04 02:58:40 +0000331bool BBinder::isInheritRt() {
332 Extras* e = mExtras.load(std::memory_order_acquire);
333
334 return e && e->mInheritRt;
335}
336
337void BBinder::setInheritRt(bool inheritRt) {
338 Extras* e = mExtras.load(std::memory_order_acquire);
339
340 if (!e) {
341 if (!inheritRt) {
342 return;
343 }
344
345 e = getOrCreateExtras();
346 if (!e) return; // out of memory
347 }
348
349 e->mInheritRt = inheritRt;
350}
351
Steven Moreland86080b82019-09-23 15:41:18 -0700352pid_t BBinder::getDebugPid() {
353 return getpid();
354}
355
Steven Morelandb8ad08d2019-08-09 14:42:56 -0700356void BBinder::setExtension(const sp<IBinder>& extension) {
357 Extras* e = getOrCreateExtras();
358 e->mExtension = extension;
359}
360
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800361BBinder::~BBinder()
362{
Bailey Forrest6913c462015-08-18 17:15:10 -0700363 Extras* e = mExtras.load(std::memory_order_relaxed);
Hans Boehm3effaba2014-08-12 22:56:00 +0000364 if (e) delete e;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800365}
366
367
Jiyong Parkb86c8662018-10-29 23:01:57 +0900368// NOLINTNEXTLINE(google-default-arguments)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800369status_t BBinder::onTransact(
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800370 uint32_t code, const Parcel& data, Parcel* reply, uint32_t /*flags*/)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800371{
372 switch (code) {
373 case INTERFACE_TRANSACTION:
374 reply->writeString16(getInterfaceDescriptor());
375 return NO_ERROR;
376
377 case DUMP_TRANSACTION: {
378 int fd = data.readFileDescriptor();
379 int argc = data.readInt32();
380 Vector<String16> args;
381 for (int i = 0; i < argc && data.dataAvail() > 0; i++) {
382 args.add(data.readString16());
383 }
384 return dump(fd, args);
385 }
Dianne Hackborn555f89d2012-05-08 18:54:22 -0700386
Dianne Hackborn23eb1e22015-10-07 17:35:27 -0700387 case SHELL_COMMAND_TRANSACTION: {
388 int in = data.readFileDescriptor();
389 int out = data.readFileDescriptor();
390 int err = data.readFileDescriptor();
391 int argc = data.readInt32();
392 Vector<String16> args;
393 for (int i = 0; i < argc && data.dataAvail() > 0; i++) {
394 args.add(data.readString16());
395 }
Dianne Hackborn1941a402016-08-29 12:30:43 -0700396 sp<IShellCallback> shellCallback = IShellCallback::asInterface(
397 data.readStrongBinder());
Dianne Hackborn23eb1e22015-10-07 17:35:27 -0700398 sp<IResultReceiver> resultReceiver = IResultReceiver::asInterface(
399 data.readStrongBinder());
400
Dianne Hackbornf2bf93b2015-10-14 15:13:02 -0700401 // XXX can't add virtuals until binaries are updated.
402 //return shellCommand(in, out, err, args, resultReceiver);
Christopher Wiley0a9a1c12016-07-20 08:28:14 -0700403 (void)in;
404 (void)out;
405 (void)err;
406
Yi Kongfdd8da92018-06-07 17:52:27 -0700407 if (resultReceiver != nullptr) {
Dianne Hackbornf2bf93b2015-10-14 15:13:02 -0700408 resultReceiver->send(INVALID_OPERATION);
409 }
Martijn Coenenaa6ee992017-08-17 15:38:08 +0200410
411 return NO_ERROR;
Dianne Hackborn23eb1e22015-10-07 17:35:27 -0700412 }
413
Dianne Hackborn555f89d2012-05-08 18:54:22 -0700414 case SYSPROPS_TRANSACTION: {
415 report_sysprop_change();
416 return NO_ERROR;
417 }
418
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800419 default:
420 return UNKNOWN_TRANSACTION;
421 }
422}
423
Steven Morelandf0212002018-12-26 13:59:23 -0800424BBinder::Extras* BBinder::getOrCreateExtras()
425{
426 Extras* e = mExtras.load(std::memory_order_acquire);
427
428 if (!e) {
429 e = new Extras;
430 Extras* expected = nullptr;
431 if (!mExtras.compare_exchange_strong(expected, e,
432 std::memory_order_release,
433 std::memory_order_acquire)) {
434 delete e;
435 e = expected; // Filled in by CAS
436 }
437 if (e == nullptr) return nullptr; // out of memory
438 }
439
440 return e;
441}
442
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800443// ---------------------------------------------------------------------------
444
445enum {
446 // This is used to transfer ownership of the remote binder from
447 // the BpRefBase object holding it (when it is constructed), to the
448 // owner of the BpRefBase object when it first acquires that BpRefBase.
449 kRemoteAcquired = 0x00000001
450};
451
452BpRefBase::BpRefBase(const sp<IBinder>& o)
Yi Kongfdd8da92018-06-07 17:52:27 -0700453 : mRemote(o.get()), mRefs(nullptr), mState(0)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800454{
455 extendObjectLifetime(OBJECT_LIFETIME_WEAK);
456
457 if (mRemote) {
458 mRemote->incStrong(this); // Removed on first IncStrong().
459 mRefs = mRemote->createWeak(this); // Held for our entire lifetime.
460 }
461}
462
463BpRefBase::~BpRefBase()
464{
465 if (mRemote) {
Bailey Forrest6913c462015-08-18 17:15:10 -0700466 if (!(mState.load(std::memory_order_relaxed)&kRemoteAcquired)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800467 mRemote->decStrong(this);
468 }
469 mRefs->decWeak(this);
470 }
471}
472
473void BpRefBase::onFirstRef()
474{
Bailey Forrest6913c462015-08-18 17:15:10 -0700475 mState.fetch_or(kRemoteAcquired, std::memory_order_relaxed);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800476}
477
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800478void BpRefBase::onLastStrongRef(const void* /*id*/)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800479{
480 if (mRemote) {
481 mRemote->decStrong(this);
482 }
483}
484
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800485bool BpRefBase::onIncStrongAttempted(uint32_t /*flags*/, const void* /*id*/)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800486{
487 return mRemote ? mRefs->attemptIncStrong(this) : false;
488}
489
490// ---------------------------------------------------------------------------
491
Steven Moreland61ff8492019-09-26 16:05:45 -0700492} // namespace android