blob: a404d10369ba9b0c4b2bc52485e4e8f1c5d16824 [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
17#define LOG_TAG "ServiceManagement"
18
Jiyong Park3ab546c2017-04-06 20:28:07 +090019#include <android/dlext.h>
Yifan Hong9a22d1d2017-01-25 14:19:26 -080020#include <condition_variable>
21#include <dlfcn.h>
22#include <dirent.h>
Steven Moreland405d7612017-04-07 20:31:22 -070023#include <fstream>
24#include <pthread.h>
Yifan Hong9a22d1d2017-01-25 14:19:26 -080025#include <unistd.h>
26
27#include <mutex>
28#include <regex>
Yifan Hongbd0d8f72017-05-24 19:43:51 -070029#include <set>
Yifan Hong9a22d1d2017-01-25 14:19:26 -080030
Martijn Coenen12f04d92016-12-07 17:29:41 +010031#include <hidl/HidlBinderSupport.h>
Steven Moreland5d5ef7f2016-10-20 19:19:55 -070032#include <hidl/ServiceManagement.h>
Steven Moreland5d5ef7f2016-10-20 19:19:55 -070033#include <hidl/Status.h>
34
Steven Moreland337e6b62017-01-18 17:25:13 -080035#include <android-base/logging.h>
Steven Morelandc1cee2c2017-03-24 16:23:11 +000036#include <android-base/properties.h>
Steven Moreland5d5ef7f2016-10-20 19:19:55 -070037#include <hwbinder/IPCThreadState.h>
38#include <hwbinder/Parcel.h>
Steven Moreland5d5ef7f2016-10-20 19:19:55 -070039
Steven Moreland2a2678e2017-07-21 18:07:38 -070040#include <android/hidl/manager/1.1/IServiceManager.h>
41#include <android/hidl/manager/1.1/BpHwServiceManager.h>
42#include <android/hidl/manager/1.1/BnHwServiceManager.h>
Steven Moreland5d5ef7f2016-10-20 19:19:55 -070043
Yifan Hong9a22d1d2017-01-25 14:19:26 -080044#define RE_COMPONENT "[a-zA-Z_][a-zA-Z_0-9]*"
45#define RE_PATH RE_COMPONENT "(?:[.]" RE_COMPONENT ")*"
46static const std::regex gLibraryFileNamePattern("(" RE_PATH "@[0-9]+[.][0-9]+)-impl(.*?).so");
47
Jiyong Park3ab546c2017-04-06 20:28:07 +090048extern "C" {
49 android_namespace_t* android_get_exported_namespace(const char*);
50}
51
Steven Morelandc1cee2c2017-03-24 16:23:11 +000052using android::base::WaitForProperty;
53
Steven Moreland2a2678e2017-07-21 18:07:38 -070054using IServiceManager1_0 = android::hidl::manager::V1_0::IServiceManager;
55using IServiceManager1_1 = android::hidl::manager::V1_1::IServiceManager;
Steven Moreland337e6b62017-01-18 17:25:13 -080056using android::hidl::manager::V1_0::IServiceNotification;
Steven Moreland2a2678e2017-07-21 18:07:38 -070057using android::hidl::manager::V1_1::BpHwServiceManager;
58using android::hidl::manager::V1_1::BnHwServiceManager;
Steven Moreland5d5ef7f2016-10-20 19:19:55 -070059
60namespace android {
61namespace hardware {
62
Yifan Hong953e6b02017-03-16 14:52:40 -070063namespace details {
64extern Mutex gDefaultServiceManagerLock;
Steven Moreland2a2678e2017-07-21 18:07:38 -070065extern sp<android::hidl::manager::V1_1::IServiceManager> gDefaultServiceManager;
Yifan Hong953e6b02017-03-16 14:52:40 -070066} // namespace details
67
Steven Morelandc1cee2c2017-03-24 16:23:11 +000068static const char* kHwServicemanagerReadyProperty = "hwservicemanager.ready";
69
70void waitForHwServiceManager() {
71 using std::literals::chrono_literals::operator""s;
72
73 while (!WaitForProperty(kHwServicemanagerReadyProperty, "true", 1s)) {
74 LOG(WARNING) << "Waited for hwservicemanager.ready for a second, waiting another...";
75 }
76}
77
Steven Moreland405d7612017-04-07 20:31:22 -070078bool endsWith(const std::string &in, const std::string &suffix) {
79 return in.size() >= suffix.size() &&
80 in.substr(in.size() - suffix.size()) == suffix;
81}
Steven Moreland5d5ef7f2016-10-20 19:19:55 -070082
Steven Moreland405d7612017-04-07 20:31:22 -070083bool startsWith(const std::string &in, const std::string &prefix) {
84 return in.size() >= prefix.size() &&
85 in.substr(0, prefix.size()) == prefix;
86}
87
88std::string binaryName() {
89 std::ifstream ifs("/proc/self/cmdline");
90 std::string cmdline;
91 if (!ifs.is_open()) {
92 return "";
93 }
94 ifs >> cmdline;
95
96 size_t idx = cmdline.rfind("/");
97 if (idx != std::string::npos) {
98 cmdline = cmdline.substr(idx + 1);
99 }
100
101 return cmdline;
102}
103
104void tryShortenProcessName(const std::string &packageName) {
105 std::string processName = binaryName();
106
107 if (!startsWith(processName, packageName)) {
108 return;
109 }
110
111 // e.x. android.hardware.module.foo@1.0 -> foo@1.0
112 size_t lastDot = packageName.rfind('.');
113 size_t secondDot = packageName.rfind('.', lastDot - 1);
114
115 if (secondDot == std::string::npos) {
116 return;
117 }
118
119 std::string newName = processName.substr(secondDot + 1,
120 16 /* TASK_COMM_LEN */ - 1);
121 ALOGI("Removing namespace from process name %s to %s.",
122 processName.c_str(), newName.c_str());
123
124 int rc = pthread_setname_np(pthread_self(), newName.c_str());
125 ALOGI_IF(rc != 0, "Removing namespace from process name %s failed.",
126 processName.c_str());
127}
128
129namespace details {
130
131void onRegistration(const std::string &packageName,
132 const std::string& /* interfaceName */,
133 const std::string& /* instanceName */) {
134 tryShortenProcessName(packageName);
135}
136
137} // details
138
Steven Moreland2a2678e2017-07-21 18:07:38 -0700139sp<IServiceManager1_0> defaultServiceManager() {
140 return defaultServiceManager1_1();
141}
142sp<IServiceManager1_1> defaultServiceManager1_1() {
Steven Moreland5d5ef7f2016-10-20 19:19:55 -0700143 {
Yifan Hong953e6b02017-03-16 14:52:40 -0700144 AutoMutex _l(details::gDefaultServiceManagerLock);
145 if (details::gDefaultServiceManager != NULL) {
146 return details::gDefaultServiceManager;
Yifan Hong8fb656b2017-03-16 14:30:40 -0700147 }
Steven Moreland405d7612017-04-07 20:31:22 -0700148
Yifan Hong8fb656b2017-03-16 14:30:40 -0700149 if (access("/dev/hwbinder", F_OK|R_OK|W_OK) != 0) {
150 // HwBinder not available on this device or not accessible to
151 // this process.
152 return nullptr;
153 }
Steven Morelandc1cee2c2017-03-24 16:23:11 +0000154
155 waitForHwServiceManager();
156
Yifan Hong953e6b02017-03-16 14:52:40 -0700157 while (details::gDefaultServiceManager == NULL) {
158 details::gDefaultServiceManager =
Steven Moreland2a2678e2017-07-21 18:07:38 -0700159 fromBinder<IServiceManager1_1, BpHwServiceManager, BnHwServiceManager>(
Yifan Hong8fb656b2017-03-16 14:30:40 -0700160 ProcessState::self()->getContextObject(NULL));
Yifan Hong953e6b02017-03-16 14:52:40 -0700161 if (details::gDefaultServiceManager == NULL) {
Steven Morelandc1cee2c2017-03-24 16:23:11 +0000162 LOG(ERROR) << "Waited for hwservicemanager, but got nullptr.";
Steven Moreland5d5ef7f2016-10-20 19:19:55 -0700163 sleep(1);
Yifan Hong8fb656b2017-03-16 14:30:40 -0700164 }
Steven Moreland5d5ef7f2016-10-20 19:19:55 -0700165 }
166 }
167
Yifan Hong953e6b02017-03-16 14:52:40 -0700168 return details::gDefaultServiceManager;
Steven Moreland5d5ef7f2016-10-20 19:19:55 -0700169}
170
Steven Moreland0091c092017-01-20 23:15:18 +0000171std::vector<std::string> search(const std::string &path,
172 const std::string &prefix,
173 const std::string &suffix) {
174 std::unique_ptr<DIR, decltype(&closedir)> dir(opendir(path.c_str()), closedir);
175 if (!dir) return {};
176
177 std::vector<std::string> results{};
178
179 dirent* dp;
180 while ((dp = readdir(dir.get())) != nullptr) {
181 std::string name = dp->d_name;
182
Steven Moreland819c05d2017-04-06 17:24:22 -0700183 if (startsWith(name, prefix) &&
184 endsWith(name, suffix)) {
Steven Moreland0091c092017-01-20 23:15:18 +0000185 results.push_back(name);
186 }
187 }
188
189 return results;
190}
191
Yifan Hongbd0d8f72017-05-24 19:43:51 -0700192bool matchPackageName(const std::string& lib, std::string* matchedName, std::string* implName) {
Yifan Hong9a22d1d2017-01-25 14:19:26 -0800193 std::smatch match;
194 if (std::regex_match(lib, match, gLibraryFileNamePattern)) {
195 *matchedName = match.str(1) + "::I*";
Yifan Hongbd0d8f72017-05-24 19:43:51 -0700196 *implName = match.str(2);
Yifan Hong9a22d1d2017-01-25 14:19:26 -0800197 return true;
198 }
199 return false;
200}
201
Yifan Hong7f49f592017-02-03 15:11:44 -0800202static void registerReference(const hidl_string &interfaceName, const hidl_string &instanceName) {
Steven Moreland2a2678e2017-07-21 18:07:38 -0700203 sp<IServiceManager1_0> binderizedManager = defaultServiceManager();
Yifan Hong7f49f592017-02-03 15:11:44 -0800204 if (binderizedManager == nullptr) {
205 LOG(WARNING) << "Could not registerReference for "
206 << interfaceName << "/" << instanceName
207 << ": null binderized manager.";
208 return;
209 }
Martijn Coenenaf4aba52017-04-27 09:41:13 -0700210 auto ret = binderizedManager->registerPassthroughClient(interfaceName, instanceName);
Yifan Hong7f49f592017-02-03 15:11:44 -0800211 if (!ret.isOk()) {
212 LOG(WARNING) << "Could not registerReference for "
213 << interfaceName << "/" << instanceName
214 << ": " << ret.description();
Steven Moreland0aeaa782017-03-22 08:11:07 -0700215 return;
Yifan Hong7f49f592017-02-03 15:11:44 -0800216 }
Steven Morelande681aa52017-02-15 16:22:37 -0800217 LOG(VERBOSE) << "Successfully registerReference for "
218 << interfaceName << "/" << instanceName;
Yifan Hong7f49f592017-02-03 15:11:44 -0800219}
220
Yifan Hongbd0d8f72017-05-24 19:43:51 -0700221using InstanceDebugInfo = hidl::manager::V1_0::IServiceManager::InstanceDebugInfo;
222static inline void fetchPidsForPassthroughLibraries(
223 std::map<std::string, InstanceDebugInfo>* infos) {
224 static const std::string proc = "/proc/";
225
226 std::map<std::string, std::set<pid_t>> pids;
227 std::unique_ptr<DIR, decltype(&closedir)> dir(opendir(proc.c_str()), closedir);
228 if (!dir) return;
229 dirent* dp;
230 while ((dp = readdir(dir.get())) != nullptr) {
231 pid_t pid = strtoll(dp->d_name, NULL, 0);
232 if (pid == 0) continue;
233 std::string mapsPath = proc + dp->d_name + "/maps";
234 std::ifstream ifs{mapsPath};
235 if (!ifs.is_open()) continue;
236
237 for (std::string line; std::getline(ifs, line);) {
238 // The last token of line should look like
239 // vendor/lib64/hw/android.hardware.foo@1.0-impl-extra.so
240 // Use some simple filters to ignore bad lines before extracting libFileName
241 // and checking the key in info to make parsing faster.
242 if (line.back() != 'o') continue;
243 if (line.rfind('@') == std::string::npos) continue;
244
245 auto spacePos = line.rfind(' ');
246 if (spacePos == std::string::npos) continue;
247 auto libFileName = line.substr(spacePos + 1);
248 auto it = infos->find(libFileName);
249 if (it == infos->end()) continue;
250 pids[libFileName].insert(pid);
251 }
252 }
253 for (auto& pair : *infos) {
254 pair.second.clientPids =
255 std::vector<pid_t>{pids[pair.first].begin(), pids[pair.first].end()};
256 }
257}
258
Steven Moreland2a2678e2017-07-21 18:07:38 -0700259struct PassthroughServiceManager : IServiceManager1_1 {
Steven Moreland519306f2017-06-06 17:14:12 -0700260 static void openLibs(const std::string& fqName,
261 std::function<bool /* continue */(void* /* handle */,
262 const std::string& /* lib */, const std::string& /* sym */)> eachLib) {
Steven Moreland819c05d2017-04-06 17:24:22 -0700263 //fqName looks like android.hardware.foo@1.0::IFoo
Steven Moreland519306f2017-06-06 17:14:12 -0700264 size_t idx = fqName.find("::");
Steven Moreland819c05d2017-04-06 17:24:22 -0700265
266 if (idx == std::string::npos ||
Steven Moreland519306f2017-06-06 17:14:12 -0700267 idx + strlen("::") + 1 >= fqName.size()) {
Steven Moreland337e6b62017-01-18 17:25:13 -0800268 LOG(ERROR) << "Invalid interface name passthrough lookup: " << fqName;
Steven Moreland519306f2017-06-06 17:14:12 -0700269 return;
Steven Moreland337e6b62017-01-18 17:25:13 -0800270 }
271
Steven Moreland519306f2017-06-06 17:14:12 -0700272 std::string packageAndVersion = fqName.substr(0, idx);
273 std::string ifaceName = fqName.substr(idx + strlen("::"));
Steven Moreland819c05d2017-04-06 17:24:22 -0700274
275 const std::string prefix = packageAndVersion + "-impl";
276 const std::string sym = "HIDL_FETCH_" + ifaceName;
Steven Moreland348802d2017-02-23 12:48:55 -0800277
Jiyong Park3ab546c2017-04-06 20:28:07 +0900278 const android_namespace_t* sphal_namespace = android_get_exported_namespace("sphal");
Steven Moreland337e6b62017-01-18 17:25:13 -0800279 const int dlMode = RTLD_LAZY;
280 void *handle = nullptr;
281
Steven Morelanda29905c2017-03-01 10:42:35 -0800282 dlerror(); // clear
283
Steven Morelandf7dea692017-07-14 12:49:28 -0700284 std::vector<std::string> paths = {HAL_LIBRARY_PATH_ODM, HAL_LIBRARY_PATH_VENDOR,
285 HAL_LIBRARY_PATH_SYSTEM};
286#ifdef LIBHIDL_TARGET_DEBUGGABLE
287 const char* env = std::getenv("TREBLE_TESTING_OVERRIDE");
288 const bool trebleTestingOverride = env && !strcmp(env, "true");
289 if (trebleTestingOverride) {
290 const char* vtsRootPath = std::getenv("VTS_ROOT_PATH");
291 if (vtsRootPath && strlen(vtsRootPath) > 0) {
292 const std::string halLibraryPathVtsOverride =
293 std::string(vtsRootPath) + HAL_LIBRARY_PATH_SYSTEM;
294 paths.push_back(halLibraryPathVtsOverride);
295 }
296 }
297#endif
298 for (const std::string& path : paths) {
Steven Moreland0091c092017-01-20 23:15:18 +0000299 std::vector<std::string> libs = search(path, prefix, ".so");
300
Steven Moreland0091c092017-01-20 23:15:18 +0000301 for (const std::string &lib : libs) {
Steven Moreland348802d2017-02-23 12:48:55 -0800302 const std::string fullPath = path + lib;
303
Jiyong Park3ab546c2017-04-06 20:28:07 +0900304 // If sphal namespace is available, try to load from the
305 // namespace first. If it fails, fall back to the original
306 // dlopen, which loads from the current namespace.
307 if (sphal_namespace != nullptr && path != HAL_LIBRARY_PATH_SYSTEM) {
308 const android_dlextinfo dlextinfo = {
309 .flags = ANDROID_DLEXT_USE_NAMESPACE,
310 // const_cast is dirty but required because
311 // library_namespace field is non-const.
312 .library_namespace = const_cast<android_namespace_t*>(sphal_namespace),
313 };
314 handle = android_dlopen_ext(fullPath.c_str(), dlMode, &dlextinfo);
315 if (handle == nullptr) {
316 const char* error = dlerror();
317 LOG(WARNING) << "Failed to dlopen " << lib << " from sphal namespace:"
318 << (error == nullptr ? "unknown error" : error);
319 } else {
320 LOG(DEBUG) << lib << " loaded from sphal namespace.";
321 }
322 }
323 if (handle == nullptr) {
324 handle = dlopen(fullPath.c_str(), dlMode);
325 }
326
Steven Moreland348802d2017-02-23 12:48:55 -0800327 if (handle == nullptr) {
328 const char* error = dlerror();
329 LOG(ERROR) << "Failed to dlopen " << lib << ": "
330 << (error == nullptr ? "unknown error" : error);
331 continue;
Steven Moreland0091c092017-01-20 23:15:18 +0000332 }
Steven Moreland348802d2017-02-23 12:48:55 -0800333
Steven Moreland519306f2017-06-06 17:14:12 -0700334 if (!eachLib(handle, lib, sym)) {
335 return;
Steven Moreland348802d2017-02-23 12:48:55 -0800336 }
Steven Moreland337e6b62017-01-18 17:25:13 -0800337 }
338 }
Steven Moreland519306f2017-06-06 17:14:12 -0700339 }
Steven Moreland337e6b62017-01-18 17:25:13 -0800340
Steven Moreland519306f2017-06-06 17:14:12 -0700341 Return<sp<IBase>> get(const hidl_string& fqName,
342 const hidl_string& name) override {
343 sp<IBase> ret = nullptr;
344
345 openLibs(fqName, [&](void* handle, const std::string &lib, const std::string &sym) {
346 IBase* (*generator)(const char* name);
347 *(void **)(&generator) = dlsym(handle, sym.c_str());
348 if(!generator) {
349 const char* error = dlerror();
350 LOG(ERROR) << "Passthrough lookup opened " << lib
351 << " but could not find symbol " << sym << ": "
352 << (error == nullptr ? "unknown error" : error);
353 dlclose(handle);
354 return true;
355 }
356
357 ret = (*generator)(name.c_str());
358
359 if (ret == nullptr) {
360 dlclose(handle);
361 return true; // this module doesn't provide this instance name
362 }
363
364 registerReference(fqName, name);
365 return false;
366 });
367
368 return ret;
Steven Moreland337e6b62017-01-18 17:25:13 -0800369 }
370
Martijn Coenen67a02492017-03-06 13:04:48 +0100371 Return<bool> add(const hidl_string& /* name */,
Steven Moreland337e6b62017-01-18 17:25:13 -0800372 const sp<IBase>& /* service */) override {
373 LOG(FATAL) << "Cannot register services with passthrough service manager.";
374 return false;
375 }
376
Steven Morelandc601c5f2017-04-06 09:26:07 -0700377 Return<Transport> getTransport(const hidl_string& /* fqName */,
378 const hidl_string& /* name */) {
379 LOG(FATAL) << "Cannot getTransport with passthrough service manager.";
380 return Transport::EMPTY;
381 }
382
Yifan Hong705e5da2017-03-02 16:59:39 -0800383 Return<void> list(list_cb /* _hidl_cb */) override {
384 LOG(FATAL) << "Cannot list services with passthrough service manager.";
Steven Moreland337e6b62017-01-18 17:25:13 -0800385 return Void();
386 }
387 Return<void> listByInterface(const hidl_string& /* fqInstanceName */,
388 listByInterface_cb /* _hidl_cb */) override {
389 // TODO: add this functionality
390 LOG(FATAL) << "Cannot list services with passthrough service manager.";
391 return Void();
392 }
393
394 Return<bool> registerForNotifications(const hidl_string& /* fqName */,
395 const hidl_string& /* name */,
396 const sp<IServiceNotification>& /* callback */) override {
397 // This makes no sense.
398 LOG(FATAL) << "Cannot register for notifications with passthrough service manager.";
399 return false;
400 }
401
Yifan Hong705e5da2017-03-02 16:59:39 -0800402 Return<void> debugDump(debugDump_cb _hidl_cb) override {
403 using Arch = ::android::hidl::base::V1_0::DebugInfo::Architecture;
Yifan Hongbd0d8f72017-05-24 19:43:51 -0700404 using std::literals::string_literals::operator""s;
Yifan Hong705e5da2017-03-02 16:59:39 -0800405 static std::vector<std::pair<Arch, std::vector<const char *>>> sAllPaths{
406 {Arch::IS_64BIT, {HAL_LIBRARY_PATH_ODM_64BIT,
407 HAL_LIBRARY_PATH_VENDOR_64BIT,
408 HAL_LIBRARY_PATH_SYSTEM_64BIT}},
409 {Arch::IS_32BIT, {HAL_LIBRARY_PATH_ODM_32BIT,
410 HAL_LIBRARY_PATH_VENDOR_32BIT,
411 HAL_LIBRARY_PATH_SYSTEM_32BIT}}
412 };
Yifan Hongbd0d8f72017-05-24 19:43:51 -0700413 std::map<std::string, InstanceDebugInfo> map;
Yifan Hong705e5da2017-03-02 16:59:39 -0800414 for (const auto &pair : sAllPaths) {
415 Arch arch = pair.first;
416 for (const auto &path : pair.second) {
417 std::vector<std::string> libs = search(path, "", ".so");
418 for (const std::string &lib : libs) {
419 std::string matchedName;
Yifan Hongbd0d8f72017-05-24 19:43:51 -0700420 std::string implName;
421 if (matchPackageName(lib, &matchedName, &implName)) {
422 std::string instanceName{"* ("s + path + ")"s};
423 if (!implName.empty()) instanceName += " ("s + implName + ")"s;
424 map.emplace(path + lib, InstanceDebugInfo{.interfaceName = matchedName,
425 .instanceName = instanceName,
426 .clientPids = {},
427 .arch = arch});
Yifan Hong705e5da2017-03-02 16:59:39 -0800428 }
429 }
430 }
431 }
Yifan Hongbd0d8f72017-05-24 19:43:51 -0700432 fetchPidsForPassthroughLibraries(&map);
433 hidl_vec<InstanceDebugInfo> vec;
434 vec.resize(map.size());
435 size_t idx = 0;
436 for (auto&& pair : map) {
437 vec[idx++] = std::move(pair.second);
438 }
Yifan Hong705e5da2017-03-02 16:59:39 -0800439 _hidl_cb(vec);
Yifan Hong7f49f592017-02-03 15:11:44 -0800440 return Void();
441 }
442
Martijn Coenenaf4aba52017-04-27 09:41:13 -0700443 Return<void> registerPassthroughClient(const hidl_string &, const hidl_string &) override {
Yifan Hong7f49f592017-02-03 15:11:44 -0800444 // This makes no sense.
445 LOG(FATAL) << "Cannot call registerPassthroughClient on passthrough service manager. "
446 << "Call it on defaultServiceManager() instead.";
Yifan Hong9a22d1d2017-01-25 14:19:26 -0800447 return Void();
448 }
449
Steven Moreland2a2678e2017-07-21 18:07:38 -0700450 Return<bool> unregisterForNotifications(const hidl_string& /* fqName */,
451 const hidl_string& /* name */,
452 const sp<IServiceNotification>& /* callback */) override {
453 // This makes no sense.
454 LOG(FATAL) << "Cannot unregister for notifications with passthrough service manager.";
455 return false;
456 }
457
Steven Moreland337e6b62017-01-18 17:25:13 -0800458};
459
Steven Moreland2a2678e2017-07-21 18:07:38 -0700460sp<IServiceManager1_0> getPassthroughServiceManager() {
461 return getPassthroughServiceManager1_1();
462}
463sp<IServiceManager1_1> getPassthroughServiceManager1_1() {
Steven Moreland337e6b62017-01-18 17:25:13 -0800464 static sp<PassthroughServiceManager> manager(new PassthroughServiceManager());
465 return manager;
466}
467
Steven Morelandcbefd352017-01-23 20:29:05 -0800468namespace details {
469
Steven Moreland519306f2017-06-06 17:14:12 -0700470void preloadPassthroughService(const std::string &descriptor) {
471 PassthroughServiceManager::openLibs(descriptor,
472 [&](void* /* handle */, const std::string& /* lib */, const std::string& /* sym */) {
473 // do nothing
474 return true; // open all libs
475 });
476}
477
Steven Morelandcbefd352017-01-23 20:29:05 -0800478struct Waiter : IServiceNotification {
479 Return<void> onRegistration(const hidl_string& /* fqName */,
480 const hidl_string& /* name */,
481 bool /* preexisting */) override {
482 std::unique_lock<std::mutex> lock(mMutex);
483 if (mRegistered) {
484 return Void();
485 }
486 mRegistered = true;
487 lock.unlock();
488
489 mCondition.notify_one();
490 return Void();
491 }
492
Steven Moreland49605102017-03-28 09:33:06 -0700493 void wait(const std::string &interface, const std::string &instanceName) {
494 using std::literals::chrono_literals::operator""s;
495
Steven Morelandcbefd352017-01-23 20:29:05 -0800496 std::unique_lock<std::mutex> lock(mMutex);
Steven Moreland49605102017-03-28 09:33:06 -0700497 while(true) {
498 mCondition.wait_for(lock, 1s, [this]{
499 return mRegistered;
500 });
501
502 if (mRegistered) {
503 break;
504 }
505
506 LOG(WARNING) << "Waited one second for "
507 << interface << "/" << instanceName
508 << ". Waiting another...";
509 }
Steven Morelandcbefd352017-01-23 20:29:05 -0800510 }
511
512private:
513 std::mutex mMutex;
514 std::condition_variable mCondition;
515 bool mRegistered = false;
516};
517
518void waitForHwService(
519 const std::string &interface, const std::string &instanceName) {
Steven Moreland2a2678e2017-07-21 18:07:38 -0700520 const sp<IServiceManager1_1> manager = defaultServiceManager1_1();
Steven Morelandcbefd352017-01-23 20:29:05 -0800521
522 if (manager == nullptr) {
523 LOG(ERROR) << "Could not get default service manager.";
524 return;
525 }
526
527 sp<Waiter> waiter = new Waiter();
528 Return<bool> ret = manager->registerForNotifications(interface, instanceName, waiter);
529
530 if (!ret.isOk()) {
531 LOG(ERROR) << "Transport error, " << ret.description()
532 << ", during notification registration for "
533 << interface << "/" << instanceName << ".";
534 return;
535 }
536
537 if (!ret) {
538 LOG(ERROR) << "Could not register for notifications for "
539 << interface << "/" << instanceName << ".";
540 return;
541 }
542
Steven Moreland49605102017-03-28 09:33:06 -0700543 waiter->wait(interface, instanceName);
Steven Moreland2a2678e2017-07-21 18:07:38 -0700544
545 if (!manager->unregisterForNotifications(interface, instanceName, waiter).withDefault(false)) {
546 LOG(ERROR) << "Could not unregister service notification for "
547 << interface << "/" << instanceName << ".";
548 }
Steven Morelandcbefd352017-01-23 20:29:05 -0800549}
550
551}; // namespace details
552
Steven Moreland5d5ef7f2016-10-20 19:19:55 -0700553}; // namespace hardware
554}; // namespace android