blob: 2f6e9c3a1b138aa5b271fdf1639097a586df8eb7 [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
27#include <stdio.h>
28
29namespace android {
30
31// ---------------------------------------------------------------------------
32
Mathias Agopian83c04462009-05-22 19:00:22 -070033IBinder::IBinder()
34 : RefBase()
35{
36}
37
38IBinder::~IBinder()
39{
40}
41
42// ---------------------------------------------------------------------------
43
Colin Cross6f4f3ab2014-02-05 17:42:44 -080044sp<IInterface> IBinder::queryLocalInterface(const String16& /*descriptor*/)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080045{
Yi Kongfdd8da92018-06-07 17:52:27 -070046 return nullptr;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080047}
48
49BBinder* IBinder::localBinder()
50{
Yi Kongfdd8da92018-06-07 17:52:27 -070051 return nullptr;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080052}
53
54BpBinder* IBinder::remoteBinder()
55{
Yi Kongfdd8da92018-06-07 17:52:27 -070056 return nullptr;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080057}
58
59bool IBinder::checkSubclass(const void* /*subclassID*/) const
60{
61 return false;
62}
63
Dianne Hackborn23eb1e22015-10-07 17:35:27 -070064
Dianne Hackbornf2bf93b2015-10-14 15:13:02 -070065status_t IBinder::shellCommand(const sp<IBinder>& target, int in, int out, int err,
Dianne Hackborn1941a402016-08-29 12:30:43 -070066 Vector<String16>& args, const sp<IShellCallback>& callback,
67 const sp<IResultReceiver>& resultReceiver)
Dianne Hackborn23eb1e22015-10-07 17:35:27 -070068{
Dianne Hackbornf2bf93b2015-10-14 15:13:02 -070069 Parcel send;
70 Parcel reply;
71 send.writeFileDescriptor(in);
72 send.writeFileDescriptor(out);
73 send.writeFileDescriptor(err);
74 const size_t numArgs = args.size();
75 send.writeInt32(numArgs);
76 for (size_t i = 0; i < numArgs; i++) {
77 send.writeString16(args[i]);
Dianne Hackborn23eb1e22015-10-07 17:35:27 -070078 }
Yi Kongfdd8da92018-06-07 17:52:27 -070079 send.writeStrongBinder(callback != nullptr ? IInterface::asBinder(callback) : nullptr);
80 send.writeStrongBinder(resultReceiver != nullptr ? IInterface::asBinder(resultReceiver) : nullptr);
Dianne Hackbornf2bf93b2015-10-14 15:13:02 -070081 return target->transact(SHELL_COMMAND_TRANSACTION, send, &reply);
Dianne Hackborn23eb1e22015-10-07 17:35:27 -070082}
83
Steven Morelandb8ad08d2019-08-09 14:42:56 -070084status_t IBinder::getExtension(sp<IBinder>* out) {
85 BBinder* local = this->localBinder();
86 if (local != nullptr) {
87 *out = local->getExtension();
88 return OK;
89 }
90
91 BpBinder* proxy = this->remoteBinder();
92 LOG_ALWAYS_FATAL_IF(proxy == nullptr);
93
94 Parcel data;
95 Parcel reply;
96 status_t status = transact(EXTENSION_TRANSACTION, data, &reply);
97 if (status != OK) return status;
98
99 return reply.readNullableStrongBinder(out);
100}
101
Steven Moreland86080b82019-09-23 15:41:18 -0700102status_t IBinder::getDebugPid(pid_t* out) {
103 BBinder* local = this->localBinder();
104 if (local != nullptr) {
105 *out = local->getDebugPid();
106 return OK;
107 }
108
109 BpBinder* proxy = this->remoteBinder();
110 LOG_ALWAYS_FATAL_IF(proxy == nullptr);
111
112 Parcel data;
113 Parcel reply;
114 status_t status = transact(DEBUG_PID_TRANSACTION, data, &reply);
115 if (status != OK) return status;
116
117 int32_t pid;
118 status = reply.readInt32(&pid);
119 if (status != OK) return status;
120
121 if (pid < 0 || pid > std::numeric_limits<pid_t>::max()) {
122 return BAD_VALUE;
123 }
124 *out = pid;
125 return OK;
126}
127
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800128// ---------------------------------------------------------------------------
129
130class BBinder::Extras
131{
132public:
Steven Morelandf0212002018-12-26 13:59:23 -0800133 // unlocked objects
134 bool mRequestingSid = false;
Steven Morelandb8ad08d2019-08-09 14:42:56 -0700135 sp<IBinder> mExtension;
Steven Morelandf0212002018-12-26 13:59:23 -0800136
137 // for below objects
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800138 Mutex mLock;
139 BpBinder::ObjectManager mObjects;
140};
141
142// ---------------------------------------------------------------------------
143
Bailey Forrest6913c462015-08-18 17:15:10 -0700144BBinder::BBinder() : mExtras(nullptr)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800145{
146}
147
148bool BBinder::isBinderAlive() const
149{
150 return true;
151}
152
153status_t BBinder::pingBinder()
154{
155 return NO_ERROR;
156}
157
Mathias Agopian83c04462009-05-22 19:00:22 -0700158const String16& BBinder::getInterfaceDescriptor() const
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800159{
Dan Egnor386a3322010-05-06 00:55:09 -0700160 // This is a local static rather than a global static,
161 // to avoid static initializer ordering issues.
162 static String16 sEmptyDescriptor;
Steve Block32397c12012-01-05 23:22:43 +0000163 ALOGW("reached BBinder::getInterfaceDescriptor (this=%p)", this);
Mathias Agopian83c04462009-05-22 19:00:22 -0700164 return sEmptyDescriptor;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800165}
166
Jiyong Parkb86c8662018-10-29 23:01:57 +0900167// NOLINTNEXTLINE(google-default-arguments)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800168status_t BBinder::transact(
169 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
170{
171 data.setDataPosition(0);
172
173 status_t err = NO_ERROR;
174 switch (code) {
175 case PING_TRANSACTION:
Steven Moreland6e69d652019-07-10 14:17:55 -0700176 err = pingBinder();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800177 break;
Steven Morelandb8ad08d2019-08-09 14:42:56 -0700178 case EXTENSION_TRANSACTION:
179 err = reply->writeStrongBinder(getExtension());
180 break;
Steven Moreland86080b82019-09-23 15:41:18 -0700181 case DEBUG_PID_TRANSACTION:
182 err = reply->writeInt32(getDebugPid());
183 break;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800184 default:
185 err = onTransact(code, data, reply, flags);
186 break;
187 }
188
Steven Morelanda86a3562019-08-01 23:28:34 +0000189 // In case this is being transacted on in the same process.
Yi Kongfdd8da92018-06-07 17:52:27 -0700190 if (reply != nullptr) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800191 reply->setDataPosition(0);
192 }
193
194 return err;
195}
196
Jiyong Parkb86c8662018-10-29 23:01:57 +0900197// NOLINTNEXTLINE(google-default-arguments)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800198status_t BBinder::linkToDeath(
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800199 const sp<DeathRecipient>& /*recipient*/, void* /*cookie*/,
200 uint32_t /*flags*/)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800201{
202 return INVALID_OPERATION;
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::unlinkToDeath(
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800207 const wp<DeathRecipient>& /*recipient*/, void* /*cookie*/,
208 uint32_t /*flags*/, wp<DeathRecipient>* /*outRecipient*/)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800209{
210 return INVALID_OPERATION;
211}
212
Dianne Hackborn23eb1e22015-10-07 17:35:27 -0700213status_t BBinder::dump(int /*fd*/, const Vector<String16>& /*args*/)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800214{
215 return NO_ERROR;
216}
217
218void BBinder::attachObject(
219 const void* objectID, void* object, void* cleanupCookie,
220 object_cleanup_func func)
221{
Steven Morelandf0212002018-12-26 13:59:23 -0800222 Extras* e = getOrCreateExtras();
223 if (!e) return; // out of memory
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800224
225 AutoMutex _l(e->mLock);
226 e->mObjects.attach(objectID, object, cleanupCookie, func);
227}
228
229void* BBinder::findObject(const void* objectID) const
230{
Bailey Forrest6913c462015-08-18 17:15:10 -0700231 Extras* e = mExtras.load(std::memory_order_acquire);
Yi Kongfdd8da92018-06-07 17:52:27 -0700232 if (!e) return nullptr;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800233
234 AutoMutex _l(e->mLock);
235 return e->mObjects.find(objectID);
236}
237
238void BBinder::detachObject(const void* objectID)
239{
Bailey Forrest6913c462015-08-18 17:15:10 -0700240 Extras* e = mExtras.load(std::memory_order_acquire);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800241 if (!e) return;
242
243 AutoMutex _l(e->mLock);
244 e->mObjects.detach(objectID);
245}
246
247BBinder* BBinder::localBinder()
248{
249 return this;
250}
251
Steven Morelandf0212002018-12-26 13:59:23 -0800252bool BBinder::isRequestingSid()
253{
254 Extras* e = mExtras.load(std::memory_order_acquire);
255
256 return e && e->mRequestingSid;
257}
258
259void BBinder::setRequestingSid(bool requestingSid)
260{
261 Extras* e = mExtras.load(std::memory_order_acquire);
262
263 if (!e) {
264 // default is false. Most things don't need sids, so avoiding allocations when possible.
265 if (!requestingSid) {
266 return;
267 }
268
269 e = getOrCreateExtras();
270 if (!e) return; // out of memory
271 }
272
Steven Moreland3668be62019-02-08 17:56:55 -0800273 e->mRequestingSid = requestingSid;
Steven Morelandf0212002018-12-26 13:59:23 -0800274}
275
Steven Morelandb8ad08d2019-08-09 14:42:56 -0700276sp<IBinder> BBinder::getExtension() {
277 Extras* e = mExtras.load(std::memory_order_acquire);
278 if (e == nullptr) return nullptr;
279 return e->mExtension;
280}
281
Steven Moreland86080b82019-09-23 15:41:18 -0700282pid_t BBinder::getDebugPid() {
283 return getpid();
284}
285
Steven Morelandb8ad08d2019-08-09 14:42:56 -0700286void BBinder::setExtension(const sp<IBinder>& extension) {
287 Extras* e = getOrCreateExtras();
288 e->mExtension = extension;
289}
290
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800291BBinder::~BBinder()
292{
Bailey Forrest6913c462015-08-18 17:15:10 -0700293 Extras* e = mExtras.load(std::memory_order_relaxed);
Hans Boehm3effaba2014-08-12 22:56:00 +0000294 if (e) delete e;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800295}
296
297
Jiyong Parkb86c8662018-10-29 23:01:57 +0900298// NOLINTNEXTLINE(google-default-arguments)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800299status_t BBinder::onTransact(
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800300 uint32_t code, const Parcel& data, Parcel* reply, uint32_t /*flags*/)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800301{
302 switch (code) {
303 case INTERFACE_TRANSACTION:
304 reply->writeString16(getInterfaceDescriptor());
305 return NO_ERROR;
306
307 case DUMP_TRANSACTION: {
308 int fd = data.readFileDescriptor();
309 int argc = data.readInt32();
310 Vector<String16> args;
311 for (int i = 0; i < argc && data.dataAvail() > 0; i++) {
312 args.add(data.readString16());
313 }
314 return dump(fd, args);
315 }
Dianne Hackborn555f89d2012-05-08 18:54:22 -0700316
Dianne Hackborn23eb1e22015-10-07 17:35:27 -0700317 case SHELL_COMMAND_TRANSACTION: {
318 int in = data.readFileDescriptor();
319 int out = data.readFileDescriptor();
320 int err = data.readFileDescriptor();
321 int argc = data.readInt32();
322 Vector<String16> args;
323 for (int i = 0; i < argc && data.dataAvail() > 0; i++) {
324 args.add(data.readString16());
325 }
Dianne Hackborn1941a402016-08-29 12:30:43 -0700326 sp<IShellCallback> shellCallback = IShellCallback::asInterface(
327 data.readStrongBinder());
Dianne Hackborn23eb1e22015-10-07 17:35:27 -0700328 sp<IResultReceiver> resultReceiver = IResultReceiver::asInterface(
329 data.readStrongBinder());
330
Dianne Hackbornf2bf93b2015-10-14 15:13:02 -0700331 // XXX can't add virtuals until binaries are updated.
332 //return shellCommand(in, out, err, args, resultReceiver);
Christopher Wiley0a9a1c12016-07-20 08:28:14 -0700333 (void)in;
334 (void)out;
335 (void)err;
336
Yi Kongfdd8da92018-06-07 17:52:27 -0700337 if (resultReceiver != nullptr) {
Dianne Hackbornf2bf93b2015-10-14 15:13:02 -0700338 resultReceiver->send(INVALID_OPERATION);
339 }
Martijn Coenenaa6ee992017-08-17 15:38:08 +0200340
341 return NO_ERROR;
Dianne Hackborn23eb1e22015-10-07 17:35:27 -0700342 }
343
Dianne Hackborn555f89d2012-05-08 18:54:22 -0700344 case SYSPROPS_TRANSACTION: {
345 report_sysprop_change();
346 return NO_ERROR;
347 }
348
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800349 default:
350 return UNKNOWN_TRANSACTION;
351 }
352}
353
Steven Morelandf0212002018-12-26 13:59:23 -0800354BBinder::Extras* BBinder::getOrCreateExtras()
355{
356 Extras* e = mExtras.load(std::memory_order_acquire);
357
358 if (!e) {
359 e = new Extras;
360 Extras* expected = nullptr;
361 if (!mExtras.compare_exchange_strong(expected, e,
362 std::memory_order_release,
363 std::memory_order_acquire)) {
364 delete e;
365 e = expected; // Filled in by CAS
366 }
367 if (e == nullptr) return nullptr; // out of memory
368 }
369
370 return e;
371}
372
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800373// ---------------------------------------------------------------------------
374
375enum {
376 // This is used to transfer ownership of the remote binder from
377 // the BpRefBase object holding it (when it is constructed), to the
378 // owner of the BpRefBase object when it first acquires that BpRefBase.
379 kRemoteAcquired = 0x00000001
380};
381
382BpRefBase::BpRefBase(const sp<IBinder>& o)
Yi Kongfdd8da92018-06-07 17:52:27 -0700383 : mRemote(o.get()), mRefs(nullptr), mState(0)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800384{
385 extendObjectLifetime(OBJECT_LIFETIME_WEAK);
386
387 if (mRemote) {
388 mRemote->incStrong(this); // Removed on first IncStrong().
389 mRefs = mRemote->createWeak(this); // Held for our entire lifetime.
390 }
391}
392
393BpRefBase::~BpRefBase()
394{
395 if (mRemote) {
Bailey Forrest6913c462015-08-18 17:15:10 -0700396 if (!(mState.load(std::memory_order_relaxed)&kRemoteAcquired)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800397 mRemote->decStrong(this);
398 }
399 mRefs->decWeak(this);
400 }
401}
402
403void BpRefBase::onFirstRef()
404{
Bailey Forrest6913c462015-08-18 17:15:10 -0700405 mState.fetch_or(kRemoteAcquired, std::memory_order_relaxed);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800406}
407
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800408void BpRefBase::onLastStrongRef(const void* /*id*/)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800409{
410 if (mRemote) {
411 mRemote->decStrong(this);
412 }
413}
414
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800415bool BpRefBase::onIncStrongAttempted(uint32_t /*flags*/, const void* /*id*/)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800416{
417 return mRemote ? mRefs->attemptIncStrong(this) : false;
418}
419
420// ---------------------------------------------------------------------------
421
Steven Moreland61ff8492019-09-26 16:05:45 -0700422} // namespace android