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