blob: 2554351cc672b08ef3b9b0d74caa2d999b42dc03 [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
Hans Boehm08ff8022014-08-12 22:56:00 +000019#include <stdatomic.h>
Dianne Hackborn555f89d2012-05-08 18:54:22 -070020#include <utils/misc.h>
Mathias Agopianc5b2c0b2009-05-19 19:08:10 -070021#include <binder/BpBinder.h>
22#include <binder/IInterface.h>
23#include <binder/Parcel.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080024
25#include <stdio.h>
26
27namespace android {
28
29// ---------------------------------------------------------------------------
30
Mathias Agopian83c04462009-05-22 19:00:22 -070031IBinder::IBinder()
32 : RefBase()
33{
34}
35
36IBinder::~IBinder()
37{
38}
39
40// ---------------------------------------------------------------------------
41
Colin Cross6f4f3ab2014-02-05 17:42:44 -080042sp<IInterface> IBinder::queryLocalInterface(const String16& /*descriptor*/)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080043{
44 return NULL;
45}
46
47BBinder* IBinder::localBinder()
48{
49 return NULL;
50}
51
52BpBinder* IBinder::remoteBinder()
53{
54 return NULL;
55}
56
57bool IBinder::checkSubclass(const void* /*subclassID*/) const
58{
59 return false;
60}
61
62// ---------------------------------------------------------------------------
63
64class BBinder::Extras
65{
66public:
67 Mutex mLock;
68 BpBinder::ObjectManager mObjects;
69};
70
71// ---------------------------------------------------------------------------
72
73BBinder::BBinder()
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080074{
Hans Boehm08ff8022014-08-12 22:56:00 +000075 atomic_init(&mExtras, 0);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080076}
77
78bool BBinder::isBinderAlive() const
79{
80 return true;
81}
82
83status_t BBinder::pingBinder()
84{
85 return NO_ERROR;
86}
87
Mathias Agopian83c04462009-05-22 19:00:22 -070088const String16& BBinder::getInterfaceDescriptor() const
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080089{
Dan Egnor386a3322010-05-06 00:55:09 -070090 // This is a local static rather than a global static,
91 // to avoid static initializer ordering issues.
92 static String16 sEmptyDescriptor;
Steve Block32397c12012-01-05 23:22:43 +000093 ALOGW("reached BBinder::getInterfaceDescriptor (this=%p)", this);
Mathias Agopian83c04462009-05-22 19:00:22 -070094 return sEmptyDescriptor;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080095}
96
97status_t BBinder::transact(
98 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
99{
100 data.setDataPosition(0);
101
102 status_t err = NO_ERROR;
103 switch (code) {
104 case PING_TRANSACTION:
105 reply->writeInt32(pingBinder());
106 break;
107 default:
108 err = onTransact(code, data, reply, flags);
109 break;
110 }
111
112 if (reply != NULL) {
113 reply->setDataPosition(0);
114 }
115
116 return err;
117}
118
119status_t BBinder::linkToDeath(
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800120 const sp<DeathRecipient>& /*recipient*/, void* /*cookie*/,
121 uint32_t /*flags*/)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800122{
123 return INVALID_OPERATION;
124}
125
126status_t BBinder::unlinkToDeath(
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800127 const wp<DeathRecipient>& /*recipient*/, void* /*cookie*/,
128 uint32_t /*flags*/, wp<DeathRecipient>* /*outRecipient*/)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800129{
130 return INVALID_OPERATION;
131}
132
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800133 status_t BBinder::dump(int /*fd*/, const Vector<String16>& /*args*/)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800134{
135 return NO_ERROR;
136}
137
138void BBinder::attachObject(
139 const void* objectID, void* object, void* cleanupCookie,
140 object_cleanup_func func)
141{
Hans Boehm08ff8022014-08-12 22:56:00 +0000142 Extras* e = reinterpret_cast<Extras*>(
143 atomic_load_explicit(&mExtras, memory_order_acquire));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800144
145 if (!e) {
146 e = new Extras;
Hans Boehm553231f2014-08-19 13:42:54 -0700147 uintptr_t expected = 0;
Hans Boehm08ff8022014-08-12 22:56:00 +0000148 if (!atomic_compare_exchange_strong_explicit(
149 &mExtras, &expected,
150 reinterpret_cast<uintptr_t>(e),
151 memory_order_release,
152 memory_order_acquire)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800153 delete e;
Hans Boehm08ff8022014-08-12 22:56:00 +0000154 e = reinterpret_cast<Extras*>(expected); // Filled in by CAS
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800155 }
156 if (e == 0) return; // out of memory
157 }
158
159 AutoMutex _l(e->mLock);
160 e->mObjects.attach(objectID, object, cleanupCookie, func);
161}
162
163void* BBinder::findObject(const void* objectID) const
164{
Hans Boehm08ff8022014-08-12 22:56:00 +0000165 Extras* e = reinterpret_cast<Extras*>(
166 atomic_load_explicit(&mExtras, memory_order_acquire));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800167 if (!e) return NULL;
168
169 AutoMutex _l(e->mLock);
170 return e->mObjects.find(objectID);
171}
172
173void BBinder::detachObject(const void* objectID)
174{
Hans Boehm08ff8022014-08-12 22:56:00 +0000175 Extras* e = reinterpret_cast<Extras*>(
176 atomic_load_explicit(&mExtras, memory_order_acquire));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800177 if (!e) return;
178
179 AutoMutex _l(e->mLock);
180 e->mObjects.detach(objectID);
181}
182
183BBinder* BBinder::localBinder()
184{
185 return this;
186}
187
188BBinder::~BBinder()
189{
Hans Boehm08ff8022014-08-12 22:56:00 +0000190 Extras* e = reinterpret_cast<Extras*>(
191 atomic_load_explicit(&mExtras, memory_order_relaxed));
192 if (e) delete e;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800193}
194
195
196status_t BBinder::onTransact(
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800197 uint32_t code, const Parcel& data, Parcel* reply, uint32_t /*flags*/)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800198{
199 switch (code) {
200 case INTERFACE_TRANSACTION:
201 reply->writeString16(getInterfaceDescriptor());
202 return NO_ERROR;
203
204 case DUMP_TRANSACTION: {
205 int fd = data.readFileDescriptor();
206 int argc = data.readInt32();
207 Vector<String16> args;
208 for (int i = 0; i < argc && data.dataAvail() > 0; i++) {
209 args.add(data.readString16());
210 }
211 return dump(fd, args);
212 }
Dianne Hackborn555f89d2012-05-08 18:54:22 -0700213
214 case SYSPROPS_TRANSACTION: {
215 report_sysprop_change();
216 return NO_ERROR;
217 }
218
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800219 default:
220 return UNKNOWN_TRANSACTION;
221 }
222}
223
224// ---------------------------------------------------------------------------
225
226enum {
227 // This is used to transfer ownership of the remote binder from
228 // the BpRefBase object holding it (when it is constructed), to the
229 // owner of the BpRefBase object when it first acquires that BpRefBase.
230 kRemoteAcquired = 0x00000001
231};
232
233BpRefBase::BpRefBase(const sp<IBinder>& o)
234 : mRemote(o.get()), mRefs(NULL), mState(0)
235{
236 extendObjectLifetime(OBJECT_LIFETIME_WEAK);
237
238 if (mRemote) {
239 mRemote->incStrong(this); // Removed on first IncStrong().
240 mRefs = mRemote->createWeak(this); // Held for our entire lifetime.
241 }
242}
243
244BpRefBase::~BpRefBase()
245{
246 if (mRemote) {
247 if (!(mState&kRemoteAcquired)) {
248 mRemote->decStrong(this);
249 }
250 mRefs->decWeak(this);
251 }
252}
253
254void BpRefBase::onFirstRef()
255{
256 android_atomic_or(kRemoteAcquired, &mState);
257}
258
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800259void BpRefBase::onLastStrongRef(const void* /*id*/)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800260{
261 if (mRemote) {
262 mRemote->decStrong(this);
263 }
264}
265
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800266bool BpRefBase::onIncStrongAttempted(uint32_t /*flags*/, const void* /*id*/)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800267{
268 return mRemote ? mRefs->attemptIncStrong(this) : false;
269}
270
271// ---------------------------------------------------------------------------
272
273}; // namespace android