blob: 0c677a891863d1e69355e3c70f802089e68e38f0 [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
Yifan Hong02530ec2021-06-10 13:38:38 -070029using android::BBinder;
Yifan Hongdbf58d42021-06-03 19:21:11 -070030using android::defaultServiceManager;
31using android::OK;
32using android::RpcServer;
Yifan Hong02530ec2021-06-10 13:38:38 -070033using android::sp;
Yifan Hongdbf58d42021-06-03 19:21:11 -070034using android::statusToString;
35using android::String16;
36using android::base::Basename;
37using android::base::GetBoolProperty;
38using android::base::InitLogging;
39using android::base::LogdLogger;
40using android::base::LogId;
41using android::base::LogSeverity;
Yifan Hongdbf58d42021-06-03 19:21:11 -070042using android::base::StdioLogger;
43using android::base::StringPrintf;
44
45namespace {
46int Usage(const char* program) {
47 auto format = R"(dispatch calls to RPC service.
48Usage:
Yifan Hong34823232021-06-07 17:23:00 -070049 %s <service_name>
Yifan Hongdbf58d42021-06-03 19:21:11 -070050 <service_name>: the service to connect to.
51)";
52 LOG(ERROR) << StringPrintf(format, Basename(program).c_str());
53 return EX_USAGE;
54}
55
Yifan Hong34823232021-06-07 17:23:00 -070056int Dispatch(const char* name) {
Yifan Hongdbf58d42021-06-03 19:21:11 -070057 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 Hong02530ec2021-06-10 13:38:38 -070079 auto keepAliveBinder = sp<BBinder>::make();
80 auto status = binder->setRpcClientDebug(std::move(socket), keepAliveBinder);
Yifan Hongdbf58d42021-06-03 19:21:11 -070081 if (status != OK) {
82 LOG(ERROR) << "setRpcClientDebug failed with " << statusToString(status);
83 return EX_SOFTWARE;
84 }
Yifan Hong34823232021-06-07 17:23:00 -070085 LOG(INFO) << "Finish setting up RPC on service " << name << " on port" << port;
Yifan Hongdbf58d42021-06-03 19:21:11 -070086
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.
92class ServiceDispatcherLogger {
93public:
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
103private:
104 LogdLogger mLogdLogger{};
105};
106
107} // namespace
108
109int 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 Hongdbf58d42021-06-03 19:21:11 -0700118 int opt;
Yifan Hong34823232021-06-07 17:23:00 -0700119 while (-1 != (opt = getopt(argc, argv, ""))) {
Yifan Hongdbf58d42021-06-03 19:21:11 -0700120 switch (opt) {
Yifan Hongdbf58d42021-06-03 19:21:11 -0700121 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 Hong34823232021-06-07 17:23:00 -0700129 return Dispatch(name);
Yifan Hongdbf58d42021-06-03 19:21:11 -0700130}