blob: 7ce2a318a949bb1648130b6d3a6842ab466c0cc1 [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>
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>
Dianne Hackborn23eb1e22015-10-07 17:35:27 -070023#include <binder/IResultReceiver.h>
Mathias Agopianc5b2c0b2009-05-19 19:08:10 -070024#include <binder/Parcel.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080025
26#include <stdio.h>
27
28namespace android {
29
30// ---------------------------------------------------------------------------
31
Mathias Agopian83c04462009-05-22 19:00:22 -070032IBinder::IBinder()
33 : RefBase()
34{
35}
36
37IBinder::~IBinder()
38{
39}
40
41// ---------------------------------------------------------------------------
42
Colin Cross6f4f3ab2014-02-05 17:42:44 -080043sp<IInterface> IBinder::queryLocalInterface(const String16& /*descriptor*/)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080044{
45 return NULL;
46}
47
48BBinder* IBinder::localBinder()
49{
50 return NULL;
51}
52
53BpBinder* IBinder::remoteBinder()
54{
55 return NULL;
56}
57
58bool IBinder::checkSubclass(const void* /*subclassID*/) const
59{
60 return false;
61}
62
Dianne Hackborn23eb1e22015-10-07 17:35:27 -070063
Dianne Hackbornf2bf93b2015-10-14 15:13:02 -070064status_t IBinder::shellCommand(const sp<IBinder>& target, int in, int out, int err,
65 Vector<String16>& args, const sp<IResultReceiver>& resultReceiver)
Dianne Hackborn23eb1e22015-10-07 17:35:27 -070066{
Dianne Hackbornf2bf93b2015-10-14 15:13:02 -070067 Parcel send;
68 Parcel reply;
69 send.writeFileDescriptor(in);
70 send.writeFileDescriptor(out);
71 send.writeFileDescriptor(err);
72 const size_t numArgs = args.size();
73 send.writeInt32(numArgs);
74 for (size_t i = 0; i < numArgs; i++) {
75 send.writeString16(args[i]);
Dianne Hackborn23eb1e22015-10-07 17:35:27 -070076 }
Dianne Hackbornf2bf93b2015-10-14 15:13:02 -070077 send.writeStrongBinder(resultReceiver != NULL ? IInterface::asBinder(resultReceiver) : NULL);
78 return target->transact(SHELL_COMMAND_TRANSACTION, send, &reply);
Dianne Hackborn23eb1e22015-10-07 17:35:27 -070079}
80
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080081// ---------------------------------------------------------------------------
82
83class BBinder::Extras
84{
85public:
86 Mutex mLock;
87 BpBinder::ObjectManager mObjects;
88};
89
90// ---------------------------------------------------------------------------
91
Bailey Forrest6913c462015-08-18 17:15:10 -070092BBinder::BBinder() : mExtras(nullptr)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080093{
94}
95
96bool BBinder::isBinderAlive() const
97{
98 return true;
99}
100
101status_t BBinder::pingBinder()
102{
103 return NO_ERROR;
104}
105
Mathias Agopian83c04462009-05-22 19:00:22 -0700106const String16& BBinder::getInterfaceDescriptor() const
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800107{
Dan Egnor386a3322010-05-06 00:55:09 -0700108 // This is a local static rather than a global static,
109 // to avoid static initializer ordering issues.
110 static String16 sEmptyDescriptor;
Steve Block32397c12012-01-05 23:22:43 +0000111 ALOGW("reached BBinder::getInterfaceDescriptor (this=%p)", this);
Mathias Agopian83c04462009-05-22 19:00:22 -0700112 return sEmptyDescriptor;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800113}
114
115status_t BBinder::transact(
116 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
117{
118 data.setDataPosition(0);
119
120 status_t err = NO_ERROR;
121 switch (code) {
122 case PING_TRANSACTION:
123 reply->writeInt32(pingBinder());
124 break;
125 default:
126 err = onTransact(code, data, reply, flags);
127 break;
128 }
129
130 if (reply != NULL) {
131 reply->setDataPosition(0);
132 }
133
134 return err;
135}
136
137status_t BBinder::linkToDeath(
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800138 const sp<DeathRecipient>& /*recipient*/, void* /*cookie*/,
139 uint32_t /*flags*/)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800140{
141 return INVALID_OPERATION;
142}
143
144status_t BBinder::unlinkToDeath(
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800145 const wp<DeathRecipient>& /*recipient*/, void* /*cookie*/,
146 uint32_t /*flags*/, wp<DeathRecipient>* /*outRecipient*/)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800147{
148 return INVALID_OPERATION;
149}
150
Dianne Hackborn23eb1e22015-10-07 17:35:27 -0700151status_t BBinder::dump(int /*fd*/, const Vector<String16>& /*args*/)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800152{
153 return NO_ERROR;
154}
155
156void BBinder::attachObject(
157 const void* objectID, void* object, void* cleanupCookie,
158 object_cleanup_func func)
159{
Bailey Forrest6913c462015-08-18 17:15:10 -0700160 Extras* e = mExtras.load(std::memory_order_acquire);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800161
162 if (!e) {
163 e = new Extras;
Bailey Forrest6913c462015-08-18 17:15:10 -0700164 Extras* expected = nullptr;
165 if (!mExtras.compare_exchange_strong(expected, e,
166 std::memory_order_release,
167 std::memory_order_acquire)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800168 delete e;
Bailey Forrest6913c462015-08-18 17:15:10 -0700169 e = expected; // Filled in by CAS
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800170 }
171 if (e == 0) return; // out of memory
172 }
173
174 AutoMutex _l(e->mLock);
175 e->mObjects.attach(objectID, object, cleanupCookie, func);
176}
177
178void* BBinder::findObject(const void* objectID) const
179{
Bailey Forrest6913c462015-08-18 17:15:10 -0700180 Extras* e = mExtras.load(std::memory_order_acquire);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800181 if (!e) return NULL;
182
183 AutoMutex _l(e->mLock);
184 return e->mObjects.find(objectID);
185}
186
187void BBinder::detachObject(const void* objectID)
188{
Bailey Forrest6913c462015-08-18 17:15:10 -0700189 Extras* e = mExtras.load(std::memory_order_acquire);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800190 if (!e) return;
191
192 AutoMutex _l(e->mLock);
193 e->mObjects.detach(objectID);
194}
195
196BBinder* BBinder::localBinder()
197{
198 return this;
199}
200
201BBinder::~BBinder()
202{
Bailey Forrest6913c462015-08-18 17:15:10 -0700203 Extras* e = mExtras.load(std::memory_order_relaxed);
Hans Boehm3effaba2014-08-12 22:56:00 +0000204 if (e) delete e;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800205}
206
207
208status_t BBinder::onTransact(
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800209 uint32_t code, const Parcel& data, Parcel* reply, uint32_t /*flags*/)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800210{
211 switch (code) {
212 case INTERFACE_TRANSACTION:
213 reply->writeString16(getInterfaceDescriptor());
214 return NO_ERROR;
215
216 case DUMP_TRANSACTION: {
217 int fd = data.readFileDescriptor();
218 int argc = data.readInt32();
219 Vector<String16> args;
220 for (int i = 0; i < argc && data.dataAvail() > 0; i++) {
221 args.add(data.readString16());
222 }
223 return dump(fd, args);
224 }
Dianne Hackborn555f89d2012-05-08 18:54:22 -0700225
Dianne Hackborn23eb1e22015-10-07 17:35:27 -0700226 case SHELL_COMMAND_TRANSACTION: {
227 int in = data.readFileDescriptor();
228 int out = data.readFileDescriptor();
229 int err = data.readFileDescriptor();
230 int argc = data.readInt32();
231 Vector<String16> args;
232 for (int i = 0; i < argc && data.dataAvail() > 0; i++) {
233 args.add(data.readString16());
234 }
235 sp<IResultReceiver> resultReceiver = IResultReceiver::asInterface(
236 data.readStrongBinder());
237
Dianne Hackbornf2bf93b2015-10-14 15:13:02 -0700238 // XXX can't add virtuals until binaries are updated.
239 //return shellCommand(in, out, err, args, resultReceiver);
Christopher Wiley0a9a1c12016-07-20 08:28:14 -0700240 (void)in;
241 (void)out;
242 (void)err;
243
Dianne Hackbornf2bf93b2015-10-14 15:13:02 -0700244 if (resultReceiver != NULL) {
245 resultReceiver->send(INVALID_OPERATION);
246 }
Dianne Hackborn23eb1e22015-10-07 17:35:27 -0700247 }
248
Dianne Hackborn555f89d2012-05-08 18:54:22 -0700249 case SYSPROPS_TRANSACTION: {
250 report_sysprop_change();
251 return NO_ERROR;
252 }
253
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800254 default:
255 return UNKNOWN_TRANSACTION;
256 }
257}
258
259// ---------------------------------------------------------------------------
260
261enum {
262 // This is used to transfer ownership of the remote binder from
263 // the BpRefBase object holding it (when it is constructed), to the
264 // owner of the BpRefBase object when it first acquires that BpRefBase.
265 kRemoteAcquired = 0x00000001
266};
267
268BpRefBase::BpRefBase(const sp<IBinder>& o)
269 : mRemote(o.get()), mRefs(NULL), mState(0)
270{
271 extendObjectLifetime(OBJECT_LIFETIME_WEAK);
272
273 if (mRemote) {
274 mRemote->incStrong(this); // Removed on first IncStrong().
275 mRefs = mRemote->createWeak(this); // Held for our entire lifetime.
276 }
277}
278
279BpRefBase::~BpRefBase()
280{
281 if (mRemote) {
Bailey Forrest6913c462015-08-18 17:15:10 -0700282 if (!(mState.load(std::memory_order_relaxed)&kRemoteAcquired)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800283 mRemote->decStrong(this);
284 }
285 mRefs->decWeak(this);
286 }
287}
288
289void BpRefBase::onFirstRef()
290{
Bailey Forrest6913c462015-08-18 17:15:10 -0700291 mState.fetch_or(kRemoteAcquired, std::memory_order_relaxed);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800292}
293
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800294void BpRefBase::onLastStrongRef(const void* /*id*/)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800295{
296 if (mRemote) {
297 mRemote->decStrong(this);
298 }
299}
300
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800301bool BpRefBase::onIncStrongAttempted(uint32_t /*flags*/, const void* /*id*/)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800302{
303 return mRemote ? mRefs->attemptIncStrong(this) : false;
304}
305
306// ---------------------------------------------------------------------------
307
308}; // namespace android