| 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; | 
|  | 34 | using ::android::wp; | 
|  | 35 |  | 
| Steven Moreland | 71cddc3 | 2018-08-30 23:39:22 -0700 | [diff] [blame] | 36 | namespace ABBinderTag { | 
|  | 37 |  | 
|  | 38 | static const void* kId = "ABBinder"; | 
|  | 39 | static void* kValue = static_cast<void*>(new bool{true}); | 
| Steven Moreland | 9496895 | 2018-09-05 14:42:59 -0700 | [diff] [blame] | 40 | void clean(const void* /*id*/, void* /*obj*/, void* /*cookie*/){/* do nothing */}; | 
| Steven Moreland | 71cddc3 | 2018-08-30 23:39:22 -0700 | [diff] [blame] | 41 |  | 
|  | 42 | static void attach(const sp<IBinder>& binder) { | 
| Steven Moreland | 9496895 | 2018-09-05 14:42:59 -0700 | [diff] [blame] | 43 | binder->attachObject(kId, kValue, nullptr /*cookie*/, clean); | 
| Steven Moreland | 71cddc3 | 2018-08-30 23:39:22 -0700 | [diff] [blame] | 44 | } | 
|  | 45 | static bool has(const sp<IBinder>& binder) { | 
|  | 46 | return binder != nullptr && binder->findObject(kId) == kValue; | 
|  | 47 | } | 
|  | 48 |  | 
| Steven Moreland | 6cf66ac | 2018-11-02 18:14:54 -0700 | [diff] [blame] | 49 | }  // namespace ABBinderTag | 
| Steven Moreland | 71cddc3 | 2018-08-30 23:39:22 -0700 | [diff] [blame] | 50 |  | 
| Steven Moreland | 9496895 | 2018-09-05 14:42:59 -0700 | [diff] [blame] | 51 | namespace ABpBinderTag { | 
|  | 52 |  | 
|  | 53 | static std::mutex gLock; | 
|  | 54 | static const void* kId = "ABpBinder"; | 
|  | 55 | struct Value { | 
|  | 56 | wp<ABpBinder> binder; | 
|  | 57 | }; | 
|  | 58 | void clean(const void* id, void* obj, void* cookie) { | 
|  | 59 | CHECK(id == kId) << id << " " << obj << " " << cookie; | 
|  | 60 |  | 
|  | 61 | delete static_cast<Value*>(obj); | 
|  | 62 | }; | 
|  | 63 |  | 
| Steven Moreland | 6cf66ac | 2018-11-02 18:14:54 -0700 | [diff] [blame] | 64 | }  // namespace ABpBinderTag | 
| Steven Moreland | 9496895 | 2018-09-05 14:42:59 -0700 | [diff] [blame] | 65 |  | 
| Steven Moreland | 2e87adc | 2018-08-20 19:47:00 -0700 | [diff] [blame] | 66 | AIBinder::AIBinder(const AIBinder_Class* clazz) : mClazz(clazz) {} | 
|  | 67 | AIBinder::~AIBinder() {} | 
|  | 68 |  | 
| Steven Moreland | 71cddc3 | 2018-08-30 23:39:22 -0700 | [diff] [blame] | 69 | bool AIBinder::associateClass(const AIBinder_Class* clazz) { | 
| Steven Moreland | 2e87adc | 2018-08-20 19:47:00 -0700 | [diff] [blame] | 70 | using ::android::String8; | 
|  | 71 |  | 
| Steven Moreland | 71cddc3 | 2018-08-30 23:39:22 -0700 | [diff] [blame] | 72 | if (clazz == nullptr) return false; | 
|  | 73 | if (mClazz == clazz) return true; | 
| Steven Moreland | 2e87adc | 2018-08-20 19:47:00 -0700 | [diff] [blame] | 74 |  | 
|  | 75 | String8 newDescriptor(clazz->getInterfaceDescriptor()); | 
|  | 76 |  | 
|  | 77 | if (mClazz != nullptr) { | 
|  | 78 | String8 currentDescriptor(mClazz->getInterfaceDescriptor()); | 
|  | 79 | if (newDescriptor == currentDescriptor) { | 
|  | 80 | LOG(ERROR) << __func__ << ": Class descriptors '" << currentDescriptor | 
|  | 81 | << "' match during associateClass, but they are different class objects. " | 
|  | 82 | "Class descriptor collision?"; | 
| Steven Moreland | 71cddc3 | 2018-08-30 23:39:22 -0700 | [diff] [blame] | 83 | } else { | 
|  | 84 | LOG(ERROR) << __func__ | 
|  | 85 | << ": Class cannot be associated on object which already has a class. " | 
|  | 86 | "Trying to associate to '" | 
|  | 87 | << newDescriptor.c_str() << "' but already set to '" | 
|  | 88 | << currentDescriptor.c_str() << "'."; | 
| Steven Moreland | 2e87adc | 2018-08-20 19:47:00 -0700 | [diff] [blame] | 89 | } | 
|  | 90 |  | 
| Steven Moreland | 71cddc3 | 2018-08-30 23:39:22 -0700 | [diff] [blame] | 91 | // always a failure because we know mClazz != clazz | 
|  | 92 | return false; | 
| Steven Moreland | 2e87adc | 2018-08-20 19:47:00 -0700 | [diff] [blame] | 93 | } | 
|  | 94 |  | 
| Steven Moreland | 6cf66ac | 2018-11-02 18:14:54 -0700 | [diff] [blame] | 95 | CHECK(asABpBinder() != nullptr);  // ABBinder always has a descriptor | 
| Steven Moreland | 71cddc3 | 2018-08-30 23:39:22 -0700 | [diff] [blame] | 96 |  | 
| Steven Moreland | 2e87adc | 2018-08-20 19:47:00 -0700 | [diff] [blame] | 97 | String8 descriptor(getBinder()->getInterfaceDescriptor()); | 
|  | 98 | if (descriptor != newDescriptor) { | 
|  | 99 | LOG(ERROR) << __func__ << ": Expecting binder to have class '" << newDescriptor.c_str() | 
|  | 100 | << "' but descriptor is actually '" << descriptor.c_str() << "'."; | 
| Steven Moreland | 71cddc3 | 2018-08-30 23:39:22 -0700 | [diff] [blame] | 101 | return false; | 
| Steven Moreland | 2e87adc | 2018-08-20 19:47:00 -0700 | [diff] [blame] | 102 | } | 
|  | 103 |  | 
| Steven Moreland | 71cddc3 | 2018-08-30 23:39:22 -0700 | [diff] [blame] | 104 | // 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] | 105 | mClazz = clazz; | 
|  | 106 |  | 
| Steven Moreland | 71cddc3 | 2018-08-30 23:39:22 -0700 | [diff] [blame] | 107 | return true; | 
| Steven Moreland | 2e87adc | 2018-08-20 19:47:00 -0700 | [diff] [blame] | 108 | } | 
|  | 109 |  | 
|  | 110 | ABBinder::ABBinder(const AIBinder_Class* clazz, void* userData) | 
| Steven Moreland | 6cf66ac | 2018-11-02 18:14:54 -0700 | [diff] [blame] | 111 | : AIBinder(clazz), BBinder(), mUserData(userData) { | 
| Steven Moreland | 2e87adc | 2018-08-20 19:47:00 -0700 | [diff] [blame] | 112 | CHECK(clazz != nullptr); | 
|  | 113 | } | 
|  | 114 | ABBinder::~ABBinder() { | 
|  | 115 | getClass()->onDestroy(mUserData); | 
|  | 116 | } | 
|  | 117 |  | 
|  | 118 | const String16& ABBinder::getInterfaceDescriptor() const { | 
|  | 119 | return getClass()->getInterfaceDescriptor(); | 
|  | 120 | } | 
|  | 121 |  | 
| Steven Moreland | 5d62e44 | 2018-09-13 15:01:02 -0700 | [diff] [blame] | 122 | status_t ABBinder::onTransact(transaction_code_t code, const Parcel& data, Parcel* reply, | 
|  | 123 | binder_flags_t flags) { | 
| Steven Moreland | 2e87adc | 2018-08-20 19:47:00 -0700 | [diff] [blame] | 124 | if (isUserCommand(code)) { | 
|  | 125 | if (!data.checkInterface(this)) { | 
| Steven Moreland | 9d089af | 2018-09-18 08:58:09 -0700 | [diff] [blame] | 126 | return STATUS_BAD_TYPE; | 
| Steven Moreland | 2e87adc | 2018-08-20 19:47:00 -0700 | [diff] [blame] | 127 | } | 
|  | 128 |  | 
|  | 129 | const AParcel in = AParcel::readOnly(this, &data); | 
|  | 130 | AParcel out = AParcel(this, reply, false /*owns*/); | 
|  | 131 |  | 
| Steven Moreland | 5d62e44 | 2018-09-13 15:01:02 -0700 | [diff] [blame] | 132 | binder_status_t status = getClass()->onTransact(this, code, &in, &out); | 
|  | 133 | return PruneStatusT(status); | 
| Steven Moreland | 2e87adc | 2018-08-20 19:47:00 -0700 | [diff] [blame] | 134 | } else { | 
|  | 135 | return BBinder::onTransact(code, data, reply, flags); | 
|  | 136 | } | 
|  | 137 | } | 
|  | 138 |  | 
| Steven Moreland | 71cddc3 | 2018-08-30 23:39:22 -0700 | [diff] [blame] | 139 | ABpBinder::ABpBinder(const ::android::sp<::android::IBinder>& binder) | 
| Steven Moreland | 6cf66ac | 2018-11-02 18:14:54 -0700 | [diff] [blame] | 140 | : AIBinder(nullptr /*clazz*/), BpRefBase(binder) { | 
| Steven Moreland | 2e87adc | 2018-08-20 19:47:00 -0700 | [diff] [blame] | 141 | CHECK(binder != nullptr); | 
|  | 142 | } | 
|  | 143 | ABpBinder::~ABpBinder() {} | 
|  | 144 |  | 
| Steven Moreland | 9496895 | 2018-09-05 14:42:59 -0700 | [diff] [blame] | 145 | void ABpBinder::onLastStrongRef(const void* id) { | 
|  | 146 | { | 
|  | 147 | std::lock_guard<std::mutex> lock(ABpBinderTag::gLock); | 
|  | 148 | // Since ABpBinder is OBJECT_LIFETIME_WEAK, we must remove this weak reference in order for | 
|  | 149 | // the ABpBinder to be deleted. Since a strong reference to this ABpBinder object should no | 
|  | 150 | // longer be able to exist at the time of this method call, there is no longer a need to | 
|  | 151 | // recover it. | 
|  | 152 |  | 
|  | 153 | ABpBinderTag::Value* value = | 
|  | 154 | static_cast<ABpBinderTag::Value*>(remote()->findObject(ABpBinderTag::kId)); | 
|  | 155 | if (value != nullptr) { | 
|  | 156 | value->binder = nullptr; | 
|  | 157 | } | 
|  | 158 | } | 
|  | 159 |  | 
|  | 160 | BpRefBase::onLastStrongRef(id); | 
|  | 161 | } | 
|  | 162 |  | 
|  | 163 | sp<AIBinder> ABpBinder::lookupOrCreateFromBinder(const ::android::sp<::android::IBinder>& binder) { | 
| Steven Moreland | 71cddc3 | 2018-08-30 23:39:22 -0700 | [diff] [blame] | 164 | if (binder == nullptr) { | 
|  | 165 | return nullptr; | 
|  | 166 | } | 
|  | 167 | if (ABBinderTag::has(binder)) { | 
|  | 168 | return static_cast<ABBinder*>(binder.get()); | 
|  | 169 | } | 
| Steven Moreland | 9496895 | 2018-09-05 14:42:59 -0700 | [diff] [blame] | 170 |  | 
|  | 171 | // The following code ensures that for a given binder object (remote or local), if it is not an | 
|  | 172 | // ABBinder then at most one ABpBinder object exists in a given process representing it. | 
|  | 173 | std::lock_guard<std::mutex> lock(ABpBinderTag::gLock); | 
|  | 174 |  | 
|  | 175 | ABpBinderTag::Value* value = | 
|  | 176 | static_cast<ABpBinderTag::Value*>(binder->findObject(ABpBinderTag::kId)); | 
|  | 177 | if (value == nullptr) { | 
|  | 178 | value = new ABpBinderTag::Value; | 
|  | 179 | binder->attachObject(ABpBinderTag::kId, static_cast<void*>(value), nullptr /*cookie*/, | 
|  | 180 | ABpBinderTag::clean); | 
|  | 181 | } | 
|  | 182 |  | 
|  | 183 | sp<ABpBinder> ret = value->binder.promote(); | 
|  | 184 | if (ret == nullptr) { | 
|  | 185 | ret = new ABpBinder(binder); | 
|  | 186 | value->binder = ret; | 
|  | 187 | } | 
|  | 188 |  | 
|  | 189 | return ret; | 
| Steven Moreland | 71cddc3 | 2018-08-30 23:39:22 -0700 | [diff] [blame] | 190 | } | 
|  | 191 |  | 
| Steven Moreland | 2e87adc | 2018-08-20 19:47:00 -0700 | [diff] [blame] | 192 | struct AIBinder_Weak { | 
|  | 193 | wp<AIBinder> binder; | 
|  | 194 | }; | 
|  | 195 | AIBinder_Weak* AIBinder_Weak_new(AIBinder* binder) { | 
| Steven Moreland | 5ccb70f | 2018-09-04 16:30:21 -0700 | [diff] [blame] | 196 | if (binder == nullptr) { | 
|  | 197 | return nullptr; | 
|  | 198 | } | 
|  | 199 |  | 
| Steven Moreland | 2e87adc | 2018-08-20 19:47:00 -0700 | [diff] [blame] | 200 | return new AIBinder_Weak{wp<AIBinder>(binder)}; | 
|  | 201 | } | 
| Steven Moreland | 9b80e28 | 2018-09-19 13:57:23 -0700 | [diff] [blame] | 202 | void AIBinder_Weak_delete(AIBinder_Weak* weakBinder) { | 
|  | 203 | delete weakBinder; | 
| Steven Moreland | 2e87adc | 2018-08-20 19:47:00 -0700 | [diff] [blame] | 204 | } | 
|  | 205 | AIBinder* AIBinder_Weak_promote(AIBinder_Weak* weakBinder) { | 
| Steven Moreland | 5ccb70f | 2018-09-04 16:30:21 -0700 | [diff] [blame] | 206 | if (weakBinder == nullptr) { | 
|  | 207 | return nullptr; | 
|  | 208 | } | 
|  | 209 |  | 
| Steven Moreland | 2e87adc | 2018-08-20 19:47:00 -0700 | [diff] [blame] | 210 | sp<AIBinder> binder = weakBinder->binder.promote(); | 
|  | 211 | AIBinder_incStrong(binder.get()); | 
|  | 212 | return binder.get(); | 
|  | 213 | } | 
|  | 214 |  | 
|  | 215 | AIBinder_Class::AIBinder_Class(const char* interfaceDescriptor, AIBinder_Class_onCreate onCreate, | 
|  | 216 | AIBinder_Class_onDestroy onDestroy, | 
|  | 217 | AIBinder_Class_onTransact onTransact) | 
| Steven Moreland | 6cf66ac | 2018-11-02 18:14:54 -0700 | [diff] [blame] | 218 | : onCreate(onCreate), | 
|  | 219 | onDestroy(onDestroy), | 
|  | 220 | onTransact(onTransact), | 
|  | 221 | mInterfaceDescriptor(interfaceDescriptor) {} | 
| Steven Moreland | 2e87adc | 2018-08-20 19:47:00 -0700 | [diff] [blame] | 222 |  | 
|  | 223 | AIBinder_Class* AIBinder_Class_define(const char* interfaceDescriptor, | 
|  | 224 | AIBinder_Class_onCreate onCreate, | 
|  | 225 | AIBinder_Class_onDestroy onDestroy, | 
|  | 226 | AIBinder_Class_onTransact onTransact) { | 
|  | 227 | if (interfaceDescriptor == nullptr || onCreate == nullptr || onDestroy == nullptr || | 
|  | 228 | onTransact == nullptr) { | 
|  | 229 | return nullptr; | 
|  | 230 | } | 
|  | 231 |  | 
|  | 232 | return new AIBinder_Class(interfaceDescriptor, onCreate, onDestroy, onTransact); | 
|  | 233 | } | 
|  | 234 |  | 
| Steven Moreland | 901c745 | 2018-09-04 16:17:28 -0700 | [diff] [blame] | 235 | void AIBinder_DeathRecipient::TransferDeathRecipient::binderDied(const wp<IBinder>& who) { | 
|  | 236 | CHECK(who == mWho); | 
|  | 237 |  | 
|  | 238 | mOnDied(mCookie); | 
|  | 239 | mWho = nullptr; | 
|  | 240 | } | 
|  | 241 |  | 
|  | 242 | AIBinder_DeathRecipient::AIBinder_DeathRecipient(AIBinder_DeathRecipient_onBinderDied onDied) | 
| Steven Moreland | 6cf66ac | 2018-11-02 18:14:54 -0700 | [diff] [blame] | 243 | : mOnDied(onDied) { | 
| Steven Moreland | 901c745 | 2018-09-04 16:17:28 -0700 | [diff] [blame] | 244 | CHECK(onDied != nullptr); | 
|  | 245 | } | 
|  | 246 |  | 
|  | 247 | binder_status_t AIBinder_DeathRecipient::linkToDeath(AIBinder* binder, void* cookie) { | 
|  | 248 | CHECK(binder != nullptr); | 
|  | 249 |  | 
|  | 250 | std::lock_guard<std::mutex> l(mDeathRecipientsMutex); | 
|  | 251 |  | 
|  | 252 | sp<TransferDeathRecipient> recipient = | 
|  | 253 | new TransferDeathRecipient(binder->getBinder(), cookie, mOnDied); | 
|  | 254 |  | 
| Steven Moreland | 5d62e44 | 2018-09-13 15:01:02 -0700 | [diff] [blame] | 255 | status_t status = binder->getBinder()->linkToDeath(recipient, cookie, 0 /*flags*/); | 
|  | 256 | if (status != STATUS_OK) { | 
|  | 257 | return PruneStatusT(status); | 
| Steven Moreland | 901c745 | 2018-09-04 16:17:28 -0700 | [diff] [blame] | 258 | } | 
|  | 259 |  | 
|  | 260 | mDeathRecipients.push_back(recipient); | 
| Steven Moreland | 5d62e44 | 2018-09-13 15:01:02 -0700 | [diff] [blame] | 261 | return STATUS_OK; | 
| Steven Moreland | 901c745 | 2018-09-04 16:17:28 -0700 | [diff] [blame] | 262 | } | 
|  | 263 |  | 
|  | 264 | binder_status_t AIBinder_DeathRecipient::unlinkToDeath(AIBinder* binder, void* cookie) { | 
|  | 265 | CHECK(binder != nullptr); | 
|  | 266 |  | 
|  | 267 | std::lock_guard<std::mutex> l(mDeathRecipientsMutex); | 
|  | 268 |  | 
|  | 269 | for (auto it = mDeathRecipients.rbegin(); it != mDeathRecipients.rend(); ++it) { | 
|  | 270 | sp<TransferDeathRecipient> recipient = *it; | 
|  | 271 |  | 
| Steven Moreland | 5d62e44 | 2018-09-13 15:01:02 -0700 | [diff] [blame] | 272 | if (recipient->getCookie() == cookie && recipient->getWho() == binder->getBinder()) { | 
| Steven Moreland | 901c745 | 2018-09-04 16:17:28 -0700 | [diff] [blame] | 273 | mDeathRecipients.erase(it.base() - 1); | 
|  | 274 |  | 
| Steven Moreland | 5d62e44 | 2018-09-13 15:01:02 -0700 | [diff] [blame] | 275 | status_t status = binder->getBinder()->unlinkToDeath(recipient, cookie, 0 /*flags*/); | 
|  | 276 | if (status != ::android::OK) { | 
| Steven Moreland | 901c745 | 2018-09-04 16:17:28 -0700 | [diff] [blame] | 277 | LOG(ERROR) << __func__ | 
|  | 278 | << ": removed reference to death recipient but unlink failed."; | 
|  | 279 | } | 
| Steven Moreland | 5d62e44 | 2018-09-13 15:01:02 -0700 | [diff] [blame] | 280 | return PruneStatusT(status); | 
| Steven Moreland | 901c745 | 2018-09-04 16:17:28 -0700 | [diff] [blame] | 281 | } | 
|  | 282 | } | 
|  | 283 |  | 
| Steven Moreland | 5d62e44 | 2018-09-13 15:01:02 -0700 | [diff] [blame] | 284 | return STATUS_NAME_NOT_FOUND; | 
| Steven Moreland | 901c745 | 2018-09-04 16:17:28 -0700 | [diff] [blame] | 285 | } | 
|  | 286 |  | 
|  | 287 | // start of C-API methods | 
|  | 288 |  | 
| Steven Moreland | 2e87adc | 2018-08-20 19:47:00 -0700 | [diff] [blame] | 289 | AIBinder* AIBinder_new(const AIBinder_Class* clazz, void* args) { | 
|  | 290 | if (clazz == nullptr) { | 
|  | 291 | LOG(ERROR) << __func__ << ": Must provide class to construct local binder."; | 
|  | 292 | return nullptr; | 
|  | 293 | } | 
|  | 294 |  | 
|  | 295 | void* userData = clazz->onCreate(args); | 
|  | 296 |  | 
| Steven Moreland | 71cddc3 | 2018-08-30 23:39:22 -0700 | [diff] [blame] | 297 | sp<AIBinder> ret = new ABBinder(clazz, userData); | 
|  | 298 | ABBinderTag::attach(ret->getBinder()); | 
|  | 299 |  | 
|  | 300 | AIBinder_incStrong(ret.get()); | 
|  | 301 | return ret.get(); | 
| Steven Moreland | 2e87adc | 2018-08-20 19:47:00 -0700 | [diff] [blame] | 302 | } | 
|  | 303 |  | 
| Steven Moreland | 5ea54da | 2018-09-04 13:29:55 -0700 | [diff] [blame] | 304 | bool AIBinder_isRemote(const AIBinder* binder) { | 
| Steven Moreland | 2e87adc | 2018-08-20 19:47:00 -0700 | [diff] [blame] | 305 | if (binder == nullptr) { | 
| Steven Moreland | 56f752a | 2018-11-05 17:23:37 -0800 | [diff] [blame] | 306 | return false; | 
| Steven Moreland | 2e87adc | 2018-08-20 19:47:00 -0700 | [diff] [blame] | 307 | } | 
|  | 308 |  | 
|  | 309 | return binder->isRemote(); | 
|  | 310 | } | 
|  | 311 |  | 
| Steven Moreland | 65867d7 | 2018-09-04 14:22:26 -0700 | [diff] [blame] | 312 | bool AIBinder_isAlive(const AIBinder* binder) { | 
|  | 313 | if (binder == nullptr) { | 
|  | 314 | return false; | 
|  | 315 | } | 
|  | 316 |  | 
|  | 317 | return const_cast<AIBinder*>(binder)->getBinder()->isBinderAlive(); | 
|  | 318 | } | 
|  | 319 |  | 
|  | 320 | binder_status_t AIBinder_ping(AIBinder* binder) { | 
|  | 321 | if (binder == nullptr) { | 
| Steven Moreland | 5d62e44 | 2018-09-13 15:01:02 -0700 | [diff] [blame] | 322 | return STATUS_UNEXPECTED_NULL; | 
| Steven Moreland | 65867d7 | 2018-09-04 14:22:26 -0700 | [diff] [blame] | 323 | } | 
|  | 324 |  | 
| Steven Moreland | 5d62e44 | 2018-09-13 15:01:02 -0700 | [diff] [blame] | 325 | return PruneStatusT(binder->getBinder()->pingBinder()); | 
| Steven Moreland | 65867d7 | 2018-09-04 14:22:26 -0700 | [diff] [blame] | 326 | } | 
|  | 327 |  | 
| Steven Moreland | 901c745 | 2018-09-04 16:17:28 -0700 | [diff] [blame] | 328 | binder_status_t AIBinder_linkToDeath(AIBinder* binder, AIBinder_DeathRecipient* recipient, | 
|  | 329 | void* cookie) { | 
|  | 330 | if (binder == nullptr || recipient == nullptr) { | 
|  | 331 | LOG(ERROR) << __func__ << ": Must provide binder and recipient."; | 
| Steven Moreland | 5d62e44 | 2018-09-13 15:01:02 -0700 | [diff] [blame] | 332 | return STATUS_UNEXPECTED_NULL; | 
| Steven Moreland | 901c745 | 2018-09-04 16:17:28 -0700 | [diff] [blame] | 333 | } | 
|  | 334 |  | 
| Steven Moreland | 5d62e44 | 2018-09-13 15:01:02 -0700 | [diff] [blame] | 335 | // returns binder_status_t | 
| Steven Moreland | 901c745 | 2018-09-04 16:17:28 -0700 | [diff] [blame] | 336 | return recipient->linkToDeath(binder, cookie); | 
|  | 337 | } | 
|  | 338 |  | 
|  | 339 | binder_status_t AIBinder_unlinkToDeath(AIBinder* binder, AIBinder_DeathRecipient* recipient, | 
|  | 340 | void* cookie) { | 
|  | 341 | if (binder == nullptr || recipient == nullptr) { | 
|  | 342 | LOG(ERROR) << __func__ << ": Must provide binder and recipient."; | 
| Steven Moreland | 5d62e44 | 2018-09-13 15:01:02 -0700 | [diff] [blame] | 343 | return STATUS_UNEXPECTED_NULL; | 
| Steven Moreland | 901c745 | 2018-09-04 16:17:28 -0700 | [diff] [blame] | 344 | } | 
|  | 345 |  | 
| Steven Moreland | 5d62e44 | 2018-09-13 15:01:02 -0700 | [diff] [blame] | 346 | // returns binder_status_t | 
| Steven Moreland | 901c745 | 2018-09-04 16:17:28 -0700 | [diff] [blame] | 347 | return recipient->unlinkToDeath(binder, cookie); | 
|  | 348 | } | 
|  | 349 |  | 
| Steven Moreland | f3034b0 | 2018-11-12 17:37:46 -0800 | [diff] [blame] | 350 | uid_t AIBinder_getCallingUid() { | 
|  | 351 | return ::android::IPCThreadState::self()->getCallingUid(); | 
|  | 352 | } | 
|  | 353 |  | 
|  | 354 | pid_t AIBinder_getCallingPid() { | 
|  | 355 | return ::android::IPCThreadState::self()->getCallingPid(); | 
|  | 356 | } | 
|  | 357 |  | 
| Steven Moreland | 2e87adc | 2018-08-20 19:47:00 -0700 | [diff] [blame] | 358 | void AIBinder_incStrong(AIBinder* binder) { | 
|  | 359 | if (binder == nullptr) { | 
|  | 360 | LOG(ERROR) << __func__ << ": on null binder"; | 
|  | 361 | return; | 
|  | 362 | } | 
|  | 363 |  | 
|  | 364 | binder->incStrong(nullptr); | 
|  | 365 | } | 
|  | 366 | void AIBinder_decStrong(AIBinder* binder) { | 
|  | 367 | if (binder == nullptr) { | 
|  | 368 | LOG(ERROR) << __func__ << ": on null binder"; | 
|  | 369 | return; | 
|  | 370 | } | 
|  | 371 |  | 
|  | 372 | binder->decStrong(nullptr); | 
|  | 373 | } | 
|  | 374 | int32_t AIBinder_debugGetRefCount(AIBinder* binder) { | 
|  | 375 | if (binder == nullptr) { | 
|  | 376 | LOG(ERROR) << __func__ << ": on null binder"; | 
|  | 377 | return -1; | 
|  | 378 | } | 
|  | 379 |  | 
|  | 380 | return binder->getStrongCount(); | 
|  | 381 | } | 
|  | 382 |  | 
| Steven Moreland | 71cddc3 | 2018-08-30 23:39:22 -0700 | [diff] [blame] | 383 | bool AIBinder_associateClass(AIBinder* binder, const AIBinder_Class* clazz) { | 
|  | 384 | if (binder == nullptr) { | 
|  | 385 | return false; | 
| Steven Moreland | 2e87adc | 2018-08-20 19:47:00 -0700 | [diff] [blame] | 386 | } | 
|  | 387 |  | 
| Steven Moreland | 71cddc3 | 2018-08-30 23:39:22 -0700 | [diff] [blame] | 388 | return binder->associateClass(clazz); | 
| Steven Moreland | 2e87adc | 2018-08-20 19:47:00 -0700 | [diff] [blame] | 389 | } | 
|  | 390 |  | 
|  | 391 | const AIBinder_Class* AIBinder_getClass(AIBinder* binder) { | 
|  | 392 | if (binder == nullptr) { | 
|  | 393 | return nullptr; | 
|  | 394 | } | 
|  | 395 |  | 
|  | 396 | return binder->getClass(); | 
|  | 397 | } | 
|  | 398 |  | 
|  | 399 | void* AIBinder_getUserData(AIBinder* binder) { | 
|  | 400 | if (binder == nullptr) { | 
|  | 401 | return nullptr; | 
|  | 402 | } | 
|  | 403 |  | 
|  | 404 | ABBinder* bBinder = binder->asABBinder(); | 
|  | 405 | if (bBinder == nullptr) { | 
|  | 406 | return nullptr; | 
|  | 407 | } | 
|  | 408 |  | 
|  | 409 | return bBinder->getUserData(); | 
|  | 410 | } | 
|  | 411 |  | 
|  | 412 | binder_status_t AIBinder_prepareTransaction(AIBinder* binder, AParcel** in) { | 
|  | 413 | if (binder == nullptr || in == nullptr) { | 
|  | 414 | LOG(ERROR) << __func__ << ": requires non-null parameters."; | 
| Steven Moreland | 5d62e44 | 2018-09-13 15:01:02 -0700 | [diff] [blame] | 415 | return STATUS_UNEXPECTED_NULL; | 
| Steven Moreland | 2e87adc | 2018-08-20 19:47:00 -0700 | [diff] [blame] | 416 | } | 
|  | 417 | const AIBinder_Class* clazz = binder->getClass(); | 
|  | 418 | if (clazz == nullptr) { | 
|  | 419 | LOG(ERROR) << __func__ | 
|  | 420 | << ": Class must be defined for a remote binder transaction. See " | 
|  | 421 | "AIBinder_associateClass."; | 
| Steven Moreland | 5d62e44 | 2018-09-13 15:01:02 -0700 | [diff] [blame] | 422 | return STATUS_INVALID_OPERATION; | 
| Steven Moreland | 2e87adc | 2018-08-20 19:47:00 -0700 | [diff] [blame] | 423 | } | 
|  | 424 |  | 
| Steven Moreland | 3527c2e | 2018-09-05 17:07:14 -0700 | [diff] [blame] | 425 | if (!binder->isRemote()) { | 
| Steven Moreland | 74521c8 | 2018-09-07 14:50:40 -0700 | [diff] [blame] | 426 | LOG(WARNING) << "A binder object at " << binder | 
|  | 427 | << " is being transacted on, however, this object is in the same process as " | 
|  | 428 | "its proxy. Transacting with this binder is expensive compared to just " | 
|  | 429 | "calling the corresponding functionality in the same process."; | 
| Steven Moreland | 3527c2e | 2018-09-05 17:07:14 -0700 | [diff] [blame] | 430 | } | 
|  | 431 |  | 
| Steven Moreland | 2e87adc | 2018-08-20 19:47:00 -0700 | [diff] [blame] | 432 | *in = new AParcel(binder); | 
| Steven Moreland | f18615b | 2018-09-14 11:43:06 -0700 | [diff] [blame] | 433 | status_t status = (*in)->get()->writeInterfaceToken(clazz->getInterfaceDescriptor()); | 
| Steven Moreland | 5d62e44 | 2018-09-13 15:01:02 -0700 | [diff] [blame] | 434 | binder_status_t ret = PruneStatusT(status); | 
|  | 435 |  | 
|  | 436 | if (ret != STATUS_OK) { | 
| Steven Moreland | 2e87adc | 2018-08-20 19:47:00 -0700 | [diff] [blame] | 437 | delete *in; | 
|  | 438 | *in = nullptr; | 
|  | 439 | } | 
|  | 440 |  | 
| Steven Moreland | 5d62e44 | 2018-09-13 15:01:02 -0700 | [diff] [blame] | 441 | return ret; | 
| Steven Moreland | 2e87adc | 2018-08-20 19:47:00 -0700 | [diff] [blame] | 442 | } | 
|  | 443 |  | 
| Steven Moreland | 9b80e28 | 2018-09-19 13:57:23 -0700 | [diff] [blame] | 444 | static void DestroyParcel(AParcel** parcel) { | 
|  | 445 | delete *parcel; | 
|  | 446 | *parcel = nullptr; | 
|  | 447 | } | 
|  | 448 |  | 
| Steven Moreland | 2e87adc | 2018-08-20 19:47:00 -0700 | [diff] [blame] | 449 | binder_status_t AIBinder_transact(AIBinder* binder, transaction_code_t code, AParcel** in, | 
|  | 450 | AParcel** out, binder_flags_t flags) { | 
|  | 451 | if (in == nullptr) { | 
|  | 452 | LOG(ERROR) << __func__ << ": requires non-null in parameter"; | 
| Steven Moreland | 5d62e44 | 2018-09-13 15:01:02 -0700 | [diff] [blame] | 453 | return STATUS_UNEXPECTED_NULL; | 
| Steven Moreland | 2e87adc | 2018-08-20 19:47:00 -0700 | [diff] [blame] | 454 | } | 
|  | 455 |  | 
| Steven Moreland | caa776c | 2018-09-04 13:48:11 -0700 | [diff] [blame] | 456 | using AutoParcelDestroyer = std::unique_ptr<AParcel*, void (*)(AParcel**)>; | 
| Steven Moreland | 2e87adc | 2018-08-20 19:47:00 -0700 | [diff] [blame] | 457 | // This object is the input to the transaction. This function takes ownership of it and deletes | 
|  | 458 | // it. | 
| Steven Moreland | 9b80e28 | 2018-09-19 13:57:23 -0700 | [diff] [blame] | 459 | AutoParcelDestroyer forIn(in, DestroyParcel); | 
| Steven Moreland | 2e87adc | 2018-08-20 19:47:00 -0700 | [diff] [blame] | 460 |  | 
|  | 461 | if (!isUserCommand(code)) { | 
|  | 462 | 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] | 463 | return STATUS_UNKNOWN_TRANSACTION; | 
| Steven Moreland | 2e87adc | 2018-08-20 19:47:00 -0700 | [diff] [blame] | 464 | } | 
|  | 465 |  | 
|  | 466 | if ((flags & ~FLAG_ONEWAY) != 0) { | 
|  | 467 | LOG(ERROR) << __func__ << ": Unrecognized flags sent: " << flags; | 
| Steven Moreland | 5d62e44 | 2018-09-13 15:01:02 -0700 | [diff] [blame] | 468 | return STATUS_BAD_VALUE; | 
| Steven Moreland | 2e87adc | 2018-08-20 19:47:00 -0700 | [diff] [blame] | 469 | } | 
|  | 470 |  | 
|  | 471 | if (binder == nullptr || *in == nullptr || out == nullptr) { | 
|  | 472 | LOG(ERROR) << __func__ << ": requires non-null parameters."; | 
| Steven Moreland | 5d62e44 | 2018-09-13 15:01:02 -0700 | [diff] [blame] | 473 | return STATUS_UNEXPECTED_NULL; | 
| Steven Moreland | 2e87adc | 2018-08-20 19:47:00 -0700 | [diff] [blame] | 474 | } | 
|  | 475 |  | 
|  | 476 | if ((*in)->getBinder() != binder) { | 
|  | 477 | LOG(ERROR) << __func__ << ": parcel is associated with binder object " << binder | 
|  | 478 | << " but called with " << (*in)->getBinder(); | 
| Steven Moreland | 5d62e44 | 2018-09-13 15:01:02 -0700 | [diff] [blame] | 479 | return STATUS_BAD_VALUE; | 
| Steven Moreland | 2e87adc | 2018-08-20 19:47:00 -0700 | [diff] [blame] | 480 | } | 
|  | 481 |  | 
|  | 482 | *out = new AParcel(binder); | 
|  | 483 |  | 
| Steven Moreland | f18615b | 2018-09-14 11:43:06 -0700 | [diff] [blame] | 484 | status_t status = binder->getBinder()->transact(code, *(*in)->get(), (*out)->get(), flags); | 
| Steven Moreland | 5d62e44 | 2018-09-13 15:01:02 -0700 | [diff] [blame] | 485 | binder_status_t ret = PruneStatusT(status); | 
| Steven Moreland | 2e87adc | 2018-08-20 19:47:00 -0700 | [diff] [blame] | 486 |  | 
| Steven Moreland | 5d62e44 | 2018-09-13 15:01:02 -0700 | [diff] [blame] | 487 | if (ret != STATUS_OK) { | 
| Steven Moreland | 2e87adc | 2018-08-20 19:47:00 -0700 | [diff] [blame] | 488 | delete *out; | 
|  | 489 | *out = nullptr; | 
|  | 490 | } | 
|  | 491 |  | 
| Steven Moreland | 5d62e44 | 2018-09-13 15:01:02 -0700 | [diff] [blame] | 492 | return ret; | 
| Steven Moreland | 2e87adc | 2018-08-20 19:47:00 -0700 | [diff] [blame] | 493 | } | 
| Steven Moreland | 901c745 | 2018-09-04 16:17:28 -0700 | [diff] [blame] | 494 |  | 
|  | 495 | AIBinder_DeathRecipient* AIBinder_DeathRecipient_new( | 
|  | 496 | AIBinder_DeathRecipient_onBinderDied onBinderDied) { | 
|  | 497 | if (onBinderDied == nullptr) { | 
|  | 498 | LOG(ERROR) << __func__ << ": requires non-null onBinderDied parameter."; | 
|  | 499 | return nullptr; | 
|  | 500 | } | 
|  | 501 | return new AIBinder_DeathRecipient(onBinderDied); | 
|  | 502 | } | 
|  | 503 |  | 
| Steven Moreland | 9b80e28 | 2018-09-19 13:57:23 -0700 | [diff] [blame] | 504 | void AIBinder_DeathRecipient_delete(AIBinder_DeathRecipient* recipient) { | 
|  | 505 | delete recipient; | 
| Steven Moreland | 901c745 | 2018-09-04 16:17:28 -0700 | [diff] [blame] | 506 | } |