blob: bb690f41404782934030a8bd698c3dbef01be00f [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
Yifan Hong9a22d1d2017-01-25 14:19:26 -080057#define RE_COMPONENT "[a-zA-Z_][a-zA-Z_0-9]*"
58#define RE_PATH RE_COMPONENT "(?:[.]" RE_COMPONENT ")*"
59static const std::regex gLibraryFileNamePattern("(" RE_PATH "@[0-9]+[.][0-9]+)-impl(.*?).so");
60
Steven Morelandbb9eb2a2018-10-11 12:10:01 -070061using ::android::hidl::base::V1_0::IBase;
Steven Moreland2a2678e2017-07-21 18:07:38 -070062using IServiceManager1_0 = android::hidl::manager::V1_0::IServiceManager;
63using IServiceManager1_1 = android::hidl::manager::V1_1::IServiceManager;
Steven Moreland89909692018-05-30 14:11:19 -070064using IServiceManager1_2 = android::hidl::manager::V1_2::IServiceManager;
Steven Morelandbb9eb2a2018-10-11 12:10:01 -070065using ::android::hidl::manager::V1_0::IServiceNotification;
Steven Moreland5d5ef7f2016-10-20 19:19:55 -070066
67namespace android {
68namespace hardware {
69
Yifan Hongdeacf142018-07-10 17:33:34 -070070#if defined(__ANDROID_RECOVERY__)
71static constexpr bool kIsRecovery = true;
72#else
73static constexpr bool kIsRecovery = false;
74#endif
75
Steven Morelandbb9eb2a2018-10-11 12:10:01 -070076static void waitForHwServiceManager() {
Steven Moreland2056cf32019-09-17 17:41:02 -070077 // TODO(b/31559095): need bionic host so that we can use 'prop_info' returned
78 // from WaitForProperty
79#ifdef __ANDROID__
80 static const char* kHwServicemanagerReadyProperty = "hwservicemanager.ready";
81
Steven Morelandc1cee2c2017-03-24 16:23:11 +000082 using std::literals::chrono_literals::operator""s;
83
Steven Moreland2056cf32019-09-17 17:41:02 -070084 using android::base::WaitForProperty;
Steven Morelandc1cee2c2017-03-24 16:23:11 +000085 while (!WaitForProperty(kHwServicemanagerReadyProperty, "true", 1s)) {
86 LOG(WARNING) << "Waited for hwservicemanager.ready for a second, waiting another...";
87 }
Steven Moreland2056cf32019-09-17 17:41:02 -070088#endif // __ANDROID__
Steven Morelandc1cee2c2017-03-24 16:23:11 +000089}
90
Steven Morelandbb9eb2a2018-10-11 12:10:01 -070091static std::string binaryName() {
Steven Moreland405d7612017-04-07 20:31:22 -070092 std::ifstream ifs("/proc/self/cmdline");
93 std::string cmdline;
94 if (!ifs.is_open()) {
95 return "";
96 }
97 ifs >> cmdline;
98
Chih-Hung Hsieh41649d52017-08-03 14:27:21 -070099 size_t idx = cmdline.rfind('/');
Steven Moreland405d7612017-04-07 20:31:22 -0700100 if (idx != std::string::npos) {
101 cmdline = cmdline.substr(idx + 1);
102 }
103
104 return cmdline;
105}
106
Steven Morelandbb9eb2a2018-10-11 12:10:01 -0700107static std::string packageWithoutVersion(const std::string& packageAndVersion) {
Steven Moreland7d09a402018-04-06 10:44:05 -0700108 size_t at = packageAndVersion.find('@');
109 if (at == std::string::npos) return packageAndVersion;
110 return packageAndVersion.substr(0, at);
111}
112
Steven Morelandbb9eb2a2018-10-11 12:10:01 -0700113static void tryShortenProcessName(const std::string& descriptor) {
Steven Moreland7d09a402018-04-06 10:44:05 -0700114 const static std::string kTasks = "/proc/self/task/";
115
116 // make sure that this binary name is in the same package
Steven Moreland405d7612017-04-07 20:31:22 -0700117 std::string processName = binaryName();
118
Steven Moreland7d09a402018-04-06 10:44:05 -0700119 // e.x. android.hardware.foo is this package
Steven Moreland8092aa02018-10-12 15:54:56 -0700120 if (!base::StartsWith(packageWithoutVersion(processName), packageWithoutVersion(descriptor))) {
Steven Moreland405d7612017-04-07 20:31:22 -0700121 return;
122 }
123
Steven Morelandbb9eb2a2018-10-11 12:10:01 -0700124 // e.x. android.hardware.module.foo@1.2::IFoo -> foo@1.2
125 size_t lastDot = descriptor.rfind('.');
Steven Moreland7d09a402018-04-06 10:44:05 -0700126 if (lastDot == std::string::npos) return;
Steven Morelandbb9eb2a2018-10-11 12:10:01 -0700127 size_t secondDot = descriptor.rfind('.', lastDot - 1);
Steven Moreland7d09a402018-04-06 10:44:05 -0700128 if (secondDot == std::string::npos) return;
Steven Moreland405d7612017-04-07 20:31:22 -0700129
Steven Moreland7d09a402018-04-06 10:44:05 -0700130 std::string newName = processName.substr(secondDot + 1, std::string::npos);
131 ALOGI("Removing namespace from process name %s to %s.", processName.c_str(), newName.c_str());
132
133 std::unique_ptr<DIR, decltype(&closedir)> dir(opendir(kTasks.c_str()), closedir);
134 if (dir == nullptr) return;
135
136 dirent* dp;
137 while ((dp = readdir(dir.get())) != nullptr) {
138 if (dp->d_type != DT_DIR) continue;
139 if (dp->d_name[0] == '.') continue;
140
141 std::fstream fs(kTasks + dp->d_name + "/comm");
142 if (!fs.is_open()) {
143 ALOGI("Could not rename process, failed read comm for %s.", dp->d_name);
144 continue;
145 }
146
147 std::string oldComm;
148 fs >> oldComm;
149
150 // don't rename if it already has an explicit name
Steven Moreland8092aa02018-10-12 15:54:56 -0700151 if (base::StartsWith(descriptor, oldComm)) {
Steven Moreland7d09a402018-04-06 10:44:05 -0700152 fs.seekg(0, fs.beg);
153 fs << newName;
154 }
Steven Moreland405d7612017-04-07 20:31:22 -0700155 }
Steven Moreland405d7612017-04-07 20:31:22 -0700156}
157
158namespace details {
159
Peter Kalauskas64ffced2018-09-05 16:41:08 -0700160/*
161 * Returns the age of the current process by reading /proc/self/stat and comparing starttime to the
162 * current time. This is useful for measuring how long it took a HAL to register itself.
163 */
Steven Morelandbb9eb2a2018-10-11 12:10:01 -0700164static long getProcessAgeMs() {
Peter Kalauskas64ffced2018-09-05 16:41:08 -0700165 constexpr const int PROCFS_STAT_STARTTIME_INDEX = 21;
166 std::string content;
167 android::base::ReadFileToString("/proc/self/stat", &content, false);
168 auto stats = android::base::Split(content, " ");
169 if (stats.size() <= PROCFS_STAT_STARTTIME_INDEX) {
170 LOG(INFO) << "Could not read starttime from /proc/self/stat";
171 return -1;
172 }
173 const std::string& startTimeString = stats[PROCFS_STAT_STARTTIME_INDEX];
174 static const int64_t ticksPerSecond = sysconf(_SC_CLK_TCK);
175 const int64_t uptime = android::uptimeMillis();
176
177 unsigned long long startTimeInClockTicks = 0;
178 if (android::base::ParseUint(startTimeString, &startTimeInClockTicks)) {
179 long startTimeMs = 1000ULL * startTimeInClockTicks / ticksPerSecond;
180 return uptime - startTimeMs;
181 }
182 return -1;
183}
184
Steven Morelandbb9eb2a2018-10-11 12:10:01 -0700185static void onRegistrationImpl(const std::string& descriptor, const std::string& instanceName) {
Peter Kalauskas64ffced2018-09-05 16:41:08 -0700186 long halStartDelay = getProcessAgeMs();
187 if (halStartDelay >= 0) {
188 // The "start delay" printed here is an estimate of how long it took the HAL to go from
189 // process creation to registering itself as a HAL. Actual start time could be longer
190 // because the process might not have joined the threadpool yet, so it might not be ready to
191 // process transactions.
Steven Morelandbb9eb2a2018-10-11 12:10:01 -0700192 LOG(INFO) << "Registered " << descriptor << "/" << instanceName << " (start delay of "
Peter Kalauskas64ffced2018-09-05 16:41:08 -0700193 << halStartDelay << "ms)";
194 }
Steven Morelandbb9eb2a2018-10-11 12:10:01 -0700195
196 tryShortenProcessName(descriptor);
197}
198
199void onRegistration(const std::string& packageName, const std::string& interfaceName,
200 const std::string& instanceName) {
201 return onRegistrationImpl(packageName + "::" + interfaceName, instanceName);
Steven Moreland405d7612017-04-07 20:31:22 -0700202}
203
204} // details
205
Steven Moreland2a2678e2017-07-21 18:07:38 -0700206sp<IServiceManager1_0> defaultServiceManager() {
Steven Moreland89909692018-05-30 14:11:19 -0700207 return defaultServiceManager1_2();
Steven Moreland2a2678e2017-07-21 18:07:38 -0700208}
209sp<IServiceManager1_1> defaultServiceManager1_1() {
Steven Moreland89909692018-05-30 14:11:19 -0700210 return defaultServiceManager1_2();
211}
212sp<IServiceManager1_2> defaultServiceManager1_2() {
213 using android::hidl::manager::V1_2::BnHwServiceManager;
214 using android::hidl::manager::V1_2::BpHwServiceManager;
215
216 static std::mutex gDefaultServiceManagerLock;
217 static sp<IServiceManager1_2> gDefaultServiceManager;
218
Steven Moreland5d5ef7f2016-10-20 19:19:55 -0700219 {
Steven Moreland89909692018-05-30 14:11:19 -0700220 std::lock_guard<std::mutex> _l(gDefaultServiceManagerLock);
221 if (gDefaultServiceManager != nullptr) {
222 return gDefaultServiceManager;
Yifan Hong8fb656b2017-03-16 14:30:40 -0700223 }
Steven Moreland405d7612017-04-07 20:31:22 -0700224
Yifan Hong8fb656b2017-03-16 14:30:40 -0700225 if (access("/dev/hwbinder", F_OK|R_OK|W_OK) != 0) {
226 // HwBinder not available on this device or not accessible to
227 // this process.
228 return nullptr;
229 }
Steven Morelandc1cee2c2017-03-24 16:23:11 +0000230
231 waitForHwServiceManager();
232
Steven Moreland89909692018-05-30 14:11:19 -0700233 while (gDefaultServiceManager == nullptr) {
234 gDefaultServiceManager =
235 fromBinder<IServiceManager1_2, BpHwServiceManager, BnHwServiceManager>(
236 ProcessState::self()->getContextObject(nullptr));
237 if (gDefaultServiceManager == nullptr) {
Steven Morelandc1cee2c2017-03-24 16:23:11 +0000238 LOG(ERROR) << "Waited for hwservicemanager, but got nullptr.";
Steven Moreland5d5ef7f2016-10-20 19:19:55 -0700239 sleep(1);
Yifan Hong8fb656b2017-03-16 14:30:40 -0700240 }
Steven Moreland5d5ef7f2016-10-20 19:19:55 -0700241 }
242 }
243
Steven Moreland89909692018-05-30 14:11:19 -0700244 return gDefaultServiceManager;
Steven Moreland5d5ef7f2016-10-20 19:19:55 -0700245}
246
Elliott Hughes19306142018-10-26 13:13:04 -0700247static std::vector<std::string> findFiles(const std::string& path, const std::string& prefix,
248 const std::string& suffix) {
Steven Moreland0091c092017-01-20 23:15:18 +0000249 std::unique_ptr<DIR, decltype(&closedir)> dir(opendir(path.c_str()), closedir);
250 if (!dir) return {};
251
252 std::vector<std::string> results{};
253
254 dirent* dp;
255 while ((dp = readdir(dir.get())) != nullptr) {
256 std::string name = dp->d_name;
257
Steven Moreland8092aa02018-10-12 15:54:56 -0700258 if (base::StartsWith(name, prefix) && base::EndsWith(name, suffix)) {
Steven Moreland0091c092017-01-20 23:15:18 +0000259 results.push_back(name);
260 }
261 }
262
263 return results;
264}
265
Yifan Hongbd0d8f72017-05-24 19:43:51 -0700266bool matchPackageName(const std::string& lib, std::string* matchedName, std::string* implName) {
Yifan Hong9a22d1d2017-01-25 14:19:26 -0800267 std::smatch match;
268 if (std::regex_match(lib, match, gLibraryFileNamePattern)) {
269 *matchedName = match.str(1) + "::I*";
Yifan Hongbd0d8f72017-05-24 19:43:51 -0700270 *implName = match.str(2);
Yifan Hong9a22d1d2017-01-25 14:19:26 -0800271 return true;
272 }
273 return false;
274}
275
Yifan Hong7f49f592017-02-03 15:11:44 -0800276static void registerReference(const hidl_string &interfaceName, const hidl_string &instanceName) {
Yifan Hongdeacf142018-07-10 17:33:34 -0700277 if (kIsRecovery) {
278 // No hwservicemanager in recovery.
279 return;
280 }
281
Steven Moreland2a2678e2017-07-21 18:07:38 -0700282 sp<IServiceManager1_0> binderizedManager = defaultServiceManager();
Yifan Hong7f49f592017-02-03 15:11:44 -0800283 if (binderizedManager == nullptr) {
284 LOG(WARNING) << "Could not registerReference for "
285 << interfaceName << "/" << instanceName
286 << ": null binderized manager.";
287 return;
288 }
Martijn Coenenaf4aba52017-04-27 09:41:13 -0700289 auto ret = binderizedManager->registerPassthroughClient(interfaceName, instanceName);
Yifan Hong7f49f592017-02-03 15:11:44 -0800290 if (!ret.isOk()) {
291 LOG(WARNING) << "Could not registerReference for "
292 << interfaceName << "/" << instanceName
293 << ": " << ret.description();
Steven Moreland0aeaa782017-03-22 08:11:07 -0700294 return;
Yifan Hong7f49f592017-02-03 15:11:44 -0800295 }
Steven Morelande681aa52017-02-15 16:22:37 -0800296 LOG(VERBOSE) << "Successfully registerReference for "
297 << interfaceName << "/" << instanceName;
Yifan Hong7f49f592017-02-03 15:11:44 -0800298}
299
Yifan Hongbd0d8f72017-05-24 19:43:51 -0700300using InstanceDebugInfo = hidl::manager::V1_0::IServiceManager::InstanceDebugInfo;
301static inline void fetchPidsForPassthroughLibraries(
302 std::map<std::string, InstanceDebugInfo>* infos) {
303 static const std::string proc = "/proc/";
304
305 std::map<std::string, std::set<pid_t>> pids;
306 std::unique_ptr<DIR, decltype(&closedir)> dir(opendir(proc.c_str()), closedir);
307 if (!dir) return;
308 dirent* dp;
309 while ((dp = readdir(dir.get())) != nullptr) {
Stephen Hinesfd9ecee2017-09-27 18:52:52 -0700310 pid_t pid = strtoll(dp->d_name, nullptr, 0);
Yifan Hongbd0d8f72017-05-24 19:43:51 -0700311 if (pid == 0) continue;
312 std::string mapsPath = proc + dp->d_name + "/maps";
313 std::ifstream ifs{mapsPath};
314 if (!ifs.is_open()) continue;
315
316 for (std::string line; std::getline(ifs, line);) {
317 // The last token of line should look like
318 // vendor/lib64/hw/android.hardware.foo@1.0-impl-extra.so
319 // Use some simple filters to ignore bad lines before extracting libFileName
320 // and checking the key in info to make parsing faster.
321 if (line.back() != 'o') continue;
322 if (line.rfind('@') == std::string::npos) continue;
323
324 auto spacePos = line.rfind(' ');
325 if (spacePos == std::string::npos) continue;
326 auto libFileName = line.substr(spacePos + 1);
327 auto it = infos->find(libFileName);
328 if (it == infos->end()) continue;
329 pids[libFileName].insert(pid);
330 }
331 }
332 for (auto& pair : *infos) {
333 pair.second.clientPids =
334 std::vector<pid_t>{pids[pair.first].begin(), pids[pair.first].end()};
335 }
336}
337
Steven Moreland2a2678e2017-07-21 18:07:38 -0700338struct PassthroughServiceManager : IServiceManager1_1 {
Chih-Hung Hsieh41649d52017-08-03 14:27:21 -0700339 static void openLibs(
340 const std::string& fqName,
341 const std::function<bool /* continue */ (void* /* handle */, const std::string& /* lib */,
342 const std::string& /* sym */)>& eachLib) {
Steven Moreland819c05d2017-04-06 17:24:22 -0700343 //fqName looks like android.hardware.foo@1.0::IFoo
Steven Moreland519306f2017-06-06 17:14:12 -0700344 size_t idx = fqName.find("::");
Steven Moreland819c05d2017-04-06 17:24:22 -0700345
346 if (idx == std::string::npos ||
Steven Moreland519306f2017-06-06 17:14:12 -0700347 idx + strlen("::") + 1 >= fqName.size()) {
Steven Moreland337e6b62017-01-18 17:25:13 -0800348 LOG(ERROR) << "Invalid interface name passthrough lookup: " << fqName;
Steven Moreland519306f2017-06-06 17:14:12 -0700349 return;
Steven Moreland337e6b62017-01-18 17:25:13 -0800350 }
351
Steven Moreland519306f2017-06-06 17:14:12 -0700352 std::string packageAndVersion = fqName.substr(0, idx);
353 std::string ifaceName = fqName.substr(idx + strlen("::"));
Steven Moreland819c05d2017-04-06 17:24:22 -0700354
355 const std::string prefix = packageAndVersion + "-impl";
356 const std::string sym = "HIDL_FETCH_" + ifaceName;
Steven Moreland348802d2017-02-23 12:48:55 -0800357
Steven Moreland77f4c852017-10-12 11:05:53 -0700358 constexpr int dlMode = RTLD_LAZY;
359 void* handle = nullptr;
Steven Moreland337e6b62017-01-18 17:25:13 -0800360
Steven Morelanda29905c2017-03-01 10:42:35 -0800361 dlerror(); // clear
362
Justin Yun1f048102017-12-01 15:30:08 +0900363 static std::string halLibPathVndkSp = android::base::StringPrintf(
364 HAL_LIBRARY_PATH_VNDK_SP_FOR_VERSION, details::getVndkVersionStr().c_str());
Justin Yun6a323ed2018-10-18 18:41:56 +0900365 std::vector<std::string> paths = {
366 HAL_LIBRARY_PATH_ODM, HAL_LIBRARY_PATH_VENDOR, halLibPathVndkSp,
367#ifndef __ANDROID_VNDK__
368 HAL_LIBRARY_PATH_SYSTEM,
369#endif
370 };
Steven Moreland77f4c852017-10-12 11:05:53 -0700371
Steven Morelandf7dea692017-07-14 12:49:28 -0700372#ifdef LIBHIDL_TARGET_DEBUGGABLE
373 const char* env = std::getenv("TREBLE_TESTING_OVERRIDE");
374 const bool trebleTestingOverride = env && !strcmp(env, "true");
375 if (trebleTestingOverride) {
Steven Moreland77f4c852017-10-12 11:05:53 -0700376 // Load HAL implementations that are statically linked
377 handle = dlopen(nullptr, dlMode);
378 if (handle == nullptr) {
379 const char* error = dlerror();
380 LOG(ERROR) << "Failed to dlopen self: "
381 << (error == nullptr ? "unknown error" : error);
382 } else if (!eachLib(handle, "SELF", sym)) {
383 return;
384 }
385
Steven Morelandf7dea692017-07-14 12:49:28 -0700386 const char* vtsRootPath = std::getenv("VTS_ROOT_PATH");
387 if (vtsRootPath && strlen(vtsRootPath) > 0) {
388 const std::string halLibraryPathVtsOverride =
389 std::string(vtsRootPath) + HAL_LIBRARY_PATH_SYSTEM;
Steven Moreland77f4c852017-10-12 11:05:53 -0700390 paths.insert(paths.begin(), halLibraryPathVtsOverride);
Steven Morelandf7dea692017-07-14 12:49:28 -0700391 }
392 }
393#endif
Steven Moreland77f4c852017-10-12 11:05:53 -0700394
Steven Morelandf7dea692017-07-14 12:49:28 -0700395 for (const std::string& path : paths) {
Elliott Hughes19306142018-10-26 13:13:04 -0700396 std::vector<std::string> libs = findFiles(path, prefix, ".so");
Steven Moreland0091c092017-01-20 23:15:18 +0000397
Steven Moreland0091c092017-01-20 23:15:18 +0000398 for (const std::string &lib : libs) {
Steven Moreland348802d2017-02-23 12:48:55 -0800399 const std::string fullPath = path + lib;
400
Yifan Hongdeacf142018-07-10 17:33:34 -0700401 if (kIsRecovery || path == HAL_LIBRARY_PATH_SYSTEM) {
Jiyong Park3ab546c2017-04-06 20:28:07 +0900402 handle = dlopen(fullPath.c_str(), dlMode);
Steven Moreland65c00cb2017-10-12 11:35:22 -0700403 } else {
Steven Moreland2056cf32019-09-17 17:41:02 -0700404#if !defined(__ANDROID_RECOVERY__) && defined(__ANDROID__)
Steven Moreland65c00cb2017-10-12 11:35:22 -0700405 handle = android_load_sphal_library(fullPath.c_str(), dlMode);
Jerry Zhang86ae9992018-05-30 17:11:09 -0700406#endif
Jiyong Park3ab546c2017-04-06 20:28:07 +0900407 }
408
Steven Moreland348802d2017-02-23 12:48:55 -0800409 if (handle == nullptr) {
410 const char* error = dlerror();
411 LOG(ERROR) << "Failed to dlopen " << lib << ": "
412 << (error == nullptr ? "unknown error" : error);
413 continue;
Steven Moreland0091c092017-01-20 23:15:18 +0000414 }
Steven Moreland348802d2017-02-23 12:48:55 -0800415
Steven Moreland519306f2017-06-06 17:14:12 -0700416 if (!eachLib(handle, lib, sym)) {
417 return;
Steven Moreland348802d2017-02-23 12:48:55 -0800418 }
Steven Moreland337e6b62017-01-18 17:25:13 -0800419 }
420 }
Steven Moreland519306f2017-06-06 17:14:12 -0700421 }
Steven Moreland337e6b62017-01-18 17:25:13 -0800422
Steven Moreland519306f2017-06-06 17:14:12 -0700423 Return<sp<IBase>> get(const hidl_string& fqName,
424 const hidl_string& name) override {
425 sp<IBase> ret = nullptr;
426
427 openLibs(fqName, [&](void* handle, const std::string &lib, const std::string &sym) {
428 IBase* (*generator)(const char* name);
429 *(void **)(&generator) = dlsym(handle, sym.c_str());
430 if(!generator) {
431 const char* error = dlerror();
432 LOG(ERROR) << "Passthrough lookup opened " << lib
433 << " but could not find symbol " << sym << ": "
434 << (error == nullptr ? "unknown error" : error);
435 dlclose(handle);
436 return true;
437 }
438
439 ret = (*generator)(name.c_str());
440
441 if (ret == nullptr) {
442 dlclose(handle);
443 return true; // this module doesn't provide this instance name
444 }
445
Steven Morelande69e8d32018-03-14 10:55:42 -0700446 // Actual fqname might be a subclass.
447 // This assumption is tested in vts_treble_vintf_test
448 using ::android::hardware::details::getDescriptor;
449 std::string actualFqName = getDescriptor(ret.get());
450 CHECK(actualFqName.size() > 0);
451 registerReference(actualFqName, name);
Steven Moreland519306f2017-06-06 17:14:12 -0700452 return false;
453 });
454
455 return ret;
Steven Moreland337e6b62017-01-18 17:25:13 -0800456 }
457
Martijn Coenen67a02492017-03-06 13:04:48 +0100458 Return<bool> add(const hidl_string& /* name */,
Steven Moreland337e6b62017-01-18 17:25:13 -0800459 const sp<IBase>& /* service */) override {
460 LOG(FATAL) << "Cannot register services with passthrough service manager.";
461 return false;
462 }
463
Steven Morelandc601c5f2017-04-06 09:26:07 -0700464 Return<Transport> getTransport(const hidl_string& /* fqName */,
465 const hidl_string& /* name */) {
466 LOG(FATAL) << "Cannot getTransport with passthrough service manager.";
467 return Transport::EMPTY;
468 }
469
Yifan Hong705e5da2017-03-02 16:59:39 -0800470 Return<void> list(list_cb /* _hidl_cb */) override {
471 LOG(FATAL) << "Cannot list services with passthrough service manager.";
Steven Moreland337e6b62017-01-18 17:25:13 -0800472 return Void();
473 }
474 Return<void> listByInterface(const hidl_string& /* fqInstanceName */,
475 listByInterface_cb /* _hidl_cb */) override {
476 // TODO: add this functionality
477 LOG(FATAL) << "Cannot list services with passthrough service manager.";
478 return Void();
479 }
480
481 Return<bool> registerForNotifications(const hidl_string& /* fqName */,
482 const hidl_string& /* name */,
483 const sp<IServiceNotification>& /* callback */) override {
484 // This makes no sense.
485 LOG(FATAL) << "Cannot register for notifications with passthrough service manager.";
486 return false;
487 }
488
Yifan Hong705e5da2017-03-02 16:59:39 -0800489 Return<void> debugDump(debugDump_cb _hidl_cb) override {
490 using Arch = ::android::hidl::base::V1_0::DebugInfo::Architecture;
Yifan Hongbd0d8f72017-05-24 19:43:51 -0700491 using std::literals::string_literals::operator""s;
Justin Yun1f048102017-12-01 15:30:08 +0900492 static std::string halLibPathVndkSp64 = android::base::StringPrintf(
493 HAL_LIBRARY_PATH_VNDK_SP_64BIT_FOR_VERSION, details::getVndkVersionStr().c_str());
494 static std::string halLibPathVndkSp32 = android::base::StringPrintf(
495 HAL_LIBRARY_PATH_VNDK_SP_32BIT_FOR_VERSION, details::getVndkVersionStr().c_str());
Jiyong Park56eb1492017-08-04 16:16:13 +0900496 static std::vector<std::pair<Arch, std::vector<const char*>>> sAllPaths{
497 {Arch::IS_64BIT,
Justin Yun6a323ed2018-10-18 18:41:56 +0900498 {
499 HAL_LIBRARY_PATH_ODM_64BIT, HAL_LIBRARY_PATH_VENDOR_64BIT,
500 halLibPathVndkSp64.c_str(),
501#ifndef __ANDROID_VNDK__
502 HAL_LIBRARY_PATH_SYSTEM_64BIT,
503#endif
504 }},
Jiyong Park56eb1492017-08-04 16:16:13 +0900505 {Arch::IS_32BIT,
Justin Yun6a323ed2018-10-18 18:41:56 +0900506 {
507 HAL_LIBRARY_PATH_ODM_32BIT, HAL_LIBRARY_PATH_VENDOR_32BIT,
508 halLibPathVndkSp32.c_str(),
509#ifndef __ANDROID_VNDK__
510 HAL_LIBRARY_PATH_SYSTEM_32BIT,
511#endif
512 }}};
Yifan Hongbd0d8f72017-05-24 19:43:51 -0700513 std::map<std::string, InstanceDebugInfo> map;
Yifan Hong705e5da2017-03-02 16:59:39 -0800514 for (const auto &pair : sAllPaths) {
515 Arch arch = pair.first;
516 for (const auto &path : pair.second) {
Elliott Hughes19306142018-10-26 13:13:04 -0700517 std::vector<std::string> libs = findFiles(path, "", ".so");
Yifan Hong705e5da2017-03-02 16:59:39 -0800518 for (const std::string &lib : libs) {
519 std::string matchedName;
Yifan Hongbd0d8f72017-05-24 19:43:51 -0700520 std::string implName;
521 if (matchPackageName(lib, &matchedName, &implName)) {
522 std::string instanceName{"* ("s + path + ")"s};
523 if (!implName.empty()) instanceName += " ("s + implName + ")"s;
524 map.emplace(path + lib, InstanceDebugInfo{.interfaceName = matchedName,
525 .instanceName = instanceName,
526 .clientPids = {},
527 .arch = arch});
Yifan Hong705e5da2017-03-02 16:59:39 -0800528 }
529 }
530 }
531 }
Yifan Hongbd0d8f72017-05-24 19:43:51 -0700532 fetchPidsForPassthroughLibraries(&map);
533 hidl_vec<InstanceDebugInfo> vec;
534 vec.resize(map.size());
535 size_t idx = 0;
536 for (auto&& pair : map) {
537 vec[idx++] = std::move(pair.second);
538 }
Yifan Hong705e5da2017-03-02 16:59:39 -0800539 _hidl_cb(vec);
Yifan Hong7f49f592017-02-03 15:11:44 -0800540 return Void();
541 }
542
Martijn Coenenaf4aba52017-04-27 09:41:13 -0700543 Return<void> registerPassthroughClient(const hidl_string &, const hidl_string &) override {
Yifan Hong7f49f592017-02-03 15:11:44 -0800544 // This makes no sense.
545 LOG(FATAL) << "Cannot call registerPassthroughClient on passthrough service manager. "
546 << "Call it on defaultServiceManager() instead.";
Yifan Hong9a22d1d2017-01-25 14:19:26 -0800547 return Void();
548 }
549
Steven Moreland2a2678e2017-07-21 18:07:38 -0700550 Return<bool> unregisterForNotifications(const hidl_string& /* fqName */,
551 const hidl_string& /* name */,
552 const sp<IServiceNotification>& /* callback */) override {
553 // This makes no sense.
554 LOG(FATAL) << "Cannot unregister for notifications with passthrough service manager.";
555 return false;
556 }
557
Steven Moreland337e6b62017-01-18 17:25:13 -0800558};
559
Steven Moreland2a2678e2017-07-21 18:07:38 -0700560sp<IServiceManager1_0> getPassthroughServiceManager() {
561 return getPassthroughServiceManager1_1();
562}
563sp<IServiceManager1_1> getPassthroughServiceManager1_1() {
Steven Moreland337e6b62017-01-18 17:25:13 -0800564 static sp<PassthroughServiceManager> manager(new PassthroughServiceManager());
565 return manager;
566}
567
Steven Moreland92b247c2019-08-21 14:53:11 -0700568std::vector<std::string> getAllHalInstanceNames(const std::string& descriptor) {
569 std::vector<std::string> ret;
570 auto sm = defaultServiceManager1_2();
571 sm->listManifestByInterface(descriptor, [&](const auto& instances) {
572 ret.reserve(instances.size());
573 for (const auto& i : instances) {
574 ret.push_back(i);
575 }
576 });
577 return ret;
578}
579
Steven Morelandcbefd352017-01-23 20:29:05 -0800580namespace details {
581
Steven Moreland519306f2017-06-06 17:14:12 -0700582void preloadPassthroughService(const std::string &descriptor) {
583 PassthroughServiceManager::openLibs(descriptor,
584 [&](void* /* handle */, const std::string& /* lib */, const std::string& /* sym */) {
585 // do nothing
586 return true; // open all libs
587 });
588}
589
Steven Morelandcbefd352017-01-23 20:29:05 -0800590struct Waiter : IServiceNotification {
Martijn Coenen773609e2017-10-24 09:57:53 +0200591 Waiter(const std::string& interface, const std::string& instanceName,
592 const sp<IServiceManager1_1>& sm) : mInterfaceName(interface),
593 mInstanceName(instanceName), mSm(sm) {
Martijn Coenene6cdfd32017-11-13 14:03:05 +0100594 }
595
596 void onFirstRef() override {
Martijn Coenenb88ae7f2017-10-24 11:45:41 +0200597 // If this process only has one binder thread, and we're calling wait() from
598 // that thread, it will block forever because we hung up the one and only
599 // binder thread on a condition variable that can only be notified by an
600 // incoming binder call.
Tobias Lindskog88e886f2018-01-05 10:32:43 +0100601 if (IPCThreadState::self()->isOnlyBinderThread()) {
Martijn Coenenb88ae7f2017-10-24 11:45:41 +0200602 LOG(WARNING) << "Can't efficiently wait for " << mInterfaceName << "/"
603 << mInstanceName << ", because we are called from "
604 << "the only binder thread in this process.";
605 return;
606 }
607
Martijn Coenene6cdfd32017-11-13 14:03:05 +0100608 Return<bool> ret = mSm->registerForNotifications(mInterfaceName, mInstanceName, this);
Martijn Coenen773609e2017-10-24 09:57:53 +0200609
610 if (!ret.isOk()) {
611 LOG(ERROR) << "Transport error, " << ret.description()
Martijn Coenene6cdfd32017-11-13 14:03:05 +0100612 << ", during notification registration for " << mInterfaceName << "/"
613 << mInstanceName << ".";
Martijn Coenen773609e2017-10-24 09:57:53 +0200614 return;
615 }
616
617 if (!ret) {
Martijn Coenene6cdfd32017-11-13 14:03:05 +0100618 LOG(ERROR) << "Could not register for notifications for " << mInterfaceName << "/"
619 << mInstanceName << ".";
Martijn Coenen773609e2017-10-24 09:57:53 +0200620 return;
621 }
622
623 mRegisteredForNotifications = true;
624 }
625
626 ~Waiter() {
Martijn Coenen3fa15c22018-02-10 11:30:00 +0100627 if (!mDoneCalled) {
628 LOG(FATAL)
629 << "Waiter still registered for notifications, call done() before dropping ref!";
Martijn Coenen773609e2017-10-24 09:57:53 +0200630 }
631 }
632
Steven Morelandcbefd352017-01-23 20:29:05 -0800633 Return<void> onRegistration(const hidl_string& /* fqName */,
634 const hidl_string& /* name */,
635 bool /* preexisting */) override {
636 std::unique_lock<std::mutex> lock(mMutex);
637 if (mRegistered) {
638 return Void();
639 }
640 mRegistered = true;
641 lock.unlock();
642
643 mCondition.notify_one();
644 return Void();
645 }
646
Steven Moreland0771d8a2018-05-09 13:29:14 -0700647 void wait(bool timeout) {
Steven Moreland49605102017-03-28 09:33:06 -0700648 using std::literals::chrono_literals::operator""s;
649
Martijn Coenen773609e2017-10-24 09:57:53 +0200650 if (!mRegisteredForNotifications) {
651 // As an alternative, just sleep for a second and return
652 LOG(WARNING) << "Waiting one second for " << mInterfaceName << "/" << mInstanceName;
653 sleep(1);
654 return;
655 }
656
Steven Morelandcbefd352017-01-23 20:29:05 -0800657 std::unique_lock<std::mutex> lock(mMutex);
Steven Moreland0771d8a2018-05-09 13:29:14 -0700658 do {
Steven Moreland49605102017-03-28 09:33:06 -0700659 mCondition.wait_for(lock, 1s, [this]{
660 return mRegistered;
661 });
662
663 if (mRegistered) {
664 break;
665 }
666
Steven Moreland0771d8a2018-05-09 13:29:14 -0700667 LOG(WARNING) << "Waited one second for " << mInterfaceName << "/" << mInstanceName;
668 } while (!timeout);
Steven Morelandcbefd352017-01-23 20:29:05 -0800669 }
670
Martijn Coenen773609e2017-10-24 09:57:53 +0200671 // Be careful when using this; after calling reset(), you must always try to retrieve
672 // the corresponding service before blocking on the waiter; otherwise, you might run
673 // into a race-condition where the service has just (re-)registered, you clear the state
674 // here, and subsequently calling waiter->wait() will block forever.
675 void reset() {
676 std::unique_lock<std::mutex> lock(mMutex);
677 mRegistered = false;
678 }
Martijn Coenen3fa15c22018-02-10 11:30:00 +0100679
680 // done() must be called before dropping the last strong ref to the Waiter, to make
681 // sure we can properly unregister with hwservicemanager.
682 void done() {
683 if (mRegisteredForNotifications) {
684 if (!mSm->unregisterForNotifications(mInterfaceName, mInstanceName, this)
685 .withDefault(false)) {
686 LOG(ERROR) << "Could not unregister service notification for " << mInterfaceName
687 << "/" << mInstanceName << ".";
688 } else {
689 mRegisteredForNotifications = false;
690 }
691 }
692 mDoneCalled = true;
693 }
694
695 private:
Martijn Coenen773609e2017-10-24 09:57:53 +0200696 const std::string mInterfaceName;
697 const std::string mInstanceName;
Steven Morelandad1f9922018-10-02 19:37:05 -0700698 sp<IServiceManager1_1> mSm;
Steven Morelandcbefd352017-01-23 20:29:05 -0800699 std::mutex mMutex;
700 std::condition_variable mCondition;
701 bool mRegistered = false;
Martijn Coenen773609e2017-10-24 09:57:53 +0200702 bool mRegisteredForNotifications = false;
Martijn Coenen3fa15c22018-02-10 11:30:00 +0100703 bool mDoneCalled = false;
Steven Morelandcbefd352017-01-23 20:29:05 -0800704};
705
706void waitForHwService(
707 const std::string &interface, const std::string &instanceName) {
Martijn Coenen773609e2017-10-24 09:57:53 +0200708 sp<Waiter> waiter = new Waiter(interface, instanceName, defaultServiceManager1_1());
Steven Moreland0771d8a2018-05-09 13:29:14 -0700709 waiter->wait(false /* timeout */);
Martijn Coenen3fa15c22018-02-10 11:30:00 +0100710 waiter->done();
Martijn Coenen773609e2017-10-24 09:57:53 +0200711}
Steven Morelandcbefd352017-01-23 20:29:05 -0800712
Martijn Coenen773609e2017-10-24 09:57:53 +0200713// Prints relevant error/warning messages for error return values from
714// details::canCastInterface(), both transaction errors (!castReturn.isOk())
715// as well as actual cast failures (castReturn.isOk() && castReturn = false).
716// Returns 'true' if the error is non-fatal and it's useful to retry
717bool handleCastError(const Return<bool>& castReturn, const std::string& descriptor,
718 const std::string& instance) {
719 if (castReturn.isOk()) {
720 if (castReturn) {
721 details::logAlwaysFatal("Successful cast value passed into handleCastError.");
722 }
723 // This should never happen, and there's not really a point in retrying.
724 ALOGE("getService: received incompatible service (bug in hwservicemanager?) for "
725 "%s/%s.", descriptor.c_str(), instance.c_str());
726 return false;
Steven Morelandcbefd352017-01-23 20:29:05 -0800727 }
Martijn Coenen773609e2017-10-24 09:57:53 +0200728 if (castReturn.isDeadObject()) {
729 ALOGW("getService: found dead hwbinder service for %s/%s.", descriptor.c_str(),
730 instance.c_str());
731 return true;
Steven Morelandcbefd352017-01-23 20:29:05 -0800732 }
Martijn Coenen773609e2017-10-24 09:57:53 +0200733 // This can happen due to:
734 // 1) No SELinux permissions
735 // 2) Other transaction failure (no buffer space, kernel error)
736 // The first isn't recoverable, but the second is.
737 // Since we can't yet differentiate between the two, and clients depend
738 // on us not blocking in case 1), treat this as a fatal error for now.
739 ALOGW("getService: unable to call into hwbinder service for %s/%s.",
740 descriptor.c_str(), instance.c_str());
741 return false;
Steven Morelandcbefd352017-01-23 20:29:05 -0800742}
743
Steven Moreland84fe49e2019-08-20 17:47:26 +0000744#ifdef ENFORCE_VINTF_MANIFEST
745static constexpr bool kEnforceVintfManifest = true;
746#else
747static constexpr bool kEnforceVintfManifest = false;
748#endif
749
750#ifdef LIBHIDL_TARGET_DEBUGGABLE
751static constexpr bool kDebuggable = true;
752#else
753static constexpr bool kDebuggable = false;
754#endif
755
756static inline bool isTrebleTestingOverride() {
757 if (kEnforceVintfManifest && !kDebuggable) {
758 // don't allow testing override in production
759 return false;
760 }
761
762 const char* env = std::getenv("TREBLE_TESTING_OVERRIDE");
763 return env && !strcmp(env, "true");
764}
765
Martijn Coenen86c3abb2017-10-24 09:33:28 +0200766sp<::android::hidl::base::V1_0::IBase> getRawServiceInternal(const std::string& descriptor,
767 const std::string& instance,
768 bool retry, bool getStub) {
Steven Moreland84fe49e2019-08-20 17:47:26 +0000769 using Transport = IServiceManager1_0::Transport;
Martijn Coenend35678f2018-03-07 09:51:01 +0100770 sp<Waiter> waiter;
Martijn Coenen86c3abb2017-10-24 09:33:28 +0200771
Yifan Hongdeacf142018-07-10 17:33:34 -0700772 sp<IServiceManager1_1> sm;
773 Transport transport = Transport::EMPTY;
774 if (kIsRecovery) {
Yifan Hongdeacf142018-07-10 17:33:34 -0700775 transport = Transport::PASSTHROUGH;
Yifan Hongdeacf142018-07-10 17:33:34 -0700776 } else {
777 sm = defaultServiceManager1_1();
778 if (sm == nullptr) {
779 ALOGE("getService: defaultServiceManager() is null");
780 return nullptr;
781 }
782
783 Return<Transport> transportRet = sm->getTransport(descriptor, instance);
784
785 if (!transportRet.isOk()) {
786 ALOGE("getService: defaultServiceManager()->getTransport returns %s",
787 transportRet.description().c_str());
788 return nullptr;
789 }
790 transport = transportRet;
Martijn Coenen86c3abb2017-10-24 09:33:28 +0200791 }
792
Martijn Coenen86c3abb2017-10-24 09:33:28 +0200793 const bool vintfHwbinder = (transport == Transport::HWBINDER);
794 const bool vintfPassthru = (transport == Transport::PASSTHROUGH);
Steven Moreland84fe49e2019-08-20 17:47:26 +0000795 const bool trebleTestingOverride = isTrebleTestingOverride();
796 const bool allowLegacy = !kEnforceVintfManifest || (trebleTestingOverride && kDebuggable);
797 const bool vintfLegacy = (transport == Transport::EMPTY) && allowLegacy;
Martijn Coenen86c3abb2017-10-24 09:33:28 +0200798
Steven Moreland84fe49e2019-08-20 17:47:26 +0000799 if (!kEnforceVintfManifest) {
800 ALOGE("getService: Potential race detected. The VINTF manifest is not being enforced. If "
801 "a HAL server has a delay in starting and it is not in the manifest, it will not be "
802 "retrieved. Please make sure all HALs on this device are in the VINTF manifest and "
803 "enable PRODUCT_ENFORCE_VINTF_MANIFEST on this device (this is also enabled by "
804 "PRODUCT_FULL_TREBLE). PRODUCT_ENFORCE_VINTF_MANIFEST will ensure that no race "
805 "condition is possible here.");
806 }
Martijn Coenen86c3abb2017-10-24 09:33:28 +0200807
Steven Moreland76371242018-04-19 17:11:39 -0700808 for (int tries = 0; !getStub && (vintfHwbinder || vintfLegacy); tries++) {
809 if (waiter == nullptr && tries > 0) {
Martijn Coenend35678f2018-03-07 09:51:01 +0100810 waiter = new Waiter(descriptor, instance, sm);
811 }
Steven Moreland76371242018-04-19 17:11:39 -0700812 if (waiter != nullptr) {
813 waiter->reset(); // don't reorder this -- see comments on reset()
814 }
Martijn Coenen86c3abb2017-10-24 09:33:28 +0200815 Return<sp<IBase>> ret = sm->get(descriptor, instance);
816 if (!ret.isOk()) {
817 ALOGE("getService: defaultServiceManager()->get returns %s for %s/%s.",
818 ret.description().c_str(), descriptor.c_str(), instance.c_str());
819 break;
820 }
821 sp<IBase> base = ret;
Martijn Coenen773609e2017-10-24 09:57:53 +0200822 if (base != nullptr) {
823 Return<bool> canCastRet =
824 details::canCastInterface(base.get(), descriptor.c_str(), true /* emitError */);
825
826 if (canCastRet.isOk() && canCastRet) {
Steven Moreland76371242018-04-19 17:11:39 -0700827 if (waiter != nullptr) {
828 waiter->done();
829 }
Martijn Coenen773609e2017-10-24 09:57:53 +0200830 return base; // still needs to be wrapped by Bp class.
Martijn Coenen86c3abb2017-10-24 09:33:28 +0200831 }
Martijn Coenen773609e2017-10-24 09:57:53 +0200832
833 if (!handleCastError(canCastRet, descriptor, instance)) break;
Martijn Coenen86c3abb2017-10-24 09:33:28 +0200834 }
835
Martijn Coenen773609e2017-10-24 09:57:53 +0200836 // In case of legacy or we were not asked to retry, don't.
837 if (vintfLegacy || !retry) break;
Martijn Coenen86c3abb2017-10-24 09:33:28 +0200838
Steven Moreland76371242018-04-19 17:11:39 -0700839 if (waiter != nullptr) {
840 ALOGI("getService: Trying again for %s/%s...", descriptor.c_str(), instance.c_str());
Steven Moreland0771d8a2018-05-09 13:29:14 -0700841 waiter->wait(true /* timeout */);
Steven Moreland76371242018-04-19 17:11:39 -0700842 }
Martijn Coenen86c3abb2017-10-24 09:33:28 +0200843 }
844
Martijn Coenend35678f2018-03-07 09:51:01 +0100845 if (waiter != nullptr) {
846 waiter->done();
847 }
Martijn Coenen3fa15c22018-02-10 11:30:00 +0100848
Martijn Coenen86c3abb2017-10-24 09:33:28 +0200849 if (getStub || vintfPassthru || vintfLegacy) {
Steven Moreland84fe49e2019-08-20 17:47:26 +0000850 const sp<IServiceManager1_0> pm = getPassthroughServiceManager();
Martijn Coenen86c3abb2017-10-24 09:33:28 +0200851 if (pm != nullptr) {
852 sp<IBase> base = pm->get(descriptor, instance).withDefault(nullptr);
853 if (!getStub || trebleTestingOverride) {
854 base = wrapPassthrough(base);
855 }
856 return base;
857 }
858 }
859
860 return nullptr;
861}
862
Steven Morelandbb9eb2a2018-10-11 12:10:01 -0700863status_t registerAsServiceInternal(const sp<IBase>& service, const std::string& name) {
864 if (service == nullptr) {
865 return UNEXPECTED_NULL;
866 }
867
868 sp<IServiceManager1_2> sm = defaultServiceManager1_2();
869 if (sm == nullptr) {
870 return INVALID_OPERATION;
871 }
872
Steven Moreland84fe49e2019-08-20 17:47:26 +0000873 const std::string descriptor = getDescriptor(service.get());
874
875 if (kEnforceVintfManifest && !isTrebleTestingOverride()) {
876 using Transport = IServiceManager1_0::Transport;
877 Transport transport = sm->getTransport(descriptor, name);
878
879 if (transport != Transport::HWBINDER) {
880 LOG(ERROR) << "Service " << descriptor << "/" << name
881 << " must be in VINTF manifest in order to register/get.";
882 return UNKNOWN_ERROR;
883 }
884 }
885
Steven Morelandbb9eb2a2018-10-11 12:10:01 -0700886 bool registered = false;
887 Return<void> ret = service->interfaceChain([&](const auto& chain) {
888 registered = sm->addWithChain(name.c_str(), service, chain).withDefault(false);
889 });
890
891 if (!ret.isOk()) {
892 LOG(ERROR) << "Could not retrieve interface chain: " << ret.description();
893 }
894
895 if (registered) {
Steven Moreland84fe49e2019-08-20 17:47:26 +0000896 onRegistrationImpl(descriptor, name);
Steven Morelandbb9eb2a2018-10-11 12:10:01 -0700897 }
898
899 return registered ? OK : UNKNOWN_ERROR;
900}
901
Bernhard Rosenkränzer0c28fd22018-06-04 16:01:47 +0200902} // namespace details
Steven Morelandcbefd352017-01-23 20:29:05 -0800903
Bernhard Rosenkränzer0c28fd22018-06-04 16:01:47 +0200904} // namespace hardware
905} // namespace android