blob: c19ef584f91acaaee9ae1afa5bf8ed23d72af868 [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
Yifan Hong9a22d1d2017-01-25 14:19:26 -080019#include <condition_variable>
20#include <dlfcn.h>
21#include <dirent.h>
Steven Morelandcd4dbdf2017-04-07 20:31:22 -070022#include <fstream>
23#include <pthread.h>
Yifan Hong9a22d1d2017-01-25 14:19:26 -080024#include <unistd.h>
25
26#include <mutex>
27#include <regex>
Yifan Hongbd0d8f72017-05-24 19:43:51 -070028#include <set>
Yifan Hong9a22d1d2017-01-25 14:19:26 -080029
Martijn Coenen12f04d92016-12-07 17:29:41 +010030#include <hidl/HidlBinderSupport.h>
Steven Moreland5d5ef7f2016-10-20 19:19:55 -070031#include <hidl/ServiceManagement.h>
Steven Moreland5d5ef7f2016-10-20 19:19:55 -070032#include <hidl/Status.h>
33
Steven Moreland337e6b62017-01-18 17:25:13 -080034#include <android-base/logging.h>
Steven Morelandc1cee2c2017-03-24 16:23:11 +000035#include <android-base/properties.h>
Steven Moreland5d5ef7f2016-10-20 19:19:55 -070036#include <hwbinder/IPCThreadState.h>
37#include <hwbinder/Parcel.h>
Steven Moreland5d5ef7f2016-10-20 19:19:55 -070038
Steven Moreland2a2678e2017-07-21 18:07:38 -070039#include <android/hidl/manager/1.1/IServiceManager.h>
40#include <android/hidl/manager/1.1/BpHwServiceManager.h>
41#include <android/hidl/manager/1.1/BnHwServiceManager.h>
Steven Moreland5d5ef7f2016-10-20 19:19:55 -070042
Yifan Hong9a22d1d2017-01-25 14:19:26 -080043#define RE_COMPONENT "[a-zA-Z_][a-zA-Z_0-9]*"
44#define RE_PATH RE_COMPONENT "(?:[.]" RE_COMPONENT ")*"
45static const std::regex gLibraryFileNamePattern("(" RE_PATH "@[0-9]+[.][0-9]+)-impl(.*?).so");
46
Steven Morelandc1cee2c2017-03-24 16:23:11 +000047using android::base::WaitForProperty;
48
Steven Moreland2a2678e2017-07-21 18:07:38 -070049using IServiceManager1_0 = android::hidl::manager::V1_0::IServiceManager;
50using IServiceManager1_1 = android::hidl::manager::V1_1::IServiceManager;
Steven Moreland337e6b62017-01-18 17:25:13 -080051using android::hidl::manager::V1_0::IServiceNotification;
Steven Moreland2a2678e2017-07-21 18:07:38 -070052using android::hidl::manager::V1_1::BpHwServiceManager;
53using android::hidl::manager::V1_1::BnHwServiceManager;
Steven Moreland5d5ef7f2016-10-20 19:19:55 -070054
55namespace android {
56namespace hardware {
57
Yifan Hong953e6b02017-03-16 14:52:40 -070058namespace details {
59extern Mutex gDefaultServiceManagerLock;
Steven Moreland2a2678e2017-07-21 18:07:38 -070060extern sp<android::hidl::manager::V1_1::IServiceManager> gDefaultServiceManager;
Yifan Hong953e6b02017-03-16 14:52:40 -070061} // namespace details
62
Steven Morelandc1cee2c2017-03-24 16:23:11 +000063static const char* kHwServicemanagerReadyProperty = "hwservicemanager.ready";
64
65void waitForHwServiceManager() {
66 using std::literals::chrono_literals::operator""s;
67
68 while (!WaitForProperty(kHwServicemanagerReadyProperty, "true", 1s)) {
69 LOG(WARNING) << "Waited for hwservicemanager.ready for a second, waiting another...";
70 }
71}
72
Steven Morelandcd4dbdf2017-04-07 20:31:22 -070073bool endsWith(const std::string &in, const std::string &suffix) {
74 return in.size() >= suffix.size() &&
75 in.substr(in.size() - suffix.size()) == suffix;
76}
Steven Moreland5d5ef7f2016-10-20 19:19:55 -070077
Steven Morelandcd4dbdf2017-04-07 20:31:22 -070078bool startsWith(const std::string &in, const std::string &prefix) {
79 return in.size() >= prefix.size() &&
80 in.substr(0, prefix.size()) == prefix;
81}
82
83std::string binaryName() {
84 std::ifstream ifs("/proc/self/cmdline");
85 std::string cmdline;
86 if (!ifs.is_open()) {
87 return "";
88 }
89 ifs >> cmdline;
90
91 size_t idx = cmdline.rfind("/");
92 if (idx != std::string::npos) {
93 cmdline = cmdline.substr(idx + 1);
94 }
95
96 return cmdline;
97}
98
99void tryShortenProcessName(const std::string &packageName) {
100 std::string processName = binaryName();
101
102 if (!startsWith(processName, packageName)) {
103 return;
104 }
105
106 // e.x. android.hardware.module.foo@1.0 -> foo@1.0
107 size_t lastDot = packageName.rfind('.');
108 size_t secondDot = packageName.rfind('.', lastDot - 1);
109
110 if (secondDot == std::string::npos) {
111 return;
112 }
113
114 std::string newName = processName.substr(secondDot + 1,
115 16 /* TASK_COMM_LEN */ - 1);
116 ALOGI("Removing namespace from process name %s to %s.",
117 processName.c_str(), newName.c_str());
118
119 int rc = pthread_setname_np(pthread_self(), newName.c_str());
120 ALOGI_IF(rc != 0, "Removing namespace from process name %s failed.",
121 processName.c_str());
122}
123
124namespace details {
125
126void onRegistration(const std::string &packageName,
127 const std::string& /* interfaceName */,
128 const std::string& /* instanceName */) {
129 tryShortenProcessName(packageName);
130}
131
132} // details
133
Steven Moreland2a2678e2017-07-21 18:07:38 -0700134sp<IServiceManager1_0> defaultServiceManager() {
135 return defaultServiceManager1_1();
136}
137sp<IServiceManager1_1> defaultServiceManager1_1() {
Steven Moreland5d5ef7f2016-10-20 19:19:55 -0700138 {
Yifan Hong953e6b02017-03-16 14:52:40 -0700139 AutoMutex _l(details::gDefaultServiceManagerLock);
140 if (details::gDefaultServiceManager != NULL) {
141 return details::gDefaultServiceManager;
Yifan Hong8fb656b2017-03-16 14:30:40 -0700142 }
Steven Morelandcd4dbdf2017-04-07 20:31:22 -0700143
Yifan Hong8fb656b2017-03-16 14:30:40 -0700144 if (access("/dev/hwbinder", F_OK|R_OK|W_OK) != 0) {
145 // HwBinder not available on this device or not accessible to
146 // this process.
147 return nullptr;
148 }
Steven Morelandc1cee2c2017-03-24 16:23:11 +0000149
150 waitForHwServiceManager();
151
Yifan Hong953e6b02017-03-16 14:52:40 -0700152 while (details::gDefaultServiceManager == NULL) {
153 details::gDefaultServiceManager =
Steven Moreland2a2678e2017-07-21 18:07:38 -0700154 fromBinder<IServiceManager1_1, BpHwServiceManager, BnHwServiceManager>(
Yifan Hong8fb656b2017-03-16 14:30:40 -0700155 ProcessState::self()->getContextObject(NULL));
Yifan Hong953e6b02017-03-16 14:52:40 -0700156 if (details::gDefaultServiceManager == NULL) {
Steven Morelandc1cee2c2017-03-24 16:23:11 +0000157 LOG(ERROR) << "Waited for hwservicemanager, but got nullptr.";
Steven Moreland5d5ef7f2016-10-20 19:19:55 -0700158 sleep(1);
Yifan Hong8fb656b2017-03-16 14:30:40 -0700159 }
Steven Moreland5d5ef7f2016-10-20 19:19:55 -0700160 }
161 }
162
Yifan Hong953e6b02017-03-16 14:52:40 -0700163 return details::gDefaultServiceManager;
Steven Moreland5d5ef7f2016-10-20 19:19:55 -0700164}
165
Steven Moreland0091c092017-01-20 23:15:18 +0000166std::vector<std::string> search(const std::string &path,
167 const std::string &prefix,
168 const std::string &suffix) {
169 std::unique_ptr<DIR, decltype(&closedir)> dir(opendir(path.c_str()), closedir);
170 if (!dir) return {};
171
172 std::vector<std::string> results{};
173
174 dirent* dp;
175 while ((dp = readdir(dir.get())) != nullptr) {
176 std::string name = dp->d_name;
177
Steven Morelandda8e6172017-04-06 17:24:22 -0700178 if (startsWith(name, prefix) &&
179 endsWith(name, suffix)) {
Steven Moreland0091c092017-01-20 23:15:18 +0000180 results.push_back(name);
181 }
182 }
183
184 return results;
185}
186
Yifan Hongbd0d8f72017-05-24 19:43:51 -0700187bool matchPackageName(const std::string& lib, std::string* matchedName, std::string* implName) {
Yifan Hong9a22d1d2017-01-25 14:19:26 -0800188 std::smatch match;
189 if (std::regex_match(lib, match, gLibraryFileNamePattern)) {
190 *matchedName = match.str(1) + "::I*";
Yifan Hongbd0d8f72017-05-24 19:43:51 -0700191 *implName = match.str(2);
Yifan Hong9a22d1d2017-01-25 14:19:26 -0800192 return true;
193 }
194 return false;
195}
196
Yifan Hong7f49f592017-02-03 15:11:44 -0800197static void registerReference(const hidl_string &interfaceName, const hidl_string &instanceName) {
Steven Moreland2a2678e2017-07-21 18:07:38 -0700198 sp<IServiceManager1_0> binderizedManager = defaultServiceManager();
Yifan Hong7f49f592017-02-03 15:11:44 -0800199 if (binderizedManager == nullptr) {
200 LOG(WARNING) << "Could not registerReference for "
201 << interfaceName << "/" << instanceName
202 << ": null binderized manager.";
203 return;
204 }
Martijn Coenenbf13ad02017-04-27 09:41:13 -0700205 auto ret = binderizedManager->registerPassthroughClient(interfaceName, instanceName);
Yifan Hong7f49f592017-02-03 15:11:44 -0800206 if (!ret.isOk()) {
207 LOG(WARNING) << "Could not registerReference for "
208 << interfaceName << "/" << instanceName
209 << ": " << ret.description();
Steven Moreland0aeaa782017-03-22 08:11:07 -0700210 return;
Yifan Hong7f49f592017-02-03 15:11:44 -0800211 }
Steven Morelande681aa52017-02-15 16:22:37 -0800212 LOG(VERBOSE) << "Successfully registerReference for "
213 << interfaceName << "/" << instanceName;
Yifan Hong7f49f592017-02-03 15:11:44 -0800214}
215
Yifan Hongbd0d8f72017-05-24 19:43:51 -0700216using InstanceDebugInfo = hidl::manager::V1_0::IServiceManager::InstanceDebugInfo;
217static inline void fetchPidsForPassthroughLibraries(
218 std::map<std::string, InstanceDebugInfo>* infos) {
219 static const std::string proc = "/proc/";
220
221 std::map<std::string, std::set<pid_t>> pids;
222 std::unique_ptr<DIR, decltype(&closedir)> dir(opendir(proc.c_str()), closedir);
223 if (!dir) return;
224 dirent* dp;
225 while ((dp = readdir(dir.get())) != nullptr) {
226 pid_t pid = strtoll(dp->d_name, NULL, 0);
227 if (pid == 0) continue;
228 std::string mapsPath = proc + dp->d_name + "/maps";
229 std::ifstream ifs{mapsPath};
230 if (!ifs.is_open()) continue;
231
232 for (std::string line; std::getline(ifs, line);) {
233 // The last token of line should look like
234 // vendor/lib64/hw/android.hardware.foo@1.0-impl-extra.so
235 // Use some simple filters to ignore bad lines before extracting libFileName
236 // and checking the key in info to make parsing faster.
237 if (line.back() != 'o') continue;
238 if (line.rfind('@') == std::string::npos) continue;
239
240 auto spacePos = line.rfind(' ');
241 if (spacePos == std::string::npos) continue;
242 auto libFileName = line.substr(spacePos + 1);
243 auto it = infos->find(libFileName);
244 if (it == infos->end()) continue;
245 pids[libFileName].insert(pid);
246 }
247 }
248 for (auto& pair : *infos) {
249 pair.second.clientPids =
250 std::vector<pid_t>{pids[pair.first].begin(), pids[pair.first].end()};
251 }
252}
253
Steven Moreland2a2678e2017-07-21 18:07:38 -0700254struct PassthroughServiceManager : IServiceManager1_1 {
Steven Moreland519306f2017-06-06 17:14:12 -0700255 static void openLibs(const std::string& fqName,
256 std::function<bool /* continue */(void* /* handle */,
257 const std::string& /* lib */, const std::string& /* sym */)> eachLib) {
Steven Morelandda8e6172017-04-06 17:24:22 -0700258 //fqName looks like android.hardware.foo@1.0::IFoo
Steven Moreland519306f2017-06-06 17:14:12 -0700259 size_t idx = fqName.find("::");
Steven Morelandda8e6172017-04-06 17:24:22 -0700260
261 if (idx == std::string::npos ||
Steven Moreland519306f2017-06-06 17:14:12 -0700262 idx + strlen("::") + 1 >= fqName.size()) {
Steven Moreland337e6b62017-01-18 17:25:13 -0800263 LOG(ERROR) << "Invalid interface name passthrough lookup: " << fqName;
Steven Moreland519306f2017-06-06 17:14:12 -0700264 return;
Steven Moreland337e6b62017-01-18 17:25:13 -0800265 }
266
Steven Moreland519306f2017-06-06 17:14:12 -0700267 std::string packageAndVersion = fqName.substr(0, idx);
268 std::string ifaceName = fqName.substr(idx + strlen("::"));
Steven Morelandda8e6172017-04-06 17:24:22 -0700269
270 const std::string prefix = packageAndVersion + "-impl";
271 const std::string sym = "HIDL_FETCH_" + ifaceName;
Steven Moreland348802d2017-02-23 12:48:55 -0800272
Steven Moreland337e6b62017-01-18 17:25:13 -0800273 const int dlMode = RTLD_LAZY;
274 void *handle = nullptr;
275
Steven Morelanda29905c2017-03-01 10:42:35 -0800276 dlerror(); // clear
277
Steven Morelandf7dea692017-07-14 12:49:28 -0700278 std::vector<std::string> paths = {HAL_LIBRARY_PATH_ODM, HAL_LIBRARY_PATH_VENDOR,
279 HAL_LIBRARY_PATH_SYSTEM};
280#ifdef LIBHIDL_TARGET_DEBUGGABLE
281 const char* env = std::getenv("TREBLE_TESTING_OVERRIDE");
282 const bool trebleTestingOverride = env && !strcmp(env, "true");
283 if (trebleTestingOverride) {
284 const char* vtsRootPath = std::getenv("VTS_ROOT_PATH");
285 if (vtsRootPath && strlen(vtsRootPath) > 0) {
286 const std::string halLibraryPathVtsOverride =
287 std::string(vtsRootPath) + HAL_LIBRARY_PATH_SYSTEM;
288 paths.push_back(halLibraryPathVtsOverride);
289 }
290 }
291#endif
292 for (const std::string& path : paths) {
Steven Moreland0091c092017-01-20 23:15:18 +0000293 std::vector<std::string> libs = search(path, prefix, ".so");
294
Steven Moreland0091c092017-01-20 23:15:18 +0000295 for (const std::string &lib : libs) {
Steven Moreland348802d2017-02-23 12:48:55 -0800296 const std::string fullPath = path + lib;
297
298 handle = dlopen(fullPath.c_str(), dlMode);
299 if (handle == nullptr) {
300 const char* error = dlerror();
301 LOG(ERROR) << "Failed to dlopen " << lib << ": "
302 << (error == nullptr ? "unknown error" : error);
303 continue;
Steven Moreland0091c092017-01-20 23:15:18 +0000304 }
Steven Moreland348802d2017-02-23 12:48:55 -0800305
Steven Moreland519306f2017-06-06 17:14:12 -0700306 if (!eachLib(handle, lib, sym)) {
307 return;
Steven Moreland348802d2017-02-23 12:48:55 -0800308 }
Steven Moreland337e6b62017-01-18 17:25:13 -0800309 }
310 }
Steven Moreland519306f2017-06-06 17:14:12 -0700311 }
Steven Moreland337e6b62017-01-18 17:25:13 -0800312
Steven Moreland519306f2017-06-06 17:14:12 -0700313 Return<sp<IBase>> get(const hidl_string& fqName,
314 const hidl_string& name) override {
315 sp<IBase> ret = nullptr;
316
317 openLibs(fqName, [&](void* handle, const std::string &lib, const std::string &sym) {
318 IBase* (*generator)(const char* name);
319 *(void **)(&generator) = dlsym(handle, sym.c_str());
320 if(!generator) {
321 const char* error = dlerror();
322 LOG(ERROR) << "Passthrough lookup opened " << lib
323 << " but could not find symbol " << sym << ": "
324 << (error == nullptr ? "unknown error" : error);
325 dlclose(handle);
326 return true;
327 }
328
329 ret = (*generator)(name.c_str());
330
331 if (ret == nullptr) {
332 dlclose(handle);
333 return true; // this module doesn't provide this instance name
334 }
335
336 registerReference(fqName, name);
337 return false;
338 });
339
340 return ret;
Steven Moreland337e6b62017-01-18 17:25:13 -0800341 }
342
Martijn Coenen67a02492017-03-06 13:04:48 +0100343 Return<bool> add(const hidl_string& /* name */,
Steven Moreland337e6b62017-01-18 17:25:13 -0800344 const sp<IBase>& /* service */) override {
345 LOG(FATAL) << "Cannot register services with passthrough service manager.";
346 return false;
347 }
348
Steven Moreland330e3e22017-04-06 09:26:07 -0700349 Return<Transport> getTransport(const hidl_string& /* fqName */,
350 const hidl_string& /* name */) {
351 LOG(FATAL) << "Cannot getTransport with passthrough service manager.";
352 return Transport::EMPTY;
353 }
354
Yifan Hong705e5da2017-03-02 16:59:39 -0800355 Return<void> list(list_cb /* _hidl_cb */) override {
356 LOG(FATAL) << "Cannot list services with passthrough service manager.";
Steven Moreland337e6b62017-01-18 17:25:13 -0800357 return Void();
358 }
359 Return<void> listByInterface(const hidl_string& /* fqInstanceName */,
360 listByInterface_cb /* _hidl_cb */) override {
361 // TODO: add this functionality
362 LOG(FATAL) << "Cannot list services with passthrough service manager.";
363 return Void();
364 }
365
366 Return<bool> registerForNotifications(const hidl_string& /* fqName */,
367 const hidl_string& /* name */,
368 const sp<IServiceNotification>& /* callback */) override {
369 // This makes no sense.
370 LOG(FATAL) << "Cannot register for notifications with passthrough service manager.";
371 return false;
372 }
373
Yifan Hong705e5da2017-03-02 16:59:39 -0800374 Return<void> debugDump(debugDump_cb _hidl_cb) override {
375 using Arch = ::android::hidl::base::V1_0::DebugInfo::Architecture;
Yifan Hongbd0d8f72017-05-24 19:43:51 -0700376 using std::literals::string_literals::operator""s;
Yifan Hong705e5da2017-03-02 16:59:39 -0800377 static std::vector<std::pair<Arch, std::vector<const char *>>> sAllPaths{
378 {Arch::IS_64BIT, {HAL_LIBRARY_PATH_ODM_64BIT,
379 HAL_LIBRARY_PATH_VENDOR_64BIT,
380 HAL_LIBRARY_PATH_SYSTEM_64BIT}},
381 {Arch::IS_32BIT, {HAL_LIBRARY_PATH_ODM_32BIT,
382 HAL_LIBRARY_PATH_VENDOR_32BIT,
383 HAL_LIBRARY_PATH_SYSTEM_32BIT}}
384 };
Yifan Hongbd0d8f72017-05-24 19:43:51 -0700385 std::map<std::string, InstanceDebugInfo> map;
Yifan Hong705e5da2017-03-02 16:59:39 -0800386 for (const auto &pair : sAllPaths) {
387 Arch arch = pair.first;
388 for (const auto &path : pair.second) {
389 std::vector<std::string> libs = search(path, "", ".so");
390 for (const std::string &lib : libs) {
391 std::string matchedName;
Yifan Hongbd0d8f72017-05-24 19:43:51 -0700392 std::string implName;
393 if (matchPackageName(lib, &matchedName, &implName)) {
394 std::string instanceName{"* ("s + path + ")"s};
395 if (!implName.empty()) instanceName += " ("s + implName + ")"s;
396 map.emplace(path + lib, InstanceDebugInfo{.interfaceName = matchedName,
397 .instanceName = instanceName,
398 .clientPids = {},
399 .arch = arch});
Yifan Hong705e5da2017-03-02 16:59:39 -0800400 }
401 }
402 }
403 }
Yifan Hongbd0d8f72017-05-24 19:43:51 -0700404 fetchPidsForPassthroughLibraries(&map);
405 hidl_vec<InstanceDebugInfo> vec;
406 vec.resize(map.size());
407 size_t idx = 0;
408 for (auto&& pair : map) {
409 vec[idx++] = std::move(pair.second);
410 }
Yifan Hong705e5da2017-03-02 16:59:39 -0800411 _hidl_cb(vec);
Yifan Hong7f49f592017-02-03 15:11:44 -0800412 return Void();
413 }
414
Martijn Coenenbf13ad02017-04-27 09:41:13 -0700415 Return<void> registerPassthroughClient(const hidl_string &, const hidl_string &) override {
Yifan Hong7f49f592017-02-03 15:11:44 -0800416 // This makes no sense.
417 LOG(FATAL) << "Cannot call registerPassthroughClient on passthrough service manager. "
418 << "Call it on defaultServiceManager() instead.";
Yifan Hong9a22d1d2017-01-25 14:19:26 -0800419 return Void();
420 }
421
Steven Moreland2a2678e2017-07-21 18:07:38 -0700422 Return<bool> unregisterForNotifications(const hidl_string& /* fqName */,
423 const hidl_string& /* name */,
424 const sp<IServiceNotification>& /* callback */) override {
425 // This makes no sense.
426 LOG(FATAL) << "Cannot unregister for notifications with passthrough service manager.";
427 return false;
428 }
429
Steven Moreland337e6b62017-01-18 17:25:13 -0800430};
431
Steven Moreland2a2678e2017-07-21 18:07:38 -0700432sp<IServiceManager1_0> getPassthroughServiceManager() {
433 return getPassthroughServiceManager1_1();
434}
435sp<IServiceManager1_1> getPassthroughServiceManager1_1() {
Steven Moreland337e6b62017-01-18 17:25:13 -0800436 static sp<PassthroughServiceManager> manager(new PassthroughServiceManager());
437 return manager;
438}
439
Steven Morelandcbefd352017-01-23 20:29:05 -0800440namespace details {
441
Steven Moreland519306f2017-06-06 17:14:12 -0700442void preloadPassthroughService(const std::string &descriptor) {
443 PassthroughServiceManager::openLibs(descriptor,
444 [&](void* /* handle */, const std::string& /* lib */, const std::string& /* sym */) {
445 // do nothing
446 return true; // open all libs
447 });
448}
449
Steven Morelandcbefd352017-01-23 20:29:05 -0800450struct Waiter : IServiceNotification {
451 Return<void> onRegistration(const hidl_string& /* fqName */,
452 const hidl_string& /* name */,
453 bool /* preexisting */) override {
454 std::unique_lock<std::mutex> lock(mMutex);
455 if (mRegistered) {
456 return Void();
457 }
458 mRegistered = true;
459 lock.unlock();
460
461 mCondition.notify_one();
462 return Void();
463 }
464
Steven Morelandf1e14f22017-03-28 09:33:06 -0700465 void wait(const std::string &interface, const std::string &instanceName) {
466 using std::literals::chrono_literals::operator""s;
467
Steven Morelandcbefd352017-01-23 20:29:05 -0800468 std::unique_lock<std::mutex> lock(mMutex);
Steven Morelandf1e14f22017-03-28 09:33:06 -0700469 while(true) {
470 mCondition.wait_for(lock, 1s, [this]{
471 return mRegistered;
472 });
473
474 if (mRegistered) {
475 break;
476 }
477
478 LOG(WARNING) << "Waited one second for "
479 << interface << "/" << instanceName
480 << ". Waiting another...";
481 }
Steven Morelandcbefd352017-01-23 20:29:05 -0800482 }
483
484private:
485 std::mutex mMutex;
486 std::condition_variable mCondition;
487 bool mRegistered = false;
488};
489
490void waitForHwService(
491 const std::string &interface, const std::string &instanceName) {
Steven Moreland2a2678e2017-07-21 18:07:38 -0700492 const sp<IServiceManager1_1> manager = defaultServiceManager1_1();
Steven Morelandcbefd352017-01-23 20:29:05 -0800493
494 if (manager == nullptr) {
495 LOG(ERROR) << "Could not get default service manager.";
496 return;
497 }
498
499 sp<Waiter> waiter = new Waiter();
500 Return<bool> ret = manager->registerForNotifications(interface, instanceName, waiter);
501
502 if (!ret.isOk()) {
503 LOG(ERROR) << "Transport error, " << ret.description()
504 << ", during notification registration for "
505 << interface << "/" << instanceName << ".";
506 return;
507 }
508
509 if (!ret) {
510 LOG(ERROR) << "Could not register for notifications for "
511 << interface << "/" << instanceName << ".";
512 return;
513 }
514
Steven Morelandf1e14f22017-03-28 09:33:06 -0700515 waiter->wait(interface, instanceName);
Steven Moreland2a2678e2017-07-21 18:07:38 -0700516
517 if (!manager->unregisterForNotifications(interface, instanceName, waiter).withDefault(false)) {
518 LOG(ERROR) << "Could not unregister service notification for "
519 << interface << "/" << instanceName << ".";
520 }
Steven Morelandcbefd352017-01-23 20:29:05 -0800521}
522
523}; // namespace details
524
Steven Moreland5d5ef7f2016-10-20 19:19:55 -0700525}; // namespace hardware
526}; // namespace android