2 * Copyright (C) 2005 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #include <binder/Binder.h>
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>
30 // ---------------------------------------------------------------------------
41 // ---------------------------------------------------------------------------
43 sp<IInterface> IBinder::queryLocalInterface(const String16& /*descriptor*/)
48 BBinder* IBinder::localBinder()
53 BpBinder* IBinder::remoteBinder()
58 bool IBinder::checkSubclass(const void* /*subclassID*/) const
64 status_t IBinder::shellCommand(const sp<IBinder>& target, int in, int out, int err,
65 Vector<String16>& args, const sp<IResultReceiver>& resultReceiver)
69 send.writeFileDescriptor(in);
70 send.writeFileDescriptor(out);
71 send.writeFileDescriptor(err);
72 const size_t numArgs = args.size();
73 send.writeInt32(numArgs);
74 for (size_t i = 0; i < numArgs; i++) {
75 send.writeString16(args[i]);
77 send.writeStrongBinder(resultReceiver != NULL ? IInterface::asBinder(resultReceiver) : NULL);
78 return target->transact(SHELL_COMMAND_TRANSACTION, send, &reply);
81 // ---------------------------------------------------------------------------
87 BpBinder::ObjectManager mObjects;
90 // ---------------------------------------------------------------------------
92 BBinder::BBinder() : mExtras(nullptr)
96 bool BBinder::isBinderAlive() const
101 status_t BBinder::pingBinder()
106 const String16& BBinder::getInterfaceDescriptor() const
108 // This is a local static rather than a global static,
109 // to avoid static initializer ordering issues.
110 static String16 sEmptyDescriptor;
111 ALOGW("reached BBinder::getInterfaceDescriptor (this=%p)", this);
112 return sEmptyDescriptor;
115 status_t BBinder::transact(
116 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
118 data.setDataPosition(0);
120 status_t err = NO_ERROR;
122 case PING_TRANSACTION:
123 reply->writeInt32(pingBinder());
126 err = onTransact(code, data, reply, flags);
131 reply->setDataPosition(0);
137 status_t BBinder::linkToDeath(
138 const sp<DeathRecipient>& /*recipient*/, void* /*cookie*/,
141 return INVALID_OPERATION;
144 status_t BBinder::unlinkToDeath(
145 const wp<DeathRecipient>& /*recipient*/, void* /*cookie*/,
146 uint32_t /*flags*/, wp<DeathRecipient>* /*outRecipient*/)
148 return INVALID_OPERATION;
151 status_t BBinder::dump(int /*fd*/, const Vector<String16>& /*args*/)
156 void BBinder::attachObject(
157 const void* objectID, void* object, void* cleanupCookie,
158 object_cleanup_func func)
160 Extras* e = mExtras.load(std::memory_order_acquire);
164 Extras* expected = nullptr;
165 if (!mExtras.compare_exchange_strong(expected, e,
166 std::memory_order_release,
167 std::memory_order_acquire)) {
169 e = expected; // Filled in by CAS
171 if (e == 0) return; // out of memory
174 AutoMutex _l(e->mLock);
175 e->mObjects.attach(objectID, object, cleanupCookie, func);
178 void* BBinder::findObject(const void* objectID) const
180 Extras* e = mExtras.load(std::memory_order_acquire);
183 AutoMutex _l(e->mLock);
184 return e->mObjects.find(objectID);
187 void BBinder::detachObject(const void* objectID)
189 Extras* e = mExtras.load(std::memory_order_acquire);
192 AutoMutex _l(e->mLock);
193 e->mObjects.detach(objectID);
196 BBinder* BBinder::localBinder()
203 Extras* e = mExtras.load(std::memory_order_relaxed);
208 status_t BBinder::onTransact(
209 uint32_t code, const Parcel& data, Parcel* reply, uint32_t /*flags*/)
212 case INTERFACE_TRANSACTION:
213 reply->writeString16(getInterfaceDescriptor());
216 case DUMP_TRANSACTION: {
217 int fd = data.readFileDescriptor();
218 int argc = data.readInt32();
219 Vector<String16> args;
220 for (int i = 0; i < argc && data.dataAvail() > 0; i++) {
221 args.add(data.readString16());
223 return dump(fd, args);
226 case SHELL_COMMAND_TRANSACTION: {
227 int in = data.readFileDescriptor();
228 int out = data.readFileDescriptor();
229 int err = data.readFileDescriptor();
230 int argc = data.readInt32();
231 Vector<String16> args;
232 for (int i = 0; i < argc && data.dataAvail() > 0; i++) {
233 args.add(data.readString16());
235 sp<IResultReceiver> resultReceiver = IResultReceiver::asInterface(
236 data.readStrongBinder());
238 // XXX can't add virtuals until binaries are updated.
239 //return shellCommand(in, out, err, args, resultReceiver);
240 if (resultReceiver != NULL) {
241 resultReceiver->send(INVALID_OPERATION);
245 case SYSPROPS_TRANSACTION: {
246 report_sysprop_change();
251 return UNKNOWN_TRANSACTION;
255 // ---------------------------------------------------------------------------
258 // This is used to transfer ownership of the remote binder from
259 // the BpRefBase object holding it (when it is constructed), to the
260 // owner of the BpRefBase object when it first acquires that BpRefBase.
261 kRemoteAcquired = 0x00000001
264 BpRefBase::BpRefBase(const sp<IBinder>& o)
265 : mRemote(o.get()), mRefs(NULL), mState(0)
267 extendObjectLifetime(OBJECT_LIFETIME_WEAK);
270 mRemote->incStrong(this); // Removed on first IncStrong().
271 mRefs = mRemote->createWeak(this); // Held for our entire lifetime.
275 BpRefBase::~BpRefBase()
278 if (!(mState.load(std::memory_order_relaxed)&kRemoteAcquired)) {
279 mRemote->decStrong(this);
281 mRefs->decWeak(this);
285 void BpRefBase::onFirstRef()
287 mState.fetch_or(kRemoteAcquired, std::memory_order_relaxed);
290 void BpRefBase::onLastStrongRef(const void* /*id*/)
293 mRemote->decStrong(this);
297 bool BpRefBase::onIncStrongAttempted(uint32_t /*flags*/, const void* /*id*/)
299 return mRemote ? mRefs->attemptIncStrong(this) : false;
302 // ---------------------------------------------------------------------------
304 }; // namespace android