blob: b671c54c70d27c0b3fe6de29fcf647a250fefac3 [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>
Yifan Honge4358822021-06-14 12:49:43 -070026#include <android/os/IServiceManager.h>
Yifan Hongdbf58d42021-06-03 19:21:11 -070027#include <binder/IServiceManager.h>
28#include <binder/RpcServer.h>
29
30using android::defaultServiceManager;
31using android::OK;
32using android::RpcServer;
33using android::statusToString;
34using android::String16;
35using android::base::Basename;
36using android::base::GetBoolProperty;
37using android::base::InitLogging;
38using android::base::LogdLogger;
39using android::base::LogId;
40using android::base::LogSeverity;
Yifan Hongdbf58d42021-06-03 19:21:11 -070041using android::base::StdioLogger;
42using android::base::StringPrintf;
Yifan Honge4358822021-06-14 12:49:43 -070043using std::string_view_literals::operator""sv;
Yifan Hongdbf58d42021-06-03 19:21:11 -070044
45namespace {
46int Usage(const char* program) {
Yifan Honge4358822021-06-14 12:49:43 -070047 auto basename = Basename(program);
Yifan Hongdbf58d42021-06-03 19:21:11 -070048 auto format = R"(dispatch calls to RPC service.
49Usage:
Yifan Hong34823232021-06-07 17:23:00 -070050 %s <service_name>
Yifan Hongdbf58d42021-06-03 19:21:11 -070051 <service_name>: the service to connect to.
Yifan Honge4358822021-06-14 12:49:43 -070052 %s manager
53 Runs an RPC-friendly service that redirects calls to servicemanager.
54
55 If successful, writes port number and a new line character to stdout, and
56 blocks until killed.
57 Otherwise, writes error message to stderr and exits with non-zero code.
Yifan Hongdbf58d42021-06-03 19:21:11 -070058)";
Yifan Honge4358822021-06-14 12:49:43 -070059 LOG(ERROR) << StringPrintf(format, basename.c_str(), basename.c_str());
Yifan Hongdbf58d42021-06-03 19:21:11 -070060 return EX_USAGE;
61}
62
Yifan Hong34823232021-06-07 17:23:00 -070063int Dispatch(const char* name) {
Yifan Hongdbf58d42021-06-03 19:21:11 -070064 auto sm = defaultServiceManager();
65 if (nullptr == sm) {
66 LOG(ERROR) << "No servicemanager";
67 return EX_SOFTWARE;
68 }
69 auto binder = sm->checkService(String16(name));
70 if (nullptr == binder) {
71 LOG(ERROR) << "No service \"" << name << "\"";
72 return EX_SOFTWARE;
73 }
74 auto rpcServer = RpcServer::make();
75 if (nullptr == rpcServer) {
76 LOG(ERROR) << "Cannot create RpcServer";
77 return EX_SOFTWARE;
78 }
79 rpcServer->iUnderstandThisCodeIsExperimentalAndIWillNotUseItInProduction();
80 unsigned int port;
81 if (!rpcServer->setupInetServer(0, &port)) {
82 LOG(ERROR) << "setupInetServer failed";
83 return EX_SOFTWARE;
84 }
85 auto socket = rpcServer->releaseServer();
Yifan Hong34823232021-06-07 17:23:00 -070086 auto status = binder->setRpcClientDebug(std::move(socket));
Yifan Hongdbf58d42021-06-03 19:21:11 -070087 if (status != OK) {
88 LOG(ERROR) << "setRpcClientDebug failed with " << statusToString(status);
89 return EX_SOFTWARE;
90 }
Yifan Honge4358822021-06-14 12:49:43 -070091 LOG(INFO) << "Finish setting up RPC on service " << name << " on port " << port;
Yifan Hongdbf58d42021-06-03 19:21:11 -070092
93 std::cout << port << std::endl;
94 return EX_OK;
95}
96
Yifan Honge4358822021-06-14 12:49:43 -070097// Workaround for b/191059588.
98// TODO(b/191059588): Once we can run RpcServer on single-threaded services,
99// `servicedispatcher manager` should call Dispatch("manager") directly.
100int wrapServiceManager() {
101 auto sm = defaultServiceManager();
102 if (nullptr == sm) {
103 LOG(ERROR) << "No servicemanager";
104 return EX_SOFTWARE;
105 }
106 auto service = sm->checkService(String16("manager"));
107 if (nullptr == service) {
108 LOG(ERROR) << "No service called `manager`";
109 return EX_SOFTWARE;
110 }
111
112 auto rpcServer = RpcServer::make();
113 rpcServer->iUnderstandThisCodeIsExperimentalAndIWillNotUseItInProduction();
114 rpcServer->setRootObject(service);
115 unsigned int port;
116 if (!rpcServer->setupInetServer(0, &port)) {
117 LOG(ERROR) << "Unable to set up inet server";
118 return EX_SOFTWARE;
119 }
120 LOG(INFO) << "Finish wrapping servicemanager with RPC on port " << port;
121 std::cout << port << std::endl;
122 rpcServer->join();
123
124 LOG(FATAL) << "Wrapped servicemanager exits; this should not happen!";
125 __builtin_unreachable();
126}
127
Yifan Hongdbf58d42021-06-03 19:21:11 -0700128// Log to logd. For warning and more severe messages, also log to stderr.
129class ServiceDispatcherLogger {
130public:
131 void operator()(LogId id, LogSeverity severity, const char* tag, const char* file,
132 unsigned int line, const char* message) {
133 mLogdLogger(id, severity, tag, file, line, message);
134 if (severity >= LogSeverity::WARNING) {
135 std::cout << std::flush;
136 std::cerr << Basename(getprogname()) << ": " << message << std::endl;
137 }
138 }
139
140private:
141 LogdLogger mLogdLogger{};
142};
143
144} // namespace
145
146int main(int argc, char* argv[]) {
147 InitLogging(argv, ServiceDispatcherLogger());
148
149 if (!GetBoolProperty("ro.debuggable", false)) {
150 LOG(ERROR) << "servicedispatcher is only allowed on debuggable builds.";
151 return EX_NOPERM;
152 }
153 LOG(WARNING) << "WARNING: servicedispatcher is debug only. Use with caution.";
154
Yifan Hongdbf58d42021-06-03 19:21:11 -0700155 int opt;
Yifan Hong34823232021-06-07 17:23:00 -0700156 while (-1 != (opt = getopt(argc, argv, ""))) {
Yifan Hongdbf58d42021-06-03 19:21:11 -0700157 switch (opt) {
Yifan Hongdbf58d42021-06-03 19:21:11 -0700158 default: {
159 return Usage(argv[0]);
160 }
161 }
162 }
Yifan Honge4358822021-06-14 12:49:43 -0700163
Yifan Hongdbf58d42021-06-03 19:21:11 -0700164 if (optind + 1 != argc) return Usage(argv[0]);
165 auto name = argv[optind];
166
Yifan Honge4358822021-06-14 12:49:43 -0700167 if (name == "manager"sv) {
168 return wrapServiceManager();
169 }
Yifan Hong34823232021-06-07 17:23:00 -0700170 return Dispatch(name);
Yifan Hongdbf58d42021-06-03 19:21:11 -0700171}