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