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 | 2a2678e | 2017-07-21 18:07:38 -0700 | [diff] [blame] | 44 | #include <android/hidl/manager/1.1/IServiceManager.h> |
| 45 | #include <android/hidl/manager/1.1/BpHwServiceManager.h> |
| 46 | #include <android/hidl/manager/1.1/BnHwServiceManager.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 | 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); |
Stephen Hines | fd9ecee | 2017-09-27 18:52:52 -0700 | [diff] [blame] | 145 | if (details::gDefaultServiceManager != nullptr) { |
Yifan Hong | 953e6b0 | 2017-03-16 14:52:40 -0700 | [diff] [blame] | 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 | |
Stephen Hines | fd9ecee | 2017-09-27 18:52:52 -0700 | [diff] [blame] | 157 | while (details::gDefaultServiceManager == nullptr) { |
Yifan Hong | 953e6b0 | 2017-03-16 14:52:40 -0700 | [diff] [blame] | 158 | details::gDefaultServiceManager = |
Steven Moreland | 2a2678e | 2017-07-21 18:07:38 -0700 | [diff] [blame] | 159 | fromBinder<IServiceManager1_1, BpHwServiceManager, BnHwServiceManager>( |
Stephen Hines | fd9ecee | 2017-09-27 18:52:52 -0700 | [diff] [blame] | 160 | ProcessState::self()->getContextObject(nullptr)); |
| 161 | if (details::gDefaultServiceManager == nullptr) { |
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) { |
Stephen Hines | fd9ecee | 2017-09-27 18:52:52 -0700 | [diff] [blame] | 231 | pid_t pid = strtoll(dp->d_name, nullptr, 0); |
Yifan Hong | bd0d8f7 | 2017-05-24 19:43:51 -0700 | [diff] [blame] | 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 | |
Steven Moreland | 77f4c85 | 2017-10-12 11:05:53 -0700 | [diff] [blame] | 279 | constexpr int dlMode = RTLD_LAZY; |
| 280 | void* handle = nullptr; |
Steven Moreland | 337e6b6 | 2017-01-18 17:25:13 -0800 | [diff] [blame] | 281 | |
Steven Moreland | a29905c | 2017-03-01 10:42:35 -0800 | [diff] [blame] | 282 | dlerror(); // clear |
| 283 | |
Justin Yun | 1f04810 | 2017-12-01 15:30:08 +0900 | [diff] [blame] | 284 | static std::string halLibPathVndkSp = android::base::StringPrintf( |
| 285 | HAL_LIBRARY_PATH_VNDK_SP_FOR_VERSION, details::getVndkVersionStr().c_str()); |
Steven Moreland | f7dea69 | 2017-07-14 12:49:28 -0700 | [diff] [blame] | 286 | 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] | 287 | halLibPathVndkSp, HAL_LIBRARY_PATH_SYSTEM}; |
Steven Moreland | 77f4c85 | 2017-10-12 11:05:53 -0700 | [diff] [blame] | 288 | |
Steven Moreland | f7dea69 | 2017-07-14 12:49:28 -0700 | [diff] [blame] | 289 | #ifdef LIBHIDL_TARGET_DEBUGGABLE |
| 290 | const char* env = std::getenv("TREBLE_TESTING_OVERRIDE"); |
| 291 | const bool trebleTestingOverride = env && !strcmp(env, "true"); |
| 292 | if (trebleTestingOverride) { |
Steven Moreland | 77f4c85 | 2017-10-12 11:05:53 -0700 | [diff] [blame] | 293 | // Load HAL implementations that are statically linked |
| 294 | handle = dlopen(nullptr, dlMode); |
| 295 | if (handle == nullptr) { |
| 296 | const char* error = dlerror(); |
| 297 | LOG(ERROR) << "Failed to dlopen self: " |
| 298 | << (error == nullptr ? "unknown error" : error); |
| 299 | } else if (!eachLib(handle, "SELF", sym)) { |
| 300 | return; |
| 301 | } |
| 302 | |
Steven Moreland | f7dea69 | 2017-07-14 12:49:28 -0700 | [diff] [blame] | 303 | const char* vtsRootPath = std::getenv("VTS_ROOT_PATH"); |
| 304 | if (vtsRootPath && strlen(vtsRootPath) > 0) { |
| 305 | const std::string halLibraryPathVtsOverride = |
| 306 | std::string(vtsRootPath) + HAL_LIBRARY_PATH_SYSTEM; |
Steven Moreland | 77f4c85 | 2017-10-12 11:05:53 -0700 | [diff] [blame] | 307 | paths.insert(paths.begin(), halLibraryPathVtsOverride); |
Steven Moreland | f7dea69 | 2017-07-14 12:49:28 -0700 | [diff] [blame] | 308 | } |
| 309 | } |
| 310 | #endif |
Steven Moreland | 77f4c85 | 2017-10-12 11:05:53 -0700 | [diff] [blame] | 311 | |
Steven Moreland | f7dea69 | 2017-07-14 12:49:28 -0700 | [diff] [blame] | 312 | for (const std::string& path : paths) { |
Steven Moreland | 0091c09 | 2017-01-20 23:15:18 +0000 | [diff] [blame] | 313 | std::vector<std::string> libs = search(path, prefix, ".so"); |
| 314 | |
Steven Moreland | 0091c09 | 2017-01-20 23:15:18 +0000 | [diff] [blame] | 315 | for (const std::string &lib : libs) { |
Steven Moreland | 348802d | 2017-02-23 12:48:55 -0800 | [diff] [blame] | 316 | const std::string fullPath = path + lib; |
| 317 | |
Steven Moreland | 65c00cb | 2017-10-12 11:35:22 -0700 | [diff] [blame] | 318 | if (path == HAL_LIBRARY_PATH_SYSTEM) { |
Jiyong Park | 3ab546c | 2017-04-06 20:28:07 +0900 | [diff] [blame] | 319 | handle = dlopen(fullPath.c_str(), dlMode); |
Steven Moreland | 65c00cb | 2017-10-12 11:35:22 -0700 | [diff] [blame] | 320 | } else { |
| 321 | handle = android_load_sphal_library(fullPath.c_str(), dlMode); |
Jiyong Park | 3ab546c | 2017-04-06 20:28:07 +0900 | [diff] [blame] | 322 | } |
| 323 | |
Steven Moreland | 348802d | 2017-02-23 12:48:55 -0800 | [diff] [blame] | 324 | if (handle == nullptr) { |
| 325 | const char* error = dlerror(); |
| 326 | LOG(ERROR) << "Failed to dlopen " << lib << ": " |
| 327 | << (error == nullptr ? "unknown error" : error); |
| 328 | continue; |
Steven Moreland | 0091c09 | 2017-01-20 23:15:18 +0000 | [diff] [blame] | 329 | } |
Steven Moreland | 348802d | 2017-02-23 12:48:55 -0800 | [diff] [blame] | 330 | |
Steven Moreland | 519306f | 2017-06-06 17:14:12 -0700 | [diff] [blame] | 331 | if (!eachLib(handle, lib, sym)) { |
| 332 | return; |
Steven Moreland | 348802d | 2017-02-23 12:48:55 -0800 | [diff] [blame] | 333 | } |
Steven Moreland | 337e6b6 | 2017-01-18 17:25:13 -0800 | [diff] [blame] | 334 | } |
| 335 | } |
Steven Moreland | 519306f | 2017-06-06 17:14:12 -0700 | [diff] [blame] | 336 | } |
Steven Moreland | 337e6b6 | 2017-01-18 17:25:13 -0800 | [diff] [blame] | 337 | |
Steven Moreland | 519306f | 2017-06-06 17:14:12 -0700 | [diff] [blame] | 338 | Return<sp<IBase>> get(const hidl_string& fqName, |
| 339 | const hidl_string& name) override { |
| 340 | sp<IBase> ret = nullptr; |
| 341 | |
| 342 | openLibs(fqName, [&](void* handle, const std::string &lib, const std::string &sym) { |
| 343 | IBase* (*generator)(const char* name); |
| 344 | *(void **)(&generator) = dlsym(handle, sym.c_str()); |
| 345 | if(!generator) { |
| 346 | const char* error = dlerror(); |
| 347 | LOG(ERROR) << "Passthrough lookup opened " << lib |
| 348 | << " but could not find symbol " << sym << ": " |
| 349 | << (error == nullptr ? "unknown error" : error); |
| 350 | dlclose(handle); |
| 351 | return true; |
| 352 | } |
| 353 | |
| 354 | ret = (*generator)(name.c_str()); |
| 355 | |
| 356 | if (ret == nullptr) { |
| 357 | dlclose(handle); |
| 358 | return true; // this module doesn't provide this instance name |
| 359 | } |
| 360 | |
Steven Moreland | 83cb4b3 | 2018-03-14 10:55:42 -0700 | [diff] [blame] | 361 | // Actual fqname might be a subclass. |
| 362 | // This assumption is tested in vts_treble_vintf_test |
| 363 | using ::android::hardware::details::getDescriptor; |
| 364 | std::string actualFqName = getDescriptor(ret.get()); |
| 365 | CHECK(actualFqName.size() > 0); |
| 366 | registerReference(actualFqName, name); |
Steven Moreland | 519306f | 2017-06-06 17:14:12 -0700 | [diff] [blame] | 367 | return false; |
| 368 | }); |
| 369 | |
| 370 | return ret; |
Steven Moreland | 337e6b6 | 2017-01-18 17:25:13 -0800 | [diff] [blame] | 371 | } |
| 372 | |
Martijn Coenen | 67a0249 | 2017-03-06 13:04:48 +0100 | [diff] [blame] | 373 | Return<bool> add(const hidl_string& /* name */, |
Steven Moreland | 337e6b6 | 2017-01-18 17:25:13 -0800 | [diff] [blame] | 374 | const sp<IBase>& /* service */) override { |
| 375 | LOG(FATAL) << "Cannot register services with passthrough service manager."; |
| 376 | return false; |
| 377 | } |
| 378 | |
Steven Moreland | c601c5f | 2017-04-06 09:26:07 -0700 | [diff] [blame] | 379 | Return<Transport> getTransport(const hidl_string& /* fqName */, |
| 380 | const hidl_string& /* name */) { |
| 381 | LOG(FATAL) << "Cannot getTransport with passthrough service manager."; |
| 382 | return Transport::EMPTY; |
| 383 | } |
| 384 | |
Yifan Hong | 705e5da | 2017-03-02 16:59:39 -0800 | [diff] [blame] | 385 | Return<void> list(list_cb /* _hidl_cb */) override { |
| 386 | LOG(FATAL) << "Cannot list services with passthrough service manager."; |
Steven Moreland | 337e6b6 | 2017-01-18 17:25:13 -0800 | [diff] [blame] | 387 | return Void(); |
| 388 | } |
| 389 | Return<void> listByInterface(const hidl_string& /* fqInstanceName */, |
| 390 | listByInterface_cb /* _hidl_cb */) override { |
| 391 | // TODO: add this functionality |
| 392 | LOG(FATAL) << "Cannot list services with passthrough service manager."; |
| 393 | return Void(); |
| 394 | } |
| 395 | |
| 396 | Return<bool> registerForNotifications(const hidl_string& /* fqName */, |
| 397 | const hidl_string& /* name */, |
| 398 | const sp<IServiceNotification>& /* callback */) override { |
| 399 | // This makes no sense. |
| 400 | LOG(FATAL) << "Cannot register for notifications with passthrough service manager."; |
| 401 | return false; |
| 402 | } |
| 403 | |
Yifan Hong | 705e5da | 2017-03-02 16:59:39 -0800 | [diff] [blame] | 404 | Return<void> debugDump(debugDump_cb _hidl_cb) override { |
| 405 | using Arch = ::android::hidl::base::V1_0::DebugInfo::Architecture; |
Yifan Hong | bd0d8f7 | 2017-05-24 19:43:51 -0700 | [diff] [blame] | 406 | using std::literals::string_literals::operator""s; |
Justin Yun | 1f04810 | 2017-12-01 15:30:08 +0900 | [diff] [blame] | 407 | static std::string halLibPathVndkSp64 = android::base::StringPrintf( |
| 408 | HAL_LIBRARY_PATH_VNDK_SP_64BIT_FOR_VERSION, details::getVndkVersionStr().c_str()); |
| 409 | static std::string halLibPathVndkSp32 = android::base::StringPrintf( |
| 410 | HAL_LIBRARY_PATH_VNDK_SP_32BIT_FOR_VERSION, details::getVndkVersionStr().c_str()); |
Jiyong Park | 56eb149 | 2017-08-04 16:16:13 +0900 | [diff] [blame] | 411 | static std::vector<std::pair<Arch, std::vector<const char*>>> sAllPaths{ |
| 412 | {Arch::IS_64BIT, |
| 413 | {HAL_LIBRARY_PATH_ODM_64BIT, HAL_LIBRARY_PATH_VENDOR_64BIT, |
Justin Yun | 1f04810 | 2017-12-01 15:30:08 +0900 | [diff] [blame] | 414 | halLibPathVndkSp64.c_str(), HAL_LIBRARY_PATH_SYSTEM_64BIT}}, |
Jiyong Park | 56eb149 | 2017-08-04 16:16:13 +0900 | [diff] [blame] | 415 | {Arch::IS_32BIT, |
| 416 | {HAL_LIBRARY_PATH_ODM_32BIT, HAL_LIBRARY_PATH_VENDOR_32BIT, |
Justin Yun | 1f04810 | 2017-12-01 15:30:08 +0900 | [diff] [blame] | 417 | halLibPathVndkSp32.c_str(), HAL_LIBRARY_PATH_SYSTEM_32BIT}}}; |
Yifan Hong | bd0d8f7 | 2017-05-24 19:43:51 -0700 | [diff] [blame] | 418 | std::map<std::string, InstanceDebugInfo> map; |
Yifan Hong | 705e5da | 2017-03-02 16:59:39 -0800 | [diff] [blame] | 419 | for (const auto &pair : sAllPaths) { |
| 420 | Arch arch = pair.first; |
| 421 | for (const auto &path : pair.second) { |
| 422 | std::vector<std::string> libs = search(path, "", ".so"); |
| 423 | for (const std::string &lib : libs) { |
| 424 | std::string matchedName; |
Yifan Hong | bd0d8f7 | 2017-05-24 19:43:51 -0700 | [diff] [blame] | 425 | std::string implName; |
| 426 | if (matchPackageName(lib, &matchedName, &implName)) { |
| 427 | std::string instanceName{"* ("s + path + ")"s}; |
| 428 | if (!implName.empty()) instanceName += " ("s + implName + ")"s; |
| 429 | map.emplace(path + lib, InstanceDebugInfo{.interfaceName = matchedName, |
| 430 | .instanceName = instanceName, |
| 431 | .clientPids = {}, |
| 432 | .arch = arch}); |
Yifan Hong | 705e5da | 2017-03-02 16:59:39 -0800 | [diff] [blame] | 433 | } |
| 434 | } |
| 435 | } |
| 436 | } |
Yifan Hong | bd0d8f7 | 2017-05-24 19:43:51 -0700 | [diff] [blame] | 437 | fetchPidsForPassthroughLibraries(&map); |
| 438 | hidl_vec<InstanceDebugInfo> vec; |
| 439 | vec.resize(map.size()); |
| 440 | size_t idx = 0; |
| 441 | for (auto&& pair : map) { |
| 442 | vec[idx++] = std::move(pair.second); |
| 443 | } |
Yifan Hong | 705e5da | 2017-03-02 16:59:39 -0800 | [diff] [blame] | 444 | _hidl_cb(vec); |
Yifan Hong | 7f49f59 | 2017-02-03 15:11:44 -0800 | [diff] [blame] | 445 | return Void(); |
| 446 | } |
| 447 | |
Martijn Coenen | af4aba5 | 2017-04-27 09:41:13 -0700 | [diff] [blame] | 448 | Return<void> registerPassthroughClient(const hidl_string &, const hidl_string &) override { |
Yifan Hong | 7f49f59 | 2017-02-03 15:11:44 -0800 | [diff] [blame] | 449 | // This makes no sense. |
| 450 | LOG(FATAL) << "Cannot call registerPassthroughClient on passthrough service manager. " |
| 451 | << "Call it on defaultServiceManager() instead."; |
Yifan Hong | 9a22d1d | 2017-01-25 14:19:26 -0800 | [diff] [blame] | 452 | return Void(); |
| 453 | } |
| 454 | |
Steven Moreland | 2a2678e | 2017-07-21 18:07:38 -0700 | [diff] [blame] | 455 | Return<bool> unregisterForNotifications(const hidl_string& /* fqName */, |
| 456 | const hidl_string& /* name */, |
| 457 | const sp<IServiceNotification>& /* callback */) override { |
| 458 | // This makes no sense. |
| 459 | LOG(FATAL) << "Cannot unregister for notifications with passthrough service manager."; |
| 460 | return false; |
| 461 | } |
| 462 | |
Steven Moreland | 337e6b6 | 2017-01-18 17:25:13 -0800 | [diff] [blame] | 463 | }; |
| 464 | |
Steven Moreland | 2a2678e | 2017-07-21 18:07:38 -0700 | [diff] [blame] | 465 | sp<IServiceManager1_0> getPassthroughServiceManager() { |
| 466 | return getPassthroughServiceManager1_1(); |
| 467 | } |
| 468 | sp<IServiceManager1_1> getPassthroughServiceManager1_1() { |
Steven Moreland | 337e6b6 | 2017-01-18 17:25:13 -0800 | [diff] [blame] | 469 | static sp<PassthroughServiceManager> manager(new PassthroughServiceManager()); |
| 470 | return manager; |
| 471 | } |
| 472 | |
Steven Moreland | cbefd35 | 2017-01-23 20:29:05 -0800 | [diff] [blame] | 473 | namespace details { |
| 474 | |
Steven Moreland | 519306f | 2017-06-06 17:14:12 -0700 | [diff] [blame] | 475 | void preloadPassthroughService(const std::string &descriptor) { |
| 476 | PassthroughServiceManager::openLibs(descriptor, |
| 477 | [&](void* /* handle */, const std::string& /* lib */, const std::string& /* sym */) { |
| 478 | // do nothing |
| 479 | return true; // open all libs |
| 480 | }); |
| 481 | } |
| 482 | |
Steven Moreland | cbefd35 | 2017-01-23 20:29:05 -0800 | [diff] [blame] | 483 | struct Waiter : IServiceNotification { |
Martijn Coenen | 773609e | 2017-10-24 09:57:53 +0200 | [diff] [blame] | 484 | Waiter(const std::string& interface, const std::string& instanceName, |
| 485 | const sp<IServiceManager1_1>& sm) : mInterfaceName(interface), |
| 486 | mInstanceName(instanceName), mSm(sm) { |
Martijn Coenen | e6cdfd3 | 2017-11-13 14:03:05 +0100 | [diff] [blame] | 487 | } |
| 488 | |
| 489 | void onFirstRef() override { |
Martijn Coenen | b88ae7f | 2017-10-24 11:45:41 +0200 | [diff] [blame] | 490 | // If this process only has one binder thread, and we're calling wait() from |
| 491 | // that thread, it will block forever because we hung up the one and only |
| 492 | // binder thread on a condition variable that can only be notified by an |
| 493 | // incoming binder call. |
Tobias Lindskog | 88e886f | 2018-01-05 10:32:43 +0100 | [diff] [blame] | 494 | if (IPCThreadState::self()->isOnlyBinderThread()) { |
Martijn Coenen | b88ae7f | 2017-10-24 11:45:41 +0200 | [diff] [blame] | 495 | LOG(WARNING) << "Can't efficiently wait for " << mInterfaceName << "/" |
| 496 | << mInstanceName << ", because we are called from " |
| 497 | << "the only binder thread in this process."; |
| 498 | return; |
| 499 | } |
| 500 | |
Martijn Coenen | e6cdfd3 | 2017-11-13 14:03:05 +0100 | [diff] [blame] | 501 | Return<bool> ret = mSm->registerForNotifications(mInterfaceName, mInstanceName, this); |
Martijn Coenen | 773609e | 2017-10-24 09:57:53 +0200 | [diff] [blame] | 502 | |
| 503 | if (!ret.isOk()) { |
| 504 | LOG(ERROR) << "Transport error, " << ret.description() |
Martijn Coenen | e6cdfd3 | 2017-11-13 14:03:05 +0100 | [diff] [blame] | 505 | << ", during notification registration for " << mInterfaceName << "/" |
| 506 | << mInstanceName << "."; |
Martijn Coenen | 773609e | 2017-10-24 09:57:53 +0200 | [diff] [blame] | 507 | return; |
| 508 | } |
| 509 | |
| 510 | if (!ret) { |
Martijn Coenen | e6cdfd3 | 2017-11-13 14:03:05 +0100 | [diff] [blame] | 511 | LOG(ERROR) << "Could not register for notifications for " << mInterfaceName << "/" |
| 512 | << mInstanceName << "."; |
Martijn Coenen | 773609e | 2017-10-24 09:57:53 +0200 | [diff] [blame] | 513 | return; |
| 514 | } |
| 515 | |
| 516 | mRegisteredForNotifications = true; |
| 517 | } |
| 518 | |
| 519 | ~Waiter() { |
Martijn Coenen | 3fa15c2 | 2018-02-10 11:30:00 +0100 | [diff] [blame] | 520 | if (!mDoneCalled) { |
| 521 | LOG(FATAL) |
| 522 | << "Waiter still registered for notifications, call done() before dropping ref!"; |
Martijn Coenen | 773609e | 2017-10-24 09:57:53 +0200 | [diff] [blame] | 523 | } |
| 524 | } |
| 525 | |
Steven Moreland | cbefd35 | 2017-01-23 20:29:05 -0800 | [diff] [blame] | 526 | Return<void> onRegistration(const hidl_string& /* fqName */, |
| 527 | const hidl_string& /* name */, |
| 528 | bool /* preexisting */) override { |
| 529 | std::unique_lock<std::mutex> lock(mMutex); |
| 530 | if (mRegistered) { |
| 531 | return Void(); |
| 532 | } |
| 533 | mRegistered = true; |
| 534 | lock.unlock(); |
| 535 | |
| 536 | mCondition.notify_one(); |
| 537 | return Void(); |
| 538 | } |
| 539 | |
Steven Moreland | 0771d8a | 2018-05-09 13:29:14 -0700 | [diff] [blame^] | 540 | void wait(bool timeout) { |
Steven Moreland | 4960510 | 2017-03-28 09:33:06 -0700 | [diff] [blame] | 541 | using std::literals::chrono_literals::operator""s; |
| 542 | |
Martijn Coenen | 773609e | 2017-10-24 09:57:53 +0200 | [diff] [blame] | 543 | if (!mRegisteredForNotifications) { |
| 544 | // As an alternative, just sleep for a second and return |
| 545 | LOG(WARNING) << "Waiting one second for " << mInterfaceName << "/" << mInstanceName; |
| 546 | sleep(1); |
| 547 | return; |
| 548 | } |
| 549 | |
Steven Moreland | cbefd35 | 2017-01-23 20:29:05 -0800 | [diff] [blame] | 550 | std::unique_lock<std::mutex> lock(mMutex); |
Steven Moreland | 0771d8a | 2018-05-09 13:29:14 -0700 | [diff] [blame^] | 551 | do { |
Steven Moreland | 4960510 | 2017-03-28 09:33:06 -0700 | [diff] [blame] | 552 | mCondition.wait_for(lock, 1s, [this]{ |
| 553 | return mRegistered; |
| 554 | }); |
| 555 | |
| 556 | if (mRegistered) { |
| 557 | break; |
| 558 | } |
| 559 | |
Steven Moreland | 0771d8a | 2018-05-09 13:29:14 -0700 | [diff] [blame^] | 560 | LOG(WARNING) << "Waited one second for " << mInterfaceName << "/" << mInstanceName; |
| 561 | } while (!timeout); |
Steven Moreland | cbefd35 | 2017-01-23 20:29:05 -0800 | [diff] [blame] | 562 | } |
| 563 | |
Martijn Coenen | 773609e | 2017-10-24 09:57:53 +0200 | [diff] [blame] | 564 | // Be careful when using this; after calling reset(), you must always try to retrieve |
| 565 | // the corresponding service before blocking on the waiter; otherwise, you might run |
| 566 | // into a race-condition where the service has just (re-)registered, you clear the state |
| 567 | // here, and subsequently calling waiter->wait() will block forever. |
| 568 | void reset() { |
| 569 | std::unique_lock<std::mutex> lock(mMutex); |
| 570 | mRegistered = false; |
| 571 | } |
Martijn Coenen | 3fa15c2 | 2018-02-10 11:30:00 +0100 | [diff] [blame] | 572 | |
| 573 | // done() must be called before dropping the last strong ref to the Waiter, to make |
| 574 | // sure we can properly unregister with hwservicemanager. |
| 575 | void done() { |
| 576 | if (mRegisteredForNotifications) { |
| 577 | if (!mSm->unregisterForNotifications(mInterfaceName, mInstanceName, this) |
| 578 | .withDefault(false)) { |
| 579 | LOG(ERROR) << "Could not unregister service notification for " << mInterfaceName |
| 580 | << "/" << mInstanceName << "."; |
| 581 | } else { |
| 582 | mRegisteredForNotifications = false; |
| 583 | } |
| 584 | } |
| 585 | mDoneCalled = true; |
| 586 | } |
| 587 | |
| 588 | private: |
Martijn Coenen | 773609e | 2017-10-24 09:57:53 +0200 | [diff] [blame] | 589 | const std::string mInterfaceName; |
| 590 | const std::string mInstanceName; |
| 591 | const sp<IServiceManager1_1>& mSm; |
Steven Moreland | cbefd35 | 2017-01-23 20:29:05 -0800 | [diff] [blame] | 592 | std::mutex mMutex; |
| 593 | std::condition_variable mCondition; |
| 594 | bool mRegistered = false; |
Martijn Coenen | 773609e | 2017-10-24 09:57:53 +0200 | [diff] [blame] | 595 | bool mRegisteredForNotifications = false; |
Martijn Coenen | 3fa15c2 | 2018-02-10 11:30:00 +0100 | [diff] [blame] | 596 | bool mDoneCalled = false; |
Steven Moreland | cbefd35 | 2017-01-23 20:29:05 -0800 | [diff] [blame] | 597 | }; |
| 598 | |
| 599 | void waitForHwService( |
| 600 | const std::string &interface, const std::string &instanceName) { |
Martijn Coenen | 773609e | 2017-10-24 09:57:53 +0200 | [diff] [blame] | 601 | sp<Waiter> waiter = new Waiter(interface, instanceName, defaultServiceManager1_1()); |
Steven Moreland | 0771d8a | 2018-05-09 13:29:14 -0700 | [diff] [blame^] | 602 | waiter->wait(false /* timeout */); |
Martijn Coenen | 3fa15c2 | 2018-02-10 11:30:00 +0100 | [diff] [blame] | 603 | waiter->done(); |
Martijn Coenen | 773609e | 2017-10-24 09:57:53 +0200 | [diff] [blame] | 604 | } |
Steven Moreland | cbefd35 | 2017-01-23 20:29:05 -0800 | [diff] [blame] | 605 | |
Martijn Coenen | 773609e | 2017-10-24 09:57:53 +0200 | [diff] [blame] | 606 | // Prints relevant error/warning messages for error return values from |
| 607 | // details::canCastInterface(), both transaction errors (!castReturn.isOk()) |
| 608 | // as well as actual cast failures (castReturn.isOk() && castReturn = false). |
| 609 | // Returns 'true' if the error is non-fatal and it's useful to retry |
| 610 | bool handleCastError(const Return<bool>& castReturn, const std::string& descriptor, |
| 611 | const std::string& instance) { |
| 612 | if (castReturn.isOk()) { |
| 613 | if (castReturn) { |
| 614 | details::logAlwaysFatal("Successful cast value passed into handleCastError."); |
| 615 | } |
| 616 | // This should never happen, and there's not really a point in retrying. |
| 617 | ALOGE("getService: received incompatible service (bug in hwservicemanager?) for " |
| 618 | "%s/%s.", descriptor.c_str(), instance.c_str()); |
| 619 | return false; |
Steven Moreland | cbefd35 | 2017-01-23 20:29:05 -0800 | [diff] [blame] | 620 | } |
Martijn Coenen | 773609e | 2017-10-24 09:57:53 +0200 | [diff] [blame] | 621 | if (castReturn.isDeadObject()) { |
| 622 | ALOGW("getService: found dead hwbinder service for %s/%s.", descriptor.c_str(), |
| 623 | instance.c_str()); |
| 624 | return true; |
Steven Moreland | cbefd35 | 2017-01-23 20:29:05 -0800 | [diff] [blame] | 625 | } |
Martijn Coenen | 773609e | 2017-10-24 09:57:53 +0200 | [diff] [blame] | 626 | // This can happen due to: |
| 627 | // 1) No SELinux permissions |
| 628 | // 2) Other transaction failure (no buffer space, kernel error) |
| 629 | // The first isn't recoverable, but the second is. |
| 630 | // Since we can't yet differentiate between the two, and clients depend |
| 631 | // on us not blocking in case 1), treat this as a fatal error for now. |
| 632 | ALOGW("getService: unable to call into hwbinder service for %s/%s.", |
| 633 | descriptor.c_str(), instance.c_str()); |
| 634 | return false; |
Steven Moreland | cbefd35 | 2017-01-23 20:29:05 -0800 | [diff] [blame] | 635 | } |
| 636 | |
Martijn Coenen | 86c3abb | 2017-10-24 09:33:28 +0200 | [diff] [blame] | 637 | sp<::android::hidl::base::V1_0::IBase> getRawServiceInternal(const std::string& descriptor, |
| 638 | const std::string& instance, |
| 639 | bool retry, bool getStub) { |
| 640 | using Transport = ::android::hidl::manager::V1_0::IServiceManager::Transport; |
| 641 | using ::android::hidl::base::V1_0::IBase; |
| 642 | using ::android::hidl::manager::V1_0::IServiceManager; |
Martijn Coenen | 0e6f8ba | 2018-03-07 09:51:01 +0100 | [diff] [blame] | 643 | sp<Waiter> waiter; |
Martijn Coenen | 86c3abb | 2017-10-24 09:33:28 +0200 | [diff] [blame] | 644 | |
Martijn Coenen | 773609e | 2017-10-24 09:57:53 +0200 | [diff] [blame] | 645 | const sp<IServiceManager1_1> sm = defaultServiceManager1_1(); |
Martijn Coenen | 86c3abb | 2017-10-24 09:33:28 +0200 | [diff] [blame] | 646 | if (sm == nullptr) { |
| 647 | ALOGE("getService: defaultServiceManager() is null"); |
| 648 | return nullptr; |
| 649 | } |
| 650 | |
| 651 | Return<Transport> transportRet = sm->getTransport(descriptor, instance); |
| 652 | |
| 653 | if (!transportRet.isOk()) { |
| 654 | ALOGE("getService: defaultServiceManager()->getTransport returns %s", |
| 655 | transportRet.description().c_str()); |
| 656 | return nullptr; |
| 657 | } |
| 658 | Transport transport = transportRet; |
| 659 | const bool vintfHwbinder = (transport == Transport::HWBINDER); |
| 660 | const bool vintfPassthru = (transport == Transport::PASSTHROUGH); |
| 661 | |
Steven Moreland | 757394b | 2017-12-13 14:09:24 -0800 | [diff] [blame] | 662 | #ifdef ENFORCE_VINTF_MANIFEST |
Martijn Coenen | 86c3abb | 2017-10-24 09:33:28 +0200 | [diff] [blame] | 663 | |
| 664 | #ifdef LIBHIDL_TARGET_DEBUGGABLE |
| 665 | const char* env = std::getenv("TREBLE_TESTING_OVERRIDE"); |
| 666 | const bool trebleTestingOverride = env && !strcmp(env, "true"); |
| 667 | const bool vintfLegacy = (transport == Transport::EMPTY) && trebleTestingOverride; |
Steven Moreland | 757394b | 2017-12-13 14:09:24 -0800 | [diff] [blame] | 668 | #else // ENFORCE_VINTF_MANIFEST but not LIBHIDL_TARGET_DEBUGGABLE |
Martijn Coenen | 86c3abb | 2017-10-24 09:33:28 +0200 | [diff] [blame] | 669 | const bool trebleTestingOverride = false; |
| 670 | const bool vintfLegacy = false; |
| 671 | #endif // LIBHIDL_TARGET_DEBUGGABLE |
| 672 | |
Steven Moreland | 757394b | 2017-12-13 14:09:24 -0800 | [diff] [blame] | 673 | #else // not ENFORCE_VINTF_MANIFEST |
Martijn Coenen | 86c3abb | 2017-10-24 09:33:28 +0200 | [diff] [blame] | 674 | const char* env = std::getenv("TREBLE_TESTING_OVERRIDE"); |
| 675 | const bool trebleTestingOverride = env && !strcmp(env, "true"); |
| 676 | const bool vintfLegacy = (transport == Transport::EMPTY); |
Steven Moreland | 757394b | 2017-12-13 14:09:24 -0800 | [diff] [blame] | 677 | #endif // ENFORCE_VINTF_MANIFEST |
Martijn Coenen | 86c3abb | 2017-10-24 09:33:28 +0200 | [diff] [blame] | 678 | |
Steven Moreland | 3ae9bf9 | 2018-04-19 17:11:39 -0700 | [diff] [blame] | 679 | for (int tries = 0; !getStub && (vintfHwbinder || vintfLegacy); tries++) { |
| 680 | if (waiter == nullptr && tries > 0) { |
Martijn Coenen | 0e6f8ba | 2018-03-07 09:51:01 +0100 | [diff] [blame] | 681 | waiter = new Waiter(descriptor, instance, sm); |
| 682 | } |
Steven Moreland | 3ae9bf9 | 2018-04-19 17:11:39 -0700 | [diff] [blame] | 683 | if (waiter != nullptr) { |
| 684 | waiter->reset(); // don't reorder this -- see comments on reset() |
| 685 | } |
Martijn Coenen | 86c3abb | 2017-10-24 09:33:28 +0200 | [diff] [blame] | 686 | Return<sp<IBase>> ret = sm->get(descriptor, instance); |
| 687 | if (!ret.isOk()) { |
| 688 | ALOGE("getService: defaultServiceManager()->get returns %s for %s/%s.", |
| 689 | ret.description().c_str(), descriptor.c_str(), instance.c_str()); |
| 690 | break; |
| 691 | } |
| 692 | sp<IBase> base = ret; |
Martijn Coenen | 773609e | 2017-10-24 09:57:53 +0200 | [diff] [blame] | 693 | if (base != nullptr) { |
| 694 | Return<bool> canCastRet = |
| 695 | details::canCastInterface(base.get(), descriptor.c_str(), true /* emitError */); |
| 696 | |
| 697 | if (canCastRet.isOk() && canCastRet) { |
Steven Moreland | 3ae9bf9 | 2018-04-19 17:11:39 -0700 | [diff] [blame] | 698 | if (waiter != nullptr) { |
| 699 | waiter->done(); |
| 700 | } |
Martijn Coenen | 773609e | 2017-10-24 09:57:53 +0200 | [diff] [blame] | 701 | return base; // still needs to be wrapped by Bp class. |
Martijn Coenen | 86c3abb | 2017-10-24 09:33:28 +0200 | [diff] [blame] | 702 | } |
Martijn Coenen | 773609e | 2017-10-24 09:57:53 +0200 | [diff] [blame] | 703 | |
| 704 | if (!handleCastError(canCastRet, descriptor, instance)) break; |
Martijn Coenen | 86c3abb | 2017-10-24 09:33:28 +0200 | [diff] [blame] | 705 | } |
| 706 | |
Martijn Coenen | 773609e | 2017-10-24 09:57:53 +0200 | [diff] [blame] | 707 | // In case of legacy or we were not asked to retry, don't. |
| 708 | if (vintfLegacy || !retry) break; |
Martijn Coenen | 86c3abb | 2017-10-24 09:33:28 +0200 | [diff] [blame] | 709 | |
Steven Moreland | 3ae9bf9 | 2018-04-19 17:11:39 -0700 | [diff] [blame] | 710 | if (waiter != nullptr) { |
| 711 | 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^] | 712 | waiter->wait(true /* timeout */); |
Steven Moreland | 3ae9bf9 | 2018-04-19 17:11:39 -0700 | [diff] [blame] | 713 | } |
Martijn Coenen | 86c3abb | 2017-10-24 09:33:28 +0200 | [diff] [blame] | 714 | } |
| 715 | |
Martijn Coenen | 0e6f8ba | 2018-03-07 09:51:01 +0100 | [diff] [blame] | 716 | if (waiter != nullptr) { |
| 717 | waiter->done(); |
| 718 | } |
Martijn Coenen | 3fa15c2 | 2018-02-10 11:30:00 +0100 | [diff] [blame] | 719 | |
Martijn Coenen | 86c3abb | 2017-10-24 09:33:28 +0200 | [diff] [blame] | 720 | if (getStub || vintfPassthru || vintfLegacy) { |
| 721 | const sp<IServiceManager> pm = getPassthroughServiceManager(); |
| 722 | if (pm != nullptr) { |
| 723 | sp<IBase> base = pm->get(descriptor, instance).withDefault(nullptr); |
| 724 | if (!getStub || trebleTestingOverride) { |
| 725 | base = wrapPassthrough(base); |
| 726 | } |
| 727 | return base; |
| 728 | } |
| 729 | } |
| 730 | |
| 731 | return nullptr; |
| 732 | } |
| 733 | |
Steven Moreland | cbefd35 | 2017-01-23 20:29:05 -0800 | [diff] [blame] | 734 | }; // namespace details |
| 735 | |
Steven Moreland | 5d5ef7f | 2016-10-20 19:19:55 -0700 | [diff] [blame] | 736 | }; // namespace hardware |
| 737 | }; // namespace android |