xref: /aosp_15_r20/frameworks/native/libs/binder/Binder.cpp (revision 38e8c45f13ce32b0dcecb25141ffecaf386fa17f)
1*38e8c45fSAndroid Build Coastguard Worker /*
2*38e8c45fSAndroid Build Coastguard Worker  * Copyright (C) 2005 The Android Open Source Project
3*38e8c45fSAndroid Build Coastguard Worker  *
4*38e8c45fSAndroid Build Coastguard Worker  * Licensed under the Apache License, Version 2.0 (the "License");
5*38e8c45fSAndroid Build Coastguard Worker  * you may not use this file except in compliance with the License.
6*38e8c45fSAndroid Build Coastguard Worker  * You may obtain a copy of the License at
7*38e8c45fSAndroid Build Coastguard Worker  *
8*38e8c45fSAndroid Build Coastguard Worker  *      http://www.apache.org/licenses/LICENSE-2.0
9*38e8c45fSAndroid Build Coastguard Worker  *
10*38e8c45fSAndroid Build Coastguard Worker  * Unless required by applicable law or agreed to in writing, software
11*38e8c45fSAndroid Build Coastguard Worker  * distributed under the License is distributed on an "AS IS" BASIS,
12*38e8c45fSAndroid Build Coastguard Worker  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*38e8c45fSAndroid Build Coastguard Worker  * See the License for the specific language governing permissions and
14*38e8c45fSAndroid Build Coastguard Worker  * limitations under the License.
15*38e8c45fSAndroid Build Coastguard Worker  */
16*38e8c45fSAndroid Build Coastguard Worker 
17*38e8c45fSAndroid Build Coastguard Worker #include <binder/Binder.h>
18*38e8c45fSAndroid Build Coastguard Worker 
19*38e8c45fSAndroid Build Coastguard Worker #include <atomic>
20*38e8c45fSAndroid Build Coastguard Worker #include <set>
21*38e8c45fSAndroid Build Coastguard Worker 
22*38e8c45fSAndroid Build Coastguard Worker #include <binder/BpBinder.h>
23*38e8c45fSAndroid Build Coastguard Worker #include <binder/IInterface.h>
24*38e8c45fSAndroid Build Coastguard Worker #include <binder/IPCThreadState.h>
25*38e8c45fSAndroid Build Coastguard Worker #include <binder/IResultReceiver.h>
26*38e8c45fSAndroid Build Coastguard Worker #include <binder/IShellCallback.h>
27*38e8c45fSAndroid Build Coastguard Worker #include <binder/Parcel.h>
28*38e8c45fSAndroid Build Coastguard Worker #include <binder/RecordedTransaction.h>
29*38e8c45fSAndroid Build Coastguard Worker #include <binder/RpcServer.h>
30*38e8c45fSAndroid Build Coastguard Worker #include <binder/unique_fd.h>
31*38e8c45fSAndroid Build Coastguard Worker #include <pthread.h>
32*38e8c45fSAndroid Build Coastguard Worker 
33*38e8c45fSAndroid Build Coastguard Worker #include <inttypes.h>
34*38e8c45fSAndroid Build Coastguard Worker #include <stdio.h>
35*38e8c45fSAndroid Build Coastguard Worker 
36*38e8c45fSAndroid Build Coastguard Worker #ifdef __linux__
37*38e8c45fSAndroid Build Coastguard Worker #include <linux/sched.h>
38*38e8c45fSAndroid Build Coastguard Worker #endif
39*38e8c45fSAndroid Build Coastguard Worker 
40*38e8c45fSAndroid Build Coastguard Worker #include "BuildFlags.h"
41*38e8c45fSAndroid Build Coastguard Worker #include "OS.h"
42*38e8c45fSAndroid Build Coastguard Worker #include "RpcState.h"
43*38e8c45fSAndroid Build Coastguard Worker 
44*38e8c45fSAndroid Build Coastguard Worker namespace android {
45*38e8c45fSAndroid Build Coastguard Worker 
46*38e8c45fSAndroid Build Coastguard Worker using android::binder::unique_fd;
47*38e8c45fSAndroid Build Coastguard Worker 
48*38e8c45fSAndroid Build Coastguard Worker constexpr uid_t kUidRoot = 0;
49*38e8c45fSAndroid Build Coastguard Worker 
50*38e8c45fSAndroid Build Coastguard Worker // Service implementations inherit from BBinder and IBinder, and this is frozen
51*38e8c45fSAndroid Build Coastguard Worker // in prebuilts.
52*38e8c45fSAndroid Build Coastguard Worker #ifdef __LP64__
53*38e8c45fSAndroid Build Coastguard Worker static_assert(sizeof(IBinder) == 24);
54*38e8c45fSAndroid Build Coastguard Worker static_assert(sizeof(BBinder) == 40);
55*38e8c45fSAndroid Build Coastguard Worker #else
56*38e8c45fSAndroid Build Coastguard Worker static_assert(sizeof(IBinder) == 12);
57*38e8c45fSAndroid Build Coastguard Worker static_assert(sizeof(BBinder) == 20);
58*38e8c45fSAndroid Build Coastguard Worker #endif
59*38e8c45fSAndroid Build Coastguard Worker 
60*38e8c45fSAndroid Build Coastguard Worker // global b/c b/230079120 - consistent symbol table
61*38e8c45fSAndroid Build Coastguard Worker #ifdef BINDER_RPC_DEV_SERVERS
62*38e8c45fSAndroid Build Coastguard Worker constexpr bool kEnableRpcDevServers = true;
63*38e8c45fSAndroid Build Coastguard Worker #else
64*38e8c45fSAndroid Build Coastguard Worker constexpr bool kEnableRpcDevServers = false;
65*38e8c45fSAndroid Build Coastguard Worker #endif
66*38e8c45fSAndroid Build Coastguard Worker 
67*38e8c45fSAndroid Build Coastguard Worker #ifdef BINDER_ENABLE_RECORDING
68*38e8c45fSAndroid Build Coastguard Worker constexpr bool kEnableRecording = true;
69*38e8c45fSAndroid Build Coastguard Worker #else
70*38e8c45fSAndroid Build Coastguard Worker constexpr bool kEnableRecording = false;
71*38e8c45fSAndroid Build Coastguard Worker #endif
72*38e8c45fSAndroid Build Coastguard Worker 
73*38e8c45fSAndroid Build Coastguard Worker // Log any reply transactions for which the data exceeds this size
74*38e8c45fSAndroid Build Coastguard Worker #define LOG_REPLIES_OVER_SIZE (300 * 1024)
75*38e8c45fSAndroid Build Coastguard Worker // ---------------------------------------------------------------------------
76*38e8c45fSAndroid Build Coastguard Worker 
IBinder()77*38e8c45fSAndroid Build Coastguard Worker IBinder::IBinder()
78*38e8c45fSAndroid Build Coastguard Worker     : RefBase()
79*38e8c45fSAndroid Build Coastguard Worker {
80*38e8c45fSAndroid Build Coastguard Worker }
81*38e8c45fSAndroid Build Coastguard Worker 
~IBinder()82*38e8c45fSAndroid Build Coastguard Worker IBinder::~IBinder()
83*38e8c45fSAndroid Build Coastguard Worker {
84*38e8c45fSAndroid Build Coastguard Worker }
85*38e8c45fSAndroid Build Coastguard Worker 
86*38e8c45fSAndroid Build Coastguard Worker // ---------------------------------------------------------------------------
87*38e8c45fSAndroid Build Coastguard Worker 
queryLocalInterface(const String16 &)88*38e8c45fSAndroid Build Coastguard Worker sp<IInterface>  IBinder::queryLocalInterface(const String16& /*descriptor*/)
89*38e8c45fSAndroid Build Coastguard Worker {
90*38e8c45fSAndroid Build Coastguard Worker     return nullptr;
91*38e8c45fSAndroid Build Coastguard Worker }
92*38e8c45fSAndroid Build Coastguard Worker 
localBinder()93*38e8c45fSAndroid Build Coastguard Worker BBinder* IBinder::localBinder()
94*38e8c45fSAndroid Build Coastguard Worker {
95*38e8c45fSAndroid Build Coastguard Worker     return nullptr;
96*38e8c45fSAndroid Build Coastguard Worker }
97*38e8c45fSAndroid Build Coastguard Worker 
remoteBinder()98*38e8c45fSAndroid Build Coastguard Worker BpBinder* IBinder::remoteBinder()
99*38e8c45fSAndroid Build Coastguard Worker {
100*38e8c45fSAndroid Build Coastguard Worker     return nullptr;
101*38e8c45fSAndroid Build Coastguard Worker }
102*38e8c45fSAndroid Build Coastguard Worker 
checkSubclass(const void *) const103*38e8c45fSAndroid Build Coastguard Worker bool IBinder::checkSubclass(const void* /*subclassID*/) const
104*38e8c45fSAndroid Build Coastguard Worker {
105*38e8c45fSAndroid Build Coastguard Worker     return false;
106*38e8c45fSAndroid Build Coastguard Worker }
107*38e8c45fSAndroid Build Coastguard Worker 
108*38e8c45fSAndroid Build Coastguard Worker 
shellCommand(const sp<IBinder> & target,int in,int out,int err,Vector<String16> & args,const sp<IShellCallback> & callback,const sp<IResultReceiver> & resultReceiver)109*38e8c45fSAndroid Build Coastguard Worker status_t IBinder::shellCommand(const sp<IBinder>& target, int in, int out, int err,
110*38e8c45fSAndroid Build Coastguard Worker     Vector<String16>& args, const sp<IShellCallback>& callback,
111*38e8c45fSAndroid Build Coastguard Worker     const sp<IResultReceiver>& resultReceiver)
112*38e8c45fSAndroid Build Coastguard Worker {
113*38e8c45fSAndroid Build Coastguard Worker     Parcel send;
114*38e8c45fSAndroid Build Coastguard Worker     Parcel reply;
115*38e8c45fSAndroid Build Coastguard Worker     send.writeFileDescriptor(in);
116*38e8c45fSAndroid Build Coastguard Worker     send.writeFileDescriptor(out);
117*38e8c45fSAndroid Build Coastguard Worker     send.writeFileDescriptor(err);
118*38e8c45fSAndroid Build Coastguard Worker     const size_t numArgs = args.size();
119*38e8c45fSAndroid Build Coastguard Worker     send.writeInt32(numArgs);
120*38e8c45fSAndroid Build Coastguard Worker     for (size_t i = 0; i < numArgs; i++) {
121*38e8c45fSAndroid Build Coastguard Worker         send.writeString16(args[i]);
122*38e8c45fSAndroid Build Coastguard Worker     }
123*38e8c45fSAndroid Build Coastguard Worker     send.writeStrongBinder(callback != nullptr ? IInterface::asBinder(callback) : nullptr);
124*38e8c45fSAndroid Build Coastguard Worker     send.writeStrongBinder(resultReceiver != nullptr ? IInterface::asBinder(resultReceiver) : nullptr);
125*38e8c45fSAndroid Build Coastguard Worker     return target->transact(SHELL_COMMAND_TRANSACTION, send, &reply);
126*38e8c45fSAndroid Build Coastguard Worker }
127*38e8c45fSAndroid Build Coastguard Worker 
getExtension(sp<IBinder> * out)128*38e8c45fSAndroid Build Coastguard Worker status_t IBinder::getExtension(sp<IBinder>* out) {
129*38e8c45fSAndroid Build Coastguard Worker     BBinder* local = this->localBinder();
130*38e8c45fSAndroid Build Coastguard Worker     if (local != nullptr) {
131*38e8c45fSAndroid Build Coastguard Worker         *out = local->getExtension();
132*38e8c45fSAndroid Build Coastguard Worker         return OK;
133*38e8c45fSAndroid Build Coastguard Worker     }
134*38e8c45fSAndroid Build Coastguard Worker 
135*38e8c45fSAndroid Build Coastguard Worker     BpBinder* proxy = this->remoteBinder();
136*38e8c45fSAndroid Build Coastguard Worker     LOG_ALWAYS_FATAL_IF(proxy == nullptr);
137*38e8c45fSAndroid Build Coastguard Worker 
138*38e8c45fSAndroid Build Coastguard Worker     Parcel data;
139*38e8c45fSAndroid Build Coastguard Worker     Parcel reply;
140*38e8c45fSAndroid Build Coastguard Worker     status_t status = transact(EXTENSION_TRANSACTION, data, &reply);
141*38e8c45fSAndroid Build Coastguard Worker     if (status != OK) return status;
142*38e8c45fSAndroid Build Coastguard Worker 
143*38e8c45fSAndroid Build Coastguard Worker     return reply.readNullableStrongBinder(out);
144*38e8c45fSAndroid Build Coastguard Worker }
145*38e8c45fSAndroid Build Coastguard Worker 
addFrozenStateChangeCallback(const wp<FrozenStateChangeCallback> & callback)146*38e8c45fSAndroid Build Coastguard Worker status_t IBinder::addFrozenStateChangeCallback(const wp<FrozenStateChangeCallback>& callback) {
147*38e8c45fSAndroid Build Coastguard Worker     BpBinder* proxy = this->remoteBinder();
148*38e8c45fSAndroid Build Coastguard Worker     if (proxy != nullptr) {
149*38e8c45fSAndroid Build Coastguard Worker         return proxy->addFrozenStateChangeCallback(callback);
150*38e8c45fSAndroid Build Coastguard Worker     }
151*38e8c45fSAndroid Build Coastguard Worker     return INVALID_OPERATION;
152*38e8c45fSAndroid Build Coastguard Worker }
153*38e8c45fSAndroid Build Coastguard Worker 
removeFrozenStateChangeCallback(const wp<FrozenStateChangeCallback> & callback)154*38e8c45fSAndroid Build Coastguard Worker status_t IBinder::removeFrozenStateChangeCallback(const wp<FrozenStateChangeCallback>& callback) {
155*38e8c45fSAndroid Build Coastguard Worker     BpBinder* proxy = this->remoteBinder();
156*38e8c45fSAndroid Build Coastguard Worker     if (proxy != nullptr) {
157*38e8c45fSAndroid Build Coastguard Worker         return proxy->removeFrozenStateChangeCallback(callback);
158*38e8c45fSAndroid Build Coastguard Worker     }
159*38e8c45fSAndroid Build Coastguard Worker     return INVALID_OPERATION;
160*38e8c45fSAndroid Build Coastguard Worker }
161*38e8c45fSAndroid Build Coastguard Worker 
getDebugPid(pid_t * out)162*38e8c45fSAndroid Build Coastguard Worker status_t IBinder::getDebugPid(pid_t* out) {
163*38e8c45fSAndroid Build Coastguard Worker     BBinder* local = this->localBinder();
164*38e8c45fSAndroid Build Coastguard Worker     if (local != nullptr) {
165*38e8c45fSAndroid Build Coastguard Worker       *out = local->getDebugPid();
166*38e8c45fSAndroid Build Coastguard Worker       return OK;
167*38e8c45fSAndroid Build Coastguard Worker     }
168*38e8c45fSAndroid Build Coastguard Worker 
169*38e8c45fSAndroid Build Coastguard Worker     BpBinder* proxy = this->remoteBinder();
170*38e8c45fSAndroid Build Coastguard Worker     LOG_ALWAYS_FATAL_IF(proxy == nullptr);
171*38e8c45fSAndroid Build Coastguard Worker 
172*38e8c45fSAndroid Build Coastguard Worker     Parcel data;
173*38e8c45fSAndroid Build Coastguard Worker     Parcel reply;
174*38e8c45fSAndroid Build Coastguard Worker     status_t status = transact(DEBUG_PID_TRANSACTION, data, &reply);
175*38e8c45fSAndroid Build Coastguard Worker     if (status != OK) return status;
176*38e8c45fSAndroid Build Coastguard Worker 
177*38e8c45fSAndroid Build Coastguard Worker     int32_t pid;
178*38e8c45fSAndroid Build Coastguard Worker     status = reply.readInt32(&pid);
179*38e8c45fSAndroid Build Coastguard Worker     if (status != OK) return status;
180*38e8c45fSAndroid Build Coastguard Worker 
181*38e8c45fSAndroid Build Coastguard Worker     if (pid < 0 || pid > std::numeric_limits<pid_t>::max()) {
182*38e8c45fSAndroid Build Coastguard Worker         return BAD_VALUE;
183*38e8c45fSAndroid Build Coastguard Worker     }
184*38e8c45fSAndroid Build Coastguard Worker     *out = pid;
185*38e8c45fSAndroid Build Coastguard Worker     return OK;
186*38e8c45fSAndroid Build Coastguard Worker }
187*38e8c45fSAndroid Build Coastguard Worker 
setRpcClientDebug(unique_fd socketFd,const sp<IBinder> & keepAliveBinder)188*38e8c45fSAndroid Build Coastguard Worker status_t IBinder::setRpcClientDebug(unique_fd socketFd, const sp<IBinder>& keepAliveBinder) {
189*38e8c45fSAndroid Build Coastguard Worker     if (!kEnableRpcDevServers) {
190*38e8c45fSAndroid Build Coastguard Worker         ALOGW("setRpcClientDebug disallowed because RPC is not enabled");
191*38e8c45fSAndroid Build Coastguard Worker         return INVALID_OPERATION;
192*38e8c45fSAndroid Build Coastguard Worker     }
193*38e8c45fSAndroid Build Coastguard Worker     if (!kEnableKernelIpc) {
194*38e8c45fSAndroid Build Coastguard Worker         ALOGW("setRpcClientDebug disallowed because kernel binder is not enabled");
195*38e8c45fSAndroid Build Coastguard Worker         return INVALID_OPERATION;
196*38e8c45fSAndroid Build Coastguard Worker     }
197*38e8c45fSAndroid Build Coastguard Worker 
198*38e8c45fSAndroid Build Coastguard Worker     BBinder* local = this->localBinder();
199*38e8c45fSAndroid Build Coastguard Worker     if (local != nullptr) {
200*38e8c45fSAndroid Build Coastguard Worker         return local->BBinder::setRpcClientDebug(std::move(socketFd), keepAliveBinder);
201*38e8c45fSAndroid Build Coastguard Worker     }
202*38e8c45fSAndroid Build Coastguard Worker 
203*38e8c45fSAndroid Build Coastguard Worker     BpBinder* proxy = this->remoteBinder();
204*38e8c45fSAndroid Build Coastguard Worker     LOG_ALWAYS_FATAL_IF(proxy == nullptr, "binder object must be either local or remote");
205*38e8c45fSAndroid Build Coastguard Worker 
206*38e8c45fSAndroid Build Coastguard Worker     Parcel data;
207*38e8c45fSAndroid Build Coastguard Worker     Parcel reply;
208*38e8c45fSAndroid Build Coastguard Worker     status_t status;
209*38e8c45fSAndroid Build Coastguard Worker     if (status = data.writeBool(socketFd.ok()); status != OK) return status;
210*38e8c45fSAndroid Build Coastguard Worker     if (socketFd.ok()) {
211*38e8c45fSAndroid Build Coastguard Worker         // writeUniqueFileDescriptor currently makes an unnecessary dup().
212*38e8c45fSAndroid Build Coastguard Worker         status = data.writeFileDescriptor(socketFd.release(), true /* own */);
213*38e8c45fSAndroid Build Coastguard Worker         if (status != OK) return status;
214*38e8c45fSAndroid Build Coastguard Worker     }
215*38e8c45fSAndroid Build Coastguard Worker     if (status = data.writeStrongBinder(keepAliveBinder); status != OK) return status;
216*38e8c45fSAndroid Build Coastguard Worker     return transact(SET_RPC_CLIENT_TRANSACTION, data, &reply);
217*38e8c45fSAndroid Build Coastguard Worker }
218*38e8c45fSAndroid Build Coastguard Worker 
withLock(const std::function<void ()> & doWithLock)219*38e8c45fSAndroid Build Coastguard Worker void IBinder::withLock(const std::function<void()>& doWithLock) {
220*38e8c45fSAndroid Build Coastguard Worker     BBinder* local = localBinder();
221*38e8c45fSAndroid Build Coastguard Worker     if (local) {
222*38e8c45fSAndroid Build Coastguard Worker         local->withLock(doWithLock);
223*38e8c45fSAndroid Build Coastguard Worker         return;
224*38e8c45fSAndroid Build Coastguard Worker     }
225*38e8c45fSAndroid Build Coastguard Worker     BpBinder* proxy = this->remoteBinder();
226*38e8c45fSAndroid Build Coastguard Worker     LOG_ALWAYS_FATAL_IF(proxy == nullptr, "binder object must be either local or remote");
227*38e8c45fSAndroid Build Coastguard Worker     proxy->withLock(doWithLock);
228*38e8c45fSAndroid Build Coastguard Worker }
229*38e8c45fSAndroid Build Coastguard Worker 
lookupOrCreateWeak(const void * objectID,object_make_func make,const void * makeArgs)230*38e8c45fSAndroid Build Coastguard Worker sp<IBinder> IBinder::lookupOrCreateWeak(const void* objectID, object_make_func make,
231*38e8c45fSAndroid Build Coastguard Worker                                         const void* makeArgs) {
232*38e8c45fSAndroid Build Coastguard Worker     BBinder* local = localBinder();
233*38e8c45fSAndroid Build Coastguard Worker     if (local) {
234*38e8c45fSAndroid Build Coastguard Worker         return local->lookupOrCreateWeak(objectID, make, makeArgs);
235*38e8c45fSAndroid Build Coastguard Worker     }
236*38e8c45fSAndroid Build Coastguard Worker     BpBinder* proxy = this->remoteBinder();
237*38e8c45fSAndroid Build Coastguard Worker     LOG_ALWAYS_FATAL_IF(proxy == nullptr, "binder object must be either local or remote");
238*38e8c45fSAndroid Build Coastguard Worker     return proxy->lookupOrCreateWeak(objectID, make, makeArgs);
239*38e8c45fSAndroid Build Coastguard Worker }
240*38e8c45fSAndroid Build Coastguard Worker 
241*38e8c45fSAndroid Build Coastguard Worker // ---------------------------------------------------------------------------
242*38e8c45fSAndroid Build Coastguard Worker 
243*38e8c45fSAndroid Build Coastguard Worker class BBinder::RpcServerLink : public IBinder::DeathRecipient {
244*38e8c45fSAndroid Build Coastguard Worker public:
245*38e8c45fSAndroid Build Coastguard Worker     // On binder died, calls RpcServer::shutdown on @a rpcServer, and removes itself from @a binder.
RpcServerLink(const sp<RpcServer> & rpcServer,const sp<IBinder> & keepAliveBinder,const wp<BBinder> & binder)246*38e8c45fSAndroid Build Coastguard Worker     RpcServerLink(const sp<RpcServer>& rpcServer, const sp<IBinder>& keepAliveBinder,
247*38e8c45fSAndroid Build Coastguard Worker                   const wp<BBinder>& binder)
248*38e8c45fSAndroid Build Coastguard Worker           : mRpcServer(rpcServer), mKeepAliveBinder(keepAliveBinder), mBinder(binder) {}
249*38e8c45fSAndroid Build Coastguard Worker     virtual ~RpcServerLink();
binderDied(const wp<IBinder> &)250*38e8c45fSAndroid Build Coastguard Worker     void binderDied(const wp<IBinder>&) override {
251*38e8c45fSAndroid Build Coastguard Worker         auto promoted = mBinder.promote();
252*38e8c45fSAndroid Build Coastguard Worker         ALOGI("RpcBinder: binder died, shutting down RpcServer for %s",
253*38e8c45fSAndroid Build Coastguard Worker               promoted ? String8(promoted->getInterfaceDescriptor()).c_str() : "<NULL>");
254*38e8c45fSAndroid Build Coastguard Worker 
255*38e8c45fSAndroid Build Coastguard Worker         if (mRpcServer == nullptr) {
256*38e8c45fSAndroid Build Coastguard Worker             ALOGW("RpcServerLink: Unable to shut down RpcServer because it does not exist.");
257*38e8c45fSAndroid Build Coastguard Worker         } else {
258*38e8c45fSAndroid Build Coastguard Worker             ALOGW_IF(!mRpcServer->shutdown(),
259*38e8c45fSAndroid Build Coastguard Worker                      "RpcServerLink: RpcServer did not shut down properly. Not started?");
260*38e8c45fSAndroid Build Coastguard Worker         }
261*38e8c45fSAndroid Build Coastguard Worker         mRpcServer.clear();
262*38e8c45fSAndroid Build Coastguard Worker 
263*38e8c45fSAndroid Build Coastguard Worker         if (promoted) {
264*38e8c45fSAndroid Build Coastguard Worker             promoted->removeRpcServerLink(sp<RpcServerLink>::fromExisting(this));
265*38e8c45fSAndroid Build Coastguard Worker         }
266*38e8c45fSAndroid Build Coastguard Worker         mBinder.clear();
267*38e8c45fSAndroid Build Coastguard Worker     }
268*38e8c45fSAndroid Build Coastguard Worker 
269*38e8c45fSAndroid Build Coastguard Worker private:
270*38e8c45fSAndroid Build Coastguard Worker     sp<RpcServer> mRpcServer;
271*38e8c45fSAndroid Build Coastguard Worker     sp<IBinder> mKeepAliveBinder; // hold to avoid automatically unlinking
272*38e8c45fSAndroid Build Coastguard Worker     wp<BBinder> mBinder;
273*38e8c45fSAndroid Build Coastguard Worker };
~RpcServerLink()274*38e8c45fSAndroid Build Coastguard Worker BBinder::RpcServerLink::~RpcServerLink() {}
275*38e8c45fSAndroid Build Coastguard Worker 
276*38e8c45fSAndroid Build Coastguard Worker class BBinder::Extras
277*38e8c45fSAndroid Build Coastguard Worker {
278*38e8c45fSAndroid Build Coastguard Worker public:
279*38e8c45fSAndroid Build Coastguard Worker     // unlocked objects
280*38e8c45fSAndroid Build Coastguard Worker     sp<IBinder> mExtension;
281*38e8c45fSAndroid Build Coastguard Worker #ifdef __linux__
282*38e8c45fSAndroid Build Coastguard Worker     int mPolicy = SCHED_NORMAL;
283*38e8c45fSAndroid Build Coastguard Worker     int mPriority = 0;
284*38e8c45fSAndroid Build Coastguard Worker #endif
285*38e8c45fSAndroid Build Coastguard Worker     bool mRequestingSid = false;
286*38e8c45fSAndroid Build Coastguard Worker     bool mInheritRt = false;
287*38e8c45fSAndroid Build Coastguard Worker 
288*38e8c45fSAndroid Build Coastguard Worker     // for below objects
289*38e8c45fSAndroid Build Coastguard Worker     RpcMutex mLock;
290*38e8c45fSAndroid Build Coastguard Worker     std::set<sp<RpcServerLink>> mRpcServerLinks;
291*38e8c45fSAndroid Build Coastguard Worker     BpBinder::ObjectManager mObjects;
292*38e8c45fSAndroid Build Coastguard Worker 
293*38e8c45fSAndroid Build Coastguard Worker     unique_fd mRecordingFd;
294*38e8c45fSAndroid Build Coastguard Worker };
295*38e8c45fSAndroid Build Coastguard Worker 
296*38e8c45fSAndroid Build Coastguard Worker // ---------------------------------------------------------------------------
297*38e8c45fSAndroid Build Coastguard Worker 
BBinder()298*38e8c45fSAndroid Build Coastguard Worker BBinder::BBinder() : mExtras(nullptr), mStability(0), mParceled(false), mRecordingOn(false) {}
299*38e8c45fSAndroid Build Coastguard Worker 
isBinderAlive() const300*38e8c45fSAndroid Build Coastguard Worker bool BBinder::isBinderAlive() const
301*38e8c45fSAndroid Build Coastguard Worker {
302*38e8c45fSAndroid Build Coastguard Worker     return true;
303*38e8c45fSAndroid Build Coastguard Worker }
304*38e8c45fSAndroid Build Coastguard Worker 
pingBinder()305*38e8c45fSAndroid Build Coastguard Worker status_t BBinder::pingBinder()
306*38e8c45fSAndroid Build Coastguard Worker {
307*38e8c45fSAndroid Build Coastguard Worker     return NO_ERROR;
308*38e8c45fSAndroid Build Coastguard Worker }
309*38e8c45fSAndroid Build Coastguard Worker 
startRecordingTransactions(const Parcel & data)310*38e8c45fSAndroid Build Coastguard Worker status_t BBinder::startRecordingTransactions(const Parcel& data) {
311*38e8c45fSAndroid Build Coastguard Worker     if (!kEnableRecording) {
312*38e8c45fSAndroid Build Coastguard Worker         ALOGW("Binder recording disallowed because recording is not enabled");
313*38e8c45fSAndroid Build Coastguard Worker         return INVALID_OPERATION;
314*38e8c45fSAndroid Build Coastguard Worker     }
315*38e8c45fSAndroid Build Coastguard Worker     if (!kEnableKernelIpc) {
316*38e8c45fSAndroid Build Coastguard Worker         ALOGW("Binder recording disallowed because kernel binder is not enabled");
317*38e8c45fSAndroid Build Coastguard Worker         return INVALID_OPERATION;
318*38e8c45fSAndroid Build Coastguard Worker     }
319*38e8c45fSAndroid Build Coastguard Worker     uid_t uid = IPCThreadState::self()->getCallingUid();
320*38e8c45fSAndroid Build Coastguard Worker     if (uid != kUidRoot) {
321*38e8c45fSAndroid Build Coastguard Worker         ALOGE("Binder recording not allowed because client %" PRIu32 " is not root", uid);
322*38e8c45fSAndroid Build Coastguard Worker         return PERMISSION_DENIED;
323*38e8c45fSAndroid Build Coastguard Worker     }
324*38e8c45fSAndroid Build Coastguard Worker     Extras* e = getOrCreateExtras();
325*38e8c45fSAndroid Build Coastguard Worker     RpcMutexUniqueLock lock(e->mLock);
326*38e8c45fSAndroid Build Coastguard Worker     if (mRecordingOn) {
327*38e8c45fSAndroid Build Coastguard Worker         ALOGI("Could not start Binder recording. Another is already in progress.");
328*38e8c45fSAndroid Build Coastguard Worker         return INVALID_OPERATION;
329*38e8c45fSAndroid Build Coastguard Worker     } else {
330*38e8c45fSAndroid Build Coastguard Worker         status_t readStatus = data.readUniqueFileDescriptor(&(e->mRecordingFd));
331*38e8c45fSAndroid Build Coastguard Worker         if (readStatus != OK) {
332*38e8c45fSAndroid Build Coastguard Worker             return readStatus;
333*38e8c45fSAndroid Build Coastguard Worker         }
334*38e8c45fSAndroid Build Coastguard Worker         mRecordingOn = true;
335*38e8c45fSAndroid Build Coastguard Worker         ALOGI("Started Binder recording.");
336*38e8c45fSAndroid Build Coastguard Worker         return NO_ERROR;
337*38e8c45fSAndroid Build Coastguard Worker     }
338*38e8c45fSAndroid Build Coastguard Worker }
339*38e8c45fSAndroid Build Coastguard Worker 
stopRecordingTransactions()340*38e8c45fSAndroid Build Coastguard Worker status_t BBinder::stopRecordingTransactions() {
341*38e8c45fSAndroid Build Coastguard Worker     if (!kEnableRecording) {
342*38e8c45fSAndroid Build Coastguard Worker         ALOGW("Binder recording disallowed because recording is not enabled");
343*38e8c45fSAndroid Build Coastguard Worker         return INVALID_OPERATION;
344*38e8c45fSAndroid Build Coastguard Worker     }
345*38e8c45fSAndroid Build Coastguard Worker     if (!kEnableKernelIpc) {
346*38e8c45fSAndroid Build Coastguard Worker         ALOGW("Binder recording disallowed because kernel binder is not enabled");
347*38e8c45fSAndroid Build Coastguard Worker         return INVALID_OPERATION;
348*38e8c45fSAndroid Build Coastguard Worker     }
349*38e8c45fSAndroid Build Coastguard Worker     uid_t uid = IPCThreadState::self()->getCallingUid();
350*38e8c45fSAndroid Build Coastguard Worker     if (uid != kUidRoot) {
351*38e8c45fSAndroid Build Coastguard Worker         ALOGE("Binder recording not allowed because client %" PRIu32 " is not root", uid);
352*38e8c45fSAndroid Build Coastguard Worker         return PERMISSION_DENIED;
353*38e8c45fSAndroid Build Coastguard Worker     }
354*38e8c45fSAndroid Build Coastguard Worker     Extras* e = getOrCreateExtras();
355*38e8c45fSAndroid Build Coastguard Worker     RpcMutexUniqueLock lock(e->mLock);
356*38e8c45fSAndroid Build Coastguard Worker     if (mRecordingOn) {
357*38e8c45fSAndroid Build Coastguard Worker         e->mRecordingFd.reset();
358*38e8c45fSAndroid Build Coastguard Worker         mRecordingOn = false;
359*38e8c45fSAndroid Build Coastguard Worker         ALOGI("Stopped Binder recording.");
360*38e8c45fSAndroid Build Coastguard Worker         return NO_ERROR;
361*38e8c45fSAndroid Build Coastguard Worker     } else {
362*38e8c45fSAndroid Build Coastguard Worker         ALOGI("Could not stop Binder recording. One is not in progress.");
363*38e8c45fSAndroid Build Coastguard Worker         return INVALID_OPERATION;
364*38e8c45fSAndroid Build Coastguard Worker     }
365*38e8c45fSAndroid Build Coastguard Worker }
366*38e8c45fSAndroid Build Coastguard Worker 
getInterfaceDescriptor() const367*38e8c45fSAndroid Build Coastguard Worker const String16& BBinder::getInterfaceDescriptor() const
368*38e8c45fSAndroid Build Coastguard Worker {
369*38e8c45fSAndroid Build Coastguard Worker     static StaticString16 sBBinder(u"BBinder");
370*38e8c45fSAndroid Build Coastguard Worker     ALOGW("Reached BBinder::getInterfaceDescriptor (this=%p). Override?", this);
371*38e8c45fSAndroid Build Coastguard Worker     return sBBinder;
372*38e8c45fSAndroid Build Coastguard Worker }
373*38e8c45fSAndroid Build Coastguard Worker 
374*38e8c45fSAndroid Build Coastguard Worker // NOLINTNEXTLINE(google-default-arguments)
transact(uint32_t code,const Parcel & data,Parcel * reply,uint32_t flags)375*38e8c45fSAndroid Build Coastguard Worker status_t BBinder::transact(
376*38e8c45fSAndroid Build Coastguard Worker     uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
377*38e8c45fSAndroid Build Coastguard Worker {
378*38e8c45fSAndroid Build Coastguard Worker     data.setDataPosition(0);
379*38e8c45fSAndroid Build Coastguard Worker 
380*38e8c45fSAndroid Build Coastguard Worker     if (reply != nullptr && (flags & FLAG_CLEAR_BUF)) {
381*38e8c45fSAndroid Build Coastguard Worker         reply->markSensitive();
382*38e8c45fSAndroid Build Coastguard Worker     }
383*38e8c45fSAndroid Build Coastguard Worker 
384*38e8c45fSAndroid Build Coastguard Worker     status_t err = NO_ERROR;
385*38e8c45fSAndroid Build Coastguard Worker     switch (code) {
386*38e8c45fSAndroid Build Coastguard Worker         case PING_TRANSACTION:
387*38e8c45fSAndroid Build Coastguard Worker             err = pingBinder();
388*38e8c45fSAndroid Build Coastguard Worker             break;
389*38e8c45fSAndroid Build Coastguard Worker         case START_RECORDING_TRANSACTION:
390*38e8c45fSAndroid Build Coastguard Worker             err = startRecordingTransactions(data);
391*38e8c45fSAndroid Build Coastguard Worker             break;
392*38e8c45fSAndroid Build Coastguard Worker         case STOP_RECORDING_TRANSACTION:
393*38e8c45fSAndroid Build Coastguard Worker             err = stopRecordingTransactions();
394*38e8c45fSAndroid Build Coastguard Worker             break;
395*38e8c45fSAndroid Build Coastguard Worker         case EXTENSION_TRANSACTION:
396*38e8c45fSAndroid Build Coastguard Worker             LOG_ALWAYS_FATAL_IF(reply == nullptr, "reply == nullptr");
397*38e8c45fSAndroid Build Coastguard Worker             err = reply->writeStrongBinder(getExtension());
398*38e8c45fSAndroid Build Coastguard Worker             break;
399*38e8c45fSAndroid Build Coastguard Worker         case DEBUG_PID_TRANSACTION:
400*38e8c45fSAndroid Build Coastguard Worker             LOG_ALWAYS_FATAL_IF(reply == nullptr, "reply == nullptr");
401*38e8c45fSAndroid Build Coastguard Worker             err = reply->writeInt32(getDebugPid());
402*38e8c45fSAndroid Build Coastguard Worker             break;
403*38e8c45fSAndroid Build Coastguard Worker         case SET_RPC_CLIENT_TRANSACTION: {
404*38e8c45fSAndroid Build Coastguard Worker             err = setRpcClientDebug(data);
405*38e8c45fSAndroid Build Coastguard Worker             break;
406*38e8c45fSAndroid Build Coastguard Worker         }
407*38e8c45fSAndroid Build Coastguard Worker         default:
408*38e8c45fSAndroid Build Coastguard Worker             err = onTransact(code, data, reply, flags);
409*38e8c45fSAndroid Build Coastguard Worker             break;
410*38e8c45fSAndroid Build Coastguard Worker     }
411*38e8c45fSAndroid Build Coastguard Worker 
412*38e8c45fSAndroid Build Coastguard Worker     // In case this is being transacted on in the same process.
413*38e8c45fSAndroid Build Coastguard Worker     if (reply != nullptr) {
414*38e8c45fSAndroid Build Coastguard Worker         reply->setDataPosition(0);
415*38e8c45fSAndroid Build Coastguard Worker         if (reply->dataSize() > LOG_REPLIES_OVER_SIZE) {
416*38e8c45fSAndroid Build Coastguard Worker             ALOGW("Large reply transaction of %zu bytes, interface descriptor %s, code %d",
417*38e8c45fSAndroid Build Coastguard Worker                   reply->dataSize(), String8(getInterfaceDescriptor()).c_str(), code);
418*38e8c45fSAndroid Build Coastguard Worker         }
419*38e8c45fSAndroid Build Coastguard Worker     }
420*38e8c45fSAndroid Build Coastguard Worker 
421*38e8c45fSAndroid Build Coastguard Worker     if (kEnableKernelIpc && mRecordingOn && code != START_RECORDING_TRANSACTION) [[unlikely]] {
422*38e8c45fSAndroid Build Coastguard Worker         Extras* e = mExtras.load(std::memory_order_acquire);
423*38e8c45fSAndroid Build Coastguard Worker         RpcMutexUniqueLock lock(e->mLock);
424*38e8c45fSAndroid Build Coastguard Worker         if (mRecordingOn) {
425*38e8c45fSAndroid Build Coastguard Worker             Parcel emptyReply;
426*38e8c45fSAndroid Build Coastguard Worker             timespec ts;
427*38e8c45fSAndroid Build Coastguard Worker             timespec_get(&ts, TIME_UTC);
428*38e8c45fSAndroid Build Coastguard Worker             auto transaction = android::binder::debug::RecordedTransaction::
429*38e8c45fSAndroid Build Coastguard Worker                     fromDetails(getInterfaceDescriptor(), code, flags, ts, data,
430*38e8c45fSAndroid Build Coastguard Worker                                 reply ? *reply : emptyReply, err);
431*38e8c45fSAndroid Build Coastguard Worker             if (transaction) {
432*38e8c45fSAndroid Build Coastguard Worker                 if (status_t err = transaction->dumpToFile(e->mRecordingFd); err != NO_ERROR) {
433*38e8c45fSAndroid Build Coastguard Worker                     ALOGI("Failed to dump RecordedTransaction to file with error %d", err);
434*38e8c45fSAndroid Build Coastguard Worker                 }
435*38e8c45fSAndroid Build Coastguard Worker             } else {
436*38e8c45fSAndroid Build Coastguard Worker                 ALOGI("Failed to create RecordedTransaction object.");
437*38e8c45fSAndroid Build Coastguard Worker             }
438*38e8c45fSAndroid Build Coastguard Worker         }
439*38e8c45fSAndroid Build Coastguard Worker     }
440*38e8c45fSAndroid Build Coastguard Worker 
441*38e8c45fSAndroid Build Coastguard Worker     return err;
442*38e8c45fSAndroid Build Coastguard Worker }
443*38e8c45fSAndroid Build Coastguard Worker 
444*38e8c45fSAndroid Build Coastguard Worker // NOLINTNEXTLINE(google-default-arguments)
linkToDeath(const sp<DeathRecipient> &,void *,uint32_t)445*38e8c45fSAndroid Build Coastguard Worker status_t BBinder::linkToDeath(
446*38e8c45fSAndroid Build Coastguard Worker     const sp<DeathRecipient>& /*recipient*/, void* /*cookie*/,
447*38e8c45fSAndroid Build Coastguard Worker     uint32_t /*flags*/)
448*38e8c45fSAndroid Build Coastguard Worker {
449*38e8c45fSAndroid Build Coastguard Worker     return INVALID_OPERATION;
450*38e8c45fSAndroid Build Coastguard Worker }
451*38e8c45fSAndroid Build Coastguard Worker 
452*38e8c45fSAndroid Build Coastguard Worker // NOLINTNEXTLINE(google-default-arguments)
unlinkToDeath(const wp<DeathRecipient> &,void *,uint32_t,wp<DeathRecipient> *)453*38e8c45fSAndroid Build Coastguard Worker status_t BBinder::unlinkToDeath(
454*38e8c45fSAndroid Build Coastguard Worker     const wp<DeathRecipient>& /*recipient*/, void* /*cookie*/,
455*38e8c45fSAndroid Build Coastguard Worker     uint32_t /*flags*/, wp<DeathRecipient>* /*outRecipient*/)
456*38e8c45fSAndroid Build Coastguard Worker {
457*38e8c45fSAndroid Build Coastguard Worker     return INVALID_OPERATION;
458*38e8c45fSAndroid Build Coastguard Worker }
459*38e8c45fSAndroid Build Coastguard Worker 
dump(int,const Vector<String16> &)460*38e8c45fSAndroid Build Coastguard Worker status_t BBinder::dump(int /*fd*/, const Vector<String16>& /*args*/)
461*38e8c45fSAndroid Build Coastguard Worker {
462*38e8c45fSAndroid Build Coastguard Worker     return NO_ERROR;
463*38e8c45fSAndroid Build Coastguard Worker }
464*38e8c45fSAndroid Build Coastguard Worker 
attachObject(const void * objectID,void * object,void * cleanupCookie,object_cleanup_func func)465*38e8c45fSAndroid Build Coastguard Worker void* BBinder::attachObject(const void* objectID, void* object, void* cleanupCookie,
466*38e8c45fSAndroid Build Coastguard Worker                             object_cleanup_func func) {
467*38e8c45fSAndroid Build Coastguard Worker     Extras* e = getOrCreateExtras();
468*38e8c45fSAndroid Build Coastguard Worker     LOG_ALWAYS_FATAL_IF(!e, "no memory");
469*38e8c45fSAndroid Build Coastguard Worker 
470*38e8c45fSAndroid Build Coastguard Worker     RpcMutexUniqueLock _l(e->mLock);
471*38e8c45fSAndroid Build Coastguard Worker     return e->mObjects.attach(objectID, object, cleanupCookie, func);
472*38e8c45fSAndroid Build Coastguard Worker }
473*38e8c45fSAndroid Build Coastguard Worker 
findObject(const void * objectID) const474*38e8c45fSAndroid Build Coastguard Worker void* BBinder::findObject(const void* objectID) const
475*38e8c45fSAndroid Build Coastguard Worker {
476*38e8c45fSAndroid Build Coastguard Worker     Extras* e = mExtras.load(std::memory_order_acquire);
477*38e8c45fSAndroid Build Coastguard Worker     if (!e) return nullptr;
478*38e8c45fSAndroid Build Coastguard Worker 
479*38e8c45fSAndroid Build Coastguard Worker     RpcMutexUniqueLock _l(e->mLock);
480*38e8c45fSAndroid Build Coastguard Worker     return e->mObjects.find(objectID);
481*38e8c45fSAndroid Build Coastguard Worker }
482*38e8c45fSAndroid Build Coastguard Worker 
detachObject(const void * objectID)483*38e8c45fSAndroid Build Coastguard Worker void* BBinder::detachObject(const void* objectID) {
484*38e8c45fSAndroid Build Coastguard Worker     Extras* e = mExtras.load(std::memory_order_acquire);
485*38e8c45fSAndroid Build Coastguard Worker     if (!e) return nullptr;
486*38e8c45fSAndroid Build Coastguard Worker 
487*38e8c45fSAndroid Build Coastguard Worker     RpcMutexUniqueLock _l(e->mLock);
488*38e8c45fSAndroid Build Coastguard Worker     return e->mObjects.detach(objectID);
489*38e8c45fSAndroid Build Coastguard Worker }
490*38e8c45fSAndroid Build Coastguard Worker 
withLock(const std::function<void ()> & doWithLock)491*38e8c45fSAndroid Build Coastguard Worker void BBinder::withLock(const std::function<void()>& doWithLock) {
492*38e8c45fSAndroid Build Coastguard Worker     Extras* e = getOrCreateExtras();
493*38e8c45fSAndroid Build Coastguard Worker     LOG_ALWAYS_FATAL_IF(!e, "no memory");
494*38e8c45fSAndroid Build Coastguard Worker 
495*38e8c45fSAndroid Build Coastguard Worker     RpcMutexUniqueLock _l(e->mLock);
496*38e8c45fSAndroid Build Coastguard Worker     doWithLock();
497*38e8c45fSAndroid Build Coastguard Worker }
498*38e8c45fSAndroid Build Coastguard Worker 
lookupOrCreateWeak(const void * objectID,object_make_func make,const void * makeArgs)499*38e8c45fSAndroid Build Coastguard Worker sp<IBinder> BBinder::lookupOrCreateWeak(const void* objectID, object_make_func make,
500*38e8c45fSAndroid Build Coastguard Worker                                         const void* makeArgs) {
501*38e8c45fSAndroid Build Coastguard Worker     Extras* e = getOrCreateExtras();
502*38e8c45fSAndroid Build Coastguard Worker     LOG_ALWAYS_FATAL_IF(!e, "no memory");
503*38e8c45fSAndroid Build Coastguard Worker     RpcMutexUniqueLock _l(e->mLock);
504*38e8c45fSAndroid Build Coastguard Worker     return e->mObjects.lookupOrCreateWeak(objectID, make, makeArgs);
505*38e8c45fSAndroid Build Coastguard Worker }
506*38e8c45fSAndroid Build Coastguard Worker 
localBinder()507*38e8c45fSAndroid Build Coastguard Worker BBinder* BBinder::localBinder()
508*38e8c45fSAndroid Build Coastguard Worker {
509*38e8c45fSAndroid Build Coastguard Worker     return this;
510*38e8c45fSAndroid Build Coastguard Worker }
511*38e8c45fSAndroid Build Coastguard Worker 
isRequestingSid()512*38e8c45fSAndroid Build Coastguard Worker bool BBinder::isRequestingSid()
513*38e8c45fSAndroid Build Coastguard Worker {
514*38e8c45fSAndroid Build Coastguard Worker     Extras* e = mExtras.load(std::memory_order_acquire);
515*38e8c45fSAndroid Build Coastguard Worker 
516*38e8c45fSAndroid Build Coastguard Worker     return e && e->mRequestingSid;
517*38e8c45fSAndroid Build Coastguard Worker }
518*38e8c45fSAndroid Build Coastguard Worker 
setRequestingSid(bool requestingSid)519*38e8c45fSAndroid Build Coastguard Worker void BBinder::setRequestingSid(bool requestingSid)
520*38e8c45fSAndroid Build Coastguard Worker {
521*38e8c45fSAndroid Build Coastguard Worker     LOG_ALWAYS_FATAL_IF(mParceled,
522*38e8c45fSAndroid Build Coastguard Worker                         "setRequestingSid() should not be called after a binder object "
523*38e8c45fSAndroid Build Coastguard Worker                         "is parceled/sent to another process");
524*38e8c45fSAndroid Build Coastguard Worker 
525*38e8c45fSAndroid Build Coastguard Worker     Extras* e = mExtras.load(std::memory_order_acquire);
526*38e8c45fSAndroid Build Coastguard Worker 
527*38e8c45fSAndroid Build Coastguard Worker     if (!e) {
528*38e8c45fSAndroid Build Coastguard Worker         // default is false. Most things don't need sids, so avoiding allocations when possible.
529*38e8c45fSAndroid Build Coastguard Worker         if (!requestingSid) {
530*38e8c45fSAndroid Build Coastguard Worker             return;
531*38e8c45fSAndroid Build Coastguard Worker         }
532*38e8c45fSAndroid Build Coastguard Worker 
533*38e8c45fSAndroid Build Coastguard Worker         e = getOrCreateExtras();
534*38e8c45fSAndroid Build Coastguard Worker         if (!e) return; // out of memory
535*38e8c45fSAndroid Build Coastguard Worker     }
536*38e8c45fSAndroid Build Coastguard Worker 
537*38e8c45fSAndroid Build Coastguard Worker     e->mRequestingSid = requestingSid;
538*38e8c45fSAndroid Build Coastguard Worker }
539*38e8c45fSAndroid Build Coastguard Worker 
getExtension()540*38e8c45fSAndroid Build Coastguard Worker sp<IBinder> BBinder::getExtension() {
541*38e8c45fSAndroid Build Coastguard Worker     Extras* e = mExtras.load(std::memory_order_acquire);
542*38e8c45fSAndroid Build Coastguard Worker     if (e == nullptr) return nullptr;
543*38e8c45fSAndroid Build Coastguard Worker     return e->mExtension;
544*38e8c45fSAndroid Build Coastguard Worker }
545*38e8c45fSAndroid Build Coastguard Worker 
546*38e8c45fSAndroid Build Coastguard Worker #ifdef __linux__
setMinSchedulerPolicy(int policy,int priority)547*38e8c45fSAndroid Build Coastguard Worker void BBinder::setMinSchedulerPolicy(int policy, int priority) {
548*38e8c45fSAndroid Build Coastguard Worker     LOG_ALWAYS_FATAL_IF(mParceled,
549*38e8c45fSAndroid Build Coastguard Worker                         "setMinSchedulerPolicy() should not be called after a binder object "
550*38e8c45fSAndroid Build Coastguard Worker                         "is parceled/sent to another process");
551*38e8c45fSAndroid Build Coastguard Worker 
552*38e8c45fSAndroid Build Coastguard Worker     switch (policy) {
553*38e8c45fSAndroid Build Coastguard Worker     case SCHED_NORMAL:
554*38e8c45fSAndroid Build Coastguard Worker       LOG_ALWAYS_FATAL_IF(priority < -20 || priority > 19, "Invalid priority for SCHED_NORMAL: %d", priority);
555*38e8c45fSAndroid Build Coastguard Worker       break;
556*38e8c45fSAndroid Build Coastguard Worker     case SCHED_RR:
557*38e8c45fSAndroid Build Coastguard Worker     case SCHED_FIFO:
558*38e8c45fSAndroid Build Coastguard Worker       LOG_ALWAYS_FATAL_IF(priority < 1 || priority > 99, "Invalid priority for sched %d: %d", policy, priority);
559*38e8c45fSAndroid Build Coastguard Worker       break;
560*38e8c45fSAndroid Build Coastguard Worker     default:
561*38e8c45fSAndroid Build Coastguard Worker       LOG_ALWAYS_FATAL("Unrecognized scheduling policy: %d", policy);
562*38e8c45fSAndroid Build Coastguard Worker     }
563*38e8c45fSAndroid Build Coastguard Worker 
564*38e8c45fSAndroid Build Coastguard Worker     Extras* e = mExtras.load(std::memory_order_acquire);
565*38e8c45fSAndroid Build Coastguard Worker 
566*38e8c45fSAndroid Build Coastguard Worker     if (e == nullptr) {
567*38e8c45fSAndroid Build Coastguard Worker         // Avoid allocations if called with default.
568*38e8c45fSAndroid Build Coastguard Worker         if (policy == SCHED_NORMAL && priority == 0) {
569*38e8c45fSAndroid Build Coastguard Worker             return;
570*38e8c45fSAndroid Build Coastguard Worker         }
571*38e8c45fSAndroid Build Coastguard Worker 
572*38e8c45fSAndroid Build Coastguard Worker         e = getOrCreateExtras();
573*38e8c45fSAndroid Build Coastguard Worker         if (!e) return; // out of memory
574*38e8c45fSAndroid Build Coastguard Worker     }
575*38e8c45fSAndroid Build Coastguard Worker 
576*38e8c45fSAndroid Build Coastguard Worker     e->mPolicy = policy;
577*38e8c45fSAndroid Build Coastguard Worker     e->mPriority = priority;
578*38e8c45fSAndroid Build Coastguard Worker }
579*38e8c45fSAndroid Build Coastguard Worker 
getMinSchedulerPolicy()580*38e8c45fSAndroid Build Coastguard Worker int BBinder::getMinSchedulerPolicy() {
581*38e8c45fSAndroid Build Coastguard Worker     Extras* e = mExtras.load(std::memory_order_acquire);
582*38e8c45fSAndroid Build Coastguard Worker     if (e == nullptr) return SCHED_NORMAL;
583*38e8c45fSAndroid Build Coastguard Worker     return e->mPolicy;
584*38e8c45fSAndroid Build Coastguard Worker }
585*38e8c45fSAndroid Build Coastguard Worker 
getMinSchedulerPriority()586*38e8c45fSAndroid Build Coastguard Worker int BBinder::getMinSchedulerPriority() {
587*38e8c45fSAndroid Build Coastguard Worker     Extras* e = mExtras.load(std::memory_order_acquire);
588*38e8c45fSAndroid Build Coastguard Worker     if (e == nullptr) return 0;
589*38e8c45fSAndroid Build Coastguard Worker     return e->mPriority;
590*38e8c45fSAndroid Build Coastguard Worker }
591*38e8c45fSAndroid Build Coastguard Worker #endif // __linux__
592*38e8c45fSAndroid Build Coastguard Worker 
isInheritRt()593*38e8c45fSAndroid Build Coastguard Worker bool BBinder::isInheritRt() {
594*38e8c45fSAndroid Build Coastguard Worker     Extras* e = mExtras.load(std::memory_order_acquire);
595*38e8c45fSAndroid Build Coastguard Worker 
596*38e8c45fSAndroid Build Coastguard Worker     return e && e->mInheritRt;
597*38e8c45fSAndroid Build Coastguard Worker }
598*38e8c45fSAndroid Build Coastguard Worker 
setInheritRt(bool inheritRt)599*38e8c45fSAndroid Build Coastguard Worker void BBinder::setInheritRt(bool inheritRt) {
600*38e8c45fSAndroid Build Coastguard Worker     LOG_ALWAYS_FATAL_IF(mParceled,
601*38e8c45fSAndroid Build Coastguard Worker                         "setInheritRt() should not be called after a binder object "
602*38e8c45fSAndroid Build Coastguard Worker                         "is parceled/sent to another process");
603*38e8c45fSAndroid Build Coastguard Worker 
604*38e8c45fSAndroid Build Coastguard Worker     Extras* e = mExtras.load(std::memory_order_acquire);
605*38e8c45fSAndroid Build Coastguard Worker 
606*38e8c45fSAndroid Build Coastguard Worker     if (!e) {
607*38e8c45fSAndroid Build Coastguard Worker         if (!inheritRt) {
608*38e8c45fSAndroid Build Coastguard Worker             return;
609*38e8c45fSAndroid Build Coastguard Worker         }
610*38e8c45fSAndroid Build Coastguard Worker 
611*38e8c45fSAndroid Build Coastguard Worker         e = getOrCreateExtras();
612*38e8c45fSAndroid Build Coastguard Worker         if (!e) return; // out of memory
613*38e8c45fSAndroid Build Coastguard Worker     }
614*38e8c45fSAndroid Build Coastguard Worker 
615*38e8c45fSAndroid Build Coastguard Worker     e->mInheritRt = inheritRt;
616*38e8c45fSAndroid Build Coastguard Worker }
617*38e8c45fSAndroid Build Coastguard Worker 
getDebugPid()618*38e8c45fSAndroid Build Coastguard Worker pid_t BBinder::getDebugPid() {
619*38e8c45fSAndroid Build Coastguard Worker #ifdef __linux__
620*38e8c45fSAndroid Build Coastguard Worker     return getpid();
621*38e8c45fSAndroid Build Coastguard Worker #else
622*38e8c45fSAndroid Build Coastguard Worker     // TODO: handle other OSes
623*38e8c45fSAndroid Build Coastguard Worker     return 0;
624*38e8c45fSAndroid Build Coastguard Worker #endif // __linux__
625*38e8c45fSAndroid Build Coastguard Worker }
626*38e8c45fSAndroid Build Coastguard Worker 
setExtension(const sp<IBinder> & extension)627*38e8c45fSAndroid Build Coastguard Worker void BBinder::setExtension(const sp<IBinder>& extension) {
628*38e8c45fSAndroid Build Coastguard Worker     LOG_ALWAYS_FATAL_IF(mParceled,
629*38e8c45fSAndroid Build Coastguard Worker                         "setExtension() should not be called after a binder object "
630*38e8c45fSAndroid Build Coastguard Worker                         "is parceled/sent to another process");
631*38e8c45fSAndroid Build Coastguard Worker 
632*38e8c45fSAndroid Build Coastguard Worker     Extras* e = getOrCreateExtras();
633*38e8c45fSAndroid Build Coastguard Worker     e->mExtension = extension;
634*38e8c45fSAndroid Build Coastguard Worker }
635*38e8c45fSAndroid Build Coastguard Worker 
wasParceled()636*38e8c45fSAndroid Build Coastguard Worker bool BBinder::wasParceled() {
637*38e8c45fSAndroid Build Coastguard Worker     return mParceled;
638*38e8c45fSAndroid Build Coastguard Worker }
639*38e8c45fSAndroid Build Coastguard Worker 
setParceled()640*38e8c45fSAndroid Build Coastguard Worker void BBinder::setParceled() {
641*38e8c45fSAndroid Build Coastguard Worker     mParceled = true;
642*38e8c45fSAndroid Build Coastguard Worker }
643*38e8c45fSAndroid Build Coastguard Worker 
setRpcClientDebug(const Parcel & data)644*38e8c45fSAndroid Build Coastguard Worker status_t BBinder::setRpcClientDebug(const Parcel& data) {
645*38e8c45fSAndroid Build Coastguard Worker     if (!kEnableRpcDevServers) {
646*38e8c45fSAndroid Build Coastguard Worker         ALOGW("%s: disallowed because RPC is not enabled", __PRETTY_FUNCTION__);
647*38e8c45fSAndroid Build Coastguard Worker         return INVALID_OPERATION;
648*38e8c45fSAndroid Build Coastguard Worker     }
649*38e8c45fSAndroid Build Coastguard Worker     if (!kEnableKernelIpc) {
650*38e8c45fSAndroid Build Coastguard Worker         ALOGW("setRpcClientDebug disallowed because kernel binder is not enabled");
651*38e8c45fSAndroid Build Coastguard Worker         return INVALID_OPERATION;
652*38e8c45fSAndroid Build Coastguard Worker     }
653*38e8c45fSAndroid Build Coastguard Worker     uid_t uid = IPCThreadState::self()->getCallingUid();
654*38e8c45fSAndroid Build Coastguard Worker     if (uid != kUidRoot) {
655*38e8c45fSAndroid Build Coastguard Worker         ALOGE("%s: not allowed because client %" PRIu32 " is not root", __PRETTY_FUNCTION__, uid);
656*38e8c45fSAndroid Build Coastguard Worker         return PERMISSION_DENIED;
657*38e8c45fSAndroid Build Coastguard Worker     }
658*38e8c45fSAndroid Build Coastguard Worker     status_t status;
659*38e8c45fSAndroid Build Coastguard Worker     bool hasSocketFd;
660*38e8c45fSAndroid Build Coastguard Worker     unique_fd clientFd;
661*38e8c45fSAndroid Build Coastguard Worker 
662*38e8c45fSAndroid Build Coastguard Worker     if (status = data.readBool(&hasSocketFd); status != OK) return status;
663*38e8c45fSAndroid Build Coastguard Worker     if (hasSocketFd) {
664*38e8c45fSAndroid Build Coastguard Worker         if (status = data.readUniqueFileDescriptor(&clientFd); status != OK) return status;
665*38e8c45fSAndroid Build Coastguard Worker     }
666*38e8c45fSAndroid Build Coastguard Worker     sp<IBinder> keepAliveBinder;
667*38e8c45fSAndroid Build Coastguard Worker     if (status = data.readNullableStrongBinder(&keepAliveBinder); status != OK) return status;
668*38e8c45fSAndroid Build Coastguard Worker 
669*38e8c45fSAndroid Build Coastguard Worker     return setRpcClientDebug(std::move(clientFd), keepAliveBinder);
670*38e8c45fSAndroid Build Coastguard Worker }
671*38e8c45fSAndroid Build Coastguard Worker 
setRpcClientDebug(unique_fd socketFd,const sp<IBinder> & keepAliveBinder)672*38e8c45fSAndroid Build Coastguard Worker status_t BBinder::setRpcClientDebug(unique_fd socketFd, const sp<IBinder>& keepAliveBinder) {
673*38e8c45fSAndroid Build Coastguard Worker     if (!kEnableRpcDevServers) {
674*38e8c45fSAndroid Build Coastguard Worker         ALOGW("%s: disallowed because RPC is not enabled", __PRETTY_FUNCTION__);
675*38e8c45fSAndroid Build Coastguard Worker         return INVALID_OPERATION;
676*38e8c45fSAndroid Build Coastguard Worker     }
677*38e8c45fSAndroid Build Coastguard Worker     if (!kEnableKernelIpc) {
678*38e8c45fSAndroid Build Coastguard Worker         ALOGW("setRpcClientDebug disallowed because kernel binder is not enabled");
679*38e8c45fSAndroid Build Coastguard Worker         return INVALID_OPERATION;
680*38e8c45fSAndroid Build Coastguard Worker     }
681*38e8c45fSAndroid Build Coastguard Worker 
682*38e8c45fSAndroid Build Coastguard Worker     const int socketFdForPrint = socketFd.get();
683*38e8c45fSAndroid Build Coastguard Worker     LOG_RPC_DETAIL("%s(fd=%d)", __PRETTY_FUNCTION__, socketFdForPrint);
684*38e8c45fSAndroid Build Coastguard Worker 
685*38e8c45fSAndroid Build Coastguard Worker     if (!socketFd.ok()) {
686*38e8c45fSAndroid Build Coastguard Worker         ALOGE("%s: No socket FD provided.", __PRETTY_FUNCTION__);
687*38e8c45fSAndroid Build Coastguard Worker         return BAD_VALUE;
688*38e8c45fSAndroid Build Coastguard Worker     }
689*38e8c45fSAndroid Build Coastguard Worker 
690*38e8c45fSAndroid Build Coastguard Worker     if (keepAliveBinder == nullptr) {
691*38e8c45fSAndroid Build Coastguard Worker         ALOGE("%s: No keepAliveBinder provided.", __PRETTY_FUNCTION__);
692*38e8c45fSAndroid Build Coastguard Worker         return UNEXPECTED_NULL;
693*38e8c45fSAndroid Build Coastguard Worker     }
694*38e8c45fSAndroid Build Coastguard Worker 
695*38e8c45fSAndroid Build Coastguard Worker     size_t binderThreadPoolMaxCount = ProcessState::self()->getThreadPoolMaxTotalThreadCount();
696*38e8c45fSAndroid Build Coastguard Worker     if (binderThreadPoolMaxCount <= 1) {
697*38e8c45fSAndroid Build Coastguard Worker         ALOGE("%s: ProcessState thread pool max count is %zu. RPC is disabled for this service "
698*38e8c45fSAndroid Build Coastguard Worker               "because RPC requires the service to support multithreading.",
699*38e8c45fSAndroid Build Coastguard Worker               __PRETTY_FUNCTION__, binderThreadPoolMaxCount);
700*38e8c45fSAndroid Build Coastguard Worker         return INVALID_OPERATION;
701*38e8c45fSAndroid Build Coastguard Worker     }
702*38e8c45fSAndroid Build Coastguard Worker 
703*38e8c45fSAndroid Build Coastguard Worker     // Weak ref to avoid circular dependency:
704*38e8c45fSAndroid Build Coastguard Worker     // BBinder -> RpcServerLink ----> RpcServer -X-> BBinder
705*38e8c45fSAndroid Build Coastguard Worker     //                          `-X-> BBinder
706*38e8c45fSAndroid Build Coastguard Worker     auto weakThis = wp<BBinder>::fromExisting(this);
707*38e8c45fSAndroid Build Coastguard Worker 
708*38e8c45fSAndroid Build Coastguard Worker     Extras* e = getOrCreateExtras();
709*38e8c45fSAndroid Build Coastguard Worker     RpcMutexUniqueLock _l(e->mLock);
710*38e8c45fSAndroid Build Coastguard Worker     auto rpcServer = RpcServer::make();
711*38e8c45fSAndroid Build Coastguard Worker     LOG_ALWAYS_FATAL_IF(rpcServer == nullptr, "RpcServer::make returns null");
712*38e8c45fSAndroid Build Coastguard Worker     auto link = sp<RpcServerLink>::make(rpcServer, keepAliveBinder, weakThis);
713*38e8c45fSAndroid Build Coastguard Worker     if (auto status = keepAliveBinder->linkToDeath(link, nullptr, 0); status != OK) {
714*38e8c45fSAndroid Build Coastguard Worker         ALOGE("%s: keepAliveBinder->linkToDeath returns %s", __PRETTY_FUNCTION__,
715*38e8c45fSAndroid Build Coastguard Worker               statusToString(status).c_str());
716*38e8c45fSAndroid Build Coastguard Worker         return status;
717*38e8c45fSAndroid Build Coastguard Worker     }
718*38e8c45fSAndroid Build Coastguard Worker     rpcServer->setRootObjectWeak(weakThis);
719*38e8c45fSAndroid Build Coastguard Worker     if (auto status = rpcServer->setupExternalServer(std::move(socketFd)); status != OK) {
720*38e8c45fSAndroid Build Coastguard Worker         return status;
721*38e8c45fSAndroid Build Coastguard Worker     }
722*38e8c45fSAndroid Build Coastguard Worker     rpcServer->setMaxThreads(binderThreadPoolMaxCount);
723*38e8c45fSAndroid Build Coastguard Worker     ALOGI("RpcBinder: Started Binder debug on %s", String8(getInterfaceDescriptor()).c_str());
724*38e8c45fSAndroid Build Coastguard Worker     rpcServer->start();
725*38e8c45fSAndroid Build Coastguard Worker     e->mRpcServerLinks.emplace(link);
726*38e8c45fSAndroid Build Coastguard Worker     LOG_RPC_DETAIL("%s(fd=%d) successful", __PRETTY_FUNCTION__, socketFdForPrint);
727*38e8c45fSAndroid Build Coastguard Worker     return OK;
728*38e8c45fSAndroid Build Coastguard Worker }
729*38e8c45fSAndroid Build Coastguard Worker 
removeRpcServerLink(const sp<RpcServerLink> & link)730*38e8c45fSAndroid Build Coastguard Worker void BBinder::removeRpcServerLink(const sp<RpcServerLink>& link) {
731*38e8c45fSAndroid Build Coastguard Worker     Extras* e = mExtras.load(std::memory_order_acquire);
732*38e8c45fSAndroid Build Coastguard Worker     if (!e) return;
733*38e8c45fSAndroid Build Coastguard Worker     RpcMutexUniqueLock _l(e->mLock);
734*38e8c45fSAndroid Build Coastguard Worker     (void)e->mRpcServerLinks.erase(link);
735*38e8c45fSAndroid Build Coastguard Worker }
736*38e8c45fSAndroid Build Coastguard Worker 
~BBinder()737*38e8c45fSAndroid Build Coastguard Worker BBinder::~BBinder()
738*38e8c45fSAndroid Build Coastguard Worker {
739*38e8c45fSAndroid Build Coastguard Worker     if (!wasParceled()) {
740*38e8c45fSAndroid Build Coastguard Worker         if (getExtension()) {
741*38e8c45fSAndroid Build Coastguard Worker             ALOGW("Binder %p destroyed with extension attached before being parceled.", this);
742*38e8c45fSAndroid Build Coastguard Worker         }
743*38e8c45fSAndroid Build Coastguard Worker         if (isRequestingSid()) {
744*38e8c45fSAndroid Build Coastguard Worker             ALOGW("Binder %p destroyed when requesting SID before being parceled.", this);
745*38e8c45fSAndroid Build Coastguard Worker         }
746*38e8c45fSAndroid Build Coastguard Worker         if (isInheritRt()) {
747*38e8c45fSAndroid Build Coastguard Worker             ALOGW("Binder %p destroyed after setInheritRt before being parceled.", this);
748*38e8c45fSAndroid Build Coastguard Worker         }
749*38e8c45fSAndroid Build Coastguard Worker #ifdef __linux__
750*38e8c45fSAndroid Build Coastguard Worker         if (getMinSchedulerPolicy() != SCHED_NORMAL) {
751*38e8c45fSAndroid Build Coastguard Worker             ALOGW("Binder %p destroyed after setMinSchedulerPolicy before being parceled.", this);
752*38e8c45fSAndroid Build Coastguard Worker         }
753*38e8c45fSAndroid Build Coastguard Worker         if (getMinSchedulerPriority() != 0) {
754*38e8c45fSAndroid Build Coastguard Worker             ALOGW("Binder %p destroyed after setMinSchedulerPolicy before being parceled.", this);
755*38e8c45fSAndroid Build Coastguard Worker         }
756*38e8c45fSAndroid Build Coastguard Worker #endif // __linux__
757*38e8c45fSAndroid Build Coastguard Worker     }
758*38e8c45fSAndroid Build Coastguard Worker 
759*38e8c45fSAndroid Build Coastguard Worker     Extras* e = mExtras.load(std::memory_order_relaxed);
760*38e8c45fSAndroid Build Coastguard Worker     if (e) delete e;
761*38e8c45fSAndroid Build Coastguard Worker }
762*38e8c45fSAndroid Build Coastguard Worker 
763*38e8c45fSAndroid Build Coastguard Worker 
764*38e8c45fSAndroid Build Coastguard Worker // NOLINTNEXTLINE(google-default-arguments)
onTransact(uint32_t code,const Parcel & data,Parcel * reply,uint32_t)765*38e8c45fSAndroid Build Coastguard Worker status_t BBinder::onTransact(
766*38e8c45fSAndroid Build Coastguard Worker     uint32_t code, const Parcel& data, Parcel* reply, uint32_t /*flags*/)
767*38e8c45fSAndroid Build Coastguard Worker {
768*38e8c45fSAndroid Build Coastguard Worker     switch (code) {
769*38e8c45fSAndroid Build Coastguard Worker         case INTERFACE_TRANSACTION:
770*38e8c45fSAndroid Build Coastguard Worker             LOG_ALWAYS_FATAL_IF(reply == nullptr, "reply == nullptr");
771*38e8c45fSAndroid Build Coastguard Worker             reply->writeString16(getInterfaceDescriptor());
772*38e8c45fSAndroid Build Coastguard Worker             return NO_ERROR;
773*38e8c45fSAndroid Build Coastguard Worker 
774*38e8c45fSAndroid Build Coastguard Worker         case DUMP_TRANSACTION: {
775*38e8c45fSAndroid Build Coastguard Worker             int fd = data.readFileDescriptor();
776*38e8c45fSAndroid Build Coastguard Worker             int argc = data.readInt32();
777*38e8c45fSAndroid Build Coastguard Worker             Vector<String16> args;
778*38e8c45fSAndroid Build Coastguard Worker             for (int i = 0; i < argc && data.dataAvail() > 0; i++) {
779*38e8c45fSAndroid Build Coastguard Worker                args.add(data.readString16());
780*38e8c45fSAndroid Build Coastguard Worker             }
781*38e8c45fSAndroid Build Coastguard Worker             return dump(fd, args);
782*38e8c45fSAndroid Build Coastguard Worker         }
783*38e8c45fSAndroid Build Coastguard Worker 
784*38e8c45fSAndroid Build Coastguard Worker         case SHELL_COMMAND_TRANSACTION: {
785*38e8c45fSAndroid Build Coastguard Worker             int in = data.readFileDescriptor();
786*38e8c45fSAndroid Build Coastguard Worker             int out = data.readFileDescriptor();
787*38e8c45fSAndroid Build Coastguard Worker             int err = data.readFileDescriptor();
788*38e8c45fSAndroid Build Coastguard Worker             int argc = data.readInt32();
789*38e8c45fSAndroid Build Coastguard Worker             Vector<String16> args;
790*38e8c45fSAndroid Build Coastguard Worker             for (int i = 0; i < argc && data.dataAvail() > 0; i++) {
791*38e8c45fSAndroid Build Coastguard Worker                args.add(data.readString16());
792*38e8c45fSAndroid Build Coastguard Worker             }
793*38e8c45fSAndroid Build Coastguard Worker             sp<IBinder> shellCallbackBinder = data.readStrongBinder();
794*38e8c45fSAndroid Build Coastguard Worker             sp<IResultReceiver> resultReceiver = IResultReceiver::asInterface(
795*38e8c45fSAndroid Build Coastguard Worker                     data.readStrongBinder());
796*38e8c45fSAndroid Build Coastguard Worker 
797*38e8c45fSAndroid Build Coastguard Worker             // XXX can't add virtuals until binaries are updated.
798*38e8c45fSAndroid Build Coastguard Worker             // sp<IShellCallback> shellCallback = IShellCallback::asInterface(
799*38e8c45fSAndroid Build Coastguard Worker             //        shellCallbackBinder);
800*38e8c45fSAndroid Build Coastguard Worker             // return shellCommand(in, out, err, args, resultReceiver);
801*38e8c45fSAndroid Build Coastguard Worker             (void)in;
802*38e8c45fSAndroid Build Coastguard Worker             (void)out;
803*38e8c45fSAndroid Build Coastguard Worker             (void)err;
804*38e8c45fSAndroid Build Coastguard Worker 
805*38e8c45fSAndroid Build Coastguard Worker             if (resultReceiver != nullptr) {
806*38e8c45fSAndroid Build Coastguard Worker                 resultReceiver->send(INVALID_OPERATION);
807*38e8c45fSAndroid Build Coastguard Worker             }
808*38e8c45fSAndroid Build Coastguard Worker 
809*38e8c45fSAndroid Build Coastguard Worker             return NO_ERROR;
810*38e8c45fSAndroid Build Coastguard Worker         }
811*38e8c45fSAndroid Build Coastguard Worker 
812*38e8c45fSAndroid Build Coastguard Worker         case SYSPROPS_TRANSACTION: {
813*38e8c45fSAndroid Build Coastguard Worker             if (!binder::os::report_sysprop_change()) return INVALID_OPERATION;
814*38e8c45fSAndroid Build Coastguard Worker             return NO_ERROR;
815*38e8c45fSAndroid Build Coastguard Worker         }
816*38e8c45fSAndroid Build Coastguard Worker 
817*38e8c45fSAndroid Build Coastguard Worker         default:
818*38e8c45fSAndroid Build Coastguard Worker             return UNKNOWN_TRANSACTION;
819*38e8c45fSAndroid Build Coastguard Worker     }
820*38e8c45fSAndroid Build Coastguard Worker }
821*38e8c45fSAndroid Build Coastguard Worker 
getOrCreateExtras()822*38e8c45fSAndroid Build Coastguard Worker BBinder::Extras* BBinder::getOrCreateExtras()
823*38e8c45fSAndroid Build Coastguard Worker {
824*38e8c45fSAndroid Build Coastguard Worker     Extras* e = mExtras.load(std::memory_order_acquire);
825*38e8c45fSAndroid Build Coastguard Worker 
826*38e8c45fSAndroid Build Coastguard Worker     if (!e) {
827*38e8c45fSAndroid Build Coastguard Worker         e = new Extras;
828*38e8c45fSAndroid Build Coastguard Worker         Extras* expected = nullptr;
829*38e8c45fSAndroid Build Coastguard Worker         if (!mExtras.compare_exchange_strong(expected, e,
830*38e8c45fSAndroid Build Coastguard Worker                                              std::memory_order_release,
831*38e8c45fSAndroid Build Coastguard Worker                                              std::memory_order_acquire)) {
832*38e8c45fSAndroid Build Coastguard Worker             delete e;
833*38e8c45fSAndroid Build Coastguard Worker             e = expected;  // Filled in by CAS
834*38e8c45fSAndroid Build Coastguard Worker         }
835*38e8c45fSAndroid Build Coastguard Worker         if (e == nullptr) return nullptr; // out of memory
836*38e8c45fSAndroid Build Coastguard Worker     }
837*38e8c45fSAndroid Build Coastguard Worker 
838*38e8c45fSAndroid Build Coastguard Worker     return e;
839*38e8c45fSAndroid Build Coastguard Worker }
840*38e8c45fSAndroid Build Coastguard Worker 
841*38e8c45fSAndroid Build Coastguard Worker // ---------------------------------------------------------------------------
842*38e8c45fSAndroid Build Coastguard Worker 
843*38e8c45fSAndroid Build Coastguard Worker enum {
844*38e8c45fSAndroid Build Coastguard Worker     // This is used to transfer ownership of the remote binder from
845*38e8c45fSAndroid Build Coastguard Worker     // the BpRefBase object holding it (when it is constructed), to the
846*38e8c45fSAndroid Build Coastguard Worker     // owner of the BpRefBase object when it first acquires that BpRefBase.
847*38e8c45fSAndroid Build Coastguard Worker     kRemoteAcquired = 0x00000001
848*38e8c45fSAndroid Build Coastguard Worker };
849*38e8c45fSAndroid Build Coastguard Worker 
BpRefBase(const sp<IBinder> & o)850*38e8c45fSAndroid Build Coastguard Worker BpRefBase::BpRefBase(const sp<IBinder>& o)
851*38e8c45fSAndroid Build Coastguard Worker     : mRemote(o.get()), mRefs(nullptr), mState(0)
852*38e8c45fSAndroid Build Coastguard Worker {
853*38e8c45fSAndroid Build Coastguard Worker     extendObjectLifetime(OBJECT_LIFETIME_WEAK);
854*38e8c45fSAndroid Build Coastguard Worker 
855*38e8c45fSAndroid Build Coastguard Worker     if (mRemote) {
856*38e8c45fSAndroid Build Coastguard Worker         mRemote->incStrong(this);           // Removed on first IncStrong().
857*38e8c45fSAndroid Build Coastguard Worker         mRefs = mRemote->createWeak(this);  // Held for our entire lifetime.
858*38e8c45fSAndroid Build Coastguard Worker     }
859*38e8c45fSAndroid Build Coastguard Worker }
860*38e8c45fSAndroid Build Coastguard Worker 
~BpRefBase()861*38e8c45fSAndroid Build Coastguard Worker BpRefBase::~BpRefBase()
862*38e8c45fSAndroid Build Coastguard Worker {
863*38e8c45fSAndroid Build Coastguard Worker     if (mRemote) {
864*38e8c45fSAndroid Build Coastguard Worker         if (!(mState.load(std::memory_order_relaxed)&kRemoteAcquired)) {
865*38e8c45fSAndroid Build Coastguard Worker             mRemote->decStrong(this);
866*38e8c45fSAndroid Build Coastguard Worker         }
867*38e8c45fSAndroid Build Coastguard Worker         mRefs->decWeak(this);
868*38e8c45fSAndroid Build Coastguard Worker     }
869*38e8c45fSAndroid Build Coastguard Worker }
870*38e8c45fSAndroid Build Coastguard Worker 
onFirstRef()871*38e8c45fSAndroid Build Coastguard Worker void BpRefBase::onFirstRef()
872*38e8c45fSAndroid Build Coastguard Worker {
873*38e8c45fSAndroid Build Coastguard Worker     mState.fetch_or(kRemoteAcquired, std::memory_order_relaxed);
874*38e8c45fSAndroid Build Coastguard Worker }
875*38e8c45fSAndroid Build Coastguard Worker 
onLastStrongRef(const void *)876*38e8c45fSAndroid Build Coastguard Worker void BpRefBase::onLastStrongRef(const void* /*id*/)
877*38e8c45fSAndroid Build Coastguard Worker {
878*38e8c45fSAndroid Build Coastguard Worker     if (mRemote) {
879*38e8c45fSAndroid Build Coastguard Worker         mRemote->decStrong(this);
880*38e8c45fSAndroid Build Coastguard Worker     }
881*38e8c45fSAndroid Build Coastguard Worker }
882*38e8c45fSAndroid Build Coastguard Worker 
onIncStrongAttempted(uint32_t,const void *)883*38e8c45fSAndroid Build Coastguard Worker bool BpRefBase::onIncStrongAttempted(uint32_t /*flags*/, const void* /*id*/)
884*38e8c45fSAndroid Build Coastguard Worker {
885*38e8c45fSAndroid Build Coastguard Worker     return mRemote ? mRefs->attemptIncStrong(this) : false;
886*38e8c45fSAndroid Build Coastguard Worker }
887*38e8c45fSAndroid Build Coastguard Worker 
888*38e8c45fSAndroid Build Coastguard Worker // ---------------------------------------------------------------------------
889*38e8c45fSAndroid Build Coastguard Worker 
890*38e8c45fSAndroid Build Coastguard Worker } // namespace android
891