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> |
Steven Moreland | cd4dbdf | 2017-04-07 20:31:22 -0700 | [diff] [blame] | 22 | #include <fstream> |
| 23 | #include <pthread.h> |
Yifan Hong | 9a22d1d | 2017-01-25 14:19:26 -0800 | [diff] [blame] | 24 | #include <unistd.h> |
| 25 | |
| 26 | #include <mutex> |
| 27 | #include <regex> |
Yifan Hong | bd0d8f7 | 2017-05-24 19:43:51 -0700 | [diff] [blame] | 28 | #include <set> |
Yifan Hong | 9a22d1d | 2017-01-25 14:19:26 -0800 | [diff] [blame] | 29 | |
Martijn Coenen | 12f04d9 | 2016-12-07 17:29:41 +0100 | [diff] [blame] | 30 | #include <hidl/HidlBinderSupport.h> |
Steven Moreland | 5d5ef7f | 2016-10-20 19:19:55 -0700 | [diff] [blame] | 31 | #include <hidl/ServiceManagement.h> |
Steven Moreland | 5d5ef7f | 2016-10-20 19:19:55 -0700 | [diff] [blame] | 32 | #include <hidl/Status.h> |
| 33 | |
Steven Moreland | 337e6b6 | 2017-01-18 17:25:13 -0800 | [diff] [blame] | 34 | #include <android-base/logging.h> |
Steven Moreland | c1cee2c | 2017-03-24 16:23:11 +0000 | [diff] [blame] | 35 | #include <android-base/properties.h> |
Steven Moreland | 5d5ef7f | 2016-10-20 19:19:55 -0700 | [diff] [blame] | 36 | #include <hwbinder/IPCThreadState.h> |
| 37 | #include <hwbinder/Parcel.h> |
Steven Moreland | 5d5ef7f | 2016-10-20 19:19:55 -0700 | [diff] [blame] | 38 | |
| 39 | #include <android/hidl/manager/1.0/IServiceManager.h> |
Yifan Hong | 4e92599 | 2017-01-09 17:47:17 -0800 | [diff] [blame] | 40 | #include <android/hidl/manager/1.0/BpHwServiceManager.h> |
| 41 | #include <android/hidl/manager/1.0/BnHwServiceManager.h> |
Steven Moreland | 5d5ef7f | 2016-10-20 19:19:55 -0700 | [diff] [blame] | 42 | |
Yifan Hong | 9a22d1d | 2017-01-25 14:19:26 -0800 | [diff] [blame] | 43 | #define RE_COMPONENT "[a-zA-Z_][a-zA-Z_0-9]*" |
| 44 | #define RE_PATH RE_COMPONENT "(?:[.]" RE_COMPONENT ")*" |
| 45 | static const std::regex gLibraryFileNamePattern("(" RE_PATH "@[0-9]+[.][0-9]+)-impl(.*?).so"); |
| 46 | |
Steven Moreland | c1cee2c | 2017-03-24 16:23:11 +0000 | [diff] [blame] | 47 | using android::base::WaitForProperty; |
| 48 | |
Steven Moreland | 5d5ef7f | 2016-10-20 19:19:55 -0700 | [diff] [blame] | 49 | using android::hidl::manager::V1_0::IServiceManager; |
Steven Moreland | 337e6b6 | 2017-01-18 17:25:13 -0800 | [diff] [blame] | 50 | using android::hidl::manager::V1_0::IServiceNotification; |
Yifan Hong | 4e92599 | 2017-01-09 17:47:17 -0800 | [diff] [blame] | 51 | using android::hidl::manager::V1_0::BpHwServiceManager; |
| 52 | using android::hidl::manager::V1_0::BnHwServiceManager; |
Steven Moreland | 5d5ef7f | 2016-10-20 19:19:55 -0700 | [diff] [blame] | 53 | |
| 54 | namespace android { |
| 55 | namespace hardware { |
| 56 | |
Yifan Hong | 953e6b0 | 2017-03-16 14:52:40 -0700 | [diff] [blame] | 57 | namespace details { |
| 58 | extern Mutex gDefaultServiceManagerLock; |
| 59 | extern sp<android::hidl::manager::V1_0::IServiceManager> gDefaultServiceManager; |
| 60 | } // namespace details |
| 61 | |
Steven Moreland | c1cee2c | 2017-03-24 16:23:11 +0000 | [diff] [blame] | 62 | static const char* kHwServicemanagerReadyProperty = "hwservicemanager.ready"; |
| 63 | |
| 64 | void waitForHwServiceManager() { |
| 65 | using std::literals::chrono_literals::operator""s; |
| 66 | |
| 67 | while (!WaitForProperty(kHwServicemanagerReadyProperty, "true", 1s)) { |
| 68 | LOG(WARNING) << "Waited for hwservicemanager.ready for a second, waiting another..."; |
| 69 | } |
| 70 | } |
| 71 | |
Steven Moreland | cd4dbdf | 2017-04-07 20:31:22 -0700 | [diff] [blame] | 72 | bool endsWith(const std::string &in, const std::string &suffix) { |
| 73 | return in.size() >= suffix.size() && |
| 74 | in.substr(in.size() - suffix.size()) == suffix; |
| 75 | } |
Steven Moreland | 5d5ef7f | 2016-10-20 19:19:55 -0700 | [diff] [blame] | 76 | |
Steven Moreland | cd4dbdf | 2017-04-07 20:31:22 -0700 | [diff] [blame] | 77 | bool startsWith(const std::string &in, const std::string &prefix) { |
| 78 | return in.size() >= prefix.size() && |
| 79 | in.substr(0, prefix.size()) == prefix; |
| 80 | } |
| 81 | |
| 82 | std::string binaryName() { |
| 83 | std::ifstream ifs("/proc/self/cmdline"); |
| 84 | std::string cmdline; |
| 85 | if (!ifs.is_open()) { |
| 86 | return ""; |
| 87 | } |
| 88 | ifs >> cmdline; |
| 89 | |
| 90 | size_t idx = cmdline.rfind("/"); |
| 91 | if (idx != std::string::npos) { |
| 92 | cmdline = cmdline.substr(idx + 1); |
| 93 | } |
| 94 | |
| 95 | return cmdline; |
| 96 | } |
| 97 | |
| 98 | void tryShortenProcessName(const std::string &packageName) { |
| 99 | std::string processName = binaryName(); |
| 100 | |
| 101 | if (!startsWith(processName, packageName)) { |
| 102 | return; |
| 103 | } |
| 104 | |
| 105 | // e.x. android.hardware.module.foo@1.0 -> foo@1.0 |
| 106 | size_t lastDot = packageName.rfind('.'); |
| 107 | size_t secondDot = packageName.rfind('.', lastDot - 1); |
| 108 | |
| 109 | if (secondDot == std::string::npos) { |
| 110 | return; |
| 111 | } |
| 112 | |
| 113 | std::string newName = processName.substr(secondDot + 1, |
| 114 | 16 /* TASK_COMM_LEN */ - 1); |
| 115 | ALOGI("Removing namespace from process name %s to %s.", |
| 116 | processName.c_str(), newName.c_str()); |
| 117 | |
| 118 | int rc = pthread_setname_np(pthread_self(), newName.c_str()); |
| 119 | ALOGI_IF(rc != 0, "Removing namespace from process name %s failed.", |
| 120 | processName.c_str()); |
| 121 | } |
| 122 | |
| 123 | namespace details { |
| 124 | |
| 125 | void onRegistration(const std::string &packageName, |
| 126 | const std::string& /* interfaceName */, |
| 127 | const std::string& /* instanceName */) { |
| 128 | tryShortenProcessName(packageName); |
| 129 | } |
| 130 | |
| 131 | } // details |
| 132 | |
| 133 | sp<IServiceManager> defaultServiceManager() { |
Steven Moreland | 5d5ef7f | 2016-10-20 19:19:55 -0700 | [diff] [blame] | 134 | { |
Yifan Hong | 953e6b0 | 2017-03-16 14:52:40 -0700 | [diff] [blame] | 135 | AutoMutex _l(details::gDefaultServiceManagerLock); |
| 136 | if (details::gDefaultServiceManager != NULL) { |
| 137 | return details::gDefaultServiceManager; |
Yifan Hong | 8fb656b | 2017-03-16 14:30:40 -0700 | [diff] [blame] | 138 | } |
Steven Moreland | cd4dbdf | 2017-04-07 20:31:22 -0700 | [diff] [blame] | 139 | |
Yifan Hong | 8fb656b | 2017-03-16 14:30:40 -0700 | [diff] [blame] | 140 | if (access("/dev/hwbinder", F_OK|R_OK|W_OK) != 0) { |
| 141 | // HwBinder not available on this device or not accessible to |
| 142 | // this process. |
| 143 | return nullptr; |
| 144 | } |
Steven Moreland | c1cee2c | 2017-03-24 16:23:11 +0000 | [diff] [blame] | 145 | |
| 146 | waitForHwServiceManager(); |
| 147 | |
Yifan Hong | 953e6b0 | 2017-03-16 14:52:40 -0700 | [diff] [blame] | 148 | while (details::gDefaultServiceManager == NULL) { |
| 149 | details::gDefaultServiceManager = |
Yifan Hong | 8fb656b | 2017-03-16 14:30:40 -0700 | [diff] [blame] | 150 | fromBinder<IServiceManager, BpHwServiceManager, BnHwServiceManager>( |
| 151 | ProcessState::self()->getContextObject(NULL)); |
Yifan Hong | 953e6b0 | 2017-03-16 14:52:40 -0700 | [diff] [blame] | 152 | if (details::gDefaultServiceManager == NULL) { |
Steven Moreland | c1cee2c | 2017-03-24 16:23:11 +0000 | [diff] [blame] | 153 | LOG(ERROR) << "Waited for hwservicemanager, but got nullptr."; |
Steven Moreland | 5d5ef7f | 2016-10-20 19:19:55 -0700 | [diff] [blame] | 154 | sleep(1); |
Yifan Hong | 8fb656b | 2017-03-16 14:30:40 -0700 | [diff] [blame] | 155 | } |
Steven Moreland | 5d5ef7f | 2016-10-20 19:19:55 -0700 | [diff] [blame] | 156 | } |
| 157 | } |
| 158 | |
Yifan Hong | 953e6b0 | 2017-03-16 14:52:40 -0700 | [diff] [blame] | 159 | return details::gDefaultServiceManager; |
Steven Moreland | 5d5ef7f | 2016-10-20 19:19:55 -0700 | [diff] [blame] | 160 | } |
| 161 | |
Steven Moreland | 0091c09 | 2017-01-20 23:15:18 +0000 | [diff] [blame] | 162 | std::vector<std::string> search(const std::string &path, |
| 163 | const std::string &prefix, |
| 164 | const std::string &suffix) { |
| 165 | std::unique_ptr<DIR, decltype(&closedir)> dir(opendir(path.c_str()), closedir); |
| 166 | if (!dir) return {}; |
| 167 | |
| 168 | std::vector<std::string> results{}; |
| 169 | |
| 170 | dirent* dp; |
| 171 | while ((dp = readdir(dir.get())) != nullptr) { |
| 172 | std::string name = dp->d_name; |
| 173 | |
Steven Moreland | da8e617 | 2017-04-06 17:24:22 -0700 | [diff] [blame] | 174 | if (startsWith(name, prefix) && |
| 175 | endsWith(name, suffix)) { |
Steven Moreland | 0091c09 | 2017-01-20 23:15:18 +0000 | [diff] [blame] | 176 | results.push_back(name); |
| 177 | } |
| 178 | } |
| 179 | |
| 180 | return results; |
| 181 | } |
| 182 | |
Yifan Hong | bd0d8f7 | 2017-05-24 19:43:51 -0700 | [diff] [blame] | 183 | bool matchPackageName(const std::string& lib, std::string* matchedName, std::string* implName) { |
Yifan Hong | 9a22d1d | 2017-01-25 14:19:26 -0800 | [diff] [blame] | 184 | std::smatch match; |
| 185 | if (std::regex_match(lib, match, gLibraryFileNamePattern)) { |
| 186 | *matchedName = match.str(1) + "::I*"; |
Yifan Hong | bd0d8f7 | 2017-05-24 19:43:51 -0700 | [diff] [blame] | 187 | *implName = match.str(2); |
Yifan Hong | 9a22d1d | 2017-01-25 14:19:26 -0800 | [diff] [blame] | 188 | return true; |
| 189 | } |
| 190 | return false; |
| 191 | } |
| 192 | |
Yifan Hong | 7f49f59 | 2017-02-03 15:11:44 -0800 | [diff] [blame] | 193 | static void registerReference(const hidl_string &interfaceName, const hidl_string &instanceName) { |
| 194 | sp<IServiceManager> binderizedManager = defaultServiceManager(); |
| 195 | if (binderizedManager == nullptr) { |
| 196 | LOG(WARNING) << "Could not registerReference for " |
| 197 | << interfaceName << "/" << instanceName |
| 198 | << ": null binderized manager."; |
| 199 | return; |
| 200 | } |
Martijn Coenen | bf13ad0 | 2017-04-27 09:41:13 -0700 | [diff] [blame] | 201 | auto ret = binderizedManager->registerPassthroughClient(interfaceName, instanceName); |
Yifan Hong | 7f49f59 | 2017-02-03 15:11:44 -0800 | [diff] [blame] | 202 | if (!ret.isOk()) { |
| 203 | LOG(WARNING) << "Could not registerReference for " |
| 204 | << interfaceName << "/" << instanceName |
| 205 | << ": " << ret.description(); |
Steven Moreland | 0aeaa78 | 2017-03-22 08:11:07 -0700 | [diff] [blame] | 206 | return; |
Yifan Hong | 7f49f59 | 2017-02-03 15:11:44 -0800 | [diff] [blame] | 207 | } |
Steven Moreland | e681aa5 | 2017-02-15 16:22:37 -0800 | [diff] [blame] | 208 | LOG(VERBOSE) << "Successfully registerReference for " |
| 209 | << interfaceName << "/" << instanceName; |
Yifan Hong | 7f49f59 | 2017-02-03 15:11:44 -0800 | [diff] [blame] | 210 | } |
| 211 | |
Yifan Hong | bd0d8f7 | 2017-05-24 19:43:51 -0700 | [diff] [blame] | 212 | using InstanceDebugInfo = hidl::manager::V1_0::IServiceManager::InstanceDebugInfo; |
| 213 | static inline void fetchPidsForPassthroughLibraries( |
| 214 | std::map<std::string, InstanceDebugInfo>* infos) { |
| 215 | static const std::string proc = "/proc/"; |
| 216 | |
| 217 | std::map<std::string, std::set<pid_t>> pids; |
| 218 | std::unique_ptr<DIR, decltype(&closedir)> dir(opendir(proc.c_str()), closedir); |
| 219 | if (!dir) return; |
| 220 | dirent* dp; |
| 221 | while ((dp = readdir(dir.get())) != nullptr) { |
| 222 | pid_t pid = strtoll(dp->d_name, NULL, 0); |
| 223 | if (pid == 0) continue; |
| 224 | std::string mapsPath = proc + dp->d_name + "/maps"; |
| 225 | std::ifstream ifs{mapsPath}; |
| 226 | if (!ifs.is_open()) continue; |
| 227 | |
| 228 | for (std::string line; std::getline(ifs, line);) { |
| 229 | // The last token of line should look like |
| 230 | // vendor/lib64/hw/android.hardware.foo@1.0-impl-extra.so |
| 231 | // Use some simple filters to ignore bad lines before extracting libFileName |
| 232 | // and checking the key in info to make parsing faster. |
| 233 | if (line.back() != 'o') continue; |
| 234 | if (line.rfind('@') == std::string::npos) continue; |
| 235 | |
| 236 | auto spacePos = line.rfind(' '); |
| 237 | if (spacePos == std::string::npos) continue; |
| 238 | auto libFileName = line.substr(spacePos + 1); |
| 239 | auto it = infos->find(libFileName); |
| 240 | if (it == infos->end()) continue; |
| 241 | pids[libFileName].insert(pid); |
| 242 | } |
| 243 | } |
| 244 | for (auto& pair : *infos) { |
| 245 | pair.second.clientPids = |
| 246 | std::vector<pid_t>{pids[pair.first].begin(), pids[pair.first].end()}; |
| 247 | } |
| 248 | } |
| 249 | |
Steven Moreland | 337e6b6 | 2017-01-18 17:25:13 -0800 | [diff] [blame] | 250 | struct PassthroughServiceManager : IServiceManager { |
Steven Moreland | 519306f | 2017-06-06 17:14:12 -0700 | [diff] [blame^] | 251 | static void openLibs(const std::string& fqName, |
| 252 | std::function<bool /* continue */(void* /* handle */, |
| 253 | const std::string& /* lib */, const std::string& /* sym */)> eachLib) { |
Steven Moreland | da8e617 | 2017-04-06 17:24:22 -0700 | [diff] [blame] | 254 | //fqName looks like android.hardware.foo@1.0::IFoo |
Steven Moreland | 519306f | 2017-06-06 17:14:12 -0700 | [diff] [blame^] | 255 | size_t idx = fqName.find("::"); |
Steven Moreland | da8e617 | 2017-04-06 17:24:22 -0700 | [diff] [blame] | 256 | |
| 257 | if (idx == std::string::npos || |
Steven Moreland | 519306f | 2017-06-06 17:14:12 -0700 | [diff] [blame^] | 258 | idx + strlen("::") + 1 >= fqName.size()) { |
Steven Moreland | 337e6b6 | 2017-01-18 17:25:13 -0800 | [diff] [blame] | 259 | LOG(ERROR) << "Invalid interface name passthrough lookup: " << fqName; |
Steven Moreland | 519306f | 2017-06-06 17:14:12 -0700 | [diff] [blame^] | 260 | return; |
Steven Moreland | 337e6b6 | 2017-01-18 17:25:13 -0800 | [diff] [blame] | 261 | } |
| 262 | |
Steven Moreland | 519306f | 2017-06-06 17:14:12 -0700 | [diff] [blame^] | 263 | std::string packageAndVersion = fqName.substr(0, idx); |
| 264 | std::string ifaceName = fqName.substr(idx + strlen("::")); |
Steven Moreland | da8e617 | 2017-04-06 17:24:22 -0700 | [diff] [blame] | 265 | |
| 266 | const std::string prefix = packageAndVersion + "-impl"; |
| 267 | const std::string sym = "HIDL_FETCH_" + ifaceName; |
Steven Moreland | 348802d | 2017-02-23 12:48:55 -0800 | [diff] [blame] | 268 | |
Steven Moreland | 337e6b6 | 2017-01-18 17:25:13 -0800 | [diff] [blame] | 269 | const int dlMode = RTLD_LAZY; |
| 270 | void *handle = nullptr; |
| 271 | |
Steven Moreland | a29905c | 2017-03-01 10:42:35 -0800 | [diff] [blame] | 272 | dlerror(); // clear |
| 273 | |
Steven Moreland | 337e6b6 | 2017-01-18 17:25:13 -0800 | [diff] [blame] | 274 | for (const std::string &path : { |
| 275 | HAL_LIBRARY_PATH_ODM, HAL_LIBRARY_PATH_VENDOR, HAL_LIBRARY_PATH_SYSTEM |
| 276 | }) { |
Steven Moreland | 0091c09 | 2017-01-20 23:15:18 +0000 | [diff] [blame] | 277 | std::vector<std::string> libs = search(path, prefix, ".so"); |
| 278 | |
Steven Moreland | 0091c09 | 2017-01-20 23:15:18 +0000 | [diff] [blame] | 279 | for (const std::string &lib : libs) { |
Steven Moreland | 348802d | 2017-02-23 12:48:55 -0800 | [diff] [blame] | 280 | const std::string fullPath = path + lib; |
| 281 | |
| 282 | handle = dlopen(fullPath.c_str(), dlMode); |
| 283 | if (handle == nullptr) { |
| 284 | const char* error = dlerror(); |
| 285 | LOG(ERROR) << "Failed to dlopen " << lib << ": " |
| 286 | << (error == nullptr ? "unknown error" : error); |
| 287 | continue; |
Steven Moreland | 0091c09 | 2017-01-20 23:15:18 +0000 | [diff] [blame] | 288 | } |
Steven Moreland | 348802d | 2017-02-23 12:48:55 -0800 | [diff] [blame] | 289 | |
Steven Moreland | 519306f | 2017-06-06 17:14:12 -0700 | [diff] [blame^] | 290 | if (!eachLib(handle, lib, sym)) { |
| 291 | return; |
Steven Moreland | 348802d | 2017-02-23 12:48:55 -0800 | [diff] [blame] | 292 | } |
Steven Moreland | 337e6b6 | 2017-01-18 17:25:13 -0800 | [diff] [blame] | 293 | } |
| 294 | } |
Steven Moreland | 519306f | 2017-06-06 17:14:12 -0700 | [diff] [blame^] | 295 | } |
Steven Moreland | 337e6b6 | 2017-01-18 17:25:13 -0800 | [diff] [blame] | 296 | |
Steven Moreland | 519306f | 2017-06-06 17:14:12 -0700 | [diff] [blame^] | 297 | Return<sp<IBase>> get(const hidl_string& fqName, |
| 298 | const hidl_string& name) override { |
| 299 | sp<IBase> ret = nullptr; |
| 300 | |
| 301 | openLibs(fqName, [&](void* handle, const std::string &lib, const std::string &sym) { |
| 302 | IBase* (*generator)(const char* name); |
| 303 | *(void **)(&generator) = dlsym(handle, sym.c_str()); |
| 304 | if(!generator) { |
| 305 | const char* error = dlerror(); |
| 306 | LOG(ERROR) << "Passthrough lookup opened " << lib |
| 307 | << " but could not find symbol " << sym << ": " |
| 308 | << (error == nullptr ? "unknown error" : error); |
| 309 | dlclose(handle); |
| 310 | return true; |
| 311 | } |
| 312 | |
| 313 | ret = (*generator)(name.c_str()); |
| 314 | |
| 315 | if (ret == nullptr) { |
| 316 | dlclose(handle); |
| 317 | return true; // this module doesn't provide this instance name |
| 318 | } |
| 319 | |
| 320 | registerReference(fqName, name); |
| 321 | return false; |
| 322 | }); |
| 323 | |
| 324 | return ret; |
Steven Moreland | 337e6b6 | 2017-01-18 17:25:13 -0800 | [diff] [blame] | 325 | } |
| 326 | |
Martijn Coenen | 67a0249 | 2017-03-06 13:04:48 +0100 | [diff] [blame] | 327 | Return<bool> add(const hidl_string& /* name */, |
Steven Moreland | 337e6b6 | 2017-01-18 17:25:13 -0800 | [diff] [blame] | 328 | const sp<IBase>& /* service */) override { |
| 329 | LOG(FATAL) << "Cannot register services with passthrough service manager."; |
| 330 | return false; |
| 331 | } |
| 332 | |
Steven Moreland | 330e3e2 | 2017-04-06 09:26:07 -0700 | [diff] [blame] | 333 | Return<Transport> getTransport(const hidl_string& /* fqName */, |
| 334 | const hidl_string& /* name */) { |
| 335 | LOG(FATAL) << "Cannot getTransport with passthrough service manager."; |
| 336 | return Transport::EMPTY; |
| 337 | } |
| 338 | |
Yifan Hong | 705e5da | 2017-03-02 16:59:39 -0800 | [diff] [blame] | 339 | Return<void> list(list_cb /* _hidl_cb */) override { |
| 340 | LOG(FATAL) << "Cannot list services with passthrough service manager."; |
Steven Moreland | 337e6b6 | 2017-01-18 17:25:13 -0800 | [diff] [blame] | 341 | return Void(); |
| 342 | } |
| 343 | Return<void> listByInterface(const hidl_string& /* fqInstanceName */, |
| 344 | listByInterface_cb /* _hidl_cb */) override { |
| 345 | // TODO: add this functionality |
| 346 | LOG(FATAL) << "Cannot list services with passthrough service manager."; |
| 347 | return Void(); |
| 348 | } |
| 349 | |
| 350 | Return<bool> registerForNotifications(const hidl_string& /* fqName */, |
| 351 | const hidl_string& /* name */, |
| 352 | const sp<IServiceNotification>& /* callback */) override { |
| 353 | // This makes no sense. |
| 354 | LOG(FATAL) << "Cannot register for notifications with passthrough service manager."; |
| 355 | return false; |
| 356 | } |
| 357 | |
Yifan Hong | 705e5da | 2017-03-02 16:59:39 -0800 | [diff] [blame] | 358 | Return<void> debugDump(debugDump_cb _hidl_cb) override { |
| 359 | using Arch = ::android::hidl::base::V1_0::DebugInfo::Architecture; |
Yifan Hong | bd0d8f7 | 2017-05-24 19:43:51 -0700 | [diff] [blame] | 360 | using std::literals::string_literals::operator""s; |
Yifan Hong | 705e5da | 2017-03-02 16:59:39 -0800 | [diff] [blame] | 361 | static std::vector<std::pair<Arch, std::vector<const char *>>> sAllPaths{ |
| 362 | {Arch::IS_64BIT, {HAL_LIBRARY_PATH_ODM_64BIT, |
| 363 | HAL_LIBRARY_PATH_VENDOR_64BIT, |
| 364 | HAL_LIBRARY_PATH_SYSTEM_64BIT}}, |
| 365 | {Arch::IS_32BIT, {HAL_LIBRARY_PATH_ODM_32BIT, |
| 366 | HAL_LIBRARY_PATH_VENDOR_32BIT, |
| 367 | HAL_LIBRARY_PATH_SYSTEM_32BIT}} |
| 368 | }; |
Yifan Hong | bd0d8f7 | 2017-05-24 19:43:51 -0700 | [diff] [blame] | 369 | std::map<std::string, InstanceDebugInfo> map; |
Yifan Hong | 705e5da | 2017-03-02 16:59:39 -0800 | [diff] [blame] | 370 | for (const auto &pair : sAllPaths) { |
| 371 | Arch arch = pair.first; |
| 372 | for (const auto &path : pair.second) { |
| 373 | std::vector<std::string> libs = search(path, "", ".so"); |
| 374 | for (const std::string &lib : libs) { |
| 375 | std::string matchedName; |
Yifan Hong | bd0d8f7 | 2017-05-24 19:43:51 -0700 | [diff] [blame] | 376 | std::string implName; |
| 377 | if (matchPackageName(lib, &matchedName, &implName)) { |
| 378 | std::string instanceName{"* ("s + path + ")"s}; |
| 379 | if (!implName.empty()) instanceName += " ("s + implName + ")"s; |
| 380 | map.emplace(path + lib, InstanceDebugInfo{.interfaceName = matchedName, |
| 381 | .instanceName = instanceName, |
| 382 | .clientPids = {}, |
| 383 | .arch = arch}); |
Yifan Hong | 705e5da | 2017-03-02 16:59:39 -0800 | [diff] [blame] | 384 | } |
| 385 | } |
| 386 | } |
| 387 | } |
Yifan Hong | bd0d8f7 | 2017-05-24 19:43:51 -0700 | [diff] [blame] | 388 | fetchPidsForPassthroughLibraries(&map); |
| 389 | hidl_vec<InstanceDebugInfo> vec; |
| 390 | vec.resize(map.size()); |
| 391 | size_t idx = 0; |
| 392 | for (auto&& pair : map) { |
| 393 | vec[idx++] = std::move(pair.second); |
| 394 | } |
Yifan Hong | 705e5da | 2017-03-02 16:59:39 -0800 | [diff] [blame] | 395 | _hidl_cb(vec); |
Yifan Hong | 7f49f59 | 2017-02-03 15:11:44 -0800 | [diff] [blame] | 396 | return Void(); |
| 397 | } |
| 398 | |
Martijn Coenen | bf13ad0 | 2017-04-27 09:41:13 -0700 | [diff] [blame] | 399 | Return<void> registerPassthroughClient(const hidl_string &, const hidl_string &) override { |
Yifan Hong | 7f49f59 | 2017-02-03 15:11:44 -0800 | [diff] [blame] | 400 | // This makes no sense. |
| 401 | LOG(FATAL) << "Cannot call registerPassthroughClient on passthrough service manager. " |
| 402 | << "Call it on defaultServiceManager() instead."; |
Yifan Hong | 9a22d1d | 2017-01-25 14:19:26 -0800 | [diff] [blame] | 403 | return Void(); |
| 404 | } |
| 405 | |
Steven Moreland | 337e6b6 | 2017-01-18 17:25:13 -0800 | [diff] [blame] | 406 | }; |
| 407 | |
| 408 | sp<IServiceManager> getPassthroughServiceManager() { |
| 409 | static sp<PassthroughServiceManager> manager(new PassthroughServiceManager()); |
| 410 | return manager; |
| 411 | } |
| 412 | |
Steven Moreland | cbefd35 | 2017-01-23 20:29:05 -0800 | [diff] [blame] | 413 | namespace details { |
| 414 | |
Steven Moreland | 519306f | 2017-06-06 17:14:12 -0700 | [diff] [blame^] | 415 | void preloadPassthroughService(const std::string &descriptor) { |
| 416 | PassthroughServiceManager::openLibs(descriptor, |
| 417 | [&](void* /* handle */, const std::string& /* lib */, const std::string& /* sym */) { |
| 418 | // do nothing |
| 419 | return true; // open all libs |
| 420 | }); |
| 421 | } |
| 422 | |
Steven Moreland | cbefd35 | 2017-01-23 20:29:05 -0800 | [diff] [blame] | 423 | struct Waiter : IServiceNotification { |
| 424 | Return<void> onRegistration(const hidl_string& /* fqName */, |
| 425 | const hidl_string& /* name */, |
| 426 | bool /* preexisting */) override { |
| 427 | std::unique_lock<std::mutex> lock(mMutex); |
| 428 | if (mRegistered) { |
| 429 | return Void(); |
| 430 | } |
| 431 | mRegistered = true; |
| 432 | lock.unlock(); |
| 433 | |
| 434 | mCondition.notify_one(); |
| 435 | return Void(); |
| 436 | } |
| 437 | |
Steven Moreland | f1e14f2 | 2017-03-28 09:33:06 -0700 | [diff] [blame] | 438 | void wait(const std::string &interface, const std::string &instanceName) { |
| 439 | using std::literals::chrono_literals::operator""s; |
| 440 | |
Steven Moreland | cbefd35 | 2017-01-23 20:29:05 -0800 | [diff] [blame] | 441 | std::unique_lock<std::mutex> lock(mMutex); |
Steven Moreland | f1e14f2 | 2017-03-28 09:33:06 -0700 | [diff] [blame] | 442 | while(true) { |
| 443 | mCondition.wait_for(lock, 1s, [this]{ |
| 444 | return mRegistered; |
| 445 | }); |
| 446 | |
| 447 | if (mRegistered) { |
| 448 | break; |
| 449 | } |
| 450 | |
| 451 | LOG(WARNING) << "Waited one second for " |
| 452 | << interface << "/" << instanceName |
| 453 | << ". Waiting another..."; |
| 454 | } |
Steven Moreland | cbefd35 | 2017-01-23 20:29:05 -0800 | [diff] [blame] | 455 | } |
| 456 | |
| 457 | private: |
| 458 | std::mutex mMutex; |
| 459 | std::condition_variable mCondition; |
| 460 | bool mRegistered = false; |
| 461 | }; |
| 462 | |
| 463 | void waitForHwService( |
| 464 | const std::string &interface, const std::string &instanceName) { |
| 465 | const sp<IServiceManager> manager = defaultServiceManager(); |
| 466 | |
| 467 | if (manager == nullptr) { |
| 468 | LOG(ERROR) << "Could not get default service manager."; |
| 469 | return; |
| 470 | } |
| 471 | |
| 472 | sp<Waiter> waiter = new Waiter(); |
| 473 | Return<bool> ret = manager->registerForNotifications(interface, instanceName, waiter); |
| 474 | |
| 475 | if (!ret.isOk()) { |
| 476 | LOG(ERROR) << "Transport error, " << ret.description() |
| 477 | << ", during notification registration for " |
| 478 | << interface << "/" << instanceName << "."; |
| 479 | return; |
| 480 | } |
| 481 | |
| 482 | if (!ret) { |
| 483 | LOG(ERROR) << "Could not register for notifications for " |
| 484 | << interface << "/" << instanceName << "."; |
| 485 | return; |
| 486 | } |
| 487 | |
Steven Moreland | f1e14f2 | 2017-03-28 09:33:06 -0700 | [diff] [blame] | 488 | waiter->wait(interface, instanceName); |
Steven Moreland | cbefd35 | 2017-01-23 20:29:05 -0800 | [diff] [blame] | 489 | } |
| 490 | |
| 491 | }; // namespace details |
| 492 | |
Steven Moreland | 5d5ef7f | 2016-10-20 19:19:55 -0700 | [diff] [blame] | 493 | }; // namespace hardware |
| 494 | }; // namespace android |