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