1 /* 2 * Copyright (C) 2016 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 #define LOG_TAG "hwservicemanager" 18 19 #include "ServiceManager.h" 20 #include "Vintf.h" 21 22 #include <android-base/logging.h> 23 #include <android-base/properties.h> 24 #include <hwbinder/IPCThreadState.h> 25 #include <hidl/HidlSupport.h> 26 #include <hidl/HidlTransportSupport.h> 27 #include <regex> 28 #include <sstream> 29 #include <thread> 30 31 using android::hardware::IPCThreadState; 32 using ::android::hardware::interfacesEqual; 33 34 namespace android { 35 namespace hidl { 36 namespace manager { 37 namespace implementation { 38 39 AccessControl::CallingContext getBinderCallingContext() { 40 const auto& self = IPCThreadState::self(); 41 42 pid_t pid = self->getCallingPid(); 43 const char* sid = self->getCallingSid(); 44 45 if (sid == nullptr) { 46 if (pid != getpid()) { 47 android_errorWriteLog(0x534e4554, "121035042"); 48 } 49 50 return AccessControl::getCallingContext(pid); 51 } else { 52 return { true, sid, pid }; 53 } 54 } 55 56 static constexpr uint64_t kServiceDiedCookie = 0; 57 static constexpr uint64_t kPackageListenerDiedCookie = 1; 58 static constexpr uint64_t kServiceListenerDiedCookie = 2; 59 static constexpr uint64_t kClientCallbackDiedCookie = 3; 60 61 size_t ServiceManager::countExistingService() const { 62 size_t total = 0; 63 forEachExistingService([&] (const HidlService *) { 64 ++total; 65 return true; // continue 66 }); 67 return total; 68 } 69 70 void ServiceManager::forEachExistingService(std::function<bool(const HidlService *)> f) const { 71 forEachServiceEntry([&] (const HidlService *service) { 72 if (service->getService() == nullptr) { 73 return true; // continue 74 } 75 return f(service); 76 }); 77 } 78 79 void ServiceManager::forEachExistingService(std::function<bool(HidlService *)> f) { 80 forEachServiceEntry([&] (HidlService *service) { 81 if (service->getService() == nullptr) { 82 return true; // continue 83 } 84 return f(service); 85 }); 86 } 87 88 void ServiceManager::forEachServiceEntry(std::function<bool(const HidlService *)> f) const { 89 for (const auto& interfaceMapping : mServiceMap) { 90 const auto& instanceMap = interfaceMapping.second.getInstanceMap(); 91 92 for (const auto& instanceMapping : instanceMap) { 93 if (!f(instanceMapping.second.get())) { 94 return; 95 } 96 } 97 } 98 } 99 100 void ServiceManager::forEachServiceEntry(std::function<bool(HidlService *)> f) { 101 for (auto& interfaceMapping : mServiceMap) { 102 auto& instanceMap = interfaceMapping.second.getInstanceMap(); 103 104 for (auto& instanceMapping : instanceMap) { 105 if (!f(instanceMapping.second.get())) { 106 return; 107 } 108 } 109 } 110 } 111 112 HidlService* ServiceManager::lookup(const std::string& fqName, const std::string& name) { 113 auto ifaceIt = mServiceMap.find(fqName); 114 if (ifaceIt == mServiceMap.end()) { 115 return nullptr; 116 } 117 118 PackageInterfaceMap &ifaceMap = ifaceIt->second; 119 120 HidlService *hidlService = ifaceMap.lookup(name); 121 122 return hidlService; 123 } 124 125 void ServiceManager::serviceDied(uint64_t cookie, const wp<IBase>& who) { 126 bool serviceRemoved = false; 127 switch (cookie) { 128 case kServiceDiedCookie: 129 serviceRemoved = removeService(who, nullptr /* restrictToInstanceName */); 130 break; 131 case kPackageListenerDiedCookie: 132 serviceRemoved = removePackageListener(who); 133 break; 134 case kServiceListenerDiedCookie: 135 serviceRemoved = removeServiceListener(who); 136 break; 137 case kClientCallbackDiedCookie: { 138 sp<IBase> base = who.promote(); 139 IClientCallback* callback = static_cast<IClientCallback*>(base.get()); 140 serviceRemoved = unregisterClientCallback(nullptr /*service*/, 141 sp<IClientCallback>(callback)); 142 } break; 143 } 144 145 if (!serviceRemoved) { 146 LOG(ERROR) << "Received death notification but interface instance not removed. Cookie: " 147 << cookie << " Service pointer: " << who.promote().get(); 148 } 149 } 150 151 ServiceManager::InstanceMap &ServiceManager::PackageInterfaceMap::getInstanceMap() { 152 return mInstanceMap; 153 } 154 155 const ServiceManager::InstanceMap &ServiceManager::PackageInterfaceMap::getInstanceMap() const { 156 return mInstanceMap; 157 } 158 159 const HidlService *ServiceManager::PackageInterfaceMap::lookup( 160 const std::string &name) const { 161 auto it = mInstanceMap.find(name); 162 163 if (it == mInstanceMap.end()) { 164 return nullptr; 165 } 166 167 return it->second.get(); 168 } 169 170 HidlService *ServiceManager::PackageInterfaceMap::lookup( 171 const std::string &name) { 172 173 return const_cast<HidlService*>( 174 const_cast<const PackageInterfaceMap*>(this)->lookup(name)); 175 } 176 177 void ServiceManager::PackageInterfaceMap::insertService( 178 std::unique_ptr<HidlService> &&service) { 179 mInstanceMap.insert({service->getInstanceName(), std::move(service)}); 180 } 181 182 void ServiceManager::PackageInterfaceMap::sendPackageRegistrationNotification( 183 const hidl_string &fqName, 184 const hidl_string &instanceName) { 185 186 for (auto it = mPackageListeners.begin(); it != mPackageListeners.end();) { 187 auto ret = (*it)->onRegistration(fqName, instanceName, false /* preexisting */); 188 if (ret.isOk()) { 189 ++it; 190 } else { 191 LOG(ERROR) << "Dropping registration callback for " << fqName << "/" << instanceName 192 << ": transport error."; 193 it = mPackageListeners.erase(it); 194 } 195 } 196 } 197 198 void ServiceManager::PackageInterfaceMap::addPackageListener(sp<IServiceNotification> listener) { 199 for (const auto &instanceMapping : mInstanceMap) { 200 const std::unique_ptr<HidlService> &service = instanceMapping.second; 201 202 if (service->getService() == nullptr) { 203 continue; 204 } 205 206 auto ret = listener->onRegistration( 207 service->getInterfaceName(), 208 service->getInstanceName(), 209 true /* preexisting */); 210 if (!ret.isOk()) { 211 LOG(ERROR) << "Not adding package listener for " << service->getInterfaceName() 212 << "/" << service->getInstanceName() << ": transport error " 213 << "when sending notification for already registered instance."; 214 return; 215 } 216 } 217 mPackageListeners.push_back(listener); 218 } 219 220 bool ServiceManager::PackageInterfaceMap::removePackageListener(const wp<IBase>& who) { 221 bool found = false; 222 223 for (auto it = mPackageListeners.begin(); it != mPackageListeners.end();) { 224 if (interfacesEqual(*it, who.promote())) { 225 it = mPackageListeners.erase(it); 226 found = true; 227 } else { 228 ++it; 229 } 230 } 231 232 return found; 233 } 234 235 bool ServiceManager::PackageInterfaceMap::removeServiceListener(const wp<IBase>& who) { 236 bool found = false; 237 238 for (auto &servicePair : getInstanceMap()) { 239 const std::unique_ptr<HidlService> &service = servicePair.second; 240 found |= service->removeListener(who); 241 } 242 243 return found; 244 } 245 246 static void tryStartService(const std::string& fqName, const std::string& name) { 247 using ::android::base::SetProperty; 248 249 // The "happy path" here is starting up a service that is configured as a 250 // lazy HAL, but we aren't sure that is the case. If the service doesn't 251 // have an 'interface' entry in its .rc file OR if the service is already 252 // running, then this will be a no-op. So, for instance, if a service is 253 // deadlocked during startup, you will see this message repeatedly. 254 LOG(INFO) << "Since " << fqName << "/" << name 255 << " is not registered, trying to start it as a lazy HAL."; 256 257 std::thread([=] { 258 if (!SetProperty("ctl.interface_start", fqName + "/" + name)) { 259 LOG(INFO) << "Tried to start " << fqName << "/" << name 260 << " as a lazy service, but was unable to. Usually this happens when a " 261 "service is not installed, but if the service is intended to be used as a " 262 "lazy service, then it may be configured incorrectly."; 263 } 264 }).detach(); 265 } 266 267 // Methods from ::android::hidl::manager::V1_0::IServiceManager follow. 268 Return<sp<IBase>> ServiceManager::get(const hidl_string& hidlFqName, 269 const hidl_string& hidlName) { 270 const std::string fqName = hidlFqName; 271 const std::string name = hidlName; 272 273 if (!mAcl.canGet(fqName, getBinderCallingContext())) { 274 return nullptr; 275 } 276 277 HidlService* hidlService = lookup(fqName, name); 278 if (hidlService == nullptr) { 279 tryStartService(fqName, name); 280 return nullptr; 281 } 282 283 sp<IBase> service = hidlService->getService(); 284 if (service == nullptr) { 285 tryStartService(fqName, name); 286 return nullptr; 287 } 288 289 // Let HidlService know that we handed out a client. If the client drops the service before the 290 // next time handleClientCallbacks is called, it will still know that the service had been handed out. 291 hidlService->guaranteeClient(); 292 forEachExistingService([&] (HidlService *otherService) { 293 if (otherService != hidlService && interfacesEqual(service, otherService->getService())) { 294 otherService->guaranteeClient(); 295 } 296 return true; 297 }); 298 299 // This is executed immediately after the binder driver confirms the transaction. The driver 300 // will update the appropriate data structures to reflect the fact that the client now has the 301 // service this function is returning. Nothing else can update the HidlService at the same 302 // time. This will run before anything else can modify the HidlService which is owned by this 303 // object, so it will be in the same state that it was when this function returns. 304 hardware::addPostCommandTask([hidlService] { 305 hidlService->handleClientCallbacks(false /* isCalledOnInterval */, 1 /*knownClientCount*/); 306 }); 307 308 return service; 309 } 310 311 Return<bool> ServiceManager::add(const hidl_string& name, const sp<IBase>& service) { 312 bool addSuccess = false; 313 314 if (service == nullptr) { 315 return false; 316 } 317 318 auto pidcon = getBinderCallingContext(); 319 320 if (!mAcl.canAdd(IBase::descriptor, pidcon)) { 321 LOG(ERROR) << "Missing permissions to add IBase"; 322 return false; 323 } 324 325 auto ret = service->interfaceChain([&](const auto &interfaceChain) { 326 addSuccess = addImpl(name, service, interfaceChain, pidcon); 327 }); 328 329 if (!ret.isOk()) { 330 LOG(ERROR) << "Failed to retrieve interface chain: " << ret.description(); 331 return false; 332 } 333 334 return addSuccess; 335 } 336 337 bool ServiceManager::addImpl(const std::string& name, 338 const sp<IBase>& service, 339 const hidl_vec<hidl_string>& interfaceChain, 340 const AccessControl::CallingContext& callingContext) { 341 if (interfaceChain.size() == 0) { 342 LOG(WARNING) << "Empty interface chain for " << name; 343 return false; 344 } 345 346 // First, verify you're allowed to add() the whole interface hierarchy 347 for(size_t i = 0; i < interfaceChain.size(); i++) { 348 const std::string fqName = interfaceChain[i]; 349 350 if (!mAcl.canAdd(fqName, callingContext)) { 351 return false; 352 } 353 } 354 355 const std::string childFqName = interfaceChain[0]; 356 357 // Detect duplicate registration 358 if (interfaceChain.size() > 1) { 359 // second to last entry should be the highest base class other than IBase. 360 const std::string baseFqName = interfaceChain[interfaceChain.size() - 2]; 361 const HidlService *hidlService = lookup(baseFqName, name); 362 if (hidlService != nullptr && hidlService->getService() != nullptr) { 363 // This shouldn't occur during normal operation. Here are some cases where 364 // it might get hit: 365 // - bad configuration (service installed on device multiple times) 366 // - race between death notification and a new service being registered 367 // (previous logs should indicate a separate problem) 368 pid_t newServicePid = IPCThreadState::self()->getCallingPid(); 369 pid_t oldServicePid = hidlService->getDebugPid(); 370 LOG(WARNING) << "Detected instance of " << childFqName << " (pid: " << newServicePid 371 << ") registering over instance of or with base of " << baseFqName << " (pid: " 372 << oldServicePid << ")."; 373 } 374 } 375 376 // Unregister superclass if subclass is registered over it 377 { 378 // For IBar extends IFoo if IFoo/default is being registered, remove 379 // IBar/default. This makes sure the following two things are equivalent 380 // 1). IBar::castFrom(IFoo::getService(X)) 381 // 2). IBar::getService(X) 382 // assuming that IBar is declared in the device manifest and there 383 // is also not an IBaz extends IFoo and there is no race. 384 const HidlService *hidlService = lookup(childFqName, name); 385 if (hidlService != nullptr) { 386 const sp<IBase> remove = hidlService->getService(); 387 388 if (remove != nullptr) { 389 const std::string instanceName = name; 390 removeService(remove, &instanceName /* restrictToInstanceName */); 391 } 392 } 393 } 394 395 // Detect missing manifest entries of superclass, when subclass in manifest. 396 { 397 // Ideally we could require all HALs registered with hwservicemanager to 398 // be in the VINTF manifest. However, this would prevent tests from 399 // running, and we would need another method of registering them (AIDL 400 // servicemanager gets around this because only certain objects are 401 // VINTF objects). So, for HIDL, we rely on VTS. 402 // 403 // When registering a HAL, in the client process, it checks to make sure 404 // that the last (leaf) class in the chain is in the VINTF manifest and 405 // fails. However, this fails to take into account parent classes. If 406 // parent classes are associated with certain VTS tests, then those VTS 407 // tests will not run until vts_treble_vintf_vendor_test fails and the 408 // failures are fixed (namely adding this into a manifest). 409 // 410 // So, here we make sure that if something is in the manifest, all of 411 // its parent classes are. 412 using ::android::hardware::getTransport; 413 if (vintf::Transport::EMPTY != getTransport(childFqName, name)) { 414 bool parentsInManifest = true; 415 416 // skip over latest, and check over all interfaces except the base 417 // interface (android.hidl.base is never in the manifest) 418 for (size_t i = 1; i + 1 < interfaceChain.size(); i++) { 419 if (vintf::Transport::EMPTY == getTransport(interfaceChain[i], name)) { 420 LOG(ERROR) << childFqName << "/" << name 421 << " is in the VINTF manifest, but its superclass " 422 << interfaceChain[i] << " is not. Refusing to register."; 423 parentsInManifest = false; 424 } 425 } 426 if (!parentsInManifest) { 427 return false; 428 } 429 } 430 } 431 432 for(size_t i = 0; i < interfaceChain.size(); i++) { 433 const std::string fqName = interfaceChain[i]; 434 435 PackageInterfaceMap &ifaceMap = mServiceMap[fqName]; 436 HidlService *hidlService = ifaceMap.lookup(name); 437 438 if (hidlService == nullptr) { 439 ifaceMap.insertService( 440 std::make_unique<HidlService>(fqName, name, service, callingContext.pid)); 441 } else { 442 hidlService->setService(service, callingContext.pid); 443 } 444 445 ifaceMap.sendPackageRegistrationNotification(fqName, name); 446 } 447 448 bool linkRet = service->linkToDeath(this, kServiceDiedCookie).withDefault(false); 449 if (!linkRet) { 450 LOG(ERROR) << "Could not link to death for " << interfaceChain[0] << "/" << name; 451 } 452 453 return true; 454 } 455 456 Return<ServiceManager::Transport> ServiceManager::getTransport(const hidl_string& fqName, 457 const hidl_string& name) { 458 using ::android::hardware::getTransport; 459 460 if (!mAcl.canGet(fqName, getBinderCallingContext())) { 461 return Transport::EMPTY; 462 } 463 464 switch (getTransport(fqName, name)) { 465 case vintf::Transport::HWBINDER: 466 return Transport::HWBINDER; 467 case vintf::Transport::PASSTHROUGH: 468 return Transport::PASSTHROUGH; 469 case vintf::Transport::EMPTY: 470 default: 471 return Transport::EMPTY; 472 } 473 } 474 475 Return<void> ServiceManager::list(list_cb _hidl_cb) { 476 if (!mAcl.canList(getBinderCallingContext())) { 477 _hidl_cb({}); 478 return Void(); 479 } 480 481 hidl_vec<hidl_string> list; 482 483 list.resize(countExistingService()); 484 485 size_t idx = 0; 486 forEachExistingService([&] (const HidlService *service) { 487 list[idx++] = service->string(); 488 return true; // continue 489 }); 490 491 _hidl_cb(list); 492 return Void(); 493 } 494 495 Return<void> ServiceManager::listByInterface(const hidl_string& fqName, 496 listByInterface_cb _hidl_cb) { 497 if (!mAcl.canGet(fqName, getBinderCallingContext())) { 498 _hidl_cb({}); 499 return Void(); 500 } 501 502 auto ifaceIt = mServiceMap.find(fqName); 503 if (ifaceIt == mServiceMap.end()) { 504 _hidl_cb(hidl_vec<hidl_string>()); 505 return Void(); 506 } 507 508 const auto &instanceMap = ifaceIt->second.getInstanceMap(); 509 510 hidl_vec<hidl_string> list; 511 512 size_t total = 0; 513 for (const auto &serviceMapping : instanceMap) { 514 const std::unique_ptr<HidlService> &service = serviceMapping.second; 515 if (service->getService() == nullptr) continue; 516 517 ++total; 518 } 519 list.resize(total); 520 521 size_t idx = 0; 522 for (const auto &serviceMapping : instanceMap) { 523 const std::unique_ptr<HidlService> &service = serviceMapping.second; 524 if (service->getService() == nullptr) continue; 525 526 list[idx++] = service->getInstanceName(); 527 } 528 529 _hidl_cb(list); 530 return Void(); 531 } 532 533 Return<bool> ServiceManager::registerForNotifications(const hidl_string& fqName, 534 const hidl_string& name, 535 const sp<IServiceNotification>& callback) { 536 if (callback == nullptr) { 537 return false; 538 } 539 540 if (!mAcl.canGet(fqName, getBinderCallingContext())) { 541 return false; 542 } 543 544 PackageInterfaceMap &ifaceMap = mServiceMap[fqName]; 545 546 if (name.empty()) { 547 bool ret = callback->linkToDeath(this, kPackageListenerDiedCookie).withDefault(false); 548 if (!ret) { 549 LOG(ERROR) << "Failed to register death recipient for " << fqName << "/" << name; 550 return false; 551 } 552 ifaceMap.addPackageListener(callback); 553 return true; 554 } 555 556 HidlService *service = ifaceMap.lookup(name); 557 558 bool ret = callback->linkToDeath(this, kServiceListenerDiedCookie).withDefault(false); 559 if (!ret) { 560 LOG(ERROR) << "Failed to register death recipient for " << fqName << "/" << name; 561 return false; 562 } 563 564 if (service == nullptr) { 565 auto adding = std::make_unique<HidlService>(fqName, name); 566 adding->addListener(callback); 567 ifaceMap.insertService(std::move(adding)); 568 } else { 569 service->addListener(callback); 570 } 571 572 return true; 573 } 574 575 Return<bool> ServiceManager::unregisterForNotifications(const hidl_string& fqName, 576 const hidl_string& name, 577 const sp<IServiceNotification>& callback) { 578 if (callback == nullptr) { 579 LOG(ERROR) << "Cannot unregister null callback for " << fqName << "/" << name; 580 return false; 581 } 582 583 // NOTE: don't need ACL since callback is binder token, and if someone has gotten it, 584 // then they already have access to it. 585 586 if (fqName.empty()) { 587 bool success = false; 588 success |= removePackageListener(callback); 589 success |= removeServiceListener(callback); 590 return success; 591 } 592 593 PackageInterfaceMap &ifaceMap = mServiceMap[fqName]; 594 595 if (name.empty()) { 596 bool success = false; 597 success |= ifaceMap.removePackageListener(callback); 598 success |= ifaceMap.removeServiceListener(callback); 599 return success; 600 } 601 602 HidlService *service = ifaceMap.lookup(name); 603 604 if (service == nullptr) { 605 return false; 606 } 607 608 return service->removeListener(callback); 609 } 610 611 Return<bool> ServiceManager::registerClientCallback(const hidl_string& hidlFqName, 612 const hidl_string& hidlName, 613 const sp<IBase>& server, 614 const sp<IClientCallback>& cb) { 615 if (server == nullptr || cb == nullptr) return false; 616 617 const std::string fqName = hidlFqName; 618 const std::string name = hidlName; 619 620 // only the server of the interface can register a client callback 621 pid_t pid = IPCThreadState::self()->getCallingPid(); 622 if (!mAcl.canAdd(fqName, getBinderCallingContext())) { 623 return false; 624 } 625 626 HidlService* registered = lookup(fqName, name); 627 628 if (registered == nullptr) { 629 return false; 630 } 631 632 // sanity 633 if (registered->getDebugPid() != pid) { 634 LOG(WARNING) << "Only a server can register for client callbacks (for " << fqName 635 << "/" << name << ")"; 636 return false; 637 } 638 639 sp<IBase> service = registered->getService(); 640 641 if (!interfacesEqual(service, server)) { 642 LOG(WARNING) << "Tried to register client callback for " << fqName << "/" << name 643 << " but a different service is registered under this name."; 644 return false; 645 } 646 647 bool linkRet = cb->linkToDeath(this, kClientCallbackDiedCookie).withDefault(false); 648 if (!linkRet) { 649 LOG(ERROR) << "Could not link to death for registerClientCallback"; 650 return false; 651 } 652 653 // knownClientCount 654 // - one from binder transaction (base here) 655 // - one from hwservicemanager 656 registered->addClientCallback(cb, 2 /*knownClientCount*/); 657 658 return true; 659 } 660 661 Return<bool> ServiceManager::unregisterClientCallback(const sp<IBase>& server, 662 const sp<IClientCallback>& cb) { 663 if (cb == nullptr) return false; 664 665 bool removed = false; 666 667 forEachExistingService([&] (HidlService *service) { 668 if (server == nullptr || interfacesEqual(service->getService(), server)) { 669 removed |= service->removeClientCallback(cb); 670 } 671 return true; // continue 672 }); 673 674 return removed; 675 } 676 677 void ServiceManager::handleClientCallbacks() { 678 forEachServiceEntry([&] (HidlService *service) { 679 // hwservicemanager will hold one reference, so knownClientCount is 1. 680 service->handleClientCallbacks(true /* isCalledOnInterval */, 1 /*knownClientCount*/); 681 return true; // continue 682 }); 683 } 684 685 Return<bool> ServiceManager::addWithChain(const hidl_string& name, 686 const sp<IBase>& service, 687 const hidl_vec<hidl_string>& chain) { 688 if (service == nullptr) { 689 return false; 690 } 691 692 auto callingContext = getBinderCallingContext(); 693 694 return addImpl(name, service, chain, callingContext); 695 } 696 697 Return<void> ServiceManager::listManifestByInterface(const hidl_string& fqName, 698 listManifestByInterface_cb _hidl_cb) { 699 if (!mAcl.canGet(fqName, getBinderCallingContext())) { 700 _hidl_cb({}); 701 return Void(); 702 } 703 704 std::set<std::string> instances = getInstances(fqName); 705 hidl_vec<hidl_string> ret(instances.begin(), instances.end()); 706 707 _hidl_cb(ret); 708 return Void(); 709 } 710 711 Return<bool> ServiceManager::tryUnregister(const hidl_string& hidlFqName, 712 const hidl_string& hidlName, 713 const sp<IBase>& service) { 714 const std::string fqName = hidlFqName; 715 const std::string name = hidlName; 716 717 if (service == nullptr) { 718 return false; 719 } 720 721 if (!mAcl.canAdd(fqName, getBinderCallingContext())) { 722 return false; 723 } 724 725 HidlService* registered = lookup(fqName, name); 726 727 // sanity 728 pid_t pid = IPCThreadState::self()->getCallingPid(); 729 if (registered->getDebugPid() != pid) { 730 LOG(WARNING) << "Only a server can unregister itself (for " << fqName 731 << "/" << name << ")"; 732 return false; 733 } 734 735 sp<IBase> server = registered->getService(); 736 737 if (!interfacesEqual(service, server)) { 738 LOG(WARNING) << "Tried to unregister for " << fqName << "/" << name 739 << " but a different service is registered under this name."; 740 return false; 741 } 742 743 // knownClientCount 744 // - one from binder transaction (base here) 745 // - one from hwservicemanager 746 bool clients = registered->forceHandleClientCallbacks(false /*isCalledOnInterval*/, 2 /*knownClientCount*/); 747 748 if (clients) { 749 // client callbacks are either disabled or there are other clients 750 LOG(INFO) << "Tried to unregister for " << fqName << "/" << name 751 << " but there are clients: " << clients; 752 return false; 753 } 754 755 // will remove entire parent hierarchy 756 bool success = removeService(service, &name /*restrictToInstanceName*/); 757 758 if (registered->getService() != nullptr) { 759 LOG(ERROR) << "Bad state. Unregistration failed for " << fqName << "/" << name << "."; 760 return false; 761 } 762 763 return success; 764 } 765 766 Return<void> ServiceManager::debugDump(debugDump_cb _cb) { 767 if (!mAcl.canList(getBinderCallingContext())) { 768 _cb({}); 769 return Void(); 770 } 771 772 std::vector<IServiceManager::InstanceDebugInfo> list; 773 forEachServiceEntry([&] (const HidlService *service) { 774 hidl_vec<int32_t> clientPids; 775 clientPids.resize(service->getPassthroughClients().size()); 776 777 size_t i = 0; 778 for (pid_t p : service->getPassthroughClients()) { 779 clientPids[i++] = p; 780 } 781 782 list.push_back({ 783 .interfaceName = service->getInterfaceName(), 784 .instanceName = service->getInstanceName(), 785 .pid = service->getDebugPid(), 786 .clientPids = clientPids, 787 .arch = ::android::hidl::base::V1_0::DebugInfo::Architecture::UNKNOWN 788 }); 789 790 return true; // continue 791 }); 792 793 _cb(list); 794 return Void(); 795 } 796 797 798 Return<void> ServiceManager::registerPassthroughClient(const hidl_string &fqName, 799 const hidl_string &name) { 800 auto callingContext = getBinderCallingContext(); 801 802 if (!mAcl.canGet(fqName, callingContext)) { 803 /* We guard this function with "get", because it's typically used in 804 * the getService() path, albeit for a passthrough service in this 805 * case 806 */ 807 return Void(); 808 } 809 810 PackageInterfaceMap &ifaceMap = mServiceMap[fqName]; 811 812 if (name.empty()) { 813 LOG(WARNING) << "registerPassthroughClient encounters empty instance name for " 814 << fqName.c_str(); 815 return Void(); 816 } 817 818 HidlService *service = ifaceMap.lookup(name); 819 820 if (service == nullptr) { 821 auto adding = std::make_unique<HidlService>(fqName, name); 822 adding->registerPassthroughClient(callingContext.pid); 823 ifaceMap.insertService(std::move(adding)); 824 } else { 825 service->registerPassthroughClient(callingContext.pid); 826 } 827 return Void(); 828 } 829 830 bool ServiceManager::removeService(const wp<IBase>& who, const std::string* restrictToInstanceName) { 831 bool keepInstance = false; 832 bool removed = false; 833 for (auto &interfaceMapping : mServiceMap) { 834 auto &instanceMap = interfaceMapping.second.getInstanceMap(); 835 836 for (auto &servicePair : instanceMap) { 837 const std::string &instanceName = servicePair.first; 838 const std::unique_ptr<HidlService> &service = servicePair.second; 839 840 if (interfacesEqual(service->getService(), who.promote())) { 841 if (restrictToInstanceName != nullptr && *restrictToInstanceName != instanceName) { 842 // We cannot remove all instances of this service, so we don't return that it 843 // has been entirely removed. 844 keepInstance = true; 845 continue; 846 } 847 848 service->setService(nullptr, static_cast<pid_t>(IServiceManager::PidConstant::NO_PID)); 849 removed = true; 850 } 851 } 852 } 853 854 return !keepInstance && removed; 855 } 856 857 bool ServiceManager::removePackageListener(const wp<IBase>& who) { 858 bool found = false; 859 860 for (auto &interfaceMapping : mServiceMap) { 861 found |= interfaceMapping.second.removePackageListener(who); 862 } 863 864 return found; 865 } 866 867 bool ServiceManager::removeServiceListener(const wp<IBase>& who) { 868 bool found = false; 869 for (auto &interfaceMapping : mServiceMap) { 870 auto &packageInterfaceMap = interfaceMapping.second; 871 872 found |= packageInterfaceMap.removeServiceListener(who); 873 } 874 return found; 875 } 876 } // namespace implementation 877 } // namespace manager 878 } // namespace hidl 879 } // namespace android 880