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