Steven Moreland | 5d5ef7f | 2016-10-20 19:19:55 -0700 | [diff] [blame] | 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 "ServiceManagement" |
| 18 | |
Yifan Hong | 9a22d1d | 2017-01-25 14:19:26 -0800 | [diff] [blame] | 19 | #include <condition_variable> |
| 20 | #include <dlfcn.h> |
| 21 | #include <dirent.h> |
| 22 | #include <unistd.h> |
| 23 | |
| 24 | #include <mutex> |
| 25 | #include <regex> |
| 26 | |
Martijn Coenen | 12f04d9 | 2016-12-07 17:29:41 +0100 | [diff] [blame] | 27 | #include <hidl/HidlBinderSupport.h> |
Steven Moreland | 5d5ef7f | 2016-10-20 19:19:55 -0700 | [diff] [blame] | 28 | #include <hidl/ServiceManagement.h> |
| 29 | #include <hidl/Static.h> |
| 30 | #include <hidl/Status.h> |
| 31 | |
Steven Moreland | 337e6b6 | 2017-01-18 17:25:13 -0800 | [diff] [blame] | 32 | #include <android-base/logging.h> |
Steven Moreland | 337e6b6 | 2017-01-18 17:25:13 -0800 | [diff] [blame] | 33 | #include <hidl-util/FQName.h> |
Steven Moreland | 0091c09 | 2017-01-20 23:15:18 +0000 | [diff] [blame] | 34 | #include <hidl-util/StringHelper.h> |
Steven Moreland | 5d5ef7f | 2016-10-20 19:19:55 -0700 | [diff] [blame] | 35 | #include <hwbinder/IPCThreadState.h> |
| 36 | #include <hwbinder/Parcel.h> |
Steven Moreland | 5d5ef7f | 2016-10-20 19:19:55 -0700 | [diff] [blame] | 37 | |
| 38 | #include <android/hidl/manager/1.0/IServiceManager.h> |
Yifan Hong | 4e92599 | 2017-01-09 17:47:17 -0800 | [diff] [blame] | 39 | #include <android/hidl/manager/1.0/BpHwServiceManager.h> |
| 40 | #include <android/hidl/manager/1.0/BnHwServiceManager.h> |
Steven Moreland | 5d5ef7f | 2016-10-20 19:19:55 -0700 | [diff] [blame] | 41 | |
Yifan Hong | 9a22d1d | 2017-01-25 14:19:26 -0800 | [diff] [blame] | 42 | #define RE_COMPONENT "[a-zA-Z_][a-zA-Z_0-9]*" |
| 43 | #define RE_PATH RE_COMPONENT "(?:[.]" RE_COMPONENT ")*" |
| 44 | static const std::regex gLibraryFileNamePattern("(" RE_PATH "@[0-9]+[.][0-9]+)-impl(.*?).so"); |
| 45 | |
Steven Moreland | 5d5ef7f | 2016-10-20 19:19:55 -0700 | [diff] [blame] | 46 | using android::hidl::manager::V1_0::IServiceManager; |
Steven Moreland | 337e6b6 | 2017-01-18 17:25:13 -0800 | [diff] [blame] | 47 | using android::hidl::manager::V1_0::IServiceNotification; |
Yifan Hong | 4e92599 | 2017-01-09 17:47:17 -0800 | [diff] [blame] | 48 | using android::hidl::manager::V1_0::BpHwServiceManager; |
| 49 | using android::hidl::manager::V1_0::BnHwServiceManager; |
Steven Moreland | 5d5ef7f | 2016-10-20 19:19:55 -0700 | [diff] [blame] | 50 | |
| 51 | namespace android { |
| 52 | namespace hardware { |
| 53 | |
| 54 | sp<IServiceManager> defaultServiceManager() { |
| 55 | |
Steven Moreland | 5d5ef7f | 2016-10-20 19:19:55 -0700 | [diff] [blame] | 56 | { |
| 57 | AutoMutex _l(gDefaultServiceManagerLock); |
Yifan Hong | 8fb656b | 2017-03-16 14:30:40 -0700 | [diff] [blame^] | 58 | if (gDefaultServiceManager != NULL) { |
| 59 | return gDefaultServiceManager; |
| 60 | } |
| 61 | if (access("/dev/hwbinder", F_OK|R_OK|W_OK) != 0) { |
| 62 | // HwBinder not available on this device or not accessible to |
| 63 | // this process. |
| 64 | return nullptr; |
| 65 | } |
Steven Moreland | 5d5ef7f | 2016-10-20 19:19:55 -0700 | [diff] [blame] | 66 | while (gDefaultServiceManager == NULL) { |
Yifan Hong | 8fb656b | 2017-03-16 14:30:40 -0700 | [diff] [blame^] | 67 | gDefaultServiceManager = |
| 68 | fromBinder<IServiceManager, BpHwServiceManager, BnHwServiceManager>( |
| 69 | ProcessState::self()->getContextObject(NULL)); |
| 70 | if (gDefaultServiceManager == NULL) { |
Steven Moreland | 5d5ef7f | 2016-10-20 19:19:55 -0700 | [diff] [blame] | 71 | sleep(1); |
Yifan Hong | 8fb656b | 2017-03-16 14:30:40 -0700 | [diff] [blame^] | 72 | } |
Steven Moreland | 5d5ef7f | 2016-10-20 19:19:55 -0700 | [diff] [blame] | 73 | } |
| 74 | } |
| 75 | |
| 76 | return gDefaultServiceManager; |
| 77 | } |
| 78 | |
Steven Moreland | 0091c09 | 2017-01-20 23:15:18 +0000 | [diff] [blame] | 79 | std::vector<std::string> search(const std::string &path, |
| 80 | const std::string &prefix, |
| 81 | const std::string &suffix) { |
| 82 | std::unique_ptr<DIR, decltype(&closedir)> dir(opendir(path.c_str()), closedir); |
| 83 | if (!dir) return {}; |
| 84 | |
| 85 | std::vector<std::string> results{}; |
| 86 | |
| 87 | dirent* dp; |
| 88 | while ((dp = readdir(dir.get())) != nullptr) { |
| 89 | std::string name = dp->d_name; |
| 90 | |
| 91 | if (StringHelper::StartsWith(name, prefix) && |
| 92 | StringHelper::EndsWith(name, suffix)) { |
| 93 | results.push_back(name); |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | return results; |
| 98 | } |
| 99 | |
Yifan Hong | 9a22d1d | 2017-01-25 14:19:26 -0800 | [diff] [blame] | 100 | bool matchPackageName(const std::string &lib, std::string *matchedName) { |
| 101 | std::smatch match; |
| 102 | if (std::regex_match(lib, match, gLibraryFileNamePattern)) { |
| 103 | *matchedName = match.str(1) + "::I*"; |
| 104 | return true; |
| 105 | } |
| 106 | return false; |
| 107 | } |
| 108 | |
Yifan Hong | 7f49f59 | 2017-02-03 15:11:44 -0800 | [diff] [blame] | 109 | static void registerReference(const hidl_string &interfaceName, const hidl_string &instanceName) { |
| 110 | sp<IServiceManager> binderizedManager = defaultServiceManager(); |
| 111 | if (binderizedManager == nullptr) { |
| 112 | LOG(WARNING) << "Could not registerReference for " |
| 113 | << interfaceName << "/" << instanceName |
| 114 | << ": null binderized manager."; |
| 115 | return; |
| 116 | } |
| 117 | auto ret = binderizedManager->registerPassthroughClient(interfaceName, instanceName, getpid()); |
| 118 | if (!ret.isOk()) { |
| 119 | LOG(WARNING) << "Could not registerReference for " |
| 120 | << interfaceName << "/" << instanceName |
| 121 | << ": " << ret.description(); |
| 122 | } |
Steven Moreland | e681aa5 | 2017-02-15 16:22:37 -0800 | [diff] [blame] | 123 | LOG(VERBOSE) << "Successfully registerReference for " |
| 124 | << interfaceName << "/" << instanceName; |
Yifan Hong | 7f49f59 | 2017-02-03 15:11:44 -0800 | [diff] [blame] | 125 | } |
| 126 | |
Steven Moreland | 337e6b6 | 2017-01-18 17:25:13 -0800 | [diff] [blame] | 127 | struct PassthroughServiceManager : IServiceManager { |
| 128 | Return<sp<IBase>> get(const hidl_string& fqName, |
| 129 | const hidl_string& name) override { |
Steven Moreland | 348802d | 2017-02-23 12:48:55 -0800 | [diff] [blame] | 130 | const FQName iface(fqName); |
Steven Moreland | 337e6b6 | 2017-01-18 17:25:13 -0800 | [diff] [blame] | 131 | |
| 132 | if (!iface.isValid() || |
| 133 | !iface.isFullyQualified() || |
| 134 | iface.isIdentifier()) { |
| 135 | LOG(ERROR) << "Invalid interface name passthrough lookup: " << fqName; |
| 136 | return nullptr; |
| 137 | } |
| 138 | |
Steven Moreland | 348802d | 2017-02-23 12:48:55 -0800 | [diff] [blame] | 139 | const std::string prefix = iface.getPackageAndVersion().string() + "-impl"; |
| 140 | const std::string sym = "HIDL_FETCH_" + iface.name(); |
| 141 | |
Steven Moreland | 337e6b6 | 2017-01-18 17:25:13 -0800 | [diff] [blame] | 142 | const int dlMode = RTLD_LAZY; |
| 143 | void *handle = nullptr; |
| 144 | |
Steven Moreland | 0091c09 | 2017-01-20 23:15:18 +0000 | [diff] [blame] | 145 | // TODO: lookup in VINTF instead |
| 146 | // TODO(b/34135607): Remove HAL_LIBRARY_PATH_SYSTEM |
| 147 | |
Steven Moreland | a29905c | 2017-03-01 10:42:35 -0800 | [diff] [blame] | 148 | dlerror(); // clear |
| 149 | |
Steven Moreland | 337e6b6 | 2017-01-18 17:25:13 -0800 | [diff] [blame] | 150 | for (const std::string &path : { |
| 151 | HAL_LIBRARY_PATH_ODM, HAL_LIBRARY_PATH_VENDOR, HAL_LIBRARY_PATH_SYSTEM |
| 152 | }) { |
Steven Moreland | 0091c09 | 2017-01-20 23:15:18 +0000 | [diff] [blame] | 153 | std::vector<std::string> libs = search(path, prefix, ".so"); |
| 154 | |
Steven Moreland | 0091c09 | 2017-01-20 23:15:18 +0000 | [diff] [blame] | 155 | for (const std::string &lib : libs) { |
Steven Moreland | 348802d | 2017-02-23 12:48:55 -0800 | [diff] [blame] | 156 | const std::string fullPath = path + lib; |
| 157 | |
| 158 | handle = dlopen(fullPath.c_str(), dlMode); |
| 159 | if (handle == nullptr) { |
| 160 | const char* error = dlerror(); |
| 161 | LOG(ERROR) << "Failed to dlopen " << lib << ": " |
| 162 | << (error == nullptr ? "unknown error" : error); |
| 163 | continue; |
Steven Moreland | 0091c09 | 2017-01-20 23:15:18 +0000 | [diff] [blame] | 164 | } |
Steven Moreland | 348802d | 2017-02-23 12:48:55 -0800 | [diff] [blame] | 165 | |
| 166 | IBase* (*generator)(const char* name); |
| 167 | *(void **)(&generator) = dlsym(handle, sym.c_str()); |
| 168 | if(!generator) { |
| 169 | const char* error = dlerror(); |
| 170 | LOG(ERROR) << "Passthrough lookup opened " << lib |
| 171 | << " but could not find symbol " << sym << ": " |
| 172 | << (error == nullptr ? "unknown error" : error); |
| 173 | dlclose(handle); |
| 174 | continue; |
| 175 | } |
| 176 | |
| 177 | IBase *interface = (*generator)(name); |
| 178 | |
| 179 | if (interface == nullptr) { |
| 180 | dlclose(handle); |
| 181 | continue; // this module doesn't provide this instance name |
| 182 | } |
| 183 | |
| 184 | registerReference(fqName, name); |
| 185 | |
| 186 | return interface; |
Steven Moreland | 337e6b6 | 2017-01-18 17:25:13 -0800 | [diff] [blame] | 187 | } |
| 188 | } |
| 189 | |
Steven Moreland | 348802d | 2017-02-23 12:48:55 -0800 | [diff] [blame] | 190 | return nullptr; |
Steven Moreland | 337e6b6 | 2017-01-18 17:25:13 -0800 | [diff] [blame] | 191 | } |
| 192 | |
Martijn Coenen | 67a0249 | 2017-03-06 13:04:48 +0100 | [diff] [blame] | 193 | Return<bool> add(const hidl_string& /* name */, |
Steven Moreland | 337e6b6 | 2017-01-18 17:25:13 -0800 | [diff] [blame] | 194 | const sp<IBase>& /* service */) override { |
| 195 | LOG(FATAL) << "Cannot register services with passthrough service manager."; |
| 196 | return false; |
| 197 | } |
| 198 | |
Yifan Hong | 705e5da | 2017-03-02 16:59:39 -0800 | [diff] [blame] | 199 | Return<void> list(list_cb /* _hidl_cb */) override { |
| 200 | LOG(FATAL) << "Cannot list services with passthrough service manager."; |
Steven Moreland | 337e6b6 | 2017-01-18 17:25:13 -0800 | [diff] [blame] | 201 | return Void(); |
| 202 | } |
| 203 | Return<void> listByInterface(const hidl_string& /* fqInstanceName */, |
| 204 | listByInterface_cb /* _hidl_cb */) override { |
| 205 | // TODO: add this functionality |
| 206 | LOG(FATAL) << "Cannot list services with passthrough service manager."; |
| 207 | return Void(); |
| 208 | } |
| 209 | |
| 210 | Return<bool> registerForNotifications(const hidl_string& /* fqName */, |
| 211 | const hidl_string& /* name */, |
| 212 | const sp<IServiceNotification>& /* callback */) override { |
| 213 | // This makes no sense. |
| 214 | LOG(FATAL) << "Cannot register for notifications with passthrough service manager."; |
| 215 | return false; |
| 216 | } |
| 217 | |
Yifan Hong | 705e5da | 2017-03-02 16:59:39 -0800 | [diff] [blame] | 218 | Return<void> debugDump(debugDump_cb _hidl_cb) override { |
| 219 | using Arch = ::android::hidl::base::V1_0::DebugInfo::Architecture; |
| 220 | static std::vector<std::pair<Arch, std::vector<const char *>>> sAllPaths{ |
| 221 | {Arch::IS_64BIT, {HAL_LIBRARY_PATH_ODM_64BIT, |
| 222 | HAL_LIBRARY_PATH_VENDOR_64BIT, |
| 223 | HAL_LIBRARY_PATH_SYSTEM_64BIT}}, |
| 224 | {Arch::IS_32BIT, {HAL_LIBRARY_PATH_ODM_32BIT, |
| 225 | HAL_LIBRARY_PATH_VENDOR_32BIT, |
| 226 | HAL_LIBRARY_PATH_SYSTEM_32BIT}} |
| 227 | }; |
| 228 | std::vector<InstanceDebugInfo> vec; |
| 229 | for (const auto &pair : sAllPaths) { |
| 230 | Arch arch = pair.first; |
| 231 | for (const auto &path : pair.second) { |
| 232 | std::vector<std::string> libs = search(path, "", ".so"); |
| 233 | for (const std::string &lib : libs) { |
| 234 | std::string matchedName; |
| 235 | if (matchPackageName(lib, &matchedName)) { |
| 236 | vec.push_back({ |
| 237 | .interfaceName = matchedName, |
| 238 | .instanceName = "*", |
| 239 | .clientPids = {}, |
| 240 | .arch = arch |
| 241 | }); |
| 242 | } |
| 243 | } |
| 244 | } |
| 245 | } |
| 246 | _hidl_cb(vec); |
Yifan Hong | 7f49f59 | 2017-02-03 15:11:44 -0800 | [diff] [blame] | 247 | return Void(); |
| 248 | } |
| 249 | |
| 250 | Return<void> registerPassthroughClient(const hidl_string &, const hidl_string &, int32_t) override { |
| 251 | // This makes no sense. |
| 252 | LOG(FATAL) << "Cannot call registerPassthroughClient on passthrough service manager. " |
| 253 | << "Call it on defaultServiceManager() instead."; |
Yifan Hong | 9a22d1d | 2017-01-25 14:19:26 -0800 | [diff] [blame] | 254 | return Void(); |
| 255 | } |
| 256 | |
Steven Moreland | 337e6b6 | 2017-01-18 17:25:13 -0800 | [diff] [blame] | 257 | }; |
| 258 | |
| 259 | sp<IServiceManager> getPassthroughServiceManager() { |
| 260 | static sp<PassthroughServiceManager> manager(new PassthroughServiceManager()); |
| 261 | return manager; |
| 262 | } |
| 263 | |
Steven Moreland | cbefd35 | 2017-01-23 20:29:05 -0800 | [diff] [blame] | 264 | namespace details { |
| 265 | |
| 266 | struct Waiter : IServiceNotification { |
| 267 | Return<void> onRegistration(const hidl_string& /* fqName */, |
| 268 | const hidl_string& /* name */, |
| 269 | bool /* preexisting */) override { |
| 270 | std::unique_lock<std::mutex> lock(mMutex); |
| 271 | if (mRegistered) { |
| 272 | return Void(); |
| 273 | } |
| 274 | mRegistered = true; |
| 275 | lock.unlock(); |
| 276 | |
| 277 | mCondition.notify_one(); |
| 278 | return Void(); |
| 279 | } |
| 280 | |
| 281 | void wait() { |
| 282 | std::unique_lock<std::mutex> lock(mMutex); |
| 283 | mCondition.wait(lock, [this]{ |
| 284 | return mRegistered; |
| 285 | }); |
| 286 | } |
| 287 | |
| 288 | private: |
| 289 | std::mutex mMutex; |
| 290 | std::condition_variable mCondition; |
| 291 | bool mRegistered = false; |
| 292 | }; |
| 293 | |
| 294 | void waitForHwService( |
| 295 | const std::string &interface, const std::string &instanceName) { |
| 296 | const sp<IServiceManager> manager = defaultServiceManager(); |
| 297 | |
| 298 | if (manager == nullptr) { |
| 299 | LOG(ERROR) << "Could not get default service manager."; |
| 300 | return; |
| 301 | } |
| 302 | |
| 303 | sp<Waiter> waiter = new Waiter(); |
| 304 | Return<bool> ret = manager->registerForNotifications(interface, instanceName, waiter); |
| 305 | |
| 306 | if (!ret.isOk()) { |
| 307 | LOG(ERROR) << "Transport error, " << ret.description() |
| 308 | << ", during notification registration for " |
| 309 | << interface << "/" << instanceName << "."; |
| 310 | return; |
| 311 | } |
| 312 | |
| 313 | if (!ret) { |
| 314 | LOG(ERROR) << "Could not register for notifications for " |
| 315 | << interface << "/" << instanceName << "."; |
| 316 | return; |
| 317 | } |
| 318 | |
| 319 | waiter->wait(); |
| 320 | } |
| 321 | |
| 322 | }; // namespace details |
| 323 | |
Steven Moreland | 5d5ef7f | 2016-10-20 19:19:55 -0700 | [diff] [blame] | 324 | }; // namespace hardware |
| 325 | }; // namespace android |