blob: 221c002594f8914b4627755051c652db61566426 [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
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800102// ---------------------------------------------------------------------------
103
104class BBinder::Extras
105{
106public:
Steven Moreland3085a472018-12-26 13:59:23 -0800107 // unlocked objects
108 bool mRequestingSid = false;
Steven Morelandb8ad08d2019-08-09 14:42:56 -0700109 sp<IBinder> mExtension;
Steven Moreland3085a472018-12-26 13:59:23 -0800110
111 // for below objects
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800112 Mutex mLock;
113 BpBinder::ObjectManager mObjects;
114};
115
116// ---------------------------------------------------------------------------
117
Bailey Forrest6913c462015-08-18 17:15:10 -0700118BBinder::BBinder() : mExtras(nullptr)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800119{
120}
121
122bool BBinder::isBinderAlive() const
123{
124 return true;
125}
126
127status_t BBinder::pingBinder()
128{
129 return NO_ERROR;
130}
131
Mathias Agopian83c04462009-05-22 19:00:22 -0700132const String16& BBinder::getInterfaceDescriptor() const
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800133{
Dan Egnor386a3322010-05-06 00:55:09 -0700134 // This is a local static rather than a global static,
135 // to avoid static initializer ordering issues.
136 static String16 sEmptyDescriptor;
Steve Block32397c12012-01-05 23:22:43 +0000137 ALOGW("reached BBinder::getInterfaceDescriptor (this=%p)", this);
Mathias Agopian83c04462009-05-22 19:00:22 -0700138 return sEmptyDescriptor;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800139}
140
Jiyong Parkb86c8662018-10-29 23:01:57 +0900141// NOLINTNEXTLINE(google-default-arguments)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800142status_t BBinder::transact(
143 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
144{
145 data.setDataPosition(0);
146
147 status_t err = NO_ERROR;
148 switch (code) {
149 case PING_TRANSACTION:
Steven Moreland6e69d652019-07-10 14:17:55 -0700150 err = pingBinder();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800151 break;
Steven Morelandb8ad08d2019-08-09 14:42:56 -0700152 case EXTENSION_TRANSACTION:
153 err = reply->writeStrongBinder(getExtension());
154 break;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800155 default:
156 err = onTransact(code, data, reply, flags);
157 break;
158 }
159
Steven Morelanddea3cf92019-07-16 18:06:55 -0700160 // In case this is being transacted on in the same process.
Yi Kongfdd8da92018-06-07 17:52:27 -0700161 if (reply != nullptr) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800162 reply->setDataPosition(0);
163 }
164
165 return err;
166}
167
Jiyong Parkb86c8662018-10-29 23:01:57 +0900168// NOLINTNEXTLINE(google-default-arguments)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800169status_t BBinder::linkToDeath(
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800170 const sp<DeathRecipient>& /*recipient*/, void* /*cookie*/,
171 uint32_t /*flags*/)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800172{
173 return INVALID_OPERATION;
174}
175
Jiyong Parkb86c8662018-10-29 23:01:57 +0900176// NOLINTNEXTLINE(google-default-arguments)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800177status_t BBinder::unlinkToDeath(
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800178 const wp<DeathRecipient>& /*recipient*/, void* /*cookie*/,
179 uint32_t /*flags*/, wp<DeathRecipient>* /*outRecipient*/)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800180{
181 return INVALID_OPERATION;
182}
183
Dianne Hackborn23eb1e22015-10-07 17:35:27 -0700184status_t BBinder::dump(int /*fd*/, const Vector<String16>& /*args*/)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800185{
186 return NO_ERROR;
187}
188
189void BBinder::attachObject(
190 const void* objectID, void* object, void* cleanupCookie,
191 object_cleanup_func func)
192{
Steven Moreland3085a472018-12-26 13:59:23 -0800193 Extras* e = getOrCreateExtras();
194 if (!e) return; // out of memory
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800195
196 AutoMutex _l(e->mLock);
197 e->mObjects.attach(objectID, object, cleanupCookie, func);
198}
199
200void* BBinder::findObject(const void* objectID) const
201{
Bailey Forrest6913c462015-08-18 17:15:10 -0700202 Extras* e = mExtras.load(std::memory_order_acquire);
Yi Kongfdd8da92018-06-07 17:52:27 -0700203 if (!e) return nullptr;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800204
205 AutoMutex _l(e->mLock);
206 return e->mObjects.find(objectID);
207}
208
209void BBinder::detachObject(const void* objectID)
210{
Bailey Forrest6913c462015-08-18 17:15:10 -0700211 Extras* e = mExtras.load(std::memory_order_acquire);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800212 if (!e) return;
213
214 AutoMutex _l(e->mLock);
215 e->mObjects.detach(objectID);
216}
217
218BBinder* BBinder::localBinder()
219{
220 return this;
221}
222
Steven Moreland3085a472018-12-26 13:59:23 -0800223bool BBinder::isRequestingSid()
224{
225 Extras* e = mExtras.load(std::memory_order_acquire);
226
227 return e && e->mRequestingSid;
228}
229
230void BBinder::setRequestingSid(bool requestingSid)
231{
232 Extras* e = mExtras.load(std::memory_order_acquire);
233
234 if (!e) {
235 // default is false. Most things don't need sids, so avoiding allocations when possible.
236 if (!requestingSid) {
237 return;
238 }
239
240 e = getOrCreateExtras();
241 if (!e) return; // out of memory
242 }
243
244 e->mRequestingSid = true;
245}
246
Steven Morelandb8ad08d2019-08-09 14:42:56 -0700247sp<IBinder> BBinder::getExtension() {
248 Extras* e = mExtras.load(std::memory_order_acquire);
249 if (e == nullptr) return nullptr;
250 return e->mExtension;
251}
252
253void BBinder::setExtension(const sp<IBinder>& extension) {
254 Extras* e = getOrCreateExtras();
255 e->mExtension = extension;
256}
257
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800258BBinder::~BBinder()
259{
Bailey Forrest6913c462015-08-18 17:15:10 -0700260 Extras* e = mExtras.load(std::memory_order_relaxed);
Hans Boehm3effaba2014-08-12 22:56:00 +0000261 if (e) delete e;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800262}
263
264
Jiyong Parkb86c8662018-10-29 23:01:57 +0900265// NOLINTNEXTLINE(google-default-arguments)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800266status_t BBinder::onTransact(
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800267 uint32_t code, const Parcel& data, Parcel* reply, uint32_t /*flags*/)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800268{
269 switch (code) {
270 case INTERFACE_TRANSACTION:
271 reply->writeString16(getInterfaceDescriptor());
272 return NO_ERROR;
273
274 case DUMP_TRANSACTION: {
275 int fd = data.readFileDescriptor();
276 int argc = data.readInt32();
277 Vector<String16> args;
278 for (int i = 0; i < argc && data.dataAvail() > 0; i++) {
279 args.add(data.readString16());
280 }
281 return dump(fd, args);
282 }
Dianne Hackborn555f89d2012-05-08 18:54:22 -0700283
Dianne Hackborn23eb1e22015-10-07 17:35:27 -0700284 case SHELL_COMMAND_TRANSACTION: {
285 int in = data.readFileDescriptor();
286 int out = data.readFileDescriptor();
287 int err = data.readFileDescriptor();
288 int argc = data.readInt32();
289 Vector<String16> args;
290 for (int i = 0; i < argc && data.dataAvail() > 0; i++) {
291 args.add(data.readString16());
292 }
Dianne Hackborn1941a402016-08-29 12:30:43 -0700293 sp<IShellCallback> shellCallback = IShellCallback::asInterface(
294 data.readStrongBinder());
Dianne Hackborn23eb1e22015-10-07 17:35:27 -0700295 sp<IResultReceiver> resultReceiver = IResultReceiver::asInterface(
296 data.readStrongBinder());
297
Dianne Hackbornf2bf93b2015-10-14 15:13:02 -0700298 // XXX can't add virtuals until binaries are updated.
299 //return shellCommand(in, out, err, args, resultReceiver);
Christopher Wiley0a9a1c12016-07-20 08:28:14 -0700300 (void)in;
301 (void)out;
302 (void)err;
303
Yi Kongfdd8da92018-06-07 17:52:27 -0700304 if (resultReceiver != nullptr) {
Dianne Hackbornf2bf93b2015-10-14 15:13:02 -0700305 resultReceiver->send(INVALID_OPERATION);
306 }
Martijn Coenenaa6ee992017-08-17 15:38:08 +0200307
308 return NO_ERROR;
Dianne Hackborn23eb1e22015-10-07 17:35:27 -0700309 }
310
Dianne Hackborn555f89d2012-05-08 18:54:22 -0700311 case SYSPROPS_TRANSACTION: {
312 report_sysprop_change();
313 return NO_ERROR;
314 }
315
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800316 default:
317 return UNKNOWN_TRANSACTION;
318 }
319}
320
Steven Moreland3085a472018-12-26 13:59:23 -0800321BBinder::Extras* BBinder::getOrCreateExtras()
322{
323 Extras* e = mExtras.load(std::memory_order_acquire);
324
325 if (!e) {
326 e = new Extras;
327 Extras* expected = nullptr;
328 if (!mExtras.compare_exchange_strong(expected, e,
329 std::memory_order_release,
330 std::memory_order_acquire)) {
331 delete e;
332 e = expected; // Filled in by CAS
333 }
334 if (e == nullptr) return nullptr; // out of memory
335 }
336
337 return e;
338}
339
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800340// ---------------------------------------------------------------------------
341
342enum {
343 // This is used to transfer ownership of the remote binder from
344 // the BpRefBase object holding it (when it is constructed), to the
345 // owner of the BpRefBase object when it first acquires that BpRefBase.
346 kRemoteAcquired = 0x00000001
347};
348
349BpRefBase::BpRefBase(const sp<IBinder>& o)
Yi Kongfdd8da92018-06-07 17:52:27 -0700350 : mRemote(o.get()), mRefs(nullptr), mState(0)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800351{
352 extendObjectLifetime(OBJECT_LIFETIME_WEAK);
353
354 if (mRemote) {
355 mRemote->incStrong(this); // Removed on first IncStrong().
356 mRefs = mRemote->createWeak(this); // Held for our entire lifetime.
357 }
358}
359
360BpRefBase::~BpRefBase()
361{
362 if (mRemote) {
Bailey Forrest6913c462015-08-18 17:15:10 -0700363 if (!(mState.load(std::memory_order_relaxed)&kRemoteAcquired)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800364 mRemote->decStrong(this);
365 }
366 mRefs->decWeak(this);
367 }
368}
369
370void BpRefBase::onFirstRef()
371{
Bailey Forrest6913c462015-08-18 17:15:10 -0700372 mState.fetch_or(kRemoteAcquired, std::memory_order_relaxed);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800373}
374
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800375void BpRefBase::onLastStrongRef(const void* /*id*/)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800376{
377 if (mRemote) {
378 mRemote->decStrong(this);
379 }
380}
381
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800382bool BpRefBase::onIncStrongAttempted(uint32_t /*flags*/, const void* /*id*/)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800383{
384 return mRemote ? mRefs->attemptIncStrong(this) : false;
385}
386
387// ---------------------------------------------------------------------------
388
389}; // namespace android