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 "RpcServer" |
| 18 | |
| 19 | #include <sys/socket.h> |
| 20 | #include <sys/un.h> |
| 21 | |
Steven Moreland | f137de9 | 2021-04-24 01:54:26 +0000 | [diff] [blame] | 22 | #include <thread> |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 23 | #include <vector> |
| 24 | |
| 25 | #include <binder/Parcel.h> |
| 26 | #include <binder/RpcServer.h> |
| 27 | #include <log/log.h> |
| 28 | #include "RpcState.h" |
| 29 | |
Steven Moreland | 611d15f | 2021-05-01 01:28:27 +0000 | [diff] [blame] | 30 | #include "RpcSocketAddress.h" |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 31 | #include "RpcWireFormat.h" |
| 32 | |
| 33 | namespace android { |
| 34 | |
Steven Moreland | 611d15f | 2021-05-01 01:28:27 +0000 | [diff] [blame] | 35 | using base::unique_fd; |
| 36 | |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 37 | RpcServer::RpcServer() {} |
| 38 | RpcServer::~RpcServer() {} |
| 39 | |
| 40 | sp<RpcServer> RpcServer::make() { |
Steven Moreland | 1a3a8ef | 2021-04-02 02:52:46 +0000 | [diff] [blame] | 41 | return sp<RpcServer>::make(); |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 42 | } |
| 43 | |
| 44 | void RpcServer::iUnderstandThisCodeIsExperimentalAndIWillNotUseItInProduction() { |
| 45 | mAgreedExperimental = true; |
| 46 | } |
| 47 | |
Steven Moreland | 611d15f | 2021-05-01 01:28:27 +0000 | [diff] [blame] | 48 | bool RpcServer::setupUnixDomainServer(const char* path) { |
| 49 | return setupSocketServer(UnixSocketAddress(path)); |
| 50 | } |
| 51 | |
Steven Moreland | 611d15f | 2021-05-01 01:28:27 +0000 | [diff] [blame] | 52 | bool RpcServer::setupVsockServer(unsigned int port) { |
| 53 | // realizing value w/ this type at compile time to avoid ubsan abort |
| 54 | constexpr unsigned int kAnyCid = VMADDR_CID_ANY; |
| 55 | |
| 56 | return setupSocketServer(VsockSocketAddress(kAnyCid, port)); |
| 57 | } |
| 58 | |
Steven Moreland | 611d15f | 2021-05-01 01:28:27 +0000 | [diff] [blame] | 59 | bool RpcServer::setupInetServer(unsigned int port, unsigned int* assignedPort) { |
| 60 | const char* kAddr = "127.0.0.1"; |
| 61 | |
| 62 | if (assignedPort != nullptr) *assignedPort = 0; |
| 63 | auto aiStart = InetSocketAddress::getAddrInfo(kAddr, port); |
| 64 | if (aiStart == nullptr) return false; |
| 65 | for (auto ai = aiStart.get(); ai != nullptr; ai = ai->ai_next) { |
| 66 | InetSocketAddress socketAddress(ai->ai_addr, ai->ai_addrlen, kAddr, port); |
| 67 | if (!setupSocketServer(socketAddress)) { |
| 68 | continue; |
| 69 | } |
| 70 | |
| 71 | LOG_ALWAYS_FATAL_IF(socketAddress.addr()->sa_family != AF_INET, "expecting inet"); |
| 72 | sockaddr_in addr{}; |
| 73 | socklen_t len = sizeof(addr); |
| 74 | if (0 != getsockname(mServer.get(), reinterpret_cast<sockaddr*>(&addr), &len)) { |
| 75 | int savedErrno = errno; |
| 76 | ALOGE("Could not getsockname at %s: %s", socketAddress.toString().c_str(), |
| 77 | strerror(savedErrno)); |
| 78 | return false; |
| 79 | } |
| 80 | LOG_ALWAYS_FATAL_IF(len != sizeof(addr), "Wrong socket type: len %zu vs len %zu", |
| 81 | static_cast<size_t>(len), sizeof(addr)); |
| 82 | unsigned int realPort = ntohs(addr.sin_port); |
| 83 | LOG_ALWAYS_FATAL_IF(port != 0 && realPort != port, |
| 84 | "Requesting inet server on %s but it is set up on %u.", |
| 85 | socketAddress.toString().c_str(), realPort); |
| 86 | |
| 87 | if (assignedPort != nullptr) { |
| 88 | *assignedPort = realPort; |
| 89 | } |
| 90 | |
| 91 | return true; |
| 92 | } |
| 93 | ALOGE("None of the socket address resolved for %s:%u can be set up as inet server.", kAddr, |
| 94 | port); |
| 95 | return false; |
| 96 | } |
| 97 | |
Steven Moreland | f137de9 | 2021-04-24 01:54:26 +0000 | [diff] [blame] | 98 | void RpcServer::setMaxThreads(size_t threads) { |
| 99 | LOG_ALWAYS_FATAL_IF(threads <= 0, "RpcServer is useless without threads"); |
Steven Moreland | 611d15f | 2021-05-01 01:28:27 +0000 | [diff] [blame] | 100 | LOG_ALWAYS_FATAL_IF(mStarted, "must be called before started"); |
Steven Moreland | f137de9 | 2021-04-24 01:54:26 +0000 | [diff] [blame] | 101 | mMaxThreads = threads; |
| 102 | } |
| 103 | |
| 104 | size_t RpcServer::getMaxThreads() { |
| 105 | return mMaxThreads; |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | void RpcServer::setRootObject(const sp<IBinder>& binder) { |
Steven Moreland | ebafe33 | 2021-04-24 00:24:35 +0000 | [diff] [blame] | 109 | std::lock_guard<std::mutex> _l(mLock); |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 110 | mRootObject = binder; |
| 111 | } |
| 112 | |
| 113 | sp<IBinder> RpcServer::getRootObject() { |
Steven Moreland | ebafe33 | 2021-04-24 00:24:35 +0000 | [diff] [blame] | 114 | std::lock_guard<std::mutex> _l(mLock); |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 115 | return mRootObject; |
| 116 | } |
| 117 | |
Steven Moreland | 611d15f | 2021-05-01 01:28:27 +0000 | [diff] [blame] | 118 | void RpcServer::join() { |
Steven Moreland | f137de9 | 2021-04-24 01:54:26 +0000 | [diff] [blame] | 119 | LOG_ALWAYS_FATAL_IF(!mAgreedExperimental, "no!"); |
Steven Moreland | f137de9 | 2021-04-24 01:54:26 +0000 | [diff] [blame] | 120 | { |
| 121 | std::lock_guard<std::mutex> _l(mLock); |
Steven Moreland | 611d15f | 2021-05-01 01:28:27 +0000 | [diff] [blame] | 122 | LOG_ALWAYS_FATAL_IF(mServer.get() == -1, "RpcServer must be setup to join."); |
Steven Moreland | f137de9 | 2021-04-24 01:54:26 +0000 | [diff] [blame] | 123 | } |
| 124 | |
Steven Moreland | 736664b | 2021-05-01 04:27:25 +0000 | [diff] [blame] | 125 | while (true) { |
Yifan Hong | 00db74d | 2021-05-07 18:42:46 -0700 | [diff] [blame] | 126 | unique_fd clientFd(TEMP_FAILURE_RETRY( |
| 127 | accept4(mServer.get(), nullptr, nullptr /*length*/, SOCK_CLOEXEC))); |
Steven Moreland | 736664b | 2021-05-01 04:27:25 +0000 | [diff] [blame] | 128 | |
| 129 | if (clientFd < 0) { |
| 130 | ALOGE("Could not accept4 socket: %s", strerror(errno)); |
| 131 | continue; |
| 132 | } |
| 133 | LOG_RPC_DETAIL("accept4 on fd %d yields fd %d", mServer.get(), clientFd.get()); |
| 134 | |
Steven Moreland | 736664b | 2021-05-01 04:27:25 +0000 | [diff] [blame] | 135 | { |
| 136 | std::lock_guard<std::mutex> _l(mLock); |
Steven Moreland | a63ff93 | 2021-05-12 00:03:15 +0000 | [diff] [blame^] | 137 | std::thread thread = |
| 138 | std::thread(&RpcServer::establishConnection, this, |
| 139 | std::move(sp<RpcServer>::fromExisting(this)), std::move(clientFd)); |
| 140 | mConnectingThreads[thread.get_id()] = std::move(thread); |
Steven Moreland | 736664b | 2021-05-01 04:27:25 +0000 | [diff] [blame] | 141 | } |
| 142 | } |
Steven Moreland | f137de9 | 2021-04-24 01:54:26 +0000 | [diff] [blame] | 143 | } |
| 144 | |
Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 145 | std::vector<sp<RpcSession>> RpcServer::listSessions() { |
Steven Moreland | 611d15f | 2021-05-01 01:28:27 +0000 | [diff] [blame] | 146 | std::lock_guard<std::mutex> _l(mLock); |
Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 147 | std::vector<sp<RpcSession>> sessions; |
| 148 | for (auto& [id, session] : mSessions) { |
Steven Moreland | 736664b | 2021-05-01 04:27:25 +0000 | [diff] [blame] | 149 | (void)id; |
Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 150 | sessions.push_back(session); |
Steven Moreland | 736664b | 2021-05-01 04:27:25 +0000 | [diff] [blame] | 151 | } |
Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 152 | return sessions; |
Steven Moreland | 611d15f | 2021-05-01 01:28:27 +0000 | [diff] [blame] | 153 | } |
| 154 | |
Steven Moreland | a63ff93 | 2021-05-12 00:03:15 +0000 | [diff] [blame^] | 155 | void RpcServer::establishConnection(sp<RpcServer>&& server, base::unique_fd clientFd) { |
| 156 | LOG_ALWAYS_FATAL_IF(this != server.get(), "Must pass same ownership object"); |
| 157 | |
| 158 | // TODO(b/183988761): cannot trust this simple ID |
| 159 | LOG_ALWAYS_FATAL_IF(!mAgreedExperimental, "no!"); |
| 160 | int32_t id; |
| 161 | if (sizeof(id) != read(clientFd.get(), &id, sizeof(id))) { |
| 162 | ALOGE("Could not read ID from fd %d", clientFd.get()); |
| 163 | return; |
| 164 | } |
| 165 | |
| 166 | std::thread thisThread; |
| 167 | sp<RpcSession> session; |
| 168 | { |
| 169 | std::lock_guard<std::mutex> _l(mLock); |
| 170 | |
| 171 | auto threadId = mConnectingThreads.find(std::this_thread::get_id()); |
| 172 | LOG_ALWAYS_FATAL_IF(threadId == mConnectingThreads.end(), |
| 173 | "Must establish connection on owned thread"); |
| 174 | thisThread = std::move(threadId->second); |
| 175 | mConnectingThreads.erase(threadId); |
| 176 | |
| 177 | if (id == RPC_SESSION_ID_NEW) { |
| 178 | LOG_ALWAYS_FATAL_IF(mSessionIdCounter >= INT32_MAX, "Out of session IDs"); |
| 179 | mSessionIdCounter++; |
| 180 | |
| 181 | session = RpcSession::make(); |
| 182 | session->setForServer(wp<RpcServer>::fromExisting(this), mSessionIdCounter); |
| 183 | |
| 184 | mSessions[mSessionIdCounter] = session; |
| 185 | } else { |
| 186 | auto it = mSessions.find(id); |
| 187 | if (it == mSessions.end()) { |
| 188 | ALOGE("Cannot add thread, no record of session with ID %d", id); |
| 189 | return; |
| 190 | } |
| 191 | session = it->second; |
| 192 | } |
| 193 | } |
| 194 | |
| 195 | // avoid strong cycle |
| 196 | server = nullptr; |
| 197 | // |
| 198 | // |
| 199 | // DO NOT ACCESS MEMBER VARIABLES BELOW |
| 200 | // |
| 201 | |
| 202 | session->join(std::move(thisThread), std::move(clientFd)); |
| 203 | } |
| 204 | |
Steven Moreland | 611d15f | 2021-05-01 01:28:27 +0000 | [diff] [blame] | 205 | bool RpcServer::setupSocketServer(const RpcSocketAddress& addr) { |
Steven Moreland | 704fc1a | 2021-05-04 23:13:14 +0000 | [diff] [blame] | 206 | LOG_RPC_DETAIL("Setting up socket server %s", addr.toString().c_str()); |
| 207 | |
Steven Moreland | 611d15f | 2021-05-01 01:28:27 +0000 | [diff] [blame] | 208 | { |
| 209 | std::lock_guard<std::mutex> _l(mLock); |
| 210 | LOG_ALWAYS_FATAL_IF(mServer.get() != -1, "Each RpcServer can only have one server."); |
| 211 | } |
| 212 | |
| 213 | unique_fd serverFd( |
| 214 | TEMP_FAILURE_RETRY(socket(addr.addr()->sa_family, SOCK_STREAM | SOCK_CLOEXEC, 0))); |
| 215 | if (serverFd == -1) { |
| 216 | ALOGE("Could not create socket: %s", strerror(errno)); |
| 217 | return false; |
| 218 | } |
| 219 | |
| 220 | if (0 != TEMP_FAILURE_RETRY(bind(serverFd.get(), addr.addr(), addr.addrSize()))) { |
| 221 | int savedErrno = errno; |
| 222 | ALOGE("Could not bind socket at %s: %s", addr.toString().c_str(), strerror(savedErrno)); |
| 223 | return false; |
| 224 | } |
| 225 | |
| 226 | if (0 != TEMP_FAILURE_RETRY(listen(serverFd.get(), 1 /*backlog*/))) { |
| 227 | int savedErrno = errno; |
| 228 | ALOGE("Could not listen socket at %s: %s", addr.toString().c_str(), strerror(savedErrno)); |
| 229 | return false; |
| 230 | } |
| 231 | |
Steven Moreland | 704fc1a | 2021-05-04 23:13:14 +0000 | [diff] [blame] | 232 | LOG_RPC_DETAIL("Successfully setup socket server %s", addr.toString().c_str()); |
| 233 | |
Steven Moreland | 611d15f | 2021-05-01 01:28:27 +0000 | [diff] [blame] | 234 | mServer = std::move(serverFd); |
| 235 | return true; |
| 236 | } |
| 237 | |
Steven Moreland | ee78e76 | 2021-05-05 21:12:51 +0000 | [diff] [blame] | 238 | void RpcServer::onSessionTerminating(const sp<RpcSession>& session) { |
| 239 | auto id = session->mId; |
| 240 | LOG_ALWAYS_FATAL_IF(id == std::nullopt, "Server sessions must be initialized with ID"); |
| 241 | LOG_RPC_DETAIL("Dropping session %d", *id); |
| 242 | |
| 243 | std::lock_guard<std::mutex> _l(mLock); |
| 244 | auto it = mSessions.find(*id); |
| 245 | LOG_ALWAYS_FATAL_IF(it == mSessions.end(), "Bad state, unknown session id %d", *id); |
| 246 | LOG_ALWAYS_FATAL_IF(it->second != session, "Bad state, session has id mismatch %d", *id); |
| 247 | (void)mSessions.erase(it); |
| 248 | } |
| 249 | |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 250 | } // namespace android |