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