blob: cbe7ac60da6ed5d24d09794a2c7a1b472ba6e6cd [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;
Steven Moreland5d5ef7f2016-10-20 19:19:55 -070062
63namespace android {
64namespace hardware {
65
Yifan Hongdeacf142018-07-10 17:33:34 -070066#if defined(__ANDROID_RECOVERY__)
67static constexpr bool kIsRecovery = true;
68#else
69static constexpr bool kIsRecovery = false;
70#endif
71
Steven Morelandbb9eb2a2018-10-11 12:10:01 -070072static void waitForHwServiceManager() {
Steven Moreland2056cf32019-09-17 17:41:02 -070073 // TODO(b/31559095): need bionic host so that we can use 'prop_info' returned
74 // from WaitForProperty
75#ifdef __ANDROID__
76 static const char* kHwServicemanagerReadyProperty = "hwservicemanager.ready";
77
Steven Morelandc1cee2c2017-03-24 16:23:11 +000078 using std::literals::chrono_literals::operator""s;
79
Steven Moreland2056cf32019-09-17 17:41:02 -070080 using android::base::WaitForProperty;
Steven Morelandc1cee2c2017-03-24 16:23:11 +000081 while (!WaitForProperty(kHwServicemanagerReadyProperty, "true", 1s)) {
82 LOG(WARNING) << "Waited for hwservicemanager.ready for a second, waiting another...";
83 }
Steven Moreland2056cf32019-09-17 17:41:02 -070084#endif // __ANDROID__
Steven Morelandc1cee2c2017-03-24 16:23:11 +000085}
86
Steven Morelandbb9eb2a2018-10-11 12:10:01 -070087static std::string binaryName() {
Steven Moreland405d7612017-04-07 20:31:22 -070088 std::ifstream ifs("/proc/self/cmdline");
89 std::string cmdline;
Steven Morelandd682c0e2020-07-21 17:47:32 +000090 if (!ifs) {
Steven Moreland405d7612017-04-07 20:31:22 -070091 return "";
92 }
93 ifs >> cmdline;
94
Chih-Hung Hsieh41649d52017-08-03 14:27:21 -070095 size_t idx = cmdline.rfind('/');
Steven Moreland405d7612017-04-07 20:31:22 -070096 if (idx != std::string::npos) {
97 cmdline = cmdline.substr(idx + 1);
98 }
99
100 return cmdline;
101}
102
Steven Morelandbb9eb2a2018-10-11 12:10:01 -0700103static std::string packageWithoutVersion(const std::string& packageAndVersion) {
Steven Moreland7d09a402018-04-06 10:44:05 -0700104 size_t at = packageAndVersion.find('@');
105 if (at == std::string::npos) return packageAndVersion;
106 return packageAndVersion.substr(0, at);
107}
108
Steven Morelandd682c0e2020-07-21 17:47:32 +0000109__attribute__((noinline)) static void tryShortenProcessName(const std::string& descriptor) {
Steven Moreland7d09a402018-04-06 10:44:05 -0700110 const static std::string kTasks = "/proc/self/task/";
111
112 // make sure that this binary name is in the same package
Steven Moreland405d7612017-04-07 20:31:22 -0700113 std::string processName = binaryName();
114
Steven Moreland7d09a402018-04-06 10:44:05 -0700115 // e.x. android.hardware.foo is this package
Steven Moreland8092aa02018-10-12 15:54:56 -0700116 if (!base::StartsWith(packageWithoutVersion(processName), packageWithoutVersion(descriptor))) {
Steven Moreland405d7612017-04-07 20:31:22 -0700117 return;
118 }
119
Steven Morelandbb9eb2a2018-10-11 12:10:01 -0700120 // e.x. android.hardware.module.foo@1.2::IFoo -> foo@1.2
121 size_t lastDot = descriptor.rfind('.');
Steven Moreland7d09a402018-04-06 10:44:05 -0700122 if (lastDot == std::string::npos) return;
Steven Morelandbb9eb2a2018-10-11 12:10:01 -0700123 size_t secondDot = descriptor.rfind('.', lastDot - 1);
Steven Moreland7d09a402018-04-06 10:44:05 -0700124 if (secondDot == std::string::npos) return;
Steven Moreland405d7612017-04-07 20:31:22 -0700125
Steven Moreland7d09a402018-04-06 10:44:05 -0700126 std::string newName = processName.substr(secondDot + 1, std::string::npos);
127 ALOGI("Removing namespace from process name %s to %s.", processName.c_str(), newName.c_str());
128
129 std::unique_ptr<DIR, decltype(&closedir)> dir(opendir(kTasks.c_str()), closedir);
130 if (dir == nullptr) return;
131
132 dirent* dp;
133 while ((dp = readdir(dir.get())) != nullptr) {
134 if (dp->d_type != DT_DIR) continue;
135 if (dp->d_name[0] == '.') continue;
136
137 std::fstream fs(kTasks + dp->d_name + "/comm");
Steven Morelandd682c0e2020-07-21 17:47:32 +0000138 if (!fs) {
Steven Moreland7d09a402018-04-06 10:44:05 -0700139 ALOGI("Could not rename process, failed read comm for %s.", dp->d_name);
140 continue;
141 }
142
143 std::string oldComm;
Steven Morelandd682c0e2020-07-21 17:47:32 +0000144 if (!(fs >> oldComm)) continue;
Steven Moreland7d09a402018-04-06 10:44:05 -0700145
146 // don't rename if it already has an explicit name
Steven Moreland8092aa02018-10-12 15:54:56 -0700147 if (base::StartsWith(descriptor, oldComm)) {
Steven Morelandd682c0e2020-07-21 17:47:32 +0000148 if (!fs.seekg(0, fs.beg)) continue;
Steven Moreland7d09a402018-04-06 10:44:05 -0700149 fs << newName;
150 }
Steven Moreland405d7612017-04-07 20:31:22 -0700151 }
Steven Moreland405d7612017-04-07 20:31:22 -0700152}
153
154namespace details {
155
Steven Morelandb939e7e2020-07-08 01:04:23 +0000156#ifdef ENFORCE_VINTF_MANIFEST
157static constexpr bool kEnforceVintfManifest = true;
158#else
159static constexpr bool kEnforceVintfManifest = false;
160#endif
161
162#ifdef LIBHIDL_TARGET_DEBUGGABLE
163static constexpr bool kDebuggable = true;
164#else
165static constexpr bool kDebuggable = false;
166#endif
167
Steven Moreland206d2812020-07-08 21:21:48 +0000168static bool* getTrebleTestingOverridePtr() {
169 static bool gTrebleTestingOverride = false;
170 return &gTrebleTestingOverride;
171}
Steven Morelandb939e7e2020-07-08 01:04:23 +0000172
173void setTrebleTestingOverride(bool testingOverride) {
Steven Moreland206d2812020-07-08 21:21:48 +0000174 *getTrebleTestingOverridePtr() = testingOverride;
Steven Morelandb939e7e2020-07-08 01:04:23 +0000175}
176
177static inline bool isTrebleTestingOverride() {
178 if (kEnforceVintfManifest && !kDebuggable) {
179 // 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
Peter Kalauskas64ffced2018-09-05 16:41:08 -0700186/*
187 * Returns the age of the current process by reading /proc/self/stat and comparing starttime to the
188 * current time. This is useful for measuring how long it took a HAL to register itself.
189 */
Steven Morelandd682c0e2020-07-21 17:47:32 +0000190__attribute__((noinline)) static long getProcessAgeMs() {
Peter Kalauskas64ffced2018-09-05 16:41:08 -0700191 constexpr const int PROCFS_STAT_STARTTIME_INDEX = 21;
192 std::string content;
193 android::base::ReadFileToString("/proc/self/stat", &content, false);
194 auto stats = android::base::Split(content, " ");
195 if (stats.size() <= PROCFS_STAT_STARTTIME_INDEX) {
196 LOG(INFO) << "Could not read starttime from /proc/self/stat";
197 return -1;
198 }
199 const std::string& startTimeString = stats[PROCFS_STAT_STARTTIME_INDEX];
200 static const int64_t ticksPerSecond = sysconf(_SC_CLK_TCK);
201 const int64_t uptime = android::uptimeMillis();
202
203 unsigned long long startTimeInClockTicks = 0;
204 if (android::base::ParseUint(startTimeString, &startTimeInClockTicks)) {
205 long startTimeMs = 1000ULL * startTimeInClockTicks / ticksPerSecond;
206 return uptime - startTimeMs;
207 }
208 return -1;
209}
210
Steven Morelandbb9eb2a2018-10-11 12:10:01 -0700211static void onRegistrationImpl(const std::string& descriptor, const std::string& instanceName) {
Peter Kalauskas64ffced2018-09-05 16:41:08 -0700212 long halStartDelay = getProcessAgeMs();
213 if (halStartDelay >= 0) {
214 // The "start delay" printed here is an estimate of how long it took the HAL to go from
215 // process creation to registering itself as a HAL. Actual start time could be longer
216 // because the process might not have joined the threadpool yet, so it might not be ready to
217 // process transactions.
Steven Morelandbb9eb2a2018-10-11 12:10:01 -0700218 LOG(INFO) << "Registered " << descriptor << "/" << instanceName << " (start delay of "
Peter Kalauskas64ffced2018-09-05 16:41:08 -0700219 << halStartDelay << "ms)";
220 }
Steven Morelandbb9eb2a2018-10-11 12:10:01 -0700221
222 tryShortenProcessName(descriptor);
223}
224
Steven Moreland911852c2020-05-11 10:51:26 -0700225// only used by prebuilts - should be able to remove
Steven Morelandbb9eb2a2018-10-11 12:10:01 -0700226void onRegistration(const std::string& packageName, const std::string& interfaceName,
227 const std::string& instanceName) {
228 return onRegistrationImpl(packageName + "::" + interfaceName, instanceName);
Steven Moreland405d7612017-04-07 20:31:22 -0700229}
230
231} // details
232
Steven Moreland2a2678e2017-07-21 18:07:38 -0700233sp<IServiceManager1_0> defaultServiceManager() {
Steven Moreland89909692018-05-30 14:11:19 -0700234 return defaultServiceManager1_2();
Steven Moreland2a2678e2017-07-21 18:07:38 -0700235}
236sp<IServiceManager1_1> defaultServiceManager1_1() {
Steven Moreland89909692018-05-30 14:11:19 -0700237 return defaultServiceManager1_2();
238}
239sp<IServiceManager1_2> defaultServiceManager1_2() {
240 using android::hidl::manager::V1_2::BnHwServiceManager;
241 using android::hidl::manager::V1_2::BpHwServiceManager;
242
Steven Moreland10d55632020-04-21 14:10:53 -0700243 static std::mutex& gDefaultServiceManagerLock = *new std::mutex;
244 static sp<IServiceManager1_2>& gDefaultServiceManager = *new sp<IServiceManager1_2>;
Steven Moreland89909692018-05-30 14:11:19 -0700245
Steven Moreland5d5ef7f2016-10-20 19:19:55 -0700246 {
Steven Moreland89909692018-05-30 14:11:19 -0700247 std::lock_guard<std::mutex> _l(gDefaultServiceManagerLock);
248 if (gDefaultServiceManager != nullptr) {
249 return gDefaultServiceManager;
Yifan Hong8fb656b2017-03-16 14:30:40 -0700250 }
Steven Moreland405d7612017-04-07 20:31:22 -0700251
Yifan Hong8fb656b2017-03-16 14:30:40 -0700252 if (access("/dev/hwbinder", F_OK|R_OK|W_OK) != 0) {
253 // HwBinder not available on this device or not accessible to
254 // this process.
255 return nullptr;
256 }
Steven Morelandc1cee2c2017-03-24 16:23:11 +0000257
258 waitForHwServiceManager();
259
Steven Moreland89909692018-05-30 14:11:19 -0700260 while (gDefaultServiceManager == nullptr) {
261 gDefaultServiceManager =
262 fromBinder<IServiceManager1_2, BpHwServiceManager, BnHwServiceManager>(
263 ProcessState::self()->getContextObject(nullptr));
264 if (gDefaultServiceManager == nullptr) {
Steven Morelandc1cee2c2017-03-24 16:23:11 +0000265 LOG(ERROR) << "Waited for hwservicemanager, but got nullptr.";
Steven Moreland5d5ef7f2016-10-20 19:19:55 -0700266 sleep(1);
Yifan Hong8fb656b2017-03-16 14:30:40 -0700267 }
Steven Moreland5d5ef7f2016-10-20 19:19:55 -0700268 }
269 }
270
Steven Moreland89909692018-05-30 14:11:19 -0700271 return gDefaultServiceManager;
Steven Moreland5d5ef7f2016-10-20 19:19:55 -0700272}
273
Elliott Hughes19306142018-10-26 13:13:04 -0700274static std::vector<std::string> findFiles(const std::string& path, const std::string& prefix,
275 const std::string& suffix) {
Steven Moreland0091c092017-01-20 23:15:18 +0000276 std::unique_ptr<DIR, decltype(&closedir)> dir(opendir(path.c_str()), closedir);
277 if (!dir) return {};
278
279 std::vector<std::string> results{};
280
281 dirent* dp;
282 while ((dp = readdir(dir.get())) != nullptr) {
283 std::string name = dp->d_name;
284
Steven Moreland8092aa02018-10-12 15:54:56 -0700285 if (base::StartsWith(name, prefix) && base::EndsWith(name, suffix)) {
Steven Moreland0091c092017-01-20 23:15:18 +0000286 results.push_back(name);
287 }
288 }
289
290 return results;
291}
292
Steven Morelandccc3a972020-02-26 16:38:31 -0800293static bool matchPackageName(const std::string& lib, std::string* matchedName,
294 std::string* implName) {
295#define RE_COMPONENT "[a-zA-Z_][a-zA-Z_0-9]*"
296#define RE_PATH RE_COMPONENT "(?:[.]" RE_COMPONENT ")*"
297 static const std::regex gLibraryFileNamePattern("(" RE_PATH "@[0-9]+[.][0-9]+)-impl(.*?).so");
298#undef RE_PATH
299#undef RE_COMPONENT
300
Yifan Hong9a22d1d2017-01-25 14:19:26 -0800301 std::smatch match;
302 if (std::regex_match(lib, match, gLibraryFileNamePattern)) {
303 *matchedName = match.str(1) + "::I*";
Yifan Hongbd0d8f72017-05-24 19:43:51 -0700304 *implName = match.str(2);
Yifan Hong9a22d1d2017-01-25 14:19:26 -0800305 return true;
306 }
307 return false;
308}
309
Yifan Hong7f49f592017-02-03 15:11:44 -0800310static void registerReference(const hidl_string &interfaceName, const hidl_string &instanceName) {
Yifan Hongdeacf142018-07-10 17:33:34 -0700311 if (kIsRecovery) {
312 // No hwservicemanager in recovery.
313 return;
314 }
315
Steven Moreland2a2678e2017-07-21 18:07:38 -0700316 sp<IServiceManager1_0> binderizedManager = defaultServiceManager();
Yifan Hong7f49f592017-02-03 15:11:44 -0800317 if (binderizedManager == nullptr) {
318 LOG(WARNING) << "Could not registerReference for "
319 << interfaceName << "/" << instanceName
320 << ": null binderized manager.";
321 return;
322 }
Martijn Coenenaf4aba52017-04-27 09:41:13 -0700323 auto ret = binderizedManager->registerPassthroughClient(interfaceName, instanceName);
Yifan Hong7f49f592017-02-03 15:11:44 -0800324 if (!ret.isOk()) {
325 LOG(WARNING) << "Could not registerReference for "
326 << interfaceName << "/" << instanceName
327 << ": " << ret.description();
Steven Moreland0aeaa782017-03-22 08:11:07 -0700328 return;
Yifan Hong7f49f592017-02-03 15:11:44 -0800329 }
Steven Morelande681aa52017-02-15 16:22:37 -0800330 LOG(VERBOSE) << "Successfully registerReference for "
331 << interfaceName << "/" << instanceName;
Yifan Hong7f49f592017-02-03 15:11:44 -0800332}
333
Yifan Hongbd0d8f72017-05-24 19:43:51 -0700334using InstanceDebugInfo = hidl::manager::V1_0::IServiceManager::InstanceDebugInfo;
335static inline void fetchPidsForPassthroughLibraries(
336 std::map<std::string, InstanceDebugInfo>* infos) {
337 static const std::string proc = "/proc/";
338
339 std::map<std::string, std::set<pid_t>> pids;
340 std::unique_ptr<DIR, decltype(&closedir)> dir(opendir(proc.c_str()), closedir);
341 if (!dir) return;
342 dirent* dp;
343 while ((dp = readdir(dir.get())) != nullptr) {
Stephen Hinesfd9ecee2017-09-27 18:52:52 -0700344 pid_t pid = strtoll(dp->d_name, nullptr, 0);
Yifan Hongbd0d8f72017-05-24 19:43:51 -0700345 if (pid == 0) continue;
346 std::string mapsPath = proc + dp->d_name + "/maps";
347 std::ifstream ifs{mapsPath};
348 if (!ifs.is_open()) continue;
349
350 for (std::string line; std::getline(ifs, line);) {
351 // The last token of line should look like
352 // vendor/lib64/hw/android.hardware.foo@1.0-impl-extra.so
353 // Use some simple filters to ignore bad lines before extracting libFileName
354 // and checking the key in info to make parsing faster.
355 if (line.back() != 'o') continue;
356 if (line.rfind('@') == std::string::npos) continue;
357
358 auto spacePos = line.rfind(' ');
359 if (spacePos == std::string::npos) continue;
360 auto libFileName = line.substr(spacePos + 1);
361 auto it = infos->find(libFileName);
362 if (it == infos->end()) continue;
363 pids[libFileName].insert(pid);
364 }
365 }
366 for (auto& pair : *infos) {
367 pair.second.clientPids =
368 std::vector<pid_t>{pids[pair.first].begin(), pids[pair.first].end()};
369 }
370}
371
Steven Moreland2a2678e2017-07-21 18:07:38 -0700372struct PassthroughServiceManager : IServiceManager1_1 {
Chih-Hung Hsieh41649d52017-08-03 14:27:21 -0700373 static void openLibs(
374 const std::string& fqName,
375 const std::function<bool /* continue */ (void* /* handle */, const std::string& /* lib */,
376 const std::string& /* sym */)>& eachLib) {
Steven Moreland819c05d2017-04-06 17:24:22 -0700377 //fqName looks like android.hardware.foo@1.0::IFoo
Steven Moreland519306f2017-06-06 17:14:12 -0700378 size_t idx = fqName.find("::");
Steven Moreland819c05d2017-04-06 17:24:22 -0700379
380 if (idx == std::string::npos ||
Steven Moreland519306f2017-06-06 17:14:12 -0700381 idx + strlen("::") + 1 >= fqName.size()) {
Steven Moreland337e6b62017-01-18 17:25:13 -0800382 LOG(ERROR) << "Invalid interface name passthrough lookup: " << fqName;
Steven Moreland519306f2017-06-06 17:14:12 -0700383 return;
Steven Moreland337e6b62017-01-18 17:25:13 -0800384 }
385
Steven Moreland519306f2017-06-06 17:14:12 -0700386 std::string packageAndVersion = fqName.substr(0, idx);
387 std::string ifaceName = fqName.substr(idx + strlen("::"));
Steven Moreland819c05d2017-04-06 17:24:22 -0700388
389 const std::string prefix = packageAndVersion + "-impl";
390 const std::string sym = "HIDL_FETCH_" + ifaceName;
Steven Moreland348802d2017-02-23 12:48:55 -0800391
Steven Moreland77f4c852017-10-12 11:05:53 -0700392 constexpr int dlMode = RTLD_LAZY;
393 void* handle = nullptr;
Steven Moreland337e6b62017-01-18 17:25:13 -0800394
Steven Morelanda29905c2017-03-01 10:42:35 -0800395 dlerror(); // clear
396
Jooyung Han544d9b52020-07-16 19:49:39 +0900397 static std::string halLibPathVndkSp = details::getVndkSpHwPath();
Justin Yun6a323ed2018-10-18 18:41:56 +0900398 std::vector<std::string> paths = {
399 HAL_LIBRARY_PATH_ODM, HAL_LIBRARY_PATH_VENDOR, halLibPathVndkSp,
400#ifndef __ANDROID_VNDK__
401 HAL_LIBRARY_PATH_SYSTEM,
402#endif
403 };
Steven Moreland77f4c852017-10-12 11:05:53 -0700404
Steven Morelandb939e7e2020-07-08 01:04:23 +0000405 if (details::isTrebleTestingOverride()) {
Steven Moreland77f4c852017-10-12 11:05:53 -0700406 // Load HAL implementations that are statically linked
407 handle = dlopen(nullptr, dlMode);
408 if (handle == nullptr) {
409 const char* error = dlerror();
410 LOG(ERROR) << "Failed to dlopen self: "
411 << (error == nullptr ? "unknown error" : error);
412 } else if (!eachLib(handle, "SELF", sym)) {
413 return;
414 }
Steven Morelandf7dea692017-07-14 12:49:28 -0700415 }
Steven Moreland77f4c852017-10-12 11:05:53 -0700416
Steven Morelandf7dea692017-07-14 12:49:28 -0700417 for (const std::string& path : paths) {
Elliott Hughes19306142018-10-26 13:13:04 -0700418 std::vector<std::string> libs = findFiles(path, prefix, ".so");
Steven Moreland0091c092017-01-20 23:15:18 +0000419
Steven Moreland0091c092017-01-20 23:15:18 +0000420 for (const std::string &lib : libs) {
Steven Moreland348802d2017-02-23 12:48:55 -0800421 const std::string fullPath = path + lib;
422
Yifan Hongdeacf142018-07-10 17:33:34 -0700423 if (kIsRecovery || path == HAL_LIBRARY_PATH_SYSTEM) {
Jiyong Park3ab546c2017-04-06 20:28:07 +0900424 handle = dlopen(fullPath.c_str(), dlMode);
Steven Moreland65c00cb2017-10-12 11:35:22 -0700425 } else {
Steven Moreland2056cf32019-09-17 17:41:02 -0700426#if !defined(__ANDROID_RECOVERY__) && defined(__ANDROID__)
Steven Moreland65c00cb2017-10-12 11:35:22 -0700427 handle = android_load_sphal_library(fullPath.c_str(), dlMode);
Jerry Zhang86ae9992018-05-30 17:11:09 -0700428#endif
Jiyong Park3ab546c2017-04-06 20:28:07 +0900429 }
430
Steven Moreland348802d2017-02-23 12:48:55 -0800431 if (handle == nullptr) {
432 const char* error = dlerror();
433 LOG(ERROR) << "Failed to dlopen " << lib << ": "
434 << (error == nullptr ? "unknown error" : error);
435 continue;
Steven Moreland0091c092017-01-20 23:15:18 +0000436 }
Steven Moreland348802d2017-02-23 12:48:55 -0800437
Steven Moreland519306f2017-06-06 17:14:12 -0700438 if (!eachLib(handle, lib, sym)) {
439 return;
Steven Moreland348802d2017-02-23 12:48:55 -0800440 }
Steven Moreland337e6b62017-01-18 17:25:13 -0800441 }
442 }
Steven Moreland519306f2017-06-06 17:14:12 -0700443 }
Steven Moreland337e6b62017-01-18 17:25:13 -0800444
Steven Moreland519306f2017-06-06 17:14:12 -0700445 Return<sp<IBase>> get(const hidl_string& fqName,
446 const hidl_string& name) override {
447 sp<IBase> ret = nullptr;
448
449 openLibs(fqName, [&](void* handle, const std::string &lib, const std::string &sym) {
450 IBase* (*generator)(const char* name);
451 *(void **)(&generator) = dlsym(handle, sym.c_str());
452 if(!generator) {
453 const char* error = dlerror();
454 LOG(ERROR) << "Passthrough lookup opened " << lib
455 << " but could not find symbol " << sym << ": "
456 << (error == nullptr ? "unknown error" : error);
457 dlclose(handle);
458 return true;
459 }
460
461 ret = (*generator)(name.c_str());
462
463 if (ret == nullptr) {
464 dlclose(handle);
465 return true; // this module doesn't provide this instance name
466 }
467
Steven Morelande69e8d32018-03-14 10:55:42 -0700468 // Actual fqname might be a subclass.
469 // This assumption is tested in vts_treble_vintf_test
470 using ::android::hardware::details::getDescriptor;
471 std::string actualFqName = getDescriptor(ret.get());
472 CHECK(actualFqName.size() > 0);
473 registerReference(actualFqName, name);
Steven Moreland519306f2017-06-06 17:14:12 -0700474 return false;
475 });
476
477 return ret;
Steven Moreland337e6b62017-01-18 17:25:13 -0800478 }
479
Martijn Coenen67a02492017-03-06 13:04:48 +0100480 Return<bool> add(const hidl_string& /* name */,
Steven Moreland337e6b62017-01-18 17:25:13 -0800481 const sp<IBase>& /* service */) override {
482 LOG(FATAL) << "Cannot register services with passthrough service manager.";
483 return false;
484 }
485
Steven Morelandc601c5f2017-04-06 09:26:07 -0700486 Return<Transport> getTransport(const hidl_string& /* fqName */,
487 const hidl_string& /* name */) {
488 LOG(FATAL) << "Cannot getTransport with passthrough service manager.";
489 return Transport::EMPTY;
490 }
491
Yifan Hong705e5da2017-03-02 16:59:39 -0800492 Return<void> list(list_cb /* _hidl_cb */) override {
493 LOG(FATAL) << "Cannot list services with passthrough service manager.";
Steven Moreland337e6b62017-01-18 17:25:13 -0800494 return Void();
495 }
496 Return<void> listByInterface(const hidl_string& /* fqInstanceName */,
497 listByInterface_cb /* _hidl_cb */) override {
498 // TODO: add this functionality
499 LOG(FATAL) << "Cannot list services with passthrough service manager.";
500 return Void();
501 }
502
503 Return<bool> registerForNotifications(const hidl_string& /* fqName */,
504 const hidl_string& /* name */,
505 const sp<IServiceNotification>& /* callback */) override {
506 // This makes no sense.
507 LOG(FATAL) << "Cannot register for notifications with passthrough service manager.";
508 return false;
509 }
510
Yifan Hong705e5da2017-03-02 16:59:39 -0800511 Return<void> debugDump(debugDump_cb _hidl_cb) override {
512 using Arch = ::android::hidl::base::V1_0::DebugInfo::Architecture;
Yifan Hongbd0d8f72017-05-24 19:43:51 -0700513 using std::literals::string_literals::operator""s;
Jooyung Han544d9b52020-07-16 19:49:39 +0900514 static std::string halLibPathVndkSp64 = details::getVndkSpHwPath("lib64");
515 static std::string halLibPathVndkSp32 = details::getVndkSpHwPath("lib");
Jiyong Park56eb1492017-08-04 16:16:13 +0900516 static std::vector<std::pair<Arch, std::vector<const char*>>> sAllPaths{
517 {Arch::IS_64BIT,
Justin Yun6a323ed2018-10-18 18:41:56 +0900518 {
519 HAL_LIBRARY_PATH_ODM_64BIT, HAL_LIBRARY_PATH_VENDOR_64BIT,
520 halLibPathVndkSp64.c_str(),
521#ifndef __ANDROID_VNDK__
522 HAL_LIBRARY_PATH_SYSTEM_64BIT,
523#endif
524 }},
Jiyong Park56eb1492017-08-04 16:16:13 +0900525 {Arch::IS_32BIT,
Justin Yun6a323ed2018-10-18 18:41:56 +0900526 {
527 HAL_LIBRARY_PATH_ODM_32BIT, HAL_LIBRARY_PATH_VENDOR_32BIT,
528 halLibPathVndkSp32.c_str(),
529#ifndef __ANDROID_VNDK__
530 HAL_LIBRARY_PATH_SYSTEM_32BIT,
531#endif
532 }}};
Yifan Hongbd0d8f72017-05-24 19:43:51 -0700533 std::map<std::string, InstanceDebugInfo> map;
Yifan Hong705e5da2017-03-02 16:59:39 -0800534 for (const auto &pair : sAllPaths) {
535 Arch arch = pair.first;
536 for (const auto &path : pair.second) {
Elliott Hughes19306142018-10-26 13:13:04 -0700537 std::vector<std::string> libs = findFiles(path, "", ".so");
Yifan Hong705e5da2017-03-02 16:59:39 -0800538 for (const std::string &lib : libs) {
539 std::string matchedName;
Yifan Hongbd0d8f72017-05-24 19:43:51 -0700540 std::string implName;
541 if (matchPackageName(lib, &matchedName, &implName)) {
542 std::string instanceName{"* ("s + path + ")"s};
543 if (!implName.empty()) instanceName += " ("s + implName + ")"s;
544 map.emplace(path + lib, InstanceDebugInfo{.interfaceName = matchedName,
545 .instanceName = instanceName,
546 .clientPids = {},
547 .arch = arch});
Yifan Hong705e5da2017-03-02 16:59:39 -0800548 }
549 }
550 }
551 }
Yifan Hongbd0d8f72017-05-24 19:43:51 -0700552 fetchPidsForPassthroughLibraries(&map);
553 hidl_vec<InstanceDebugInfo> vec;
554 vec.resize(map.size());
555 size_t idx = 0;
556 for (auto&& pair : map) {
557 vec[idx++] = std::move(pair.second);
558 }
Yifan Hong705e5da2017-03-02 16:59:39 -0800559 _hidl_cb(vec);
Yifan Hong7f49f592017-02-03 15:11:44 -0800560 return Void();
561 }
562
Martijn Coenenaf4aba52017-04-27 09:41:13 -0700563 Return<void> registerPassthroughClient(const hidl_string &, const hidl_string &) override {
Yifan Hong7f49f592017-02-03 15:11:44 -0800564 // This makes no sense.
565 LOG(FATAL) << "Cannot call registerPassthroughClient on passthrough service manager. "
566 << "Call it on defaultServiceManager() instead.";
Yifan Hong9a22d1d2017-01-25 14:19:26 -0800567 return Void();
568 }
569
Steven Moreland2a2678e2017-07-21 18:07:38 -0700570 Return<bool> unregisterForNotifications(const hidl_string& /* fqName */,
571 const hidl_string& /* name */,
572 const sp<IServiceNotification>& /* callback */) override {
573 // This makes no sense.
574 LOG(FATAL) << "Cannot unregister for notifications with passthrough service manager.";
575 return false;
576 }
577
Steven Moreland337e6b62017-01-18 17:25:13 -0800578};
579
Steven Moreland2a2678e2017-07-21 18:07:38 -0700580sp<IServiceManager1_0> getPassthroughServiceManager() {
581 return getPassthroughServiceManager1_1();
582}
583sp<IServiceManager1_1> getPassthroughServiceManager1_1() {
Steven Moreland337e6b62017-01-18 17:25:13 -0800584 static sp<PassthroughServiceManager> manager(new PassthroughServiceManager());
585 return manager;
586}
587
Steven Moreland92b247c2019-08-21 14:53:11 -0700588std::vector<std::string> getAllHalInstanceNames(const std::string& descriptor) {
589 std::vector<std::string> ret;
590 auto sm = defaultServiceManager1_2();
591 sm->listManifestByInterface(descriptor, [&](const auto& instances) {
592 ret.reserve(instances.size());
593 for (const auto& i : instances) {
594 ret.push_back(i);
595 }
596 });
597 return ret;
598}
599
Steven Morelandcbefd352017-01-23 20:29:05 -0800600namespace details {
601
Steven Moreland519306f2017-06-06 17:14:12 -0700602void preloadPassthroughService(const std::string &descriptor) {
603 PassthroughServiceManager::openLibs(descriptor,
604 [&](void* /* handle */, const std::string& /* lib */, const std::string& /* sym */) {
605 // do nothing
606 return true; // open all libs
607 });
608}
609
Steven Morelandcbefd352017-01-23 20:29:05 -0800610struct Waiter : IServiceNotification {
Martijn Coenen773609e2017-10-24 09:57:53 +0200611 Waiter(const std::string& interface, const std::string& instanceName,
612 const sp<IServiceManager1_1>& sm) : mInterfaceName(interface),
613 mInstanceName(instanceName), mSm(sm) {
Martijn Coenene6cdfd32017-11-13 14:03:05 +0100614 }
615
616 void onFirstRef() override {
Martijn Coenenb88ae7f2017-10-24 11:45:41 +0200617 // If this process only has one binder thread, and we're calling wait() from
618 // that thread, it will block forever because we hung up the one and only
619 // binder thread on a condition variable that can only be notified by an
620 // incoming binder call.
Tobias Lindskog88e886f2018-01-05 10:32:43 +0100621 if (IPCThreadState::self()->isOnlyBinderThread()) {
Martijn Coenenb88ae7f2017-10-24 11:45:41 +0200622 LOG(WARNING) << "Can't efficiently wait for " << mInterfaceName << "/"
623 << mInstanceName << ", because we are called from "
624 << "the only binder thread in this process.";
625 return;
626 }
627
Martijn Coenene6cdfd32017-11-13 14:03:05 +0100628 Return<bool> ret = mSm->registerForNotifications(mInterfaceName, mInstanceName, this);
Martijn Coenen773609e2017-10-24 09:57:53 +0200629
630 if (!ret.isOk()) {
631 LOG(ERROR) << "Transport error, " << ret.description()
Martijn Coenene6cdfd32017-11-13 14:03:05 +0100632 << ", during notification registration for " << mInterfaceName << "/"
633 << mInstanceName << ".";
Martijn Coenen773609e2017-10-24 09:57:53 +0200634 return;
635 }
636
637 if (!ret) {
Martijn Coenene6cdfd32017-11-13 14:03:05 +0100638 LOG(ERROR) << "Could not register for notifications for " << mInterfaceName << "/"
639 << mInstanceName << ".";
Martijn Coenen773609e2017-10-24 09:57:53 +0200640 return;
641 }
642
643 mRegisteredForNotifications = true;
644 }
645
646 ~Waiter() {
Martijn Coenen3fa15c22018-02-10 11:30:00 +0100647 if (!mDoneCalled) {
648 LOG(FATAL)
649 << "Waiter still registered for notifications, call done() before dropping ref!";
Martijn Coenen773609e2017-10-24 09:57:53 +0200650 }
651 }
652
Steven Morelandcbefd352017-01-23 20:29:05 -0800653 Return<void> onRegistration(const hidl_string& /* fqName */,
654 const hidl_string& /* name */,
655 bool /* preexisting */) override {
656 std::unique_lock<std::mutex> lock(mMutex);
657 if (mRegistered) {
658 return Void();
659 }
660 mRegistered = true;
661 lock.unlock();
662
663 mCondition.notify_one();
664 return Void();
665 }
666
Steven Moreland0771d8a2018-05-09 13:29:14 -0700667 void wait(bool timeout) {
Steven Moreland49605102017-03-28 09:33:06 -0700668 using std::literals::chrono_literals::operator""s;
669
Martijn Coenen773609e2017-10-24 09:57:53 +0200670 if (!mRegisteredForNotifications) {
671 // As an alternative, just sleep for a second and return
672 LOG(WARNING) << "Waiting one second for " << mInterfaceName << "/" << mInstanceName;
673 sleep(1);
674 return;
675 }
676
Steven Morelandcbefd352017-01-23 20:29:05 -0800677 std::unique_lock<std::mutex> lock(mMutex);
Steven Moreland0771d8a2018-05-09 13:29:14 -0700678 do {
Steven Moreland49605102017-03-28 09:33:06 -0700679 mCondition.wait_for(lock, 1s, [this]{
680 return mRegistered;
681 });
682
683 if (mRegistered) {
684 break;
685 }
686
Steven Moreland0771d8a2018-05-09 13:29:14 -0700687 LOG(WARNING) << "Waited one second for " << mInterfaceName << "/" << mInstanceName;
688 } while (!timeout);
Steven Morelandcbefd352017-01-23 20:29:05 -0800689 }
690
Martijn Coenen773609e2017-10-24 09:57:53 +0200691 // Be careful when using this; after calling reset(), you must always try to retrieve
692 // the corresponding service before blocking on the waiter; otherwise, you might run
693 // into a race-condition where the service has just (re-)registered, you clear the state
694 // here, and subsequently calling waiter->wait() will block forever.
695 void reset() {
696 std::unique_lock<std::mutex> lock(mMutex);
697 mRegistered = false;
698 }
Martijn Coenen3fa15c22018-02-10 11:30:00 +0100699
700 // done() must be called before dropping the last strong ref to the Waiter, to make
701 // sure we can properly unregister with hwservicemanager.
702 void done() {
703 if (mRegisteredForNotifications) {
704 if (!mSm->unregisterForNotifications(mInterfaceName, mInstanceName, this)
705 .withDefault(false)) {
706 LOG(ERROR) << "Could not unregister service notification for " << mInterfaceName
707 << "/" << mInstanceName << ".";
708 } else {
709 mRegisteredForNotifications = false;
710 }
711 }
712 mDoneCalled = true;
713 }
714
715 private:
Martijn Coenen773609e2017-10-24 09:57:53 +0200716 const std::string mInterfaceName;
717 const std::string mInstanceName;
Steven Morelandad1f9922018-10-02 19:37:05 -0700718 sp<IServiceManager1_1> mSm;
Steven Morelandcbefd352017-01-23 20:29:05 -0800719 std::mutex mMutex;
720 std::condition_variable mCondition;
721 bool mRegistered = false;
Martijn Coenen773609e2017-10-24 09:57:53 +0200722 bool mRegisteredForNotifications = false;
Martijn Coenen3fa15c22018-02-10 11:30:00 +0100723 bool mDoneCalled = false;
Steven Morelandcbefd352017-01-23 20:29:05 -0800724};
725
726void waitForHwService(
727 const std::string &interface, const std::string &instanceName) {
Martijn Coenen773609e2017-10-24 09:57:53 +0200728 sp<Waiter> waiter = new Waiter(interface, instanceName, defaultServiceManager1_1());
Steven Moreland0771d8a2018-05-09 13:29:14 -0700729 waiter->wait(false /* timeout */);
Martijn Coenen3fa15c22018-02-10 11:30:00 +0100730 waiter->done();
Martijn Coenen773609e2017-10-24 09:57:53 +0200731}
Steven Morelandcbefd352017-01-23 20:29:05 -0800732
Martijn Coenen773609e2017-10-24 09:57:53 +0200733// Prints relevant error/warning messages for error return values from
734// details::canCastInterface(), both transaction errors (!castReturn.isOk())
735// as well as actual cast failures (castReturn.isOk() && castReturn = false).
736// Returns 'true' if the error is non-fatal and it's useful to retry
737bool handleCastError(const Return<bool>& castReturn, const std::string& descriptor,
738 const std::string& instance) {
739 if (castReturn.isOk()) {
740 if (castReturn) {
741 details::logAlwaysFatal("Successful cast value passed into handleCastError.");
742 }
743 // This should never happen, and there's not really a point in retrying.
744 ALOGE("getService: received incompatible service (bug in hwservicemanager?) for "
745 "%s/%s.", descriptor.c_str(), instance.c_str());
746 return false;
Steven Morelandcbefd352017-01-23 20:29:05 -0800747 }
Martijn Coenen773609e2017-10-24 09:57:53 +0200748 if (castReturn.isDeadObject()) {
749 ALOGW("getService: found dead hwbinder service for %s/%s.", descriptor.c_str(),
750 instance.c_str());
751 return true;
Steven Morelandcbefd352017-01-23 20:29:05 -0800752 }
Martijn Coenen773609e2017-10-24 09:57:53 +0200753 // This can happen due to:
754 // 1) No SELinux permissions
755 // 2) Other transaction failure (no buffer space, kernel error)
756 // The first isn't recoverable, but the second is.
757 // Since we can't yet differentiate between the two, and clients depend
758 // on us not blocking in case 1), treat this as a fatal error for now.
759 ALOGW("getService: unable to call into hwbinder service for %s/%s.",
760 descriptor.c_str(), instance.c_str());
761 return false;
Steven Morelandcbefd352017-01-23 20:29:05 -0800762}
763
Martijn Coenen86c3abb2017-10-24 09:33:28 +0200764sp<::android::hidl::base::V1_0::IBase> getRawServiceInternal(const std::string& descriptor,
765 const std::string& instance,
766 bool retry, bool getStub) {
Steven Moreland84fe49e2019-08-20 17:47:26 +0000767 using Transport = IServiceManager1_0::Transport;
Martijn Coenend35678f2018-03-07 09:51:01 +0100768 sp<Waiter> waiter;
Martijn Coenen86c3abb2017-10-24 09:33:28 +0200769
Yifan Hongdeacf142018-07-10 17:33:34 -0700770 sp<IServiceManager1_1> sm;
771 Transport transport = Transport::EMPTY;
772 if (kIsRecovery) {
Yifan Hongdeacf142018-07-10 17:33:34 -0700773 transport = Transport::PASSTHROUGH;
Yifan Hongdeacf142018-07-10 17:33:34 -0700774 } else {
775 sm = defaultServiceManager1_1();
776 if (sm == nullptr) {
777 ALOGE("getService: defaultServiceManager() is null");
778 return nullptr;
779 }
780
781 Return<Transport> transportRet = sm->getTransport(descriptor, instance);
782
783 if (!transportRet.isOk()) {
784 ALOGE("getService: defaultServiceManager()->getTransport returns %s",
785 transportRet.description().c_str());
786 return nullptr;
787 }
788 transport = transportRet;
Martijn Coenen86c3abb2017-10-24 09:33:28 +0200789 }
790
Martijn Coenen86c3abb2017-10-24 09:33:28 +0200791 const bool vintfHwbinder = (transport == Transport::HWBINDER);
792 const bool vintfPassthru = (transport == Transport::PASSTHROUGH);
Steven Moreland84fe49e2019-08-20 17:47:26 +0000793 const bool trebleTestingOverride = isTrebleTestingOverride();
794 const bool allowLegacy = !kEnforceVintfManifest || (trebleTestingOverride && kDebuggable);
795 const bool vintfLegacy = (transport == Transport::EMPTY) && allowLegacy;
Martijn Coenen86c3abb2017-10-24 09:33:28 +0200796
Steven Moreland84fe49e2019-08-20 17:47:26 +0000797 if (!kEnforceVintfManifest) {
798 ALOGE("getService: Potential race detected. The VINTF manifest is not being enforced. If "
799 "a HAL server has a delay in starting and it is not in the manifest, it will not be "
800 "retrieved. Please make sure all HALs on this device are in the VINTF manifest and "
801 "enable PRODUCT_ENFORCE_VINTF_MANIFEST on this device (this is also enabled by "
802 "PRODUCT_FULL_TREBLE). PRODUCT_ENFORCE_VINTF_MANIFEST will ensure that no race "
803 "condition is possible here.");
Steven Morelandaadec3c2019-08-22 17:06:48 -0700804 sleep(1);
Steven Moreland84fe49e2019-08-20 17:47:26 +0000805 }
Martijn Coenen86c3abb2017-10-24 09:33:28 +0200806
Steven Moreland76371242018-04-19 17:11:39 -0700807 for (int tries = 0; !getStub && (vintfHwbinder || vintfLegacy); tries++) {
808 if (waiter == nullptr && tries > 0) {
Martijn Coenend35678f2018-03-07 09:51:01 +0100809 waiter = new Waiter(descriptor, instance, sm);
810 }
Steven Moreland76371242018-04-19 17:11:39 -0700811 if (waiter != nullptr) {
812 waiter->reset(); // don't reorder this -- see comments on reset()
813 }
Martijn Coenen86c3abb2017-10-24 09:33:28 +0200814 Return<sp<IBase>> ret = sm->get(descriptor, instance);
815 if (!ret.isOk()) {
816 ALOGE("getService: defaultServiceManager()->get returns %s for %s/%s.",
817 ret.description().c_str(), descriptor.c_str(), instance.c_str());
818 break;
819 }
820 sp<IBase> base = ret;
Martijn Coenen773609e2017-10-24 09:57:53 +0200821 if (base != nullptr) {
822 Return<bool> canCastRet =
823 details::canCastInterface(base.get(), descriptor.c_str(), true /* emitError */);
824
825 if (canCastRet.isOk() && canCastRet) {
Steven Moreland76371242018-04-19 17:11:39 -0700826 if (waiter != nullptr) {
827 waiter->done();
828 }
Martijn Coenen773609e2017-10-24 09:57:53 +0200829 return base; // still needs to be wrapped by Bp class.
Martijn Coenen86c3abb2017-10-24 09:33:28 +0200830 }
Martijn Coenen773609e2017-10-24 09:57:53 +0200831
832 if (!handleCastError(canCastRet, descriptor, instance)) break;
Martijn Coenen86c3abb2017-10-24 09:33:28 +0200833 }
834
Martijn Coenen773609e2017-10-24 09:57:53 +0200835 // In case of legacy or we were not asked to retry, don't.
836 if (vintfLegacy || !retry) break;
Martijn Coenen86c3abb2017-10-24 09:33:28 +0200837
Steven Moreland76371242018-04-19 17:11:39 -0700838 if (waiter != nullptr) {
839 ALOGI("getService: Trying again for %s/%s...", descriptor.c_str(), instance.c_str());
Steven Moreland0771d8a2018-05-09 13:29:14 -0700840 waiter->wait(true /* timeout */);
Steven Moreland76371242018-04-19 17:11:39 -0700841 }
Martijn Coenen86c3abb2017-10-24 09:33:28 +0200842 }
843
Martijn Coenend35678f2018-03-07 09:51:01 +0100844 if (waiter != nullptr) {
845 waiter->done();
846 }
Martijn Coenen3fa15c22018-02-10 11:30:00 +0100847
Martijn Coenen86c3abb2017-10-24 09:33:28 +0200848 if (getStub || vintfPassthru || vintfLegacy) {
Steven Moreland84fe49e2019-08-20 17:47:26 +0000849 const sp<IServiceManager1_0> pm = getPassthroughServiceManager();
Martijn Coenen86c3abb2017-10-24 09:33:28 +0200850 if (pm != nullptr) {
851 sp<IBase> base = pm->get(descriptor, instance).withDefault(nullptr);
852 if (!getStub || trebleTestingOverride) {
853 base = wrapPassthrough(base);
854 }
855 return base;
856 }
857 }
858
859 return nullptr;
860}
861
Steven Morelandbb9eb2a2018-10-11 12:10:01 -0700862status_t registerAsServiceInternal(const sp<IBase>& service, const std::string& name) {
863 if (service == nullptr) {
864 return UNEXPECTED_NULL;
865 }
866
867 sp<IServiceManager1_2> sm = defaultServiceManager1_2();
868 if (sm == nullptr) {
869 return INVALID_OPERATION;
870 }
871
Steven Moreland84fe49e2019-08-20 17:47:26 +0000872 const std::string descriptor = getDescriptor(service.get());
873
874 if (kEnforceVintfManifest && !isTrebleTestingOverride()) {
875 using Transport = IServiceManager1_0::Transport;
876 Transport transport = sm->getTransport(descriptor, name);
877
878 if (transport != Transport::HWBINDER) {
879 LOG(ERROR) << "Service " << descriptor << "/" << name
880 << " must be in VINTF manifest in order to register/get.";
881 return UNKNOWN_ERROR;
882 }
883 }
884
Steven Morelandbb9eb2a2018-10-11 12:10:01 -0700885 bool registered = false;
886 Return<void> ret = service->interfaceChain([&](const auto& chain) {
887 registered = sm->addWithChain(name.c_str(), service, chain).withDefault(false);
888 });
889
890 if (!ret.isOk()) {
891 LOG(ERROR) << "Could not retrieve interface chain: " << ret.description();
892 }
893
894 if (registered) {
Steven Moreland84fe49e2019-08-20 17:47:26 +0000895 onRegistrationImpl(descriptor, name);
Steven Morelandbb9eb2a2018-10-11 12:10:01 -0700896 }
897
898 return registered ? OK : UNKNOWN_ERROR;
899}
900
Bernhard Rosenkränzer0c28fd22018-06-04 16:01:47 +0200901} // namespace details
Steven Morelandcbefd352017-01-23 20:29:05 -0800902
Bernhard Rosenkränzer0c28fd22018-06-04 16:01:47 +0200903} // namespace hardware
904} // namespace android