blob: bdbc0d3b8b06bdb72ad9538c69da35d6a9089fa3 [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);
272 mWho = nullptr;
273}
274
275AIBinder_DeathRecipient::AIBinder_DeathRecipient(AIBinder_DeathRecipient_onBinderDied onDied)
Steven Moreland6cf66ac2018-11-02 18:14:54 -0700276 : mOnDied(onDied) {
Steven Moreland901c7452018-09-04 16:17:28 -0700277 CHECK(onDied != nullptr);
278}
279
280binder_status_t AIBinder_DeathRecipient::linkToDeath(AIBinder* binder, void* cookie) {
281 CHECK(binder != nullptr);
282
283 std::lock_guard<std::mutex> l(mDeathRecipientsMutex);
284
285 sp<TransferDeathRecipient> recipient =
286 new TransferDeathRecipient(binder->getBinder(), cookie, mOnDied);
287
Steven Moreland5d62e442018-09-13 15:01:02 -0700288 status_t status = binder->getBinder()->linkToDeath(recipient, cookie, 0 /*flags*/);
289 if (status != STATUS_OK) {
290 return PruneStatusT(status);
Steven Moreland901c7452018-09-04 16:17:28 -0700291 }
292
293 mDeathRecipients.push_back(recipient);
Steven Moreland5d62e442018-09-13 15:01:02 -0700294 return STATUS_OK;
Steven Moreland901c7452018-09-04 16:17:28 -0700295}
296
297binder_status_t AIBinder_DeathRecipient::unlinkToDeath(AIBinder* binder, void* cookie) {
298 CHECK(binder != nullptr);
299
300 std::lock_guard<std::mutex> l(mDeathRecipientsMutex);
301
302 for (auto it = mDeathRecipients.rbegin(); it != mDeathRecipients.rend(); ++it) {
303 sp<TransferDeathRecipient> recipient = *it;
304
Steven Moreland5d62e442018-09-13 15:01:02 -0700305 if (recipient->getCookie() == cookie && recipient->getWho() == binder->getBinder()) {
Steven Moreland901c7452018-09-04 16:17:28 -0700306 mDeathRecipients.erase(it.base() - 1);
307
Steven Moreland5d62e442018-09-13 15:01:02 -0700308 status_t status = binder->getBinder()->unlinkToDeath(recipient, cookie, 0 /*flags*/);
309 if (status != ::android::OK) {
Steven Moreland901c7452018-09-04 16:17:28 -0700310 LOG(ERROR) << __func__
311 << ": removed reference to death recipient but unlink failed.";
312 }
Steven Moreland5d62e442018-09-13 15:01:02 -0700313 return PruneStatusT(status);
Steven Moreland901c7452018-09-04 16:17:28 -0700314 }
315 }
316
Steven Moreland5d62e442018-09-13 15:01:02 -0700317 return STATUS_NAME_NOT_FOUND;
Steven Moreland901c7452018-09-04 16:17:28 -0700318}
319
320// start of C-API methods
321
Steven Moreland2e87adc2018-08-20 19:47:00 -0700322AIBinder* AIBinder_new(const AIBinder_Class* clazz, void* args) {
323 if (clazz == nullptr) {
324 LOG(ERROR) << __func__ << ": Must provide class to construct local binder.";
325 return nullptr;
326 }
327
328 void* userData = clazz->onCreate(args);
329
Steven Moreland71cddc32018-08-30 23:39:22 -0700330 sp<AIBinder> ret = new ABBinder(clazz, userData);
331 ABBinderTag::attach(ret->getBinder());
332
333 AIBinder_incStrong(ret.get());
334 return ret.get();
Steven Moreland2e87adc2018-08-20 19:47:00 -0700335}
336
Steven Moreland5ea54da2018-09-04 13:29:55 -0700337bool AIBinder_isRemote(const AIBinder* binder) {
Steven Moreland2e87adc2018-08-20 19:47:00 -0700338 if (binder == nullptr) {
Steven Moreland56f752a2018-11-05 17:23:37 -0800339 return false;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700340 }
341
342 return binder->isRemote();
343}
344
Steven Moreland65867d72018-09-04 14:22:26 -0700345bool AIBinder_isAlive(const AIBinder* binder) {
346 if (binder == nullptr) {
347 return false;
348 }
349
350 return const_cast<AIBinder*>(binder)->getBinder()->isBinderAlive();
351}
352
353binder_status_t AIBinder_ping(AIBinder* binder) {
354 if (binder == nullptr) {
Steven Moreland5d62e442018-09-13 15:01:02 -0700355 return STATUS_UNEXPECTED_NULL;
Steven Moreland65867d72018-09-04 14:22:26 -0700356 }
357
Steven Moreland5d62e442018-09-13 15:01:02 -0700358 return PruneStatusT(binder->getBinder()->pingBinder());
Steven Moreland65867d72018-09-04 14:22:26 -0700359}
360
Steven Morelanda194c452019-03-04 16:47:07 -0800361binder_status_t AIBinder_dump(AIBinder* binder, int fd, const char** args, uint32_t numArgs) {
362 if (binder == nullptr) {
363 return STATUS_UNEXPECTED_NULL;
364 }
365
366 ABBinder* bBinder = binder->asABBinder();
367 if (bBinder != nullptr) {
368 AIBinder_onDump onDump = binder->getClass()->onDump;
369 if (onDump == nullptr) {
370 return STATUS_OK;
371 }
372 return PruneStatusT(onDump(bBinder, fd, args, numArgs));
373 }
374
375 ::android::Vector<String16> utf16Args;
376 utf16Args.setCapacity(numArgs);
377 for (uint32_t i = 0; i < numArgs; i++) {
378 utf16Args.push(String16(String8(args[i])));
379 }
380
381 status_t status = binder->getBinder()->dump(fd, utf16Args);
382 return PruneStatusT(status);
383}
384
Steven Moreland901c7452018-09-04 16:17:28 -0700385binder_status_t AIBinder_linkToDeath(AIBinder* binder, AIBinder_DeathRecipient* recipient,
386 void* cookie) {
387 if (binder == nullptr || recipient == nullptr) {
388 LOG(ERROR) << __func__ << ": Must provide binder and recipient.";
Steven Moreland5d62e442018-09-13 15:01:02 -0700389 return STATUS_UNEXPECTED_NULL;
Steven Moreland901c7452018-09-04 16:17:28 -0700390 }
391
Steven Moreland5d62e442018-09-13 15:01:02 -0700392 // returns binder_status_t
Steven Moreland901c7452018-09-04 16:17:28 -0700393 return recipient->linkToDeath(binder, cookie);
394}
395
396binder_status_t AIBinder_unlinkToDeath(AIBinder* binder, AIBinder_DeathRecipient* recipient,
397 void* cookie) {
398 if (binder == nullptr || recipient == nullptr) {
399 LOG(ERROR) << __func__ << ": Must provide binder and recipient.";
Steven Moreland5d62e442018-09-13 15:01:02 -0700400 return STATUS_UNEXPECTED_NULL;
Steven Moreland901c7452018-09-04 16:17:28 -0700401 }
402
Steven Moreland5d62e442018-09-13 15:01:02 -0700403 // returns binder_status_t
Steven Moreland901c7452018-09-04 16:17:28 -0700404 return recipient->unlinkToDeath(binder, cookie);
405}
406
Steven Morelandf3034b02018-11-12 17:37:46 -0800407uid_t AIBinder_getCallingUid() {
408 return ::android::IPCThreadState::self()->getCallingUid();
409}
410
411pid_t AIBinder_getCallingPid() {
412 return ::android::IPCThreadState::self()->getCallingPid();
413}
414
Steven Moreland2e87adc2018-08-20 19:47:00 -0700415void AIBinder_incStrong(AIBinder* binder) {
416 if (binder == nullptr) {
417 LOG(ERROR) << __func__ << ": on null binder";
418 return;
419 }
420
421 binder->incStrong(nullptr);
422}
423void AIBinder_decStrong(AIBinder* binder) {
424 if (binder == nullptr) {
425 LOG(ERROR) << __func__ << ": on null binder";
426 return;
427 }
428
429 binder->decStrong(nullptr);
430}
431int32_t AIBinder_debugGetRefCount(AIBinder* binder) {
432 if (binder == nullptr) {
433 LOG(ERROR) << __func__ << ": on null binder";
434 return -1;
435 }
436
437 return binder->getStrongCount();
438}
439
Steven Moreland71cddc32018-08-30 23:39:22 -0700440bool AIBinder_associateClass(AIBinder* binder, const AIBinder_Class* clazz) {
441 if (binder == nullptr) {
442 return false;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700443 }
444
Steven Moreland71cddc32018-08-30 23:39:22 -0700445 return binder->associateClass(clazz);
Steven Moreland2e87adc2018-08-20 19:47:00 -0700446}
447
448const AIBinder_Class* AIBinder_getClass(AIBinder* binder) {
449 if (binder == nullptr) {
450 return nullptr;
451 }
452
453 return binder->getClass();
454}
455
456void* AIBinder_getUserData(AIBinder* binder) {
457 if (binder == nullptr) {
458 return nullptr;
459 }
460
461 ABBinder* bBinder = binder->asABBinder();
462 if (bBinder == nullptr) {
463 return nullptr;
464 }
465
466 return bBinder->getUserData();
467}
468
469binder_status_t AIBinder_prepareTransaction(AIBinder* binder, AParcel** in) {
470 if (binder == nullptr || in == nullptr) {
471 LOG(ERROR) << __func__ << ": requires non-null parameters.";
Steven Moreland5d62e442018-09-13 15:01:02 -0700472 return STATUS_UNEXPECTED_NULL;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700473 }
474 const AIBinder_Class* clazz = binder->getClass();
475 if (clazz == nullptr) {
476 LOG(ERROR) << __func__
477 << ": Class must be defined for a remote binder transaction. See "
478 "AIBinder_associateClass.";
Steven Moreland5d62e442018-09-13 15:01:02 -0700479 return STATUS_INVALID_OPERATION;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700480 }
481
Steven Moreland3527c2e2018-09-05 17:07:14 -0700482 if (!binder->isRemote()) {
Steven Moreland74521c82018-09-07 14:50:40 -0700483 LOG(WARNING) << "A binder object at " << binder
484 << " is being transacted on, however, this object is in the same process as "
485 "its proxy. Transacting with this binder is expensive compared to just "
486 "calling the corresponding functionality in the same process.";
Steven Moreland3527c2e2018-09-05 17:07:14 -0700487 }
488
Steven Moreland2e87adc2018-08-20 19:47:00 -0700489 *in = new AParcel(binder);
Steven Morelandf18615b2018-09-14 11:43:06 -0700490 status_t status = (*in)->get()->writeInterfaceToken(clazz->getInterfaceDescriptor());
Steven Moreland5d62e442018-09-13 15:01:02 -0700491 binder_status_t ret = PruneStatusT(status);
492
493 if (ret != STATUS_OK) {
Steven Moreland2e87adc2018-08-20 19:47:00 -0700494 delete *in;
495 *in = nullptr;
496 }
497
Steven Moreland5d62e442018-09-13 15:01:02 -0700498 return ret;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700499}
500
Steven Moreland9b80e282018-09-19 13:57:23 -0700501static void DestroyParcel(AParcel** parcel) {
502 delete *parcel;
503 *parcel = nullptr;
504}
505
Steven Moreland2e87adc2018-08-20 19:47:00 -0700506binder_status_t AIBinder_transact(AIBinder* binder, transaction_code_t code, AParcel** in,
507 AParcel** out, binder_flags_t flags) {
508 if (in == nullptr) {
509 LOG(ERROR) << __func__ << ": requires non-null in parameter";
Steven Moreland5d62e442018-09-13 15:01:02 -0700510 return STATUS_UNEXPECTED_NULL;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700511 }
512
Steven Morelandcaa776c2018-09-04 13:48:11 -0700513 using AutoParcelDestroyer = std::unique_ptr<AParcel*, void (*)(AParcel**)>;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700514 // This object is the input to the transaction. This function takes ownership of it and deletes
515 // it.
Steven Moreland9b80e282018-09-19 13:57:23 -0700516 AutoParcelDestroyer forIn(in, DestroyParcel);
Steven Moreland2e87adc2018-08-20 19:47:00 -0700517
518 if (!isUserCommand(code)) {
519 LOG(ERROR) << __func__ << ": Only user-defined transactions can be made from the NDK.";
Steven Moreland5d62e442018-09-13 15:01:02 -0700520 return STATUS_UNKNOWN_TRANSACTION;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700521 }
522
523 if ((flags & ~FLAG_ONEWAY) != 0) {
524 LOG(ERROR) << __func__ << ": Unrecognized flags sent: " << flags;
Steven Moreland5d62e442018-09-13 15:01:02 -0700525 return STATUS_BAD_VALUE;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700526 }
527
528 if (binder == nullptr || *in == nullptr || out == nullptr) {
529 LOG(ERROR) << __func__ << ": requires non-null parameters.";
Steven Moreland5d62e442018-09-13 15:01:02 -0700530 return STATUS_UNEXPECTED_NULL;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700531 }
532
533 if ((*in)->getBinder() != binder) {
534 LOG(ERROR) << __func__ << ": parcel is associated with binder object " << binder
535 << " but called with " << (*in)->getBinder();
Steven Moreland5d62e442018-09-13 15:01:02 -0700536 return STATUS_BAD_VALUE;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700537 }
538
539 *out = new AParcel(binder);
540
Steven Morelandf18615b2018-09-14 11:43:06 -0700541 status_t status = binder->getBinder()->transact(code, *(*in)->get(), (*out)->get(), flags);
Steven Moreland5d62e442018-09-13 15:01:02 -0700542 binder_status_t ret = PruneStatusT(status);
Steven Moreland2e87adc2018-08-20 19:47:00 -0700543
Steven Moreland5d62e442018-09-13 15:01:02 -0700544 if (ret != STATUS_OK) {
Steven Moreland2e87adc2018-08-20 19:47:00 -0700545 delete *out;
546 *out = nullptr;
547 }
548
Steven Moreland5d62e442018-09-13 15:01:02 -0700549 return ret;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700550}
Steven Moreland901c7452018-09-04 16:17:28 -0700551
552AIBinder_DeathRecipient* AIBinder_DeathRecipient_new(
553 AIBinder_DeathRecipient_onBinderDied onBinderDied) {
554 if (onBinderDied == nullptr) {
555 LOG(ERROR) << __func__ << ": requires non-null onBinderDied parameter.";
556 return nullptr;
557 }
558 return new AIBinder_DeathRecipient(onBinderDied);
559}
560
Steven Moreland9b80e282018-09-19 13:57:23 -0700561void AIBinder_DeathRecipient_delete(AIBinder_DeathRecipient* recipient) {
562 delete recipient;
Steven Moreland901c7452018-09-04 16:17:28 -0700563}