blob: 8c9bc5bd61dcfd833643b8bb2c402b5d4ec70727 [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__
Devin Moore22e6f1e2023-11-22 22:29:54 +000020#include <android/api-level.h>
Jiyong Park3ab546c2017-04-06 20:28:07 +090021#include <android/dlext.h>
Steven Moreland2056cf32019-09-17 17:41:02 -070022#endif // __ANDROID__
23
Yifan Hong9a22d1d2017-01-25 14:19:26 -080024#include <condition_variable>
25#include <dlfcn.h>
26#include <dirent.h>
Steven Moreland405d7612017-04-07 20:31:22 -070027#include <fstream>
28#include <pthread.h>
Yifan Hong9a22d1d2017-01-25 14:19:26 -080029#include <unistd.h>
30
31#include <mutex>
32#include <regex>
Yifan Hongbd0d8f72017-05-24 19:43:51 -070033#include <set>
Yifan Hong9a22d1d2017-01-25 14:19:26 -080034
Martijn Coenen12f04d92016-12-07 17:29:41 +010035#include <hidl/HidlBinderSupport.h>
Justin Yun1f048102017-12-01 15:30:08 +090036#include <hidl/HidlInternal.h>
Steven Morelande69e8d32018-03-14 10:55:42 -070037#include <hidl/HidlTransportUtils.h>
Steven Moreland5d5ef7f2016-10-20 19:19:55 -070038#include <hidl/ServiceManagement.h>
Steven Moreland5d5ef7f2016-10-20 19:19:55 -070039#include <hidl/Status.h>
Peter Kalauskas64ffced2018-09-05 16:41:08 -070040#include <utils/SystemClock.h>
Steven Moreland5d5ef7f2016-10-20 19:19:55 -070041
Peter Kalauskas64ffced2018-09-05 16:41:08 -070042#include <android-base/file.h>
Steven Moreland337e6b62017-01-18 17:25:13 -080043#include <android-base/logging.h>
Peter Kalauskas64ffced2018-09-05 16:41:08 -070044#include <android-base/parseint.h>
Steven Morelandc1cee2c2017-03-24 16:23:11 +000045#include <android-base/properties.h>
Justin Yun1f048102017-12-01 15:30:08 +090046#include <android-base/stringprintf.h>
Peter Kalauskas64ffced2018-09-05 16:41:08 -070047#include <android-base/strings.h>
Steven Moreland5d5ef7f2016-10-20 19:19:55 -070048#include <hwbinder/IPCThreadState.h>
49#include <hwbinder/Parcel.h>
Steven Moreland2056cf32019-09-17 17:41:02 -070050#if !defined(__ANDROID_RECOVERY__) && defined(__ANDROID__)
Jiyong Parkba8ace12017-05-15 15:44:39 +090051#include <vndksupport/linker.h>
Jerry Zhang86ae9992018-05-30 17:11:09 -070052#endif
Steven Moreland5d5ef7f2016-10-20 19:19:55 -070053
Steven Moreland89909692018-05-30 14:11:19 -070054#include <android/hidl/manager/1.2/BnHwServiceManager.h>
55#include <android/hidl/manager/1.2/BpHwServiceManager.h>
56#include <android/hidl/manager/1.2/IServiceManager.h>
Steven Moreland5d5ef7f2016-10-20 19:19:55 -070057
Steven Morelandbb9eb2a2018-10-11 12:10:01 -070058using ::android::hidl::base::V1_0::IBase;
Steven Moreland2a2678e2017-07-21 18:07:38 -070059using IServiceManager1_0 = android::hidl::manager::V1_0::IServiceManager;
60using IServiceManager1_1 = android::hidl::manager::V1_1::IServiceManager;
Steven Moreland89909692018-05-30 14:11:19 -070061using IServiceManager1_2 = android::hidl::manager::V1_2::IServiceManager;
Steven Morelandbb9eb2a2018-10-11 12:10:01 -070062using ::android::hidl::manager::V1_0::IServiceNotification;
Devin Mooree1973ce2022-04-15 20:54:03 +000063using ::android::hidl::manager::V1_2::IClientCallback;
Steven Moreland5d5ef7f2016-10-20 19:19:55 -070064
65namespace android {
66namespace hardware {
67
Yifan Hongdeacf142018-07-10 17:33:34 -070068#if defined(__ANDROID_RECOVERY__)
69static constexpr bool kIsRecovery = true;
70#else
71static constexpr bool kIsRecovery = false;
72#endif
73
Steven Morelandbb9eb2a2018-10-11 12:10:01 -070074static void waitForHwServiceManager() {
Steven Moreland2056cf32019-09-17 17:41:02 -070075 // TODO(b/31559095): need bionic host so that we can use 'prop_info' returned
76 // from WaitForProperty
77#ifdef __ANDROID__
78 static const char* kHwServicemanagerReadyProperty = "hwservicemanager.ready";
79
Steven Morelandc1cee2c2017-03-24 16:23:11 +000080 using std::literals::chrono_literals::operator""s;
81
Steven Moreland2056cf32019-09-17 17:41:02 -070082 using android::base::WaitForProperty;
Steven Morelandc1cee2c2017-03-24 16:23:11 +000083 while (!WaitForProperty(kHwServicemanagerReadyProperty, "true", 1s)) {
84 LOG(WARNING) << "Waited for hwservicemanager.ready for a second, waiting another...";
85 }
Steven Moreland2056cf32019-09-17 17:41:02 -070086#endif // __ANDROID__
Steven Morelandc1cee2c2017-03-24 16:23:11 +000087}
88
Steven Morelandbb9eb2a2018-10-11 12:10:01 -070089static std::string binaryName() {
Steven Moreland405d7612017-04-07 20:31:22 -070090 std::ifstream ifs("/proc/self/cmdline");
91 std::string cmdline;
Steven Morelandd682c0e2020-07-21 17:47:32 +000092 if (!ifs) {
Steven Moreland405d7612017-04-07 20:31:22 -070093 return "";
94 }
95 ifs >> cmdline;
96
Chih-Hung Hsieh41649d52017-08-03 14:27:21 -070097 size_t idx = cmdline.rfind('/');
Steven Moreland405d7612017-04-07 20:31:22 -070098 if (idx != std::string::npos) {
99 cmdline = cmdline.substr(idx + 1);
100 }
101
102 return cmdline;
103}
104
Steven Morelandbb9eb2a2018-10-11 12:10:01 -0700105static std::string packageWithoutVersion(const std::string& packageAndVersion) {
Steven Moreland7d09a402018-04-06 10:44:05 -0700106 size_t at = packageAndVersion.find('@');
107 if (at == std::string::npos) return packageAndVersion;
108 return packageAndVersion.substr(0, at);
109}
110
Steven Morelandd682c0e2020-07-21 17:47:32 +0000111__attribute__((noinline)) static void tryShortenProcessName(const std::string& descriptor) {
Steven Moreland7d09a402018-04-06 10:44:05 -0700112 const static std::string kTasks = "/proc/self/task/";
113
114 // make sure that this binary name is in the same package
Steven Moreland405d7612017-04-07 20:31:22 -0700115 std::string processName = binaryName();
116
Steven Moreland7d09a402018-04-06 10:44:05 -0700117 // e.x. android.hardware.foo is this package
Steven Moreland8092aa02018-10-12 15:54:56 -0700118 if (!base::StartsWith(packageWithoutVersion(processName), packageWithoutVersion(descriptor))) {
Steven Moreland405d7612017-04-07 20:31:22 -0700119 return;
120 }
121
Steven Morelandbb9eb2a2018-10-11 12:10:01 -0700122 // e.x. android.hardware.module.foo@1.2::IFoo -> foo@1.2
123 size_t lastDot = descriptor.rfind('.');
Steven Moreland7d09a402018-04-06 10:44:05 -0700124 if (lastDot == std::string::npos) return;
Steven Morelandbb9eb2a2018-10-11 12:10:01 -0700125 size_t secondDot = descriptor.rfind('.', lastDot - 1);
Steven Moreland7d09a402018-04-06 10:44:05 -0700126 if (secondDot == std::string::npos) return;
Steven Moreland405d7612017-04-07 20:31:22 -0700127
Steven Moreland7d09a402018-04-06 10:44:05 -0700128 std::string newName = processName.substr(secondDot + 1, std::string::npos);
129 ALOGI("Removing namespace from process name %s to %s.", processName.c_str(), newName.c_str());
130
131 std::unique_ptr<DIR, decltype(&closedir)> dir(opendir(kTasks.c_str()), closedir);
132 if (dir == nullptr) return;
133
134 dirent* dp;
135 while ((dp = readdir(dir.get())) != nullptr) {
136 if (dp->d_type != DT_DIR) continue;
137 if (dp->d_name[0] == '.') continue;
138
139 std::fstream fs(kTasks + dp->d_name + "/comm");
Steven Morelandd682c0e2020-07-21 17:47:32 +0000140 if (!fs) {
Steven Moreland7d09a402018-04-06 10:44:05 -0700141 ALOGI("Could not rename process, failed read comm for %s.", dp->d_name);
142 continue;
143 }
144
145 std::string oldComm;
Steven Morelandd682c0e2020-07-21 17:47:32 +0000146 if (!(fs >> oldComm)) continue;
Steven Moreland7d09a402018-04-06 10:44:05 -0700147
148 // don't rename if it already has an explicit name
Steven Moreland8092aa02018-10-12 15:54:56 -0700149 if (base::StartsWith(descriptor, oldComm)) {
Steven Morelandd682c0e2020-07-21 17:47:32 +0000150 if (!fs.seekg(0, fs.beg)) continue;
Steven Moreland7d09a402018-04-06 10:44:05 -0700151 fs << newName;
152 }
Steven Moreland405d7612017-04-07 20:31:22 -0700153 }
Steven Moreland405d7612017-04-07 20:31:22 -0700154}
155
156namespace details {
157
Steven Morelandb939e7e2020-07-08 01:04:23 +0000158#ifdef ENFORCE_VINTF_MANIFEST
159static constexpr bool kEnforceVintfManifest = true;
160#else
161static constexpr bool kEnforceVintfManifest = false;
162#endif
163
Steven Moreland206d2812020-07-08 21:21:48 +0000164static bool* getTrebleTestingOverridePtr() {
165 static bool gTrebleTestingOverride = false;
166 return &gTrebleTestingOverride;
167}
Steven Morelandb939e7e2020-07-08 01:04:23 +0000168
169void setTrebleTestingOverride(bool testingOverride) {
Steven Moreland206d2812020-07-08 21:21:48 +0000170 *getTrebleTestingOverridePtr() = testingOverride;
Steven Morelandb939e7e2020-07-08 01:04:23 +0000171}
172
Steven Morelandf47cdf72021-04-07 22:13:13 +0000173static bool isDebuggable() {
174 static bool debuggable = base::GetBoolProperty("ro.debuggable", false);
175 return debuggable;
176}
177
Steven Morelandb939e7e2020-07-08 01:04:23 +0000178static inline bool isTrebleTestingOverride() {
Steven Moreland4acdab72022-08-09 16:27:46 +0000179 // return false early so we don't need to check the debuggable property
180 if (!*getTrebleTestingOverridePtr()) return false;
181
Steven Morelandf47cdf72021-04-07 22:13:13 +0000182 if (kEnforceVintfManifest && !isDebuggable()) {
Steven Morelandb939e7e2020-07-08 01:04:23 +0000183 // don't allow testing override in production
184 return false;
185 }
186
Steven Moreland4acdab72022-08-09 16:27:46 +0000187 return true;
Steven Morelandb939e7e2020-07-08 01:04:23 +0000188}
189
Steven Morelandbb9eb2a2018-10-11 12:10:01 -0700190static void onRegistrationImpl(const std::string& descriptor, const std::string& instanceName) {
Steven Moreland489e7572021-01-21 19:02:54 +0000191 LOG(INFO) << "Registered " << descriptor << "/" << instanceName;
Steven Morelandbb9eb2a2018-10-11 12:10:01 -0700192 tryShortenProcessName(descriptor);
193}
194
Steven Moreland911852c2020-05-11 10:51:26 -0700195// only used by prebuilts - should be able to remove
Steven Morelandbb9eb2a2018-10-11 12:10:01 -0700196void onRegistration(const std::string& packageName, const std::string& interfaceName,
197 const std::string& instanceName) {
198 return onRegistrationImpl(packageName + "::" + interfaceName, instanceName);
Steven Moreland405d7612017-04-07 20:31:22 -0700199}
200
201} // details
202
Steven Moreland2a2678e2017-07-21 18:07:38 -0700203sp<IServiceManager1_0> defaultServiceManager() {
Steven Moreland89909692018-05-30 14:11:19 -0700204 return defaultServiceManager1_2();
Steven Moreland2a2678e2017-07-21 18:07:38 -0700205}
206sp<IServiceManager1_1> defaultServiceManager1_1() {
Steven Moreland89909692018-05-30 14:11:19 -0700207 return defaultServiceManager1_2();
208}
Devin Mooree1973ce2022-04-15 20:54:03 +0000209static bool isServiceManager(const hidl_string& fqName) {
210 return fqName == IServiceManager1_0::descriptor || fqName == IServiceManager1_1::descriptor ||
211 fqName == IServiceManager1_2::descriptor;
212}
Devin Moore22e6f1e2023-11-22 22:29:54 +0000213
214bool isHidlSupported() {
215#ifdef __ANDROID__
216 // TODO(b/218588089) remove this temporary support variable once Cuttlefish
217 // (the only current Android V launching device) no longer requires HIDL.
218 constexpr bool kTempHidlSupport = true;
219 static const char* kVendorApiProperty = "ro.vendor.api_level";
220 // HIDL and hwservicemanager are not supported in Android V+
221 return android::base::GetIntProperty(kVendorApiProperty, 0) < __ANDROID_API_V__ ||
222 kTempHidlSupport;
223#else
224 // No access to properties and no requirement for dropping HIDL support if
225 // this isn't Android
226 return true;
227#endif // __ANDROID__
Devin Mooree1973ce2022-04-15 20:54:03 +0000228}
229
230/*
231 * A replacement for hwservicemanager when it is not installed on a device.
232 *
233 * Clients in the framework need to continue supporting HIDL services through
234 * hwservicemanager for upgrading devices. Being unable to get an instance of
235 * hardware service manager is a hard error, so this implementation is returned
236 * to be able service the requests and tell clients there are no services
237 * registered.
238 */
239struct NoHwServiceManager : public IServiceManager1_2 {
240 Return<sp<IBase>> get(const hidl_string& fqName, const hidl_string&) override {
241 sp<IBase> ret = nullptr;
242
243 if (isServiceManager(fqName)) {
244 ret = defaultServiceManager1_2();
245 }
246 return ret;
247 }
248
249 Return<bool> add(const hidl_string& name, const sp<IBase>& /* service */) override {
250 LOG(INFO) << "Cannot add " << name << " without hwservicemanager";
251 return false;
252 }
253
254 Return<Transport> getTransport(const hidl_string& fqName, const hidl_string& name) {
255 LOG(INFO) << "Trying to get transport of " << fqName << "/" << name
256 << " without hwservicemanager";
Devin Mooreadbf9a52023-06-06 23:07:08 +0000257 return Transport::EMPTY;
Devin Mooree1973ce2022-04-15 20:54:03 +0000258 }
259
260 Return<void> list(list_cb _hidl_cb) override {
261 _hidl_cb({});
262 LOG(INFO) << "Cannot list all services without hwservicemanager";
263 return Void();
264 }
265 Return<void> listByInterface(const hidl_string& fqName, listByInterface_cb _hidl_cb) override {
266 _hidl_cb({});
267 LOG(INFO) << "Cannot list service " << fqName << " without hwservicemanager";
268 return Void();
269 }
270
271 Return<bool> registerForNotifications(const hidl_string& fqName, const hidl_string& name,
272 const sp<IServiceNotification>& /* callback */) override {
273 LOG(INFO) << "Cannot register for notifications for " << fqName << "/" << name
274 << " without hwservicemanager";
275 return false;
276 }
277
278 Return<void> debugDump(debugDump_cb _hidl_cb) override {
279 _hidl_cb({});
280 return Void();
281 }
282
283 Return<void> registerPassthroughClient(const hidl_string& fqName,
284 const hidl_string& name) override {
285 LOG(INFO) << "This process is a client of " << fqName << "/" << name
286 << " passthrough HAL, but it won't show up in lshal because hwservicemanager is "
287 "not installed";
288 return Void();
289 }
290
291 Return<bool> unregisterForNotifications(
292 const hidl_string& fqName, const hidl_string& name,
293 const sp<IServiceNotification>& /* callback */) override {
294 LOG(INFO) << "Cannot unregister for notifications for " << fqName << "/" << name
295 << " without hwservicemanager";
296 return false;
297 }
298 Return<bool> registerClientCallback(const hidl_string& fqName, const hidl_string& name,
299 const sp<IBase>&, const sp<IClientCallback>&) {
300 LOG(INFO) << "Cannot add client callback for " << fqName << "/" << name
301 << " without hwservicemanager";
302 return false;
303 }
304 Return<bool> unregisterClientCallback(const sp<IBase>&, const sp<IClientCallback>&) {
305 LOG(INFO) << "Cannot unregister client callbacks without hwservicemanager";
306 return false;
307 }
308 Return<bool> addWithChain(const hidl_string& fqName, const sp<IBase>&,
309 const hidl_vec<hidl_string>&) {
310 LOG(INFO) << "Cannot add " << fqName << " with chain without hwservicemanager";
311 return false;
312 }
313 Return<void> listManifestByInterface(const hidl_string& fqName, listManifestByInterface_cb) {
314 LOG(INFO) << "Cannot list manifest for " << fqName << " without hwservicemanager";
315 return Void();
316 }
317 Return<bool> tryUnregister(const hidl_string& fqName, const hidl_string& name,
318 const sp<IBase>&) {
319 LOG(INFO) << "Cannot unregister service " << fqName << "/" << name
320 << " without hwservicemanager";
321 return false;
322 }
323};
324
Steven Moreland89909692018-05-30 14:11:19 -0700325sp<IServiceManager1_2> defaultServiceManager1_2() {
326 using android::hidl::manager::V1_2::BnHwServiceManager;
327 using android::hidl::manager::V1_2::BpHwServiceManager;
328
Steven Moreland10d55632020-04-21 14:10:53 -0700329 static std::mutex& gDefaultServiceManagerLock = *new std::mutex;
330 static sp<IServiceManager1_2>& gDefaultServiceManager = *new sp<IServiceManager1_2>;
Steven Moreland89909692018-05-30 14:11:19 -0700331
Steven Moreland5d5ef7f2016-10-20 19:19:55 -0700332 {
Steven Moreland89909692018-05-30 14:11:19 -0700333 std::lock_guard<std::mutex> _l(gDefaultServiceManagerLock);
334 if (gDefaultServiceManager != nullptr) {
335 return gDefaultServiceManager;
Yifan Hong8fb656b2017-03-16 14:30:40 -0700336 }
Steven Moreland405d7612017-04-07 20:31:22 -0700337
Devin Moore22e6f1e2023-11-22 22:29:54 +0000338 if (!isHidlSupported()) {
Devin Mooree1973ce2022-04-15 20:54:03 +0000339 // hwservicemanager is not available on this device.
Devin Moore22e6f1e2023-11-22 22:29:54 +0000340 LOG(WARNING) << "hwservicemanager is not supported on the device.";
Devin Mooree1973ce2022-04-15 20:54:03 +0000341 gDefaultServiceManager = sp<NoHwServiceManager>::make();
342 return gDefaultServiceManager;
343 }
344
Yifan Hong8fb656b2017-03-16 14:30:40 -0700345 if (access("/dev/hwbinder", F_OK|R_OK|W_OK) != 0) {
346 // HwBinder not available on this device or not accessible to
347 // this process.
348 return nullptr;
349 }
Steven Morelandc1cee2c2017-03-24 16:23:11 +0000350
351 waitForHwServiceManager();
352
Steven Moreland89909692018-05-30 14:11:19 -0700353 while (gDefaultServiceManager == nullptr) {
354 gDefaultServiceManager =
355 fromBinder<IServiceManager1_2, BpHwServiceManager, BnHwServiceManager>(
356 ProcessState::self()->getContextObject(nullptr));
357 if (gDefaultServiceManager == nullptr) {
Steven Morelandc1cee2c2017-03-24 16:23:11 +0000358 LOG(ERROR) << "Waited for hwservicemanager, but got nullptr.";
Steven Moreland5d5ef7f2016-10-20 19:19:55 -0700359 sleep(1);
Yifan Hong8fb656b2017-03-16 14:30:40 -0700360 }
Steven Moreland5d5ef7f2016-10-20 19:19:55 -0700361 }
362 }
363
Steven Moreland89909692018-05-30 14:11:19 -0700364 return gDefaultServiceManager;
Steven Moreland5d5ef7f2016-10-20 19:19:55 -0700365}
366
Elliott Hughes19306142018-10-26 13:13:04 -0700367static std::vector<std::string> findFiles(const std::string& path, const std::string& prefix,
368 const std::string& suffix) {
Steven Moreland0091c092017-01-20 23:15:18 +0000369 std::unique_ptr<DIR, decltype(&closedir)> dir(opendir(path.c_str()), closedir);
370 if (!dir) return {};
371
372 std::vector<std::string> results{};
373
374 dirent* dp;
375 while ((dp = readdir(dir.get())) != nullptr) {
376 std::string name = dp->d_name;
377
Steven Moreland8092aa02018-10-12 15:54:56 -0700378 if (base::StartsWith(name, prefix) && base::EndsWith(name, suffix)) {
Steven Moreland0091c092017-01-20 23:15:18 +0000379 results.push_back(name);
380 }
381 }
382
383 return results;
384}
385
Steven Morelandccc3a972020-02-26 16:38:31 -0800386static bool matchPackageName(const std::string& lib, std::string* matchedName,
387 std::string* implName) {
388#define RE_COMPONENT "[a-zA-Z_][a-zA-Z_0-9]*"
389#define RE_PATH RE_COMPONENT "(?:[.]" RE_COMPONENT ")*"
390 static const std::regex gLibraryFileNamePattern("(" RE_PATH "@[0-9]+[.][0-9]+)-impl(.*?).so");
391#undef RE_PATH
392#undef RE_COMPONENT
393
Yifan Hong9a22d1d2017-01-25 14:19:26 -0800394 std::smatch match;
395 if (std::regex_match(lib, match, gLibraryFileNamePattern)) {
396 *matchedName = match.str(1) + "::I*";
Yifan Hongbd0d8f72017-05-24 19:43:51 -0700397 *implName = match.str(2);
Yifan Hong9a22d1d2017-01-25 14:19:26 -0800398 return true;
399 }
400 return false;
401}
402
Yifan Hong7f49f592017-02-03 15:11:44 -0800403static void registerReference(const hidl_string &interfaceName, const hidl_string &instanceName) {
Yifan Hongdeacf142018-07-10 17:33:34 -0700404 if (kIsRecovery) {
405 // No hwservicemanager in recovery.
406 return;
407 }
408
Steven Moreland2a2678e2017-07-21 18:07:38 -0700409 sp<IServiceManager1_0> binderizedManager = defaultServiceManager();
Yifan Hong7f49f592017-02-03 15:11:44 -0800410 if (binderizedManager == nullptr) {
411 LOG(WARNING) << "Could not registerReference for "
412 << interfaceName << "/" << instanceName
413 << ": null binderized manager.";
414 return;
415 }
Martijn Coenenaf4aba52017-04-27 09:41:13 -0700416 auto ret = binderizedManager->registerPassthroughClient(interfaceName, instanceName);
Yifan Hong7f49f592017-02-03 15:11:44 -0800417 if (!ret.isOk()) {
418 LOG(WARNING) << "Could not registerReference for "
419 << interfaceName << "/" << instanceName
420 << ": " << ret.description();
Steven Moreland0aeaa782017-03-22 08:11:07 -0700421 return;
Yifan Hong7f49f592017-02-03 15:11:44 -0800422 }
Steven Morelande681aa52017-02-15 16:22:37 -0800423 LOG(VERBOSE) << "Successfully registerReference for "
424 << interfaceName << "/" << instanceName;
Yifan Hong7f49f592017-02-03 15:11:44 -0800425}
426
Yifan Hongbd0d8f72017-05-24 19:43:51 -0700427using InstanceDebugInfo = hidl::manager::V1_0::IServiceManager::InstanceDebugInfo;
428static inline void fetchPidsForPassthroughLibraries(
429 std::map<std::string, InstanceDebugInfo>* infos) {
430 static const std::string proc = "/proc/";
431
432 std::map<std::string, std::set<pid_t>> pids;
433 std::unique_ptr<DIR, decltype(&closedir)> dir(opendir(proc.c_str()), closedir);
434 if (!dir) return;
435 dirent* dp;
436 while ((dp = readdir(dir.get())) != nullptr) {
Stephen Hinesfd9ecee2017-09-27 18:52:52 -0700437 pid_t pid = strtoll(dp->d_name, nullptr, 0);
Yifan Hongbd0d8f72017-05-24 19:43:51 -0700438 if (pid == 0) continue;
439 std::string mapsPath = proc + dp->d_name + "/maps";
440 std::ifstream ifs{mapsPath};
441 if (!ifs.is_open()) continue;
442
443 for (std::string line; std::getline(ifs, line);) {
444 // The last token of line should look like
445 // vendor/lib64/hw/android.hardware.foo@1.0-impl-extra.so
446 // Use some simple filters to ignore bad lines before extracting libFileName
447 // and checking the key in info to make parsing faster.
448 if (line.back() != 'o') continue;
449 if (line.rfind('@') == std::string::npos) continue;
450
451 auto spacePos = line.rfind(' ');
452 if (spacePos == std::string::npos) continue;
453 auto libFileName = line.substr(spacePos + 1);
454 auto it = infos->find(libFileName);
455 if (it == infos->end()) continue;
456 pids[libFileName].insert(pid);
457 }
458 }
459 for (auto& pair : *infos) {
460 pair.second.clientPids =
461 std::vector<pid_t>{pids[pair.first].begin(), pids[pair.first].end()};
462 }
463}
464
Steven Moreland2a2678e2017-07-21 18:07:38 -0700465struct PassthroughServiceManager : IServiceManager1_1 {
Chih-Hung Hsieh41649d52017-08-03 14:27:21 -0700466 static void openLibs(
467 const std::string& fqName,
468 const std::function<bool /* continue */ (void* /* handle */, const std::string& /* lib */,
469 const std::string& /* sym */)>& eachLib) {
Steven Moreland819c05d2017-04-06 17:24:22 -0700470 //fqName looks like android.hardware.foo@1.0::IFoo
Steven Moreland519306f2017-06-06 17:14:12 -0700471 size_t idx = fqName.find("::");
Steven Moreland819c05d2017-04-06 17:24:22 -0700472
473 if (idx == std::string::npos ||
Steven Moreland519306f2017-06-06 17:14:12 -0700474 idx + strlen("::") + 1 >= fqName.size()) {
Steven Moreland337e6b62017-01-18 17:25:13 -0800475 LOG(ERROR) << "Invalid interface name passthrough lookup: " << fqName;
Steven Moreland519306f2017-06-06 17:14:12 -0700476 return;
Steven Moreland337e6b62017-01-18 17:25:13 -0800477 }
478
Steven Moreland519306f2017-06-06 17:14:12 -0700479 std::string packageAndVersion = fqName.substr(0, idx);
480 std::string ifaceName = fqName.substr(idx + strlen("::"));
Steven Moreland819c05d2017-04-06 17:24:22 -0700481
482 const std::string prefix = packageAndVersion + "-impl";
483 const std::string sym = "HIDL_FETCH_" + ifaceName;
Steven Moreland348802d2017-02-23 12:48:55 -0800484
Steven Moreland77f4c852017-10-12 11:05:53 -0700485 constexpr int dlMode = RTLD_LAZY;
486 void* handle = nullptr;
Steven Moreland337e6b62017-01-18 17:25:13 -0800487
Steven Morelanda29905c2017-03-01 10:42:35 -0800488 dlerror(); // clear
489
Jooyung Han219106c2020-07-17 15:01:19 +0900490 static std::string halLibPathVndkSp = details::getVndkSpHwPath();
Justin Yun6a323ed2018-10-18 18:41:56 +0900491 std::vector<std::string> paths = {
492 HAL_LIBRARY_PATH_ODM, HAL_LIBRARY_PATH_VENDOR, halLibPathVndkSp,
493#ifndef __ANDROID_VNDK__
494 HAL_LIBRARY_PATH_SYSTEM,
495#endif
496 };
Steven Moreland77f4c852017-10-12 11:05:53 -0700497
Steven Morelandb939e7e2020-07-08 01:04:23 +0000498 if (details::isTrebleTestingOverride()) {
Steven Moreland77f4c852017-10-12 11:05:53 -0700499 // Load HAL implementations that are statically linked
500 handle = dlopen(nullptr, dlMode);
501 if (handle == nullptr) {
502 const char* error = dlerror();
503 LOG(ERROR) << "Failed to dlopen self: "
504 << (error == nullptr ? "unknown error" : error);
505 } else if (!eachLib(handle, "SELF", sym)) {
506 return;
507 }
Steven Morelandf7dea692017-07-14 12:49:28 -0700508 }
Steven Moreland77f4c852017-10-12 11:05:53 -0700509
Steven Morelandf7dea692017-07-14 12:49:28 -0700510 for (const std::string& path : paths) {
Elliott Hughes19306142018-10-26 13:13:04 -0700511 std::vector<std::string> libs = findFiles(path, prefix, ".so");
Steven Moreland0091c092017-01-20 23:15:18 +0000512
Steven Moreland0091c092017-01-20 23:15:18 +0000513 for (const std::string &lib : libs) {
Steven Moreland348802d2017-02-23 12:48:55 -0800514 const std::string fullPath = path + lib;
515
Yifan Hongdeacf142018-07-10 17:33:34 -0700516 if (kIsRecovery || path == HAL_LIBRARY_PATH_SYSTEM) {
Jiyong Park3ab546c2017-04-06 20:28:07 +0900517 handle = dlopen(fullPath.c_str(), dlMode);
Steven Moreland65c00cb2017-10-12 11:35:22 -0700518 } else {
Steven Moreland2056cf32019-09-17 17:41:02 -0700519#if !defined(__ANDROID_RECOVERY__) && defined(__ANDROID__)
Steven Moreland65c00cb2017-10-12 11:35:22 -0700520 handle = android_load_sphal_library(fullPath.c_str(), dlMode);
Jerry Zhang86ae9992018-05-30 17:11:09 -0700521#endif
Jiyong Park3ab546c2017-04-06 20:28:07 +0900522 }
523
Steven Moreland348802d2017-02-23 12:48:55 -0800524 if (handle == nullptr) {
525 const char* error = dlerror();
526 LOG(ERROR) << "Failed to dlopen " << lib << ": "
527 << (error == nullptr ? "unknown error" : error);
528 continue;
Steven Moreland0091c092017-01-20 23:15:18 +0000529 }
Steven Moreland348802d2017-02-23 12:48:55 -0800530
Steven Moreland519306f2017-06-06 17:14:12 -0700531 if (!eachLib(handle, lib, sym)) {
532 return;
Steven Moreland348802d2017-02-23 12:48:55 -0800533 }
Steven Moreland337e6b62017-01-18 17:25:13 -0800534 }
535 }
Steven Moreland519306f2017-06-06 17:14:12 -0700536 }
Steven Moreland337e6b62017-01-18 17:25:13 -0800537
Steven Moreland519306f2017-06-06 17:14:12 -0700538 Return<sp<IBase>> get(const hidl_string& fqName,
539 const hidl_string& name) override {
540 sp<IBase> ret = nullptr;
Devin Mooree1973ce2022-04-15 20:54:03 +0000541 // This is required to run without hwservicemanager while we have
542 // passthrough HIDL services. Once the passthrough HIDL services have
543 // been removed, the PassthroughServiceManager will no longer be needed.
Devin Moore22e6f1e2023-11-22 22:29:54 +0000544 if (!isHidlSupported() && isServiceManager(fqName)) {
Devin Mooree1973ce2022-04-15 20:54:03 +0000545 return defaultServiceManager1_2();
546 }
Steven Moreland519306f2017-06-06 17:14:12 -0700547
548 openLibs(fqName, [&](void* handle, const std::string &lib, const std::string &sym) {
549 IBase* (*generator)(const char* name);
550 *(void **)(&generator) = dlsym(handle, sym.c_str());
551 if(!generator) {
552 const char* error = dlerror();
Steven Morelandedd49122021-02-17 22:41:51 +0000553 LOG(ERROR) << "Passthrough lookup opened " << lib << " but could not find symbol "
554 << sym << ": " << (error == nullptr ? "unknown error" : error)
555 << ". Keeping library open.";
556
557 // dlclose too problematic in multi-threaded environment
558 // dlclose(handle);
559
560 return true; // continue
Steven Moreland519306f2017-06-06 17:14:12 -0700561 }
562
563 ret = (*generator)(name.c_str());
564
565 if (ret == nullptr) {
Steven Morelandedd49122021-02-17 22:41:51 +0000566 LOG(ERROR) << "Could not find instance '" << name.c_str() << "' in library " << lib
567 << ". Keeping library open.";
568
569 // dlclose too problematic in multi-threaded environment
570 // dlclose(handle);
571
572 // this module doesn't provide this particular instance
573 return true; // continue
Steven Moreland519306f2017-06-06 17:14:12 -0700574 }
575
Steven Morelande69e8d32018-03-14 10:55:42 -0700576 // Actual fqname might be a subclass.
577 // This assumption is tested in vts_treble_vintf_test
578 using ::android::hardware::details::getDescriptor;
579 std::string actualFqName = getDescriptor(ret.get());
580 CHECK(actualFqName.size() > 0);
581 registerReference(actualFqName, name);
Steven Moreland519306f2017-06-06 17:14:12 -0700582 return false;
583 });
584
585 return ret;
Steven Moreland337e6b62017-01-18 17:25:13 -0800586 }
587
Martijn Coenen67a02492017-03-06 13:04:48 +0100588 Return<bool> add(const hidl_string& /* name */,
Steven Moreland337e6b62017-01-18 17:25:13 -0800589 const sp<IBase>& /* service */) override {
590 LOG(FATAL) << "Cannot register services with passthrough service manager.";
591 return false;
592 }
593
Steven Morelandc601c5f2017-04-06 09:26:07 -0700594 Return<Transport> getTransport(const hidl_string& /* fqName */,
595 const hidl_string& /* name */) {
596 LOG(FATAL) << "Cannot getTransport with passthrough service manager.";
597 return Transport::EMPTY;
598 }
599
Yifan Hong705e5da2017-03-02 16:59:39 -0800600 Return<void> list(list_cb /* _hidl_cb */) override {
601 LOG(FATAL) << "Cannot list services with passthrough service manager.";
Steven Moreland337e6b62017-01-18 17:25:13 -0800602 return Void();
603 }
604 Return<void> listByInterface(const hidl_string& /* fqInstanceName */,
605 listByInterface_cb /* _hidl_cb */) override {
606 // TODO: add this functionality
607 LOG(FATAL) << "Cannot list services with passthrough service manager.";
608 return Void();
609 }
610
611 Return<bool> registerForNotifications(const hidl_string& /* fqName */,
612 const hidl_string& /* name */,
613 const sp<IServiceNotification>& /* callback */) override {
614 // This makes no sense.
615 LOG(FATAL) << "Cannot register for notifications with passthrough service manager.";
616 return false;
617 }
618
Yifan Hong705e5da2017-03-02 16:59:39 -0800619 Return<void> debugDump(debugDump_cb _hidl_cb) override {
620 using Arch = ::android::hidl::base::V1_0::DebugInfo::Architecture;
Yifan Hongbd0d8f72017-05-24 19:43:51 -0700621 using std::literals::string_literals::operator""s;
Jooyung Han219106c2020-07-17 15:01:19 +0900622 static std::string halLibPathVndkSp64 = details::getVndkSpHwPath("lib64");
623 static std::string halLibPathVndkSp32 = details::getVndkSpHwPath("lib");
Jiyong Park56eb1492017-08-04 16:16:13 +0900624 static std::vector<std::pair<Arch, std::vector<const char*>>> sAllPaths{
625 {Arch::IS_64BIT,
Justin Yun6a323ed2018-10-18 18:41:56 +0900626 {
627 HAL_LIBRARY_PATH_ODM_64BIT, HAL_LIBRARY_PATH_VENDOR_64BIT,
628 halLibPathVndkSp64.c_str(),
629#ifndef __ANDROID_VNDK__
630 HAL_LIBRARY_PATH_SYSTEM_64BIT,
631#endif
632 }},
Jiyong Park56eb1492017-08-04 16:16:13 +0900633 {Arch::IS_32BIT,
Justin Yun6a323ed2018-10-18 18:41:56 +0900634 {
635 HAL_LIBRARY_PATH_ODM_32BIT, HAL_LIBRARY_PATH_VENDOR_32BIT,
636 halLibPathVndkSp32.c_str(),
637#ifndef __ANDROID_VNDK__
638 HAL_LIBRARY_PATH_SYSTEM_32BIT,
639#endif
640 }}};
Yifan Hongbd0d8f72017-05-24 19:43:51 -0700641 std::map<std::string, InstanceDebugInfo> map;
Yifan Hong705e5da2017-03-02 16:59:39 -0800642 for (const auto &pair : sAllPaths) {
643 Arch arch = pair.first;
644 for (const auto &path : pair.second) {
Elliott Hughes19306142018-10-26 13:13:04 -0700645 std::vector<std::string> libs = findFiles(path, "", ".so");
Yifan Hong705e5da2017-03-02 16:59:39 -0800646 for (const std::string &lib : libs) {
647 std::string matchedName;
Yifan Hongbd0d8f72017-05-24 19:43:51 -0700648 std::string implName;
649 if (matchPackageName(lib, &matchedName, &implName)) {
650 std::string instanceName{"* ("s + path + ")"s};
651 if (!implName.empty()) instanceName += " ("s + implName + ")"s;
652 map.emplace(path + lib, InstanceDebugInfo{.interfaceName = matchedName,
653 .instanceName = instanceName,
654 .clientPids = {},
655 .arch = arch});
Yifan Hong705e5da2017-03-02 16:59:39 -0800656 }
657 }
658 }
659 }
Yifan Hongbd0d8f72017-05-24 19:43:51 -0700660 fetchPidsForPassthroughLibraries(&map);
661 hidl_vec<InstanceDebugInfo> vec;
662 vec.resize(map.size());
663 size_t idx = 0;
664 for (auto&& pair : map) {
665 vec[idx++] = std::move(pair.second);
666 }
Yifan Hong705e5da2017-03-02 16:59:39 -0800667 _hidl_cb(vec);
Yifan Hong7f49f592017-02-03 15:11:44 -0800668 return Void();
669 }
670
Martijn Coenenaf4aba52017-04-27 09:41:13 -0700671 Return<void> registerPassthroughClient(const hidl_string &, const hidl_string &) override {
Yifan Hong7f49f592017-02-03 15:11:44 -0800672 // This makes no sense.
673 LOG(FATAL) << "Cannot call registerPassthroughClient on passthrough service manager. "
674 << "Call it on defaultServiceManager() instead.";
Yifan Hong9a22d1d2017-01-25 14:19:26 -0800675 return Void();
676 }
677
Steven Moreland2a2678e2017-07-21 18:07:38 -0700678 Return<bool> unregisterForNotifications(const hidl_string& /* fqName */,
679 const hidl_string& /* name */,
680 const sp<IServiceNotification>& /* callback */) override {
681 // This makes no sense.
682 LOG(FATAL) << "Cannot unregister for notifications with passthrough service manager.";
683 return false;
684 }
685
Steven Moreland337e6b62017-01-18 17:25:13 -0800686};
687
Steven Moreland2a2678e2017-07-21 18:07:38 -0700688sp<IServiceManager1_0> getPassthroughServiceManager() {
689 return getPassthroughServiceManager1_1();
690}
691sp<IServiceManager1_1> getPassthroughServiceManager1_1() {
Steven Moreland337e6b62017-01-18 17:25:13 -0800692 static sp<PassthroughServiceManager> manager(new PassthroughServiceManager());
693 return manager;
694}
695
Steven Moreland92b247c2019-08-21 14:53:11 -0700696std::vector<std::string> getAllHalInstanceNames(const std::string& descriptor) {
697 std::vector<std::string> ret;
698 auto sm = defaultServiceManager1_2();
699 sm->listManifestByInterface(descriptor, [&](const auto& instances) {
700 ret.reserve(instances.size());
701 for (const auto& i : instances) {
702 ret.push_back(i);
703 }
704 });
705 return ret;
706}
707
Steven Morelandcbefd352017-01-23 20:29:05 -0800708namespace details {
709
Steven Moreland519306f2017-06-06 17:14:12 -0700710void preloadPassthroughService(const std::string &descriptor) {
711 PassthroughServiceManager::openLibs(descriptor,
712 [&](void* /* handle */, const std::string& /* lib */, const std::string& /* sym */) {
713 // do nothing
714 return true; // open all libs
715 });
716}
717
Steven Morelandcbefd352017-01-23 20:29:05 -0800718struct Waiter : IServiceNotification {
Martijn Coenen773609e2017-10-24 09:57:53 +0200719 Waiter(const std::string& interface, const std::string& instanceName,
720 const sp<IServiceManager1_1>& sm) : mInterfaceName(interface),
721 mInstanceName(instanceName), mSm(sm) {
Martijn Coenene6cdfd32017-11-13 14:03:05 +0100722 }
723
724 void onFirstRef() override {
Martijn Coenenb88ae7f2017-10-24 11:45:41 +0200725 // If this process only has one binder thread, and we're calling wait() from
726 // that thread, it will block forever because we hung up the one and only
727 // binder thread on a condition variable that can only be notified by an
728 // incoming binder call.
Tobias Lindskog88e886f2018-01-05 10:32:43 +0100729 if (IPCThreadState::self()->isOnlyBinderThread()) {
Martijn Coenenb88ae7f2017-10-24 11:45:41 +0200730 LOG(WARNING) << "Can't efficiently wait for " << mInterfaceName << "/"
731 << mInstanceName << ", because we are called from "
732 << "the only binder thread in this process.";
733 return;
734 }
735
Martijn Coenene6cdfd32017-11-13 14:03:05 +0100736 Return<bool> ret = mSm->registerForNotifications(mInterfaceName, mInstanceName, this);
Martijn Coenen773609e2017-10-24 09:57:53 +0200737
738 if (!ret.isOk()) {
739 LOG(ERROR) << "Transport error, " << ret.description()
Martijn Coenene6cdfd32017-11-13 14:03:05 +0100740 << ", during notification registration for " << mInterfaceName << "/"
741 << mInstanceName << ".";
Martijn Coenen773609e2017-10-24 09:57:53 +0200742 return;
743 }
744
745 if (!ret) {
Martijn Coenene6cdfd32017-11-13 14:03:05 +0100746 LOG(ERROR) << "Could not register for notifications for " << mInterfaceName << "/"
747 << mInstanceName << ".";
Martijn Coenen773609e2017-10-24 09:57:53 +0200748 return;
749 }
750
751 mRegisteredForNotifications = true;
752 }
753
754 ~Waiter() {
Martijn Coenen3fa15c22018-02-10 11:30:00 +0100755 if (!mDoneCalled) {
756 LOG(FATAL)
757 << "Waiter still registered for notifications, call done() before dropping ref!";
Martijn Coenen773609e2017-10-24 09:57:53 +0200758 }
759 }
760
Steven Morelandcbefd352017-01-23 20:29:05 -0800761 Return<void> onRegistration(const hidl_string& /* fqName */,
762 const hidl_string& /* name */,
763 bool /* preexisting */) override {
764 std::unique_lock<std::mutex> lock(mMutex);
765 if (mRegistered) {
766 return Void();
767 }
768 mRegistered = true;
769 lock.unlock();
770
771 mCondition.notify_one();
772 return Void();
773 }
774
Steven Moreland0771d8a2018-05-09 13:29:14 -0700775 void wait(bool timeout) {
Steven Moreland49605102017-03-28 09:33:06 -0700776 using std::literals::chrono_literals::operator""s;
777
Martijn Coenen773609e2017-10-24 09:57:53 +0200778 if (!mRegisteredForNotifications) {
779 // As an alternative, just sleep for a second and return
780 LOG(WARNING) << "Waiting one second for " << mInterfaceName << "/" << mInstanceName;
781 sleep(1);
782 return;
783 }
784
Steven Morelandcbefd352017-01-23 20:29:05 -0800785 std::unique_lock<std::mutex> lock(mMutex);
Steven Moreland0771d8a2018-05-09 13:29:14 -0700786 do {
Steven Moreland49605102017-03-28 09:33:06 -0700787 mCondition.wait_for(lock, 1s, [this]{
788 return mRegistered;
789 });
790
791 if (mRegistered) {
792 break;
793 }
794
Steven Moreland0771d8a2018-05-09 13:29:14 -0700795 LOG(WARNING) << "Waited one second for " << mInterfaceName << "/" << mInstanceName;
796 } while (!timeout);
Steven Morelandcbefd352017-01-23 20:29:05 -0800797 }
798
Martijn Coenen773609e2017-10-24 09:57:53 +0200799 // Be careful when using this; after calling reset(), you must always try to retrieve
800 // the corresponding service before blocking on the waiter; otherwise, you might run
801 // into a race-condition where the service has just (re-)registered, you clear the state
802 // here, and subsequently calling waiter->wait() will block forever.
803 void reset() {
804 std::unique_lock<std::mutex> lock(mMutex);
805 mRegistered = false;
806 }
Martijn Coenen3fa15c22018-02-10 11:30:00 +0100807
808 // done() must be called before dropping the last strong ref to the Waiter, to make
809 // sure we can properly unregister with hwservicemanager.
810 void done() {
811 if (mRegisteredForNotifications) {
812 if (!mSm->unregisterForNotifications(mInterfaceName, mInstanceName, this)
813 .withDefault(false)) {
814 LOG(ERROR) << "Could not unregister service notification for " << mInterfaceName
815 << "/" << mInstanceName << ".";
816 } else {
817 mRegisteredForNotifications = false;
818 }
819 }
820 mDoneCalled = true;
821 }
822
823 private:
Martijn Coenen773609e2017-10-24 09:57:53 +0200824 const std::string mInterfaceName;
825 const std::string mInstanceName;
Steven Morelandad1f9922018-10-02 19:37:05 -0700826 sp<IServiceManager1_1> mSm;
Steven Morelandcbefd352017-01-23 20:29:05 -0800827 std::mutex mMutex;
828 std::condition_variable mCondition;
829 bool mRegistered = false;
Martijn Coenen773609e2017-10-24 09:57:53 +0200830 bool mRegisteredForNotifications = false;
Martijn Coenen3fa15c22018-02-10 11:30:00 +0100831 bool mDoneCalled = false;
Steven Morelandcbefd352017-01-23 20:29:05 -0800832};
833
834void waitForHwService(
835 const std::string &interface, const std::string &instanceName) {
Martijn Coenen773609e2017-10-24 09:57:53 +0200836 sp<Waiter> waiter = new Waiter(interface, instanceName, defaultServiceManager1_1());
Steven Moreland0771d8a2018-05-09 13:29:14 -0700837 waiter->wait(false /* timeout */);
Martijn Coenen3fa15c22018-02-10 11:30:00 +0100838 waiter->done();
Martijn Coenen773609e2017-10-24 09:57:53 +0200839}
Steven Morelandcbefd352017-01-23 20:29:05 -0800840
Martijn Coenen773609e2017-10-24 09:57:53 +0200841// Prints relevant error/warning messages for error return values from
842// details::canCastInterface(), both transaction errors (!castReturn.isOk())
843// as well as actual cast failures (castReturn.isOk() && castReturn = false).
844// Returns 'true' if the error is non-fatal and it's useful to retry
845bool handleCastError(const Return<bool>& castReturn, const std::string& descriptor,
846 const std::string& instance) {
847 if (castReturn.isOk()) {
848 if (castReturn) {
849 details::logAlwaysFatal("Successful cast value passed into handleCastError.");
850 }
851 // This should never happen, and there's not really a point in retrying.
852 ALOGE("getService: received incompatible service (bug in hwservicemanager?) for "
853 "%s/%s.", descriptor.c_str(), instance.c_str());
854 return false;
Steven Morelandcbefd352017-01-23 20:29:05 -0800855 }
Martijn Coenen773609e2017-10-24 09:57:53 +0200856 if (castReturn.isDeadObject()) {
857 ALOGW("getService: found dead hwbinder service for %s/%s.", descriptor.c_str(),
858 instance.c_str());
859 return true;
Steven Morelandcbefd352017-01-23 20:29:05 -0800860 }
Martijn Coenen773609e2017-10-24 09:57:53 +0200861 // This can happen due to:
862 // 1) No SELinux permissions
863 // 2) Other transaction failure (no buffer space, kernel error)
864 // The first isn't recoverable, but the second is.
865 // Since we can't yet differentiate between the two, and clients depend
866 // on us not blocking in case 1), treat this as a fatal error for now.
867 ALOGW("getService: unable to call into hwbinder service for %s/%s.",
868 descriptor.c_str(), instance.c_str());
869 return false;
Steven Morelandcbefd352017-01-23 20:29:05 -0800870}
871
Martijn Coenen86c3abb2017-10-24 09:33:28 +0200872sp<::android::hidl::base::V1_0::IBase> getRawServiceInternal(const std::string& descriptor,
873 const std::string& instance,
874 bool retry, bool getStub) {
Steven Moreland84fe49e2019-08-20 17:47:26 +0000875 using Transport = IServiceManager1_0::Transport;
Martijn Coenend35678f2018-03-07 09:51:01 +0100876 sp<Waiter> waiter;
Martijn Coenen86c3abb2017-10-24 09:33:28 +0200877
Yifan Hongdeacf142018-07-10 17:33:34 -0700878 sp<IServiceManager1_1> sm;
879 Transport transport = Transport::EMPTY;
880 if (kIsRecovery) {
Yifan Hongdeacf142018-07-10 17:33:34 -0700881 transport = Transport::PASSTHROUGH;
Yifan Hongdeacf142018-07-10 17:33:34 -0700882 } else {
883 sm = defaultServiceManager1_1();
884 if (sm == nullptr) {
885 ALOGE("getService: defaultServiceManager() is null");
886 return nullptr;
887 }
888
889 Return<Transport> transportRet = sm->getTransport(descriptor, instance);
890
891 if (!transportRet.isOk()) {
892 ALOGE("getService: defaultServiceManager()->getTransport returns %s",
893 transportRet.description().c_str());
894 return nullptr;
895 }
896 transport = transportRet;
Martijn Coenen86c3abb2017-10-24 09:33:28 +0200897 }
898
Martijn Coenen86c3abb2017-10-24 09:33:28 +0200899 const bool vintfHwbinder = (transport == Transport::HWBINDER);
900 const bool vintfPassthru = (transport == Transport::PASSTHROUGH);
Steven Moreland84fe49e2019-08-20 17:47:26 +0000901 const bool trebleTestingOverride = isTrebleTestingOverride();
Steven Morelandf47cdf72021-04-07 22:13:13 +0000902 const bool allowLegacy = !kEnforceVintfManifest || (trebleTestingOverride && isDebuggable());
Steven Moreland84fe49e2019-08-20 17:47:26 +0000903 const bool vintfLegacy = (transport == Transport::EMPTY) && allowLegacy;
Martijn Coenen86c3abb2017-10-24 09:33:28 +0200904
Steven Moreland84fe49e2019-08-20 17:47:26 +0000905 if (!kEnforceVintfManifest) {
906 ALOGE("getService: Potential race detected. The VINTF manifest is not being enforced. If "
907 "a HAL server has a delay in starting and it is not in the manifest, it will not be "
908 "retrieved. Please make sure all HALs on this device are in the VINTF manifest and "
909 "enable PRODUCT_ENFORCE_VINTF_MANIFEST on this device (this is also enabled by "
910 "PRODUCT_FULL_TREBLE). PRODUCT_ENFORCE_VINTF_MANIFEST will ensure that no race "
911 "condition is possible here.");
Steven Morelandaadec3c2019-08-22 17:06:48 -0700912 sleep(1);
Steven Moreland84fe49e2019-08-20 17:47:26 +0000913 }
Martijn Coenen86c3abb2017-10-24 09:33:28 +0200914
Steven Moreland76371242018-04-19 17:11:39 -0700915 for (int tries = 0; !getStub && (vintfHwbinder || vintfLegacy); tries++) {
916 if (waiter == nullptr && tries > 0) {
Martijn Coenend35678f2018-03-07 09:51:01 +0100917 waiter = new Waiter(descriptor, instance, sm);
918 }
Steven Moreland76371242018-04-19 17:11:39 -0700919 if (waiter != nullptr) {
920 waiter->reset(); // don't reorder this -- see comments on reset()
921 }
Martijn Coenen86c3abb2017-10-24 09:33:28 +0200922 Return<sp<IBase>> ret = sm->get(descriptor, instance);
923 if (!ret.isOk()) {
924 ALOGE("getService: defaultServiceManager()->get returns %s for %s/%s.",
925 ret.description().c_str(), descriptor.c_str(), instance.c_str());
926 break;
927 }
928 sp<IBase> base = ret;
Martijn Coenen773609e2017-10-24 09:57:53 +0200929 if (base != nullptr) {
930 Return<bool> canCastRet =
931 details::canCastInterface(base.get(), descriptor.c_str(), true /* emitError */);
932
933 if (canCastRet.isOk() && canCastRet) {
Steven Moreland76371242018-04-19 17:11:39 -0700934 if (waiter != nullptr) {
935 waiter->done();
936 }
Martijn Coenen773609e2017-10-24 09:57:53 +0200937 return base; // still needs to be wrapped by Bp class.
Martijn Coenen86c3abb2017-10-24 09:33:28 +0200938 }
Martijn Coenen773609e2017-10-24 09:57:53 +0200939
940 if (!handleCastError(canCastRet, descriptor, instance)) break;
Martijn Coenen86c3abb2017-10-24 09:33:28 +0200941 }
942
Martijn Coenen773609e2017-10-24 09:57:53 +0200943 // In case of legacy or we were not asked to retry, don't.
944 if (vintfLegacy || !retry) break;
Martijn Coenen86c3abb2017-10-24 09:33:28 +0200945
Steven Moreland76371242018-04-19 17:11:39 -0700946 if (waiter != nullptr) {
947 ALOGI("getService: Trying again for %s/%s...", descriptor.c_str(), instance.c_str());
Steven Moreland0771d8a2018-05-09 13:29:14 -0700948 waiter->wait(true /* timeout */);
Steven Moreland76371242018-04-19 17:11:39 -0700949 }
Martijn Coenen86c3abb2017-10-24 09:33:28 +0200950 }
951
Martijn Coenend35678f2018-03-07 09:51:01 +0100952 if (waiter != nullptr) {
953 waiter->done();
954 }
Martijn Coenen3fa15c22018-02-10 11:30:00 +0100955
Martijn Coenen86c3abb2017-10-24 09:33:28 +0200956 if (getStub || vintfPassthru || vintfLegacy) {
Steven Moreland84fe49e2019-08-20 17:47:26 +0000957 const sp<IServiceManager1_0> pm = getPassthroughServiceManager();
Martijn Coenen86c3abb2017-10-24 09:33:28 +0200958 if (pm != nullptr) {
959 sp<IBase> base = pm->get(descriptor, instance).withDefault(nullptr);
960 if (!getStub || trebleTestingOverride) {
961 base = wrapPassthrough(base);
962 }
963 return base;
964 }
965 }
966
967 return nullptr;
968}
969
Steven Morelandbb9eb2a2018-10-11 12:10:01 -0700970status_t registerAsServiceInternal(const sp<IBase>& service, const std::string& name) {
971 if (service == nullptr) {
972 return UNEXPECTED_NULL;
973 }
974
975 sp<IServiceManager1_2> sm = defaultServiceManager1_2();
976 if (sm == nullptr) {
977 return INVALID_OPERATION;
978 }
979
Steven Moreland84fe49e2019-08-20 17:47:26 +0000980 const std::string descriptor = getDescriptor(service.get());
981
982 if (kEnforceVintfManifest && !isTrebleTestingOverride()) {
983 using Transport = IServiceManager1_0::Transport;
Steven Moreland7d1b0732021-01-21 22:47:40 +0000984 Return<Transport> transport = sm->getTransport(descriptor, name);
985
986 if (!transport.isOk()) {
987 LOG(ERROR) << "Could not get transport for " << descriptor << "/" << name << ": "
988 << transport.description();
989 return UNKNOWN_ERROR;
990 }
Steven Moreland84fe49e2019-08-20 17:47:26 +0000991
992 if (transport != Transport::HWBINDER) {
993 LOG(ERROR) << "Service " << descriptor << "/" << name
994 << " must be in VINTF manifest in order to register/get.";
995 return UNKNOWN_ERROR;
996 }
997 }
998
Steven Morelandbb9eb2a2018-10-11 12:10:01 -0700999 bool registered = false;
1000 Return<void> ret = service->interfaceChain([&](const auto& chain) {
1001 registered = sm->addWithChain(name.c_str(), service, chain).withDefault(false);
1002 });
1003
1004 if (!ret.isOk()) {
1005 LOG(ERROR) << "Could not retrieve interface chain: " << ret.description();
1006 }
1007
1008 if (registered) {
Steven Moreland84fe49e2019-08-20 17:47:26 +00001009 onRegistrationImpl(descriptor, name);
Steven Morelandbb9eb2a2018-10-11 12:10:01 -07001010 }
1011
1012 return registered ? OK : UNKNOWN_ERROR;
1013}
1014
Bernhard Rosenkränzer0c28fd22018-06-04 16:01:47 +02001015} // namespace details
Steven Morelandcbefd352017-01-23 20:29:05 -08001016
Bernhard Rosenkränzer0c28fd22018-06-04 16:01:47 +02001017} // namespace hardware
1018} // namespace android