Binder.cpp revision 23eb1e20cf59e1238b65cc108f31dfbfd85b2337
1/* 2 * Copyright (C) 2005 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 <binder/Binder.h> 18 19#include <atomic> 20#include <utils/misc.h> 21#include <binder/BpBinder.h> 22#include <binder/IInterface.h> 23#include <binder/IResultReceiver.h> 24#include <binder/Parcel.h> 25 26#include <stdio.h> 27 28namespace android { 29 30// --------------------------------------------------------------------------- 31 32IBinder::IBinder() 33 : RefBase() 34{ 35} 36 37IBinder::~IBinder() 38{ 39} 40 41// --------------------------------------------------------------------------- 42 43sp<IInterface> IBinder::queryLocalInterface(const String16& /*descriptor*/) 44{ 45 return NULL; 46} 47 48BBinder* IBinder::localBinder() 49{ 50 return NULL; 51} 52 53BpBinder* IBinder::remoteBinder() 54{ 55 return NULL; 56} 57 58bool IBinder::checkSubclass(const void* /*subclassID*/) const 59{ 60 return false; 61} 62 63 64status_t IBinder::shellCommand(int /*in*/, int out, int /*err*/, Vector<String16>& /*args*/, 65 const sp<IResultReceiver>& resultReceiver) 66{ 67 if (out >= 0) { 68 dprintf(out, "Shell commands not supported.\n"); 69 } 70 if (resultReceiver != NULL) { 71 resultReceiver->send(INVALID_OPERATION); 72 } 73 return NO_ERROR; 74} 75 76// --------------------------------------------------------------------------- 77 78class BBinder::Extras 79{ 80public: 81 Mutex mLock; 82 BpBinder::ObjectManager mObjects; 83}; 84 85// --------------------------------------------------------------------------- 86 87BBinder::BBinder() : mExtras(nullptr) 88{ 89} 90 91bool BBinder::isBinderAlive() const 92{ 93 return true; 94} 95 96status_t BBinder::pingBinder() 97{ 98 return NO_ERROR; 99} 100 101const String16& BBinder::getInterfaceDescriptor() const 102{ 103 // This is a local static rather than a global static, 104 // to avoid static initializer ordering issues. 105 static String16 sEmptyDescriptor; 106 ALOGW("reached BBinder::getInterfaceDescriptor (this=%p)", this); 107 return sEmptyDescriptor; 108} 109 110status_t BBinder::transact( 111 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags) 112{ 113 data.setDataPosition(0); 114 115 status_t err = NO_ERROR; 116 switch (code) { 117 case PING_TRANSACTION: 118 reply->writeInt32(pingBinder()); 119 break; 120 default: 121 err = onTransact(code, data, reply, flags); 122 break; 123 } 124 125 if (reply != NULL) { 126 reply->setDataPosition(0); 127 } 128 129 return err; 130} 131 132status_t BBinder::linkToDeath( 133 const sp<DeathRecipient>& /*recipient*/, void* /*cookie*/, 134 uint32_t /*flags*/) 135{ 136 return INVALID_OPERATION; 137} 138 139status_t BBinder::unlinkToDeath( 140 const wp<DeathRecipient>& /*recipient*/, void* /*cookie*/, 141 uint32_t /*flags*/, wp<DeathRecipient>* /*outRecipient*/) 142{ 143 return INVALID_OPERATION; 144} 145 146status_t BBinder::dump(int /*fd*/, const Vector<String16>& /*args*/) 147{ 148 return NO_ERROR; 149} 150 151void BBinder::attachObject( 152 const void* objectID, void* object, void* cleanupCookie, 153 object_cleanup_func func) 154{ 155 Extras* e = mExtras.load(std::memory_order_acquire); 156 157 if (!e) { 158 e = new Extras; 159 Extras* expected = nullptr; 160 if (!mExtras.compare_exchange_strong(expected, e, 161 std::memory_order_release, 162 std::memory_order_acquire)) { 163 delete e; 164 e = expected; // Filled in by CAS 165 } 166 if (e == 0) return; // out of memory 167 } 168 169 AutoMutex _l(e->mLock); 170 e->mObjects.attach(objectID, object, cleanupCookie, func); 171} 172 173void* BBinder::findObject(const void* objectID) const 174{ 175 Extras* e = mExtras.load(std::memory_order_acquire); 176 if (!e) return NULL; 177 178 AutoMutex _l(e->mLock); 179 return e->mObjects.find(objectID); 180} 181 182void BBinder::detachObject(const void* objectID) 183{ 184 Extras* e = mExtras.load(std::memory_order_acquire); 185 if (!e) return; 186 187 AutoMutex _l(e->mLock); 188 e->mObjects.detach(objectID); 189} 190 191BBinder* BBinder::localBinder() 192{ 193 return this; 194} 195 196BBinder::~BBinder() 197{ 198 Extras* e = mExtras.load(std::memory_order_relaxed); 199 if (e) delete e; 200} 201 202 203status_t BBinder::onTransact( 204 uint32_t code, const Parcel& data, Parcel* reply, uint32_t /*flags*/) 205{ 206 switch (code) { 207 case INTERFACE_TRANSACTION: 208 reply->writeString16(getInterfaceDescriptor()); 209 return NO_ERROR; 210 211 case DUMP_TRANSACTION: { 212 int fd = data.readFileDescriptor(); 213 int argc = data.readInt32(); 214 Vector<String16> args; 215 for (int i = 0; i < argc && data.dataAvail() > 0; i++) { 216 args.add(data.readString16()); 217 } 218 return dump(fd, args); 219 } 220 221 case SHELL_COMMAND_TRANSACTION: { 222 int in = data.readFileDescriptor(); 223 int out = data.readFileDescriptor(); 224 int err = data.readFileDescriptor(); 225 int argc = data.readInt32(); 226 Vector<String16> args; 227 for (int i = 0; i < argc && data.dataAvail() > 0; i++) { 228 args.add(data.readString16()); 229 } 230 sp<IResultReceiver> resultReceiver = IResultReceiver::asInterface( 231 data.readStrongBinder()); 232 233 return shellCommand(in, out, err, args, resultReceiver); 234 } 235 236 case SYSPROPS_TRANSACTION: { 237 report_sysprop_change(); 238 return NO_ERROR; 239 } 240 241 default: 242 return UNKNOWN_TRANSACTION; 243 } 244} 245 246// --------------------------------------------------------------------------- 247 248enum { 249 // This is used to transfer ownership of the remote binder from 250 // the BpRefBase object holding it (when it is constructed), to the 251 // owner of the BpRefBase object when it first acquires that BpRefBase. 252 kRemoteAcquired = 0x00000001 253}; 254 255BpRefBase::BpRefBase(const sp<IBinder>& o) 256 : mRemote(o.get()), mRefs(NULL), mState(0) 257{ 258 extendObjectLifetime(OBJECT_LIFETIME_WEAK); 259 260 if (mRemote) { 261 mRemote->incStrong(this); // Removed on first IncStrong(). 262 mRefs = mRemote->createWeak(this); // Held for our entire lifetime. 263 } 264} 265 266BpRefBase::~BpRefBase() 267{ 268 if (mRemote) { 269 if (!(mState.load(std::memory_order_relaxed)&kRemoteAcquired)) { 270 mRemote->decStrong(this); 271 } 272 mRefs->decWeak(this); 273 } 274} 275 276void BpRefBase::onFirstRef() 277{ 278 mState.fetch_or(kRemoteAcquired, std::memory_order_relaxed); 279} 280 281void BpRefBase::onLastStrongRef(const void* /*id*/) 282{ 283 if (mRemote) { 284 mRemote->decStrong(this); 285 } 286} 287 288bool BpRefBase::onIncStrongAttempted(uint32_t /*flags*/, const void* /*id*/) 289{ 290 return mRemote ? mRefs->attemptIncStrong(this) : false; 291} 292 293// --------------------------------------------------------------------------- 294 295}; // namespace android 296