blob: 9e55c2c8c3aea4cd3b1bd2e25d813b92dd47cdbe [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);
127
128 status_t err = NO_ERROR;
129 switch (code) {
130 case PING_TRANSACTION:
Steven Moreland6e69d652019-07-10 14:17:55 -0700131 err = pingBinder();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800132 break;
133 default:
134 err = onTransact(code, data, reply, flags);
135 break;
136 }
137
Steven Morelanddea3cf92019-07-16 18:06:55 -0700138 // In case this is being transacted on in the same process.
Yi Kongfdd8da92018-06-07 17:52:27 -0700139 if (reply != nullptr) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800140 reply->setDataPosition(0);
141 }
142
143 return err;
144}
145
Jiyong Parkb86c8662018-10-29 23:01:57 +0900146// NOLINTNEXTLINE(google-default-arguments)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800147status_t BBinder::linkToDeath(
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800148 const sp<DeathRecipient>& /*recipient*/, void* /*cookie*/,
149 uint32_t /*flags*/)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800150{
151 return INVALID_OPERATION;
152}
153
Jiyong Parkb86c8662018-10-29 23:01:57 +0900154// NOLINTNEXTLINE(google-default-arguments)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800155status_t BBinder::unlinkToDeath(
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800156 const wp<DeathRecipient>& /*recipient*/, void* /*cookie*/,
157 uint32_t /*flags*/, wp<DeathRecipient>* /*outRecipient*/)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800158{
159 return INVALID_OPERATION;
160}
161
Dianne Hackborn23eb1e22015-10-07 17:35:27 -0700162status_t BBinder::dump(int /*fd*/, const Vector<String16>& /*args*/)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800163{
164 return NO_ERROR;
165}
166
167void BBinder::attachObject(
168 const void* objectID, void* object, void* cleanupCookie,
169 object_cleanup_func func)
170{
Steven Moreland3085a472018-12-26 13:59:23 -0800171 Extras* e = getOrCreateExtras();
172 if (!e) return; // out of memory
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800173
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);
Yi Kongfdd8da92018-06-07 17:52:27 -0700181 if (!e) return nullptr;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800182
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
Steven Moreland3085a472018-12-26 13:59:23 -0800201bool BBinder::isRequestingSid()
202{
203 Extras* e = mExtras.load(std::memory_order_acquire);
204
205 return e && e->mRequestingSid;
206}
207
208void BBinder::setRequestingSid(bool requestingSid)
209{
210 Extras* e = mExtras.load(std::memory_order_acquire);
211
212 if (!e) {
213 // default is false. Most things don't need sids, so avoiding allocations when possible.
214 if (!requestingSid) {
215 return;
216 }
217
218 e = getOrCreateExtras();
219 if (!e) return; // out of memory
220 }
221
222 e->mRequestingSid = true;
223}
224
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800225BBinder::~BBinder()
226{
Bailey Forrest6913c462015-08-18 17:15:10 -0700227 Extras* e = mExtras.load(std::memory_order_relaxed);
Hans Boehm3effaba2014-08-12 22:56:00 +0000228 if (e) delete e;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800229}
230
231
Jiyong Parkb86c8662018-10-29 23:01:57 +0900232// NOLINTNEXTLINE(google-default-arguments)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800233status_t BBinder::onTransact(
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800234 uint32_t code, const Parcel& data, Parcel* reply, uint32_t /*flags*/)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800235{
236 switch (code) {
237 case INTERFACE_TRANSACTION:
238 reply->writeString16(getInterfaceDescriptor());
239 return NO_ERROR;
240
241 case DUMP_TRANSACTION: {
242 int fd = data.readFileDescriptor();
243 int argc = data.readInt32();
244 Vector<String16> args;
245 for (int i = 0; i < argc && data.dataAvail() > 0; i++) {
246 args.add(data.readString16());
247 }
248 return dump(fd, args);
249 }
Dianne Hackborn555f89d2012-05-08 18:54:22 -0700250
Dianne Hackborn23eb1e22015-10-07 17:35:27 -0700251 case SHELL_COMMAND_TRANSACTION: {
252 int in = data.readFileDescriptor();
253 int out = data.readFileDescriptor();
254 int err = data.readFileDescriptor();
255 int argc = data.readInt32();
256 Vector<String16> args;
257 for (int i = 0; i < argc && data.dataAvail() > 0; i++) {
258 args.add(data.readString16());
259 }
Dianne Hackborn1941a402016-08-29 12:30:43 -0700260 sp<IShellCallback> shellCallback = IShellCallback::asInterface(
261 data.readStrongBinder());
Dianne Hackborn23eb1e22015-10-07 17:35:27 -0700262 sp<IResultReceiver> resultReceiver = IResultReceiver::asInterface(
263 data.readStrongBinder());
264
Dianne Hackbornf2bf93b2015-10-14 15:13:02 -0700265 // XXX can't add virtuals until binaries are updated.
266 //return shellCommand(in, out, err, args, resultReceiver);
Christopher Wiley0a9a1c12016-07-20 08:28:14 -0700267 (void)in;
268 (void)out;
269 (void)err;
270
Yi Kongfdd8da92018-06-07 17:52:27 -0700271 if (resultReceiver != nullptr) {
Dianne Hackbornf2bf93b2015-10-14 15:13:02 -0700272 resultReceiver->send(INVALID_OPERATION);
273 }
Martijn Coenenaa6ee992017-08-17 15:38:08 +0200274
275 return NO_ERROR;
Dianne Hackborn23eb1e22015-10-07 17:35:27 -0700276 }
277
Dianne Hackborn555f89d2012-05-08 18:54:22 -0700278 case SYSPROPS_TRANSACTION: {
279 report_sysprop_change();
280 return NO_ERROR;
281 }
282
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800283 default:
284 return UNKNOWN_TRANSACTION;
285 }
286}
287
Steven Moreland3085a472018-12-26 13:59:23 -0800288BBinder::Extras* BBinder::getOrCreateExtras()
289{
290 Extras* e = mExtras.load(std::memory_order_acquire);
291
292 if (!e) {
293 e = new Extras;
294 Extras* expected = nullptr;
295 if (!mExtras.compare_exchange_strong(expected, e,
296 std::memory_order_release,
297 std::memory_order_acquire)) {
298 delete e;
299 e = expected; // Filled in by CAS
300 }
301 if (e == nullptr) return nullptr; // out of memory
302 }
303
304 return e;
305}
306
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800307// ---------------------------------------------------------------------------
308
309enum {
310 // This is used to transfer ownership of the remote binder from
311 // the BpRefBase object holding it (when it is constructed), to the
312 // owner of the BpRefBase object when it first acquires that BpRefBase.
313 kRemoteAcquired = 0x00000001
314};
315
316BpRefBase::BpRefBase(const sp<IBinder>& o)
Yi Kongfdd8da92018-06-07 17:52:27 -0700317 : mRemote(o.get()), mRefs(nullptr), mState(0)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800318{
319 extendObjectLifetime(OBJECT_LIFETIME_WEAK);
320
321 if (mRemote) {
322 mRemote->incStrong(this); // Removed on first IncStrong().
323 mRefs = mRemote->createWeak(this); // Held for our entire lifetime.
324 }
325}
326
327BpRefBase::~BpRefBase()
328{
329 if (mRemote) {
Bailey Forrest6913c462015-08-18 17:15:10 -0700330 if (!(mState.load(std::memory_order_relaxed)&kRemoteAcquired)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800331 mRemote->decStrong(this);
332 }
333 mRefs->decWeak(this);
334 }
335}
336
337void BpRefBase::onFirstRef()
338{
Bailey Forrest6913c462015-08-18 17:15:10 -0700339 mState.fetch_or(kRemoteAcquired, std::memory_order_relaxed);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800340}
341
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800342void BpRefBase::onLastStrongRef(const void* /*id*/)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800343{
344 if (mRemote) {
345 mRemote->decStrong(this);
346 }
347}
348
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800349bool BpRefBase::onIncStrongAttempted(uint32_t /*flags*/, const void* /*id*/)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800350{
351 return mRemote ? mRefs->attemptIncStrong(this) : false;
352}
353
354// ---------------------------------------------------------------------------
355
356}; // namespace android