blob: 985d8177f8ff7ddde879713387e90e1bd08a4035 [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
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080084// ---------------------------------------------------------------------------
85
86class BBinder::Extras
87{
88public:
Steven Moreland3085a472018-12-26 13:59:23 -080089 // unlocked objects
90 bool mRequestingSid = false;
91
92 // for below objects
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080093 Mutex mLock;
94 BpBinder::ObjectManager mObjects;
95};
96
97// ---------------------------------------------------------------------------
98
Bailey Forrest6913c462015-08-18 17:15:10 -070099BBinder::BBinder() : mExtras(nullptr)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800100{
101}
102
103bool BBinder::isBinderAlive() const
104{
105 return true;
106}
107
108status_t BBinder::pingBinder()
109{
110 return NO_ERROR;
111}
112
Mathias Agopian83c04462009-05-22 19:00:22 -0700113const String16& BBinder::getInterfaceDescriptor() const
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800114{
Dan Egnor386a3322010-05-06 00:55:09 -0700115 // This is a local static rather than a global static,
116 // to avoid static initializer ordering issues.
117 static String16 sEmptyDescriptor;
Steve Block32397c12012-01-05 23:22:43 +0000118 ALOGW("reached BBinder::getInterfaceDescriptor (this=%p)", this);
Mathias Agopian83c04462009-05-22 19:00:22 -0700119 return sEmptyDescriptor;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800120}
121
Jiyong Parkb86c8662018-10-29 23:01:57 +0900122// NOLINTNEXTLINE(google-default-arguments)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800123status_t BBinder::transact(
124 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
125{
126 data.setDataPosition(0);
Steven Morelanddea3cf92019-07-16 18:06:55 -0700127 data.setTransactingBinder(this);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800128
129 status_t err = NO_ERROR;
130 switch (code) {
131 case PING_TRANSACTION:
Steven Moreland6e69d652019-07-10 14:17:55 -0700132 err = pingBinder();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800133 break;
134 default:
135 err = onTransact(code, data, reply, flags);
136 break;
137 }
138
Steven Morelanddea3cf92019-07-16 18:06:55 -0700139 // In case this is being transacted on in the same process.
Yi Kongfdd8da92018-06-07 17:52:27 -0700140 if (reply != nullptr) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800141 reply->setDataPosition(0);
Steven Morelanddea3cf92019-07-16 18:06:55 -0700142 reply->setTransactingBinder(this);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800143 }
144
145 return err;
146}
147
Jiyong Parkb86c8662018-10-29 23:01:57 +0900148// NOLINTNEXTLINE(google-default-arguments)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800149status_t BBinder::linkToDeath(
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800150 const sp<DeathRecipient>& /*recipient*/, void* /*cookie*/,
151 uint32_t /*flags*/)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800152{
153 return INVALID_OPERATION;
154}
155
Jiyong Parkb86c8662018-10-29 23:01:57 +0900156// NOLINTNEXTLINE(google-default-arguments)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800157status_t BBinder::unlinkToDeath(
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800158 const wp<DeathRecipient>& /*recipient*/, void* /*cookie*/,
159 uint32_t /*flags*/, wp<DeathRecipient>* /*outRecipient*/)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800160{
161 return INVALID_OPERATION;
162}
163
Dianne Hackborn23eb1e22015-10-07 17:35:27 -0700164status_t BBinder::dump(int /*fd*/, const Vector<String16>& /*args*/)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800165{
166 return NO_ERROR;
167}
168
169void BBinder::attachObject(
170 const void* objectID, void* object, void* cleanupCookie,
171 object_cleanup_func func)
172{
Steven Moreland3085a472018-12-26 13:59:23 -0800173 Extras* e = getOrCreateExtras();
174 if (!e) return; // out of memory
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800175
176 AutoMutex _l(e->mLock);
177 e->mObjects.attach(objectID, object, cleanupCookie, func);
178}
179
180void* BBinder::findObject(const void* objectID) const
181{
Bailey Forrest6913c462015-08-18 17:15:10 -0700182 Extras* e = mExtras.load(std::memory_order_acquire);
Yi Kongfdd8da92018-06-07 17:52:27 -0700183 if (!e) return nullptr;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800184
185 AutoMutex _l(e->mLock);
186 return e->mObjects.find(objectID);
187}
188
189void BBinder::detachObject(const void* objectID)
190{
Bailey Forrest6913c462015-08-18 17:15:10 -0700191 Extras* e = mExtras.load(std::memory_order_acquire);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800192 if (!e) return;
193
194 AutoMutex _l(e->mLock);
195 e->mObjects.detach(objectID);
196}
197
198BBinder* BBinder::localBinder()
199{
200 return this;
201}
202
Steven Moreland3085a472018-12-26 13:59:23 -0800203bool BBinder::isRequestingSid()
204{
205 Extras* e = mExtras.load(std::memory_order_acquire);
206
207 return e && e->mRequestingSid;
208}
209
210void BBinder::setRequestingSid(bool requestingSid)
211{
212 Extras* e = mExtras.load(std::memory_order_acquire);
213
214 if (!e) {
215 // default is false. Most things don't need sids, so avoiding allocations when possible.
216 if (!requestingSid) {
217 return;
218 }
219
220 e = getOrCreateExtras();
221 if (!e) return; // out of memory
222 }
223
224 e->mRequestingSid = true;
225}
226
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800227BBinder::~BBinder()
228{
Bailey Forrest6913c462015-08-18 17:15:10 -0700229 Extras* e = mExtras.load(std::memory_order_relaxed);
Hans Boehm3effaba2014-08-12 22:56:00 +0000230 if (e) delete e;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800231}
232
233
Jiyong Parkb86c8662018-10-29 23:01:57 +0900234// NOLINTNEXTLINE(google-default-arguments)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800235status_t BBinder::onTransact(
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800236 uint32_t code, const Parcel& data, Parcel* reply, uint32_t /*flags*/)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800237{
238 switch (code) {
239 case INTERFACE_TRANSACTION:
240 reply->writeString16(getInterfaceDescriptor());
241 return NO_ERROR;
242
243 case DUMP_TRANSACTION: {
244 int fd = data.readFileDescriptor();
245 int argc = data.readInt32();
246 Vector<String16> args;
247 for (int i = 0; i < argc && data.dataAvail() > 0; i++) {
248 args.add(data.readString16());
249 }
250 return dump(fd, args);
251 }
Dianne Hackborn555f89d2012-05-08 18:54:22 -0700252
Dianne Hackborn23eb1e22015-10-07 17:35:27 -0700253 case SHELL_COMMAND_TRANSACTION: {
254 int in = data.readFileDescriptor();
255 int out = data.readFileDescriptor();
256 int err = data.readFileDescriptor();
257 int argc = data.readInt32();
258 Vector<String16> args;
259 for (int i = 0; i < argc && data.dataAvail() > 0; i++) {
260 args.add(data.readString16());
261 }
Dianne Hackborn1941a402016-08-29 12:30:43 -0700262 sp<IShellCallback> shellCallback = IShellCallback::asInterface(
263 data.readStrongBinder());
Dianne Hackborn23eb1e22015-10-07 17:35:27 -0700264 sp<IResultReceiver> resultReceiver = IResultReceiver::asInterface(
265 data.readStrongBinder());
266
Dianne Hackbornf2bf93b2015-10-14 15:13:02 -0700267 // XXX can't add virtuals until binaries are updated.
268 //return shellCommand(in, out, err, args, resultReceiver);
Christopher Wiley0a9a1c12016-07-20 08:28:14 -0700269 (void)in;
270 (void)out;
271 (void)err;
272
Yi Kongfdd8da92018-06-07 17:52:27 -0700273 if (resultReceiver != nullptr) {
Dianne Hackbornf2bf93b2015-10-14 15:13:02 -0700274 resultReceiver->send(INVALID_OPERATION);
275 }
Martijn Coenenaa6ee992017-08-17 15:38:08 +0200276
277 return NO_ERROR;
Dianne Hackborn23eb1e22015-10-07 17:35:27 -0700278 }
279
Dianne Hackborn555f89d2012-05-08 18:54:22 -0700280 case SYSPROPS_TRANSACTION: {
281 report_sysprop_change();
282 return NO_ERROR;
283 }
284
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800285 default:
286 return UNKNOWN_TRANSACTION;
287 }
288}
289
Steven Moreland3085a472018-12-26 13:59:23 -0800290BBinder::Extras* BBinder::getOrCreateExtras()
291{
292 Extras* e = mExtras.load(std::memory_order_acquire);
293
294 if (!e) {
295 e = new Extras;
296 Extras* expected = nullptr;
297 if (!mExtras.compare_exchange_strong(expected, e,
298 std::memory_order_release,
299 std::memory_order_acquire)) {
300 delete e;
301 e = expected; // Filled in by CAS
302 }
303 if (e == nullptr) return nullptr; // out of memory
304 }
305
306 return e;
307}
308
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800309// ---------------------------------------------------------------------------
310
311enum {
312 // This is used to transfer ownership of the remote binder from
313 // the BpRefBase object holding it (when it is constructed), to the
314 // owner of the BpRefBase object when it first acquires that BpRefBase.
315 kRemoteAcquired = 0x00000001
316};
317
318BpRefBase::BpRefBase(const sp<IBinder>& o)
Yi Kongfdd8da92018-06-07 17:52:27 -0700319 : mRemote(o.get()), mRefs(nullptr), mState(0)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800320{
321 extendObjectLifetime(OBJECT_LIFETIME_WEAK);
322
323 if (mRemote) {
324 mRemote->incStrong(this); // Removed on first IncStrong().
325 mRefs = mRemote->createWeak(this); // Held for our entire lifetime.
326 }
327}
328
329BpRefBase::~BpRefBase()
330{
331 if (mRemote) {
Bailey Forrest6913c462015-08-18 17:15:10 -0700332 if (!(mState.load(std::memory_order_relaxed)&kRemoteAcquired)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800333 mRemote->decStrong(this);
334 }
335 mRefs->decWeak(this);
336 }
337}
338
339void BpRefBase::onFirstRef()
340{
Bailey Forrest6913c462015-08-18 17:15:10 -0700341 mState.fetch_or(kRemoteAcquired, std::memory_order_relaxed);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800342}
343
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800344void BpRefBase::onLastStrongRef(const void* /*id*/)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800345{
346 if (mRemote) {
347 mRemote->decStrong(this);
348 }
349}
350
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800351bool BpRefBase::onIncStrongAttempted(uint32_t /*flags*/, const void* /*id*/)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800352{
353 return mRemote ? mRefs->attemptIncStrong(this) : false;
354}
355
356// ---------------------------------------------------------------------------
357
358}; // namespace android