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> |
| 26 | #include <binder/IServiceManager.h> |
| 27 | #include <binder/RpcServer.h> |
| 28 | |
| 29 | using android::defaultServiceManager; |
| 30 | using android::OK; |
| 31 | using android::RpcServer; |
| 32 | using android::statusToString; |
| 33 | using android::String16; |
| 34 | using android::base::Basename; |
| 35 | using android::base::GetBoolProperty; |
| 36 | using android::base::InitLogging; |
| 37 | using android::base::LogdLogger; |
| 38 | using android::base::LogId; |
| 39 | using android::base::LogSeverity; |
Yifan Hong | dbf58d4 | 2021-06-03 19:21:11 -0700 | [diff] [blame] | 40 | using android::base::StdioLogger; |
| 41 | using android::base::StringPrintf; |
| 42 | |
| 43 | namespace { |
| 44 | int Usage(const char* program) { |
| 45 | auto format = R"(dispatch calls to RPC service. |
| 46 | Usage: |
Yifan Hong | 3482323 | 2021-06-07 17:23:00 -0700 | [diff] [blame] | 47 | %s <service_name> |
Yifan Hong | dbf58d4 | 2021-06-03 19:21:11 -0700 | [diff] [blame] | 48 | <service_name>: the service to connect to. |
| 49 | )"; |
| 50 | LOG(ERROR) << StringPrintf(format, Basename(program).c_str()); |
| 51 | return EX_USAGE; |
| 52 | } |
| 53 | |
Yifan Hong | 3482323 | 2021-06-07 17:23:00 -0700 | [diff] [blame] | 54 | int Dispatch(const char* name) { |
Yifan Hong | dbf58d4 | 2021-06-03 19:21:11 -0700 | [diff] [blame] | 55 | auto sm = defaultServiceManager(); |
| 56 | if (nullptr == sm) { |
| 57 | LOG(ERROR) << "No servicemanager"; |
| 58 | return EX_SOFTWARE; |
| 59 | } |
| 60 | auto binder = sm->checkService(String16(name)); |
| 61 | if (nullptr == binder) { |
| 62 | LOG(ERROR) << "No service \"" << name << "\""; |
| 63 | return EX_SOFTWARE; |
| 64 | } |
| 65 | auto rpcServer = RpcServer::make(); |
| 66 | if (nullptr == rpcServer) { |
| 67 | LOG(ERROR) << "Cannot create RpcServer"; |
| 68 | return EX_SOFTWARE; |
| 69 | } |
| 70 | rpcServer->iUnderstandThisCodeIsExperimentalAndIWillNotUseItInProduction(); |
| 71 | unsigned int port; |
| 72 | if (!rpcServer->setupInetServer(0, &port)) { |
| 73 | LOG(ERROR) << "setupInetServer failed"; |
| 74 | return EX_SOFTWARE; |
| 75 | } |
| 76 | auto socket = rpcServer->releaseServer(); |
Yifan Hong | 3482323 | 2021-06-07 17:23:00 -0700 | [diff] [blame] | 77 | auto status = binder->setRpcClientDebug(std::move(socket)); |
Yifan Hong | dbf58d4 | 2021-06-03 19:21:11 -0700 | [diff] [blame] | 78 | if (status != OK) { |
| 79 | LOG(ERROR) << "setRpcClientDebug failed with " << statusToString(status); |
| 80 | return EX_SOFTWARE; |
| 81 | } |
Yifan Hong | 3482323 | 2021-06-07 17:23:00 -0700 | [diff] [blame] | 82 | LOG(INFO) << "Finish setting up RPC on service " << name << " on port" << port; |
Yifan Hong | dbf58d4 | 2021-06-03 19:21:11 -0700 | [diff] [blame] | 83 | |
| 84 | std::cout << port << std::endl; |
Yifan Hong | d3cb24a | 2021-06-10 17:20:57 -0700 | [diff] [blame^] | 85 | |
| 86 | TEMP_FAILURE_RETRY(pause()); |
| 87 | |
| 88 | PLOG(FATAL) << "TEMP_FAILURE_RETRY(pause()) exits; this should not happen!"; |
| 89 | __builtin_unreachable(); |
Yifan Hong | dbf58d4 | 2021-06-03 19:21:11 -0700 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | // Log to logd. For warning and more severe messages, also log to stderr. |
| 93 | class ServiceDispatcherLogger { |
| 94 | public: |
| 95 | void operator()(LogId id, LogSeverity severity, const char* tag, const char* file, |
| 96 | unsigned int line, const char* message) { |
| 97 | mLogdLogger(id, severity, tag, file, line, message); |
| 98 | if (severity >= LogSeverity::WARNING) { |
| 99 | std::cout << std::flush; |
| 100 | std::cerr << Basename(getprogname()) << ": " << message << std::endl; |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | private: |
| 105 | LogdLogger mLogdLogger{}; |
| 106 | }; |
| 107 | |
| 108 | } // namespace |
| 109 | |
| 110 | int main(int argc, char* argv[]) { |
| 111 | InitLogging(argv, ServiceDispatcherLogger()); |
| 112 | |
| 113 | if (!GetBoolProperty("ro.debuggable", false)) { |
| 114 | LOG(ERROR) << "servicedispatcher is only allowed on debuggable builds."; |
| 115 | return EX_NOPERM; |
| 116 | } |
| 117 | LOG(WARNING) << "WARNING: servicedispatcher is debug only. Use with caution."; |
| 118 | |
Yifan Hong | dbf58d4 | 2021-06-03 19:21:11 -0700 | [diff] [blame] | 119 | int opt; |
Yifan Hong | 3482323 | 2021-06-07 17:23:00 -0700 | [diff] [blame] | 120 | while (-1 != (opt = getopt(argc, argv, ""))) { |
Yifan Hong | dbf58d4 | 2021-06-03 19:21:11 -0700 | [diff] [blame] | 121 | switch (opt) { |
Yifan Hong | dbf58d4 | 2021-06-03 19:21:11 -0700 | [diff] [blame] | 122 | default: { |
| 123 | return Usage(argv[0]); |
| 124 | } |
| 125 | } |
| 126 | } |
| 127 | if (optind + 1 != argc) return Usage(argv[0]); |
| 128 | auto name = argv[optind]; |
| 129 | |
Yifan Hong | 3482323 | 2021-06-07 17:23:00 -0700 | [diff] [blame] | 130 | return Dispatch(name); |
Yifan Hong | dbf58d4 | 2021-06-03 19:21:11 -0700 | [diff] [blame] | 131 | } |