Steven Moreland | 5d5ef7f | 2016-10-20 19:19:55 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #define LOG_TAG "ServiceManagement" |
| 18 | |
Jiyong Park | 3ab546c | 2017-04-06 20:28:07 +0900 | [diff] [blame] | 19 | #include <android/dlext.h> |
Yifan Hong | 9a22d1d | 2017-01-25 14:19:26 -0800 | [diff] [blame] | 20 | #include <condition_variable> |
| 21 | #include <dlfcn.h> |
| 22 | #include <dirent.h> |
Steven Moreland | 405d761 | 2017-04-07 20:31:22 -0700 | [diff] [blame] | 23 | #include <fstream> |
| 24 | #include <pthread.h> |
Yifan Hong | 9a22d1d | 2017-01-25 14:19:26 -0800 | [diff] [blame] | 25 | #include <unistd.h> |
| 26 | |
| 27 | #include <mutex> |
| 28 | #include <regex> |
Yifan Hong | bd0d8f7 | 2017-05-24 19:43:51 -0700 | [diff] [blame] | 29 | #include <set> |
Yifan Hong | 9a22d1d | 2017-01-25 14:19:26 -0800 | [diff] [blame] | 30 | |
Martijn Coenen | 12f04d9 | 2016-12-07 17:29:41 +0100 | [diff] [blame] | 31 | #include <hidl/HidlBinderSupport.h> |
Justin Yun | 1f04810 | 2017-12-01 15:30:08 +0900 | [diff] [blame] | 32 | #include <hidl/HidlInternal.h> |
Steven Moreland | e69e8d3 | 2018-03-14 10:55:42 -0700 | [diff] [blame] | 33 | #include <hidl/HidlTransportUtils.h> |
Steven Moreland | 5d5ef7f | 2016-10-20 19:19:55 -0700 | [diff] [blame] | 34 | #include <hidl/ServiceManagement.h> |
Steven Moreland | 5d5ef7f | 2016-10-20 19:19:55 -0700 | [diff] [blame] | 35 | #include <hidl/Status.h> |
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 | |
Steven Moreland | 0091c09 | 2017-01-20 23:15:18 +0000 | [diff] [blame] | 241 | std::vector<std::string> search(const std::string &path, |
| 242 | const std::string &prefix, |
| 243 | const std::string &suffix) { |
| 244 | std::unique_ptr<DIR, decltype(&closedir)> dir(opendir(path.c_str()), closedir); |
| 245 | if (!dir) return {}; |
| 246 | |
| 247 | std::vector<std::string> results{}; |
| 248 | |
| 249 | dirent* dp; |
| 250 | while ((dp = readdir(dir.get())) != nullptr) { |
| 251 | std::string name = dp->d_name; |
| 252 | |
Steven Moreland | 8092aa0 | 2018-10-12 15:54:56 -0700 | [diff] [blame^] | 253 | if (base::StartsWith(name, prefix) && base::EndsWith(name, suffix)) { |
Steven Moreland | 0091c09 | 2017-01-20 23:15:18 +0000 | [diff] [blame] | 254 | results.push_back(name); |
| 255 | } |
| 256 | } |
| 257 | |
| 258 | return results; |
| 259 | } |
| 260 | |
Yifan Hong | bd0d8f7 | 2017-05-24 19:43:51 -0700 | [diff] [blame] | 261 | bool matchPackageName(const std::string& lib, std::string* matchedName, std::string* implName) { |
Yifan Hong | 9a22d1d | 2017-01-25 14:19:26 -0800 | [diff] [blame] | 262 | std::smatch match; |
| 263 | if (std::regex_match(lib, match, gLibraryFileNamePattern)) { |
| 264 | *matchedName = match.str(1) + "::I*"; |
Yifan Hong | bd0d8f7 | 2017-05-24 19:43:51 -0700 | [diff] [blame] | 265 | *implName = match.str(2); |
Yifan Hong | 9a22d1d | 2017-01-25 14:19:26 -0800 | [diff] [blame] | 266 | return true; |
| 267 | } |
| 268 | return false; |
| 269 | } |
| 270 | |
Yifan Hong | 7f49f59 | 2017-02-03 15:11:44 -0800 | [diff] [blame] | 271 | static void registerReference(const hidl_string &interfaceName, const hidl_string &instanceName) { |
Yifan Hong | deacf14 | 2018-07-10 17:33:34 -0700 | [diff] [blame] | 272 | if (kIsRecovery) { |
| 273 | // No hwservicemanager in recovery. |
| 274 | return; |
| 275 | } |
| 276 | |
Steven Moreland | 2a2678e | 2017-07-21 18:07:38 -0700 | [diff] [blame] | 277 | sp<IServiceManager1_0> binderizedManager = defaultServiceManager(); |
Yifan Hong | 7f49f59 | 2017-02-03 15:11:44 -0800 | [diff] [blame] | 278 | if (binderizedManager == nullptr) { |
| 279 | LOG(WARNING) << "Could not registerReference for " |
| 280 | << interfaceName << "/" << instanceName |
| 281 | << ": null binderized manager."; |
| 282 | return; |
| 283 | } |
Martijn Coenen | af4aba5 | 2017-04-27 09:41:13 -0700 | [diff] [blame] | 284 | auto ret = binderizedManager->registerPassthroughClient(interfaceName, instanceName); |
Yifan Hong | 7f49f59 | 2017-02-03 15:11:44 -0800 | [diff] [blame] | 285 | if (!ret.isOk()) { |
| 286 | LOG(WARNING) << "Could not registerReference for " |
| 287 | << interfaceName << "/" << instanceName |
| 288 | << ": " << ret.description(); |
Steven Moreland | 0aeaa78 | 2017-03-22 08:11:07 -0700 | [diff] [blame] | 289 | return; |
Yifan Hong | 7f49f59 | 2017-02-03 15:11:44 -0800 | [diff] [blame] | 290 | } |
Steven Moreland | e681aa5 | 2017-02-15 16:22:37 -0800 | [diff] [blame] | 291 | LOG(VERBOSE) << "Successfully registerReference for " |
| 292 | << interfaceName << "/" << instanceName; |
Yifan Hong | 7f49f59 | 2017-02-03 15:11:44 -0800 | [diff] [blame] | 293 | } |
| 294 | |
Yifan Hong | bd0d8f7 | 2017-05-24 19:43:51 -0700 | [diff] [blame] | 295 | using InstanceDebugInfo = hidl::manager::V1_0::IServiceManager::InstanceDebugInfo; |
| 296 | static inline void fetchPidsForPassthroughLibraries( |
| 297 | std::map<std::string, InstanceDebugInfo>* infos) { |
| 298 | static const std::string proc = "/proc/"; |
| 299 | |
| 300 | std::map<std::string, std::set<pid_t>> pids; |
| 301 | std::unique_ptr<DIR, decltype(&closedir)> dir(opendir(proc.c_str()), closedir); |
| 302 | if (!dir) return; |
| 303 | dirent* dp; |
| 304 | while ((dp = readdir(dir.get())) != nullptr) { |
Stephen Hines | fd9ecee | 2017-09-27 18:52:52 -0700 | [diff] [blame] | 305 | pid_t pid = strtoll(dp->d_name, nullptr, 0); |
Yifan Hong | bd0d8f7 | 2017-05-24 19:43:51 -0700 | [diff] [blame] | 306 | if (pid == 0) continue; |
| 307 | std::string mapsPath = proc + dp->d_name + "/maps"; |
| 308 | std::ifstream ifs{mapsPath}; |
| 309 | if (!ifs.is_open()) continue; |
| 310 | |
| 311 | for (std::string line; std::getline(ifs, line);) { |
| 312 | // The last token of line should look like |
| 313 | // vendor/lib64/hw/android.hardware.foo@1.0-impl-extra.so |
| 314 | // Use some simple filters to ignore bad lines before extracting libFileName |
| 315 | // and checking the key in info to make parsing faster. |
| 316 | if (line.back() != 'o') continue; |
| 317 | if (line.rfind('@') == std::string::npos) continue; |
| 318 | |
| 319 | auto spacePos = line.rfind(' '); |
| 320 | if (spacePos == std::string::npos) continue; |
| 321 | auto libFileName = line.substr(spacePos + 1); |
| 322 | auto it = infos->find(libFileName); |
| 323 | if (it == infos->end()) continue; |
| 324 | pids[libFileName].insert(pid); |
| 325 | } |
| 326 | } |
| 327 | for (auto& pair : *infos) { |
| 328 | pair.second.clientPids = |
| 329 | std::vector<pid_t>{pids[pair.first].begin(), pids[pair.first].end()}; |
| 330 | } |
| 331 | } |
| 332 | |
Steven Moreland | 2a2678e | 2017-07-21 18:07:38 -0700 | [diff] [blame] | 333 | struct PassthroughServiceManager : IServiceManager1_1 { |
Chih-Hung Hsieh | 41649d5 | 2017-08-03 14:27:21 -0700 | [diff] [blame] | 334 | static void openLibs( |
| 335 | const std::string& fqName, |
| 336 | const std::function<bool /* continue */ (void* /* handle */, const std::string& /* lib */, |
| 337 | const std::string& /* sym */)>& eachLib) { |
Steven Moreland | 819c05d | 2017-04-06 17:24:22 -0700 | [diff] [blame] | 338 | //fqName looks like android.hardware.foo@1.0::IFoo |
Steven Moreland | 519306f | 2017-06-06 17:14:12 -0700 | [diff] [blame] | 339 | size_t idx = fqName.find("::"); |
Steven Moreland | 819c05d | 2017-04-06 17:24:22 -0700 | [diff] [blame] | 340 | |
| 341 | if (idx == std::string::npos || |
Steven Moreland | 519306f | 2017-06-06 17:14:12 -0700 | [diff] [blame] | 342 | idx + strlen("::") + 1 >= fqName.size()) { |
Steven Moreland | 337e6b6 | 2017-01-18 17:25:13 -0800 | [diff] [blame] | 343 | LOG(ERROR) << "Invalid interface name passthrough lookup: " << fqName; |
Steven Moreland | 519306f | 2017-06-06 17:14:12 -0700 | [diff] [blame] | 344 | return; |
Steven Moreland | 337e6b6 | 2017-01-18 17:25:13 -0800 | [diff] [blame] | 345 | } |
| 346 | |
Steven Moreland | 519306f | 2017-06-06 17:14:12 -0700 | [diff] [blame] | 347 | std::string packageAndVersion = fqName.substr(0, idx); |
| 348 | std::string ifaceName = fqName.substr(idx + strlen("::")); |
Steven Moreland | 819c05d | 2017-04-06 17:24:22 -0700 | [diff] [blame] | 349 | |
| 350 | const std::string prefix = packageAndVersion + "-impl"; |
| 351 | const std::string sym = "HIDL_FETCH_" + ifaceName; |
Steven Moreland | 348802d | 2017-02-23 12:48:55 -0800 | [diff] [blame] | 352 | |
Steven Moreland | 77f4c85 | 2017-10-12 11:05:53 -0700 | [diff] [blame] | 353 | constexpr int dlMode = RTLD_LAZY; |
| 354 | void* handle = nullptr; |
Steven Moreland | 337e6b6 | 2017-01-18 17:25:13 -0800 | [diff] [blame] | 355 | |
Steven Moreland | a29905c | 2017-03-01 10:42:35 -0800 | [diff] [blame] | 356 | dlerror(); // clear |
| 357 | |
Justin Yun | 1f04810 | 2017-12-01 15:30:08 +0900 | [diff] [blame] | 358 | static std::string halLibPathVndkSp = android::base::StringPrintf( |
| 359 | HAL_LIBRARY_PATH_VNDK_SP_FOR_VERSION, details::getVndkVersionStr().c_str()); |
Steven Moreland | f7dea69 | 2017-07-14 12:49:28 -0700 | [diff] [blame] | 360 | std::vector<std::string> paths = {HAL_LIBRARY_PATH_ODM, HAL_LIBRARY_PATH_VENDOR, |
Justin Yun | 1f04810 | 2017-12-01 15:30:08 +0900 | [diff] [blame] | 361 | halLibPathVndkSp, HAL_LIBRARY_PATH_SYSTEM}; |
Steven Moreland | 77f4c85 | 2017-10-12 11:05:53 -0700 | [diff] [blame] | 362 | |
Steven Moreland | f7dea69 | 2017-07-14 12:49:28 -0700 | [diff] [blame] | 363 | #ifdef LIBHIDL_TARGET_DEBUGGABLE |
| 364 | const char* env = std::getenv("TREBLE_TESTING_OVERRIDE"); |
| 365 | const bool trebleTestingOverride = env && !strcmp(env, "true"); |
| 366 | if (trebleTestingOverride) { |
Steven Moreland | 77f4c85 | 2017-10-12 11:05:53 -0700 | [diff] [blame] | 367 | // Load HAL implementations that are statically linked |
| 368 | handle = dlopen(nullptr, dlMode); |
| 369 | if (handle == nullptr) { |
| 370 | const char* error = dlerror(); |
| 371 | LOG(ERROR) << "Failed to dlopen self: " |
| 372 | << (error == nullptr ? "unknown error" : error); |
| 373 | } else if (!eachLib(handle, "SELF", sym)) { |
| 374 | return; |
| 375 | } |
| 376 | |
Steven Moreland | f7dea69 | 2017-07-14 12:49:28 -0700 | [diff] [blame] | 377 | const char* vtsRootPath = std::getenv("VTS_ROOT_PATH"); |
| 378 | if (vtsRootPath && strlen(vtsRootPath) > 0) { |
| 379 | const std::string halLibraryPathVtsOverride = |
| 380 | std::string(vtsRootPath) + HAL_LIBRARY_PATH_SYSTEM; |
Steven Moreland | 77f4c85 | 2017-10-12 11:05:53 -0700 | [diff] [blame] | 381 | paths.insert(paths.begin(), halLibraryPathVtsOverride); |
Steven Moreland | f7dea69 | 2017-07-14 12:49:28 -0700 | [diff] [blame] | 382 | } |
| 383 | } |
| 384 | #endif |
Steven Moreland | 77f4c85 | 2017-10-12 11:05:53 -0700 | [diff] [blame] | 385 | |
Steven Moreland | f7dea69 | 2017-07-14 12:49:28 -0700 | [diff] [blame] | 386 | for (const std::string& path : paths) { |
Steven Moreland | 0091c09 | 2017-01-20 23:15:18 +0000 | [diff] [blame] | 387 | std::vector<std::string> libs = search(path, prefix, ".so"); |
| 388 | |
Steven Moreland | 0091c09 | 2017-01-20 23:15:18 +0000 | [diff] [blame] | 389 | for (const std::string &lib : libs) { |
Steven Moreland | 348802d | 2017-02-23 12:48:55 -0800 | [diff] [blame] | 390 | const std::string fullPath = path + lib; |
| 391 | |
Yifan Hong | deacf14 | 2018-07-10 17:33:34 -0700 | [diff] [blame] | 392 | if (kIsRecovery || path == HAL_LIBRARY_PATH_SYSTEM) { |
Jiyong Park | 3ab546c | 2017-04-06 20:28:07 +0900 | [diff] [blame] | 393 | handle = dlopen(fullPath.c_str(), dlMode); |
Steven Moreland | 65c00cb | 2017-10-12 11:35:22 -0700 | [diff] [blame] | 394 | } else { |
Jerry Zhang | 86ae999 | 2018-05-30 17:11:09 -0700 | [diff] [blame] | 395 | #if !defined(__ANDROID_RECOVERY__) |
Steven Moreland | 65c00cb | 2017-10-12 11:35:22 -0700 | [diff] [blame] | 396 | handle = android_load_sphal_library(fullPath.c_str(), dlMode); |
Jerry Zhang | 86ae999 | 2018-05-30 17:11:09 -0700 | [diff] [blame] | 397 | #endif |
Jiyong Park | 3ab546c | 2017-04-06 20:28:07 +0900 | [diff] [blame] | 398 | } |
| 399 | |
Steven Moreland | 348802d | 2017-02-23 12:48:55 -0800 | [diff] [blame] | 400 | if (handle == nullptr) { |
| 401 | const char* error = dlerror(); |
| 402 | LOG(ERROR) << "Failed to dlopen " << lib << ": " |
| 403 | << (error == nullptr ? "unknown error" : error); |
| 404 | continue; |
Steven Moreland | 0091c09 | 2017-01-20 23:15:18 +0000 | [diff] [blame] | 405 | } |
Steven Moreland | 348802d | 2017-02-23 12:48:55 -0800 | [diff] [blame] | 406 | |
Steven Moreland | 519306f | 2017-06-06 17:14:12 -0700 | [diff] [blame] | 407 | if (!eachLib(handle, lib, sym)) { |
| 408 | return; |
Steven Moreland | 348802d | 2017-02-23 12:48:55 -0800 | [diff] [blame] | 409 | } |
Steven Moreland | 337e6b6 | 2017-01-18 17:25:13 -0800 | [diff] [blame] | 410 | } |
| 411 | } |
Steven Moreland | 519306f | 2017-06-06 17:14:12 -0700 | [diff] [blame] | 412 | } |
Steven Moreland | 337e6b6 | 2017-01-18 17:25:13 -0800 | [diff] [blame] | 413 | |
Steven Moreland | 519306f | 2017-06-06 17:14:12 -0700 | [diff] [blame] | 414 | Return<sp<IBase>> get(const hidl_string& fqName, |
| 415 | const hidl_string& name) override { |
| 416 | sp<IBase> ret = nullptr; |
| 417 | |
| 418 | openLibs(fqName, [&](void* handle, const std::string &lib, const std::string &sym) { |
| 419 | IBase* (*generator)(const char* name); |
| 420 | *(void **)(&generator) = dlsym(handle, sym.c_str()); |
| 421 | if(!generator) { |
| 422 | const char* error = dlerror(); |
| 423 | LOG(ERROR) << "Passthrough lookup opened " << lib |
| 424 | << " but could not find symbol " << sym << ": " |
| 425 | << (error == nullptr ? "unknown error" : error); |
| 426 | dlclose(handle); |
| 427 | return true; |
| 428 | } |
| 429 | |
| 430 | ret = (*generator)(name.c_str()); |
| 431 | |
| 432 | if (ret == nullptr) { |
| 433 | dlclose(handle); |
| 434 | return true; // this module doesn't provide this instance name |
| 435 | } |
| 436 | |
Steven Moreland | e69e8d3 | 2018-03-14 10:55:42 -0700 | [diff] [blame] | 437 | // Actual fqname might be a subclass. |
| 438 | // This assumption is tested in vts_treble_vintf_test |
| 439 | using ::android::hardware::details::getDescriptor; |
| 440 | std::string actualFqName = getDescriptor(ret.get()); |
| 441 | CHECK(actualFqName.size() > 0); |
| 442 | registerReference(actualFqName, name); |
Steven Moreland | 519306f | 2017-06-06 17:14:12 -0700 | [diff] [blame] | 443 | return false; |
| 444 | }); |
| 445 | |
| 446 | return ret; |
Steven Moreland | 337e6b6 | 2017-01-18 17:25:13 -0800 | [diff] [blame] | 447 | } |
| 448 | |
Martijn Coenen | 67a0249 | 2017-03-06 13:04:48 +0100 | [diff] [blame] | 449 | Return<bool> add(const hidl_string& /* name */, |
Steven Moreland | 337e6b6 | 2017-01-18 17:25:13 -0800 | [diff] [blame] | 450 | const sp<IBase>& /* service */) override { |
| 451 | LOG(FATAL) << "Cannot register services with passthrough service manager."; |
| 452 | return false; |
| 453 | } |
| 454 | |
Steven Moreland | c601c5f | 2017-04-06 09:26:07 -0700 | [diff] [blame] | 455 | Return<Transport> getTransport(const hidl_string& /* fqName */, |
| 456 | const hidl_string& /* name */) { |
| 457 | LOG(FATAL) << "Cannot getTransport with passthrough service manager."; |
| 458 | return Transport::EMPTY; |
| 459 | } |
| 460 | |
Yifan Hong | 705e5da | 2017-03-02 16:59:39 -0800 | [diff] [blame] | 461 | Return<void> list(list_cb /* _hidl_cb */) override { |
| 462 | LOG(FATAL) << "Cannot list services with passthrough service manager."; |
Steven Moreland | 337e6b6 | 2017-01-18 17:25:13 -0800 | [diff] [blame] | 463 | return Void(); |
| 464 | } |
| 465 | Return<void> listByInterface(const hidl_string& /* fqInstanceName */, |
| 466 | listByInterface_cb /* _hidl_cb */) override { |
| 467 | // TODO: add this functionality |
| 468 | LOG(FATAL) << "Cannot list services with passthrough service manager."; |
| 469 | return Void(); |
| 470 | } |
| 471 | |
| 472 | Return<bool> registerForNotifications(const hidl_string& /* fqName */, |
| 473 | const hidl_string& /* name */, |
| 474 | const sp<IServiceNotification>& /* callback */) override { |
| 475 | // This makes no sense. |
| 476 | LOG(FATAL) << "Cannot register for notifications with passthrough service manager."; |
| 477 | return false; |
| 478 | } |
| 479 | |
Yifan Hong | 705e5da | 2017-03-02 16:59:39 -0800 | [diff] [blame] | 480 | Return<void> debugDump(debugDump_cb _hidl_cb) override { |
| 481 | using Arch = ::android::hidl::base::V1_0::DebugInfo::Architecture; |
Yifan Hong | bd0d8f7 | 2017-05-24 19:43:51 -0700 | [diff] [blame] | 482 | using std::literals::string_literals::operator""s; |
Justin Yun | 1f04810 | 2017-12-01 15:30:08 +0900 | [diff] [blame] | 483 | static std::string halLibPathVndkSp64 = android::base::StringPrintf( |
| 484 | HAL_LIBRARY_PATH_VNDK_SP_64BIT_FOR_VERSION, details::getVndkVersionStr().c_str()); |
| 485 | static std::string halLibPathVndkSp32 = android::base::StringPrintf( |
| 486 | HAL_LIBRARY_PATH_VNDK_SP_32BIT_FOR_VERSION, details::getVndkVersionStr().c_str()); |
Jiyong Park | 56eb149 | 2017-08-04 16:16:13 +0900 | [diff] [blame] | 487 | static std::vector<std::pair<Arch, std::vector<const char*>>> sAllPaths{ |
| 488 | {Arch::IS_64BIT, |
| 489 | {HAL_LIBRARY_PATH_ODM_64BIT, HAL_LIBRARY_PATH_VENDOR_64BIT, |
Justin Yun | 1f04810 | 2017-12-01 15:30:08 +0900 | [diff] [blame] | 490 | halLibPathVndkSp64.c_str(), HAL_LIBRARY_PATH_SYSTEM_64BIT}}, |
Jiyong Park | 56eb149 | 2017-08-04 16:16:13 +0900 | [diff] [blame] | 491 | {Arch::IS_32BIT, |
| 492 | {HAL_LIBRARY_PATH_ODM_32BIT, HAL_LIBRARY_PATH_VENDOR_32BIT, |
Justin Yun | 1f04810 | 2017-12-01 15:30:08 +0900 | [diff] [blame] | 493 | halLibPathVndkSp32.c_str(), HAL_LIBRARY_PATH_SYSTEM_32BIT}}}; |
Yifan Hong | bd0d8f7 | 2017-05-24 19:43:51 -0700 | [diff] [blame] | 494 | std::map<std::string, InstanceDebugInfo> map; |
Yifan Hong | 705e5da | 2017-03-02 16:59:39 -0800 | [diff] [blame] | 495 | for (const auto &pair : sAllPaths) { |
| 496 | Arch arch = pair.first; |
| 497 | for (const auto &path : pair.second) { |
| 498 | std::vector<std::string> libs = search(path, "", ".so"); |
| 499 | for (const std::string &lib : libs) { |
| 500 | std::string matchedName; |
Yifan Hong | bd0d8f7 | 2017-05-24 19:43:51 -0700 | [diff] [blame] | 501 | std::string implName; |
| 502 | if (matchPackageName(lib, &matchedName, &implName)) { |
| 503 | std::string instanceName{"* ("s + path + ")"s}; |
| 504 | if (!implName.empty()) instanceName += " ("s + implName + ")"s; |
| 505 | map.emplace(path + lib, InstanceDebugInfo{.interfaceName = matchedName, |
| 506 | .instanceName = instanceName, |
| 507 | .clientPids = {}, |
| 508 | .arch = arch}); |
Yifan Hong | 705e5da | 2017-03-02 16:59:39 -0800 | [diff] [blame] | 509 | } |
| 510 | } |
| 511 | } |
| 512 | } |
Yifan Hong | bd0d8f7 | 2017-05-24 19:43:51 -0700 | [diff] [blame] | 513 | fetchPidsForPassthroughLibraries(&map); |
| 514 | hidl_vec<InstanceDebugInfo> vec; |
| 515 | vec.resize(map.size()); |
| 516 | size_t idx = 0; |
| 517 | for (auto&& pair : map) { |
| 518 | vec[idx++] = std::move(pair.second); |
| 519 | } |
Yifan Hong | 705e5da | 2017-03-02 16:59:39 -0800 | [diff] [blame] | 520 | _hidl_cb(vec); |
Yifan Hong | 7f49f59 | 2017-02-03 15:11:44 -0800 | [diff] [blame] | 521 | return Void(); |
| 522 | } |
| 523 | |
Martijn Coenen | af4aba5 | 2017-04-27 09:41:13 -0700 | [diff] [blame] | 524 | Return<void> registerPassthroughClient(const hidl_string &, const hidl_string &) override { |
Yifan Hong | 7f49f59 | 2017-02-03 15:11:44 -0800 | [diff] [blame] | 525 | // This makes no sense. |
| 526 | LOG(FATAL) << "Cannot call registerPassthroughClient on passthrough service manager. " |
| 527 | << "Call it on defaultServiceManager() instead."; |
Yifan Hong | 9a22d1d | 2017-01-25 14:19:26 -0800 | [diff] [blame] | 528 | return Void(); |
| 529 | } |
| 530 | |
Steven Moreland | 2a2678e | 2017-07-21 18:07:38 -0700 | [diff] [blame] | 531 | Return<bool> unregisterForNotifications(const hidl_string& /* fqName */, |
| 532 | const hidl_string& /* name */, |
| 533 | const sp<IServiceNotification>& /* callback */) override { |
| 534 | // This makes no sense. |
| 535 | LOG(FATAL) << "Cannot unregister for notifications with passthrough service manager."; |
| 536 | return false; |
| 537 | } |
| 538 | |
Steven Moreland | 337e6b6 | 2017-01-18 17:25:13 -0800 | [diff] [blame] | 539 | }; |
| 540 | |
Steven Moreland | 2a2678e | 2017-07-21 18:07:38 -0700 | [diff] [blame] | 541 | sp<IServiceManager1_0> getPassthroughServiceManager() { |
| 542 | return getPassthroughServiceManager1_1(); |
| 543 | } |
| 544 | sp<IServiceManager1_1> getPassthroughServiceManager1_1() { |
Steven Moreland | 337e6b6 | 2017-01-18 17:25:13 -0800 | [diff] [blame] | 545 | static sp<PassthroughServiceManager> manager(new PassthroughServiceManager()); |
| 546 | return manager; |
| 547 | } |
| 548 | |
Steven Moreland | cbefd35 | 2017-01-23 20:29:05 -0800 | [diff] [blame] | 549 | namespace details { |
| 550 | |
Steven Moreland | 519306f | 2017-06-06 17:14:12 -0700 | [diff] [blame] | 551 | void preloadPassthroughService(const std::string &descriptor) { |
| 552 | PassthroughServiceManager::openLibs(descriptor, |
| 553 | [&](void* /* handle */, const std::string& /* lib */, const std::string& /* sym */) { |
| 554 | // do nothing |
| 555 | return true; // open all libs |
| 556 | }); |
| 557 | } |
| 558 | |
Steven Moreland | cbefd35 | 2017-01-23 20:29:05 -0800 | [diff] [blame] | 559 | struct Waiter : IServiceNotification { |
Martijn Coenen | 773609e | 2017-10-24 09:57:53 +0200 | [diff] [blame] | 560 | Waiter(const std::string& interface, const std::string& instanceName, |
| 561 | const sp<IServiceManager1_1>& sm) : mInterfaceName(interface), |
| 562 | mInstanceName(instanceName), mSm(sm) { |
Martijn Coenen | e6cdfd3 | 2017-11-13 14:03:05 +0100 | [diff] [blame] | 563 | } |
| 564 | |
| 565 | void onFirstRef() override { |
Martijn Coenen | b88ae7f | 2017-10-24 11:45:41 +0200 | [diff] [blame] | 566 | // If this process only has one binder thread, and we're calling wait() from |
| 567 | // that thread, it will block forever because we hung up the one and only |
| 568 | // binder thread on a condition variable that can only be notified by an |
| 569 | // incoming binder call. |
Tobias Lindskog | 88e886f | 2018-01-05 10:32:43 +0100 | [diff] [blame] | 570 | if (IPCThreadState::self()->isOnlyBinderThread()) { |
Martijn Coenen | b88ae7f | 2017-10-24 11:45:41 +0200 | [diff] [blame] | 571 | LOG(WARNING) << "Can't efficiently wait for " << mInterfaceName << "/" |
| 572 | << mInstanceName << ", because we are called from " |
| 573 | << "the only binder thread in this process."; |
| 574 | return; |
| 575 | } |
| 576 | |
Martijn Coenen | e6cdfd3 | 2017-11-13 14:03:05 +0100 | [diff] [blame] | 577 | Return<bool> ret = mSm->registerForNotifications(mInterfaceName, mInstanceName, this); |
Martijn Coenen | 773609e | 2017-10-24 09:57:53 +0200 | [diff] [blame] | 578 | |
| 579 | if (!ret.isOk()) { |
| 580 | LOG(ERROR) << "Transport error, " << ret.description() |
Martijn Coenen | e6cdfd3 | 2017-11-13 14:03:05 +0100 | [diff] [blame] | 581 | << ", during notification registration for " << mInterfaceName << "/" |
| 582 | << mInstanceName << "."; |
Martijn Coenen | 773609e | 2017-10-24 09:57:53 +0200 | [diff] [blame] | 583 | return; |
| 584 | } |
| 585 | |
| 586 | if (!ret) { |
Martijn Coenen | e6cdfd3 | 2017-11-13 14:03:05 +0100 | [diff] [blame] | 587 | LOG(ERROR) << "Could not register for notifications for " << mInterfaceName << "/" |
| 588 | << mInstanceName << "."; |
Martijn Coenen | 773609e | 2017-10-24 09:57:53 +0200 | [diff] [blame] | 589 | return; |
| 590 | } |
| 591 | |
| 592 | mRegisteredForNotifications = true; |
| 593 | } |
| 594 | |
| 595 | ~Waiter() { |
Martijn Coenen | 3fa15c2 | 2018-02-10 11:30:00 +0100 | [diff] [blame] | 596 | if (!mDoneCalled) { |
| 597 | LOG(FATAL) |
| 598 | << "Waiter still registered for notifications, call done() before dropping ref!"; |
Martijn Coenen | 773609e | 2017-10-24 09:57:53 +0200 | [diff] [blame] | 599 | } |
| 600 | } |
| 601 | |
Steven Moreland | cbefd35 | 2017-01-23 20:29:05 -0800 | [diff] [blame] | 602 | Return<void> onRegistration(const hidl_string& /* fqName */, |
| 603 | const hidl_string& /* name */, |
| 604 | bool /* preexisting */) override { |
| 605 | std::unique_lock<std::mutex> lock(mMutex); |
| 606 | if (mRegistered) { |
| 607 | return Void(); |
| 608 | } |
| 609 | mRegistered = true; |
| 610 | lock.unlock(); |
| 611 | |
| 612 | mCondition.notify_one(); |
| 613 | return Void(); |
| 614 | } |
| 615 | |
Steven Moreland | 0771d8a | 2018-05-09 13:29:14 -0700 | [diff] [blame] | 616 | void wait(bool timeout) { |
Steven Moreland | 4960510 | 2017-03-28 09:33:06 -0700 | [diff] [blame] | 617 | using std::literals::chrono_literals::operator""s; |
| 618 | |
Martijn Coenen | 773609e | 2017-10-24 09:57:53 +0200 | [diff] [blame] | 619 | if (!mRegisteredForNotifications) { |
| 620 | // As an alternative, just sleep for a second and return |
| 621 | LOG(WARNING) << "Waiting one second for " << mInterfaceName << "/" << mInstanceName; |
| 622 | sleep(1); |
| 623 | return; |
| 624 | } |
| 625 | |
Steven Moreland | cbefd35 | 2017-01-23 20:29:05 -0800 | [diff] [blame] | 626 | std::unique_lock<std::mutex> lock(mMutex); |
Steven Moreland | 0771d8a | 2018-05-09 13:29:14 -0700 | [diff] [blame] | 627 | do { |
Steven Moreland | 4960510 | 2017-03-28 09:33:06 -0700 | [diff] [blame] | 628 | mCondition.wait_for(lock, 1s, [this]{ |
| 629 | return mRegistered; |
| 630 | }); |
| 631 | |
| 632 | if (mRegistered) { |
| 633 | break; |
| 634 | } |
| 635 | |
Steven Moreland | 0771d8a | 2018-05-09 13:29:14 -0700 | [diff] [blame] | 636 | LOG(WARNING) << "Waited one second for " << mInterfaceName << "/" << mInstanceName; |
| 637 | } while (!timeout); |
Steven Moreland | cbefd35 | 2017-01-23 20:29:05 -0800 | [diff] [blame] | 638 | } |
| 639 | |
Martijn Coenen | 773609e | 2017-10-24 09:57:53 +0200 | [diff] [blame] | 640 | // Be careful when using this; after calling reset(), you must always try to retrieve |
| 641 | // the corresponding service before blocking on the waiter; otherwise, you might run |
| 642 | // into a race-condition where the service has just (re-)registered, you clear the state |
| 643 | // here, and subsequently calling waiter->wait() will block forever. |
| 644 | void reset() { |
| 645 | std::unique_lock<std::mutex> lock(mMutex); |
| 646 | mRegistered = false; |
| 647 | } |
Martijn Coenen | 3fa15c2 | 2018-02-10 11:30:00 +0100 | [diff] [blame] | 648 | |
| 649 | // done() must be called before dropping the last strong ref to the Waiter, to make |
| 650 | // sure we can properly unregister with hwservicemanager. |
| 651 | void done() { |
| 652 | if (mRegisteredForNotifications) { |
| 653 | if (!mSm->unregisterForNotifications(mInterfaceName, mInstanceName, this) |
| 654 | .withDefault(false)) { |
| 655 | LOG(ERROR) << "Could not unregister service notification for " << mInterfaceName |
| 656 | << "/" << mInstanceName << "."; |
| 657 | } else { |
| 658 | mRegisteredForNotifications = false; |
| 659 | } |
| 660 | } |
| 661 | mDoneCalled = true; |
| 662 | } |
| 663 | |
| 664 | private: |
Martijn Coenen | 773609e | 2017-10-24 09:57:53 +0200 | [diff] [blame] | 665 | const std::string mInterfaceName; |
| 666 | const std::string mInstanceName; |
Steven Moreland | ad1f992 | 2018-10-02 19:37:05 -0700 | [diff] [blame] | 667 | sp<IServiceManager1_1> mSm; |
Steven Moreland | cbefd35 | 2017-01-23 20:29:05 -0800 | [diff] [blame] | 668 | std::mutex mMutex; |
| 669 | std::condition_variable mCondition; |
| 670 | bool mRegistered = false; |
Martijn Coenen | 773609e | 2017-10-24 09:57:53 +0200 | [diff] [blame] | 671 | bool mRegisteredForNotifications = false; |
Martijn Coenen | 3fa15c2 | 2018-02-10 11:30:00 +0100 | [diff] [blame] | 672 | bool mDoneCalled = false; |
Steven Moreland | cbefd35 | 2017-01-23 20:29:05 -0800 | [diff] [blame] | 673 | }; |
| 674 | |
| 675 | void waitForHwService( |
| 676 | const std::string &interface, const std::string &instanceName) { |
Martijn Coenen | 773609e | 2017-10-24 09:57:53 +0200 | [diff] [blame] | 677 | sp<Waiter> waiter = new Waiter(interface, instanceName, defaultServiceManager1_1()); |
Steven Moreland | 0771d8a | 2018-05-09 13:29:14 -0700 | [diff] [blame] | 678 | waiter->wait(false /* timeout */); |
Martijn Coenen | 3fa15c2 | 2018-02-10 11:30:00 +0100 | [diff] [blame] | 679 | waiter->done(); |
Martijn Coenen | 773609e | 2017-10-24 09:57:53 +0200 | [diff] [blame] | 680 | } |
Steven Moreland | cbefd35 | 2017-01-23 20:29:05 -0800 | [diff] [blame] | 681 | |
Martijn Coenen | 773609e | 2017-10-24 09:57:53 +0200 | [diff] [blame] | 682 | // Prints relevant error/warning messages for error return values from |
| 683 | // details::canCastInterface(), both transaction errors (!castReturn.isOk()) |
| 684 | // as well as actual cast failures (castReturn.isOk() && castReturn = false). |
| 685 | // Returns 'true' if the error is non-fatal and it's useful to retry |
| 686 | bool handleCastError(const Return<bool>& castReturn, const std::string& descriptor, |
| 687 | const std::string& instance) { |
| 688 | if (castReturn.isOk()) { |
| 689 | if (castReturn) { |
| 690 | details::logAlwaysFatal("Successful cast value passed into handleCastError."); |
| 691 | } |
| 692 | // This should never happen, and there's not really a point in retrying. |
| 693 | ALOGE("getService: received incompatible service (bug in hwservicemanager?) for " |
| 694 | "%s/%s.", descriptor.c_str(), instance.c_str()); |
| 695 | return false; |
Steven Moreland | cbefd35 | 2017-01-23 20:29:05 -0800 | [diff] [blame] | 696 | } |
Martijn Coenen | 773609e | 2017-10-24 09:57:53 +0200 | [diff] [blame] | 697 | if (castReturn.isDeadObject()) { |
| 698 | ALOGW("getService: found dead hwbinder service for %s/%s.", descriptor.c_str(), |
| 699 | instance.c_str()); |
| 700 | return true; |
Steven Moreland | cbefd35 | 2017-01-23 20:29:05 -0800 | [diff] [blame] | 701 | } |
Martijn Coenen | 773609e | 2017-10-24 09:57:53 +0200 | [diff] [blame] | 702 | // This can happen due to: |
| 703 | // 1) No SELinux permissions |
| 704 | // 2) Other transaction failure (no buffer space, kernel error) |
| 705 | // The first isn't recoverable, but the second is. |
| 706 | // Since we can't yet differentiate between the two, and clients depend |
| 707 | // on us not blocking in case 1), treat this as a fatal error for now. |
| 708 | ALOGW("getService: unable to call into hwbinder service for %s/%s.", |
| 709 | descriptor.c_str(), instance.c_str()); |
| 710 | return false; |
Steven Moreland | cbefd35 | 2017-01-23 20:29:05 -0800 | [diff] [blame] | 711 | } |
| 712 | |
Martijn Coenen | 86c3abb | 2017-10-24 09:33:28 +0200 | [diff] [blame] | 713 | sp<::android::hidl::base::V1_0::IBase> getRawServiceInternal(const std::string& descriptor, |
| 714 | const std::string& instance, |
| 715 | bool retry, bool getStub) { |
| 716 | using Transport = ::android::hidl::manager::V1_0::IServiceManager::Transport; |
Martijn Coenen | 86c3abb | 2017-10-24 09:33:28 +0200 | [diff] [blame] | 717 | using ::android::hidl::manager::V1_0::IServiceManager; |
Martijn Coenen | d35678f | 2018-03-07 09:51:01 +0100 | [diff] [blame] | 718 | sp<Waiter> waiter; |
Martijn Coenen | 86c3abb | 2017-10-24 09:33:28 +0200 | [diff] [blame] | 719 | |
Yifan Hong | deacf14 | 2018-07-10 17:33:34 -0700 | [diff] [blame] | 720 | sp<IServiceManager1_1> sm; |
| 721 | Transport transport = Transport::EMPTY; |
| 722 | if (kIsRecovery) { |
Yifan Hong | deacf14 | 2018-07-10 17:33:34 -0700 | [diff] [blame] | 723 | transport = Transport::PASSTHROUGH; |
Yifan Hong | deacf14 | 2018-07-10 17:33:34 -0700 | [diff] [blame] | 724 | } else { |
| 725 | sm = defaultServiceManager1_1(); |
| 726 | if (sm == nullptr) { |
| 727 | ALOGE("getService: defaultServiceManager() is null"); |
| 728 | return nullptr; |
| 729 | } |
| 730 | |
| 731 | Return<Transport> transportRet = sm->getTransport(descriptor, instance); |
| 732 | |
| 733 | if (!transportRet.isOk()) { |
| 734 | ALOGE("getService: defaultServiceManager()->getTransport returns %s", |
| 735 | transportRet.description().c_str()); |
| 736 | return nullptr; |
| 737 | } |
| 738 | transport = transportRet; |
Martijn Coenen | 86c3abb | 2017-10-24 09:33:28 +0200 | [diff] [blame] | 739 | } |
| 740 | |
Martijn Coenen | 86c3abb | 2017-10-24 09:33:28 +0200 | [diff] [blame] | 741 | const bool vintfHwbinder = (transport == Transport::HWBINDER); |
| 742 | const bool vintfPassthru = (transport == Transport::PASSTHROUGH); |
| 743 | |
Steven Moreland | 757394b | 2017-12-13 14:09:24 -0800 | [diff] [blame] | 744 | #ifdef ENFORCE_VINTF_MANIFEST |
Martijn Coenen | 86c3abb | 2017-10-24 09:33:28 +0200 | [diff] [blame] | 745 | |
| 746 | #ifdef LIBHIDL_TARGET_DEBUGGABLE |
| 747 | const char* env = std::getenv("TREBLE_TESTING_OVERRIDE"); |
| 748 | const bool trebleTestingOverride = env && !strcmp(env, "true"); |
| 749 | const bool vintfLegacy = (transport == Transport::EMPTY) && trebleTestingOverride; |
Steven Moreland | 757394b | 2017-12-13 14:09:24 -0800 | [diff] [blame] | 750 | #else // ENFORCE_VINTF_MANIFEST but not LIBHIDL_TARGET_DEBUGGABLE |
Martijn Coenen | 86c3abb | 2017-10-24 09:33:28 +0200 | [diff] [blame] | 751 | const bool trebleTestingOverride = false; |
| 752 | const bool vintfLegacy = false; |
| 753 | #endif // LIBHIDL_TARGET_DEBUGGABLE |
| 754 | |
Steven Moreland | 757394b | 2017-12-13 14:09:24 -0800 | [diff] [blame] | 755 | #else // not ENFORCE_VINTF_MANIFEST |
Martijn Coenen | 86c3abb | 2017-10-24 09:33:28 +0200 | [diff] [blame] | 756 | const char* env = std::getenv("TREBLE_TESTING_OVERRIDE"); |
| 757 | const bool trebleTestingOverride = env && !strcmp(env, "true"); |
| 758 | const bool vintfLegacy = (transport == Transport::EMPTY); |
Steven Moreland | 757394b | 2017-12-13 14:09:24 -0800 | [diff] [blame] | 759 | #endif // ENFORCE_VINTF_MANIFEST |
Martijn Coenen | 86c3abb | 2017-10-24 09:33:28 +0200 | [diff] [blame] | 760 | |
Steven Moreland | 7637124 | 2018-04-19 17:11:39 -0700 | [diff] [blame] | 761 | for (int tries = 0; !getStub && (vintfHwbinder || vintfLegacy); tries++) { |
| 762 | if (waiter == nullptr && tries > 0) { |
Martijn Coenen | d35678f | 2018-03-07 09:51:01 +0100 | [diff] [blame] | 763 | waiter = new Waiter(descriptor, instance, sm); |
| 764 | } |
Steven Moreland | 7637124 | 2018-04-19 17:11:39 -0700 | [diff] [blame] | 765 | if (waiter != nullptr) { |
| 766 | waiter->reset(); // don't reorder this -- see comments on reset() |
| 767 | } |
Martijn Coenen | 86c3abb | 2017-10-24 09:33:28 +0200 | [diff] [blame] | 768 | Return<sp<IBase>> ret = sm->get(descriptor, instance); |
| 769 | if (!ret.isOk()) { |
| 770 | ALOGE("getService: defaultServiceManager()->get returns %s for %s/%s.", |
| 771 | ret.description().c_str(), descriptor.c_str(), instance.c_str()); |
| 772 | break; |
| 773 | } |
| 774 | sp<IBase> base = ret; |
Martijn Coenen | 773609e | 2017-10-24 09:57:53 +0200 | [diff] [blame] | 775 | if (base != nullptr) { |
| 776 | Return<bool> canCastRet = |
| 777 | details::canCastInterface(base.get(), descriptor.c_str(), true /* emitError */); |
| 778 | |
| 779 | if (canCastRet.isOk() && canCastRet) { |
Steven Moreland | 7637124 | 2018-04-19 17:11:39 -0700 | [diff] [blame] | 780 | if (waiter != nullptr) { |
| 781 | waiter->done(); |
| 782 | } |
Martijn Coenen | 773609e | 2017-10-24 09:57:53 +0200 | [diff] [blame] | 783 | return base; // still needs to be wrapped by Bp class. |
Martijn Coenen | 86c3abb | 2017-10-24 09:33:28 +0200 | [diff] [blame] | 784 | } |
Martijn Coenen | 773609e | 2017-10-24 09:57:53 +0200 | [diff] [blame] | 785 | |
| 786 | if (!handleCastError(canCastRet, descriptor, instance)) break; |
Martijn Coenen | 86c3abb | 2017-10-24 09:33:28 +0200 | [diff] [blame] | 787 | } |
| 788 | |
Martijn Coenen | 773609e | 2017-10-24 09:57:53 +0200 | [diff] [blame] | 789 | // In case of legacy or we were not asked to retry, don't. |
| 790 | if (vintfLegacy || !retry) break; |
Martijn Coenen | 86c3abb | 2017-10-24 09:33:28 +0200 | [diff] [blame] | 791 | |
Steven Moreland | 7637124 | 2018-04-19 17:11:39 -0700 | [diff] [blame] | 792 | if (waiter != nullptr) { |
| 793 | 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] | 794 | waiter->wait(true /* timeout */); |
Steven Moreland | 7637124 | 2018-04-19 17:11:39 -0700 | [diff] [blame] | 795 | } |
Martijn Coenen | 86c3abb | 2017-10-24 09:33:28 +0200 | [diff] [blame] | 796 | } |
| 797 | |
Martijn Coenen | d35678f | 2018-03-07 09:51:01 +0100 | [diff] [blame] | 798 | if (waiter != nullptr) { |
| 799 | waiter->done(); |
| 800 | } |
Martijn Coenen | 3fa15c2 | 2018-02-10 11:30:00 +0100 | [diff] [blame] | 801 | |
Martijn Coenen | 86c3abb | 2017-10-24 09:33:28 +0200 | [diff] [blame] | 802 | if (getStub || vintfPassthru || vintfLegacy) { |
| 803 | const sp<IServiceManager> pm = getPassthroughServiceManager(); |
| 804 | if (pm != nullptr) { |
| 805 | sp<IBase> base = pm->get(descriptor, instance).withDefault(nullptr); |
| 806 | if (!getStub || trebleTestingOverride) { |
| 807 | base = wrapPassthrough(base); |
| 808 | } |
| 809 | return base; |
| 810 | } |
| 811 | } |
| 812 | |
| 813 | return nullptr; |
| 814 | } |
| 815 | |
Steven Moreland | bb9eb2a | 2018-10-11 12:10:01 -0700 | [diff] [blame] | 816 | status_t registerAsServiceInternal(const sp<IBase>& service, const std::string& name) { |
| 817 | if (service == nullptr) { |
| 818 | return UNEXPECTED_NULL; |
| 819 | } |
| 820 | |
| 821 | sp<IServiceManager1_2> sm = defaultServiceManager1_2(); |
| 822 | if (sm == nullptr) { |
| 823 | return INVALID_OPERATION; |
| 824 | } |
| 825 | |
| 826 | bool registered = false; |
| 827 | Return<void> ret = service->interfaceChain([&](const auto& chain) { |
| 828 | registered = sm->addWithChain(name.c_str(), service, chain).withDefault(false); |
| 829 | }); |
| 830 | |
| 831 | if (!ret.isOk()) { |
| 832 | LOG(ERROR) << "Could not retrieve interface chain: " << ret.description(); |
| 833 | } |
| 834 | |
| 835 | if (registered) { |
| 836 | onRegistrationImpl(getDescriptor(service.get()), name); |
| 837 | } |
| 838 | |
| 839 | return registered ? OK : UNKNOWN_ERROR; |
| 840 | } |
| 841 | |
Bernhard Rosenkränzer | 0c28fd2 | 2018-06-04 16:01:47 +0200 | [diff] [blame] | 842 | } // namespace details |
Steven Moreland | cbefd35 | 2017-01-23 20:29:05 -0800 | [diff] [blame] | 843 | |
Bernhard Rosenkränzer | 0c28fd2 | 2018-06-04 16:01:47 +0200 | [diff] [blame] | 844 | } // namespace hardware |
| 845 | } // namespace android |