Steven Moreland | 5fb3d65 | 2016-11-03 13:45:18 -0700 | [diff] [blame] | 1 | #define LOG_TAG "hwservicemanager" |
| 2 | |
Steven Moreland | d83d110 | 2016-10-25 15:01:47 -0700 | [diff] [blame] | 3 | #include "ServiceManager.h" |
Steven Moreland | 37aed80 | 2017-04-06 09:16:42 -0700 | [diff] [blame] | 4 | #include "Vintf.h" |
Steven Moreland | d83d110 | 2016-10-25 15:01:47 -0700 | [diff] [blame] | 5 | |
Steven Moreland | 5fb3d65 | 2016-11-03 13:45:18 -0700 | [diff] [blame] | 6 | #include <android-base/logging.h> |
Steven Moreland | 5a31745 | 2017-10-05 18:49:48 -0700 | [diff] [blame] | 7 | #include <android-base/properties.h> |
Steven Moreland | cdf9472 | 2017-03-21 12:16:31 -0700 | [diff] [blame] | 8 | #include <hwbinder/IPCThreadState.h> |
Steven Moreland | d83d110 | 2016-10-25 15:01:47 -0700 | [diff] [blame] | 9 | #include <hidl/HidlSupport.h> |
Steven Moreland | 6f4fbe1 | 2017-07-21 18:07:42 -0700 | [diff] [blame] | 10 | #include <hidl/HidlTransportSupport.h> |
Steven Moreland | 5fb3d65 | 2016-11-03 13:45:18 -0700 | [diff] [blame] | 11 | #include <regex> |
Steven Moreland | 7623781 | 2016-11-08 15:59:04 -0800 | [diff] [blame] | 12 | #include <sstream> |
Steven Moreland | 58cef2c | 2018-01-24 16:41:38 -0800 | [diff] [blame] | 13 | #include <thread> |
Steven Moreland | d83d110 | 2016-10-25 15:01:47 -0700 | [diff] [blame] | 14 | |
Steven Moreland | cdf9472 | 2017-03-21 12:16:31 -0700 | [diff] [blame] | 15 | using android::hardware::IPCThreadState; |
| 16 | |
Steven Moreland | d83d110 | 2016-10-25 15:01:47 -0700 | [diff] [blame] | 17 | namespace android { |
| 18 | namespace hidl { |
| 19 | namespace manager { |
Steven Moreland | d83d110 | 2016-10-25 15:01:47 -0700 | [diff] [blame] | 20 | namespace implementation { |
| 21 | |
Martijn Coenen | 7fafc14 | 2017-03-06 16:17:51 +0100 | [diff] [blame] | 22 | static constexpr uint64_t kServiceDiedCookie = 0; |
| 23 | static constexpr uint64_t kPackageListenerDiedCookie = 1; |
| 24 | static constexpr uint64_t kServiceListenerDiedCookie = 2; |
| 25 | |
Yifan Hong | 83c49f6 | 2017-01-25 14:16:34 -0800 | [diff] [blame] | 26 | size_t ServiceManager::countExistingService() const { |
| 27 | size_t total = 0; |
| 28 | forEachExistingService([&] (const HidlService *) { |
| 29 | ++total; |
| 30 | }); |
| 31 | return total; |
| 32 | } |
| 33 | |
| 34 | void ServiceManager::forEachExistingService(std::function<void(const HidlService *)> f) const { |
Yifan Hong | ee531a8 | 2017-02-03 15:10:18 -0800 | [diff] [blame] | 35 | forEachServiceEntry([f] (const HidlService *service) { |
| 36 | if (service->getService() == nullptr) { |
| 37 | return; |
| 38 | } |
| 39 | f(service); |
| 40 | }); |
| 41 | } |
| 42 | |
| 43 | void ServiceManager::forEachServiceEntry(std::function<void(const HidlService *)> f) const { |
Yifan Hong | 83c49f6 | 2017-01-25 14:16:34 -0800 | [diff] [blame] | 44 | for (const auto &interfaceMapping : mServiceMap) { |
| 45 | const auto &instanceMap = interfaceMapping.second.getInstanceMap(); |
| 46 | |
| 47 | for (const auto &instanceMapping : instanceMap) { |
Yifan Hong | ee531a8 | 2017-02-03 15:10:18 -0800 | [diff] [blame] | 48 | f(instanceMapping.second.get()); |
Yifan Hong | 83c49f6 | 2017-01-25 14:16:34 -0800 | [diff] [blame] | 49 | } |
| 50 | } |
| 51 | } |
| 52 | |
Martijn Coenen | 7fafc14 | 2017-03-06 16:17:51 +0100 | [diff] [blame] | 53 | void ServiceManager::serviceDied(uint64_t cookie, const wp<IBase>& who) { |
Steven Moreland | 3af80b9 | 2018-05-09 15:31:18 -0700 | [diff] [blame] | 54 | bool serviceRemoved = false; |
Martijn Coenen | 7fafc14 | 2017-03-06 16:17:51 +0100 | [diff] [blame] | 55 | switch (cookie) { |
| 56 | case kServiceDiedCookie: |
Steven Moreland | 3af80b9 | 2018-05-09 15:31:18 -0700 | [diff] [blame] | 57 | serviceRemoved = removeService(who, nullptr /* restrictToInstanceName */); |
Martijn Coenen | 7fafc14 | 2017-03-06 16:17:51 +0100 | [diff] [blame] | 58 | break; |
| 59 | case kPackageListenerDiedCookie: |
Steven Moreland | 3af80b9 | 2018-05-09 15:31:18 -0700 | [diff] [blame] | 60 | serviceRemoved = removePackageListener(who); |
Martijn Coenen | 7fafc14 | 2017-03-06 16:17:51 +0100 | [diff] [blame] | 61 | break; |
| 62 | case kServiceListenerDiedCookie: |
Steven Moreland | 3af80b9 | 2018-05-09 15:31:18 -0700 | [diff] [blame] | 63 | serviceRemoved = removeServiceListener(who); |
Martijn Coenen | 7fafc14 | 2017-03-06 16:17:51 +0100 | [diff] [blame] | 64 | break; |
| 65 | } |
Steven Moreland | 3af80b9 | 2018-05-09 15:31:18 -0700 | [diff] [blame] | 66 | |
| 67 | if (!serviceRemoved) { |
| 68 | LOG(ERROR) << "Received death notification but serivce not removed. Cookie: " << cookie |
| 69 | << " Service pointer: " << who.promote().get(); |
| 70 | } |
Martijn Coenen | 46847a6 | 2016-12-19 05:36:12 +0100 | [diff] [blame] | 71 | } |
| 72 | |
Steven Moreland | 2173d3c | 2016-11-09 15:00:58 -0800 | [diff] [blame] | 73 | ServiceManager::InstanceMap &ServiceManager::PackageInterfaceMap::getInstanceMap() { |
| 74 | return mInstanceMap; |
Steven Moreland | 5fb3d65 | 2016-11-03 13:45:18 -0700 | [diff] [blame] | 75 | } |
| 76 | |
Steven Moreland | 2173d3c | 2016-11-09 15:00:58 -0800 | [diff] [blame] | 77 | const ServiceManager::InstanceMap &ServiceManager::PackageInterfaceMap::getInstanceMap() const { |
| 78 | return mInstanceMap; |
Steven Moreland | 5fb3d65 | 2016-11-03 13:45:18 -0700 | [diff] [blame] | 79 | } |
| 80 | |
Steven Moreland | d544cf6 | 2017-01-04 15:24:32 -0800 | [diff] [blame] | 81 | const HidlService *ServiceManager::PackageInterfaceMap::lookup( |
| 82 | const std::string &name) const { |
| 83 | auto it = mInstanceMap.find(name); |
Steven Moreland | 7623781 | 2016-11-08 15:59:04 -0800 | [diff] [blame] | 84 | |
Steven Moreland | d544cf6 | 2017-01-04 15:24:32 -0800 | [diff] [blame] | 85 | if (it == mInstanceMap.end()) { |
| 86 | return nullptr; |
Steven Moreland | 5fb3d65 | 2016-11-03 13:45:18 -0700 | [diff] [blame] | 87 | } |
| 88 | |
Steven Moreland | d544cf6 | 2017-01-04 15:24:32 -0800 | [diff] [blame] | 89 | return it->second.get(); |
Steven Moreland | 2173d3c | 2016-11-09 15:00:58 -0800 | [diff] [blame] | 90 | } |
Steven Moreland | 5fb3d65 | 2016-11-03 13:45:18 -0700 | [diff] [blame] | 91 | |
Steven Moreland | d544cf6 | 2017-01-04 15:24:32 -0800 | [diff] [blame] | 92 | HidlService *ServiceManager::PackageInterfaceMap::lookup( |
| 93 | const std::string &name) { |
Steven Moreland | 5fb3d65 | 2016-11-03 13:45:18 -0700 | [diff] [blame] | 94 | |
Steven Moreland | 2173d3c | 2016-11-09 15:00:58 -0800 | [diff] [blame] | 95 | return const_cast<HidlService*>( |
Steven Moreland | d544cf6 | 2017-01-04 15:24:32 -0800 | [diff] [blame] | 96 | const_cast<const PackageInterfaceMap*>(this)->lookup(name)); |
Steven Moreland | 2173d3c | 2016-11-09 15:00:58 -0800 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | void ServiceManager::PackageInterfaceMap::insertService( |
| 100 | std::unique_ptr<HidlService> &&service) { |
Steven Moreland | d544cf6 | 2017-01-04 15:24:32 -0800 | [diff] [blame] | 101 | mInstanceMap.insert({service->getInstanceName(), std::move(service)}); |
Steven Moreland | 2173d3c | 2016-11-09 15:00:58 -0800 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | void ServiceManager::PackageInterfaceMap::sendPackageRegistrationNotification( |
| 105 | const hidl_string &fqName, |
Martijn Coenen | 7fafc14 | 2017-03-06 16:17:51 +0100 | [diff] [blame] | 106 | const hidl_string &instanceName) { |
Steven Moreland | 2173d3c | 2016-11-09 15:00:58 -0800 | [diff] [blame] | 107 | |
Martijn Coenen | 7fafc14 | 2017-03-06 16:17:51 +0100 | [diff] [blame] | 108 | for (auto it = mPackageListeners.begin(); it != mPackageListeners.end();) { |
| 109 | auto ret = (*it)->onRegistration(fqName, instanceName, false /* preexisting */); |
| 110 | if (ret.isOk()) { |
| 111 | ++it; |
| 112 | } else { |
| 113 | LOG(ERROR) << "Dropping registration callback for " << fqName << "/" << instanceName |
| 114 | << ": transport error."; |
| 115 | it = mPackageListeners.erase(it); |
| 116 | } |
Steven Moreland | 2173d3c | 2016-11-09 15:00:58 -0800 | [diff] [blame] | 117 | } |
| 118 | } |
Steven Moreland | 2173d3c | 2016-11-09 15:00:58 -0800 | [diff] [blame] | 119 | |
Martijn Coenen | 7fafc14 | 2017-03-06 16:17:51 +0100 | [diff] [blame] | 120 | void ServiceManager::PackageInterfaceMap::addPackageListener(sp<IServiceNotification> listener) { |
Steven Moreland | 2173d3c | 2016-11-09 15:00:58 -0800 | [diff] [blame] | 121 | for (const auto &instanceMapping : mInstanceMap) { |
| 122 | const std::unique_ptr<HidlService> &service = instanceMapping.second; |
| 123 | |
| 124 | if (service->getService() == nullptr) { |
| 125 | continue; |
| 126 | } |
| 127 | |
Steven Moreland | 30acc22 | 2017-01-26 11:44:38 -0800 | [diff] [blame] | 128 | auto ret = listener->onRegistration( |
Steven Moreland | d544cf6 | 2017-01-04 15:24:32 -0800 | [diff] [blame] | 129 | service->getInterfaceName(), |
| 130 | service->getInstanceName(), |
| 131 | true /* preexisting */); |
Martijn Coenen | 7fafc14 | 2017-03-06 16:17:51 +0100 | [diff] [blame] | 132 | if (!ret.isOk()) { |
| 133 | LOG(ERROR) << "Not adding package listener for " << service->getInterfaceName() |
| 134 | << "/" << service->getInstanceName() << ": transport error " |
| 135 | << "when sending notification for already registered instance."; |
| 136 | return; |
| 137 | } |
Steven Moreland | 2173d3c | 2016-11-09 15:00:58 -0800 | [diff] [blame] | 138 | } |
Martijn Coenen | 7fafc14 | 2017-03-06 16:17:51 +0100 | [diff] [blame] | 139 | mPackageListeners.push_back(listener); |
| 140 | } |
| 141 | |
| 142 | bool ServiceManager::PackageInterfaceMap::removePackageListener(const wp<IBase>& who) { |
Steven Moreland | 6f4fbe1 | 2017-07-21 18:07:42 -0700 | [diff] [blame] | 143 | using ::android::hardware::interfacesEqual; |
| 144 | |
Martijn Coenen | 7fafc14 | 2017-03-06 16:17:51 +0100 | [diff] [blame] | 145 | bool found = false; |
| 146 | |
| 147 | for (auto it = mPackageListeners.begin(); it != mPackageListeners.end();) { |
Steven Moreland | 6f4fbe1 | 2017-07-21 18:07:42 -0700 | [diff] [blame] | 148 | if (interfacesEqual(*it, who.promote())) { |
Martijn Coenen | 7fafc14 | 2017-03-06 16:17:51 +0100 | [diff] [blame] | 149 | it = mPackageListeners.erase(it); |
| 150 | found = true; |
| 151 | } else { |
| 152 | ++it; |
| 153 | } |
| 154 | } |
| 155 | |
| 156 | return found; |
Steven Moreland | 5fb3d65 | 2016-11-03 13:45:18 -0700 | [diff] [blame] | 157 | } |
| 158 | |
Steven Moreland | 6f4fbe1 | 2017-07-21 18:07:42 -0700 | [diff] [blame] | 159 | bool ServiceManager::PackageInterfaceMap::removeServiceListener(const wp<IBase>& who) { |
| 160 | using ::android::hardware::interfacesEqual; |
| 161 | |
| 162 | bool found = false; |
| 163 | |
| 164 | for (auto &servicePair : getInstanceMap()) { |
| 165 | const std::unique_ptr<HidlService> &service = servicePair.second; |
| 166 | found |= service->removeListener(who); |
| 167 | } |
| 168 | |
| 169 | return found; |
| 170 | } |
| 171 | |
Steven Moreland | 5a31745 | 2017-10-05 18:49:48 -0700 | [diff] [blame] | 172 | static void tryStartService(const std::string& fqName, const std::string& name) { |
| 173 | using ::android::base::SetProperty; |
| 174 | |
Steven Moreland | 58cef2c | 2018-01-24 16:41:38 -0800 | [diff] [blame] | 175 | std::thread([=] { |
| 176 | bool success = SetProperty("ctl.interface_start", fqName + "/" + name); |
Steven Moreland | 5a31745 | 2017-10-05 18:49:48 -0700 | [diff] [blame] | 177 | |
Steven Moreland | 58cef2c | 2018-01-24 16:41:38 -0800 | [diff] [blame] | 178 | if (!success) { |
| 179 | LOG(ERROR) << "Failed to set property for starting " << fqName << "/" << name; |
| 180 | } |
| 181 | }).detach(); |
Steven Moreland | 5a31745 | 2017-10-05 18:49:48 -0700 | [diff] [blame] | 182 | } |
| 183 | |
Steven Moreland | d83d110 | 2016-10-25 15:01:47 -0700 | [diff] [blame] | 184 | // Methods from ::android::hidl::manager::V1_0::IServiceManager follow. |
Steven Moreland | 5a31745 | 2017-10-05 18:49:48 -0700 | [diff] [blame] | 185 | Return<sp<IBase>> ServiceManager::get(const hidl_string& hidlFqName, |
| 186 | const hidl_string& hidlName) { |
| 187 | const std::string fqName = hidlFqName; |
| 188 | const std::string name = hidlName; |
| 189 | |
Martijn Coenen | 7ce83be | 2017-04-07 16:19:32 -0700 | [diff] [blame] | 190 | pid_t pid = IPCThreadState::self()->getCallingPid(); |
| 191 | if (!mAcl.canGet(fqName, pid)) { |
| 192 | return nullptr; |
| 193 | } |
| 194 | |
Steven Moreland | d544cf6 | 2017-01-04 15:24:32 -0800 | [diff] [blame] | 195 | auto ifaceIt = mServiceMap.find(fqName); |
Steven Moreland | 5fb3d65 | 2016-11-03 13:45:18 -0700 | [diff] [blame] | 196 | if (ifaceIt == mServiceMap.end()) { |
Steven Moreland | 5a31745 | 2017-10-05 18:49:48 -0700 | [diff] [blame] | 197 | tryStartService(fqName, hidlName); |
Martijn Coenen | 7b02bf9 | 2017-01-02 15:17:58 +0100 | [diff] [blame] | 198 | return nullptr; |
Steven Moreland | 5fb3d65 | 2016-11-03 13:45:18 -0700 | [diff] [blame] | 199 | } |
| 200 | |
Steven Moreland | 2173d3c | 2016-11-09 15:00:58 -0800 | [diff] [blame] | 201 | const PackageInterfaceMap &ifaceMap = ifaceIt->second; |
Steven Moreland | d544cf6 | 2017-01-04 15:24:32 -0800 | [diff] [blame] | 202 | const HidlService *hidlService = ifaceMap.lookup(name); |
Steven Moreland | 5fb3d65 | 2016-11-03 13:45:18 -0700 | [diff] [blame] | 203 | |
Steven Moreland | d544cf6 | 2017-01-04 15:24:32 -0800 | [diff] [blame] | 204 | if (hidlService == nullptr) { |
Steven Moreland | 5a31745 | 2017-10-05 18:49:48 -0700 | [diff] [blame] | 205 | tryStartService(fqName, hidlName); |
Martijn Coenen | 7b02bf9 | 2017-01-02 15:17:58 +0100 | [diff] [blame] | 206 | return nullptr; |
Steven Moreland | d83d110 | 2016-10-25 15:01:47 -0700 | [diff] [blame] | 207 | } |
Steven Moreland | d544cf6 | 2017-01-04 15:24:32 -0800 | [diff] [blame] | 208 | |
Steven Moreland | 5a31745 | 2017-10-05 18:49:48 -0700 | [diff] [blame] | 209 | sp<IBase> service = hidlService->getService(); |
| 210 | if (service == nullptr) { |
| 211 | tryStartService(fqName, hidlName); |
| 212 | return nullptr; |
| 213 | } |
| 214 | |
| 215 | return service; |
Steven Moreland | d83d110 | 2016-10-25 15:01:47 -0700 | [diff] [blame] | 216 | } |
| 217 | |
Martijn Coenen | 1a5da1c | 2017-03-06 13:05:39 +0100 | [diff] [blame] | 218 | Return<bool> ServiceManager::add(const hidl_string& name, const sp<IBase>& service) { |
| 219 | bool isValidService = false; |
Steven Moreland | d83d110 | 2016-10-25 15:01:47 -0700 | [diff] [blame] | 220 | |
Martijn Coenen | 1a5da1c | 2017-03-06 13:05:39 +0100 | [diff] [blame] | 221 | if (service == nullptr) { |
Steven Moreland | 5fb3d65 | 2016-11-03 13:45:18 -0700 | [diff] [blame] | 222 | return false; |
Steven Moreland | d83d110 | 2016-10-25 15:01:47 -0700 | [diff] [blame] | 223 | } |
Steven Moreland | 5fb3d65 | 2016-11-03 13:45:18 -0700 | [diff] [blame] | 224 | |
Steven Moreland | cdf9472 | 2017-03-21 12:16:31 -0700 | [diff] [blame] | 225 | // TODO(b/34235311): use HIDL way to determine this |
| 226 | // also, this assumes that the PID that is registering is the pid that is the service |
| 227 | pid_t pid = IPCThreadState::self()->getCallingPid(); |
Steven Moreland | 4034c1c | 2017-11-03 17:42:27 -0700 | [diff] [blame] | 228 | auto context = mAcl.getContext(pid); |
Steven Moreland | cdf9472 | 2017-03-21 12:16:31 -0700 | [diff] [blame] | 229 | |
Martijn Coenen | 1a5da1c | 2017-03-06 13:05:39 +0100 | [diff] [blame] | 230 | auto ret = service->interfaceChain([&](const auto &interfaceChain) { |
| 231 | if (interfaceChain.size() == 0) { |
| 232 | return; |
Steven Moreland | 5fb3d65 | 2016-11-03 13:45:18 -0700 | [diff] [blame] | 233 | } |
Steven Moreland | 978473f | 2016-11-28 14:51:07 -0800 | [diff] [blame] | 234 | |
Martijn Coenen | 7ce83be | 2017-04-07 16:19:32 -0700 | [diff] [blame] | 235 | // First, verify you're allowed to add() the whole interface hierarchy |
| 236 | for(size_t i = 0; i < interfaceChain.size(); i++) { |
Steven Moreland | 76936fe | 2017-09-19 12:18:30 -0700 | [diff] [blame] | 237 | const std::string fqName = interfaceChain[i]; |
Martijn Coenen | 7ce83be | 2017-04-07 16:19:32 -0700 | [diff] [blame] | 238 | |
Steven Moreland | 4034c1c | 2017-11-03 17:42:27 -0700 | [diff] [blame] | 239 | if (!mAcl.canAdd(fqName, context, pid)) { |
Martijn Coenen | 7ce83be | 2017-04-07 16:19:32 -0700 | [diff] [blame] | 240 | return; |
| 241 | } |
| 242 | } |
| 243 | |
Steven Moreland | 76936fe | 2017-09-19 12:18:30 -0700 | [diff] [blame] | 244 | { |
| 245 | // For IBar extends IFoo if IFoo/default is being registered, remove |
| 246 | // IBar/default. This makes sure the following two things are equivalent |
| 247 | // 1). IBar::castFrom(IFoo::getService(X)) |
| 248 | // 2). IBar::getService(X) |
| 249 | // assuming that IBar is declared in the device manifest and there |
| 250 | // is also not an IBaz extends IFoo. |
| 251 | const std::string childFqName = interfaceChain[0]; |
| 252 | const PackageInterfaceMap &ifaceMap = mServiceMap[childFqName]; |
| 253 | const HidlService *hidlService = ifaceMap.lookup(name); |
| 254 | if (hidlService != nullptr) { |
| 255 | const sp<IBase> remove = hidlService->getService(); |
| 256 | |
| 257 | if (remove != nullptr) { |
| 258 | const std::string instanceName = name; |
Steven Moreland | 1ac63d9 | 2017-10-06 12:48:14 -0700 | [diff] [blame] | 259 | removeService(remove, &instanceName /* restrictToInstanceName */); |
Steven Moreland | 76936fe | 2017-09-19 12:18:30 -0700 | [diff] [blame] | 260 | } |
| 261 | } |
| 262 | } |
| 263 | |
Martijn Coenen | 1a5da1c | 2017-03-06 13:05:39 +0100 | [diff] [blame] | 264 | for(size_t i = 0; i < interfaceChain.size(); i++) { |
Steven Moreland | 76936fe | 2017-09-19 12:18:30 -0700 | [diff] [blame] | 265 | const std::string fqName = interfaceChain[i]; |
Martijn Coenen | 1a5da1c | 2017-03-06 13:05:39 +0100 | [diff] [blame] | 266 | |
| 267 | PackageInterfaceMap &ifaceMap = mServiceMap[fqName]; |
| 268 | HidlService *hidlService = ifaceMap.lookup(name); |
| 269 | |
| 270 | if (hidlService == nullptr) { |
| 271 | ifaceMap.insertService( |
Steven Moreland | cdf9472 | 2017-03-21 12:16:31 -0700 | [diff] [blame] | 272 | std::make_unique<HidlService>(fqName, name, service, pid)); |
Martijn Coenen | 1a5da1c | 2017-03-06 13:05:39 +0100 | [diff] [blame] | 273 | } else { |
Steven Moreland | cdf9472 | 2017-03-21 12:16:31 -0700 | [diff] [blame] | 274 | hidlService->setService(service, pid); |
Martijn Coenen | 1a5da1c | 2017-03-06 13:05:39 +0100 | [diff] [blame] | 275 | } |
| 276 | |
| 277 | ifaceMap.sendPackageRegistrationNotification(fqName, name); |
| 278 | } |
| 279 | |
Steven Moreland | 3505c38 | 2017-10-06 09:58:57 -0700 | [diff] [blame] | 280 | bool linkRet = service->linkToDeath(this, kServiceDiedCookie).withDefault(false); |
| 281 | if (!linkRet) { |
| 282 | LOG(ERROR) << "Could not link to death for " << interfaceChain[0] << "/" << name; |
| 283 | } |
Martijn Coenen | 1a5da1c | 2017-03-06 13:05:39 +0100 | [diff] [blame] | 284 | |
| 285 | isValidService = true; |
| 286 | }); |
| 287 | |
| 288 | if (!ret.isOk()) { |
| 289 | LOG(ERROR) << "Failed to retrieve interface chain."; |
| 290 | return false; |
Steven Moreland | d83d110 | 2016-10-25 15:01:47 -0700 | [diff] [blame] | 291 | } |
Steven Moreland | d544cf6 | 2017-01-04 15:24:32 -0800 | [diff] [blame] | 292 | |
Martijn Coenen | 1a5da1c | 2017-03-06 13:05:39 +0100 | [diff] [blame] | 293 | return isValidService; |
Steven Moreland | d83d110 | 2016-10-25 15:01:47 -0700 | [diff] [blame] | 294 | } |
| 295 | |
Steven Moreland | 37aed80 | 2017-04-06 09:16:42 -0700 | [diff] [blame] | 296 | Return<ServiceManager::Transport> ServiceManager::getTransport(const hidl_string& fqName, |
| 297 | const hidl_string& name) { |
| 298 | using ::android::hardware::getTransport; |
| 299 | |
Martijn Coenen | 7ce83be | 2017-04-07 16:19:32 -0700 | [diff] [blame] | 300 | pid_t pid = IPCThreadState::self()->getCallingPid(); |
| 301 | if (!mAcl.canGet(fqName, pid)) { |
| 302 | return Transport::EMPTY; |
| 303 | } |
| 304 | |
Steven Moreland | 37aed80 | 2017-04-06 09:16:42 -0700 | [diff] [blame] | 305 | switch (getTransport(fqName, name)) { |
| 306 | case vintf::Transport::HWBINDER: |
| 307 | return Transport::HWBINDER; |
| 308 | case vintf::Transport::PASSTHROUGH: |
| 309 | return Transport::PASSTHROUGH; |
| 310 | case vintf::Transport::EMPTY: |
| 311 | default: |
| 312 | return Transport::EMPTY; |
| 313 | } |
| 314 | } |
| 315 | |
Steven Moreland | 7623781 | 2016-11-08 15:59:04 -0800 | [diff] [blame] | 316 | Return<void> ServiceManager::list(list_cb _hidl_cb) { |
Martijn Coenen | 7ce83be | 2017-04-07 16:19:32 -0700 | [diff] [blame] | 317 | pid_t pid = IPCThreadState::self()->getCallingPid(); |
| 318 | if (!mAcl.canList(pid)) { |
| 319 | _hidl_cb({}); |
| 320 | return Void(); |
| 321 | } |
Steven Moreland | 7623781 | 2016-11-08 15:59:04 -0800 | [diff] [blame] | 322 | |
| 323 | hidl_vec<hidl_string> list; |
Martijn Coenen | 7ce83be | 2017-04-07 16:19:32 -0700 | [diff] [blame] | 324 | |
Yifan Hong | 83c49f6 | 2017-01-25 14:16:34 -0800 | [diff] [blame] | 325 | list.resize(countExistingService()); |
Steven Moreland | 7623781 | 2016-11-08 15:59:04 -0800 | [diff] [blame] | 326 | |
| 327 | size_t idx = 0; |
Yifan Hong | 83c49f6 | 2017-01-25 14:16:34 -0800 | [diff] [blame] | 328 | forEachExistingService([&] (const HidlService *service) { |
| 329 | list[idx++] = service->string(); |
| 330 | }); |
Steven Moreland | 7623781 | 2016-11-08 15:59:04 -0800 | [diff] [blame] | 331 | |
| 332 | _hidl_cb(list); |
| 333 | return Void(); |
| 334 | } |
| 335 | |
Steven Moreland | 2173d3c | 2016-11-09 15:00:58 -0800 | [diff] [blame] | 336 | Return<void> ServiceManager::listByInterface(const hidl_string& fqName, |
| 337 | listByInterface_cb _hidl_cb) { |
Martijn Coenen | 7ce83be | 2017-04-07 16:19:32 -0700 | [diff] [blame] | 338 | pid_t pid = IPCThreadState::self()->getCallingPid(); |
| 339 | if (!mAcl.canGet(fqName, pid)) { |
| 340 | _hidl_cb({}); |
| 341 | return Void(); |
| 342 | } |
| 343 | |
Steven Moreland | d544cf6 | 2017-01-04 15:24:32 -0800 | [diff] [blame] | 344 | auto ifaceIt = mServiceMap.find(fqName); |
Steven Moreland | 7623781 | 2016-11-08 15:59:04 -0800 | [diff] [blame] | 345 | if (ifaceIt == mServiceMap.end()) { |
| 346 | _hidl_cb(hidl_vec<hidl_string>()); |
| 347 | return Void(); |
| 348 | } |
| 349 | |
Steven Moreland | 2173d3c | 2016-11-09 15:00:58 -0800 | [diff] [blame] | 350 | const auto &instanceMap = ifaceIt->second.getInstanceMap(); |
Steven Moreland | 7623781 | 2016-11-08 15:59:04 -0800 | [diff] [blame] | 351 | |
| 352 | hidl_vec<hidl_string> list; |
Steven Moreland | fc56b61 | 2017-01-23 20:10:45 -0800 | [diff] [blame] | 353 | |
| 354 | size_t total = 0; |
| 355 | for (const auto &serviceMapping : instanceMap) { |
| 356 | const std::unique_ptr<HidlService> &service = serviceMapping.second; |
| 357 | if (service->getService() == nullptr) continue; |
| 358 | |
| 359 | ++total; |
| 360 | } |
| 361 | list.resize(total); |
Steven Moreland | 7623781 | 2016-11-08 15:59:04 -0800 | [diff] [blame] | 362 | |
| 363 | size_t idx = 0; |
Steven Moreland | 2173d3c | 2016-11-09 15:00:58 -0800 | [diff] [blame] | 364 | for (const auto &serviceMapping : instanceMap) { |
Steven Moreland | 7623781 | 2016-11-08 15:59:04 -0800 | [diff] [blame] | 365 | const std::unique_ptr<HidlService> &service = serviceMapping.second; |
Steven Moreland | fc56b61 | 2017-01-23 20:10:45 -0800 | [diff] [blame] | 366 | if (service->getService() == nullptr) continue; |
Steven Moreland | 7623781 | 2016-11-08 15:59:04 -0800 | [diff] [blame] | 367 | |
Steven Moreland | d544cf6 | 2017-01-04 15:24:32 -0800 | [diff] [blame] | 368 | list[idx++] = service->getInstanceName(); |
Steven Moreland | 7623781 | 2016-11-08 15:59:04 -0800 | [diff] [blame] | 369 | } |
| 370 | |
| 371 | _hidl_cb(list); |
| 372 | return Void(); |
| 373 | } |
| 374 | |
Steven Moreland | 2173d3c | 2016-11-09 15:00:58 -0800 | [diff] [blame] | 375 | Return<bool> ServiceManager::registerForNotifications(const hidl_string& fqName, |
| 376 | const hidl_string& name, |
| 377 | const sp<IServiceNotification>& callback) { |
| 378 | if (callback == nullptr) { |
| 379 | return false; |
| 380 | } |
| 381 | |
Martijn Coenen | 7ce83be | 2017-04-07 16:19:32 -0700 | [diff] [blame] | 382 | pid_t pid = IPCThreadState::self()->getCallingPid(); |
| 383 | if (!mAcl.canGet(fqName, pid)) { |
| 384 | return false; |
| 385 | } |
| 386 | |
Steven Moreland | d544cf6 | 2017-01-04 15:24:32 -0800 | [diff] [blame] | 387 | PackageInterfaceMap &ifaceMap = mServiceMap[fqName]; |
Steven Moreland | 2173d3c | 2016-11-09 15:00:58 -0800 | [diff] [blame] | 388 | |
| 389 | if (name.empty()) { |
Steven Moreland | 78c899e | 2018-09-27 16:45:18 -0700 | [diff] [blame^] | 390 | bool ret = callback->linkToDeath(this, kPackageListenerDiedCookie).withDefault(false); |
| 391 | if (!ret) { |
Martijn Coenen | 7fafc14 | 2017-03-06 16:17:51 +0100 | [diff] [blame] | 392 | LOG(ERROR) << "Failed to register death recipient for " << fqName << "/" << name; |
| 393 | return false; |
| 394 | } |
Steven Moreland | 2173d3c | 2016-11-09 15:00:58 -0800 | [diff] [blame] | 395 | ifaceMap.addPackageListener(callback); |
| 396 | return true; |
| 397 | } |
| 398 | |
Steven Moreland | d544cf6 | 2017-01-04 15:24:32 -0800 | [diff] [blame] | 399 | HidlService *service = ifaceMap.lookup(name); |
Steven Moreland | 2173d3c | 2016-11-09 15:00:58 -0800 | [diff] [blame] | 400 | |
Steven Moreland | 78c899e | 2018-09-27 16:45:18 -0700 | [diff] [blame^] | 401 | bool ret = callback->linkToDeath(this, kServiceListenerDiedCookie).withDefault(false); |
| 402 | if (!ret) { |
Martijn Coenen | 7fafc14 | 2017-03-06 16:17:51 +0100 | [diff] [blame] | 403 | LOG(ERROR) << "Failed to register death recipient for " << fqName << "/" << name; |
| 404 | return false; |
| 405 | } |
| 406 | |
Steven Moreland | 2173d3c | 2016-11-09 15:00:58 -0800 | [diff] [blame] | 407 | if (service == nullptr) { |
Steven Moreland | cdf9472 | 2017-03-21 12:16:31 -0700 | [diff] [blame] | 408 | auto adding = std::make_unique<HidlService>(fqName, name); |
Steven Moreland | d544cf6 | 2017-01-04 15:24:32 -0800 | [diff] [blame] | 409 | adding->addListener(callback); |
| 410 | ifaceMap.insertService(std::move(adding)); |
Steven Moreland | 2173d3c | 2016-11-09 15:00:58 -0800 | [diff] [blame] | 411 | } else { |
| 412 | service->addListener(callback); |
| 413 | } |
| 414 | |
| 415 | return true; |
| 416 | } |
| 417 | |
Steven Moreland | 6f4fbe1 | 2017-07-21 18:07:42 -0700 | [diff] [blame] | 418 | Return<bool> ServiceManager::unregisterForNotifications(const hidl_string& fqName, |
| 419 | const hidl_string& name, |
| 420 | const sp<IServiceNotification>& callback) { |
| 421 | if (callback == nullptr) { |
| 422 | LOG(ERROR) << "Cannot unregister null callback for " << fqName << "/" << name; |
| 423 | return false; |
| 424 | } |
| 425 | |
| 426 | // NOTE: don't need ACL since callback is binder token, and if someone has gotten it, |
| 427 | // then they already have access to it. |
| 428 | |
| 429 | if (fqName.empty()) { |
| 430 | bool success = false; |
| 431 | success |= removePackageListener(callback); |
| 432 | success |= removeServiceListener(callback); |
| 433 | return success; |
| 434 | } |
| 435 | |
| 436 | PackageInterfaceMap &ifaceMap = mServiceMap[fqName]; |
| 437 | |
| 438 | if (name.empty()) { |
| 439 | bool success = false; |
| 440 | success |= ifaceMap.removePackageListener(callback); |
| 441 | success |= ifaceMap.removeServiceListener(callback); |
| 442 | return success; |
| 443 | } |
| 444 | |
| 445 | HidlService *service = ifaceMap.lookup(name); |
| 446 | |
| 447 | if (service == nullptr) { |
| 448 | return false; |
| 449 | } |
| 450 | |
| 451 | return service->removeListener(callback); |
| 452 | } |
| 453 | |
Yifan Hong | 83c49f6 | 2017-01-25 14:16:34 -0800 | [diff] [blame] | 454 | Return<void> ServiceManager::debugDump(debugDump_cb _cb) { |
Martijn Coenen | 7ce83be | 2017-04-07 16:19:32 -0700 | [diff] [blame] | 455 | pid_t pid = IPCThreadState::self()->getCallingPid(); |
| 456 | if (!mAcl.canList(pid)) { |
| 457 | _cb({}); |
| 458 | return Void(); |
| 459 | } |
| 460 | |
Yifan Hong | ee531a8 | 2017-02-03 15:10:18 -0800 | [diff] [blame] | 461 | std::vector<IServiceManager::InstanceDebugInfo> list; |
| 462 | forEachServiceEntry([&] (const HidlService *service) { |
Yifan Hong | ee531a8 | 2017-02-03 15:10:18 -0800 | [diff] [blame] | 463 | hidl_vec<int32_t> clientPids; |
| 464 | clientPids.resize(service->getPassthroughClients().size()); |
| 465 | |
| 466 | size_t i = 0; |
| 467 | for (pid_t p : service->getPassthroughClients()) { |
| 468 | clientPids[i++] = p; |
| 469 | } |
| 470 | |
| 471 | list.push_back({ |
Steven Moreland | cdf9472 | 2017-03-21 12:16:31 -0700 | [diff] [blame] | 472 | .pid = service->getPid(), |
Yifan Hong | 83c49f6 | 2017-01-25 14:16:34 -0800 | [diff] [blame] | 473 | .interfaceName = service->getInterfaceName(), |
| 474 | .instanceName = service->getInstanceName(), |
Yifan Hong | 1d2deb1 | 2017-03-02 16:58:22 -0800 | [diff] [blame] | 475 | .clientPids = clientPids, |
| 476 | .arch = ::android::hidl::base::V1_0::DebugInfo::Architecture::UNKNOWN |
Yifan Hong | ee531a8 | 2017-02-03 15:10:18 -0800 | [diff] [blame] | 477 | }); |
Yifan Hong | 83c49f6 | 2017-01-25 14:16:34 -0800 | [diff] [blame] | 478 | }); |
| 479 | |
| 480 | _cb(list); |
| 481 | return Void(); |
| 482 | } |
| 483 | |
Yifan Hong | ee531a8 | 2017-02-03 15:10:18 -0800 | [diff] [blame] | 484 | |
| 485 | Return<void> ServiceManager::registerPassthroughClient(const hidl_string &fqName, |
Martijn Coenen | 737de1b | 2017-04-27 09:29:43 -0700 | [diff] [blame] | 486 | const hidl_string &name) { |
| 487 | pid_t pid = IPCThreadState::self()->getCallingPid(); |
| 488 | if (!mAcl.canGet(fqName, pid)) { |
Martijn Coenen | 7ce83be | 2017-04-07 16:19:32 -0700 | [diff] [blame] | 489 | /* We guard this function with "get", because it's typically used in |
| 490 | * the getService() path, albeit for a passthrough service in this |
| 491 | * case |
| 492 | */ |
| 493 | return Void(); |
| 494 | } |
Yifan Hong | ee531a8 | 2017-02-03 15:10:18 -0800 | [diff] [blame] | 495 | |
| 496 | PackageInterfaceMap &ifaceMap = mServiceMap[fqName]; |
| 497 | |
| 498 | if (name.empty()) { |
| 499 | LOG(WARNING) << "registerPassthroughClient encounters empty instance name for " |
| 500 | << fqName.c_str(); |
| 501 | return Void(); |
| 502 | } |
| 503 | |
| 504 | HidlService *service = ifaceMap.lookup(name); |
| 505 | |
| 506 | if (service == nullptr) { |
Steven Moreland | cdf9472 | 2017-03-21 12:16:31 -0700 | [diff] [blame] | 507 | auto adding = std::make_unique<HidlService>(fqName, name); |
Yifan Hong | ee531a8 | 2017-02-03 15:10:18 -0800 | [diff] [blame] | 508 | adding->registerPassthroughClient(pid); |
| 509 | ifaceMap.insertService(std::move(adding)); |
| 510 | } else { |
| 511 | service->registerPassthroughClient(pid); |
| 512 | } |
| 513 | return Void(); |
| 514 | } |
| 515 | |
Steven Moreland | 76936fe | 2017-09-19 12:18:30 -0700 | [diff] [blame] | 516 | bool ServiceManager::removeService(const wp<IBase>& who, const std::string* restrictToInstanceName) { |
Steven Moreland | 6f4fbe1 | 2017-07-21 18:07:42 -0700 | [diff] [blame] | 517 | using ::android::hardware::interfacesEqual; |
| 518 | |
Steven Moreland | 76936fe | 2017-09-19 12:18:30 -0700 | [diff] [blame] | 519 | bool keepInstance = false; |
| 520 | bool removed = false; |
Martijn Coenen | 46847a6 | 2016-12-19 05:36:12 +0100 | [diff] [blame] | 521 | for (auto &interfaceMapping : mServiceMap) { |
| 522 | auto &instanceMap = interfaceMapping.second.getInstanceMap(); |
| 523 | |
Steven Moreland | d544cf6 | 2017-01-04 15:24:32 -0800 | [diff] [blame] | 524 | for (auto &servicePair : instanceMap) { |
Steven Moreland | 76936fe | 2017-09-19 12:18:30 -0700 | [diff] [blame] | 525 | const std::string &instanceName = servicePair.first; |
Steven Moreland | d544cf6 | 2017-01-04 15:24:32 -0800 | [diff] [blame] | 526 | const std::unique_ptr<HidlService> &service = servicePair.second; |
Steven Moreland | 76936fe | 2017-09-19 12:18:30 -0700 | [diff] [blame] | 527 | |
Steven Moreland | 6f4fbe1 | 2017-07-21 18:07:42 -0700 | [diff] [blame] | 528 | if (interfacesEqual(service->getService(), who.promote())) { |
Steven Moreland | 76936fe | 2017-09-19 12:18:30 -0700 | [diff] [blame] | 529 | if (restrictToInstanceName != nullptr && *restrictToInstanceName != instanceName) { |
| 530 | // We cannot remove all instances of this service, so we don't return that it |
| 531 | // has been entirely removed. |
| 532 | keepInstance = true; |
| 533 | continue; |
| 534 | } |
| 535 | |
Steven Moreland | cdf9472 | 2017-03-21 12:16:31 -0700 | [diff] [blame] | 536 | service->setService(nullptr, static_cast<pid_t>(IServiceManager::PidConstant::NO_PID)); |
Steven Moreland | 76936fe | 2017-09-19 12:18:30 -0700 | [diff] [blame] | 537 | removed = true; |
Martijn Coenen | 46847a6 | 2016-12-19 05:36:12 +0100 | [diff] [blame] | 538 | } |
| 539 | } |
| 540 | } |
Steven Moreland | 76936fe | 2017-09-19 12:18:30 -0700 | [diff] [blame] | 541 | |
| 542 | return !keepInstance && removed; |
Martijn Coenen | 46847a6 | 2016-12-19 05:36:12 +0100 | [diff] [blame] | 543 | } |
Yifan Hong | 83c49f6 | 2017-01-25 14:16:34 -0800 | [diff] [blame] | 544 | |
Martijn Coenen | 7fafc14 | 2017-03-06 16:17:51 +0100 | [diff] [blame] | 545 | bool ServiceManager::removePackageListener(const wp<IBase>& who) { |
| 546 | bool found = false; |
| 547 | |
| 548 | for (auto &interfaceMapping : mServiceMap) { |
| 549 | found |= interfaceMapping.second.removePackageListener(who); |
| 550 | } |
| 551 | |
| 552 | return found; |
| 553 | } |
| 554 | |
| 555 | bool ServiceManager::removeServiceListener(const wp<IBase>& who) { |
| 556 | bool found = false; |
| 557 | for (auto &interfaceMapping : mServiceMap) { |
Steven Moreland | 6f4fbe1 | 2017-07-21 18:07:42 -0700 | [diff] [blame] | 558 | auto &packageInterfaceMap = interfaceMapping.second; |
Martijn Coenen | 7fafc14 | 2017-03-06 16:17:51 +0100 | [diff] [blame] | 559 | |
Steven Moreland | 6f4fbe1 | 2017-07-21 18:07:42 -0700 | [diff] [blame] | 560 | found |= packageInterfaceMap.removeServiceListener(who); |
Martijn Coenen | 7fafc14 | 2017-03-06 16:17:51 +0100 | [diff] [blame] | 561 | } |
| 562 | return found; |
| 563 | } |
Steven Moreland | 6f4fbe1 | 2017-07-21 18:07:42 -0700 | [diff] [blame] | 564 | } // namespace implementation |
Steven Moreland | d83d110 | 2016-10-25 15:01:47 -0700 | [diff] [blame] | 565 | } // namespace manager |
| 566 | } // namespace hidl |
| 567 | } // namespace android |