blob: 5d1979fdd0785b372ab07ca8db49d983828fc57f [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 Morelandf47cdf72021-04-07 22:13:13 +0000178 if (kEnforceVintfManifest && !isDebuggable()) {
Steven Morelandb939e7e2020-07-08 01:04:23 +0000179 // don't allow testing override in production
180 return false;
181 }
182
Steven Moreland206d2812020-07-08 21:21:48 +0000183 return *getTrebleTestingOverridePtr();
Steven Morelandb939e7e2020-07-08 01:04:23 +0000184}
185
Steven Morelandbb9eb2a2018-10-11 12:10:01 -0700186static void onRegistrationImpl(const std::string& descriptor, const std::string& instanceName) {
Steven Moreland489e7572021-01-21 19:02:54 +0000187 LOG(INFO) << "Registered " << descriptor << "/" << instanceName;
Steven Morelandbb9eb2a2018-10-11 12:10:01 -0700188 tryShortenProcessName(descriptor);
189}
190
Steven Moreland911852c2020-05-11 10:51:26 -0700191// only used by prebuilts - should be able to remove
Steven Morelandbb9eb2a2018-10-11 12:10:01 -0700192void onRegistration(const std::string& packageName, const std::string& interfaceName,
193 const std::string& instanceName) {
194 return onRegistrationImpl(packageName + "::" + interfaceName, instanceName);
Steven Moreland405d7612017-04-07 20:31:22 -0700195}
196
197} // details
198
Steven Moreland2a2678e2017-07-21 18:07:38 -0700199sp<IServiceManager1_0> defaultServiceManager() {
Steven Moreland89909692018-05-30 14:11:19 -0700200 return defaultServiceManager1_2();
Steven Moreland2a2678e2017-07-21 18:07:38 -0700201}
202sp<IServiceManager1_1> defaultServiceManager1_1() {
Steven Moreland89909692018-05-30 14:11:19 -0700203 return defaultServiceManager1_2();
204}
Devin Mooree1973ce2022-04-15 20:54:03 +0000205static bool isServiceManager(const hidl_string& fqName) {
206 return fqName == IServiceManager1_0::descriptor || fqName == IServiceManager1_1::descriptor ||
207 fqName == IServiceManager1_2::descriptor;
208}
209static bool isHwServiceManagerInstalled() {
210 return access("/system/bin/hwservicemanager", F_OK) == 0;
211}
212
213/*
214 * A replacement for hwservicemanager when it is not installed on a device.
215 *
216 * Clients in the framework need to continue supporting HIDL services through
217 * hwservicemanager for upgrading devices. Being unable to get an instance of
218 * hardware service manager is a hard error, so this implementation is returned
219 * to be able service the requests and tell clients there are no services
220 * registered.
221 */
222struct NoHwServiceManager : public IServiceManager1_2 {
223 Return<sp<IBase>> get(const hidl_string& fqName, const hidl_string&) override {
224 sp<IBase> ret = nullptr;
225
226 if (isServiceManager(fqName)) {
227 ret = defaultServiceManager1_2();
228 }
229 return ret;
230 }
231
232 Return<bool> add(const hidl_string& name, const sp<IBase>& /* service */) override {
233 LOG(INFO) << "Cannot add " << name << " without hwservicemanager";
234 return false;
235 }
236
237 Return<Transport> getTransport(const hidl_string& fqName, const hidl_string& name) {
238 LOG(INFO) << "Trying to get transport of " << fqName << "/" << name
239 << " without hwservicemanager";
240 return Transport::PASSTHROUGH;
241 }
242
243 Return<void> list(list_cb _hidl_cb) override {
244 _hidl_cb({});
245 LOG(INFO) << "Cannot list all services without hwservicemanager";
246 return Void();
247 }
248 Return<void> listByInterface(const hidl_string& fqName, listByInterface_cb _hidl_cb) override {
249 _hidl_cb({});
250 LOG(INFO) << "Cannot list service " << fqName << " without hwservicemanager";
251 return Void();
252 }
253
254 Return<bool> registerForNotifications(const hidl_string& fqName, const hidl_string& name,
255 const sp<IServiceNotification>& /* callback */) override {
256 LOG(INFO) << "Cannot register for notifications for " << fqName << "/" << name
257 << " without hwservicemanager";
258 return false;
259 }
260
261 Return<void> debugDump(debugDump_cb _hidl_cb) override {
262 _hidl_cb({});
263 return Void();
264 }
265
266 Return<void> registerPassthroughClient(const hidl_string& fqName,
267 const hidl_string& name) override {
268 LOG(INFO) << "This process is a client of " << fqName << "/" << name
269 << " passthrough HAL, but it won't show up in lshal because hwservicemanager is "
270 "not installed";
271 return Void();
272 }
273
274 Return<bool> unregisterForNotifications(
275 const hidl_string& fqName, const hidl_string& name,
276 const sp<IServiceNotification>& /* callback */) override {
277 LOG(INFO) << "Cannot unregister for notifications for " << fqName << "/" << name
278 << " without hwservicemanager";
279 return false;
280 }
281 Return<bool> registerClientCallback(const hidl_string& fqName, const hidl_string& name,
282 const sp<IBase>&, const sp<IClientCallback>&) {
283 LOG(INFO) << "Cannot add client callback for " << fqName << "/" << name
284 << " without hwservicemanager";
285 return false;
286 }
287 Return<bool> unregisterClientCallback(const sp<IBase>&, const sp<IClientCallback>&) {
288 LOG(INFO) << "Cannot unregister client callbacks without hwservicemanager";
289 return false;
290 }
291 Return<bool> addWithChain(const hidl_string& fqName, const sp<IBase>&,
292 const hidl_vec<hidl_string>&) {
293 LOG(INFO) << "Cannot add " << fqName << " with chain without hwservicemanager";
294 return false;
295 }
296 Return<void> listManifestByInterface(const hidl_string& fqName, listManifestByInterface_cb) {
297 LOG(INFO) << "Cannot list manifest for " << fqName << " without hwservicemanager";
298 return Void();
299 }
300 Return<bool> tryUnregister(const hidl_string& fqName, const hidl_string& name,
301 const sp<IBase>&) {
302 LOG(INFO) << "Cannot unregister service " << fqName << "/" << name
303 << " without hwservicemanager";
304 return false;
305 }
306};
307
Steven Moreland89909692018-05-30 14:11:19 -0700308sp<IServiceManager1_2> defaultServiceManager1_2() {
309 using android::hidl::manager::V1_2::BnHwServiceManager;
310 using android::hidl::manager::V1_2::BpHwServiceManager;
311
Steven Moreland10d55632020-04-21 14:10:53 -0700312 static std::mutex& gDefaultServiceManagerLock = *new std::mutex;
313 static sp<IServiceManager1_2>& gDefaultServiceManager = *new sp<IServiceManager1_2>;
Steven Moreland89909692018-05-30 14:11:19 -0700314
Steven Moreland5d5ef7f2016-10-20 19:19:55 -0700315 {
Steven Moreland89909692018-05-30 14:11:19 -0700316 std::lock_guard<std::mutex> _l(gDefaultServiceManagerLock);
317 if (gDefaultServiceManager != nullptr) {
318 return gDefaultServiceManager;
Yifan Hong8fb656b2017-03-16 14:30:40 -0700319 }
Steven Moreland405d7612017-04-07 20:31:22 -0700320
Devin Mooree1973ce2022-04-15 20:54:03 +0000321 if (!isHwServiceManagerInstalled()) {
322 // hwservicemanager is not available on this device.
323 gDefaultServiceManager = sp<NoHwServiceManager>::make();
324 return gDefaultServiceManager;
325 }
326
Yifan Hong8fb656b2017-03-16 14:30:40 -0700327 if (access("/dev/hwbinder", F_OK|R_OK|W_OK) != 0) {
328 // HwBinder not available on this device or not accessible to
329 // this process.
330 return nullptr;
331 }
Steven Morelandc1cee2c2017-03-24 16:23:11 +0000332
333 waitForHwServiceManager();
334
Steven Moreland89909692018-05-30 14:11:19 -0700335 while (gDefaultServiceManager == nullptr) {
336 gDefaultServiceManager =
337 fromBinder<IServiceManager1_2, BpHwServiceManager, BnHwServiceManager>(
338 ProcessState::self()->getContextObject(nullptr));
339 if (gDefaultServiceManager == nullptr) {
Steven Morelandc1cee2c2017-03-24 16:23:11 +0000340 LOG(ERROR) << "Waited for hwservicemanager, but got nullptr.";
Steven Moreland5d5ef7f2016-10-20 19:19:55 -0700341 sleep(1);
Yifan Hong8fb656b2017-03-16 14:30:40 -0700342 }
Steven Moreland5d5ef7f2016-10-20 19:19:55 -0700343 }
344 }
345
Steven Moreland89909692018-05-30 14:11:19 -0700346 return gDefaultServiceManager;
Steven Moreland5d5ef7f2016-10-20 19:19:55 -0700347}
348
Elliott Hughes19306142018-10-26 13:13:04 -0700349static std::vector<std::string> findFiles(const std::string& path, const std::string& prefix,
350 const std::string& suffix) {
Steven Moreland0091c092017-01-20 23:15:18 +0000351 std::unique_ptr<DIR, decltype(&closedir)> dir(opendir(path.c_str()), closedir);
352 if (!dir) return {};
353
354 std::vector<std::string> results{};
355
356 dirent* dp;
357 while ((dp = readdir(dir.get())) != nullptr) {
358 std::string name = dp->d_name;
359
Steven Moreland8092aa02018-10-12 15:54:56 -0700360 if (base::StartsWith(name, prefix) && base::EndsWith(name, suffix)) {
Steven Moreland0091c092017-01-20 23:15:18 +0000361 results.push_back(name);
362 }
363 }
364
365 return results;
366}
367
Steven Morelandccc3a972020-02-26 16:38:31 -0800368static bool matchPackageName(const std::string& lib, std::string* matchedName,
369 std::string* implName) {
370#define RE_COMPONENT "[a-zA-Z_][a-zA-Z_0-9]*"
371#define RE_PATH RE_COMPONENT "(?:[.]" RE_COMPONENT ")*"
372 static const std::regex gLibraryFileNamePattern("(" RE_PATH "@[0-9]+[.][0-9]+)-impl(.*?).so");
373#undef RE_PATH
374#undef RE_COMPONENT
375
Yifan Hong9a22d1d2017-01-25 14:19:26 -0800376 std::smatch match;
377 if (std::regex_match(lib, match, gLibraryFileNamePattern)) {
378 *matchedName = match.str(1) + "::I*";
Yifan Hongbd0d8f72017-05-24 19:43:51 -0700379 *implName = match.str(2);
Yifan Hong9a22d1d2017-01-25 14:19:26 -0800380 return true;
381 }
382 return false;
383}
384
Yifan Hong7f49f592017-02-03 15:11:44 -0800385static void registerReference(const hidl_string &interfaceName, const hidl_string &instanceName) {
Yifan Hongdeacf142018-07-10 17:33:34 -0700386 if (kIsRecovery) {
387 // No hwservicemanager in recovery.
388 return;
389 }
390
Steven Moreland2a2678e2017-07-21 18:07:38 -0700391 sp<IServiceManager1_0> binderizedManager = defaultServiceManager();
Yifan Hong7f49f592017-02-03 15:11:44 -0800392 if (binderizedManager == nullptr) {
393 LOG(WARNING) << "Could not registerReference for "
394 << interfaceName << "/" << instanceName
395 << ": null binderized manager.";
396 return;
397 }
Martijn Coenenaf4aba52017-04-27 09:41:13 -0700398 auto ret = binderizedManager->registerPassthroughClient(interfaceName, instanceName);
Yifan Hong7f49f592017-02-03 15:11:44 -0800399 if (!ret.isOk()) {
400 LOG(WARNING) << "Could not registerReference for "
401 << interfaceName << "/" << instanceName
402 << ": " << ret.description();
Steven Moreland0aeaa782017-03-22 08:11:07 -0700403 return;
Yifan Hong7f49f592017-02-03 15:11:44 -0800404 }
Steven Morelande681aa52017-02-15 16:22:37 -0800405 LOG(VERBOSE) << "Successfully registerReference for "
406 << interfaceName << "/" << instanceName;
Yifan Hong7f49f592017-02-03 15:11:44 -0800407}
408
Yifan Hongbd0d8f72017-05-24 19:43:51 -0700409using InstanceDebugInfo = hidl::manager::V1_0::IServiceManager::InstanceDebugInfo;
410static inline void fetchPidsForPassthroughLibraries(
411 std::map<std::string, InstanceDebugInfo>* infos) {
412 static const std::string proc = "/proc/";
413
414 std::map<std::string, std::set<pid_t>> pids;
415 std::unique_ptr<DIR, decltype(&closedir)> dir(opendir(proc.c_str()), closedir);
416 if (!dir) return;
417 dirent* dp;
418 while ((dp = readdir(dir.get())) != nullptr) {
Stephen Hinesfd9ecee2017-09-27 18:52:52 -0700419 pid_t pid = strtoll(dp->d_name, nullptr, 0);
Yifan Hongbd0d8f72017-05-24 19:43:51 -0700420 if (pid == 0) continue;
421 std::string mapsPath = proc + dp->d_name + "/maps";
422 std::ifstream ifs{mapsPath};
423 if (!ifs.is_open()) continue;
424
425 for (std::string line; std::getline(ifs, line);) {
426 // The last token of line should look like
427 // vendor/lib64/hw/android.hardware.foo@1.0-impl-extra.so
428 // Use some simple filters to ignore bad lines before extracting libFileName
429 // and checking the key in info to make parsing faster.
430 if (line.back() != 'o') continue;
431 if (line.rfind('@') == std::string::npos) continue;
432
433 auto spacePos = line.rfind(' ');
434 if (spacePos == std::string::npos) continue;
435 auto libFileName = line.substr(spacePos + 1);
436 auto it = infos->find(libFileName);
437 if (it == infos->end()) continue;
438 pids[libFileName].insert(pid);
439 }
440 }
441 for (auto& pair : *infos) {
442 pair.second.clientPids =
443 std::vector<pid_t>{pids[pair.first].begin(), pids[pair.first].end()};
444 }
445}
446
Steven Moreland2a2678e2017-07-21 18:07:38 -0700447struct PassthroughServiceManager : IServiceManager1_1 {
Chih-Hung Hsieh41649d52017-08-03 14:27:21 -0700448 static void openLibs(
449 const std::string& fqName,
450 const std::function<bool /* continue */ (void* /* handle */, const std::string& /* lib */,
451 const std::string& /* sym */)>& eachLib) {
Steven Moreland819c05d2017-04-06 17:24:22 -0700452 //fqName looks like android.hardware.foo@1.0::IFoo
Steven Moreland519306f2017-06-06 17:14:12 -0700453 size_t idx = fqName.find("::");
Steven Moreland819c05d2017-04-06 17:24:22 -0700454
455 if (idx == std::string::npos ||
Steven Moreland519306f2017-06-06 17:14:12 -0700456 idx + strlen("::") + 1 >= fqName.size()) {
Steven Moreland337e6b62017-01-18 17:25:13 -0800457 LOG(ERROR) << "Invalid interface name passthrough lookup: " << fqName;
Steven Moreland519306f2017-06-06 17:14:12 -0700458 return;
Steven Moreland337e6b62017-01-18 17:25:13 -0800459 }
460
Steven Moreland519306f2017-06-06 17:14:12 -0700461 std::string packageAndVersion = fqName.substr(0, idx);
462 std::string ifaceName = fqName.substr(idx + strlen("::"));
Steven Moreland819c05d2017-04-06 17:24:22 -0700463
464 const std::string prefix = packageAndVersion + "-impl";
465 const std::string sym = "HIDL_FETCH_" + ifaceName;
Steven Moreland348802d2017-02-23 12:48:55 -0800466
Steven Moreland77f4c852017-10-12 11:05:53 -0700467 constexpr int dlMode = RTLD_LAZY;
468 void* handle = nullptr;
Steven Moreland337e6b62017-01-18 17:25:13 -0800469
Steven Morelanda29905c2017-03-01 10:42:35 -0800470 dlerror(); // clear
471
Jooyung Han219106c2020-07-17 15:01:19 +0900472 static std::string halLibPathVndkSp = details::getVndkSpHwPath();
Justin Yun6a323ed2018-10-18 18:41:56 +0900473 std::vector<std::string> paths = {
474 HAL_LIBRARY_PATH_ODM, HAL_LIBRARY_PATH_VENDOR, halLibPathVndkSp,
475#ifndef __ANDROID_VNDK__
476 HAL_LIBRARY_PATH_SYSTEM,
477#endif
478 };
Steven Moreland77f4c852017-10-12 11:05:53 -0700479
Steven Morelandb939e7e2020-07-08 01:04:23 +0000480 if (details::isTrebleTestingOverride()) {
Steven Moreland77f4c852017-10-12 11:05:53 -0700481 // Load HAL implementations that are statically linked
482 handle = dlopen(nullptr, dlMode);
483 if (handle == nullptr) {
484 const char* error = dlerror();
485 LOG(ERROR) << "Failed to dlopen self: "
486 << (error == nullptr ? "unknown error" : error);
487 } else if (!eachLib(handle, "SELF", sym)) {
488 return;
489 }
Steven Morelandf7dea692017-07-14 12:49:28 -0700490 }
Steven Moreland77f4c852017-10-12 11:05:53 -0700491
Steven Morelandf7dea692017-07-14 12:49:28 -0700492 for (const std::string& path : paths) {
Elliott Hughes19306142018-10-26 13:13:04 -0700493 std::vector<std::string> libs = findFiles(path, prefix, ".so");
Steven Moreland0091c092017-01-20 23:15:18 +0000494
Steven Moreland0091c092017-01-20 23:15:18 +0000495 for (const std::string &lib : libs) {
Steven Moreland348802d2017-02-23 12:48:55 -0800496 const std::string fullPath = path + lib;
497
Yifan Hongdeacf142018-07-10 17:33:34 -0700498 if (kIsRecovery || path == HAL_LIBRARY_PATH_SYSTEM) {
Jiyong Park3ab546c2017-04-06 20:28:07 +0900499 handle = dlopen(fullPath.c_str(), dlMode);
Steven Moreland65c00cb2017-10-12 11:35:22 -0700500 } else {
Steven Moreland2056cf32019-09-17 17:41:02 -0700501#if !defined(__ANDROID_RECOVERY__) && defined(__ANDROID__)
Steven Moreland65c00cb2017-10-12 11:35:22 -0700502 handle = android_load_sphal_library(fullPath.c_str(), dlMode);
Jerry Zhang86ae9992018-05-30 17:11:09 -0700503#endif
Jiyong Park3ab546c2017-04-06 20:28:07 +0900504 }
505
Steven Moreland348802d2017-02-23 12:48:55 -0800506 if (handle == nullptr) {
507 const char* error = dlerror();
508 LOG(ERROR) << "Failed to dlopen " << lib << ": "
509 << (error == nullptr ? "unknown error" : error);
510 continue;
Steven Moreland0091c092017-01-20 23:15:18 +0000511 }
Steven Moreland348802d2017-02-23 12:48:55 -0800512
Steven Moreland519306f2017-06-06 17:14:12 -0700513 if (!eachLib(handle, lib, sym)) {
514 return;
Steven Moreland348802d2017-02-23 12:48:55 -0800515 }
Steven Moreland337e6b62017-01-18 17:25:13 -0800516 }
517 }
Steven Moreland519306f2017-06-06 17:14:12 -0700518 }
Steven Moreland337e6b62017-01-18 17:25:13 -0800519
Steven Moreland519306f2017-06-06 17:14:12 -0700520 Return<sp<IBase>> get(const hidl_string& fqName,
521 const hidl_string& name) override {
522 sp<IBase> ret = nullptr;
Devin Mooree1973ce2022-04-15 20:54:03 +0000523 // This is required to run without hwservicemanager while we have
524 // passthrough HIDL services. Once the passthrough HIDL services have
525 // been removed, the PassthroughServiceManager will no longer be needed.
526 if (!isHwServiceManagerInstalled() && isServiceManager(fqName)) {
527 return defaultServiceManager1_2();
528 }
Steven Moreland519306f2017-06-06 17:14:12 -0700529
530 openLibs(fqName, [&](void* handle, const std::string &lib, const std::string &sym) {
531 IBase* (*generator)(const char* name);
532 *(void **)(&generator) = dlsym(handle, sym.c_str());
533 if(!generator) {
534 const char* error = dlerror();
Steven Morelandedd49122021-02-17 22:41:51 +0000535 LOG(ERROR) << "Passthrough lookup opened " << lib << " but could not find symbol "
536 << sym << ": " << (error == nullptr ? "unknown error" : error)
537 << ". Keeping library open.";
538
539 // dlclose too problematic in multi-threaded environment
540 // dlclose(handle);
541
542 return true; // continue
Steven Moreland519306f2017-06-06 17:14:12 -0700543 }
544
545 ret = (*generator)(name.c_str());
546
547 if (ret == nullptr) {
Steven Morelandedd49122021-02-17 22:41:51 +0000548 LOG(ERROR) << "Could not find instance '" << name.c_str() << "' in library " << lib
549 << ". Keeping library open.";
550
551 // dlclose too problematic in multi-threaded environment
552 // dlclose(handle);
553
554 // this module doesn't provide this particular instance
555 return true; // continue
Steven Moreland519306f2017-06-06 17:14:12 -0700556 }
557
Steven Morelande69e8d32018-03-14 10:55:42 -0700558 // Actual fqname might be a subclass.
559 // This assumption is tested in vts_treble_vintf_test
560 using ::android::hardware::details::getDescriptor;
561 std::string actualFqName = getDescriptor(ret.get());
562 CHECK(actualFqName.size() > 0);
563 registerReference(actualFqName, name);
Steven Moreland519306f2017-06-06 17:14:12 -0700564 return false;
565 });
566
567 return ret;
Steven Moreland337e6b62017-01-18 17:25:13 -0800568 }
569
Martijn Coenen67a02492017-03-06 13:04:48 +0100570 Return<bool> add(const hidl_string& /* name */,
Steven Moreland337e6b62017-01-18 17:25:13 -0800571 const sp<IBase>& /* service */) override {
572 LOG(FATAL) << "Cannot register services with passthrough service manager.";
573 return false;
574 }
575
Steven Morelandc601c5f2017-04-06 09:26:07 -0700576 Return<Transport> getTransport(const hidl_string& /* fqName */,
577 const hidl_string& /* name */) {
578 LOG(FATAL) << "Cannot getTransport with passthrough service manager.";
579 return Transport::EMPTY;
580 }
581
Yifan Hong705e5da2017-03-02 16:59:39 -0800582 Return<void> list(list_cb /* _hidl_cb */) override {
583 LOG(FATAL) << "Cannot list services with passthrough service manager.";
Steven Moreland337e6b62017-01-18 17:25:13 -0800584 return Void();
585 }
586 Return<void> listByInterface(const hidl_string& /* fqInstanceName */,
587 listByInterface_cb /* _hidl_cb */) override {
588 // TODO: add this functionality
589 LOG(FATAL) << "Cannot list services with passthrough service manager.";
590 return Void();
591 }
592
593 Return<bool> registerForNotifications(const hidl_string& /* fqName */,
594 const hidl_string& /* name */,
595 const sp<IServiceNotification>& /* callback */) override {
596 // This makes no sense.
597 LOG(FATAL) << "Cannot register for notifications with passthrough service manager.";
598 return false;
599 }
600
Yifan Hong705e5da2017-03-02 16:59:39 -0800601 Return<void> debugDump(debugDump_cb _hidl_cb) override {
602 using Arch = ::android::hidl::base::V1_0::DebugInfo::Architecture;
Yifan Hongbd0d8f72017-05-24 19:43:51 -0700603 using std::literals::string_literals::operator""s;
Jooyung Han219106c2020-07-17 15:01:19 +0900604 static std::string halLibPathVndkSp64 = details::getVndkSpHwPath("lib64");
605 static std::string halLibPathVndkSp32 = details::getVndkSpHwPath("lib");
Jiyong Park56eb1492017-08-04 16:16:13 +0900606 static std::vector<std::pair<Arch, std::vector<const char*>>> sAllPaths{
607 {Arch::IS_64BIT,
Justin Yun6a323ed2018-10-18 18:41:56 +0900608 {
609 HAL_LIBRARY_PATH_ODM_64BIT, HAL_LIBRARY_PATH_VENDOR_64BIT,
610 halLibPathVndkSp64.c_str(),
611#ifndef __ANDROID_VNDK__
612 HAL_LIBRARY_PATH_SYSTEM_64BIT,
613#endif
614 }},
Jiyong Park56eb1492017-08-04 16:16:13 +0900615 {Arch::IS_32BIT,
Justin Yun6a323ed2018-10-18 18:41:56 +0900616 {
617 HAL_LIBRARY_PATH_ODM_32BIT, HAL_LIBRARY_PATH_VENDOR_32BIT,
618 halLibPathVndkSp32.c_str(),
619#ifndef __ANDROID_VNDK__
620 HAL_LIBRARY_PATH_SYSTEM_32BIT,
621#endif
622 }}};
Yifan Hongbd0d8f72017-05-24 19:43:51 -0700623 std::map<std::string, InstanceDebugInfo> map;
Yifan Hong705e5da2017-03-02 16:59:39 -0800624 for (const auto &pair : sAllPaths) {
625 Arch arch = pair.first;
626 for (const auto &path : pair.second) {
Elliott Hughes19306142018-10-26 13:13:04 -0700627 std::vector<std::string> libs = findFiles(path, "", ".so");
Yifan Hong705e5da2017-03-02 16:59:39 -0800628 for (const std::string &lib : libs) {
629 std::string matchedName;
Yifan Hongbd0d8f72017-05-24 19:43:51 -0700630 std::string implName;
631 if (matchPackageName(lib, &matchedName, &implName)) {
632 std::string instanceName{"* ("s + path + ")"s};
633 if (!implName.empty()) instanceName += " ("s + implName + ")"s;
634 map.emplace(path + lib, InstanceDebugInfo{.interfaceName = matchedName,
635 .instanceName = instanceName,
636 .clientPids = {},
637 .arch = arch});
Yifan Hong705e5da2017-03-02 16:59:39 -0800638 }
639 }
640 }
641 }
Yifan Hongbd0d8f72017-05-24 19:43:51 -0700642 fetchPidsForPassthroughLibraries(&map);
643 hidl_vec<InstanceDebugInfo> vec;
644 vec.resize(map.size());
645 size_t idx = 0;
646 for (auto&& pair : map) {
647 vec[idx++] = std::move(pair.second);
648 }
Yifan Hong705e5da2017-03-02 16:59:39 -0800649 _hidl_cb(vec);
Yifan Hong7f49f592017-02-03 15:11:44 -0800650 return Void();
651 }
652
Martijn Coenenaf4aba52017-04-27 09:41:13 -0700653 Return<void> registerPassthroughClient(const hidl_string &, const hidl_string &) override {
Yifan Hong7f49f592017-02-03 15:11:44 -0800654 // This makes no sense.
655 LOG(FATAL) << "Cannot call registerPassthroughClient on passthrough service manager. "
656 << "Call it on defaultServiceManager() instead.";
Yifan Hong9a22d1d2017-01-25 14:19:26 -0800657 return Void();
658 }
659
Steven Moreland2a2678e2017-07-21 18:07:38 -0700660 Return<bool> unregisterForNotifications(const hidl_string& /* fqName */,
661 const hidl_string& /* name */,
662 const sp<IServiceNotification>& /* callback */) override {
663 // This makes no sense.
664 LOG(FATAL) << "Cannot unregister for notifications with passthrough service manager.";
665 return false;
666 }
667
Steven Moreland337e6b62017-01-18 17:25:13 -0800668};
669
Steven Moreland2a2678e2017-07-21 18:07:38 -0700670sp<IServiceManager1_0> getPassthroughServiceManager() {
671 return getPassthroughServiceManager1_1();
672}
673sp<IServiceManager1_1> getPassthroughServiceManager1_1() {
Steven Moreland337e6b62017-01-18 17:25:13 -0800674 static sp<PassthroughServiceManager> manager(new PassthroughServiceManager());
675 return manager;
676}
677
Steven Moreland92b247c2019-08-21 14:53:11 -0700678std::vector<std::string> getAllHalInstanceNames(const std::string& descriptor) {
679 std::vector<std::string> ret;
680 auto sm = defaultServiceManager1_2();
681 sm->listManifestByInterface(descriptor, [&](const auto& instances) {
682 ret.reserve(instances.size());
683 for (const auto& i : instances) {
684 ret.push_back(i);
685 }
686 });
687 return ret;
688}
689
Steven Morelandcbefd352017-01-23 20:29:05 -0800690namespace details {
691
Steven Moreland519306f2017-06-06 17:14:12 -0700692void preloadPassthroughService(const std::string &descriptor) {
693 PassthroughServiceManager::openLibs(descriptor,
694 [&](void* /* handle */, const std::string& /* lib */, const std::string& /* sym */) {
695 // do nothing
696 return true; // open all libs
697 });
698}
699
Steven Morelandcbefd352017-01-23 20:29:05 -0800700struct Waiter : IServiceNotification {
Martijn Coenen773609e2017-10-24 09:57:53 +0200701 Waiter(const std::string& interface, const std::string& instanceName,
702 const sp<IServiceManager1_1>& sm) : mInterfaceName(interface),
703 mInstanceName(instanceName), mSm(sm) {
Martijn Coenene6cdfd32017-11-13 14:03:05 +0100704 }
705
706 void onFirstRef() override {
Martijn Coenenb88ae7f2017-10-24 11:45:41 +0200707 // If this process only has one binder thread, and we're calling wait() from
708 // that thread, it will block forever because we hung up the one and only
709 // binder thread on a condition variable that can only be notified by an
710 // incoming binder call.
Tobias Lindskog88e886f2018-01-05 10:32:43 +0100711 if (IPCThreadState::self()->isOnlyBinderThread()) {
Martijn Coenenb88ae7f2017-10-24 11:45:41 +0200712 LOG(WARNING) << "Can't efficiently wait for " << mInterfaceName << "/"
713 << mInstanceName << ", because we are called from "
714 << "the only binder thread in this process.";
715 return;
716 }
717
Martijn Coenene6cdfd32017-11-13 14:03:05 +0100718 Return<bool> ret = mSm->registerForNotifications(mInterfaceName, mInstanceName, this);
Martijn Coenen773609e2017-10-24 09:57:53 +0200719
720 if (!ret.isOk()) {
721 LOG(ERROR) << "Transport error, " << ret.description()
Martijn Coenene6cdfd32017-11-13 14:03:05 +0100722 << ", during notification registration for " << mInterfaceName << "/"
723 << mInstanceName << ".";
Martijn Coenen773609e2017-10-24 09:57:53 +0200724 return;
725 }
726
727 if (!ret) {
Martijn Coenene6cdfd32017-11-13 14:03:05 +0100728 LOG(ERROR) << "Could not register for notifications for " << mInterfaceName << "/"
729 << mInstanceName << ".";
Martijn Coenen773609e2017-10-24 09:57:53 +0200730 return;
731 }
732
733 mRegisteredForNotifications = true;
734 }
735
736 ~Waiter() {
Martijn Coenen3fa15c22018-02-10 11:30:00 +0100737 if (!mDoneCalled) {
738 LOG(FATAL)
739 << "Waiter still registered for notifications, call done() before dropping ref!";
Martijn Coenen773609e2017-10-24 09:57:53 +0200740 }
741 }
742
Steven Morelandcbefd352017-01-23 20:29:05 -0800743 Return<void> onRegistration(const hidl_string& /* fqName */,
744 const hidl_string& /* name */,
745 bool /* preexisting */) override {
746 std::unique_lock<std::mutex> lock(mMutex);
747 if (mRegistered) {
748 return Void();
749 }
750 mRegistered = true;
751 lock.unlock();
752
753 mCondition.notify_one();
754 return Void();
755 }
756
Steven Moreland0771d8a2018-05-09 13:29:14 -0700757 void wait(bool timeout) {
Steven Moreland49605102017-03-28 09:33:06 -0700758 using std::literals::chrono_literals::operator""s;
759
Martijn Coenen773609e2017-10-24 09:57:53 +0200760 if (!mRegisteredForNotifications) {
761 // As an alternative, just sleep for a second and return
762 LOG(WARNING) << "Waiting one second for " << mInterfaceName << "/" << mInstanceName;
763 sleep(1);
764 return;
765 }
766
Steven Morelandcbefd352017-01-23 20:29:05 -0800767 std::unique_lock<std::mutex> lock(mMutex);
Steven Moreland0771d8a2018-05-09 13:29:14 -0700768 do {
Steven Moreland49605102017-03-28 09:33:06 -0700769 mCondition.wait_for(lock, 1s, [this]{
770 return mRegistered;
771 });
772
773 if (mRegistered) {
774 break;
775 }
776
Steven Moreland0771d8a2018-05-09 13:29:14 -0700777 LOG(WARNING) << "Waited one second for " << mInterfaceName << "/" << mInstanceName;
778 } while (!timeout);
Steven Morelandcbefd352017-01-23 20:29:05 -0800779 }
780
Martijn Coenen773609e2017-10-24 09:57:53 +0200781 // Be careful when using this; after calling reset(), you must always try to retrieve
782 // the corresponding service before blocking on the waiter; otherwise, you might run
783 // into a race-condition where the service has just (re-)registered, you clear the state
784 // here, and subsequently calling waiter->wait() will block forever.
785 void reset() {
786 std::unique_lock<std::mutex> lock(mMutex);
787 mRegistered = false;
788 }
Martijn Coenen3fa15c22018-02-10 11:30:00 +0100789
790 // done() must be called before dropping the last strong ref to the Waiter, to make
791 // sure we can properly unregister with hwservicemanager.
792 void done() {
793 if (mRegisteredForNotifications) {
794 if (!mSm->unregisterForNotifications(mInterfaceName, mInstanceName, this)
795 .withDefault(false)) {
796 LOG(ERROR) << "Could not unregister service notification for " << mInterfaceName
797 << "/" << mInstanceName << ".";
798 } else {
799 mRegisteredForNotifications = false;
800 }
801 }
802 mDoneCalled = true;
803 }
804
805 private:
Martijn Coenen773609e2017-10-24 09:57:53 +0200806 const std::string mInterfaceName;
807 const std::string mInstanceName;
Steven Morelandad1f9922018-10-02 19:37:05 -0700808 sp<IServiceManager1_1> mSm;
Steven Morelandcbefd352017-01-23 20:29:05 -0800809 std::mutex mMutex;
810 std::condition_variable mCondition;
811 bool mRegistered = false;
Martijn Coenen773609e2017-10-24 09:57:53 +0200812 bool mRegisteredForNotifications = false;
Martijn Coenen3fa15c22018-02-10 11:30:00 +0100813 bool mDoneCalled = false;
Steven Morelandcbefd352017-01-23 20:29:05 -0800814};
815
816void waitForHwService(
817 const std::string &interface, const std::string &instanceName) {
Martijn Coenen773609e2017-10-24 09:57:53 +0200818 sp<Waiter> waiter = new Waiter(interface, instanceName, defaultServiceManager1_1());
Steven Moreland0771d8a2018-05-09 13:29:14 -0700819 waiter->wait(false /* timeout */);
Martijn Coenen3fa15c22018-02-10 11:30:00 +0100820 waiter->done();
Martijn Coenen773609e2017-10-24 09:57:53 +0200821}
Steven Morelandcbefd352017-01-23 20:29:05 -0800822
Martijn Coenen773609e2017-10-24 09:57:53 +0200823// Prints relevant error/warning messages for error return values from
824// details::canCastInterface(), both transaction errors (!castReturn.isOk())
825// as well as actual cast failures (castReturn.isOk() && castReturn = false).
826// Returns 'true' if the error is non-fatal and it's useful to retry
827bool handleCastError(const Return<bool>& castReturn, const std::string& descriptor,
828 const std::string& instance) {
829 if (castReturn.isOk()) {
830 if (castReturn) {
831 details::logAlwaysFatal("Successful cast value passed into handleCastError.");
832 }
833 // This should never happen, and there's not really a point in retrying.
834 ALOGE("getService: received incompatible service (bug in hwservicemanager?) for "
835 "%s/%s.", descriptor.c_str(), instance.c_str());
836 return false;
Steven Morelandcbefd352017-01-23 20:29:05 -0800837 }
Martijn Coenen773609e2017-10-24 09:57:53 +0200838 if (castReturn.isDeadObject()) {
839 ALOGW("getService: found dead hwbinder service for %s/%s.", descriptor.c_str(),
840 instance.c_str());
841 return true;
Steven Morelandcbefd352017-01-23 20:29:05 -0800842 }
Martijn Coenen773609e2017-10-24 09:57:53 +0200843 // This can happen due to:
844 // 1) No SELinux permissions
845 // 2) Other transaction failure (no buffer space, kernel error)
846 // The first isn't recoverable, but the second is.
847 // Since we can't yet differentiate between the two, and clients depend
848 // on us not blocking in case 1), treat this as a fatal error for now.
849 ALOGW("getService: unable to call into hwbinder service for %s/%s.",
850 descriptor.c_str(), instance.c_str());
851 return false;
Steven Morelandcbefd352017-01-23 20:29:05 -0800852}
853
Martijn Coenen86c3abb2017-10-24 09:33:28 +0200854sp<::android::hidl::base::V1_0::IBase> getRawServiceInternal(const std::string& descriptor,
855 const std::string& instance,
856 bool retry, bool getStub) {
Steven Moreland84fe49e2019-08-20 17:47:26 +0000857 using Transport = IServiceManager1_0::Transport;
Martijn Coenend35678f2018-03-07 09:51:01 +0100858 sp<Waiter> waiter;
Martijn Coenen86c3abb2017-10-24 09:33:28 +0200859
Yifan Hongdeacf142018-07-10 17:33:34 -0700860 sp<IServiceManager1_1> sm;
861 Transport transport = Transport::EMPTY;
862 if (kIsRecovery) {
Yifan Hongdeacf142018-07-10 17:33:34 -0700863 transport = Transport::PASSTHROUGH;
Yifan Hongdeacf142018-07-10 17:33:34 -0700864 } else {
865 sm = defaultServiceManager1_1();
866 if (sm == nullptr) {
867 ALOGE("getService: defaultServiceManager() is null");
868 return nullptr;
869 }
870
871 Return<Transport> transportRet = sm->getTransport(descriptor, instance);
872
873 if (!transportRet.isOk()) {
874 ALOGE("getService: defaultServiceManager()->getTransport returns %s",
875 transportRet.description().c_str());
876 return nullptr;
877 }
878 transport = transportRet;
Martijn Coenen86c3abb2017-10-24 09:33:28 +0200879 }
880
Martijn Coenen86c3abb2017-10-24 09:33:28 +0200881 const bool vintfHwbinder = (transport == Transport::HWBINDER);
882 const bool vintfPassthru = (transport == Transport::PASSTHROUGH);
Steven Moreland84fe49e2019-08-20 17:47:26 +0000883 const bool trebleTestingOverride = isTrebleTestingOverride();
Steven Morelandf47cdf72021-04-07 22:13:13 +0000884 const bool allowLegacy = !kEnforceVintfManifest || (trebleTestingOverride && isDebuggable());
Steven Moreland84fe49e2019-08-20 17:47:26 +0000885 const bool vintfLegacy = (transport == Transport::EMPTY) && allowLegacy;
Martijn Coenen86c3abb2017-10-24 09:33:28 +0200886
Steven Moreland84fe49e2019-08-20 17:47:26 +0000887 if (!kEnforceVintfManifest) {
888 ALOGE("getService: Potential race detected. The VINTF manifest is not being enforced. If "
889 "a HAL server has a delay in starting and it is not in the manifest, it will not be "
890 "retrieved. Please make sure all HALs on this device are in the VINTF manifest and "
891 "enable PRODUCT_ENFORCE_VINTF_MANIFEST on this device (this is also enabled by "
892 "PRODUCT_FULL_TREBLE). PRODUCT_ENFORCE_VINTF_MANIFEST will ensure that no race "
893 "condition is possible here.");
Steven Morelandaadec3c2019-08-22 17:06:48 -0700894 sleep(1);
Steven Moreland84fe49e2019-08-20 17:47:26 +0000895 }
Martijn Coenen86c3abb2017-10-24 09:33:28 +0200896
Steven Moreland76371242018-04-19 17:11:39 -0700897 for (int tries = 0; !getStub && (vintfHwbinder || vintfLegacy); tries++) {
898 if (waiter == nullptr && tries > 0) {
Martijn Coenend35678f2018-03-07 09:51:01 +0100899 waiter = new Waiter(descriptor, instance, sm);
900 }
Steven Moreland76371242018-04-19 17:11:39 -0700901 if (waiter != nullptr) {
902 waiter->reset(); // don't reorder this -- see comments on reset()
903 }
Martijn Coenen86c3abb2017-10-24 09:33:28 +0200904 Return<sp<IBase>> ret = sm->get(descriptor, instance);
905 if (!ret.isOk()) {
906 ALOGE("getService: defaultServiceManager()->get returns %s for %s/%s.",
907 ret.description().c_str(), descriptor.c_str(), instance.c_str());
908 break;
909 }
910 sp<IBase> base = ret;
Martijn Coenen773609e2017-10-24 09:57:53 +0200911 if (base != nullptr) {
912 Return<bool> canCastRet =
913 details::canCastInterface(base.get(), descriptor.c_str(), true /* emitError */);
914
915 if (canCastRet.isOk() && canCastRet) {
Steven Moreland76371242018-04-19 17:11:39 -0700916 if (waiter != nullptr) {
917 waiter->done();
918 }
Martijn Coenen773609e2017-10-24 09:57:53 +0200919 return base; // still needs to be wrapped by Bp class.
Martijn Coenen86c3abb2017-10-24 09:33:28 +0200920 }
Martijn Coenen773609e2017-10-24 09:57:53 +0200921
922 if (!handleCastError(canCastRet, descriptor, instance)) break;
Martijn Coenen86c3abb2017-10-24 09:33:28 +0200923 }
924
Martijn Coenen773609e2017-10-24 09:57:53 +0200925 // In case of legacy or we were not asked to retry, don't.
926 if (vintfLegacy || !retry) break;
Martijn Coenen86c3abb2017-10-24 09:33:28 +0200927
Steven Moreland76371242018-04-19 17:11:39 -0700928 if (waiter != nullptr) {
929 ALOGI("getService: Trying again for %s/%s...", descriptor.c_str(), instance.c_str());
Steven Moreland0771d8a2018-05-09 13:29:14 -0700930 waiter->wait(true /* timeout */);
Steven Moreland76371242018-04-19 17:11:39 -0700931 }
Martijn Coenen86c3abb2017-10-24 09:33:28 +0200932 }
933
Martijn Coenend35678f2018-03-07 09:51:01 +0100934 if (waiter != nullptr) {
935 waiter->done();
936 }
Martijn Coenen3fa15c22018-02-10 11:30:00 +0100937
Martijn Coenen86c3abb2017-10-24 09:33:28 +0200938 if (getStub || vintfPassthru || vintfLegacy) {
Steven Moreland84fe49e2019-08-20 17:47:26 +0000939 const sp<IServiceManager1_0> pm = getPassthroughServiceManager();
Martijn Coenen86c3abb2017-10-24 09:33:28 +0200940 if (pm != nullptr) {
941 sp<IBase> base = pm->get(descriptor, instance).withDefault(nullptr);
942 if (!getStub || trebleTestingOverride) {
943 base = wrapPassthrough(base);
944 }
945 return base;
946 }
947 }
948
949 return nullptr;
950}
951
Steven Morelandbb9eb2a2018-10-11 12:10:01 -0700952status_t registerAsServiceInternal(const sp<IBase>& service, const std::string& name) {
953 if (service == nullptr) {
954 return UNEXPECTED_NULL;
955 }
956
957 sp<IServiceManager1_2> sm = defaultServiceManager1_2();
958 if (sm == nullptr) {
959 return INVALID_OPERATION;
960 }
961
Steven Moreland84fe49e2019-08-20 17:47:26 +0000962 const std::string descriptor = getDescriptor(service.get());
963
964 if (kEnforceVintfManifest && !isTrebleTestingOverride()) {
965 using Transport = IServiceManager1_0::Transport;
Steven Moreland7d1b0732021-01-21 22:47:40 +0000966 Return<Transport> transport = sm->getTransport(descriptor, name);
967
968 if (!transport.isOk()) {
969 LOG(ERROR) << "Could not get transport for " << descriptor << "/" << name << ": "
970 << transport.description();
971 return UNKNOWN_ERROR;
972 }
Steven Moreland84fe49e2019-08-20 17:47:26 +0000973
974 if (transport != Transport::HWBINDER) {
975 LOG(ERROR) << "Service " << descriptor << "/" << name
976 << " must be in VINTF manifest in order to register/get.";
977 return UNKNOWN_ERROR;
978 }
979 }
980
Steven Morelandbb9eb2a2018-10-11 12:10:01 -0700981 bool registered = false;
982 Return<void> ret = service->interfaceChain([&](const auto& chain) {
983 registered = sm->addWithChain(name.c_str(), service, chain).withDefault(false);
984 });
985
986 if (!ret.isOk()) {
987 LOG(ERROR) << "Could not retrieve interface chain: " << ret.description();
988 }
989
990 if (registered) {
Steven Moreland84fe49e2019-08-20 17:47:26 +0000991 onRegistrationImpl(descriptor, name);
Steven Morelandbb9eb2a2018-10-11 12:10:01 -0700992 }
993
994 return registered ? OK : UNKNOWN_ERROR;
995}
996
Bernhard Rosenkränzer0c28fd22018-06-04 16:01:47 +0200997} // namespace details
Steven Morelandcbefd352017-01-23 20:29:05 -0800998
Bernhard Rosenkränzer0c28fd22018-06-04 16:01:47 +0200999} // namespace hardware
1000} // namespace android