xref: /aosp_15_r20/frameworks/native/libs/binder/RpcState.h (revision 38e8c45f13ce32b0dcecb25141ffecaf386fa17f)
1*38e8c45fSAndroid Build Coastguard Worker /*
2*38e8c45fSAndroid Build Coastguard Worker  * Copyright (C) 2020 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 #pragma once
17*38e8c45fSAndroid Build Coastguard Worker 
18*38e8c45fSAndroid Build Coastguard Worker #include <binder/Functional.h>
19*38e8c45fSAndroid Build Coastguard Worker #include <binder/IBinder.h>
20*38e8c45fSAndroid Build Coastguard Worker #include <binder/Parcel.h>
21*38e8c45fSAndroid Build Coastguard Worker #include <binder/RpcSession.h>
22*38e8c45fSAndroid Build Coastguard Worker #include <binder/RpcThreads.h>
23*38e8c45fSAndroid Build Coastguard Worker #include <binder/unique_fd.h>
24*38e8c45fSAndroid Build Coastguard Worker 
25*38e8c45fSAndroid Build Coastguard Worker #include <map>
26*38e8c45fSAndroid Build Coastguard Worker #include <optional>
27*38e8c45fSAndroid Build Coastguard Worker #include <queue>
28*38e8c45fSAndroid Build Coastguard Worker 
29*38e8c45fSAndroid Build Coastguard Worker #include <sys/uio.h>
30*38e8c45fSAndroid Build Coastguard Worker 
31*38e8c45fSAndroid Build Coastguard Worker namespace android {
32*38e8c45fSAndroid Build Coastguard Worker 
33*38e8c45fSAndroid Build Coastguard Worker struct RpcWireHeader;
34*38e8c45fSAndroid Build Coastguard Worker 
35*38e8c45fSAndroid Build Coastguard Worker /**
36*38e8c45fSAndroid Build Coastguard Worker  * Log a lot more information about RPC calls, when debugging issues. Usually,
37*38e8c45fSAndroid Build Coastguard Worker  * you would want to enable this in only one process. If repeated issues require
38*38e8c45fSAndroid Build Coastguard Worker  * a specific subset of logs to debug, this could be broken up like
39*38e8c45fSAndroid Build Coastguard Worker  * IPCThreadState's.
40*38e8c45fSAndroid Build Coastguard Worker  */
41*38e8c45fSAndroid Build Coastguard Worker #define SHOULD_LOG_RPC_DETAIL false
42*38e8c45fSAndroid Build Coastguard Worker 
43*38e8c45fSAndroid Build Coastguard Worker #if SHOULD_LOG_RPC_DETAIL
44*38e8c45fSAndroid Build Coastguard Worker #define LOG_RPC_DETAIL(...) ALOGI(__VA_ARGS__)
45*38e8c45fSAndroid Build Coastguard Worker #else
46*38e8c45fSAndroid Build Coastguard Worker #define LOG_RPC_DETAIL(...) ALOGV(__VA_ARGS__) // for type checking
47*38e8c45fSAndroid Build Coastguard Worker #endif
48*38e8c45fSAndroid Build Coastguard Worker 
49*38e8c45fSAndroid Build Coastguard Worker #define RPC_FLAKE_PRONE false
50*38e8c45fSAndroid Build Coastguard Worker 
51*38e8c45fSAndroid Build Coastguard Worker #if RPC_FLAKE_PRONE
52*38e8c45fSAndroid Build Coastguard Worker void rpcMaybeWaitToFlake();
53*38e8c45fSAndroid Build Coastguard Worker #define MAYBE_WAIT_IN_FLAKE_MODE rpcMaybeWaitToFlake()
54*38e8c45fSAndroid Build Coastguard Worker #else
55*38e8c45fSAndroid Build Coastguard Worker #define MAYBE_WAIT_IN_FLAKE_MODE do {} while (false)
56*38e8c45fSAndroid Build Coastguard Worker #endif
57*38e8c45fSAndroid Build Coastguard Worker 
58*38e8c45fSAndroid Build Coastguard Worker /**
59*38e8c45fSAndroid Build Coastguard Worker  * Abstracts away management of ref counts and the wire format from
60*38e8c45fSAndroid Build Coastguard Worker  * RpcSession
61*38e8c45fSAndroid Build Coastguard Worker  */
62*38e8c45fSAndroid Build Coastguard Worker class RpcState {
63*38e8c45fSAndroid Build Coastguard Worker public:
64*38e8c45fSAndroid Build Coastguard Worker     RpcState();
65*38e8c45fSAndroid Build Coastguard Worker     ~RpcState();
66*38e8c45fSAndroid Build Coastguard Worker 
67*38e8c45fSAndroid Build Coastguard Worker     [[nodiscard]] static bool validateProtocolVersion(uint32_t version);
68*38e8c45fSAndroid Build Coastguard Worker 
69*38e8c45fSAndroid Build Coastguard Worker     [[nodiscard]] status_t readNewSessionResponse(const sp<RpcSession::RpcConnection>& connection,
70*38e8c45fSAndroid Build Coastguard Worker                                                   const sp<RpcSession>& session, uint32_t* version);
71*38e8c45fSAndroid Build Coastguard Worker     [[nodiscard]] status_t sendConnectionInit(const sp<RpcSession::RpcConnection>& connection,
72*38e8c45fSAndroid Build Coastguard Worker                                               const sp<RpcSession>& session);
73*38e8c45fSAndroid Build Coastguard Worker     [[nodiscard]] status_t readConnectionInit(const sp<RpcSession::RpcConnection>& connection,
74*38e8c45fSAndroid Build Coastguard Worker                                               const sp<RpcSession>& session);
75*38e8c45fSAndroid Build Coastguard Worker 
76*38e8c45fSAndroid Build Coastguard Worker     // TODO(b/182940634): combine some special transactions into one "getServerInfo" call?
77*38e8c45fSAndroid Build Coastguard Worker     sp<IBinder> getRootObject(const sp<RpcSession::RpcConnection>& connection,
78*38e8c45fSAndroid Build Coastguard Worker                               const sp<RpcSession>& session);
79*38e8c45fSAndroid Build Coastguard Worker     [[nodiscard]] status_t getMaxThreads(const sp<RpcSession::RpcConnection>& connection,
80*38e8c45fSAndroid Build Coastguard Worker                                          const sp<RpcSession>& session, size_t* maxThreadsOut);
81*38e8c45fSAndroid Build Coastguard Worker     [[nodiscard]] status_t getSessionId(const sp<RpcSession::RpcConnection>& connection,
82*38e8c45fSAndroid Build Coastguard Worker                                         const sp<RpcSession>& session,
83*38e8c45fSAndroid Build Coastguard Worker                                         std::vector<uint8_t>* sessionIdOut);
84*38e8c45fSAndroid Build Coastguard Worker 
85*38e8c45fSAndroid Build Coastguard Worker     [[nodiscard]] status_t transact(const sp<RpcSession::RpcConnection>& connection,
86*38e8c45fSAndroid Build Coastguard Worker                                     const sp<IBinder>& address, uint32_t code, const Parcel& data,
87*38e8c45fSAndroid Build Coastguard Worker                                     const sp<RpcSession>& session, Parcel* reply, uint32_t flags);
88*38e8c45fSAndroid Build Coastguard Worker     [[nodiscard]] status_t transactAddress(const sp<RpcSession::RpcConnection>& connection,
89*38e8c45fSAndroid Build Coastguard Worker                                            uint64_t address, uint32_t code, const Parcel& data,
90*38e8c45fSAndroid Build Coastguard Worker                                            const sp<RpcSession>& session, Parcel* reply,
91*38e8c45fSAndroid Build Coastguard Worker                                            uint32_t flags);
92*38e8c45fSAndroid Build Coastguard Worker 
93*38e8c45fSAndroid Build Coastguard Worker     /**
94*38e8c45fSAndroid Build Coastguard Worker      * The ownership model here carries an implicit strong refcount whenever a
95*38e8c45fSAndroid Build Coastguard Worker      * binder is sent across processes. Since we have a local strong count in
96*38e8c45fSAndroid Build Coastguard Worker      * sp<> over these objects, we only ever need to keep one of these. So,
97*38e8c45fSAndroid Build Coastguard Worker      * typically we tell the remote process that we drop all the implicit dec
98*38e8c45fSAndroid Build Coastguard Worker      * strongs, and we hold onto the last one. 'target' here is the target
99*38e8c45fSAndroid Build Coastguard Worker      * timesRecd (the number of remaining reference counts) we wish to keep.
100*38e8c45fSAndroid Build Coastguard Worker      * Typically this should be '0' or '1'. The target is used instead of an
101*38e8c45fSAndroid Build Coastguard Worker      * explicit decrement count in order to allow multiple threads to lower the
102*38e8c45fSAndroid Build Coastguard Worker      * number of counts simultaneously. Since we only lower the count to 0 when
103*38e8c45fSAndroid Build Coastguard Worker      * a binder is deleted, targets of '1' should only be sent when the caller
104*38e8c45fSAndroid Build Coastguard Worker      * owns a local strong reference to the binder. Larger targets may be used
105*38e8c45fSAndroid Build Coastguard Worker      * for testing, and to make the function generic, but generally this should
106*38e8c45fSAndroid Build Coastguard Worker      * be avoided because it would be hard to guarantee another thread doesn't
107*38e8c45fSAndroid Build Coastguard Worker      * lower the number of held refcounts to '1'. Note also, these refcounts
108*38e8c45fSAndroid Build Coastguard Worker      * must be sent actively. If they are sent when binders are deleted, this
109*38e8c45fSAndroid Build Coastguard Worker      * can cause leaks, since even remote binders carry an implicit strong ref
110*38e8c45fSAndroid Build Coastguard Worker      * when they are sent to another process.
111*38e8c45fSAndroid Build Coastguard Worker      */
112*38e8c45fSAndroid Build Coastguard Worker     [[nodiscard]] status_t sendDecStrongToTarget(const sp<RpcSession::RpcConnection>& connection,
113*38e8c45fSAndroid Build Coastguard Worker                                                  const sp<RpcSession>& session, uint64_t address,
114*38e8c45fSAndroid Build Coastguard Worker                                                  size_t target);
115*38e8c45fSAndroid Build Coastguard Worker 
116*38e8c45fSAndroid Build Coastguard Worker     enum class CommandType {
117*38e8c45fSAndroid Build Coastguard Worker         ANY,
118*38e8c45fSAndroid Build Coastguard Worker         CONTROL_ONLY,
119*38e8c45fSAndroid Build Coastguard Worker     };
120*38e8c45fSAndroid Build Coastguard Worker     [[nodiscard]] status_t getAndExecuteCommand(const sp<RpcSession::RpcConnection>& connection,
121*38e8c45fSAndroid Build Coastguard Worker                                                 const sp<RpcSession>& session, CommandType type);
122*38e8c45fSAndroid Build Coastguard Worker     [[nodiscard]] status_t drainCommands(const sp<RpcSession::RpcConnection>& connection,
123*38e8c45fSAndroid Build Coastguard Worker                                          const sp<RpcSession>& session, CommandType type);
124*38e8c45fSAndroid Build Coastguard Worker 
125*38e8c45fSAndroid Build Coastguard Worker     /**
126*38e8c45fSAndroid Build Coastguard Worker      * Called by Parcel for outgoing binders. This implies one refcount of
127*38e8c45fSAndroid Build Coastguard Worker      * ownership to the outgoing binder.
128*38e8c45fSAndroid Build Coastguard Worker      */
129*38e8c45fSAndroid Build Coastguard Worker     [[nodiscard]] status_t onBinderLeaving(const sp<RpcSession>& session, const sp<IBinder>& binder,
130*38e8c45fSAndroid Build Coastguard Worker                                            uint64_t* outAddress);
131*38e8c45fSAndroid Build Coastguard Worker 
132*38e8c45fSAndroid Build Coastguard Worker     /**
133*38e8c45fSAndroid Build Coastguard Worker      * Called by Parcel for incoming binders. This either returns the refcount
134*38e8c45fSAndroid Build Coastguard Worker      * to the process, if this process already has one, or it takes ownership of
135*38e8c45fSAndroid Build Coastguard Worker      * that refcount
136*38e8c45fSAndroid Build Coastguard Worker      */
137*38e8c45fSAndroid Build Coastguard Worker     [[nodiscard]] status_t onBinderEntering(const sp<RpcSession>& session, uint64_t address,
138*38e8c45fSAndroid Build Coastguard Worker                                             sp<IBinder>* out);
139*38e8c45fSAndroid Build Coastguard Worker     /**
140*38e8c45fSAndroid Build Coastguard Worker      * Called on incoming binders to update refcounting information. This should
141*38e8c45fSAndroid Build Coastguard Worker      * only be called when it is done as part of making progress on a
142*38e8c45fSAndroid Build Coastguard Worker      * transaction.
143*38e8c45fSAndroid Build Coastguard Worker      */
144*38e8c45fSAndroid Build Coastguard Worker     [[nodiscard]] status_t flushExcessBinderRefs(const sp<RpcSession>& session, uint64_t address,
145*38e8c45fSAndroid Build Coastguard Worker                                                  const sp<IBinder>& binder);
146*38e8c45fSAndroid Build Coastguard Worker     /**
147*38e8c45fSAndroid Build Coastguard Worker      * Called when the RpcSession is shutdown.
148*38e8c45fSAndroid Build Coastguard Worker      * Send obituaries for each known remote binder with this session.
149*38e8c45fSAndroid Build Coastguard Worker      */
150*38e8c45fSAndroid Build Coastguard Worker     [[nodiscard]] status_t sendObituaries(const sp<RpcSession>& session);
151*38e8c45fSAndroid Build Coastguard Worker 
152*38e8c45fSAndroid Build Coastguard Worker     LIBBINDER_INTERNAL_EXPORTED size_t countBinders();
153*38e8c45fSAndroid Build Coastguard Worker     LIBBINDER_INTERNAL_EXPORTED void dump();
154*38e8c45fSAndroid Build Coastguard Worker 
155*38e8c45fSAndroid Build Coastguard Worker     /**
156*38e8c45fSAndroid Build Coastguard Worker      * Called when reading or writing data to a session fails to clean up
157*38e8c45fSAndroid Build Coastguard Worker      * data associated with the session in order to cleanup binders.
158*38e8c45fSAndroid Build Coastguard Worker      * Specifically, we have a strong dependency cycle, since BpBinder is
159*38e8c45fSAndroid Build Coastguard Worker      * OBJECT_LIFETIME_WEAK (so that onAttemptIncStrong may return true).
160*38e8c45fSAndroid Build Coastguard Worker      *
161*38e8c45fSAndroid Build Coastguard Worker      *     BpBinder -> RpcSession -> RpcState
162*38e8c45fSAndroid Build Coastguard Worker      *      ^-----------------------------/
163*38e8c45fSAndroid Build Coastguard Worker      *
164*38e8c45fSAndroid Build Coastguard Worker      * In the success case, eventually all refcounts should be propagated over
165*38e8c45fSAndroid Build Coastguard Worker      * the session, though this could also be called to eagerly cleanup
166*38e8c45fSAndroid Build Coastguard Worker      * the session.
167*38e8c45fSAndroid Build Coastguard Worker      *
168*38e8c45fSAndroid Build Coastguard Worker      * WARNING: RpcState is responsible for calling this when the session is
169*38e8c45fSAndroid Build Coastguard Worker      * no longer recoverable.
170*38e8c45fSAndroid Build Coastguard Worker      */
171*38e8c45fSAndroid Build Coastguard Worker     void clear();
172*38e8c45fSAndroid Build Coastguard Worker 
173*38e8c45fSAndroid Build Coastguard Worker private:
174*38e8c45fSAndroid Build Coastguard Worker     void clear(RpcMutexUniqueLock nodeLock);
175*38e8c45fSAndroid Build Coastguard Worker     void dumpLocked();
176*38e8c45fSAndroid Build Coastguard Worker 
177*38e8c45fSAndroid Build Coastguard Worker     // Alternative to std::vector<uint8_t> that doesn't abort on allocation failure and caps
178*38e8c45fSAndroid Build Coastguard Worker     // large allocations to avoid being requested from allocating too much data.
179*38e8c45fSAndroid Build Coastguard Worker     struct CommandData {
180*38e8c45fSAndroid Build Coastguard Worker         explicit CommandData(size_t size);
validCommandData181*38e8c45fSAndroid Build Coastguard Worker         bool valid() { return mSize == 0 || mData != nullptr; }
sizeCommandData182*38e8c45fSAndroid Build Coastguard Worker         size_t size() { return mSize; }
dataCommandData183*38e8c45fSAndroid Build Coastguard Worker         uint8_t* data() { return mData.get(); }
releaseCommandData184*38e8c45fSAndroid Build Coastguard Worker         uint8_t* release() { return mData.release(); }
185*38e8c45fSAndroid Build Coastguard Worker 
186*38e8c45fSAndroid Build Coastguard Worker     private:
187*38e8c45fSAndroid Build Coastguard Worker         std::unique_ptr<uint8_t[]> mData;
188*38e8c45fSAndroid Build Coastguard Worker         size_t mSize;
189*38e8c45fSAndroid Build Coastguard Worker     };
190*38e8c45fSAndroid Build Coastguard Worker 
191*38e8c45fSAndroid Build Coastguard Worker     [[nodiscard]] status_t rpcSend(
192*38e8c45fSAndroid Build Coastguard Worker             const sp<RpcSession::RpcConnection>& connection, const sp<RpcSession>& session,
193*38e8c45fSAndroid Build Coastguard Worker             const char* what, iovec* iovs, int niovs,
194*38e8c45fSAndroid Build Coastguard Worker             const std::optional<binder::impl::SmallFunction<status_t()>>& altPoll,
195*38e8c45fSAndroid Build Coastguard Worker             const std::vector<std::variant<binder::unique_fd, binder::borrowed_fd>>* ancillaryFds =
196*38e8c45fSAndroid Build Coastguard Worker                     nullptr);
197*38e8c45fSAndroid Build Coastguard Worker     [[nodiscard]] status_t rpcRec(const sp<RpcSession::RpcConnection>& connection,
198*38e8c45fSAndroid Build Coastguard Worker                                   const sp<RpcSession>& session, const char* what, iovec* iovs,
199*38e8c45fSAndroid Build Coastguard Worker                                   int niovs,
200*38e8c45fSAndroid Build Coastguard Worker                                   std::vector<std::variant<binder::unique_fd, binder::borrowed_fd>>*
201*38e8c45fSAndroid Build Coastguard Worker                                           ancillaryFds = nullptr);
202*38e8c45fSAndroid Build Coastguard Worker 
203*38e8c45fSAndroid Build Coastguard Worker     [[nodiscard]] status_t waitForReply(const sp<RpcSession::RpcConnection>& connection,
204*38e8c45fSAndroid Build Coastguard Worker                                         const sp<RpcSession>& session, Parcel* reply);
205*38e8c45fSAndroid Build Coastguard Worker     [[nodiscard]] status_t processCommand(
206*38e8c45fSAndroid Build Coastguard Worker             const sp<RpcSession::RpcConnection>& connection, const sp<RpcSession>& session,
207*38e8c45fSAndroid Build Coastguard Worker             const RpcWireHeader& command, CommandType type,
208*38e8c45fSAndroid Build Coastguard Worker             std::vector<std::variant<binder::unique_fd, binder::borrowed_fd>>&& ancillaryFds);
209*38e8c45fSAndroid Build Coastguard Worker     [[nodiscard]] status_t processTransact(
210*38e8c45fSAndroid Build Coastguard Worker             const sp<RpcSession::RpcConnection>& connection, const sp<RpcSession>& session,
211*38e8c45fSAndroid Build Coastguard Worker             const RpcWireHeader& command,
212*38e8c45fSAndroid Build Coastguard Worker             std::vector<std::variant<binder::unique_fd, binder::borrowed_fd>>&& ancillaryFds);
213*38e8c45fSAndroid Build Coastguard Worker     [[nodiscard]] status_t processTransactInternal(
214*38e8c45fSAndroid Build Coastguard Worker             const sp<RpcSession::RpcConnection>& connection, const sp<RpcSession>& session,
215*38e8c45fSAndroid Build Coastguard Worker             CommandData transactionData,
216*38e8c45fSAndroid Build Coastguard Worker             std::vector<std::variant<binder::unique_fd, binder::borrowed_fd>>&& ancillaryFds);
217*38e8c45fSAndroid Build Coastguard Worker     [[nodiscard]] status_t processDecStrong(const sp<RpcSession::RpcConnection>& connection,
218*38e8c45fSAndroid Build Coastguard Worker                                             const sp<RpcSession>& session,
219*38e8c45fSAndroid Build Coastguard Worker                                             const RpcWireHeader& command);
220*38e8c45fSAndroid Build Coastguard Worker 
221*38e8c45fSAndroid Build Coastguard Worker     // Whether `parcel` is compatible with `session`.
222*38e8c45fSAndroid Build Coastguard Worker     [[nodiscard]] static status_t validateParcel(const sp<RpcSession>& session,
223*38e8c45fSAndroid Build Coastguard Worker                                                  const Parcel& parcel, std::string* errorMsg);
224*38e8c45fSAndroid Build Coastguard Worker 
225*38e8c45fSAndroid Build Coastguard Worker     struct BinderNode {
226*38e8c45fSAndroid Build Coastguard Worker         // Two cases:
227*38e8c45fSAndroid Build Coastguard Worker         // A - local binder we are serving
228*38e8c45fSAndroid Build Coastguard Worker         // B - remote binder, we are sending transactions to
229*38e8c45fSAndroid Build Coastguard Worker         wp<IBinder> binder;
230*38e8c45fSAndroid Build Coastguard Worker 
231*38e8c45fSAndroid Build Coastguard Worker         // if timesSent > 0, this will be equal to binder.promote()
232*38e8c45fSAndroid Build Coastguard Worker         sp<IBinder> sentRef;
233*38e8c45fSAndroid Build Coastguard Worker 
234*38e8c45fSAndroid Build Coastguard Worker         // Number of times we've sent this binder out of process, which
235*38e8c45fSAndroid Build Coastguard Worker         // translates to an implicit strong count. A client must send RPC binder
236*38e8c45fSAndroid Build Coastguard Worker         // socket's dec ref for each time it is sent out of process in order to
237*38e8c45fSAndroid Build Coastguard Worker         // deallocate it. Note, a proxy binder we are holding onto might be
238*38e8c45fSAndroid Build Coastguard Worker         // sent (this is important when the only remaining refcount of this
239*38e8c45fSAndroid Build Coastguard Worker         // binder is the one associated with a transaction sending it back to
240*38e8c45fSAndroid Build Coastguard Worker         // its server)
241*38e8c45fSAndroid Build Coastguard Worker         size_t timesSent = 0;
242*38e8c45fSAndroid Build Coastguard Worker 
243*38e8c45fSAndroid Build Coastguard Worker         // Number of times we've received this binder, each time corresponds to
244*38e8c45fSAndroid Build Coastguard Worker         // a reference we hold over the wire (not a local incStrong/decStrong)
245*38e8c45fSAndroid Build Coastguard Worker         size_t timesRecd = 0;
246*38e8c45fSAndroid Build Coastguard Worker 
247*38e8c45fSAndroid Build Coastguard Worker         // transaction ID, for async transactions
248*38e8c45fSAndroid Build Coastguard Worker         uint64_t asyncNumber = 0;
249*38e8c45fSAndroid Build Coastguard Worker 
250*38e8c45fSAndroid Build Coastguard Worker         //
251*38e8c45fSAndroid Build Coastguard Worker         // CASE A - local binder we are serving
252*38e8c45fSAndroid Build Coastguard Worker         //
253*38e8c45fSAndroid Build Coastguard Worker 
254*38e8c45fSAndroid Build Coastguard Worker         // async transaction queue, _only_ for local binder
255*38e8c45fSAndroid Build Coastguard Worker         struct AsyncTodo {
256*38e8c45fSAndroid Build Coastguard Worker             sp<IBinder> ref;
257*38e8c45fSAndroid Build Coastguard Worker             CommandData data;
258*38e8c45fSAndroid Build Coastguard Worker             std::vector<std::variant<binder::unique_fd, binder::borrowed_fd>> ancillaryFds;
259*38e8c45fSAndroid Build Coastguard Worker             uint64_t asyncNumber = 0;
260*38e8c45fSAndroid Build Coastguard Worker 
261*38e8c45fSAndroid Build Coastguard Worker             bool operator<(const AsyncTodo& o) const {
262*38e8c45fSAndroid Build Coastguard Worker                 return asyncNumber > /* !!! */ o.asyncNumber;
263*38e8c45fSAndroid Build Coastguard Worker             }
264*38e8c45fSAndroid Build Coastguard Worker         };
265*38e8c45fSAndroid Build Coastguard Worker         std::priority_queue<AsyncTodo> asyncTodo;
266*38e8c45fSAndroid Build Coastguard Worker 
267*38e8c45fSAndroid Build Coastguard Worker         //
268*38e8c45fSAndroid Build Coastguard Worker         // CASE B - remote binder, we are sending transactions to
269*38e8c45fSAndroid Build Coastguard Worker         //
270*38e8c45fSAndroid Build Coastguard Worker 
271*38e8c45fSAndroid Build Coastguard Worker         // (no additional data specific to remote binders)
272*38e8c45fSAndroid Build Coastguard Worker 
273*38e8c45fSAndroid Build Coastguard Worker         std::string toString() const;
274*38e8c45fSAndroid Build Coastguard Worker     };
275*38e8c45fSAndroid Build Coastguard Worker 
276*38e8c45fSAndroid Build Coastguard Worker     // Checks if there is any reference left to a node and erases it. If this
277*38e8c45fSAndroid Build Coastguard Worker     // is the last node, shuts down the session.
278*38e8c45fSAndroid Build Coastguard Worker     //
279*38e8c45fSAndroid Build Coastguard Worker     // Node lock is passed here for convenience, so that we can release it
280*38e8c45fSAndroid Build Coastguard Worker     // and terminate the session, but we could leave it up to the caller
281*38e8c45fSAndroid Build Coastguard Worker     // by returning a continuation if we needed to erase multiple specific
282*38e8c45fSAndroid Build Coastguard Worker     // nodes. It may be tempting to allow the client to keep on holding the
283*38e8c45fSAndroid Build Coastguard Worker     // lock and instead just return whether or not we should shutdown, but
284*38e8c45fSAndroid Build Coastguard Worker     // this introduces the posssibility that another thread calls
285*38e8c45fSAndroid Build Coastguard Worker     // getRootBinder and thinks it is valid, rather than immediately getting
286*38e8c45fSAndroid Build Coastguard Worker     // an error.
287*38e8c45fSAndroid Build Coastguard Worker     sp<IBinder> tryEraseNode(const sp<RpcSession>& session, RpcMutexUniqueLock nodeLock,
288*38e8c45fSAndroid Build Coastguard Worker                              std::map<uint64_t, BinderNode>::iterator& it);
289*38e8c45fSAndroid Build Coastguard Worker 
290*38e8c45fSAndroid Build Coastguard Worker     // true - success
291*38e8c45fSAndroid Build Coastguard Worker     // false - session shutdown, halt
292*38e8c45fSAndroid Build Coastguard Worker     [[nodiscard]] bool nodeProgressAsyncNumber(BinderNode* node);
293*38e8c45fSAndroid Build Coastguard Worker 
294*38e8c45fSAndroid Build Coastguard Worker     RpcMutex mNodeMutex;
295*38e8c45fSAndroid Build Coastguard Worker     bool mTerminated = false;
296*38e8c45fSAndroid Build Coastguard Worker     uint32_t mNextId = 0;
297*38e8c45fSAndroid Build Coastguard Worker     // binders known by both sides of a session
298*38e8c45fSAndroid Build Coastguard Worker     std::map<uint64_t, BinderNode> mNodeForAddress;
299*38e8c45fSAndroid Build Coastguard Worker };
300*38e8c45fSAndroid Build Coastguard Worker 
301*38e8c45fSAndroid Build Coastguard Worker } // namespace android
302