blob: 78c1e960492809d3ef6c319883c7daef4e37b732 [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 ||
214 access("/system/system_ext/bin/hwservicemanager", F_OK) == 0;
Devin Mooree1973ce2022-04-15 20:54:03 +0000215}
216
217/*
218 * A replacement for hwservicemanager when it is not installed on a device.
219 *
220 * Clients in the framework need to continue supporting HIDL services through
221 * hwservicemanager for upgrading devices. Being unable to get an instance of
222 * hardware service manager is a hard error, so this implementation is returned
223 * to be able service the requests and tell clients there are no services
224 * registered.
225 */
226struct NoHwServiceManager : public IServiceManager1_2 {
227 Return<sp<IBase>> get(const hidl_string& fqName, const hidl_string&) override {
228 sp<IBase> ret = nullptr;
229
230 if (isServiceManager(fqName)) {
231 ret = defaultServiceManager1_2();
232 }
233 return ret;
234 }
235
236 Return<bool> add(const hidl_string& name, const sp<IBase>& /* service */) override {
237 LOG(INFO) << "Cannot add " << name << " without hwservicemanager";
238 return false;
239 }
240
241 Return<Transport> getTransport(const hidl_string& fqName, const hidl_string& name) {
242 LOG(INFO) << "Trying to get transport of " << fqName << "/" << name
243 << " without hwservicemanager";
Devin Mooreadbf9a52023-06-06 23:07:08 +0000244 return Transport::EMPTY;
Devin Mooree1973ce2022-04-15 20:54:03 +0000245 }
246
247 Return<void> list(list_cb _hidl_cb) override {
248 _hidl_cb({});
249 LOG(INFO) << "Cannot list all services without hwservicemanager";
250 return Void();
251 }
252 Return<void> listByInterface(const hidl_string& fqName, listByInterface_cb _hidl_cb) override {
253 _hidl_cb({});
254 LOG(INFO) << "Cannot list service " << fqName << " without hwservicemanager";
255 return Void();
256 }
257
258 Return<bool> registerForNotifications(const hidl_string& fqName, const hidl_string& name,
259 const sp<IServiceNotification>& /* callback */) override {
260 LOG(INFO) << "Cannot register for notifications for " << fqName << "/" << name
261 << " without hwservicemanager";
262 return false;
263 }
264
265 Return<void> debugDump(debugDump_cb _hidl_cb) override {
266 _hidl_cb({});
267 return Void();
268 }
269
270 Return<void> registerPassthroughClient(const hidl_string& fqName,
271 const hidl_string& name) override {
272 LOG(INFO) << "This process is a client of " << fqName << "/" << name
273 << " passthrough HAL, but it won't show up in lshal because hwservicemanager is "
274 "not installed";
275 return Void();
276 }
277
278 Return<bool> unregisterForNotifications(
279 const hidl_string& fqName, const hidl_string& name,
280 const sp<IServiceNotification>& /* callback */) override {
281 LOG(INFO) << "Cannot unregister for notifications for " << fqName << "/" << name
282 << " without hwservicemanager";
283 return false;
284 }
285 Return<bool> registerClientCallback(const hidl_string& fqName, const hidl_string& name,
286 const sp<IBase>&, const sp<IClientCallback>&) {
287 LOG(INFO) << "Cannot add client callback for " << fqName << "/" << name
288 << " without hwservicemanager";
289 return false;
290 }
291 Return<bool> unregisterClientCallback(const sp<IBase>&, const sp<IClientCallback>&) {
292 LOG(INFO) << "Cannot unregister client callbacks without hwservicemanager";
293 return false;
294 }
295 Return<bool> addWithChain(const hidl_string& fqName, const sp<IBase>&,
296 const hidl_vec<hidl_string>&) {
297 LOG(INFO) << "Cannot add " << fqName << " with chain without hwservicemanager";
298 return false;
299 }
300 Return<void> listManifestByInterface(const hidl_string& fqName, listManifestByInterface_cb) {
301 LOG(INFO) << "Cannot list manifest for " << fqName << " without hwservicemanager";
302 return Void();
303 }
304 Return<bool> tryUnregister(const hidl_string& fqName, const hidl_string& name,
305 const sp<IBase>&) {
306 LOG(INFO) << "Cannot unregister service " << fqName << "/" << name
307 << " without hwservicemanager";
308 return false;
309 }
310};
311
Steven Moreland89909692018-05-30 14:11:19 -0700312sp<IServiceManager1_2> defaultServiceManager1_2() {
313 using android::hidl::manager::V1_2::BnHwServiceManager;
314 using android::hidl::manager::V1_2::BpHwServiceManager;
315
Steven Moreland10d55632020-04-21 14:10:53 -0700316 static std::mutex& gDefaultServiceManagerLock = *new std::mutex;
317 static sp<IServiceManager1_2>& gDefaultServiceManager = *new sp<IServiceManager1_2>;
Steven Moreland89909692018-05-30 14:11:19 -0700318
Steven Moreland5d5ef7f2016-10-20 19:19:55 -0700319 {
Steven Moreland89909692018-05-30 14:11:19 -0700320 std::lock_guard<std::mutex> _l(gDefaultServiceManagerLock);
321 if (gDefaultServiceManager != nullptr) {
322 return gDefaultServiceManager;
Yifan Hong8fb656b2017-03-16 14:30:40 -0700323 }
Steven Moreland405d7612017-04-07 20:31:22 -0700324
Devin Mooree1973ce2022-04-15 20:54:03 +0000325 if (!isHwServiceManagerInstalled()) {
326 // hwservicemanager is not available on this device.
Devin Moore91db8672023-09-01 16:36:30 +0000327 LOG(WARNING)
328 << "hwservicemanager is not installed on the device. If HIDL support "
329 << "is still needed, hwservicemanager and android.hidl.allocator@1.0-service "
330 << "need to be added to the device's PRODUCT_PACKAGES and the kernel config "
331 << "needs to have 'hwbinder' in CONFIG_ANDROID_BINDER_DEVICES.";
Devin Mooree1973ce2022-04-15 20:54:03 +0000332 gDefaultServiceManager = sp<NoHwServiceManager>::make();
333 return gDefaultServiceManager;
334 }
335
Yifan Hong8fb656b2017-03-16 14:30:40 -0700336 if (access("/dev/hwbinder", F_OK|R_OK|W_OK) != 0) {
337 // HwBinder not available on this device or not accessible to
338 // this process.
339 return nullptr;
340 }
Steven Morelandc1cee2c2017-03-24 16:23:11 +0000341
342 waitForHwServiceManager();
343
Steven Moreland89909692018-05-30 14:11:19 -0700344 while (gDefaultServiceManager == nullptr) {
345 gDefaultServiceManager =
346 fromBinder<IServiceManager1_2, BpHwServiceManager, BnHwServiceManager>(
347 ProcessState::self()->getContextObject(nullptr));
348 if (gDefaultServiceManager == nullptr) {
Steven Morelandc1cee2c2017-03-24 16:23:11 +0000349 LOG(ERROR) << "Waited for hwservicemanager, but got nullptr.";
Steven Moreland5d5ef7f2016-10-20 19:19:55 -0700350 sleep(1);
Yifan Hong8fb656b2017-03-16 14:30:40 -0700351 }
Steven Moreland5d5ef7f2016-10-20 19:19:55 -0700352 }
353 }
354
Steven Moreland89909692018-05-30 14:11:19 -0700355 return gDefaultServiceManager;
Steven Moreland5d5ef7f2016-10-20 19:19:55 -0700356}
357
Elliott Hughes19306142018-10-26 13:13:04 -0700358static std::vector<std::string> findFiles(const std::string& path, const std::string& prefix,
359 const std::string& suffix) {
Steven Moreland0091c092017-01-20 23:15:18 +0000360 std::unique_ptr<DIR, decltype(&closedir)> dir(opendir(path.c_str()), closedir);
361 if (!dir) return {};
362
363 std::vector<std::string> results{};
364
365 dirent* dp;
366 while ((dp = readdir(dir.get())) != nullptr) {
367 std::string name = dp->d_name;
368
Steven Moreland8092aa02018-10-12 15:54:56 -0700369 if (base::StartsWith(name, prefix) && base::EndsWith(name, suffix)) {
Steven Moreland0091c092017-01-20 23:15:18 +0000370 results.push_back(name);
371 }
372 }
373
374 return results;
375}
376
Steven Morelandccc3a972020-02-26 16:38:31 -0800377static bool matchPackageName(const std::string& lib, std::string* matchedName,
378 std::string* implName) {
379#define RE_COMPONENT "[a-zA-Z_][a-zA-Z_0-9]*"
380#define RE_PATH RE_COMPONENT "(?:[.]" RE_COMPONENT ")*"
381 static const std::regex gLibraryFileNamePattern("(" RE_PATH "@[0-9]+[.][0-9]+)-impl(.*?).so");
382#undef RE_PATH
383#undef RE_COMPONENT
384
Yifan Hong9a22d1d2017-01-25 14:19:26 -0800385 std::smatch match;
386 if (std::regex_match(lib, match, gLibraryFileNamePattern)) {
387 *matchedName = match.str(1) + "::I*";
Yifan Hongbd0d8f72017-05-24 19:43:51 -0700388 *implName = match.str(2);
Yifan Hong9a22d1d2017-01-25 14:19:26 -0800389 return true;
390 }
391 return false;
392}
393
Yifan Hong7f49f592017-02-03 15:11:44 -0800394static void registerReference(const hidl_string &interfaceName, const hidl_string &instanceName) {
Yifan Hongdeacf142018-07-10 17:33:34 -0700395 if (kIsRecovery) {
396 // No hwservicemanager in recovery.
397 return;
398 }
399
Steven Moreland2a2678e2017-07-21 18:07:38 -0700400 sp<IServiceManager1_0> binderizedManager = defaultServiceManager();
Yifan Hong7f49f592017-02-03 15:11:44 -0800401 if (binderizedManager == nullptr) {
402 LOG(WARNING) << "Could not registerReference for "
403 << interfaceName << "/" << instanceName
404 << ": null binderized manager.";
405 return;
406 }
Martijn Coenenaf4aba52017-04-27 09:41:13 -0700407 auto ret = binderizedManager->registerPassthroughClient(interfaceName, instanceName);
Yifan Hong7f49f592017-02-03 15:11:44 -0800408 if (!ret.isOk()) {
409 LOG(WARNING) << "Could not registerReference for "
410 << interfaceName << "/" << instanceName
411 << ": " << ret.description();
Steven Moreland0aeaa782017-03-22 08:11:07 -0700412 return;
Yifan Hong7f49f592017-02-03 15:11:44 -0800413 }
Steven Morelande681aa52017-02-15 16:22:37 -0800414 LOG(VERBOSE) << "Successfully registerReference for "
415 << interfaceName << "/" << instanceName;
Yifan Hong7f49f592017-02-03 15:11:44 -0800416}
417
Yifan Hongbd0d8f72017-05-24 19:43:51 -0700418using InstanceDebugInfo = hidl::manager::V1_0::IServiceManager::InstanceDebugInfo;
419static inline void fetchPidsForPassthroughLibraries(
420 std::map<std::string, InstanceDebugInfo>* infos) {
421 static const std::string proc = "/proc/";
422
423 std::map<std::string, std::set<pid_t>> pids;
424 std::unique_ptr<DIR, decltype(&closedir)> dir(opendir(proc.c_str()), closedir);
425 if (!dir) return;
426 dirent* dp;
427 while ((dp = readdir(dir.get())) != nullptr) {
Stephen Hinesfd9ecee2017-09-27 18:52:52 -0700428 pid_t pid = strtoll(dp->d_name, nullptr, 0);
Yifan Hongbd0d8f72017-05-24 19:43:51 -0700429 if (pid == 0) continue;
430 std::string mapsPath = proc + dp->d_name + "/maps";
431 std::ifstream ifs{mapsPath};
432 if (!ifs.is_open()) continue;
433
434 for (std::string line; std::getline(ifs, line);) {
435 // The last token of line should look like
436 // vendor/lib64/hw/android.hardware.foo@1.0-impl-extra.so
437 // Use some simple filters to ignore bad lines before extracting libFileName
438 // and checking the key in info to make parsing faster.
439 if (line.back() != 'o') continue;
440 if (line.rfind('@') == std::string::npos) continue;
441
442 auto spacePos = line.rfind(' ');
443 if (spacePos == std::string::npos) continue;
444 auto libFileName = line.substr(spacePos + 1);
445 auto it = infos->find(libFileName);
446 if (it == infos->end()) continue;
447 pids[libFileName].insert(pid);
448 }
449 }
450 for (auto& pair : *infos) {
451 pair.second.clientPids =
452 std::vector<pid_t>{pids[pair.first].begin(), pids[pair.first].end()};
453 }
454}
455
Steven Moreland2a2678e2017-07-21 18:07:38 -0700456struct PassthroughServiceManager : IServiceManager1_1 {
Chih-Hung Hsieh41649d52017-08-03 14:27:21 -0700457 static void openLibs(
458 const std::string& fqName,
459 const std::function<bool /* continue */ (void* /* handle */, const std::string& /* lib */,
460 const std::string& /* sym */)>& eachLib) {
Steven Moreland819c05d2017-04-06 17:24:22 -0700461 //fqName looks like android.hardware.foo@1.0::IFoo
Steven Moreland519306f2017-06-06 17:14:12 -0700462 size_t idx = fqName.find("::");
Steven Moreland819c05d2017-04-06 17:24:22 -0700463
464 if (idx == std::string::npos ||
Steven Moreland519306f2017-06-06 17:14:12 -0700465 idx + strlen("::") + 1 >= fqName.size()) {
Steven Moreland337e6b62017-01-18 17:25:13 -0800466 LOG(ERROR) << "Invalid interface name passthrough lookup: " << fqName;
Steven Moreland519306f2017-06-06 17:14:12 -0700467 return;
Steven Moreland337e6b62017-01-18 17:25:13 -0800468 }
469
Steven Moreland519306f2017-06-06 17:14:12 -0700470 std::string packageAndVersion = fqName.substr(0, idx);
471 std::string ifaceName = fqName.substr(idx + strlen("::"));
Steven Moreland819c05d2017-04-06 17:24:22 -0700472
473 const std::string prefix = packageAndVersion + "-impl";
474 const std::string sym = "HIDL_FETCH_" + ifaceName;
Steven Moreland348802d2017-02-23 12:48:55 -0800475
Steven Moreland77f4c852017-10-12 11:05:53 -0700476 constexpr int dlMode = RTLD_LAZY;
477 void* handle = nullptr;
Steven Moreland337e6b62017-01-18 17:25:13 -0800478
Steven Morelanda29905c2017-03-01 10:42:35 -0800479 dlerror(); // clear
480
Jooyung Han219106c2020-07-17 15:01:19 +0900481 static std::string halLibPathVndkSp = details::getVndkSpHwPath();
Justin Yun6a323ed2018-10-18 18:41:56 +0900482 std::vector<std::string> paths = {
483 HAL_LIBRARY_PATH_ODM, HAL_LIBRARY_PATH_VENDOR, halLibPathVndkSp,
484#ifndef __ANDROID_VNDK__
485 HAL_LIBRARY_PATH_SYSTEM,
486#endif
487 };
Steven Moreland77f4c852017-10-12 11:05:53 -0700488
Steven Morelandb939e7e2020-07-08 01:04:23 +0000489 if (details::isTrebleTestingOverride()) {
Steven Moreland77f4c852017-10-12 11:05:53 -0700490 // Load HAL implementations that are statically linked
491 handle = dlopen(nullptr, dlMode);
492 if (handle == nullptr) {
493 const char* error = dlerror();
494 LOG(ERROR) << "Failed to dlopen self: "
495 << (error == nullptr ? "unknown error" : error);
496 } else if (!eachLib(handle, "SELF", sym)) {
497 return;
498 }
Steven Morelandf7dea692017-07-14 12:49:28 -0700499 }
Steven Moreland77f4c852017-10-12 11:05:53 -0700500
Steven Morelandf7dea692017-07-14 12:49:28 -0700501 for (const std::string& path : paths) {
Elliott Hughes19306142018-10-26 13:13:04 -0700502 std::vector<std::string> libs = findFiles(path, prefix, ".so");
Steven Moreland0091c092017-01-20 23:15:18 +0000503
Steven Moreland0091c092017-01-20 23:15:18 +0000504 for (const std::string &lib : libs) {
Steven Moreland348802d2017-02-23 12:48:55 -0800505 const std::string fullPath = path + lib;
506
Yifan Hongdeacf142018-07-10 17:33:34 -0700507 if (kIsRecovery || path == HAL_LIBRARY_PATH_SYSTEM) {
Jiyong Park3ab546c2017-04-06 20:28:07 +0900508 handle = dlopen(fullPath.c_str(), dlMode);
Steven Moreland65c00cb2017-10-12 11:35:22 -0700509 } else {
Steven Moreland2056cf32019-09-17 17:41:02 -0700510#if !defined(__ANDROID_RECOVERY__) && defined(__ANDROID__)
Steven Moreland65c00cb2017-10-12 11:35:22 -0700511 handle = android_load_sphal_library(fullPath.c_str(), dlMode);
Jerry Zhang86ae9992018-05-30 17:11:09 -0700512#endif
Jiyong Park3ab546c2017-04-06 20:28:07 +0900513 }
514
Steven Moreland348802d2017-02-23 12:48:55 -0800515 if (handle == nullptr) {
516 const char* error = dlerror();
517 LOG(ERROR) << "Failed to dlopen " << lib << ": "
518 << (error == nullptr ? "unknown error" : error);
519 continue;
Steven Moreland0091c092017-01-20 23:15:18 +0000520 }
Steven Moreland348802d2017-02-23 12:48:55 -0800521
Steven Moreland519306f2017-06-06 17:14:12 -0700522 if (!eachLib(handle, lib, sym)) {
523 return;
Steven Moreland348802d2017-02-23 12:48:55 -0800524 }
Steven Moreland337e6b62017-01-18 17:25:13 -0800525 }
526 }
Steven Moreland519306f2017-06-06 17:14:12 -0700527 }
Steven Moreland337e6b62017-01-18 17:25:13 -0800528
Steven Moreland519306f2017-06-06 17:14:12 -0700529 Return<sp<IBase>> get(const hidl_string& fqName,
530 const hidl_string& name) override {
531 sp<IBase> ret = nullptr;
Devin Mooree1973ce2022-04-15 20:54:03 +0000532 // This is required to run without hwservicemanager while we have
533 // passthrough HIDL services. Once the passthrough HIDL services have
534 // been removed, the PassthroughServiceManager will no longer be needed.
535 if (!isHwServiceManagerInstalled() && isServiceManager(fqName)) {
536 return defaultServiceManager1_2();
537 }
Steven Moreland519306f2017-06-06 17:14:12 -0700538
539 openLibs(fqName, [&](void* handle, const std::string &lib, const std::string &sym) {
540 IBase* (*generator)(const char* name);
541 *(void **)(&generator) = dlsym(handle, sym.c_str());
542 if(!generator) {
543 const char* error = dlerror();
Steven Morelandedd49122021-02-17 22:41:51 +0000544 LOG(ERROR) << "Passthrough lookup opened " << lib << " but could not find symbol "
545 << sym << ": " << (error == nullptr ? "unknown error" : error)
546 << ". Keeping library open.";
547
548 // dlclose too problematic in multi-threaded environment
549 // dlclose(handle);
550
551 return true; // continue
Steven Moreland519306f2017-06-06 17:14:12 -0700552 }
553
554 ret = (*generator)(name.c_str());
555
556 if (ret == nullptr) {
Steven Morelandedd49122021-02-17 22:41:51 +0000557 LOG(ERROR) << "Could not find instance '" << name.c_str() << "' in library " << lib
558 << ". Keeping library open.";
559
560 // dlclose too problematic in multi-threaded environment
561 // dlclose(handle);
562
563 // this module doesn't provide this particular instance
564 return true; // continue
Steven Moreland519306f2017-06-06 17:14:12 -0700565 }
566
Steven Morelande69e8d32018-03-14 10:55:42 -0700567 // Actual fqname might be a subclass.
568 // This assumption is tested in vts_treble_vintf_test
569 using ::android::hardware::details::getDescriptor;
570 std::string actualFqName = getDescriptor(ret.get());
571 CHECK(actualFqName.size() > 0);
572 registerReference(actualFqName, name);
Steven Moreland519306f2017-06-06 17:14:12 -0700573 return false;
574 });
575
576 return ret;
Steven Moreland337e6b62017-01-18 17:25:13 -0800577 }
578
Martijn Coenen67a02492017-03-06 13:04:48 +0100579 Return<bool> add(const hidl_string& /* name */,
Steven Moreland337e6b62017-01-18 17:25:13 -0800580 const sp<IBase>& /* service */) override {
581 LOG(FATAL) << "Cannot register services with passthrough service manager.";
582 return false;
583 }
584
Steven Morelandc601c5f2017-04-06 09:26:07 -0700585 Return<Transport> getTransport(const hidl_string& /* fqName */,
586 const hidl_string& /* name */) {
587 LOG(FATAL) << "Cannot getTransport with passthrough service manager.";
588 return Transport::EMPTY;
589 }
590
Yifan Hong705e5da2017-03-02 16:59:39 -0800591 Return<void> list(list_cb /* _hidl_cb */) override {
592 LOG(FATAL) << "Cannot list services with passthrough service manager.";
Steven Moreland337e6b62017-01-18 17:25:13 -0800593 return Void();
594 }
595 Return<void> listByInterface(const hidl_string& /* fqInstanceName */,
596 listByInterface_cb /* _hidl_cb */) override {
597 // TODO: add this functionality
598 LOG(FATAL) << "Cannot list services with passthrough service manager.";
599 return Void();
600 }
601
602 Return<bool> registerForNotifications(const hidl_string& /* fqName */,
603 const hidl_string& /* name */,
604 const sp<IServiceNotification>& /* callback */) override {
605 // This makes no sense.
606 LOG(FATAL) << "Cannot register for notifications with passthrough service manager.";
607 return false;
608 }
609
Yifan Hong705e5da2017-03-02 16:59:39 -0800610 Return<void> debugDump(debugDump_cb _hidl_cb) override {
611 using Arch = ::android::hidl::base::V1_0::DebugInfo::Architecture;
Yifan Hongbd0d8f72017-05-24 19:43:51 -0700612 using std::literals::string_literals::operator""s;
Jooyung Han219106c2020-07-17 15:01:19 +0900613 static std::string halLibPathVndkSp64 = details::getVndkSpHwPath("lib64");
614 static std::string halLibPathVndkSp32 = details::getVndkSpHwPath("lib");
Jiyong Park56eb1492017-08-04 16:16:13 +0900615 static std::vector<std::pair<Arch, std::vector<const char*>>> sAllPaths{
616 {Arch::IS_64BIT,
Justin Yun6a323ed2018-10-18 18:41:56 +0900617 {
618 HAL_LIBRARY_PATH_ODM_64BIT, HAL_LIBRARY_PATH_VENDOR_64BIT,
619 halLibPathVndkSp64.c_str(),
620#ifndef __ANDROID_VNDK__
621 HAL_LIBRARY_PATH_SYSTEM_64BIT,
622#endif
623 }},
Jiyong Park56eb1492017-08-04 16:16:13 +0900624 {Arch::IS_32BIT,
Justin Yun6a323ed2018-10-18 18:41:56 +0900625 {
626 HAL_LIBRARY_PATH_ODM_32BIT, HAL_LIBRARY_PATH_VENDOR_32BIT,
627 halLibPathVndkSp32.c_str(),
628#ifndef __ANDROID_VNDK__
629 HAL_LIBRARY_PATH_SYSTEM_32BIT,
630#endif
631 }}};
Yifan Hongbd0d8f72017-05-24 19:43:51 -0700632 std::map<std::string, InstanceDebugInfo> map;
Yifan Hong705e5da2017-03-02 16:59:39 -0800633 for (const auto &pair : sAllPaths) {
634 Arch arch = pair.first;
635 for (const auto &path : pair.second) {
Elliott Hughes19306142018-10-26 13:13:04 -0700636 std::vector<std::string> libs = findFiles(path, "", ".so");
Yifan Hong705e5da2017-03-02 16:59:39 -0800637 for (const std::string &lib : libs) {
638 std::string matchedName;
Yifan Hongbd0d8f72017-05-24 19:43:51 -0700639 std::string implName;
640 if (matchPackageName(lib, &matchedName, &implName)) {
641 std::string instanceName{"* ("s + path + ")"s};
642 if (!implName.empty()) instanceName += " ("s + implName + ")"s;
643 map.emplace(path + lib, InstanceDebugInfo{.interfaceName = matchedName,
644 .instanceName = instanceName,
645 .clientPids = {},
646 .arch = arch});
Yifan Hong705e5da2017-03-02 16:59:39 -0800647 }
648 }
649 }
650 }
Yifan Hongbd0d8f72017-05-24 19:43:51 -0700651 fetchPidsForPassthroughLibraries(&map);
652 hidl_vec<InstanceDebugInfo> vec;
653 vec.resize(map.size());
654 size_t idx = 0;
655 for (auto&& pair : map) {
656 vec[idx++] = std::move(pair.second);
657 }
Yifan Hong705e5da2017-03-02 16:59:39 -0800658 _hidl_cb(vec);
Yifan Hong7f49f592017-02-03 15:11:44 -0800659 return Void();
660 }
661
Martijn Coenenaf4aba52017-04-27 09:41:13 -0700662 Return<void> registerPassthroughClient(const hidl_string &, const hidl_string &) override {
Yifan Hong7f49f592017-02-03 15:11:44 -0800663 // This makes no sense.
664 LOG(FATAL) << "Cannot call registerPassthroughClient on passthrough service manager. "
665 << "Call it on defaultServiceManager() instead.";
Yifan Hong9a22d1d2017-01-25 14:19:26 -0800666 return Void();
667 }
668
Steven Moreland2a2678e2017-07-21 18:07:38 -0700669 Return<bool> unregisterForNotifications(const hidl_string& /* fqName */,
670 const hidl_string& /* name */,
671 const sp<IServiceNotification>& /* callback */) override {
672 // This makes no sense.
673 LOG(FATAL) << "Cannot unregister for notifications with passthrough service manager.";
674 return false;
675 }
676
Steven Moreland337e6b62017-01-18 17:25:13 -0800677};
678
Steven Moreland2a2678e2017-07-21 18:07:38 -0700679sp<IServiceManager1_0> getPassthroughServiceManager() {
680 return getPassthroughServiceManager1_1();
681}
682sp<IServiceManager1_1> getPassthroughServiceManager1_1() {
Steven Moreland337e6b62017-01-18 17:25:13 -0800683 static sp<PassthroughServiceManager> manager(new PassthroughServiceManager());
684 return manager;
685}
686
Steven Moreland92b247c2019-08-21 14:53:11 -0700687std::vector<std::string> getAllHalInstanceNames(const std::string& descriptor) {
688 std::vector<std::string> ret;
689 auto sm = defaultServiceManager1_2();
690 sm->listManifestByInterface(descriptor, [&](const auto& instances) {
691 ret.reserve(instances.size());
692 for (const auto& i : instances) {
693 ret.push_back(i);
694 }
695 });
696 return ret;
697}
698
Steven Morelandcbefd352017-01-23 20:29:05 -0800699namespace details {
700
Steven Moreland519306f2017-06-06 17:14:12 -0700701void preloadPassthroughService(const std::string &descriptor) {
702 PassthroughServiceManager::openLibs(descriptor,
703 [&](void* /* handle */, const std::string& /* lib */, const std::string& /* sym */) {
704 // do nothing
705 return true; // open all libs
706 });
707}
708
Steven Morelandcbefd352017-01-23 20:29:05 -0800709struct Waiter : IServiceNotification {
Martijn Coenen773609e2017-10-24 09:57:53 +0200710 Waiter(const std::string& interface, const std::string& instanceName,
711 const sp<IServiceManager1_1>& sm) : mInterfaceName(interface),
712 mInstanceName(instanceName), mSm(sm) {
Martijn Coenene6cdfd32017-11-13 14:03:05 +0100713 }
714
715 void onFirstRef() override {
Martijn Coenenb88ae7f2017-10-24 11:45:41 +0200716 // If this process only has one binder thread, and we're calling wait() from
717 // that thread, it will block forever because we hung up the one and only
718 // binder thread on a condition variable that can only be notified by an
719 // incoming binder call.
Tobias Lindskog88e886f2018-01-05 10:32:43 +0100720 if (IPCThreadState::self()->isOnlyBinderThread()) {
Martijn Coenenb88ae7f2017-10-24 11:45:41 +0200721 LOG(WARNING) << "Can't efficiently wait for " << mInterfaceName << "/"
722 << mInstanceName << ", because we are called from "
723 << "the only binder thread in this process.";
724 return;
725 }
726
Martijn Coenene6cdfd32017-11-13 14:03:05 +0100727 Return<bool> ret = mSm->registerForNotifications(mInterfaceName, mInstanceName, this);
Martijn Coenen773609e2017-10-24 09:57:53 +0200728
729 if (!ret.isOk()) {
730 LOG(ERROR) << "Transport error, " << ret.description()
Martijn Coenene6cdfd32017-11-13 14:03:05 +0100731 << ", during notification registration for " << mInterfaceName << "/"
732 << mInstanceName << ".";
Martijn Coenen773609e2017-10-24 09:57:53 +0200733 return;
734 }
735
736 if (!ret) {
Martijn Coenene6cdfd32017-11-13 14:03:05 +0100737 LOG(ERROR) << "Could not register for notifications for " << mInterfaceName << "/"
738 << mInstanceName << ".";
Martijn Coenen773609e2017-10-24 09:57:53 +0200739 return;
740 }
741
742 mRegisteredForNotifications = true;
743 }
744
745 ~Waiter() {
Martijn Coenen3fa15c22018-02-10 11:30:00 +0100746 if (!mDoneCalled) {
747 LOG(FATAL)
748 << "Waiter still registered for notifications, call done() before dropping ref!";
Martijn Coenen773609e2017-10-24 09:57:53 +0200749 }
750 }
751
Steven Morelandcbefd352017-01-23 20:29:05 -0800752 Return<void> onRegistration(const hidl_string& /* fqName */,
753 const hidl_string& /* name */,
754 bool /* preexisting */) override {
755 std::unique_lock<std::mutex> lock(mMutex);
756 if (mRegistered) {
757 return Void();
758 }
759 mRegistered = true;
760 lock.unlock();
761
762 mCondition.notify_one();
763 return Void();
764 }
765
Steven Moreland0771d8a2018-05-09 13:29:14 -0700766 void wait(bool timeout) {
Steven Moreland49605102017-03-28 09:33:06 -0700767 using std::literals::chrono_literals::operator""s;
768
Martijn Coenen773609e2017-10-24 09:57:53 +0200769 if (!mRegisteredForNotifications) {
770 // As an alternative, just sleep for a second and return
771 LOG(WARNING) << "Waiting one second for " << mInterfaceName << "/" << mInstanceName;
772 sleep(1);
773 return;
774 }
775
Steven Morelandcbefd352017-01-23 20:29:05 -0800776 std::unique_lock<std::mutex> lock(mMutex);
Steven Moreland0771d8a2018-05-09 13:29:14 -0700777 do {
Steven Moreland49605102017-03-28 09:33:06 -0700778 mCondition.wait_for(lock, 1s, [this]{
779 return mRegistered;
780 });
781
782 if (mRegistered) {
783 break;
784 }
785
Steven Moreland0771d8a2018-05-09 13:29:14 -0700786 LOG(WARNING) << "Waited one second for " << mInterfaceName << "/" << mInstanceName;
787 } while (!timeout);
Steven Morelandcbefd352017-01-23 20:29:05 -0800788 }
789
Martijn Coenen773609e2017-10-24 09:57:53 +0200790 // Be careful when using this; after calling reset(), you must always try to retrieve
791 // the corresponding service before blocking on the waiter; otherwise, you might run
792 // into a race-condition where the service has just (re-)registered, you clear the state
793 // here, and subsequently calling waiter->wait() will block forever.
794 void reset() {
795 std::unique_lock<std::mutex> lock(mMutex);
796 mRegistered = false;
797 }
Martijn Coenen3fa15c22018-02-10 11:30:00 +0100798
799 // done() must be called before dropping the last strong ref to the Waiter, to make
800 // sure we can properly unregister with hwservicemanager.
801 void done() {
802 if (mRegisteredForNotifications) {
803 if (!mSm->unregisterForNotifications(mInterfaceName, mInstanceName, this)
804 .withDefault(false)) {
805 LOG(ERROR) << "Could not unregister service notification for " << mInterfaceName
806 << "/" << mInstanceName << ".";
807 } else {
808 mRegisteredForNotifications = false;
809 }
810 }
811 mDoneCalled = true;
812 }
813
814 private:
Martijn Coenen773609e2017-10-24 09:57:53 +0200815 const std::string mInterfaceName;
816 const std::string mInstanceName;
Steven Morelandad1f9922018-10-02 19:37:05 -0700817 sp<IServiceManager1_1> mSm;
Steven Morelandcbefd352017-01-23 20:29:05 -0800818 std::mutex mMutex;
819 std::condition_variable mCondition;
820 bool mRegistered = false;
Martijn Coenen773609e2017-10-24 09:57:53 +0200821 bool mRegisteredForNotifications = false;
Martijn Coenen3fa15c22018-02-10 11:30:00 +0100822 bool mDoneCalled = false;
Steven Morelandcbefd352017-01-23 20:29:05 -0800823};
824
825void waitForHwService(
826 const std::string &interface, const std::string &instanceName) {
Martijn Coenen773609e2017-10-24 09:57:53 +0200827 sp<Waiter> waiter = new Waiter(interface, instanceName, defaultServiceManager1_1());
Steven Moreland0771d8a2018-05-09 13:29:14 -0700828 waiter->wait(false /* timeout */);
Martijn Coenen3fa15c22018-02-10 11:30:00 +0100829 waiter->done();
Martijn Coenen773609e2017-10-24 09:57:53 +0200830}
Steven Morelandcbefd352017-01-23 20:29:05 -0800831
Martijn Coenen773609e2017-10-24 09:57:53 +0200832// Prints relevant error/warning messages for error return values from
833// details::canCastInterface(), both transaction errors (!castReturn.isOk())
834// as well as actual cast failures (castReturn.isOk() && castReturn = false).
835// Returns 'true' if the error is non-fatal and it's useful to retry
836bool handleCastError(const Return<bool>& castReturn, const std::string& descriptor,
837 const std::string& instance) {
838 if (castReturn.isOk()) {
839 if (castReturn) {
840 details::logAlwaysFatal("Successful cast value passed into handleCastError.");
841 }
842 // This should never happen, and there's not really a point in retrying.
843 ALOGE("getService: received incompatible service (bug in hwservicemanager?) for "
844 "%s/%s.", descriptor.c_str(), instance.c_str());
845 return false;
Steven Morelandcbefd352017-01-23 20:29:05 -0800846 }
Martijn Coenen773609e2017-10-24 09:57:53 +0200847 if (castReturn.isDeadObject()) {
848 ALOGW("getService: found dead hwbinder service for %s/%s.", descriptor.c_str(),
849 instance.c_str());
850 return true;
Steven Morelandcbefd352017-01-23 20:29:05 -0800851 }
Martijn Coenen773609e2017-10-24 09:57:53 +0200852 // This can happen due to:
853 // 1) No SELinux permissions
854 // 2) Other transaction failure (no buffer space, kernel error)
855 // The first isn't recoverable, but the second is.
856 // Since we can't yet differentiate between the two, and clients depend
857 // on us not blocking in case 1), treat this as a fatal error for now.
858 ALOGW("getService: unable to call into hwbinder service for %s/%s.",
859 descriptor.c_str(), instance.c_str());
860 return false;
Steven Morelandcbefd352017-01-23 20:29:05 -0800861}
862
Martijn Coenen86c3abb2017-10-24 09:33:28 +0200863sp<::android::hidl::base::V1_0::IBase> getRawServiceInternal(const std::string& descriptor,
864 const std::string& instance,
865 bool retry, bool getStub) {
Steven Moreland84fe49e2019-08-20 17:47:26 +0000866 using Transport = IServiceManager1_0::Transport;
Martijn Coenend35678f2018-03-07 09:51:01 +0100867 sp<Waiter> waiter;
Martijn Coenen86c3abb2017-10-24 09:33:28 +0200868
Yifan Hongdeacf142018-07-10 17:33:34 -0700869 sp<IServiceManager1_1> sm;
870 Transport transport = Transport::EMPTY;
871 if (kIsRecovery) {
Yifan Hongdeacf142018-07-10 17:33:34 -0700872 transport = Transport::PASSTHROUGH;
Yifan Hongdeacf142018-07-10 17:33:34 -0700873 } else {
874 sm = defaultServiceManager1_1();
875 if (sm == nullptr) {
876 ALOGE("getService: defaultServiceManager() is null");
877 return nullptr;
878 }
879
880 Return<Transport> transportRet = sm->getTransport(descriptor, instance);
881
882 if (!transportRet.isOk()) {
883 ALOGE("getService: defaultServiceManager()->getTransport returns %s",
884 transportRet.description().c_str());
885 return nullptr;
886 }
887 transport = transportRet;
Martijn Coenen86c3abb2017-10-24 09:33:28 +0200888 }
889
Martijn Coenen86c3abb2017-10-24 09:33:28 +0200890 const bool vintfHwbinder = (transport == Transport::HWBINDER);
891 const bool vintfPassthru = (transport == Transport::PASSTHROUGH);
Steven Moreland84fe49e2019-08-20 17:47:26 +0000892 const bool trebleTestingOverride = isTrebleTestingOverride();
Steven Morelandf47cdf72021-04-07 22:13:13 +0000893 const bool allowLegacy = !kEnforceVintfManifest || (trebleTestingOverride && isDebuggable());
Steven Moreland84fe49e2019-08-20 17:47:26 +0000894 const bool vintfLegacy = (transport == Transport::EMPTY) && allowLegacy;
Martijn Coenen86c3abb2017-10-24 09:33:28 +0200895
Steven Moreland84fe49e2019-08-20 17:47:26 +0000896 if (!kEnforceVintfManifest) {
897 ALOGE("getService: Potential race detected. The VINTF manifest is not being enforced. If "
898 "a HAL server has a delay in starting and it is not in the manifest, it will not be "
899 "retrieved. Please make sure all HALs on this device are in the VINTF manifest and "
900 "enable PRODUCT_ENFORCE_VINTF_MANIFEST on this device (this is also enabled by "
901 "PRODUCT_FULL_TREBLE). PRODUCT_ENFORCE_VINTF_MANIFEST will ensure that no race "
902 "condition is possible here.");
Steven Morelandaadec3c2019-08-22 17:06:48 -0700903 sleep(1);
Steven Moreland84fe49e2019-08-20 17:47:26 +0000904 }
Martijn Coenen86c3abb2017-10-24 09:33:28 +0200905
Steven Moreland76371242018-04-19 17:11:39 -0700906 for (int tries = 0; !getStub && (vintfHwbinder || vintfLegacy); tries++) {
907 if (waiter == nullptr && tries > 0) {
Martijn Coenend35678f2018-03-07 09:51:01 +0100908 waiter = new Waiter(descriptor, instance, sm);
909 }
Steven Moreland76371242018-04-19 17:11:39 -0700910 if (waiter != nullptr) {
911 waiter->reset(); // don't reorder this -- see comments on reset()
912 }
Martijn Coenen86c3abb2017-10-24 09:33:28 +0200913 Return<sp<IBase>> ret = sm->get(descriptor, instance);
914 if (!ret.isOk()) {
915 ALOGE("getService: defaultServiceManager()->get returns %s for %s/%s.",
916 ret.description().c_str(), descriptor.c_str(), instance.c_str());
917 break;
918 }
919 sp<IBase> base = ret;
Martijn Coenen773609e2017-10-24 09:57:53 +0200920 if (base != nullptr) {
921 Return<bool> canCastRet =
922 details::canCastInterface(base.get(), descriptor.c_str(), true /* emitError */);
923
924 if (canCastRet.isOk() && canCastRet) {
Steven Moreland76371242018-04-19 17:11:39 -0700925 if (waiter != nullptr) {
926 waiter->done();
927 }
Martijn Coenen773609e2017-10-24 09:57:53 +0200928 return base; // still needs to be wrapped by Bp class.
Martijn Coenen86c3abb2017-10-24 09:33:28 +0200929 }
Martijn Coenen773609e2017-10-24 09:57:53 +0200930
931 if (!handleCastError(canCastRet, descriptor, instance)) break;
Martijn Coenen86c3abb2017-10-24 09:33:28 +0200932 }
933
Martijn Coenen773609e2017-10-24 09:57:53 +0200934 // In case of legacy or we were not asked to retry, don't.
935 if (vintfLegacy || !retry) break;
Martijn Coenen86c3abb2017-10-24 09:33:28 +0200936
Steven Moreland76371242018-04-19 17:11:39 -0700937 if (waiter != nullptr) {
938 ALOGI("getService: Trying again for %s/%s...", descriptor.c_str(), instance.c_str());
Steven Moreland0771d8a2018-05-09 13:29:14 -0700939 waiter->wait(true /* timeout */);
Steven Moreland76371242018-04-19 17:11:39 -0700940 }
Martijn Coenen86c3abb2017-10-24 09:33:28 +0200941 }
942
Martijn Coenend35678f2018-03-07 09:51:01 +0100943 if (waiter != nullptr) {
944 waiter->done();
945 }
Martijn Coenen3fa15c22018-02-10 11:30:00 +0100946
Martijn Coenen86c3abb2017-10-24 09:33:28 +0200947 if (getStub || vintfPassthru || vintfLegacy) {
Steven Moreland84fe49e2019-08-20 17:47:26 +0000948 const sp<IServiceManager1_0> pm = getPassthroughServiceManager();
Martijn Coenen86c3abb2017-10-24 09:33:28 +0200949 if (pm != nullptr) {
950 sp<IBase> base = pm->get(descriptor, instance).withDefault(nullptr);
951 if (!getStub || trebleTestingOverride) {
952 base = wrapPassthrough(base);
953 }
954 return base;
955 }
956 }
957
958 return nullptr;
959}
960
Steven Morelandbb9eb2a2018-10-11 12:10:01 -0700961status_t registerAsServiceInternal(const sp<IBase>& service, const std::string& name) {
962 if (service == nullptr) {
963 return UNEXPECTED_NULL;
964 }
965
966 sp<IServiceManager1_2> sm = defaultServiceManager1_2();
967 if (sm == nullptr) {
968 return INVALID_OPERATION;
969 }
970
Steven Moreland84fe49e2019-08-20 17:47:26 +0000971 const std::string descriptor = getDescriptor(service.get());
972
973 if (kEnforceVintfManifest && !isTrebleTestingOverride()) {
974 using Transport = IServiceManager1_0::Transport;
Steven Moreland7d1b0732021-01-21 22:47:40 +0000975 Return<Transport> transport = sm->getTransport(descriptor, name);
976
977 if (!transport.isOk()) {
978 LOG(ERROR) << "Could not get transport for " << descriptor << "/" << name << ": "
979 << transport.description();
980 return UNKNOWN_ERROR;
981 }
Steven Moreland84fe49e2019-08-20 17:47:26 +0000982
983 if (transport != Transport::HWBINDER) {
984 LOG(ERROR) << "Service " << descriptor << "/" << name
985 << " must be in VINTF manifest in order to register/get.";
986 return UNKNOWN_ERROR;
987 }
988 }
989
Steven Morelandbb9eb2a2018-10-11 12:10:01 -0700990 bool registered = false;
991 Return<void> ret = service->interfaceChain([&](const auto& chain) {
992 registered = sm->addWithChain(name.c_str(), service, chain).withDefault(false);
993 });
994
995 if (!ret.isOk()) {
996 LOG(ERROR) << "Could not retrieve interface chain: " << ret.description();
997 }
998
999 if (registered) {
Steven Moreland84fe49e2019-08-20 17:47:26 +00001000 onRegistrationImpl(descriptor, name);
Steven Morelandbb9eb2a2018-10-11 12:10:01 -07001001 }
1002
1003 return registered ? OK : UNKNOWN_ERROR;
1004}
1005
Bernhard Rosenkränzer0c28fd22018-06-04 16:01:47 +02001006} // namespace details
Steven Morelandcbefd352017-01-23 20:29:05 -08001007
Bernhard Rosenkränzer0c28fd22018-06-04 16:01:47 +02001008} // namespace hardware
1009} // namespace android