Yifan Hong | dbf58d4 | 2021-06-03 19:21:11 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2021 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 | |
Yifan Hong | dbf58d4 | 2021-06-03 19:21:11 -0700 | [diff] [blame] | 17 | #include <sysexits.h> |
| 18 | #include <unistd.h> |
| 19 | |
| 20 | #include <iostream> |
| 21 | |
| 22 | #include <android-base/file.h> |
| 23 | #include <android-base/logging.h> |
Yifan Hong | dbf58d4 | 2021-06-03 19:21:11 -0700 | [diff] [blame] | 24 | #include <android-base/properties.h> |
| 25 | #include <android-base/stringprintf.h> |
Yifan Hong | bd8ca57 | 2021-06-14 19:58:34 -0700 | [diff] [blame] | 26 | #include <android/os/BnServiceManager.h> |
Yifan Hong | e435882 | 2021-06-14 12:49:43 -0700 | [diff] [blame] | 27 | #include <android/os/IServiceManager.h> |
Yifan Hong | dbf58d4 | 2021-06-03 19:21:11 -0700 | [diff] [blame] | 28 | #include <binder/IServiceManager.h> |
| 29 | #include <binder/RpcServer.h> |
| 30 | |
Yifan Hong | 02530ec | 2021-06-10 13:38:38 -0700 | [diff] [blame] | 31 | using android::BBinder; |
Yifan Hong | dbf58d4 | 2021-06-03 19:21:11 -0700 | [diff] [blame] | 32 | using android::defaultServiceManager; |
| 33 | using android::OK; |
| 34 | using android::RpcServer; |
Yifan Hong | 02530ec | 2021-06-10 13:38:38 -0700 | [diff] [blame] | 35 | using android::sp; |
Yifan Hong | dbf58d4 | 2021-06-03 19:21:11 -0700 | [diff] [blame] | 36 | using android::statusToString; |
| 37 | using android::String16; |
| 38 | using android::base::Basename; |
| 39 | using android::base::GetBoolProperty; |
| 40 | using android::base::InitLogging; |
| 41 | using android::base::LogdLogger; |
| 42 | using android::base::LogId; |
| 43 | using android::base::LogSeverity; |
Yifan Hong | dbf58d4 | 2021-06-03 19:21:11 -0700 | [diff] [blame] | 44 | using android::base::StdioLogger; |
| 45 | using android::base::StringPrintf; |
Yifan Hong | e435882 | 2021-06-14 12:49:43 -0700 | [diff] [blame] | 46 | using std::string_view_literals::operator""sv; |
Yifan Hong | dbf58d4 | 2021-06-03 19:21:11 -0700 | [diff] [blame] | 47 | |
| 48 | namespace { |
Yifan Hong | 9ffa66e | 2021-06-16 16:44:20 -0700 | [diff] [blame] | 49 | |
| 50 | using ServiceRetriever = decltype(&android::IServiceManager::checkService); |
| 51 | |
Yifan Hong | dbf58d4 | 2021-06-03 19:21:11 -0700 | [diff] [blame] | 52 | int Usage(const char* program) { |
Yifan Hong | e435882 | 2021-06-14 12:49:43 -0700 | [diff] [blame] | 53 | auto basename = Basename(program); |
Yifan Hong | dbf58d4 | 2021-06-03 19:21:11 -0700 | [diff] [blame] | 54 | auto format = R"(dispatch calls to RPC service. |
| 55 | Usage: |
Yifan Hong | 9ffa66e | 2021-06-16 16:44:20 -0700 | [diff] [blame] | 56 | %s [-g] <service_name> |
Yifan Hong | dbf58d4 | 2021-06-03 19:21:11 -0700 | [diff] [blame] | 57 | <service_name>: the service to connect to. |
Yifan Hong | 9ffa66e | 2021-06-16 16:44:20 -0700 | [diff] [blame] | 58 | %s [-g] manager |
Yifan Hong | e435882 | 2021-06-14 12:49:43 -0700 | [diff] [blame] | 59 | Runs an RPC-friendly service that redirects calls to servicemanager. |
| 60 | |
Yifan Hong | 9ffa66e | 2021-06-16 16:44:20 -0700 | [diff] [blame] | 61 | -g: use getService() instead of checkService(). |
| 62 | |
Yifan Hong | e435882 | 2021-06-14 12:49:43 -0700 | [diff] [blame] | 63 | If successful, writes port number and a new line character to stdout, and |
| 64 | blocks until killed. |
| 65 | Otherwise, writes error message to stderr and exits with non-zero code. |
Yifan Hong | dbf58d4 | 2021-06-03 19:21:11 -0700 | [diff] [blame] | 66 | )"; |
Yifan Hong | e435882 | 2021-06-14 12:49:43 -0700 | [diff] [blame] | 67 | LOG(ERROR) << StringPrintf(format, basename.c_str(), basename.c_str()); |
Yifan Hong | dbf58d4 | 2021-06-03 19:21:11 -0700 | [diff] [blame] | 68 | return EX_USAGE; |
| 69 | } |
| 70 | |
Yifan Hong | 9ffa66e | 2021-06-16 16:44:20 -0700 | [diff] [blame] | 71 | int Dispatch(const char* name, const ServiceRetriever& serviceRetriever) { |
Yifan Hong | dbf58d4 | 2021-06-03 19:21:11 -0700 | [diff] [blame] | 72 | auto sm = defaultServiceManager(); |
| 73 | if (nullptr == sm) { |
| 74 | LOG(ERROR) << "No servicemanager"; |
| 75 | return EX_SOFTWARE; |
| 76 | } |
Yifan Hong | 9ffa66e | 2021-06-16 16:44:20 -0700 | [diff] [blame] | 77 | auto binder = std::invoke(serviceRetriever, defaultServiceManager(), String16(name)); |
Yifan Hong | dbf58d4 | 2021-06-03 19:21:11 -0700 | [diff] [blame] | 78 | if (nullptr == binder) { |
| 79 | LOG(ERROR) << "No service \"" << name << "\""; |
| 80 | return EX_SOFTWARE; |
| 81 | } |
| 82 | auto rpcServer = RpcServer::make(); |
| 83 | if (nullptr == rpcServer) { |
| 84 | LOG(ERROR) << "Cannot create RpcServer"; |
| 85 | return EX_SOFTWARE; |
| 86 | } |
| 87 | rpcServer->iUnderstandThisCodeIsExperimentalAndIWillNotUseItInProduction(); |
| 88 | unsigned int port; |
| 89 | if (!rpcServer->setupInetServer(0, &port)) { |
| 90 | LOG(ERROR) << "setupInetServer failed"; |
| 91 | return EX_SOFTWARE; |
| 92 | } |
| 93 | auto socket = rpcServer->releaseServer(); |
Yifan Hong | 02530ec | 2021-06-10 13:38:38 -0700 | [diff] [blame] | 94 | auto keepAliveBinder = sp<BBinder>::make(); |
| 95 | auto status = binder->setRpcClientDebug(std::move(socket), keepAliveBinder); |
Yifan Hong | dbf58d4 | 2021-06-03 19:21:11 -0700 | [diff] [blame] | 96 | if (status != OK) { |
| 97 | LOG(ERROR) << "setRpcClientDebug failed with " << statusToString(status); |
| 98 | return EX_SOFTWARE; |
| 99 | } |
Yifan Hong | e435882 | 2021-06-14 12:49:43 -0700 | [diff] [blame] | 100 | LOG(INFO) << "Finish setting up RPC on service " << name << " on port " << port; |
Yifan Hong | dbf58d4 | 2021-06-03 19:21:11 -0700 | [diff] [blame] | 101 | |
| 102 | std::cout << port << std::endl; |
Yifan Hong | d3cb24a | 2021-06-10 17:20:57 -0700 | [diff] [blame] | 103 | |
| 104 | TEMP_FAILURE_RETRY(pause()); |
| 105 | |
| 106 | PLOG(FATAL) << "TEMP_FAILURE_RETRY(pause()) exits; this should not happen!"; |
| 107 | __builtin_unreachable(); |
Yifan Hong | dbf58d4 | 2021-06-03 19:21:11 -0700 | [diff] [blame] | 108 | } |
| 109 | |
Yifan Hong | bd8ca57 | 2021-06-14 19:58:34 -0700 | [diff] [blame] | 110 | // Wrapper that wraps a BpServiceManager as a BnServiceManager. |
| 111 | class ServiceManagerProxyToNative : public android::os::BnServiceManager { |
| 112 | public: |
| 113 | ServiceManagerProxyToNative(const sp<android::os::IServiceManager>& impl) : mImpl(impl) {} |
| 114 | android::binder::Status getService(const std::string&, |
| 115 | android::sp<android::IBinder>*) override { |
| 116 | // We can't send BpBinder for regular binder over RPC. |
| 117 | return android::binder::Status::fromStatusT(android::INVALID_OPERATION); |
| 118 | } |
| 119 | android::binder::Status checkService(const std::string&, |
| 120 | android::sp<android::IBinder>*) override { |
| 121 | // We can't send BpBinder for regular binder over RPC. |
| 122 | return android::binder::Status::fromStatusT(android::INVALID_OPERATION); |
| 123 | } |
| 124 | android::binder::Status addService(const std::string&, const android::sp<android::IBinder>&, |
| 125 | bool, int32_t) override { |
| 126 | // We can't send BpBinder for RPC over regular binder. |
| 127 | return android::binder::Status::fromStatusT(android::INVALID_OPERATION); |
| 128 | } |
| 129 | android::binder::Status listServices(int32_t dumpPriority, |
| 130 | std::vector<std::string>* _aidl_return) override { |
| 131 | return mImpl->listServices(dumpPriority, _aidl_return); |
| 132 | } |
| 133 | android::binder::Status registerForNotifications( |
| 134 | const std::string&, const android::sp<android::os::IServiceCallback>&) override { |
| 135 | // We can't send BpBinder for RPC over regular binder. |
| 136 | return android::binder::Status::fromStatusT(android::INVALID_OPERATION); |
| 137 | } |
| 138 | android::binder::Status unregisterForNotifications( |
| 139 | const std::string&, const android::sp<android::os::IServiceCallback>&) override { |
| 140 | // We can't send BpBinder for RPC over regular binder. |
| 141 | return android::binder::Status::fromStatusT(android::INVALID_OPERATION); |
| 142 | } |
| 143 | android::binder::Status isDeclared(const std::string& name, bool* _aidl_return) override { |
| 144 | return mImpl->isDeclared(name, _aidl_return); |
| 145 | } |
| 146 | android::binder::Status getDeclaredInstances(const std::string& iface, |
| 147 | std::vector<std::string>* _aidl_return) override { |
| 148 | return mImpl->getDeclaredInstances(iface, _aidl_return); |
| 149 | } |
| 150 | android::binder::Status updatableViaApex(const std::string& name, |
| 151 | std::optional<std::string>* _aidl_return) override { |
| 152 | return mImpl->updatableViaApex(name, _aidl_return); |
| 153 | } |
| 154 | android::binder::Status registerClientCallback( |
| 155 | const std::string&, const android::sp<android::IBinder>&, |
| 156 | const android::sp<android::os::IClientCallback>&) override { |
| 157 | // We can't send BpBinder for RPC over regular binder. |
| 158 | return android::binder::Status::fromStatusT(android::INVALID_OPERATION); |
| 159 | } |
| 160 | android::binder::Status tryUnregisterService(const std::string&, |
| 161 | const android::sp<android::IBinder>&) override { |
| 162 | // We can't send BpBinder for RPC over regular binder. |
| 163 | return android::binder::Status::fromStatusT(android::INVALID_OPERATION); |
| 164 | } |
| 165 | android::binder::Status getServiceDebugInfo( |
| 166 | std::vector<android::os::ServiceDebugInfo>* _aidl_return) override { |
| 167 | return mImpl->getServiceDebugInfo(_aidl_return); |
| 168 | } |
| 169 | |
| 170 | private: |
| 171 | sp<android::os::IServiceManager> mImpl; |
| 172 | }; |
| 173 | |
Yifan Hong | e435882 | 2021-06-14 12:49:43 -0700 | [diff] [blame] | 174 | // Workaround for b/191059588. |
| 175 | // TODO(b/191059588): Once we can run RpcServer on single-threaded services, |
| 176 | // `servicedispatcher manager` should call Dispatch("manager") directly. |
Yifan Hong | 9ffa66e | 2021-06-16 16:44:20 -0700 | [diff] [blame] | 177 | int wrapServiceManager(const ServiceRetriever& serviceRetriever) { |
Yifan Hong | e435882 | 2021-06-14 12:49:43 -0700 | [diff] [blame] | 178 | auto sm = defaultServiceManager(); |
| 179 | if (nullptr == sm) { |
| 180 | LOG(ERROR) << "No servicemanager"; |
| 181 | return EX_SOFTWARE; |
| 182 | } |
Yifan Hong | 9ffa66e | 2021-06-16 16:44:20 -0700 | [diff] [blame] | 183 | auto service = std::invoke(serviceRetriever, defaultServiceManager(), String16("manager")); |
Yifan Hong | e435882 | 2021-06-14 12:49:43 -0700 | [diff] [blame] | 184 | if (nullptr == service) { |
| 185 | LOG(ERROR) << "No service called `manager`"; |
| 186 | return EX_SOFTWARE; |
| 187 | } |
Yifan Hong | bd8ca57 | 2021-06-14 19:58:34 -0700 | [diff] [blame] | 188 | auto interface = android::os::IServiceManager::asInterface(service); |
| 189 | if (nullptr == interface) { |
| 190 | LOG(ERROR) << "Cannot cast service called `manager` to IServiceManager"; |
| 191 | return EX_SOFTWARE; |
| 192 | } |
| 193 | |
| 194 | // Work around restriction that doesn't allow us to send proxy over RPC. |
| 195 | interface = sp<ServiceManagerProxyToNative>::make(interface); |
| 196 | service = ServiceManagerProxyToNative::asBinder(interface); |
Yifan Hong | e435882 | 2021-06-14 12:49:43 -0700 | [diff] [blame] | 197 | |
| 198 | auto rpcServer = RpcServer::make(); |
| 199 | rpcServer->iUnderstandThisCodeIsExperimentalAndIWillNotUseItInProduction(); |
| 200 | rpcServer->setRootObject(service); |
| 201 | unsigned int port; |
| 202 | if (!rpcServer->setupInetServer(0, &port)) { |
| 203 | LOG(ERROR) << "Unable to set up inet server"; |
| 204 | return EX_SOFTWARE; |
| 205 | } |
| 206 | LOG(INFO) << "Finish wrapping servicemanager with RPC on port " << port; |
| 207 | std::cout << port << std::endl; |
| 208 | rpcServer->join(); |
| 209 | |
| 210 | LOG(FATAL) << "Wrapped servicemanager exits; this should not happen!"; |
| 211 | __builtin_unreachable(); |
| 212 | } |
| 213 | |
Yifan Hong | dbf58d4 | 2021-06-03 19:21:11 -0700 | [diff] [blame] | 214 | // Log to logd. For warning and more severe messages, also log to stderr. |
| 215 | class ServiceDispatcherLogger { |
| 216 | public: |
| 217 | void operator()(LogId id, LogSeverity severity, const char* tag, const char* file, |
| 218 | unsigned int line, const char* message) { |
| 219 | mLogdLogger(id, severity, tag, file, line, message); |
| 220 | if (severity >= LogSeverity::WARNING) { |
| 221 | std::cout << std::flush; |
| 222 | std::cerr << Basename(getprogname()) << ": " << message << std::endl; |
| 223 | } |
| 224 | } |
| 225 | |
| 226 | private: |
| 227 | LogdLogger mLogdLogger{}; |
| 228 | }; |
| 229 | |
| 230 | } // namespace |
| 231 | |
| 232 | int main(int argc, char* argv[]) { |
| 233 | InitLogging(argv, ServiceDispatcherLogger()); |
| 234 | |
| 235 | if (!GetBoolProperty("ro.debuggable", false)) { |
| 236 | LOG(ERROR) << "servicedispatcher is only allowed on debuggable builds."; |
| 237 | return EX_NOPERM; |
| 238 | } |
| 239 | LOG(WARNING) << "WARNING: servicedispatcher is debug only. Use with caution."; |
| 240 | |
Yifan Hong | dbf58d4 | 2021-06-03 19:21:11 -0700 | [diff] [blame] | 241 | int opt; |
Yifan Hong | 9ffa66e | 2021-06-16 16:44:20 -0700 | [diff] [blame] | 242 | ServiceRetriever serviceRetriever = &android::IServiceManager::checkService; |
| 243 | while (-1 != (opt = getopt(argc, argv, "g"))) { |
Yifan Hong | dbf58d4 | 2021-06-03 19:21:11 -0700 | [diff] [blame] | 244 | switch (opt) { |
Yifan Hong | 9ffa66e | 2021-06-16 16:44:20 -0700 | [diff] [blame] | 245 | case 'g': { |
| 246 | serviceRetriever = &android::IServiceManager::getService; |
| 247 | } break; |
Yifan Hong | dbf58d4 | 2021-06-03 19:21:11 -0700 | [diff] [blame] | 248 | default: { |
| 249 | return Usage(argv[0]); |
| 250 | } |
| 251 | } |
| 252 | } |
Yifan Hong | e435882 | 2021-06-14 12:49:43 -0700 | [diff] [blame] | 253 | |
Yifan Hong | dbf58d4 | 2021-06-03 19:21:11 -0700 | [diff] [blame] | 254 | if (optind + 1 != argc) return Usage(argv[0]); |
| 255 | auto name = argv[optind]; |
| 256 | |
Yifan Hong | e435882 | 2021-06-14 12:49:43 -0700 | [diff] [blame] | 257 | if (name == "manager"sv) { |
Yifan Hong | 9ffa66e | 2021-06-16 16:44:20 -0700 | [diff] [blame] | 258 | return wrapServiceManager(serviceRetriever); |
Yifan Hong | e435882 | 2021-06-14 12:49:43 -0700 | [diff] [blame] | 259 | } |
Yifan Hong | 9ffa66e | 2021-06-16 16:44:20 -0700 | [diff] [blame] | 260 | return Dispatch(name, serviceRetriever); |
Yifan Hong | dbf58d4 | 2021-06-03 19:21:11 -0700 | [diff] [blame] | 261 | } |