-
Notifications
You must be signed in to change notification settings - Fork 0
/
ServiceManager.cpp
457 lines (369 loc) · 13.4 KB
/
ServiceManager.cpp
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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
#define LOG_TAG "hwservicemanager"
#include "ServiceManager.h"
#include "Vintf.h"
#include <android-base/logging.h>
#include <hwbinder/IPCThreadState.h>
#include <hidl/HidlSupport.h>
#include <regex>
#include <sstream>
using android::hardware::IPCThreadState;
namespace android {
namespace hidl {
namespace manager {
namespace V1_0 {
namespace implementation {
static constexpr uint64_t kServiceDiedCookie = 0;
static constexpr uint64_t kPackageListenerDiedCookie = 1;
static constexpr uint64_t kServiceListenerDiedCookie = 2;
size_t ServiceManager::countExistingService() const {
size_t total = 0;
forEachExistingService([&] (const HidlService *) {
++total;
});
return total;
}
void ServiceManager::forEachExistingService(std::function<void(const HidlService *)> f) const {
forEachServiceEntry([f] (const HidlService *service) {
if (service->getService() == nullptr) {
return;
}
f(service);
});
}
void ServiceManager::forEachServiceEntry(std::function<void(const HidlService *)> f) const {
for (const auto &interfaceMapping : mServiceMap) {
const auto &instanceMap = interfaceMapping.second.getInstanceMap();
for (const auto &instanceMapping : instanceMap) {
f(instanceMapping.second.get());
}
}
}
void ServiceManager::serviceDied(uint64_t cookie, const wp<IBase>& who) {
switch (cookie) {
case kServiceDiedCookie:
remove(who);
break;
case kPackageListenerDiedCookie:
removePackageListener(who);
break;
case kServiceListenerDiedCookie:
removeServiceListener(who);
break;
}
}
ServiceManager::InstanceMap &ServiceManager::PackageInterfaceMap::getInstanceMap() {
return mInstanceMap;
}
const ServiceManager::InstanceMap &ServiceManager::PackageInterfaceMap::getInstanceMap() const {
return mInstanceMap;
}
const HidlService *ServiceManager::PackageInterfaceMap::lookup(
const std::string &name) const {
auto it = mInstanceMap.find(name);
if (it == mInstanceMap.end()) {
return nullptr;
}
return it->second.get();
}
HidlService *ServiceManager::PackageInterfaceMap::lookup(
const std::string &name) {
return const_cast<HidlService*>(
const_cast<const PackageInterfaceMap*>(this)->lookup(name));
}
void ServiceManager::PackageInterfaceMap::insertService(
std::unique_ptr<HidlService> &&service) {
mInstanceMap.insert({service->getInstanceName(), std::move(service)});
}
void ServiceManager::PackageInterfaceMap::sendPackageRegistrationNotification(
const hidl_string &fqName,
const hidl_string &instanceName) {
for (auto it = mPackageListeners.begin(); it != mPackageListeners.end();) {
auto ret = (*it)->onRegistration(fqName, instanceName, false /* preexisting */);
if (ret.isOk()) {
++it;
} else {
LOG(ERROR) << "Dropping registration callback for " << fqName << "/" << instanceName
<< ": transport error.";
it = mPackageListeners.erase(it);
}
}
}
void ServiceManager::PackageInterfaceMap::addPackageListener(sp<IServiceNotification> listener) {
for (const auto &instanceMapping : mInstanceMap) {
const std::unique_ptr<HidlService> &service = instanceMapping.second;
if (service->getService() == nullptr) {
continue;
}
auto ret = listener->onRegistration(
service->getInterfaceName(),
service->getInstanceName(),
true /* preexisting */);
if (!ret.isOk()) {
LOG(ERROR) << "Not adding package listener for " << service->getInterfaceName()
<< "/" << service->getInstanceName() << ": transport error "
<< "when sending notification for already registered instance.";
return;
}
}
mPackageListeners.push_back(listener);
}
bool ServiceManager::PackageInterfaceMap::removePackageListener(const wp<IBase>& who) {
bool found = false;
for (auto it = mPackageListeners.begin(); it != mPackageListeners.end();) {
if (*it == who) {
it = mPackageListeners.erase(it);
found = true;
} else {
++it;
}
}
return found;
}
// Methods from ::android::hidl::manager::V1_0::IServiceManager follow.
Return<sp<IBase>> ServiceManager::get(const hidl_string& fqName,
const hidl_string& name) {
pid_t pid = IPCThreadState::self()->getCallingPid();
if (!mAcl.canGet(fqName, pid)) {
return nullptr;
}
auto ifaceIt = mServiceMap.find(fqName);
if (ifaceIt == mServiceMap.end()) {
return nullptr;
}
const PackageInterfaceMap &ifaceMap = ifaceIt->second;
const HidlService *hidlService = ifaceMap.lookup(name);
if (hidlService == nullptr) {
return nullptr;
}
return hidlService->getService();
}
Return<bool> ServiceManager::add(const hidl_string& name, const sp<IBase>& service) {
bool isValidService = false;
if (service == nullptr) {
return false;
}
// TODO(b/34235311): use HIDL way to determine this
// also, this assumes that the PID that is registering is the pid that is the service
pid_t pid = IPCThreadState::self()->getCallingPid();
auto ret = service->interfaceChain([&](const auto &interfaceChain) {
if (interfaceChain.size() == 0) {
return;
}
// First, verify you're allowed to add() the whole interface hierarchy
for(size_t i = 0; i < interfaceChain.size(); i++) {
std::string fqName = interfaceChain[i];
if (!mAcl.canAdd(fqName, pid)) {
return;
}
}
for(size_t i = 0; i < interfaceChain.size(); i++) {
std::string fqName = interfaceChain[i];
PackageInterfaceMap &ifaceMap = mServiceMap[fqName];
HidlService *hidlService = ifaceMap.lookup(name);
if (hidlService == nullptr) {
ifaceMap.insertService(
std::make_unique<HidlService>(fqName, name, service, pid));
} else {
if (hidlService->getService() != nullptr) {
auto ret = hidlService->getService()->unlinkToDeath(this);
ret.isOk(); // ignore
}
hidlService->setService(service, pid);
}
ifaceMap.sendPackageRegistrationNotification(fqName, name);
}
auto linkRet = service->linkToDeath(this, 0 /*cookie*/);
linkRet.isOk(); // ignore
isValidService = true;
});
if (!ret.isOk()) {
LOG(ERROR) << "Failed to retrieve interface chain.";
return false;
}
return isValidService;
}
Return<ServiceManager::Transport> ServiceManager::getTransport(const hidl_string& fqName,
const hidl_string& name) {
using ::android::hardware::getTransport;
pid_t pid = IPCThreadState::self()->getCallingPid();
if (!mAcl.canGet(fqName, pid)) {
return Transport::EMPTY;
}
switch (getTransport(fqName, name)) {
case vintf::Transport::HWBINDER:
return Transport::HWBINDER;
case vintf::Transport::PASSTHROUGH:
return Transport::PASSTHROUGH;
case vintf::Transport::EMPTY:
default:
return Transport::EMPTY;
}
}
Return<void> ServiceManager::list(list_cb _hidl_cb) {
pid_t pid = IPCThreadState::self()->getCallingPid();
if (!mAcl.canList(pid)) {
_hidl_cb({});
return Void();
}
hidl_vec<hidl_string> list;
list.resize(countExistingService());
size_t idx = 0;
forEachExistingService([&] (const HidlService *service) {
list[idx++] = service->string();
});
_hidl_cb(list);
return Void();
}
Return<void> ServiceManager::listByInterface(const hidl_string& fqName,
listByInterface_cb _hidl_cb) {
pid_t pid = IPCThreadState::self()->getCallingPid();
if (!mAcl.canGet(fqName, pid)) {
_hidl_cb({});
return Void();
}
auto ifaceIt = mServiceMap.find(fqName);
if (ifaceIt == mServiceMap.end()) {
_hidl_cb(hidl_vec<hidl_string>());
return Void();
}
const auto &instanceMap = ifaceIt->second.getInstanceMap();
hidl_vec<hidl_string> list;
size_t total = 0;
for (const auto &serviceMapping : instanceMap) {
const std::unique_ptr<HidlService> &service = serviceMapping.second;
if (service->getService() == nullptr) continue;
++total;
}
list.resize(total);
size_t idx = 0;
for (const auto &serviceMapping : instanceMap) {
const std::unique_ptr<HidlService> &service = serviceMapping.second;
if (service->getService() == nullptr) continue;
list[idx++] = service->getInstanceName();
}
_hidl_cb(list);
return Void();
}
Return<bool> ServiceManager::registerForNotifications(const hidl_string& fqName,
const hidl_string& name,
const sp<IServiceNotification>& callback) {
if (callback == nullptr) {
return false;
}
pid_t pid = IPCThreadState::self()->getCallingPid();
if (!mAcl.canGet(fqName, pid)) {
return false;
}
PackageInterfaceMap &ifaceMap = mServiceMap[fqName];
if (name.empty()) {
auto ret = callback->linkToDeath(this, kPackageListenerDiedCookie /*cookie*/);
if (!ret.isOk()) {
LOG(ERROR) << "Failed to register death recipient for " << fqName << "/" << name;
return false;
}
ifaceMap.addPackageListener(callback);
return true;
}
HidlService *service = ifaceMap.lookup(name);
auto ret = callback->linkToDeath(this, kServiceListenerDiedCookie);
if (!ret.isOk()) {
LOG(ERROR) << "Failed to register death recipient for " << fqName << "/" << name;
return false;
}
if (service == nullptr) {
auto adding = std::make_unique<HidlService>(fqName, name);
adding->addListener(callback);
ifaceMap.insertService(std::move(adding));
} else {
service->addListener(callback);
}
return true;
}
Return<void> ServiceManager::debugDump(debugDump_cb _cb) {
pid_t pid = IPCThreadState::self()->getCallingPid();
if (!mAcl.canList(pid)) {
_cb({});
return Void();
}
std::vector<IServiceManager::InstanceDebugInfo> list;
forEachServiceEntry([&] (const HidlService *service) {
hidl_vec<int32_t> clientPids;
clientPids.resize(service->getPassthroughClients().size());
size_t i = 0;
for (pid_t p : service->getPassthroughClients()) {
clientPids[i++] = p;
}
list.push_back({
.pid = service->getPid(),
.interfaceName = service->getInterfaceName(),
.instanceName = service->getInstanceName(),
.clientPids = clientPids,
.arch = ::android::hidl::base::V1_0::DebugInfo::Architecture::UNKNOWN
});
});
_cb(list);
return Void();
}
Return<void> ServiceManager::registerPassthroughClient(const hidl_string &fqName,
const hidl_string &name) {
pid_t pid = IPCThreadState::self()->getCallingPid();
if (!mAcl.canGet(fqName, pid)) {
/* We guard this function with "get", because it's typically used in
* the getService() path, albeit for a passthrough service in this
* case
*/
return Void();
}
PackageInterfaceMap &ifaceMap = mServiceMap[fqName];
if (name.empty()) {
LOG(WARNING) << "registerPassthroughClient encounters empty instance name for "
<< fqName.c_str();
return Void();
}
HidlService *service = ifaceMap.lookup(name);
if (service == nullptr) {
auto adding = std::make_unique<HidlService>(fqName, name);
adding->registerPassthroughClient(pid);
ifaceMap.insertService(std::move(adding));
} else {
service->registerPassthroughClient(pid);
}
return Void();
}
bool ServiceManager::remove(const wp<IBase>& who) {
bool found = false;
for (auto &interfaceMapping : mServiceMap) {
auto &instanceMap = interfaceMapping.second.getInstanceMap();
for (auto &servicePair : instanceMap) {
const std::unique_ptr<HidlService> &service = servicePair.second;
if (service->getService() == who) {
service->setService(nullptr, static_cast<pid_t>(IServiceManager::PidConstant::NO_PID));
found = true;
}
}
}
return found;
}
bool ServiceManager::removePackageListener(const wp<IBase>& who) {
bool found = false;
for (auto &interfaceMapping : mServiceMap) {
found |= interfaceMapping.second.removePackageListener(who);
}
return found;
}
bool ServiceManager::removeServiceListener(const wp<IBase>& who) {
bool found = false;
for (auto &interfaceMapping : mServiceMap) {
auto &instanceMap = interfaceMapping.second.getInstanceMap();
for (auto &servicePair : instanceMap) {
const std::unique_ptr<HidlService> &service = servicePair.second;
found |= service->removeListener(who);
}
}
return found;
}
} // namespace implementation
} // namespace V1_0
} // namespace manager
} // namespace hidl
} // namespace android