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