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