blob: a2fa842cf6bca5e82192b611a6e9f454348cbb17 [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;
85 return EX_OK;
86}
87
88// Log to logd. For warning and more severe messages, also log to stderr.
89class ServiceDispatcherLogger {
90public:
91 void operator()(LogId id, LogSeverity severity, const char* tag, const char* file,
92 unsigned int line, const char* message) {
93 mLogdLogger(id, severity, tag, file, line, message);
94 if (severity >= LogSeverity::WARNING) {
95 std::cout << std::flush;
96 std::cerr << Basename(getprogname()) << ": " << message << std::endl;
97 }
98 }
99
100private:
101 LogdLogger mLogdLogger{};
102};
103
104} // namespace
105
106int main(int argc, char* argv[]) {
107 InitLogging(argv, ServiceDispatcherLogger());
108
109 if (!GetBoolProperty("ro.debuggable", false)) {
110 LOG(ERROR) << "servicedispatcher is only allowed on debuggable builds.";
111 return EX_NOPERM;
112 }
113 LOG(WARNING) << "WARNING: servicedispatcher is debug only. Use with caution.";
114
Yifan Hongdbf58d42021-06-03 19:21:11 -0700115 int opt;
Yifan Hong34823232021-06-07 17:23:00 -0700116 while (-1 != (opt = getopt(argc, argv, ""))) {
Yifan Hongdbf58d42021-06-03 19:21:11 -0700117 switch (opt) {
Yifan Hongdbf58d42021-06-03 19:21:11 -0700118 default: {
119 return Usage(argv[0]);
120 }
121 }
122 }
123 if (optind + 1 != argc) return Usage(argv[0]);
124 auto name = argv[optind];
125
Yifan Hong34823232021-06-07 17:23:00 -0700126 return Dispatch(name);
Yifan Hongdbf58d42021-06-03 19:21:11 -0700127}