blob: 890ef30ec768764c20be1e0605e343298f032aef [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>
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{
46 return NULL;
47}
48
49BBinder* IBinder::localBinder()
50{
51 return NULL;
52}
53
54BpBinder* IBinder::remoteBinder()
55{
56 return NULL;
57}
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 }
Dianne Hackborn1941a402016-08-29 12:30:43 -070079 send.writeStrongBinder(callback != NULL ? IInterface::asBinder(callback) : NULL);
Dianne Hackbornf2bf93b2015-10-14 15:13:02 -070080 send.writeStrongBinder(resultReceiver != NULL ? IInterface::asBinder(resultReceiver) : NULL);
81 return target->transact(SHELL_COMMAND_TRANSACTION, send, &reply);
Dianne Hackborn23eb1e22015-10-07 17:35:27 -070082}
83
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080084// ---------------------------------------------------------------------------
85
86class BBinder::Extras
87{
88public:
89 Mutex mLock;
90 BpBinder::ObjectManager mObjects;
91};
92
93// ---------------------------------------------------------------------------
94
Bailey Forrest6913c462015-08-18 17:15:10 -070095BBinder::BBinder() : mExtras(nullptr)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080096{
97}
98
99bool BBinder::isBinderAlive() const
100{
101 return true;
102}
103
104status_t BBinder::pingBinder()
105{
106 return NO_ERROR;
107}
108
Mathias Agopian83c04462009-05-22 19:00:22 -0700109const String16& BBinder::getInterfaceDescriptor() const
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800110{
Dan Egnor386a3322010-05-06 00:55:09 -0700111 // This is a local static rather than a global static,
112 // to avoid static initializer ordering issues.
113 static String16 sEmptyDescriptor;
Steve Block32397c12012-01-05 23:22:43 +0000114 ALOGW("reached BBinder::getInterfaceDescriptor (this=%p)", this);
Mathias Agopian83c04462009-05-22 19:00:22 -0700115 return sEmptyDescriptor;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800116}
117
118status_t BBinder::transact(
119 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
120{
121 data.setDataPosition(0);
122
123 status_t err = NO_ERROR;
124 switch (code) {
125 case PING_TRANSACTION:
126 reply->writeInt32(pingBinder());
127 break;
128 default:
129 err = onTransact(code, data, reply, flags);
130 break;
131 }
132
133 if (reply != NULL) {
134 reply->setDataPosition(0);
135 }
136
137 return err;
138}
139
140status_t BBinder::linkToDeath(
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800141 const sp<DeathRecipient>& /*recipient*/, void* /*cookie*/,
142 uint32_t /*flags*/)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800143{
144 return INVALID_OPERATION;
145}
146
147status_t BBinder::unlinkToDeath(
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800148 const wp<DeathRecipient>& /*recipient*/, void* /*cookie*/,
149 uint32_t /*flags*/, wp<DeathRecipient>* /*outRecipient*/)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800150{
151 return INVALID_OPERATION;
152}
153
Dianne Hackborn23eb1e22015-10-07 17:35:27 -0700154status_t BBinder::dump(int /*fd*/, const Vector<String16>& /*args*/)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800155{
156 return NO_ERROR;
157}
158
159void BBinder::attachObject(
160 const void* objectID, void* object, void* cleanupCookie,
161 object_cleanup_func func)
162{
Bailey Forrest6913c462015-08-18 17:15:10 -0700163 Extras* e = mExtras.load(std::memory_order_acquire);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800164
165 if (!e) {
166 e = new Extras;
Bailey Forrest6913c462015-08-18 17:15:10 -0700167 Extras* expected = nullptr;
168 if (!mExtras.compare_exchange_strong(expected, e,
169 std::memory_order_release,
170 std::memory_order_acquire)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800171 delete e;
Bailey Forrest6913c462015-08-18 17:15:10 -0700172 e = expected; // Filled in by CAS
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800173 }
174 if (e == 0) return; // out of memory
175 }
176
177 AutoMutex _l(e->mLock);
178 e->mObjects.attach(objectID, object, cleanupCookie, func);
179}
180
181void* BBinder::findObject(const void* objectID) const
182{
Bailey Forrest6913c462015-08-18 17:15:10 -0700183 Extras* e = mExtras.load(std::memory_order_acquire);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800184 if (!e) return NULL;
185
186 AutoMutex _l(e->mLock);
187 return e->mObjects.find(objectID);
188}
189
190void BBinder::detachObject(const void* objectID)
191{
Bailey Forrest6913c462015-08-18 17:15:10 -0700192 Extras* e = mExtras.load(std::memory_order_acquire);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800193 if (!e) return;
194
195 AutoMutex _l(e->mLock);
196 e->mObjects.detach(objectID);
197}
198
199BBinder* BBinder::localBinder()
200{
201 return this;
202}
203
204BBinder::~BBinder()
205{
Bailey Forrest6913c462015-08-18 17:15:10 -0700206 Extras* e = mExtras.load(std::memory_order_relaxed);
Hans Boehm3effaba2014-08-12 22:56:00 +0000207 if (e) delete e;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800208}
209
210
211status_t BBinder::onTransact(
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800212 uint32_t code, const Parcel& data, Parcel* reply, uint32_t /*flags*/)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800213{
214 switch (code) {
215 case INTERFACE_TRANSACTION:
216 reply->writeString16(getInterfaceDescriptor());
217 return NO_ERROR;
218
219 case DUMP_TRANSACTION: {
220 int fd = data.readFileDescriptor();
221 int argc = data.readInt32();
222 Vector<String16> args;
223 for (int i = 0; i < argc && data.dataAvail() > 0; i++) {
224 args.add(data.readString16());
225 }
226 return dump(fd, args);
227 }
Dianne Hackborn555f89d2012-05-08 18:54:22 -0700228
Dianne Hackborn23eb1e22015-10-07 17:35:27 -0700229 case SHELL_COMMAND_TRANSACTION: {
230 int in = data.readFileDescriptor();
231 int out = data.readFileDescriptor();
232 int err = data.readFileDescriptor();
233 int argc = data.readInt32();
234 Vector<String16> args;
235 for (int i = 0; i < argc && data.dataAvail() > 0; i++) {
236 args.add(data.readString16());
237 }
Dianne Hackborn1941a402016-08-29 12:30:43 -0700238 sp<IShellCallback> shellCallback = IShellCallback::asInterface(
239 data.readStrongBinder());
Dianne Hackborn23eb1e22015-10-07 17:35:27 -0700240 sp<IResultReceiver> resultReceiver = IResultReceiver::asInterface(
241 data.readStrongBinder());
242
Dianne Hackbornf2bf93b2015-10-14 15:13:02 -0700243 // XXX can't add virtuals until binaries are updated.
244 //return shellCommand(in, out, err, args, resultReceiver);
Christopher Wiley0a9a1c12016-07-20 08:28:14 -0700245 (void)in;
246 (void)out;
247 (void)err;
248
Dianne Hackbornf2bf93b2015-10-14 15:13:02 -0700249 if (resultReceiver != NULL) {
250 resultReceiver->send(INVALID_OPERATION);
251 }
Dianne Hackborn23eb1e22015-10-07 17:35:27 -0700252 }
253
Dianne Hackborn555f89d2012-05-08 18:54:22 -0700254 case SYSPROPS_TRANSACTION: {
255 report_sysprop_change();
256 return NO_ERROR;
257 }
258
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800259 default:
260 return UNKNOWN_TRANSACTION;
261 }
262}
263
264// ---------------------------------------------------------------------------
265
266enum {
267 // This is used to transfer ownership of the remote binder from
268 // the BpRefBase object holding it (when it is constructed), to the
269 // owner of the BpRefBase object when it first acquires that BpRefBase.
270 kRemoteAcquired = 0x00000001
271};
272
273BpRefBase::BpRefBase(const sp<IBinder>& o)
274 : mRemote(o.get()), mRefs(NULL), mState(0)
275{
276 extendObjectLifetime(OBJECT_LIFETIME_WEAK);
277
278 if (mRemote) {
279 mRemote->incStrong(this); // Removed on first IncStrong().
280 mRefs = mRemote->createWeak(this); // Held for our entire lifetime.
281 }
282}
283
284BpRefBase::~BpRefBase()
285{
286 if (mRemote) {
Bailey Forrest6913c462015-08-18 17:15:10 -0700287 if (!(mState.load(std::memory_order_relaxed)&kRemoteAcquired)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800288 mRemote->decStrong(this);
289 }
290 mRefs->decWeak(this);
291 }
292}
293
294void BpRefBase::onFirstRef()
295{
Bailey Forrest6913c462015-08-18 17:15:10 -0700296 mState.fetch_or(kRemoteAcquired, std::memory_order_relaxed);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800297}
298
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800299void BpRefBase::onLastStrongRef(const void* /*id*/)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800300{
301 if (mRemote) {
302 mRemote->decStrong(this);
303 }
304}
305
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800306bool BpRefBase::onIncStrongAttempted(uint32_t /*flags*/, const void* /*id*/)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800307{
308 return mRemote ? mRefs->attemptIncStrong(this) : false;
309}
310
311// ---------------------------------------------------------------------------
312
313}; // namespace android