blob: f61df084fc89c1b8639cc7255e51ac7f287c88f2 [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
17#include <stdint.h>
18#include <sysexits.h>
19#include <unistd.h>
20
21#include <iostream>
22
23#include <android-base/file.h>
24#include <android-base/logging.h>
25#include <android-base/parseint.h>
26#include <android-base/properties.h>
27#include <android-base/stringprintf.h>
28#include <binder/IServiceManager.h>
29#include <binder/RpcServer.h>
30
31using android::defaultServiceManager;
32using android::OK;
33using android::RpcServer;
34using 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;
42using android::base::ParseUint;
43using android::base::StdioLogger;
44using android::base::StringPrintf;
45
46namespace {
47int Usage(const char* program) {
48 auto format = R"(dispatch calls to RPC service.
49Usage:
50 %s [-n <num_threads>] <service_name>
51 -n <num_threads>: number of RPC threads added to the service (default 1).
52 <service_name>: the service to connect to.
53)";
54 LOG(ERROR) << StringPrintf(format, Basename(program).c_str());
55 return EX_USAGE;
56}
57
58int Dispatch(const char* name, uint32_t numThreads) {
59 auto sm = defaultServiceManager();
60 if (nullptr == sm) {
61 LOG(ERROR) << "No servicemanager";
62 return EX_SOFTWARE;
63 }
64 auto binder = sm->checkService(String16(name));
65 if (nullptr == binder) {
66 LOG(ERROR) << "No service \"" << name << "\"";
67 return EX_SOFTWARE;
68 }
69 auto rpcServer = RpcServer::make();
70 if (nullptr == rpcServer) {
71 LOG(ERROR) << "Cannot create RpcServer";
72 return EX_SOFTWARE;
73 }
74 rpcServer->iUnderstandThisCodeIsExperimentalAndIWillNotUseItInProduction();
75 unsigned int port;
76 if (!rpcServer->setupInetServer(0, &port)) {
77 LOG(ERROR) << "setupInetServer failed";
78 return EX_SOFTWARE;
79 }
80 auto socket = rpcServer->releaseServer();
81 auto status = binder->setRpcClientDebug(std::move(socket), numThreads);
82 if (status != OK) {
83 LOG(ERROR) << "setRpcClientDebug failed with " << statusToString(status);
84 return EX_SOFTWARE;
85 }
86 LOG(INFO) << "Finish setting up RPC on service " << name << " with " << numThreads
87 << " threads on port" << port;
88
89 std::cout << port << std::endl;
90 return EX_OK;
91}
92
93// Log to logd. For warning and more severe messages, also log to stderr.
94class ServiceDispatcherLogger {
95public:
96 void operator()(LogId id, LogSeverity severity, const char* tag, const char* file,
97 unsigned int line, const char* message) {
98 mLogdLogger(id, severity, tag, file, line, message);
99 if (severity >= LogSeverity::WARNING) {
100 std::cout << std::flush;
101 std::cerr << Basename(getprogname()) << ": " << message << std::endl;
102 }
103 }
104
105private:
106 LogdLogger mLogdLogger{};
107};
108
109} // namespace
110
111int main(int argc, char* argv[]) {
112 InitLogging(argv, ServiceDispatcherLogger());
113
114 if (!GetBoolProperty("ro.debuggable", false)) {
115 LOG(ERROR) << "servicedispatcher is only allowed on debuggable builds.";
116 return EX_NOPERM;
117 }
118 LOG(WARNING) << "WARNING: servicedispatcher is debug only. Use with caution.";
119
120 uint32_t numThreads = 1;
121 int opt;
122 while (-1 != (opt = getopt(argc, argv, "n:"))) {
123 switch (opt) {
124 case 'n': {
125 if (!ParseUint(optarg, &numThreads)) {
126 return Usage(argv[0]);
127 }
128 } break;
129 default: {
130 return Usage(argv[0]);
131 }
132 }
133 }
134 if (optind + 1 != argc) return Usage(argv[0]);
135 auto name = argv[optind];
136
137 return Dispatch(name, numThreads);
138}