blob: 7324cf5bea8a648983661ed5ba5d37298a26b8f1 [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>
Mathias Agopianc5b2c0b2009-05-19 19:08:10 -070020#include <binder/BpBinder.h>
21#include <binder/IInterface.h>
Steven Morelandf0f97d32019-07-10 18:35:46 -070022#include <binder/IPCThreadState.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>
Steven Morelandf0f97d32019-07-10 18:35:46 -070026#include <cutils/android_filesystem_config.h>
27#include <cutils/compiler.h>
28#include <utils/misc.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080029
30#include <stdio.h>
31
32namespace android {
33
34// ---------------------------------------------------------------------------
35
Mathias Agopian83c04462009-05-22 19:00:22 -070036IBinder::IBinder()
37 : RefBase()
38{
39}
40
41IBinder::~IBinder()
42{
43}
44
45// ---------------------------------------------------------------------------
46
Colin Cross6f4f3ab2014-02-05 17:42:44 -080047sp<IInterface> IBinder::queryLocalInterface(const String16& /*descriptor*/)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080048{
Yi Kongfdd8da92018-06-07 17:52:27 -070049 return nullptr;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080050}
51
52BBinder* IBinder::localBinder()
53{
Yi Kongfdd8da92018-06-07 17:52:27 -070054 return nullptr;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080055}
56
57BpBinder* IBinder::remoteBinder()
58{
Yi Kongfdd8da92018-06-07 17:52:27 -070059 return nullptr;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080060}
61
62bool IBinder::checkSubclass(const void* /*subclassID*/) const
63{
64 return false;
65}
66
Dianne Hackborn23eb1e22015-10-07 17:35:27 -070067
Dianne Hackbornf2bf93b2015-10-14 15:13:02 -070068status_t IBinder::shellCommand(const sp<IBinder>& target, int in, int out, int err,
Dianne Hackborn1941a402016-08-29 12:30:43 -070069 Vector<String16>& args, const sp<IShellCallback>& callback,
70 const sp<IResultReceiver>& resultReceiver)
Dianne Hackborn23eb1e22015-10-07 17:35:27 -070071{
Dianne Hackbornf2bf93b2015-10-14 15:13:02 -070072 Parcel send;
73 Parcel reply;
74 send.writeFileDescriptor(in);
75 send.writeFileDescriptor(out);
76 send.writeFileDescriptor(err);
77 const size_t numArgs = args.size();
78 send.writeInt32(numArgs);
79 for (size_t i = 0; i < numArgs; i++) {
80 send.writeString16(args[i]);
Dianne Hackborn23eb1e22015-10-07 17:35:27 -070081 }
Yi Kongfdd8da92018-06-07 17:52:27 -070082 send.writeStrongBinder(callback != nullptr ? IInterface::asBinder(callback) : nullptr);
83 send.writeStrongBinder(resultReceiver != nullptr ? IInterface::asBinder(resultReceiver) : nullptr);
Dianne Hackbornf2bf93b2015-10-14 15:13:02 -070084 return target->transact(SHELL_COMMAND_TRANSACTION, send, &reply);
Dianne Hackborn23eb1e22015-10-07 17:35:27 -070085}
86
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080087// ---------------------------------------------------------------------------
88
89class BBinder::Extras
90{
91public:
Steven Moreland3085a472018-12-26 13:59:23 -080092 // unlocked objects
93 bool mRequestingSid = false;
94
95 // for below objects
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080096 Mutex mLock;
97 BpBinder::ObjectManager mObjects;
98};
99
100// ---------------------------------------------------------------------------
101
Bailey Forrest6913c462015-08-18 17:15:10 -0700102BBinder::BBinder() : mExtras(nullptr)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800103{
104}
105
106bool BBinder::isBinderAlive() const
107{
108 return true;
109}
110
111status_t BBinder::pingBinder()
112{
113 return NO_ERROR;
114}
115
Mathias Agopian83c04462009-05-22 19:00:22 -0700116const String16& BBinder::getInterfaceDescriptor() const
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800117{
Dan Egnor386a3322010-05-06 00:55:09 -0700118 // This is a local static rather than a global static,
119 // to avoid static initializer ordering issues.
120 static String16 sEmptyDescriptor;
Steve Block32397c12012-01-05 23:22:43 +0000121 ALOGW("reached BBinder::getInterfaceDescriptor (this=%p)", this);
Mathias Agopian83c04462009-05-22 19:00:22 -0700122 return sEmptyDescriptor;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800123}
124
Jiyong Parkb86c8662018-10-29 23:01:57 +0900125// NOLINTNEXTLINE(google-default-arguments)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800126status_t BBinder::transact(
127 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
128{
129 data.setDataPosition(0);
130
Steven Morelandf0f97d32019-07-10 18:35:46 -0700131 // Shell command transaction is conventionally implemented by
132 // overriding onTransact by copy/pasting the parceling code from
133 // this file. So, we must check permissions for it before we call
134 // onTransact. This check is here because shell APIs aren't
135 // guaranteed to be stable, and so they should only be used by
136 // developers.
137 if (CC_UNLIKELY(code == SHELL_COMMAND_TRANSACTION)) {
138 uid_t uid = IPCThreadState::self()->getCallingUid();
139 if (uid != AID_SHELL && uid != AID_ROOT) {
140 return PERMISSION_DENIED;
141 }
142 }
143
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800144 status_t err = NO_ERROR;
145 switch (code) {
146 case PING_TRANSACTION:
147 reply->writeInt32(pingBinder());
148 break;
149 default:
150 err = onTransact(code, data, reply, flags);
151 break;
152 }
153
Yi Kongfdd8da92018-06-07 17:52:27 -0700154 if (reply != nullptr) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800155 reply->setDataPosition(0);
156 }
157
158 return err;
159}
160
Jiyong Parkb86c8662018-10-29 23:01:57 +0900161// NOLINTNEXTLINE(google-default-arguments)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800162status_t BBinder::linkToDeath(
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800163 const sp<DeathRecipient>& /*recipient*/, void* /*cookie*/,
164 uint32_t /*flags*/)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800165{
166 return INVALID_OPERATION;
167}
168
Jiyong Parkb86c8662018-10-29 23:01:57 +0900169// NOLINTNEXTLINE(google-default-arguments)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800170status_t BBinder::unlinkToDeath(
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800171 const wp<DeathRecipient>& /*recipient*/, void* /*cookie*/,
172 uint32_t /*flags*/, wp<DeathRecipient>* /*outRecipient*/)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800173{
174 return INVALID_OPERATION;
175}
176
Dianne Hackborn23eb1e22015-10-07 17:35:27 -0700177status_t BBinder::dump(int /*fd*/, const Vector<String16>& /*args*/)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800178{
179 return NO_ERROR;
180}
181
182void BBinder::attachObject(
183 const void* objectID, void* object, void* cleanupCookie,
184 object_cleanup_func func)
185{
Steven Moreland3085a472018-12-26 13:59:23 -0800186 Extras* e = getOrCreateExtras();
187 if (!e) return; // out of memory
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800188
189 AutoMutex _l(e->mLock);
190 e->mObjects.attach(objectID, object, cleanupCookie, func);
191}
192
193void* BBinder::findObject(const void* objectID) const
194{
Bailey Forrest6913c462015-08-18 17:15:10 -0700195 Extras* e = mExtras.load(std::memory_order_acquire);
Yi Kongfdd8da92018-06-07 17:52:27 -0700196 if (!e) return nullptr;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800197
198 AutoMutex _l(e->mLock);
199 return e->mObjects.find(objectID);
200}
201
202void BBinder::detachObject(const void* objectID)
203{
Bailey Forrest6913c462015-08-18 17:15:10 -0700204 Extras* e = mExtras.load(std::memory_order_acquire);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800205 if (!e) return;
206
207 AutoMutex _l(e->mLock);
208 e->mObjects.detach(objectID);
209}
210
211BBinder* BBinder::localBinder()
212{
213 return this;
214}
215
Steven Moreland3085a472018-12-26 13:59:23 -0800216bool BBinder::isRequestingSid()
217{
218 Extras* e = mExtras.load(std::memory_order_acquire);
219
220 return e && e->mRequestingSid;
221}
222
223void BBinder::setRequestingSid(bool requestingSid)
224{
225 Extras* e = mExtras.load(std::memory_order_acquire);
226
227 if (!e) {
228 // default is false. Most things don't need sids, so avoiding allocations when possible.
229 if (!requestingSid) {
230 return;
231 }
232
233 e = getOrCreateExtras();
234 if (!e) return; // out of memory
235 }
236
237 e->mRequestingSid = true;
238}
239
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800240BBinder::~BBinder()
241{
Bailey Forrest6913c462015-08-18 17:15:10 -0700242 Extras* e = mExtras.load(std::memory_order_relaxed);
Hans Boehm3effaba2014-08-12 22:56:00 +0000243 if (e) delete e;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800244}
245
246
Jiyong Parkb86c8662018-10-29 23:01:57 +0900247// NOLINTNEXTLINE(google-default-arguments)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800248status_t BBinder::onTransact(
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800249 uint32_t code, const Parcel& data, Parcel* reply, uint32_t /*flags*/)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800250{
251 switch (code) {
252 case INTERFACE_TRANSACTION:
253 reply->writeString16(getInterfaceDescriptor());
254 return NO_ERROR;
255
256 case DUMP_TRANSACTION: {
257 int fd = data.readFileDescriptor();
258 int argc = data.readInt32();
259 Vector<String16> args;
260 for (int i = 0; i < argc && data.dataAvail() > 0; i++) {
261 args.add(data.readString16());
262 }
263 return dump(fd, args);
264 }
Dianne Hackborn555f89d2012-05-08 18:54:22 -0700265
Dianne Hackborn23eb1e22015-10-07 17:35:27 -0700266 case SHELL_COMMAND_TRANSACTION: {
267 int in = data.readFileDescriptor();
268 int out = data.readFileDescriptor();
269 int err = data.readFileDescriptor();
270 int argc = data.readInt32();
271 Vector<String16> args;
272 for (int i = 0; i < argc && data.dataAvail() > 0; i++) {
273 args.add(data.readString16());
274 }
Dianne Hackborn1941a402016-08-29 12:30:43 -0700275 sp<IShellCallback> shellCallback = IShellCallback::asInterface(
276 data.readStrongBinder());
Dianne Hackborn23eb1e22015-10-07 17:35:27 -0700277 sp<IResultReceiver> resultReceiver = IResultReceiver::asInterface(
278 data.readStrongBinder());
279
Dianne Hackbornf2bf93b2015-10-14 15:13:02 -0700280 // XXX can't add virtuals until binaries are updated.
281 //return shellCommand(in, out, err, args, resultReceiver);
Christopher Wiley0a9a1c12016-07-20 08:28:14 -0700282 (void)in;
283 (void)out;
284 (void)err;
285
Yi Kongfdd8da92018-06-07 17:52:27 -0700286 if (resultReceiver != nullptr) {
Dianne Hackbornf2bf93b2015-10-14 15:13:02 -0700287 resultReceiver->send(INVALID_OPERATION);
288 }
Martijn Coenenaa6ee992017-08-17 15:38:08 +0200289
290 return NO_ERROR;
Dianne Hackborn23eb1e22015-10-07 17:35:27 -0700291 }
292
Dianne Hackborn555f89d2012-05-08 18:54:22 -0700293 case SYSPROPS_TRANSACTION: {
294 report_sysprop_change();
295 return NO_ERROR;
296 }
297
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800298 default:
299 return UNKNOWN_TRANSACTION;
300 }
301}
302
Steven Moreland3085a472018-12-26 13:59:23 -0800303BBinder::Extras* BBinder::getOrCreateExtras()
304{
305 Extras* e = mExtras.load(std::memory_order_acquire);
306
307 if (!e) {
308 e = new Extras;
309 Extras* expected = nullptr;
310 if (!mExtras.compare_exchange_strong(expected, e,
311 std::memory_order_release,
312 std::memory_order_acquire)) {
313 delete e;
314 e = expected; // Filled in by CAS
315 }
316 if (e == nullptr) return nullptr; // out of memory
317 }
318
319 return e;
320}
321
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800322// ---------------------------------------------------------------------------
323
324enum {
325 // This is used to transfer ownership of the remote binder from
326 // the BpRefBase object holding it (when it is constructed), to the
327 // owner of the BpRefBase object when it first acquires that BpRefBase.
328 kRemoteAcquired = 0x00000001
329};
330
331BpRefBase::BpRefBase(const sp<IBinder>& o)
Yi Kongfdd8da92018-06-07 17:52:27 -0700332 : mRemote(o.get()), mRefs(nullptr), mState(0)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800333{
334 extendObjectLifetime(OBJECT_LIFETIME_WEAK);
335
336 if (mRemote) {
337 mRemote->incStrong(this); // Removed on first IncStrong().
338 mRefs = mRemote->createWeak(this); // Held for our entire lifetime.
339 }
340}
341
342BpRefBase::~BpRefBase()
343{
344 if (mRemote) {
Bailey Forrest6913c462015-08-18 17:15:10 -0700345 if (!(mState.load(std::memory_order_relaxed)&kRemoteAcquired)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800346 mRemote->decStrong(this);
347 }
348 mRefs->decWeak(this);
349 }
350}
351
352void BpRefBase::onFirstRef()
353{
Bailey Forrest6913c462015-08-18 17:15:10 -0700354 mState.fetch_or(kRemoteAcquired, std::memory_order_relaxed);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800355}
356
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800357void BpRefBase::onLastStrongRef(const void* /*id*/)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800358{
359 if (mRemote) {
360 mRemote->decStrong(this);
361 }
362}
363
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800364bool BpRefBase::onIncStrongAttempted(uint32_t /*flags*/, const void* /*id*/)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800365{
366 return mRemote ? mRefs->attemptIncStrong(this) : false;
367}
368
369// ---------------------------------------------------------------------------
370
371}; // namespace android