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