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 | e435882 | 2021-06-14 12:49:43 -0700 | [diff] [blame^] | 26 | #include <android/os/IServiceManager.h> |
Yifan Hong | dbf58d4 | 2021-06-03 19:21:11 -0700 | [diff] [blame] | 27 | #include <binder/IServiceManager.h> |
| 28 | #include <binder/RpcServer.h> |
| 29 | |
| 30 | using android::defaultServiceManager; |
| 31 | using android::OK; |
| 32 | using android::RpcServer; |
| 33 | using android::statusToString; |
| 34 | using android::String16; |
| 35 | using android::base::Basename; |
| 36 | using android::base::GetBoolProperty; |
| 37 | using android::base::InitLogging; |
| 38 | using android::base::LogdLogger; |
| 39 | using android::base::LogId; |
| 40 | using android::base::LogSeverity; |
Yifan Hong | dbf58d4 | 2021-06-03 19:21:11 -0700 | [diff] [blame] | 41 | using android::base::StdioLogger; |
| 42 | using android::base::StringPrintf; |
Yifan Hong | e435882 | 2021-06-14 12:49:43 -0700 | [diff] [blame^] | 43 | using std::string_view_literals::operator""sv; |
Yifan Hong | dbf58d4 | 2021-06-03 19:21:11 -0700 | [diff] [blame] | 44 | |
| 45 | namespace { |
| 46 | int Usage(const char* program) { |
Yifan Hong | e435882 | 2021-06-14 12:49:43 -0700 | [diff] [blame^] | 47 | auto basename = Basename(program); |
Yifan Hong | dbf58d4 | 2021-06-03 19:21:11 -0700 | [diff] [blame] | 48 | auto format = R"(dispatch calls to RPC service. |
| 49 | Usage: |
Yifan Hong | 3482323 | 2021-06-07 17:23:00 -0700 | [diff] [blame] | 50 | %s <service_name> |
Yifan Hong | dbf58d4 | 2021-06-03 19:21:11 -0700 | [diff] [blame] | 51 | <service_name>: the service to connect to. |
Yifan Hong | e435882 | 2021-06-14 12:49:43 -0700 | [diff] [blame^] | 52 | %s manager |
| 53 | Runs an RPC-friendly service that redirects calls to servicemanager. |
| 54 | |
| 55 | If successful, writes port number and a new line character to stdout, and |
| 56 | blocks until killed. |
| 57 | Otherwise, writes error message to stderr and exits with non-zero code. |
Yifan Hong | dbf58d4 | 2021-06-03 19:21:11 -0700 | [diff] [blame] | 58 | )"; |
Yifan Hong | e435882 | 2021-06-14 12:49:43 -0700 | [diff] [blame^] | 59 | LOG(ERROR) << StringPrintf(format, basename.c_str(), basename.c_str()); |
Yifan Hong | dbf58d4 | 2021-06-03 19:21:11 -0700 | [diff] [blame] | 60 | return EX_USAGE; |
| 61 | } |
| 62 | |
Yifan Hong | 3482323 | 2021-06-07 17:23:00 -0700 | [diff] [blame] | 63 | int Dispatch(const char* name) { |
Yifan Hong | dbf58d4 | 2021-06-03 19:21:11 -0700 | [diff] [blame] | 64 | auto sm = defaultServiceManager(); |
| 65 | if (nullptr == sm) { |
| 66 | LOG(ERROR) << "No servicemanager"; |
| 67 | return EX_SOFTWARE; |
| 68 | } |
| 69 | auto binder = sm->checkService(String16(name)); |
| 70 | if (nullptr == binder) { |
| 71 | LOG(ERROR) << "No service \"" << name << "\""; |
| 72 | return EX_SOFTWARE; |
| 73 | } |
| 74 | auto rpcServer = RpcServer::make(); |
| 75 | if (nullptr == rpcServer) { |
| 76 | LOG(ERROR) << "Cannot create RpcServer"; |
| 77 | return EX_SOFTWARE; |
| 78 | } |
| 79 | rpcServer->iUnderstandThisCodeIsExperimentalAndIWillNotUseItInProduction(); |
| 80 | unsigned int port; |
| 81 | if (!rpcServer->setupInetServer(0, &port)) { |
| 82 | LOG(ERROR) << "setupInetServer failed"; |
| 83 | return EX_SOFTWARE; |
| 84 | } |
| 85 | auto socket = rpcServer->releaseServer(); |
Yifan Hong | 3482323 | 2021-06-07 17:23:00 -0700 | [diff] [blame] | 86 | auto status = binder->setRpcClientDebug(std::move(socket)); |
Yifan Hong | dbf58d4 | 2021-06-03 19:21:11 -0700 | [diff] [blame] | 87 | if (status != OK) { |
| 88 | LOG(ERROR) << "setRpcClientDebug failed with " << statusToString(status); |
| 89 | return EX_SOFTWARE; |
| 90 | } |
Yifan Hong | e435882 | 2021-06-14 12:49:43 -0700 | [diff] [blame^] | 91 | LOG(INFO) << "Finish setting up RPC on service " << name << " on port " << port; |
Yifan Hong | dbf58d4 | 2021-06-03 19:21:11 -0700 | [diff] [blame] | 92 | |
| 93 | std::cout << port << std::endl; |
| 94 | return EX_OK; |
| 95 | } |
| 96 | |
Yifan Hong | e435882 | 2021-06-14 12:49:43 -0700 | [diff] [blame^] | 97 | // Workaround for b/191059588. |
| 98 | // TODO(b/191059588): Once we can run RpcServer on single-threaded services, |
| 99 | // `servicedispatcher manager` should call Dispatch("manager") directly. |
| 100 | int wrapServiceManager() { |
| 101 | auto sm = defaultServiceManager(); |
| 102 | if (nullptr == sm) { |
| 103 | LOG(ERROR) << "No servicemanager"; |
| 104 | return EX_SOFTWARE; |
| 105 | } |
| 106 | auto service = sm->checkService(String16("manager")); |
| 107 | if (nullptr == service) { |
| 108 | LOG(ERROR) << "No service called `manager`"; |
| 109 | return EX_SOFTWARE; |
| 110 | } |
| 111 | |
| 112 | auto rpcServer = RpcServer::make(); |
| 113 | rpcServer->iUnderstandThisCodeIsExperimentalAndIWillNotUseItInProduction(); |
| 114 | rpcServer->setRootObject(service); |
| 115 | unsigned int port; |
| 116 | if (!rpcServer->setupInetServer(0, &port)) { |
| 117 | LOG(ERROR) << "Unable to set up inet server"; |
| 118 | return EX_SOFTWARE; |
| 119 | } |
| 120 | LOG(INFO) << "Finish wrapping servicemanager with RPC on port " << port; |
| 121 | std::cout << port << std::endl; |
| 122 | rpcServer->join(); |
| 123 | |
| 124 | LOG(FATAL) << "Wrapped servicemanager exits; this should not happen!"; |
| 125 | __builtin_unreachable(); |
| 126 | } |
| 127 | |
Yifan Hong | dbf58d4 | 2021-06-03 19:21:11 -0700 | [diff] [blame] | 128 | // Log to logd. For warning and more severe messages, also log to stderr. |
| 129 | class ServiceDispatcherLogger { |
| 130 | public: |
| 131 | void operator()(LogId id, LogSeverity severity, const char* tag, const char* file, |
| 132 | unsigned int line, const char* message) { |
| 133 | mLogdLogger(id, severity, tag, file, line, message); |
| 134 | if (severity >= LogSeverity::WARNING) { |
| 135 | std::cout << std::flush; |
| 136 | std::cerr << Basename(getprogname()) << ": " << message << std::endl; |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | private: |
| 141 | LogdLogger mLogdLogger{}; |
| 142 | }; |
| 143 | |
| 144 | } // namespace |
| 145 | |
| 146 | int main(int argc, char* argv[]) { |
| 147 | InitLogging(argv, ServiceDispatcherLogger()); |
| 148 | |
| 149 | if (!GetBoolProperty("ro.debuggable", false)) { |
| 150 | LOG(ERROR) << "servicedispatcher is only allowed on debuggable builds."; |
| 151 | return EX_NOPERM; |
| 152 | } |
| 153 | LOG(WARNING) << "WARNING: servicedispatcher is debug only. Use with caution."; |
| 154 | |
Yifan Hong | dbf58d4 | 2021-06-03 19:21:11 -0700 | [diff] [blame] | 155 | int opt; |
Yifan Hong | 3482323 | 2021-06-07 17:23:00 -0700 | [diff] [blame] | 156 | while (-1 != (opt = getopt(argc, argv, ""))) { |
Yifan Hong | dbf58d4 | 2021-06-03 19:21:11 -0700 | [diff] [blame] | 157 | switch (opt) { |
Yifan Hong | dbf58d4 | 2021-06-03 19:21:11 -0700 | [diff] [blame] | 158 | default: { |
| 159 | return Usage(argv[0]); |
| 160 | } |
| 161 | } |
| 162 | } |
Yifan Hong | e435882 | 2021-06-14 12:49:43 -0700 | [diff] [blame^] | 163 | |
Yifan Hong | dbf58d4 | 2021-06-03 19:21:11 -0700 | [diff] [blame] | 164 | if (optind + 1 != argc) return Usage(argv[0]); |
| 165 | auto name = argv[optind]; |
| 166 | |
Yifan Hong | e435882 | 2021-06-14 12:49:43 -0700 | [diff] [blame^] | 167 | if (name == "manager"sv) { |
| 168 | return wrapServiceManager(); |
| 169 | } |
Yifan Hong | 3482323 | 2021-06-07 17:23:00 -0700 | [diff] [blame] | 170 | return Dispatch(name); |
Yifan Hong | dbf58d4 | 2021-06-03 19:21:11 -0700 | [diff] [blame] | 171 | } |