blob: b06ca86f570f674b96e452e27c7f1a52a6dd12fe [file] [log] [blame]
Steven Moreland2e87adc2018-08-20 19:47:00 -07001/*
2 * Copyright (C) 2018 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
17#include <android/binder_ibinder.h>
Steven Moreland4d5ad492018-09-13 12:49:16 -070018#include "ibinder_internal.h"
Steven Moreland2e87adc2018-08-20 19:47:00 -070019
20#include <android/binder_status.h>
Steven Moreland4d5ad492018-09-13 12:49:16 -070021#include "parcel_internal.h"
Steven Moreland5d62e442018-09-13 15:01:02 -070022#include "status_internal.h"
Steven Moreland2e87adc2018-08-20 19:47:00 -070023
24#include <android-base/logging.h>
Steven Morelandf3034b02018-11-12 17:37:46 -080025#include <binder/IPCThreadState.h>
Steven Moreland2e87adc2018-08-20 19:47:00 -070026
Steven Moreland901c7452018-09-04 16:17:28 -070027using DeathRecipient = ::android::IBinder::DeathRecipient;
28
Steven Moreland2e87adc2018-08-20 19:47:00 -070029using ::android::IBinder;
30using ::android::Parcel;
31using ::android::sp;
Steven Moreland5d62e442018-09-13 15:01:02 -070032using ::android::status_t;
Steven Moreland2e87adc2018-08-20 19:47:00 -070033using ::android::String16;
Steven Morelanda194c452019-03-04 16:47:07 -080034using ::android::String8;
Steven Moreland2e87adc2018-08-20 19:47:00 -070035using ::android::wp;
36
Steven Moreland71cddc32018-08-30 23:39:22 -070037namespace ABBinderTag {
38
39static const void* kId = "ABBinder";
40static void* kValue = static_cast<void*>(new bool{true});
Steven Moreland94968952018-09-05 14:42:59 -070041void clean(const void* /*id*/, void* /*obj*/, void* /*cookie*/){/* do nothing */};
Steven Moreland71cddc32018-08-30 23:39:22 -070042
43static void attach(const sp<IBinder>& binder) {
Steven Moreland94968952018-09-05 14:42:59 -070044 binder->attachObject(kId, kValue, nullptr /*cookie*/, clean);
Steven Moreland71cddc32018-08-30 23:39:22 -070045}
46static bool has(const sp<IBinder>& binder) {
47 return binder != nullptr && binder->findObject(kId) == kValue;
48}
49
Steven Moreland6cf66ac2018-11-02 18:14:54 -070050} // namespace ABBinderTag
Steven Moreland71cddc32018-08-30 23:39:22 -070051
Steven Moreland94968952018-09-05 14:42:59 -070052namespace ABpBinderTag {
53
54static std::mutex gLock;
55static const void* kId = "ABpBinder";
56struct Value {
57 wp<ABpBinder> binder;
58};
59void clean(const void* id, void* obj, void* cookie) {
60 CHECK(id == kId) << id << " " << obj << " " << cookie;
61
62 delete static_cast<Value*>(obj);
63};
64
Steven Moreland6cf66ac2018-11-02 18:14:54 -070065} // namespace ABpBinderTag
Steven Moreland94968952018-09-05 14:42:59 -070066
Steven Moreland2e87adc2018-08-20 19:47:00 -070067AIBinder::AIBinder(const AIBinder_Class* clazz) : mClazz(clazz) {}
68AIBinder::~AIBinder() {}
69
Steven Moreland71cddc32018-08-30 23:39:22 -070070bool AIBinder::associateClass(const AIBinder_Class* clazz) {
Steven Moreland71cddc32018-08-30 23:39:22 -070071 if (clazz == nullptr) return false;
72 if (mClazz == clazz) return true;
Steven Moreland2e87adc2018-08-20 19:47:00 -070073
74 String8 newDescriptor(clazz->getInterfaceDescriptor());
75
76 if (mClazz != nullptr) {
77 String8 currentDescriptor(mClazz->getInterfaceDescriptor());
78 if (newDescriptor == currentDescriptor) {
79 LOG(ERROR) << __func__ << ": Class descriptors '" << currentDescriptor
80 << "' match during associateClass, but they are different class objects. "
81 "Class descriptor collision?";
Steven Moreland71cddc32018-08-30 23:39:22 -070082 } else {
83 LOG(ERROR) << __func__
84 << ": Class cannot be associated on object which already has a class. "
85 "Trying to associate to '"
86 << newDescriptor.c_str() << "' but already set to '"
87 << currentDescriptor.c_str() << "'.";
Steven Moreland2e87adc2018-08-20 19:47:00 -070088 }
89
Steven Moreland71cddc32018-08-30 23:39:22 -070090 // always a failure because we know mClazz != clazz
91 return false;
Steven Moreland2e87adc2018-08-20 19:47:00 -070092 }
93
Steven Moreland6cf66ac2018-11-02 18:14:54 -070094 CHECK(asABpBinder() != nullptr); // ABBinder always has a descriptor
Steven Moreland71cddc32018-08-30 23:39:22 -070095
Steven Moreland2e87adc2018-08-20 19:47:00 -070096 String8 descriptor(getBinder()->getInterfaceDescriptor());
97 if (descriptor != newDescriptor) {
98 LOG(ERROR) << __func__ << ": Expecting binder to have class '" << newDescriptor.c_str()
99 << "' but descriptor is actually '" << descriptor.c_str() << "'.";
Steven Moreland71cddc32018-08-30 23:39:22 -0700100 return false;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700101 }
102
Steven Moreland71cddc32018-08-30 23:39:22 -0700103 // if this is a local object, it's not one known to libbinder_ndk
Steven Moreland2e87adc2018-08-20 19:47:00 -0700104 mClazz = clazz;
105
Steven Moreland71cddc32018-08-30 23:39:22 -0700106 return true;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700107}
108
109ABBinder::ABBinder(const AIBinder_Class* clazz, void* userData)
Steven Moreland6cf66ac2018-11-02 18:14:54 -0700110 : AIBinder(clazz), BBinder(), mUserData(userData) {
Steven Moreland2e87adc2018-08-20 19:47:00 -0700111 CHECK(clazz != nullptr);
112}
113ABBinder::~ABBinder() {
114 getClass()->onDestroy(mUserData);
115}
116
117const String16& ABBinder::getInterfaceDescriptor() const {
118 return getClass()->getInterfaceDescriptor();
119}
120
Steven Morelanda194c452019-03-04 16:47:07 -0800121status_t ABBinder::dump(int fd, const ::android::Vector<String16>& args) {
122 AIBinder_onDump onDump = getClass()->onDump;
123
124 if (onDump == nullptr) {
125 return STATUS_OK;
126 }
127
128 // technically UINT32_MAX would be okay here, but INT32_MAX is expected since this may be
129 // null in Java
130 if (args.size() > INT32_MAX) {
131 LOG(ERROR) << "ABBinder::dump received too many arguments: " << args.size();
132 return STATUS_BAD_VALUE;
133 }
134
135 std::vector<String8> utf8Args; // owns memory of utf8s
136 utf8Args.reserve(args.size());
137 std::vector<const char*> utf8Pointers; // what can be passed over NDK API
138 utf8Pointers.reserve(args.size());
139
140 for (size_t i = 0; i < args.size(); i++) {
141 utf8Args.push_back(String8(args[i]));
142 utf8Pointers.push_back(utf8Args[i].c_str());
143 }
144
145 return onDump(this, fd, utf8Pointers.data(), utf8Pointers.size());
146}
147
Steven Moreland5d62e442018-09-13 15:01:02 -0700148status_t ABBinder::onTransact(transaction_code_t code, const Parcel& data, Parcel* reply,
149 binder_flags_t flags) {
Steven Moreland2e87adc2018-08-20 19:47:00 -0700150 if (isUserCommand(code)) {
151 if (!data.checkInterface(this)) {
Steven Moreland9d089af2018-09-18 08:58:09 -0700152 return STATUS_BAD_TYPE;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700153 }
154
155 const AParcel in = AParcel::readOnly(this, &data);
156 AParcel out = AParcel(this, reply, false /*owns*/);
157
Steven Moreland5d62e442018-09-13 15:01:02 -0700158 binder_status_t status = getClass()->onTransact(this, code, &in, &out);
159 return PruneStatusT(status);
Steven Moreland2e87adc2018-08-20 19:47:00 -0700160 } else {
161 return BBinder::onTransact(code, data, reply, flags);
162 }
163}
164
Steven Moreland71cddc32018-08-30 23:39:22 -0700165ABpBinder::ABpBinder(const ::android::sp<::android::IBinder>& binder)
Steven Moreland6cf66ac2018-11-02 18:14:54 -0700166 : AIBinder(nullptr /*clazz*/), BpRefBase(binder) {
Steven Moreland2e87adc2018-08-20 19:47:00 -0700167 CHECK(binder != nullptr);
168}
169ABpBinder::~ABpBinder() {}
170
Steven Moreland94968952018-09-05 14:42:59 -0700171void ABpBinder::onLastStrongRef(const void* id) {
172 {
173 std::lock_guard<std::mutex> lock(ABpBinderTag::gLock);
174 // Since ABpBinder is OBJECT_LIFETIME_WEAK, we must remove this weak reference in order for
175 // the ABpBinder to be deleted. Since a strong reference to this ABpBinder object should no
176 // longer be able to exist at the time of this method call, there is no longer a need to
177 // recover it.
178
179 ABpBinderTag::Value* value =
180 static_cast<ABpBinderTag::Value*>(remote()->findObject(ABpBinderTag::kId));
181 if (value != nullptr) {
182 value->binder = nullptr;
183 }
184 }
185
186 BpRefBase::onLastStrongRef(id);
187}
188
189sp<AIBinder> ABpBinder::lookupOrCreateFromBinder(const ::android::sp<::android::IBinder>& binder) {
Steven Moreland71cddc32018-08-30 23:39:22 -0700190 if (binder == nullptr) {
191 return nullptr;
192 }
193 if (ABBinderTag::has(binder)) {
194 return static_cast<ABBinder*>(binder.get());
195 }
Steven Moreland94968952018-09-05 14:42:59 -0700196
197 // The following code ensures that for a given binder object (remote or local), if it is not an
198 // ABBinder then at most one ABpBinder object exists in a given process representing it.
199 std::lock_guard<std::mutex> lock(ABpBinderTag::gLock);
200
201 ABpBinderTag::Value* value =
202 static_cast<ABpBinderTag::Value*>(binder->findObject(ABpBinderTag::kId));
203 if (value == nullptr) {
204 value = new ABpBinderTag::Value;
205 binder->attachObject(ABpBinderTag::kId, static_cast<void*>(value), nullptr /*cookie*/,
206 ABpBinderTag::clean);
207 }
208
209 sp<ABpBinder> ret = value->binder.promote();
210 if (ret == nullptr) {
211 ret = new ABpBinder(binder);
212 value->binder = ret;
213 }
214
215 return ret;
Steven Moreland71cddc32018-08-30 23:39:22 -0700216}
217
Steven Moreland2e87adc2018-08-20 19:47:00 -0700218struct AIBinder_Weak {
219 wp<AIBinder> binder;
220};
221AIBinder_Weak* AIBinder_Weak_new(AIBinder* binder) {
Steven Moreland5ccb70f2018-09-04 16:30:21 -0700222 if (binder == nullptr) {
223 return nullptr;
224 }
225
Steven Moreland2e87adc2018-08-20 19:47:00 -0700226 return new AIBinder_Weak{wp<AIBinder>(binder)};
227}
Steven Moreland9b80e282018-09-19 13:57:23 -0700228void AIBinder_Weak_delete(AIBinder_Weak* weakBinder) {
229 delete weakBinder;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700230}
231AIBinder* AIBinder_Weak_promote(AIBinder_Weak* weakBinder) {
Steven Moreland5ccb70f2018-09-04 16:30:21 -0700232 if (weakBinder == nullptr) {
233 return nullptr;
234 }
235
Steven Moreland2e87adc2018-08-20 19:47:00 -0700236 sp<AIBinder> binder = weakBinder->binder.promote();
237 AIBinder_incStrong(binder.get());
238 return binder.get();
239}
240
241AIBinder_Class::AIBinder_Class(const char* interfaceDescriptor, AIBinder_Class_onCreate onCreate,
242 AIBinder_Class_onDestroy onDestroy,
243 AIBinder_Class_onTransact onTransact)
Steven Moreland6cf66ac2018-11-02 18:14:54 -0700244 : onCreate(onCreate),
245 onDestroy(onDestroy),
246 onTransact(onTransact),
247 mInterfaceDescriptor(interfaceDescriptor) {}
Steven Moreland2e87adc2018-08-20 19:47:00 -0700248
249AIBinder_Class* AIBinder_Class_define(const char* interfaceDescriptor,
250 AIBinder_Class_onCreate onCreate,
251 AIBinder_Class_onDestroy onDestroy,
252 AIBinder_Class_onTransact onTransact) {
253 if (interfaceDescriptor == nullptr || onCreate == nullptr || onDestroy == nullptr ||
254 onTransact == nullptr) {
255 return nullptr;
256 }
257
258 return new AIBinder_Class(interfaceDescriptor, onCreate, onDestroy, onTransact);
259}
260
Steven Morelanda194c452019-03-04 16:47:07 -0800261void AIBinder_Class_setOnDump(AIBinder_Class* clazz, AIBinder_onDump onDump) {
262 CHECK(clazz != nullptr) << "setOnDump requires non-null clazz";
263
264 // this is required to be called before instances are instantiated
265 clazz->onDump = onDump;
266}
267
Steven Moreland901c7452018-09-04 16:17:28 -0700268void AIBinder_DeathRecipient::TransferDeathRecipient::binderDied(const wp<IBinder>& who) {
269 CHECK(who == mWho);
270
271 mOnDied(mCookie);
Steven Moreland64127ca2019-03-13 09:25:44 -0700272
273 sp<AIBinder_DeathRecipient> recipient = mParentRecipient.promote();
274 sp<IBinder> strongWho = who.promote();
275
276 // otherwise this will be cleaned up later with pruneDeadTransferEntriesLocked
277 if (recipient != nullptr && strongWho != nullptr) {
278 status_t result = recipient->unlinkToDeath(strongWho, mCookie);
279 if (result != ::android::DEAD_OBJECT) {
280 LOG(WARNING) << "Unlinking to dead binder resulted in: " << result;
281 }
282 }
283
Steven Moreland901c7452018-09-04 16:17:28 -0700284 mWho = nullptr;
285}
286
287AIBinder_DeathRecipient::AIBinder_DeathRecipient(AIBinder_DeathRecipient_onBinderDied onDied)
Steven Moreland6cf66ac2018-11-02 18:14:54 -0700288 : mOnDied(onDied) {
Steven Moreland901c7452018-09-04 16:17:28 -0700289 CHECK(onDied != nullptr);
290}
291
Steven Moreland64127ca2019-03-13 09:25:44 -0700292void AIBinder_DeathRecipient::pruneDeadTransferEntriesLocked() {
293 mDeathRecipients.erase(std::remove_if(mDeathRecipients.begin(), mDeathRecipients.end(),
294 [](const sp<TransferDeathRecipient>& tdr) {
295 return tdr->getWho() == nullptr;
296 }),
297 mDeathRecipients.end());
298}
299
300binder_status_t AIBinder_DeathRecipient::linkToDeath(sp<IBinder> binder, void* cookie) {
Steven Moreland901c7452018-09-04 16:17:28 -0700301 CHECK(binder != nullptr);
302
303 std::lock_guard<std::mutex> l(mDeathRecipientsMutex);
304
305 sp<TransferDeathRecipient> recipient =
Steven Moreland64127ca2019-03-13 09:25:44 -0700306 new TransferDeathRecipient(binder, cookie, this, mOnDied);
Steven Moreland901c7452018-09-04 16:17:28 -0700307
Steven Moreland64127ca2019-03-13 09:25:44 -0700308 status_t status = binder->linkToDeath(recipient, cookie, 0 /*flags*/);
Steven Moreland5d62e442018-09-13 15:01:02 -0700309 if (status != STATUS_OK) {
310 return PruneStatusT(status);
Steven Moreland901c7452018-09-04 16:17:28 -0700311 }
312
313 mDeathRecipients.push_back(recipient);
Steven Moreland64127ca2019-03-13 09:25:44 -0700314
315 pruneDeadTransferEntriesLocked();
Steven Moreland5d62e442018-09-13 15:01:02 -0700316 return STATUS_OK;
Steven Moreland901c7452018-09-04 16:17:28 -0700317}
318
Steven Moreland64127ca2019-03-13 09:25:44 -0700319binder_status_t AIBinder_DeathRecipient::unlinkToDeath(sp<IBinder> binder, void* cookie) {
Steven Moreland901c7452018-09-04 16:17:28 -0700320 CHECK(binder != nullptr);
321
322 std::lock_guard<std::mutex> l(mDeathRecipientsMutex);
323
324 for (auto it = mDeathRecipients.rbegin(); it != mDeathRecipients.rend(); ++it) {
325 sp<TransferDeathRecipient> recipient = *it;
326
Steven Moreland64127ca2019-03-13 09:25:44 -0700327 if (recipient->getCookie() == cookie && recipient->getWho() == binder) {
Steven Moreland901c7452018-09-04 16:17:28 -0700328 mDeathRecipients.erase(it.base() - 1);
329
Steven Moreland64127ca2019-03-13 09:25:44 -0700330 status_t status = binder->unlinkToDeath(recipient, cookie, 0 /*flags*/);
Steven Moreland5d62e442018-09-13 15:01:02 -0700331 if (status != ::android::OK) {
Steven Moreland901c7452018-09-04 16:17:28 -0700332 LOG(ERROR) << __func__
333 << ": removed reference to death recipient but unlink failed.";
334 }
Steven Moreland5d62e442018-09-13 15:01:02 -0700335 return PruneStatusT(status);
Steven Moreland901c7452018-09-04 16:17:28 -0700336 }
337 }
338
Steven Moreland5d62e442018-09-13 15:01:02 -0700339 return STATUS_NAME_NOT_FOUND;
Steven Moreland901c7452018-09-04 16:17:28 -0700340}
341
342// start of C-API methods
343
Steven Moreland2e87adc2018-08-20 19:47:00 -0700344AIBinder* AIBinder_new(const AIBinder_Class* clazz, void* args) {
345 if (clazz == nullptr) {
346 LOG(ERROR) << __func__ << ": Must provide class to construct local binder.";
347 return nullptr;
348 }
349
350 void* userData = clazz->onCreate(args);
351
Steven Moreland71cddc32018-08-30 23:39:22 -0700352 sp<AIBinder> ret = new ABBinder(clazz, userData);
353 ABBinderTag::attach(ret->getBinder());
354
355 AIBinder_incStrong(ret.get());
356 return ret.get();
Steven Moreland2e87adc2018-08-20 19:47:00 -0700357}
358
Steven Moreland5ea54da2018-09-04 13:29:55 -0700359bool AIBinder_isRemote(const AIBinder* binder) {
Steven Moreland2e87adc2018-08-20 19:47:00 -0700360 if (binder == nullptr) {
Steven Moreland56f752a2018-11-05 17:23:37 -0800361 return false;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700362 }
363
364 return binder->isRemote();
365}
366
Steven Moreland65867d72018-09-04 14:22:26 -0700367bool AIBinder_isAlive(const AIBinder* binder) {
368 if (binder == nullptr) {
369 return false;
370 }
371
372 return const_cast<AIBinder*>(binder)->getBinder()->isBinderAlive();
373}
374
375binder_status_t AIBinder_ping(AIBinder* binder) {
376 if (binder == nullptr) {
Steven Moreland5d62e442018-09-13 15:01:02 -0700377 return STATUS_UNEXPECTED_NULL;
Steven Moreland65867d72018-09-04 14:22:26 -0700378 }
379
Steven Moreland5d62e442018-09-13 15:01:02 -0700380 return PruneStatusT(binder->getBinder()->pingBinder());
Steven Moreland65867d72018-09-04 14:22:26 -0700381}
382
Steven Morelanda194c452019-03-04 16:47:07 -0800383binder_status_t AIBinder_dump(AIBinder* binder, int fd, const char** args, uint32_t numArgs) {
384 if (binder == nullptr) {
385 return STATUS_UNEXPECTED_NULL;
386 }
387
388 ABBinder* bBinder = binder->asABBinder();
389 if (bBinder != nullptr) {
390 AIBinder_onDump onDump = binder->getClass()->onDump;
391 if (onDump == nullptr) {
392 return STATUS_OK;
393 }
394 return PruneStatusT(onDump(bBinder, fd, args, numArgs));
395 }
396
397 ::android::Vector<String16> utf16Args;
398 utf16Args.setCapacity(numArgs);
399 for (uint32_t i = 0; i < numArgs; i++) {
400 utf16Args.push(String16(String8(args[i])));
401 }
402
403 status_t status = binder->getBinder()->dump(fd, utf16Args);
404 return PruneStatusT(status);
405}
406
Steven Moreland901c7452018-09-04 16:17:28 -0700407binder_status_t AIBinder_linkToDeath(AIBinder* binder, AIBinder_DeathRecipient* recipient,
408 void* cookie) {
409 if (binder == nullptr || recipient == nullptr) {
410 LOG(ERROR) << __func__ << ": Must provide binder and recipient.";
Steven Moreland5d62e442018-09-13 15:01:02 -0700411 return STATUS_UNEXPECTED_NULL;
Steven Moreland901c7452018-09-04 16:17:28 -0700412 }
413
Steven Moreland5d62e442018-09-13 15:01:02 -0700414 // returns binder_status_t
Steven Moreland64127ca2019-03-13 09:25:44 -0700415 return recipient->linkToDeath(binder->getBinder(), cookie);
Steven Moreland901c7452018-09-04 16:17:28 -0700416}
417
418binder_status_t AIBinder_unlinkToDeath(AIBinder* binder, AIBinder_DeathRecipient* recipient,
419 void* cookie) {
420 if (binder == nullptr || recipient == nullptr) {
421 LOG(ERROR) << __func__ << ": Must provide binder and recipient.";
Steven Moreland5d62e442018-09-13 15:01:02 -0700422 return STATUS_UNEXPECTED_NULL;
Steven Moreland901c7452018-09-04 16:17:28 -0700423 }
424
Steven Moreland5d62e442018-09-13 15:01:02 -0700425 // returns binder_status_t
Steven Moreland64127ca2019-03-13 09:25:44 -0700426 return recipient->unlinkToDeath(binder->getBinder(), cookie);
Steven Moreland901c7452018-09-04 16:17:28 -0700427}
428
Steven Morelandf3034b02018-11-12 17:37:46 -0800429uid_t AIBinder_getCallingUid() {
430 return ::android::IPCThreadState::self()->getCallingUid();
431}
432
433pid_t AIBinder_getCallingPid() {
434 return ::android::IPCThreadState::self()->getCallingPid();
435}
436
Steven Moreland2e87adc2018-08-20 19:47:00 -0700437void AIBinder_incStrong(AIBinder* binder) {
438 if (binder == nullptr) {
439 LOG(ERROR) << __func__ << ": on null binder";
440 return;
441 }
442
443 binder->incStrong(nullptr);
444}
445void AIBinder_decStrong(AIBinder* binder) {
446 if (binder == nullptr) {
447 LOG(ERROR) << __func__ << ": on null binder";
448 return;
449 }
450
451 binder->decStrong(nullptr);
452}
453int32_t AIBinder_debugGetRefCount(AIBinder* binder) {
454 if (binder == nullptr) {
455 LOG(ERROR) << __func__ << ": on null binder";
456 return -1;
457 }
458
459 return binder->getStrongCount();
460}
461
Steven Moreland71cddc32018-08-30 23:39:22 -0700462bool AIBinder_associateClass(AIBinder* binder, const AIBinder_Class* clazz) {
463 if (binder == nullptr) {
464 return false;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700465 }
466
Steven Moreland71cddc32018-08-30 23:39:22 -0700467 return binder->associateClass(clazz);
Steven Moreland2e87adc2018-08-20 19:47:00 -0700468}
469
470const AIBinder_Class* AIBinder_getClass(AIBinder* binder) {
471 if (binder == nullptr) {
472 return nullptr;
473 }
474
475 return binder->getClass();
476}
477
478void* AIBinder_getUserData(AIBinder* binder) {
479 if (binder == nullptr) {
480 return nullptr;
481 }
482
483 ABBinder* bBinder = binder->asABBinder();
484 if (bBinder == nullptr) {
485 return nullptr;
486 }
487
488 return bBinder->getUserData();
489}
490
491binder_status_t AIBinder_prepareTransaction(AIBinder* binder, AParcel** in) {
492 if (binder == nullptr || in == nullptr) {
493 LOG(ERROR) << __func__ << ": requires non-null parameters.";
Steven Moreland5d62e442018-09-13 15:01:02 -0700494 return STATUS_UNEXPECTED_NULL;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700495 }
496 const AIBinder_Class* clazz = binder->getClass();
497 if (clazz == nullptr) {
498 LOG(ERROR) << __func__
499 << ": Class must be defined for a remote binder transaction. See "
500 "AIBinder_associateClass.";
Steven Moreland5d62e442018-09-13 15:01:02 -0700501 return STATUS_INVALID_OPERATION;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700502 }
503
Steven Moreland3527c2e2018-09-05 17:07:14 -0700504 if (!binder->isRemote()) {
Steven Moreland74521c82018-09-07 14:50:40 -0700505 LOG(WARNING) << "A binder object at " << binder
506 << " is being transacted on, however, this object is in the same process as "
507 "its proxy. Transacting with this binder is expensive compared to just "
508 "calling the corresponding functionality in the same process.";
Steven Moreland3527c2e2018-09-05 17:07:14 -0700509 }
510
Steven Moreland2e87adc2018-08-20 19:47:00 -0700511 *in = new AParcel(binder);
Steven Morelandf18615b2018-09-14 11:43:06 -0700512 status_t status = (*in)->get()->writeInterfaceToken(clazz->getInterfaceDescriptor());
Steven Moreland5d62e442018-09-13 15:01:02 -0700513 binder_status_t ret = PruneStatusT(status);
514
515 if (ret != STATUS_OK) {
Steven Moreland2e87adc2018-08-20 19:47:00 -0700516 delete *in;
517 *in = nullptr;
518 }
519
Steven Moreland5d62e442018-09-13 15:01:02 -0700520 return ret;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700521}
522
Steven Moreland9b80e282018-09-19 13:57:23 -0700523static void DestroyParcel(AParcel** parcel) {
524 delete *parcel;
525 *parcel = nullptr;
526}
527
Steven Moreland2e87adc2018-08-20 19:47:00 -0700528binder_status_t AIBinder_transact(AIBinder* binder, transaction_code_t code, AParcel** in,
529 AParcel** out, binder_flags_t flags) {
530 if (in == nullptr) {
531 LOG(ERROR) << __func__ << ": requires non-null in parameter";
Steven Moreland5d62e442018-09-13 15:01:02 -0700532 return STATUS_UNEXPECTED_NULL;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700533 }
534
Steven Morelandcaa776c2018-09-04 13:48:11 -0700535 using AutoParcelDestroyer = std::unique_ptr<AParcel*, void (*)(AParcel**)>;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700536 // This object is the input to the transaction. This function takes ownership of it and deletes
537 // it.
Steven Moreland9b80e282018-09-19 13:57:23 -0700538 AutoParcelDestroyer forIn(in, DestroyParcel);
Steven Moreland2e87adc2018-08-20 19:47:00 -0700539
540 if (!isUserCommand(code)) {
541 LOG(ERROR) << __func__ << ": Only user-defined transactions can be made from the NDK.";
Steven Moreland5d62e442018-09-13 15:01:02 -0700542 return STATUS_UNKNOWN_TRANSACTION;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700543 }
544
545 if ((flags & ~FLAG_ONEWAY) != 0) {
546 LOG(ERROR) << __func__ << ": Unrecognized flags sent: " << flags;
Steven Moreland5d62e442018-09-13 15:01:02 -0700547 return STATUS_BAD_VALUE;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700548 }
549
550 if (binder == nullptr || *in == nullptr || out == nullptr) {
551 LOG(ERROR) << __func__ << ": requires non-null parameters.";
Steven Moreland5d62e442018-09-13 15:01:02 -0700552 return STATUS_UNEXPECTED_NULL;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700553 }
554
555 if ((*in)->getBinder() != binder) {
556 LOG(ERROR) << __func__ << ": parcel is associated with binder object " << binder
557 << " but called with " << (*in)->getBinder();
Steven Moreland5d62e442018-09-13 15:01:02 -0700558 return STATUS_BAD_VALUE;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700559 }
560
561 *out = new AParcel(binder);
562
Steven Morelandf18615b2018-09-14 11:43:06 -0700563 status_t status = binder->getBinder()->transact(code, *(*in)->get(), (*out)->get(), flags);
Steven Moreland5d62e442018-09-13 15:01:02 -0700564 binder_status_t ret = PruneStatusT(status);
Steven Moreland2e87adc2018-08-20 19:47:00 -0700565
Steven Moreland5d62e442018-09-13 15:01:02 -0700566 if (ret != STATUS_OK) {
Steven Moreland2e87adc2018-08-20 19:47:00 -0700567 delete *out;
568 *out = nullptr;
569 }
570
Steven Moreland5d62e442018-09-13 15:01:02 -0700571 return ret;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700572}
Steven Moreland901c7452018-09-04 16:17:28 -0700573
574AIBinder_DeathRecipient* AIBinder_DeathRecipient_new(
575 AIBinder_DeathRecipient_onBinderDied onBinderDied) {
576 if (onBinderDied == nullptr) {
577 LOG(ERROR) << __func__ << ": requires non-null onBinderDied parameter.";
578 return nullptr;
579 }
Steven Moreland64127ca2019-03-13 09:25:44 -0700580 auto ret = new AIBinder_DeathRecipient(onBinderDied);
581 ret->incStrong(nullptr);
582 return ret;
Steven Moreland901c7452018-09-04 16:17:28 -0700583}
584
Steven Moreland9b80e282018-09-19 13:57:23 -0700585void AIBinder_DeathRecipient_delete(AIBinder_DeathRecipient* recipient) {
Steven Moreland64127ca2019-03-13 09:25:44 -0700586 if (recipient == nullptr) {
587 return;
588 }
589
590 recipient->decStrong(nullptr);
Steven Moreland901c7452018-09-04 16:17:28 -0700591}
Steven Morelandea14ef22019-08-20 10:50:08 -0700592
593binder_status_t AIBinder_getExtension(AIBinder* binder, AIBinder** outExt) {
594 if (binder == nullptr || outExt == nullptr) {
595 if (outExt != nullptr) {
596 *outExt = nullptr;
597 }
598 return STATUS_UNEXPECTED_NULL;
599 }
600
601 sp<IBinder> ext;
602 status_t res = binder->getBinder()->getExtension(&ext);
603
604 if (res != android::OK) {
605 *outExt = nullptr;
606 return PruneStatusT(res);
607 }
608
609 sp<AIBinder> ret = ABpBinder::lookupOrCreateFromBinder(ext);
610 if (ret != nullptr) ret->incStrong(binder);
611
612 *outExt = ret.get();
613 return STATUS_OK;
614}
615
616binder_status_t AIBinder_setExtension(AIBinder* binder, AIBinder* ext) {
617 if (binder == nullptr || ext == nullptr) {
618 return STATUS_UNEXPECTED_NULL;
619 }
620
621 ABBinder* rawBinder = binder->asABBinder();
622 if (rawBinder == nullptr) {
623 return STATUS_INVALID_OPERATION;
624 }
625
626 rawBinder->setExtension(ext->getBinder());
627 return STATUS_OK;
628}