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