blob: 35d69aa85bf2cd0ccb9015b1627ad4663a5d68fc [file] [log] [blame]
Yifan Hongdbf58d42021-06-03 19:21:11 -07001/*
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 Hongdbf58d42021-06-03 19:21:11 -070017#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 Hongdbf58d42021-06-03 19:21:11 -070024#include <android-base/properties.h>
25#include <android-base/stringprintf.h>
26#include <binder/IServiceManager.h>
27#include <binder/RpcServer.h>
28
29using android::defaultServiceManager;
30using android::OK;
31using android::RpcServer;
32using android::statusToString;
33using android::String16;
34using android::base::Basename;
35using android::base::GetBoolProperty;
36using android::base::InitLogging;
37using android::base::LogdLogger;
38using android::base::LogId;
39using android::base::LogSeverity;
Yifan Hongdbf58d42021-06-03 19:21:11 -070040using android::base::StdioLogger;
41using android::base::StringPrintf;
42
43namespace {
44int Usage(const char* program) {
45 auto format = R"(dispatch calls to RPC service.
46Usage:
Yifan Hong34823232021-06-07 17:23:00 -070047 %s <service_name>
Yifan Hongdbf58d42021-06-03 19:21:11 -070048 <service_name>: the service to connect to.
49)";
50 LOG(ERROR) << StringPrintf(format, Basename(program).c_str());
51 return EX_USAGE;
52}
53
Yifan Hong34823232021-06-07 17:23:00 -070054int Dispatch(const char* name) {
Yifan Hongdbf58d42021-06-03 19:21:11 -070055 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 Hong34823232021-06-07 17:23:00 -070077 auto status = binder->setRpcClientDebug(std::move(socket));
Yifan Hongdbf58d42021-06-03 19:21:11 -070078 if (status != OK) {
79 LOG(ERROR) << "setRpcClientDebug failed with " << statusToString(status);
80 return EX_SOFTWARE;
81 }
Yifan Hong34823232021-06-07 17:23:00 -070082 LOG(INFO) << "Finish setting up RPC on service " << name << " on port" << port;
Yifan Hongdbf58d42021-06-03 19:21:11 -070083
84 std::cout << port << std::endl;
Yifan Hongd3cb24a2021-06-10 17:20:57 -070085
86 TEMP_FAILURE_RETRY(pause());
87
88 PLOG(FATAL) << "TEMP_FAILURE_RETRY(pause()) exits; this should not happen!";
89 __builtin_unreachable();
Yifan Hongdbf58d42021-06-03 19:21:11 -070090}
91
92// Log to logd. For warning and more severe messages, also log to stderr.
93class ServiceDispatcherLogger {
94public:
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
104private:
105 LogdLogger mLogdLogger{};
106};
107
108} // namespace
109
110int 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 Hongdbf58d42021-06-03 19:21:11 -0700119 int opt;
Yifan Hong34823232021-06-07 17:23:00 -0700120 while (-1 != (opt = getopt(argc, argv, ""))) {
Yifan Hongdbf58d42021-06-03 19:21:11 -0700121 switch (opt) {
Yifan Hongdbf58d42021-06-03 19:21:11 -0700122 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 Hong34823232021-06-07 17:23:00 -0700130 return Dispatch(name);
Yifan Hongdbf58d42021-06-03 19:21:11 -0700131}