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 | |
| 52 | #ifdef __BIONIC__ |
| 53 | |
| 54 | bool RpcServer::setupVsockServer(unsigned int port) { |
| 55 | // realizing value w/ this type at compile time to avoid ubsan abort |
| 56 | constexpr unsigned int kAnyCid = VMADDR_CID_ANY; |
| 57 | |
| 58 | return setupSocketServer(VsockSocketAddress(kAnyCid, port)); |
| 59 | } |
| 60 | |
| 61 | #endif // __BIONIC__ |
| 62 | |
| 63 | bool RpcServer::setupInetServer(unsigned int port, unsigned int* assignedPort) { |
| 64 | const char* kAddr = "127.0.0.1"; |
| 65 | |
| 66 | if (assignedPort != nullptr) *assignedPort = 0; |
| 67 | auto aiStart = InetSocketAddress::getAddrInfo(kAddr, port); |
| 68 | if (aiStart == nullptr) return false; |
| 69 | for (auto ai = aiStart.get(); ai != nullptr; ai = ai->ai_next) { |
| 70 | InetSocketAddress socketAddress(ai->ai_addr, ai->ai_addrlen, kAddr, port); |
| 71 | if (!setupSocketServer(socketAddress)) { |
| 72 | continue; |
| 73 | } |
| 74 | |
| 75 | LOG_ALWAYS_FATAL_IF(socketAddress.addr()->sa_family != AF_INET, "expecting inet"); |
| 76 | sockaddr_in addr{}; |
| 77 | socklen_t len = sizeof(addr); |
| 78 | if (0 != getsockname(mServer.get(), reinterpret_cast<sockaddr*>(&addr), &len)) { |
| 79 | int savedErrno = errno; |
| 80 | ALOGE("Could not getsockname at %s: %s", socketAddress.toString().c_str(), |
| 81 | strerror(savedErrno)); |
| 82 | return false; |
| 83 | } |
| 84 | LOG_ALWAYS_FATAL_IF(len != sizeof(addr), "Wrong socket type: len %zu vs len %zu", |
| 85 | static_cast<size_t>(len), sizeof(addr)); |
| 86 | unsigned int realPort = ntohs(addr.sin_port); |
| 87 | LOG_ALWAYS_FATAL_IF(port != 0 && realPort != port, |
| 88 | "Requesting inet server on %s but it is set up on %u.", |
| 89 | socketAddress.toString().c_str(), realPort); |
| 90 | |
| 91 | if (assignedPort != nullptr) { |
| 92 | *assignedPort = realPort; |
| 93 | } |
| 94 | |
| 95 | return true; |
| 96 | } |
| 97 | ALOGE("None of the socket address resolved for %s:%u can be set up as inet server.", kAddr, |
| 98 | port); |
| 99 | return false; |
| 100 | } |
| 101 | |
Steven Moreland | f137de9 | 2021-04-24 01:54:26 +0000 | [diff] [blame] | 102 | void RpcServer::setMaxThreads(size_t threads) { |
| 103 | LOG_ALWAYS_FATAL_IF(threads <= 0, "RpcServer is useless without threads"); |
Steven Moreland | 611d15f | 2021-05-01 01:28:27 +0000 | [diff] [blame] | 104 | LOG_ALWAYS_FATAL_IF(mStarted, "must be called before started"); |
Steven Moreland | f137de9 | 2021-04-24 01:54:26 +0000 | [diff] [blame] | 105 | mMaxThreads = threads; |
| 106 | } |
| 107 | |
| 108 | size_t RpcServer::getMaxThreads() { |
| 109 | return mMaxThreads; |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | void RpcServer::setRootObject(const sp<IBinder>& binder) { |
Steven Moreland | ebafe33 | 2021-04-24 00:24:35 +0000 | [diff] [blame] | 113 | std::lock_guard<std::mutex> _l(mLock); |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 114 | mRootObject = binder; |
| 115 | } |
| 116 | |
| 117 | sp<IBinder> RpcServer::getRootObject() { |
Steven Moreland | ebafe33 | 2021-04-24 00:24:35 +0000 | [diff] [blame] | 118 | std::lock_guard<std::mutex> _l(mLock); |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 119 | return mRootObject; |
| 120 | } |
| 121 | |
Steven Moreland | 611d15f | 2021-05-01 01:28:27 +0000 | [diff] [blame] | 122 | void RpcServer::join() { |
Steven Moreland | f137de9 | 2021-04-24 01:54:26 +0000 | [diff] [blame] | 123 | LOG_ALWAYS_FATAL_IF(!mAgreedExperimental, "no!"); |
| 124 | |
Steven Moreland | f137de9 | 2021-04-24 01:54:26 +0000 | [diff] [blame] | 125 | std::vector<std::thread> pool; |
| 126 | { |
| 127 | std::lock_guard<std::mutex> _l(mLock); |
Steven Moreland | 611d15f | 2021-05-01 01:28:27 +0000 | [diff] [blame] | 128 | 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] | 129 | } |
| 130 | |
Steven Moreland | 736664b | 2021-05-01 04:27:25 +0000 | [diff] [blame^] | 131 | while (true) { |
| 132 | unique_fd clientFd( |
| 133 | TEMP_FAILURE_RETRY(accept4(mServer.get(), nullptr, 0 /*length*/, SOCK_CLOEXEC))); |
| 134 | |
| 135 | if (clientFd < 0) { |
| 136 | ALOGE("Could not accept4 socket: %s", strerror(errno)); |
| 137 | continue; |
| 138 | } |
| 139 | LOG_RPC_DETAIL("accept4 on fd %d yields fd %d", mServer.get(), clientFd.get()); |
| 140 | |
| 141 | // TODO(b/183988761): cannot trust this simple ID |
| 142 | LOG_ALWAYS_FATAL_IF(!mAgreedExperimental, "no!"); |
| 143 | int32_t id; |
| 144 | if (sizeof(id) != read(clientFd.get(), &id, sizeof(id))) { |
| 145 | ALOGE("Could not read ID from fd %d", clientFd.get()); |
| 146 | continue; |
| 147 | } |
| 148 | |
| 149 | { |
| 150 | std::lock_guard<std::mutex> _l(mLock); |
| 151 | |
| 152 | sp<RpcConnection> connection; |
| 153 | if (id == RPC_CONNECTION_ID_NEW) { |
| 154 | // new client! |
| 155 | LOG_ALWAYS_FATAL_IF(mConnectionIdCounter >= INT32_MAX, "Out of connection IDs"); |
| 156 | mConnectionIdCounter++; |
| 157 | |
| 158 | connection = RpcConnection::make(); |
| 159 | connection->setForServer(wp<RpcServer>::fromExisting(this), mConnectionIdCounter); |
| 160 | |
| 161 | mConnections[mConnectionIdCounter] = connection; |
| 162 | } else { |
| 163 | auto it = mConnections.find(id); |
| 164 | if (it == mConnections.end()) { |
| 165 | ALOGE("Cannot add thread, no record of connection with ID %d", id); |
| 166 | continue; |
| 167 | } |
| 168 | connection = it->second; |
| 169 | } |
| 170 | |
| 171 | connection->startThread(std::move(clientFd)); |
| 172 | } |
| 173 | } |
Steven Moreland | f137de9 | 2021-04-24 01:54:26 +0000 | [diff] [blame] | 174 | } |
| 175 | |
Steven Moreland | 611d15f | 2021-05-01 01:28:27 +0000 | [diff] [blame] | 176 | std::vector<sp<RpcConnection>> RpcServer::listConnections() { |
| 177 | std::lock_guard<std::mutex> _l(mLock); |
Steven Moreland | 736664b | 2021-05-01 04:27:25 +0000 | [diff] [blame^] | 178 | std::vector<sp<RpcConnection>> connections; |
| 179 | for (auto& [id, connection] : mConnections) { |
| 180 | (void)id; |
| 181 | connections.push_back(connection); |
| 182 | } |
| 183 | return connections; |
Steven Moreland | 611d15f | 2021-05-01 01:28:27 +0000 | [diff] [blame] | 184 | } |
| 185 | |
| 186 | bool RpcServer::setupSocketServer(const RpcSocketAddress& addr) { |
| 187 | { |
| 188 | std::lock_guard<std::mutex> _l(mLock); |
| 189 | LOG_ALWAYS_FATAL_IF(mServer.get() != -1, "Each RpcServer can only have one server."); |
| 190 | } |
| 191 | |
| 192 | unique_fd serverFd( |
| 193 | TEMP_FAILURE_RETRY(socket(addr.addr()->sa_family, SOCK_STREAM | SOCK_CLOEXEC, 0))); |
| 194 | if (serverFd == -1) { |
| 195 | ALOGE("Could not create socket: %s", strerror(errno)); |
| 196 | return false; |
| 197 | } |
| 198 | |
| 199 | if (0 != TEMP_FAILURE_RETRY(bind(serverFd.get(), addr.addr(), addr.addrSize()))) { |
| 200 | int savedErrno = errno; |
| 201 | ALOGE("Could not bind socket at %s: %s", addr.toString().c_str(), strerror(savedErrno)); |
| 202 | return false; |
| 203 | } |
| 204 | |
| 205 | if (0 != TEMP_FAILURE_RETRY(listen(serverFd.get(), 1 /*backlog*/))) { |
| 206 | int savedErrno = errno; |
| 207 | ALOGE("Could not listen socket at %s: %s", addr.toString().c_str(), strerror(savedErrno)); |
| 208 | return false; |
| 209 | } |
| 210 | |
| 211 | mServer = std::move(serverFd); |
| 212 | return true; |
| 213 | } |
| 214 | |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 215 | } // namespace android |