1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
|
/*
* Copyright (C) 2021 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <sysexits.h>
#include <unistd.h>
#include <iostream>
#include <android-base/file.h>
#include <android-base/logging.h>
#include <android-base/properties.h>
#include <android-base/stringprintf.h>
#include <android/os/BnServiceManager.h>
#include <android/os/IServiceManager.h>
#include <binder/IServiceManager.h>
#include <binder/RpcServer.h>
using android::BBinder;
using android::defaultServiceManager;
using android::OK;
using android::RpcServer;
using android::sp;
using android::statusToString;
using android::String16;
using android::base::Basename;
using android::base::GetBoolProperty;
using android::base::InitLogging;
using android::base::LogdLogger;
using android::base::LogId;
using android::base::LogSeverity;
using android::base::StdioLogger;
using android::base::StringPrintf;
using std::string_view_literals::operator""sv;
namespace {
using ServiceRetriever = decltype(&android::IServiceManager::checkService);
int Usage(const char* program) {
auto basename = Basename(program);
auto format = R"(dispatch calls to RPC service.
Usage:
%s [-g] <service_name>
<service_name>: the service to connect to.
%s [-g] manager
Runs an RPC-friendly service that redirects calls to servicemanager.
-g: use getService() instead of checkService().
If successful, writes port number and a new line character to stdout, and
blocks until killed.
Otherwise, writes error message to stderr and exits with non-zero code.
)";
LOG(ERROR) << StringPrintf(format, basename.c_str(), basename.c_str());
return EX_USAGE;
}
int Dispatch(const char* name, const ServiceRetriever& serviceRetriever) {
auto sm = defaultServiceManager();
if (nullptr == sm) {
LOG(ERROR) << "No servicemanager";
return EX_SOFTWARE;
}
auto binder = std::invoke(serviceRetriever, defaultServiceManager(), String16(name));
if (nullptr == binder) {
LOG(ERROR) << "No service \"" << name << "\"";
return EX_SOFTWARE;
}
auto rpcServer = RpcServer::make();
if (nullptr == rpcServer) {
LOG(ERROR) << "Cannot create RpcServer";
return EX_SOFTWARE;
}
rpcServer->iUnderstandThisCodeIsExperimentalAndIWillNotUseItInProduction();
unsigned int port;
if (!rpcServer->setupInetServer(0, &port)) {
LOG(ERROR) << "setupInetServer failed";
return EX_SOFTWARE;
}
auto socket = rpcServer->releaseServer();
auto keepAliveBinder = sp<BBinder>::make();
auto status = binder->setRpcClientDebug(std::move(socket), keepAliveBinder);
if (status != OK) {
LOG(ERROR) << "setRpcClientDebug failed with " << statusToString(status);
return EX_SOFTWARE;
}
LOG(INFO) << "Finish setting up RPC on service " << name << " on port " << port;
std::cout << port << std::endl;
TEMP_FAILURE_RETRY(pause());
PLOG(FATAL) << "TEMP_FAILURE_RETRY(pause()) exits; this should not happen!";
__builtin_unreachable();
}
// Wrapper that wraps a BpServiceManager as a BnServiceManager.
class ServiceManagerProxyToNative : public android::os::BnServiceManager {
public:
ServiceManagerProxyToNative(const sp<android::os::IServiceManager>& impl) : mImpl(impl) {}
android::binder::Status getService(const std::string&,
android::sp<android::IBinder>*) override {
// We can't send BpBinder for regular binder over RPC.
return android::binder::Status::fromStatusT(android::INVALID_OPERATION);
}
android::binder::Status checkService(const std::string&,
android::sp<android::IBinder>*) override {
// We can't send BpBinder for regular binder over RPC.
return android::binder::Status::fromStatusT(android::INVALID_OPERATION);
}
android::binder::Status addService(const std::string&, const android::sp<android::IBinder>&,
bool, int32_t) override {
// We can't send BpBinder for RPC over regular binder.
return android::binder::Status::fromStatusT(android::INVALID_OPERATION);
}
android::binder::Status listServices(int32_t dumpPriority,
std::vector<std::string>* _aidl_return) override {
return mImpl->listServices(dumpPriority, _aidl_return);
}
android::binder::Status registerForNotifications(
const std::string&, const android::sp<android::os::IServiceCallback>&) override {
// We can't send BpBinder for RPC over regular binder.
return android::binder::Status::fromStatusT(android::INVALID_OPERATION);
}
android::binder::Status unregisterForNotifications(
const std::string&, const android::sp<android::os::IServiceCallback>&) override {
// We can't send BpBinder for RPC over regular binder.
return android::binder::Status::fromStatusT(android::INVALID_OPERATION);
}
android::binder::Status isDeclared(const std::string& name, bool* _aidl_return) override {
return mImpl->isDeclared(name, _aidl_return);
}
android::binder::Status getDeclaredInstances(const std::string& iface,
std::vector<std::string>* _aidl_return) override {
return mImpl->getDeclaredInstances(iface, _aidl_return);
}
android::binder::Status updatableViaApex(const std::string& name,
std::optional<std::string>* _aidl_return) override {
return mImpl->updatableViaApex(name, _aidl_return);
}
android::binder::Status registerClientCallback(
const std::string&, const android::sp<android::IBinder>&,
const android::sp<android::os::IClientCallback>&) override {
// We can't send BpBinder for RPC over regular binder.
return android::binder::Status::fromStatusT(android::INVALID_OPERATION);
}
android::binder::Status tryUnregisterService(const std::string&,
const android::sp<android::IBinder>&) override {
// We can't send BpBinder for RPC over regular binder.
return android::binder::Status::fromStatusT(android::INVALID_OPERATION);
}
android::binder::Status getServiceDebugInfo(
std::vector<android::os::ServiceDebugInfo>* _aidl_return) override {
return mImpl->getServiceDebugInfo(_aidl_return);
}
private:
sp<android::os::IServiceManager> mImpl;
};
// Workaround for b/191059588.
// TODO(b/191059588): Once we can run RpcServer on single-threaded services,
// `servicedispatcher manager` should call Dispatch("manager") directly.
int wrapServiceManager(const ServiceRetriever& serviceRetriever) {
auto sm = defaultServiceManager();
if (nullptr == sm) {
LOG(ERROR) << "No servicemanager";
return EX_SOFTWARE;
}
auto service = std::invoke(serviceRetriever, defaultServiceManager(), String16("manager"));
if (nullptr == service) {
LOG(ERROR) << "No service called `manager`";
return EX_SOFTWARE;
}
auto interface = android::os::IServiceManager::asInterface(service);
if (nullptr == interface) {
LOG(ERROR) << "Cannot cast service called `manager` to IServiceManager";
return EX_SOFTWARE;
}
// Work around restriction that doesn't allow us to send proxy over RPC.
interface = sp<ServiceManagerProxyToNative>::make(interface);
service = ServiceManagerProxyToNative::asBinder(interface);
auto rpcServer = RpcServer::make();
rpcServer->iUnderstandThisCodeIsExperimentalAndIWillNotUseItInProduction();
rpcServer->setRootObject(service);
unsigned int port;
if (!rpcServer->setupInetServer(0, &port)) {
LOG(ERROR) << "Unable to set up inet server";
return EX_SOFTWARE;
}
LOG(INFO) << "Finish wrapping servicemanager with RPC on port " << port;
std::cout << port << std::endl;
rpcServer->join();
LOG(FATAL) << "Wrapped servicemanager exits; this should not happen!";
__builtin_unreachable();
}
// Log to logd. For warning and more severe messages, also log to stderr.
class ServiceDispatcherLogger {
public:
void operator()(LogId id, LogSeverity severity, const char* tag, const char* file,
unsigned int line, const char* message) {
mLogdLogger(id, severity, tag, file, line, message);
if (severity >= LogSeverity::WARNING) {
std::cout << std::flush;
std::cerr << Basename(getprogname()) << ": " << message << std::endl;
}
}
private:
LogdLogger mLogdLogger{};
};
} // namespace
int main(int argc, char* argv[]) {
InitLogging(argv, ServiceDispatcherLogger());
if (!GetBoolProperty("ro.debuggable", false)) {
LOG(ERROR) << "servicedispatcher is only allowed on debuggable builds.";
return EX_NOPERM;
}
LOG(WARNING) << "WARNING: servicedispatcher is debug only. Use with caution.";
int opt;
ServiceRetriever serviceRetriever = &android::IServiceManager::checkService;
while (-1 != (opt = getopt(argc, argv, "g"))) {
switch (opt) {
case 'g': {
serviceRetriever = &android::IServiceManager::getService;
} break;
default: {
return Usage(argv[0]);
}
}
}
if (optind + 1 != argc) return Usage(argv[0]);
auto name = argv[optind];
if (name == "manager"sv) {
return wrapServiceManager(serviceRetriever);
}
return Dispatch(name, serviceRetriever);
}
|