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
28 namespace android {
29
30 // ---------------------------------------------------------------------------
31
IBinder()32 IBinder::IBinder()
33 : RefBase()
34 {
35 }
36
~IBinder()37 IBinder::~IBinder()
38 {
39 }
40
41 // ---------------------------------------------------------------------------
42
queryLocalInterface(const String16 &)43 sp<IInterface> IBinder::queryLocalInterface(const String16& /*descriptor*/)
44 {
45 return NULL;
46 }
47
localBinder()48 BBinder* IBinder::localBinder()
49 {
50 return NULL;
51 }
52
remoteBinder()53 BpBinder* IBinder::remoteBinder()
54 {
55 return NULL;
56 }
57
checkSubclass(const void *) const58 bool IBinder::checkSubclass(const void* /*subclassID*/) const
59 {
60 return false;
61 }
62
63
shellCommand(const sp<IBinder> & target,int in,int out,int err,Vector<String16> & args,const sp<IResultReceiver> & resultReceiver)64 status_t IBinder::shellCommand(const sp<IBinder>& target, int in, int out, int err,
65 Vector<String16>& args, const sp<IResultReceiver>& resultReceiver)
66 {
67 Parcel send;
68 Parcel reply;
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]);
76 }
77 send.writeStrongBinder(resultReceiver != NULL ? IInterface::asBinder(resultReceiver) : NULL);
78 return target->transact(SHELL_COMMAND_TRANSACTION, send, &reply);
79 }
80
81 // ---------------------------------------------------------------------------
82
83 class BBinder::Extras
84 {
85 public:
86 Mutex mLock;
87 BpBinder::ObjectManager mObjects;
88 };
89
90 // ---------------------------------------------------------------------------
91
BBinder()92 BBinder::BBinder() : mExtras(nullptr)
93 {
94 }
95
isBinderAlive() const96 bool BBinder::isBinderAlive() const
97 {
98 return true;
99 }
100
pingBinder()101 status_t BBinder::pingBinder()
102 {
103 return NO_ERROR;
104 }
105
getInterfaceDescriptor() const106 const String16& BBinder::getInterfaceDescriptor() const
107 {
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;
113 }
114
transact(uint32_t code,const Parcel & data,Parcel * reply,uint32_t flags)115 status_t BBinder::transact(
116 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
117 {
118 data.setDataPosition(0);
119
120 status_t err = NO_ERROR;
121 switch (code) {
122 case PING_TRANSACTION:
123 reply->writeInt32(pingBinder());
124 break;
125 default:
126 err = onTransact(code, data, reply, flags);
127 break;
128 }
129
130 if (reply != NULL) {
131 reply->setDataPosition(0);
132 }
133
134 return err;
135 }
136
linkToDeath(const sp<DeathRecipient> &,void *,uint32_t)137 status_t BBinder::linkToDeath(
138 const sp<DeathRecipient>& /*recipient*/, void* /*cookie*/,
139 uint32_t /*flags*/)
140 {
141 return INVALID_OPERATION;
142 }
143
unlinkToDeath(const wp<DeathRecipient> &,void *,uint32_t,wp<DeathRecipient> *)144 status_t BBinder::unlinkToDeath(
145 const wp<DeathRecipient>& /*recipient*/, void* /*cookie*/,
146 uint32_t /*flags*/, wp<DeathRecipient>* /*outRecipient*/)
147 {
148 return INVALID_OPERATION;
149 }
150
dump(int,const Vector<String16> &)151 status_t BBinder::dump(int /*fd*/, const Vector<String16>& /*args*/)
152 {
153 return NO_ERROR;
154 }
155
attachObject(const void * objectID,void * object,void * cleanupCookie,object_cleanup_func func)156 void BBinder::attachObject(
157 const void* objectID, void* object, void* cleanupCookie,
158 object_cleanup_func func)
159 {
160 Extras* e = mExtras.load(std::memory_order_acquire);
161
162 if (!e) {
163 e = new Extras;
164 Extras* expected = nullptr;
165 if (!mExtras.compare_exchange_strong(expected, e,
166 std::memory_order_release,
167 std::memory_order_acquire)) {
168 delete e;
169 e = expected; // Filled in by CAS
170 }
171 if (e == 0) return; // out of memory
172 }
173
174 AutoMutex _l(e->mLock);
175 e->mObjects.attach(objectID, object, cleanupCookie, func);
176 }
177
findObject(const void * objectID) const178 void* BBinder::findObject(const void* objectID) const
179 {
180 Extras* e = mExtras.load(std::memory_order_acquire);
181 if (!e) return NULL;
182
183 AutoMutex _l(e->mLock);
184 return e->mObjects.find(objectID);
185 }
186
detachObject(const void * objectID)187 void BBinder::detachObject(const void* objectID)
188 {
189 Extras* e = mExtras.load(std::memory_order_acquire);
190 if (!e) return;
191
192 AutoMutex _l(e->mLock);
193 e->mObjects.detach(objectID);
194 }
195
localBinder()196 BBinder* BBinder::localBinder()
197 {
198 return this;
199 }
200
~BBinder()201 BBinder::~BBinder()
202 {
203 Extras* e = mExtras.load(std::memory_order_relaxed);
204 if (e) delete e;
205 }
206
207
onTransact(uint32_t code,const Parcel & data,Parcel * reply,uint32_t)208 status_t BBinder::onTransact(
209 uint32_t code, const Parcel& data, Parcel* reply, uint32_t /*flags*/)
210 {
211 switch (code) {
212 case INTERFACE_TRANSACTION:
213 reply->writeString16(getInterfaceDescriptor());
214 return NO_ERROR;
215
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());
222 }
223 return dump(fd, args);
224 }
225
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());
234 }
235 sp<IResultReceiver> resultReceiver = IResultReceiver::asInterface(
236 data.readStrongBinder());
237
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);
242 }
243 }
244
245 case SYSPROPS_TRANSACTION: {
246 report_sysprop_change();
247 return NO_ERROR;
248 }
249
250 default:
251 return UNKNOWN_TRANSACTION;
252 }
253 }
254
255 // ---------------------------------------------------------------------------
256
257 enum {
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
262 };
263
BpRefBase(const sp<IBinder> & o)264 BpRefBase::BpRefBase(const sp<IBinder>& o)
265 : mRemote(o.get()), mRefs(NULL), mState(0)
266 {
267 extendObjectLifetime(OBJECT_LIFETIME_WEAK);
268
269 if (mRemote) {
270 mRemote->incStrong(this); // Removed on first IncStrong().
271 mRefs = mRemote->createWeak(this); // Held for our entire lifetime.
272 }
273 }
274
~BpRefBase()275 BpRefBase::~BpRefBase()
276 {
277 if (mRemote) {
278 if (!(mState.load(std::memory_order_relaxed)&kRemoteAcquired)) {
279 mRemote->decStrong(this);
280 }
281 mRefs->decWeak(this);
282 }
283 }
284
onFirstRef()285 void BpRefBase::onFirstRef()
286 {
287 mState.fetch_or(kRemoteAcquired, std::memory_order_relaxed);
288 }
289
onLastStrongRef(const void *)290 void BpRefBase::onLastStrongRef(const void* /*id*/)
291 {
292 if (mRemote) {
293 mRemote->decStrong(this);
294 }
295 }
296
onIncStrongAttempted(uint32_t,const void *)297 bool BpRefBase::onIncStrongAttempted(uint32_t /*flags*/, const void* /*id*/)
298 {
299 return mRemote ? mRefs->attemptIncStrong(this) : false;
300 }
301
302 // ---------------------------------------------------------------------------
303
304 }; // namespace android
305