blob: 6d85b57e8442e608a04575eea58725dd69b8d1bb [file] [log] [blame]
Steven Moreland5d5ef7f2016-10-20 19:19:55 -07001/*
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 Morelandfcaa97f2019-06-24 12:07:22 -070017#define LOG_TAG "HidlServiceManagement"
Steven Moreland5d5ef7f2016-10-20 19:19:55 -070018
Steven Moreland2056cf32019-09-17 17:41:02 -070019#ifdef __ANDROID__
Jiyong Park3ab546c2017-04-06 20:28:07 +090020#include <android/dlext.h>
Steven Moreland2056cf32019-09-17 17:41:02 -070021#endif // __ANDROID__
22
Yifan Hong9a22d1d2017-01-25 14:19:26 -080023#include <condition_variable>
24#include <dlfcn.h>
25#include <dirent.h>
Steven Moreland405d7612017-04-07 20:31:22 -070026#include <fstream>
27#include <pthread.h>
Yifan Hong9a22d1d2017-01-25 14:19:26 -080028#include <unistd.h>
29
30#include <mutex>
31#include <regex>
Yifan Hongbd0d8f72017-05-24 19:43:51 -070032#include <set>
Yifan Hong9a22d1d2017-01-25 14:19:26 -080033
Martijn Coenen12f04d92016-12-07 17:29:41 +010034#include <hidl/HidlBinderSupport.h>
Justin Yun1f048102017-12-01 15:30:08 +090035#include <hidl/HidlInternal.h>
Steven Morelande69e8d32018-03-14 10:55:42 -070036#include <hidl/HidlTransportUtils.h>
Steven Moreland5d5ef7f2016-10-20 19:19:55 -070037#include <hidl/ServiceManagement.h>
Steven Moreland5d5ef7f2016-10-20 19:19:55 -070038#include <hidl/Status.h>
Peter Kalauskas64ffced2018-09-05 16:41:08 -070039#include <utils/SystemClock.h>
Steven Moreland5d5ef7f2016-10-20 19:19:55 -070040
Peter Kalauskas64ffced2018-09-05 16:41:08 -070041#include <android-base/file.h>
Steven Moreland337e6b62017-01-18 17:25:13 -080042#include <android-base/logging.h>
Peter Kalauskas64ffced2018-09-05 16:41:08 -070043#include <android-base/parseint.h>
Steven Morelandc1cee2c2017-03-24 16:23:11 +000044#include <android-base/properties.h>
Justin Yun1f048102017-12-01 15:30:08 +090045#include <android-base/stringprintf.h>
Peter Kalauskas64ffced2018-09-05 16:41:08 -070046#include <android-base/strings.h>
Steven Moreland5d5ef7f2016-10-20 19:19:55 -070047#include <hwbinder/IPCThreadState.h>
48#include <hwbinder/Parcel.h>
Steven Moreland2056cf32019-09-17 17:41:02 -070049#if !defined(__ANDROID_RECOVERY__) && defined(__ANDROID__)
Jiyong Parkba8ace12017-05-15 15:44:39 +090050#include <vndksupport/linker.h>
Jerry Zhang86ae9992018-05-30 17:11:09 -070051#endif
Steven Moreland5d5ef7f2016-10-20 19:19:55 -070052
Steven Moreland89909692018-05-30 14:11:19 -070053#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 Moreland5d5ef7f2016-10-20 19:19:55 -070056
Steven Morelandbb9eb2a2018-10-11 12:10:01 -070057using ::android::hidl::base::V1_0::IBase;
Steven Moreland2a2678e2017-07-21 18:07:38 -070058using IServiceManager1_0 = android::hidl::manager::V1_0::IServiceManager;
59using IServiceManager1_1 = android::hidl::manager::V1_1::IServiceManager;
Steven Moreland89909692018-05-30 14:11:19 -070060using IServiceManager1_2 = android::hidl::manager::V1_2::IServiceManager;
Steven Morelandbb9eb2a2018-10-11 12:10:01 -070061using ::android::hidl::manager::V1_0::IServiceNotification;
Devin Mooree1973ce2022-04-15 20:54:03 +000062using ::android::hidl::manager::V1_2::IClientCallback;
Steven Moreland5d5ef7f2016-10-20 19:19:55 -070063
64namespace android {
65namespace hardware {
66
Yifan Hongdeacf142018-07-10 17:33:34 -070067#if defined(__ANDROID_RECOVERY__)
68static constexpr bool kIsRecovery = true;
69#else
70static constexpr bool kIsRecovery = false;
71#endif
72
Steven Morelandbb9eb2a2018-10-11 12:10:01 -070073static void waitForHwServiceManager() {
Steven Moreland2056cf32019-09-17 17:41:02 -070074 // 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 Morelandc1cee2c2017-03-24 16:23:11 +000079 using std::literals::chrono_literals::operator""s;
80
Steven Moreland2056cf32019-09-17 17:41:02 -070081 using android::base::WaitForProperty;
Steven Morelandc1cee2c2017-03-24 16:23:11 +000082 while (!WaitForProperty(kHwServicemanagerReadyProperty, "true", 1s)) {
83 LOG(WARNING) << "Waited for hwservicemanager.ready for a second, waiting another...";
84 }
Steven Moreland2056cf32019-09-17 17:41:02 -070085#endif // __ANDROID__
Steven Morelandc1cee2c2017-03-24 16:23:11 +000086}
87
Steven Morelandbb9eb2a2018-10-11 12:10:01 -070088static std::string binaryName() {
Steven Moreland405d7612017-04-07 20:31:22 -070089 std::ifstream ifs("/proc/self/cmdline");
90 std::string cmdline;
Steven Morelandd682c0e2020-07-21 17:47:32 +000091 if (!ifs) {
Steven Moreland405d7612017-04-07 20:31:22 -070092 return "";
93 }
94 ifs >> cmdline;
95
Chih-Hung Hsieh41649d52017-08-03 14:27:21 -070096 size_t idx = cmdline.rfind('/');
Steven Moreland405d7612017-04-07 20:31:22 -070097 if (idx != std::string::npos) {
98 cmdline = cmdline.substr(idx + 1);
99 }
100
101 return cmdline;
102}
103
Steven Morelandbb9eb2a2018-10-11 12:10:01 -0700104static std::string packageWithoutVersion(const std::string& packageAndVersion) {
Steven Moreland7d09a402018-04-06 10:44:05 -0700105 size_t at = packageAndVersion.find('@');
106 if (at == std::string::npos) return packageAndVersion;
107 return packageAndVersion.substr(0, at);
108}
109
Steven Morelandd682c0e2020-07-21 17:47:32 +0000110__attribute__((noinline)) static void tryShortenProcessName(const std::string& descriptor) {
Steven Moreland7d09a402018-04-06 10:44:05 -0700111 const static std::string kTasks = "/proc/self/task/";
112
113 // make sure that this binary name is in the same package
Steven Moreland405d7612017-04-07 20:31:22 -0700114 std::string processName = binaryName();
115
Steven Moreland7d09a402018-04-06 10:44:05 -0700116 // e.x. android.hardware.foo is this package
Steven Moreland8092aa02018-10-12 15:54:56 -0700117 if (!base::StartsWith(packageWithoutVersion(processName), packageWithoutVersion(descriptor))) {
Steven Moreland405d7612017-04-07 20:31:22 -0700118 return;
119 }
120
Steven Morelandbb9eb2a2018-10-11 12:10:01 -0700121 // e.x. android.hardware.module.foo@1.2::IFoo -> foo@1.2
122 size_t lastDot = descriptor.rfind('.');
Steven Moreland7d09a402018-04-06 10:44:05 -0700123 if (lastDot == std::string::npos) return;
Steven Morelandbb9eb2a2018-10-11 12:10:01 -0700124 size_t secondDot = descriptor.rfind('.', lastDot - 1);
Steven Moreland7d09a402018-04-06 10:44:05 -0700125 if (secondDot == std::string::npos) return;
Steven Moreland405d7612017-04-07 20:31:22 -0700126
Steven Moreland7d09a402018-04-06 10:44:05 -0700127 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 Morelandd682c0e2020-07-21 17:47:32 +0000139 if (!fs) {
Steven Moreland7d09a402018-04-06 10:44:05 -0700140 ALOGI("Could not rename process, failed read comm for %s.", dp->d_name);
141 continue;
142 }
143
144 std::string oldComm;
Steven Morelandd682c0e2020-07-21 17:47:32 +0000145 if (!(fs >> oldComm)) continue;
Steven Moreland7d09a402018-04-06 10:44:05 -0700146
147 // don't rename if it already has an explicit name
Steven Moreland8092aa02018-10-12 15:54:56 -0700148 if (base::StartsWith(descriptor, oldComm)) {
Steven Morelandd682c0e2020-07-21 17:47:32 +0000149 if (!fs.seekg(0, fs.beg)) continue;
Steven Moreland7d09a402018-04-06 10:44:05 -0700150 fs << newName;
151 }
Steven Moreland405d7612017-04-07 20:31:22 -0700152 }
Steven Moreland405d7612017-04-07 20:31:22 -0700153}
154
155namespace details {
156
Steven Morelandb939e7e2020-07-08 01:04:23 +0000157#ifdef ENFORCE_VINTF_MANIFEST
158static constexpr bool kEnforceVintfManifest = true;
159#else
160static constexpr bool kEnforceVintfManifest = false;
161#endif
162
Steven Moreland206d2812020-07-08 21:21:48 +0000163static bool* getTrebleTestingOverridePtr() {
164 static bool gTrebleTestingOverride = false;
165 return &gTrebleTestingOverride;
166}
Steven Morelandb939e7e2020-07-08 01:04:23 +0000167
168void setTrebleTestingOverride(bool testingOverride) {
Steven Moreland206d2812020-07-08 21:21:48 +0000169 *getTrebleTestingOverridePtr() = testingOverride;
Steven Morelandb939e7e2020-07-08 01:04:23 +0000170}
171
Steven Morelandf47cdf72021-04-07 22:13:13 +0000172static bool isDebuggable() {
173 static bool debuggable = base::GetBoolProperty("ro.debuggable", false);
174 return debuggable;
175}
176
Steven Morelandb939e7e2020-07-08 01:04:23 +0000177static inline bool isTrebleTestingOverride() {
Steven Moreland4acdab72022-08-09 16:27:46 +0000178 // return false early so we don't need to check the debuggable property
179 if (!*getTrebleTestingOverridePtr()) return false;
180
Steven Morelandf47cdf72021-04-07 22:13:13 +0000181 if (kEnforceVintfManifest && !isDebuggable()) {
Steven Morelandb939e7e2020-07-08 01:04:23 +0000182 // don't allow testing override in production
183 return false;
184 }
185
Steven Moreland4acdab72022-08-09 16:27:46 +0000186 return true;
Steven Morelandb939e7e2020-07-08 01:04:23 +0000187}
188
Steven Morelandbb9eb2a2018-10-11 12:10:01 -0700189static void onRegistrationImpl(const std::string& descriptor, const std::string& instanceName) {
Steven Moreland489e7572021-01-21 19:02:54 +0000190 LOG(INFO) << "Registered " << descriptor << "/" << instanceName;
Steven Morelandbb9eb2a2018-10-11 12:10:01 -0700191 tryShortenProcessName(descriptor);
192}
193
Steven Moreland911852c2020-05-11 10:51:26 -0700194// only used by prebuilts - should be able to remove
Steven Morelandbb9eb2a2018-10-11 12:10:01 -0700195void onRegistration(const std::string& packageName, const std::string& interfaceName,
196 const std::string& instanceName) {
197 return onRegistrationImpl(packageName + "::" + interfaceName, instanceName);
Steven Moreland405d7612017-04-07 20:31:22 -0700198}
199
200} // details
201
Steven Moreland2a2678e2017-07-21 18:07:38 -0700202sp<IServiceManager1_0> defaultServiceManager() {
Steven Moreland89909692018-05-30 14:11:19 -0700203 return defaultServiceManager1_2();
Steven Moreland2a2678e2017-07-21 18:07:38 -0700204}
205sp<IServiceManager1_1> defaultServiceManager1_1() {
Steven Moreland89909692018-05-30 14:11:19 -0700206 return defaultServiceManager1_2();
207}
Devin Mooree1973ce2022-04-15 20:54:03 +0000208static bool isServiceManager(const hidl_string& fqName) {
209 return fqName == IServiceManager1_0::descriptor || fqName == IServiceManager1_1::descriptor ||
210 fqName == IServiceManager1_2::descriptor;
211}
212static bool isHwServiceManagerInstalled() {
Devin Moore39ca6b72023-05-31 23:31:19 +0000213 return access("/system_ext/bin/hwservicemanager", F_OK) == 0 ||
Devin Moore49670672023-09-06 19:57:03 +0000214 access("/system/system_ext/bin/hwservicemanager", F_OK) == 0 ||
215 access("/system/bin/hwservicemanager", F_OK) == 0;
Devin Mooree1973ce2022-04-15 20:54:03 +0000216}
217
218/*
219 * A replacement for hwservicemanager when it is not installed on a device.
220 *
221 * Clients in the framework need to continue supporting HIDL services through
222 * hwservicemanager for upgrading devices. Being unable to get an instance of
223 * hardware service manager is a hard error, so this implementation is returned
224 * to be able service the requests and tell clients there are no services
225 * registered.
226 */
227struct NoHwServiceManager : public IServiceManager1_2 {
228 Return<sp<IBase>> get(const hidl_string& fqName, const hidl_string&) override {
229 sp<IBase> ret = nullptr;
230
231 if (isServiceManager(fqName)) {
232 ret = defaultServiceManager1_2();
233 }
234 return ret;
235 }
236
237 Return<bool> add(const hidl_string& name, const sp<IBase>& /* service */) override {
238 LOG(INFO) << "Cannot add " << name << " without hwservicemanager";
239 return false;
240 }
241
242 Return<Transport> getTransport(const hidl_string& fqName, const hidl_string& name) {
243 LOG(INFO) << "Trying to get transport of " << fqName << "/" << name
244 << " without hwservicemanager";
Devin Mooreadbf9a52023-06-06 23:07:08 +0000245 return Transport::EMPTY;
Devin Mooree1973ce2022-04-15 20:54:03 +0000246 }
247
248 Return<void> list(list_cb _hidl_cb) override {
249 _hidl_cb({});
250 LOG(INFO) << "Cannot list all services without hwservicemanager";
251 return Void();
252 }
253 Return<void> listByInterface(const hidl_string& fqName, listByInterface_cb _hidl_cb) override {
254 _hidl_cb({});
255 LOG(INFO) << "Cannot list service " << fqName << " without hwservicemanager";
256 return Void();
257 }
258
259 Return<bool> registerForNotifications(const hidl_string& fqName, const hidl_string& name,
260 const sp<IServiceNotification>& /* callback */) override {
261 LOG(INFO) << "Cannot register for notifications for " << fqName << "/" << name
262 << " without hwservicemanager";
263 return false;
264 }
265
266 Return<void> debugDump(debugDump_cb _hidl_cb) override {
267 _hidl_cb({});
268 return Void();
269 }
270
271 Return<void> registerPassthroughClient(const hidl_string& fqName,
272 const hidl_string& name) override {
273 LOG(INFO) << "This process is a client of " << fqName << "/" << name
274 << " passthrough HAL, but it won't show up in lshal because hwservicemanager is "
275 "not installed";
276 return Void();
277 }
278
279 Return<bool> unregisterForNotifications(
280 const hidl_string& fqName, const hidl_string& name,
281 const sp<IServiceNotification>& /* callback */) override {
282 LOG(INFO) << "Cannot unregister for notifications for " << fqName << "/" << name
283 << " without hwservicemanager";
284 return false;
285 }
286 Return<bool> registerClientCallback(const hidl_string& fqName, const hidl_string& name,
287 const sp<IBase>&, const sp<IClientCallback>&) {
288 LOG(INFO) << "Cannot add client callback for " << fqName << "/" << name
289 << " without hwservicemanager";
290 return false;
291 }
292 Return<bool> unregisterClientCallback(const sp<IBase>&, const sp<IClientCallback>&) {
293 LOG(INFO) << "Cannot unregister client callbacks without hwservicemanager";
294 return false;
295 }
296 Return<bool> addWithChain(const hidl_string& fqName, const sp<IBase>&,
297 const hidl_vec<hidl_string>&) {
298 LOG(INFO) << "Cannot add " << fqName << " with chain without hwservicemanager";
299 return false;
300 }
301 Return<void> listManifestByInterface(const hidl_string& fqName, listManifestByInterface_cb) {
302 LOG(INFO) << "Cannot list manifest for " << fqName << " without hwservicemanager";
303 return Void();
304 }
305 Return<bool> tryUnregister(const hidl_string& fqName, const hidl_string& name,
306 const sp<IBase>&) {
307 LOG(INFO) << "Cannot unregister service " << fqName << "/" << name
308 << " without hwservicemanager";
309 return false;
310 }
311};
312
Steven Moreland89909692018-05-30 14:11:19 -0700313sp<IServiceManager1_2> defaultServiceManager1_2() {
314 using android::hidl::manager::V1_2::BnHwServiceManager;
315 using android::hidl::manager::V1_2::BpHwServiceManager;
316
Steven Moreland10d55632020-04-21 14:10:53 -0700317 static std::mutex& gDefaultServiceManagerLock = *new std::mutex;
318 static sp<IServiceManager1_2>& gDefaultServiceManager = *new sp<IServiceManager1_2>;
Steven Moreland89909692018-05-30 14:11:19 -0700319
Steven Moreland5d5ef7f2016-10-20 19:19:55 -0700320 {
Steven Moreland89909692018-05-30 14:11:19 -0700321 std::lock_guard<std::mutex> _l(gDefaultServiceManagerLock);
322 if (gDefaultServiceManager != nullptr) {
323 return gDefaultServiceManager;
Yifan Hong8fb656b2017-03-16 14:30:40 -0700324 }
Steven Moreland405d7612017-04-07 20:31:22 -0700325
Devin Mooree1973ce2022-04-15 20:54:03 +0000326 if (!isHwServiceManagerInstalled()) {
327 // hwservicemanager is not available on this device.
Devin Moore91db8672023-09-01 16:36:30 +0000328 LOG(WARNING)
329 << "hwservicemanager is not installed on the device. If HIDL support "
330 << "is still needed, hwservicemanager and android.hidl.allocator@1.0-service "
331 << "need to be added to the device's PRODUCT_PACKAGES and the kernel config "
332 << "needs to have 'hwbinder' in CONFIG_ANDROID_BINDER_DEVICES.";
Devin Mooree1973ce2022-04-15 20:54:03 +0000333 gDefaultServiceManager = sp<NoHwServiceManager>::make();
334 return gDefaultServiceManager;
335 }
336
Yifan Hong8fb656b2017-03-16 14:30:40 -0700337 if (access("/dev/hwbinder", F_OK|R_OK|W_OK) != 0) {
338 // HwBinder not available on this device or not accessible to
339 // this process.
340 return nullptr;
341 }
Steven Morelandc1cee2c2017-03-24 16:23:11 +0000342
343 waitForHwServiceManager();
344
Steven Moreland89909692018-05-30 14:11:19 -0700345 while (gDefaultServiceManager == nullptr) {
346 gDefaultServiceManager =
347 fromBinder<IServiceManager1_2, BpHwServiceManager, BnHwServiceManager>(
348 ProcessState::self()->getContextObject(nullptr));
349 if (gDefaultServiceManager == nullptr) {
Steven Morelandc1cee2c2017-03-24 16:23:11 +0000350 LOG(ERROR) << "Waited for hwservicemanager, but got nullptr.";
Steven Moreland5d5ef7f2016-10-20 19:19:55 -0700351 sleep(1);
Yifan Hong8fb656b2017-03-16 14:30:40 -0700352 }
Steven Moreland5d5ef7f2016-10-20 19:19:55 -0700353 }
354 }
355
Steven Moreland89909692018-05-30 14:11:19 -0700356 return gDefaultServiceManager;
Steven Moreland5d5ef7f2016-10-20 19:19:55 -0700357}
358
Elliott Hughes19306142018-10-26 13:13:04 -0700359static std::vector<std::string> findFiles(const std::string& path, const std::string& prefix,
360 const std::string& suffix) {
Steven Moreland0091c092017-01-20 23:15:18 +0000361 std::unique_ptr<DIR, decltype(&closedir)> dir(opendir(path.c_str()), closedir);
362 if (!dir) return {};
363
364 std::vector<std::string> results{};
365
366 dirent* dp;
367 while ((dp = readdir(dir.get())) != nullptr) {
368 std::string name = dp->d_name;
369
Steven Moreland8092aa02018-10-12 15:54:56 -0700370 if (base::StartsWith(name, prefix) && base::EndsWith(name, suffix)) {
Steven Moreland0091c092017-01-20 23:15:18 +0000371 results.push_back(name);
372 }
373 }
374
375 return results;
376}
377
Steven Morelandccc3a972020-02-26 16:38:31 -0800378static bool matchPackageName(const std::string& lib, std::string* matchedName,
379 std::string* implName) {
380#define RE_COMPONENT "[a-zA-Z_][a-zA-Z_0-9]*"
381#define RE_PATH RE_COMPONENT "(?:[.]" RE_COMPONENT ")*"
382 static const std::regex gLibraryFileNamePattern("(" RE_PATH "@[0-9]+[.][0-9]+)-impl(.*?).so");
383#undef RE_PATH
384#undef RE_COMPONENT
385
Yifan Hong9a22d1d2017-01-25 14:19:26 -0800386 std::smatch match;
387 if (std::regex_match(lib, match, gLibraryFileNamePattern)) {
388 *matchedName = match.str(1) + "::I*";
Yifan Hongbd0d8f72017-05-24 19:43:51 -0700389 *implName = match.str(2);
Yifan Hong9a22d1d2017-01-25 14:19:26 -0800390 return true;
391 }
392 return false;
393}
394
Yifan Hong7f49f592017-02-03 15:11:44 -0800395static void registerReference(const hidl_string &interfaceName, const hidl_string &instanceName) {
Yifan Hongdeacf142018-07-10 17:33:34 -0700396 if (kIsRecovery) {
397 // No hwservicemanager in recovery.
398 return;
399 }
400
Steven Moreland2a2678e2017-07-21 18:07:38 -0700401 sp<IServiceManager1_0> binderizedManager = defaultServiceManager();
Yifan Hong7f49f592017-02-03 15:11:44 -0800402 if (binderizedManager == nullptr) {
403 LOG(WARNING) << "Could not registerReference for "
404 << interfaceName << "/" << instanceName
405 << ": null binderized manager.";
406 return;
407 }
Martijn Coenenaf4aba52017-04-27 09:41:13 -0700408 auto ret = binderizedManager->registerPassthroughClient(interfaceName, instanceName);
Yifan Hong7f49f592017-02-03 15:11:44 -0800409 if (!ret.isOk()) {
410 LOG(WARNING) << "Could not registerReference for "
411 << interfaceName << "/" << instanceName
412 << ": " << ret.description();
Steven Moreland0aeaa782017-03-22 08:11:07 -0700413 return;
Yifan Hong7f49f592017-02-03 15:11:44 -0800414 }
Steven Morelande681aa52017-02-15 16:22:37 -0800415 LOG(VERBOSE) << "Successfully registerReference for "
416 << interfaceName << "/" << instanceName;
Yifan Hong7f49f592017-02-03 15:11:44 -0800417}
418
Yifan Hongbd0d8f72017-05-24 19:43:51 -0700419using InstanceDebugInfo = hidl::manager::V1_0::IServiceManager::InstanceDebugInfo;
420static inline void fetchPidsForPassthroughLibraries(
421 std::map<std::string, InstanceDebugInfo>* infos) {
422 static const std::string proc = "/proc/";
423
424 std::map<std::string, std::set<pid_t>> pids;
425 std::unique_ptr<DIR, decltype(&closedir)> dir(opendir(proc.c_str()), closedir);
426 if (!dir) return;
427 dirent* dp;
428 while ((dp = readdir(dir.get())) != nullptr) {
Stephen Hinesfd9ecee2017-09-27 18:52:52 -0700429 pid_t pid = strtoll(dp->d_name, nullptr, 0);
Yifan Hongbd0d8f72017-05-24 19:43:51 -0700430 if (pid == 0) continue;
431 std::string mapsPath = proc + dp->d_name + "/maps";
432 std::ifstream ifs{mapsPath};
433 if (!ifs.is_open()) continue;
434
435 for (std::string line; std::getline(ifs, line);) {
436 // The last token of line should look like
437 // vendor/lib64/hw/android.hardware.foo@1.0-impl-extra.so
438 // Use some simple filters to ignore bad lines before extracting libFileName
439 // and checking the key in info to make parsing faster.
440 if (line.back() != 'o') continue;
441 if (line.rfind('@') == std::string::npos) continue;
442
443 auto spacePos = line.rfind(' ');
444 if (spacePos == std::string::npos) continue;
445 auto libFileName = line.substr(spacePos + 1);
446 auto it = infos->find(libFileName);
447 if (it == infos->end()) continue;
448 pids[libFileName].insert(pid);
449 }
450 }
451 for (auto& pair : *infos) {
452 pair.second.clientPids =
453 std::vector<pid_t>{pids[pair.first].begin(), pids[pair.first].end()};
454 }
455}
456
Steven Moreland2a2678e2017-07-21 18:07:38 -0700457struct PassthroughServiceManager : IServiceManager1_1 {
Chih-Hung Hsieh41649d52017-08-03 14:27:21 -0700458 static void openLibs(
459 const std::string& fqName,
460 const std::function<bool /* continue */ (void* /* handle */, const std::string& /* lib */,
461 const std::string& /* sym */)>& eachLib) {
Steven Moreland819c05d2017-04-06 17:24:22 -0700462 //fqName looks like android.hardware.foo@1.0::IFoo
Steven Moreland519306f2017-06-06 17:14:12 -0700463 size_t idx = fqName.find("::");
Steven Moreland819c05d2017-04-06 17:24:22 -0700464
465 if (idx == std::string::npos ||
Steven Moreland519306f2017-06-06 17:14:12 -0700466 idx + strlen("::") + 1 >= fqName.size()) {
Steven Moreland337e6b62017-01-18 17:25:13 -0800467 LOG(ERROR) << "Invalid interface name passthrough lookup: " << fqName;
Steven Moreland519306f2017-06-06 17:14:12 -0700468 return;
Steven Moreland337e6b62017-01-18 17:25:13 -0800469 }
470
Steven Moreland519306f2017-06-06 17:14:12 -0700471 std::string packageAndVersion = fqName.substr(0, idx);
472 std::string ifaceName = fqName.substr(idx + strlen("::"));
Steven Moreland819c05d2017-04-06 17:24:22 -0700473
474 const std::string prefix = packageAndVersion + "-impl";
475 const std::string sym = "HIDL_FETCH_" + ifaceName;
Steven Moreland348802d2017-02-23 12:48:55 -0800476
Steven Moreland77f4c852017-10-12 11:05:53 -0700477 constexpr int dlMode = RTLD_LAZY;
478 void* handle = nullptr;
Steven Moreland337e6b62017-01-18 17:25:13 -0800479
Steven Morelanda29905c2017-03-01 10:42:35 -0800480 dlerror(); // clear
481
Jooyung Han219106c2020-07-17 15:01:19 +0900482 static std::string halLibPathVndkSp = details::getVndkSpHwPath();
Justin Yun6a323ed2018-10-18 18:41:56 +0900483 std::vector<std::string> paths = {
484 HAL_LIBRARY_PATH_ODM, HAL_LIBRARY_PATH_VENDOR, halLibPathVndkSp,
485#ifndef __ANDROID_VNDK__
486 HAL_LIBRARY_PATH_SYSTEM,
487#endif
488 };
Steven Moreland77f4c852017-10-12 11:05:53 -0700489
Steven Morelandb939e7e2020-07-08 01:04:23 +0000490 if (details::isTrebleTestingOverride()) {
Steven Moreland77f4c852017-10-12 11:05:53 -0700491 // Load HAL implementations that are statically linked
492 handle = dlopen(nullptr, dlMode);
493 if (handle == nullptr) {
494 const char* error = dlerror();
495 LOG(ERROR) << "Failed to dlopen self: "
496 << (error == nullptr ? "unknown error" : error);
497 } else if (!eachLib(handle, "SELF", sym)) {
498 return;
499 }
Steven Morelandf7dea692017-07-14 12:49:28 -0700500 }
Steven Moreland77f4c852017-10-12 11:05:53 -0700501
Steven Morelandf7dea692017-07-14 12:49:28 -0700502 for (const std::string& path : paths) {
Elliott Hughes19306142018-10-26 13:13:04 -0700503 std::vector<std::string> libs = findFiles(path, prefix, ".so");
Steven Moreland0091c092017-01-20 23:15:18 +0000504
Steven Moreland0091c092017-01-20 23:15:18 +0000505 for (const std::string &lib : libs) {
Steven Moreland348802d2017-02-23 12:48:55 -0800506 const std::string fullPath = path + lib;
507
Yifan Hongdeacf142018-07-10 17:33:34 -0700508 if (kIsRecovery || path == HAL_LIBRARY_PATH_SYSTEM) {
Jiyong Park3ab546c2017-04-06 20:28:07 +0900509 handle = dlopen(fullPath.c_str(), dlMode);
Steven Moreland65c00cb2017-10-12 11:35:22 -0700510 } else {
Steven Moreland2056cf32019-09-17 17:41:02 -0700511#if !defined(__ANDROID_RECOVERY__) && defined(__ANDROID__)
Steven Moreland65c00cb2017-10-12 11:35:22 -0700512 handle = android_load_sphal_library(fullPath.c_str(), dlMode);
Jerry Zhang86ae9992018-05-30 17:11:09 -0700513#endif
Jiyong Park3ab546c2017-04-06 20:28:07 +0900514 }
515
Steven Moreland348802d2017-02-23 12:48:55 -0800516 if (handle == nullptr) {
517 const char* error = dlerror();
518 LOG(ERROR) << "Failed to dlopen " << lib << ": "
519 << (error == nullptr ? "unknown error" : error);
520 continue;
Steven Moreland0091c092017-01-20 23:15:18 +0000521 }
Steven Moreland348802d2017-02-23 12:48:55 -0800522
Steven Moreland519306f2017-06-06 17:14:12 -0700523 if (!eachLib(handle, lib, sym)) {
524 return;
Steven Moreland348802d2017-02-23 12:48:55 -0800525 }
Steven Moreland337e6b62017-01-18 17:25:13 -0800526 }
527 }
Steven Moreland519306f2017-06-06 17:14:12 -0700528 }
Steven Moreland337e6b62017-01-18 17:25:13 -0800529
Steven Moreland519306f2017-06-06 17:14:12 -0700530 Return<sp<IBase>> get(const hidl_string& fqName,
531 const hidl_string& name) override {
532 sp<IBase> ret = nullptr;
Devin Mooree1973ce2022-04-15 20:54:03 +0000533 // This is required to run without hwservicemanager while we have
534 // passthrough HIDL services. Once the passthrough HIDL services have
535 // been removed, the PassthroughServiceManager will no longer be needed.
536 if (!isHwServiceManagerInstalled() && isServiceManager(fqName)) {
537 return defaultServiceManager1_2();
538 }
Steven Moreland519306f2017-06-06 17:14:12 -0700539
540 openLibs(fqName, [&](void* handle, const std::string &lib, const std::string &sym) {
541 IBase* (*generator)(const char* name);
542 *(void **)(&generator) = dlsym(handle, sym.c_str());
543 if(!generator) {
544 const char* error = dlerror();
Steven Morelandedd49122021-02-17 22:41:51 +0000545 LOG(ERROR) << "Passthrough lookup opened " << lib << " but could not find symbol "
546 << sym << ": " << (error == nullptr ? "unknown error" : error)
547 << ". Keeping library open.";
548
549 // dlclose too problematic in multi-threaded environment
550 // dlclose(handle);
551
552 return true; // continue
Steven Moreland519306f2017-06-06 17:14:12 -0700553 }
554
555 ret = (*generator)(name.c_str());
556
557 if (ret == nullptr) {
Steven Morelandedd49122021-02-17 22:41:51 +0000558 LOG(ERROR) << "Could not find instance '" << name.c_str() << "' in library " << lib
559 << ". Keeping library open.";
560
561 // dlclose too problematic in multi-threaded environment
562 // dlclose(handle);
563
564 // this module doesn't provide this particular instance
565 return true; // continue
Steven Moreland519306f2017-06-06 17:14:12 -0700566 }
567
Steven Morelande69e8d32018-03-14 10:55:42 -0700568 // Actual fqname might be a subclass.
569 // This assumption is tested in vts_treble_vintf_test
570 using ::android::hardware::details::getDescriptor;
571 std::string actualFqName = getDescriptor(ret.get());
572 CHECK(actualFqName.size() > 0);
573 registerReference(actualFqName, name);
Steven Moreland519306f2017-06-06 17:14:12 -0700574 return false;
575 });
576
577 return ret;
Steven Moreland337e6b62017-01-18 17:25:13 -0800578 }
579
Martijn Coenen67a02492017-03-06 13:04:48 +0100580 Return<bool> add(const hidl_string& /* name */,
Steven Moreland337e6b62017-01-18 17:25:13 -0800581 const sp<IBase>& /* service */) override {
582 LOG(FATAL) << "Cannot register services with passthrough service manager.";
583 return false;
584 }
585
Steven Morelandc601c5f2017-04-06 09:26:07 -0700586 Return<Transport> getTransport(const hidl_string& /* fqName */,
587 const hidl_string& /* name */) {
588 LOG(FATAL) << "Cannot getTransport with passthrough service manager.";
589 return Transport::EMPTY;
590 }
591
Yifan Hong705e5da2017-03-02 16:59:39 -0800592 Return<void> list(list_cb /* _hidl_cb */) override {
593 LOG(FATAL) << "Cannot list services with passthrough service manager.";
Steven Moreland337e6b62017-01-18 17:25:13 -0800594 return Void();
595 }
596 Return<void> listByInterface(const hidl_string& /* fqInstanceName */,
597 listByInterface_cb /* _hidl_cb */) override {
598 // TODO: add this functionality
599 LOG(FATAL) << "Cannot list services with passthrough service manager.";
600 return Void();
601 }
602
603 Return<bool> registerForNotifications(const hidl_string& /* fqName */,
604 const hidl_string& /* name */,
605 const sp<IServiceNotification>& /* callback */) override {
606 // This makes no sense.
607 LOG(FATAL) << "Cannot register for notifications with passthrough service manager.";
608 return false;
609 }
610
Yifan Hong705e5da2017-03-02 16:59:39 -0800611 Return<void> debugDump(debugDump_cb _hidl_cb) override {
612 using Arch = ::android::hidl::base::V1_0::DebugInfo::Architecture;
Yifan Hongbd0d8f72017-05-24 19:43:51 -0700613 using std::literals::string_literals::operator""s;
Jooyung Han219106c2020-07-17 15:01:19 +0900614 static std::string halLibPathVndkSp64 = details::getVndkSpHwPath("lib64");
615 static std::string halLibPathVndkSp32 = details::getVndkSpHwPath("lib");
Jiyong Park56eb1492017-08-04 16:16:13 +0900616 static std::vector<std::pair<Arch, std::vector<const char*>>> sAllPaths{
617 {Arch::IS_64BIT,
Justin Yun6a323ed2018-10-18 18:41:56 +0900618 {
619 HAL_LIBRARY_PATH_ODM_64BIT, HAL_LIBRARY_PATH_VENDOR_64BIT,
620 halLibPathVndkSp64.c_str(),
621#ifndef __ANDROID_VNDK__
622 HAL_LIBRARY_PATH_SYSTEM_64BIT,
623#endif
624 }},
Jiyong Park56eb1492017-08-04 16:16:13 +0900625 {Arch::IS_32BIT,
Justin Yun6a323ed2018-10-18 18:41:56 +0900626 {
627 HAL_LIBRARY_PATH_ODM_32BIT, HAL_LIBRARY_PATH_VENDOR_32BIT,
628 halLibPathVndkSp32.c_str(),
629#ifndef __ANDROID_VNDK__
630 HAL_LIBRARY_PATH_SYSTEM_32BIT,
631#endif
632 }}};
Yifan Hongbd0d8f72017-05-24 19:43:51 -0700633 std::map<std::string, InstanceDebugInfo> map;
Yifan Hong705e5da2017-03-02 16:59:39 -0800634 for (const auto &pair : sAllPaths) {
635 Arch arch = pair.first;
636 for (const auto &path : pair.second) {
Elliott Hughes19306142018-10-26 13:13:04 -0700637 std::vector<std::string> libs = findFiles(path, "", ".so");
Yifan Hong705e5da2017-03-02 16:59:39 -0800638 for (const std::string &lib : libs) {
639 std::string matchedName;
Yifan Hongbd0d8f72017-05-24 19:43:51 -0700640 std::string implName;
641 if (matchPackageName(lib, &matchedName, &implName)) {
642 std::string instanceName{"* ("s + path + ")"s};
643 if (!implName.empty()) instanceName += " ("s + implName + ")"s;
644 map.emplace(path + lib, InstanceDebugInfo{.interfaceName = matchedName,
645 .instanceName = instanceName,
646 .clientPids = {},
647 .arch = arch});
Yifan Hong705e5da2017-03-02 16:59:39 -0800648 }
649 }
650 }
651 }
Yifan Hongbd0d8f72017-05-24 19:43:51 -0700652 fetchPidsForPassthroughLibraries(&map);
653 hidl_vec<InstanceDebugInfo> vec;
654 vec.resize(map.size());
655 size_t idx = 0;
656 for (auto&& pair : map) {
657 vec[idx++] = std::move(pair.second);
658 }
Yifan Hong705e5da2017-03-02 16:59:39 -0800659 _hidl_cb(vec);
Yifan Hong7f49f592017-02-03 15:11:44 -0800660 return Void();
661 }
662
Martijn Coenenaf4aba52017-04-27 09:41:13 -0700663 Return<void> registerPassthroughClient(const hidl_string &, const hidl_string &) override {
Yifan Hong7f49f592017-02-03 15:11:44 -0800664 // This makes no sense.
665 LOG(FATAL) << "Cannot call registerPassthroughClient on passthrough service manager. "
666 << "Call it on defaultServiceManager() instead.";
Yifan Hong9a22d1d2017-01-25 14:19:26 -0800667 return Void();
668 }
669
Steven Moreland2a2678e2017-07-21 18:07:38 -0700670 Return<bool> unregisterForNotifications(const hidl_string& /* fqName */,
671 const hidl_string& /* name */,
672 const sp<IServiceNotification>& /* callback */) override {
673 // This makes no sense.
674 LOG(FATAL) << "Cannot unregister for notifications with passthrough service manager.";
675 return false;
676 }
677
Steven Moreland337e6b62017-01-18 17:25:13 -0800678};
679
Steven Moreland2a2678e2017-07-21 18:07:38 -0700680sp<IServiceManager1_0> getPassthroughServiceManager() {
681 return getPassthroughServiceManager1_1();
682}
683sp<IServiceManager1_1> getPassthroughServiceManager1_1() {
Steven Moreland337e6b62017-01-18 17:25:13 -0800684 static sp<PassthroughServiceManager> manager(new PassthroughServiceManager());
685 return manager;
686}
687
Steven Moreland92b247c2019-08-21 14:53:11 -0700688std::vector<std::string> getAllHalInstanceNames(const std::string& descriptor) {
689 std::vector<std::string> ret;
690 auto sm = defaultServiceManager1_2();
691 sm->listManifestByInterface(descriptor, [&](const auto& instances) {
692 ret.reserve(instances.size());
693 for (const auto& i : instances) {
694 ret.push_back(i);
695 }
696 });
697 return ret;
698}
699
Steven Morelandcbefd352017-01-23 20:29:05 -0800700namespace details {
701
Steven Moreland519306f2017-06-06 17:14:12 -0700702void preloadPassthroughService(const std::string &descriptor) {
703 PassthroughServiceManager::openLibs(descriptor,
704 [&](void* /* handle */, const std::string& /* lib */, const std::string& /* sym */) {
705 // do nothing
706 return true; // open all libs
707 });
708}
709
Steven Morelandcbefd352017-01-23 20:29:05 -0800710struct Waiter : IServiceNotification {
Martijn Coenen773609e2017-10-24 09:57:53 +0200711 Waiter(const std::string& interface, const std::string& instanceName,
712 const sp<IServiceManager1_1>& sm) : mInterfaceName(interface),
713 mInstanceName(instanceName), mSm(sm) {
Martijn Coenene6cdfd32017-11-13 14:03:05 +0100714 }
715
716 void onFirstRef() override {
Martijn Coenenb88ae7f2017-10-24 11:45:41 +0200717 // If this process only has one binder thread, and we're calling wait() from
718 // that thread, it will block forever because we hung up the one and only
719 // binder thread on a condition variable that can only be notified by an
720 // incoming binder call.
Tobias Lindskog88e886f2018-01-05 10:32:43 +0100721 if (IPCThreadState::self()->isOnlyBinderThread()) {
Martijn Coenenb88ae7f2017-10-24 11:45:41 +0200722 LOG(WARNING) << "Can't efficiently wait for " << mInterfaceName << "/"
723 << mInstanceName << ", because we are called from "
724 << "the only binder thread in this process.";
725 return;
726 }
727
Martijn Coenene6cdfd32017-11-13 14:03:05 +0100728 Return<bool> ret = mSm->registerForNotifications(mInterfaceName, mInstanceName, this);
Martijn Coenen773609e2017-10-24 09:57:53 +0200729
730 if (!ret.isOk()) {
731 LOG(ERROR) << "Transport error, " << ret.description()
Martijn Coenene6cdfd32017-11-13 14:03:05 +0100732 << ", during notification registration for " << mInterfaceName << "/"
733 << mInstanceName << ".";
Martijn Coenen773609e2017-10-24 09:57:53 +0200734 return;
735 }
736
737 if (!ret) {
Martijn Coenene6cdfd32017-11-13 14:03:05 +0100738 LOG(ERROR) << "Could not register for notifications for " << mInterfaceName << "/"
739 << mInstanceName << ".";
Martijn Coenen773609e2017-10-24 09:57:53 +0200740 return;
741 }
742
743 mRegisteredForNotifications = true;
744 }
745
746 ~Waiter() {
Martijn Coenen3fa15c22018-02-10 11:30:00 +0100747 if (!mDoneCalled) {
748 LOG(FATAL)
749 << "Waiter still registered for notifications, call done() before dropping ref!";
Martijn Coenen773609e2017-10-24 09:57:53 +0200750 }
751 }
752
Steven Morelandcbefd352017-01-23 20:29:05 -0800753 Return<void> onRegistration(const hidl_string& /* fqName */,
754 const hidl_string& /* name */,
755 bool /* preexisting */) override {
756 std::unique_lock<std::mutex> lock(mMutex);
757 if (mRegistered) {
758 return Void();
759 }
760 mRegistered = true;
761 lock.unlock();
762
763 mCondition.notify_one();
764 return Void();
765 }
766
Steven Moreland0771d8a2018-05-09 13:29:14 -0700767 void wait(bool timeout) {
Steven Moreland49605102017-03-28 09:33:06 -0700768 using std::literals::chrono_literals::operator""s;
769
Martijn Coenen773609e2017-10-24 09:57:53 +0200770 if (!mRegisteredForNotifications) {
771 // As an alternative, just sleep for a second and return
772 LOG(WARNING) << "Waiting one second for " << mInterfaceName << "/" << mInstanceName;
773 sleep(1);
774 return;
775 }
776
Steven Morelandcbefd352017-01-23 20:29:05 -0800777 std::unique_lock<std::mutex> lock(mMutex);
Steven Moreland0771d8a2018-05-09 13:29:14 -0700778 do {
Steven Moreland49605102017-03-28 09:33:06 -0700779 mCondition.wait_for(lock, 1s, [this]{
780 return mRegistered;
781 });
782
783 if (mRegistered) {
784 break;
785 }
786
Steven Moreland0771d8a2018-05-09 13:29:14 -0700787 LOG(WARNING) << "Waited one second for " << mInterfaceName << "/" << mInstanceName;
788 } while (!timeout);
Steven Morelandcbefd352017-01-23 20:29:05 -0800789 }
790
Martijn Coenen773609e2017-10-24 09:57:53 +0200791 // Be careful when using this; after calling reset(), you must always try to retrieve
792 // the corresponding service before blocking on the waiter; otherwise, you might run
793 // into a race-condition where the service has just (re-)registered, you clear the state
794 // here, and subsequently calling waiter->wait() will block forever.
795 void reset() {
796 std::unique_lock<std::mutex> lock(mMutex);
797 mRegistered = false;
798 }
Martijn Coenen3fa15c22018-02-10 11:30:00 +0100799
800 // done() must be called before dropping the last strong ref to the Waiter, to make
801 // sure we can properly unregister with hwservicemanager.
802 void done() {
803 if (mRegisteredForNotifications) {
804 if (!mSm->unregisterForNotifications(mInterfaceName, mInstanceName, this)
805 .withDefault(false)) {
806 LOG(ERROR) << "Could not unregister service notification for " << mInterfaceName
807 << "/" << mInstanceName << ".";
808 } else {
809 mRegisteredForNotifications = false;
810 }
811 }
812 mDoneCalled = true;
813 }
814
815 private:
Martijn Coenen773609e2017-10-24 09:57:53 +0200816 const std::string mInterfaceName;
817 const std::string mInstanceName;
Steven Morelandad1f9922018-10-02 19:37:05 -0700818 sp<IServiceManager1_1> mSm;
Steven Morelandcbefd352017-01-23 20:29:05 -0800819 std::mutex mMutex;
820 std::condition_variable mCondition;
821 bool mRegistered = false;
Martijn Coenen773609e2017-10-24 09:57:53 +0200822 bool mRegisteredForNotifications = false;
Martijn Coenen3fa15c22018-02-10 11:30:00 +0100823 bool mDoneCalled = false;
Steven Morelandcbefd352017-01-23 20:29:05 -0800824};
825
826void waitForHwService(
827 const std::string &interface, const std::string &instanceName) {
Martijn Coenen773609e2017-10-24 09:57:53 +0200828 sp<Waiter> waiter = new Waiter(interface, instanceName, defaultServiceManager1_1());
Steven Moreland0771d8a2018-05-09 13:29:14 -0700829 waiter->wait(false /* timeout */);
Martijn Coenen3fa15c22018-02-10 11:30:00 +0100830 waiter->done();
Martijn Coenen773609e2017-10-24 09:57:53 +0200831}
Steven Morelandcbefd352017-01-23 20:29:05 -0800832
Martijn Coenen773609e2017-10-24 09:57:53 +0200833// Prints relevant error/warning messages for error return values from
834// details::canCastInterface(), both transaction errors (!castReturn.isOk())
835// as well as actual cast failures (castReturn.isOk() && castReturn = false).
836// Returns 'true' if the error is non-fatal and it's useful to retry
837bool handleCastError(const Return<bool>& castReturn, const std::string& descriptor,
838 const std::string& instance) {
839 if (castReturn.isOk()) {
840 if (castReturn) {
841 details::logAlwaysFatal("Successful cast value passed into handleCastError.");
842 }
843 // This should never happen, and there's not really a point in retrying.
844 ALOGE("getService: received incompatible service (bug in hwservicemanager?) for "
845 "%s/%s.", descriptor.c_str(), instance.c_str());
846 return false;
Steven Morelandcbefd352017-01-23 20:29:05 -0800847 }
Martijn Coenen773609e2017-10-24 09:57:53 +0200848 if (castReturn.isDeadObject()) {
849 ALOGW("getService: found dead hwbinder service for %s/%s.", descriptor.c_str(),
850 instance.c_str());
851 return true;
Steven Morelandcbefd352017-01-23 20:29:05 -0800852 }
Martijn Coenen773609e2017-10-24 09:57:53 +0200853 // This can happen due to:
854 // 1) No SELinux permissions
855 // 2) Other transaction failure (no buffer space, kernel error)
856 // The first isn't recoverable, but the second is.
857 // Since we can't yet differentiate between the two, and clients depend
858 // on us not blocking in case 1), treat this as a fatal error for now.
859 ALOGW("getService: unable to call into hwbinder service for %s/%s.",
860 descriptor.c_str(), instance.c_str());
861 return false;
Steven Morelandcbefd352017-01-23 20:29:05 -0800862}
863
Martijn Coenen86c3abb2017-10-24 09:33:28 +0200864sp<::android::hidl::base::V1_0::IBase> getRawServiceInternal(const std::string& descriptor,
865 const std::string& instance,
866 bool retry, bool getStub) {
Steven Moreland84fe49e2019-08-20 17:47:26 +0000867 using Transport = IServiceManager1_0::Transport;
Martijn Coenend35678f2018-03-07 09:51:01 +0100868 sp<Waiter> waiter;
Martijn Coenen86c3abb2017-10-24 09:33:28 +0200869
Yifan Hongdeacf142018-07-10 17:33:34 -0700870 sp<IServiceManager1_1> sm;
871 Transport transport = Transport::EMPTY;
872 if (kIsRecovery) {
Yifan Hongdeacf142018-07-10 17:33:34 -0700873 transport = Transport::PASSTHROUGH;
Yifan Hongdeacf142018-07-10 17:33:34 -0700874 } else {
875 sm = defaultServiceManager1_1();
876 if (sm == nullptr) {
877 ALOGE("getService: defaultServiceManager() is null");
878 return nullptr;
879 }
880
881 Return<Transport> transportRet = sm->getTransport(descriptor, instance);
882
883 if (!transportRet.isOk()) {
884 ALOGE("getService: defaultServiceManager()->getTransport returns %s",
885 transportRet.description().c_str());
886 return nullptr;
887 }
888 transport = transportRet;
Martijn Coenen86c3abb2017-10-24 09:33:28 +0200889 }
890
Martijn Coenen86c3abb2017-10-24 09:33:28 +0200891 const bool vintfHwbinder = (transport == Transport::HWBINDER);
892 const bool vintfPassthru = (transport == Transport::PASSTHROUGH);
Steven Moreland84fe49e2019-08-20 17:47:26 +0000893 const bool trebleTestingOverride = isTrebleTestingOverride();
Steven Morelandf47cdf72021-04-07 22:13:13 +0000894 const bool allowLegacy = !kEnforceVintfManifest || (trebleTestingOverride && isDebuggable());
Steven Moreland84fe49e2019-08-20 17:47:26 +0000895 const bool vintfLegacy = (transport == Transport::EMPTY) && allowLegacy;
Martijn Coenen86c3abb2017-10-24 09:33:28 +0200896
Steven Moreland84fe49e2019-08-20 17:47:26 +0000897 if (!kEnforceVintfManifest) {
898 ALOGE("getService: Potential race detected. The VINTF manifest is not being enforced. If "
899 "a HAL server has a delay in starting and it is not in the manifest, it will not be "
900 "retrieved. Please make sure all HALs on this device are in the VINTF manifest and "
901 "enable PRODUCT_ENFORCE_VINTF_MANIFEST on this device (this is also enabled by "
902 "PRODUCT_FULL_TREBLE). PRODUCT_ENFORCE_VINTF_MANIFEST will ensure that no race "
903 "condition is possible here.");
Steven Morelandaadec3c2019-08-22 17:06:48 -0700904 sleep(1);
Steven Moreland84fe49e2019-08-20 17:47:26 +0000905 }
Martijn Coenen86c3abb2017-10-24 09:33:28 +0200906
Steven Moreland76371242018-04-19 17:11:39 -0700907 for (int tries = 0; !getStub && (vintfHwbinder || vintfLegacy); tries++) {
908 if (waiter == nullptr && tries > 0) {
Martijn Coenend35678f2018-03-07 09:51:01 +0100909 waiter = new Waiter(descriptor, instance, sm);
910 }
Steven Moreland76371242018-04-19 17:11:39 -0700911 if (waiter != nullptr) {
912 waiter->reset(); // don't reorder this -- see comments on reset()
913 }
Martijn Coenen86c3abb2017-10-24 09:33:28 +0200914 Return<sp<IBase>> ret = sm->get(descriptor, instance);
915 if (!ret.isOk()) {
916 ALOGE("getService: defaultServiceManager()->get returns %s for %s/%s.",
917 ret.description().c_str(), descriptor.c_str(), instance.c_str());
918 break;
919 }
920 sp<IBase> base = ret;
Martijn Coenen773609e2017-10-24 09:57:53 +0200921 if (base != nullptr) {
922 Return<bool> canCastRet =
923 details::canCastInterface(base.get(), descriptor.c_str(), true /* emitError */);
924
925 if (canCastRet.isOk() && canCastRet) {
Steven Moreland76371242018-04-19 17:11:39 -0700926 if (waiter != nullptr) {
927 waiter->done();
928 }
Martijn Coenen773609e2017-10-24 09:57:53 +0200929 return base; // still needs to be wrapped by Bp class.
Martijn Coenen86c3abb2017-10-24 09:33:28 +0200930 }
Martijn Coenen773609e2017-10-24 09:57:53 +0200931
932 if (!handleCastError(canCastRet, descriptor, instance)) break;
Martijn Coenen86c3abb2017-10-24 09:33:28 +0200933 }
934
Martijn Coenen773609e2017-10-24 09:57:53 +0200935 // In case of legacy or we were not asked to retry, don't.
936 if (vintfLegacy || !retry) break;
Martijn Coenen86c3abb2017-10-24 09:33:28 +0200937
Steven Moreland76371242018-04-19 17:11:39 -0700938 if (waiter != nullptr) {
939 ALOGI("getService: Trying again for %s/%s...", descriptor.c_str(), instance.c_str());
Steven Moreland0771d8a2018-05-09 13:29:14 -0700940 waiter->wait(true /* timeout */);
Steven Moreland76371242018-04-19 17:11:39 -0700941 }
Martijn Coenen86c3abb2017-10-24 09:33:28 +0200942 }
943
Martijn Coenend35678f2018-03-07 09:51:01 +0100944 if (waiter != nullptr) {
945 waiter->done();
946 }
Martijn Coenen3fa15c22018-02-10 11:30:00 +0100947
Martijn Coenen86c3abb2017-10-24 09:33:28 +0200948 if (getStub || vintfPassthru || vintfLegacy) {
Steven Moreland84fe49e2019-08-20 17:47:26 +0000949 const sp<IServiceManager1_0> pm = getPassthroughServiceManager();
Martijn Coenen86c3abb2017-10-24 09:33:28 +0200950 if (pm != nullptr) {
951 sp<IBase> base = pm->get(descriptor, instance).withDefault(nullptr);
952 if (!getStub || trebleTestingOverride) {
953 base = wrapPassthrough(base);
954 }
955 return base;
956 }
957 }
958
959 return nullptr;
960}
961
Steven Morelandbb9eb2a2018-10-11 12:10:01 -0700962status_t registerAsServiceInternal(const sp<IBase>& service, const std::string& name) {
963 if (service == nullptr) {
964 return UNEXPECTED_NULL;
965 }
966
967 sp<IServiceManager1_2> sm = defaultServiceManager1_2();
968 if (sm == nullptr) {
969 return INVALID_OPERATION;
970 }
971
Steven Moreland84fe49e2019-08-20 17:47:26 +0000972 const std::string descriptor = getDescriptor(service.get());
973
974 if (kEnforceVintfManifest && !isTrebleTestingOverride()) {
975 using Transport = IServiceManager1_0::Transport;
Steven Moreland7d1b0732021-01-21 22:47:40 +0000976 Return<Transport> transport = sm->getTransport(descriptor, name);
977
978 if (!transport.isOk()) {
979 LOG(ERROR) << "Could not get transport for " << descriptor << "/" << name << ": "
980 << transport.description();
981 return UNKNOWN_ERROR;
982 }
Steven Moreland84fe49e2019-08-20 17:47:26 +0000983
984 if (transport != Transport::HWBINDER) {
985 LOG(ERROR) << "Service " << descriptor << "/" << name
986 << " must be in VINTF manifest in order to register/get.";
987 return UNKNOWN_ERROR;
988 }
989 }
990
Steven Morelandbb9eb2a2018-10-11 12:10:01 -0700991 bool registered = false;
992 Return<void> ret = service->interfaceChain([&](const auto& chain) {
993 registered = sm->addWithChain(name.c_str(), service, chain).withDefault(false);
994 });
995
996 if (!ret.isOk()) {
997 LOG(ERROR) << "Could not retrieve interface chain: " << ret.description();
998 }
999
1000 if (registered) {
Steven Moreland84fe49e2019-08-20 17:47:26 +00001001 onRegistrationImpl(descriptor, name);
Steven Morelandbb9eb2a2018-10-11 12:10:01 -07001002 }
1003
1004 return registered ? OK : UNKNOWN_ERROR;
1005}
1006
Bernhard Rosenkränzer0c28fd22018-06-04 16:01:47 +02001007} // namespace details
Steven Morelandcbefd352017-01-23 20:29:05 -08001008
Bernhard Rosenkränzer0c28fd22018-06-04 16:01:47 +02001009} // namespace hardware
1010} // namespace android