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> |
Justin Yun | 1f04810 | 2017-12-01 15:30:08 +0900 | [diff] [blame] | 32 | #include <hidl/HidlInternal.h> |
Steven Moreland | 83cb4b3 | 2018-03-14 10:55:42 -0700 | [diff] [blame] | 33 | #include <hidl/HidlTransportUtils.h> |
Steven Moreland | 5d5ef7f | 2016-10-20 19:19:55 -0700 | [diff] [blame] | 34 | #include <hidl/ServiceManagement.h> |
Steven Moreland | 5d5ef7f | 2016-10-20 19:19:55 -0700 | [diff] [blame] | 35 | #include <hidl/Status.h> |
| 36 | |
Steven Moreland | 337e6b6 | 2017-01-18 17:25:13 -0800 | [diff] [blame] | 37 | #include <android-base/logging.h> |
Steven Moreland | c1cee2c | 2017-03-24 16:23:11 +0000 | [diff] [blame] | 38 | #include <android-base/properties.h> |
Justin Yun | 1f04810 | 2017-12-01 15:30:08 +0900 | [diff] [blame] | 39 | #include <android-base/stringprintf.h> |
Steven Moreland | 5d5ef7f | 2016-10-20 19:19:55 -0700 | [diff] [blame] | 40 | #include <hwbinder/IPCThreadState.h> |
| 41 | #include <hwbinder/Parcel.h> |
Jiyong Park | ba8ace1 | 2017-05-15 15:44:39 +0900 | [diff] [blame] | 42 | #include <vndksupport/linker.h> |
Steven Moreland | 5d5ef7f | 2016-10-20 19:19:55 -0700 | [diff] [blame] | 43 | |
Steven Moreland | 8990969 | 2018-05-30 14:11:19 -0700 | [diff] [blame] | 44 | #include <android/hidl/manager/1.2/BnHwServiceManager.h> |
| 45 | #include <android/hidl/manager/1.2/BpHwServiceManager.h> |
| 46 | #include <android/hidl/manager/1.2/IServiceManager.h> |
Steven Moreland | 5d5ef7f | 2016-10-20 19:19:55 -0700 | [diff] [blame] | 47 | |
Yifan Hong | 9a22d1d | 2017-01-25 14:19:26 -0800 | [diff] [blame] | 48 | #define RE_COMPONENT "[a-zA-Z_][a-zA-Z_0-9]*" |
| 49 | #define RE_PATH RE_COMPONENT "(?:[.]" RE_COMPONENT ")*" |
| 50 | static const std::regex gLibraryFileNamePattern("(" RE_PATH "@[0-9]+[.][0-9]+)-impl(.*?).so"); |
| 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 | 8990969 | 2018-05-30 14:11:19 -0700 | [diff] [blame] | 56 | using IServiceManager1_2 = android::hidl::manager::V1_2::IServiceManager; |
Steven Moreland | 337e6b6 | 2017-01-18 17:25:13 -0800 | [diff] [blame] | 57 | using android::hidl::manager::V1_0::IServiceNotification; |
Steven Moreland | 5d5ef7f | 2016-10-20 19:19:55 -0700 | [diff] [blame] | 58 | |
| 59 | namespace android { |
| 60 | namespace hardware { |
| 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 | 405d761 | 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 | 405d761 | 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 | |
Chih-Hung Hsieh | 41649d5 | 2017-08-03 14:27:21 -0700 | [diff] [blame] | 90 | size_t idx = cmdline.rfind('/'); |
Steven Moreland | 405d761 | 2017-04-07 20:31:22 -0700 | [diff] [blame] | 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 | |
Steven Moreland | 2a2678e | 2017-07-21 18:07:38 -0700 | [diff] [blame] | 133 | sp<IServiceManager1_0> defaultServiceManager() { |
Steven Moreland | 8990969 | 2018-05-30 14:11:19 -0700 | [diff] [blame] | 134 | return defaultServiceManager1_2(); |
Steven Moreland | 2a2678e | 2017-07-21 18:07:38 -0700 | [diff] [blame] | 135 | } |
| 136 | sp<IServiceManager1_1> defaultServiceManager1_1() { |
Steven Moreland | 8990969 | 2018-05-30 14:11:19 -0700 | [diff] [blame] | 137 | return defaultServiceManager1_2(); |
| 138 | } |
| 139 | sp<IServiceManager1_2> defaultServiceManager1_2() { |
| 140 | using android::hidl::manager::V1_2::BnHwServiceManager; |
| 141 | using android::hidl::manager::V1_2::BpHwServiceManager; |
| 142 | |
| 143 | static std::mutex gDefaultServiceManagerLock; |
| 144 | static sp<IServiceManager1_2> gDefaultServiceManager; |
| 145 | |
Steven Moreland | 5d5ef7f | 2016-10-20 19:19:55 -0700 | [diff] [blame] | 146 | { |
Steven Moreland | 8990969 | 2018-05-30 14:11:19 -0700 | [diff] [blame] | 147 | std::lock_guard<std::mutex> _l(gDefaultServiceManagerLock); |
| 148 | if (gDefaultServiceManager != nullptr) { |
| 149 | return gDefaultServiceManager; |
Yifan Hong | 8fb656b | 2017-03-16 14:30:40 -0700 | [diff] [blame] | 150 | } |
Steven Moreland | 405d761 | 2017-04-07 20:31:22 -0700 | [diff] [blame] | 151 | |
Yifan Hong | 8fb656b | 2017-03-16 14:30:40 -0700 | [diff] [blame] | 152 | if (access("/dev/hwbinder", F_OK|R_OK|W_OK) != 0) { |
| 153 | // HwBinder not available on this device or not accessible to |
| 154 | // this process. |
| 155 | return nullptr; |
| 156 | } |
Steven Moreland | c1cee2c | 2017-03-24 16:23:11 +0000 | [diff] [blame] | 157 | |
| 158 | waitForHwServiceManager(); |
| 159 | |
Steven Moreland | 8990969 | 2018-05-30 14:11:19 -0700 | [diff] [blame] | 160 | while (gDefaultServiceManager == nullptr) { |
| 161 | gDefaultServiceManager = |
| 162 | fromBinder<IServiceManager1_2, BpHwServiceManager, BnHwServiceManager>( |
| 163 | ProcessState::self()->getContextObject(nullptr)); |
| 164 | if (gDefaultServiceManager == nullptr) { |
Steven Moreland | c1cee2c | 2017-03-24 16:23:11 +0000 | [diff] [blame] | 165 | LOG(ERROR) << "Waited for hwservicemanager, but got nullptr."; |
Steven Moreland | 5d5ef7f | 2016-10-20 19:19:55 -0700 | [diff] [blame] | 166 | sleep(1); |
Yifan Hong | 8fb656b | 2017-03-16 14:30:40 -0700 | [diff] [blame] | 167 | } |
Steven Moreland | 5d5ef7f | 2016-10-20 19:19:55 -0700 | [diff] [blame] | 168 | } |
| 169 | } |
| 170 | |
Steven Moreland | 8990969 | 2018-05-30 14:11:19 -0700 | [diff] [blame] | 171 | return gDefaultServiceManager; |
Steven Moreland | 5d5ef7f | 2016-10-20 19:19:55 -0700 | [diff] [blame] | 172 | } |
| 173 | |
Steven Moreland | 0091c09 | 2017-01-20 23:15:18 +0000 | [diff] [blame] | 174 | std::vector<std::string> search(const std::string &path, |
| 175 | const std::string &prefix, |
| 176 | const std::string &suffix) { |
| 177 | std::unique_ptr<DIR, decltype(&closedir)> dir(opendir(path.c_str()), closedir); |
| 178 | if (!dir) return {}; |
| 179 | |
| 180 | std::vector<std::string> results{}; |
| 181 | |
| 182 | dirent* dp; |
| 183 | while ((dp = readdir(dir.get())) != nullptr) { |
| 184 | std::string name = dp->d_name; |
| 185 | |
Steven Moreland | 819c05d | 2017-04-06 17:24:22 -0700 | [diff] [blame] | 186 | if (startsWith(name, prefix) && |
| 187 | endsWith(name, suffix)) { |
Steven Moreland | 0091c09 | 2017-01-20 23:15:18 +0000 | [diff] [blame] | 188 | results.push_back(name); |
| 189 | } |
| 190 | } |
| 191 | |
| 192 | return results; |
| 193 | } |
| 194 | |
Yifan Hong | bd0d8f7 | 2017-05-24 19:43:51 -0700 | [diff] [blame] | 195 | bool matchPackageName(const std::string& lib, std::string* matchedName, std::string* implName) { |
Yifan Hong | 9a22d1d | 2017-01-25 14:19:26 -0800 | [diff] [blame] | 196 | std::smatch match; |
| 197 | if (std::regex_match(lib, match, gLibraryFileNamePattern)) { |
| 198 | *matchedName = match.str(1) + "::I*"; |
Yifan Hong | bd0d8f7 | 2017-05-24 19:43:51 -0700 | [diff] [blame] | 199 | *implName = match.str(2); |
Yifan Hong | 9a22d1d | 2017-01-25 14:19:26 -0800 | [diff] [blame] | 200 | return true; |
| 201 | } |
| 202 | return false; |
| 203 | } |
| 204 | |
Yifan Hong | 7f49f59 | 2017-02-03 15:11:44 -0800 | [diff] [blame] | 205 | static void registerReference(const hidl_string &interfaceName, const hidl_string &instanceName) { |
Steven Moreland | 2a2678e | 2017-07-21 18:07:38 -0700 | [diff] [blame] | 206 | sp<IServiceManager1_0> binderizedManager = defaultServiceManager(); |
Yifan Hong | 7f49f59 | 2017-02-03 15:11:44 -0800 | [diff] [blame] | 207 | if (binderizedManager == nullptr) { |
| 208 | LOG(WARNING) << "Could not registerReference for " |
| 209 | << interfaceName << "/" << instanceName |
| 210 | << ": null binderized manager."; |
| 211 | return; |
| 212 | } |
Martijn Coenen | af4aba5 | 2017-04-27 09:41:13 -0700 | [diff] [blame] | 213 | auto ret = binderizedManager->registerPassthroughClient(interfaceName, instanceName); |
Yifan Hong | 7f49f59 | 2017-02-03 15:11:44 -0800 | [diff] [blame] | 214 | if (!ret.isOk()) { |
| 215 | LOG(WARNING) << "Could not registerReference for " |
| 216 | << interfaceName << "/" << instanceName |
| 217 | << ": " << ret.description(); |
Steven Moreland | 0aeaa78 | 2017-03-22 08:11:07 -0700 | [diff] [blame] | 218 | return; |
Yifan Hong | 7f49f59 | 2017-02-03 15:11:44 -0800 | [diff] [blame] | 219 | } |
Steven Moreland | e681aa5 | 2017-02-15 16:22:37 -0800 | [diff] [blame] | 220 | LOG(VERBOSE) << "Successfully registerReference for " |
| 221 | << interfaceName << "/" << instanceName; |
Yifan Hong | 7f49f59 | 2017-02-03 15:11:44 -0800 | [diff] [blame] | 222 | } |
| 223 | |
Yifan Hong | bd0d8f7 | 2017-05-24 19:43:51 -0700 | [diff] [blame] | 224 | using InstanceDebugInfo = hidl::manager::V1_0::IServiceManager::InstanceDebugInfo; |
| 225 | static inline void fetchPidsForPassthroughLibraries( |
| 226 | std::map<std::string, InstanceDebugInfo>* infos) { |
| 227 | static const std::string proc = "/proc/"; |
| 228 | |
| 229 | std::map<std::string, std::set<pid_t>> pids; |
| 230 | std::unique_ptr<DIR, decltype(&closedir)> dir(opendir(proc.c_str()), closedir); |
| 231 | if (!dir) return; |
| 232 | dirent* dp; |
| 233 | while ((dp = readdir(dir.get())) != nullptr) { |
Stephen Hines | fd9ecee | 2017-09-27 18:52:52 -0700 | [diff] [blame] | 234 | pid_t pid = strtoll(dp->d_name, nullptr, 0); |
Yifan Hong | bd0d8f7 | 2017-05-24 19:43:51 -0700 | [diff] [blame] | 235 | if (pid == 0) continue; |
| 236 | std::string mapsPath = proc + dp->d_name + "/maps"; |
| 237 | std::ifstream ifs{mapsPath}; |
| 238 | if (!ifs.is_open()) continue; |
| 239 | |
| 240 | for (std::string line; std::getline(ifs, line);) { |
| 241 | // The last token of line should look like |
| 242 | // vendor/lib64/hw/android.hardware.foo@1.0-impl-extra.so |
| 243 | // Use some simple filters to ignore bad lines before extracting libFileName |
| 244 | // and checking the key in info to make parsing faster. |
| 245 | if (line.back() != 'o') continue; |
| 246 | if (line.rfind('@') == std::string::npos) continue; |
| 247 | |
| 248 | auto spacePos = line.rfind(' '); |
| 249 | if (spacePos == std::string::npos) continue; |
| 250 | auto libFileName = line.substr(spacePos + 1); |
| 251 | auto it = infos->find(libFileName); |
| 252 | if (it == infos->end()) continue; |
| 253 | pids[libFileName].insert(pid); |
| 254 | } |
| 255 | } |
| 256 | for (auto& pair : *infos) { |
| 257 | pair.second.clientPids = |
| 258 | std::vector<pid_t>{pids[pair.first].begin(), pids[pair.first].end()}; |
| 259 | } |
| 260 | } |
| 261 | |
Steven Moreland | 2a2678e | 2017-07-21 18:07:38 -0700 | [diff] [blame] | 262 | struct PassthroughServiceManager : IServiceManager1_1 { |
Chih-Hung Hsieh | 41649d5 | 2017-08-03 14:27:21 -0700 | [diff] [blame] | 263 | static void openLibs( |
| 264 | const std::string& fqName, |
| 265 | const std::function<bool /* continue */ (void* /* handle */, const std::string& /* lib */, |
| 266 | const std::string& /* sym */)>& eachLib) { |
Steven Moreland | 819c05d | 2017-04-06 17:24:22 -0700 | [diff] [blame] | 267 | //fqName looks like android.hardware.foo@1.0::IFoo |
Steven Moreland | 519306f | 2017-06-06 17:14:12 -0700 | [diff] [blame] | 268 | size_t idx = fqName.find("::"); |
Steven Moreland | 819c05d | 2017-04-06 17:24:22 -0700 | [diff] [blame] | 269 | |
| 270 | if (idx == std::string::npos || |
Steven Moreland | 519306f | 2017-06-06 17:14:12 -0700 | [diff] [blame] | 271 | idx + strlen("::") + 1 >= fqName.size()) { |
Steven Moreland | 337e6b6 | 2017-01-18 17:25:13 -0800 | [diff] [blame] | 272 | LOG(ERROR) << "Invalid interface name passthrough lookup: " << fqName; |
Steven Moreland | 519306f | 2017-06-06 17:14:12 -0700 | [diff] [blame] | 273 | return; |
Steven Moreland | 337e6b6 | 2017-01-18 17:25:13 -0800 | [diff] [blame] | 274 | } |
| 275 | |
Steven Moreland | 519306f | 2017-06-06 17:14:12 -0700 | [diff] [blame] | 276 | std::string packageAndVersion = fqName.substr(0, idx); |
| 277 | std::string ifaceName = fqName.substr(idx + strlen("::")); |
Steven Moreland | 819c05d | 2017-04-06 17:24:22 -0700 | [diff] [blame] | 278 | |
| 279 | const std::string prefix = packageAndVersion + "-impl"; |
| 280 | const std::string sym = "HIDL_FETCH_" + ifaceName; |
Steven Moreland | 348802d | 2017-02-23 12:48:55 -0800 | [diff] [blame] | 281 | |
Steven Moreland | 77f4c85 | 2017-10-12 11:05:53 -0700 | [diff] [blame] | 282 | constexpr int dlMode = RTLD_LAZY; |
| 283 | void* handle = nullptr; |
Steven Moreland | 337e6b6 | 2017-01-18 17:25:13 -0800 | [diff] [blame] | 284 | |
Steven Moreland | a29905c | 2017-03-01 10:42:35 -0800 | [diff] [blame] | 285 | dlerror(); // clear |
| 286 | |
Justin Yun | 1f04810 | 2017-12-01 15:30:08 +0900 | [diff] [blame] | 287 | static std::string halLibPathVndkSp = android::base::StringPrintf( |
| 288 | HAL_LIBRARY_PATH_VNDK_SP_FOR_VERSION, details::getVndkVersionStr().c_str()); |
Steven Moreland | f7dea69 | 2017-07-14 12:49:28 -0700 | [diff] [blame] | 289 | std::vector<std::string> paths = {HAL_LIBRARY_PATH_ODM, HAL_LIBRARY_PATH_VENDOR, |
Justin Yun | 1f04810 | 2017-12-01 15:30:08 +0900 | [diff] [blame] | 290 | halLibPathVndkSp, HAL_LIBRARY_PATH_SYSTEM}; |
Steven Moreland | 77f4c85 | 2017-10-12 11:05:53 -0700 | [diff] [blame] | 291 | |
Steven Moreland | f7dea69 | 2017-07-14 12:49:28 -0700 | [diff] [blame] | 292 | #ifdef LIBHIDL_TARGET_DEBUGGABLE |
| 293 | const char* env = std::getenv("TREBLE_TESTING_OVERRIDE"); |
| 294 | const bool trebleTestingOverride = env && !strcmp(env, "true"); |
| 295 | if (trebleTestingOverride) { |
Steven Moreland | 77f4c85 | 2017-10-12 11:05:53 -0700 | [diff] [blame] | 296 | // Load HAL implementations that are statically linked |
| 297 | handle = dlopen(nullptr, dlMode); |
| 298 | if (handle == nullptr) { |
| 299 | const char* error = dlerror(); |
| 300 | LOG(ERROR) << "Failed to dlopen self: " |
| 301 | << (error == nullptr ? "unknown error" : error); |
| 302 | } else if (!eachLib(handle, "SELF", sym)) { |
| 303 | return; |
| 304 | } |
| 305 | |
Steven Moreland | f7dea69 | 2017-07-14 12:49:28 -0700 | [diff] [blame] | 306 | const char* vtsRootPath = std::getenv("VTS_ROOT_PATH"); |
| 307 | if (vtsRootPath && strlen(vtsRootPath) > 0) { |
| 308 | const std::string halLibraryPathVtsOverride = |
| 309 | std::string(vtsRootPath) + HAL_LIBRARY_PATH_SYSTEM; |
Steven Moreland | 77f4c85 | 2017-10-12 11:05:53 -0700 | [diff] [blame] | 310 | paths.insert(paths.begin(), halLibraryPathVtsOverride); |
Steven Moreland | f7dea69 | 2017-07-14 12:49:28 -0700 | [diff] [blame] | 311 | } |
| 312 | } |
| 313 | #endif |
Steven Moreland | 77f4c85 | 2017-10-12 11:05:53 -0700 | [diff] [blame] | 314 | |
Steven Moreland | f7dea69 | 2017-07-14 12:49:28 -0700 | [diff] [blame] | 315 | for (const std::string& path : paths) { |
Steven Moreland | 0091c09 | 2017-01-20 23:15:18 +0000 | [diff] [blame] | 316 | std::vector<std::string> libs = search(path, prefix, ".so"); |
| 317 | |
Steven Moreland | 0091c09 | 2017-01-20 23:15:18 +0000 | [diff] [blame] | 318 | for (const std::string &lib : libs) { |
Steven Moreland | 348802d | 2017-02-23 12:48:55 -0800 | [diff] [blame] | 319 | const std::string fullPath = path + lib; |
| 320 | |
Steven Moreland | 65c00cb | 2017-10-12 11:35:22 -0700 | [diff] [blame] | 321 | if (path == HAL_LIBRARY_PATH_SYSTEM) { |
Jiyong Park | 3ab546c | 2017-04-06 20:28:07 +0900 | [diff] [blame] | 322 | handle = dlopen(fullPath.c_str(), dlMode); |
Steven Moreland | 65c00cb | 2017-10-12 11:35:22 -0700 | [diff] [blame] | 323 | } else { |
| 324 | handle = android_load_sphal_library(fullPath.c_str(), dlMode); |
Jiyong Park | 3ab546c | 2017-04-06 20:28:07 +0900 | [diff] [blame] | 325 | } |
| 326 | |
Steven Moreland | 348802d | 2017-02-23 12:48:55 -0800 | [diff] [blame] | 327 | if (handle == nullptr) { |
| 328 | const char* error = dlerror(); |
| 329 | LOG(ERROR) << "Failed to dlopen " << lib << ": " |
| 330 | << (error == nullptr ? "unknown error" : error); |
| 331 | continue; |
Steven Moreland | 0091c09 | 2017-01-20 23:15:18 +0000 | [diff] [blame] | 332 | } |
Steven Moreland | 348802d | 2017-02-23 12:48:55 -0800 | [diff] [blame] | 333 | |
Steven Moreland | 519306f | 2017-06-06 17:14:12 -0700 | [diff] [blame] | 334 | if (!eachLib(handle, lib, sym)) { |
| 335 | return; |
Steven Moreland | 348802d | 2017-02-23 12:48:55 -0800 | [diff] [blame] | 336 | } |
Steven Moreland | 337e6b6 | 2017-01-18 17:25:13 -0800 | [diff] [blame] | 337 | } |
| 338 | } |
Steven Moreland | 519306f | 2017-06-06 17:14:12 -0700 | [diff] [blame] | 339 | } |
Steven Moreland | 337e6b6 | 2017-01-18 17:25:13 -0800 | [diff] [blame] | 340 | |
Steven Moreland | 519306f | 2017-06-06 17:14:12 -0700 | [diff] [blame] | 341 | Return<sp<IBase>> get(const hidl_string& fqName, |
| 342 | const hidl_string& name) override { |
| 343 | sp<IBase> ret = nullptr; |
| 344 | |
| 345 | openLibs(fqName, [&](void* handle, const std::string &lib, const std::string &sym) { |
| 346 | IBase* (*generator)(const char* name); |
| 347 | *(void **)(&generator) = dlsym(handle, sym.c_str()); |
| 348 | if(!generator) { |
| 349 | const char* error = dlerror(); |
| 350 | LOG(ERROR) << "Passthrough lookup opened " << lib |
| 351 | << " but could not find symbol " << sym << ": " |
| 352 | << (error == nullptr ? "unknown error" : error); |
| 353 | dlclose(handle); |
| 354 | return true; |
| 355 | } |
| 356 | |
| 357 | ret = (*generator)(name.c_str()); |
| 358 | |
| 359 | if (ret == nullptr) { |
| 360 | dlclose(handle); |
| 361 | return true; // this module doesn't provide this instance name |
| 362 | } |
| 363 | |
Steven Moreland | 83cb4b3 | 2018-03-14 10:55:42 -0700 | [diff] [blame] | 364 | // Actual fqname might be a subclass. |
| 365 | // This assumption is tested in vts_treble_vintf_test |
| 366 | using ::android::hardware::details::getDescriptor; |
| 367 | std::string actualFqName = getDescriptor(ret.get()); |
| 368 | CHECK(actualFqName.size() > 0); |
| 369 | registerReference(actualFqName, name); |
Steven Moreland | 519306f | 2017-06-06 17:14:12 -0700 | [diff] [blame] | 370 | return false; |
| 371 | }); |
| 372 | |
| 373 | return ret; |
Steven Moreland | 337e6b6 | 2017-01-18 17:25:13 -0800 | [diff] [blame] | 374 | } |
| 375 | |
Martijn Coenen | 67a0249 | 2017-03-06 13:04:48 +0100 | [diff] [blame] | 376 | Return<bool> add(const hidl_string& /* name */, |
Steven Moreland | 337e6b6 | 2017-01-18 17:25:13 -0800 | [diff] [blame] | 377 | const sp<IBase>& /* service */) override { |
| 378 | LOG(FATAL) << "Cannot register services with passthrough service manager."; |
| 379 | return false; |
| 380 | } |
| 381 | |
Steven Moreland | c601c5f | 2017-04-06 09:26:07 -0700 | [diff] [blame] | 382 | Return<Transport> getTransport(const hidl_string& /* fqName */, |
| 383 | const hidl_string& /* name */) { |
| 384 | LOG(FATAL) << "Cannot getTransport with passthrough service manager."; |
| 385 | return Transport::EMPTY; |
| 386 | } |
| 387 | |
Yifan Hong | 705e5da | 2017-03-02 16:59:39 -0800 | [diff] [blame] | 388 | Return<void> list(list_cb /* _hidl_cb */) override { |
| 389 | LOG(FATAL) << "Cannot list services with passthrough service manager."; |
Steven Moreland | 337e6b6 | 2017-01-18 17:25:13 -0800 | [diff] [blame] | 390 | return Void(); |
| 391 | } |
| 392 | Return<void> listByInterface(const hidl_string& /* fqInstanceName */, |
| 393 | listByInterface_cb /* _hidl_cb */) override { |
| 394 | // TODO: add this functionality |
| 395 | LOG(FATAL) << "Cannot list services with passthrough service manager."; |
| 396 | return Void(); |
| 397 | } |
| 398 | |
| 399 | Return<bool> registerForNotifications(const hidl_string& /* fqName */, |
| 400 | const hidl_string& /* name */, |
| 401 | const sp<IServiceNotification>& /* callback */) override { |
| 402 | // This makes no sense. |
| 403 | LOG(FATAL) << "Cannot register for notifications with passthrough service manager."; |
| 404 | return false; |
| 405 | } |
| 406 | |
Yifan Hong | 705e5da | 2017-03-02 16:59:39 -0800 | [diff] [blame] | 407 | Return<void> debugDump(debugDump_cb _hidl_cb) override { |
| 408 | using Arch = ::android::hidl::base::V1_0::DebugInfo::Architecture; |
Yifan Hong | bd0d8f7 | 2017-05-24 19:43:51 -0700 | [diff] [blame] | 409 | using std::literals::string_literals::operator""s; |
Justin Yun | 1f04810 | 2017-12-01 15:30:08 +0900 | [diff] [blame] | 410 | static std::string halLibPathVndkSp64 = android::base::StringPrintf( |
| 411 | HAL_LIBRARY_PATH_VNDK_SP_64BIT_FOR_VERSION, details::getVndkVersionStr().c_str()); |
| 412 | static std::string halLibPathVndkSp32 = android::base::StringPrintf( |
| 413 | HAL_LIBRARY_PATH_VNDK_SP_32BIT_FOR_VERSION, details::getVndkVersionStr().c_str()); |
Jiyong Park | 56eb149 | 2017-08-04 16:16:13 +0900 | [diff] [blame] | 414 | static std::vector<std::pair<Arch, std::vector<const char*>>> sAllPaths{ |
| 415 | {Arch::IS_64BIT, |
| 416 | {HAL_LIBRARY_PATH_ODM_64BIT, HAL_LIBRARY_PATH_VENDOR_64BIT, |
Justin Yun | 1f04810 | 2017-12-01 15:30:08 +0900 | [diff] [blame] | 417 | halLibPathVndkSp64.c_str(), HAL_LIBRARY_PATH_SYSTEM_64BIT}}, |
Jiyong Park | 56eb149 | 2017-08-04 16:16:13 +0900 | [diff] [blame] | 418 | {Arch::IS_32BIT, |
| 419 | {HAL_LIBRARY_PATH_ODM_32BIT, HAL_LIBRARY_PATH_VENDOR_32BIT, |
Justin Yun | 1f04810 | 2017-12-01 15:30:08 +0900 | [diff] [blame] | 420 | halLibPathVndkSp32.c_str(), HAL_LIBRARY_PATH_SYSTEM_32BIT}}}; |
Yifan Hong | bd0d8f7 | 2017-05-24 19:43:51 -0700 | [diff] [blame] | 421 | std::map<std::string, InstanceDebugInfo> map; |
Yifan Hong | 705e5da | 2017-03-02 16:59:39 -0800 | [diff] [blame] | 422 | for (const auto &pair : sAllPaths) { |
| 423 | Arch arch = pair.first; |
| 424 | for (const auto &path : pair.second) { |
| 425 | std::vector<std::string> libs = search(path, "", ".so"); |
| 426 | for (const std::string &lib : libs) { |
| 427 | std::string matchedName; |
Yifan Hong | bd0d8f7 | 2017-05-24 19:43:51 -0700 | [diff] [blame] | 428 | std::string implName; |
| 429 | if (matchPackageName(lib, &matchedName, &implName)) { |
| 430 | std::string instanceName{"* ("s + path + ")"s}; |
| 431 | if (!implName.empty()) instanceName += " ("s + implName + ")"s; |
| 432 | map.emplace(path + lib, InstanceDebugInfo{.interfaceName = matchedName, |
| 433 | .instanceName = instanceName, |
| 434 | .clientPids = {}, |
| 435 | .arch = arch}); |
Yifan Hong | 705e5da | 2017-03-02 16:59:39 -0800 | [diff] [blame] | 436 | } |
| 437 | } |
| 438 | } |
| 439 | } |
Yifan Hong | bd0d8f7 | 2017-05-24 19:43:51 -0700 | [diff] [blame] | 440 | fetchPidsForPassthroughLibraries(&map); |
| 441 | hidl_vec<InstanceDebugInfo> vec; |
| 442 | vec.resize(map.size()); |
| 443 | size_t idx = 0; |
| 444 | for (auto&& pair : map) { |
| 445 | vec[idx++] = std::move(pair.second); |
| 446 | } |
Yifan Hong | 705e5da | 2017-03-02 16:59:39 -0800 | [diff] [blame] | 447 | _hidl_cb(vec); |
Yifan Hong | 7f49f59 | 2017-02-03 15:11:44 -0800 | [diff] [blame] | 448 | return Void(); |
| 449 | } |
| 450 | |
Martijn Coenen | af4aba5 | 2017-04-27 09:41:13 -0700 | [diff] [blame] | 451 | Return<void> registerPassthroughClient(const hidl_string &, const hidl_string &) override { |
Yifan Hong | 7f49f59 | 2017-02-03 15:11:44 -0800 | [diff] [blame] | 452 | // This makes no sense. |
| 453 | LOG(FATAL) << "Cannot call registerPassthroughClient on passthrough service manager. " |
| 454 | << "Call it on defaultServiceManager() instead."; |
Yifan Hong | 9a22d1d | 2017-01-25 14:19:26 -0800 | [diff] [blame] | 455 | return Void(); |
| 456 | } |
| 457 | |
Steven Moreland | 2a2678e | 2017-07-21 18:07:38 -0700 | [diff] [blame] | 458 | Return<bool> unregisterForNotifications(const hidl_string& /* fqName */, |
| 459 | const hidl_string& /* name */, |
| 460 | const sp<IServiceNotification>& /* callback */) override { |
| 461 | // This makes no sense. |
| 462 | LOG(FATAL) << "Cannot unregister for notifications with passthrough service manager."; |
| 463 | return false; |
| 464 | } |
| 465 | |
Steven Moreland | 337e6b6 | 2017-01-18 17:25:13 -0800 | [diff] [blame] | 466 | }; |
| 467 | |
Steven Moreland | 2a2678e | 2017-07-21 18:07:38 -0700 | [diff] [blame] | 468 | sp<IServiceManager1_0> getPassthroughServiceManager() { |
| 469 | return getPassthroughServiceManager1_1(); |
| 470 | } |
| 471 | sp<IServiceManager1_1> getPassthroughServiceManager1_1() { |
Steven Moreland | 337e6b6 | 2017-01-18 17:25:13 -0800 | [diff] [blame] | 472 | static sp<PassthroughServiceManager> manager(new PassthroughServiceManager()); |
| 473 | return manager; |
| 474 | } |
| 475 | |
Steven Moreland | cbefd35 | 2017-01-23 20:29:05 -0800 | [diff] [blame] | 476 | namespace details { |
| 477 | |
Steven Moreland | 519306f | 2017-06-06 17:14:12 -0700 | [diff] [blame] | 478 | void preloadPassthroughService(const std::string &descriptor) { |
| 479 | PassthroughServiceManager::openLibs(descriptor, |
| 480 | [&](void* /* handle */, const std::string& /* lib */, const std::string& /* sym */) { |
| 481 | // do nothing |
| 482 | return true; // open all libs |
| 483 | }); |
| 484 | } |
| 485 | |
Steven Moreland | cbefd35 | 2017-01-23 20:29:05 -0800 | [diff] [blame] | 486 | struct Waiter : IServiceNotification { |
Martijn Coenen | 773609e | 2017-10-24 09:57:53 +0200 | [diff] [blame] | 487 | Waiter(const std::string& interface, const std::string& instanceName, |
| 488 | const sp<IServiceManager1_1>& sm) : mInterfaceName(interface), |
| 489 | mInstanceName(instanceName), mSm(sm) { |
Martijn Coenen | e6cdfd3 | 2017-11-13 14:03:05 +0100 | [diff] [blame] | 490 | } |
| 491 | |
| 492 | void onFirstRef() override { |
Martijn Coenen | b88ae7f | 2017-10-24 11:45:41 +0200 | [diff] [blame] | 493 | // If this process only has one binder thread, and we're calling wait() from |
| 494 | // that thread, it will block forever because we hung up the one and only |
| 495 | // binder thread on a condition variable that can only be notified by an |
| 496 | // incoming binder call. |
Tobias Lindskog | 88e886f | 2018-01-05 10:32:43 +0100 | [diff] [blame] | 497 | if (IPCThreadState::self()->isOnlyBinderThread()) { |
Martijn Coenen | b88ae7f | 2017-10-24 11:45:41 +0200 | [diff] [blame] | 498 | LOG(WARNING) << "Can't efficiently wait for " << mInterfaceName << "/" |
| 499 | << mInstanceName << ", because we are called from " |
| 500 | << "the only binder thread in this process."; |
| 501 | return; |
| 502 | } |
| 503 | |
Martijn Coenen | e6cdfd3 | 2017-11-13 14:03:05 +0100 | [diff] [blame] | 504 | Return<bool> ret = mSm->registerForNotifications(mInterfaceName, mInstanceName, this); |
Martijn Coenen | 773609e | 2017-10-24 09:57:53 +0200 | [diff] [blame] | 505 | |
| 506 | if (!ret.isOk()) { |
| 507 | LOG(ERROR) << "Transport error, " << ret.description() |
Martijn Coenen | e6cdfd3 | 2017-11-13 14:03:05 +0100 | [diff] [blame] | 508 | << ", during notification registration for " << mInterfaceName << "/" |
| 509 | << mInstanceName << "."; |
Martijn Coenen | 773609e | 2017-10-24 09:57:53 +0200 | [diff] [blame] | 510 | return; |
| 511 | } |
| 512 | |
| 513 | if (!ret) { |
Martijn Coenen | e6cdfd3 | 2017-11-13 14:03:05 +0100 | [diff] [blame] | 514 | LOG(ERROR) << "Could not register for notifications for " << mInterfaceName << "/" |
| 515 | << mInstanceName << "."; |
Martijn Coenen | 773609e | 2017-10-24 09:57:53 +0200 | [diff] [blame] | 516 | return; |
| 517 | } |
| 518 | |
| 519 | mRegisteredForNotifications = true; |
| 520 | } |
| 521 | |
| 522 | ~Waiter() { |
Martijn Coenen | 3fa15c2 | 2018-02-10 11:30:00 +0100 | [diff] [blame] | 523 | if (!mDoneCalled) { |
| 524 | LOG(FATAL) |
| 525 | << "Waiter still registered for notifications, call done() before dropping ref!"; |
Martijn Coenen | 773609e | 2017-10-24 09:57:53 +0200 | [diff] [blame] | 526 | } |
| 527 | } |
| 528 | |
Steven Moreland | cbefd35 | 2017-01-23 20:29:05 -0800 | [diff] [blame] | 529 | Return<void> onRegistration(const hidl_string& /* fqName */, |
| 530 | const hidl_string& /* name */, |
| 531 | bool /* preexisting */) override { |
| 532 | std::unique_lock<std::mutex> lock(mMutex); |
| 533 | if (mRegistered) { |
| 534 | return Void(); |
| 535 | } |
| 536 | mRegistered = true; |
| 537 | lock.unlock(); |
| 538 | |
| 539 | mCondition.notify_one(); |
| 540 | return Void(); |
| 541 | } |
| 542 | |
Steven Moreland | 0771d8a | 2018-05-09 13:29:14 -0700 | [diff] [blame] | 543 | void wait(bool timeout) { |
Steven Moreland | 4960510 | 2017-03-28 09:33:06 -0700 | [diff] [blame] | 544 | using std::literals::chrono_literals::operator""s; |
| 545 | |
Martijn Coenen | 773609e | 2017-10-24 09:57:53 +0200 | [diff] [blame] | 546 | if (!mRegisteredForNotifications) { |
| 547 | // As an alternative, just sleep for a second and return |
| 548 | LOG(WARNING) << "Waiting one second for " << mInterfaceName << "/" << mInstanceName; |
| 549 | sleep(1); |
| 550 | return; |
| 551 | } |
| 552 | |
Steven Moreland | cbefd35 | 2017-01-23 20:29:05 -0800 | [diff] [blame] | 553 | std::unique_lock<std::mutex> lock(mMutex); |
Steven Moreland | 0771d8a | 2018-05-09 13:29:14 -0700 | [diff] [blame] | 554 | do { |
Steven Moreland | 4960510 | 2017-03-28 09:33:06 -0700 | [diff] [blame] | 555 | mCondition.wait_for(lock, 1s, [this]{ |
| 556 | return mRegistered; |
| 557 | }); |
| 558 | |
| 559 | if (mRegistered) { |
| 560 | break; |
| 561 | } |
| 562 | |
Steven Moreland | 0771d8a | 2018-05-09 13:29:14 -0700 | [diff] [blame] | 563 | LOG(WARNING) << "Waited one second for " << mInterfaceName << "/" << mInstanceName; |
| 564 | } while (!timeout); |
Steven Moreland | cbefd35 | 2017-01-23 20:29:05 -0800 | [diff] [blame] | 565 | } |
| 566 | |
Martijn Coenen | 773609e | 2017-10-24 09:57:53 +0200 | [diff] [blame] | 567 | // Be careful when using this; after calling reset(), you must always try to retrieve |
| 568 | // the corresponding service before blocking on the waiter; otherwise, you might run |
| 569 | // into a race-condition where the service has just (re-)registered, you clear the state |
| 570 | // here, and subsequently calling waiter->wait() will block forever. |
| 571 | void reset() { |
| 572 | std::unique_lock<std::mutex> lock(mMutex); |
| 573 | mRegistered = false; |
| 574 | } |
Martijn Coenen | 3fa15c2 | 2018-02-10 11:30:00 +0100 | [diff] [blame] | 575 | |
| 576 | // done() must be called before dropping the last strong ref to the Waiter, to make |
| 577 | // sure we can properly unregister with hwservicemanager. |
| 578 | void done() { |
| 579 | if (mRegisteredForNotifications) { |
| 580 | if (!mSm->unregisterForNotifications(mInterfaceName, mInstanceName, this) |
| 581 | .withDefault(false)) { |
| 582 | LOG(ERROR) << "Could not unregister service notification for " << mInterfaceName |
| 583 | << "/" << mInstanceName << "."; |
| 584 | } else { |
| 585 | mRegisteredForNotifications = false; |
| 586 | } |
| 587 | } |
| 588 | mDoneCalled = true; |
| 589 | } |
| 590 | |
| 591 | private: |
Martijn Coenen | 773609e | 2017-10-24 09:57:53 +0200 | [diff] [blame] | 592 | const std::string mInterfaceName; |
| 593 | const std::string mInstanceName; |
| 594 | const sp<IServiceManager1_1>& mSm; |
Steven Moreland | cbefd35 | 2017-01-23 20:29:05 -0800 | [diff] [blame] | 595 | std::mutex mMutex; |
| 596 | std::condition_variable mCondition; |
| 597 | bool mRegistered = false; |
Martijn Coenen | 773609e | 2017-10-24 09:57:53 +0200 | [diff] [blame] | 598 | bool mRegisteredForNotifications = false; |
Martijn Coenen | 3fa15c2 | 2018-02-10 11:30:00 +0100 | [diff] [blame] | 599 | bool mDoneCalled = false; |
Steven Moreland | cbefd35 | 2017-01-23 20:29:05 -0800 | [diff] [blame] | 600 | }; |
| 601 | |
| 602 | void waitForHwService( |
| 603 | const std::string &interface, const std::string &instanceName) { |
Martijn Coenen | 773609e | 2017-10-24 09:57:53 +0200 | [diff] [blame] | 604 | sp<Waiter> waiter = new Waiter(interface, instanceName, defaultServiceManager1_1()); |
Steven Moreland | 0771d8a | 2018-05-09 13:29:14 -0700 | [diff] [blame] | 605 | waiter->wait(false /* timeout */); |
Martijn Coenen | 3fa15c2 | 2018-02-10 11:30:00 +0100 | [diff] [blame] | 606 | waiter->done(); |
Martijn Coenen | 773609e | 2017-10-24 09:57:53 +0200 | [diff] [blame] | 607 | } |
Steven Moreland | cbefd35 | 2017-01-23 20:29:05 -0800 | [diff] [blame] | 608 | |
Martijn Coenen | 773609e | 2017-10-24 09:57:53 +0200 | [diff] [blame] | 609 | // Prints relevant error/warning messages for error return values from |
| 610 | // details::canCastInterface(), both transaction errors (!castReturn.isOk()) |
| 611 | // as well as actual cast failures (castReturn.isOk() && castReturn = false). |
| 612 | // Returns 'true' if the error is non-fatal and it's useful to retry |
| 613 | bool handleCastError(const Return<bool>& castReturn, const std::string& descriptor, |
| 614 | const std::string& instance) { |
| 615 | if (castReturn.isOk()) { |
| 616 | if (castReturn) { |
| 617 | details::logAlwaysFatal("Successful cast value passed into handleCastError."); |
| 618 | } |
| 619 | // This should never happen, and there's not really a point in retrying. |
| 620 | ALOGE("getService: received incompatible service (bug in hwservicemanager?) for " |
| 621 | "%s/%s.", descriptor.c_str(), instance.c_str()); |
| 622 | return false; |
Steven Moreland | cbefd35 | 2017-01-23 20:29:05 -0800 | [diff] [blame] | 623 | } |
Martijn Coenen | 773609e | 2017-10-24 09:57:53 +0200 | [diff] [blame] | 624 | if (castReturn.isDeadObject()) { |
| 625 | ALOGW("getService: found dead hwbinder service for %s/%s.", descriptor.c_str(), |
| 626 | instance.c_str()); |
| 627 | return true; |
Steven Moreland | cbefd35 | 2017-01-23 20:29:05 -0800 | [diff] [blame] | 628 | } |
Martijn Coenen | 773609e | 2017-10-24 09:57:53 +0200 | [diff] [blame] | 629 | // This can happen due to: |
| 630 | // 1) No SELinux permissions |
| 631 | // 2) Other transaction failure (no buffer space, kernel error) |
| 632 | // The first isn't recoverable, but the second is. |
| 633 | // Since we can't yet differentiate between the two, and clients depend |
| 634 | // on us not blocking in case 1), treat this as a fatal error for now. |
| 635 | ALOGW("getService: unable to call into hwbinder service for %s/%s.", |
| 636 | descriptor.c_str(), instance.c_str()); |
| 637 | return false; |
Steven Moreland | cbefd35 | 2017-01-23 20:29:05 -0800 | [diff] [blame] | 638 | } |
| 639 | |
Martijn Coenen | 86c3abb | 2017-10-24 09:33:28 +0200 | [diff] [blame] | 640 | sp<::android::hidl::base::V1_0::IBase> getRawServiceInternal(const std::string& descriptor, |
| 641 | const std::string& instance, |
| 642 | bool retry, bool getStub) { |
| 643 | using Transport = ::android::hidl::manager::V1_0::IServiceManager::Transport; |
| 644 | using ::android::hidl::base::V1_0::IBase; |
| 645 | using ::android::hidl::manager::V1_0::IServiceManager; |
Martijn Coenen | 0e6f8ba | 2018-03-07 09:51:01 +0100 | [diff] [blame] | 646 | sp<Waiter> waiter; |
Martijn Coenen | 86c3abb | 2017-10-24 09:33:28 +0200 | [diff] [blame] | 647 | |
Martijn Coenen | 773609e | 2017-10-24 09:57:53 +0200 | [diff] [blame] | 648 | const sp<IServiceManager1_1> sm = defaultServiceManager1_1(); |
Martijn Coenen | 86c3abb | 2017-10-24 09:33:28 +0200 | [diff] [blame] | 649 | if (sm == nullptr) { |
| 650 | ALOGE("getService: defaultServiceManager() is null"); |
| 651 | return nullptr; |
| 652 | } |
| 653 | |
| 654 | Return<Transport> transportRet = sm->getTransport(descriptor, instance); |
| 655 | |
| 656 | if (!transportRet.isOk()) { |
| 657 | ALOGE("getService: defaultServiceManager()->getTransport returns %s", |
| 658 | transportRet.description().c_str()); |
| 659 | return nullptr; |
| 660 | } |
| 661 | Transport transport = transportRet; |
| 662 | const bool vintfHwbinder = (transport == Transport::HWBINDER); |
| 663 | const bool vintfPassthru = (transport == Transport::PASSTHROUGH); |
| 664 | |
Steven Moreland | 757394b | 2017-12-13 14:09:24 -0800 | [diff] [blame] | 665 | #ifdef ENFORCE_VINTF_MANIFEST |
Martijn Coenen | 86c3abb | 2017-10-24 09:33:28 +0200 | [diff] [blame] | 666 | |
| 667 | #ifdef LIBHIDL_TARGET_DEBUGGABLE |
| 668 | const char* env = std::getenv("TREBLE_TESTING_OVERRIDE"); |
| 669 | const bool trebleTestingOverride = env && !strcmp(env, "true"); |
| 670 | const bool vintfLegacy = (transport == Transport::EMPTY) && trebleTestingOverride; |
Steven Moreland | 757394b | 2017-12-13 14:09:24 -0800 | [diff] [blame] | 671 | #else // ENFORCE_VINTF_MANIFEST but not LIBHIDL_TARGET_DEBUGGABLE |
Martijn Coenen | 86c3abb | 2017-10-24 09:33:28 +0200 | [diff] [blame] | 672 | const bool trebleTestingOverride = false; |
| 673 | const bool vintfLegacy = false; |
| 674 | #endif // LIBHIDL_TARGET_DEBUGGABLE |
| 675 | |
Steven Moreland | 757394b | 2017-12-13 14:09:24 -0800 | [diff] [blame] | 676 | #else // not ENFORCE_VINTF_MANIFEST |
Martijn Coenen | 86c3abb | 2017-10-24 09:33:28 +0200 | [diff] [blame] | 677 | const char* env = std::getenv("TREBLE_TESTING_OVERRIDE"); |
| 678 | const bool trebleTestingOverride = env && !strcmp(env, "true"); |
| 679 | const bool vintfLegacy = (transport == Transport::EMPTY); |
Steven Moreland | 757394b | 2017-12-13 14:09:24 -0800 | [diff] [blame] | 680 | #endif // ENFORCE_VINTF_MANIFEST |
Martijn Coenen | 86c3abb | 2017-10-24 09:33:28 +0200 | [diff] [blame] | 681 | |
Steven Moreland | 3ae9bf9 | 2018-04-19 17:11:39 -0700 | [diff] [blame] | 682 | for (int tries = 0; !getStub && (vintfHwbinder || vintfLegacy); tries++) { |
| 683 | if (waiter == nullptr && tries > 0) { |
Martijn Coenen | 0e6f8ba | 2018-03-07 09:51:01 +0100 | [diff] [blame] | 684 | waiter = new Waiter(descriptor, instance, sm); |
| 685 | } |
Steven Moreland | 3ae9bf9 | 2018-04-19 17:11:39 -0700 | [diff] [blame] | 686 | if (waiter != nullptr) { |
| 687 | waiter->reset(); // don't reorder this -- see comments on reset() |
| 688 | } |
Martijn Coenen | 86c3abb | 2017-10-24 09:33:28 +0200 | [diff] [blame] | 689 | Return<sp<IBase>> ret = sm->get(descriptor, instance); |
| 690 | if (!ret.isOk()) { |
| 691 | ALOGE("getService: defaultServiceManager()->get returns %s for %s/%s.", |
| 692 | ret.description().c_str(), descriptor.c_str(), instance.c_str()); |
| 693 | break; |
| 694 | } |
| 695 | sp<IBase> base = ret; |
Martijn Coenen | 773609e | 2017-10-24 09:57:53 +0200 | [diff] [blame] | 696 | if (base != nullptr) { |
| 697 | Return<bool> canCastRet = |
| 698 | details::canCastInterface(base.get(), descriptor.c_str(), true /* emitError */); |
| 699 | |
| 700 | if (canCastRet.isOk() && canCastRet) { |
Steven Moreland | 3ae9bf9 | 2018-04-19 17:11:39 -0700 | [diff] [blame] | 701 | if (waiter != nullptr) { |
| 702 | waiter->done(); |
| 703 | } |
Martijn Coenen | 773609e | 2017-10-24 09:57:53 +0200 | [diff] [blame] | 704 | return base; // still needs to be wrapped by Bp class. |
Martijn Coenen | 86c3abb | 2017-10-24 09:33:28 +0200 | [diff] [blame] | 705 | } |
Martijn Coenen | 773609e | 2017-10-24 09:57:53 +0200 | [diff] [blame] | 706 | |
| 707 | if (!handleCastError(canCastRet, descriptor, instance)) break; |
Martijn Coenen | 86c3abb | 2017-10-24 09:33:28 +0200 | [diff] [blame] | 708 | } |
| 709 | |
Martijn Coenen | 773609e | 2017-10-24 09:57:53 +0200 | [diff] [blame] | 710 | // In case of legacy or we were not asked to retry, don't. |
| 711 | if (vintfLegacy || !retry) break; |
Martijn Coenen | 86c3abb | 2017-10-24 09:33:28 +0200 | [diff] [blame] | 712 | |
Steven Moreland | 3ae9bf9 | 2018-04-19 17:11:39 -0700 | [diff] [blame] | 713 | if (waiter != nullptr) { |
| 714 | ALOGI("getService: Trying again for %s/%s...", descriptor.c_str(), instance.c_str()); |
Steven Moreland | 0771d8a | 2018-05-09 13:29:14 -0700 | [diff] [blame] | 715 | waiter->wait(true /* timeout */); |
Steven Moreland | 3ae9bf9 | 2018-04-19 17:11:39 -0700 | [diff] [blame] | 716 | } |
Martijn Coenen | 86c3abb | 2017-10-24 09:33:28 +0200 | [diff] [blame] | 717 | } |
| 718 | |
Martijn Coenen | 0e6f8ba | 2018-03-07 09:51:01 +0100 | [diff] [blame] | 719 | if (waiter != nullptr) { |
| 720 | waiter->done(); |
| 721 | } |
Martijn Coenen | 3fa15c2 | 2018-02-10 11:30:00 +0100 | [diff] [blame] | 722 | |
Martijn Coenen | 86c3abb | 2017-10-24 09:33:28 +0200 | [diff] [blame] | 723 | if (getStub || vintfPassthru || vintfLegacy) { |
| 724 | const sp<IServiceManager> pm = getPassthroughServiceManager(); |
| 725 | if (pm != nullptr) { |
| 726 | sp<IBase> base = pm->get(descriptor, instance).withDefault(nullptr); |
| 727 | if (!getStub || trebleTestingOverride) { |
| 728 | base = wrapPassthrough(base); |
| 729 | } |
| 730 | return base; |
| 731 | } |
| 732 | } |
| 733 | |
| 734 | return nullptr; |
| 735 | } |
| 736 | |
Bernhard Rosenkränzer | 0c28fd2 | 2018-06-04 16:01:47 +0200 | [diff] [blame^] | 737 | } // namespace details |
Steven Moreland | cbefd35 | 2017-01-23 20:29:05 -0800 | [diff] [blame] | 738 | |
Bernhard Rosenkränzer | 0c28fd2 | 2018-06-04 16:01:47 +0200 | [diff] [blame^] | 739 | } // namespace hardware |
| 740 | } // namespace android |