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