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