Steven Moreland | 2e87adc | 2018-08-20 19:47:00 -0700 | [diff] [blame] | 1 | /* |
| 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 Moreland | 4d5ad49 | 2018-09-13 12:49:16 -0700 | [diff] [blame] | 18 | #include "ibinder_internal.h" |
Steven Moreland | 2e87adc | 2018-08-20 19:47:00 -0700 | [diff] [blame] | 19 | |
| 20 | #include <android/binder_status.h> |
Steven Moreland | 4d5ad49 | 2018-09-13 12:49:16 -0700 | [diff] [blame] | 21 | #include "parcel_internal.h" |
Steven Moreland | 5d62e44 | 2018-09-13 15:01:02 -0700 | [diff] [blame] | 22 | #include "status_internal.h" |
Steven Moreland | 2e87adc | 2018-08-20 19:47:00 -0700 | [diff] [blame] | 23 | |
| 24 | #include <android-base/logging.h> |
Steven Moreland | f3034b0 | 2018-11-12 17:37:46 -0800 | [diff] [blame] | 25 | #include <binder/IPCThreadState.h> |
Steven Moreland | 2e87adc | 2018-08-20 19:47:00 -0700 | [diff] [blame] | 26 | |
Steven Moreland | 901c745 | 2018-09-04 16:17:28 -0700 | [diff] [blame] | 27 | using DeathRecipient = ::android::IBinder::DeathRecipient; |
| 28 | |
Steven Moreland | 2e87adc | 2018-08-20 19:47:00 -0700 | [diff] [blame] | 29 | using ::android::IBinder; |
| 30 | using ::android::Parcel; |
| 31 | using ::android::sp; |
Steven Moreland | 5d62e44 | 2018-09-13 15:01:02 -0700 | [diff] [blame] | 32 | using ::android::status_t; |
Steven Moreland | 2e87adc | 2018-08-20 19:47:00 -0700 | [diff] [blame] | 33 | using ::android::String16; |
Steven Moreland | a194c45 | 2019-03-04 16:47:07 -0800 | [diff] [blame^] | 34 | using ::android::String8; |
Steven Moreland | 2e87adc | 2018-08-20 19:47:00 -0700 | [diff] [blame] | 35 | using ::android::wp; |
| 36 | |
Steven Moreland | 71cddc3 | 2018-08-30 23:39:22 -0700 | [diff] [blame] | 37 | namespace ABBinderTag { |
| 38 | |
| 39 | static const void* kId = "ABBinder"; |
| 40 | static void* kValue = static_cast<void*>(new bool{true}); |
Steven Moreland | 9496895 | 2018-09-05 14:42:59 -0700 | [diff] [blame] | 41 | void clean(const void* /*id*/, void* /*obj*/, void* /*cookie*/){/* do nothing */}; |
Steven Moreland | 71cddc3 | 2018-08-30 23:39:22 -0700 | [diff] [blame] | 42 | |
| 43 | static void attach(const sp<IBinder>& binder) { |
Steven Moreland | 9496895 | 2018-09-05 14:42:59 -0700 | [diff] [blame] | 44 | binder->attachObject(kId, kValue, nullptr /*cookie*/, clean); |
Steven Moreland | 71cddc3 | 2018-08-30 23:39:22 -0700 | [diff] [blame] | 45 | } |
| 46 | static bool has(const sp<IBinder>& binder) { |
| 47 | return binder != nullptr && binder->findObject(kId) == kValue; |
| 48 | } |
| 49 | |
Steven Moreland | 6cf66ac | 2018-11-02 18:14:54 -0700 | [diff] [blame] | 50 | } // namespace ABBinderTag |
Steven Moreland | 71cddc3 | 2018-08-30 23:39:22 -0700 | [diff] [blame] | 51 | |
Steven Moreland | 9496895 | 2018-09-05 14:42:59 -0700 | [diff] [blame] | 52 | namespace ABpBinderTag { |
| 53 | |
| 54 | static std::mutex gLock; |
| 55 | static const void* kId = "ABpBinder"; |
| 56 | struct Value { |
| 57 | wp<ABpBinder> binder; |
| 58 | }; |
| 59 | void 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 Moreland | 6cf66ac | 2018-11-02 18:14:54 -0700 | [diff] [blame] | 65 | } // namespace ABpBinderTag |
Steven Moreland | 9496895 | 2018-09-05 14:42:59 -0700 | [diff] [blame] | 66 | |
Steven Moreland | 2e87adc | 2018-08-20 19:47:00 -0700 | [diff] [blame] | 67 | AIBinder::AIBinder(const AIBinder_Class* clazz) : mClazz(clazz) {} |
| 68 | AIBinder::~AIBinder() {} |
| 69 | |
Steven Moreland | 71cddc3 | 2018-08-30 23:39:22 -0700 | [diff] [blame] | 70 | bool AIBinder::associateClass(const AIBinder_Class* clazz) { |
Steven Moreland | 71cddc3 | 2018-08-30 23:39:22 -0700 | [diff] [blame] | 71 | if (clazz == nullptr) return false; |
| 72 | if (mClazz == clazz) return true; |
Steven Moreland | 2e87adc | 2018-08-20 19:47:00 -0700 | [diff] [blame] | 73 | |
| 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 Moreland | 71cddc3 | 2018-08-30 23:39:22 -0700 | [diff] [blame] | 82 | } 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 Moreland | 2e87adc | 2018-08-20 19:47:00 -0700 | [diff] [blame] | 88 | } |
| 89 | |
Steven Moreland | 71cddc3 | 2018-08-30 23:39:22 -0700 | [diff] [blame] | 90 | // always a failure because we know mClazz != clazz |
| 91 | return false; |
Steven Moreland | 2e87adc | 2018-08-20 19:47:00 -0700 | [diff] [blame] | 92 | } |
| 93 | |
Steven Moreland | 6cf66ac | 2018-11-02 18:14:54 -0700 | [diff] [blame] | 94 | CHECK(asABpBinder() != nullptr); // ABBinder always has a descriptor |
Steven Moreland | 71cddc3 | 2018-08-30 23:39:22 -0700 | [diff] [blame] | 95 | |
Steven Moreland | 2e87adc | 2018-08-20 19:47:00 -0700 | [diff] [blame] | 96 | 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 Moreland | 71cddc3 | 2018-08-30 23:39:22 -0700 | [diff] [blame] | 100 | return false; |
Steven Moreland | 2e87adc | 2018-08-20 19:47:00 -0700 | [diff] [blame] | 101 | } |
| 102 | |
Steven Moreland | 71cddc3 | 2018-08-30 23:39:22 -0700 | [diff] [blame] | 103 | // if this is a local object, it's not one known to libbinder_ndk |
Steven Moreland | 2e87adc | 2018-08-20 19:47:00 -0700 | [diff] [blame] | 104 | mClazz = clazz; |
| 105 | |
Steven Moreland | 71cddc3 | 2018-08-30 23:39:22 -0700 | [diff] [blame] | 106 | return true; |
Steven Moreland | 2e87adc | 2018-08-20 19:47:00 -0700 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | ABBinder::ABBinder(const AIBinder_Class* clazz, void* userData) |
Steven Moreland | 6cf66ac | 2018-11-02 18:14:54 -0700 | [diff] [blame] | 110 | : AIBinder(clazz), BBinder(), mUserData(userData) { |
Steven Moreland | 2e87adc | 2018-08-20 19:47:00 -0700 | [diff] [blame] | 111 | CHECK(clazz != nullptr); |
| 112 | } |
| 113 | ABBinder::~ABBinder() { |
| 114 | getClass()->onDestroy(mUserData); |
| 115 | } |
| 116 | |
| 117 | const String16& ABBinder::getInterfaceDescriptor() const { |
| 118 | return getClass()->getInterfaceDescriptor(); |
| 119 | } |
| 120 | |
Steven Moreland | a194c45 | 2019-03-04 16:47:07 -0800 | [diff] [blame^] | 121 | status_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 Moreland | 5d62e44 | 2018-09-13 15:01:02 -0700 | [diff] [blame] | 148 | status_t ABBinder::onTransact(transaction_code_t code, const Parcel& data, Parcel* reply, |
| 149 | binder_flags_t flags) { |
Steven Moreland | 2e87adc | 2018-08-20 19:47:00 -0700 | [diff] [blame] | 150 | if (isUserCommand(code)) { |
| 151 | if (!data.checkInterface(this)) { |
Steven Moreland | 9d089af | 2018-09-18 08:58:09 -0700 | [diff] [blame] | 152 | return STATUS_BAD_TYPE; |
Steven Moreland | 2e87adc | 2018-08-20 19:47:00 -0700 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | const AParcel in = AParcel::readOnly(this, &data); |
| 156 | AParcel out = AParcel(this, reply, false /*owns*/); |
| 157 | |
Steven Moreland | 5d62e44 | 2018-09-13 15:01:02 -0700 | [diff] [blame] | 158 | binder_status_t status = getClass()->onTransact(this, code, &in, &out); |
| 159 | return PruneStatusT(status); |
Steven Moreland | 2e87adc | 2018-08-20 19:47:00 -0700 | [diff] [blame] | 160 | } else { |
| 161 | return BBinder::onTransact(code, data, reply, flags); |
| 162 | } |
| 163 | } |
| 164 | |
Steven Moreland | 71cddc3 | 2018-08-30 23:39:22 -0700 | [diff] [blame] | 165 | ABpBinder::ABpBinder(const ::android::sp<::android::IBinder>& binder) |
Steven Moreland | 6cf66ac | 2018-11-02 18:14:54 -0700 | [diff] [blame] | 166 | : AIBinder(nullptr /*clazz*/), BpRefBase(binder) { |
Steven Moreland | 2e87adc | 2018-08-20 19:47:00 -0700 | [diff] [blame] | 167 | CHECK(binder != nullptr); |
| 168 | } |
| 169 | ABpBinder::~ABpBinder() {} |
| 170 | |
Steven Moreland | 9496895 | 2018-09-05 14:42:59 -0700 | [diff] [blame] | 171 | void 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 | |
| 189 | sp<AIBinder> ABpBinder::lookupOrCreateFromBinder(const ::android::sp<::android::IBinder>& binder) { |
Steven Moreland | 71cddc3 | 2018-08-30 23:39:22 -0700 | [diff] [blame] | 190 | if (binder == nullptr) { |
| 191 | return nullptr; |
| 192 | } |
| 193 | if (ABBinderTag::has(binder)) { |
| 194 | return static_cast<ABBinder*>(binder.get()); |
| 195 | } |
Steven Moreland | 9496895 | 2018-09-05 14:42:59 -0700 | [diff] [blame] | 196 | |
| 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 Moreland | 71cddc3 | 2018-08-30 23:39:22 -0700 | [diff] [blame] | 216 | } |
| 217 | |
Steven Moreland | 2e87adc | 2018-08-20 19:47:00 -0700 | [diff] [blame] | 218 | struct AIBinder_Weak { |
| 219 | wp<AIBinder> binder; |
| 220 | }; |
| 221 | AIBinder_Weak* AIBinder_Weak_new(AIBinder* binder) { |
Steven Moreland | 5ccb70f | 2018-09-04 16:30:21 -0700 | [diff] [blame] | 222 | if (binder == nullptr) { |
| 223 | return nullptr; |
| 224 | } |
| 225 | |
Steven Moreland | 2e87adc | 2018-08-20 19:47:00 -0700 | [diff] [blame] | 226 | return new AIBinder_Weak{wp<AIBinder>(binder)}; |
| 227 | } |
Steven Moreland | 9b80e28 | 2018-09-19 13:57:23 -0700 | [diff] [blame] | 228 | void AIBinder_Weak_delete(AIBinder_Weak* weakBinder) { |
| 229 | delete weakBinder; |
Steven Moreland | 2e87adc | 2018-08-20 19:47:00 -0700 | [diff] [blame] | 230 | } |
| 231 | AIBinder* AIBinder_Weak_promote(AIBinder_Weak* weakBinder) { |
Steven Moreland | 5ccb70f | 2018-09-04 16:30:21 -0700 | [diff] [blame] | 232 | if (weakBinder == nullptr) { |
| 233 | return nullptr; |
| 234 | } |
| 235 | |
Steven Moreland | 2e87adc | 2018-08-20 19:47:00 -0700 | [diff] [blame] | 236 | sp<AIBinder> binder = weakBinder->binder.promote(); |
| 237 | AIBinder_incStrong(binder.get()); |
| 238 | return binder.get(); |
| 239 | } |
| 240 | |
| 241 | AIBinder_Class::AIBinder_Class(const char* interfaceDescriptor, AIBinder_Class_onCreate onCreate, |
| 242 | AIBinder_Class_onDestroy onDestroy, |
| 243 | AIBinder_Class_onTransact onTransact) |
Steven Moreland | 6cf66ac | 2018-11-02 18:14:54 -0700 | [diff] [blame] | 244 | : onCreate(onCreate), |
| 245 | onDestroy(onDestroy), |
| 246 | onTransact(onTransact), |
| 247 | mInterfaceDescriptor(interfaceDescriptor) {} |
Steven Moreland | 2e87adc | 2018-08-20 19:47:00 -0700 | [diff] [blame] | 248 | |
| 249 | AIBinder_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 Moreland | a194c45 | 2019-03-04 16:47:07 -0800 | [diff] [blame^] | 261 | void 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 Moreland | 901c745 | 2018-09-04 16:17:28 -0700 | [diff] [blame] | 268 | void AIBinder_DeathRecipient::TransferDeathRecipient::binderDied(const wp<IBinder>& who) { |
| 269 | CHECK(who == mWho); |
| 270 | |
| 271 | mOnDied(mCookie); |
| 272 | mWho = nullptr; |
| 273 | } |
| 274 | |
| 275 | AIBinder_DeathRecipient::AIBinder_DeathRecipient(AIBinder_DeathRecipient_onBinderDied onDied) |
Steven Moreland | 6cf66ac | 2018-11-02 18:14:54 -0700 | [diff] [blame] | 276 | : mOnDied(onDied) { |
Steven Moreland | 901c745 | 2018-09-04 16:17:28 -0700 | [diff] [blame] | 277 | CHECK(onDied != nullptr); |
| 278 | } |
| 279 | |
| 280 | binder_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 Moreland | 5d62e44 | 2018-09-13 15:01:02 -0700 | [diff] [blame] | 288 | status_t status = binder->getBinder()->linkToDeath(recipient, cookie, 0 /*flags*/); |
| 289 | if (status != STATUS_OK) { |
| 290 | return PruneStatusT(status); |
Steven Moreland | 901c745 | 2018-09-04 16:17:28 -0700 | [diff] [blame] | 291 | } |
| 292 | |
| 293 | mDeathRecipients.push_back(recipient); |
Steven Moreland | 5d62e44 | 2018-09-13 15:01:02 -0700 | [diff] [blame] | 294 | return STATUS_OK; |
Steven Moreland | 901c745 | 2018-09-04 16:17:28 -0700 | [diff] [blame] | 295 | } |
| 296 | |
| 297 | binder_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 Moreland | 5d62e44 | 2018-09-13 15:01:02 -0700 | [diff] [blame] | 305 | if (recipient->getCookie() == cookie && recipient->getWho() == binder->getBinder()) { |
Steven Moreland | 901c745 | 2018-09-04 16:17:28 -0700 | [diff] [blame] | 306 | mDeathRecipients.erase(it.base() - 1); |
| 307 | |
Steven Moreland | 5d62e44 | 2018-09-13 15:01:02 -0700 | [diff] [blame] | 308 | status_t status = binder->getBinder()->unlinkToDeath(recipient, cookie, 0 /*flags*/); |
| 309 | if (status != ::android::OK) { |
Steven Moreland | 901c745 | 2018-09-04 16:17:28 -0700 | [diff] [blame] | 310 | LOG(ERROR) << __func__ |
| 311 | << ": removed reference to death recipient but unlink failed."; |
| 312 | } |
Steven Moreland | 5d62e44 | 2018-09-13 15:01:02 -0700 | [diff] [blame] | 313 | return PruneStatusT(status); |
Steven Moreland | 901c745 | 2018-09-04 16:17:28 -0700 | [diff] [blame] | 314 | } |
| 315 | } |
| 316 | |
Steven Moreland | 5d62e44 | 2018-09-13 15:01:02 -0700 | [diff] [blame] | 317 | return STATUS_NAME_NOT_FOUND; |
Steven Moreland | 901c745 | 2018-09-04 16:17:28 -0700 | [diff] [blame] | 318 | } |
| 319 | |
| 320 | // start of C-API methods |
| 321 | |
Steven Moreland | 2e87adc | 2018-08-20 19:47:00 -0700 | [diff] [blame] | 322 | AIBinder* 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 Moreland | 71cddc3 | 2018-08-30 23:39:22 -0700 | [diff] [blame] | 330 | sp<AIBinder> ret = new ABBinder(clazz, userData); |
| 331 | ABBinderTag::attach(ret->getBinder()); |
| 332 | |
| 333 | AIBinder_incStrong(ret.get()); |
| 334 | return ret.get(); |
Steven Moreland | 2e87adc | 2018-08-20 19:47:00 -0700 | [diff] [blame] | 335 | } |
| 336 | |
Steven Moreland | 5ea54da | 2018-09-04 13:29:55 -0700 | [diff] [blame] | 337 | bool AIBinder_isRemote(const AIBinder* binder) { |
Steven Moreland | 2e87adc | 2018-08-20 19:47:00 -0700 | [diff] [blame] | 338 | if (binder == nullptr) { |
Steven Moreland | 56f752a | 2018-11-05 17:23:37 -0800 | [diff] [blame] | 339 | return false; |
Steven Moreland | 2e87adc | 2018-08-20 19:47:00 -0700 | [diff] [blame] | 340 | } |
| 341 | |
| 342 | return binder->isRemote(); |
| 343 | } |
| 344 | |
Steven Moreland | 65867d7 | 2018-09-04 14:22:26 -0700 | [diff] [blame] | 345 | bool AIBinder_isAlive(const AIBinder* binder) { |
| 346 | if (binder == nullptr) { |
| 347 | return false; |
| 348 | } |
| 349 | |
| 350 | return const_cast<AIBinder*>(binder)->getBinder()->isBinderAlive(); |
| 351 | } |
| 352 | |
| 353 | binder_status_t AIBinder_ping(AIBinder* binder) { |
| 354 | if (binder == nullptr) { |
Steven Moreland | 5d62e44 | 2018-09-13 15:01:02 -0700 | [diff] [blame] | 355 | return STATUS_UNEXPECTED_NULL; |
Steven Moreland | 65867d7 | 2018-09-04 14:22:26 -0700 | [diff] [blame] | 356 | } |
| 357 | |
Steven Moreland | 5d62e44 | 2018-09-13 15:01:02 -0700 | [diff] [blame] | 358 | return PruneStatusT(binder->getBinder()->pingBinder()); |
Steven Moreland | 65867d7 | 2018-09-04 14:22:26 -0700 | [diff] [blame] | 359 | } |
| 360 | |
Steven Moreland | a194c45 | 2019-03-04 16:47:07 -0800 | [diff] [blame^] | 361 | binder_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 Moreland | 901c745 | 2018-09-04 16:17:28 -0700 | [diff] [blame] | 385 | binder_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 Moreland | 5d62e44 | 2018-09-13 15:01:02 -0700 | [diff] [blame] | 389 | return STATUS_UNEXPECTED_NULL; |
Steven Moreland | 901c745 | 2018-09-04 16:17:28 -0700 | [diff] [blame] | 390 | } |
| 391 | |
Steven Moreland | 5d62e44 | 2018-09-13 15:01:02 -0700 | [diff] [blame] | 392 | // returns binder_status_t |
Steven Moreland | 901c745 | 2018-09-04 16:17:28 -0700 | [diff] [blame] | 393 | return recipient->linkToDeath(binder, cookie); |
| 394 | } |
| 395 | |
| 396 | binder_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 Moreland | 5d62e44 | 2018-09-13 15:01:02 -0700 | [diff] [blame] | 400 | return STATUS_UNEXPECTED_NULL; |
Steven Moreland | 901c745 | 2018-09-04 16:17:28 -0700 | [diff] [blame] | 401 | } |
| 402 | |
Steven Moreland | 5d62e44 | 2018-09-13 15:01:02 -0700 | [diff] [blame] | 403 | // returns binder_status_t |
Steven Moreland | 901c745 | 2018-09-04 16:17:28 -0700 | [diff] [blame] | 404 | return recipient->unlinkToDeath(binder, cookie); |
| 405 | } |
| 406 | |
Steven Moreland | f3034b0 | 2018-11-12 17:37:46 -0800 | [diff] [blame] | 407 | uid_t AIBinder_getCallingUid() { |
| 408 | return ::android::IPCThreadState::self()->getCallingUid(); |
| 409 | } |
| 410 | |
| 411 | pid_t AIBinder_getCallingPid() { |
| 412 | return ::android::IPCThreadState::self()->getCallingPid(); |
| 413 | } |
| 414 | |
Steven Moreland | 2e87adc | 2018-08-20 19:47:00 -0700 | [diff] [blame] | 415 | void AIBinder_incStrong(AIBinder* binder) { |
| 416 | if (binder == nullptr) { |
| 417 | LOG(ERROR) << __func__ << ": on null binder"; |
| 418 | return; |
| 419 | } |
| 420 | |
| 421 | binder->incStrong(nullptr); |
| 422 | } |
| 423 | void AIBinder_decStrong(AIBinder* binder) { |
| 424 | if (binder == nullptr) { |
| 425 | LOG(ERROR) << __func__ << ": on null binder"; |
| 426 | return; |
| 427 | } |
| 428 | |
| 429 | binder->decStrong(nullptr); |
| 430 | } |
| 431 | int32_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 Moreland | 71cddc3 | 2018-08-30 23:39:22 -0700 | [diff] [blame] | 440 | bool AIBinder_associateClass(AIBinder* binder, const AIBinder_Class* clazz) { |
| 441 | if (binder == nullptr) { |
| 442 | return false; |
Steven Moreland | 2e87adc | 2018-08-20 19:47:00 -0700 | [diff] [blame] | 443 | } |
| 444 | |
Steven Moreland | 71cddc3 | 2018-08-30 23:39:22 -0700 | [diff] [blame] | 445 | return binder->associateClass(clazz); |
Steven Moreland | 2e87adc | 2018-08-20 19:47:00 -0700 | [diff] [blame] | 446 | } |
| 447 | |
| 448 | const AIBinder_Class* AIBinder_getClass(AIBinder* binder) { |
| 449 | if (binder == nullptr) { |
| 450 | return nullptr; |
| 451 | } |
| 452 | |
| 453 | return binder->getClass(); |
| 454 | } |
| 455 | |
| 456 | void* 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 | |
| 469 | binder_status_t AIBinder_prepareTransaction(AIBinder* binder, AParcel** in) { |
| 470 | if (binder == nullptr || in == nullptr) { |
| 471 | LOG(ERROR) << __func__ << ": requires non-null parameters."; |
Steven Moreland | 5d62e44 | 2018-09-13 15:01:02 -0700 | [diff] [blame] | 472 | return STATUS_UNEXPECTED_NULL; |
Steven Moreland | 2e87adc | 2018-08-20 19:47:00 -0700 | [diff] [blame] | 473 | } |
| 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 Moreland | 5d62e44 | 2018-09-13 15:01:02 -0700 | [diff] [blame] | 479 | return STATUS_INVALID_OPERATION; |
Steven Moreland | 2e87adc | 2018-08-20 19:47:00 -0700 | [diff] [blame] | 480 | } |
| 481 | |
Steven Moreland | 3527c2e | 2018-09-05 17:07:14 -0700 | [diff] [blame] | 482 | if (!binder->isRemote()) { |
Steven Moreland | 74521c8 | 2018-09-07 14:50:40 -0700 | [diff] [blame] | 483 | 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 Moreland | 3527c2e | 2018-09-05 17:07:14 -0700 | [diff] [blame] | 487 | } |
| 488 | |
Steven Moreland | 2e87adc | 2018-08-20 19:47:00 -0700 | [diff] [blame] | 489 | *in = new AParcel(binder); |
Steven Moreland | f18615b | 2018-09-14 11:43:06 -0700 | [diff] [blame] | 490 | status_t status = (*in)->get()->writeInterfaceToken(clazz->getInterfaceDescriptor()); |
Steven Moreland | 5d62e44 | 2018-09-13 15:01:02 -0700 | [diff] [blame] | 491 | binder_status_t ret = PruneStatusT(status); |
| 492 | |
| 493 | if (ret != STATUS_OK) { |
Steven Moreland | 2e87adc | 2018-08-20 19:47:00 -0700 | [diff] [blame] | 494 | delete *in; |
| 495 | *in = nullptr; |
| 496 | } |
| 497 | |
Steven Moreland | 5d62e44 | 2018-09-13 15:01:02 -0700 | [diff] [blame] | 498 | return ret; |
Steven Moreland | 2e87adc | 2018-08-20 19:47:00 -0700 | [diff] [blame] | 499 | } |
| 500 | |
Steven Moreland | 9b80e28 | 2018-09-19 13:57:23 -0700 | [diff] [blame] | 501 | static void DestroyParcel(AParcel** parcel) { |
| 502 | delete *parcel; |
| 503 | *parcel = nullptr; |
| 504 | } |
| 505 | |
Steven Moreland | 2e87adc | 2018-08-20 19:47:00 -0700 | [diff] [blame] | 506 | binder_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 Moreland | 5d62e44 | 2018-09-13 15:01:02 -0700 | [diff] [blame] | 510 | return STATUS_UNEXPECTED_NULL; |
Steven Moreland | 2e87adc | 2018-08-20 19:47:00 -0700 | [diff] [blame] | 511 | } |
| 512 | |
Steven Moreland | caa776c | 2018-09-04 13:48:11 -0700 | [diff] [blame] | 513 | using AutoParcelDestroyer = std::unique_ptr<AParcel*, void (*)(AParcel**)>; |
Steven Moreland | 2e87adc | 2018-08-20 19:47:00 -0700 | [diff] [blame] | 514 | // This object is the input to the transaction. This function takes ownership of it and deletes |
| 515 | // it. |
Steven Moreland | 9b80e28 | 2018-09-19 13:57:23 -0700 | [diff] [blame] | 516 | AutoParcelDestroyer forIn(in, DestroyParcel); |
Steven Moreland | 2e87adc | 2018-08-20 19:47:00 -0700 | [diff] [blame] | 517 | |
| 518 | if (!isUserCommand(code)) { |
| 519 | LOG(ERROR) << __func__ << ": Only user-defined transactions can be made from the NDK."; |
Steven Moreland | 5d62e44 | 2018-09-13 15:01:02 -0700 | [diff] [blame] | 520 | return STATUS_UNKNOWN_TRANSACTION; |
Steven Moreland | 2e87adc | 2018-08-20 19:47:00 -0700 | [diff] [blame] | 521 | } |
| 522 | |
| 523 | if ((flags & ~FLAG_ONEWAY) != 0) { |
| 524 | LOG(ERROR) << __func__ << ": Unrecognized flags sent: " << flags; |
Steven Moreland | 5d62e44 | 2018-09-13 15:01:02 -0700 | [diff] [blame] | 525 | return STATUS_BAD_VALUE; |
Steven Moreland | 2e87adc | 2018-08-20 19:47:00 -0700 | [diff] [blame] | 526 | } |
| 527 | |
| 528 | if (binder == nullptr || *in == nullptr || out == nullptr) { |
| 529 | LOG(ERROR) << __func__ << ": requires non-null parameters."; |
Steven Moreland | 5d62e44 | 2018-09-13 15:01:02 -0700 | [diff] [blame] | 530 | return STATUS_UNEXPECTED_NULL; |
Steven Moreland | 2e87adc | 2018-08-20 19:47:00 -0700 | [diff] [blame] | 531 | } |
| 532 | |
| 533 | if ((*in)->getBinder() != binder) { |
| 534 | LOG(ERROR) << __func__ << ": parcel is associated with binder object " << binder |
| 535 | << " but called with " << (*in)->getBinder(); |
Steven Moreland | 5d62e44 | 2018-09-13 15:01:02 -0700 | [diff] [blame] | 536 | return STATUS_BAD_VALUE; |
Steven Moreland | 2e87adc | 2018-08-20 19:47:00 -0700 | [diff] [blame] | 537 | } |
| 538 | |
| 539 | *out = new AParcel(binder); |
| 540 | |
Steven Moreland | f18615b | 2018-09-14 11:43:06 -0700 | [diff] [blame] | 541 | status_t status = binder->getBinder()->transact(code, *(*in)->get(), (*out)->get(), flags); |
Steven Moreland | 5d62e44 | 2018-09-13 15:01:02 -0700 | [diff] [blame] | 542 | binder_status_t ret = PruneStatusT(status); |
Steven Moreland | 2e87adc | 2018-08-20 19:47:00 -0700 | [diff] [blame] | 543 | |
Steven Moreland | 5d62e44 | 2018-09-13 15:01:02 -0700 | [diff] [blame] | 544 | if (ret != STATUS_OK) { |
Steven Moreland | 2e87adc | 2018-08-20 19:47:00 -0700 | [diff] [blame] | 545 | delete *out; |
| 546 | *out = nullptr; |
| 547 | } |
| 548 | |
Steven Moreland | 5d62e44 | 2018-09-13 15:01:02 -0700 | [diff] [blame] | 549 | return ret; |
Steven Moreland | 2e87adc | 2018-08-20 19:47:00 -0700 | [diff] [blame] | 550 | } |
Steven Moreland | 901c745 | 2018-09-04 16:17:28 -0700 | [diff] [blame] | 551 | |
| 552 | AIBinder_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 Moreland | 9b80e28 | 2018-09-19 13:57:23 -0700 | [diff] [blame] | 561 | void AIBinder_DeathRecipient_delete(AIBinder_DeathRecipient* recipient) { |
| 562 | delete recipient; |
Steven Moreland | 901c745 | 2018-09-04 16:17:28 -0700 | [diff] [blame] | 563 | } |