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