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 | |
| 17 | #define LOG_TAG "RpcConnection" |
| 18 | |
| 19 | #include <binder/RpcConnection.h> |
| 20 | |
Yifan Hong | 0d2bd11 | 2021-04-13 17:38:36 -0700 | [diff] [blame] | 21 | #include <arpa/inet.h> |
| 22 | #include <netdb.h> |
| 23 | #include <netinet/in.h> |
| 24 | #include <sys/socket.h> |
| 25 | #include <sys/types.h> |
| 26 | #include <sys/un.h> |
| 27 | #include <unistd.h> |
| 28 | |
| 29 | #include <string_view> |
| 30 | |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 31 | #include <binder/Parcel.h> |
| 32 | #include <binder/Stability.h> |
Steven Moreland | c163595 | 2021-04-01 16:20:47 +0000 | [diff] [blame] | 33 | #include <utils/String8.h> |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 34 | |
| 35 | #include "RpcState.h" |
| 36 | #include "RpcWireFormat.h" |
| 37 | |
Steven Moreland | c163595 | 2021-04-01 16:20:47 +0000 | [diff] [blame] | 38 | #ifdef __GLIBC__ |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 39 | extern "C" pid_t gettid(); |
| 40 | #endif |
| 41 | |
Steven Moreland | c163595 | 2021-04-01 16:20:47 +0000 | [diff] [blame] | 42 | #ifdef __BIONIC__ |
| 43 | #include <linux/vm_sockets.h> |
| 44 | #endif |
| 45 | |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 46 | namespace android { |
| 47 | |
| 48 | using base::unique_fd; |
Yifan Hong | 0d2bd11 | 2021-04-13 17:38:36 -0700 | [diff] [blame] | 49 | using AddrInfo = std::unique_ptr<addrinfo, decltype(&freeaddrinfo)>; |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 50 | |
Steven Moreland | c163595 | 2021-04-01 16:20:47 +0000 | [diff] [blame] | 51 | RpcConnection::SocketAddress::~SocketAddress() {} |
| 52 | |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 53 | RpcConnection::RpcConnection() { |
| 54 | LOG_RPC_DETAIL("RpcConnection created %p", this); |
| 55 | |
| 56 | mState = std::make_unique<RpcState>(); |
| 57 | } |
| 58 | RpcConnection::~RpcConnection() { |
| 59 | LOG_RPC_DETAIL("RpcConnection destroyed %p", this); |
Steven Moreland | af816d8 | 2021-04-19 23:11:33 +0000 | [diff] [blame] | 60 | |
| 61 | std::lock_guard<std::mutex> _l(mSocketMutex); |
| 62 | LOG_ALWAYS_FATAL_IF(mServers.size() != 0, |
| 63 | "Should not be able to destroy a connection with servers in use."); |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | sp<RpcConnection> RpcConnection::make() { |
Steven Moreland | 1a3a8ef | 2021-04-02 02:52:46 +0000 | [diff] [blame] | 67 | return sp<RpcConnection>::make(); |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 68 | } |
| 69 | |
Steven Moreland | c163595 | 2021-04-01 16:20:47 +0000 | [diff] [blame] | 70 | class UnixSocketAddress : public RpcConnection::SocketAddress { |
| 71 | public: |
| 72 | explicit UnixSocketAddress(const char* path) : mAddr({.sun_family = AF_UNIX}) { |
| 73 | unsigned int pathLen = strlen(path) + 1; |
Steven Moreland | cda6085 | 2021-04-14 23:45:32 +0000 | [diff] [blame] | 74 | LOG_ALWAYS_FATAL_IF(pathLen > sizeof(mAddr.sun_path), "Socket path is too long: %u %s", |
| 75 | pathLen, path); |
Steven Moreland | c163595 | 2021-04-01 16:20:47 +0000 | [diff] [blame] | 76 | memcpy(mAddr.sun_path, path, pathLen); |
| 77 | } |
| 78 | virtual ~UnixSocketAddress() {} |
| 79 | std::string toString() const override { |
| 80 | return String8::format("path '%.*s'", static_cast<int>(sizeof(mAddr.sun_path)), |
| 81 | mAddr.sun_path) |
| 82 | .c_str(); |
| 83 | } |
| 84 | const sockaddr* addr() const override { return reinterpret_cast<const sockaddr*>(&mAddr); } |
| 85 | size_t addrSize() const override { return sizeof(mAddr); } |
| 86 | |
| 87 | private: |
| 88 | sockaddr_un mAddr; |
| 89 | }; |
| 90 | |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 91 | bool RpcConnection::setupUnixDomainServer(const char* path) { |
Steven Moreland | d47b32c | 2021-04-13 02:03:08 +0000 | [diff] [blame] | 92 | return setupSocketServer(UnixSocketAddress(path)); |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | bool RpcConnection::addUnixDomainClient(const char* path) { |
Steven Moreland | d47b32c | 2021-04-13 02:03:08 +0000 | [diff] [blame] | 96 | return addSocketClient(UnixSocketAddress(path)); |
Steven Moreland | 5358354 | 2021-03-30 00:25:41 +0000 | [diff] [blame] | 97 | } |
| 98 | |
Steven Moreland | c163595 | 2021-04-01 16:20:47 +0000 | [diff] [blame] | 99 | #ifdef __BIONIC__ |
| 100 | |
| 101 | class VsockSocketAddress : public RpcConnection::SocketAddress { |
| 102 | public: |
| 103 | VsockSocketAddress(unsigned int cid, unsigned int port) |
| 104 | : mAddr({ |
| 105 | .svm_family = AF_VSOCK, |
| 106 | .svm_port = port, |
| 107 | .svm_cid = cid, |
| 108 | }) {} |
| 109 | virtual ~VsockSocketAddress() {} |
| 110 | std::string toString() const override { |
Yifan Hong | 4c79153 | 2021-04-14 12:38:46 -0700 | [diff] [blame] | 111 | return String8::format("cid %u port %u", mAddr.svm_cid, mAddr.svm_port).c_str(); |
Steven Moreland | c163595 | 2021-04-01 16:20:47 +0000 | [diff] [blame] | 112 | } |
| 113 | const sockaddr* addr() const override { return reinterpret_cast<const sockaddr*>(&mAddr); } |
| 114 | size_t addrSize() const override { return sizeof(mAddr); } |
| 115 | |
| 116 | private: |
| 117 | sockaddr_vm mAddr; |
| 118 | }; |
| 119 | |
| 120 | bool RpcConnection::setupVsockServer(unsigned int port) { |
| 121 | // realizing value w/ this type at compile time to avoid ubsan abort |
| 122 | constexpr unsigned int kAnyCid = VMADDR_CID_ANY; |
| 123 | |
Steven Moreland | d47b32c | 2021-04-13 02:03:08 +0000 | [diff] [blame] | 124 | return setupSocketServer(VsockSocketAddress(kAnyCid, port)); |
Steven Moreland | c163595 | 2021-04-01 16:20:47 +0000 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | bool RpcConnection::addVsockClient(unsigned int cid, unsigned int port) { |
Steven Moreland | d47b32c | 2021-04-13 02:03:08 +0000 | [diff] [blame] | 128 | return addSocketClient(VsockSocketAddress(cid, port)); |
Steven Moreland | c163595 | 2021-04-01 16:20:47 +0000 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | #endif // __BIONIC__ |
| 132 | |
Yifan Hong | 0d2bd11 | 2021-04-13 17:38:36 -0700 | [diff] [blame] | 133 | class SocketAddressImpl : public RpcConnection::SocketAddress { |
| 134 | public: |
Steven Moreland | 158b63d | 2021-04-26 22:50:06 +0000 | [diff] [blame^] | 135 | SocketAddressImpl(const sockaddr* sockAddr, size_t size, const char* addr, unsigned int port) |
| 136 | : mSockAddr(sockAddr), mSize(size), mAddr(addr), mPort(port) {} |
Yifan Hong | 0d2bd11 | 2021-04-13 17:38:36 -0700 | [diff] [blame] | 137 | [[nodiscard]] std::string toString() const override { |
Steven Moreland | 158b63d | 2021-04-26 22:50:06 +0000 | [diff] [blame^] | 138 | return String8::format("%s:%u", mAddr, mPort).c_str(); |
Yifan Hong | 0d2bd11 | 2021-04-13 17:38:36 -0700 | [diff] [blame] | 139 | } |
Steven Moreland | 158b63d | 2021-04-26 22:50:06 +0000 | [diff] [blame^] | 140 | [[nodiscard]] const sockaddr* addr() const override { return mSockAddr; } |
Yifan Hong | 0d2bd11 | 2021-04-13 17:38:36 -0700 | [diff] [blame] | 141 | [[nodiscard]] size_t addrSize() const override { return mSize; } |
Yifan Hong | 0d2bd11 | 2021-04-13 17:38:36 -0700 | [diff] [blame] | 142 | |
| 143 | private: |
Steven Moreland | 158b63d | 2021-04-26 22:50:06 +0000 | [diff] [blame^] | 144 | const sockaddr* mSockAddr; |
| 145 | size_t mSize; |
| 146 | const char* mAddr; |
| 147 | unsigned int mPort; |
Yifan Hong | 0d2bd11 | 2021-04-13 17:38:36 -0700 | [diff] [blame] | 148 | }; |
| 149 | |
| 150 | AddrInfo GetAddrInfo(const char* addr, unsigned int port) { |
| 151 | addrinfo hint{ |
| 152 | .ai_flags = 0, |
| 153 | .ai_family = AF_UNSPEC, |
| 154 | .ai_socktype = SOCK_STREAM, |
| 155 | .ai_protocol = 0, |
| 156 | }; |
| 157 | addrinfo* aiStart = nullptr; |
| 158 | if (int rc = getaddrinfo(addr, std::to_string(port).data(), &hint, &aiStart); 0 != rc) { |
| 159 | ALOGE("Unable to resolve %s:%u: %s", addr, port, gai_strerror(rc)); |
| 160 | return AddrInfo(nullptr, nullptr); |
| 161 | } |
| 162 | if (aiStart == nullptr) { |
| 163 | ALOGE("Unable to resolve %s:%u: getaddrinfo returns null", addr, port); |
| 164 | return AddrInfo(nullptr, nullptr); |
| 165 | } |
| 166 | return AddrInfo(aiStart, &freeaddrinfo); |
| 167 | } |
| 168 | |
| 169 | bool RpcConnection::setupInetServer(unsigned int port) { |
Steven Moreland | 158b63d | 2021-04-26 22:50:06 +0000 | [diff] [blame^] | 170 | const char* kAddr = "127.0.0.1"; |
| 171 | |
| 172 | auto aiStart = GetAddrInfo(kAddr, port); |
Yifan Hong | 0d2bd11 | 2021-04-13 17:38:36 -0700 | [diff] [blame] | 173 | if (aiStart == nullptr) return false; |
Yifan Hong | 0d2bd11 | 2021-04-13 17:38:36 -0700 | [diff] [blame] | 174 | for (auto ai = aiStart.get(); ai != nullptr; ai = ai->ai_next) { |
Steven Moreland | 158b63d | 2021-04-26 22:50:06 +0000 | [diff] [blame^] | 175 | SocketAddressImpl socketAddress(ai->ai_addr, ai->ai_addrlen, kAddr, port); |
Yifan Hong | 0d2bd11 | 2021-04-13 17:38:36 -0700 | [diff] [blame] | 176 | if (setupSocketServer(socketAddress)) return true; |
| 177 | } |
Steven Moreland | 158b63d | 2021-04-26 22:50:06 +0000 | [diff] [blame^] | 178 | ALOGE("None of the socket address resolved for %s:%u can be set up as inet server.", kAddr, |
Yifan Hong | 0d2bd11 | 2021-04-13 17:38:36 -0700 | [diff] [blame] | 179 | port); |
| 180 | return false; |
| 181 | } |
| 182 | |
| 183 | bool RpcConnection::addInetClient(const char* addr, unsigned int port) { |
| 184 | auto aiStart = GetAddrInfo(addr, port); |
| 185 | if (aiStart == nullptr) return false; |
Yifan Hong | 0d2bd11 | 2021-04-13 17:38:36 -0700 | [diff] [blame] | 186 | for (auto ai = aiStart.get(); ai != nullptr; ai = ai->ai_next) { |
Steven Moreland | 158b63d | 2021-04-26 22:50:06 +0000 | [diff] [blame^] | 187 | SocketAddressImpl socketAddress(ai->ai_addr, ai->ai_addrlen, addr, port); |
Yifan Hong | 0d2bd11 | 2021-04-13 17:38:36 -0700 | [diff] [blame] | 188 | if (addSocketClient(socketAddress)) return true; |
| 189 | } |
| 190 | ALOGE("None of the socket address resolved for %s:%u can be added as inet client.", addr, port); |
| 191 | return false; |
| 192 | } |
| 193 | |
Steven Moreland | d47b32c | 2021-04-13 02:03:08 +0000 | [diff] [blame] | 194 | bool RpcConnection::addNullDebuggingClient() { |
| 195 | unique_fd serverFd(TEMP_FAILURE_RETRY(open("/dev/null", O_WRONLY | O_CLOEXEC))); |
| 196 | |
| 197 | if (serverFd == -1) { |
| 198 | ALOGE("Could not connect to /dev/null: %s", strerror(errno)); |
| 199 | return false; |
| 200 | } |
| 201 | |
| 202 | addClient(std::move(serverFd)); |
| 203 | return true; |
| 204 | } |
| 205 | |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 206 | sp<IBinder> RpcConnection::getRootObject() { |
Steven Moreland | 1a3a8ef | 2021-04-02 02:52:46 +0000 | [diff] [blame] | 207 | ExclusiveSocket socket(sp<RpcConnection>::fromExisting(this), SocketUse::CLIENT); |
| 208 | return state()->getRootObject(socket.fd(), sp<RpcConnection>::fromExisting(this)); |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 209 | } |
| 210 | |
| 211 | status_t RpcConnection::transact(const RpcAddress& address, uint32_t code, const Parcel& data, |
| 212 | Parcel* reply, uint32_t flags) { |
Steven Moreland | 1a3a8ef | 2021-04-02 02:52:46 +0000 | [diff] [blame] | 213 | ExclusiveSocket socket(sp<RpcConnection>::fromExisting(this), |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 214 | (flags & IBinder::FLAG_ONEWAY) ? SocketUse::CLIENT_ASYNC |
| 215 | : SocketUse::CLIENT); |
Steven Moreland | 1a3a8ef | 2021-04-02 02:52:46 +0000 | [diff] [blame] | 216 | return state()->transact(socket.fd(), address, code, data, |
| 217 | sp<RpcConnection>::fromExisting(this), reply, flags); |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 218 | } |
| 219 | |
| 220 | status_t RpcConnection::sendDecStrong(const RpcAddress& address) { |
Steven Moreland | 1a3a8ef | 2021-04-02 02:52:46 +0000 | [diff] [blame] | 221 | ExclusiveSocket socket(sp<RpcConnection>::fromExisting(this), SocketUse::CLIENT_REFCOUNT); |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 222 | return state()->sendDecStrong(socket.fd(), address); |
| 223 | } |
| 224 | |
| 225 | void RpcConnection::join() { |
Steven Moreland | af816d8 | 2021-04-19 23:11:33 +0000 | [diff] [blame] | 226 | // TODO(b/185167543): do this dynamically, instead of from a static number |
| 227 | // of threads |
| 228 | unique_fd clientFd( |
| 229 | TEMP_FAILURE_RETRY(accept4(mServer.get(), nullptr, 0 /*length*/, SOCK_CLOEXEC))); |
| 230 | if (clientFd < 0) { |
| 231 | // If this log becomes confusing, should save more state from setupUnixDomainServer |
| 232 | // in order to output here. |
| 233 | ALOGE("Could not accept4 socket: %s", strerror(errno)); |
| 234 | return; |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 235 | } |
| 236 | |
Steven Moreland | af816d8 | 2021-04-19 23:11:33 +0000 | [diff] [blame] | 237 | LOG_RPC_DETAIL("accept4 on fd %d yields fd %d", mServer.get(), clientFd.get()); |
| 238 | |
| 239 | // must be registered to allow arbitrary client code executing commands to |
| 240 | // be able to do nested calls (we can't only read from it) |
| 241 | sp<ConnectionSocket> socket = assignServerToThisThread(std::move(clientFd)); |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 242 | |
| 243 | while (true) { |
Steven Moreland | 1a3a8ef | 2021-04-02 02:52:46 +0000 | [diff] [blame] | 244 | status_t error = |
Steven Moreland | af816d8 | 2021-04-19 23:11:33 +0000 | [diff] [blame] | 245 | state()->getAndExecuteCommand(socket->fd, sp<RpcConnection>::fromExisting(this)); |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 246 | |
| 247 | if (error != OK) { |
| 248 | ALOGI("Binder socket thread closing w/ status %s", statusToString(error).c_str()); |
Steven Moreland | af816d8 | 2021-04-19 23:11:33 +0000 | [diff] [blame] | 249 | break; |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 250 | } |
| 251 | } |
Steven Moreland | af816d8 | 2021-04-19 23:11:33 +0000 | [diff] [blame] | 252 | |
| 253 | LOG_ALWAYS_FATAL_IF(!removeServerSocket(socket), |
| 254 | "bad state: socket object guaranteed to be in list"); |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 255 | } |
| 256 | |
| 257 | void RpcConnection::setForServer(const wp<RpcServer>& server) { |
| 258 | mForServer = server; |
| 259 | } |
| 260 | |
| 261 | wp<RpcServer> RpcConnection::server() { |
| 262 | return mForServer; |
| 263 | } |
| 264 | |
Steven Moreland | d47b32c | 2021-04-13 02:03:08 +0000 | [diff] [blame] | 265 | bool RpcConnection::setupSocketServer(const SocketAddress& addr) { |
Steven Moreland | c163595 | 2021-04-01 16:20:47 +0000 | [diff] [blame] | 266 | LOG_ALWAYS_FATAL_IF(mServer.get() != -1, "Each RpcConnection can only have one server."); |
| 267 | |
| 268 | unique_fd serverFd( |
| 269 | TEMP_FAILURE_RETRY(socket(addr.addr()->sa_family, SOCK_STREAM | SOCK_CLOEXEC, 0))); |
| 270 | if (serverFd == -1) { |
| 271 | ALOGE("Could not create socket: %s", strerror(errno)); |
| 272 | return false; |
| 273 | } |
| 274 | |
| 275 | if (0 != TEMP_FAILURE_RETRY(bind(serverFd.get(), addr.addr(), addr.addrSize()))) { |
| 276 | int savedErrno = errno; |
| 277 | ALOGE("Could not bind socket at %s: %s", addr.toString().c_str(), strerror(savedErrno)); |
| 278 | return false; |
| 279 | } |
| 280 | |
| 281 | if (0 != TEMP_FAILURE_RETRY(listen(serverFd.get(), 1 /*backlog*/))) { |
| 282 | int savedErrno = errno; |
| 283 | ALOGE("Could not listen socket at %s: %s", addr.toString().c_str(), strerror(savedErrno)); |
| 284 | return false; |
| 285 | } |
| 286 | |
| 287 | mServer = std::move(serverFd); |
| 288 | return true; |
Steven Moreland | 5358354 | 2021-03-30 00:25:41 +0000 | [diff] [blame] | 289 | } |
| 290 | |
Steven Moreland | d47b32c | 2021-04-13 02:03:08 +0000 | [diff] [blame] | 291 | bool RpcConnection::addSocketClient(const SocketAddress& addr) { |
Steven Moreland | c163595 | 2021-04-01 16:20:47 +0000 | [diff] [blame] | 292 | unique_fd serverFd( |
| 293 | TEMP_FAILURE_RETRY(socket(addr.addr()->sa_family, SOCK_STREAM | SOCK_CLOEXEC, 0))); |
| 294 | if (serverFd == -1) { |
| 295 | int savedErrno = errno; |
| 296 | ALOGE("Could not create socket at %s: %s", addr.toString().c_str(), strerror(savedErrno)); |
| 297 | return false; |
| 298 | } |
| 299 | |
| 300 | if (0 != TEMP_FAILURE_RETRY(connect(serverFd.get(), addr.addr(), addr.addrSize()))) { |
| 301 | int savedErrno = errno; |
| 302 | ALOGE("Could not connect socket at %s: %s", addr.toString().c_str(), strerror(savedErrno)); |
| 303 | return false; |
| 304 | } |
| 305 | |
| 306 | LOG_RPC_DETAIL("Socket at %s client with fd %d", addr.toString().c_str(), serverFd.get()); |
| 307 | |
Steven Moreland | d47b32c | 2021-04-13 02:03:08 +0000 | [diff] [blame] | 308 | addClient(std::move(serverFd)); |
Steven Moreland | c163595 | 2021-04-01 16:20:47 +0000 | [diff] [blame] | 309 | return true; |
| 310 | } |
| 311 | |
Steven Moreland | d47b32c | 2021-04-13 02:03:08 +0000 | [diff] [blame] | 312 | void RpcConnection::addClient(unique_fd&& fd) { |
| 313 | std::lock_guard<std::mutex> _l(mSocketMutex); |
| 314 | sp<ConnectionSocket> connection = sp<ConnectionSocket>::make(); |
| 315 | connection->fd = std::move(fd); |
| 316 | mClients.push_back(connection); |
| 317 | } |
| 318 | |
Steven Moreland | af816d8 | 2021-04-19 23:11:33 +0000 | [diff] [blame] | 319 | sp<RpcConnection::ConnectionSocket> RpcConnection::assignServerToThisThread(unique_fd&& fd) { |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 320 | std::lock_guard<std::mutex> _l(mSocketMutex); |
Steven Moreland | 1a3a8ef | 2021-04-02 02:52:46 +0000 | [diff] [blame] | 321 | sp<ConnectionSocket> connection = sp<ConnectionSocket>::make(); |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 322 | connection->fd = std::move(fd); |
Steven Moreland | af816d8 | 2021-04-19 23:11:33 +0000 | [diff] [blame] | 323 | connection->exclusiveTid = gettid(); |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 324 | mServers.push_back(connection); |
Steven Moreland | af816d8 | 2021-04-19 23:11:33 +0000 | [diff] [blame] | 325 | |
| 326 | return connection; |
| 327 | } |
| 328 | |
| 329 | bool RpcConnection::removeServerSocket(const sp<ConnectionSocket>& socket) { |
| 330 | std::lock_guard<std::mutex> _l(mSocketMutex); |
| 331 | if (auto it = std::find(mServers.begin(), mServers.end(), socket); it != mServers.end()) { |
| 332 | mServers.erase(it); |
| 333 | return true; |
| 334 | } |
| 335 | return false; |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 336 | } |
| 337 | |
| 338 | RpcConnection::ExclusiveSocket::ExclusiveSocket(const sp<RpcConnection>& connection, SocketUse use) |
| 339 | : mConnection(connection) { |
| 340 | pid_t tid = gettid(); |
| 341 | std::unique_lock<std::mutex> _l(mConnection->mSocketMutex); |
| 342 | |
| 343 | mConnection->mWaitingThreads++; |
| 344 | while (true) { |
| 345 | sp<ConnectionSocket> exclusive; |
| 346 | sp<ConnectionSocket> available; |
| 347 | |
| 348 | // CHECK FOR DEDICATED CLIENT SOCKET |
| 349 | // |
Steven Moreland | af816d8 | 2021-04-19 23:11:33 +0000 | [diff] [blame] | 350 | // A server/looper should always use a dedicated connection if available |
| 351 | findSocket(tid, &exclusive, &available, mConnection->mClients, mConnection->mClientsOffset); |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 352 | |
Steven Moreland | af816d8 | 2021-04-19 23:11:33 +0000 | [diff] [blame] | 353 | // WARNING: this assumes a server cannot request its client to send |
| 354 | // a transaction, as mServers is excluded below. |
| 355 | // |
| 356 | // Imagine we have more than one thread in play, and a single thread |
| 357 | // sends a synchronous, then an asynchronous command. Imagine the |
| 358 | // asynchronous command is sent on the first client socket. Then, if |
| 359 | // we naively send a synchronous command to that same socket, the |
| 360 | // thread on the far side might be busy processing the asynchronous |
| 361 | // command. So, we move to considering the second available thread |
| 362 | // for subsequent calls. |
| 363 | if (use == SocketUse::CLIENT_ASYNC && (exclusive != nullptr || available != nullptr)) { |
| 364 | mConnection->mClientsOffset = |
| 365 | (mConnection->mClientsOffset + 1) % mConnection->mClients.size(); |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 366 | } |
| 367 | |
Steven Moreland | af816d8 | 2021-04-19 23:11:33 +0000 | [diff] [blame] | 368 | // USE SERVING SOCKET (for nested transaction) |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 369 | // |
| 370 | // asynchronous calls cannot be nested |
| 371 | if (use != SocketUse::CLIENT_ASYNC) { |
Steven Moreland | af816d8 | 2021-04-19 23:11:33 +0000 | [diff] [blame] | 372 | // server sockets are always assigned to a thread |
| 373 | findSocket(tid, &exclusive, nullptr /*available*/, mConnection->mServers, |
| 374 | 0 /* index hint */); |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 375 | } |
| 376 | |
| 377 | // if our thread is already using a connection, prioritize using that |
| 378 | if (exclusive != nullptr) { |
| 379 | mSocket = exclusive; |
| 380 | mReentrant = true; |
| 381 | break; |
| 382 | } else if (available != nullptr) { |
| 383 | mSocket = available; |
| 384 | mSocket->exclusiveTid = tid; |
| 385 | break; |
| 386 | } |
| 387 | |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 388 | // in regular binder, this would usually be a deadlock :) |
| 389 | LOG_ALWAYS_FATAL_IF(mConnection->mClients.size() == 0, |
| 390 | "Not a client of any connection. You must create a connection to an " |
| 391 | "RPC server to make any non-nested (e.g. oneway or on another thread) " |
| 392 | "calls."); |
| 393 | |
| 394 | LOG_RPC_DETAIL("No available connection (have %zu clients and %zu servers). Waiting...", |
| 395 | mConnection->mClients.size(), mConnection->mServers.size()); |
| 396 | mConnection->mSocketCv.wait(_l); |
| 397 | } |
| 398 | mConnection->mWaitingThreads--; |
| 399 | } |
| 400 | |
| 401 | void RpcConnection::ExclusiveSocket::findSocket(pid_t tid, sp<ConnectionSocket>* exclusive, |
| 402 | sp<ConnectionSocket>* available, |
| 403 | std::vector<sp<ConnectionSocket>>& sockets, |
| 404 | size_t socketsIndexHint) { |
| 405 | LOG_ALWAYS_FATAL_IF(sockets.size() > 0 && socketsIndexHint >= sockets.size(), |
| 406 | "Bad index %zu >= %zu", socketsIndexHint, sockets.size()); |
| 407 | |
| 408 | if (*exclusive != nullptr) return; // consistent with break below |
| 409 | |
| 410 | for (size_t i = 0; i < sockets.size(); i++) { |
| 411 | sp<ConnectionSocket>& socket = sockets[(i + socketsIndexHint) % sockets.size()]; |
| 412 | |
| 413 | // take first available connection (intuition = caching) |
| 414 | if (available && *available == nullptr && socket->exclusiveTid == std::nullopt) { |
| 415 | *available = socket; |
| 416 | continue; |
| 417 | } |
| 418 | |
| 419 | // though, prefer to take connection which is already inuse by this thread |
| 420 | // (nested transactions) |
| 421 | if (exclusive && socket->exclusiveTid == tid) { |
| 422 | *exclusive = socket; |
| 423 | break; // consistent with return above |
| 424 | } |
| 425 | } |
| 426 | } |
| 427 | |
| 428 | RpcConnection::ExclusiveSocket::~ExclusiveSocket() { |
| 429 | // reentrant use of a connection means something less deep in the call stack |
| 430 | // is using this fd, and it retains the right to it. So, we don't give up |
| 431 | // exclusive ownership, and no thread is freed. |
| 432 | if (!mReentrant) { |
| 433 | std::unique_lock<std::mutex> _l(mConnection->mSocketMutex); |
| 434 | mSocket->exclusiveTid = std::nullopt; |
| 435 | if (mConnection->mWaitingThreads > 0) { |
| 436 | _l.unlock(); |
| 437 | mConnection->mSocketCv.notify_one(); |
| 438 | } |
| 439 | } |
| 440 | } |
| 441 | |
| 442 | } // namespace android |