blob: 6ca3b16324901343dda6ffea4dae8a2df2c2882e [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
176 status_t err = NO_ERROR;
177 switch (code) {
178 case PING_TRANSACTION:
Steven Moreland6e69d652019-07-10 14:17:55 -0700179 err = pingBinder();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800180 break;
Steven Morelandb8ad08d2019-08-09 14:42:56 -0700181 case EXTENSION_TRANSACTION:
182 err = reply->writeStrongBinder(getExtension());
183 break;
Steven Moreland86080b82019-09-23 15:41:18 -0700184 case DEBUG_PID_TRANSACTION:
185 err = reply->writeInt32(getDebugPid());
186 break;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800187 default:
188 err = onTransact(code, data, reply, flags);
189 break;
190 }
191
Steven Morelanda86a3562019-08-01 23:28:34 +0000192 // In case this is being transacted on in the same process.
Yi Kongfdd8da92018-06-07 17:52:27 -0700193 if (reply != nullptr) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800194 reply->setDataPosition(0);
195 }
196
197 return err;
198}
199
Jiyong Parkb86c8662018-10-29 23:01:57 +0900200// NOLINTNEXTLINE(google-default-arguments)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800201status_t BBinder::linkToDeath(
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800202 const sp<DeathRecipient>& /*recipient*/, void* /*cookie*/,
203 uint32_t /*flags*/)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800204{
205 return INVALID_OPERATION;
206}
207
Jiyong Parkb86c8662018-10-29 23:01:57 +0900208// NOLINTNEXTLINE(google-default-arguments)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800209status_t BBinder::unlinkToDeath(
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800210 const wp<DeathRecipient>& /*recipient*/, void* /*cookie*/,
211 uint32_t /*flags*/, wp<DeathRecipient>* /*outRecipient*/)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800212{
213 return INVALID_OPERATION;
214}
215
Dianne Hackborn23eb1e22015-10-07 17:35:27 -0700216status_t BBinder::dump(int /*fd*/, const Vector<String16>& /*args*/)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800217{
218 return NO_ERROR;
219}
220
221void BBinder::attachObject(
222 const void* objectID, void* object, void* cleanupCookie,
223 object_cleanup_func func)
224{
Steven Morelandf0212002018-12-26 13:59:23 -0800225 Extras* e = getOrCreateExtras();
226 if (!e) return; // out of memory
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800227
228 AutoMutex _l(e->mLock);
229 e->mObjects.attach(objectID, object, cleanupCookie, func);
230}
231
232void* BBinder::findObject(const void* objectID) const
233{
Bailey Forrest6913c462015-08-18 17:15:10 -0700234 Extras* e = mExtras.load(std::memory_order_acquire);
Yi Kongfdd8da92018-06-07 17:52:27 -0700235 if (!e) return nullptr;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800236
237 AutoMutex _l(e->mLock);
238 return e->mObjects.find(objectID);
239}
240
241void BBinder::detachObject(const void* objectID)
242{
Bailey Forrest6913c462015-08-18 17:15:10 -0700243 Extras* e = mExtras.load(std::memory_order_acquire);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800244 if (!e) return;
245
246 AutoMutex _l(e->mLock);
247 e->mObjects.detach(objectID);
248}
249
250BBinder* BBinder::localBinder()
251{
252 return this;
253}
254
Steven Morelandf0212002018-12-26 13:59:23 -0800255bool BBinder::isRequestingSid()
256{
257 Extras* e = mExtras.load(std::memory_order_acquire);
258
259 return e && e->mRequestingSid;
260}
261
262void BBinder::setRequestingSid(bool requestingSid)
263{
264 Extras* e = mExtras.load(std::memory_order_acquire);
265
266 if (!e) {
267 // default is false. Most things don't need sids, so avoiding allocations when possible.
268 if (!requestingSid) {
269 return;
270 }
271
272 e = getOrCreateExtras();
273 if (!e) return; // out of memory
274 }
275
Steven Moreland3668be62019-02-08 17:56:55 -0800276 e->mRequestingSid = requestingSid;
Steven Morelandf0212002018-12-26 13:59:23 -0800277}
278
Steven Morelandb8ad08d2019-08-09 14:42:56 -0700279sp<IBinder> BBinder::getExtension() {
280 Extras* e = mExtras.load(std::memory_order_acquire);
281 if (e == nullptr) return nullptr;
282 return e->mExtension;
283}
284
Steven Morelandbf1915b2020-07-16 22:43:02 +0000285void BBinder::setMinSchedulerPolicy(int policy, int priority) {
286 switch (policy) {
287 case SCHED_NORMAL:
288 LOG_ALWAYS_FATAL_IF(priority < -20 || priority > 19, "Invalid priority for SCHED_NORMAL: %d", priority);
289 break;
290 case SCHED_RR:
291 case SCHED_FIFO:
292 LOG_ALWAYS_FATAL_IF(priority < 1 || priority > 99, "Invalid priority for sched %d: %d", policy, priority);
293 break;
294 default:
295 LOG_ALWAYS_FATAL("Unrecognized scheduling policy: %d", policy);
296 }
297
298 Extras* e = mExtras.load(std::memory_order_acquire);
299
300 if (e == nullptr) {
301 // Avoid allocations if called with default.
302 if (policy == SCHED_NORMAL && priority == 0) {
303 return;
304 }
305
306 e = getOrCreateExtras();
307 if (!e) return; // out of memory
308 }
309
310 e->mPolicy = policy;
311 e->mPriority = priority;
312}
313
314int BBinder::getMinSchedulerPolicy() {
315 Extras* e = mExtras.load(std::memory_order_acquire);
316 if (e == nullptr) return SCHED_NORMAL;
317 return e->mPolicy;
318}
319
320int BBinder::getMinSchedulerPriority() {
321 Extras* e = mExtras.load(std::memory_order_acquire);
322 if (e == nullptr) return 0;
323 return e->mPriority;
324}
325
Steven Moreland86080b82019-09-23 15:41:18 -0700326pid_t BBinder::getDebugPid() {
327 return getpid();
328}
329
Steven Morelandb8ad08d2019-08-09 14:42:56 -0700330void BBinder::setExtension(const sp<IBinder>& extension) {
331 Extras* e = getOrCreateExtras();
332 e->mExtension = extension;
333}
334
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800335BBinder::~BBinder()
336{
Bailey Forrest6913c462015-08-18 17:15:10 -0700337 Extras* e = mExtras.load(std::memory_order_relaxed);
Hans Boehm3effaba2014-08-12 22:56:00 +0000338 if (e) delete e;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800339}
340
341
Jiyong Parkb86c8662018-10-29 23:01:57 +0900342// NOLINTNEXTLINE(google-default-arguments)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800343status_t BBinder::onTransact(
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800344 uint32_t code, const Parcel& data, Parcel* reply, uint32_t /*flags*/)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800345{
346 switch (code) {
347 case INTERFACE_TRANSACTION:
348 reply->writeString16(getInterfaceDescriptor());
349 return NO_ERROR;
350
351 case DUMP_TRANSACTION: {
352 int fd = data.readFileDescriptor();
353 int argc = data.readInt32();
354 Vector<String16> args;
355 for (int i = 0; i < argc && data.dataAvail() > 0; i++) {
356 args.add(data.readString16());
357 }
358 return dump(fd, args);
359 }
Dianne Hackborn555f89d2012-05-08 18:54:22 -0700360
Dianne Hackborn23eb1e22015-10-07 17:35:27 -0700361 case SHELL_COMMAND_TRANSACTION: {
362 int in = data.readFileDescriptor();
363 int out = data.readFileDescriptor();
364 int err = data.readFileDescriptor();
365 int argc = data.readInt32();
366 Vector<String16> args;
367 for (int i = 0; i < argc && data.dataAvail() > 0; i++) {
368 args.add(data.readString16());
369 }
Dianne Hackborn1941a402016-08-29 12:30:43 -0700370 sp<IShellCallback> shellCallback = IShellCallback::asInterface(
371 data.readStrongBinder());
Dianne Hackborn23eb1e22015-10-07 17:35:27 -0700372 sp<IResultReceiver> resultReceiver = IResultReceiver::asInterface(
373 data.readStrongBinder());
374
Dianne Hackbornf2bf93b2015-10-14 15:13:02 -0700375 // XXX can't add virtuals until binaries are updated.
376 //return shellCommand(in, out, err, args, resultReceiver);
Christopher Wiley0a9a1c12016-07-20 08:28:14 -0700377 (void)in;
378 (void)out;
379 (void)err;
380
Yi Kongfdd8da92018-06-07 17:52:27 -0700381 if (resultReceiver != nullptr) {
Dianne Hackbornf2bf93b2015-10-14 15:13:02 -0700382 resultReceiver->send(INVALID_OPERATION);
383 }
Martijn Coenenaa6ee992017-08-17 15:38:08 +0200384
385 return NO_ERROR;
Dianne Hackborn23eb1e22015-10-07 17:35:27 -0700386 }
387
Dianne Hackborn555f89d2012-05-08 18:54:22 -0700388 case SYSPROPS_TRANSACTION: {
389 report_sysprop_change();
390 return NO_ERROR;
391 }
392
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800393 default:
394 return UNKNOWN_TRANSACTION;
395 }
396}
397
Steven Morelandf0212002018-12-26 13:59:23 -0800398BBinder::Extras* BBinder::getOrCreateExtras()
399{
400 Extras* e = mExtras.load(std::memory_order_acquire);
401
402 if (!e) {
403 e = new Extras;
404 Extras* expected = nullptr;
405 if (!mExtras.compare_exchange_strong(expected, e,
406 std::memory_order_release,
407 std::memory_order_acquire)) {
408 delete e;
409 e = expected; // Filled in by CAS
410 }
411 if (e == nullptr) return nullptr; // out of memory
412 }
413
414 return e;
415}
416
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800417// ---------------------------------------------------------------------------
418
419enum {
420 // This is used to transfer ownership of the remote binder from
421 // the BpRefBase object holding it (when it is constructed), to the
422 // owner of the BpRefBase object when it first acquires that BpRefBase.
423 kRemoteAcquired = 0x00000001
424};
425
426BpRefBase::BpRefBase(const sp<IBinder>& o)
Yi Kongfdd8da92018-06-07 17:52:27 -0700427 : mRemote(o.get()), mRefs(nullptr), mState(0)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800428{
429 extendObjectLifetime(OBJECT_LIFETIME_WEAK);
430
431 if (mRemote) {
432 mRemote->incStrong(this); // Removed on first IncStrong().
433 mRefs = mRemote->createWeak(this); // Held for our entire lifetime.
434 }
435}
436
437BpRefBase::~BpRefBase()
438{
439 if (mRemote) {
Bailey Forrest6913c462015-08-18 17:15:10 -0700440 if (!(mState.load(std::memory_order_relaxed)&kRemoteAcquired)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800441 mRemote->decStrong(this);
442 }
443 mRefs->decWeak(this);
444 }
445}
446
447void BpRefBase::onFirstRef()
448{
Bailey Forrest6913c462015-08-18 17:15:10 -0700449 mState.fetch_or(kRemoteAcquired, std::memory_order_relaxed);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800450}
451
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800452void BpRefBase::onLastStrongRef(const void* /*id*/)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800453{
454 if (mRemote) {
455 mRemote->decStrong(this);
456 }
457}
458
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800459bool BpRefBase::onIncStrongAttempted(uint32_t /*flags*/, const void* /*id*/)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800460{
461 return mRemote ? mRefs->attemptIncStrong(this) : false;
462}
463
464// ---------------------------------------------------------------------------
465
Steven Moreland61ff8492019-09-26 16:05:45 -0700466} // namespace android