blob: d5bdd1c803ec868099eb2cfdf0063bc8a4c69f5d [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
Steven Moreland90c1f9a2021-05-03 18:27:24 +000032// Service implementations inherit from BBinder and IBinder, and this is frozen
33// in prebuilts.
34#ifdef __LP64__
35static_assert(sizeof(IBinder) == 24);
36static_assert(sizeof(BBinder) == 40);
37#else
38static_assert(sizeof(IBinder) == 12);
39static_assert(sizeof(BBinder) == 20);
40#endif
41
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080042// ---------------------------------------------------------------------------
43
Mathias Agopian83c04462009-05-22 19:00:22 -070044IBinder::IBinder()
45 : RefBase()
46{
47}
48
49IBinder::~IBinder()
50{
51}
52
53// ---------------------------------------------------------------------------
54
Colin Cross6f4f3ab2014-02-05 17:42:44 -080055sp<IInterface> IBinder::queryLocalInterface(const String16& /*descriptor*/)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080056{
Yi Kongfdd8da92018-06-07 17:52:27 -070057 return nullptr;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080058}
59
60BBinder* IBinder::localBinder()
61{
Yi Kongfdd8da92018-06-07 17:52:27 -070062 return nullptr;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080063}
64
65BpBinder* IBinder::remoteBinder()
66{
Yi Kongfdd8da92018-06-07 17:52:27 -070067 return nullptr;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080068}
69
70bool IBinder::checkSubclass(const void* /*subclassID*/) const
71{
72 return false;
73}
74
Dianne Hackborn23eb1e22015-10-07 17:35:27 -070075
Dianne Hackbornf2bf93b2015-10-14 15:13:02 -070076status_t IBinder::shellCommand(const sp<IBinder>& target, int in, int out, int err,
Dianne Hackborn1941a402016-08-29 12:30:43 -070077 Vector<String16>& args, const sp<IShellCallback>& callback,
78 const sp<IResultReceiver>& resultReceiver)
Dianne Hackborn23eb1e22015-10-07 17:35:27 -070079{
Dianne Hackbornf2bf93b2015-10-14 15:13:02 -070080 Parcel send;
81 Parcel reply;
82 send.writeFileDescriptor(in);
83 send.writeFileDescriptor(out);
84 send.writeFileDescriptor(err);
85 const size_t numArgs = args.size();
86 send.writeInt32(numArgs);
87 for (size_t i = 0; i < numArgs; i++) {
88 send.writeString16(args[i]);
Dianne Hackborn23eb1e22015-10-07 17:35:27 -070089 }
Yi Kongfdd8da92018-06-07 17:52:27 -070090 send.writeStrongBinder(callback != nullptr ? IInterface::asBinder(callback) : nullptr);
91 send.writeStrongBinder(resultReceiver != nullptr ? IInterface::asBinder(resultReceiver) : nullptr);
Dianne Hackbornf2bf93b2015-10-14 15:13:02 -070092 return target->transact(SHELL_COMMAND_TRANSACTION, send, &reply);
Dianne Hackborn23eb1e22015-10-07 17:35:27 -070093}
94
Steven Morelandb8ad08d2019-08-09 14:42:56 -070095status_t IBinder::getExtension(sp<IBinder>* out) {
96 BBinder* local = this->localBinder();
97 if (local != nullptr) {
98 *out = local->getExtension();
99 return OK;
100 }
101
102 BpBinder* proxy = this->remoteBinder();
103 LOG_ALWAYS_FATAL_IF(proxy == nullptr);
104
105 Parcel data;
106 Parcel reply;
107 status_t status = transact(EXTENSION_TRANSACTION, data, &reply);
108 if (status != OK) return status;
109
110 return reply.readNullableStrongBinder(out);
111}
112
Steven Moreland86080b82019-09-23 15:41:18 -0700113status_t IBinder::getDebugPid(pid_t* out) {
114 BBinder* local = this->localBinder();
115 if (local != nullptr) {
116 *out = local->getDebugPid();
117 return OK;
118 }
119
120 BpBinder* proxy = this->remoteBinder();
121 LOG_ALWAYS_FATAL_IF(proxy == nullptr);
122
123 Parcel data;
124 Parcel reply;
125 status_t status = transact(DEBUG_PID_TRANSACTION, data, &reply);
126 if (status != OK) return status;
127
128 int32_t pid;
129 status = reply.readInt32(&pid);
130 if (status != OK) return status;
131
132 if (pid < 0 || pid > std::numeric_limits<pid_t>::max()) {
133 return BAD_VALUE;
134 }
135 *out = pid;
136 return OK;
137}
138
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800139// ---------------------------------------------------------------------------
140
141class BBinder::Extras
142{
143public:
Steven Morelandf0212002018-12-26 13:59:23 -0800144 // unlocked objects
145 bool mRequestingSid = false;
Steven Morelandcf03cf12020-12-04 02:58:40 +0000146 bool mInheritRt = false;
Steven Morelandb8ad08d2019-08-09 14:42:56 -0700147 sp<IBinder> mExtension;
Steven Morelandbf1915b2020-07-16 22:43:02 +0000148 int mPolicy = SCHED_NORMAL;
149 int mPriority = 0;
Steven Morelandf0212002018-12-26 13:59:23 -0800150
151 // for below objects
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800152 Mutex mLock;
153 BpBinder::ObjectManager mObjects;
154};
155
156// ---------------------------------------------------------------------------
157
Steven Morelanda7fb0182020-02-26 16:02:08 -0800158BBinder::BBinder() : mExtras(nullptr), mStability(0)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800159{
160}
161
162bool BBinder::isBinderAlive() const
163{
164 return true;
165}
166
167status_t BBinder::pingBinder()
168{
169 return NO_ERROR;
170}
171
Mathias Agopian83c04462009-05-22 19:00:22 -0700172const String16& BBinder::getInterfaceDescriptor() const
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800173{
Dan Egnor386a3322010-05-06 00:55:09 -0700174 // This is a local static rather than a global static,
175 // to avoid static initializer ordering issues.
176 static String16 sEmptyDescriptor;
Steve Block32397c12012-01-05 23:22:43 +0000177 ALOGW("reached BBinder::getInterfaceDescriptor (this=%p)", this);
Mathias Agopian83c04462009-05-22 19:00:22 -0700178 return sEmptyDescriptor;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800179}
180
Jiyong Parkb86c8662018-10-29 23:01:57 +0900181// NOLINTNEXTLINE(google-default-arguments)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800182status_t BBinder::transact(
183 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
184{
185 data.setDataPosition(0);
186
Steven Morelandf183fdd2020-10-27 00:12:12 +0000187 if (reply != nullptr && (flags & FLAG_CLEAR_BUF)) {
188 reply->markSensitive();
189 }
190
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800191 status_t err = NO_ERROR;
192 switch (code) {
193 case PING_TRANSACTION:
Steven Moreland6e69d652019-07-10 14:17:55 -0700194 err = pingBinder();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800195 break;
Steven Morelandb8ad08d2019-08-09 14:42:56 -0700196 case EXTENSION_TRANSACTION:
197 err = reply->writeStrongBinder(getExtension());
198 break;
Steven Moreland86080b82019-09-23 15:41:18 -0700199 case DEBUG_PID_TRANSACTION:
200 err = reply->writeInt32(getDebugPid());
201 break;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800202 default:
203 err = onTransact(code, data, reply, flags);
204 break;
205 }
206
Steven Morelanda86a3562019-08-01 23:28:34 +0000207 // In case this is being transacted on in the same process.
Yi Kongfdd8da92018-06-07 17:52:27 -0700208 if (reply != nullptr) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800209 reply->setDataPosition(0);
210 }
211
212 return err;
213}
214
Jiyong Parkb86c8662018-10-29 23:01:57 +0900215// NOLINTNEXTLINE(google-default-arguments)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800216status_t BBinder::linkToDeath(
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800217 const sp<DeathRecipient>& /*recipient*/, void* /*cookie*/,
218 uint32_t /*flags*/)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800219{
220 return INVALID_OPERATION;
221}
222
Jiyong Parkb86c8662018-10-29 23:01:57 +0900223// NOLINTNEXTLINE(google-default-arguments)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800224status_t BBinder::unlinkToDeath(
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800225 const wp<DeathRecipient>& /*recipient*/, void* /*cookie*/,
226 uint32_t /*flags*/, wp<DeathRecipient>* /*outRecipient*/)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800227{
228 return INVALID_OPERATION;
229}
230
Dianne Hackborn23eb1e22015-10-07 17:35:27 -0700231status_t BBinder::dump(int /*fd*/, const Vector<String16>& /*args*/)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800232{
233 return NO_ERROR;
234}
235
236void BBinder::attachObject(
237 const void* objectID, void* object, void* cleanupCookie,
238 object_cleanup_func func)
239{
Steven Morelandf0212002018-12-26 13:59:23 -0800240 Extras* e = getOrCreateExtras();
241 if (!e) return; // out of memory
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800242
243 AutoMutex _l(e->mLock);
244 e->mObjects.attach(objectID, object, cleanupCookie, func);
245}
246
247void* BBinder::findObject(const void* objectID) const
248{
Bailey Forrest6913c462015-08-18 17:15:10 -0700249 Extras* e = mExtras.load(std::memory_order_acquire);
Yi Kongfdd8da92018-06-07 17:52:27 -0700250 if (!e) return nullptr;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800251
252 AutoMutex _l(e->mLock);
253 return e->mObjects.find(objectID);
254}
255
256void BBinder::detachObject(const void* objectID)
257{
Bailey Forrest6913c462015-08-18 17:15:10 -0700258 Extras* e = mExtras.load(std::memory_order_acquire);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800259 if (!e) return;
260
261 AutoMutex _l(e->mLock);
262 e->mObjects.detach(objectID);
263}
264
265BBinder* BBinder::localBinder()
266{
267 return this;
268}
269
Steven Morelandf0212002018-12-26 13:59:23 -0800270bool BBinder::isRequestingSid()
271{
272 Extras* e = mExtras.load(std::memory_order_acquire);
273
274 return e && e->mRequestingSid;
275}
276
277void BBinder::setRequestingSid(bool requestingSid)
278{
279 Extras* e = mExtras.load(std::memory_order_acquire);
280
281 if (!e) {
282 // default is false. Most things don't need sids, so avoiding allocations when possible.
283 if (!requestingSid) {
284 return;
285 }
286
287 e = getOrCreateExtras();
288 if (!e) return; // out of memory
289 }
290
Steven Moreland3668be62019-02-08 17:56:55 -0800291 e->mRequestingSid = requestingSid;
Steven Morelandf0212002018-12-26 13:59:23 -0800292}
293
Steven Morelandb8ad08d2019-08-09 14:42:56 -0700294sp<IBinder> BBinder::getExtension() {
295 Extras* e = mExtras.load(std::memory_order_acquire);
296 if (e == nullptr) return nullptr;
297 return e->mExtension;
298}
299
Steven Morelandbf1915b2020-07-16 22:43:02 +0000300void BBinder::setMinSchedulerPolicy(int policy, int priority) {
301 switch (policy) {
302 case SCHED_NORMAL:
303 LOG_ALWAYS_FATAL_IF(priority < -20 || priority > 19, "Invalid priority for SCHED_NORMAL: %d", priority);
304 break;
305 case SCHED_RR:
306 case SCHED_FIFO:
307 LOG_ALWAYS_FATAL_IF(priority < 1 || priority > 99, "Invalid priority for sched %d: %d", policy, priority);
308 break;
309 default:
310 LOG_ALWAYS_FATAL("Unrecognized scheduling policy: %d", policy);
311 }
312
313 Extras* e = mExtras.load(std::memory_order_acquire);
314
315 if (e == nullptr) {
316 // Avoid allocations if called with default.
317 if (policy == SCHED_NORMAL && priority == 0) {
318 return;
319 }
320
321 e = getOrCreateExtras();
322 if (!e) return; // out of memory
323 }
324
325 e->mPolicy = policy;
326 e->mPriority = priority;
327}
328
329int BBinder::getMinSchedulerPolicy() {
330 Extras* e = mExtras.load(std::memory_order_acquire);
331 if (e == nullptr) return SCHED_NORMAL;
332 return e->mPolicy;
333}
334
335int BBinder::getMinSchedulerPriority() {
336 Extras* e = mExtras.load(std::memory_order_acquire);
337 if (e == nullptr) return 0;
338 return e->mPriority;
339}
340
Steven Morelandcf03cf12020-12-04 02:58:40 +0000341bool BBinder::isInheritRt() {
342 Extras* e = mExtras.load(std::memory_order_acquire);
343
344 return e && e->mInheritRt;
345}
346
347void BBinder::setInheritRt(bool inheritRt) {
348 Extras* e = mExtras.load(std::memory_order_acquire);
349
350 if (!e) {
351 if (!inheritRt) {
352 return;
353 }
354
355 e = getOrCreateExtras();
356 if (!e) return; // out of memory
357 }
358
359 e->mInheritRt = inheritRt;
360}
361
Steven Moreland86080b82019-09-23 15:41:18 -0700362pid_t BBinder::getDebugPid() {
363 return getpid();
364}
365
Steven Morelandb8ad08d2019-08-09 14:42:56 -0700366void BBinder::setExtension(const sp<IBinder>& extension) {
367 Extras* e = getOrCreateExtras();
368 e->mExtension = extension;
369}
370
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800371BBinder::~BBinder()
372{
Bailey Forrest6913c462015-08-18 17:15:10 -0700373 Extras* e = mExtras.load(std::memory_order_relaxed);
Hans Boehm3effaba2014-08-12 22:56:00 +0000374 if (e) delete e;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800375}
376
377
Jiyong Parkb86c8662018-10-29 23:01:57 +0900378// NOLINTNEXTLINE(google-default-arguments)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800379status_t BBinder::onTransact(
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800380 uint32_t code, const Parcel& data, Parcel* reply, uint32_t /*flags*/)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800381{
382 switch (code) {
383 case INTERFACE_TRANSACTION:
384 reply->writeString16(getInterfaceDescriptor());
385 return NO_ERROR;
386
387 case DUMP_TRANSACTION: {
388 int fd = data.readFileDescriptor();
389 int argc = data.readInt32();
390 Vector<String16> args;
391 for (int i = 0; i < argc && data.dataAvail() > 0; i++) {
392 args.add(data.readString16());
393 }
394 return dump(fd, args);
395 }
Dianne Hackborn555f89d2012-05-08 18:54:22 -0700396
Dianne Hackborn23eb1e22015-10-07 17:35:27 -0700397 case SHELL_COMMAND_TRANSACTION: {
398 int in = data.readFileDescriptor();
399 int out = data.readFileDescriptor();
400 int err = data.readFileDescriptor();
401 int argc = data.readInt32();
402 Vector<String16> args;
403 for (int i = 0; i < argc && data.dataAvail() > 0; i++) {
404 args.add(data.readString16());
405 }
Dianne Hackborn1941a402016-08-29 12:30:43 -0700406 sp<IShellCallback> shellCallback = IShellCallback::asInterface(
407 data.readStrongBinder());
Dianne Hackborn23eb1e22015-10-07 17:35:27 -0700408 sp<IResultReceiver> resultReceiver = IResultReceiver::asInterface(
409 data.readStrongBinder());
410
Dianne Hackbornf2bf93b2015-10-14 15:13:02 -0700411 // XXX can't add virtuals until binaries are updated.
412 //return shellCommand(in, out, err, args, resultReceiver);
Christopher Wiley0a9a1c12016-07-20 08:28:14 -0700413 (void)in;
414 (void)out;
415 (void)err;
416
Yi Kongfdd8da92018-06-07 17:52:27 -0700417 if (resultReceiver != nullptr) {
Dianne Hackbornf2bf93b2015-10-14 15:13:02 -0700418 resultReceiver->send(INVALID_OPERATION);
419 }
Martijn Coenenaa6ee992017-08-17 15:38:08 +0200420
421 return NO_ERROR;
Dianne Hackborn23eb1e22015-10-07 17:35:27 -0700422 }
423
Dianne Hackborn555f89d2012-05-08 18:54:22 -0700424 case SYSPROPS_TRANSACTION: {
425 report_sysprop_change();
426 return NO_ERROR;
427 }
428
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800429 default:
430 return UNKNOWN_TRANSACTION;
431 }
432}
433
Steven Morelandf0212002018-12-26 13:59:23 -0800434BBinder::Extras* BBinder::getOrCreateExtras()
435{
436 Extras* e = mExtras.load(std::memory_order_acquire);
437
438 if (!e) {
439 e = new Extras;
440 Extras* expected = nullptr;
441 if (!mExtras.compare_exchange_strong(expected, e,
442 std::memory_order_release,
443 std::memory_order_acquire)) {
444 delete e;
445 e = expected; // Filled in by CAS
446 }
447 if (e == nullptr) return nullptr; // out of memory
448 }
449
450 return e;
451}
452
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800453// ---------------------------------------------------------------------------
454
455enum {
456 // This is used to transfer ownership of the remote binder from
457 // the BpRefBase object holding it (when it is constructed), to the
458 // owner of the BpRefBase object when it first acquires that BpRefBase.
459 kRemoteAcquired = 0x00000001
460};
461
462BpRefBase::BpRefBase(const sp<IBinder>& o)
Yi Kongfdd8da92018-06-07 17:52:27 -0700463 : mRemote(o.get()), mRefs(nullptr), mState(0)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800464{
465 extendObjectLifetime(OBJECT_LIFETIME_WEAK);
466
467 if (mRemote) {
468 mRemote->incStrong(this); // Removed on first IncStrong().
469 mRefs = mRemote->createWeak(this); // Held for our entire lifetime.
470 }
471}
472
473BpRefBase::~BpRefBase()
474{
475 if (mRemote) {
Bailey Forrest6913c462015-08-18 17:15:10 -0700476 if (!(mState.load(std::memory_order_relaxed)&kRemoteAcquired)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800477 mRemote->decStrong(this);
478 }
479 mRefs->decWeak(this);
480 }
481}
482
483void BpRefBase::onFirstRef()
484{
Bailey Forrest6913c462015-08-18 17:15:10 -0700485 mState.fetch_or(kRemoteAcquired, std::memory_order_relaxed);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800486}
487
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800488void BpRefBase::onLastStrongRef(const void* /*id*/)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800489{
490 if (mRemote) {
491 mRemote->decStrong(this);
492 }
493}
494
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800495bool BpRefBase::onIncStrongAttempted(uint32_t /*flags*/, const void* /*id*/)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800496{
497 return mRemote ? mRefs->attemptIncStrong(this) : false;
498}
499
500// ---------------------------------------------------------------------------
501
Steven Moreland61ff8492019-09-26 16:05:45 -0700502} // namespace android