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