blob: f6cc3afc9776345afc75c930f1395e4382f07fd0 [file] [log] [blame]
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001/*
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
Mathias Agopianc5b2c0b2009-05-19 19:08:10 -070017#include <binder/Binder.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080018
Bailey Forrest6913c462015-08-18 17:15:10 -070019#include <atomic>
Dianne Hackborn555f89d2012-05-08 18:54:22 -070020#include <utils/misc.h>
Mathias Agopianc5b2c0b2009-05-19 19:08:10 -070021#include <binder/BpBinder.h>
22#include <binder/IInterface.h>
Dianne Hackborn23eb1e22015-10-07 17:35:27 -070023#include <binder/IResultReceiver.h>
Dianne Hackborn1941a402016-08-29 12:30:43 -070024#include <binder/IShellCallback.h>
Mathias Agopianc5b2c0b2009-05-19 19:08:10 -070025#include <binder/Parcel.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080026
27#include <stdio.h>
28
29namespace android {
30
31// ---------------------------------------------------------------------------
32
Mathias Agopian83c04462009-05-22 19:00:22 -070033IBinder::IBinder()
34 : RefBase()
35{
36}
37
38IBinder::~IBinder()
39{
40}
41
42// ---------------------------------------------------------------------------
43
Colin Cross6f4f3ab2014-02-05 17:42:44 -080044sp<IInterface> IBinder::queryLocalInterface(const String16& /*descriptor*/)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080045{
Yi Kongfdd8da92018-06-07 17:52:27 -070046 return nullptr;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080047}
48
49BBinder* IBinder::localBinder()
50{
Yi Kongfdd8da92018-06-07 17:52:27 -070051 return nullptr;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080052}
53
54BpBinder* IBinder::remoteBinder()
55{
Yi Kongfdd8da92018-06-07 17:52:27 -070056 return nullptr;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080057}
58
59bool IBinder::checkSubclass(const void* /*subclassID*/) const
60{
61 return false;
62}
63
Dianne Hackborn23eb1e22015-10-07 17:35:27 -070064
Dianne Hackbornf2bf93b2015-10-14 15:13:02 -070065status_t IBinder::shellCommand(const sp<IBinder>& target, int in, int out, int err,
Dianne Hackborn1941a402016-08-29 12:30:43 -070066 Vector<String16>& args, const sp<IShellCallback>& callback,
67 const sp<IResultReceiver>& resultReceiver)
Dianne Hackborn23eb1e22015-10-07 17:35:27 -070068{
Dianne Hackbornf2bf93b2015-10-14 15:13:02 -070069 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]);
Dianne Hackborn23eb1e22015-10-07 17:35:27 -070078 }
Yi Kongfdd8da92018-06-07 17:52:27 -070079 send.writeStrongBinder(callback != nullptr ? IInterface::asBinder(callback) : nullptr);
80 send.writeStrongBinder(resultReceiver != nullptr ? IInterface::asBinder(resultReceiver) : nullptr);
Dianne Hackbornf2bf93b2015-10-14 15:13:02 -070081 return target->transact(SHELL_COMMAND_TRANSACTION, send, &reply);
Dianne Hackborn23eb1e22015-10-07 17:35:27 -070082}
83
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080084// ---------------------------------------------------------------------------
85
86class BBinder::Extras
87{
88public:
89 Mutex mLock;
90 BpBinder::ObjectManager mObjects;
91};
92
93// ---------------------------------------------------------------------------
94
Bailey Forrest6913c462015-08-18 17:15:10 -070095BBinder::BBinder() : mExtras(nullptr)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080096{
97}
98
99bool BBinder::isBinderAlive() const
100{
101 return true;
102}
103
104status_t BBinder::pingBinder()
105{
106 return NO_ERROR;
107}
108
Mathias Agopian83c04462009-05-22 19:00:22 -0700109const String16& BBinder::getInterfaceDescriptor() const
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800110{
Dan Egnor386a3322010-05-06 00:55:09 -0700111 // This is a local static rather than a global static,
112 // to avoid static initializer ordering issues.
113 static String16 sEmptyDescriptor;
Steve Block32397c12012-01-05 23:22:43 +0000114 ALOGW("reached BBinder::getInterfaceDescriptor (this=%p)", this);
Mathias Agopian83c04462009-05-22 19:00:22 -0700115 return sEmptyDescriptor;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800116}
117
Jiyong Parkb86c8662018-10-29 23:01:57 +0900118// NOLINTNEXTLINE(google-default-arguments)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800119status_t BBinder::transact(
120 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
121{
122 data.setDataPosition(0);
123
124 status_t err = NO_ERROR;
125 switch (code) {
126 case PING_TRANSACTION:
127 reply->writeInt32(pingBinder());
128 break;
129 default:
130 err = onTransact(code, data, reply, flags);
131 break;
132 }
133
Yi Kongfdd8da92018-06-07 17:52:27 -0700134 if (reply != nullptr) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800135 reply->setDataPosition(0);
136 }
137
138 return err;
139}
140
Jiyong Parkb86c8662018-10-29 23:01:57 +0900141// NOLINTNEXTLINE(google-default-arguments)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800142status_t BBinder::linkToDeath(
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800143 const sp<DeathRecipient>& /*recipient*/, void* /*cookie*/,
144 uint32_t /*flags*/)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800145{
146 return INVALID_OPERATION;
147}
148
Jiyong Parkb86c8662018-10-29 23:01:57 +0900149// NOLINTNEXTLINE(google-default-arguments)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800150status_t BBinder::unlinkToDeath(
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800151 const wp<DeathRecipient>& /*recipient*/, void* /*cookie*/,
152 uint32_t /*flags*/, wp<DeathRecipient>* /*outRecipient*/)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800153{
154 return INVALID_OPERATION;
155}
156
Dianne Hackborn23eb1e22015-10-07 17:35:27 -0700157status_t BBinder::dump(int /*fd*/, const Vector<String16>& /*args*/)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800158{
159 return NO_ERROR;
160}
161
162void BBinder::attachObject(
163 const void* objectID, void* object, void* cleanupCookie,
164 object_cleanup_func func)
165{
Bailey Forrest6913c462015-08-18 17:15:10 -0700166 Extras* e = mExtras.load(std::memory_order_acquire);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800167
168 if (!e) {
169 e = new Extras;
Bailey Forrest6913c462015-08-18 17:15:10 -0700170 Extras* expected = nullptr;
171 if (!mExtras.compare_exchange_strong(expected, e,
172 std::memory_order_release,
173 std::memory_order_acquire)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800174 delete e;
Bailey Forrest6913c462015-08-18 17:15:10 -0700175 e = expected; // Filled in by CAS
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800176 }
Yi Kongfdd8da92018-06-07 17:52:27 -0700177 if (e == nullptr) return; // out of memory
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800178 }
179
180 AutoMutex _l(e->mLock);
181 e->mObjects.attach(objectID, object, cleanupCookie, func);
182}
183
184void* BBinder::findObject(const void* objectID) const
185{
Bailey Forrest6913c462015-08-18 17:15:10 -0700186 Extras* e = mExtras.load(std::memory_order_acquire);
Yi Kongfdd8da92018-06-07 17:52:27 -0700187 if (!e) return nullptr;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800188
189 AutoMutex _l(e->mLock);
190 return e->mObjects.find(objectID);
191}
192
193void BBinder::detachObject(const void* objectID)
194{
Bailey Forrest6913c462015-08-18 17:15:10 -0700195 Extras* e = mExtras.load(std::memory_order_acquire);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800196 if (!e) return;
197
198 AutoMutex _l(e->mLock);
199 e->mObjects.detach(objectID);
200}
201
202BBinder* BBinder::localBinder()
203{
204 return this;
205}
206
207BBinder::~BBinder()
208{
Bailey Forrest6913c462015-08-18 17:15:10 -0700209 Extras* e = mExtras.load(std::memory_order_relaxed);
Hans Boehm3effaba2014-08-12 22:56:00 +0000210 if (e) delete e;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800211}
212
213
Jiyong Parkb86c8662018-10-29 23:01:57 +0900214// NOLINTNEXTLINE(google-default-arguments)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800215status_t BBinder::onTransact(
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800216 uint32_t code, const Parcel& data, Parcel* reply, uint32_t /*flags*/)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800217{
218 switch (code) {
219 case INTERFACE_TRANSACTION:
220 reply->writeString16(getInterfaceDescriptor());
221 return NO_ERROR;
222
223 case DUMP_TRANSACTION: {
224 int fd = 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 return dump(fd, args);
231 }
Dianne Hackborn555f89d2012-05-08 18:54:22 -0700232
Dianne Hackborn23eb1e22015-10-07 17:35:27 -0700233 case SHELL_COMMAND_TRANSACTION: {
234 int in = data.readFileDescriptor();
235 int out = data.readFileDescriptor();
236 int err = data.readFileDescriptor();
237 int argc = data.readInt32();
238 Vector<String16> args;
239 for (int i = 0; i < argc && data.dataAvail() > 0; i++) {
240 args.add(data.readString16());
241 }
Dianne Hackborn1941a402016-08-29 12:30:43 -0700242 sp<IShellCallback> shellCallback = IShellCallback::asInterface(
243 data.readStrongBinder());
Dianne Hackborn23eb1e22015-10-07 17:35:27 -0700244 sp<IResultReceiver> resultReceiver = IResultReceiver::asInterface(
245 data.readStrongBinder());
246
Dianne Hackbornf2bf93b2015-10-14 15:13:02 -0700247 // XXX can't add virtuals until binaries are updated.
248 //return shellCommand(in, out, err, args, resultReceiver);
Christopher Wiley0a9a1c12016-07-20 08:28:14 -0700249 (void)in;
250 (void)out;
251 (void)err;
252
Yi Kongfdd8da92018-06-07 17:52:27 -0700253 if (resultReceiver != nullptr) {
Dianne Hackbornf2bf93b2015-10-14 15:13:02 -0700254 resultReceiver->send(INVALID_OPERATION);
255 }
Martijn Coenenaa6ee992017-08-17 15:38:08 +0200256
257 return NO_ERROR;
Dianne Hackborn23eb1e22015-10-07 17:35:27 -0700258 }
259
Dianne Hackborn555f89d2012-05-08 18:54:22 -0700260 case SYSPROPS_TRANSACTION: {
261 report_sysprop_change();
262 return NO_ERROR;
263 }
264
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800265 default:
266 return UNKNOWN_TRANSACTION;
267 }
268}
269
270// ---------------------------------------------------------------------------
271
272enum {
273 // This is used to transfer ownership of the remote binder from
274 // the BpRefBase object holding it (when it is constructed), to the
275 // owner of the BpRefBase object when it first acquires that BpRefBase.
276 kRemoteAcquired = 0x00000001
277};
278
279BpRefBase::BpRefBase(const sp<IBinder>& o)
Yi Kongfdd8da92018-06-07 17:52:27 -0700280 : mRemote(o.get()), mRefs(nullptr), mState(0)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800281{
282 extendObjectLifetime(OBJECT_LIFETIME_WEAK);
283
284 if (mRemote) {
285 mRemote->incStrong(this); // Removed on first IncStrong().
286 mRefs = mRemote->createWeak(this); // Held for our entire lifetime.
287 }
288}
289
290BpRefBase::~BpRefBase()
291{
292 if (mRemote) {
Bailey Forrest6913c462015-08-18 17:15:10 -0700293 if (!(mState.load(std::memory_order_relaxed)&kRemoteAcquired)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800294 mRemote->decStrong(this);
295 }
296 mRefs->decWeak(this);
297 }
298}
299
300void BpRefBase::onFirstRef()
301{
Bailey Forrest6913c462015-08-18 17:15:10 -0700302 mState.fetch_or(kRemoteAcquired, std::memory_order_relaxed);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800303}
304
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800305void BpRefBase::onLastStrongRef(const void* /*id*/)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800306{
307 if (mRemote) {
308 mRemote->decStrong(this);
309 }
310}
311
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800312bool BpRefBase::onIncStrongAttempted(uint32_t /*flags*/, const void* /*id*/)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800313{
314 return mRemote ? mRefs->attemptIncStrong(this) : false;
315}
316
317// ---------------------------------------------------------------------------
318
319}; // namespace android