blob: 649faa1c76ac0c7b668f3726fa33cab6b5cf4bfb [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
Steven Moreland46b5fea2019-10-15 11:22:18 -070020#include <android/binder_stability.h>
Steven Moreland2e87adc2018-08-20 19:47:00 -070021#include <android/binder_status.h>
Steven Moreland4d5ad492018-09-13 12:49:16 -070022#include "parcel_internal.h"
Steven Moreland5d62e442018-09-13 15:01:02 -070023#include "status_internal.h"
Steven Moreland2e87adc2018-08-20 19:47:00 -070024
25#include <android-base/logging.h>
Steven Morelandf3034b02018-11-12 17:37:46 -080026#include <binder/IPCThreadState.h>
Ruchir Rastogicc7a7462020-01-31 14:29:15 -080027#include <binder/IResultReceiver.h>
28#include <private/android_filesystem_config.h>
Steven Moreland2e87adc2018-08-20 19:47:00 -070029
Steven Moreland901c7452018-09-04 16:17:28 -070030using DeathRecipient = ::android::IBinder::DeathRecipient;
31
Steven Moreland2e87adc2018-08-20 19:47:00 -070032using ::android::IBinder;
Ruchir Rastogicc7a7462020-01-31 14:29:15 -080033using ::android::IResultReceiver;
Steven Moreland2e87adc2018-08-20 19:47:00 -070034using ::android::Parcel;
35using ::android::sp;
Steven Moreland5d62e442018-09-13 15:01:02 -070036using ::android::status_t;
Steven Moreland2e87adc2018-08-20 19:47:00 -070037using ::android::String16;
Steven Morelanda194c452019-03-04 16:47:07 -080038using ::android::String8;
Steven Moreland2e87adc2018-08-20 19:47:00 -070039using ::android::wp;
40
Steven Moreland71cddc32018-08-30 23:39:22 -070041namespace ABBinderTag {
42
43static const void* kId = "ABBinder";
44static void* kValue = static_cast<void*>(new bool{true});
Steven Moreland94968952018-09-05 14:42:59 -070045void clean(const void* /*id*/, void* /*obj*/, void* /*cookie*/){/* do nothing */};
Steven Moreland71cddc32018-08-30 23:39:22 -070046
47static void attach(const sp<IBinder>& binder) {
Steven Moreland94968952018-09-05 14:42:59 -070048 binder->attachObject(kId, kValue, nullptr /*cookie*/, clean);
Steven Moreland71cddc32018-08-30 23:39:22 -070049}
50static bool has(const sp<IBinder>& binder) {
51 return binder != nullptr && binder->findObject(kId) == kValue;
52}
53
Steven Moreland6cf66ac2018-11-02 18:14:54 -070054} // namespace ABBinderTag
Steven Moreland71cddc32018-08-30 23:39:22 -070055
Steven Moreland94968952018-09-05 14:42:59 -070056namespace ABpBinderTag {
57
58static std::mutex gLock;
59static const void* kId = "ABpBinder";
60struct Value {
61 wp<ABpBinder> binder;
62};
63void clean(const void* id, void* obj, void* cookie) {
64 CHECK(id == kId) << id << " " << obj << " " << cookie;
65
66 delete static_cast<Value*>(obj);
67};
68
Steven Moreland6cf66ac2018-11-02 18:14:54 -070069} // namespace ABpBinderTag
Steven Moreland94968952018-09-05 14:42:59 -070070
Steven Moreland2e87adc2018-08-20 19:47:00 -070071AIBinder::AIBinder(const AIBinder_Class* clazz) : mClazz(clazz) {}
72AIBinder::~AIBinder() {}
73
Steven Moreland71cddc32018-08-30 23:39:22 -070074bool AIBinder::associateClass(const AIBinder_Class* clazz) {
Steven Moreland71cddc32018-08-30 23:39:22 -070075 if (clazz == nullptr) return false;
76 if (mClazz == clazz) return true;
Steven Moreland2e87adc2018-08-20 19:47:00 -070077
78 String8 newDescriptor(clazz->getInterfaceDescriptor());
79
80 if (mClazz != nullptr) {
81 String8 currentDescriptor(mClazz->getInterfaceDescriptor());
82 if (newDescriptor == currentDescriptor) {
83 LOG(ERROR) << __func__ << ": Class descriptors '" << currentDescriptor
84 << "' match during associateClass, but they are different class objects. "
85 "Class descriptor collision?";
Steven Moreland71cddc32018-08-30 23:39:22 -070086 } else {
87 LOG(ERROR) << __func__
88 << ": Class cannot be associated on object which already has a class. "
89 "Trying to associate to '"
90 << newDescriptor.c_str() << "' but already set to '"
91 << currentDescriptor.c_str() << "'.";
Steven Moreland2e87adc2018-08-20 19:47:00 -070092 }
93
Steven Moreland71cddc32018-08-30 23:39:22 -070094 // always a failure because we know mClazz != clazz
95 return false;
Steven Moreland2e87adc2018-08-20 19:47:00 -070096 }
97
Steven Moreland6cf66ac2018-11-02 18:14:54 -070098 CHECK(asABpBinder() != nullptr); // ABBinder always has a descriptor
Steven Moreland71cddc32018-08-30 23:39:22 -070099
Steven Moreland2e87adc2018-08-20 19:47:00 -0700100 String8 descriptor(getBinder()->getInterfaceDescriptor());
101 if (descriptor != newDescriptor) {
102 LOG(ERROR) << __func__ << ": Expecting binder to have class '" << newDescriptor.c_str()
103 << "' but descriptor is actually '" << descriptor.c_str() << "'.";
Steven Moreland71cddc32018-08-30 23:39:22 -0700104 return false;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700105 }
106
Steven Moreland71cddc32018-08-30 23:39:22 -0700107 // if this is a local object, it's not one known to libbinder_ndk
Steven Moreland2e87adc2018-08-20 19:47:00 -0700108 mClazz = clazz;
109
Steven Moreland71cddc32018-08-30 23:39:22 -0700110 return true;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700111}
112
113ABBinder::ABBinder(const AIBinder_Class* clazz, void* userData)
Steven Moreland6cf66ac2018-11-02 18:14:54 -0700114 : AIBinder(clazz), BBinder(), mUserData(userData) {
Steven Moreland2e87adc2018-08-20 19:47:00 -0700115 CHECK(clazz != nullptr);
116}
117ABBinder::~ABBinder() {
118 getClass()->onDestroy(mUserData);
119}
120
121const String16& ABBinder::getInterfaceDescriptor() const {
122 return getClass()->getInterfaceDescriptor();
123}
124
Steven Morelanda194c452019-03-04 16:47:07 -0800125status_t ABBinder::dump(int fd, const ::android::Vector<String16>& args) {
126 AIBinder_onDump onDump = getClass()->onDump;
127
128 if (onDump == nullptr) {
129 return STATUS_OK;
130 }
131
132 // technically UINT32_MAX would be okay here, but INT32_MAX is expected since this may be
133 // null in Java
134 if (args.size() > INT32_MAX) {
135 LOG(ERROR) << "ABBinder::dump received too many arguments: " << args.size();
136 return STATUS_BAD_VALUE;
137 }
138
139 std::vector<String8> utf8Args; // owns memory of utf8s
140 utf8Args.reserve(args.size());
141 std::vector<const char*> utf8Pointers; // what can be passed over NDK API
142 utf8Pointers.reserve(args.size());
143
144 for (size_t i = 0; i < args.size(); i++) {
145 utf8Args.push_back(String8(args[i]));
146 utf8Pointers.push_back(utf8Args[i].c_str());
147 }
148
149 return onDump(this, fd, utf8Pointers.data(), utf8Pointers.size());
150}
151
Steven Moreland5d62e442018-09-13 15:01:02 -0700152status_t ABBinder::onTransact(transaction_code_t code, const Parcel& data, Parcel* reply,
153 binder_flags_t flags) {
Steven Moreland2e87adc2018-08-20 19:47:00 -0700154 if (isUserCommand(code)) {
155 if (!data.checkInterface(this)) {
Steven Moreland9d089af2018-09-18 08:58:09 -0700156 return STATUS_BAD_TYPE;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700157 }
158
159 const AParcel in = AParcel::readOnly(this, &data);
160 AParcel out = AParcel(this, reply, false /*owns*/);
161
Steven Moreland5d62e442018-09-13 15:01:02 -0700162 binder_status_t status = getClass()->onTransact(this, code, &in, &out);
163 return PruneStatusT(status);
Ruchir Rastogicc7a7462020-01-31 14:29:15 -0800164 } else if (code == SHELL_COMMAND_TRANSACTION) {
165 int in = data.readFileDescriptor();
166 int out = data.readFileDescriptor();
167 int err = data.readFileDescriptor();
168
169 int argc = data.readInt32();
170 std::vector<String8> utf8Args; // owns memory of utf8s
171 std::vector<const char*> utf8Pointers; // what can be passed over NDK API
172 for (int i = 0; i < argc && data.dataAvail() > 0; i++) {
173 utf8Args.push_back(String8(data.readString16()));
174 utf8Pointers.push_back(utf8Args[i].c_str());
175 }
176
177 data.readStrongBinder(); // skip over the IShellCallback
178 sp<IResultReceiver> resultReceiver = IResultReceiver::asInterface(data.readStrongBinder());
179
180 // Shell commands should only be callable by ADB.
181 uid_t uid = AIBinder_getCallingUid();
182 if (uid != AID_ROOT && uid != AID_SHELL) {
183 if (resultReceiver != nullptr) {
184 resultReceiver->send(-1);
185 }
186 return STATUS_PERMISSION_DENIED;
187 }
188
189 // Check that the file descriptors are valid.
190 if (in == STATUS_BAD_TYPE || out == STATUS_BAD_TYPE || err == STATUS_BAD_TYPE) {
191 if (resultReceiver != nullptr) {
192 resultReceiver->send(-1);
193 }
194 return STATUS_BAD_VALUE;
195 }
196
197 binder_status_t status = getClass()->handleShellCommand(
198 this, in, out, err, utf8Pointers.data(), utf8Pointers.size());
199 if (resultReceiver != nullptr) {
200 resultReceiver->send(status);
201 }
202 return status;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700203 } else {
204 return BBinder::onTransact(code, data, reply, flags);
205 }
206}
207
Steven Moreland71cddc32018-08-30 23:39:22 -0700208ABpBinder::ABpBinder(const ::android::sp<::android::IBinder>& binder)
Steven Moreland6cf66ac2018-11-02 18:14:54 -0700209 : AIBinder(nullptr /*clazz*/), BpRefBase(binder) {
Steven Moreland2e87adc2018-08-20 19:47:00 -0700210 CHECK(binder != nullptr);
211}
212ABpBinder::~ABpBinder() {}
213
Steven Moreland94968952018-09-05 14:42:59 -0700214void ABpBinder::onLastStrongRef(const void* id) {
215 {
216 std::lock_guard<std::mutex> lock(ABpBinderTag::gLock);
217 // Since ABpBinder is OBJECT_LIFETIME_WEAK, we must remove this weak reference in order for
218 // the ABpBinder to be deleted. Since a strong reference to this ABpBinder object should no
219 // longer be able to exist at the time of this method call, there is no longer a need to
220 // recover it.
221
222 ABpBinderTag::Value* value =
223 static_cast<ABpBinderTag::Value*>(remote()->findObject(ABpBinderTag::kId));
224 if (value != nullptr) {
225 value->binder = nullptr;
226 }
227 }
228
229 BpRefBase::onLastStrongRef(id);
230}
231
232sp<AIBinder> ABpBinder::lookupOrCreateFromBinder(const ::android::sp<::android::IBinder>& binder) {
Steven Moreland71cddc32018-08-30 23:39:22 -0700233 if (binder == nullptr) {
234 return nullptr;
235 }
236 if (ABBinderTag::has(binder)) {
237 return static_cast<ABBinder*>(binder.get());
238 }
Steven Moreland94968952018-09-05 14:42:59 -0700239
240 // The following code ensures that for a given binder object (remote or local), if it is not an
241 // ABBinder then at most one ABpBinder object exists in a given process representing it.
242 std::lock_guard<std::mutex> lock(ABpBinderTag::gLock);
243
244 ABpBinderTag::Value* value =
245 static_cast<ABpBinderTag::Value*>(binder->findObject(ABpBinderTag::kId));
246 if (value == nullptr) {
247 value = new ABpBinderTag::Value;
248 binder->attachObject(ABpBinderTag::kId, static_cast<void*>(value), nullptr /*cookie*/,
249 ABpBinderTag::clean);
250 }
251
252 sp<ABpBinder> ret = value->binder.promote();
253 if (ret == nullptr) {
254 ret = new ABpBinder(binder);
255 value->binder = ret;
256 }
257
258 return ret;
Steven Moreland71cddc32018-08-30 23:39:22 -0700259}
260
Steven Moreland2e87adc2018-08-20 19:47:00 -0700261struct AIBinder_Weak {
262 wp<AIBinder> binder;
263};
264AIBinder_Weak* AIBinder_Weak_new(AIBinder* binder) {
Steven Moreland5ccb70f2018-09-04 16:30:21 -0700265 if (binder == nullptr) {
266 return nullptr;
267 }
268
Steven Moreland2e87adc2018-08-20 19:47:00 -0700269 return new AIBinder_Weak{wp<AIBinder>(binder)};
270}
Steven Moreland9b80e282018-09-19 13:57:23 -0700271void AIBinder_Weak_delete(AIBinder_Weak* weakBinder) {
272 delete weakBinder;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700273}
274AIBinder* AIBinder_Weak_promote(AIBinder_Weak* weakBinder) {
Steven Moreland5ccb70f2018-09-04 16:30:21 -0700275 if (weakBinder == nullptr) {
276 return nullptr;
277 }
278
Steven Moreland2e87adc2018-08-20 19:47:00 -0700279 sp<AIBinder> binder = weakBinder->binder.promote();
280 AIBinder_incStrong(binder.get());
281 return binder.get();
282}
283
284AIBinder_Class::AIBinder_Class(const char* interfaceDescriptor, AIBinder_Class_onCreate onCreate,
285 AIBinder_Class_onDestroy onDestroy,
286 AIBinder_Class_onTransact onTransact)
Steven Moreland6cf66ac2018-11-02 18:14:54 -0700287 : onCreate(onCreate),
288 onDestroy(onDestroy),
289 onTransact(onTransact),
290 mInterfaceDescriptor(interfaceDescriptor) {}
Steven Moreland2e87adc2018-08-20 19:47:00 -0700291
292AIBinder_Class* AIBinder_Class_define(const char* interfaceDescriptor,
293 AIBinder_Class_onCreate onCreate,
294 AIBinder_Class_onDestroy onDestroy,
295 AIBinder_Class_onTransact onTransact) {
296 if (interfaceDescriptor == nullptr || onCreate == nullptr || onDestroy == nullptr ||
297 onTransact == nullptr) {
298 return nullptr;
299 }
300
301 return new AIBinder_Class(interfaceDescriptor, onCreate, onDestroy, onTransact);
302}
303
Steven Morelanda194c452019-03-04 16:47:07 -0800304void AIBinder_Class_setOnDump(AIBinder_Class* clazz, AIBinder_onDump onDump) {
305 CHECK(clazz != nullptr) << "setOnDump requires non-null clazz";
306
307 // this is required to be called before instances are instantiated
308 clazz->onDump = onDump;
309}
310
Ruchir Rastogicc7a7462020-01-31 14:29:15 -0800311void AIBinder_Class_setHandleShellCommand(AIBinder_Class* clazz,
312 AIBinder_handleShellCommand handleShellCommand) {
313 CHECK(clazz != nullptr) << "setHandleShellCommand requires non-null clazz";
314
315 clazz->handleShellCommand = handleShellCommand;
316}
317
Steven Moreland901c7452018-09-04 16:17:28 -0700318void AIBinder_DeathRecipient::TransferDeathRecipient::binderDied(const wp<IBinder>& who) {
319 CHECK(who == mWho);
320
321 mOnDied(mCookie);
Steven Moreland64127ca2019-03-13 09:25:44 -0700322
323 sp<AIBinder_DeathRecipient> recipient = mParentRecipient.promote();
324 sp<IBinder> strongWho = who.promote();
325
326 // otherwise this will be cleaned up later with pruneDeadTransferEntriesLocked
327 if (recipient != nullptr && strongWho != nullptr) {
328 status_t result = recipient->unlinkToDeath(strongWho, mCookie);
329 if (result != ::android::DEAD_OBJECT) {
330 LOG(WARNING) << "Unlinking to dead binder resulted in: " << result;
331 }
332 }
333
Steven Moreland901c7452018-09-04 16:17:28 -0700334 mWho = nullptr;
335}
336
337AIBinder_DeathRecipient::AIBinder_DeathRecipient(AIBinder_DeathRecipient_onBinderDied onDied)
Steven Moreland6cf66ac2018-11-02 18:14:54 -0700338 : mOnDied(onDied) {
Steven Moreland901c7452018-09-04 16:17:28 -0700339 CHECK(onDied != nullptr);
340}
341
Steven Moreland64127ca2019-03-13 09:25:44 -0700342void AIBinder_DeathRecipient::pruneDeadTransferEntriesLocked() {
343 mDeathRecipients.erase(std::remove_if(mDeathRecipients.begin(), mDeathRecipients.end(),
344 [](const sp<TransferDeathRecipient>& tdr) {
345 return tdr->getWho() == nullptr;
346 }),
347 mDeathRecipients.end());
348}
349
350binder_status_t AIBinder_DeathRecipient::linkToDeath(sp<IBinder> binder, void* cookie) {
Steven Moreland901c7452018-09-04 16:17:28 -0700351 CHECK(binder != nullptr);
352
353 std::lock_guard<std::mutex> l(mDeathRecipientsMutex);
354
355 sp<TransferDeathRecipient> recipient =
Steven Moreland64127ca2019-03-13 09:25:44 -0700356 new TransferDeathRecipient(binder, cookie, this, mOnDied);
Steven Moreland901c7452018-09-04 16:17:28 -0700357
Steven Moreland64127ca2019-03-13 09:25:44 -0700358 status_t status = binder->linkToDeath(recipient, cookie, 0 /*flags*/);
Steven Moreland5d62e442018-09-13 15:01:02 -0700359 if (status != STATUS_OK) {
360 return PruneStatusT(status);
Steven Moreland901c7452018-09-04 16:17:28 -0700361 }
362
363 mDeathRecipients.push_back(recipient);
Steven Moreland64127ca2019-03-13 09:25:44 -0700364
365 pruneDeadTransferEntriesLocked();
Steven Moreland5d62e442018-09-13 15:01:02 -0700366 return STATUS_OK;
Steven Moreland901c7452018-09-04 16:17:28 -0700367}
368
Steven Moreland64127ca2019-03-13 09:25:44 -0700369binder_status_t AIBinder_DeathRecipient::unlinkToDeath(sp<IBinder> binder, void* cookie) {
Steven Moreland901c7452018-09-04 16:17:28 -0700370 CHECK(binder != nullptr);
371
372 std::lock_guard<std::mutex> l(mDeathRecipientsMutex);
373
374 for (auto it = mDeathRecipients.rbegin(); it != mDeathRecipients.rend(); ++it) {
375 sp<TransferDeathRecipient> recipient = *it;
376
Steven Moreland64127ca2019-03-13 09:25:44 -0700377 if (recipient->getCookie() == cookie && recipient->getWho() == binder) {
Steven Moreland901c7452018-09-04 16:17:28 -0700378 mDeathRecipients.erase(it.base() - 1);
379
Steven Moreland64127ca2019-03-13 09:25:44 -0700380 status_t status = binder->unlinkToDeath(recipient, cookie, 0 /*flags*/);
Steven Moreland5d62e442018-09-13 15:01:02 -0700381 if (status != ::android::OK) {
Steven Moreland901c7452018-09-04 16:17:28 -0700382 LOG(ERROR) << __func__
383 << ": removed reference to death recipient but unlink failed.";
384 }
Steven Moreland5d62e442018-09-13 15:01:02 -0700385 return PruneStatusT(status);
Steven Moreland901c7452018-09-04 16:17:28 -0700386 }
387 }
388
Steven Moreland5d62e442018-09-13 15:01:02 -0700389 return STATUS_NAME_NOT_FOUND;
Steven Moreland901c7452018-09-04 16:17:28 -0700390}
391
392// start of C-API methods
393
Steven Moreland2e87adc2018-08-20 19:47:00 -0700394AIBinder* AIBinder_new(const AIBinder_Class* clazz, void* args) {
395 if (clazz == nullptr) {
396 LOG(ERROR) << __func__ << ": Must provide class to construct local binder.";
397 return nullptr;
398 }
399
400 void* userData = clazz->onCreate(args);
401
Steven Moreland71cddc32018-08-30 23:39:22 -0700402 sp<AIBinder> ret = new ABBinder(clazz, userData);
403 ABBinderTag::attach(ret->getBinder());
404
405 AIBinder_incStrong(ret.get());
406 return ret.get();
Steven Moreland2e87adc2018-08-20 19:47:00 -0700407}
408
Steven Moreland5ea54da2018-09-04 13:29:55 -0700409bool AIBinder_isRemote(const AIBinder* binder) {
Steven Moreland2e87adc2018-08-20 19:47:00 -0700410 if (binder == nullptr) {
Steven Moreland56f752a2018-11-05 17:23:37 -0800411 return false;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700412 }
413
414 return binder->isRemote();
415}
416
Steven Moreland65867d72018-09-04 14:22:26 -0700417bool AIBinder_isAlive(const AIBinder* binder) {
418 if (binder == nullptr) {
419 return false;
420 }
421
422 return const_cast<AIBinder*>(binder)->getBinder()->isBinderAlive();
423}
424
425binder_status_t AIBinder_ping(AIBinder* binder) {
426 if (binder == nullptr) {
Steven Moreland5d62e442018-09-13 15:01:02 -0700427 return STATUS_UNEXPECTED_NULL;
Steven Moreland65867d72018-09-04 14:22:26 -0700428 }
429
Steven Moreland5d62e442018-09-13 15:01:02 -0700430 return PruneStatusT(binder->getBinder()->pingBinder());
Steven Moreland65867d72018-09-04 14:22:26 -0700431}
432
Steven Morelanda194c452019-03-04 16:47:07 -0800433binder_status_t AIBinder_dump(AIBinder* binder, int fd, const char** args, uint32_t numArgs) {
434 if (binder == nullptr) {
435 return STATUS_UNEXPECTED_NULL;
436 }
437
438 ABBinder* bBinder = binder->asABBinder();
439 if (bBinder != nullptr) {
440 AIBinder_onDump onDump = binder->getClass()->onDump;
441 if (onDump == nullptr) {
442 return STATUS_OK;
443 }
444 return PruneStatusT(onDump(bBinder, fd, args, numArgs));
445 }
446
447 ::android::Vector<String16> utf16Args;
448 utf16Args.setCapacity(numArgs);
449 for (uint32_t i = 0; i < numArgs; i++) {
450 utf16Args.push(String16(String8(args[i])));
451 }
452
453 status_t status = binder->getBinder()->dump(fd, utf16Args);
454 return PruneStatusT(status);
455}
456
Steven Moreland901c7452018-09-04 16:17:28 -0700457binder_status_t AIBinder_linkToDeath(AIBinder* binder, AIBinder_DeathRecipient* recipient,
458 void* cookie) {
459 if (binder == nullptr || recipient == nullptr) {
460 LOG(ERROR) << __func__ << ": Must provide binder and recipient.";
Steven Moreland5d62e442018-09-13 15:01:02 -0700461 return STATUS_UNEXPECTED_NULL;
Steven Moreland901c7452018-09-04 16:17:28 -0700462 }
463
Steven Moreland5d62e442018-09-13 15:01:02 -0700464 // returns binder_status_t
Steven Moreland64127ca2019-03-13 09:25:44 -0700465 return recipient->linkToDeath(binder->getBinder(), cookie);
Steven Moreland901c7452018-09-04 16:17:28 -0700466}
467
468binder_status_t AIBinder_unlinkToDeath(AIBinder* binder, AIBinder_DeathRecipient* recipient,
469 void* cookie) {
470 if (binder == nullptr || recipient == nullptr) {
471 LOG(ERROR) << __func__ << ": Must provide binder and recipient.";
Steven Moreland5d62e442018-09-13 15:01:02 -0700472 return STATUS_UNEXPECTED_NULL;
Steven Moreland901c7452018-09-04 16:17:28 -0700473 }
474
Steven Moreland5d62e442018-09-13 15:01:02 -0700475 // returns binder_status_t
Steven Moreland64127ca2019-03-13 09:25:44 -0700476 return recipient->unlinkToDeath(binder->getBinder(), cookie);
Steven Moreland901c7452018-09-04 16:17:28 -0700477}
478
Steven Morelandf3034b02018-11-12 17:37:46 -0800479uid_t AIBinder_getCallingUid() {
480 return ::android::IPCThreadState::self()->getCallingUid();
481}
482
483pid_t AIBinder_getCallingPid() {
484 return ::android::IPCThreadState::self()->getCallingPid();
485}
486
Steven Moreland2e87adc2018-08-20 19:47:00 -0700487void AIBinder_incStrong(AIBinder* binder) {
488 if (binder == nullptr) {
Steven Moreland2e87adc2018-08-20 19:47:00 -0700489 return;
490 }
491
492 binder->incStrong(nullptr);
493}
494void AIBinder_decStrong(AIBinder* binder) {
495 if (binder == nullptr) {
496 LOG(ERROR) << __func__ << ": on null binder";
497 return;
498 }
499
500 binder->decStrong(nullptr);
501}
502int32_t AIBinder_debugGetRefCount(AIBinder* binder) {
503 if (binder == nullptr) {
504 LOG(ERROR) << __func__ << ": on null binder";
505 return -1;
506 }
507
508 return binder->getStrongCount();
509}
510
Steven Moreland71cddc32018-08-30 23:39:22 -0700511bool AIBinder_associateClass(AIBinder* binder, const AIBinder_Class* clazz) {
512 if (binder == nullptr) {
513 return false;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700514 }
515
Steven Moreland71cddc32018-08-30 23:39:22 -0700516 return binder->associateClass(clazz);
Steven Moreland2e87adc2018-08-20 19:47:00 -0700517}
518
519const AIBinder_Class* AIBinder_getClass(AIBinder* binder) {
520 if (binder == nullptr) {
521 return nullptr;
522 }
523
524 return binder->getClass();
525}
526
527void* AIBinder_getUserData(AIBinder* binder) {
528 if (binder == nullptr) {
529 return nullptr;
530 }
531
532 ABBinder* bBinder = binder->asABBinder();
533 if (bBinder == nullptr) {
534 return nullptr;
535 }
536
537 return bBinder->getUserData();
538}
539
540binder_status_t AIBinder_prepareTransaction(AIBinder* binder, AParcel** in) {
541 if (binder == nullptr || in == nullptr) {
542 LOG(ERROR) << __func__ << ": requires non-null parameters.";
Steven Moreland5d62e442018-09-13 15:01:02 -0700543 return STATUS_UNEXPECTED_NULL;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700544 }
545 const AIBinder_Class* clazz = binder->getClass();
546 if (clazz == nullptr) {
547 LOG(ERROR) << __func__
548 << ": Class must be defined for a remote binder transaction. See "
549 "AIBinder_associateClass.";
Steven Moreland5d62e442018-09-13 15:01:02 -0700550 return STATUS_INVALID_OPERATION;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700551 }
552
Steven Moreland3527c2e2018-09-05 17:07:14 -0700553 if (!binder->isRemote()) {
Steven Moreland74521c82018-09-07 14:50:40 -0700554 LOG(WARNING) << "A binder object at " << binder
555 << " is being transacted on, however, this object is in the same process as "
556 "its proxy. Transacting with this binder is expensive compared to just "
557 "calling the corresponding functionality in the same process.";
Steven Moreland3527c2e2018-09-05 17:07:14 -0700558 }
559
Steven Moreland2e87adc2018-08-20 19:47:00 -0700560 *in = new AParcel(binder);
Steven Morelandf18615b2018-09-14 11:43:06 -0700561 status_t status = (*in)->get()->writeInterfaceToken(clazz->getInterfaceDescriptor());
Steven Moreland5d62e442018-09-13 15:01:02 -0700562 binder_status_t ret = PruneStatusT(status);
563
564 if (ret != STATUS_OK) {
Steven Moreland2e87adc2018-08-20 19:47:00 -0700565 delete *in;
566 *in = nullptr;
567 }
568
Steven Moreland5d62e442018-09-13 15:01:02 -0700569 return ret;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700570}
571
Steven Moreland9b80e282018-09-19 13:57:23 -0700572static void DestroyParcel(AParcel** parcel) {
573 delete *parcel;
574 *parcel = nullptr;
575}
576
Steven Moreland2e87adc2018-08-20 19:47:00 -0700577binder_status_t AIBinder_transact(AIBinder* binder, transaction_code_t code, AParcel** in,
578 AParcel** out, binder_flags_t flags) {
579 if (in == nullptr) {
580 LOG(ERROR) << __func__ << ": requires non-null in parameter";
Steven Moreland5d62e442018-09-13 15:01:02 -0700581 return STATUS_UNEXPECTED_NULL;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700582 }
583
Steven Morelandcaa776c2018-09-04 13:48:11 -0700584 using AutoParcelDestroyer = std::unique_ptr<AParcel*, void (*)(AParcel**)>;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700585 // This object is the input to the transaction. This function takes ownership of it and deletes
586 // it.
Steven Moreland9b80e282018-09-19 13:57:23 -0700587 AutoParcelDestroyer forIn(in, DestroyParcel);
Steven Moreland2e87adc2018-08-20 19:47:00 -0700588
589 if (!isUserCommand(code)) {
590 LOG(ERROR) << __func__ << ": Only user-defined transactions can be made from the NDK.";
Steven Moreland5d62e442018-09-13 15:01:02 -0700591 return STATUS_UNKNOWN_TRANSACTION;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700592 }
593
Steven Moreland46b5fea2019-10-15 11:22:18 -0700594 constexpr binder_flags_t kAllFlags = FLAG_PRIVATE_VENDOR | FLAG_ONEWAY;
595 if ((flags & ~kAllFlags) != 0) {
Steven Moreland2e87adc2018-08-20 19:47:00 -0700596 LOG(ERROR) << __func__ << ": Unrecognized flags sent: " << flags;
Steven Moreland5d62e442018-09-13 15:01:02 -0700597 return STATUS_BAD_VALUE;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700598 }
599
600 if (binder == nullptr || *in == nullptr || out == nullptr) {
601 LOG(ERROR) << __func__ << ": requires non-null parameters.";
Steven Moreland5d62e442018-09-13 15:01:02 -0700602 return STATUS_UNEXPECTED_NULL;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700603 }
604
605 if ((*in)->getBinder() != binder) {
606 LOG(ERROR) << __func__ << ": parcel is associated with binder object " << binder
607 << " but called with " << (*in)->getBinder();
Steven Moreland5d62e442018-09-13 15:01:02 -0700608 return STATUS_BAD_VALUE;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700609 }
610
611 *out = new AParcel(binder);
612
Steven Morelandf18615b2018-09-14 11:43:06 -0700613 status_t status = binder->getBinder()->transact(code, *(*in)->get(), (*out)->get(), flags);
Steven Moreland5d62e442018-09-13 15:01:02 -0700614 binder_status_t ret = PruneStatusT(status);
Steven Moreland2e87adc2018-08-20 19:47:00 -0700615
Steven Moreland5d62e442018-09-13 15:01:02 -0700616 if (ret != STATUS_OK) {
Steven Moreland2e87adc2018-08-20 19:47:00 -0700617 delete *out;
618 *out = nullptr;
619 }
620
Steven Moreland5d62e442018-09-13 15:01:02 -0700621 return ret;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700622}
Steven Moreland901c7452018-09-04 16:17:28 -0700623
624AIBinder_DeathRecipient* AIBinder_DeathRecipient_new(
625 AIBinder_DeathRecipient_onBinderDied onBinderDied) {
626 if (onBinderDied == nullptr) {
627 LOG(ERROR) << __func__ << ": requires non-null onBinderDied parameter.";
628 return nullptr;
629 }
Steven Moreland64127ca2019-03-13 09:25:44 -0700630 auto ret = new AIBinder_DeathRecipient(onBinderDied);
631 ret->incStrong(nullptr);
632 return ret;
Steven Moreland901c7452018-09-04 16:17:28 -0700633}
634
Steven Moreland9b80e282018-09-19 13:57:23 -0700635void AIBinder_DeathRecipient_delete(AIBinder_DeathRecipient* recipient) {
Steven Moreland64127ca2019-03-13 09:25:44 -0700636 if (recipient == nullptr) {
637 return;
638 }
639
640 recipient->decStrong(nullptr);
Steven Moreland901c7452018-09-04 16:17:28 -0700641}
Steven Morelandea14ef22019-08-20 10:50:08 -0700642
643binder_status_t AIBinder_getExtension(AIBinder* binder, AIBinder** outExt) {
644 if (binder == nullptr || outExt == nullptr) {
645 if (outExt != nullptr) {
646 *outExt = nullptr;
647 }
648 return STATUS_UNEXPECTED_NULL;
649 }
650
651 sp<IBinder> ext;
652 status_t res = binder->getBinder()->getExtension(&ext);
653
654 if (res != android::OK) {
655 *outExt = nullptr;
656 return PruneStatusT(res);
657 }
658
659 sp<AIBinder> ret = ABpBinder::lookupOrCreateFromBinder(ext);
660 if (ret != nullptr) ret->incStrong(binder);
661
662 *outExt = ret.get();
663 return STATUS_OK;
664}
665
666binder_status_t AIBinder_setExtension(AIBinder* binder, AIBinder* ext) {
667 if (binder == nullptr || ext == nullptr) {
668 return STATUS_UNEXPECTED_NULL;
669 }
670
671 ABBinder* rawBinder = binder->asABBinder();
672 if (rawBinder == nullptr) {
673 return STATUS_INVALID_OPERATION;
674 }
675
676 rawBinder->setExtension(ext->getBinder());
677 return STATUS_OK;
678}