blob: cb0e08d1235bc077aaa1fcece5d48ce6b956f1a8 [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{
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 Morelandf0212002018-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:
131 reply->writeInt32(pingBinder());
132 break;
133 default:
134 err = onTransact(code, data, reply, flags);
135 break;
136 }
137
Yi Kongfdd8da92018-06-07 17:52:27 -0700138 if (reply != nullptr) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800139 reply->setDataPosition(0);
140 }
141
142 return err;
143}
144
Jiyong Parkb86c8662018-10-29 23:01:57 +0900145// NOLINTNEXTLINE(google-default-arguments)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800146status_t BBinder::linkToDeath(
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800147 const sp<DeathRecipient>& /*recipient*/, void* /*cookie*/,
148 uint32_t /*flags*/)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800149{
150 return INVALID_OPERATION;
151}
152
Jiyong Parkb86c8662018-10-29 23:01:57 +0900153// NOLINTNEXTLINE(google-default-arguments)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800154status_t BBinder::unlinkToDeath(
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800155 const wp<DeathRecipient>& /*recipient*/, void* /*cookie*/,
156 uint32_t /*flags*/, wp<DeathRecipient>* /*outRecipient*/)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800157{
158 return INVALID_OPERATION;
159}
160
Dianne Hackborn23eb1e22015-10-07 17:35:27 -0700161status_t BBinder::dump(int /*fd*/, const Vector<String16>& /*args*/)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800162{
163 return NO_ERROR;
164}
165
166void BBinder::attachObject(
167 const void* objectID, void* object, void* cleanupCookie,
168 object_cleanup_func func)
169{
Steven Morelandf0212002018-12-26 13:59:23 -0800170 Extras* e = getOrCreateExtras();
171 if (!e) return; // out of memory
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800172
173 AutoMutex _l(e->mLock);
174 e->mObjects.attach(objectID, object, cleanupCookie, func);
175}
176
177void* BBinder::findObject(const void* objectID) const
178{
Bailey Forrest6913c462015-08-18 17:15:10 -0700179 Extras* e = mExtras.load(std::memory_order_acquire);
Yi Kongfdd8da92018-06-07 17:52:27 -0700180 if (!e) return nullptr;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800181
182 AutoMutex _l(e->mLock);
183 return e->mObjects.find(objectID);
184}
185
186void BBinder::detachObject(const void* objectID)
187{
Bailey Forrest6913c462015-08-18 17:15:10 -0700188 Extras* e = mExtras.load(std::memory_order_acquire);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800189 if (!e) return;
190
191 AutoMutex _l(e->mLock);
192 e->mObjects.detach(objectID);
193}
194
195BBinder* BBinder::localBinder()
196{
197 return this;
198}
199
Steven Morelandf0212002018-12-26 13:59:23 -0800200bool BBinder::isRequestingSid()
201{
202 Extras* e = mExtras.load(std::memory_order_acquire);
203
204 return e && e->mRequestingSid;
205}
206
207void BBinder::setRequestingSid(bool requestingSid)
208{
209 Extras* e = mExtras.load(std::memory_order_acquire);
210
211 if (!e) {
212 // default is false. Most things don't need sids, so avoiding allocations when possible.
213 if (!requestingSid) {
214 return;
215 }
216
217 e = getOrCreateExtras();
218 if (!e) return; // out of memory
219 }
220
Steven Moreland3668be62019-02-08 17:56:55 -0800221 e->mRequestingSid = requestingSid;
Steven Morelandf0212002018-12-26 13:59:23 -0800222}
223
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800224BBinder::~BBinder()
225{
Bailey Forrest6913c462015-08-18 17:15:10 -0700226 Extras* e = mExtras.load(std::memory_order_relaxed);
Hans Boehm3effaba2014-08-12 22:56:00 +0000227 if (e) delete e;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800228}
229
230
Jiyong Parkb86c8662018-10-29 23:01:57 +0900231// NOLINTNEXTLINE(google-default-arguments)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800232status_t BBinder::onTransact(
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800233 uint32_t code, const Parcel& data, Parcel* reply, uint32_t /*flags*/)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800234{
235 switch (code) {
236 case INTERFACE_TRANSACTION:
237 reply->writeString16(getInterfaceDescriptor());
238 return NO_ERROR;
239
240 case DUMP_TRANSACTION: {
241 int fd = data.readFileDescriptor();
242 int argc = data.readInt32();
243 Vector<String16> args;
244 for (int i = 0; i < argc && data.dataAvail() > 0; i++) {
245 args.add(data.readString16());
246 }
247 return dump(fd, args);
248 }
Dianne Hackborn555f89d2012-05-08 18:54:22 -0700249
Dianne Hackborn23eb1e22015-10-07 17:35:27 -0700250 case SHELL_COMMAND_TRANSACTION: {
251 int in = data.readFileDescriptor();
252 int out = data.readFileDescriptor();
253 int err = data.readFileDescriptor();
254 int argc = data.readInt32();
255 Vector<String16> args;
256 for (int i = 0; i < argc && data.dataAvail() > 0; i++) {
257 args.add(data.readString16());
258 }
Dianne Hackborn1941a402016-08-29 12:30:43 -0700259 sp<IShellCallback> shellCallback = IShellCallback::asInterface(
260 data.readStrongBinder());
Dianne Hackborn23eb1e22015-10-07 17:35:27 -0700261 sp<IResultReceiver> resultReceiver = IResultReceiver::asInterface(
262 data.readStrongBinder());
263
Dianne Hackbornf2bf93b2015-10-14 15:13:02 -0700264 // XXX can't add virtuals until binaries are updated.
265 //return shellCommand(in, out, err, args, resultReceiver);
Christopher Wiley0a9a1c12016-07-20 08:28:14 -0700266 (void)in;
267 (void)out;
268 (void)err;
269
Yi Kongfdd8da92018-06-07 17:52:27 -0700270 if (resultReceiver != nullptr) {
Dianne Hackbornf2bf93b2015-10-14 15:13:02 -0700271 resultReceiver->send(INVALID_OPERATION);
272 }
Martijn Coenenaa6ee992017-08-17 15:38:08 +0200273
274 return NO_ERROR;
Dianne Hackborn23eb1e22015-10-07 17:35:27 -0700275 }
276
Dianne Hackborn555f89d2012-05-08 18:54:22 -0700277 case SYSPROPS_TRANSACTION: {
278 report_sysprop_change();
279 return NO_ERROR;
280 }
281
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800282 default:
283 return UNKNOWN_TRANSACTION;
284 }
285}
286
Steven Morelandf0212002018-12-26 13:59:23 -0800287BBinder::Extras* BBinder::getOrCreateExtras()
288{
289 Extras* e = mExtras.load(std::memory_order_acquire);
290
291 if (!e) {
292 e = new Extras;
293 Extras* expected = nullptr;
294 if (!mExtras.compare_exchange_strong(expected, e,
295 std::memory_order_release,
296 std::memory_order_acquire)) {
297 delete e;
298 e = expected; // Filled in by CAS
299 }
300 if (e == nullptr) return nullptr; // out of memory
301 }
302
303 return e;
304}
305
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800306// ---------------------------------------------------------------------------
307
308enum {
309 // This is used to transfer ownership of the remote binder from
310 // the BpRefBase object holding it (when it is constructed), to the
311 // owner of the BpRefBase object when it first acquires that BpRefBase.
312 kRemoteAcquired = 0x00000001
313};
314
315BpRefBase::BpRefBase(const sp<IBinder>& o)
Yi Kongfdd8da92018-06-07 17:52:27 -0700316 : mRemote(o.get()), mRefs(nullptr), mState(0)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800317{
318 extendObjectLifetime(OBJECT_LIFETIME_WEAK);
319
320 if (mRemote) {
321 mRemote->incStrong(this); // Removed on first IncStrong().
322 mRefs = mRemote->createWeak(this); // Held for our entire lifetime.
323 }
324}
325
326BpRefBase::~BpRefBase()
327{
328 if (mRemote) {
Bailey Forrest6913c462015-08-18 17:15:10 -0700329 if (!(mState.load(std::memory_order_relaxed)&kRemoteAcquired)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800330 mRemote->decStrong(this);
331 }
332 mRefs->decWeak(this);
333 }
334}
335
336void BpRefBase::onFirstRef()
337{
Bailey Forrest6913c462015-08-18 17:15:10 -0700338 mState.fetch_or(kRemoteAcquired, std::memory_order_relaxed);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800339}
340
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800341void BpRefBase::onLastStrongRef(const void* /*id*/)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800342{
343 if (mRemote) {
344 mRemote->decStrong(this);
345 }
346}
347
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800348bool BpRefBase::onIncStrongAttempted(uint32_t /*flags*/, const void* /*id*/)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800349{
350 return mRemote ? mRefs->attemptIncStrong(this) : false;
351}
352
353// ---------------------------------------------------------------------------
354
355}; // namespace android