Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2020 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | #pragma once |
| 17 | |
| 18 | #include <android-base/unique_fd.h> |
| 19 | #include <binder/IBinder.h> |
| 20 | #include <binder/RpcAddress.h> |
| 21 | #include <utils/Errors.h> |
| 22 | #include <utils/RefBase.h> |
| 23 | |
Steven Moreland | 736664b | 2021-05-01 04:27:25 +0000 | [diff] [blame] | 24 | #include <map> |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 25 | #include <optional> |
Steven Moreland | 736664b | 2021-05-01 04:27:25 +0000 | [diff] [blame] | 26 | #include <thread> |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 27 | #include <vector> |
| 28 | |
| 29 | // WARNING: This is a feature which is still in development, and it is subject |
| 30 | // to radical change. Any production use of this may subject your code to any |
| 31 | // number of problems. |
| 32 | |
| 33 | namespace android { |
| 34 | |
| 35 | class Parcel; |
| 36 | class RpcServer; |
Steven Moreland | 611d15f | 2021-05-01 01:28:27 +0000 | [diff] [blame] | 37 | class RpcSocketAddress; |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 38 | class RpcState; |
| 39 | |
| 40 | /** |
Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 41 | * This represents a session (group of connections) between a client |
| 42 | * and a server. Multiple connections are needed for multiple parallel "binder" |
| 43 | * calls which may also have nested calls. |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 44 | */ |
Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 45 | class RpcSession final : public virtual RefBase { |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 46 | public: |
Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 47 | static sp<RpcSession> make(); |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 48 | |
| 49 | /** |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 50 | * This should be called once per thread, matching 'join' in the remote |
| 51 | * process. |
| 52 | */ |
Steven Moreland | f137de9 | 2021-04-24 01:54:26 +0000 | [diff] [blame] | 53 | [[nodiscard]] bool setupUnixDomainClient(const char* path); |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 54 | |
Steven Moreland | c163595 | 2021-04-01 16:20:47 +0000 | [diff] [blame] | 55 | /** |
Steven Moreland | c163595 | 2021-04-01 16:20:47 +0000 | [diff] [blame] | 56 | * Connects to an RPC server at the CVD & port. |
| 57 | */ |
Steven Moreland | f137de9 | 2021-04-24 01:54:26 +0000 | [diff] [blame] | 58 | [[nodiscard]] bool setupVsockClient(unsigned int cvd, unsigned int port); |
Steven Moreland | c163595 | 2021-04-01 16:20:47 +0000 | [diff] [blame] | 59 | |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 60 | /** |
Yifan Hong | 0d2bd11 | 2021-04-13 17:38:36 -0700 | [diff] [blame] | 61 | * Connects to an RPC server at the given address and port. |
| 62 | */ |
Steven Moreland | f137de9 | 2021-04-24 01:54:26 +0000 | [diff] [blame] | 63 | [[nodiscard]] bool setupInetClient(const char* addr, unsigned int port); |
Yifan Hong | 0d2bd11 | 2021-04-13 17:38:36 -0700 | [diff] [blame] | 64 | |
| 65 | /** |
Steven Moreland | d47b32c | 2021-04-13 02:03:08 +0000 | [diff] [blame] | 66 | * For debugging! |
| 67 | * |
Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 68 | * Sets up an empty connection. All queries to this connection which require a |
Steven Moreland | d47b32c | 2021-04-13 02:03:08 +0000 | [diff] [blame] | 69 | * response will never be satisfied. All data sent here will be |
| 70 | * unceremoniously cast down the bottomless pit, /dev/null. |
| 71 | */ |
| 72 | [[nodiscard]] bool addNullDebuggingClient(); |
| 73 | |
| 74 | /** |
Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 75 | * Query the other side of the session for the root object hosted by that |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 76 | * process's RpcServer (if one exists) |
| 77 | */ |
| 78 | sp<IBinder> getRootObject(); |
| 79 | |
Steven Moreland | f137de9 | 2021-04-24 01:54:26 +0000 | [diff] [blame] | 80 | /** |
Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 81 | * Query the other side of the session for the maximum number of threads |
Steven Moreland | f137de9 | 2021-04-24 01:54:26 +0000 | [diff] [blame] | 82 | * it supports (maximum number of concurrent non-nested synchronous transactions) |
| 83 | */ |
Steven Moreland | 1be9135 | 2021-05-11 22:12:15 +0000 | [diff] [blame] | 84 | status_t getRemoteMaxThreads(size_t* maxThreads); |
Steven Moreland | f137de9 | 2021-04-24 01:54:26 +0000 | [diff] [blame] | 85 | |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 86 | [[nodiscard]] status_t transact(const RpcAddress& address, uint32_t code, const Parcel& data, |
| 87 | Parcel* reply, uint32_t flags); |
| 88 | [[nodiscard]] status_t sendDecStrong(const RpcAddress& address); |
| 89 | |
Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 90 | ~RpcSession(); |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 91 | |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 92 | wp<RpcServer> server(); |
| 93 | |
| 94 | // internal only |
| 95 | const std::unique_ptr<RpcState>& state() { return mState; } |
| 96 | |
Steven Moreland | 7c5e6c2 | 2021-05-01 02:55:20 +0000 | [diff] [blame] | 97 | class PrivateAccessorForId { |
| 98 | private: |
Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 99 | friend class RpcSession; |
Steven Moreland | 7c5e6c2 | 2021-05-01 02:55:20 +0000 | [diff] [blame] | 100 | friend class RpcState; |
Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 101 | explicit PrivateAccessorForId(const RpcSession* session) : mSession(session) {} |
Steven Moreland | 7c5e6c2 | 2021-05-01 02:55:20 +0000 | [diff] [blame] | 102 | |
Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 103 | const std::optional<int32_t> get() { return mSession->mId; } |
Steven Moreland | 7c5e6c2 | 2021-05-01 02:55:20 +0000 | [diff] [blame] | 104 | |
Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 105 | const RpcSession* mSession; |
Steven Moreland | 7c5e6c2 | 2021-05-01 02:55:20 +0000 | [diff] [blame] | 106 | }; |
| 107 | PrivateAccessorForId getPrivateAccessorForId() const { return PrivateAccessorForId(this); } |
| 108 | |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 109 | private: |
Steven Moreland | 7c5e6c2 | 2021-05-01 02:55:20 +0000 | [diff] [blame] | 110 | friend PrivateAccessorForId; |
Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 111 | friend sp<RpcSession>; |
Steven Moreland | f137de9 | 2021-04-24 01:54:26 +0000 | [diff] [blame] | 112 | friend RpcServer; |
Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 113 | RpcSession(); |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 114 | |
Steven Moreland | e47511f | 2021-05-20 00:07:41 +0000 | [diff] [blame] | 115 | /** This is not a pipe. */ |
| 116 | struct FdTrigger { |
| 117 | static std::unique_ptr<FdTrigger> make(); |
| 118 | /** |
| 119 | * poll() on this fd for POLLHUP to get notification when trigger is called |
| 120 | */ |
| 121 | base::borrowed_fd readFd() const { return mRead; } |
Steven Moreland | 4ec3c43 | 2021-05-20 00:32:47 +0000 | [diff] [blame] | 122 | |
Steven Moreland | e47511f | 2021-05-20 00:07:41 +0000 | [diff] [blame] | 123 | /** |
| 124 | * Close the write end of the pipe so that the read end receives POLLHUP. |
| 125 | */ |
| 126 | void trigger(); |
| 127 | |
Steven Moreland | 4ec3c43 | 2021-05-20 00:32:47 +0000 | [diff] [blame] | 128 | /** |
| 129 | * Poll for a read event. |
| 130 | * |
| 131 | * Return: |
| 132 | * true - time to read! |
| 133 | * false - trigger happened |
| 134 | */ |
| 135 | bool triggerablePollRead(base::borrowed_fd fd); |
| 136 | |
Steven Moreland | 9d11b92 | 2021-05-20 01:22:58 +0000 | [diff] [blame^] | 137 | /** |
| 138 | * Read, but allow the read to be interrupted by this trigger. |
| 139 | * |
| 140 | * Return: |
| 141 | * true - read succeeded at 'size' |
| 142 | * false - interrupted (failure or trigger) |
| 143 | */ |
| 144 | bool interruptableRecv(base::borrowed_fd fd, void* data, size_t size); |
| 145 | |
Steven Moreland | e47511f | 2021-05-20 00:07:41 +0000 | [diff] [blame] | 146 | private: |
| 147 | base::unique_fd mWrite; |
| 148 | base::unique_fd mRead; |
| 149 | }; |
| 150 | |
Steven Moreland | 7c5e6c2 | 2021-05-01 02:55:20 +0000 | [diff] [blame] | 151 | status_t readId(); |
| 152 | |
Steven Moreland | 5802c2b | 2021-05-12 20:13:04 +0000 | [diff] [blame] | 153 | // transfer ownership of thread |
| 154 | void preJoin(std::thread thread); |
| 155 | // join on thread passed to preJoin |
| 156 | void join(base::unique_fd client); |
Steven Moreland | ee78e76 | 2021-05-05 21:12:51 +0000 | [diff] [blame] | 157 | void terminateLocked(); |
Steven Moreland | f137de9 | 2021-04-24 01:54:26 +0000 | [diff] [blame] | 158 | |
Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 159 | struct RpcConnection : public RefBase { |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 160 | base::unique_fd fd; |
| 161 | |
| 162 | // whether this or another thread is currently using this fd to make |
| 163 | // or receive transactions. |
| 164 | std::optional<pid_t> exclusiveTid; |
| 165 | }; |
| 166 | |
Steven Moreland | 611d15f | 2021-05-01 01:28:27 +0000 | [diff] [blame] | 167 | bool setupSocketClient(const RpcSocketAddress& address); |
Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 168 | bool setupOneSocketClient(const RpcSocketAddress& address, int32_t sessionId); |
Steven Moreland | c8c256b | 2021-05-11 22:59:09 +0000 | [diff] [blame] | 169 | void addClientConnection(base::unique_fd fd); |
Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 170 | void setForServer(const wp<RpcServer>& server, int32_t sessionId); |
| 171 | sp<RpcConnection> assignServerToThisThread(base::unique_fd fd); |
| 172 | bool removeServerConnection(const sp<RpcConnection>& connection); |
Steven Moreland | af816d8 | 2021-04-19 23:11:33 +0000 | [diff] [blame] | 173 | |
Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 174 | enum class ConnectionUse { |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 175 | CLIENT, |
| 176 | CLIENT_ASYNC, |
| 177 | CLIENT_REFCOUNT, |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 178 | }; |
| 179 | |
Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 180 | // RAII object for session connection |
| 181 | class ExclusiveConnection { |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 182 | public: |
Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 183 | explicit ExclusiveConnection(const sp<RpcSession>& session, ConnectionUse use); |
| 184 | ~ExclusiveConnection(); |
| 185 | const base::unique_fd& fd() { return mConnection->fd; } |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 186 | |
| 187 | private: |
Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 188 | static void findConnection(pid_t tid, sp<RpcConnection>* exclusive, |
| 189 | sp<RpcConnection>* available, |
| 190 | std::vector<sp<RpcConnection>>& sockets, |
| 191 | size_t socketsIndexHint); |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 192 | |
Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 193 | sp<RpcSession> mSession; // avoid deallocation |
| 194 | sp<RpcConnection> mConnection; |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 195 | |
| 196 | // whether this is being used for a nested transaction (being on the same |
| 197 | // thread guarantees we won't write in the middle of a message, the way |
| 198 | // the wire protocol is constructed guarantees this is safe). |
| 199 | bool mReentrant = false; |
| 200 | }; |
| 201 | |
Steven Moreland | bb543a8 | 2021-05-11 02:31:50 +0000 | [diff] [blame] | 202 | // On the other side of a session, for each of mClientConnections here, there should |
| 203 | // be one of mServerConnections on the other side (and vice versa). |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 204 | // |
Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 205 | // For the simplest session, a single server with one client, you would |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 206 | // have: |
Steven Moreland | bb543a8 | 2021-05-11 02:31:50 +0000 | [diff] [blame] | 207 | // - the server has a single 'mServerConnections' and a thread listening on this |
| 208 | // - the client has a single 'mClientConnections' and makes calls to this |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 209 | // - here, when the client makes a call, the server can call back into it |
| 210 | // (nested calls), but outside of this, the client will only ever read |
| 211 | // calls from the server when it makes a call itself. |
| 212 | // |
| 213 | // For a more complicated case, the client might itself open up a thread to |
| 214 | // serve calls to the server at all times (e.g. if it hosts a callback) |
| 215 | |
Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 216 | wp<RpcServer> mForServer; // maybe null, for client sessions |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 217 | |
Steven Moreland | 7c5e6c2 | 2021-05-01 02:55:20 +0000 | [diff] [blame] | 218 | // TODO(b/183988761): this shouldn't be guessable |
| 219 | std::optional<int32_t> mId; |
| 220 | |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 221 | std::unique_ptr<RpcState> mState; |
| 222 | |
Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 223 | std::mutex mMutex; // for all below |
Steven Moreland | 736664b | 2021-05-01 04:27:25 +0000 | [diff] [blame] | 224 | |
Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 225 | std::condition_variable mAvailableConnectionCv; // for mWaitingThreads |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 226 | size_t mWaitingThreads = 0; |
Steven Moreland | bb543a8 | 2021-05-11 02:31:50 +0000 | [diff] [blame] | 227 | // hint index into clients, ++ when sending an async transaction |
| 228 | size_t mClientConnectionsOffset = 0; |
| 229 | std::vector<sp<RpcConnection>> mClientConnections; |
| 230 | std::vector<sp<RpcConnection>> mServerConnections; |
Steven Moreland | 736664b | 2021-05-01 04:27:25 +0000 | [diff] [blame] | 231 | |
Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 232 | // TODO(b/185167543): use for reverse sessions (allow client to also |
| 233 | // serve calls on a session). |
| 234 | // TODO(b/185167543): allow sharing between different sessions in a |
Steven Moreland | bb543a8 | 2021-05-11 02:31:50 +0000 | [diff] [blame] | 235 | // process? (or combine with mServerConnections) |
Steven Moreland | 736664b | 2021-05-01 04:27:25 +0000 | [diff] [blame] | 236 | std::map<std::thread::id, std::thread> mThreads; |
Steven Moreland | ee78e76 | 2021-05-05 21:12:51 +0000 | [diff] [blame] | 237 | bool mTerminated = false; |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 238 | }; |
| 239 | |
| 240 | } // namespace android |