ServiceManager.cpp 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820
  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. #define LOG_TAG "hwservicemanager"
  17. #include "ServiceManager.h"
  18. #include "Vintf.h"
  19. #include <android-base/logging.h>
  20. #include <android-base/properties.h>
  21. #include <hwbinder/IPCThreadState.h>
  22. #include <hidl/HidlSupport.h>
  23. #include <hidl/HidlTransportSupport.h>
  24. #include <regex>
  25. #include <sstream>
  26. #include <thread>
  27. using android::hardware::IPCThreadState;
  28. using ::android::hardware::interfacesEqual;
  29. namespace android {
  30. namespace hidl {
  31. namespace manager {
  32. namespace implementation {
  33. AccessControl::CallingContext getBinderCallingContext() {
  34. const auto& self = IPCThreadState::self();
  35. pid_t pid = self->getCallingPid();
  36. const char* sid = self->getCallingSid();
  37. if (sid == nullptr) {
  38. if (pid != getpid()) {
  39. android_errorWriteLog(0x534e4554, "121035042");
  40. }
  41. return AccessControl::getCallingContext(pid);
  42. } else {
  43. return { true, sid, pid };
  44. }
  45. }
  46. static constexpr uint64_t kServiceDiedCookie = 0;
  47. static constexpr uint64_t kPackageListenerDiedCookie = 1;
  48. static constexpr uint64_t kServiceListenerDiedCookie = 2;
  49. static constexpr uint64_t kClientCallbackDiedCookie = 3;
  50. size_t ServiceManager::countExistingService() const {
  51. size_t total = 0;
  52. forEachExistingService([&] (const HidlService *) {
  53. ++total;
  54. return true; // continue
  55. });
  56. return total;
  57. }
  58. void ServiceManager::forEachExistingService(std::function<bool(const HidlService *)> f) const {
  59. forEachServiceEntry([&] (const HidlService *service) {
  60. if (service->getService() == nullptr) {
  61. return true; // continue
  62. }
  63. return f(service);
  64. });
  65. }
  66. void ServiceManager::forEachExistingService(std::function<bool(HidlService *)> f) {
  67. forEachServiceEntry([&] (HidlService *service) {
  68. if (service->getService() == nullptr) {
  69. return true; // continue
  70. }
  71. return f(service);
  72. });
  73. }
  74. void ServiceManager::forEachServiceEntry(std::function<bool(const HidlService *)> f) const {
  75. for (const auto& interfaceMapping : mServiceMap) {
  76. const auto& instanceMap = interfaceMapping.second.getInstanceMap();
  77. for (const auto& instanceMapping : instanceMap) {
  78. if (!f(instanceMapping.second.get())) {
  79. return;
  80. }
  81. }
  82. }
  83. }
  84. void ServiceManager::forEachServiceEntry(std::function<bool(HidlService *)> f) {
  85. for (auto& interfaceMapping : mServiceMap) {
  86. auto& instanceMap = interfaceMapping.second.getInstanceMap();
  87. for (auto& instanceMapping : instanceMap) {
  88. if (!f(instanceMapping.second.get())) {
  89. return;
  90. }
  91. }
  92. }
  93. }
  94. HidlService* ServiceManager::lookup(const std::string& fqName, const std::string& name) {
  95. auto ifaceIt = mServiceMap.find(fqName);
  96. if (ifaceIt == mServiceMap.end()) {
  97. return nullptr;
  98. }
  99. PackageInterfaceMap &ifaceMap = ifaceIt->second;
  100. HidlService *hidlService = ifaceMap.lookup(name);
  101. return hidlService;
  102. }
  103. void ServiceManager::serviceDied(uint64_t cookie, const wp<IBase>& who) {
  104. bool serviceRemoved = false;
  105. switch (cookie) {
  106. case kServiceDiedCookie:
  107. serviceRemoved = removeService(who, nullptr /* restrictToInstanceName */);
  108. break;
  109. case kPackageListenerDiedCookie:
  110. serviceRemoved = removePackageListener(who);
  111. break;
  112. case kServiceListenerDiedCookie:
  113. serviceRemoved = removeServiceListener(who);
  114. break;
  115. case kClientCallbackDiedCookie: {
  116. sp<IBase> base = who.promote();
  117. IClientCallback* callback = static_cast<IClientCallback*>(base.get());
  118. serviceRemoved = unregisterClientCallback(nullptr /*service*/,
  119. sp<IClientCallback>(callback));
  120. } break;
  121. }
  122. if (!serviceRemoved) {
  123. LOG(ERROR) << "Received death notification but interface instance not removed. Cookie: "
  124. << cookie << " Service pointer: " << who.promote().get();
  125. }
  126. }
  127. ServiceManager::InstanceMap &ServiceManager::PackageInterfaceMap::getInstanceMap() {
  128. return mInstanceMap;
  129. }
  130. const ServiceManager::InstanceMap &ServiceManager::PackageInterfaceMap::getInstanceMap() const {
  131. return mInstanceMap;
  132. }
  133. const HidlService *ServiceManager::PackageInterfaceMap::lookup(
  134. const std::string &name) const {
  135. auto it = mInstanceMap.find(name);
  136. if (it == mInstanceMap.end()) {
  137. return nullptr;
  138. }
  139. return it->second.get();
  140. }
  141. HidlService *ServiceManager::PackageInterfaceMap::lookup(
  142. const std::string &name) {
  143. return const_cast<HidlService*>(
  144. const_cast<const PackageInterfaceMap*>(this)->lookup(name));
  145. }
  146. void ServiceManager::PackageInterfaceMap::insertService(
  147. std::unique_ptr<HidlService> &&service) {
  148. mInstanceMap.insert({service->getInstanceName(), std::move(service)});
  149. }
  150. void ServiceManager::PackageInterfaceMap::sendPackageRegistrationNotification(
  151. const hidl_string &fqName,
  152. const hidl_string &instanceName) {
  153. for (auto it = mPackageListeners.begin(); it != mPackageListeners.end();) {
  154. auto ret = (*it)->onRegistration(fqName, instanceName, false /* preexisting */);
  155. if (ret.isOk()) {
  156. ++it;
  157. } else {
  158. LOG(ERROR) << "Dropping registration callback for " << fqName << "/" << instanceName
  159. << ": transport error.";
  160. it = mPackageListeners.erase(it);
  161. }
  162. }
  163. }
  164. void ServiceManager::PackageInterfaceMap::addPackageListener(sp<IServiceNotification> listener) {
  165. for (const auto &instanceMapping : mInstanceMap) {
  166. const std::unique_ptr<HidlService> &service = instanceMapping.second;
  167. if (service->getService() == nullptr) {
  168. continue;
  169. }
  170. auto ret = listener->onRegistration(
  171. service->getInterfaceName(),
  172. service->getInstanceName(),
  173. true /* preexisting */);
  174. if (!ret.isOk()) {
  175. LOG(ERROR) << "Not adding package listener for " << service->getInterfaceName()
  176. << "/" << service->getInstanceName() << ": transport error "
  177. << "when sending notification for already registered instance.";
  178. return;
  179. }
  180. }
  181. mPackageListeners.push_back(listener);
  182. }
  183. bool ServiceManager::PackageInterfaceMap::removePackageListener(const wp<IBase>& who) {
  184. bool found = false;
  185. for (auto it = mPackageListeners.begin(); it != mPackageListeners.end();) {
  186. if (interfacesEqual(*it, who.promote())) {
  187. it = mPackageListeners.erase(it);
  188. found = true;
  189. } else {
  190. ++it;
  191. }
  192. }
  193. return found;
  194. }
  195. bool ServiceManager::PackageInterfaceMap::removeServiceListener(const wp<IBase>& who) {
  196. bool found = false;
  197. for (auto &servicePair : getInstanceMap()) {
  198. const std::unique_ptr<HidlService> &service = servicePair.second;
  199. found |= service->removeListener(who);
  200. }
  201. return found;
  202. }
  203. static void tryStartService(const std::string& fqName, const std::string& name) {
  204. using ::android::base::SetProperty;
  205. std::thread([=] {
  206. bool success = SetProperty("ctl.interface_start", fqName + "/" + name);
  207. if (!success) {
  208. LOG(ERROR) << "Failed to set property for starting " << fqName << "/" << name;
  209. }
  210. }).detach();
  211. }
  212. // Methods from ::android::hidl::manager::V1_0::IServiceManager follow.
  213. Return<sp<IBase>> ServiceManager::get(const hidl_string& hidlFqName,
  214. const hidl_string& hidlName) {
  215. const std::string fqName = hidlFqName;
  216. const std::string name = hidlName;
  217. if (!mAcl.canGet(fqName, getBinderCallingContext())) {
  218. return nullptr;
  219. }
  220. HidlService* hidlService = lookup(fqName, name);
  221. if (hidlService == nullptr) {
  222. tryStartService(fqName, name);
  223. return nullptr;
  224. }
  225. sp<IBase> service = hidlService->getService();
  226. if (service == nullptr) {
  227. tryStartService(fqName, name);
  228. return nullptr;
  229. }
  230. // Let HidlService know that we handed out a client. If the client drops the service before the
  231. // next time handleClientCallbacks is called, it will still know that the service had been handed out.
  232. hidlService->guaranteeClient();
  233. // This is executed immediately after the binder driver confirms the transaction. The driver
  234. // will update the appropriate data structures to reflect the fact that the client now has the
  235. // service this function is returning. Nothing else can update the HidlService at the same
  236. // time. This will run before anything else can modify the HidlService which is owned by this
  237. // object, so it will be in the same state that it was when this function returns.
  238. hardware::addPostCommandTask([hidlService] {
  239. hidlService->handleClientCallbacks(false /* isCalledOnInterval */);
  240. });
  241. return service;
  242. }
  243. Return<bool> ServiceManager::add(const hidl_string& name, const sp<IBase>& service) {
  244. bool addSuccess = false;
  245. if (service == nullptr) {
  246. return false;
  247. }
  248. auto pidcon = getBinderCallingContext();
  249. auto ret = service->interfaceChain([&](const auto &interfaceChain) {
  250. addSuccess = addImpl(name, service, interfaceChain, pidcon);
  251. });
  252. if (!ret.isOk()) {
  253. LOG(ERROR) << "Failed to retrieve interface chain: " << ret.description();
  254. return false;
  255. }
  256. return addSuccess;
  257. }
  258. bool ServiceManager::addImpl(const std::string& name,
  259. const sp<IBase>& service,
  260. const hidl_vec<hidl_string>& interfaceChain,
  261. const AccessControl::CallingContext& callingContext) {
  262. if (interfaceChain.size() == 0) {
  263. LOG(WARNING) << "Empty interface chain for " << name;
  264. return false;
  265. }
  266. // First, verify you're allowed to add() the whole interface hierarchy
  267. for(size_t i = 0; i < interfaceChain.size(); i++) {
  268. const std::string fqName = interfaceChain[i];
  269. if (!mAcl.canAdd(fqName, callingContext)) {
  270. return false;
  271. }
  272. }
  273. // Detect duplicate registration
  274. if (interfaceChain.size() > 1) {
  275. // second to last entry should be the highest base class other than IBase.
  276. const std::string baseFqName = interfaceChain[interfaceChain.size() - 2];
  277. const HidlService *hidlService = lookup(baseFqName, name);
  278. if (hidlService != nullptr && hidlService->getService() != nullptr) {
  279. // This shouldn't occur during normal operation. Here are some cases where
  280. // it might get hit:
  281. // - bad configuration (service installed on device multiple times)
  282. // - race between death notification and a new service being registered
  283. // (previous logs should indicate a separate problem)
  284. const std::string childFqName = interfaceChain[0];
  285. pid_t newServicePid = IPCThreadState::self()->getCallingPid();
  286. pid_t oldServicePid = hidlService->getDebugPid();
  287. LOG(WARNING) << "Detected instance of " << childFqName << " (pid: " << newServicePid
  288. << ") registering over instance of or with base of " << baseFqName << " (pid: "
  289. << oldServicePid << ").";
  290. }
  291. }
  292. // Unregister superclass if subclass is registered over it
  293. {
  294. // For IBar extends IFoo if IFoo/default is being registered, remove
  295. // IBar/default. This makes sure the following two things are equivalent
  296. // 1). IBar::castFrom(IFoo::getService(X))
  297. // 2). IBar::getService(X)
  298. // assuming that IBar is declared in the device manifest and there
  299. // is also not an IBaz extends IFoo and there is no race.
  300. const std::string childFqName = interfaceChain[0];
  301. const HidlService *hidlService = lookup(childFqName, name);
  302. if (hidlService != nullptr) {
  303. const sp<IBase> remove = hidlService->getService();
  304. if (remove != nullptr) {
  305. const std::string instanceName = name;
  306. removeService(remove, &instanceName /* restrictToInstanceName */);
  307. }
  308. }
  309. }
  310. for(size_t i = 0; i < interfaceChain.size(); i++) {
  311. const std::string fqName = interfaceChain[i];
  312. PackageInterfaceMap &ifaceMap = mServiceMap[fqName];
  313. HidlService *hidlService = ifaceMap.lookup(name);
  314. if (hidlService == nullptr) {
  315. ifaceMap.insertService(
  316. std::make_unique<HidlService>(fqName, name, service, callingContext.pid));
  317. } else {
  318. hidlService->setService(service, callingContext.pid);
  319. }
  320. ifaceMap.sendPackageRegistrationNotification(fqName, name);
  321. }
  322. bool linkRet = service->linkToDeath(this, kServiceDiedCookie).withDefault(false);
  323. if (!linkRet) {
  324. LOG(ERROR) << "Could not link to death for " << interfaceChain[0] << "/" << name;
  325. }
  326. return true;
  327. }
  328. Return<ServiceManager::Transport> ServiceManager::getTransport(const hidl_string& fqName,
  329. const hidl_string& name) {
  330. using ::android::hardware::getTransport;
  331. if (!mAcl.canGet(fqName, getBinderCallingContext())) {
  332. return Transport::EMPTY;
  333. }
  334. switch (getTransport(fqName, name)) {
  335. case vintf::Transport::HWBINDER:
  336. return Transport::HWBINDER;
  337. case vintf::Transport::PASSTHROUGH:
  338. return Transport::PASSTHROUGH;
  339. case vintf::Transport::EMPTY:
  340. default:
  341. return Transport::EMPTY;
  342. }
  343. }
  344. Return<void> ServiceManager::list(list_cb _hidl_cb) {
  345. if (!mAcl.canList(getBinderCallingContext())) {
  346. _hidl_cb({});
  347. return Void();
  348. }
  349. hidl_vec<hidl_string> list;
  350. list.resize(countExistingService());
  351. size_t idx = 0;
  352. forEachExistingService([&] (const HidlService *service) {
  353. list[idx++] = service->string();
  354. return true; // continue
  355. });
  356. _hidl_cb(list);
  357. return Void();
  358. }
  359. Return<void> ServiceManager::listByInterface(const hidl_string& fqName,
  360. listByInterface_cb _hidl_cb) {
  361. if (!mAcl.canGet(fqName, getBinderCallingContext())) {
  362. _hidl_cb({});
  363. return Void();
  364. }
  365. auto ifaceIt = mServiceMap.find(fqName);
  366. if (ifaceIt == mServiceMap.end()) {
  367. _hidl_cb(hidl_vec<hidl_string>());
  368. return Void();
  369. }
  370. const auto &instanceMap = ifaceIt->second.getInstanceMap();
  371. hidl_vec<hidl_string> list;
  372. size_t total = 0;
  373. for (const auto &serviceMapping : instanceMap) {
  374. const std::unique_ptr<HidlService> &service = serviceMapping.second;
  375. if (service->getService() == nullptr) continue;
  376. ++total;
  377. }
  378. list.resize(total);
  379. size_t idx = 0;
  380. for (const auto &serviceMapping : instanceMap) {
  381. const std::unique_ptr<HidlService> &service = serviceMapping.second;
  382. if (service->getService() == nullptr) continue;
  383. list[idx++] = service->getInstanceName();
  384. }
  385. _hidl_cb(list);
  386. return Void();
  387. }
  388. Return<bool> ServiceManager::registerForNotifications(const hidl_string& fqName,
  389. const hidl_string& name,
  390. const sp<IServiceNotification>& callback) {
  391. if (callback == nullptr) {
  392. return false;
  393. }
  394. if (!mAcl.canGet(fqName, getBinderCallingContext())) {
  395. return false;
  396. }
  397. PackageInterfaceMap &ifaceMap = mServiceMap[fqName];
  398. if (name.empty()) {
  399. bool ret = callback->linkToDeath(this, kPackageListenerDiedCookie).withDefault(false);
  400. if (!ret) {
  401. LOG(ERROR) << "Failed to register death recipient for " << fqName << "/" << name;
  402. return false;
  403. }
  404. ifaceMap.addPackageListener(callback);
  405. return true;
  406. }
  407. HidlService *service = ifaceMap.lookup(name);
  408. bool ret = callback->linkToDeath(this, kServiceListenerDiedCookie).withDefault(false);
  409. if (!ret) {
  410. LOG(ERROR) << "Failed to register death recipient for " << fqName << "/" << name;
  411. return false;
  412. }
  413. if (service == nullptr) {
  414. auto adding = std::make_unique<HidlService>(fqName, name);
  415. adding->addListener(callback);
  416. ifaceMap.insertService(std::move(adding));
  417. } else {
  418. service->addListener(callback);
  419. }
  420. return true;
  421. }
  422. Return<bool> ServiceManager::unregisterForNotifications(const hidl_string& fqName,
  423. const hidl_string& name,
  424. const sp<IServiceNotification>& callback) {
  425. if (callback == nullptr) {
  426. LOG(ERROR) << "Cannot unregister null callback for " << fqName << "/" << name;
  427. return false;
  428. }
  429. // NOTE: don't need ACL since callback is binder token, and if someone has gotten it,
  430. // then they already have access to it.
  431. if (fqName.empty()) {
  432. bool success = false;
  433. success |= removePackageListener(callback);
  434. success |= removeServiceListener(callback);
  435. return success;
  436. }
  437. PackageInterfaceMap &ifaceMap = mServiceMap[fqName];
  438. if (name.empty()) {
  439. bool success = false;
  440. success |= ifaceMap.removePackageListener(callback);
  441. success |= ifaceMap.removeServiceListener(callback);
  442. return success;
  443. }
  444. HidlService *service = ifaceMap.lookup(name);
  445. if (service == nullptr) {
  446. return false;
  447. }
  448. return service->removeListener(callback);
  449. }
  450. Return<bool> ServiceManager::registerClientCallback(const hidl_string& hidlFqName,
  451. const hidl_string& hidlName,
  452. const sp<IBase>& server,
  453. const sp<IClientCallback>& cb) {
  454. if (server == nullptr || cb == nullptr) return false;
  455. const std::string fqName = hidlFqName;
  456. const std::string name = hidlName;
  457. // only the server of the interface can register a client callback
  458. pid_t pid = IPCThreadState::self()->getCallingPid();
  459. if (!mAcl.canAdd(fqName, getBinderCallingContext())) {
  460. return false;
  461. }
  462. HidlService* registered = lookup(fqName, name);
  463. if (registered == nullptr) {
  464. return false;
  465. }
  466. // sanity
  467. if (registered->getDebugPid() != pid) {
  468. LOG(WARNING) << "Only a server can register for client callbacks (for " << fqName
  469. << "/" << name << ")";
  470. return false;
  471. }
  472. sp<IBase> service = registered->getService();
  473. if (!interfacesEqual(service, server)) {
  474. LOG(WARNING) << "Tried to register client callback for " << fqName << "/" << name
  475. << " but a different service is registered under this name.";
  476. return false;
  477. }
  478. bool linkRet = cb->linkToDeath(this, kClientCallbackDiedCookie).withDefault(false);
  479. if (!linkRet) {
  480. LOG(ERROR) << "Could not link to death for registerClientCallback";
  481. return false;
  482. }
  483. registered->addClientCallback(cb);
  484. return true;
  485. }
  486. Return<bool> ServiceManager::unregisterClientCallback(const sp<IBase>& server,
  487. const sp<IClientCallback>& cb) {
  488. if (cb == nullptr) return false;
  489. bool removed = false;
  490. forEachExistingService([&] (HidlService *service) {
  491. if (server == nullptr || interfacesEqual(service->getService(), server)) {
  492. removed |= service->removeClientCallback(cb);
  493. }
  494. return true; // continue
  495. });
  496. return removed;
  497. }
  498. void ServiceManager::handleClientCallbacks() {
  499. forEachServiceEntry([&] (HidlService *service) {
  500. service->handleClientCallbacks(true /* isCalledOnInterval */);
  501. return true; // continue
  502. });
  503. }
  504. Return<bool> ServiceManager::addWithChain(const hidl_string& name,
  505. const sp<IBase>& service,
  506. const hidl_vec<hidl_string>& chain) {
  507. if (service == nullptr) {
  508. return false;
  509. }
  510. auto callingContext = getBinderCallingContext();
  511. return addImpl(name, service, chain, callingContext);
  512. }
  513. Return<void> ServiceManager::listManifestByInterface(const hidl_string& fqName,
  514. listManifestByInterface_cb _hidl_cb) {
  515. if (!mAcl.canGet(fqName, getBinderCallingContext())) {
  516. _hidl_cb({});
  517. return Void();
  518. }
  519. std::set<std::string> instances = getInstances(fqName);
  520. hidl_vec<hidl_string> ret(instances.begin(), instances.end());
  521. _hidl_cb(ret);
  522. return Void();
  523. }
  524. Return<bool> ServiceManager::tryUnregister(const hidl_string& hidlFqName,
  525. const hidl_string& hidlName,
  526. const sp<IBase>& service) {
  527. const std::string fqName = hidlFqName;
  528. const std::string name = hidlName;
  529. if (service == nullptr) {
  530. return false;
  531. }
  532. if (!mAcl.canAdd(fqName, getBinderCallingContext())) {
  533. return false;
  534. }
  535. HidlService* registered = lookup(fqName, name);
  536. // sanity
  537. pid_t pid = IPCThreadState::self()->getCallingPid();
  538. if (registered->getDebugPid() != pid) {
  539. LOG(WARNING) << "Only a server can unregister itself (for " << fqName
  540. << "/" << name << ")";
  541. return false;
  542. }
  543. sp<IBase> server = registered->getService();
  544. if (!interfacesEqual(service, server)) {
  545. LOG(WARNING) << "Tried to unregister for " << fqName << "/" << name
  546. << " but a different service is registered under this name.";
  547. return false;
  548. }
  549. int clients = registered->forceHandleClientCallbacks(false /* isCalledOnInterval */);
  550. // clients < 0: feature not implemented or other error. Assume clients.
  551. // Otherwise:
  552. // - kernel driver will hold onto one refcount (during this transaction)
  553. // - hwservicemanager has a refcount (guaranteed by this transaction)
  554. // So, if clients > 2, then at least one other service on the system must hold a refcount.
  555. if (clients < 0 || clients > 2) {
  556. // client callbacks are either disabled or there are other clients
  557. LOG(INFO) << "Tried to unregister for " << fqName << "/" << name
  558. << " but there are clients: " << clients;
  559. return false;
  560. }
  561. // will remove entire parent hierarchy
  562. bool success = removeService(service, &name /*restrictToInstanceName*/);
  563. if (registered->getService() != nullptr) {
  564. LOG(ERROR) << "Bad state. Unregistration failed for " << fqName << "/" << name << ".";
  565. return false;
  566. }
  567. return success;
  568. }
  569. Return<void> ServiceManager::debugDump(debugDump_cb _cb) {
  570. if (!mAcl.canList(getBinderCallingContext())) {
  571. _cb({});
  572. return Void();
  573. }
  574. std::vector<IServiceManager::InstanceDebugInfo> list;
  575. forEachServiceEntry([&] (const HidlService *service) {
  576. hidl_vec<int32_t> clientPids;
  577. clientPids.resize(service->getPassthroughClients().size());
  578. size_t i = 0;
  579. for (pid_t p : service->getPassthroughClients()) {
  580. clientPids[i++] = p;
  581. }
  582. list.push_back({
  583. .pid = service->getDebugPid(),
  584. .interfaceName = service->getInterfaceName(),
  585. .instanceName = service->getInstanceName(),
  586. .clientPids = clientPids,
  587. .arch = ::android::hidl::base::V1_0::DebugInfo::Architecture::UNKNOWN
  588. });
  589. return true; // continue
  590. });
  591. _cb(list);
  592. return Void();
  593. }
  594. Return<void> ServiceManager::registerPassthroughClient(const hidl_string &fqName,
  595. const hidl_string &name) {
  596. auto callingContext = getBinderCallingContext();
  597. if (!mAcl.canGet(fqName, callingContext)) {
  598. /* We guard this function with "get", because it's typically used in
  599. * the getService() path, albeit for a passthrough service in this
  600. * case
  601. */
  602. return Void();
  603. }
  604. PackageInterfaceMap &ifaceMap = mServiceMap[fqName];
  605. if (name.empty()) {
  606. LOG(WARNING) << "registerPassthroughClient encounters empty instance name for "
  607. << fqName.c_str();
  608. return Void();
  609. }
  610. HidlService *service = ifaceMap.lookup(name);
  611. if (service == nullptr) {
  612. auto adding = std::make_unique<HidlService>(fqName, name);
  613. adding->registerPassthroughClient(callingContext.pid);
  614. ifaceMap.insertService(std::move(adding));
  615. } else {
  616. service->registerPassthroughClient(callingContext.pid);
  617. }
  618. return Void();
  619. }
  620. bool ServiceManager::removeService(const wp<IBase>& who, const std::string* restrictToInstanceName) {
  621. bool keepInstance = false;
  622. bool removed = false;
  623. for (auto &interfaceMapping : mServiceMap) {
  624. auto &instanceMap = interfaceMapping.second.getInstanceMap();
  625. for (auto &servicePair : instanceMap) {
  626. const std::string &instanceName = servicePair.first;
  627. const std::unique_ptr<HidlService> &service = servicePair.second;
  628. if (interfacesEqual(service->getService(), who.promote())) {
  629. if (restrictToInstanceName != nullptr && *restrictToInstanceName != instanceName) {
  630. // We cannot remove all instances of this service, so we don't return that it
  631. // has been entirely removed.
  632. keepInstance = true;
  633. continue;
  634. }
  635. service->setService(nullptr, static_cast<pid_t>(IServiceManager::PidConstant::NO_PID));
  636. removed = true;
  637. }
  638. }
  639. }
  640. return !keepInstance && removed;
  641. }
  642. bool ServiceManager::removePackageListener(const wp<IBase>& who) {
  643. bool found = false;
  644. for (auto &interfaceMapping : mServiceMap) {
  645. found |= interfaceMapping.second.removePackageListener(who);
  646. }
  647. return found;
  648. }
  649. bool ServiceManager::removeServiceListener(const wp<IBase>& who) {
  650. bool found = false;
  651. for (auto &interfaceMapping : mServiceMap) {
  652. auto &packageInterfaceMap = interfaceMapping.second;
  653. found |= packageInterfaceMap.removeServiceListener(who);
  654. }
  655. return found;
  656. }
  657. } // namespace implementation
  658. } // namespace manager
  659. } // namespace hidl
  660. } // namespace android